strNOcat
strNOcat

Reputation: 923

Emacs: Matching parenthesis when cursor is ON closing parenthesis

{It has been asked before: Emacs: highlight matching paren when cursor is on it, not after it but none of the answers are satisfactory}

I am using mic-paren with following .emacs settings (although the problem exists with all similar emacs packages, so it seems to be some kind of default emacs behavior)

    (paren-activate)
    (setq paren-match-face 'highlight)
    (setq paren-sexp-mode t)

which highlight the all the text between two parenthesis. It works well when the cursor is ON opening parenthesis but from the other side, I have to put my cursor AFTER the closing parenthesis. This results in strange behavior when used with slime (which requires the cursor to be put ON the closing parenthesis to display general usage information and such). Is there any way to change this behavior and make emacs match parenthesis when the cursor is ON closing parenthesis?

EDIT: Minor grammar fix

Upvotes: 0

Views: 1437

Answers (4)

Flux
Flux

Reputation: 10920

The following advice does what you want. When the block cursor is "on" the opening parenthesis, the closing parenthesis is highlighted. When the block cursor is "on" the closing parenthesis, the opening parenthesis is highlighted.

(advice-add show-paren-data-function
            :around
            (lambda (orig-fun)
              (cond ((looking-at "\\s(")
                     (funcall orig-fun))
                    ((looking-at "\\s)")
                     (save-excursion (forward-char 1) (funcall orig-fun))))))

Refer to my answer on the Emacs Stack Exchange site: https://emacs.stackexchange.com/a/63458/17507. In that answer, I provide a full explanation of the code above, and also a suggested variant.

Upvotes: 0

Tobias
Tobias

Reputation: 5198

The following works for `mic-paren'. But, it has some afterglow;-). The opening delimiter is highlighted if the cursor is on the closing delimiter or just behind it.

(defadvice mic-paren-highlight (around cursorOnClosing activate)
  "Dirty hack to highlight sexps with closing delim below cursor"
  (if (eq (char-syntax (following-char)) ?\) )
      (let ((paren-priority 'close))
        (save-excursion
          (forward-char)
          ad-do-it))
    ad-do-it))

Naturally, to make this work you need to install mic-paren correctly. Just follow the installation guide in mic-paren.el cited here:

Installation:

  • Place this file in a directory in your 'load-path and byte-compile it. You can surely ignore the warnings.
  • Put the following in your .emacs file: (GNU Emacs supports mic-paren only within a window-system but XEmacs supports mic-paren also without X)
   (when (or (featurep 'xemacs) window-system)
      (require 'mic-paren) ; loading
      (paren-activate)     ; activating
   ; set here any of the customizable variables of mic-paren:
   ; ...
   )
  • Restart your Emacs. mic-paren is now installed and activated!
  • To list the possible customizations enter C-h f paren-activate' or go to the customization groupmic-paren-matching'.

EDITS:

  • follow Stefan's hint about (featurep 'xemacs)

Upvotes: 1

Alan Mackenzie
Alan Mackenzie

Reputation: 61

What's important to remember here is that Emacs's point is between two characters, not on a character. For the show-paren facility to trigger, the point must be immediately outside a paren, whether opening or closing. The observed dissymmetry is caused by the block cursor being placed, arbitrarily, on the character after (rather than before) the point.

If this disturbs you, then a workaround would be to use a line cursor rather than a block cursor.

show-paren-mode is being enhanced for the next but one release, such that it will trigger also with the point immediately inside a paren.

Upvotes: 1

Stefan
Stefan

Reputation: 28531

Don't know about mic-paren, but using the built-in show-paren-mode, you can get what you want in Emacs-24.4 with:

(defun my-show-paren-any (orig-fun)
  (or (funcall orig-fun)
      (if (looking-at "\\s)")
          (save-excursion (forward-char 1) (funcall orig-fun)))))

(add-function :around show-paren-data-function #'my-show-paren-any)

Upvotes: 3

Related Questions