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

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

Re: Why does this lisp function to grab text from buffer not work as I e


From: Pascal J. Bourguignon
Subject: Re: Why does this lisp function to grab text from buffer not work as I expect
Date: Sat, 13 Apr 2013 00:53:52 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

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

> On Fri, 12 Apr 2013 07:18:26 -0700 (PDT) acomber <deedexy@gmail.com> wrote:
>
>> I have some text like this:
>>
>> qwerty\tberty
>> merty\tserty
>>
>> I want to grab the text berty between the tab character and end of line. 
>> Then to show it worked goto the end of the buffer and insert the text there.
>>
>> But this code doesn't work.  Any ideas why not?
>>
>> (defun do-test ()
>>   "tester"
>>   (interactive)
>>   (goto-char (point-min))
>>   (if (search-forward "Option Name" nil t)
>>      (delete-char 1) ;;delete tab
>>      (setq myStr (buffer-substring point end-of-line)) ;add text to variable
>>      ;goto end of buffer and insert text as demonstration it works
>>      (goto-char (point-max))
>>      (insert(myStr))
>>   )
>> )
>
> point and end-of-line are functions, not variables, so they need to be
> surrounded by parens.  But end-of-line moves point to the end of the
> line and returns nil; that's also why you got the error you noted in
> your other posting.  You should instead use the function
> line-end-position: (buffer-substring (point) (line-end-position)).

And on the other hand, myStr is a variable, so why is it called as a
function in (insert (myStr)) ???

The OP seems to be totally confused between variables and functions…


Also, I don't see the relationship between "Option Name" and the example
text and problem statement…

Let's take it again from the start:

>> I have some text like this:
>>
>> qwerty\tberty
>> merty\tserty
>>
>> I want to grab the text berty between the tab character and end of line. 
>> Then to show it worked goto the end of the buffer and insert the text there.

(defun my-command ()
  (interactive)
  (goto-char (point-min)) ; not in the problem statement, but assumed.
  ;; I want to grab the text berty between tab and end-of-line:
  (when (re-search-forward "\t\\(berty\\)$" nil t)
    ;; We don't know what "it" is, in "show it worked", but
    ;; let's assume that "grab" implies deleting that text.
    (delete-region (match-beginning 1) (match-end 1))
    ;; Then to show it worked goto the end of the buffer
    (goto-char (point-max))
    ;;  and insert the text there.
    (insert "berty")))


So let's start with this buffer:
------------------------------------------------------------------------
qwerty  berty
merty   serty
------------------------------------------------------------------------
and type M-x my-command RET
we should get this buffer:
------------------------------------------------------------------------
qwerty  
merty   serty
berty
------------------------------------------------------------------------


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.




reply via email to

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