部分解决Source Insight中文输入法打开时输入的乱码问题和自动添加注释

时间:2021-08-09 03:12:12

  最近在结合书本看Linux内核源码,很多需要加注释的地方,需要大写或添加注释时,总是需要切换输入法,总是要进行同样的重复输入,实在是被折磨的不行了。进行一番搜索之后,网上有很多方法也试了都不行,要么是不太理想,但是发现Source Insight可以自己定义扩展宏,而且在官网上有添加宏时用到的函数和添加方式的说明(其实在source insight的帮助中就可以看到这些东西,刚开始不知道),最终决定自己来写扩展宏。经过两天的努力,终于基本实现了预期的目标,虽然问题没有完全解决,但是已经基本满足我的需求了。

1        要解决的问题

Ø  解决中文输入法打开时,输入大写字母、括号、和下划线时产生乱码。添加扩展宏后,可以使用alt+相应的键即可实现以上输入,并且不会产生乱码。

Ø  自动添加注释,并且在输入一行完成后可以自动添加”*”

2        设置生效步骤

第一步:

  将utils.em文件直接拷贝到“我的文档”下Source Insight\Projects\Base目录中,或者在Source Insight中ProjectàOpen ProjectàBase,打开“Base”工程,然后打开工程中utils.em文件,将文件内容替换,然后重启Source Insight。

第二步:

  打开SourceInsight,OptionsàKey Assignments,然后搜索下面列表中的项,找到某项后点击“Assign New Key”即可设置相应的快捷键。快捷键栏就是要设置的快捷键,列表如下:

快捷键

描述

macro:AltA

alt+a

输入A

macro:AltB

alt+b

输入B

macro:AltC

alt+c

输入C

macro:AltD

alt+d

输入D

macro:AltE

alt+e

输入E

macro:AltF

alt+f

输入F

macro:AltG

alt+g

输入G

macro:AltH

alt+h

输入H

macro:AltI

alt+i

输入I

macro:AltJ

alt+j

输入J

macro:AltK

alt+k

输入K

macro:AltL

alt+l

输入L

macro:AltM

alt+m

输入M

macro:AltN

alt+n

输入N

macro:AltO

alt+o

输入O

macro:AltP

alt+p

输入P

macro:AltQ

alt+q

输入Q

macro:AltR

alt+r

输入R

macro:AltS

alt+s

输入S

macro:AltT

alt+t

输入T

macro:AltU

alt+u

输入U

macro:AltV

alt+v

输入V

macro:AltW

ctrl+w(不知道为什么不能设置成

alt+w)

输入W

macro:AltX

alt+x

输入X

macro:AltY

alt+y

输入Y

macro:AltZ

alt+z

输入Z

Macro:AltUnderline

alt+-

输入_

Macro:AltLeftBracket

alt+9

输入(

Macro:AltRightBracket

alt+0

输入)

Macro: InsertAsterisk_SLASH

F5

插入:

/*

 *

 */

Macro: EnterKeyDown

Enter

在”/*”开始、

“*/”结尾的注释中自动添加”*”

注:上面的快捷键的定义只是我个人的设定,可以根据个人需要设置。某些设置可能会屏蔽掉默认的快捷键

3        utils.em文件内容

 不知道如何添加文件链接,所以把文件内容粘贴出来,如果不知道怎么设置,可以给我留言,我会尽力帮忙,内容如下:

/* Utils.em - a small collection of useful editing macros */

/*-------------------------------------------------------------------------
I N S E R T H E A D E R

Inserts a comment header block at the top of the current function.
This actually works on any type of symbol, not just functions.

To use this, define an environment variable "MYNAME" and set it
to your email name. eg. set MYNAME=raygr
-------------------------------------------------------------------------*/
macro InsertHeader()
{
// Get the owner's name from the environment variable: MYNAME.
// If the variable doesn't exist, then the owner field is skipped.
szMyName = getenv(MYNAME)

// 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)

// begin assembling the title string
sz = "/* "

/* convert symbol name to T E X T L I K E T H I S */
cch = strlen(szFunc)
ich = 0
while (ich < cch)
{
ch = szFunc[ich]
if (ich > 0)
if (isupper(ch))
sz = cat(sz, " ")
else
sz = cat(sz, " ")
sz = Cat(sz, toupper(ch))
ich = ich + 1
}

sz = Cat(sz, " */")
InsBufLine(hbuf, ln, sz)
InsBufLine(hbuf, ln+1, "/*-------------------------------------------------------------------------")

/* if owner variable exists, insert Owner: name */
if (strlen(szMyName) > 0)
{
InsBufLine(hbuf, ln+2, " Owner: @szMyName@")
InsBufLine(hbuf, ln+3, " ")
ln = ln + 4
}
else
ln = ln + 2

InsBufLine(hbuf, ln, " ") // provide an indent already
InsBufLine(hbuf, ln+1, "-------------------------------------------------------------------------*/")

// put the insertion point inside the header comment
SetBufIns(hbuf, ln, 4)
}


/* InsertFileHeader:

Inserts a comment header block at the top of the current function.
This actually works on any type of symbol, not just functions.

To use this, define an environment variable "MYNAME" and set it
to your email name. eg. set MYNAME=raygr
*/

macro InsertFileHeader()
{
szMyName = getenv(MYNAME)

hbuf = GetCurrentBuf()

InsBufLine(hbuf, 0, "/*-------------------------------------------------------------------------")

/* if owner variable exists, insert Owner: name */
InsBufLine(hbuf, 1, " ")
if (strlen(szMyName) > 0)
{
sz = " Owner: @szMyName@"
InsBufLine(hbuf, 2, " ")
InsBufLine(hbuf, 3, sz)
ln = 4
}
else
ln = 2

InsBufLine(hbuf, ln, "-------------------------------------------------------------------------*/")
}



// Inserts "Returns True .. or False..." at the current line
macro ReturnTrueOrFalse()
{
hbuf = GetCurrentBuf()
ln = GetBufLineCur(hbuf)

InsBufLine(hbuf, ln, " Returns True if successful or False if errors.")
}



/* Inserts ifdef REVIEW around the selection */
macro IfdefReview()
{
IfdefSz("REVIEW");
}


/* Inserts ifdef BOGUS around the selection */
macro IfdefBogus()
{
IfdefSz("BOGUS");
}


/* Inserts ifdef NEVER around the selection */
macro IfdefNever()
{
IfdefSz("NEVER");
}


// Ask user for ifdef condition and wrap it around current
// selection.
macro InsertIfdef()
{
sz = Ask("Enter ifdef condition:")
if (sz != "")
IfdefSz(sz);
}

macro InsertCPlusPlus()
{
IfdefSz("__cplusplus");
}


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

hbuf = GetCurrentBuf()
InsBufLine(hbuf, lnFirst, "#ifdef @sz@")
InsBufLine(hbuf, lnLast+2, "#endif /* @sz@ */")
}


// Delete the current line and appends it to the clipboard buffer
macro KillLine()
{
hbufCur = GetCurrentBuf();
lnCur = GetBufLnCur(hbufCur)
hbufClip = GetBufHandle("Clipboard")
AppendBufLine(hbufClip, GetBufLine(hbufCur, lnCur))
DelBufLine(hbufCur, lnCur)
}


// Paste lines killed with KillLine (clipboard is emptied)
macro PasteKillLine()
{
Paste
EmptyBuf(GetBufHandle("Clipboard"))
}



// delete all lines in the buffer
macro EmptyBuf(hbuf)
{
lnMax = GetBufLineCount(hbuf)
while (lnMax > 0)
{
DelBufLine(hbuf, 0)
lnMax = lnMax - 1
}
}


// Ask the user for a symbol name, then jump to its declaration
macro JumpAnywhere()
{
symbol = Ask("What declaration would you like to see?")
JumpToSymbolDef(symbol)
}


// list all siblings of a user specified symbol
// A sibling is any other symbol declared in the same file.
macro OutputSiblingSymbols()
{
symbol = Ask("What symbol would you like to list siblings for?")
hbuf = ListAllSiblings(symbol)
SetCurrentBuf(hbuf)
}


// Given a symbol name, open the file its declared in and
// create a new output buffer listing all of the symbols declared
// in that file. Returns the new buffer handle.
macro ListAllSiblings(symbol)
{
loc = GetSymbolLocation(symbol)
if (loc == "")
{
msg ("@symbol@ not found.")
stop
}

hbufOutput = NewBuf("Results")

hbuf = OpenBuf(loc.file)
if (hbuf == 0)
{
msg ("Can't open file.")
stop
}

isymMax = GetBufSymCount(hbuf)
isym = 0;
while (isym < isymMax)
{
AppendBufLine(hbufOutput, GetBufSymName(hbuf, isym))
isym = isym + 1
}

CloseBuf(hbuf)

return hbufOutput

}

macro SingleLineComment()
{
hbuf = GetCurrentBuf()
hwnd = GetCurrentWnd();
lnFirst = GetWndSelLnFirst(hwnd)
lnLast = GetWndSelLnLast(hwnd)
while(lnFirst < lnLast+1)
{
tmpBuf = GetBufLine(hbuf,lnFirst)
if(tmpBuf != Nil)
{
commentBuf = cat("//",tmpBuf)
DelBufLine(hbuf,lnFirst)
InsBufLine(hbuf,lnFirst,commentBuf)
}
lnFirst = lnFirst + 1
}

}
macro strstr(src,dst)
{
dstLen = strlen(dst)
srcLen = strlen(src)
loop = 0
index = 0
while(loop < dstLen)
{
if(dst[loop] == src[loop])
{
index = index +1
loop = loop + 1
}
else
{
index = index + 1
loop = 0
if((index + dstLen) > srcLen)
{
return invalid
}
}
}
index = index - dstLen
return index
}
macro SingleLine_UnComment()
{
hbuf = GetCurrentBuf()
hwnd = GetCurrentWnd();
lnFirst = GetWndSelLnFirst(hwnd)
lnLast = GetWndSelLnLast(hwnd)

while(lnFirst < lnLast+1)
{
tmpBuf = GetBufLine(hbuf,lnFirst)
if(tmpBuf != Nil)
{
index = strstr(tmpBuf,"//")
if(index != invalid)
{
index = index + 2
unCommentBuf = strmid(tmpBuf,index,strlen(tmpBuf))
DelBufLine(hbuf,lnFirst)
InsBufLine(hbuf,lnFirst,unCommentBuf)
}
}
lnFirst = lnFirst + 1
}
}

macro InsertAsterisk_SLASH()
{
hwnd = GetCurrentWnd()
lnFirst = GetWndSelLnFirst(hwnd)
lnLast = GetWndSelLnLast(hwnd)

hbuf = GetCurrentBuf()
InsBufLine(hbuf, lnFirst, " /*")
InsBufLine(hbuf, lnLast+1, " * ")
InsBufLine(hbuf, lnLast+2, " */")
DelBufLine(hbuf,lnLast+3)
Cursor_Right
Cursor_Right
Cursor_Down
Cursor_Right
End_of_Line
}

macro InsertAsterisk()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "*")
}


macro AltA()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "A")
}

macro AltB()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "B")
}

macro AltC()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "C")
}

macro AltD()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "D")
}

macro AltE()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "E")
}

macro AltF()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "F")
}

macro AltG()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "G")
}

macro AltH()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "H")
}

macro AltI()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "I")
}

macro AltJ()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "J")
}

macro AltK()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "K")
}

macro AltL()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "L")
}

macro AltM()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "M")
}

macro AltN()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "N")
}

macro AltO()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "O")
}

macro AltP()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "P")
}

macro AltQ()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "Q")
}

macro AltR()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "R")
}

macro AltS()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "S")
}

macro AltT()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "T")
}

macro AltU()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "U")
}

macro AltV()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "V")
}

macro AltW()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "W")
}

macro AltX()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "X")
}

macro AltY()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "Y")
}

macro AltZ()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "Z")
}

macro AltUnderline()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "_")
}

macro AltLeftBracket()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, "(")
}

macro AltRightBracket()
{
hbuf = GetCurrentBuf()
if (hbuf != 0)
SetBufSelText(hbuf, ")")
}

macro is_valid_comment(src)
{
srcLen = strlen(src)
tmp = 0
index = 0
ch_ascii = 0

while (index < srcLen)
{
ch_ascii = AsciiFromChar(src[index])
// 42 is the ASCII value of '*' in source insight
// 47 is the ASCII value of '/' in source insight
if (ch_ascii == 42)
{
tmp = index + 1
if (tmp < srcLen)
{
ch_ascii = AsciiFromChar(src[tmp])
if (ch_ascii == 47)
{
return invalid
}
}
return index
}
else if (ch_ascii == 47)
{
tmp = index + 1
if (tmp < srcLen)
{
ch_ascii = AsciiFromChar(src[tmp])
if (ch_ascii == 42)
{
return tmp
}
}
else
{
return invalid
}
}
else if (ch_ascii != 32)
{
return invalid
}
index = index + 1
}
return invalid
}

macro EnterKeyDown()
{
hwnd = GetCurrentWnd()
lnFirst = GetWndSelLnFirst(hwnd)
lnLast = GetWndSelLnLast(hwnd)

hbuf = GetCurrentBuf()
tmpBuf = GetBufLine(hbuf,lnFirst)

index = 0
tmp = 1
ins_buf = " "
if (tmpBuf != Nil)
{
index = is_valid_comment(tmpBuf)
if (index != invalid)
{
if (index == 0)
{
InsBufLine(hbuf, lnLast+1, "*")
}
else
{
while (tmp < index)
{
ins_buf = cat(ins_buf, " ")
tmp = tmp + 1
}
ins_buf = cat(ins_buf, "*")
InsBufLine(hbuf, lnLast+1, ins_buf)
}
}
else
{
Insert_New_Line
}
}
else
{
Insert_New_Line
}
Cursor_Down
End_of_Line
}