diff options
author | sefidel <contact@sefidel.net> | 2022-03-11 07:49:36 +0900 |
---|---|---|
committer | sefidel <contact@sefidel.net> | 2022-03-11 07:49:36 +0900 |
commit | de95ea87833f04611329339c80d723bde696093f (patch) | |
tree | c3989a62e2ba78e4fc22c692bc88b9834d395c41 /fnl/nvrc/macro | |
parent | 74a909c037a8d5baafc4c1aff5adf3a1c6b214c3 (diff) | |
download | nvimrc-de95ea87833f04611329339c80d723bde696093f.tar.gz nvimrc-de95ea87833f04611329339c80d723bde696093f.zip |
feat(macro/event): use lua API
* fixes #5 * datwaft/nvim.conf 1067340
Diffstat (limited to 'fnl/nvrc/macro')
-rw-r--r-- | fnl/nvrc/macro/event.fnl | 116 |
1 files changed, 71 insertions, 45 deletions
diff --git a/fnl/nvrc/macro/event.fnl b/fnl/nvrc/macro/event.fnl index 0727969..f7a808d 100644 --- a/fnl/nvrc/macro/event.fnl +++ b/fnl/nvrc/macro/event.fnl @@ -15,49 +15,75 @@ (accumulate [is? false _ v (ipairs xs) :until is?] (= v x))) +(fn nil? [x] + (= nil x)) + +(fn str? [x] + (= :string (type x))) + +(fn tbl? [x] + (= :table (type x))) + +(fn empty? [xs] + (= 0 (length xs))) + +(lambda seta! [?group events pattern ...] + (match [...] + (where [options command ?desc] (tbl? options)) + (do + (assert-compile (or (str? ?group) (sym? ?group) (nil? ?group)) "expected string or symbol or nil for group" ?group) + (assert-compile (or (tbl? events) (str? events) (sym? events)) "expected table or string or symbol for events" events) + (assert-compile (or (tbl? pattern) (str? pattern) (sym? pattern)) "expected table or string or symbol for pattern" pattern) + (assert-compile (tbl? options) "expected table for options" options) + (assert-compile (or (str? command) (list? command) (fn? command) (sym? command)) "expected string or list or function or symbol for command" command) + (assert-compile (or (nil? ?desc) (str? ?desc)) "expected string or nil for description" ?desc) + (let [events (if (and (tbl? events) (not (sym? events))) + (icollect [_ v (ipairs events)] (->str v)) + (->str events)) + pattern (if (and (tbl? pattern) (not (sym? pattern))) + (icollect [_ v (ipairs pattern)] (->str v)) + (->str pattern)) + options (collect [_ v (ipairs options)] (->str v) true) + options (if (= "<buffer>" pattern) + (doto options (tset :buffer 0)) + options) + command (if (and (not (fn? command)) (list? command)) `#,command + command) + desc (if (and (not ?desc) (or (fn? command) (sym? command))) (view command) + ?desc) + options (if desc (doto options (tset :desc desc)) + options) + options (if options.buffer + options + (doto options (tset :pattern pattern))) + options (if ?group + (doto options (tset :group ?group)) + options) + options (if (str? command) + (doto options (tset :command command)) + (doto options (tset :callback command)))] + `(vim.api.nvim_create_autocmd ,events ,options))) + [command ?desc] (seta! ?group events pattern [] command ?desc))) + (lambda ag! [name ...] - "Defines an autocommand group using the vim API." - `(do - (vim.cmd ,(format "augroup %s" name)) - (vim.cmd :autocmd!) - (do - ,...) - (vim.cmd "augroup END"))) - -(lambda agb! [name ...] - "Defines a buffer-local autocommand group using the vim API." - `(do - (vim.cmd ,(format "augroup %s" name)) - (vim.cmd "autocmd! * <buffer>") - (do - ,...) - (vim.cmd "augroup END"))) - -(lambda au! [events pattern ...] - "Defines an autocommand using the vim API." - (let [events (as-> [$ events] (if (sequence? $) $ [$]) - (icollect [_ v (ipairs $)] - (->str v)) (concat $ ",")) - pattern (as-> [$ pattern] (if (sequence? $) $ [$]) - (icollect [_ v (ipairs $)] - (->str v)) (concat $ ",")) - once? (or (includes? [...] `++once) (includes? [...] :++once)) - nested? (or (includes? [...] `++nested) (includes? [...] :++nested)) - command (last [...])] - (if (fn? command) - (let [fsym (gensym-checksum "__" command)] - `(do - (global ,fsym ,command) - (vim.cmd ,(format (if (and once? nested?) - "autocmd %s %s ++once ++nested call %s" once? - "autocmd %s %s ++once call %s" nested? - "autocmd %s %s ++nested call %s" - "autocmd %s %s call %s") - events pattern (vlua fsym))))) - `(vim.cmd ,(format (if (and once? nested?) - "autocmd %s %s ++once ++nested %s" once? - "autocmd %s %s ++once %s" nested? - "autocmd %s %s ++nested %s" "autocmd %s %s %s") - events pattern command))))) - -{: ag! : agb! : au!} + "Defines an autocommand group using the Lua API." + (assert-compile (or (str? name) (sym? name)) "expected string or symbol for name" name) + (let [name (->str name)] + (if (empty? [...]) + `(vim.api.nvim_create_augroup ,name {}) + (let [statements (icollect [_ statement (ipairs [...])] + (do + (assert-compile (list? statement) "expected list for statement") + (let [[_ events pattern options command ?desc] statement] + (seta! name events pattern options command ?desc))))] + `(do + (vim.api.nvim_create_augroup ,name {}) + ,(unpack statements)))))) + +(lambda au! [...] + "Defines an autocommand using the Lua API." + (seta! nil ...)) + + + +{: ag! : au!} |