looking-back - looking-at

Andreas Röhler andreas.roehler at online.de
Sun Feb 3 03:19:19 EST 2008


Am Sonntag, 3. Februar 2008 02:15 schrieb Stephen J. Turnbull:
> Andreas Röhler writes:
>  > AFAIS `looking-back' as pendant to `looking-at' is not
>  > available at XEmacs. Would like to have it for
>  > compatibility reasons, should I'm being right so far.
>
> Not every 3-line function needs to be a primitive.  What does it do
> (*exactly*, there are a number of possible semantics it could have)?
>
> Quoting the whole defun, especially including the docstring, would
> probably be the best idea.
>
>

from GNU's subr.el:

(defun looking-back (regexp &optional limit greedy)
  "Return non-nil if text before point matches regular expression REGEXP.
Like `looking-at' except matches before point, and is slower.
LIMIT if non-nil speeds up the search by specifying a minimum
starting position, to avoid checking matches that would start
before LIMIT.

If GREEDY is non-nil, extend the match backwards as far as possible,
stopping when a single additional previous character cannot be part
of a match for REGEXP."
  (let ((start (point))
	(pos
	 (save-excursion
	   (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
		(point)))))
    (if (and greedy pos)
	(save-restriction
	  (narrow-to-region (point-min) start)
	  (while (and (> pos (point-min))
		      (save-excursion
			(goto-char pos)
			(backward-char 1)
			(looking-at (concat "\\(?:"  regexp "\\)\\'"))))
	    (setq pos (1- pos)))
	  (save-excursion
	    (goto-char pos)
	    (looking-at (concat "\\(?:"  regexp "\\)\\'")))))
    (not (null pos))))

BTW tried with `char-before' to write an alternative,
but that turned out much more complicated. 

Andreas Röhler



More information about the XEmacs-Beta mailing list