From d37dcfa30ff20aa2d5b0d6c2e43ef5930e72e375 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Sat, 11 Sep 2021 05:11:53 +0300 Subject: [PATCH] New minor mode 'show-paren-local-mode' * lisp/paren.el (show-paren--delete-overlays): New function, extracted from show-paren-mode. (show-paren-local-mode): New minor mode. (show-paren-mode): Update docstring to mention it (bug#29381). * doc/emacs/programs.texi (Matching): Update show-paren-mode section. --- doc/emacs/programs.texi | 3 ++- etc/NEWS | 4 ++++ lisp/paren.el | 32 +++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/doc/emacs/programs.texi b/doc/emacs/programs.texi index 09216c278b1..522c092c3b3 100644 --- a/doc/emacs/programs.texi +++ b/doc/emacs/programs.texi @@ -813,7 +813,8 @@ displayed. The default is 102400. of automatic matching. Whenever point is before an opening delimiter or after a closing delimiter, the delimiter, its matching delimiter, and optionally the text between them are highlighted. To toggle Show -Paren mode, type @kbd{M-x show-paren-mode}. To customize it, type +Paren mode, type @kbd{M-x show-paren-mode}. To toggle it in single +buffer, type @kbd{M-x show-paren-local-mode}. To customize it, type @kbd{M-x customize-group @key{RET} paren-showing}. The customizable options which control the operation of this mode include: diff --git a/etc/NEWS b/etc/NEWS index ea7bb6b10ae..50cf0748b1b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -983,6 +983,10 @@ achieve that. It used to be enabled when Emacs is started in GUI mode but not when started in text mode. The cursor still only actually blinks in GUI frames. +** New minor mode 'show-paren-local-mode'. +It serves as a local counterpart for 'show-paren-mode', allowing you +to toggle it separately in different buffers. + * Changes in Specialized Modes and Packages in Emacs 28.1 diff --git a/lisp/paren.el b/lisp/paren.el index a45a08abd36..708605f7944 100644 --- a/lisp/paren.el +++ b/lisp/paren.el @@ -101,9 +101,11 @@ its position." (define-minor-mode show-paren-mode "Toggle visualization of matching parens (Show Paren mode). -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." +When enabled, any matching parenthesis is highlighted in `show-paren-style' +after `show-paren-delay' seconds of Emacs idle time. + +This is a global minor mode. To toggle the mode in a single buffer, +use `show-paren-local-mode'." :global t :group 'paren-showing ;; Enable or disable the mechanism. ;; First get rid of the old idle timer. @@ -114,8 +116,28 @@ matching parenthesis is highlighted in `show-paren-style' after show-paren-delay t #'show-paren-function)) (unless show-paren-mode - (delete-overlay show-paren--overlay) - (delete-overlay show-paren--overlay-1))) + (show-paren--delete-overlays))) + +(defun show-paren--delete-overlays () + (delete-overlay show-paren--overlay) + (delete-overlay show-paren--overlay-1)) + +;;;###autoload +(define-minor-mode show-paren-local-mode + "Toggle `show-paren-mode' only in this buffer." + :variable (buffer-local-value 'show-paren-mode (current-buffer)) + (cond + ((eq show-paren-mode (default-value 'show-paren-mode)) + (unless show-paren-mode + (show-paren--delete-overlays)) + (kill-local-variable 'show-paren-mode)) + ((not (default-value 'show-paren-mode)) + ;; Locally enabled, but globally disabled. + (show-paren-mode 1) ; Setup the timer. + (setq-default show-paren-mode nil) ; But keep it globally disabled. + ) + (t ;; Locally disabled only. + (show-paren--delete-overlays)))) (defun show-paren--unescaped-p (pos) "Determine whether the paren after POS is unescaped." -- 2.39.5