【问题标题】:Emulate dabbrev-expand in hippie-expand, limiting to matching buffers在 hippie-expand 中模拟 dabbrev-expand,限制匹配缓冲区
【发布时间】:2012-12-07 03:04:59
【问题描述】:

如果我使用dabbrev-expand 进行扩展,Emacs 会搜索当前缓冲区,然后搜索具有相同模式的其他缓冲区。这由dabbrev-friend-buffer-function 处理,默认设置为dabbrev--same-major-mode-p

这很好用,但我想使用hippie-expand

(setq hippie-expand-try-functions-list
  '(try-expand-dabbrev
    try-expand-dabbrev-all-buffers))

这会从所有缓冲区中提取完成,甚至是与我当前的主要模式不匹配的缓冲区。

我如何才能使用 hippie-expand 与仅来自使用与当前缓冲区相同主模式的缓冲区的 dabbrev 补全?

【问题讨论】:

    标签: emacs autocomplete elisp emacs24


    【解决方案1】:

    快速而肮脏的解决方案:将函数try-expand-dabbrev-all-buffers的源代码复制到一个新位置,将其重命名(比如)try-expand-dabbrev-all-buffers-same-mode,并将表达式(buffer-list)替换为表达式:

    (remove-if-not (lambda (x) (eq major-mode (with-current-buffer x major-mode)))
                   (buffer-list))
    

    (您需要(require 'cl) 才能获得remove-if-not,或者根据mapcardelq 重新实现它。)

    当然,在hippie-expand-try-functions-list 中将try-expand-dabbrev-all-buffers 替换为try-expand-dabbrev-all-buffers-same-mode

    你可以使用C-hf获取try-expand-dabbrev-all-buffers的来源。

    【讨论】:

    • 好主意。由于我不打算使用try-expand-dabbrev-all-buffers,我可以使用defadvice 根据您的谓词使(buffer-list) 返回缓冲区。
    • 这是我的第一个想法,但建议这样一个基本功能对我来说似乎充满了危险。
    • 我同意它可能会出错(希望很少)。我已经更新了我的答案,只需调用 try-expand-dabbrev-all-buffers 并重新定义 matching-buffers
    【解决方案2】:

    基于 Sean 的出色建议(假设您安装了 dash.el 列表实用程序库):

    (autoload '--filter "dash" nil t)
    
    ;; only consider buffers in the same mode with try-expand-dabbrev-all-buffers
    (defun try-expand-dabbrev-matching-buffers (old)
      (let ((matching-buffers (--filter
                               (eq major-mode (with-current-buffer it major-mode))
                               (buffer-list))))
        (flet ((buffer-list () matching-buffers))
          (try-expand-dabbrev-all-buffers old))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2014-01-28
      • 2016-06-18
      • 2022-12-22
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      相关资源
      最近更新 更多