I want to override the function helm-swoop-pre-input-optimize
in man
mode using its hook, but not anywhere else. Is there something similar to make-local-variable
for functions?
1 Answer
At first the general statement: There is no buffer-local setting for the function cell of a symbol.
Now, I am just putting together the comments of lawlist and John Kitchin.
One can call an overriding function assigned to a buffer-local variable in an :around
advice of helm-swoop-pre-input-optimize
:
(defvar-local helm-swoop-pre-input-optimize-function nil
"Function overriding `helm-swoop-pre-input-optimize'.")
(define-advice helm-swoop-pre-input-optimize (:around (fun &rest args) override)
"Override `helm-swoop-pre-input-optimize'.
Use the function registered at `helm-swoop-pre-input-optimize-function'
instead of `helm-swoop-pre-input-optimize' if the value of
`helm-swoop-pre-input-optimize-function' is non-nil."
(if helm-swoop-pre-input-optimize-function
(apply helm-swoop-pre-input-optimize-function args)
(apply fun args)))
(defun my-setup-Man-mode ()
"My personal setup for `Man-mode'."
(setq helm-swoop-pre-input-optimize-function
#'MY-SPECIAL-helm-swoop-pre-input-optimize))
(add-hook 'Man-mode #'my-setup-Man-mode)
Replace MY-SPECIAL-helm-swoop-pre-input-optimize
with your special override for Man-mode
.
This solution is similar to that one on stackoverflow.
Since I do not use helm
you have to do the testing yourself.
A slight variation:
Instead of a buffer local variable you can also use an abnormal hook variable.
Add your function special to Man-mode
as buffer-local hook to that variable.
(defvar helm-swoop-pre-input-optimize-functions nil
"Functions overriding `helm-swoop-pre-input-optimize'.
They are called with the same arguments as
`helm-swoop-pre-input-optimize' until one of them succeeds.")
(define-advice helm-swoop-pre-input-optimize (:around (fun &rest args) override)
"Override `helm-swoop-pre-input-optimize'.
Use the functions registered at `helm-swoop-pre-input-optimize-functions'
instead of `helm-swoop-pre-input-optimize' if one of them succeeds.
Use the original `helm-swoop-pre-input-optimize' otherwise."
(or (run-hook-with-args-until-success 'helm-swoop-pre-input-optimize-functions)
(apply fun args)))
(defun my-setup-Man-mode ()
"My personal setup for `Man-mode'."
(add-hook 'helm-swoop-pre-input-optimize-functions
#'MY-SPECIAL-helm-swoop-pre-input-optimize
nil 'local))
(add-hook 'Man-mode #'my-setup-Man-mode)
helm-
: emacs.stackexchange.com/a/51228/2287 In that example, I create a simple function (bound to a local-variable) that generates a message ... it is activated by calling(funcall NAME-OF-LOCAL-VARIABLE)
funcall
ed.helm-swoop-pre-input-optimize
to do two things:(if (eq major-mode 'Man-mode) (progn ... CUSTOM-STUFF / MAYBE FUNCALL LOCAL-VARIABLE) ... INSERT GUTS OF ORIGINAL FUNCTION)
. Another idea would be to edit all places where helm calls that function, and change it to(funcall MY-LOCAL-VARAIBLE)
.