emacs - 自动创建可重复命令

标签 emacs elisp

已经有 some questions关于可重复的 emacs 命令(即 C-x z [ repeat-command ] 的行为,其中每个后续的 z 重复最后一个命令),但是,没有一个自动解决方案 可以处理非前缀键绑定(bind)(我的术语: C-c p 是带有前缀 C-c 的前缀键绑定(bind),而击键 M-s-+ 另一方面是非前缀键绑定(bind))。

在我的设置中,我绑定(bind)了 M-s-+text-scale-increaseM-s--text-scale-decrease 。最好直接点击 +-用于在初始命令后重复缩放。这可以通过以下 elisp 来实现:

(defvar text-scale-temp-keymap (make-sparse-keymap))
(define-key text-scale-temp-keymap (kbd "+") 'text-scale-increase)
(define-key text-scale-temp-keymap (kbd "-") 'text-scale-decrease)

(defun text-scale-increase-rep (inc)
  (interactive "p")
  (text-scale-increase inc)
  (set-temporary-overlay-map text-scale-temp-keymap t))

(defun text-scale-decrease-rep (inc)
  (interactive "p")
  (text-scale-decrease inc)
  (set-temporary-overlay-map text-scale-temp-keymap t))

(global-set-key (kbd "M-s-+") 'text-scale-increase-rep)
(global-set-key (kbd "M-s--") 'text-scale-decrease-rep)

但是,每次我想创建可重复的键绑定(bind)时都重复此代码是很麻烦且不必要的。我正在寻求一种自动化任务的方法。图片此代码

(make-repeatable-command 'text-scale-increase '(("+" . text-scale-increase) 
                                                ("-" . text-scale-decrease)))

将创建名为 text-scale-increase-rep 的命令和一个名为 text-scale-increase-temporary-map 的覆盖键盘映射用相应的键。

我认为这是可能的,但是如何实现呢?

最佳答案

试试这个:

(defun defrepeatable (alist)
  (lexical-let ((keymap (make-sparse-keymap))
                (func (cdar alist)))
    (mapcar (lambda(x) (define-key keymap (car x) (cdr x))) alist)
    (lambda (arg)
      (interactive "p")
      (funcall func arg)
      (set-temporary-overlay-map keymap t))))

(global-set-key (kbd "C-z")
                (defrepeatable
                    '(("+" . text-scale-increase) 
                      ("-" . text-scale-decrease)))) 

关于emacs - 自动创建可重复命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881847/

相关文章:

emacs - 帮助为 emacs etags 搜索编写 emacs lisp

regex - 提取正则表达式的匹配项?

windows - 在现有的 emacs 框架中打开文件 (Windows)

emacs - 在区域行前面添加给定的字符串

emacs - 在emacs中,cl-flet和cl-letf有什么区别?

emacs - 运行选择和替换

emacs - js2 模式下的高锁损坏

elisp - elisp 中的 cons 和 list 函数调用的区别

emacs - 突出显示第一个不匹配的括号

emacs - 在启动时添加 Hook 到 emacs scrap