diff options
| author | Justin Burkett | 2021-06-22 11:36:27 -0400 |
|---|---|---|
| committer | Justin Burkett | 2021-06-22 11:36:27 -0400 |
| commit | cd0c48cda2e7cc1d3bc93d3611e267a7d022de8a (patch) | |
| tree | e74fce2329650203cb1de0f43f3cadaf24bcca40 | |
| parent | eb5a2e3de16dca7286a323bad62b55d3c08349e0 (diff) | |
| download | emacs-cd0c48cda2e7cc1d3bc93d3611e267a7d022de8a.tar.gz emacs-cd0c48cda2e7cc1d3bc93d3611e267a7d022de8a.zip | |
Clarify usage of keymap replacements in docstrings and README
| -rw-r--r-- | README.org | 43 | ||||
| -rw-r--r-- | which-key.el | 28 |
2 files changed, 32 insertions, 39 deletions
diff --git a/README.org b/README.org index 2280f704acf..37c742edcce 100644 --- a/README.org +++ b/README.org | |||
| @@ -271,37 +271,38 @@ | |||
| 271 | **** Keymap-based replacement | 271 | **** Keymap-based replacement |
| 272 | Using this method, which-key can display a custom string for a key | 272 | Using this method, which-key can display a custom string for a key |
| 273 | definition in some keymap. There are two ways to define a keymap-based | 273 | definition in some keymap. There are two ways to define a keymap-based |
| 274 | replacement. The first is to use | 274 | replacement. The preferred way is to use =define-key= (or a command that |
| 275 | =which-key-add-keymap-based-replacements=. The statement | 275 | uses =define-key= internally) with a cons cell as the definition. For |
| 276 | example, | ||
| 277 | |||
| 278 | #+BEGIN_SRC emacs-lisp | ||
| 279 | (define-key some-map "f" '("foo" . command-foo)) | ||
| 280 | (define-key some-map "b" '("bar-prefix" . (keymap))) | ||
| 281 | #+END_SRC | ||
| 282 | |||
| 283 | binds =command-foo= to =f= in =some-map=, but also stores the string "foo" | ||
| 284 | which which-key will extract to use to describe this command. The second | ||
| 285 | example binds an empty keymap to =b= in =some-map= and uses "bar-prefix" to | ||
| 286 | describe it. These bindings are accepted by =define-key= natively (i.e., | ||
| 287 | with or without which-key being loaded). Since many key-binding utilities | ||
| 288 | use =define-key= internally, this functionality should be available with | ||
| 289 | your favorite method of defining keys as well. | ||
| 290 | |||
| 291 | The second method is to use =which-key-add-keymap-based-replacements=. The | ||
| 292 | statement | ||
| 276 | 293 | ||
| 277 | #+BEGIN_SRC emacs-lisp | 294 | #+BEGIN_SRC emacs-lisp |
| 278 | (define-key some-map "f" 'long-command-name-foo) | 295 | (define-key some-map "f" 'long-command-name-foo) |
| 279 | (define-key some-map "b" some-prefix-map) | 296 | (define-key some-map "b" some-prefix-map) |
| 280 | (which-key-add-keymap-based-replacements some-map | 297 | (which-key-add-keymap-based-replacements some-map |
| 281 | "f" '("foo" . long-command-name-foo) | 298 | "f" '("foo" . long-command-name-foo) |
| 282 | ;; or | 299 | "b" '("bar-prefix" . (keymap))) |
| 283 | ;; "f" "foo" (see the docstring) | ||
| 284 | "b" '("bar-prefix" . (keymap)) | ||
| 285 | ;; or | ||
| 286 | ;; "b" "bar-prefix" (see the docstring) | ||
| 287 | ) | ||
| 288 | #+END_SRC | 300 | #+END_SRC |
| 289 | 301 | ||
| 290 | uses =define-key= to add two bindings and tells which-key to use the string | 302 | uses =define-key= to add two bindings and tells which-key to use the string |
| 291 | "foo" in place of "command-foo" and the string "bar-prefix" for an empty | 303 | "foo" in place of "command-foo" and the string "bar-prefix" for an empty |
| 292 | prefix map. =which-key-add-keymap-based-replacements= uses =define-key= to | 304 | prefix map. =which-key-add-keymap-based-replacements= just uses |
| 293 | bind (or rebind) the command, and you may also use =define-key= directly as | 305 | =define-key= to bind (or rebind) the command. |
| 294 | follows. | ||
| 295 | |||
| 296 | #+BEGIN_SRC emacs-lisp | ||
| 297 | (define-key some-map "f" '("foo" . command-foo)) | ||
| 298 | (define-key some-map "b" '("bar-prefix" . (keymap))) | ||
| 299 | #+END_SRC | ||
| 300 | |||
| 301 | Here =define-key= uses the natively supported =(NAME . COMMAND)= notation | ||
| 302 | to simultaneously define a command and give that command a name. Since many | ||
| 303 | key-binding utilities use =define-key= internally, this functionality | ||
| 304 | should be available with your favorite method of defining keys as well. | ||
| 305 | 306 | ||
| 306 | There are other methods of telling which-key to replace command names, | 307 | There are other methods of telling which-key to replace command names, |
| 307 | which are described next. The keymap-based replacements should be the most | 308 | which are described next. The keymap-based replacements should be the most |
diff --git a/which-key.el b/which-key.el index 3a0ce9753fe..8598fa666e1 100644 --- a/which-key.el +++ b/which-key.el | |||
| @@ -895,27 +895,19 @@ but more functional." | |||
| 895 | ;;;###autoload | 895 | ;;;###autoload |
| 896 | (defun which-key-add-keymap-based-replacements (keymap key replacement &rest more) | 896 | (defun which-key-add-keymap-based-replacements (keymap key replacement &rest more) |
| 897 | "Replace the description of KEY using REPLACEMENT in KEYMAP. | 897 | "Replace the description of KEY using REPLACEMENT in KEYMAP. |
| 898 | KEY should take a format suitable for use in | 898 | KEY should take a format suitable for use in `kbd'. REPLACEMENT |
| 899 | `kbd'. REPLACEMENT is the string to use to describe the | 899 | should be a cons cell of the form \(STRING . COMMAND\) for each |
| 900 | command associated with KEY in the KEYMAP. You may also use a | 900 | REPLACEMENT, where STRING is the replacement string and COMMAND |
| 901 | cons cell of the form \(STRING . COMMAND\) for each REPLACEMENT, | 901 | is a symbol corresponding to the intended command to be |
| 902 | where STRING is the replacement string and COMMAND is a symbol | 902 | replaced. COMMAND can be nil if the binding corresponds to a key |
| 903 | corresponding to the intended command to be replaced. In the | 903 | prefix. An example is |
| 904 | latter case, which-key will verify the intended command before | ||
| 905 | performing the replacement. COMMAND should be nil if the binding | ||
| 906 | corresponds to a key prefix. For example, | ||
| 907 | 904 | ||
| 908 | \(which-key-add-keymap-based-replacements global-map | 905 | \(which-key-add-keymap-based-replacements global-map |
| 909 | \"C-x w\" \"Save as\"\) | 906 | \"C-x w\" '\(\"Save as\" . write-file\)\). |
| 910 | 907 | ||
| 911 | and | 908 | For backwards compatibility, REPLACEMENT can also be a string, |
| 912 | 909 | but the above format is preferred, and the option to use a string | |
| 913 | \(which-key-add-keymap-based-replacements global-map | 910 | for REPLACEMENT will eventually be removed." |
| 914 | \"C-x w\" '\(\"Save as\" . write-file\)\) | ||
| 915 | |||
| 916 | both have the same effect for the \"C-x C-w\" key binding, but | ||
| 917 | the latter causes which-key to verify that the key sequence is | ||
| 918 | actually bound to write-file before performing the replacement." | ||
| 919 | (while key | 911 | (while key |
| 920 | (cond ((consp replacement) | 912 | (cond ((consp replacement) |
| 921 | (define-key keymap (kbd key) replacement)) | 913 | (define-key keymap (kbd key) replacement)) |