From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Inserting output from a program into a buffer
Date: Mon, 22 Feb 2010 10:41:07 +0100 [thread overview]
Message-ID: <87zl316218.fsf@galatea.lan.informatimago.com> (raw)
In-Reply-To: slrnho3vpj.49k.tim@bart.johnson.com
Tim Johnson <tim@johnsons-web.com> writes:
> On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> <..>
>> You could get a string without attribute using the function
>> buffer-substring-no-properties instead of buffer-substring.
> Oh! Deja vu here... I believe that what I really need is
> buffer-substring-<etc> to copy the region directly to a
> variable.
In general, functions don't modify variables. This would be a bad case
of side effect. In emacs lisp it would be somewhat possible, since all
the variables are dynamic variables. (In Scheme or Common Lisp it
wouldn't even be possible for lexical variables!). However, even with
dynamic variables, you cannot modify the variables if they are rebound
in an inner dynamic scope.
(defun f (x)
(let ((var 42))
(list var
(progn (set-var x)
var))))
(defun set-var (x)
(setq var x))
(defvar var 0)
(set-var 33)
var ; --> 33 ; seems to work.
(f 22) ; --> (42 22) ; seems to have worked.
var ; --> 33 ; but it really didn't worked.
Well, really, it worked as it should but not as you would have wanted.
(Notice that this effect is a design bug in LISP that was detected in
1960 and corrected in the following years by the introduction of lexical
variables. emacs lisp is somewhat retrograde (or at least conservator)
on this point).
Anyways, it is a bad idea to try to modify variables from called
functions.
What you really need is to BIND the RESULT of buffer-string and other
functions TO a variable. This is done with LET:
(let ((string (buffer-substring (point-min) (+ 10 (point-min)))))
(string= "Newsgroups" string))
--> t
Or, in the improbable case you need to keep a value across functions,
you may store it in a global variables (but global variables should be
avoided as side effects in functions trying to modify them).
(defvar *my-text* "")
(setf *my-text* (buffer-substring (point-min) (+ 10 (point-min))))
*my-text* ; --> #("Newsgroups" 0 10 (fontified t face message-header-name))
Notice that while there's no lexical variables to protect against, I
still think that we should use the *x* convention in emacs lisp for
global variables, so that they're not accidentally shadowed by normal
variables (lexical wanabees) such as the poor var above.
But who I am to say such things, I'm not RMS...
--
__Pascal Bourguignon__
next prev parent reply other threads:[~2010-02-22 9:41 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-21 2:45 Inserting output from a program into a buffer Tim Johnson
2010-02-21 3:15 ` Tim Landscheidt
2010-02-21 4:45 ` Tim Johnson
2010-02-21 5:02 ` Barry Margolin
[not found] ` <slrnho2o6e.49k.tim@bart.johnson.com>
[not found] ` <87zl324774.fsf@lion.rapttech.com.au>
2010-02-22 0:45 ` Tim Johnson
2010-02-22 1:57 ` Pascal J. Bourguignon
2010-02-22 4:45 ` Tim Johnson
2010-02-22 9:41 ` Pascal J. Bourguignon [this message]
2010-02-22 19:44 ` Tim Johnson
2010-02-22 23:23 ` Pascal J. Bourguignon
2010-02-23 0:44 ` Tim Johnson
2010-02-22 23:06 ` jpkotta
2010-02-22 7:22 ` Tim X
2010-02-21 6:28 ` tomas
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87zl316218.fsf@galatea.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.
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).