Scite 注释框 |
|
--~ Welcome, reader --~ This script is used to comment parts of a text in a nicely formatted box. --~ It is quite configurable (try different settings, experiment!), and I tried --~ to add some comments in the whole script, to make it a bit more understandable. --~ Anyhow, if you have any comments, please mail to jerous gmail com --~ Have fun! --~ ]erous. --~ to use, make sure this script is loaded, add following in the userproperties --~ and press F11 when done! --~ command.name.3.*=Box-comment --~ command.3.*=comment_type1 --~ command.subsystem.3.*=3 --~ command.mode.3.*=savebefore:no --~ command.shortcut.3.*=F11 --~ char(s) indicating start of comment for 1 line com_start_char = '| ' com_end_char = '|' --~ char(s) indicating commentblock com_block_start_char = '/*\\' com_block_end_char = '*/' --~ character to use at the top line com_topline_char = '_' --~ character to use at the ending line com_endline_char = '_' --~ add following lines to the front/end of the commentbox --~ as an array; e.g. com_add_to_front = {"Author: ]erous", "Date: today"} com_add_to_front = {"Commented on "..os.date("%d-%m-%yT%H:%M:%S"), "By Me", ""} com_add_to_end = {} --~ create a comment box for the currently selected text --~ if no text is selected, the current line will be 'box-commented' function comment_type1() --~ retrieve selected region... p1=editor:LineFromPosition(editor.SelectionStart); p2=editor:LineFromPosition(editor.SelectionEnd); --~ if nothing selected, then take the line we are working on if p1==p2 then p2=p1+1 end --~ read the lines in the selection to an array --~ + add some new text to start and end (view config up here) lines = read_lines(p1,p2) --~ get the length of the longest line max_str_len=get_longest_line(lines)+1 --~ create the start and endstrings 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 --~ format the text... text = format_text(lines, max_str_len) --~ ... and add the header and footer text=str_header..text..str_footer --~ and replace! editor:ReplaceSel(text) --~ print(text) end --~ format the text, by adding com_start_char and com_end_char to respectively front and end --~ and formatting it nicely --~ lines: array containing the lines (without carriage returns etc) --~ max_str_len: length of the longest line --~ return: string formatted text 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 --~ read the lines from the current selection --~ and add some stuff to the front and end --~ *_line: the lines from which the selection respectively starts and ends --~ return: table containing all lines to be displayed, with returns removed, and tabs replaced by spaces function read_lines(start_line, end_line) local ret = {} --~ add things to add to the front for i=1,table.getn(com_add_to_front) do table.insert(ret,table.getn(ret)+1,com_add_to_front[i]) end --~ add the text for i=start_line,end_line-1 do line=editor:GetLine(i) --~ remove returns, and replace tabs by the userdefined nr of spaces 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 --~ add things to add to the 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 --~ retrieve the length of the longest line --~ lines: array containing the lines to get longest line from --~ return: int containing the length; -1 if failed 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