oracleyue
oracleyue

Reputation: 416

Emacs Lisp define a function with after-make-frame-functions embedded in

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

Upvotes: 3

Views: 1646

Answers (1)

Stefan
Stefan

Reputation: 28541

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

Upvotes: 4

Related Questions