From de95ea87833f04611329339c80d723bde696093f Mon Sep 17 00:00:00 2001 From: sefidel Date: Fri, 11 Mar 2022 07:49:36 +0900 Subject: feat(macro/event): use lua API * fixes #5 * datwaft/nvim.conf 1067340 --- fnl/nvrc/events.fnl | 16 +++---- fnl/nvrc/macro/event.fnl | 116 +++++++++++++++++++++++++++++------------------ 2 files changed, 79 insertions(+), 53 deletions(-) (limited to 'fnl') diff --git a/fnl/nvrc/events.fnl b/fnl/nvrc/events.fnl index c5b4d8e..545294d 100644 --- a/fnl/nvrc/events.fnl +++ b/fnl/nvrc/events.fnl @@ -11,11 +11,11 @@ ; Restore cursor style to beam on exit (ag! restore-cursor - (au! VimLeave * #(set! guicursor ["a:ver75-blinkon0"]))) + (au! VimLeave * (set! guicursor ["a:ver75-blinkon0"]))) ; Restore the last cursor line (ag! restore-last-cursor-line - (au! BufReadPost * #(if (and (> (line "'\"") 1) + (au! BufReadPost * (if (and (> (line "'\"") 1) (<= (line "'\"") (line "$"))) (cmd! "normal! g'\"")))) @@ -26,22 +26,22 @@ ;; Read file when it changes on disk (ag! read-file-on-disk-change (au! [FocusGained BufEnter CursorHold CursorHoldI] * - #(if (and (not= :c (mode)) + (if (and (not= :c (mode)) (not (bufexists? "[Command Line]"))) (cmd! "checktime"))) (au! FileChangedShellPost * - #(echo! "File changed on disk. Buffer reloaded."))) + (echo! "File changed on disk. Buffer reloaded."))) (ag! terminal-options ;; Enter Terminal-mode (insert) agtomatically (au! TermOpen * "startinsert") ;; Disables line number on terminal buffers - (au! TermOpen * #(do + (au! TermOpen * (do (setl! nonumber) (setl! norelativenumber))) ;; Disables spell on terminal buffers - (au! TermOpen * #(setl! nospell)) + (au! TermOpen * (setl! nospell)) ;; Disables sign column on terminal buffers - (au! TermOpen * #(setl! signcolumn :no)) + (au! TermOpen * (setl! signcolumn :no)) ;; Disables colorcolumn on terminal buffers - (au! TermOpen * #(setl! colorcolumn []))) + (au! TermOpen * (setl! colorcolumn []))) 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 (= "" 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! * ") - (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!} -- cgit 1.4.1