4

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?)

Community
  • 1
  • 1
Kevin
  • 1,548
  • 2
  • 19
  • 34
  • 1
    FWIW, I count 114 uses of `search-forward-regexp` vs 3685 use of `re-search-forward` in Emacs's source code. So I'd be inclined to declare `re-search-forward` as the recommended function to use in source code. – Stefan Apr 25 '16 at 13:01

3 Answers3

1

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.

Drew
  • 29,895
  • 7
  • 74
  • 104
1

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.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • I agree, the (interactive) difference was all that I could see. Search-forward-regexp can be used both ways, of course. But since I read in the manual in a few places to avoid using interactive funs (eg beginning-of-line) vs (goto-char (point-min)) in code, I figured that it was "important" (or at least useful in some way) to pick the right search expression for my code. – Kevin Apr 26 '16 at 02:42
1

The use of "re-" meaning regular-expression will be common for most Emacs users, but must not for beginners, resp. non-programmers. The alias is more verbose, which might be helpful.

Andreas Röhler
  • 4,804
  • 14
  • 18