Magicode logo
Magicode
0
2 min read

Neovim Autocmdを便利に書くFennelマクロ

autocommandを楽にかけるFennelマクロを紹介します.

たくさんautocommandを書く人におすすめです.

使用例

Quick fixを閉じる

(au! :grep-cmd :QuickFixCmdPost :cwindow {:pattern :*grep*})

カーソルの場所を覚えておく

;; remind cursor position
(au! :restore-position 
     :BufReadPost 
     (when (and (> (vim.fn.line "'\"") 1)
                (<= (vim.fn.line "'\"") (vim.fn.line "$")))
       (vim.cmd "normal! g'\"")))

非同期に実行する

(au! :reload-copilot
     :VimEnter
     (async-do! (vim.cmd "Copilot restart")))

マクロ

(local M {})

(fn _callback [group event body ?opt]
  `(let [opt# {:callback (λ [] ,body)
               :group (vim.api.nvim_create_augroup ,group {:clear true})}]
     (each [k# v# (pairs (or ,?opt {}))]
       (tset opt# k# v#))
     (vim.api.nvim_create_autocmd ,event opt#)))


(fn _command [group event command ?opt]
  `(let [opt# {:command ,command
               :group (vim.api.nvim_create_augroup ,group {:clear true})}]
     (each [k# v# (pairs (or ,?opt {}))]
       (tset opt# k# v#))
     (vim.api.nvim_create_autocmd ,event opt#)))

(fn M.au! [group event body ?opt]
  (if
    (= (type body) :table) (_callback group event body ?opt)
    (= (type body) :string) (_command group event body ?opt)
    (assert-compile false "au: body must be a sequence or string" body)))

(fn M.async-do! [body]
  `(do
     (var async# nil)
     (set async#
          (vim.loop.new_async
            (vim.schedule_wrap
                  (λ []
                    ,body
                    (async#:close)))))
     (async#:send)))

M

Discussion

コメントにはログインが必要です。