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)
.