3

Can I wrap the following code into a function (e.g, my-load-theme) with a theme name as the input argument (you may include more, e.g., frame if necessary)? That is, I can use (my-load-theme 'solarized) to equivalently run the following codes. How to define such a function? I got stuck due to the embedment of load-theme in the lambda function required by after-make-frame-functions function.

Looking forward to elisp masters! Many thanks!

(add-hook 'after-make-frame-functions
    (lambda (frame)
      (select-frame frame)
      (when (display-graphic-p frame)
        (load-theme 'solarized t))))

Thanks to Stefan and comments from https://emacs-china.org/t/topic/5387, here are two solutions:

use lexical-binding

;; -*- lexical-binding:t -*-
(defun my-load-theme (theme)
  (add-hook 'after-make-frame-functions
    (lambda (frame)
      (select-frame frame)
      (when (display-graphic-p frame)
        (load-theme theme t)))))

when call the function: (my-load-theme 'solarized)

(Note: put ;; -*- lexical-binding:t -*- in the top line of your .el file)

use dynamic-binding

(defun my-load-theme (theme)
  (add-hook 'after-make-frame-functions
    `(lambda (frame)
      (select-frame frame)
      (when (display-graphic-p frame)
        (load-theme ,theme t)))))

when call the function: (my-load-theme '(quote solarized))

If you want to call the function in the same way as (my-load-theme 'solarized), replace the line (load-theme ,theme t) in the function definition with (load-theme ',theme t).

1 Answer 1

4

Yes, of course, you can do:

;; -*- lexical-binding:t -*-
(defun my-load-theme (theme)
  (add-hook 'after-make-frame-functions
    (lambda (frame)
      (select-frame frame)
      (when (display-graphic-p frame)
        (load-theme theme t)))))

after which you can do (my-load-them 'solarized) and it will setup after-make-frame-functions so as to load solarized after the creation of every next frame. Personally, I'd refine the code so as to avoid loading the same theme over and over again (you may not notice it if you don't use too many frames, but I do).

Sign up to request clarification or add additional context in comments.

3 Comments

oh, I saw this option but not tested ;; -*- lexical-binding:t -*-. (I didn't choose it before, since I have no idea on the function scope of this lexical statement: only this .el file or any include/require this .el) Let me try it. There is another way which use "`" and ",", called injection or sth, which is supposed to be equivalent but not working :(
what I mean, is the second solution given by coredump, in stackoverflow.com/questions/33382928/…. I tried but failed to work (I add ` before (lambda and , on left of theme in the last line).
The -*- lexical-binding:t -*- needs to be on the first line of the file and it affects all the code that's in its file, but it doesn't impose any requirement on other code in other files. As for the other option, it works but you need to add ', rather than just , in front of theme.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.