aboutsummaryrefslogtreecommitdiff
path: root/lua/vis.lua
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-03-19 14:47:38 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-03-19 14:58:07 +0100
commit000cbb0c810ad213fa70310d81fdc9afcb9643ee (patch)
treea1dc37929902c1c53af838a3780452ad88736063 /lua/vis.lua
parent5166e81e794c0faa80742ad6806745b13b967a51 (diff)
downloadvis-000cbb0c810ad213fa70310d81fdc9afcb9643ee.tar.gz
vis-000cbb0c810ad213fa70310d81fdc9afcb9643ee.tar.xz
vis-lua: allow operators to be defined as lua functions
Diffstat (limited to 'lua/vis.lua')
-rw-r--r--lua/vis.lua36
1 files changed, 36 insertions, 0 deletions
diff --git a/lua/vis.lua b/lua/vis.lua
index 93a3e5d..a6ed5bb 100644
--- a/lua/vis.lua
+++ b/lua/vis.lua
@@ -5,6 +5,42 @@
---
-- @type Vis
+--- Map a new operator.
+--
+-- Sets up a mapping in normal, visual and operator pending mode.
+-- The operator function will receive the @{File}, @{Range} and position
+-- to operate on and is expected to return the new cursor position.
+--
+-- @tparam string key the key to associate with the new operator
+-- @tparam function operator the operator logic implemented as Lua function
+-- @tparam[opt] string help the single line help text as displayed in `:help`
+-- @treturn bool whether the new operator could be installed
+-- @usage
+-- vis:operator_new("gq", function(file, range, pos)
+-- local status, out, err = vis:pipe(file, range, "fmt")
+-- if not status then
+-- vis:info(err)
+-- else
+-- file:delete(range)
+-- file:insert(range.start, out)
+-- end
+-- return range.start -- new cursor location
+-- end, "Formating operator, filter range through fmt(1)")
+--
+vis.operator_new = function(vis, key, operator, help)
+ local id = vis:operator_register(operator)
+ if id < 0 then
+ return false
+ end
+ local binding = function()
+ vis:operator(id)
+ end
+ vis:map(vis.modes.NORMAL, key, binding, help)
+ vis:map(vis.modes.VISUAL, key, binding, help)
+ vis:map(vis.modes.OPERATOR_PENDING, key, binding, help)
+ return true
+end
+
--- Map a new motion.
--
-- Sets up a mapping in normal, visual and operator pending mode.