Jussie. With each update there is a change. But still no cigar
Here is what I am trying to do. create a macro that gives 1/2 tab.
Now when the line begins with a tab, it should copy the tab and add 2,3,4 spaces depending on the tab setting (4 6 or 8 columns).
at 4 column tabs, it adds 5 spaces (should be 2 : 3 too many)
at 6 column tabs, it adds 8 spaces (should be 3 : 5 too many)
at 8 column tabs, it adds 11 spaces (should be 4 : 9 too many)
Here is the code for 8 column tabs:
Code: Select all
function replace_string()
local half_tab = 4
local half_tab_chrs = ' ' -- equal half_tab spaces
-- get the line
local data_line=get_line_text()
debug_output("original:"..data_line.."\n")
MoveLineHome()
if(string.len(data_line)>0)then
-- delete the line, it will be replaced
MarkBlockSet()
MoveLineEnd()
MarkBlockReset()
MarkDeleteEx()
while string.len(data_line)>0 do
debug_output("check 1st 2 characters:"..string.sub(data_line,1,half_tab).."\n")
if(string.byte(data_line,1)==9)then -- a tab, copy this
debug_output("tab found. Copy\n")
print("\t")
data_line=string.sub(data_line,2)
elseif(string.sub(data_line,1,half_tab)==half_tab_chrs)then -- half_tab chars already here, replace with tab and done
debug_output("half tab found. Tab and copy rest\n")
debug_output("\t"..string.sub(data_line,half_tab+1).."\n")
print("\t"..string.sub(data_line,half_tab+1))
data_line=''
else -- word: add half_tab chars and done.
debug_output("string found, half tab and copy\n")
debug_output(half_tab_chrs..data_line.."\n")
print(half_tab_chrs..data_line)
data_line=''
end
debug_output("Remaining Line:"..data_line.."\n")
end
end
end
The nice thing is, the debug output is perfect.
The error comes in the 7th line from the end:
I test it by running it on a line that has characters like
ABC
1st run (b=blank):
bbbbABC
perfect
2nd run:
_ TAB_ _ABC
perfect
3rd run should be:
_ TAB_ _bbbbABC
but I get
_ TAB_ _bbbbbbbbbbbABC
way too many spaces.
