Function: treemacs--persist

treemacs--persist is a byte-compiled function defined in treemacs-persistence.el.

Signature

(treemacs--persist)

Documentation

Persist treemacs' state in treemacs-persist-file.

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-persistence.el
(defun treemacs--persist ()
  "Persist treemacs' state in `treemacs-persist-file'."
  (unless (or (treemacs--should-not-run-persistence?)
              (null (get 'treemacs :state-is-restored)))
    (unless (file-exists-p treemacs-persist-file)
      (make-directory (file-name-directory treemacs-persist-file) :with-parents))
    (condition-case e
        (let ((txt nil)
              (buffer nil)
              (no-kill nil)
              ;; no surprisese when using `abbreviate-file-name'
              (directory-abbrev-alist nil)
              (abbreviated-home-dir nil)
              (file-precious-flag t))
          (--if-let (get-file-buffer treemacs-persist-file)
              (setq buffer it
                    no-kill t)
            (setq buffer (find-file-noselect treemacs-persist-file :no-warn)
                  desktop-save-buffer nil))
          (with-current-buffer buffer
            (dolist (ws (--reject (null (treemacs-workspace->projects it))
                                  (append (treemacs-workspaces)
                                          (treemacs-disabled-workspaces))))
              (push (format "* %s%s\n"
                            (if (treemacs-workspace->is-disabled? ws) "COMMENT " "")
                            (treemacs-workspace->name ws))
                    txt)
              (dolist (pr (treemacs-workspace->projects ws))
                (push (format "** %s%s\n"
                              (if (treemacs-project->is-disabled? pr) "COMMENT " "")
                              (treemacs-project->name pr))
                      txt)
                (push (format
                       " - path :: %s\n"
                       (-let [path (treemacs-project->path pr)]
                         (if (--any? (string-prefix-p it path) treemacs--no-abbr-on-persist-prefixes)
                             path
                           (abbreviate-file-name path))))
                      txt)))
            (delete-region (point-min) (point-max))
            (insert (apply #'concat (nreverse txt)))
            (-let [inhibit-message t] (save-buffer))
            (unless no-kill (kill-buffer))))
      (error (treemacs-log-err "Error '%s' when persisting workspace." e)))))