2022-11-30 22:27:40 -08:00
|
|
|
;;; macros.fnl - useful macros
|
|
|
|
|
|
|
|
;; Helpful keymaping functions
|
2023-04-21 18:37:24 -07:00
|
|
|
(lambda bind! [modes key cmd ?a1 ?a2]
|
2022-11-30 22:27:40 -08:00
|
|
|
(let [desc (or ?a2 ?a1)
|
|
|
|
buf (if (and ?a1 ?a2) ?a1 ?a2)
|
|
|
|
opts { :noremap true
|
|
|
|
:silent true
|
|
|
|
:buffer buf }]
|
|
|
|
(when desc
|
|
|
|
(tset opts :desc desc))
|
2023-04-21 18:37:24 -07:00
|
|
|
(if (table? modes)
|
|
|
|
(let [output {}]
|
|
|
|
(each [_ mode (ipairs modes)]
|
|
|
|
(table.insert output `(vim.keymap.set ,mode ,key ,cmd ,opts)))
|
|
|
|
output)
|
|
|
|
`(vim.keymap.set ,modes ,key ,cmd ,opts))))
|
2022-11-30 22:27:40 -08:00
|
|
|
|
|
|
|
;; Better autocommands
|
|
|
|
(lambda hook! [hooks ?patterns callback]
|
2022-12-30 21:18:28 -08:00
|
|
|
(let [hook_table (if (= (type hooks) :table)
|
|
|
|
hooks
|
2022-11-30 22:27:40 -08:00
|
|
|
[ hooks ])
|
|
|
|
pattern_table (if (not ?patterns)
|
|
|
|
[]
|
|
|
|
(= (type ?patterns) :table)
|
2022-12-30 21:18:28 -08:00
|
|
|
?patterns
|
2022-11-30 22:27:40 -08:00
|
|
|
[ ?patterns ])]
|
2023-04-21 18:37:24 -07:00
|
|
|
(var group (.. (tostring (math.random 0 1000000000) "-config-hook")))
|
2022-11-30 22:27:40 -08:00
|
|
|
(each [_ hook (ipairs hook_table)]
|
|
|
|
(set group (.. group "-" hook)))
|
|
|
|
(each [_ pattern (ipairs pattern_table)]
|
|
|
|
(set group (.. group "-" pattern)))
|
|
|
|
`(vim.api.nvim_create_autocmd ,hook_table
|
2022-12-30 21:18:28 -08:00
|
|
|
{ :group
|
|
|
|
(vim.api.nvim_create_augroup ,group
|
2022-11-30 22:27:40 -08:00
|
|
|
{ :clear true })
|
2022-12-30 21:18:28 -08:00
|
|
|
:pattern ,pattern_table
|
2022-11-30 22:27:40 -08:00
|
|
|
:callback ,callback })))
|
2023-01-01 16:18:50 -08:00
|
|
|
|
|
|
|
;; Utility functions for use!
|
|
|
|
(lambda create-list-entries [...]
|
|
|
|
(let [output { :active-entry nil
|
|
|
|
:entries {} }]
|
|
|
|
(lambda output.process [self entry]
|
|
|
|
(var result false)
|
|
|
|
(when self.active-entry
|
|
|
|
(if (and (. self :entries self.active-entry :is-first)
|
|
|
|
(not (list? entry)))
|
|
|
|
(do (tset self :entries self.active-entry :data entry)
|
|
|
|
(set self.active-entry nil)
|
|
|
|
(set result true))
|
|
|
|
(= (type entry) :string)
|
|
|
|
(set self.active-entry nil)
|
|
|
|
(do (table.insert (. self :entries self.active-entry :data) entry)
|
|
|
|
(tset self :entries self.active-entry :is-first false)
|
|
|
|
(set result true))))
|
|
|
|
(when (and (= (type entry) :string) (. self :entries entry))
|
|
|
|
(assert-compile (not (. self :entries entry :present))
|
|
|
|
(.. "':" entry "' cannot appear more than once in `use'"))
|
|
|
|
(set self.active-entry entry)
|
|
|
|
(tset self :entries entry :present true)
|
|
|
|
(set result true))
|
|
|
|
result)
|
|
|
|
(lambda output.splice-into [self other]
|
|
|
|
(each [name entry (pairs self.entries)]
|
|
|
|
(when entry.present
|
|
|
|
(tset other name entry.data))))
|
|
|
|
(each [_ val (ipairs [...])]
|
|
|
|
(tset output :entries val { :present false :is-first true :data `(lambda [])}))
|
|
|
|
output))
|
|
|
|
|
2022-11-30 22:27:40 -08:00
|
|
|
;; Nicer macro for use
|
2022-12-31 18:18:49 -08:00
|
|
|
;; :config acts like use-package's :init and :config options,
|
|
|
|
;; taking in the following lisp expressions as its arguments
|
2022-11-30 22:27:40 -08:00
|
|
|
(lambda use! [repo ...]
|
|
|
|
(local output [ repo ])
|
|
|
|
(var last_key nil)
|
2023-01-01 16:18:50 -08:00
|
|
|
(var list_entries (create-list-entries :config :setup
|
|
|
|
:run))
|
2022-11-30 22:27:40 -08:00
|
|
|
(each [_ val (ipairs [...])]
|
2023-01-01 16:18:50 -08:00
|
|
|
(if last_key
|
|
|
|
(do (tset output last_key val)
|
|
|
|
(set last_key nil))
|
|
|
|
(not (list_entries:process val))
|
2022-11-30 22:27:40 -08:00
|
|
|
(set last_key val)))
|
2023-01-01 16:18:50 -08:00
|
|
|
(list_entries:splice-into output)
|
2022-11-30 22:27:40 -08:00
|
|
|
`(use ,output))
|
|
|
|
|
2022-12-31 18:18:49 -08:00
|
|
|
;; Call a plugin's `setup function'
|
2022-11-30 22:27:40 -08:00
|
|
|
(lambda setup! [pkg ...] (local output [ ])
|
|
|
|
(var last_key nil)
|
|
|
|
(each [_ val (ipairs [...])]
|
|
|
|
(if last_key
|
|
|
|
(do (tset output last_key val)
|
|
|
|
(set last_key nil))
|
|
|
|
(set last_key val)))
|
|
|
|
`((. (require ,pkg) :setup) ,output))
|
|
|
|
|
|
|
|
;; Call module function
|
|
|
|
(lambda module-call! [mod func ...]
|
|
|
|
`((. (require ,mod) ,func) ,...))
|
|
|
|
|
2022-12-31 18:18:49 -08:00
|
|
|
;; Return module function
|
2022-11-30 22:27:40 -08:00
|
|
|
(lambda module-fn! [mod func]
|
|
|
|
`(. (require ,mod) ,func))
|
|
|
|
|
2022-12-31 18:18:49 -08:00
|
|
|
;; Basically does what the Emacs function of the same name does.
|
|
|
|
;; Wraps the code inside with a `winsaveview' and `winrestview'
|
|
|
|
(lambda save-excursion! [...]
|
|
|
|
(let [args [...]
|
|
|
|
len (# args)]
|
|
|
|
(tset args len `(let [retvar# ,(. args len)]
|
|
|
|
(vim.fn.winrestview save#)
|
|
|
|
retvar#))
|
|
|
|
`(let [save# (vim.fn.winsaveview)]
|
|
|
|
,(unpack args))))
|
|
|
|
|
2022-12-30 21:18:28 -08:00
|
|
|
{: bind!
|
2022-11-30 22:27:40 -08:00
|
|
|
: hook!
|
|
|
|
: use!
|
|
|
|
: setup!
|
|
|
|
: module-call!
|
2022-12-31 18:18:49 -08:00
|
|
|
: module-fn!
|
|
|
|
: save-excursion!}
|