all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Perry Smith <pedzsan@gmail.com>
To: "help-gnu-emacs@gnu.org Help" <help-gnu-emacs@gnu.org>
Subject: Re: Help needed with defadvice
Date: Fri, 22 Nov 2013 09:50:02 -0600	[thread overview]
Message-ID: <ABB1EF2F-15FF-457E-B11E-A87FD363ED0A@gmail.com> (raw)
In-Reply-To: <8738mo7w0j.fsf@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3948 bytes --]


On Nov 22, 2013, at 1:09 AM, Alex Kost <alezost@gmail.com> wrote:

> Perry Smith (2013-11-22 06:45 +0400) wrote:
> 
>> I have this defadvice:
>> 
>> (defadvice get-buffer-create (around inherit activate)
>>  (let ((set-list (mapcar '(lambda ( v )
>> 			     (cons v (symbol-value v)))
>> 			  inherited-alist)))
>>    (with-current-buffer ad-do-it 
>>      (mapcar '(lambda ( c )
>> 		 (message "Setting %s to %s inside %s"
>> 			  (car c) (cdr c) (buffer-name (current-buffer)))
>> 		 (set (car c) (cdr c)))
>> 	      set-list))))
>> 
>> inherited-alist is a list of symbols that I add to.  When a buffer is created, I run through the list of variables and get their values as seen from the current buffer.  I then call get-buffer-create (via ad-do-it) and do a set on each of the variables.  The "message" is there just for debugging.  I get the messages like I expect .... e.g. "Setting foo to dog inside cat.c" or whatever.  All the symbols in inherited-alist are buffer-local variables.

To Eric: changing let to let* had not effect.

> 
> 1. With `set' you set a global value, you probably want
> (set (make-local-variable (car c)) (cdr c)) instead of
> (set (car c) (cdr c)).

No change.  If this helps, when I do ^h-v cscope-out-buffer I get:

cscope-out-buffer is a variable defined in `cscope.el'.
Its value is "banana"

  Automatically becomes buffer-local when set.

Documentation:
Buffer associated with the cscope process

"banana" is the global I set with:
(set-default 'cscope-out-buffer "banana") in *scratch*

I'm coming to the conclusion that something after get-buffer-create
is blasting the local variables back to their defaults.  (By the
way, I have added another message after the set and it
shows that it is being set.  I've also explicitly set one of these
variables outside of the let and it is still not being set.

I changed it to be:

(defadvice get-buffer-create (around inherit activate)
  (let* ((set-list (mapcar '(lambda ( v )
			      (cons v (symbol-value v)))
			   inherited-alist)))
    (with-current-buffer ad-do-it 
      (mapcar (lambda ( c )
		(message "Setting %s to %s inside %s"
			 (car c) (cdr c) (buffer-name (current-buffer)))
		(set (make-local-variable (car c)) (cdr c)))
	      set-list)))
  (setq cscope-out-buffer "juice"))

and describe-variable still has the same output.  It is still set to "banana" (not "juice")

(I tried setq as well as set with make-variable-buffer-local).

> 
> 2. Don't quote lambdas: <http://www.emacswiki.org/emacs/QuotedLambda>.

Ok.  (no change to my issue)

> 
> 3. "alist" means "association list", so you shouldn't call
> `inherited-alist' like this as it's a simple list.  For info about
> alists look at (info "(elisp) Association Lists").

Thanks... I should have known that.  In my defense, I wrote this
25 years ago and I haven't touched it since.

> 
> 4. I believe making an advice for `get-buffer-create' is not a good
> idea.  Don't you have a lot of messages about setting variables in
> *temp* buffers?  Thus you will set unintended variables for any new
> buffer.  Do you really need that?

I had it "up" a level or two doing advice for create-file-buffer but it had
the same problem.  The message will go away when I'm done.  I'm not
sure where I want to put it.

On perhaps a side note, the documentation for make-variable-buffer-local 
has this curious sentence:

Note that binding the variable with `let', or setting it while
a `let'-style binding made in this buffer is in effect,
does not make the variable buffer-local.

I think I'm doing the 2nd phrase -- right?  But then I get confused and
I don't understand why my other example works because it is inside
a let binding a well.  Even if I remove my let, I am likely going to be
inside a let but whoever is calling down to the function I am advising.

Thank you for your help,
Perry


[-- Attachment #2: Message signed with OpenPGP using GPGMail --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

  reply	other threads:[~2013-11-22 15:50 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-22  2:45 Help needed with defadvice Perry Smith
2013-11-22  6:27 ` Eric Abrahamsen
2013-11-22  7:09 ` Alex Kost
2013-11-22 15:50   ` Perry Smith [this message]
2013-11-22 21:56     ` Alex Kost
2013-11-23  1:41       ` Perry Smith
2013-11-23  7:00         ` Alex Kost
2013-11-23 14:48         ` Stefan Monnier
2013-11-23 15:22           ` Perry Smith
2013-11-23 15:38             ` Stefan Monnier
2013-11-23 16:24               ` Perry Smith
2013-11-24 18:21                 ` Stefan Monnier
2013-11-23 15:43           ` Alex Kost
     [not found]   ` <mailman.6834.1385135422.10748.help-gnu-emacs@gnu.org>
2013-11-23  8:35     ` jack-mac
2013-11-22 14:44 ` Stefan Monnier

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=ABB1EF2F-15FF-457E-B11E-A87FD363ED0A@gmail.com \
    --to=pedzsan@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.