/*
* utils.em
*
* Created on: March 13, 2015
* Author: [email protected]
*/
//szMyName = getenv(MYNAME)
/*
* 1. GetCurrentBuf(): 获取当前输入的句柄
* 2. GetCurSymbol(): 获取当前光标后的符号
* 3. GetBufLnCur(): 获取当前光标所在的行号
* 4. hwnd=GetCurrentWnd(), sel=GetWndSel(hwnd), col=sel.ichFirst+1 : 获取列号
* 5. InsBufLine(hbuf, ln, sz): 在ln行插入sz的字符内容
* 6. SetBufIns(hbuf, ln, n): 将光标转移到第ln行的第n在字符
* 7. sz = cat(sz, "abc"): 将abc添加到sz后面
*/
/******************************************************************************/
macro InsertHeader()
{
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)
if (ln < 0)
{
ln = 0
}
sz = "/******************************************************************************/"
InsBufLine(hbuf, ln, sz)
SetBufIns(hbuf, ln+1, 0)
}
/******************************************************************************/
macro GetCurrentFileName(pathName)
{
name = ""
nlength = strlen(pathName)
i = nlength - 1
while (i + 1)
{
ch = pathName[i]
if ("\\" == "@ch@")
break
i = i - 1
}
i = i + 1
while (i < nlength)
{
name = cat(name, pathName[i])
i = i + 1
}
return name
}
macro MonthNumToName(Month)
{
if (Month == 1)
return "January"
if (Month == 2)
return "February"
if (Month == 3)
return "March"
if (Month == 4)
return "April"
if (Month == 5)
return "May"
if (Month == 6)
return "June"
if (Month == 7)
return "July"
if (Month == 8)
return "August"
if (Month == 9)
return "September"
if (Month == 10)
return "October"
if (Month == 11)
return "November"
if (Month == 12)
return "December"
}
macro GetCurrentTimeAndDate()
{
szTimeDate = ""
szTime = GetSysTime(1)
Hour = szTime.Hour
if (Hour < 10)
Hour = "0@Hour@"
Minute = szTime.Minute
if (Minute < 10)
Minute = "0@Minute@"
Second = szTime.Second
if (Second < 10)
Second = "0@Second@"
Year = szTime.Year
Month = szTime.Month
Day = szTime.Day
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
szMonth = MonthNumToName(Month)
/*
if (Month < 10)
szMonth = "0@Month@"
else
szMonth = Month
*/
szTimeDate = "@szMonth@ @szDay@, @Year@ @Hour@:@Minute@:@Second@"
return szTimeDate
}
macro GetMyEmailAddress()
{
szMyEmail = "xtank.nie\@gmail.com"
return szMyEmail
}
/******************************************************************************/
macro InsertFileHeader()
{
ln = 0;
hbuf = GetCurrentBuf()
InsBufLine(hbuf, ln, "/*")
szPathName = GetBufName(hBuf)
szCurrentFileName = GetCurrentFileName(szPathName)
sz = " * @szCurrentFileName@"
ln = ln + 1
InsBufLine(hbuf, ln, sz)
ln = ln + 1
InsBufLine(hbuf, ln, " *")
szTimeDate = GetCurrentTimeAndDate()
sz = " * Created on: @szTimeDate@"
ln = ln + 1
InsBufLine(hbuf, ln, sz)
szEmail = GetMyEmailAddress()
sz = " * Author: @szEmail@"
ln = ln + 1
InsBufLine(hbuf, ln, sz)
ln = ln + 1
InsBufLine(hbuf, ln, " */")
InsBufLine(hbuf, ln + 1, "")
SetBufIns(hbuf, ln+2, 0)
}
macro IfdefSz(sz)
{
hwnd = GetCurrentWnd()
lnFirst = GetWndSelLnFirst(hwnd)
lnLast = GetWndSelLnLast(hwnd)
hbuf = GetCurrentBuf()
InsBufLine(hbuf, lnFirst, "#ifdef @sz@")
InsBufLine(hbuf, lnLast+2, "#endif /* @sz@ */")
}
macro InsertCPlusPlus()
{
//IfdefSz("__cplusplus");
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)
ln_bak = ln
InsBufLine(hbuf, ln, "#ifdef __cplusplus")
ln = ln + 1
InsBufLine(hbuf, ln, "extern \"C\" {")
ln = ln + 1
InsBufLine(hbuf, ln, "#endif /* __cplusplus */")
ln = ln + 1
InsBufLine(hbuf, ln, "")
ln = ln + 1
InsBufLine(hbuf, ln, "#ifdef __cplusplus")
ln = ln + 1
InsBufLine(hbuf, ln, "}")
ln = ln + 1
InsBufLine(hbuf, ln, "#endif /* __cplusplus */")
SetBufIns(hbuf, ln_bak+3, 0)
}
macro isUpperLetter(letter)
{
if (letter == "A"
|| letter == "B"
|| letter == "C"
|| letter == "D"
|| letter == "E"
|| letter == "F"
|| letter == "G"
|| letter == "H"
|| letter == "I"
|| letter == "J"
|| letter == "K"
|| letter == "L"
|| letter == "M"
|| letter == "N"
|| letter == "O"
|| letter == "P"
|| letter == "Q"
|| letter == "R"
|| letter == "S"
|| letter == "T"
|| letter == "U"
|| letter == "V"
|| letter == "W"
|| letter == "X"
|| letter == "Y"
|| letter == "Z")
return 1
else
return 0
}
macro GetHeadFileDefine()
{
szHeadFileNameDefine = ""
hbuf = GetCurrentBuf()
szPathName = GetBufName(hBuf)
szCurrentFileName = GetCurrentFileName(szPathName)
szCurrentFileName = toupper(szCurrentFileName)
nlength = strlen(szCurrentFileName)
szHeadFileNameDefine = cat(szHeadFileNameDefine, "__")
i = 0
while (i < nlength)
{
ch = szCurrentFileName[i]
if (isUpperLetter("@ch@") == 1)
szHeadFileNameDefine = cat(szHeadFileNameDefine, "@ch@")
else
szHeadFileNameDefine = cat(szHeadFileNameDefine, "_")
i = i + 1
}
szHeadFileNameDefine = cat(szHeadFileNameDefine, "__")
return szHeadFileNameDefine
}
macro InsertDefineHeadName()
{
//IfdefSz("__cplusplus");
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)
ln_bak = ln
szHeadFileDefineName = GetHeadFileDefine()
InsBufLine(hbuf, ln, "#ifndef @szHeadFileDefineName@")
ln = ln + 1
InsBufLine(hbuf, ln, "#define @szHeadFileDefineName@")
ln = ln + 1
InsBufLine(hbuf, ln, "")
ln = ln + 1
InsBufLine(hbuf, ln, "#endif /* @szHeadFileDefineName@ */")
SetBufIns(hbuf, ln_bak+2, 0)
}
/******************************************************************************/
/******************************************************************************/
// 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);
}
// 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
}