如何仅对 *.el 文件启用 Show-Paren 模式

问题描述 投票:0回答:5

如何仅对 *.el 文件启用 Show-Paren 模式?

我已经尝试过了

(add-hook 'emacs-lisp-mode-hook '(lambda()
                                   (show-paren-mode 1)
                                   ))

但它仍然对所有情况启用 Show-Paren 模式。即使在

*scratch*
缓冲区中,我也启用了 Show-Paren 模式。

emacs elisp
5个回答
9
投票

如前所述,

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。


4
投票

show-paren-mode
是全局小模式。 这就是它听起来的意思。 这很大程度上是设计使然,因为大多数人(包括我自己)都发现这一点 次要模式对所有缓冲区都有帮助。 为什么要禁用它 文件?

来自文档

Show Paren 模式是全局次要模式。 启用后,任何 匹配的括号会在 Emacs 空闲时间的

show-paren-style' after
  
show-paren-delay' 秒内突出显示。


1
投票

您的代码是正确的。但是,您应该考虑这样一个事实:

*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
是这里的关键 - 该模式是全局的,并且在所有缓冲区中启用,无论其主要模式如何。


0
投票

我想你可以用

(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)


0
投票

M-x
自定义组
RET
括号显示

在搜索字符串中输入:show-paren-predicate 并点击

RET

默认情况下,该谓词的 Lisp 表达式为

(不是(派生模式。特殊模式))

要使 show-paren-mode 仅适用于 elisp 文件,您可以将 show-paren-predicate 设置为

(主模式.emacs-lisp-模式)

那就

C-x
C-s

© www.soinside.com 2019 - 2024. All rights reserved.