3

I use (show-paren-mode 1) to get help seeing where parentheses are. I have (setq show-paren-delay 2) to prevent intrusive highlighting when I don't need it. This is how it works now:

  1. Put cursor on a parenthesis. After 2 seconds, both parentheses highlight
  2. Use cursor keys. Highlighting stays on as long as I keep cursoring.
  3. Stop moving around. Highlighting eventually disappears, after 2 seconds.

It should work like this:

  1. Same as above.
  2. Use cursor keys. Highlighting disappears immediately!

The rationale is that I need highlighting as an aide to find the matching parenthesis, or to detect imbalance. Once I've established this, the highlighting is no longer useful and I want it gone.

forthrin
  • 451
  • 2
  • 10
  • Please provide a step-by-step recipe to describe what you're seeing and what you want to see instead. – Drew Apr 02 '18 at 14:05
  • @Drew: See updated post. – forthrin Apr 02 '18 at 14:55
  • 1
    Do you see that when you start Emacs using `emacs -Q` (no init file)? I don't. I don't see any such delay. If the cursor is moved away from a paren the highlighting ceases immediately, for me. If you don't see the problem without your init file, bisect the file to find the culprit. – Drew Apr 02 '18 at 15:06
  • @Drew OP already put the settings needed to reproduce: `(setq show-paren-delay 2) (show-paren-mode 1)`. The problem, as far as I can tell, is that `show-paren-delay` applies not only to the paren highlighting (which is what OP wants, I guess), but also to the *unhighlighting*. – npostavs Apr 02 '18 at 15:49
  • @npostavs: Is there a bug or enhancement request here? (If so, OP should `M-x report-emacs-bug`.) If not, is it not OK for OP to just remove that delay customization? – Drew Apr 02 '18 at 15:53
  • @Drew: Rewrote the question to make things clearer. Please advice me how I can configure Emacs to my liking, or if this is a bug / feature request. – forthrin Apr 03 '18 at 06:04
  • Regarding the PS, highlighting for close parens activates when the cursor is *after* the close paren. – npostavs Apr 03 '18 at 13:47
  • @npostavs: This is so non-intuitive that I didn't even notice it. What is the history behind this design? – forthrin Apr 03 '18 at 15:03
  • I don't know the history, but I find it fairly intuitive when the delay is set low (the default), then you see the paren you just typed highlighted along with its match. – npostavs Apr 03 '18 at 22:16
  • @npostavs: Ah! That must be the explanation. However, one would wonder if the behaviour should be different when cursoring over existing parentheses than when typing a new one. I seem to remember that this is how it works in some other editor. Anyway, hope someone can answer the main question, because that one quite puzzles me. – forthrin Apr 04 '18 at 03:12
  • "if the behaviour should be different when cursoring over existing parentheses than when typing a new one" - that sounds horribly confusing. – npostavs Apr 04 '18 at 08:27
  • @npostavs: OK, Sublime Text actually does like Emacs, so I agree! But I noticed it even highlights when the cursor is in *any position between the brackets*. Is this possible in Emacs? – forthrin Apr 04 '18 at 08:51
  • Maybe some of the packages mentioned in https://www.emacswiki.org/emacs/ParenthesesAppearance would be helpful for that. – npostavs Apr 04 '18 at 09:09

1 Answers1

4

It should work like this: [...] Use cursor keys. Highlighting disappears immediately!

I want show-paren-delay only when navigating the code. If I actually type a new parenthesis, I want immediate highlighting.

You can make it work like that with the following code. show-paren-clear-highlight should be added to Emacs since it's accessing internal variables.

(defun show-paren-clear-highlight ()
  "Turn off any previous paren highlighting."
  (delete-overlay show-paren--overlay)
  (delete-overlay show-paren--overlay-1))

;; Instead of relying on `delsel', you could use
;; `before-change-functions' to detect insertions.  That might be more
;; reliable, but also more complicated.
(require 'delsel)
(defun my-show-paren-update-on-insert ()
  ;; A command with `delete-selection' property probably inserts text.
  (if (get this-command 'delete-selection)
      (show-paren-function)
    (show-paren-clear-highlight)))

(add-hook 'post-command-hook #'my-show-paren-update-on-insert)
npostavs
  • 9,033
  • 1
  • 21
  • 53
  • I noticed something: I want `show-paren-delay` only when navigating the code. If I actually type a new parenthesis, I want immediate highlighting. How do I do this? (The reason I use `show-paren-delay` in the first place is to avoid this insane flickering of parenthesis when I cursor around in my code, but when I'm typing a parenthesis, I obviously want to see the matching parenthesis immediately to know my scoping is correct.) – forthrin Apr 04 '18 at 09:39
  • @forthrin Updated. Not sure what you mean by "insane flickering" though. Maybe the double-buffering in Emacs 26 would help you more? – npostavs Apr 04 '18 at 09:59
  • If you cursor down the first column of your `.emacs`, which usually has a lot of opening parenthesis, all the closing parentheses on the end of the line will blink quickly on and off as you pass them. This is not desirable. PS! I couldn't get your new snippet to work. – forthrin Apr 04 '18 at 11:58
  • @forthin While I'm holding down `C-n` the parens don't get highlighted even with the default delay (`0.125`). If you are trying the new snippet in the same session as the old one, you need to `(remove-hook 'post-command-hook #'show-paren-clear-highlight)`. – npostavs Apr 04 '18 at 12:35
  • I noticed instant highlighting when typing happens for `lisp-mode`, but not for `php-mode` and `c-mode`. Can you check this? – forthrin Apr 06 '18 at 09:17
  • @forthrin Oh, it's because they bind `)` to `c-electric-paren` instead of `self-insert-command`, updated. – npostavs Apr 06 '18 at 12:33