Skip to content

Commit 79d4834

Browse files
authored
Merge pull request #139 from aileot/feat-keymap-remap_option
feat(keymap): detect `remap` option in `extra-opts`
2 parents 04b4048 + 4a51f82 commit 79d4834

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

doc/MACROS.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ For example,
5555

5656
### api-opts
5757

58-
It is kv-table `{}` option for the api functions, `vim.api.nvim_foo`. Unless
58+
It is kv-table `{}` option for the api functions, `vim.api.nvim_foo()`. Unless
5959
otherwise noted, this option has the following features:
6060

6161
- It only accepts the same key/value described in `api.txt`.
@@ -128,9 +128,9 @@ Create or get an augroup, or override an existing augroup.
128128

129129
Note: Set `vim.fn.foobar`, or set `:foobar` to `callback` with `<command>` key
130130
in `extra-opts`, to call Vim script function `foobar` without table arg from
131-
`nvim_create_autocmd`; instead, set `#(vim.fn.foobar $)` to call `foobar` with
132-
the table arg.
133-
- [`?api-opts`](#api-opts): (kv-table) `:h nvim_create_autocmd`.
131+
`nvim_create_autocmd()`; instead, set `#(vim.fn.foobar $)` to call `foobar`
132+
with the table arg.
133+
- [`?api-opts`](#api-opts): (kv-table) `:h nvim_create_autocmd()`.
134134

135135
```fennel
136136
(augroup! :sample-augroup
@@ -215,14 +215,14 @@ c.f. [`augroup!`](#augroup), [`autocmd!`](#autocmd)
215215
Create an autocmd.
216216

217217
```fennel
218-
(autocmd! events api-opts) ; Just as an alias of `nvim_create_autocmd`.
218+
(autocmd! events api-opts) ; Just as an alias of `nvim_create_autocmd()`.
219219
(autocmd! name-or-id events ?pattern ?extra-opts callback ?api-opts)
220220
```
221221

222222
- `name-or-id`: (string|integer|nil) The autocmd group name or id to match
223-
against. It is necessary unlike `vim.api.nvim_create_autocmd` unless this
224-
`autocmd!` macro is within either `augroup!` or `augroup+`. Set it to `nil` to
225-
define `autocmd`s affiliated with no augroup.
223+
against. It is necessary unlike `nvim_create_autocmd()` unless this `autocmd!`
224+
macro is within either `augroup!` or `augroup+`. Set it to `nil` to define
225+
`autocmd`s affiliated with no augroup.
226226

227227
See [`augroup!`](#augroup) for the rest.
228228

@@ -505,7 +505,7 @@ Aliases of [`setglobal!`][setglobal], [`setglobal+`][setglobal], and so on.
505505

506506
#### `bo!`
507507

508-
Set a buffer option value. `:h nvim_buf_set_option`.
508+
Set a buffer option value. `:h nvim_buf_set_option()`.
509509

510510
```fennel
511511
(bo! ?id name value)
@@ -537,7 +537,7 @@ call setbufvar(10, '&buftype', 'nofile')
537537

538538
#### `wo!`
539539

540-
Set a window option value. `:h nvim_win_set_option`.
540+
Set a window option value. `:h nvim_win_set_option()`.
541541

542542
```fennel
543543
(wo! ?id name value)
@@ -609,6 +609,8 @@ Map `lhs` to `rhs` in `modes` recursively.
609609
- `modes`: (string|string[]) Mode short-name (map command prefix: "n", "i", "v",
610610
"x", …) or "!" for `:map!`, or empty string for `:map`.
611611
- [`?extra-opts`](#extra-opts): (bare-sequence) Additional option:
612+
- `remap`: Make the mapping recursive. This is the inverse of the "noremap"
613+
option from `nvim_set_keymap()`.
612614
- `literal`: Disable `replace_keycodes`, which is automatically enabled when
613615
`expr` is set in `extra-opts`.
614616
- `<buffer>`: Map `lhs` in current buffer by itself.
@@ -624,7 +626,7 @@ Map `lhs` to `rhs` in `modes` recursively.
624626
as Lua function; otherwise, as Normal mode command execution.
625627

626628
Note: Insert `<command>` key in `extra-opts` to set string via symbol.
627-
- [`?api-opts`](#api-opts): (kv-table) `:h nvim_set_keymap`.
629+
- [`?api-opts`](#api-opts): (kv-table) `:h nvim_set_keymap()`.
628630

629631
#### `noremap!`
630632

@@ -970,7 +972,7 @@ Create a user command.
970972
letter.
971973
- `command`: (string|function) Replacement command.
972974
- [`?api-opts`](#api-opts): (kv-table) Optional command attributes. The same as
973-
`opts` for `nvim_create_user_command`.
975+
`opts` for `nvim_create_user_command()`.
974976

975977
```fennel
976978
(command! :SayHello
@@ -1037,7 +1039,7 @@ Set a highlight group.
10371039

10381040
- `?ns-id`: (number) Namespace id for this highlight `nvim_create_namespace()`.
10391041
- `name`: (string) Highlight group name, e.g., "ErrorMsg".
1040-
- `val`: (kv-table) Highlight definition map. `:h nvim_set_hl`. As long as the
1042+
- `val`: (kv-table) Highlight definition map. `:h nvim_set_hl()`. As long as the
10411043
keys are bare-strings, `cterm` attribute map can contain `fg`/`bg` instead of
10421044
`ctermfg`/`ctermbg` key.
10431045

@@ -1081,7 +1083,7 @@ It could be an unexpected behavior that `autocmd` whose callback ends with
10811083

10821084
- Fennel `list` returns the last value.
10831085
- `pcall` returns `true` when the call succeeds without errors.
1084-
- `nvim_create_autocmd` deletes itself when its callback function returns
1086+
- `nvim_create_autocmd()` deletes itself when its callback function returns
10851087
`true`.
10861088

10871089
##### Anti-Pattern
@@ -1113,8 +1115,8 @@ It could be an unexpected behavior that `autocmd` whose callback ends with
11131115
#### Nested anonymous function in callback
11141116

11151117
`$` in the outermost hash function represents the single table argument from
1116-
`nvim_create_autocmd`; on the other hand, `$` in any hash functions included in
1117-
another anonymous function is meaningless in many cases.
1118+
`nvim_create_autocmd()`; on the other hand, `$` in any hash functions included
1119+
in another anonymous function is meaningless in many cases.
11181120

11191121
##### Anti-Pattern
11201122

fnl/nvim-laurel/macros.fnl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,12 @@
211211
(lambda option/modify [api-opts name ?val ?flag]
212212
(let [name (if (str? name) (name:lower) name)
213213
interface (match api-opts
214-
{:scope nil :buf nil :win nil}
215-
`vim.opt
216-
{:scope :local}
217-
`vim.opt_local
218-
{:scope :global}
219-
`vim.opt_global
220-
{: buf :win nil}
221-
(if (= 0 buf) `vim.bo `(. vim.bo ,buf))
222-
{: win :buf nil}
223-
(if (= 0 win) `vim.wo `(. vim.wo ,win))
224-
_
225-
(error* (.. "invalid api-opts: " (view api-opts))))
214+
{:scope nil :buf nil :win nil} `vim.opt
215+
{:scope :local} `vim.opt_local
216+
{:scope :global} `vim.opt_global
217+
{: buf :win nil} (if (= 0 buf) `vim.bo `(. vim.bo ,buf))
218+
{: win :buf nil} (if (= 0 win) `vim.wo `(. vim.wo ,win))
219+
_ (error* (.. "invalid api-opts: " (view api-opts))))
226220
opt-obj `(. ,interface ,name)
227221
?val (if (and (contains? [:formatoptions :shortmess] name)
228222
;; Convert sequence of table values into a sequence of
@@ -528,6 +522,7 @@
528522
:<command>
529523
:cb
530524
:<callback>
525+
:remap
531526
:noremap
532527
:nowait
533528
:silent
@@ -596,10 +591,12 @@
596591
@param ?api-opts kv-table"
597592
(when (and extra-opts.expr (not= false extra-opts.replace_keycodes))
598593
(set extra-opts.replace_keycodes (if extra-opts.literal false true)))
599-
(when (and extra-opts.callback (not extra-opts.expr)
600-
(or (nil? ?api-opts)
601-
(and (not ?api-opts.expr)
602-
(not (hidden-in-compile-time? ?api-opts)))))
594+
(when (or extra-opts.remap
595+
(and extra-opts.callback (not extra-opts.expr)
596+
(or (nil? ?api-opts)
597+
(and (not ?api-opts.expr)
598+
(not (hidden-in-compile-time? ?api-opts))))))
599+
(set extra-opts.remap nil)
603600
(set extra-opts.noremap nil))
604601
(let [?bufnr extra-opts.buffer
605602
api-opts (merge-api-opts (keymap/->compatible-opts! extra-opts)

tests/spec/keymap_spec.fnl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@
5959
#(let [modes [:n :t :o]]
6060
(noremap! modes :lhs :rhs)
6161
(each [_ mode (ipairs modes)]
62-
(assert.is.same :rhs (get-rhs mode :lhs)))))))
62+
(assert.is.same :rhs (get-rhs mode :lhs)))))
63+
(it "maps recursively with `remap` key in `extra-opts`"
64+
#(let [modes [:n :o :x]]
65+
(noremap! modes [:remap] :lhs :rhs)
66+
(each [_ m (ipairs modes)]
67+
(let [{: noremap} (get-mapargs m :lhs)]
68+
(assert.is.same 0 noremap)))))))
6369
(describe :map!
6470
(fn []
6571
(it "maps lhs to rhs with `noremap` set to `false` represented by `1`"

0 commit comments

Comments
 (0)