// open the corresponding .c or .h file
macro OpenCorrespondingCorHfile()
{
//返回当前缓冲区的句柄
hbuf = GetCurrentBuf ()
//返回与文件缓冲区关联的文件的名称。
bname = GetBufName (hbuf)
//返回字符串的长度
len = strlen (bname)
//返回从(len-2)到(但不包括len)范围内的bname的中间字符串
if (".c" == strmid (bname, len-2, len))
{
filename = strmid (bname, 0, len-2)#".h"
}
else if (".h" == strmid (bname, len-2, len))
{
filename = strmid (bname, 0, len-2)#".c"
}
else
{
filename = Nil
}
if (filename != Nil)
{
//将名为filename的文件打开到文件缓冲区中,并返回文件缓冲区的句柄。
hbufnext = OpenBuf (filename)
if (hbufnext != hNil)
{
SetCurrentBuf (hbufnext)
}
}
}
//获取当前时间,格式是yyyy-mm-dd-hh:mm:ss
macro GetCurrentTime()
{
time = GetSysTime(1)
str = time.year;
//monht
if(time.month<10)
str = cat("@str@-0",time.month)
else
str = cat("@str@-",time.month)
//day
if(time.day<10)
str = cat("@str@-0",time.day)
else
str = cat("@str@-",time.day)
//hour
if(time.hour<10)
str = cat("@str@-0",time.hour)
else
str = cat("@str@-",time.hour)
//minute
if(time.hour<10)
str = cat("@str@:0",time.minute)
else
str = cat("@str@:",time.minute)
//second
if(time.second<10)
str = cat("@str@:0",time.second)
else
str = cat("@str@:",time.second)
//Msg(str)
return str
}
// 搜索子串,有找到返回所在下标,没有返回-1
macro strstr ( szS1, szS2 )
{
hbuf = NewBuf ( next )
lnS1 = strlen ( szS1 )
lnS2 = strlen ( szS2 )
i = 0
j = -1
AppendBufLine ( hbuf, j )
while ( i < lnS2 )
{
if ( j == -1 )
{
i = i + 1
j = j + 1
AppendBufLine ( hbuf, j )
}
else if ( szS2[i] == szS2[j] )
{
i = i + 1
j = j + 1
//PutBufLine (hbuf, i, GetBufLine ( hbuf, j ) )
AppendBufLine ( hbuf, j )
}
else
{
j = GetBufLine ( hbuf, j )
}
}
i = 0
j = 0
while ( i < lnS1 && j < lnS2 )
{
if ( j == -1 )
{
i = i + 1
j = j + 1
}
else if ( szS1[i] == szS2[j] )
{
i = i + 1
j = j + 1
}
else
{
j = GetBufLine ( hbuf, j )
}
}
CloseBuf ( hbuf )
if ( j == lnS2 )
{
rec = i - lnS2 // 有找到
}
else
{
rec = -1 //没找到
}
return rec
}
// This function trims white spaces from the ends of the selected lines
// in the current file buffer. If the selection is empty, it does the
// whole file.
macro TrimSpaces()
{
hbuf = GetCurrentBuf()
hwnd = GetCurrentWnd()
sel = GetWndSel(hwnd)
if (sel.fExtended)
{
// use selected lines
ln = sel.lnFirst
lnLim = sel.lnLast + 1
}
else
{
// process the whole file buffer
ln = 0
lnLim = GetBufLineCount(hbuf)
}
// do for each line....
while (ln < lnLim)
{
s = GetBufLine(hbuf, ln)
sTrim = StrTrimSpaces(s)
if (s != sTrim)
PutBufLine(hbuf, ln, sTrim)
ln = ln + 1
}
}
// Helper function: trims white space from the string s.
// Returns resulting string.
macro StrTrimSpaces(s)
{
cch = strlen(s)
ich = cch - 1
chTab = CharFromAscii(9)
while (ich >= 0)
{
ch = s[ich]
if (ch != " " && ch != chTab)
return strmid(s, 0, ich + 1)
ich = ich - 1
}
return ""
}
/* 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.
-------------------------------------------------------------------------*/
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) # chTab
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@" # "###");
InsBufLine(hbuf, ln + 3, "@szLine@" # "}");
}
else if (wordinfo.szWord == "for")
{
SetBufSelText(hbuf, " (###; ###; ###)")
InsBufLine(hbuf, ln + 1, "@szLine@" # "{");
InsBufLine(hbuf, ln + 2, "@szLine@" # "###");
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@" # "}")
}
else if (wordinfo.szWord == "do")
{
InsBufLine(hbuf, ln + 1, "@szLine@" # "{")
InsBufLine(hbuf, ln + 2, "@szLine@" # "###");
InsBufLine(hbuf, ln + 3, "@szLine@" # "} while (###);")
}
else if (wordinfo.szWord == "case")
{
SetBufSelText(hbuf, " ###:")
InsBufLine(hbuf, ln + 1, "@szLine@" # "###")
InsBufLine(hbuf, ln + 2, "@szLine@" # "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
}
macro GetWordLeftOfIch ( ich, sz )
{
wordinfo = "";
chTab = CharFromAscii ( 9 );
asciiA = AsciiFromChar ( "A" );
asciiZ = AsciiFromChar ( "Z" );
ich = ich - 1;
//光标前面有空格,直接退出
if ( sz[ich] == " " || sz[ich] == chTab )
{
stop;
}
ichLim = ich + 1;
while ( ich >= 0 )
{
ch = toupper ( sz[ich] );
asciiCh = AsciiFromChar ( ch );
if ( ( asciiCh < asciiA || asciiCh > asciiZ ) && !IsNumber ( ch ) && ch != "{" && ch != "#" )
{
break;
}
ich = ich - 1;
}
ich = ich + 1;
wordinfo.szWord = strmid ( sz, ich, ichLim );
wordinfo.ich = ich;
wordinfo.ichLim = ichLim;
return wordinfo;
}
/*
This is an example macro that can be used to create a
secondary key mapping which maps keys after an initial
key press.
For example, the user presses Ctrl+K, followed by "d"
to run the "Cut Line" command.
This example assumed Ctrl+K, but you can use whatever key
combination you want.
Instructions:
1. Add this macro to a .EM file and add it to
your project.
2. Use the Key Assignments command to map this macro
to Ctrl+K. The macro name "CtrlK" will show up in the list
of commands.
3. Now you can type Ctrl+K, followed by another key
to invoke either "Cut Line" or "Paste Line". These
two commands were picked by random. You could add
what ever key combinations and commands you want.
*/
macro CtrlK()
{
// Wait for the next key press and return the key code.
key = GetKey()
// Map the key code into a simple character.
//
// If you only need a simple character, you can
// call GetChar() instead of GetKey + CharFromKey
ch = CharFromKey(key)
ch = ToUpper(ch)
if (ch == "D")
{
// Ctrl+K, followed by Ctrl+D
if (IsCtrlKeyDown(key))
// run the "Paste Line" command
Paste_Line
// Ctrl+K, followed by "D"
else
// run the "Cut Line" command
Cut_Line
// Note: you can also use IsAltKeyDown and
// IsFuncKey to further discriminate keys.
}
}