A script that allows you to comment code (configured for php / cpp, but easy adaptable to other languages I think).
Place following text in a file:
com_start_char = '| '
com_end_char = '|'
com_block_start_char = '/*\\'
com_block_end_char = '*/'
com_topline_char = '_'
com_endline_char = '_'
com_add_to_front = {"Commented on "..os.date("%d-%m-%yT%H:%M:%S"), "By Me", ""}
com_add_to_end = {}
function comment_type1()
p1=editor:LineFromPosition(editor.SelectionStart);
p2=editor:LineFromPosition(editor.SelectionEnd);
if p1==p2 then
p2=p1+1
end
lines = read_lines(p1,p2)
max_str_len=get_longest_line(lines)+1
header_len=(max_str_len+string.len(com_start_char..com_end_char)-string.len(com_block_start_char))/string.len(com_topline_char)
footer_len=(max_str_len+string.len(com_start_char..com_end_char)-string.len(com_block_end_char))/string.len(com_endline_char)
str_header = com_block_start_char..string.rep(com_topline_char, header_len).."\n"
str_footer = string.rep(com_endline_char, footer_len)..com_block_end_char
text = format_text(lines, max_str_len)
text=str_header..text..str_footer
editor:ReplaceSel(text)
end
function format_text(lines, max_str_len)
ret = ""
for i=1,table.getn(lines) do
ret=ret..com_start_char
..lines[i]
..string.rep(" ", string.len(str_header)-string.len(lines[i]..com_end_char..com_start_char)-1)
..com_end_char.."\n"
end
return ret
end
function read_lines(start_line, end_line)
local ret = {}
for i=1,table.getn(com_add_to_front) do
table.insert(ret,table.getn(ret)+1,com_add_to_front[i])
end
for i=start_line,end_line-1 do
line=editor:GetLine(i)
line=string.gsub(line, "\n", "")
line=string.gsub(line, "\r", "")
line=string.gsub(line, "\t", string.rep(" ", props['tabsize']))
table.insert(ret,table.getn(ret)+1,line)
end
for i=1,table.getn(com_add_to_end) do
table.insert(ret,table.getn(ret)+1,com_add_to_end[i])
end
return ret
end
function get_longest_line(lines)
max=-1
for i=1,table.getn(lines) do
if (string.len(lines[i])>max) then
max=string.len(lines[i])
end
end
return max
end
RecentChanges · preferences
edit · history
Last edited February 28, 2008 12:35 pm GMT (diff)