【问题标题】:Define an emacs command that calls another emacs command (preserving interactive stuff)定义一个调用另一个 emacs 命令的 emacs 命令(保留交互式内容)
【发布时间】:2010-12-08 06:42:54
【问题描述】:

如何定义一个emacs命令X来做某事,然后调用另一个emacs命令Y,同时也复制命令Y的交互界面?

我想用 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)))

这不起作用。当我调用 alt-query-replace 时,它​​显示“参数数量错误”。我希望 alt-query-replace 的交互界面与 query-replace 相同。需要查看query-replace的源码还是有通用的方法?

【问题讨论】:

    标签: emacs command elisp interactive


    【解决方案1】:

    以交互方式使用调用:

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

    【讨论】:

    • 该代码会神奇地将所有参数传递给query-replace,就好像它被直接调用一样?
    【解决方案2】:

    如果您想修改其行为而不是调用单独的函数,您可以建议原始函数。

    来自 GNU Emacs Lisp 参考手册的chapter 17.3 Around-Advice

    Around-advice 让你“包装”一个 Lisp 表达“围绕”原件 函数定义。

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

    在你的情况下,你可以写:

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

    【讨论】:

    • 我注意到人们将 ad-activate 与原始建议分开完成,但您可以在原始行中激活 around alt-query-replace (...) activate
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多