diff options
author | sefidel <contact@sefidel.net> | 2022-02-10 00:24:03 +0900 |
---|---|---|
committer | sefidel <contact@sefidel.net> | 2022-02-10 00:24:03 +0900 |
commit | 72d448e384249103748ee83b587c45924e4bc44d (patch) | |
tree | 2aa05a6aaf8c7aa37a8c278fd2fede6e62ff2218 /fnl/nvrc/macro/keymap.fnl | |
download | nvimrc-72d448e384249103748ee83b587c45924e4bc44d.tar.gz nvimrc-72d448e384249103748ee83b587c45924e4bc44d.zip |
Initial commit
Diffstat (limited to 'fnl/nvrc/macro/keymap.fnl')
-rw-r--r-- | fnl/nvrc/macro/keymap.fnl | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/fnl/nvrc/macro/keymap.fnl b/fnl/nvrc/macro/keymap.fnl new file mode 100644 index 0000000..7a784ab --- /dev/null +++ b/fnl/nvrc/macro/keymap.fnl @@ -0,0 +1,46 @@ +(local {: gmatch} string) +(local {: insert} table) + +(local {: fn?} (require :nvrc.macro.toolkit)) + +(fn ->str [x] + (tostring x)) + +(fn nil? [x] + (= nil x)) + +(fn str? [x] + (= :string (type x))) + +(fn tbl? [x] + (= :table (type x))) + +(lambda map! [[modes & options] lhs rhs ?desc] + "Defines a new mapping using the lua API. + Supports all the options that the API supports." + (assert-compile (sym? modes) "expected symbol for modes" modes) + (assert-compile (tbl? options) "expected table for options" options) + (assert-compile (str? lhs) "expected string for lhs" lhs) + (assert-compile (or (str? rhs) (list? rhs) (fn? rhs) (sym? rhs)) + "expected string or list or function or symbol for rhs" rhs) + (assert-compile (or (nil? ?desc) (str? ?desc)) + "expected string or nil for description" ?desc) + (let [modes (icollect [char (gmatch (->str modes) ".")] + char) + options (collect [_ v (ipairs options)] + (->str v) + true) + rhs (if (and (not (fn? rhs)) (list? rhs)) `#,rhs rhs) + desc (if (and (not ?desc) (or (fn? rhs) (sym? rhs))) (view rhs) ?desc) + options (if desc (doto options (tset :desc desc)) options)] + `(vim.keymap.set ,modes ,lhs ,rhs ,options))) + +(lambda mapb! [[modes & options] lhs rhs ?description] + "Defines a new mapping using the lua API. + Supports all the options that the API supports. + Automatically sets the `:buffer` option." + (let [options (doto options + (insert :buffer))] + (map! [modes (unpack options)] lhs rhs ?description))) + +{: map! : mapb!} |