function _replace_block( l1, c1, l2, c2, fill, incr, nlen ) -- Replace/Insert/Fill in rectangle block local l, line_pos, line_len, start_pos, end_pos, nblanks, blanks, txt, inslen if l1>l2 then l2,l1=l1,l2 end if c1>c2 then c2,c1=c1,c2 end if nlen>0 then inslen=nlen else inslen=string.len(fill) end editor:BeginUndoAction() editor:DeleteBack() -- we don't need old text in the selection for l = l1, l2 do line_pos = editor:PositionFromLine(l) line_len = editor.LineEndPosition[l] - line_pos if c1 > line_len then nblanks = c1 - line_len blanks = string.rep(" ",nblanks) editor:InsertText(line_pos+line_len, blanks) end start_pos = line_pos + c1 if nlen==0 then editor:InsertText(start_pos, fill) else txt = string.format("%0"..nlen.."d", fill) editor:InsertText(start_pos, txt) fill = fill + incr end end editor.CurrentPos = editor:PositionFromLine(l1) + c1 + inslen editor:EndUndoAction() end function fill_rectangle(args) -- fill_rectangle text:$(1) incr:$(2) -- with one argument - fill with it -- with two arguments (numeric) - fill with sequence if editor.SelectionMode ~= SC_SEL_RECTANGLE then _ALERT("Please provide rectangular selection") return end local ARG1 = "text:" local ARG1LEN = string.len( ARG1 ) local ARG2 = " incr:" local ARG2LEN = string.len( ARG2 ) local fill='' local incr='' local nlen=0 local pos = string.find(args,ARG1,1,true) if not pos then return end args = string.sub(args,pos+ARG1LEN) local pos = string.find(args,ARG2,1,true) if pos then fill = string.sub(args,1,pos-1) incr = string.sub(args,pos+ARG2LEN) else fill = args incr = '' end if incr ~= '' then nlen = string.len( fill ) fill = tonumber( fill ) incr = tonumber( incr ) end local p1 = editor.CurrentPos local p2 = editor.Anchor if p1 > p2 then p1, p2 = p2, p1 end _replace_block( editor:LineFromPosition(p1), editor.Column[p1], editor:LineFromPosition(p2), editor.Column[p2], fill, incr, nlen ) end |