all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Redefining functions and variables
@ 2010-07-27 15:12 Elena
  2010-07-27 20:21 ` Stefan Monnier
  0 siblings, 1 reply; 21+ messages in thread
From: Elena @ 2010-07-27 15:12 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I've noticed that some user contributed packages redefine standard
functions or variables.

I wonder whether there is a way to catch such redefinitions whenever
they happen (which also would help when accidentally redefining
something) besides rewriting "defun", "defvar", ecc.

Moreover, I wonder whether redefining a function by means of "defun"
gets same results as assigning a new function to a symbol using
"fset".

Thanks.


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

* Re: Redefining functions and variables
  2010-07-27 15:12 Redefining functions and variables Elena
@ 2010-07-27 20:21 ` Stefan Monnier
  2010-07-27 22:16   ` Elena
  0 siblings, 1 reply; 21+ messages in thread
From: Stefan Monnier @ 2010-07-27 20:21 UTC (permalink / raw)
  To: help-gnu-emacs

> I've noticed that some user contributed packages redefine standard
> functions or variables.

That's usually a bad idea.  The better way to do it is via defadvice,
which lets you do it in a way that keeps track of the presence of such
a change and knows how to combine multiple such changes.

> I wonder whether there is a way to catch such redefinitions whenever
> they happen (which also would help when accidentally redefining
> something)

Not sure what you mean by that.  I guess that means "no".

> Moreover, I wonder whether redefining a function by means of "defun"
> gets same results as assigning a new function to a symbol using
> "fset".

Mostly, yes.  But defun keeps track of the file where the defun happened
(for things like C-h f), whereas fset doesn't do any such thing.


        Stefan


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

* Re: Redefining functions and variables
  2010-07-27 20:21 ` Stefan Monnier
@ 2010-07-27 22:16   ` Elena
  2010-07-28  0:35     ` Andreas Politz
  0 siblings, 1 reply; 21+ messages in thread
From: Elena @ 2010-07-27 22:16 UTC (permalink / raw)
  To: help-gnu-emacs

On 27 Lug, 22:21, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > I wonder whether there is a way to catch such redefinitions whenever
> > they happen (which also would help when accidentally redefining
> > something)
>
> Not sure what you mean by that.

Let's assume we have two definitions for the same function:

;; File A (loaded first)
(defun foo ()
...

;; File B (loaded later)
(defun foo () ;; This is a redefinition: I'd like to get a warning.
...

Since redefining functions is the heart of interactive programming,
such a warning should be issued only while loading files. That way,
you could consider whether to rename the second function or to rewrite
it as an advice (as you suggested).

If such a goal can only be achieved by rewriting "defun" and checking
by means of "fboundp" whether the function has already been defined,
here is my (failed, as noted) attempt:

(defmacro defun (name args &rest body)
  `(progn
     ;; `load-file-name' is not null only if we are loading a file.
     (when (and load-file-name
		;; FAIL: I don't know how to quote the value of `name'.
		(fboundp ,name))
       (message "Warning: %s is being redefined in %s."
		;; FAIL: I don't know how to quote the value of `name'.
		(symbol-name ,name)
		load-file-name)
       (defun ,name ,args
	 ,@body))))

Any suggestions? Thanks.


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

* Re: Redefining functions and variables
  2010-07-27 22:16   ` Elena
@ 2010-07-28  0:35     ` Andreas Politz
  2010-07-28  7:51       ` Elena
  0 siblings, 1 reply; 21+ messages in thread
From: Andreas Politz @ 2010-07-28  0:35 UTC (permalink / raw)
  To: help-gnu-emacs

Elena <egarrulo@gmail.com> writes:

> On 27 Lug, 22:21, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
>> > I wonder whether there is a way to catch such redefinitions whenever
>> > they happen (which also would help when accidentally redefining
>> > something)
>>
>> Not sure what you mean by that.
>
> Let's assume we have two definitions for the same function:
>
> ;; File A (loaded first)
> (defun foo ()
> ...
>
> ;; File B (loaded later)
> (defun foo () ;; This is a redefinition: I'd like to get a warning.
> ...
>
> Since redefining functions is the heart of interactive programming,
> such a warning should be issued only while loading files. That way,
> you could consider whether to rename the second function or to rewrite
> it as an advice (as you suggested).
>

I think `defun' does not take no advice.

> If such a goal can only be achieved by rewriting "defun" and checking
> by means of "fboundp" whether the function has already been defined,
> here is my (failed, as noted) attempt:
>
> (defmacro defun (name args &rest body)
>   `(progn
>      ;; `load-file-name' is not null only if we are loading a file.
>      (when (and load-file-name
> 		;; FAIL: I don't know how to quote the value of `name'.
> 		(fboundp ,name))

                (fboundp ',name))

Why not use `quote'.

>        (message "Warning: %s is being redefined in %s."
> 		;; FAIL: I don't know how to quote the value of `name'.
> 		(symbol-name ,name)
> 		load-file-name)
>        (defun ,name ,args

Of course this leads to an endless recursive loop, expanding your macro
again and again ...

(funcall (symbol-function 'defun) 'foo nil)
does not seem to work either, which would mean that you can't save
`defun's definition.
>
> 	 ,@body))))
>
> Any suggestions? Thanks.

-ap


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

* Re: Redefining functions and variables
  2010-07-28  0:35     ` Andreas Politz
@ 2010-07-28  7:51       ` Elena
  2010-07-28 14:29         ` Elena
  0 siblings, 1 reply; 21+ messages in thread
From: Elena @ 2010-07-28  7:51 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 28, 12:35 am, Andreas Politz <poli...@fh-trier.de> wrote:
> Why not use `quote'.

Because it didn't work. This:

(fboundp ',name))

was expanded by `macroexpand' to:

(fboundp ...)) ;; Exactly as shown.

Using `quote' like this:

(fboundp (quote ,name)))

I got the same result.

> Of course this leads to an endless recursive loop, expanding your macro
> again and again ...

Then I should have tested the macro with `macroexpand-all' instead of
`macroexpand'. However, I've tried using the macro, and it seemed to
work, albeit it choked on the unquoted symbol passed to `fboundp'.

> (funcall (symbol-function 'defun) 'foo nil)
> does not seem to work either, which would mean that you can't save
> `defun's definition.

`defun' is not a function, it's a macro (defined in C source code).

Thanks for answering.


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

* Re: Redefining functions and variables
  2010-07-28  7:51       ` Elena
@ 2010-07-28 14:29         ` Elena
  2010-07-28 18:48           ` Andreas Politz
  0 siblings, 1 reply; 21+ messages in thread
From: Elena @ 2010-07-28 14:29 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 28, 7:51 am, Elena <egarr...@gmail.com> wrote:
> > Of course this leads to an endless recursive loop, expanding your macro
> > again and again ...
>
> Then I should have tested the macro with `macroexpand-all' instead of
> `macroexpand'. However, I've tried using the macro, and it seemed to
> work, albeit it choked on the unquoted symbol passed to `fboundp'.

I think it seemed to work because the macro was interpreted, therefore
it was expanded only once before the failure.


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

* Re: Redefining functions and variables
  2010-07-28 14:29         ` Elena
@ 2010-07-28 18:48           ` Andreas Politz
  2010-07-28 20:37             ` Pascal J. Bourguignon
  0 siblings, 1 reply; 21+ messages in thread
From: Andreas Politz @ 2010-07-28 18:48 UTC (permalink / raw)
  To: help-gnu-emacs

Elena <egarrulo@gmail.com> writes:

> On Jul 28, 7:51 am, Elena <egarr...@gmail.com> wrote:
>> > Of course this leads to an endless recursive loop, expanding your macro
>> > again and again ...
>>
>> Then I should have tested the macro with `macroexpand-all' instead of
>> `macroexpand'. However, I've tried using the macro, and it seemed to
>> work, albeit it choked on the unquoted symbol passed to `fboundp'.
>
> I think it seemed to work because the macro was interpreted, therefore
> it was expanded only once before the failure.

Don't know, but defining `defun' as a macro usually deletes the original
subst (which is a special form, which is kind of like a macro).

(defvar defun-subst (symbol-function 'defun))

(defmacro defun (name args &rest body)
  `(defun ,name ,args ,@body))

(defun foo ())
;; Enters the debugger because of recursion limit reached.

;; Restore original defun
(fset 'defun defun-subst)

-ap


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

* Re: Redefining functions and variables
  2010-07-28 18:48           ` Andreas Politz
@ 2010-07-28 20:37             ` Pascal J. Bourguignon
  2010-07-29  8:32               ` Elena
  0 siblings, 1 reply; 21+ messages in thread
From: Pascal J. Bourguignon @ 2010-07-28 20:37 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Politz <politza@fh-trier.de> writes:

> Elena <egarrulo@gmail.com> writes:
>
>> On Jul 28, 7:51 am, Elena <egarr...@gmail.com> wrote:
>>> > Of course this leads to an endless recursive loop, expanding your macro
>>> > again and again ...
>>>
>>> Then I should have tested the macro with `macroexpand-all' instead of
>>> `macroexpand'. However, I've tried using the macro, and it seemed to
>>> work, albeit it choked on the unquoted symbol passed to `fboundp'.
>>
>> I think it seemed to work because the macro was interpreted, therefore
>> it was expanded only once before the failure.
>
> Don't know, but defining `defun' as a macro usually deletes the original
> subst (which is a special form, which is kind of like a macro).
>
> (defvar defun-subst (symbol-function 'defun))
>
> (defmacro defun (name args &rest body)
>   `(defun ,name ,args ,@body))
>
> (defun foo ())
> ;; Enters the debugger because of recursion limit reached.
>
> ;; Restore original defun
> (fset 'defun defun-subst)

Another way:

(defvar old-defun 'defun) ; the symbol!
(unintern 'defun)

(defmacro defun (name args &rest body)
  `(progn
      (message "defining %S" name)
      (,old-defun ,name ,args ,@body)))

In this case there's no infinite recursion.

      
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: Redefining functions and variables
  2010-07-28 20:37             ` Pascal J. Bourguignon
@ 2010-07-29  8:32               ` Elena
  2010-07-29  9:57                 ` Stefan Monnier
                                   ` (2 more replies)
  0 siblings, 3 replies; 21+ messages in thread
From: Elena @ 2010-07-29  8:32 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 28, 8:37 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Andreas Politz <poli...@fh-trier.de> writes:
> > Elena <egarr...@gmail.com> writes:
>
> >> On Jul 28, 7:51 am, Elena <egarr...@gmail.com> wrote:
> >>> > Of course this leads to an endless recursive loop, expanding your macro
> >>> > again and again ...
>
> >>> Then I should have tested the macro with `macroexpand-all' instead of
> >>> `macroexpand'. However, I've tried using the macro, and it seemed to
> >>> work, albeit it choked on the unquoted symbol passed to `fboundp'.
>
> >> I think it seemed to work because the macro was interpreted, therefore
> >> it was expanded only once before the failure.
>
> > Don't know, but defining `defun' as a macro usually deletes the original
> > subst (which is a special form, which is kind of like a macro).
>
> > (defvar defun-subst (symbol-function 'defun))
>
> > (defmacro defun (name args &rest body)
> >   `(defun ,name ,args ,@body))
>
> > (defun foo ())
> > ;; Enters the debugger because of recursion limit reached.
>
> > ;; Restore original defun
> > (fset 'defun defun-subst)
>
> Another way:
>
> (defvar old-defun 'defun) ; the symbol!
> (unintern 'defun)
>
> (defmacro defun (name args &rest body)
>   `(progn
>       (message "defining %S" name)
>       (,old-defun ,name ,args ,@body)))

This is very close to what I was looking for. Thanks, Pascal.

My modified macro is below. Why ',name is expanded as ... (as shown by
`macroexpand-all'?

(defvar old-defun 'defun)           ; the symbol!
(unintern 'defun)

(defmacro defun (name args &rest body)
    `(progn
         ;; `load-file-name' is not null only if we are loading a
file.
         (when (and load-file-name
                    (fboundp ',name))
             (message "Warning: %s is being redefined in %s."
                      (symbol-name ',name)
                      load-file-name)
             (,old-defun ,name ,args
                         ,@body))))



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

* Re: Redefining functions and variables
  2010-07-29  8:32               ` Elena
@ 2010-07-29  9:57                 ` Stefan Monnier
  2010-07-29 10:16                   ` Elena
                                     ` (2 more replies)
  2010-07-29 10:03                 ` Pascal J. Bourguignon
  2010-07-29 20:35                 ` Johan Bockgård
  2 siblings, 3 replies; 21+ messages in thread
From: Stefan Monnier @ 2010-07-29  9:57 UTC (permalink / raw)
  To: help-gnu-emacs

> (defmacro defun (name args &rest body)

Just as is the case for `defun', redefining macros with `defmacro'
is problematic.  I really recommend not to do that and use defadvice
instead, which was designed specifically for this purpose.


        Stefan


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

* Re: Redefining functions and variables
  2010-07-29  8:32               ` Elena
  2010-07-29  9:57                 ` Stefan Monnier
@ 2010-07-29 10:03                 ` Pascal J. Bourguignon
  2010-07-29 10:28                   ` Elena
  2010-07-29 20:35                 ` Johan Bockgård
  2 siblings, 1 reply; 21+ messages in thread
From: Pascal J. Bourguignon @ 2010-07-29 10:03 UTC (permalink / raw)
  To: help-gnu-emacs

Elena <egarrulo@gmail.com> writes:

> My modified macro is below. Why ',name is expanded as ... (as shown by
> `macroexpand-all'?
>
> (defvar old-defun 'defun)           ; the symbol!
> (unintern 'defun)
>
> (defmacro defun (name args &rest body)
>     `(progn
>          ;; `load-file-name' is not null only if we are loading a
> file.
>          (when (and load-file-name
>                     (fboundp ',name))
>              (message "Warning: %s is being redefined in %s."
>                       (symbol-name ',name)
>                       load-file-name)
>              (,old-defun ,name ,args
>                          ,@body))))

It is not.

(macroexpand '(defun. test (a) (+ 1 a)))
;; --> (progn (when (and load-file-name (fboundp (quote test))) (message "Warning: %s is being redefined in %s." (symbol-name (quote test)) load-file-name) (defun test (a) (+ 1 a))))

the "..." are only used to display the list when print-length or
eval-expression-print-length are not nil.  See also print-level and
eval-expression-print-level.


%s can format symbols too:

(format ">> %s <<" 'example)
;; --> ">> example <<"

And you had your the old defun inside the when!

(defmacro defun (name args &rest body)
  `(progn
     ;; `load-file-name' is not null only if we are loading a file.
     (when (and load-file-name (fboundp ',name))
       (message "Warning: %s is being redefined in %s." ',name load-file-name))
     (,old-defun ,name ,args  ,@body)))

Note: while name is known at macroexpansion time, you should refrain
to insert it in the string like this:

      (message ,(format "Warning: %s is being redefined in %%s." name) load-file-name)

since name could contain percents and then you'd have build a wrong
format string for message.   If you want to do that, you must escape
the percents:

      (message ,(format "Warning: %s is being redefined in %%s." 
                        (escape-percent name))
               load-file-name)

with:

(defun escape-percent (string-designator)
    (let ((string (etypecase string-designator
                     (string string-designator) 
                     (symbol (symbol-name string-designator))
                     (character (string string-designator)))))
      (unsplit-string (split-string string "%" nil) "%%")))

(defun unsplit-string (string-list &rest separator)
  "Does the inverse than split-string. If no separator is provided 
then a simple space is used."
  (if (null separator)
      (setq separator " ")
      (if (= 1 (length separator))
          (setq separator (car separator))
          (error "unsplit-string: Too many separator arguments.")))
  (if (not (char-or-string-p separator))
      (error "unsplit-string: separator must be a string or a char."))
  (apply 'concat (list-insert-separator string-list separator)))

(mapcar 'escape-percent '("abc" abc % %make- make-10%-of-profit %%internal%%))
;; --> ("abc" "abc" "%%" "%%make-" "make-10%%-of-profit" "%%%%internal%%%%")


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: Redefining functions and variables
  2010-07-29  9:57                 ` Stefan Monnier
@ 2010-07-29 10:16                   ` Elena
  2010-07-29 13:14                     ` Stefan Monnier
  2010-07-30 19:32                     ` Uday S Reddy
  2010-07-29 10:31                   ` Andreas Politz
  2010-07-29 12:41                   ` Tim X
  2 siblings, 2 replies; 21+ messages in thread
From: Elena @ 2010-07-29 10:16 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 29, 9:57 am, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> Just as is the case for `defun', redefining macros with `defmacro'
> is problematic.  I really recommend not to do that and use defadvice
> instead, which was designed specifically for this purpose.

Do you mean macros can be adviced too? Something like this (it does
not compile):

(defadvice defun (before redefinition-warning activate)
	(let ((name (ad-get-arg 0)))
	 (when (and load-file-name
				(fboundp (symbol name)))
		 (message "Warning: %s is being redefined in %s."
				  (symbol-name name)
				  load-file-name))))

?





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

* Re: Redefining functions and variables
  2010-07-29 10:03                 ` Pascal J. Bourguignon
@ 2010-07-29 10:28                   ` Elena
  0 siblings, 0 replies; 21+ messages in thread
From: Elena @ 2010-07-29 10:28 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 29, 10:03 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> the "..." are only used to display the list when print-length or
> eval-expression-print-length are not nil.  See also print-level and
> eval-expression-print-level.

Didn't know about this. Thanks.

> %s can format symbols too:
>
> (format ">> %s <<" 'example)
> ;; --> ">> example <<"
>
> And you had your the old defun inside the when!

Gosh! What an oversight... Sorry.

> Note: while name is known at macroexpansion time, you should refrain
> to insert it in the string like this:
>
>       (message ,(format "Warning: %s is being redefined in %%s." name) load-file-name)
>
> since name could contain percents and then you'd have build a wrong
> format string for message.

I don't understand this. Doc about `message' says:

> Note: Use (message "%s" VALUE) to print the value of expressions and
> variables to avoid accidentally interpreting `%' as format specifiers.

And in fact:

(message "%s" 'example%)

correctly outputs:

"example%"


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

* Re: Redefining functions and variables
  2010-07-29  9:57                 ` Stefan Monnier
  2010-07-29 10:16                   ` Elena
@ 2010-07-29 10:31                   ` Andreas Politz
  2010-07-29 11:46                     ` Johan Bockgård
  2010-07-29 12:41                   ` Tim X
  2 siblings, 1 reply; 21+ messages in thread
From: Andreas Politz @ 2010-07-29 10:31 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> (defmacro defun (name args &rest body)
>
> Just as is the case for `defun', redefining macros with `defmacro'
> is problematic.  I really recommend not to do that and use defadvice
> instead, which was designed specifically for this purpose.
>
>
>         Stefan

Last time I tried to advice `defun', it gave me a `invalid function'
error or some such.

-ap


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

* Re: Redefining functions and variables
  2010-07-29 10:31                   ` Andreas Politz
@ 2010-07-29 11:46                     ` Johan Bockgård
  2010-07-29 15:54                       ` Andreas Politz
  0 siblings, 1 reply; 21+ messages in thread
From: Johan Bockgård @ 2010-07-29 11:46 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Politz <politza@fh-trier.de> writes:

> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>> Just as is the case for `defun', redefining macros with `defmacro'
>> is problematic.  I really recommend not to do that and use defadvice
>> instead, which was designed specifically for this purpose.
>
> Last time I tried to advice `defun', it gave me a `invalid function'
> error or some such.

Emacs chokes on the irregular "[DOCSTRING]" argument:

    (defun NAME ARGLIST [DOCSTRING] BODY...)

You have to provide an explicit ARGLIST:

    (defadvice FUNCTION (CLASS NAME [POSITION] [ARGLIST] FLAG...)
      [DOCSTRING] [INTERACTIVE-FORM]
      BODY...)



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

* Re: Redefining functions and variables
  2010-07-29  9:57                 ` Stefan Monnier
  2010-07-29 10:16                   ` Elena
  2010-07-29 10:31                   ` Andreas Politz
@ 2010-07-29 12:41                   ` Tim X
  2 siblings, 0 replies; 21+ messages in thread
From: Tim X @ 2010-07-29 12:41 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> (defmacro defun (name args &rest body)
>
> Just as is the case for `defun', redefining macros with `defmacro'
> is problematic.  I really recommend not to do that and use defadvice
> instead, which was designed specifically for this purpose.
>
>
>         Stefan

If anyone wants a good example of what can be achieved just using
defadvice, I would strongly recommend looking at emacspeak. 

The emacspeak package transforms emacs into a powerful text-to-speech
system. Most of this is achieved by wrapping standard emacs functions in
defadvice, which in turn sends the relevant bit of text to a
text-to-speech synthesizer. 

Another similar package, which attempts to have minimal impact on how
emacs works is speechd.el. It also uses defadvice. 

I frequently use defadvice to customize how functions work, resolve bugs
while waiting for an update in some package or simply replace one
function with one I have written. It is a very useful and powerful tool. 

Tim

-- 
tcross (at) rapttech dot com dot au


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

* Re: Redefining functions and variables
  2010-07-29 10:16                   ` Elena
@ 2010-07-29 13:14                     ` Stefan Monnier
  2010-07-30 19:32                     ` Uday S Reddy
  1 sibling, 0 replies; 21+ messages in thread
From: Stefan Monnier @ 2010-07-29 13:14 UTC (permalink / raw)
  To: help-gnu-emacs

>> Just as is the case for `defun', redefining macros with `defmacro'
>> is problematic.  I really recommend not to do that and use defadvice
>> instead, which was designed specifically for this purpose.
> Do you mean macros can be adviced too?

That's not what I meant (I meant to redefine with `defadvice' rather than
with `defun'), but that's also the case (i.e. redefine with defadvice
rather than with defmacro).


        Stefan


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

* Re: Redefining functions and variables
  2010-07-29 11:46                     ` Johan Bockgård
@ 2010-07-29 15:54                       ` Andreas Politz
  0 siblings, 0 replies; 21+ messages in thread
From: Andreas Politz @ 2010-07-29 15:54 UTC (permalink / raw)
  To: help-gnu-emacs

Johan Bockgård <bojohan+news@gnu.org> writes:

> Andreas Politz <politza@fh-trier.de> writes:
>
>> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>
>>> Just as is the case for `defun', redefining macros with `defmacro'
>>> is problematic.  I really recommend not to do that and use defadvice
>>> instead, which was designed specifically for this purpose.
>>
>> Last time I tried to advice `defun', it gave me a `invalid function'
>> error or some such.
>
> Emacs chokes on the irregular "[DOCSTRING]" argument:
>
>     (defun NAME ARGLIST [DOCSTRING] BODY...)
>
> You have to provide an explicit ARGLIST:
>
>     (defadvice FUNCTION (CLASS NAME [POSITION] [ARGLIST] FLAG...)
>       [DOCSTRING] [INTERACTIVE-FORM]
>       BODY...)

Ah, ok - thank you. I guess, I did not ivestigate it very much back
then.

-ap


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

* Re: Redefining functions and variables
  2010-07-29  8:32               ` Elena
  2010-07-29  9:57                 ` Stefan Monnier
  2010-07-29 10:03                 ` Pascal J. Bourguignon
@ 2010-07-29 20:35                 ` Johan Bockgård
  2010-07-29 22:22                   ` Tim X
  2 siblings, 1 reply; 21+ messages in thread
From: Johan Bockgård @ 2010-07-29 20:35 UTC (permalink / raw)
  To: help-gnu-emacs

Elena <egarrulo@gmail.com> writes:

>> (defmacro defun (name args &rest body)
>>   `(progn
>>       (message "defining %S" name)
>>       (,old-defun ,name ,args ,@body)))
>
> This is very close to what I was looking for.

Note that neither defadvice nor redefinition of defun will work for
(already) byte compiled code.


(byte-compile (lambda () (defun foo ()))) =>

#[nil "..." [defalias foo #[nil "..." [nil] 1]] 3]
                ^
          no `defun' after byte compilation


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

* Re: Redefining functions and variables
  2010-07-29 20:35                 ` Johan Bockgård
@ 2010-07-29 22:22                   ` Tim X
  0 siblings, 0 replies; 21+ messages in thread
From: Tim X @ 2010-07-29 22:22 UTC (permalink / raw)
  To: help-gnu-emacs

Johan Bockgård <bojohan+news@gnu.org> writes:

> Elena <egarrulo@gmail.com> writes:
>
>>> (defmacro defun (name args &rest body)
>>>   `(progn
>>>       (message "defining %S" name)
>>>       (,old-defun ,name ,args ,@body)))
>>
>> This is very close to what I was looking for.
>
> Note that neither defadvice nor redefinition of defun will work for
> (already) byte compiled code.
>
>
> (byte-compile (lambda () (defun foo ()))) =>
>
> #[nil "..." [defalias foo #[nil "..." [nil] 1]] 3]
>                 ^
>           no `defun' after byte compilation

Not sure what you mean here, but I have lots of defadvice that is
defined for functions that are already defined and byte compiled which
work fine i.e. I use defadvice to advise standard emacs lisp functions
regularly without problems.

Tim

-- 
tcross (at) rapttech dot com dot au


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

* Re: Redefining functions and variables
  2010-07-29 10:16                   ` Elena
  2010-07-29 13:14                     ` Stefan Monnier
@ 2010-07-30 19:32                     ` Uday S Reddy
  1 sibling, 0 replies; 21+ messages in thread
From: Uday S Reddy @ 2010-07-30 19:32 UTC (permalink / raw)
  To: help-gnu-emacs

On 7/29/2010 11:16 AM, Elena wrote:
> On Jul 29, 9:57 am, Stefan Monnier<monn...@iro.umontreal.ca>  wrote:
>> Just as is the case for `defun', redefining macros with `defmacro'
>> is problematic.  I really recommend not to do that and use defadvice
>> instead, which was designed specifically for this purpose.
>
> Do you mean macros can be adviced too? Something like this (it does
> not compile):

No, Stefan is advising you not to mess with defun or defmacro.

These are essentially fundamental built-in operations of Elisp and changing 
them can produce very strange behaviors.

If you are trying to use user-contributed packages that are redefining standard 
Emacs functions, then you should seriously consider whether you should use such 
packages.  Emacs doesn't enforce the quality of packages you use.  You do.

Cheers,
Uday


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

end of thread, other threads:[~2010-07-30 19:32 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-27 15:12 Redefining functions and variables Elena
2010-07-27 20:21 ` Stefan Monnier
2010-07-27 22:16   ` Elena
2010-07-28  0:35     ` Andreas Politz
2010-07-28  7:51       ` Elena
2010-07-28 14:29         ` Elena
2010-07-28 18:48           ` Andreas Politz
2010-07-28 20:37             ` Pascal J. Bourguignon
2010-07-29  8:32               ` Elena
2010-07-29  9:57                 ` Stefan Monnier
2010-07-29 10:16                   ` Elena
2010-07-29 13:14                     ` Stefan Monnier
2010-07-30 19:32                     ` Uday S Reddy
2010-07-29 10:31                   ` Andreas Politz
2010-07-29 11:46                     ` Johan Bockgård
2010-07-29 15:54                       ` Andreas Politz
2010-07-29 12:41                   ` Tim X
2010-07-29 10:03                 ` Pascal J. Bourguignon
2010-07-29 10:28                   ` Elena
2010-07-29 20:35                 ` Johan Bockgård
2010-07-29 22:22                   ` Tim X

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.