Subject: bug#9681: Broken behaviour of re-search-backward (.+ matching only a single character)

From: Jack Duthen <[email-masked]>
Date: Fri, 16 Mar 2012 16:49:37 +0100

On Thu, Oct 06, 2011 at 08:57:09AM -0400, Stefan Monnier wrote:
> re-search-* stops at the first character position that has a match.
> And then it chooses the longest match at that position.

Stepan wrote:
> So, again: it definitely needs better documentation,
> and IMO it also needs fixing.

Hi!

For my own imenu-prev-index-position-function, I needed
a backward regexp search which would match something like ".+"
the way one (like Stepan) can expect rather than the way it actually
does (as described by Stefan).

So, I just wrote a function to do that.

The way it handles the COUNT variable is not as good as one could want
but, as I almost never use it, I don't care.
It's not very efficient but, since I can't notice the time it takes
when used in the "*rescan" menu and since I can't imagine a better algorithm,
it's ok for me.

(defun jd-re-search-backward (regexp &optional bound noerror count)
  (let ((orig-point (point)) bom)
    (when (re-search-backward regexp bound noerror count)
      (setq bom (point)) ; should not be useful
      (goto-char (point-min))
      (while (re-search-forward regexp orig-point 'noerror)
        ;; remember the last beginning of match
        (setq bom (match-beginning 0)))
      (goto-char bom)
      ;; set match data (erased by the last failing search) and return T
      (looking-at regexp))))

HTH
)jack(