Search code examples
regexemacssyntaxadvising-functions

Is it possible to change emacs' regexp syntax?


I love emacs. I love regexs. I hate emacs' regex syntax - the need to escape grouping parens and braces, that you dont escape literal parens, the lack of predefined character classes, etc.

Can I replace emacs' regex engine, or tweak some setting, so that when I use the Query-replace-regexp (or one of many other) feature I can use the syntax I program with in java/js/perl/ruby/etc...?

Edit: The subject was originally "how to change emacs' regex engine" which would not only change escaping rules and add character classes, but also (not mentioned in the post) add support for various common extensions (?...). Features like non-capturing parens: (?:...), match only if/if-not followed by: (?=...)/(?!...), and others. I don't believe (though happy to be corrected) these are possible with emacs' current regex engine, and no amount of syntax replacements will fix that.

The solution below addresses the original issues of escaping and additional char classes by replacing with syntax emacs understands. A second answer (now deleted) suggested advising (add a function to run at the start of another) emacs' regex function to do the replacement for all regex processing. The author quickly censored him/herself realizing that it would likely break much existing emacs code, and eventually the post was removed.

I would still like to change the regex-engine to one which supports extensions, but I agree that changing the escaping behavior universally would cause havoc I'm not willing to chase. Thus, I'm changing the subject to match the question and accepting the response.

It crossed my mind to change the engine to support common-syntax and extensions, advise the regex function to convert emacs-internal code to common-syntax, advise the interactive functions to convert my common-syntax to emacs-syntax (so it can be converted back to common)... but I think even RMS would recommend a fork before that.


Solution

  • You could define your own elisp function which modified the regexp and then passed it back to emacs. In pseudo-elisp:

    (defun my-query-replace-regexp (regexp)
        ; modify regexp to replace ( with \(, { with \{, etc.
        (query-replace-regexp modified-regexp)
    )
    
    ; rebind C-% to use new regexp function
    (global-set-key "\C-%" 'my-query-replace-regexp)