From 4b4a82dcb6d1fc780cb18f2fa8c5ec1795066bb0 Mon Sep 17 00:00:00 2001 From: aileot Date: Sun, 7 Apr 2024 15:58:37 +0900 Subject: [PATCH] feat: accept `buffer` with no next value in `extra-opts` to set it to `0` (#268) * docs: remove misinserted comment * docs(vimdoc): describe `:buffer` in extra-opts * feat: let `:buffer` with no next value * test(keymap): add spec on :buffer in extra-opts * test(command): apply fnlfmt --- doc/laurel.md | 13 +++++++++++-- fnl/nvim-laurel/macros.fnl | 10 +++++----- test/command_spec.fnl | 4 ++-- test/keymap_spec.fnl | 13 +++++++++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/doc/laurel.md b/doc/laurel.md index 9e0df9f1..2e6e2f99 100644 --- a/doc/laurel.md +++ b/doc/laurel.md @@ -268,6 +268,9 @@ Create or get an augroup, or override an existing augroup. first pattern in string cannot be any of the keys used in `?extra-opts`. - `?extra-opts`: (bare-sequence) Additional option: - ``: Create autocmd to current buffer by itself. + - `buffer`: (number?) Create command in the buffer of the next + value. Without 0 or no following number, create autocmd to current buffer + by itself. - `callback`: (string|function) Set either callback function or Ex command. A callback is interpreted as Lua function by default. To set Ex command, you have three options: @@ -388,7 +391,9 @@ Map `lhs` to `rhs` in `modes`, non-recursively by default. `[:n :o :x]`. - `?extra-opts`: (bare-sequence) Additional option: - ``: Map `lhs` in current buffer by itself. - - `buffer`: Map `lhs` to a buffer of the next value. +- `buffer`: (number?) Map `lhs` to a buffer of the next value. With `0` or + with no following value, create autocmd to current buffer. + - `literal`: Disable `replace_keycodes`, which is automatically enabled when `expr` is set in `extra-opts`. - `remap`: Make the mapping recursive. This is the inverse of the "noremap" @@ -396,6 +401,7 @@ Map `lhs` to `rhs` in `modes`, non-recursively by default. - `wait`: Disable `nowait` _in extra-opts;_ will NOT disable `nowait` _in api-opts_. Useful in wrapper macro which set `nowait` with `&default-opts`. + - `lhs`: (string) Left-hand-side of the mapping. - `rhs`: (string|function) Right-hand-side of the mapping. Set either callback function or Key sequence. A callback is interpreted as Lua function by @@ -916,8 +922,11 @@ Create a user command. - `?extra-opts`: (bare-sequence) Optional command attributes. Additional attributes: + - ``: Create command in current buffer by itself. - - `buffer`: Create command in the buffer of the next value. + - `buffer`: Create command in the buffer of the next value. Without 0 or no + following number, create autocmd to current buffer by itself. + - `name`: (string) Name of the new user command. It must begin with an uppercase letter. - `command`: (string|function) Replacement command. diff --git a/fnl/nvim-laurel/macros.fnl b/fnl/nvim-laurel/macros.fnl index df36383c..03b62a81 100644 --- a/fnl/nvim-laurel/macros.fnl +++ b/fnl/nvim-laurel/macros.fnl @@ -423,7 +423,7 @@ (local autocmd/extra-opt-keys {: :boolean - :buffer [:number] + :buffer [:default 0 :number] :callback [:function] :command [:string] :desc [:string] @@ -481,7 +481,8 @@ extra-opts (if (nil? ?extra-opts) {} (-> ?extra-opts (extra-opts/seq->kv-table autocmd/extra-opt-keys))) - ?bufnr (if extra-opts. 0 extra-opts.buffer) + ?bufnr (if (or extra-opts. (= true extra-opts.buffer)) 0 + extra-opts.buffer) ?pat (or extra-opts.pattern ?pattern)] (set extra-opts.group ?id) (set extra-opts.buffer ?bufnr) @@ -594,7 +595,7 @@ ;; Keymap ///1 (local keymap/extra-opt-keys {: :boolean - :buffer [:number] + :buffer [:default 0 :number] :callback [:function] :desc [:string] :expr :boolean @@ -638,7 +639,6 @@ (sequence? a2) a2) ?extra-opts (when ?seq-extra-opts (-> ?seq-extra-opts - ;;(supplement-extra-opts! keymap/extra-opt-keys) (extra-opts/seq->kv-table keymap/extra-opt-keys))) [extra-opts lhs raw-rhs ?api-opts] (if-not ?extra-opts [{} a1 a2 ?a3] @@ -1108,7 +1108,7 @@ :addr [:string] :bang :boolean :bar :boolean - :buffer [:number] + :buffer [:default 0 :number] :complete [:function :string] :count [:default 0 :number] :desc [:string] diff --git a/test/command_spec.fnl b/test/command_spec.fnl index dc412f1c..7b05ec84 100644 --- a/test/command_spec.fnl +++ b/test/command_spec.fnl @@ -77,8 +77,8 @@ (. :range))))) (it* "can define command with `count` key with its value" (command! [:count 5] :Foo :bar) - (assert.is_same "5" (-> (get-command :Foo) - (. :count)))) + (assert.is_same :5 (-> (get-command :Foo) + (. :count)))) (it* "can define command with `count` key without its value" (command! [:count] :Foo :bar) (let [default-count 0] diff --git a/test/keymap_spec.fnl b/test/keymap_spec.fnl index e2dbb105..3655af20 100644 --- a/test/keymap_spec.fnl +++ b/test/keymap_spec.fnl @@ -151,6 +151,19 @@ (let [{: replace_keycodes} (get-mapargs :n :lhs)] (assert.is_nil replace_keycodes)))) (describe* :extra-opt + (describe* "`buffer`" + (it* "with the next value sets buffer-local keymap to the buffer" + (let [buf (vim.api.nvim_get_current_buf)] + (map! :n [:buffer buf] :lhs :rhs) + (assert.is_same :rhs (buf-get-rhs buf :n :lhs)))) + (it* "with no next value sets buffer-local keymap to current buffer" + (let [buf (vim.api.nvim_get_current_buf)] + (map! :n [:buffer] :lhs :rhs) + (assert.is_same :rhs (buf-get-rhs buf :n :lhs)))) + (it* "with no next value, followed by another extra-opts, sets buffer-local keymap to current buffer" + (let [buf (vim.api.nvim_get_current_buf)] + (map! :n [:buffer :nowait] :lhs :rhs) + (assert.is_same :rhs (buf-get-rhs buf :n :lhs))))) (describe* "`wait`" (it* "disables `nowait` in extra-opts regardless of the order" (map! :n [:nowait] :lhs :rhs)