我估计把这个 vimrc 研究一遍,VIM 就有小成了

This item was filled under [ 写程序工具 ]
“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"
” General
“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"
set nocompatible ” get out of horrible vi-compatible mode
filetype on ” detect the type of file
set history=1000 ” How many lines of history to remember
set cf ” enable error files and error jumping
set clipboard+=unnamed ” put yanks/etc on the clipboard
“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"
” Theme/Colors
“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"”" 阅读更多…
Bookmark and Share

15分钟轻松定制基于VIM的IDE

This item was filled under [ 写程序工具 ]

1 背景
VIM被人追捧为“无所不能”的文本编辑器,是很多Unix和Linux程序员的最爱。VIM最大的特点是扩展性极强,功能定制异常灵活。灵活性和复杂性之 间通常是矛盾关系,VIM复杂的定制参数和大量的命令也使得很多新手望而却步(我曾经也是其中之一)。本文不打算介绍详细介绍VIM的定制和扩展方法,而 是试图在最短的时间内、通过最简单的手段将VIM定制为一个适合程序开发的IDE。

本文介绍的定制是通过修改.vimrc文件完成,该文件位于用户的home目录下,如果不存在,创建一个。

2 语法高亮
Ubuntu7.10中VIM默认没有启用语法高亮,如果想在当前编辑窗口中使用,可以在命令模式下输入”syntax on”。但这样每次打开新文件都要重新设置,十分麻烦,所以,可以在.vimrc文件的末尾加上syntax on来默认启用语法高亮。

3 代码缩进
在.vimrc中加入
autocmd FileType *      set formatoptions=tcql nocindent comments&
autocmd FileType c,cpp,java,pl,sh,py set formatoptions=croql cindent comments=sr:/*,mb:*,ex:*/,://

意思是只对c,cpp,java,pl,sh,py格式的文件启动自动缩进。你也可以加入自己的文件后缀,例如js。除了对代码段进行缩进外,该设置也对 注释进行了自动缩进,例如,在注释行”/××”末尾换行,新行自动对齐到第一个×,并在行头自动加上×号。

4 自动排版
代码自动缩进的设置只对新编写的代码起作用,对于已有的代码怎么办呢?这就需要自动排版功能。
你可以在浏览模式下按1G=G进行自动排版。这对Eclipse的使用者来说很不习惯,因为在Eclipse中排版的快捷键是”ctrl + shift + f”,对于习惯使用Eclipse的程序员,可以在.vimrc中加入以下两行来设置新的快捷键:
nnoremap <c-s-f> 1G=G
inoremap <c-s-f> <ESC>1G=Gi

其中第一行直接将”ctrl + shift + f”映射为1G=G,即在浏览模式下使用快捷键”ctrl + shift + f”执行1G=G的功能。第二行使得在编辑模式下也可以使用”ctrl + shift + f”进行排版。 阅读更多…

Bookmark and Share

普通人的编辑利器——VIM (for windows)

This item was filled under [ 写程序工具 ]
FROM linxuelin
2005年5月,我开始用VIM。此后渐入佳境,原来因版权自律而放弃盗版UltraEdit的遗憾一扫而空。并且,从VIM我才体会到,什么才是真正的编辑利器。在
善用佳软或其他论坛发文,我都是先在VIM中输入,并方便高效地编辑好格式,再贴到网上。而我的通讯录、记事、读书笔记、工作

记 录也是在VIM中完成的。包括一些文档、数据的处理,让VIM过一下,也会提高不少效率。一句话,自用VIM以来,对它迷恋日深,几乎要超过 Total Commander。但是,正如善用佳软及论坛发文所提,我大力推荐“用IrfanView/XnView代替ACDSee、用7-zip代 替 WinRar/WinZip、用GIMP代替PhotoShop”,但并未推荐VIM代替UltraEdit。这并不意味着VIM不能胜任,恰恰相 反,如果说IrfanView等替代品比原共享软件还有些差距的话,VIM则远胜UltraEdit。不推荐的唯一原因是,VIM对初学者有点难。

但今天仍写了此文推荐给大家。原因有二:其一,编辑大人选定了题目,要向大众宣讲VIM和Emacs,这种勇气鼓舞了我。其二,已发两位高手的文章(

王垠之《Emacs是一种信仰!世界最强编辑器介绍》Dieken之《程序员的编辑器——VIM》

)可能会吓住一些对它们产生了兴趣的人,对此我要做些修正。
因此本文内容有二:一是帮新手体验VIM。这部分有些象实验教程,多用实例,且行文风格尽量傻瓜化,以便于新手(包括以前从未听说过VIM)可以照我的操作,一步步体验VIM。二是介绍我的应用,即VIM如何便利我的日常工作和生活,而非编程。
1 对VIM的基本介绍
一句话介绍就是:VIM是一个超级超级强大的文本编辑器。它和Emacs是一个数量级;而我认为,比UltraEdit、Editpad、 EmEditor要高一个(至少半个数量级)。如果你有兴趣,不妨一读《程序员的编辑器——VIM》,以便于对VIM有更详尽全面的理论认识。
要点1:VIM及前身VI,历史悠久(可能比多数读者的年龄更大),经历了几十年的考验和发展,值得信赖。
要点2:VIM有模式。通俗地做一下对比。notepad无模式(或称为只有一种编辑模式),你按下j,就是在正文中输入j;要保存,就要ctrl+ s;要下移一行,就要↓或鼠标。而在VIM中,打开一个文件后,并不处在编辑模式,而是Normal模式,此时你按j,VIM并不认为你要在正文中输入 j,而是代表光标下移一行。此时按i表示进入insert模式,即编辑模式,即notepad的模式。编辑模式按Esc退回normal模式。 normal模式下按:进入命令行模式,用于输入较复杂的命令。比如:w代表保存文件,:e a.txt代表打开文件a.txt。即VIM可以不用鼠 标、方向键、菜单、Ctrl、alt……发出命令。因此,以下的体验中切记进入正确的模式:Esc 到Normal;Normal下i到编辑; Normal下:到命令行。再说明一点是:文中:打头的是命令行模式,此外多数Normal模式。
要点3:复杂对抗复杂。VIM有的命令行很吓人,长长一串字符恍若天书。这些貌似复杂,实际上无非是一些简单命令的组合而已。借助于此,VIM用户可以实现非常复杂的需求。如果你没有这样的需求,就无须理会它们。
要点4:一能百能。这一点是Unix或命令行风格软件的显著长处,经过实用可以深刻体会。本文尽管有一些例子,但它运用之妙,存乎一心的感觉,仍未能 充分展示。这里先打个比方,如果windows下某软件号称能炒菜,那意味着你点一下菜单,菜就做好了——这就是易用哲学。到底怎么做,你是没有多少发言 权的,而是由软件开发者固化。而如果VIM说能,那它就是有一个“炒菜”命令,你可以把这个命令与原有的“原料”“数量”“火候”“风味”“灶具”命令结 合在一起用,甚至在它前面加上“买菜”“洗菜”,后面加上“装盘”“洗碗”做成一个自动脚本,在合适的情况下自动执行。

要点5:无限灵活,个性由我。你只要有具体需求,VIM几乎都能满足。因此不同人的VIM是不一样的,即,把

下载来的VIM配置成自己的VIM。

就此打住,开始体验之旅吧!

2 下载和安装
VIM是跨平台的。在Windows下,我们用的是gVIM for windows版。最新的是7.0版,下载包约8MB,完全安装后约20MB。
注:体积太大?你如果是高手,完全可以把不用的文件删除(包括帮助文件,它是我见过的软件中最好的帮助),精简后的体积可以不到2MB。或反之,仅下 载一个gVIM.exe(1.5MB)就能运行。但对于这次体验,鉴于第一次使用,建议全部按默认项安装,这样才能达到文中预想的结果。

PCOnline下载官方下载

安装,基本上,一直Agree、Next就行了。我通常是装在d:\program files\VIM下。
可参见







3 体验之旅
3.1 初见界面

安装完成后,按提示打开readme,你将看到

或自行打开桌面的快捷方式,也可直接运行d:\program files\VIM\VIM70\gVIM.exe,你应该看到如此界面:

什么?难看?土?我第一次见到它也是这种感觉,现在却只觉得亲切。

再一点,你有没有奇怪它是中文界面?难道我给你下载的是汉化版?会不会捆绑流氓插件啊?你如果真这样想,我只能叹息无语了。

3.2 试读html
请把此网页保存为htm,比如test.htm,然后用VIM打开它。暂时用菜单吧!结果可能是这样:

平平无奇,是吧?因为它是一个标准的VIM,并且是性能优先,视效忽略的VIM。无妨,请进入命令行模式(就是按一下冒号,注意是英文冒号:),注意到最下面一行的变化了吧。再输入sy on再回车。是不是有颜色了?它应该看起来象这个样子

重要提醒:为简化起见,下文将用
:sy on
表示(先确认在Normal模式,如不确认,请按几次Esc)你先按:进入命令行模式,再输入sy on,再回车。

补充说明:sy表示语法加亮,on当然是打开。:sy clear表示取消加亮。

3.3 改头换面
体验VIM就要按VIM的思路,所以我们要把gVIM改回VIM的本来面目。
对了,如果你认为它的界面很土,请更要多看一眼当前的样子,因为下面的操作后可能会更土!
执行如下命令
:set go=
这时的VIM应该是

不要大惊小怪,不就是菜单和工具栏不见了嘛?VIM中是不应该用到它们的。
补充说明:set表示一般性设置;go是gui option的缩写,就是界面;等于空就是什么都没有
再输入半条命令(先不要回车)
:colo ev
然后,按一下tab键。是不是自动补全为evening了?VIM是非常聪明的(但是不会象MS Word那样自做聪明)。
然后你回车,看到了什么?

天色全黑了?没错,现在是evening嘛。

补充说明:colo就是设置调色方案。VIM自带了近十种调色方案,当然你还可以在网上下载更多,或改编自己的调色方案。你可以直接在:colo (加一个空格)就连续tab,选择不同的配色方案来体验一下。当然,evening是比较经典的一种。我一直就用它。

3.4 准备并打开范例文件
简化起见,请

下载test.txt并保存为d:\test.txt。然后,在VIM中输入半条命令
:tabnew d:\t
输到这里后不要回车,按一下tab,VIM会自动补全为test.txt的。如果不对,再按几次tab,直到正确为止。然后回车。

你会看到VIM打开了test.txt,并且是以多页签(tab page)的方式。你可以点击各页签在打开的文件中间切换(高手有更方便的
键盘命令做法)。在VIM6.3中,还没有此功能。当然了,这并不意味着VIM 的用户没办法享受多页签的便利,下载一个插件后——VIM有上千个插件——就可以了。面对多页签风格的流行(IE6→Maxthon→IE7), VIM7.0也新增了此功能。
我喜欢这种简单的页签。如果你认为此页签太难看,可试一下:set go=e
这时应如下效果

3.5 统一格式,下载vimrc
上面所操作的各种命令,都是对VIM的设置。把它们写到一个_vimrc文件中就成了默认选项。每个VIM用户的_vimrc文件都不一样,因为每个人的需求不同。
为了保证下面的操作得到预期的效果,请下载我的_vimrc。依次执行下述操作:
① 关闭所有的VIM
② 找到你的d:\program files\vim\vim7\_vimrc,复制一份。

右键点击此处,“另存为”覆盖你的d:\program files\vim\vim7\_vimrc。

注:这并不是标准做法,但对windows用户是比较方便的做法。

3.6 打开test.txt
再次就模式进行提醒:Esc到Normal;Normal下i到编辑;Normal下:到命令行。
运行VIM,并在命令行执行
:e d:\test.txt
接下来,我们把此文章排一下版。如果你认为可以手工排版,请把此文件复制到10000行再发表意见。

3.7 分段并保存
原文没有分段,观察一下,发现可以在“以下是……”前面加回车,于是
:%s/以下是/\r&/gc
输入后,界面如下

并提示,请回答y或a。如果回答y,则替换当前一个;到了下一个,VIM还会再问你。所以,你回答几个y后不妨一a了之,全部替换。
补充说明:s表示替换;s/a/b 表示把a换为b;上一命令是把“以下是”换为“\r&”,\r表示回车,&表示被替换的部分,即“以下是”;%则表示全文;/gc表示一直替换(不是仅替换第一个),并且要用户确认。
看一下文件,你或许想,空2行可能更好。很简单,再重复一下刚才的命令就好了。不过,对强大的VIM来说,无须笨笨地重复输入上次的命令。在命令行按下:后,再按一下↑(向上的方向键),看到刚才的命令了吧?直接回车,提问时输入a,任务完成。
既有变化,请及时保存。命令是
:w
当然,你也可以按ctrl+s。但ctrl+s之所以有效,是因为我在_vimrc中做了定义:当按下ctrl+s时,系统执行:w动作。因此,你完全可以定义其他热键,并把它映射到:w。

3.8 继续编辑
首先删除“坚持”两字,很简单,只要按gg2x就行了。为了看效果,你可以分开按:
gg,这时光标移到文章头。
2x,这时删掉了2个字符。
再一次体验VIM的神奇,比如按5x,于是5个字符消失了。当然,这5个字符是要保留的,没关系,你按一次u,就回来了吧?
再按一下Ctrl+r,是不是又没了?再来一次u,是不是又回来了?没错。u和Ctrl+r相当于(是相当于不是等同于)windows的undo和redo。

再试一下dd。是不是整行没有了?没关系,u回来就行了。
再试一下3dd。一下子删除了3行!没关系,u回来。
再来一个dG,哇,全文都没了?!仍然u回来啊。

3.9 更多编辑
任务a:把每一句分为一行,即达到如下效果

以**为荣、
以**为耻,
解决:如果你记得上面的替换命令,就会想到,可以用2次替换(“为荣、”换为“为荣、”加回车,“为耻,”换为“为耻,”加回车)。但这两次替换在VIM中完全可以一次完成,即用正则表达式,在“为”及后面2字符(“荣、”或“耻,”)后加入回车。怎么表示呢?
:%s/为../&\r/gc
看一下高亮,没问题,直接a

哇,是不是很壮观啊?没觉得?如果是一千行一万行呢?

任务b:把行末的标点去掉,即“荣”后的顿号和“耻”后的逗号。
解决:办法太多了,仍可以用替换。但此处还是展示一个新功能:列操作。
安全起见,按两下Esc,再gg$,这时光标应到了文章第一行最后一个字符,即顿号上。
再按ctrl+v,注意一下状态栏是否提示有“可视 块” 字样。(请确认你是采用了xbeta的_vimrc文件,如果没有,请试一下ctrl+q)
再15jx,是否标点全部删除了?因为15j表示向下选中15行,x表示删除。

任务c:错落有致
操作:这次演示一下宏的用法,请严格按本教程操作。
先做一下确认:输入法没有打开,并Esc到normal状态,gg到文首。
按qa(q表示开始录制宏,宏的名字为a)
再shift+>>(表示按住shift,连按两下>,再放开shift。此时第一行应当向后tab了一下)
再j^(此时下移一行,并置光标于行首)
再shift+>>再shift+>>(即第二行向后tab两下)
再j^(下移一行,并将光标置于行首,为后续操作做准备)
再q(表示宏录制结束,VIM会提示记录完成)
这样宏录完了,可以用了。
再@a,看到了效果了吧?宏执行了一次,即3、4行成功了。还剩下6段,但是你不用按6次@a,而是只要按下
6@a就行了!!!

太神奇了!回放一下?先u,再来一次
6@a。既然可以6,当然可以千千万万!

补充说明:高手使用VIM是千变万化的。上面的前2个操作(分行+删标点),完全可以合成一个:%s/\(为.\)./\1\r/gc;或再把第3个操作(错落有致)融合到前一个操作中。

3.10 自由体验
体验光标移动:请随意按几下j和k,看看反应;再多按几下l和h,看看反应;然后,当你的光标在英文时,请按几下w和b。
对了,gVIM的每一种功能,都有无限可能,因此,你也可以在上面几个字母前加上数字,比如 9w 2h,看它的反应。

体验自动补全:i进来吧,到目前为止还没写过字呢!为简单起见,你直接把用鼠标放到中文的下一行,我们把 Eight Do’s and Eight Don’ts 输入一遍。但是,输入一个E,先别动!按ctrl+p是不是自动补全了?

体验大小写(每次操作后,都用u恢复原状),试一下g~~和guu和gUU和g??(g??后可以不按u,再按一次g??也能恢复原状)
神奇吗?这还是VIM功能的单个表演,真正的高手是把它们组合在一起,完成你自己独特的编辑需求。

3.11 体验语法高亮和折叠
此部分讲起来太复杂。如果你有其他编辑器如UltraEdit基础,会有助于你理解。但VIM在此方面的强大灵活,UltraEdit是难忘其项背的。
仍然是我们编辑的原文件,为简化计,请下载
test2.txt;下载tx1.vim并保存在d:\program files\vim\vimfiles\syntax\tx1.vim
用VIM打开(比如要打开d:\test2.txt,请在VIM中:tabnew d:\test2.txt)它。平平无奇是吗?这是因为VIM并不 知道这个文件要加亮。键入一个命令:setf tx1,这就相当于告诉VIM此文件的类型是tx1,所以就按照刚下载的tx1.vim进行高亮和折叠。
此时你会发现test2.txt完全折起来了。如何打开?你可用鼠标点最左边的+,当然,也可以用命令zr或zR打开(zR打开全部;zr打开当前;zM折叠全部;zm折叠当前)。打开后,看到高亮效果了吧。可以对文字设定前景色、背景色、粗、斜、下划线风格。

3.12 加密文件

文本文件打开最快,编辑起来最容易,并且在VIM中查找非常方便。因此,我用它来记日记,并作为通讯录。但是安全问题怎么办呢?很简单,VIM本身即 可加密(录然解密也要用VIM)。在上述打开的文件中键入:X(是大写X),VIM就提示你设定密码,安全起见要确认第2次。然后,保存(键入:w)。把 VIM或此文件关闭,再打开时,就要输入密码了。只有你输入正确的密码,才能得到正确的文件。

4 我的应用
VIM和Total Commander一样,我把它们都设定为开机自动运行,其应用不可胜数。
比如说通讯录管理,我未用任何软件,就用VIM管理一个address.txt。它可以实现:快速启动(我在VIM中定义了书签,键入`A就会打 开),加密(用VIM的:X),超强搜索(无须多说),分组折叠(fold),email和电话号的高亮显示(自定义语法文件)吧。
再比如,笔者会用VIM来辅助word, excel, bbs发文,安全删除文件,制作精简的五笔词库等。具体可参见 《
我常用的VIM功能》,《活学善用gVIM,提高工作效率:实例篇(合集)》,《用gVIM打造个性化记事本》等文章。
如下两图分别是我的个性化txt示例,和用VIM写此篇文章时的情况:

5 后记
这篇文章比最初计划写得长很多,但仍感觉很多非常优秀的功能没有提到。比如,眼下我写这篇文章,当然用VIM。但是因为有插图,所以用了htm格式。 在插入图片时,我只要输入img,VIM会自动换为“&lt;img src=”"&gt;”,并把光标放到两个引号中间,以便于我输入 图片名称。我输入lnk后,VIM会替换为“&lt;a href=”"&gt;&lt;/a&gt;”。当我想看一下 效果时,我只要:! g:\study\it\pconline\vim\xbeta-vim.htm 它就自动在浏览器中打开了。而这一个长长的路径, 因为有了自动补全和命令行历史,也非常容易。而当一些url还未确定,需要标注时,我就用一个`1,VIM就会以醒目的红色块标出来,一目了然。而下次再 打开此文件,它会记得我最后编辑的位置。
在用VIM之前,我曾试过几十种pad和editor,包括长期用Editpad,也曾经比较精通过UltraEdit,也曾经测过 EmEditor, EditPlus等。但用了VIM之后,我不再关心windows世界的成千上万的编辑器。因为使用它们的逻辑是“如果有新需求,只 能等软件升级或换一个软件”,而使用VIM的逻辑则是“如果有新需求,请做个性化配置或下载插件”。
VIM在手,夫复何求!
最后,向VIM所代表的追求卓越、开源共享的精神致敬!并希望有责任感的用户逐渐减少盗版!

————————————————————————————————————————————————————————

P.s:这篇文章的作者是善用佳软(xbeta)                      谢谢,calors的提醒
————————————————————————————————————————————————————————
Bookmark and Share
Tagged with: [ , ]

scite 快捷键。。。太好用了。。

This item was filled under [ 写程序工具 ]
Magnify text size. Ctrl+Keypad+
Reduce text size. Ctrl+Keypad-
Restore text size to normal. Ctrl+Keypad/
Cycle through recent files. Ctrl+Tab
Indent block. Tab
Dedent block. Shift+Tab
Delete to start of word. Ctrl+BackSpace
Delete to end of word. Ctrl+Delete
Delete to start of line. Ctrl+Shift+BackSpace
Delete to end of line. Ctrl+Shift+Delete
Go to start of document. Ctrl+Home
Extend selection to start of document. Ctrl+Shift+Home
Go to start of display line. Alt+Home
Extend selection to start of display line. Alt+Shift+Home
Go to end of document. Ctrl+End
Extend selection to end of document. Ctrl+Shift+End
Go to end of display line. Alt+End
Extend selection to end of display line. Alt+Shift+End
Expand or contract a fold point. Ctrl+Keypad*
Create or delete a bookmark. Ctrl+F2
Go to next bookmark. F2
Select to next bookmark. Alt+F2
Find selection. Ctrl+F3
Find selection backwards. Ctrl+Shift+F3
Scroll up. Ctrl+Up
Scroll down. Ctrl+Down
Line cut. Ctrl+L
Line copy. Ctrl+Shift+T
Line delete. Ctrl+Shift+L
Line transpose with previous. Ctrl+T
Selection duplicate. Ctrl+D
Find matching preprocessor conditional, skipping nested ones. Ctrl+K
Select to matching preprocessor conditional. Ctrl+Shift+K
Find matching preprocessor conditional backwards, skipping nested ones. Ctrl+J
Select to matching preprocessor conditional backwards. Ctrl+Shift+J
Previous paragraph. Shift extends selection. Ctrl+[
Next paragraph. Shift extends selection. Ctrl+]
Previous word. Shift extends selection. Ctrl+Left
Next word. Shift extends selection. Ctrl+Right
Previous word part. Shift extends selection Ctrl+/
Next word part. Shift extends selection. Ctrl+\

撤消(未保存的) ctrl+R

Bookmark and Share

sourceinsight使用技巧

This item was filled under [ 写程序工具 ]

1 sourceinsight screen font 的默认字体是Verdana的,它是一直变宽字体。在Document style中可以将字体改为定宽的Courier

2 勾掉indent Open Brace和Indent Close Brace的效果: 继上一段,在相对缩进行里, 如果输入”{“或”}”, 则自动和上一行列对齐

3 今天把一个用sourceinsight排版整齐的C文件,偶然用VC打开一看,全乱了。研究了半天,发现SI对每个字符的宽度不太一致。

发现选上”view –> draft view“, 就可以让每个字符的宽度一致了。快捷键是 “Alt + F12
4选中几行代码按tab键或者shift+tab可以左右移动代码,调整代码时很有用。
5使用最强大的宏功能,真的达到的完美境界

说明:
该宏文件实现一些编码程中能会到的功能, 如添加文件头、函数说明和宏定义等, 使用时能自动添加文件名、函数名和当前日期.

使用说明:
1. Project->Open Project… 打开Base工程(该工程一般在”我的文档\Source Insight\Projects\Base”中);
2. Project->Add and Remove Project Files… 加入宏文件(即mymacro.em);
3. Options->Menu Assignments 打开Menu Assignments窗口, 在Command中输入Macro, 选中要使用的宏, 添加到合适的菜单中.
http://www.sourceinsight.com/public/macros/可以找到很多宏定义文件,但大多数没什么用。
/*附上宏定义文件(一下是我精心挑选的十分好用的宏定义,不试不知道,一试真有用)*/
/* mymacro.em – a small collection of useful editing macros */
/******************************************************************************
* InsFunHeader — insert function’s information
*
* modification history
* ——————–
* 01a, 23mar2003, added DESCRIPTION by t357
* 01a, 05mar2003, t357 written
* ——————–
******************************************************************************/
macro InsFunHeader()
{
// Get the owner’s name from the environment variable: szMyName.
// If the variable doesn’t exist, then the owner field is skipped.

/*#########################################################
#########################################################
####### Set szMyName variable to your name ########
####### for example szMyName = “t357″ ########
#########################################################
#########################################################*/
szMyName = “LW”

// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
szFunc = GetCurSymbol()
ln = GetSymbolLine(szFunc)

// Get current time
szTime = GetSysTime(1)
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = “0@Day@”
else
szDay = Day
szMonth = NumToName(Month)
szInf = Ask(“Enter the information of function:”)
szDescription = Ask(“Enter the description of function:”)

// begin assembling the title string
sz = “/******************************************************************************”
InsBufLine(hbuf, ln, sz)
InsBufLine(hbuf, ln + 1, ” * @szFunc@ – @szInf@”)
InsBufLine(hbuf, ln + 2, ” * DESCRIPTION: – “)
InsBufLine(hbuf, ln + 3, ” * @szDescription@ “)
// remove by t357. CutWord(szDescription)
InsBufLine(hbuf, ln + 4, ” * Input: “)
InsBufLine(hbuf, ln + 5, ” * Output: “)
InsBufLine(hbuf, ln + 6, ” * Returns: “)
InsBufLine(hbuf, ln + 7, ” * “)
InsBufLine(hbuf, ln + 8, ” * modification history”)
InsBufLine(hbuf, ln + 9, ” * ——————–”)
InsBufLine(hbuf, ln + 10, ” * 01a, @szDay@@szMonth@@Year@, @szMyName@ written”)
InsBufLine(hbuf, ln + 11, ” * ——————–”)
InsBufLine(hbuf, ln + 12, ” ******************************************************************************/”)

// put the insertion point inside the header comment
SetBufIns(hbuf, ln + 1, strlen(szFunc) + strlen(szInf) + 8) }
/******************************************************************************
* NumToName — change the month number to name
*
* modification history
* ——————–
* 01a, 05mar2003, t357 written
* ——————–
******************************************************************************/
macro NumToName(Month)
{
if (Month == 1)
return “jan”
if (Month == 2)
return “feb”
if (Month == 3)
return “mar”
if (Month == 4)
return “apr”
if (Month == 5)
return “may”
if (Month == 6)
return “jun”
if (Month == 7)
return “jul”
if (Month == 8) return “aug”
if (Month == 9)
return “sep”
if (Month == 10)
return “oct”
if (Month == 11)
return “nov”
if (Month == 12)
return “dec”
}

/******************************************************************************
* CutWord — auto newline
*
* modification history
* ——————–
* 01a, 24mar2003, t357 fix some bug
* 01a, 05mar2003, t357 written
* ——————–
******************************************************************************/
macro CutWord(ncurLine, szInf)
{
LENGTH = 63
nlength = StrLen(szInf)
i = 0 /* loop control */
begin = 0 /* first character’s index of current line */
pre = 0 /* preceding word’s index */
hbuf = GetCurrentBuf()
// nline = GetBufLnCur()
while (i < nlength)
{
/* remove by t357
nrow = 0
sz = “”
while (nrow < 80)
{
if (nlength < 0)
break
sz = Cat(sz, szInf[nrow])
nrow = nrow + 1
nlength = nlength – 1
}
InsBufLine(hbuf, nline, sz)
szInf = szInf[nrow]
}
*/
c = szInf[i]
if (” ” == @c@ && (i – b < LENGTH))
{
pre = i
}
else if (” ” == @c@)
{
szOutput = “”
k = begin /* loop control */
while (k < pre)
{
szOutput = Cat(szOutput, szInf[k])
k = k + 1
}
InsBufLine(hbuf, ncurLine, sz)
ncurLine = ncurLine + 1
begin = pre
}
i = i + 1
}
if (h != i – 1)
{
szOutput = “”
k = begin /* loop control */
while (k < pre)
{
szOutput = Cat(szOutput, szInf[k])
k = k + 1
}
InsBufLine(hbuf, ncurLine, sz)
ncurLine = ncurLine + 1
}
}

// Wrap ifdeinef <sz> .. endif around the current selection
macro IfdefineSz(sz)
{
hwnd = GetCurrentWnd()
lnFirst = GetWndSelLnFirst(hwnd)
lnLast = GetWndSelLnLast(hwnd)

hbuf = GetCurrentBuf()
InsBufLine(hbuf, lnFirst, “#ifndef @sz@”)
InsBufLine(hbuf, lnFirst + 1, “#define @sz@”)
InsBufLine(hbuf, lnLast + 3, “#endif /* @sz@ */”)
SetBufIns(hbuf, lnFirst + 2, 0)
}
/* A U T O E X P A N D */
/*————————————————————————-
Automatically expands C statements like if, for, while, switch, etc..

To use this macro,
1. Add this file to your project or your Base project.

2. Run the Options->Key Assignments command and assign a
convenient keystroke to the “AutoExpand” command.

3. After typing a keyword, press the AutoExpand keystroke to have the
statement expanded. The expanded statement will contain a ### string
which represents a field where you are supposed to type more.

The ### string is also loaded in to the search pattern so you can
use “Search Forward” to select the next ### field.

For example:
1. you type “for” + AutoExpand key
2. this is inserted:
for (###; ###; ###)
{
###
}
3. and the first ### field is selected.
————————————————————————-*/
/******************************************************************************
* AutoExpand – Automatically expands C statements
*
* DESCRIPTION: – Automatically expands C statements like if, for, while,
* switch, etc..
*
* Input:
* Output:
* Returns:
*
* modification history
* ——————–
* 01a, 27mar2003, t357 modified
* ——————–
******************************************************************************/
macro AutoExpand()
{
// get window, sel, and buffer handles
hwnd = GetCurrentWnd()
if (hwnd == 0)
stop
sel = GetWndSel(hwnd)
if (sel.ichFirst == 0)
stop
hbuf = GetWndBuf(hwnd)

// get line the selection (insertion point) is on
szLine = GetBufLine(hbuf, sel.lnFirst);

// parse word just to the left of the insertion point
wordinfo = GetWordLeftOfIch(sel.ichFirst, szLine)
ln = sel.lnFirst;

chTab = CharFromAscii(9)

// prepare a new indented blank line to be inserted.
// keep white space on left and add a tab to indent.
// this preserves the indentation level.
ich = 0
while (szLine[ich] == ‘ ‘ || szLine[ich] == chTab)
{
ich = ich + 1
}

szLine = strmid(szLine, 0, ich)
sel.lnFirst = sel.lnLast
sel.ichFirst = wordinfo.ich
sel.ichLim = wordinfo.ich

// expand szWord keyword…
if (wordinfo.szWord == “if” ||
wordinfo.szWord == “while” ||
wordinfo.szWord == “elseif”)
{
SetBufSelText(hbuf, ” (###)”)
InsBufLine(hbuf, ln + 1, “@szLine@” # “{“);
InsBufLine(hbuf, ln + 2, “@szLine@” # chTab);
InsBufLine(hbuf, ln + 3, “@szLine@” # “}”);
}
else if (wordinfo.szWord == “for”)
{
SetBufSelText(hbuf, ” (###; ###; ###)”)
InsBufLine(hbuf, ln + 1, “@szLine@” # “{“);
InsBufLine(hbuf, ln + 2, “@szLine@” # chTab);
InsBufLine(hbuf, ln + 3, “@szLine@” # “}”);
}
else if (wordinfo.szWord == “switch”)
{
SetBufSelText(hbuf, ” (###)”)
InsBufLine(hbuf, ln + 1, “@szLine@” # “{“)
InsBufLine(hbuf, ln + 2, “@szLine@” # “case “)
InsBufLine(hbuf, ln + 3, “@szLine@” # chTab)
InsBufLine(hbuf, ln + 4, “@szLine@” # chTab # “break;”)
InsBufLine(hbuf, ln + 5, “@szLine@” # “default:”)
InsBufLine(hbuf, ln + 6, “@szLine@” # chTab)
InsBufLine(hbuf, ln + 7, “@szLine@” # “}”)
}
else if (wordinfo.szWord == “do”)
{
InsBufLine(hbuf, ln + 1, “@szLine@” # “{“)
InsBufLine(hbuf, ln + 2, “@szLine@” # chTab);
InsBufLine(hbuf, ln + 3, “@szLine@” # “} while ();”)
}
else if (wordinfo.szWord == “case”)
{
SetBufSelText(hbuf, ” ###”)
InsBufLine(hbuf, ln + 1, “@szLine@” # chTab)
InsBufLine(hbuf, ln + 2, “@szLine@” # chTab # “break;”)
}
else
stop

SetWndSel(hwnd, sel)
LoadSearchPattern(“###”, true, false, false);
Search_Forward
}
/* G E T W O R D L E F T O F I C H */
/*————————————————————————-
Given an index to a character (ich) and a string (sz),
return a “wordinfo” record variable that describes the
text word just to the left of the ich.

Output:
wordinfo.szWord = the word string
wordinfo.ich = the first ich of the word
wordinfo.ichLim = the limit ich of the word
————————————————————————-*/
macro GetWordLeftOfIch(ich, sz)
{
wordinfo = “” // create a “wordinfo” structure

chTab = CharFromAscii(9)

// scan backwords over white space, if any
ich = ich – 1;
if (ich >= 0)
while (sz[ich] == ” ” || sz[ich] == chTab)
{
ich = ich – 1;
if (ich < 0)
break;
}

// scan backwords to start of word
ichLim = ich + 1;
asciiA = AsciiFromChar(“A”)
asciiZ = AsciiFromChar(“Z”)
while (ich >= 0)
{
ch = toupper(sz[ich])
asciiCh = AsciiFromChar(ch)
if ((asciiCh < asciiA || asciiCh > asciiZ) && !IsNumber(ch))
break // stop at first non-identifier character
ich = ich – 1;
}

ich = ich + 1
wordinfo.szWord = strmid(sz, ich, ichLim)
wordinfo.ich = ich
wordinfo.ichLim = ichLim;

return wordinfo
}

//
// Comment the selected block of text using single line comments and indent it
//
macro CommentBlock()
{
hbuf = GetCurrentBuf();
hwnd = GetCurrentWnd();

sel = GetWndSel(hwnd);

iLine = sel.lnFirst;

while (iLine <= sel.lnLast)
{
szLine = GetBufLine(hbuf, iLine);
szLine = cat(“// “, szLine);
PutBufLine(hbuf, iLine, szLine);
iLine = iLine + 1;
}

if (sel.lnFirst == sel.lnLast)
{
tabSize = _tsGetTabSize() – 1;
sel.ichFirst = sel.ichFirst + tabSize;
sel.ichLim = sel.ichLim + tabSize;
}
SetWndSel(hwnd, sel);
}
//
// Undo the CommentBlock for the selected text.
//
macro UnCommentBlock()
{
hbuf = GetCurrentBuf();
hwnd = GetCurrentWnd();

sel = GetWndSel(hwnd);

iLine = sel.lnFirst;
tabSize = 0;
while (iLine <= sel.lnLast)
{
szLine = GetBufLine(hbuf, iLine);
len = strlen(szLine);
szNewLine = “”;
if (len > 1)
{
if (szLine[0] == “/” && szLine[1] == “/”)
{
if (len > 2)
{
if (AsciiFromChar(szLine[2]) == 9)
{
tabSize = _tsGetTabSize() – 1;
szNewLine = strmid(szLine, 3, strlen(szLine));
}
}

if (szNewLine == “”)
{
szNewLine = strmid(szLine, 2, strlen(szLine));
tabSize = 2;
}

PutBufLine(hbuf, iLine, szNewLine);
}
}
iLine = iLine + 1;
}

if (sel.lnFirst == sel.lnLast)
{
sel.ichFirst = sel.ichFirst – tabSize;
sel.ichLim = sel.ichLim – tabSize;
}

SetWndSel(hwnd, sel);
}

macro _tsGetTabSize()
{
szTabSize = GetReg(“TabSize”);

if (szTabSize != “”)
{
tabSize = AsciiFromChar(szTabSize[0]) – AsciiFromChar(“0″);
}
else
{
tabSize = 4;
}

return tabSize;
}
//
// Reformat a selected comment block to wrap text at 80 columns.
// The start of the selection (upper left most character of the selection) is
// handled specially, in that it specifies the left most column at which all
// lines will begin. For example, if the following block was selected starting
// at the @ symbol, through the last line of the block…
//——————————————————————————
// preamble: @ This is a line that will be wrapped keeping the “at” symbol in its current column.
// All lines following it that are selected will use that as their starting column. See below to see how the wrapping
// works for this block of text.
//——————————————————————————
// preamble: @ This is a line that will be wrapped keeping the “at” symbol in
// its current column. All lines following it that are selected
// will use that as their starting column. See below to see how
// the wrapping works for this block of text.
//
macro tsReformatCommentBlock()
{
hbuf = GetCurrentBuf();
hwnd = GetCurrentWnd();

sel = GetWndSel(hwnd);

tabSize = _tsGetTabSize();
leftTextCol = 0 – 1;
colWrap = 80;

// Find the starting column, and create a Margin string
ichFirst = sel.ichFirst;

// Single line comment reformat?
if (sel.ichFirst == sel.ichLim && sel.lnFirst == sel.lnLast)
{
ichFirst = 0;
}

rec = _tsGetStartColumn(hbuf, ichFirst, sel.lnFirst);

if (rec == “”)
stop;

colLeftMargin = rec.colMargin;
szMargin = “”;

colComment = 0;
if (rec.colComment >= 0)
{
colComment = rec.colComment + 2
szMargin = _tsAddWhiteToColumn(szMargin, 0, rec.colComment, tabSize);
szMargin = cat(szMargin, “//”);
}

szMargin = _tsAddWhiteToColumn(szMargin, colComment, rec.colMargin, tabSize);

rec = “”;

szCurMargin = “”;
if (ichFirst != 0)
{
szLine = GetBufLine(hbuf, sel.lnFirst);
szCurMargin = strmid(szLine, 0, ichFirst);
}
else
{
szCurMargin = szMargin;
szMargin = “”;
}

insertLine = sel.lnFirst;
iLine = sel.lnFirst;
szRemainder = “”;
while (1)
{
// msg(“$0-” # iLine # “:” # szRemainder);
rec = _tsGetNextCommentString(hbuf, ichFirst, szRemainder, iLine, sel.lnLast, colWrap);
ichFirst = 0;

if (rec == “”)
break;

// msg(“$1-” # rec.ln # “:” # rec.szComment);
szLine = rec.szComment;

ich = 0;
col = colLeftMargin;
len = strlen(szLine);

ichPrevCharToWhite = 0-1;
ichPrevWhiteToChar = 0-1;
// msg(“Leftovers @szRemainder@”);

while (ich < len)
{
if (AsciiFromChar(szLine[ich]) == 9)
{
col = (((col + tabSize) / tabSize) * tabSize);
}
else
{
col = col + 1;
}

if (col > colWrap)
break;

fIsWhitespace = _tsIsWhitespaceChar(szLine[ich]);
fIsWhitespace1 = 1;

if (ich + 1 < len)
{
fIsWhitespace1 = _tsIsWhitespaceChar(szLine[ich + 1]);
}

if (!fIsWhitespace && fIsWhitespace1)
ichPrevCharToWhite = ich;

ich = ich + 1;
}

if (ichPrevCharToWhite > 0)
{
// msg(“$2:” # strmid(szLine, 0, ichPrevCharToWhite + 1));
ich = ichPrevCharToWhite + 1;

while (ich < len)
{
if (!_tsIsWhitespaceChar(szLine[ich]))
{
ichPrevWhiteToChar = ich – 1;
// msg(“$3:” # strmid(szLine, ichPrevWhiteToChar + 1, len));
break;
}
ich = ich + 1;
}
}

if (ichPrevCharToWhite > 0 && col > colWrap)
{
szNewLine = cat(szCurMargin, strmid(szLine, 0, ichPrevCharToWhite + 1));
szRemainder = “”;
if (ichPrevWhiteToChar > 0)
szRemainder = strmid(szLine, ichPrevWhiteToChar + 1, len);

if (ichPrevCharToWhite > ichPrevWhiteToChar)
msg(“!!!Wrap, duplicating word ” # ichPrevWhiteToChar # ” ” # ichPrevCharToWhite # ” ” # szNewLine # ” >>> ” # szRemainder);
// msg(szLine);
// msg(col # ” ” # ichPrevWhiteToChar # ” ” # ichPrevCharToWhite # ” ” # szNewLine # ” >>> ” # szRemainder);
}
else if (szLine != “”)
{
szNewLine = cat(szCurMargin, szLine );
szRemainder = “”;
// sel.lnLast = sel.lnLast + 1;
}

iLine = rec.ln;
if (insertLine == iLine)
{
iLine = iLine + 1;
sel.lnLast = sel.lnLast + 1;

// msg(“$5-” # insertLine # “:” # szNewLine);
InsBufLine(hbuf, insertLine, szNewLine);
}
else
{
szLine = GetBufLine(hbuf, insertLine);
if (szLine != szNewLine)
{
// msg(“$6-” # insertLine # “:” # szNewLine);
PutBufLine(hbuf, insertLine, szNewLine);
}
}
insertLine = insertLine + 1;

if (szMargin != “”)
{
szCurMargin = szMargin;
szMargin = “”;
}
}

while (insertLine <= sel.lnLast)
{
DelBufLine(hbuf, insertLine);
sel.lnLast = sel.lnLast – 1;
}

len = GetBufLineLength(hbuf, insertLine-1);

sel.ichFirst = len;
sel.ichLim = len;
sel.lnFirst = sel.lnLast;
SetWndSel(hwnd, sel);
}
macro _tsAddWhiteToColumn(sz, col0, col, tabSize)
{
szTabs = ” “;
szSpaces = ” “;

tabs0 = col0 / tabSize;
tabs = (col / tabSize) – tabs0;

if (tabs == 0)
foo = col0;
else
foo = (tabs + tabs0) * tabSize;
spaces = col – foo;
// msg(col0 # ” ” # col # ” ” # tabs # ” ” # spaces # ” ” # tabs0);

if (tabs)
sz = cat(sz, strmid(szTabs, 0, tabs));

if (spaces)
sz = cat(sz, strmid(szSpaces, 0, spaces));

return sz;
}

macro _tsGetStartColumn(hbuf, ichBegin, ln)
{
szLine = GetBufLine(hbuf, ln);
len = strlen(szLine);
tabSize = _tsGetTabSize();
ich = 0;

colMargin = 0;
colComment = 0-1;

rec = “”;
rec.colMargin = colMargin;
rec.colComment = colComment;

while (ich < len)
{
if (AsciiFromChar(szLine[ich]) == 9)
{
colMargin = (((colMargin + tabSize) / tabSize) * tabSize);
}
else
{
colMargin = colMargin + 1;
}

if (colComment < 0)
{
if (ich + 1 < len)
{
if (szLine[ich] == “/” && szLine[ich+1] == “/”)
{
colComment = colMargin – 1;
ich = ich + 2;
colMargin = colMargin + 1;
continue;
}
}
}

if (ich >= ichBegin)
{
if (!_tsIsWhitespaceChar(szLine[ich]))
{
rec.colMargin = colMargin – 1;
rec.colComment = colComment;
// msg(szLine[ich]);
return rec;
}
}

ich = ich + 1;
}

return rec;
}

macro _tsGetNextCommentString(hbuf, ichSkip, szRemainder, ln, lnLast, colWrap)
{
rec = “”;

// Go until we get a string that is at least long enough to fill a line
// or, we run out of lines.
if (szRemainder == “” && ln > lnLast)
return “”;

ichFirst = ichSkip;
// msg(ichSkip);
while (1)
{
if (ln > lnLast)
{
rec.szComment = szRemainder;
rec.ln = ln;
return rec;
}

cchRemainder = strlen(szRemainder);

if (cchRemainder > colWrap)
{
rec.szComment = szRemainder;
rec.ln = ln;
return rec;
}

szLine = GetBufLine(hbuf, ln);
len = strlen(szLine);

if (ichSkip == 0)
ichFirst = _tsSkipPastCommentAndWhitespace(szLine, len);
ichSkip = 0;

ichLast = len – 1;

// Now, strip out all whitespace at the end of the line
while (ichLast >= ichFirst)
{
if (!_tsIsWhitespaceChar(szLine[ichLast]))
{
break;
}
ichLast = ichLast – 1;
}

// Entire line is whitespace?
if (ichLast < ichFirst)
{
if (szRemainder == “”)
ln = ln + 1;
rec.szComment = szRemainder;
rec.ln = ln;
return rec;

}
// length of the non whitespaced comment + 1 space + cchRemainder
if ((ichLast + 1) – ichFirst + cchRemainder + 1 > 255)
{
// It may not format the current line quite right, but
// but at least we won’t throw away some of the comment.
rec.szComment = szRemainder;
rec.ln = ln;
return rec;
}

if (szRemainder != “”)
szRemainder = cat(szRemainder, ” “);
szRemainder = cat(szRemainder, strmid(szLine, ichFirst, ichLast + 1));
ln = ln + 1;
}
}

macro _tsSkipPastCommentAndWhitespace(szLine, len)
{
ichFirst = 0;
// Skip past the comment initiator “//” if there is one.
while (ichFirst < len)
{
if (ichFirst + 1 < len)
{
if (szLine[ichFirst] == “/” && szLine[ichFirst+1] == “/”)
{
ichFirst = ichFirst + 2;
break;
}
}
ichFirst = ichFirst + 1;
}

// If no comment found in line, then start from the beginning
if (ichFirst >= len)
ichFirst = 0;

ichFirst = ichFirst;
// Now, strip out all whitespace after the comment start.
while (ichFirst < len)
{
if (!_tsIsWhitespaceChar(szLine[ichFirst]))
{
break;
}
ichFirst = ichFirst + 1;
}

return ichFirst;
}

Bookmark and Share