Is it possible to override/catch all keydown and keyup events during a minor mode?
I would like to do such a thing for several reasons, but mainly to try an alternative input method which I have not found any reasonable way of doing, in which key combinations would produce input, but in which it would be bothersome to re-press keys (it would be simpler to just hold them down if they are used in several combinations in a row).
If your minor mode is at the top of the list of all active minor-modes, its keymap will be queried before all other modes are queried (unless some even higher precedence cases fall into place, cf. "Searching Keymaps" in the Emacs Lisp documentation).
This means that you can at least handle all key events before other modes do, by binding them in your modes' keymap. You can also define a "catch all" key binding that matches every key event that is not otherwise bound in your map:
(define-key my-minor-mode-map [t] 'my-catch-all-command)
However, this will not help you getting information about key releases. Perhaps you'll find it worthwhile checking out Key chord mode for that.
Related
This is a little embarrassing, but recently I happen to bind regular keys on the fly using global-set-key every week or two.
e.g.
I bound the q key to quoted-insert and toggle-read-only today (not on purpose)
I tried global-unset-key, but now pressing the q key gives me q is undefined message (and writes nothing).
Since it isn't the first time this happens to me recently and since I hate losing all the buffers opened in my emacs every time it happens to me, I'm looking for a way to unset a char key without losing the ability to write this char OR to rebind the char to itself (global-set-key not allowing it). Trying to bind to insert-char 71 failed as well
(global-set-key (kbd "q") #'self-insert-command)
This is the default binding for all alphanumeric keys (which of course can be overridden in some major or minor mode).
There isn't a way to reset a key to its default that I'm aware of. If you want to play around with key bindings in a reversible way, probably the best thing to do is make your own minor mode and only bind keys to it (see http://shallowsky.com/blog/linux/editors/emacs-global-key-bindings.html). Then you can easily toggle the mode off, or undefine a key in its map, to get the global binding.
Finally, I use save-visited-files (in melpa) to remember (most of) my buffers. There are more complete solutions like desktop to save and restore state as well.
Searching for a better tool to learn key combination in Emacs
Though, I have used a package "guide-key" before, I like how "which-key" looks and feel.
However, I encountered one problem. In contrast with "guide-key", it can also show the next key combination.
For example,
type "C-x" then there is also "C-f" description.
Is there anyway we can configure "which-key" to do similar things?
You are searching for a better tool to show you what keys you can use to complete a key sequence that you have started (by using one or more prefix keys). You want to also have access to or immediately see additional info about those key completions, e.g., what the resulting key sequence does and what command it is bound to. You might also want to have the tool help you complete the key sequence, to invoke it.
The key completion provided by library Icicles does all of this, and more.
It is available all the time - just hit S-TAB to complete any key sequence. You can also hit S-TAB at the top level, to see all of the possible key sequences in the current context and complete any of them to invoke it.
Each key-sequence completion candidate shows you the remainder of the key sequence, followed by =, followed by the command the sequence is bound to. You can type input to match either or both parts of this candidate, and matching can be as simple or fancy as you like (regexp etc.).
As you type, the set of matching candidates is filtered to correspond. At any time you can also cycle among the current candidates. As you cycle, a description of the current candidate (which is highlighted) is shown in the mode line of the buffer (*Completions*) where they are listed.
If you want more information about the current candidate that this one-liner, just hit C-M-RET to pop up buffer *Help* with the complete documentation of the key sequence and its command.
Hit RET to choose the current candidate to invoke it. Or just hit C-g if you don't want to invoke any key now, but you just wanted info about the currently available keys.
Prefix keys that are available in the current context are also listed as candidates. E.g., prefix key 4 is available as a candidate when you are completing prefix key C-x. You can navigate up, down, and around the entire hierarchy of all key sequences by choosing prefix-key candidates or by choosing the candidate .., which goes back up.
Candidate key sequences are highlighted differently if they are local bindings (e.g. local to the current mode) or if they are prefix keys.
You can sort the candidates in various ways: prefix keys first, local keys first, command-name alphabetical order, and so on.
Given that there are so many major modes which define their own maps for this prefix (e.g. latex, org, term), is there a safe way to move these maps somewhere else, and always have the C-c key free?
I'm hoping there's a convenient way, because I noticed ErgoEmacs does exactly this.
As far as i know, there no easy way, unless you dive deep into emacs innards and create ways to remap every mode's keys on load. (I think Matthew Fidler is actually working on this)
ergoemacs-mode does not remap the C-c. It turns on cua-mode, but cua-mode doesn't remap C-c neither, it just create several clever ways so that key can be used for multiple purposes (e.g. by the speed you press the key, or whether there's a text selection, etc.). All C-c * keys are still there.
see discussion on this same question here
https://plus.google.com/103652929131043355278/posts/Nb4xn4gDB6p
In Emacs everything is about hotkeys. Everybody who creates new shortcuts knows the problem:
Which hotkeys should be used?
Which hotkeys are used by the most popular Emacs extensions (org-mode...) and should be avoided?
Are there reserved hotkeys for "users" that will never be used by extensions?
Which hotkeys should be avoided, if the code should be public? (Some keys like right/left Win are sometimes missing on keyboards, M-TAB will be catched by the windowmanager)
Is there a list of all reserved hotkeys?
Sticking to the reserved C-c <letter> sequences is as close as you'll get to guaranteeing that you won't conflict with any other code (although it's still just a convention; sometimes you'll see people making code available that uses one of those sequences, but you can report those cases as bugs to the author).
I would suggest using some of the C-c <letter> sequences as prefix bindings for grouping related functionality together. For instance, you might use C-c w <key> as the pattern for window-related functionality. That gives you a dramatically larger number of reserved bindings, probably with better mnemonic properties, and of course the subsequent <key> can be anything at all, not just a letter.
That also lets you use C-c <letter> C-h to list everything you've bound under that prefix, which can be convenient.
The GNU Emacs Lisp Reference manual has a detailed page on key binding conventions:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html
The keys C-c <letter> are reserved for the users, so packages should normally not use them (note that "letter" is really a letter of the alphabet, not "any key" or "any character"). But most users just use which ever key they like and when that binding conflicts with some package, they handle the conflict in an ad-hoc way (either by using another key, or by specifically overriding the package's binding).
automate key-binding checks evaluating forms like
(key-binding [(control c) (delete)])
which is emacs key <deletechar>?
Is there any emacs command to check specified character name?
There are two keys mapped to delete-char functions: Delete, and <deletechar>.
Delete key I have remapped, so <deletechar> left, but I can't figure out which one it is.
In Emacs, "delete" and "deletechar" are not specific keyboard keys, they are mapped to specific "key sequences". If you look at the content of the local-function-key-map variable, you may see that "deletechar" is mapped to other "key sequences". Looking at the content of the function-key-map variable will show you what terminal device mapping is in place. The variables key-translation-map and input-decode-map may also have an entry for "deletechar".
If you use both terminal and windowed versions of emacs or you use different terminal types or if you use emacs on different hardware platforms or if you use different versions of emacs, you may find that pressing a keyboard key sometimes generates a "delete" key sequence and sometimes it will generate a "deletechar" key sequence depending on what the specific key sequence mappings are. Therefore, if you have set "delete" to execute one function and "deletechar" to execute another function, the actual function that is executed may vary depending on whether you are running on terminal or windowed emacs, the terminal type, and the version of emacs.
For more info, have a look at the Keymaps for Translating Sequences of Events section of the Emacs manual and the links off the Keymaps section of the Emacs manual.