tags:

views:

113

answers:

3

I would like to temporarily override the kill-new function. I have a way I want to reimplement kill-new that works in only in certain contexts, but I don't want to reimplement a special version of kill-region on top of that. (kill-new is called from kill-region)

Since Emacs Lisp uses dynamic scoping, this should be possible, right? (On the other hand, it seems that this would be an unsafe thing to support, and it might make me a bit nervous knowing that it is possible...)

I have experimented with using let and fset, but so far have found no way to get it to work as expected. So, hopefully someone can fill in the blank in the following pseudocode:

(defun my-kill-new (string &optional replace yank-handler) 
   (message "in my-kill-new!"))

(defun foo ()
   (some-form-that-binds-a-function (kill-new my-kill-new)
   (kill-region (point) (mark))))

What should some-form-that-binds-a-function be? Or am I barking up the wrong tree?

+3  A: 

Look at the advice package, which is very good at doing all of this.

Randal Schwartz
Thanks. Good advice. :-)
scrapdog
+3  A: 

Here is a solution:

(defadvice kill-new (around my-kill-new (string &optional replace yank-handler)) 
   (message "my-kill-new!"))


(defun foo ()
  (progn (ad-enable-advice 'kill-new 'around 'my-kill-new)
     (ad-activate 'kill-new)
     (kill-region (point) (mark))
     (ad-disable-advice 'kill-new 'around 'my-kill-new)
     (ad-activate 'kill-new)))
scrapdog
I think I prefer this to using flet for this purpose. Though there is more ceremony in doing it using advice, it gives me easy access to the existing implementation of kill-new.
scrapdog
You probably want to remove the advice with an unwind-protect, so that nonlocal exits also remove the advice. Just use flet.
jrockway
+7  A: 

Your some-form-that-binds-a-function is called flet, so you were close.

Ivan Andrus