From 72d448e384249103748ee83b587c45924e4bc44d Mon Sep 17 00:00:00 2001 From: sefidel Date: Thu, 10 Feb 2022 00:24:03 +0900 Subject: Initial commit --- fnl/nvrc/macro/event.fnl | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 fnl/nvrc/macro/event.fnl (limited to 'fnl/nvrc/macro/event.fnl') diff --git a/fnl/nvrc/macro/event.fnl b/fnl/nvrc/macro/event.fnl new file mode 100644 index 0000000..3453431 --- /dev/null +++ b/fnl/nvrc/macro/event.fnl @@ -0,0 +1,63 @@ +(import-macros {: as->} :nvrc.macro.thread) + +(local {: format} string) +(local {: insert : concat} table) + +(local {: fn? : gensym-checksum : vlua} (require :nvrc.macro.toolkit)) + +(fn last [xs] + (. xs (length xs))) + +(fn ->str [x] + (tostring x)) + +(fn includes? [xs x] + (accumulate [is? false _ v (ipairs xs) :until is?] + (= v x))) + +(lambda au! [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 aub! [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 ac! [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))))) + +{: au! : aub! : ac!} -- cgit 1.4.1