在word中使用notepad++实现代码的语法高亮
轉(zhuǎn)載自:http://blog.csdn.net/woohello/article/details/7621651
有時寫文檔時需要將代碼粘貼到word中,但直接粘貼到word中的代碼雖能保持換行與縮進等格式,但在一般代碼編輯工具中的關鍵字高亮功能卻無法實現(xiàn),即粘貼到word中的代碼不在具有豐富的色彩。使用一款免費軟件——notepad++即可實現(xiàn)將關鍵字高亮的代碼粘貼到word中。
?????? 首先用notepad++打開源代碼文件。notepad++能識別C/C++、Java、matlab等多種語言的源代碼。選中要粘貼的代碼(如果該代碼文件中的所有內(nèi)容均需要粘貼,則無需選中文字)。然后在選擇 插件->NppExport->Copy HTML to clipboard。
然后在word中粘貼即可。
此外,關鍵字的顏色也可以根據(jù)自己的需求在notepad++中進行設置,設置方法:菜單->格式->語言格式設置
--------------------------------
也可以參考侯捷《word排版藝術》中的vba腳本
由于是代碼,所以推薦中文使用宋體(注釋中),而英文使用等寬字體(courier new)。
-------------------------------------
最近我經(jīng)常在word?里面寫東西,發(fā)現(xiàn)程序代碼拷貝到word?里面就沒有了在代碼編輯器里面的那種語法高亮的效果,感覺不爽。于是我上網(wǎng)搜了搜,發(fā)現(xiàn)目前在word?中實現(xiàn)語法高亮的方法主要是通過安裝一個插件。由于我先天的對插件比較反感,所以自己動手,使用word?等office?軟件都支持的VBA (Visual BAsic?For?Application)?寫了一個語法高亮的宏。
這個宏的功能比較簡單,就是利用得到文檔中選中部分的代碼,然后分詞,判斷該詞的類別,然后著色。我現(xiàn)在使用的分詞方法是VBA?提供的,大部分情況下和我們預期的比較一致。但是在某些情況下,比如連續(xù)的分隔符,這種分詞方法會和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?=?0
????wordCount?=?Selection.Words.Count
????Selection.StartOf?wdWord
????While?d?<?wordCount
????????d?=?d?+?Selection.MoveRight(wdWord,?1,?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,?1
????????????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,?1,?wdExtend
????????????????Selection.MoveEndUntil?("*")
????????????????Selection.MoveRight?wdCharacter,?2,?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
????For?l?=?1?To?lines
????????lIneNum?=?l?&?"?"
????????If?l?<?10?Then
????????????lIneNum?=?lIneNum?&?"?"
????????End?If
????????Selection.Text?=?lIneNum
????????Selection.Font.Bold?=?False
????????Selection.Font.Color?=?wdColorAutomatic
????????p?=?Selection.MoveDown(wdLine,?1,?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)選定代碼文本,然后執(zhí)行highlight腳本: “格式” → “宏” → “宏”, 選擇SyntaxHighlight宏,然后執(zhí)行就可以了。
如果想定制語法高亮,那么修改上面的腳本就是了。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結
以上是生活随笔為你收集整理的在word中使用notepad++实现代码的语法高亮的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 应用中十大常见 UX 错
- 下一篇: GitHub项目协作基本步骤