help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: match-string debugging problem


From: Pascal Bourguignon
Subject: Re: match-string debugging problem
Date: 10 Mar 2005 18:06:52 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Stephen Berman <Stephen.Berman@gmx.net> writes:

> There seems to be something about match-string that I don't
> understand.  Here is an example of the kind of code I'm working with:
> 
> (defvar mystring1 "+++++ ")
> (defvar mystring2 " ~~~~~")
> (defun mystring-list ()
>   (interactive)
>   (with-current-buffer (get-buffer-create "*test*")
>     (switch-to-buffer "*test*")
>     (dotimes (num 5)
>       (insert mystring1 "test" (int-to-string (1+ num)) mystring2 "\n"))
>     (goto-char (point-min))
>     (let ((mystring-list ()))
>       (while (re-search-forward
>             (concat "^" (regexp-quote mystring1) "\\(.+\\)"
>                     (regexp-quote mystring2) "$")
>             (point-max) t)
>       (setq mystring-list (append (list (match-string 1)) mystring-list)))
>       (insert "\n")
>       (setq mystring-list (reverse mystring-list))
>       (dolist (elt mystring-list)
>       (insert elt " ")))))
> 
> After evalling this code and typing `M-x mystring-list', buffer *test*
> consists of these lines:
> 
> +++++ test1 ~~~~~
> +++++ test2 ~~~~~
> +++++ test3 ~~~~~
> +++++ test4 ~~~~~
> +++++ test5 ~~~~~
> test1 test2 test3 test4 test5 
> 
> The last line indicates that match-string correctly matches the
> strings that build mystring-list.  But when I step through the code
> with edebug, match-string always returns nil and a wrong-type-argument
> error is raised at the insert (since nil is not char-or-string-p).
> (Edebug isn't the problem: evalling first the regexp search code in
> *test* and then (match-string 1) also returns nil.)  Because of this
> I'm having a hard time debugging other code that uses match-string.
> Can someone explain what's going on?

re-search-forward uses global state (buffer, matched range, etc) as
match-data, to communicate with match-string.  When you're debugging,
this global state is switched or modified.  One could consider it a
bug in the debugger.

See: match-data
     save-match-data   
     save-excursion
     save-buffer

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay


reply via email to

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