如果我使用dabbrev-expand
进行扩展,Emacs 会搜索当前缓冲区,然后搜索具有相同模式的其他缓冲区。这由默认设置为dabbrev--same-major-mode-p
的dabbrev-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
,或者根据mapcar
和delq
重新实现它。
然后,当然,在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))))