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

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

Re: (goto-char ...) error


From: Deniz Dogan
Subject: Re: (goto-char ...) error
Date: Tue, 22 Feb 2011 19:45:30 +0100

2011/2/22 ken <gebser@mousecar.com>:
>
> On 02/22/2011 12:31 PM Deniz Dogan wrote:
>> 2011/2/22 ken <gebser@mousecar.com>:
>>> Performing one search, I save the result with
>>>
>>> (setq ptname (re-search-forward ...))
>>>
>>> Then I want to back up one character and perform another search, so I do
>>>
>>> (goto-char (- ptname 1))
>>>
>>> But this pukes an error.  What's unkosher here?
>>>
>>
>> It's much easier if you tell us what the error is.
>>
>> I tried to reproduce the problem using this code:
>>
>> ;; search for "a"
>> (let ((ptname (re-search-forward "a" nil t)))
>>   (when ptname
>>     (goto-char (- ptname 1))))
>>
>> It all depends on how you use re-search-forward. As you can see in my
>> example, I pass t as the third argument meaning "don't error if you
>> can't find it, just return nil". I then make sure that ptname is
>> non-nil before I try to act on it using `-', otherwise we would be
>> doing (- nil 1) which makes no sense.
>>
>
> My understanding is that the 4th arg to re-search-forward is to repeat
> the search, so I set that to nil.
>
> I get the same error whether the 3rd arg is t or nil (!?):
>
> (setq ptname (re-search-forward "REGEXP" endpt t nil))
>      (if ptname
>          ((goto-char (- ptname 1))
>           ....
>
> The error line in *Messages* says:
>
> if: Invalid function: (goto-char (- begin-name-value 1))
>
>

You have one pair of parentheses too many.

Change:

((goto-char (- ptname 1))

to:

(goto-char (- ptname 1))

The error is telling you that "(goto-char (- ptname 1))" is an invalid
function, which could potentially be confusing, but it really makes
sense.  You call a function named `foo' like (foo ...), but if you do
"((foo ...))" you're trying to call a function named "(foo ...)" which
is not a valid function.

>From the documentation of `if':

(if COND THEN ELSE...)
If COND yields non-nil, do THEN, else do ELSE...

This means that your THEN clause must be a single expression.
Everything starting with the third argument to `if' is considered part
of the ELSE clause.

If you want to multiple expressions in the THEN clause, use `progn' as
such:

(if (= x y)
    (progn
      (message "They're equal")
      (message "Hooray!"))
  (message "They're not equal.")
  (message "Too bad, bro."))

I hope that helps!

-- 
Deniz Dogan



reply via email to

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