diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-03-02 08:53:35 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-03-02 12:05:46 +0100 |
| commit | 57e38835499a2051953c4d2e1901996fd69d7080 (patch) | |
| tree | d71786a693714a2cb08eae4f168e3776359e3a18 /lua/plugins/number-inc-dec.lua | |
| parent | 5a4ec36070aed439d509870b793a008fee24a48f (diff) | |
| download | vis-57e38835499a2051953c4d2e1901996fd69d7080.tar.gz vis-57e38835499a2051953c4d2e1901996fd69d7080.tar.xz | |
lua: use goto label to mimic continue statement
This is a Lua 5.2 feature supported by LuaJIT.
Diffstat (limited to 'lua/plugins/number-inc-dec.lua')
| -rw-r--r-- | lua/plugins/number-inc-dec.lua | 77 |
1 files changed, 36 insertions, 41 deletions
diff --git a/lua/plugins/number-inc-dec.lua b/lua/plugins/number-inc-dec.lua index e2f5f2e..e1142ef 100644 --- a/lua/plugins/number-inc-dec.lua +++ b/lua/plugins/number-inc-dec.lua @@ -16,47 +16,42 @@ local change = function(delta) vis.count = nil -- reset count, otherwise it affects next motion for cursor in win:cursors_iterator() do - -- poor man's continue statement, use break to process next cursor - repeat - local pos = cursor.pos - if not pos then break end - local word = file:text_object_word(pos); - if not word then break end - local data = file:content(word.start, 1024) - if not data then break end - local s, e = pattern:match(data) - if not s then break end - data = string.sub(data, s, e-1) - if #data == 0 then break end - -- align start and end for fileindex - s = word.start + s - 1 - e = word.start + e - 1 - local base, format, padding = 10, 'd', 0 - if lexer.oct_num:match(data) then - base = 8 - format = 'o' - padding = #data - elseif lexer.hex_num:match(data) then - base = 16 - format = 'x' - padding = #data - #"0x" - end - local number = tonumber(data, base == 8 and 8 or nil) - if not number then - vis:info("Not a number") - break - end - number = number + delta * count - -- string.format does not support negative hex/oct values - if base ~= 10 and number < 0 then number = 0 end - number = string.format((base == 16 and "0x" or "") .. "%0"..padding..format, number) - if base == 8 and string.sub(number, 0, 1) ~= "0" then - number = '0' .. number - end - file:delete(s, e - s) - file:insert(s, number) - cursor.pos = s - until true + local pos = cursor.pos + if not pos then goto continue end + local word = file:text_object_word(pos); + if not word then goto continue end + local data = file:content(word.start, 1024) + if not data then goto continue end + local s, e = pattern:match(data) + if not s then goto continue end + data = string.sub(data, s, e-1) + if #data == 0 then goto continue end + -- align start and end for fileindex + s = word.start + s - 1 + e = word.start + e - 1 + local base, format, padding = 10, 'd', 0 + if lexer.oct_num:match(data) then + base = 8 + format = 'o' + padding = #data + elseif lexer.hex_num:match(data) then + base = 16 + format = 'x' + padding = #data - #"0x" + end + local number = tonumber(data, base == 8 and 8 or nil) + if not number then goto continue end + number = number + delta * count + -- string.format does not support negative hex/oct values + if base ~= 10 and number < 0 then number = 0 end + number = string.format((base == 16 and "0x" or "") .. "%0"..padding..format, number) + if base == 8 and string.sub(number, 0, 1) ~= "0" then + number = '0' .. number + end + file:delete(s, e - s) + file:insert(s, number) + cursor.pos = s + ::continue:: end end |
