all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: "Herman, Géza" <geza.herman@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Parenthesis matching should consider the kind of parenthesis during pair-search
Date: Mon, 12 Feb 2024 12:58:18 +0000	[thread overview]
Message-ID: <ZcoV6m3YhGaF37iU@ACM> (raw)
In-Reply-To: <87r0hh6g1d.fsf@gmail.com>

Hello, Géza.

On Mon, Feb 12, 2024 at 12:49:03 +0100, Herman, Géza wrote:
> Hi all,

> Emacs parenthesis matching works differently than I'd have 
> expected (what show-paren-mode shows).  As I understand it, it 
> searches for the matching pair by not considering the kind of the 
> parenthesis (no matter whether it's a curly/square/etc bracket).

The low level routines like scan-lists work by scanning over text
counting the nesting level of open and close parentheses, "ignoring"
comments and strings as it goes.  (That "ignoring" is somewhat
complicated.)  When the depth of nesting gets back to zero, that's where
the matching close paren is.

> And after it found it, then it considers the kind, and if there is 
> a mismatch then it is shown in red.  Usually this is not a 
> problem, because parentheses come in pairs which are usually 
> nested.  But if this is not true, then Emacs works unexpectedly in 
> my opinion.  See this Makefile example: "$(info })" (this prints a 
> } to stdout).  In this case, emacs shows } as the "matching" pair 
> of the (.  And as the kind doesn't match, it's a mismatch.  But 
> I'd have expected that the pair of ( is the ), and } doesn't have 
> a matching pair (because this is what actually happens in this 
> example).

> What do you think, shouldn't Emacs consider the kind of 
> parenthesis when during searching for a pair? (in other words, it 
> should ignore all other kinds of parentheses when it searches for 
> a pair)

No, I don't think that.  ;-)  A lot of the time (?most of the time), we
want Emacs to point out paren mismatch errors, and that alternative way
of scanning parens would prevent this.

If you have a specific need to consider only ( and ) as the balancing
parens, ignoring [, ], {, }, etc., you can achieve this by removing the
open/close syntax-table entries from these characters first.  See the
chapter "Syntax Tables" in the Elisp manual for details.  Be aware that
swapping syntax tables repeatedly can slow your program down, since it
clears useful caches in doing so.

> Thanks,
> Geza

-- 
Alan Mackenzie (Nuremberg, Germany).



  parent reply	other threads:[~2024-02-12 12:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-12 11:49 Parenthesis matching should consider the kind of parenthesis during pair-search Herman, Géza
2024-02-12 12:34 ` Joost Kremers
2024-02-12 12:58 ` Alan Mackenzie [this message]
2024-02-12 13:10   ` Herman, Géza
2024-02-12 14:07     ` Alan Mackenzie
2024-02-12 14:10       ` Herman, Géza
2024-02-12 15:57         ` Alan Mackenzie
2024-02-12 16:05           ` Herman, Géza

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZcoV6m3YhGaF37iU@ACM \
    --to=acm@muc.de \
    --cc=emacs-devel@gnu.org \
    --cc=geza.herman@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.