天天看點

sourceinsight使用技巧SourceInsight提供的功能

<b>sourceinsight使用技巧</b>

1 sourceinsight screen font 的預設字型是Verdana的,它是一直變寬字型。在Document style中可以将字型改為定寬的Courier

2 勾掉indent Open Brace和Indent Close Brace的效果: 繼上一段,在相對縮進行裡, 如果輸入"{"或"}", 則自動和上一行列對齊

3 今天把一個用sourceinsight排版整齊的C檔案,偶然用VC打開一看,全亂了。研究了半天,發現SI對每個字元的寬度不太一緻。發現選上"view --&gt; draft view", 就可以讓每個字元的寬度一緻了。快捷鍵是 "Alt + F12"

4 選中幾行代碼按tab鍵或者shift+tab可以左右移動代碼,調整代碼時很有用。

(1)Options菜單àPreferencesàTyping卡,勾掉下面兩項∶

Typing tab indents line,regardless of selection,空行按tab無法前進

Typing tab replaces current selection,標明部分内容、再按tab時會清除所選

(2)Options菜單àDocument Options(針對不同檔案類型,分别進行設定)à下拉左上檔案類型框、選擇合适類型(c源檔案)àEditing Options框中,tab width=2à Editing Options框中,勾選Expand tabs(這樣,按tab鍵、等價于輸入2個空格)

(3)Options菜單àDocument Optionsà選擇合适的檔案類型à點選右邊中間的Auto Indent鈕à在彈出的框中,左邊一定要點Smart,右邊有兩個複選框Indent Open Brace和Indent Close Brace,具體效果可以看SIS的HELP。按照部門裡的程式設計風格要求,最友善的就是把兩個複選框都取消掉,然後點OK。

勾選Auto Indent之SMART的效果∶在C程式裡, 如果遇到行末沒有分号的語句,如IF, WHILE, SWITCH等, 寫到該行末按回車,則新行自動相對上一行縮進兩列。

勾掉Indent Open Brace和Indent Close Brace的效果∶繼上一段,在相對縮進行裡, 如果輸入"}", 則自動和上一行列對齊(好像勾不勾都會有這個功能);而輸入"{"時,不會與下面的行對齊(這是勾上Indent Open Brace時的效果)。

有個同僚比較生猛,得整彙編代碼,但在SIS裡建立PROJECT并ADD TREE的時候,根據預設設定并不會把該TREE裡面所有彙編檔案都包含進來,隻加了.inc和.asm字尾的,.s字尾的沒有。而且用SIS打開.s的檔案,一片黑白沒有色彩,感覺回到DOS的EDIT時代了…… 解決方法是在Options-&gt;Document Options裡面,點左上的Document Type下拉菜單,選擇x86 Asm Source File,然後在右邊的File filter裡*.asm;*.inc;的後面加上*.s;接着CLOSE就可以了。上面兩個問題解決了,但注意加入*.s後還需要重新ADD TREE一遍才能把這些彙編加到PROJECT裡面。

Options菜單àPreferences àTyping卡àAuto Completion框,勾掉Use automatic symbol completion window(這裡是SIS的全局設定)

Options菜單àDocument OptionsàEditing Options框中,勾掉Allow auto-complete(局部設定)

上面兩項必須全部勾選,才能啟用Auto Completion功能

Options菜單àKey assignments,通過關鍵詞Scroll 找到Scroll Half Page Up,取消小鍵盤/;通過關鍵詞Scroll 找到Scroll Half Page Down取消小鍵盤*;通過關鍵詞Function找到Function Up,取消小鍵盤-,通過關鍵詞Function找到Function down,取消小鍵盤+。

通過關鍵詞save 找到save all,更改為ctrl+shift+a,通過關鍵詞select找到select all, 更改為ctrl +a

SIS預設字型是VERDANA,很漂亮。這網頁上應該也是用的VERDANA字型。但由于美觀的緣故,VERDANA字型是不等寬的。比如下面兩行

llllllllll

MMMMMMMMMM

同樣10個字元,長度差多了.用VERDANA來看程式,有些本應該對齊的就歪了。解放方法是使用等寬的字型,但肯定比較醜。可以用DOS字型,也就是記事本裡的預設字型sysfixed 很醜,要有心理準備。比較推薦的是用Courier New。

總地說來,SourceLink根據特定的搜尋模式,把目前檔案中滿足模式的行、連結到由該行指定的其他源檔案中。

所謂特定的搜尋模式,共有兩種“File, then line”和“Line, then file”,其中前後兩部分依靠正規表達式的組的概念來予以分割。如果目前檔案具有比對行,比如“Error d:tcsrcq5.c 18: Lvalue required in function jsSort”,那麼SourceInsight在該行建立SourceLink、把該行連結到由該行指定的檔案中(即d:tcsrcq5.c,第18行)。

運作Search菜單的Parse Source Links…指令,在彈出的框中、選擇搜尋模式、并填入相應的正規表達式串,點OK,SIS就會解析目前檔案,如果有比對,就建立SourceLink。

可以打開日志資訊,運作Parse Source Links指令,日志中能夠比對模式的每一行(通常是含有錯誤資訊的行)、就會被設定上一個SourceLink

首先勾選Custom Command 中的“Parse Links in Output”,然後選擇特定的搜尋模式,最後填入合适的正規表達式。這樣,Source Insight把輸出資訊作為目前搜尋用檔案;并且,如果有比對行(通常即編譯錯誤資訊行),SIS為 該行建立SourceLink、并把每一個錯誤資訊中給定的檔案(和行号)作為link目的地,這對于我們修改源代碼錯誤非常有幫助。

目前來說,普通的替換指令、快捷鍵為ctrl+H,足以已滿足工作要求。

在彈出的替換視窗中,在Search框中勾選Selection來隻在所選文本區域中替換(當然這時你要先標明區域然後再按ctrl+H)、勾選WholeFile來在整個目前檔案内替換、兩者都不勾選來從目前光标處替換至檔案末尾;點右邊的Files…按鈕,可選擇替換多個檔案的内容。

Smart Rename指令、快捷鍵是Ctrl+’,是上下文敏感的全局搜尋替換。它可以智能地重命名全部項目檔案中的一個标示符。SourceInsight的搜尋索引(search index)使得搜尋過程進行地非常快。而且,使用Smart Rename所做的替換會被記錄在Search Results視窗中,每一條替換記錄旁有一個SourceLink連結到替換發生地檔案。

Smart Rename可以用來重命名标記(symbol)。如果勾選了Smart Reference Matching選項,Smart Rename就隻在正确的上下文範圍内進行重命名。它可以智能地重命名全部項目檔案中的一個标示符;它可以重命名函數本地變量,類或結構體成員、函數。

在彈出的Smart Rename視窗中有下面幾項∶

舊名稱。光标下的詞會被自動加載;光标的位置非常重要,這是因為Source Insight會根據本地上下文背景、準确地确定你想要重命名哪一個标記。&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;

推薦隻填單個詞、而不是字元串。

如果你在命名成員變量、或本地變量(),Old Name框中會顯示完全标記名、即上層容器名+标記名。例如,框中的“DocDraw.paintStruc”代表DocDraw是函數名,paintStruc是函數的本地成員變量。

New Name 填新名稱。隻填标記名,不填上層容器名。

Output Search Results 如果勾選,搜尋替換結果日志會被輸出到Search Results視窗中。可以通過Windows菜單來切換,或ctrl+tab切換察看。并且每一條記錄旁會有SourceLink連結到替換發生地檔案。

Confirm Each Replacement 每次替換詢問。

Skip Comments 不重名注釋部分。

【使用心得清單】

(1)如何用Smart Rename重命名數組的數組名?如果隻選取數組名,會報錯!

(2)如果勾掉Smart Reference Matching,會搜尋全部項目檔案,并且Old Name框中不顯示完全限定名;如果勾選Smart Reference Matching,無法重命名數組名,而且滑鼠位置不正确時會報錯。應該如何應對?

正規表達式,是用來比對複雜模式的特殊搜尋用字元串。正規表達式串中,許多字元具有特殊的含義。例如,有個特殊的字元代表 “行首”。

下面是SourceInsight提供的所有可用特殊字元∶

Table 4.3: Regular Expression Characters

Character

Matches

^ (at the beginning only)

beginning of line。如^Hello,比對Hello在句首。

.

any single character

[abc]

any single character that belongs to the set abc

[^abc]

any single character that does not belong to the set abc

*

zero or more occurrences of the preceding character

+

one or more occurrences of the preceding character

t

a tab character

s

a space character

w

white space (a tab or a space character)

$

the end of the line。如TRUE$,比對TRUE在句尾。

轉義字元。如果在它後面有元字元,取消其特殊含義。

可利用 “(”和 “)”、把正規表達式分割成不同的組;模式中的每個組自左向右指定為 Group #n,n=1,2,…;組的概念在替換時很有用。

例如∶

abc(xyz)可比對abcxyz,其中xyz被認為是group#1,

利用21來替換(abc)(xyz),替換結果為xyzabc。

正規表達式格式與源代碼檔案路徑相對應,這裡我的tc安裝目錄為d:tc,tc源檔案放在d:tcsrc下,并命名為qn.c或qtn.c(其中n=1,2,…)。

觀察Tc編譯器某一次輸出錯誤資訊的格式∶

Error d:tcsrcq5.c 18: Lvalue required in function jsSort

則我們要比對“d:tcsrcq5.c 18”部分,進一步地,按照SourceInsight捕捉輸出并加以解析時的要求,要以組的形式、分别比對“d:tcsrcq5.c 18”中的檔案部分和行号部分∶

行号([1-9][0-9]*)

空格行号s([1-9][0-9]*)

檔案名(d:tcsrc[qQ][tT][1-9][0-9]*.[cC])

全部加起來為∶

(d:tcsrc[qQ][tT]*[1-9][0-9]*.[cC])s([1-9][0-9]*)

我的JAVA_HOME是c:jdk,我的java源檔案放于d:javasrc中,并命名為qn.java或qtn.java(其中n=1,2,…)。

觀察JDK編譯器某一次輸出錯誤資訊的格式∶

D:javasrcQ3.java:3: ';' expected

正規表達式為∶

([dD]:javasrc[qQ][tT]*[1-9][0-9]*.java):([1-9][0-9]*)

自定義指令與項目相關,在一個項目中定義的所有自定義指令屬于該項目、隻對該項目有效(包括快捷鍵等)。

自定義指令類似于指令行批處理檔案。SIS允許自定義指令在背景運作;并可以捕捉自定義指令的輸出、放置于檔案中、或粘貼入目前光标插入點。

分别利用上面SIS對輸出資訊的處理方式,自定義指令對內建編譯器相當有用,可以捕捉編譯器輸出并建立SourceLink尋錯;自定義指令對于文本過濾也相當有用,可選中待過濾區塊、運作Sort自定義指令、粘貼回標明區塊、即完成文本過濾。

請按下面步驟建立自定義指令∶

Options菜單àCustom Command

à點右邊Add鈕、填入新自定義指令名稱,或下拉左邊Commands、選擇指令進行修改

àRun框、填入待執行指令行,可含有特殊元字元,見後面的元字元表

àDir框、執行指令行時應處的目錄,如不填,以源代碼檔案所在目錄為指令執行目錄

à勾選Output框的Capture Output、輸出被捕捉,如果勾選Paste Output,輸出被粘貼

à勾選Control Group框中的Save Files First、SIS會在運作指令前先檢查檔案是否儲存

à勾選Control Group框中的Pause When Done、SIS會在指令結束後暫停、友善檢查

à勾選Source Links in Output框中的Parse Source Links,?/p&gt;

5使用最強大的宏功能,真的達到的完美境界

說明:

    該宏檔案實作一些編碼程中能會到的功能, 如添加檔案頭、函數說明和宏定義等, 使用時能自動添加檔案名、函數名和目前日期.

使用說明:

    1. Project-&gt;Open Project... 打開Base工程(該工程一般在"我的檔案\Source Insight\Projects\Base"中);

    2. Project-&gt;Add and Remove Project Files... 加入宏檔案(即mymacro.em);

    3. Options-&gt;Menu Assignments 打開Menu Assignments視窗, 在Command中輸入Macro, 選中要使用的宏, 添加到合适的菜單中.

/*附上宏定義檔案(一下是我精心挑選的十分好用的宏定義,不試不知道,一試真有用)*/

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

/******************************************************************************

* InsFunHeader -- insert function's information

* modification history

* --------------------

* 01a, 23mar2003, added DESCRIPTION by t357

* 01a, 05mar2003, t357 written

******************************************************************************/

macro InsFunHeader()

{

// Get the owner's name from the environment variable: szMyName.

// If the variable doesn't exist, then the owner field is skipped.

/*#########################################################

#########################################################

####### Set szMyName variable to your name    ########

####### for example    szMyName = "t357"     ########

#########################################################   

#########################################################*/

szMyName = "LW"

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

// Get current time

szTime = GetSysTime(1)

Day = szTime.Day

Month = szTime.Month

Year = szTime.Year

else

szDay = Day

szMonth = NumToName(Month)

szInf = Ask("Enter the information of function:")

szDescription = Ask("Enter the description of function:")

// begin assembling the title string

sz = "/******************************************************************************"

InsBufLine(hbuf, ln, sz)

InsBufLine(hbuf, ln + 1, " * @szFunc@ - @szInf@")

InsBufLine(hbuf, ln + 2, " * DESCRIPTION: - ")

InsBufLine(hbuf, ln + 3, " *    @szDescription@ ")

// remove by t357.    CutWord(szDescription)

InsBufLine(hbuf, ln + 4, " * Input: ")

InsBufLine(hbuf, ln + 5, " * Output: ")

InsBufLine(hbuf, ln + 6, " * Returns: ")

InsBufLine(hbuf, ln + 7, " * ")

InsBufLine(hbuf, ln + 8, " * modification history")

InsBufLine(hbuf, ln + 9, " * --------------------")

InsBufLine(hbuf, ln + 10, " * 01a, @szDay@@szMonth@@Year@, @szMyName@ written")

InsBufLine(hbuf, ln + 11, " * --------------------")

InsBufLine(hbuf, ln + 12, " ******************************************************************************/")

// put the insertion point inside the header comment

SetBufIns(hbuf, ln + 1, strlen(szFunc) + strlen(szInf) + 8)

}

* NumToName -- change the month number to name

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"

* CutWord -- auto newline

* 01a, 24mar2003, t357 fix some bug

macro CutWord(ncurLine, szInf)

LENGTH = 63

nlength = StrLen(szInf)

i = 0 /* loop control */

begin = 0 /* first character's index of current line */

pre = 0 /* preceding word's index */

// nline = GetBufLnCur()

while (i {

/* remove by t357

nrow = 0

sz = ""

while (nrow {

   if (nlength     break

   sz = Cat(sz, szInf[nrow])

   nrow = nrow + 1

   nlength = nlength - 1

InsBufLine(hbuf, nline, sz)

szInf = szInf[nrow]

*/

        c = szInf[i]

        if (" " == @c@ &amp;&amp; (i - b         {

            pre = i

        }

        else if (" " == @c@)

        {

            szOutput = ""

            k = begin /* loop control */

            while (k             {

                szOutput = Cat(szOutput, szInf[k])

                k = k + 1

            }

            InsBufLine(hbuf, ncurLine, sz)

            ncurLine = ncurLine + 1

            begin = pre

        i = i + 1

    }

    if (h != i - 1)

    {

        szOutput = ""

        k = begin /* loop control */

        while (k         {

            szOutput = Cat(szOutput, szInf[k])

            k = k + 1

        InsBufLine(hbuf, ncurLine, sz)

        ncurLine = ncurLine + 1

// Wrap ifdeinef .. endif around the current selection

macro IfdefineSz(sz)

hwnd = GetCurrentWnd()

lnFirst = GetWndSelLnFirst(hwnd)

lnLast = GetWndSelLnLast(hwnd)

InsBufLine(hbuf, lnFirst, "#ifndef @sz@")

InsBufLine(hbuf, lnFirst + 1, "#define @sz@")

InsBufLine(hbuf, lnLast + 3, "#endif /* @sz@ */")

SetBufIns(hbuf, lnFirst + 2, 0)

/*   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-&gt;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.

-------------------------------------------------------------------------*/

* AutoExpand - Automatically expands C statements

* DESCRIPTION: - Automatically expands C statements like if, for, while,

*    switch, etc..

* Input:

* Output:

* Returns:

* 01a, 27mar2003, t357 modified

macro AutoExpand()

// get window, sel, and buffer handles

if (hwnd == 0)

stop

sel = GetWndSel(hwnd)

if (sel.ichFirst == 0)

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, " (###; ###; ###)")

else if (wordinfo.szWord == "switch")

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@" # "default:")

InsBufLine(hbuf, ln + 6, "@szLine@" # chTab)

InsBufLine(hbuf, ln + 7, "@szLine@" # "}")

else if (wordinfo.szWord == "do")

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;")

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

// scan backwords over white space, if any

ich = ich - 1;

if (ich &gt;= 0)

while (sz[ich] == " " || sz[ich] == chTab)

   ich = ich - 1;

   if (ich     break;

// scan backwords to start of word

ichLim = ich + 1;

asciiA = AsciiFromChar("A")

asciiZ = AsciiFromChar("Z")

while (ich &gt;= 0)

ch = toupper(sz[ich])

asciiCh = AsciiFromChar(ch)

if ((asciiCh asciiZ) &amp;&amp; !IsNumber(ch))

   break // stop at first non-identifier character

wordinfo.szWord = strmid(sz, ich, ichLim)

wordinfo.ich = ich

wordinfo.ichLim = ichLim;

return wordinfo

//

// Comment the selected block of text using single line comments and indent it

macro CommentBlock()

hbuf = GetCurrentBuf();

hwnd = GetCurrentWnd();

sel = GetWndSel(hwnd);

iLine = sel.lnFirst;

while (iLine {

   szLine = GetBufLine(hbuf, iLine);

   szLine = cat("// ", szLine);

   PutBufLine(hbuf, iLine, szLine);

   iLine = iLine + 1;

if (sel.lnFirst == sel.lnLast)

   tabSize = _tsGetTabSize() - 1;

   sel.ichFirst = sel.ichFirst + tabSize;

   sel.ichLim = sel.ichLim + tabSize;

SetWndSel(hwnd, sel);

// Undo the CommentBlock for the selected text.

macro UnCommentBlock()

tabSize = 0;

   len = strlen(szLine);

   szNewLine = "";

   if (len &gt; 1)

    if (szLine[0] == "/" &amp;&amp; szLine[1] == "/")

     if (len &gt; 2)

     {

      if (AsciiFromChar(szLine[2]) == 9)

      {

       tabSize = _tsGetTabSize() - 1;

       szNewLine = strmid(szLine, 3, strlen(szLine));

      }

     }

     if (szNewLine == "")

      szNewLine = strmid(szLine, 2, strlen(szLine));

      tabSize = 2;

     PutBufLine(hbuf, iLine, szNewLine);

   sel.ichFirst = sel.ichFirst - tabSize;

   sel.ichLim = sel.ichLim - tabSize;

macro _tsGetTabSize()

szTabSize = GetReg("TabSize");

if (szTabSize != "")

   tabSize = AsciiFromChar(szTabSize[0]) - AsciiFromChar("0");

   tabSize = 4;

return tabSize;

// Reformat a selected comment block to wrap text at 80 columns.

// The start of the selection (upper left most character of the selection) is

// handled specially, in that it specifies the left most column at which all

// lines will begin. For example, if the following block was selected starting

// at the @ symbol, through the last line of the block...

//------------------------------------------------------------------------------

// preamble: @ This is a line that will be wrapped keeping the "at" symbol in its current column.

// All lines following it that are selected will use that as their starting column. See below to see how the wrapping

// works for this block of text.

// preamble: @ This is a line that will be wrapped keeping the "at" symbol in

//     its current column. All lines following it that are selected

//     will use that as their starting column. See below to see how

//     the wrapping works for this block of text.

macro tsReformatCommentBlock()

tabSize = _tsGetTabSize();

leftTextCol = 0 - 1;

colWrap = 80;

// Find the starting column, and create a Margin string

ichFirst = sel.ichFirst;

// Single line comment reformat?

if (sel.ichFirst == sel.ichLim &amp;&amp; sel.lnFirst == sel.lnLast)

   ichFirst = 0;

rec = _tsGetStartColumn(hbuf, ichFirst, sel.lnFirst);

if (rec == "")

   stop;

colLeftMargin = rec.colMargin;

szMargin = "";

colComment = 0;

if (rec.colComment &gt;= 0)

   colComment = rec.colComment + 2

   szMargin = _tsAddWhiteToColumn(szMargin, 0, rec.colComment, tabSize);

   szMargin = cat(szMargin, "//");

szMargin = _tsAddWhiteToColumn(szMargin, colComment, rec.colMargin, tabSize);

rec = "";

szCurMargin = "";

if (ichFirst != 0)

   szLine = GetBufLine(hbuf, sel.lnFirst);

   szCurMargin = strmid(szLine, 0, ichFirst);

   szCurMargin = szMargin;

   szMargin = "";

insertLine = sel.lnFirst;

szRemainder = "";

while (1)

//   msg("$0-" # iLine # ":" # szRemainder);

   rec = _tsGetNextCommentString(hbuf, ichFirst, szRemainder, iLine, sel.lnLast, colWrap);

   if (rec == "")

    break;

//   msg("$1-" # rec.ln # ":" # rec.szComment);

   szLine = rec.szComment;

   ich = 0;

   col = colLeftMargin;

   ichPrevCharToWhite = 0-1;

   ichPrevWhiteToChar = 0-1;

//   msg("Leftovers @szRemainder@");

   while (ich    {

    if (AsciiFromChar(szLine[ich]) == 9)

     col = (((col + tabSize) / tabSize) * tabSize);

    else

     col = col + 1;

    if (col &gt; colWrap)

     break;

    fIsWhitespace = _tsIsWhitespaceChar(szLine[ich]);

    fIsWhitespace1 = 1;

    if (ich + 1     {

     fIsWhitespace1 = _tsIsWhitespaceChar(szLine[ich + 1]);

    if (!fIsWhitespace &amp;&amp; fIsWhitespace1)

     ichPrevCharToWhite = ich;

    ich = ich + 1;

   if (ichPrevCharToWhite &gt; 0)

//    msg("$2:" # strmid(szLine, 0, ichPrevCharToWhite + 1));

    ich = ichPrevCharToWhite + 1;

    while (ich     {

     if (!_tsIsWhitespaceChar(szLine[ich]))

      ichPrevWhiteToChar = ich - 1;

//      msg("$3:" # strmid(szLine, ichPrevWhiteToChar + 1, len));

      break;

     ich = ich + 1;

   if (ichPrevCharToWhite &gt; 0 &amp;&amp; col &gt; colWrap)

    szNewLine = cat(szCurMargin, strmid(szLine, 0, ichPrevCharToWhite + 1));

    szRemainder = "";

    if (ichPrevWhiteToChar &gt; 0)

     szRemainder = strmid(szLine, ichPrevWhiteToChar + 1, len);

    if (ichPrevCharToWhite &gt; ichPrevWhiteToChar)

     msg("!!!Wrap, duplicating word " # ichPrevWhiteToChar # " " # ichPrevCharToWhite # " " # szNewLine # " &gt;&gt;&gt; " # szRemainder);

//     msg(szLine);

//     msg(col # " " # ichPrevWhiteToChar # " " # ichPrevCharToWhite # " " # szNewLine # " &gt;&gt;&gt; " # szRemainder);

   else if (szLine != "")

    szNewLine = cat(szCurMargin, szLine );

//    sel.lnLast = sel.lnLast + 1;

   iLine = rec.ln;

   if (insertLine == iLine)

    iLine = iLine + 1;

    sel.lnLast = sel.lnLast + 1;

//    msg("$5-" # insertLine # ":" # szNewLine);

    InsBufLine(hbuf, insertLine, szNewLine);

   else

    szLine = GetBufLine(hbuf, insertLine);

    if (szLine != szNewLine)

//     msg("$6-" # insertLine # ":" # szNewLine);

     PutBufLine(hbuf, insertLine, szNewLine);

   insertLine = insertLine + 1;

   if (szMargin != "")

    szCurMargin = szMargin;

    szMargin = "";

while (insertLine {

   DelBufLine(hbuf, insertLine);

   sel.lnLast = sel.lnLast - 1;

len = GetBufLineLength(hbuf, insertLine-1);

sel.ichFirst = len;

sel.ichLim = len;

sel.lnFirst = sel.lnLast;

macro _tsAddWhiteToColumn(sz, col0, col, tabSize)

szTabs = "                               ";

szSpaces = "                ";

tabs0 = col0 / tabSize;

tabs = (col / tabSize) - tabs0;

if (tabs == 0)

   foo = col0;

   foo = (tabs + tabs0) * tabSize;

spaces = col - foo;

// msg(col0 # " " # col # " " # tabs # " " # spaces # " " # tabs0);

if (tabs)

   sz = cat(sz, strmid(szTabs, 0, tabs));

if (spaces)

   sz = cat(sz, strmid(szSpaces, 0, spaces));

return sz;

macro _tsGetStartColumn(hbuf, ichBegin, ln)

szLine = GetBufLine(hbuf, ln);

len = strlen(szLine);

ich = 0;

colMargin = 0;

colComment = 0-1;

rec.colMargin = colMargin;

rec.colComment = colComment;

while (ich {

   if (AsciiFromChar(szLine[ich]) == 9)

    colMargin = (((colMargin + tabSize) / tabSize) * tabSize);

    colMargin = colMargin + 1;

   if (colComment    {

     if (szLine[ich] == "/" &amp;&amp; szLine[ich+1] == "/")

      colComment = colMargin - 1;

      ich = ich + 2;

      colMargin = colMargin + 1;

      continue;

   if (ich &gt;= ichBegin)

    if (!_tsIsWhitespaceChar(szLine[ich]))

     rec.colMargin = colMargin - 1;

     rec.colComment = colComment;

//     msg(szLine[ich]);

     return rec;

   ich = ich + 1;

return rec;

macro _tsGetNextCommentString(hbuf, ichSkip, szRemainder, ln, lnLast, colWrap)

// Go until we get a string that is at least long enough to fill a line

// or, we run out of lines.

if (szRemainder == "" &amp;&amp; ln &gt; lnLast)

   return "";

ichFirst = ichSkip;

// msg(ichSkip);

   if (ln &gt; lnLast)

    rec.szComment = szRemainder;

    rec.ln = ln;

    return rec;

   cchRemainder = strlen(szRemainder);

   if (cchRemainder &gt; colWrap)

   szLine = GetBufLine(hbuf, ln);

   if (ichSkip == 0)

    ichFirst = _tsSkipPastCommentAndWhitespace(szLine, len);

   ichSkip = 0;

   ichLast = len - 1;

   // Now, strip out all whitespace at the end of the line

   while (ichLast &gt;= ichFirst)

    if (!_tsIsWhitespaceChar(szLine[ichLast]))

    ichLast = ichLast - 1;

   // Entire line is whitespace?

   if (ichLast    {

    if (szRemainder == "")

     ln = ln + 1;

   // length of the non whitespaced comment + 1 space + cchRemainder

   if ((ichLast + 1) - ichFirst + cchRemainder + 1 &gt; 255)

    // It may not format the current line quite right, but

    // but at least we won't throw away some of the comment.

   if (szRemainder != "")

    szRemainder = cat(szRemainder, " ");

   szRemainder = cat(szRemainder, strmid(szLine, ichFirst, ichLast + 1));

   ln = ln + 1;

macro _tsSkipPastCommentAndWhitespace(szLine, len)

ichFirst = 0;

// Skip past the comment initiator "//" if there is one.

while (ichFirst {

   if (ichFirst + 1    {

    if (szLine[ichFirst] == "/" &amp;&amp; szLine[ichFirst+1] == "/")

     ichFirst = ichFirst + 2;

   ichFirst = ichFirst + 1;

// If no comment found in line, then start from the beginning

if (ichFirst &gt;= len)

ichFirst = ichFirst;

// Now, strip out all whitespace after the comment start.

   if (!_tsIsWhitespaceChar(szLine[ichFirst]))

return ichFirst;