在嬉皮士扩展中模拟 dabbrev-expand,限制为匹配缓冲区



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

这工作正常,但我想使用hippie-expand.

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

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

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

快速而肮脏的解决方案:将函数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源。

基于肖恩的出色建议(假设您安装了 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))))

最新更新