
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"
}

macro PrintDate()
{
    szTime = GetSysTime(1)
    Day = szTime.Day
    Month = szTime.Month
    Year = szTime.Year
    if (Day < 10)
        szDay = "0@Day@"
    else
        szDay = Day
    szMonth = NumToName(Month)
    hbuf = GetCurrentBuf()
    SetBufSelText(hbuf, "@szMonth@ @szDay@, @Year@")
}

macro ReviewFunction()
{ 
	filename = "Review.txt"

	hbuf = OpenBuf (filename)
	if(hbuf==hNil )
	hbuf = NewBuf (filename)

	hcurbuf = GetCurrentBuf ()
	strcurname = GetBufName (hcurbuf)//当前监视文件的名称
	dwline = GetBufLnCur (hcurbuf)
	dwline = cat("第",dwline)
	dwline = cat(dwline,"行")
	strReview = Ask("输入监视意见")
	AppendBufLine (hbuf, strcurname)
	AppendBufLine (hbuf, dwline)
	AppendBufLine (hbuf, strReview)
	SaveBuf (hbuf)
	//AddFileToProj(GetCurrentProj (), filename)
	// "localx" is displayed with "Ref to Local" style 
}

macro AddFunctionComment()
{ 
    hcurbuf = GetCurrentBuf ()
    strcurname = GetBufName (hcurbuf)//当前监视文件的名称
    isymMax = GetBufSymCount (hcurbuf)
    isym = 0
    dwline = GetBufLnCur (hcurbuf)
    symname = GetCurSymbol ()
    
    InsBufLine (hcurbuf, dwline,   "/****************************************************************************")
    InsBufLine (hcurbuf, dwline+1, "****************************************************************************/")

	strName = cat(" Name:	 ", symname)
    InsBufLine (hcurbuf, dwline+1, strName)

    strReview = Ask(symname#"函数功能描述")
    strReview = cat(" Desc:   ",strReview)
    InsBufLine (hcurbuf, dwline+2, strReview)

    InsBufLine (hcurbuf, dwline+3, " Param:  ")

    dwline = dwline+4
    symbol = GetSymbolLocation(symname)
    if (symbol == "")
    {
        Msg (symname # " was not found")
        return
    }

    hsyml = SymbolChildren(symbol)
    cchild = SymListCount(hsyml)
    ichild = 0
    flag = 0
    strbuf=" *"
    while (ichild < cchild)
    {
        childsym = SymListItem(hsyml, ichild)
        if(childsym.Type =="Parameter")
        {
            //SymbolLeafName (childsym.Symbol)
            s =strmid (childsym.Symbol, strlen(symname)+1, strlen (childsym.Symbol))
            strReview = Ask(s#" : 参数含义")
            strReview =cat(" -> ",strReview)
            strReview =cat(s, strReview)
            strReview =cat(" 		 ",strReview)
            if(flag==0)
            {
                strbuf = strReview
                flag = 1
            }
            else
            {
                InsBufLine (hcurbuf, dwline, strReview)
                dwline = dwline+1;
           }
        }
        ichild = ichild + 1
    }
    InsBufLine (hcurbuf, dwline, strbuf)
    dwline = dwline+1;

    InsBufLine (hcurbuf, dwline, " Return: ")
    
    strReview = Ask("返回值含义")
    strReview =cat(" -> ",strReview)
    strReview =cat(" 		 \@retval",strReview)
    InsBufLine (hcurbuf, dwline+1, strReview)

	strReview = Ask(symname#"作者")
    strReview = cat(" Author: ",strReview)
    InsBufLine (hcurbuf, dwline+2, strReview)

    InsBufLine (hcurbuf, dwline+3, " Log: ")

	strReview = "";
    strReview = cat("	  	 Data			   	Contents",strReview)
    InsBufLine (hcurbuf, dwline+4, strReview)

    szTime = GetSysTime(1)
    Day = szTime.Day
    Month = szTime.Month
    Year = szTime.Year
    if (Day < 10)
        szDay = "0@Day@"
    else
        szDay = Day
    szMonth = NumToName(Month)
    strReview = "";
    strReview = cat("	  	 @szMonth@ @szDay@, @Year@	  	Creation of Function Prototype.",strReview)
    InsBufLine (hcurbuf, dwline+5, strReview)
}

macro AddFileHeader()
{
    hcurbuf = GetCurrentBuf ()

    strcurname = GetBufName (hcurbuf)//当前监视文件的名称
    InsBufLine (hcurbuf, 0, "/***************************************************************************")
    InsBufLine (hcurbuf, 1, "***************************************************************************/")

    strcurname = GetBufName (hcurbuf)//当前监视文件的名称
    
    ilen  = strlen(strcurname)
    while(strcurname[ilen-1] !="\\")
    {
        ilen =ilen -1
    }
    strcurname = strmid (strcurname, ilen, strlen(strcurname))

    szTime = GetSysTime(1)
    Year = szTime.Year

    InsBufLine (hcurbuf, 1, " Copyright(C) @Year@, Amoi Co., Ltd. All Rights Reserved.")
    InsBufLine (hcurbuf, 2, "")
    InsBufLine (hcurbuf, 3, " Name:	 "#strcurname)

    strReview = Ask(" 本文件功能")
    strReview = cat(" Desc:   ",strReview)
    InsBufLine (hcurbuf, 4, strReview)

    strReview = Ask(" 作者")
    strReview = cat(" Author: ",strReview)
    InsBufLine (hcurbuf, 5, strReview)

    InsBufLine (hcurbuf, 6, " Log:	 "#(szTime.year)#"-"#(szTime.month)#"-"#(szTime.day)#"   v1.0    complate the first draft.")
    InsBufLine (hcurbuf, 7, "")
}

macro InsHeaderDef()
{
	 hBuf = GetCurrentBuf()
	 szpathName = GetBufName(hBuf)
	 szfileName = GetFileName(szpathName)
	 szfileName = toupper(szfileName)
	 nlength = StrLen(szfileName)
	 i = 0 /* loop control */
	 szdefineName = ""
	 while (i < nlength)
	 {
	  if (szfileName[i] == ".")
	   szdefineName = Cat(szdefineName, "_")
	  else
	   szdefineName = Cat(szdefineName, szfileName[i])
	  i = i + 1
	 }
	 szdefineName = Cat("_", szdefineName)
	 IfdefineSz(szdefineName)
}

macro InsIfdef()
{
	sz = Ask("Enter ifdef condition:")
	if (sz != "")
	{
		hwnd = GetCurrentWnd()
		sel = GetWndSel(hwnd)
		hbuf = GetWndBuf(hwnd)
		
		// get line the selection (insertion point) is on
		szLine = GetBufLine(hbuf, sel.lnFirst - 1);
		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.
		i = 0
		ich = ""
		while (szLine[i] == " " || szLine[i] == chTab)
		{
			ich = Cat(ich, szLine[i])
			i = i + 1
		}

		InsBufLine(hbuf, sel.lnFirst, "")
		InsBufLine(hbuf, sel.lnFirst + 1, "@ich@#ifdef @sz@")
		InsBufLine(hbuf, sel.lnFirst + 2, "@ich@" # chTab)
		InsBufLine(hbuf, sel.lnFirst + 3, "@ich@#endif  /* @sz@ */")
		SetBufIns(hbuf, sel.lnFirst + 2, StrLen(ich) + StrLen(chTab))
	}
}

macro getfilename(pathname)
{
	nlength = strlen(pathname)
	i = nlength - 1
	name = ""
	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 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)
}

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@" # chTab # "case :" )
  InsBufLine(hbuf, ln + 3, "@szLine@" # chTab # chTab)
  InsBufLine(hbuf, ln + 4, "@szLine@" # chTab # chTab # "break;")
  InsBufLine(hbuf, ln + 5, "@szLine@" # chTab # "default:")
  InsBufLine(hbuf, ln + 6, "@szLine@" # chTab # chTab # "break;")
  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
}

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
}

