0

Everything works fine when using regular Emacs. Now when I try to use the same init.el file with emacs --daemon and emacsclient -c I get an error.

I have thought about using different configuration files for all Emacs, Emacs daemon and Emacs terminal.

Debugger entered--Lisp error: (error "Window system is not in use or not initialized")
    x-list-fonts("ETBembo")
    (cond ((x-list-fonts "ETBembo") '(:font "ETBembo")) ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro")) ((x-list-fonts "Lucida Grande") '(:font "Lucida Grande")) ((x-list-fonts "Verdana") '(:font "Verdana")) ((x-family-fonts "Sans Serif") '(:family "Sans Serif")) (nil (warn "Cannot find a Sans Serif Font.  Install Source San...")))
    (let* ((variable-tuple (cond ((x-list-fonts "ETBembo") '(:font "ETBembo")) ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro")) ((x-list-fonts "Lucida Grande") '(:font "Lucida Grande")) ((x-list-fonts "Verdana") '(:font "Verdana")) ((x-family-fonts "Sans Serif") '(:family "Sans Serif")) (nil (warn "Cannot find a Sans Serif Font.  Install Source San...")))) (base-font-color (face-foreground 'default nil 'default)) (headline (list ':inherit 'default ':weight 'bold ':foreground base-font-color))) (custom-theme-set-faces 'user (list 'org-level-8 (list (list t (append headline variable-tuple)))) (list 'org-level-7 (list (list t (append headline variable-tuple)))) (list 'org-level-6 (list (list t (append headline variable-tuple)))) (list 'org-level-5 (list (list t (append headline variable-tuple)))) (list 'org-level-4 (list (list t (append headline variable-tuple)))) (list 'org-level-3 (list (list t (append headline variable-tuple '(:height 1.1))))) (list 'org-level-2 (list (list t (append headline variable-tuple '(:height 1.2))))) (list 'org-level-1 (list (list t (append headline variable-tuple '(:height 1.3))))) (list 'org-document-title (list (list t (append headline variable-tuple '(:height 1.4 :underline nil)))))))
    eval-buffer(#<buffer  *load*> nil "/home/mocambo/.emacs.d/init.el" nil t)  ; Reading at buffer position 4275
    load-with-code-conversion("/home/mocambo/.emacs.d/init.el" "/home/mocambo/.emacs.d/init.el" t t)
    load("/home/mocambo/.emacs.d/init" noerror nomessage)
    startup--load-user-init-file(#f(compiled-function () #<bytecode 0xe836752f7a886e2>) #f(compiled-function () #<bytecode -0x1f3c686ddc0cdc35>) t)
    command-line()
    normal-top-level()

  Error: server did not start correctly

~/.emacs.d/init.el

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ansi-color-faces-vector
   [default default default italic underline success warning error])
 '(ansi-color-names-vector
   ["#242424" "#e5786d" "#95e454" "#cae682" "#8ac6f2" "#333366" "#ccaa8f" "#f6f3e8"])
 '(cua-mode t nil (cua-base))
 '(custom-enabled-themes '(deeper-blue))
 '(desktop-load-locked-desktop t)
 '(desktop-path '("~/.emacs.d/"))
 '(package-selected-packages '(org)))

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(org-document-title ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif" :height 1.6 :underline nil))))
 '(org-level-1 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif" :height 1.3))))
 '(org-level-2 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif" :height 1.2))))
 '(org-level-3 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif" :height 1.1))))
 '(org-level-4 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif"))))
 '(org-level-5 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif"))))
 '(org-level-6 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif"))))
 '(org-level-7 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif"))))
 '(org-level-8 ((t (:inherit default :weight bold :foreground "gray80" :family "Sans Serif")))))

;; Begin from https://github.com/james-stoup/emacs-org-mode-tutorial:

;; Must do this so the agenda knows where to look for my files
(setq org-agenda-files '("~/Org"))

;; When a TODO is set to a done state, record a timestamp
(setq org-log-done 'time)

;; Follow the links
(setq org-return-follows-link  t)

;; Associate all org files with org mode
(add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))

;; Make the indentation look nicer
(add-hook 'org-mode-hook 'org-indent-mode)

;; Shortcuts for storing links, viewing the agenda, and starting a capture
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
(define-key global-map "\C-cc" 'org-capture)

;; Wrap the lines in org mode so that things are easier to read
(add-hook 'org-mode-hook 'visual-line-mode)

(let* ((variable-tuple
        (cond ((x-list-fonts "ETBembo")         '(:font "ETBembo"))
              ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
              ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
              ((x-list-fonts "Verdana")         '(:font "Verdana"))
              ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
              (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro."))))
       (base-font-color     (face-foreground 'default nil 'default))
       (headline           `(:inherit default :weight bold :foreground ,base-font-color)))

  (custom-theme-set-faces
   'user
   `(org-level-8 ((t (,@headline ,@variable-tuple))))
   `(org-level-7 ((t (,@headline ,@variable-tuple))))
   `(org-level-6 ((t (,@headline ,@variable-tuple))))
   `(org-level-5 ((t (,@headline ,@variable-tuple))))
   `(org-level-4 ((t (,@headline ,@variable-tuple))))
   `(org-level-3 ((t (,@headline ,@variable-tuple :height 1.1))))
   `(org-level-2 ((t (,@headline ,@variable-tuple :height 1.2))))
   `(org-level-1 ((t (,@headline ,@variable-tuple :height 1.3))))
   `(org-document-title ((t (,@headline ,@variable-tuple :height 1.4 :underline nil))))))

;; Remember last session
(desktop-save-mode 1)
;;(save-place-mode 1)

;; Automatically save buffer on focus out
(defun full-auto-save ()
  (interactive)
    (save-excursion
      (dolist (buf (buffer-list))
        (set-buffer buf)
        (if (and (buffer-file-name) (buffer-modified-p))
            (basic-save-buffer)))))
;;(add-hook 'auto-save-hook 'full-auto-save)
(add-hook 'focus-out-hook 'full-auto-save)

;; Adding drawer on Headline insert
(require 'org-id)

(defadvice org-insert-heading (after add-id-stuff activate)
  (template-myid))
    
(defun template-myid ()
  (save-excursion
    (insert "\n:PROPERTIES:\n:CREATED:  "
      (format-time-string "[%Y-%m-%d %a %H:%M]")
      "\n:ID:  "
      (org-id-uuid)
      "\n:END:")))

;; Collapse lists by default
(setq org-cycle-include-plain-lists 'integrate)

;; https://stackoverflow.com/questions/22200312/refile-from-one-file-to-other
;;(setq org-refile-use-outline-path 'file)              ; Top level refiles
(setq org-refile-targets '((nil :maxlevel . 9)
                                (org-agenda-files :maxlevel . 9)))
(setq org-outline-path-complete-in-steps nil)         ; Refile in a single go
(setq org-refile-use-outline-path t)                  ; Show full paths for refiling

;; Delay restoring the desktop in daemon mode until the first client connects
(add-hook 'server-after-make-frame-hook
          (lambda ()
            "Functions to apply until the first client connects"
            (desktop-read)))

1 Answer 1

0

You can see the error in the messages you quoted:

Lisp error: (error "Window system is not in use or not initialized")
x-list-fonts("ETBembo")

The second line is the first line of the stack; it tells you what function was running when the error was created (x-list-fonts) and what the arguments were (the string "ETBembo").

As the error message says, the window system hasn’t been initialized yet. This is normal for the Emacs server, because it doesn’t know if you are going to connect using the Emacs GUI or with an Emacs running inside of a terminal. However, your init file makes the assumption that X Windows is available. That is why the error happens when you try to call x-list-fonts.

To fix this, you should delay all the stuff about fonts until there is a graphical frame. There is a hook called after-make-frame-functions (see Appendix H Standard Hooks of the Emacs Lisp Manual for more information) that you can use for this purpose. You will also want to use display-graphical-p to determine if the new frame is on a graphical display. Something like this, perhaps:

(add-hook 'after-make-frame-functions 'herman/init-org-fonts)
(defun herman/init-org-fonts (new-frame)
    (when (display-graphical-p new-frame)
          ; your init code here
        ))
2
  • Thanks ! Do you recommend using different configuration files for Emacs, Emacsclient and Emacs terminal and using hooks for that? Or are there any other variables or methods that can be checked to target the configuration more precisely?
    – Herman
    Jan 6, 2023 at 12:13
  • No and Yes respectively.
    – db48x
    Jan 6, 2023 at 13:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.