After 'emacs --deamon' I can not see new t

2019-03-18 09:32发布

问题:

Minimal config https://www.refheap.com/18816

Scenario 1.

  • Run 'emacs' from terminal.
  • M-x server-start
  • Run 'emacsclient -c' from terminal.
  • Effect: Theme applied.

Scenario 2.

  • Run 'emacs --daemon' from terminal
  • Run 'emacsclient -c'
  • Effect: Theme is not applied.

Why is that?

.emacs.d/init.d config:

(require 'package)
(package-initialize)

(defun install-pack (p)
"A utility function to help in installing emacs package."
(unless (package-installed-p p) (package-install p)))

(defun install-packs (packs)
"A utility function to help in installing emacs packages."
(unless package-archive-contents
        (package-refresh-contents))
(dolist (p packs) (install-pack p)))

;(load-theme 'tronesque)
(load-theme 'tronesque t)

or

;(load-theme 'tronesque)
;;(load-theme 'tronesque t)
(custom-set-variables
;; custom-set-variables was added by Custom.
'(custom-enabled-themes (quote (tronesque)))
'(custom-safe-themes (quote    ("b8f561a188a77e450ab8a060128244c81dea206f15c1152a6899423dd607b327" default))))
 (custom-set-faces
 ;; custom-set-faces was added by Custom.
 )

回答1:

For Emacs 24,

(if (daemonp)
    (add-hook 'after-make-frame-functions
        (lambda (frame)
            (select-frame frame)
            (load-theme 'tronesque t)))
    (load-theme 'tronesque t))

or

(if (daemonp)
    (add-hook 'after-make-frame-functions
        (lambda (frame)
            (with-selected-frame frame
                (load-theme 'tronesque t))))
    (load-theme 'tronesque t))

should do.



回答2:

Using all mentioned approaches the theme is being reloaded in vain starting from the second frame creation.

For loading it only one time I did:

(if (daemonp)
    (add-hook 'after-make-frame-functions (lambda (frame)
                        (when (eq (length (frame-list)) 2)
                            (progn
                              (select-frame frame)
                              (load-theme 'tronesque)))))
  (load-theme 'tronesque 1))

Update

After some tests in Emacs 24.5.1 with the distinguished theme and using emacs as daemon, I have got some issues.

If my first client is a terminal emacsclient -t and later I open a window client emacsclient -c, the window client loses theme configurations.

Then I came up with this solution:

;; theme
(defvar my:theme 'distinguished)
(defvar my:theme-window-loaded nil)
(defvar my:theme-terminal-loaded nil)

(if (daemonp)
    (add-hook 'after-make-frame-functions(lambda (frame)
                       (select-frame frame)
                       (if (window-system frame)
                           (unless my:theme-window-loaded
                             (if my:theme-terminal-loaded
                                 (enable-theme my:theme)
                               (load-theme my:theme t))
                             (setq my:theme-window-loaded t))
                         (unless my:theme-terminal-loaded
                           (if my:theme-window-loaded
                               (enable-theme my:theme)
                             (load-theme my:theme t))
                           (setq my:theme-terminal-loaded t)))))

  (progn
    (load-theme my:theme t)
    (if (display-graphic-p)
        (setq my:theme-window-loaded t)
      (setq my:theme-terminal-loaded t))))

It's not so elegant, I know, but solves the two problems (unnecessary reloading and lost of config).



回答3:

The following extension of the above answer fixed the problem for me with Emacs 24, setting the color-theme via the color-theme call, as shown with the solarized theme.

(if (daemonp)
(add-hook 'after-make-frame-functions
          '(lambda (f)
             (with-selected-frame f
               (when (window-system f) (color-theme-solarized-dark)))))
(color-theme-solarized-dark))

HTH

J.



回答4:

Since it's a daemon startup, there was no any frame created when the load-theme function was involved. After the startup, you created a new frame by entering `emacsclient -c', nothing happened of course.

So you have to tell emacs to apply the theme after frames are created. The hook after-make-frame-functions is made for that:

(if (daemonp)
    (add-hook 'after-make-frame-functions
              (lambda (frame)
                (load-theme 'tronesque t)))
    (load-theme 'tronesque t))

If it's a daemon startup, load theme after frames are created, otherwise load theme directly.