[Top][All Lists]

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

Re: modern regexes in emacs

From: Mattias Engdegård
Subject: Re: modern regexes in emacs
Date: Mon, 18 Feb 2019 09:55:39 +0100

17 feb. 2019 kl. 21.47 skrev Stefan Monnier <address@hidden>:
> Other than isearch, most other commands should (ideally) read their
> regexps interactively with `read-regexp`, so it should be easy for
> a third party package to advise `read-regexp` so it accepts the PCRE
> syntax (or the RX syntax, ...) and then converts it to Emacs's
> own syntax.  Shouldn't break any package at all.

Like this?

(defun backslash-mod-regexp (re)
  "Invert the backslash requirements for |(){} in RE."
    (insert re)
    (goto-char (point-min))
    (while (re-search-forward
            (rx (or (seq "["
                         (opt "^")
                         (opt "]")
                         (* (not (any "]")))
                    (seq "\\" (or (group (any "|(){}"))
                                  (seq (any "sScC") anything)
                    (group (any "|(){}"))))
            nil t)
       ((match-beginning 1) (replace-match "\\1"))
       ((match-beginning 2) (replace-match "\\\\\\&"))))

(defadvice read-regexp (after read-regexp-backslash-mod last activate)
  (when (stringp ad-return-value)
    (setq ad-return-value (backslash-mod-regexp ad-return-value))))

Of course it won't help with commands that display previously entered regexps, 
which users naturally want to see in the form entered, not converted. However, 
that should not matter functionally.

reply via email to

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