emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange behavior with 'save-excursion'


From: Andreas Schwab
Subject: Re: Strange behavior with 'save-excursion'
Date: Wed, 04 Aug 2004 15:52:41 +0200
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

"Velizar Bodurski" <address@hidden> writes:

> The code is:
> (progn
> (save-excursion
> (while (re-search-forward "^[ \t]*#define \\([a-zA-Z_0-9]+\\)[
> \t]*\\([()a-zA-Z \t0-9]*$\\)" (point-max) t 1)
> (end-of-line)
> (let ((cur (point)))
> (goto-char (point-min))
> (insert (concat "Defined: " (match-string 1) " with "
> (if (> (length (match-string 2)) 0)
> (match-string 2)
> "Empty")
> "\n"))
> (goto-char cur)))))
>
> This code is executed in *scratch* buffer with '^J'
> (eval-print-last-sexp)..
> The buffer also contains "#define ABZ test" several lines after the
> code. When the above code is executed it continuously adds new
> records in the beginning of the buffer.

When you insert the text (which is longer than the match) the value of cur
does not change, and (goto-char cur) moves you back before the start of
the last match.  So this is the expected behaviour.

> Also I executed much simplified re-search, the code was:
> (progn
> (save-excursion
> (while (re-search-forward "test" (point-max) t 1)
> (end-of-line)
> (let ((cur (point)))
> (goto-char (point-min))
> (insert "OK\n")
> (goto-char cur)))))
> the buffer contained 'test' string some lines after the above code, the
> result was that the the code executed only once as expected.

In this case the inserted text is shorter than the match, so moving back
to cur only moves you slightly back, but not before the start of the last
match.

Andreas.

-- 
Andreas Schwab, SuSE Labs, address@hidden
SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




reply via email to

[Prev in Thread] Current Thread [Next in Thread]