Wrap procedure comments

The most recent version of this code can be found in the compilation of text-editing code posted here.

#pragma TextEncoding = "UTF-8"
#pragma rtGlobals=3
#pragma IndependentModule = WrapText
#pragma version=1.01

// If your lengthy procedure comments often end up looking a bit like
// this, you
// may find this code snippet useful. To use, copy, wrapScrap(), paste.

menu "Edit"
    "Comment wrap clipboard/4", /Q, WrapText#wrapScrap()
    "Unwrap clipboard/5", /Q, WrapText#unwrapScrap()
end

function wrapScrap()   
    putscraptext commentWrap(getscraptext(), 72)   
end

function unwrapScrap() 
    putscraptext commentUnwrap(getscraptext()) 
end

// returns string wrapped at V_wrap characters, commented,
// and indented to match indent at start of s_in.
function /T commentWrap(s_in, v_wrap)
    string s_in
    variable v_wrap
   
    string s_out="", s_char=""
   
    s_in=replacestring("\t", s_in, "   ")
   
    // try to guess how far we want to indent comment
    variable indent=0
    for(indent=0;indent<25;indent+=1)
        if(!stringmatch(s_in[indent], " "))
            break
        endif
    endfor
   
    s_in=replacestring("//", s_in, " ")
    s_in=replacestring("\r", s_in, " ")
   
    // condense whitespace
    variable len
    do
        len=strlen(s_in)
        s_in=replacestring("  ", s_in, " ")
    while(strlen(s_in)!=len)
   
    variable i=0,j=0,k=0
    // i is character count in s_in
    // j is character count in current line of s_out
    do
        s_char=s_in[i]
        i+=1
        if(j==0)
            for(k=0;k<indent;k+=1,j+=1)
                s_out+=" "
            endfor 
            s_out+="// "
            j+=3
            if (!stringmatch(s_char, " "))
                s_out+=s_char
                j+=1
            endif  
        elseif (j>v_wrap)
            if(!stringmatch(s_char, " "))  
                do
                    j-=1
                    if (stringmatch(s_out[strlen(s_out)-(v_wrap+1-j)], " "))
                        break
                    endif
                while(j>indent+3)
                if(j>indent+3)
                    s_out=s_out[0,strlen(s_out)-(v_wrap+1-j)]
                    i-=(v_wrap+1-j)
                endif
            endif      
            s_out+="\r"
            j=0
        else
            s_out+=s_char
            j+=1
        endif
    while(i<len)
    s_out=replacestring("   ", s_out, "\t")
   
    return s_out   
end

// returns unwrapped string with comment indicators removed
function /T commentUnwrap(s)
    string s
   
    s=replacestring("\t", s, "   ")
    s=replacestring("//", s, "")
    s=replacestring("\r\r", s, "<$$> ")
    s=replacestring("\r", s, " ")
    s=replacestring("<$$>", s, "\r\r")
   
    // condense whitespace
    variable len
    do
        len=strlen(s)
        s=replacestring("  ", s, " ")
    while(strlen(s)!=len)
   
    do
        len=strlen(s)
        s=replacestring("\r ", s, "\r")
    while(strlen(s)!=len)
   
    do
        s[0,0]=selectstring(stringmatch (s[0]," "), s[0], "")
    while(strlen(s) && stringmatch(s[0], " "))
   
    return s
end
I don't suggest using /Q for the shortcut: on Macintosh that quits Igor!

--Jim Prouty
Software Engineer, WaveMetrics, Inc.
The /Q is a menu item flag - I call it the quiet flag - to prevent cluttering the history with •WrapText#wrapScrap().
DisplayHelpTopic  "Menu Definition Syntax" for details.
I should have pointed out that I did include a shortcut - command-4 - which should be edited to avoid conflict with other user-defined shortcuts.
I use this as an independent module so that it works regardless of whether procedure is compiled.
One use is for taking unwrapped text from outside of Igor and pasting as a comment.
Don't forget that there are also keyboard shortcuts for commenting and decommenting, but these won't wrap the text for you.

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More