From: Thorsten Jolitz <tjolitz@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Change a symbol's plist member with (let ...)?
Date: Sun, 16 Mar 2014 17:01:02 +0100 [thread overview]
Message-ID: <87y50am8u9.fsf@gmail.com> (raw)
In-Reply-To: 87eh232lto.fsf@yahoo.fr
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> Thorsten Jolitz <tjolitz@gmail.com> writes:
>>> (letf (((get major-mode 'derived-mode-parent) 'special-mode))
>>> (derived-mode-p 'text-mode))
>>> returns nil.
>>
>> I have to get more involved with this cl stuff it seems ...
>
> This one is rather easy to understand. 'letf' does a temporary 'setf'
> and then puts the previous value back. So what is 'setf' ?
>
> 'setf' is a generic way to *change* a value if you know how to *get*
> that value.
>
> Example : if foo is a cons cell, you know how to get its car
> : (car foo). Well, now (setf (car foo) 'bar) will change the car of foo
> to 'bar. IOW, setf arranges so that the next invocations of (car foo)
> will now return 'bar instead of its previous value. And it knows
> internally how to do it : it uses 'setcar' :
> (macroexpand '(setf (car foo) 'special-mode))
> => (let* ((v foo)) (setcar v (quote special-mode)))
> (the surrounding let*-binding isn't needed in this case, is needed in
> other cases, but let's ignore it.)
>
> So now :
> (setf (get major-mode 'derived-mode-parent) 'special-mode)
> simply arranges to change whatever is required so that (get major-mode
> 'derived-mode-parent) will return 'special-mode. Internally, it knows
> what to do :
> (macroexpand '(setf (get major-mode 'derived-mode-parent) 'special-mode))
> gives :
> (let* ((v major-mode)) (put v (quote derived-mode-parent) (quote special-mode)))
>
> And now if you want to do that temporarily, you use letf : letf will
> arrange to revert the change upon exiting the letf form.
> (macroexpand'(letf (((get major-mode 'derived-mode-parent) 'special-mode))
> (derived-mode-p 'text-mode)))
> will give (I added comments) :
> (let* ((v major-mode)
> (old (get v (quote derived-mode-parent)))) ; save old value
> (unwind-protect
> (progn
> (put v ; set new value
> (quote derived-mode-parent)
> (quote special-mode))
> (derived-mode-p ; run the body
> (quote text-mode)))
> (put v ; set old value back.
> (quote derived-mode-parent)
> old)))
>
> Simple, yet powerful.
Interesting, thanks for the explanations!
Amazing how intelligent this macro is ...
I do have those Paul Graham CL books, and with these CL idioms becoming
more standard in Elisp, I should take a second look at them I guess.
--
cheers,
Thorsten
next prev parent reply other threads:[~2014-03-16 16:01 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-14 21:17 Change a symbol's plist member with (let ...)? Thorsten Jolitz
2014-03-14 21:37 ` Nicolas Richard
2014-03-14 22:25 ` Thorsten Jolitz
2014-03-15 21:28 ` Nicolas Richard
2014-03-16 16:01 ` Thorsten Jolitz [this message]
2014-03-16 18:10 ` Stefan
2014-03-17 0:49 ` Thorsten Jolitz
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=87y50am8u9.fsf@gmail.com \
--to=tjolitz@gmail.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.