aboutsummaryrefslogtreecommitdiff
path: root/fnl/nvrc/macro/event.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'fnl/nvrc/macro/event.fnl')
-rw-r--r--fnl/nvrc/macro/event.fnl63
1 files changed, 63 insertions, 0 deletions
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! * <buffer>")
+ (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!}