unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: help-gnu-emacs@gnu.org
Subject: Re: Help needed with defadvice
Date: Fri, 22 Nov 2013 13:27:41 +0700	[thread overview]
Message-ID: <877gc1t0gy.fsf@ericabrahamsen.net> (raw)
In-Reply-To: AF488794-2AB5-4F7D-8949-64A73614A2FF@gmail.com

Perry Smith <pedzsan@gmail.com> writes:

> 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.
>
> When I get done and get in cat.c and ask for the value of foo, it is always nil.
>
> I have almost the same function:
>
> (defun inherit-from-buffer ( buf )
>   "Set all inherited variables of current buffer to those values of BUF"
>   (interactive "bBuffer: ")
>   (message "Inheriting from %s to %s" buf (buffer-name (current-buffer)))
>   (let ((curbuf (current-buffer))
> 	set-list)
>     (set-buffer buf)
>     (setq set-list
> 	  (mapcar '(lambda ( v )
> 		     (cons v (symbol-value v)))
> 		  inherited-alist))
>     (set-buffer curbuf)
>     (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)))
>
> which I call interactively from inside cat.c and give it an argument of another buffer to inherit from and it works as expected.  The messages are the same, everything is the same except the function, after the fact, works but doing roughly the same from inside a advice does not.

I don't really know why this wouldn't work, but maybe try changing the
first let to read:

(let* ((curbuf (current-buffer))
       (set-list (mapcar '(lambda ( v )
			    (cons v (buffer-local-value v curbuf)))
			 inherited-alist))))

Does that change anything? You could also get the full list of buffer
locals with `buffer-local-variables', and then assoc values from
inherited-alist in that list.

Hope that does something,
Eric




  reply	other threads:[~2013-11-22  6:27 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 [this message]
2013-11-22  7:09 ` Alex Kost
2013-11-22 15:50   ` Perry Smith
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

  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=877gc1t0gy.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --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).