Emacs 在hippie expand中模拟dabbrev expand,仅限于匹配缓冲区

Emacs 在hippie expand中模拟dabbrev expand,仅限于匹配缓冲区,emacs,autocomplete,elisp,emacs24,Emacs,Autocomplete,Elisp,Emacs24,如果我使用dabbrev expand进行扩展,Emacs将搜索当前缓冲区,然后搜索具有相同模式的其他缓冲区。这由dabbrev-friend-buffer函数处理,该函数默认设置为dabbrev-same-major-mode-p 这很好,但我想使用嬉皮扩展 (setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers)) 这将从所有缓冲区中提取完成,甚至是与我

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

这很好,但我想使用
嬉皮扩展

(setq hippie-expand-try-functions-list
  '(try-expand-dabbrev
    try-expand-dabbrev-all-buffers))
这将从所有缓冲区中提取完成,甚至是与我当前的主模式不匹配的缓冲区


我如何使用
hippie expand
将dabbrev completion仅来自与当前缓冲区使用相同主模式的缓冲区?

快速而肮脏的解决方案:将函数的源代码
尝试扩展dabbrev all buffers
复制到新位置,重命名它(比如)
尝试将dabbrev all buffers扩展为相同模式
,并将表达式
(缓冲区列表)
替换为表达式:

(remove-if-not (lambda (x) (eq major-mode (with-current-buffer x major-mode)))
               (buffer-list))
(您需要
(require'cl)
以获得
删除(如果不是
),或者根据
mapcar
delq
重新实现它)

然后,当然,在
嬉皮士扩展尝试功能列表中,将
尝试扩展dabbrev all buffers
替换为
尝试扩展dabbrev all buffers same mode


您可以使用C-hf获取
的源代码,并尝试使用C-hf扩展dabbrev all buffers

基于Sean的优秀建议(假设您已安装列表实用程序库):


好主意。由于我不打算使用
尝试扩展dabbrev all buffers
,我可以使用
defadvice
根据谓词生成
(缓冲区列表)
返回缓冲区。这是我的第一个想法,但建议这样一个基本函数对我来说似乎充满危险。我同意它可能出错(希望很少出错)。我已将答案更新为只调用
尝试扩展dabbrev all buffers
,并重新定义
匹配的缓冲区。
(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))))