all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to use defadvice without infinite loop
@ 2003-09-27 15:09 Ivan Kanis
  2003-09-27 20:22 ` Robert Marshall
  2003-09-28  5:14 ` Klaus Berndl
  0 siblings, 2 replies; 5+ messages in thread
From: Ivan Kanis @ 2003-09-27 15:09 UTC (permalink / raw)


I want to save VM files to _always_ be saved before calling the function
save-some buffers.

I am tired of answering 'yes' for these files when grepping, compiling
or quiting emacs.

I came up with a defadvice function but the proble is that it's
calling save-some-buffers and it produces an infinite loop. Is there a
way to avoid that?

(defadvice save-some-buffers(before my-save-some-buffers activate)
  "Ask to save buffers."
  (interactive)
  (save-some-buffers nil 'my-always-save))

(defun my-always-save()
  "Files to always save."
  (when buffer-file-name
	(when (string-match "^/home/ivank/mail" buffer-file-name)
	  (save-buffer))))

-- 
/-----------------------------------------------------------------------------*
| "When a man is attempting to describe another person's |                    |
| character, he may be right or he may be wrong, but     |      Ivan Kanis    |
| in one thing he will always succeed,that is, in        | Software Developper|
| describing himself."                                   |     www.kanis.cc   |
| (Samuel Taylor Coleridge)                              |                    |
*-----------------------------------------------------------------------------/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use defadvice without infinite loop
  2003-09-27 15:09 How to use defadvice without infinite loop Ivan Kanis
@ 2003-09-27 20:22 ` Robert Marshall
  2003-09-28  5:14 ` Klaus Berndl
  1 sibling, 0 replies; 5+ messages in thread
From: Robert Marshall @ 2003-09-27 20:22 UTC (permalink / raw)


On 27 Sep 2003, Ivan Kanis wrote:

> I want to save VM files to _always_ be saved before calling the
> function save-some buffers.
> 
> I am tired of answering 'yes' for these files when grepping,
> compiling or quiting emacs.
> 
> I came up with a defadvice function but the proble is that it's
> calling save-some-buffers and it produces an infinite loop. Is there
> a way to avoid that?
> 
> (defadvice save-some-buffers(before my-save-some-buffers activate)
>   "Ask to save buffers."
>   (interactive)
>   (save-some-buffers nil 'my-always-save))
> 
> (defun my-always-save()
>   "Files to always save."
>   (when buffer-file-name
> 	(when (string-match "^/home/ivank/mail" buffer-file-name)
> 	  (save-buffer))))
> 

use 
(ad-set-arg 1 'my-always-save)
and don't call save-some-buffers in the advice

(totally untested... but the info on 'Argument Access in Advice' is worth reading)

Robert
-- 
La grenouille songe..dans son château d'eau

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use defadvice without infinite loop
  2003-09-27 15:09 How to use defadvice without infinite loop Ivan Kanis
  2003-09-27 20:22 ` Robert Marshall
@ 2003-09-28  5:14 ` Klaus Berndl
  2003-09-29  4:58   ` Ivan Kanis
  1 sibling, 1 reply; 5+ messages in thread
From: Klaus Berndl @ 2003-09-28  5:14 UTC (permalink / raw)


On 27 Sep 2003, Ivan Kanis wrote:

>  I want to save VM files to _always_ be saved before calling the function
>  save-some buffers.
>  
>  I am tired of answering 'yes' for these files when grepping, compiling
>  or quiting emacs.
>  
>  I came up with a defadvice function but the proble is that it's
>  calling save-some-buffers and it produces an infinite loop. Is there a
>  way to avoid that?
>  
>  (defadvice save-some-buffers(before my-save-some-buffers activate)
>    "Ask to save buffers."
>    (interactive)
>    (save-some-buffers nil 'my-always-save))

The infinite loop comes because the `save-some-buffers' in your advice calls
the adviced version of `save-some-buffers' again and then again and then
again.... Normally i should write here: RTFM ;-) You should definitely read
the advice info-manual - there are some very simple and helpful examples in
it!

(defadvice save-some-buffers(before my-save-some-buffers activate)
  "Ask to save buffers."
  (ad-set-arg 1 'my-always-save))

This is all...test it!

>  
>  (defun my-always-save()
>    "Files to always save."
>    (when buffer-file-name
>  	(when (string-match "^/home/ivank/mail" buffer-file-name)
>  	  (save-buffer))))

Ciao,
Klaus

-- 
Klaus Berndl			mailto: klaus.berndl@sdm.de
sd&m AG				http://www.sdm.de
software design & management	
Thomas-Dehler-Str. 27, 81737 München, Germany
Tel +49 89 63812-392, Fax -220

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use defadvice without infinite loop
  2003-09-28  5:14 ` Klaus Berndl
@ 2003-09-29  4:58   ` Ivan Kanis
  2003-09-29 15:08     ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Ivan Kanis @ 2003-09-29  4:58 UTC (permalink / raw)



    Klaus> (defadvice save-some-buffers(before my-save-some-buffers
    Klaus> activate)
    Klaus>   "Ask to save buffers."  (ad-set-arg 1 'my-always-save))

    Klaus> This is all...test it!

It doesn't work, I need to call the function save-some-buffers
twice. I am not trying to substitute it's arguments. I have found this
code prevents the infinite loop, so far it seems to work well...

(defadvice save-some-buffers(before my-save-some-buffers activate)
  "Ask to save buffers."
  (interactive)
  ;; Prevent infinite loop
  (ad-deactivate-regexp "my-save-some-buffers")
  (save-some-buffers nil 'my-always-save)
  (ad-activate-regexp "my-save-some-buffers"))

(defun my-always-save()
  "Files to always save."
  (when buffer-file-name
	(when (string-match "^/home/ivank/mail" buffer-file-name)
	  (vm-save-buffer nil))))

-- 
/-----------------------------------------------------------------------------*
|    "Love your enemy, it will drive him nuts."    |         Ivan Kanis       |
|    (Eleanor Doan)                                |    Software Developper   |
|                                                  |        www.kanis.cc      |
*-----------------------------------------------------------------------------/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use defadvice without infinite loop
  2003-09-29  4:58   ` Ivan Kanis
@ 2003-09-29 15:08     ` Stefan Monnier
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2003-09-29 15:08 UTC (permalink / raw)


> (defadvice save-some-buffers(before my-save-some-buffers activate)
>   "Ask to save buffers."
>   (interactive)
>   ;; Prevent infinite loop
>   (ad-deactivate-regexp "my-save-some-buffers")
>   (save-some-buffers nil 'my-always-save)
>   (ad-activate-regexp "my-save-some-buffers"))

You can try an `around' advice instead of a `before' advice, and then put
two `ad-do-it' in there, modifying the args in between the two calls.
Pretty ugly as well.  Maybe the doc suggests a cleaner way to do what
you want.


        Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-09-29 15:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-27 15:09 How to use defadvice without infinite loop Ivan Kanis
2003-09-27 20:22 ` Robert Marshall
2003-09-28  5:14 ` Klaus Berndl
2003-09-29  4:58   ` Ivan Kanis
2003-09-29 15:08     ` Stefan Monnier

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.