From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
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 [thread overview]
Message-ID: <877gk7bbsv.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: 87d2tzfqub.fsf@rosalinde.fritz.box
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 {}.
next prev parent reply other threads:[~2013-04-12 22:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-12 14:18 Why does this lisp function to grab text from buffer not work as I expect acomber
2013-04-12 20:15 ` Stephen Berman
2013-04-12 22:53 ` Pascal J. Bourguignon [this message]
[not found] <mailman.24033.1365776319.855.help-gnu-emacs@gnu.org>
2013-04-12 19:41 ` Barry Margolin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877gk7bbsv.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.