Skip to content

How to warm-start treemacs in a daemon setup? #1179

@alberti42

Description

@alberti42

Foremost, thank you so much for the wonderful Emacs package! ❤️❤️❤️

I have a dummy question that I could not answer after some searching on Reddit and the internet and querying several LLMs.

What is the best way to warm-start treemacs with emacs server (daemon)? It takes 2 seconds for the explorer pane to open the first time. The first start is especially slow in the GUI session but also somehow slow in the terminal. After the first start, it is blazing fast.

I am running with Nerd Font icons. So, possibly it is a problem of caching icons and much more. Is there a simple strategy to force pre-loading everything needed for toggling the explorer? I tried the warm-start strategy below without great success:

(use-package treemacs
  :after pfuture
  :commands (treemacs treemacs-select-window treemacs-find-file)
  :bind (("C-c t t" . treemacs)
         ("C-c t s" . treemacs-select-window)
         ("C-c t f" . treemacs-find-file))
  :init
  ;; Persist workspaces/projects under XDG cache.
  (let ((cache (or (getenv "XDG_CACHE_HOME") (expand-file-name "~/.cache"))))
    (setq treemacs-persist-file (expand-file-name "emacs/treemacs-persist" cache)
          treemacs-last-error-persist-file (expand-file-name "emacs/treemacs-persist-at-last-error" cache)))
  :config
  ;; Keep this light; avoid enabling optional modes by default.
  (setq treemacs-width 35)
  (setq treemacs-no-png-images t)

  ;; Warm-start: pre-load treemacs and restore workspace state during daemon
  ;; startup so the first interactive open is fast.
  ;; `treemacs--maybe-load-workspaces' reads the persist file (frame-safe,
  ;; pure file I/O) and sets a guard so it won't re-run on first call.
  (defun emacs-config--treemacs-warmup ()
    "Pre-initialize treemacs in daemon mode (frame-safe: pure file I/O)."
    (when (daemonp)
      (condition-case err
          (progn
            (require 'treemacs nil t)
            (when (fboundp 'treemacs--maybe-load-workspaces)
              (treemacs--maybe-load-workspaces)))
        (error (message "treemacs warmup: %s" err)))))

  (add-hook 'after-init-hook #'emacs-config--treemacs-warmup))

You probably will never need to look into it, but just in case, here are my Emacs config files (https://github.com/alberti42/dotfiles/tree/public/.config/emacs).

Even a simple tip would be much appreciated. If there is a simple solution, it would be worth mentioning it in the README.md for people like me who prefer an Emacs daemon in the background.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions