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

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

Re: error in replace-match: "args out of range"


From: Tim X
Subject: Re: error in replace-match: "args out of range"
Date: Tue, 05 Apr 2011 08:21:56 +1000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

ken <address@hidden> writes:

> On 04/03/2011 07:19 PM PJ Weisberg wrote:
>> On 4/3/11, ken <address@hidden> wrote:
>>> On 03/28/2011 04:32 PM Stefan Monnier wrote:
>>>> I'm just saying that
>>>>
>>>> M-: (some-regexp-search) RET
>>>> M-: (replace-match ...) RET
>>>>
>>>> won't always do what you want, because a whole lot of code will run
>>>> between the two.
>>>>
>>>>
>>>>         Stefan
>>> Hmm.  I'm hearing "this is faith-based functionality."  Does it have to
>>> do with the fact that all processes running from one emacs session have
>>> the one and the same PID?  I recall reading a discussion on this list
>>> (which was largely outside my capacity) multiple years ago about some
>>> problem which 'would be resolved when threading is implemented.'  Is
>>> this an issue of that sort?
>> 
>> It sounds more like the sort of issue that would be *created* when
>> threading was implemented.
>
> Probably this discussion won't solve anything, so let's leave it for
> another time.
>
>
>> 
>> Based on what Stefan said, a more helpful question might be: what
>> happens between the call to match-string and the call to
>> replace-match?  Are they in the same function, and close together?
>> What can match-string tell you about the string you're trying to
>> replace?
>> 
>> -PJ
>> 
>
> Here's the relevant code fragment:
>
>     (if (re-search-forward aname-re-str head-text-end-pt t)
>       (progn  
>         (setq aname-str (match-string 2))
>         (setq head-text-nested (match-string 4))
>         (if (equal head-text-nested "")
>             (progn
>               (setq head-text-nested (create-heading-text))
>               (if (equal aname-str "")
>                   (progn
>                     (setq aname-str (create-aname head-text-nested))
>                     (replace-match head-text-nested t nil nil 4)
>                     (replace-match aname-str t nil nil 2))
>                 (replace-match head-text-nested t t nil 4))) ;ERROR:
>
> The top line above is the search.  The last line is the replace-match
> which fails.  The couple replace-match lines above it might also fail...
> haven't tested those yet.
>
> create-aname is nothing but a set of setq's and a read-from-minibuffer.
>  create-heading-text is just a read-from-minibuffer.
>
> So, yes, the search and replace-match are in the same function.  Are
> they "close together"?  I'd say yes, but that's a judgment call really.
>  The string I want to replace in this particular instance is a null string.
>

Providing the code alway helps. While Stephan's points are valid and a
common error people make, I don't think it is the problem you are
seeing.

It would help to see the regexp you are using to test this. Note that
match-string returns nil if the subexpression you are asking for did not
exist. If the subexpression did not exist, what would replace-match
replace as there is no match?

Note also, you can get rid of some of those progns i.e.

     (when (re-search-forward aname-re-str head-text-end-pt t)
          (setq aname-str (match-string 2))
          (setq head-text-nested (match-string 4))
          (when (string= head-text-nested "")
                (setq head-text-nested (create-heading-text))
                (if (not (string= aname-str ""))
                     (replace-match head-text-nested t t nil 4)
                  (setq aname-str (create-aname head-text-nested))
                  (replace-match head-text-nested t nil nil 4)
                  (replace-match aname-str t nil nil 2))))

Note that the 'else' part of an if form has an implicit progn, so by
revesing the condition test, we can eliminate that progn. The 'unless'
form can be used instead of 'when' if you want to reverse the logic of
what you are doing. 

(Note above is untested, but intention should be clear)

My guess is that it does not work because replace-match has no idea what
it is yhou want to match as the subexpression was not found i.e. is not
in the string, so it has no way to know where in the string to do the
replace.

Tim


-- 
tcross (at) rapttech dot com dot au


reply via email to

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