在word中使用notepad++实现代码的语法高亮
转载自:http://blog.csdn.net/woohello/article/details/7621651
有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩进等格式,但在一般代码编辑工具中的关键字高亮功能却无法实现,即粘贴到word中的代码不在具有丰富的色彩。使用一款免费软件——notepad++即可实现将关键字高亮的代码粘贴到word中。
首先用notepad++打开源代码文件。notepad++能识别C/C++、Java、matlab等多种语言的源代码。选中要粘贴的代码(如果该代码文件中的所有内容均需要粘贴,则无需选中文字)。然后在选择 插件->NppExport->Copy HTML to clipboard。
然后在word中粘贴即可。
此外,关键字的颜色也可以根据自己的需求在notepad++中进行设置,设置方法:菜单->格式->语言格式设置
--------------------------------
也可以参考侯捷《word排版艺术》中的vba脚本
由于是代码,所以推荐中文使用宋体(注释中),而英文使用等宽字体(courier new)。
-------------------------------------
最近我经常在word 里面写东西,发现程序代码拷贝到word 里面就没有了在代码编辑器里面的那种语法高亮的效果,感觉不爽。于是我上网搜了搜,发现目前在word 中实现语法高亮的方法主要是通过安装一个插件。由于我先天的对插件比较反感,所以自己动手,使用word 等office 软件都支持的VBA
(Visual BAsic For Application) 写了一个语法高亮的宏。
这个宏的功能比较简单,就是利用得到文档中选中部分的代码,然后分词,判断该词的类别,然后着色。我现在使用的分词方法是VBA 提供的,大部分情况下和我们预期的比较一致。但是在某些情况下,比如连续的分隔符,这种分词方法会和C 语言分析器的分词结果不同的。
这个宏除了可以语法着色,还可以为代码标注行号。(听说侯捷在《word 排版艺术》一书中也有一个为代码添加行号的宏。不知道他的宏和我的宏是否雷同。如有雷同,纯属巧合:)
'script to high light code In document
Private Function isKeyword(w) As Boolean
Dim keys As New Collection
With keys
.Add " if": .Add "else": .Add "switch": .Add "case": .Add "default": .Add "break"
.Add "goto": .Add "return": .Add "for": .Add "while": .Add "do": .Add "continue"
.Add "typedef": .Add "sizeof": .Add "NULL": .Add "new": .Add "delete": .Add "throw"
.Add "try": .Add "catch": .Add "namespace": .Add "operator": .Add "this": .Add "const_cast"
.Add "static_cast": .Add "dynamic_cast": .Add "reinterpret_cast": .Add "true"
.Add "false": .Add "null": .Add "using": .Add "typeid": .Add "and": .Add "and_eq"
.Add "bitand": .Add "bitor": .Add "compl": .Add "not": .Add "not_eq": .Add "or"
.Add "or_eq": .Add "xor": .Add "xor_eq"
End With
isKeyword = isSpecial(w, keys)
End Function
Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean
For Each i In col
If w = i Then
isSpecial = True
Exit Function
End If
Next
isspeical = False
End Function
Private Function isOperator(w) As Boolean
Dim ops As New Collection
With ops
.Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";"
.Add "%": .Add "#": .Add "!": .Add ":": .Add ",": .Add "."
.Add "||": .Add "&&": .Add "|": .Add "=": .Add "++": .Add "--"
.Add "'": .Add """"
End With
isOperator = isSpecial(w, ops)
End Function
Private Function isType(ByVal w As String) As Boolean
Dim types As New Collection
With types
.Add "void": .Add "struct": .Add "union": .Add "enum": .Add "char": .Add "short": .Add "int"
.Add "long": .Add "double": .Add "float": .Add "signed": .Add "unsigned": .Add "const": .Add "static"
.Add "extern": .Add "auto": .Add "register": .Add "volatile": .Add "bool": .Add "class": .Add " private"
.Add "protected": .Add "public": .Add "friend": .Add "inlIne": .Add "template": .Add "virtual"
.Add "asm": .Add "explicit": .Add "typename"
End With
isType = isSpecial(w, types)
End Function
Sub SyntaxHighlight()
Dim wordCount As Integer
Dim d As Integer
' set the style of selection
Selection.Style = "ccode"

d 
wordCount = Selection.Words.Count
Selection.StartOf wdWord
While d < wordCount
d , wdExtend)
w = Selection.Text
If isKeyword(Trim(w)) = True Then
Selection.Font.Color = wdColorBlue
ElseIf isType(Trim(w)) = True Then
Selection.Font.Color = wdColorDarkRed
Selection.Font.Bold = True
ElseIf isOperator(Trim(w)) = True Then
Selection.Font.Color = wdColorBrown
ElseIf Trim(w) = "//" Then
'lIne comment
Selection.MoveEnd wdLine, 
commentWords = Selection.Words.Count
d = d + commentWords
Selection.Font.Color = wdColorGreen
Selection.MoveStart wdWord, commentWords
ElseIf Trim(w) = "/*" Then
'block comment
While Selection.Characters.Last <> "/"
Selection.MoveLeft wdCharacter, , wdExtend
Selection.MoveEndUntil ("*")
Selection.MoveRight wdCharacter, , wdExtend
Wend
commentWords = Selection.Words.Count
d = d + commentWords
Selection.Font.Color = wdColorGreen
Selection.MoveStart wdWord, commentWords
End If
'move the start of selection to next word
Selection.MoveStart wdWord
Wend
' prepare For set lIne number
Selection.MoveLeft wdWord, wordCount, wdExtend
SetLIneNumber
End Sub
Private Sub SetLIneNumber()
Dim lines As Integer
lines = Selection.Paragraphs.Count
Selection.StartOf wdParagraph
To lines
lIneNum = l & " "
Then
lIneNum = lIneNum & " "
End If
Selection.Text = lIneNum
Selection.Font.Bold = False
Selection.Font.Color = wdColorAutomatic
p , wdMove)
Selection.StartOf wdLine
Next l
End Sub下面是我给出的使用说明,原文没给出使用说明。
使用方法:
1) 首先为当前文档新定义一个样式,命名为"ccode",专门用来对c代码进行格式化。由于是代码,所以推荐中文使用宋体(注释中),而英文使用等宽字体(courier new)。建立样式的步骤:在word2003中,“格式” → “新样式”
2)将上面的vba代码拷贝到文档中,步骤:在word2003中,“工具” → “宏” → ”VB编辑器“ → ”Normal工程“ → ”Microsoft Word 对象“ ,双击 ”thisDocument"对象,将上面的代码拷贝到新开的窗口中。
当然你也可以把ccode样式和highlight脚本保存到normal模板中,这样以后你再写代码的时候就可以直接用了,不用自己再辛苦定义这些了。
3)选定代码文本,然后执行highlight脚本: “格式” → “宏” → “宏”, 选择SyntaxHighlight宏,然后执行就可以了。
如果想定制语法高亮,那么修改上面的脚本就是了。
在word中使用notepad++实现代码的语法高亮的更多相关文章
- 在word中使用notepad++实现代码的语法高亮 分类: C_OHTERS 2013-09-22 10:38 2273人阅读 评论(0) 收藏
转载自:http://blog.csdn.net/woohello/article/details/7621651 有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩 ...
- 在word中显示漂亮的代码
在word中粘贴或写代码时,通常得不到想要的格式,可用‘Notepad++’工具实现. 步骤: (1)安装Notepad++软件,把代码粘贴进去,选择菜单栏中的语言,然后选择相应代码语言,如P-> ...
- 如何在Word中排出漂亮的代码
引言 学数学和计算机,当然还是用LaTeX排版技术文章更方便.但有时候还是迫不得已需要用Word写作,另外Word其实也有Word的好处,比如细节上的修改要比LaTeX方便. 从Matlab高亮代码复 ...
- 用 highlight.js 为文章中的代码添加语法高亮
来源:http://www.ghostchina.com/adding-syntax-highlighting-to-ghost-using-highlight-js/ --------------- ...
- LaTeX中Python代码的语法高亮
LaTeX中Python代码的语法高亮 本文中,"{}"中的字母为LaTeX或Python的包名,只有"Pygments"是Python的包,其他都是LaTeX ...
- MWeb 1.5 发布!增加打字机滚动模式、发布到 Evernote、印象笔记、Wordpress.com、Blogger、编辑器内代码块语法高亮
打字机滚动模式(Typewriter Scrolling) 快捷键:CMD + Option + T,菜单:View - Typewriter Scrolling ,效果如下图: 发布到 Everno ...
- SecureCRT中 secureCRT使用VIM时对语法高亮
1.在SecureCRT中 secureCRT使用VIM时对语法高亮 其实不是secureCRT的功能,而是VIM的 设置:Options ->Session Options -> Ter ...
- 在 Microsoft Word 文档 中粘贴代码实现语法高亮的方法
1.下载notepad++. 2.将代码粘贴进notepad++,或者直接用notepad++打开. 3.点击顶栏 ===> 插件 ===> NppExport ===> cope ...
- Word中的代码怎样语法高亮
在平常我们粘贴代码到Word中的时候,经常会遇到代码粘贴到Word中后没有语法高亮,看着很乱很不友好,Word自带的样式---语法使用着也不尽人意, 网上有很多做法可以使得在插入在Word中的代码能够 ...
随机推荐
- django web 中添加超链接
django web 中添加不传参的超链接的方法如下: html: 在web中的超链接中加入一下url <a href="{% url 'app_name.views.url_func ...
- js prototype之诡异
想必经常写js的人必然会经常性的用到prototype这个属性,我写这篇文章倒不是自己对prototype这个属性理解有多深刻,相反是因为自己理解肤浅,想通过写文章来加深理解.废话不多说.下面总结一下 ...
- ext4 grid edit 添加删除行
extjs--grid动态添加一行和删除一行: (1)选择rowEditing时,添加一行后的编辑方式为----startEdit(record,columnHeader) (2)选择cellEdit ...
- 利用java反射机制对方法进行调用
http://blog.csdn.net/coolcoffee168/article/details/5835143
- [GitPython]使用python管理你的git库
你想使用python来自动管理一个git仓库的时候,GitPython是一个不错的选择. 简介: 先看看下面这段代码: from git import Repo repo = Repo("~ ...
- pyqt5模块
- 163k地方门户网站系统js小Bug
163k地方门户网站系统后台在Firefox下无法通过选择区域来获取相对应的地段,IE浏览器下无些问题. Firebug提示错误为: ReferenceError: requestdataquyu i ...
- 设置Ubuntu Mysql可以远程链接
1:修改my.cnf配置文件 $sudo vim /etc/mysql/my.cnf 修改为: bind-address = 0.0.0.0 2:进行授权操作 mysql> grant all ...
- Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现
Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现 暂时还未有时间开发这效果,所以先贴出来. 先贴一张效果图,这是一张手机截屏: 左上方的风景图:背景图片 右上方的人物图:前景图 ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...