aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorgi Kirilov <>2020-11-07 14:11:12 +0200
committerMatěj Cepl <mcepl@cepl.eu>2022-08-18 19:08:33 +0200
commit9c89b5aa0bfc7858393062c7551650b3d3b30d67 (patch)
treefe0970fb668efdf74bfcfe5d8a1de6a525668be6
parent9ef1a4ec56bdcd75f41028b3f0cbbf17435212e6 (diff)
downloadvis-toggler-9c89b5aa0bfc7858393062c7551650b3d3b30d67.tar.gz
Squash commits
-rw-r--r--example.lua25
-rw-r--r--init.lua41
2 files changed, 55 insertions, 11 deletions
diff --git a/example.lua b/example.lua
new file mode 100644
index 0000000..cfb47a2
--- /dev/null
+++ b/example.lua
@@ -0,0 +1,25 @@
+return {
+ {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"},
+ {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"},
+ {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"},
+ {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"},
+
+ -- string:
+ {"upper", "lower"},
+
+ -- math:
+ {"ceil", "floor"},
+ {"min", "max"},
+
+ -- io:
+ {"stderr", "stdout"},
+
+ -- bitwise:
+ {"<<", ">>"},
+
+ -- relational:
+ {"<=", ">"},
+ {">=", "<"},
+ {"==", "~="},
+ {"true", "false"},
+}
diff --git a/init.lua b/init.lua
index 4bac6b0..489983d 100644
--- a/init.lua
+++ b/init.lua
@@ -8,9 +8,9 @@ local vis = vis
require("lpeg")
local l = lpeg
-local Cc, Cmt, Cp, P, R, V = l.Cc, l.Cmt, l.Cp, l.P, l.R, l.V
+local C, Cc, Ct, Cmt, Cp, Cg, P, R, S, V = l.C, l.Cc, l.Ct, l.Cmt, l.Cp, l.Cg, l.P, l.R, l.S, l.V
-local M = {}
+local M = {config = {}}
-- Inverted and unrolled config table.
-- With this table format, make_shift() can use the same logic for both numeric and word increment/decrement.
@@ -23,6 +23,10 @@ local count
local char_next = 17
+local dec_digit = R"09"
+local hex_digit = R("09", "af", "AF")
+local zeros = P"0"^0 / function(c) return #c end
+
local function at_or_after(_, _, pos, start, finish, is_number)
if pos >= start and pos < finish or is_number and pos < start then
return true, start, finish
@@ -35,8 +39,8 @@ local function ordinal(win, pos)
if s.pos == pos then selection = s break end
end
local line = win.file.lines[selection.line]
- local dec_num = P"-"^-1 * R"09"^1
- local patt = Cp() * dec_num * Cp() * Cc(true) + Cp() * ordinal_words * Cp()
+ local num = S"-+"^-1 * ((P"0x" + "0X") * hex_digit^1 + dec_digit^1)
+ local patt = Cp() * num * Cp() * Cc(true) + Cp() * ordinal_words * Cp()
local start, finish = P{Cmt(Cc(selection.col) * patt, at_or_after) + 1 * V(1)}:match(line)
if not (start and finish) then return end
local line_begin = selection.pos - selection.col + 1
@@ -57,13 +61,32 @@ local function toggle(func, motion)
end
local function make_shift(shift)
+ local upper
return function(word, delta)
local number = tonumber(word)
local iter = number and {number} or lookup[word]
if not iter then return end
local binary = iter[2] and #iter[2] == 2
local neighbor, rotate = shift(iter[1], iter[2], delta)
- return number and neighbor or iter[2][neighbor] or binary and iter[2][rotate]
+ if number then
+ local num = Ct(S"-+"^-1 * (Cg(P"0x" + "0X", "base") * zeros * C(hex_digit^1)^-1 +
+ zeros * C(dec_digit^1)^-1))
+ local groups = num:match(word)
+ local digits = groups[2] and #groups[2] or 0
+ local sign = neighbor < 0 and "-" or ""
+ local has_letters = groups[2] and groups[2]:find"%a"
+ if has_letters then
+ upper = groups[2]:find"%u"
+ elseif groups.base == "0X" then
+ upper = true
+ end
+ local hexfmt = upper and "%X" or "%x"
+ local abs = string.format(groups.base and hexfmt or "%d", math.abs(neighbor))
+ local dzero = #tostring(abs) - digits
+ local base = groups.base or ""
+ return sign .. base .. string.rep("0", groups[1] > 0 and groups[1] - dzero or 0) .. abs
+ end
+ return iter[2][neighbor] or binary and iter[2][rotate]
end
end
@@ -127,7 +150,7 @@ vis.events.subscribe(vis.events.INIT, function()
operator_new("g~", toggle(case), nil, nil, "Toggle-case operator")
operator_new("gu", toggle(string.lower), nil, nil, "Lower-case operator")
operator_new("gU", toggle(string.upper), nil, nil, "Upper-case operator")
- lookup, ordinal_words = preprocess(M)
+ lookup, ordinal_words = preprocess(M.config)
vis:map(vis.modes.NORMAL, "g~~", "g~il")
vis:map(vis.modes.NORMAL, "guu", "guil")
vis:map(vis.modes.NORMAL, "gUU", "gUil")
@@ -135,8 +158,4 @@ vis.events.subscribe(vis.events.INIT, function()
vis:map(vis.modes.VISUAL, "U", "gU")
end)
-return function(config)
- local ext_config = require(config)
- M = ext_config or M
- return M
-end
+return M