0

On my config i have too many add-hook,Is there any way to merge them and make them more beautiful.

  (add-hook 'go-mode-hook #'my-go-project-setup)
  (add-hook 'go-mode-hook #'lsp-deferred)
  (add-hook 'go-mode-hook #'lsp-go-install-save-hooks)
  (add-hook 'go-mode-hook #'rainbow-delimiters-mode)
  (add-hook 'go-mode-hook #'galaxy/go-defun-setup)

maybe write a macro is best? then it will like this? how to write this macro or other best way?

(add-hook-x 'go-mode-hook #'lsp-deferred
                          #'lsp-go-instal-save-hooks
                          ...)
4

No need for a macro, all you need is a function doing a loop:

(defun add-hook-x (hook &rest functions)
  (dolist (function functions)
    (add-hook hook function)))

There isn't really a need for this either once you realize that you can bundle all these functions added to the hook into a single function:

(defun my-go-mode-setup ()
  (my-go-project-setup)
  (lsp-deferred)
  (lsp-go-install-save-hooks)
  (rainbow-delimiters-mode)
  (galaxy/go-defun-setup))

(add-hook 'go-mode-hook #'my-go-mode-setup)

For the opposite case where you want to add a single function to many hooks you might find this question of mine useful: How do I "group" hooks?

| improve this answer | |
  • i know write them to a function , but it looks like ugly..thanks. – xiaodong huan Mar 6 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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