about summary refs log tree commit diff
path: root/fnl/nvrc/macro/event.fnl
blob: f7a808d91ec7f0cbd6db2c059167572195a5595e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(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)))

(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 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!}