如何仅在 *.el 文件中启用 Show-Paren 模式?
我尝试过以下方法:
(add-hook 'emacs-lisp-mode-hook '(lambda()
(show-paren-mode 1)
))
但是它仍然对所有情况启用 Show-Paren 模式。即使在*scratch*
缓冲区中,我也启用了 Show-Paren 模式。
如前所述,show-paren-mode
是一个全局的次要模式。也就是说,你可以通过以下方式在某些缓冲区上运行它:
(show-paren-mode) ;; activate the needed timer
(setq show-paren-mode ()) ;; The timer will do nothing if this is nil
(defun show-paren-local-mode ()
(interactive)
(make-local-variable 'show-paren-mode) ;; The value of shom-paren-mode will be local to this buffer.
(setq show-paren-mode t))
(add-hook 'emacs-lisp-mode-hook 'show-paren-local-mode)
这个还没有经过测试,可能无法正常工作。看文档上似乎可以,但是看代码上可能会出问题。这只有在某些版本的show-paren-mode中才能起作用。
show-paren-mode
是一个全局的小模式,它的作用直接体现在名字上了。这是有意设计的,因为大多数人(包括我自己)都会发现这个小模式在所有缓冲区中都非常有帮助。你为什么要禁用它呢?show-paren-delay
秒后,任何匹配的括号都将以show-paren-style
的方式进行高亮显示。你的代码是正确的。然而,你应该考虑到 *scratch*
缓冲区的主要模式是 lisp-interaction-mode
,它派生自 emacs-lisp-mode
(这基本上是无关紧要的),以及该模式的定义:
(define-minor-mode show-paren-mode
"Toggle visualization of matching parens (Show Paren mode).
With a prefix argument ARG, enable Show Paren mode if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or nil.
Show Paren mode is a global minor mode. When enabled, any
matching parenthesis is highlighted in `show-paren-style' after
`show-paren-delay' seconds of Emacs idle time."
:global t :group 'paren-showing
...)
:global t
是关键所在 - 这个模式是全局的,无论缓冲区的主模式如何,它都会被启用。
我认为你可以使用
(setq-default show-paren-data-function #'ignore)
(show-paren-mode)
正式启用该模式,但保持安静。然后类似于
(defun set-up-emacs-lisp-mode ()
(setq-local show-paren-data-function #'show-paren--default))
(add-hook 'emacs-lisp-mode-hook #'set-up-emacs-lisp-mode)
为了在Emacs Lisp缓冲区中启用它。我还没有测试过这个设置,只是相反的(通常启用,在文本模式下禁用)。
我曾经使用(setq-local show-paren-mode nil)
,但这会使Emacs在Ido提示中突出显示大括号,所以我更喜欢(setq-default show-paren-data-function #'ignore)
。