0

I'd like to use a particular theme. To this end, I've put the following in my init.el:

;; Setup org-mode theme
(defun org_setup()
  "Setup org-mode"
  (load-theme 'doom-laserwave)
  )

;; Startup hook for org-mode
(add-hook 'org-mode-hook
      (org_setup))

Curiously, whatever file I open (e.g., even init.el) this theme is used. Why is that? My (limited) understanding is that this code should only be run if org-mode is initiated. Am I doing something daft?

4
  • Does it happen with emacs -q -l minimal.el and a minimal init file that just contains the above code? If not, bisect your init file to figure out who the culprit is.
    – NickD
    Nov 6 '20 at 16:39
  • With just the code above I get Unable to find theme file for ‘doom-laserwave’.
    – Lyngbakr
    Nov 6 '20 at 16:50
  • 1
    I need more coffee: @lawlist saw the problem exactly. As an explanation, (func) calls the function, so what you are doing is calling the function and adding its return value to the hook. What you want to do instead is add the function itself to the hook, so it can be called later.
    – NickD
    Nov 6 '20 at 17:16
  • @NickD Ah, I see. Thanks for the insight!
    – Lyngbakr
    Nov 6 '20 at 17:20
4

Assuming that the O.P. meant to use an underscore in the function name of org_setup (which is possible, but not the standard naming convention), then the problem is with the call to add-hook itself. Instead of using (org_setup), it should be 'org_setup. Try changing the call to add-hook as follows:

(add-hook 'org-mode-hook 'org_setup)
3
  • That's exactly the solution! Can you explain why (org_setup) doesn't work, but 'org_setup does? My thought was that the former simply called my function.
    – Lyngbakr
    Nov 6 '20 at 17:11
  • 2
    Using the form of (org_setup) as the second argument to add-hook, results in the immediate evaluation of that function instead of adding the function to the org-mode-hook. Alternatively, you could have used (add-hook 'org-mode-hook (lambda () (load-theme 'doom-laserwave))). However, many users recommend writing a separate function to be attached to the mode hook so that inspecting the hook with C-h v (aka M-x describe-variable) is more readable and using remove-hook is also easier when a separate function is used.
    – lawlist
    Nov 6 '20 at 17:32
  • 2
    The former calls the function, which is why the theme is always getting applied. You want to save the function itself (not its return value) in the hook to be called later.
    – erikstokes
    Nov 6 '20 at 18:12

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.