Define an emacs command that calls another emacs command (preserving interactive stuff)

泄露秘密 提交于 2019-12-21 22:11:28

问题


How can I define an emacs command X that does something and then calls another emacs command Y and also copying the interactive interface of the command Y too?

I want to define an altenative version of query-replace with temporarilly toggled value of case-fold-search:

(defun alt-query-replace (a b c d e)
  (interactive)
  (let ((case-fold-search (not case-fold-search))
    (query-replace a b c d e)))

This doesn't work. When I call alt-query-replace, it says "wrong number of arguments". I want the interactive interface of alt-query-replace to be the same as query-replace. Do I need to inspect the source code of query-replace or is there a general approach?


回答1:


Use call-interactively:


(defun alt-query-replace ()
  (interactive)
  (let ((case-fold-search (not case-fold-search)))
    (call-interactively 'query-replace)))



回答2:


You may advise the original function, if you want to modify its behavior instead of calling a separate function.

From chapter 17.3 Around-Advice of the GNU Emacs Lisp Reference Manual:

Around-advice lets you “wrap” a Lisp expression “around” the original function definition.

 (defadvice foo (around foo-around)
   "Ignore case in `foo'."
   (let ((case-fold-search t))
     ad-do-it))

In your case, you can write:

(defadvice query-replace (around alt-query-replace (from-string to-string &optional delimited start end))
    (let ((case-fold-search (not case-fold-search)))
      ad-do-it))
(ad-activate 'query-replace)


来源:https://stackoverflow.com/questions/1593366/define-an-emacs-command-that-calls-another-emacs-command-preserving-interactive

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