Multikeys shortcuts in Emacs

馋奶兔 提交于 2019-12-11 07:57:13

问题


I'm using the following solution to quickly open files in Emacs (thanks to lawlist for the code)

The code : OPTION # 2 -- function with options:

(global-set-key (kbd "<f5>") 'lawlist-bookmark)

(defun lawlist-bookmark (choice)
  "Choices for directories and files."
  (interactive "c[D]ired | [v]ocab.org | [g]td.org | [d]iary.org | [n]otes.org")
    (cond
           ((eq choice ?D)
           (dired "/very/long/and/boring/path/which/make/me/use/tab/for/..."))
           ((eq choice ?v)
           (find-file "/Users/HOME/.0.data/vocab.org")
            (message "Opened:  %s" (buffer-name)))
          ((eq choice ?g)
           (find-file "/Users/HOME/.0.data/gtd.org")
            (message "Opened:  %s" (buffer-name)))
          ((eq choice ?d)
           (find-file "/Users/HOME/.0.data/diary.org")
            (message "Opened:  %s" (buffer-name)))
          ((eq choice ?n)
           (find-file "/Users/HOME/.0.data/notes.org")
            (message "Opened:  %s" (buffer-name)))
          (t (message "Quit"))))

It works well. I press F5 and then another key to open my file. However, I have now a lot of shorcuts and I would like to call them by pressing two (or more) keys.

For example, I have a project named "website-kate" which is a folder containing two main files index.html and stylesheet.css. I would like two shortcuts ki (that is to say: press F5 to open shorcut dial and press first k and then i for "kate" and "index") and ks (for "kate" and "stylesheet")

Of course this code doesn't work:

    ((eq choice ?ki)
    (find-file "/home/user/website-kate/index.html")
        (message "Opened:  %s" (buffer-name)))

回答1:


The interactive form using strings can only read a single key, but interactive can also take a form to evaluate instead of a string, so you can implement your own multi-key reading form. For example like this:

(interactive
 (list
  (let ((key (read-key "First key: ")))
    (cond
     ((equal key ?a)
      (message "a pressed"))
     ((equal key ?k)
      (let ((key (read-key "Second key: ")))
        (cond
         ((equal key ?i)
          (message "ki pressed"))
         (t
          (message "I don't know k%c" key)))))))))

This should be easy to extend to your full use case. (Doing it in a way that is easy to configure is slightly harder, though.)

Internally, (interactive "cFoo: ") does simply use read-key, so you're just expanding on the same concept.



来源:https://stackoverflow.com/questions/25408349/multikeys-shortcuts-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!