Skip to main content
added 13 characters in body
Source Link
Drew
  • 79.1k
  • 10
  • 123
  • 257

You can do this. I think it does what you're looking for.

(defalias 'bar 'forward-char)
(advice-add 'bar :after (lambda (&rest _) (message "AFTER")))
M-x forward-char ; No message "AFTER"
M-x bar          ; Message "AFTER"
(symbol-function 'forward-char) ; => #<subr forward-char>
(symbol-function 'bar) ; => forward-char

IOW, you advise bar, not the original function, forward-char. And you defalias bar to forward-char, not to (symbol-function 'forward-char).

Note this difference:

(defalias 'foo (symbol-function 'forward-char))
(eq (symbol-function 'foo) (symbol-function 'forward-char)) ; => t

(defalias 'bar 'forward-char)
(eq (symbol-function 'bar) (symbol-function 'forward-char)) ; => nil

(symbol-function 'forward-char) ; => #<subr forward-char>
(symbol-function 'bar) ; => forward-char

You can do this. I think it does what you're looking for.

(defalias 'bar 'forward-char)
(advice-add 'bar :after (lambda (&rest _) (message "AFTER")))
M-x forward-char ; No message "AFTER"
M-x bar          ; Message "AFTER"

IOW, you advise bar, not the original function, forward-char. And you defalias bar to forward-char, not to (symbol-function 'forward-char).

Note this difference:

(defalias 'foo (symbol-function 'forward-char))
(eq (symbol-function 'foo) (symbol-function 'forward-char)) ; => t

(defalias 'bar 'forward-char)
(eq (symbol-function 'bar) (symbol-function 'forward-char)) ; => nil

(symbol-function 'forward-char) ; => #<subr forward-char>
(symbol-function 'bar) ; => forward-char

You can do this. I think it does what you're looking for.

(defalias 'bar 'forward-char)
(advice-add 'bar :after (lambda (&rest _) (message "AFTER")))
M-x forward-char ; No message "AFTER"
M-x bar          ; Message "AFTER"
(symbol-function 'forward-char) ; => #<subr forward-char>
(symbol-function 'bar) ; => forward-char

IOW, you advise bar, not the original function, forward-char. And you defalias bar to forward-char, not to (symbol-function 'forward-char).

Note this difference:

(defalias 'foo (symbol-function 'forward-char))
(eq (symbol-function 'foo) (symbol-function 'forward-char)) ; => t

(defalias 'bar 'forward-char)
(eq (symbol-function 'bar) (symbol-function 'forward-char)) ; => nil
Source Link
Drew
  • 79.1k
  • 10
  • 123
  • 257

You can do this. I think it does what you're looking for.

(defalias 'bar 'forward-char)
(advice-add 'bar :after (lambda (&rest _) (message "AFTER")))
M-x forward-char ; No message "AFTER"
M-x bar          ; Message "AFTER"

IOW, you advise bar, not the original function, forward-char. And you defalias bar to forward-char, not to (symbol-function 'forward-char).

Note this difference:

(defalias 'foo (symbol-function 'forward-char))
(eq (symbol-function 'foo) (symbol-function 'forward-char)) ; => t

(defalias 'bar 'forward-char)
(eq (symbol-function 'bar) (symbol-function 'forward-char)) ; => nil

(symbol-function 'forward-char) ; => #<subr forward-char>
(symbol-function 'bar) ; => forward-char