2013-06-19 5 views
11

はのは、次のように私は特定の機能にキーをバインドするとしましょう:簡単な繰り返しのためにemacsにキーバインディングを書き込む方法は?

(global-set-key (kbd "C-c =") 'function-foo) 

さて、私はキーとして機能するように結合したい:私は繰り返したい場合、私は、初めてC-c =を押した後
function-fooの場合は、C-cを再度押す必要はありませんが、単に=を押すだけです。その後、function-fooを十分な時間だけ呼び出すと、=以外のキーを押すか、明示的にC-gを押して終了することができます。

これを行う方法?

+1

あなたは 'repeat'コマンドに精通していますか?これは 'C-x z'に束縛されており、前のコマンドを繰り返すために使うことができます。 'z'を押すたびにそのコマンドを繰り返します。 – mk1

+1

@ mk1私はC-x zを知っています。私は自分のキーバインディングをそのように動作させることができるのだろうと思っています...とにかく、あなたのコメントのおかげで – shelper

+0

キーボードマクロを実行するための 'C-x e'は、そのバインディングの実装がelispのどこかにある場合、それはあなた自身のバインディングを書くことの始まりかもしれません。 – pcurry

答えて

12

これは、あなたが探しているものになることがあります。

(defun function-foo() 
    (interactive) 
    (do-your-thing) 
    (set-temporary-overlay-map 
    (let ((map (make-sparse-keymap))) 
     (define-key map (kbd "=") 'function-foo) 
     map))) 
1

function-fooset-temporary-overlay-mapを使用します。

7

あなたが必要とする正確に何をしsmartrep.elパッケージがあります。ドキュメントは少し不足していますが、githubにある多数のemacs configsを調べることで、どのように使用されるべきかを把握することができます。たとえば(hereから取得):

(require 'smartrep) 
(smartrep-define-key 
    global-map "C-q" '(("n" . (scroll-other-window 1)) 
         ("p" . (scroll-other-window -1)) 
         ("N" . 'scroll-other-window) 
         ("P" . (scroll-other-window '-)) 
         ("a" . (beginning-of-buffer-other-window 0)) 
         ("e" . (end-of-buffer-other-window 0)))) 
+0

明示的にどこにも記述されていないようですが、 'C-g 'がリピートモードを終了することがわかりました。 –

4

これは私が使用するものです。あなたが繰り返しキーを指定する必要がないので、私はそれが好きです。 set-temporary-overlay-mapを使用する、提案するもの@juanleonに加えて

(require 'repeat) 
(defun make-repeatable-command (cmd) 
    "Returns a new command that is a repeatable version of CMD. 
The new command is named CMD-repeat. CMD should be a quoted 
command. 

This allows you to bind the command to a compound keystroke and 
repeat it with just the final key. For example: 

    (global-set-key (kbd \"C-c a\") (make-repeatable-command 'foo)) 

will create a new command called foo-repeat. Typing C-c a will 
just invoke foo. Typing C-c a a a will invoke foo three times, 
and so on." 
    (fset (intern (concat (symbol-name cmd) "-repeat")) 
     `(lambda ,(help-function-arglist cmd) ;; arg list 
      ,(format "A repeatable version of `%s'." (symbol-name cmd)) ;; doc string 
      ,(interactive-form cmd) ;; interactive form 
      ;; see also repeat-message-function 
      (setq last-repeatable-command ',cmd) 
      (repeat nil))) 
    (intern (concat (symbol-name cmd) "-repeat"))) 
+1

私はこれが大好きですが、CMD *が既に読み込まれていなければならないことに注意してください(自動読み込みが不十分です)。そうしないと、arglistと対話形式の質問が失敗します。 (実際は後者が自動ロードをトリガしますが、先行するarglistは間違っています)。 – phils

0

、ここで私はかなりのビットを使用し、代替です。標準ライブラリrepeat.elを使用しています。

;; 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))) 

これを使用して、異なる繰り返し可能なコマンドを定義します。例えば、

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

はその後、反復可能な接尾辞を持つキーに、このようなコマンドをバインドたとえば、C-c =C-c = = = =用...)

は、より多くの情報のためthis SO postを参照してください。

関連する問題