unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: Strange behavior with 'save-excursion'
  2004-08-04 14:11 Strange behavior with 'save-excursion' Velizar Bodurski
@ 2004-08-04 13:52 ` Andreas Schwab
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Schwab @ 2004-08-04 13:52 UTC (permalink / raw)
  Cc: emacs-devel

"Velizar Bodurski" <velo@homelan.bg> 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, schwab@suse.de
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."

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Strange behavior with 'save-excursion'
@ 2004-08-04 14:11 Velizar Bodurski
  2004-08-04 13:52 ` Andreas Schwab
  0 siblings, 1 reply; 2+ messages in thread
From: Velizar Bodurski @ 2004-08-04 14:11 UTC (permalink / raw)


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. But if the 'save-excursions' is
removed or moved inside the 'while' loop the code this time correctly,
only adds one new record and stops. 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.

Is this a correct behavior.

Some information about emacs build used (the build is without any
additional patches or modifications of the original source archive):

In GNU Emacs 21.3.1 (i686-pc-linux-gnu, X toolkit, Xaw3d scroll bars)
of 2004-01-06 on moon.batmbg.com
Important settings:
value of $LC_ALL: nil
value of $LC_COLLATE: nil
value of $LC_CTYPE: nil
value of $LC_MESSAGES: nil
value of $LC_MONETARY: nil
value of $LC_NUMERIC: nil
value of $LC_TIME: nil
value of $LANG: en_US
locale-coding-system: iso-latin-1
default-enable-multibyte-characters: t


________________________________________________
Message sent by <a href='http://mail.data.bg'>Data.BG WebMail</a>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-08-04 14:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-04 14:11 Strange behavior with 'save-excursion' Velizar Bodurski
2004-08-04 13:52 ` Andreas Schwab

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).