emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make regexp handling more regular


From: Adam Porter
Subject: Re: Make regexp handling more regular
Date: Wed, 02 Dec 2020 22:16:45 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux)

Lars Ingebrigtsen <larsi@gnus.org> writes:

> I constant source of confusion and subtle bugs is the way Emacs does
> regexp match handling: The way `string-match' (and the rest) sets a
> global state, and you sort of have to catch them "early" is often a
> challenge for new users.
>
> Experienced Emacs Lisp programmers know to be safe and will say:
>
> (when (string-match "[a-z]" string)
>   (let ((match (match-string 0 string)))
>     (foo)
>     (bar match)))
>
> while people new to Emacs Lisp will expect this to work:
>
> (when (string-match "[a-z]" string)
>   (foo)
>   (bar (match-string - string)))
>
> And sometimes it does, and sometimes it doesn't, depending on whether
> `foo' also messes with the match data.
>
> So my idle shower thought for the day is: Is there any reasonable path
> forward that the Emacs Lisp language could take here?

It's funny that you should post this today, Lars, because I just
encountered this very problem while using code from your format-spec
function in combination with code from your shr-insert-document function
(the latter of which changed the match data, making the former fail
inexplicably...until I figured it out).  Not that I'm blaming you, of
course--it's me who's using your code in unintended ways.  :)

Anyway, I'd be very happy if Emacs had "safer" matching functions like
this.  And I like the idea of prefixing them with "rx-", as was
suggested.

Thanks for your work on Emacs!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]