Emacs: desktop-save-mode without the automatic restore Light _ □ ✕
  • Posts
  • About
  • Debug
Post

Emacs: desktop-save-mode without the automatic restore

Published
2026-08-06 08:10 UTC (precision: second)
Author
Srijan Choudhary
Source feed
Srijan Choudhary, all posts excluded
Link
https://srijan.ch/emacs-desktop-save-mode-without-the-automatic-restore
Length
3708 runes
Extracted text
Emacs has a built-in way to save and restore sessions: the desktop.el library. As the Emacs manual says, putting this in the init file enables automatic save and restore of sessions: (desktop-save-mode 1) But, I wanted a different behavior: Automatically save the session But don't restore it automatically I wanted to only use the session feature to restore sessions that I closed by mistake, or where I forgot to finish something before quitting Emacs. By default, I wanted a fresh session every time. But this also means that some session history has to be maintained; otherwise, a fresh session will start to write to the desktop file and then I will not be able to recover the old session. By default, the way to do this is to not set desktop-save-mode, but manually call desktop-save when needed, and then call desktop-read when needed. But, if I forget to save, I cannot restore. This elisp is what I came up with to make it work the way I want: (defvar my/desktop-snapshot-keep 5 "How many previous sessions to keep desktop snapshots for.") (defun my/desktop--snapshots () (nreverse (directory-files desktop-dirname t (concat "\\`" (regexp-quote desktop-base-file-name) "\\.[0-9]\\{8\\}T[0-9]\\{6\\}\\'")))) (defun my/desktop--take-snapshot () (let ((live (desktop-full-file-name))) (when (file-exists-p live) (let ((dest (concat live (format-time-string ".%Y%m%dT%H%M%S" (file-attribute-modification-time (file-attributes live)))))) (unless (file-exists-p dest) (copy-file live dest t t))))) (dolist (old (nthcdr my/desktop-snapshot-keep (my/desktop--snapshots))) (delete-file old))) (defun my/desktop--label (file) (format "%s %d buffers" (format-time-string "%Y-%m-%d %H:%M:%S" (file-attribute-modification-time (file-attributes file))) (with-temp-buffer (insert-file-contents file) (how-many "^(desktop-\\(?:create-buffer\\|append-buffer-args\\) " (point-min) (point-max))))) (add-hook 'emacs-startup-hook (lambda () (require 'desktop) (setq desktop-dirname (expand-file-name user-emacs-directory)) (my/desktop--take-snapshot) (desktop--get-file-modtime) (let ((owner (desktop-owner))) (unless (and owner (desktop--emacs-pid-running-p owner)) (desktop-claim-lock))) (desktop-save-mode 1))) (defun my/desktop-restore (file) "Restore the desktop snapshot FILE, prompting for one interactively." (interactive (progn (require 'desktop) (let* ((snapshots (or (my/desktop--snapshots) (user-error "No desktop snapshots in %s" desktop-dirname))) (choices (mapcar (lambda (f) (cons (my/desktop--label f) f)) snapshots))) (list (cdr (assoc (completing-read "Restore desktop session: " choices nil t) choices)))))) (desktop-release-lock) (let ((desktop-base-file-name (file-name-nondirectory file))) (desktop-read (file-name-directory file))) (desktop--get-file-modtime)) Note that desktop-save-mode must not be enabled before after-init-hook fires, because that's where desktop.el decides to automatically restore. So, I enable it in emacs-startup-hook, which runs later. Other things inside my emacs-startup-hook lambda are workarounds for how desktop.el works, so they can be fragile if there are major changes to it. The lock check avoids a stale lock from a crashed Emacs silently disabling auto-save, and naming snapshots after the desktop file's mtime means restarting without saving anything doesn't use up one of the five slots. Here's what it looks like when I call my/desktop-restore: Of course, this can be made better by showing more information per session (like number of projects, names of last edited files or last visited buffers). Vertico/marginalia can show it nicely. Also maybe a section in emacs-dashboard to show recent sessions and click/enter to restore. Something for another day.
Stored topics

Every stored assignment is listed, including rows below the current cutoff (0.75), so a missing tag can be explained.

Concept Code Score Meter Margin Origin Path
economy, business and finance economy-business-and-finance 0.99 4.46 ↑derived from 20000225 economy, business and finance
products and services products-and-services 0.99 4.46 ↑derived from 20000225 economy, business and finance > products and services
computing and information technology computing-and-information-technology 0.99 4.46 direct economy, business and finance > products and services > computing and information technology
science and technology science-and-technology 0.98 3.85 ↑derived from 20000763 science and technology
technology and engineering technology-and-engineering 0.98 3.85 ↑derived from 20000763 science and technology > technology and engineering
information technology and computer science information-technology-and-computer 0.98 3.85 direct science and technology > technology and engineering > information technology and computer science
software and applications software-and-applications 0.95 2.96 direct economy, business and finance > products and services > computing and information technology > software and applications
Software development software-development 0.85 1.74 direct economy, business and finance > products and services > computing and information technology > software and applications > Software development
Information security information-security 0.79 1.32 direct science and technology > technology and engineering > information technology and computer science > Information security
environment environment 0.52 0.06 direct environment
Open source open-source 0.33 -0.71 direct economy, business and finance > products and services > computing and information technology > software and applications > Open source
social sciences social-sciences 0.29 -0.90 ↑derived from 20000747 science and technology > social sciences
history history 0.29 -0.90 direct science and technology > social sciences > history
society society 0.17 -1.61 ↑derived from 20001300 society
fundamental rights fundamental-rights 0.17 -1.61 ↑derived from 20001300 society > fundamental rights
privacy privacy 0.17 -1.61 direct society > fundamental rights > privacy
artificial intelligence artificial-intelligence 0.16 -1.65 direct science and technology > technology and engineering > information technology and computer science > artificial intelligence
sport sport 0.16 -1.68 ↑derived from 20001183 sport
competition discipline competition-discipline 0.16 -1.68 ↑derived from 20001183 sport > competition discipline
eSports esports 0.16 -1.68 direct sport > competition discipline > eSports
Self-hosting self-hosting 0.10 -2.22 direct economy, business and finance > products and services > computing and information technology > Self-hosting
lifestyle and leisure lifestyle-and-leisure 0.09 -2.28 ↑derived from 20000548 lifestyle and leisure
leisure leisure 0.09 -2.28 ↑derived from 20000548 lifestyle and leisure > leisure
game game 0.09 -2.28 ↑derived from 20000548 lifestyle and leisure > leisure > game
video game video-game 0.09 -2.28 direct lifestyle and leisure > leisure > game > video game
MMORPG mmorpg 0.08 -2.42 direct lifestyle and leisure > leisure > game > video game > MMORPG
scientific research scientific-research 0.07 -2.53 ↑derived from 20000739 science and technology > scientific research
scientific exploration scientific-exploration 0.07 -2.53 ↑derived from 20000739 science and technology > scientific research > scientific exploration
space exploration space-exploration 0.07 -2.53 direct science and technology > scientific research > scientific exploration > space exploration
media and entertainment industry media-and-entertainment-industry 0.07 -2.55 ↑derived from 20000306 economy, business and finance > products and services > media and entertainment industry
books and publishing books-and-publishing 0.07 -2.55 direct economy, business and finance > products and services > media and entertainment industry > books and publishing
politics and government politics-and-government 0.07 -2.56 direct politics and government
arts, culture, entertainment and media arts-culture-entertainment-and-media 0.07 -2.60 ↑derived from 20000036 arts, culture, entertainment and media
arts and entertainment arts-and-entertainment 0.07 -2.60 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment
visual arts visual-arts 0.07 -2.60 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment > visual arts
photography photography 0.07 -2.60 direct arts, culture, entertainment and media > arts and entertainment > visual arts > photography
hobby hobby 0.06 -2.75 direct lifestyle and leisure > leisure > hobby
health health 0.05 -2.95 ↑derived from 20000458 health
disease and condition disease-and-condition 0.05 -2.95 ↑derived from 20000458 health > disease and condition
mental health and disorder mental-health-and-disorder 0.05 -2.95 direct health > disease and condition > mental health and disorder
film industry film-industry 0.04 -3.09 direct economy, business and finance > products and services > media and entertainment industry > film industry
disaster, accident and emergency incident disaster-accident-and-emergency-incident 0.04 -3.12 direct disaster, accident and emergency incident
family family 0.04 -3.12 direct society > family
literature literature 0.04 -3.16 direct arts, culture, entertainment and media > arts and entertainment > literature
Cryptography cryptography 0.04 -3.18 direct science and technology > technology and engineering > information technology and computer science > Cryptography
streaming service streaming-service 0.04 -3.18 direct economy, business and finance > products and services > media and entertainment industry > streaming service
Tabletop gaming tabletop-gaming 0.04 -3.24 direct lifestyle and leisure > leisure > game > Tabletop gaming
crime, law and justice crime-law-and-justice 0.03 -3.38 ↑derived from 20000086 crime, law and justice
crime crime 0.03 -3.38 ↑derived from 20000086 crime, law and justice > crime
cyber crime cyber-crime 0.03 -3.38 direct crime, law and justice > crime > cyber crime
labour labour 0.03 -3.40 direct labour
natural science natural-science 0.03 -3.41 ↑derived from 20000719 science and technology > natural science
biology biology 0.03 -3.41 direct science and technology > natural science > biology
human interest human-interest 0.03 -3.46 direct human interest
mass media mass-media 0.02 -3.73 ↑derived from 20001182 arts, culture, entertainment and media > mass media
social media social-media 0.02 -3.73 direct arts, culture, entertainment and media > mass media > social media
conflict, war and peace conflict-war-and-peace 0.02 -3.75 direct conflict, war and peace
animation animation 0.02 -3.83 direct arts, culture, entertainment and media > arts and entertainment > animation
climate change climate-change 0.02 -3.87 direct environment > climate change
consumer goods consumer-goods 0.02 -3.93 ↑derived from 20001160 economy, business and finance > products and services > consumer goods
handicrafts handicrafts 0.02 -3.93 direct economy, business and finance > products and services > consumer goods > handicrafts
health treatment and procedure health-treatment-and-procedure 0.02 -4.08 direct health > health treatment and procedure
card game card-game 0.02 -4.17 direct lifestyle and leisure > leisure > game > card game
education education 0.01 -4.21 direct education
culture culture 0.01 -4.25 direct arts, culture, entertainment and media > culture
diet diet 0.01 -4.32 direct health > health treatment and procedure > diet
television television 0.01 -4.40 direct arts, culture, entertainment and media > mass media > television
Blogging blogging 0.01 -4.42 direct arts, culture, entertainment and media > mass media > social media > Blogging
podcast podcast 0.01 -4.46 direct economy, business and finance > products and services > media and entertainment industry > podcast
award and prize award-and-prize 0.01 -4.71 direct human interest > award and prize
lifestyle lifestyle 0.01 -4.78 ↑derived from 20001257 lifestyle and leisure > lifestyle
house and home house-and-home 0.01 -4.78 ↑derived from 20001257 lifestyle and leisure > lifestyle > house and home
gardening gardening 0.01 -4.78 direct lifestyle and leisure > lifestyle > house and home > gardening
weather weather 0.01 -4.85 ↑derived from 20001128 weather
weather forecast weather-forecast 0.01 -4.85 direct weather > weather forecast
exercise and fitness exercise-and-fitness 0.01 -4.88 direct lifestyle and leisure > wellness > exercise and fitness
wellness wellness 0.01 -4.88 ↑derived from 20001239 lifestyle and leisure > wellness
religion religion 0.01 -4.94 direct religion
food and drink enthusiasm food-and-drink-enthusiasm 0.01 -5.01 direct lifestyle and leisure > leisure > hobby > food and drink enthusiasm
board game board-game 0.01 -5.03 direct lifestyle and leisure > leisure > game > board game
travel and tourism travel-and-tourism 0.01 -5.18 direct lifestyle and leisure > leisure > travel and tourism
public health public-health 0.01 -5.26 direct health > public health
music music 0.00 -5.69 direct arts, culture, entertainment and media > arts and entertainment > music

← Back to posts

feedtagger 0.1.0-dev model: modernbert-base-zeroshot-v2.0/int8@all-s256 659 posts 0 unclassified min score 0.75