Skip to content

Commit

Permalink
feat: accept buffer with no next value in extra-opts to set it to…
Browse files Browse the repository at this point in the history
… `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
  • Loading branch information
aileot committed Apr 7, 2024
1 parent 949686a commit 4b4a82d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
13 changes: 11 additions & 2 deletions doc/laurel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
- `<buffer>`: 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:
Expand Down Expand Up @@ -388,14 +391,17 @@ Map `lhs` to `rhs` in `modes`, non-recursively by default.
`[:n :o :x]`.
- `?extra-opts`: (bare-sequence) Additional option:
- `<buffer>`: 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"
option from `nvim_set_keymap()`.
- `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
Expand Down Expand Up @@ -916,8 +922,11 @@ Create a user command.

- `?extra-opts`: (bare-sequence) Optional command attributes.
Additional attributes:

- `<buffer>`: 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.
Expand Down
10 changes: 5 additions & 5 deletions fnl/nvim-laurel/macros.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@

(local autocmd/extra-opt-keys
{:<buffer> :boolean
:buffer [:number]
:buffer [:default 0 :number]
:callback [:function]
:command [:string]
:desc [:string]
Expand Down Expand Up @@ -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.<buffer> 0 extra-opts.buffer)
?bufnr (if (or extra-opts.<buffer> (= true extra-opts.buffer)) 0
extra-opts.buffer)
?pat (or extra-opts.pattern ?pattern)]
(set extra-opts.group ?id)
(set extra-opts.buffer ?bufnr)
Expand Down Expand Up @@ -594,7 +595,7 @@
;; Keymap ///1

(local keymap/extra-opt-keys {:<buffer> :boolean
:buffer [:number]
:buffer [:default 0 :number]
:callback [:function]
:desc [:string]
:expr :boolean
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test/command_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions test/keymap_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4b4a82d

Please sign in to comment.