diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-02-28 10:23:59 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-02-28 10:43:05 +0100 |
| commit | f9cec7b86a6db8ea5c40dd3f930561591bd5362c (patch) | |
| tree | 3f71af17389c6f760412e7342bb9c87ae68c2140 /lua/plugins | |
| parent | 28fae462be95a0573113c76d9d2641e73dd1b44e (diff) | |
| download | vis-f9cec7b86a6db8ea5c40dd3f930561591bd5362c.tar.gz vis-f9cec7b86a6db8ea5c40dd3f930561591bd5362c.tar.xz | |
lua: reimplement number increment <C-a> and decrement <C-x>
Based on a patch by Denis Warsow.
Stuff which could probably be improved:
- in vim the cursor does not need to be on top of the number,
it suffices to be on the same line.
- decrementing beyond zero does not work for hexadecimal and
octal numbers, vim seems to wrap around in this case.
0x00000000 <C-x> becomes 0xffffffff
Close #506
Diffstat (limited to 'lua/plugins')
| -rw-r--r-- | lua/plugins/number-inc-dec.lua | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lua/plugins/number-inc-dec.lua b/lua/plugins/number-inc-dec.lua new file mode 100644 index 0000000..a165b5a --- /dev/null +++ b/lua/plugins/number-inc-dec.lua @@ -0,0 +1,54 @@ +-- increment/decrement number in dec/hex/oct format +local lexer = vis.lexers +if not lexer then return end + +local change = function(delta) + + local win = vis.win + local file = win.file + local count = vis.count + if not count then count = 1 end + vis.count = nil -- reset count + + for cursor in win:cursors_iterator() do + local pos = cursor.pos + local word = file:text_object_word(pos); + local s, e = word.start, word.finish; + if s then + local data = file:content(s, e - s) + if data and #data > 0 then + 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 number then + number = number + delta * count + if base ~= 10 and number < 0 then + -- string.format does not handle negative hex/oct values + -- should we wrap around? + 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 + else + vis:info("Not a number") + end + end + end + end +end + +vis:map(vis.modes.NORMAL, "<C-a>", function() change( 1) end, "Increment number") +vis:map(vis.modes.NORMAL, "<C-x>", function() change(-1) end, "Decrement number") |
