Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs recommended use of search-forward-regexp vs re-search-forward?

Tags:

regex

emacs

According to this previous SO post, Emacs has two names (re-search-forward, search-forward-regexp) for the same code function (re-search-forward).

The Emacs doc only mentions re-search-forward, as far as I can see.

Is there a preferred / recommended form that I should use? (Is one of the names deprecated?) Personally I think search-forward-regexp is more informative because it has -regexp explicitly and clearly in the name (vs the shorter and more opaque "re-"). But I would like to stay on the side of recommended practice, if possible.

Another way of asking this is "why would the emacs developers put an alias search-forward-regexp in the code but not in the doc? (Maybe the alias indicates a "better" name because maybe the alias came after the re-search-forward original?)

like image 956
Kevin Avatar asked Oct 27 '25 17:10

Kevin


2 Answers

It all depends what behavior you want, as the Q&A you pointed to explains.

By aliasing search-forward-regexp to the definition of re-search-forward at the time Emacs was built, if a user or library later changes the definition of re-search-forward then search-forward-regexp will not be automatically affected by that change. That lets users modify one of the two without necessarily modifying the other. IOW, it gives users more flexibility. (Nothing prevents someone from modifying both the same way, or even of re-aliasing search-forward-regexp to the symbol re-search-forward or to a later definition of it.

There is no preferred form between the two. This comment in the code that defines the alias tells you that neither is being deprecated:

;; Alternate names for functions - these are not being phased out.
(defalias 'search-forward-regexp (symbol-function 're-search-forward))

Your observation about the names is probably behind the aliasing. Both have been around a long time - at least as far back as Emacs 20.

like image 129
Drew Avatar answered Oct 29 '25 08:10

Drew


My impression was that search-forward-regexp was intended as the name to use for interactive use, whereas re-search-forward is normally used from Elisp code.

I'd happily declare search-forward-regexp obsolete, but it's one of those fights that aren't really worth fighting.

like image 22
Stefan Avatar answered Oct 29 '25 09:10

Stefan