unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Why does this lisp function to grab text from buffer not work as I expect
@ 2013-04-12 14:18 acomber
  2013-04-12 20:15 ` Stephen Berman
  0 siblings, 1 reply; 4+ messages in thread
From: acomber @ 2013-04-12 14:18 UTC (permalink / raw)
  To: Help-gnu-emacs

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?






--
View this message in context: http://emacs.1067599.n5.nabble.com/Why-does-this-lisp-function-to-grab-text-from-buffer-not-work-as-I-expect-tp283560.html
Sent from the Emacs - Help mailing list archive at Nabble.com.



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

* Re: Why does this lisp function to grab text from buffer not work as I expect
       [not found] <mailman.24033.1365776319.855.help-gnu-emacs@gnu.org>
@ 2013-04-12 19:41 ` Barry Margolin
  0 siblings, 0 replies; 4+ messages in thread
From: Barry Margolin @ 2013-04-12 19:41 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.24033.1365776319.855.help-gnu-emacs@gnu.org>,
 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?
> 
> 
> 
> 
> 
> 
> --
> View this message in context: 
> http://emacs.1067599.n5.nabble.com/Why-does-this-lisp-function-to-grab-text-fr
> om-buffer-not-work-as-I-expect-tp283560.html
> Sent from the Emacs - Help mailing list archive at Nabble.com.

Invisible code tends to work poorly.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


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

* Re: Why does this lisp function to grab text from buffer not work as I expect
  2013-04-12 14:18 acomber
@ 2013-04-12 20:15 ` Stephen Berman
  2013-04-12 22:53   ` Pascal J. Bourguignon
  0 siblings, 1 reply; 4+ messages in thread
From: Stephen Berman @ 2013-04-12 20:15 UTC (permalink / raw)
  To: help-gnu-emacs

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)).

Steve Berman




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

* Re: Why does this lisp function to grab text from buffer not work as I expect
  2013-04-12 20:15 ` Stephen Berman
@ 2013-04-12 22:53   ` Pascal J. Bourguignon
  0 siblings, 0 replies; 4+ messages in thread
From: Pascal J. Bourguignon @ 2013-04-12 22:53 UTC (permalink / raw)
  To: help-gnu-emacs

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 {}.




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

end of thread, other threads:[~2013-04-12 22:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.24033.1365776319.855.help-gnu-emacs@gnu.org>
2013-04-12 19:41 ` Why does this lisp function to grab text from buffer not work as I expect Barry Margolin
2013-04-12 14:18 acomber
2013-04-12 20:15 ` Stephen Berman
2013-04-12 22:53   ` Pascal J. Bourguignon

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).