Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

emacs - Use key as both prefix and command

I'd like to know how to use a key as both a prefix for other keys and a command itself.

  1. I can sorta do this with key-chord.el, by binding the key chords to the commands following the first key but it has several issues

    • Can only be used with alphanumeric keys
    • Isn't real since I have to hit the keys quickly before they timeout
  2. Some packages such as easy-kill and expand-region support this functionality, but they have complex codebases, and my lisp skills aren't spectacular...

How would I manage to do this? I'd really like <menu> to be bound to evil-ex, but I'd like to also bind <menu> as a prefix for all movements (like arrow-keys) that sets the mark like a chordless cua-selection-mode.

Since evil-ex isn't followed by movements and no movement self inserts, this would be a perfect use-case. <menu> is perfect because it's right next to the arrow keys and other motions keys (eg. end, home etc.) and it's unmodified.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand what you want, I'd suggest that it is better to forget about timers and waiting a slight delay (i.e., to distinguish the intention of <menu> as a command from its use as a prefix key).

The approach I recommend, and use quite a bit, is to define a prefix key (in your case, e.g., <menu>), and then put the command that you were thinking of using for <menu> on <menu> <menu>. That's as quick as hitting <menu> once and trying to rely on some tiny delay etc.

And it allows the command you think of as being on <menu> (really it is on <menu> <menu>) to be repeatable.

I typically make such a command repeatable, so that <menu> <menu> <menu> repeats the command once, <menu> <menu> <menu> <menu> repeats it twice, and so on. IOW, I tend to use this trick for commands that I really want to repeat easily, by just holding down a key.

Here's a simple example, from a suggestion I made more generally to emacs-devel@gnu.org back in 2009, HERE. In that mailing-list message, if you scroll down to #9 you will see the proposal to use such keys, #12 shows this same example, and #15 addresses your question directly. The thread title is "have cake will eat,eat cake will have - krazy key koncept kontroversy", and its subject is exactly the question you raised.


;; This function builds a repeatable version of its argument COMMAND.
(defun repeat-command (command)
  "Repeat COMMAND."
 (interactive)
 (let ((repeat-previous-repeated-command  command)
       (last-repeatable-command           'repeat))
   (repeat nil)))

Here is how you could then define `C-x', which is already a prefix
key, as also a repeatable key for an action command, in this case,
`backward-char':

(defun backward-char-repeat ()
  "Like `backward-char', but repeatable even on a prefix key."
  (interactive)
  (repeat-command 'backward-char))

(define-key ctl-x-map "C-x" 'backward-char-repeat)

Now just holding down `C-x' invokes `backward-char' repeatedly - once
you've gotten past the first `C-x' (the prefix).

As I say, I've long used this technique to be able to (a) have "repeating prefix keys" and (b) still have other keys defined on them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...