source insigh宏=文件保存前清理空白行

macro ProcessBlanksBeforeSave()
{
    var hwnd
    hwnd = GetCurrentWnd()
    if (hwnd == hNil)
    	stop
    var hbuf
    hbuf = GetWndBuf(hwnd)

    /* 删除所有行尾空白 */
    Trim_Whitespace

    /* 转换Tab为空格 */
    ReplaceInBuf(hbuf, "\t", "    ", 0, GetBufLineCount(hbuf), false, false, false, false)

	/* 调整连续空行为一行 */
	var lnCurBlank
	var selResult
	selResult = SearchInBuf(hbuf, "^$", 0, 0, false, true, false)
	if (selResult != Nil)
	{
	    lnCurBlank = selResult.lnFirst 
		selResult = SearchInBuf(hbuf, "^$", lnCurBlank + 1, 0, false, true, false)
		while (selResult != Nil)
		{
		    if (selResult.lnFirst == lnCurBlank + 1)
		    {
		        DelBufLine(hbuf, selResult.lnFirst)
		    }
		    else
		    {
		        lnCurBlank = selResult.lnFirst
		    }
		    selResult = SearchInBuf(hbuf, "^$", lnCurBlank + 1, 0, false, true, false)
		}
	}

	/* 文件末尾追加空行 */
	if (lnCurBlank != GetBufLineCount(hbuf) - 1)
	{
	    AppendBufLine(hbuf, "")
	}

	/* 保存文件 */
	if (IsBufDirty (hbuf))
	{
	    SaveBuf(hbuf)
	}
}

你可能感兴趣的:(source insigh宏=文件保存前清理空白行)