0

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?

lawlist
  • 18,826
  • 5
  • 37
  • 118
HappyFace
  • 751
  • 4
  • 16
  • Here is a link to a recent example that I wrote up, which is not specific to `helm-`: https://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)` – lawlist Aug 05 '19 at 19:04
  • @lawlist The problem is that the helm function is not defined by me, and it is not going to be `funcall`ed. – HappyFace Aug 05 '19 at 19:09
  • 1
    One idea would be to rewrite `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)`. – lawlist Aug 05 '19 at 19:17
  • I suggest you ask the Helm maintainer, stating what you are trying to do (the goal, not just what you've tried so far). – Drew Aug 05 '19 at 19:28
  • 1
    You can also achieve something similar as @lawlist proposed with around advice. – John Kitchin Aug 05 '19 at 22:08
  • @JohnKitchin A comment actually answering the question is often a game-stopper here. Please write an answer instead of a comment the next time. This time I had some stuff to add (the hook alternative) and took the chance to end this. – Tobias Aug 05 '19 at 23:09

1 Answers1

1

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)
Tobias
  • 32,569
  • 1
  • 34
  • 75