;;; macros.fnl - useful macros ;; Helpful keymaping functions (lambda bind! [mode key cmd ?a1 ?a2] (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)) `(vim.keymap.set ,mode ,key ,cmd ,opts))) ;; Better autocommands (lambda hook! [hooks ?patterns callback] (let [hook_table (if (= (type hooks) :table) hooks [ hooks ]) pattern_table (if (not ?patterns) [] (= (type ?patterns) :table) ?patterns [ ?patterns ])] (var group "config-hook") (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 { :group (vim.api.nvim_create_augroup ,group { :clear true }) :pattern ,pattern_table :callback ,callback }))) ;; Nicer macro for use ;; (lambda use! [repo ...] ;; (local output [ repo ]) ;; (var last_key nil) ;; (each [_ val (ipairs [...])] ;; (if last_key ;; (do (tset output last_key val) ;; (set last_key nil)) ;; (set last_key val))) ;; `(use ,output)) (lambda use! [repo ...] (local output [ repo ]) (var last_key nil) (var config_mode false) (var has_config false) (var config_entries (list `lambda [])) (each [_ val (ipairs [...])] (if config_mode (if (= (type val) :string) (set config_mode false) (table.insert config_entries val)) last_key (do (tset output last_key val) (set last_key nil)) (= val :config) (do (set config_mode true) (set has_config true)) (set last_key val))) (if has_config (tset output :config config_entries)) `(use ,output)) ;; Can be passed to :config of `use!' ;; to call a plugin's `setup' function (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) ,...)) ;; Return module function (lambda module-fn! [mod func] `(. (require ,mod) ,func)) {: bind! : hook! : use! : setup! : module-call! : module-fn!}