有没有现成的,可以把一组函数组合成一个函数的包或命令?

比如我想把outline-hide-entryoutline-show-entry 组合成一个函数,然后给这个函数绑定快捷键(或某个hdyra的 key),然后每次按就是轮流调用这两个函数,像hydra wiki里自带的outline的例子,显示和隐藏的分别绑在不同的键上,实在不好记。。而且像这种成对出现的函数,除了outline以外也有不少。不知道是否有包已经实现了类似的功能(org-cycleorg-shifttab也是类似的?)?

找到xahlee大佬的一个相关文章:http://ergoemacs.org/emacs/elisp_toggle_command.html

(defun make-toggle-function (name &rest funs)
  (lexical-let ((function-name name)
                (functions funs))
    (defalias name
      (function
       (lambda ()
         (interactive)
         (let* ((index (mod (1+ (or (get function-name 'index) -1)) (length functions)))
                (next-function (nth index functions)))
           (call-interactively next-function)
           (put function-name 'index index)))))))


(make-toggle-function
 'outline-toggle-subtree
 'outline-hide-subtree 'outline-show-subtree)

(make-toggle-function
 'outline-cycle
 'outline-hide-leaves 'outline-hide-subtree 'outline-show-children 'outline-show-subtree)

一个肤浅的实现。。抛砖引玉。

;; -*- lexical-binding: t; -*-

比lexical-let不知高到哪里去了

1 个赞