help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to shadow a function temporarily? (flet and cl-flet)


From: Stefan Monnier
Subject: Re: How to shadow a function temporarily? (flet and cl-flet)
Date: Sun, 26 Jan 2014 17:08:00 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

> (defun my-read-color (&optional prompt convert-to-RGB allow-empty-name msg)
>   (interactive "i\np\ni\np")
>   (flet ((foreground-color-at-point () (my-foreground-color-at-point))
>          (background-color-at-point () (my-background-color-at-point)))
>     (read-color prompt convert-to-RGB allow-empty-name msg)))

> Now i don't see another solution except of using the code of
> `read-color' in `my-read-color'.

IIUC foreground-color-at-point and background-color-at-point don't
suffer from the same problem as + when byte-compiled, so the above
should work.

But I'd recommend you use advice, which is cleaner (e.g. C-h f will
make it clear that something's messing with the function):

   (defvar use-my-color-improvement nil)

   (defadvice foreground-color-at-point (around my-improvement activate)
     (if use-my-color-improvement
         (setq ad-return-value (my-foreground-color-at-point))
       ad-do-it))

   (defadvice background-color-at-point (around my-improvement activate)
     (if use-my-color-improvement
         (setq ad-return-value (my-background-color-at-point))
       ad-do-it))

   ...
   ...(let ((use-my-color-improvement t))
        (read-color prompt convert-to-RGB allow-empty-name msg))
   ...

If that's for code within Emacs rather than a separate package, you'll
want to use the new advice-add instead of defadvice, of course.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]