Wrap procedure comments

Average rating
(0 votes)

#pragma TextEncoding = "UTF-8"
#pragma rtGlobals=3
#pragma IndependentModule = WrapText
 
// 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 "Procedure"
	"Comment wrap clipboard/4", /Q, wrapScrap()
end
 
function wrapScrap()	
	putscraptext commentWrap(getscraptext(), 72)	
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)
			if(indent>0)
				k=0
				do
					s_out+=" "
					k+=1
					j+=1
				while(k<indent)
			endif
			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 

I don't suggest using /Q for

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 -

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.

Back to top