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?

6
  • Here is a link to a recent example that I wrote up, which is not specific to 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)
    – lawlist
    Aug 5, 2019 at 19:04
  • @lawlist The problem is that the helm function is not defined by me, and it is not going to be funcalled.
    – HappyFace
    Aug 5, 2019 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 5, 2019 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 5, 2019 at 19:28
  • 1
    You can also achieve something similar as @lawlist proposed with around advice. Aug 5, 2019 at 22:08

1 Answer 1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.