该宏脚本使用的是 sourceinsight 4.00.0095, 逻辑上, 应该所有的4.0版本都能用, 但未测试
1. 涉及的功能有:
alt1 alt2切换标签 绑定的键位是: alt+1 alt+2 alt+3... alt+0
.h与.cpp切换 绑定的键位是: alt+F12
删除光标右边的所有的东西 绑定的键位是: ctl+k
删除光标左面的字符 绑定的键位是: ctl+backspace
删除光标右边的字符 绑定的键位是: ctl+delete
插入#ifndef *** #define **** #endif **** 绑定的键位是: ctl+shift+=
注释 绑定的键位是: alt+;
2. 具体的源码如下:
// 给所选择的行添加注释
// 使用行注释, 若已经存在的注释, 则取消注释
// 添加的注释与字符之间无空格
macro CommentBlock()
{
hbuf = GetCurrentBuf();
hwnd = GetCurrentWnd();
if (!hbuf || !hwnd) return 0
sel = GetWndSel(hwnd);
allcnt = GetBufLineCount(hbuf)
chTab = CharFromAscii(9)
chSpace = CharFromAscii(32)
chComment = CharFromAscii(47)
// 循环所有选择的行
iLine = sel.lnFirst;
while (iLine <= sel.lnLast)
{
newLine = "" //备用的新行
curLine = GetBufLine(hbuf, iLine);
col1 = 0
col2 = strlen(curLine)
has1 = 0
if (col2 > 0) {
while (col1 <= col2) {
ch = curLine[col1]
if (ch == chSpace || ch == chTab) {
newLine = newLine # ch
}
else {
if (ch == chComment && col1 < col2) {
tch = curLine[col1+1]
if (tch == chComment) {
remain = strmid(curLine, col1+2, col2)
newLine = newLine # remain
break
}
}
remain = strmid(curLine, col1, col2)
newLine = newLine # "//" # remain
break
}
col1++
}
// 把已经添加注释的行, 替换
PutBufLine(hbuf, iLine, newLine);
}
iLine = iLine + 1;
}
}
// alt + 1 switch tab, windows tab options中tab sorting使用Tab name
macro alt1() { setCurWindow(0)}
macro alt2() { setCurWindow(1)}
macro alt3() { setCurWindow(2)}
macro alt4() { setCurWindow(3)}
macro alt5() { setCurWindow(4)}
macro alt6() { setCurWindow(5)}
macro alt7() { setCurWindow(6)}
macro alt8() { setCurWindow(7)}
macro alt9() { setCurWindow(8)}
macro alt0() { setCurWindow(9)}
// 1. 创建一个buf(si中只能用buf来保存array)
// 2. 循环取所有的窗口的文件名, 按字母序插入创建的buf中
// 3. buf中保存的文件名格式是: "文件件"+","+"id", 其中id是该文件的窗口id
// 4. 根据alt的id取buf中保存的文件名, 取出窗口id, 切换到该窗口id
function setCurWindow(idx) {
// 创建buf, 并清空该buf(该buf不会删除)
nbuf = NewBuf("abcdefg")
ClearBuf (nbuf)
// 循环取每一个窗口文件并插入到buf中
cwnd = WndListCount()
iwnd = 0
while (iwnd < cwnd) {
hwnd = WndListItem(iwnd)
name = GetBufName (GetWndBuf (hwnd))
len = strlen(name)
pos = len
while (pos-- > 0) {
if (name[pos] == "\\" || name[pos] == "/") {
name = strmid(name, pos + 1, len)
break
}
}
name = name # "," # iwnd
num = GetBufLineCount (nbuf)
pos = 0
while (pos < num) {
cln = GetBufLine (nbuf, pos)
len1 = strlen(name) - 2
len2 = strlen(cln) - 2
if (compare_str(name, len1, cln, len2) < 0) {
InsBufLine (nbuf, pos, name)
break
}
pos ++
}
if (pos == num) {
AppendBufLine (nbuf, name)
}
iwnd = iwnd + 1
}
// 取idx中所对应的buf的文件名, 并设置为当前窗口
if (idx >= 0 && idx < GetBufLineCount (nbuf)) {
cln = GetBufLine (nbuf, idx)
tlen = strlen(cln)
idx_pos = strmid(cln, tlen-1, tlen)
my_cur_wnd = WndListItem(idx_pos)
SetCurrentWnd(my_cur_wnd)
}
CloseBuf (nbuf)
}
// 比较两个字符串, 按字符序
// 若顺序是: str1->str2返回-1 str2->str1返回 1
function compare_str(str1, len1, str2, len2) {
pos = 0
while (1) {
if (pos == len1 && pos == len2) return -1
else if (pos >= len1) return -1
else if (pos >= len2) return 1
t1 = AsciiFromChar(str1[pos])
t2 = AsciiFromChar(str2[pos])
if (t1 > t2) return 1
else if (t1 < t2) return -1
pos++
}
}
// 在.h或.hpp文件中插入 #ifndef *** #define **** #endif ****
macro add_def() {
hbuf = GetCurrentBuf()
if (hbuf == hNil) return 0
name = GetBufName(hbuf)
len = strlen(name)
if (len <= 3) return 0
pos = len
while (pos-- > 0) {
if (name[pos] == "\\" || name[pos] == "/") {
name = strmid(name, pos + 1, len)
break
}
}
len = strlen(name)
if (len < 3) return 0
if (name[len-1] == "h" && name[len-2] == ".") {
newname = "_" # strtrunc(name, len-2) # "_h_"
}
else if (name[len-1] == "p" && name[len-2] == "p" && name[len-3] == "h"&& name[len-4] == ".") {
newname = "_" # strtrunc(name, len-4) # "_h_"
}
else stop
newname = toupper(newname)
upadd2 = "#ifndef " # newname
upadd1 = "#define " # newname
downadd = "#endif /* "# newname # " */"
InsBufLine(hbuf, 0, upadd1)
InsBufLine(hbuf, 0, upadd2)
AppendBufLine(hbuf, "")
AppendBufLine(hbuf, downadd)
}
//删除光标右边的所有的东西, 若是空行, 则删除当前行;
//若删除的部分有内容, 则复制到clip
macro killRight()
{
hbuf = GetCurrentBuf()
hwnd = GetCurrentWnd()
sel = GetWndSel(hwnd)
allcnt = GetBufLineCount(hbuf)
if (allcnt <= 0) return;
hbufClip = GetBufHandle("Clipboard")
if (sel.fExtended ) return ;
// 循环所有选择的行
iLine = sel.lnFirst
curLine = GetBufLine(hbuf, iLine);
if (sel.ichFirst == 0) {
// 若在行首
DelBufLine(hbuf, iLine)
// 或不是空行, 则加入到clip
if (strlen(curLine) > 0) {
EmptyBuf(hbufClip)
AppendBufLine(hbufClip, curLine)
}
}
else {
// 只保留当前光标左边的,
len1 = strlen(curLine)
cacheLine = strmid(curLine, sel.ichFirst, len1)
if (strlen(cacheLine) > 0) {
//只有当有保留的字符时, 才会复制并放在缓存中, 并设置新行
newLine = strmid(curLine, 0, sel.ichFirst)
EmptyBuf(hbufClip)
AppendBufLine(hbufClip, cacheLine)
// msg("====" # cacheLine # "====")
PutBufLine(hbuf, iLine, newLine)
}
}
}
//删除光标左面的东西
// 若光标左边是空白, 则删除到第一个非空白字符或到行首
// 若光标左边是字符, 则删除到第一个空白字符或到行首
// 若光标左边是行首, 则删除到上一行, 并把光标右边的与上一行合并(即使右边是空白也会合并上去)
macro left_del() {
hbuf = GetCurrentBuf()
hwnd = GetCurrentWnd()
if (hwnd == hNil || hbuf == hNil) return 0
sel = GetWndSel(hwnd)
if (sel.fExtended ) return 0
// 循环所有选择的行
if (sel.ichFirst == 0) {
pos = sel.lnFirst
if (pos == 0) return 0
// 若在行首, 则删除当前行, 补充到上一行去; 若上一行为空, 则迭代到非空的行
curLine = GetBufLine(hbuf, pos)
preLine = ""
preLen = 0
while (pos > 0) {
//删除当前行
DelBufLine(hbuf, pos)
pos-- //pos指向上一行
preLine = getBufLine(hbuf, pos)
preLen = strlen(preLine)
if (preLen > 0) break
}
preLine = cat(preLine, curLine)
PutBufLine(hbuf, pos, preLine)
sel.lnFirst = pos
sel.lnLast = pos
sel.ichFirst = preLen
sel.ichLim = sel.ichFirst
SetWndSel(hwnd, sel)
}
else {
//若不在行首, 则向左删除到遇到的非字符
pos = sel.lnFirst
curLine = GetBufLine(hbuf, pos)
len1 = strlen(curLine)
if (len1 <= 0) return 0
idx = sel.ichFirst
tailLine = strmid(curLine, idx, len1)
preChar = -1
preSpace = -1
while (idx-- > 0) {
//退出的条件有两个:
//1.若光标旁边的字符串时, 然后遇到非字符串时; 或者相反的情况
//2.若遇到空格, 接下来不是空格时
ischar = 0
d = AsciiFromChar(curLine[idx])
if ((d >= 65 && d <= 90) || (d >= 97 && d <= 122)) ischar = 1
if (d == 32) if (preSpace < 0) preSpace = 1
if (preChar < 0) preChar = ischar
if ((preChar != ischar) || (preSpace > 0 && d != 32)) {
idx++
break
}
}
headLine = ""
if (idx > 0) headLine = strmid(curLine, 0, idx)
newLine = cat(headLine, tailLine)
PutBufLine(hbuf, pos, newLine)
if (idx > 0) sel.ichFirst = idx
else sel.ichFirst = 0
sel.ichLim = sel.ichFirst
SetWndSel(hwnd, sel)
}
}
//删除光标右边的东西, 逻辑同删除光标左边的东西
macro right_del() {
hbuf = GetCurrentBuf()
hwnd = GetCurrentWnd()
if (hwnd == hNil || hbuf == hNil) return 0
sel = GetWndSel(hwnd)
if (sel.fExtended ) return 0
pos = sel.lnFirst
curLine = GetBufLine(hbuf, pos)
curLen = strlen(curLine)
if (curLen == 0 || sel.ichFirst == curLen) {
//空行或若在行尾, 添加下一行到当前行; 若下一行为空, 则迭代到非空的行
nLine = GetBufLineCount(hbuf)
if (pos >= nLine - 1) return 0 //已经是最后一行, 则退出
idx = pos + 1
nextLine = ""
while (idx < nLine) {
//寻找有数据的行
nextLine = getBufLine(hbuf, idx)
idx++ //返回的idx永远指向下一行
if (strlen(nextLine) > 0) break
}
//
while (--idx > pos) DelBufLine(hbuf, idx)
newLine = cat(curLine, nextLine)
PutBufLine(hbuf, pos, newLine)
SetWndSel(hwnd, sel)
}
else {
//非空行且若非行尾, 则向右删除到遇到的非字符
idx = sel.ichFirst
headLine = strmid(curLine, 0, idx)
postChar = -1
postSpace = -1
while (idx < curLen) {
//退出的条件有两个:
//1.若光标旁边的字符串时, 然后遇到非字符串时; 或者相反的情况
//2.若遇到空格, 接下来不是空格时
ischar = 0
d = AsciiFromChar(curLine[idx])
if ((d >= 65 && d <= 90) || (d >= 97 && d <= 122)) ischar = 1
if (d == 32) if (postSpace < 0) postSpace = 1
if (postChar < 0) postChar = ischar
if ((postChar != ischar) || (postSpace > 0 && d != 32)) break
idx++
}
tailLine = ""
if (idx < curLen) tailLine = strmid(curLine, idx, curLen)
newLine = cat(headLine, tailLine)
PutBufLine(hbuf, pos, newLine)
SetWndSel(hwnd, sel)
}
}
// 跳转到当前文件的对应的.h或.cpp文件, 仅在同一个目录的
// .h.hpp=>.c.cpp
// .c.cpp=>.h.hpp
macro find_h_cpp() {
hbuf = GetCurrentBuf ()
if (!hbuf) stop
fname = GetBufName (hbuf)
nname = ""
nname_1 = ""
ext = ""
flen = strlen(fname)
pos = flen
while (pos > 0) {
pos = pos - 1
cha = fname[pos]
if (cha == ".") {
tname = strtrunc(fname, pos + 1)
text = strmid(fname, pos + 1, flen)
ext = toupper (text)
if (ext == "H" || ext == "HPP") {
nname = tname # "c"
nname_1 = tname # "cpp"
break
}
else if (ext == "C" || ext == "CPP") {
nname = tname # "h"
nname_1 = tname # "hpp"
break
}
else {
msg("not a valid file: " # fname)
Return
}
}
}
if (strlen(nname) > 0) {
hnbuf = OpenBuf(nname)
if (hnbuf) {
SetCurrentBuf (hnbuf)
return
}
}
if (strlen(nname_1) > 0) {
hnbuf = OpenBuf(nname_1)
if (hnbuf)
SetCurrentBuf (hnbuf)
else
msg("cannot find @nname@")
}
}