2

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
                          ...)
Drew
  • 77,472
  • 10
  • 114
  • 243

1 Answers1

5

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?

wasamasa
  • 22,178
  • 1
  • 66
  • 99