desktop.el 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. ;;; desktop.el --- save partial status of Emacs when killed -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1993-1995, 1997, 2000-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Author: Morten Welinder <terra@diku.dk>
  5. ;; Keywords: convenience
  6. ;; Favorite-brand-of-beer: None, I hate beer.
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Save the Desktop, i.e.,
  20. ;; - some global variables
  21. ;; - the list of buffers with associated files. For each buffer also
  22. ;; - the major mode
  23. ;; - the default directory
  24. ;; - the point
  25. ;; - the mark & mark-active
  26. ;; - buffer-read-only
  27. ;; - some local variables
  28. ;; - frame and window configuration
  29. ;; To use this, use customize to turn on desktop-save-mode or add the
  30. ;; following line somewhere in your init file:
  31. ;;
  32. ;; (desktop-save-mode 1)
  33. ;;
  34. ;; For further usage information, look at the section
  35. ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
  36. ;; When the desktop module is loaded, the function `desktop-kill' is
  37. ;; added to the `kill-emacs-hook'. This function is responsible for
  38. ;; saving the desktop when Emacs is killed. Furthermore an anonymous
  39. ;; function is added to the `after-init-hook'. This function is
  40. ;; responsible for loading the desktop when Emacs is started.
  41. ;; Special handling.
  42. ;; -----------------
  43. ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
  44. ;; are supplied to handle special major and minor modes respectively.
  45. ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
  46. ;; to restore a desktop buffer. Elements must have the form
  47. ;;
  48. ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
  49. ;;
  50. ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
  51. ;; evaluates the desktop file. Buffers with a major mode not specified here,
  52. ;; are restored by the default handler `desktop-restore-file-buffer'.
  53. ;; `desktop-minor-mode-handlers' is an alist of functions to restore
  54. ;; non-standard minor modes. Elements must have the form
  55. ;;
  56. ;; (MINOR-MODE . RESTORE-FUNCTION).
  57. ;;
  58. ;; Functions are called by `desktop-create-buffer' to restore minor modes.
  59. ;; Minor modes not specified here, are restored by the standard minor mode
  60. ;; function. If you write a module that defines a major or minor mode that
  61. ;; needs a special handler, then place code like
  62. ;; (defun foo-restore-desktop-buffer
  63. ;; ...
  64. ;; (add-to-list 'desktop-buffer-mode-handlers
  65. ;; '(foo-mode . foo-restore-desktop-buffer))
  66. ;; or
  67. ;; (defun bar-desktop-restore
  68. ;; ...
  69. ;; (add-to-list 'desktop-minor-mode-handlers
  70. ;; '(bar-mode . bar-desktop-restore))
  71. ;; in the module itself. The mode function must either be autoloaded,
  72. ;; or of the form "foobar-mode" and defined in library "foobar", so that
  73. ;; desktop can guess how to load its definition.
  74. ;; See the docstrings of `desktop-buffer-mode-handlers' and
  75. ;; `desktop-minor-mode-handlers' for more info.
  76. ;; Minor modes.
  77. ;; ------------
  78. ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
  79. ;; manual) are handled in the following way:
  80. ;; When `desktop-save' saves the state of a buffer to the desktop file, it
  81. ;; saves as `desktop-minor-modes' the list of names of those variables in
  82. ;; `minor-mode-alist' that have a non-nil value.
  83. ;; When `desktop-create' restores the buffer, each of the symbols in
  84. ;; `desktop-minor-modes' is called as function with parameter 1.
  85. ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
  86. ;; are used to handle non-conventional minor modes. `desktop-save' uses
  87. ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
  88. ;; functions before writing `desktop-minor-modes'. If a minor mode has a
  89. ;; variable name that is different form its function name, an entry
  90. ;; (NAME RESTORE-FUNCTION)
  91. ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
  92. ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
  93. ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
  94. ;; function different from the usual minor mode function.
  95. ;; ---------------------------------------------------------------------------
  96. ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
  97. ;; in your home directory is used for that. Saving global default values
  98. ;; for buffers is an example of misuse.
  99. ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
  100. ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
  101. ;; things you did not mean to keep. Use M-x desktop-clear RET.
  102. ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
  103. ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
  104. ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
  105. ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
  106. ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
  107. ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
  108. ;; pot@cnuce.cnr.it (Francesco Potortì) for misc. tips.
  109. ;; ---------------------------------------------------------------------------
  110. ;; TODO:
  111. ;;
  112. ;; Recognize more minor modes.
  113. ;; Save mark rings.
  114. ;;; Code:
  115. (require 'cl-lib)
  116. (require 'frameset)
  117. (defvar desktop-file-version "208"
  118. "Version number of desktop file format.
  119. Used at desktop read to provide backward compatibility.")
  120. (defconst desktop-native-file-version 208
  121. "Format version of the current desktop package, an integer.")
  122. (defvar desktop-io-file-version nil
  123. "The format version of the current desktop file (an integer) or nil.")
  124. ;; Note: Historically, the version number is embedded in the entry for
  125. ;; each buffer. It is highly inadvisable for different buffer entries
  126. ;; to have different format versions.
  127. ;; ----------------------------------------------------------------------------
  128. ;; USER OPTIONS -- settings you might want to play with.
  129. ;; ----------------------------------------------------------------------------
  130. (defgroup desktop nil
  131. "Save status of Emacs when you exit."
  132. :group 'frames)
  133. ;; Maintained for backward compatibility
  134. (define-obsolete-variable-alias 'desktop-enable 'desktop-save-mode "22.1")
  135. ;;;###autoload
  136. (define-minor-mode desktop-save-mode
  137. "Toggle desktop saving (Desktop Save mode).
  138. With a prefix argument ARG, enable Desktop Save mode if ARG is positive,
  139. and disable it otherwise. If called from Lisp, enable the mode if ARG
  140. is omitted or nil.
  141. When Desktop Save mode is enabled, the state of Emacs is saved from
  142. one session to another. In particular, Emacs will save the desktop when
  143. it exits (this may prompt you; see the option `desktop-save'). The next
  144. time Emacs starts, if this mode is active it will restore the desktop.
  145. To manually save the desktop at any time, use the command `\\[desktop-save]'.
  146. To load it, use `\\[desktop-read]'.
  147. Once a desktop file exists, Emacs will auto-save it according to the
  148. option `desktop-auto-save-timeout'.
  149. To see all the options you can set, browse the `desktop' customization group.
  150. For further details, see info node `(emacs)Saving Emacs Sessions'."
  151. :global t
  152. :group 'desktop
  153. (if desktop-save-mode
  154. (desktop-auto-save-enable)
  155. (desktop-auto-save-disable)))
  156. (defun desktop-save-mode-off ()
  157. "Disable `desktop-save-mode'. Provided for use in hooks."
  158. (desktop-save-mode 0))
  159. (defcustom desktop-save 'ask-if-new
  160. "Specifies whether the desktop should be saved when it is killed.
  161. A desktop is killed when the user changes desktop or quits Emacs.
  162. Possible values are:
  163. t -- always save.
  164. ask -- always ask.
  165. ask-if-new -- ask if no desktop file exists, otherwise just save.
  166. ask-if-exists -- ask if desktop file exists, otherwise don't save.
  167. if-exists -- save if desktop file exists, otherwise don't save.
  168. nil -- never save.
  169. The desktop is never saved when `desktop-save-mode' is nil.
  170. The variables `desktop-dirname' and `desktop-base-file-name'
  171. determine where the desktop is saved."
  172. :type
  173. '(choice
  174. (const :tag "Always save" t)
  175. (const :tag "Always ask" ask)
  176. (const :tag "Ask if desktop file is new, else do save" ask-if-new)
  177. (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
  178. (const :tag "Save if desktop file exists, else don't" if-exists)
  179. (const :tag "Never save" nil))
  180. :group 'desktop
  181. :version "22.1")
  182. (defcustom desktop-auto-save-timeout auto-save-timeout
  183. "Number of seconds idle time before auto-save of the desktop.
  184. The idle timer activates auto-saving only when window configuration changes.
  185. This applies to an existing desktop file when `desktop-save-mode' is enabled.
  186. Zero or nil means disable auto-saving due to idleness."
  187. :type '(choice (const :tag "Off" nil)
  188. (integer :tag "Seconds"))
  189. :set (lambda (symbol value)
  190. (set-default symbol value)
  191. (ignore-errors
  192. (if (and (integerp value) (> value 0))
  193. (desktop-auto-save-enable value)
  194. (desktop-auto-save-disable))))
  195. :group 'desktop
  196. :version "24.4")
  197. (defcustom desktop-load-locked-desktop 'ask
  198. "Specifies whether the desktop should be loaded if locked.
  199. Possible values are:
  200. t -- load anyway.
  201. nil -- don't load.
  202. ask -- ask the user.
  203. If the value is nil, or `ask' and the user chooses not to load the desktop,
  204. the normal hook `desktop-not-loaded-hook' is run."
  205. :type
  206. '(choice
  207. (const :tag "Load anyway" t)
  208. (const :tag "Don't load" nil)
  209. (const :tag "Ask the user" ask))
  210. :group 'desktop
  211. :version "22.2")
  212. (define-obsolete-variable-alias 'desktop-basefilename
  213. 'desktop-base-file-name "22.1")
  214. (defcustom desktop-base-file-name
  215. (convert-standard-filename ".emacs.desktop")
  216. "Name of file for Emacs desktop, excluding the directory part."
  217. :type 'file
  218. :group 'desktop)
  219. (defcustom desktop-base-lock-name
  220. (convert-standard-filename ".emacs.desktop.lock")
  221. "Name of lock file for Emacs desktop, excluding the directory part."
  222. :type 'file
  223. :group 'desktop
  224. :version "22.2")
  225. (defcustom desktop-path (list user-emacs-directory "~")
  226. "List of directories to search for the desktop file.
  227. The base name of the file is specified in `desktop-base-file-name'."
  228. :type '(repeat directory)
  229. :group 'desktop
  230. :version "23.2") ; user-emacs-directory added
  231. (defcustom desktop-missing-file-warning nil
  232. "If non-nil, offer to recreate the buffer of a deleted file.
  233. Also pause for a moment to display message about errors signaled in
  234. `desktop-buffer-mode-handlers'.
  235. If nil, just print error messages in the message buffer."
  236. :type 'boolean
  237. :group 'desktop
  238. :version "22.1")
  239. (defcustom desktop-no-desktop-file-hook nil
  240. "Normal hook run when `desktop-read' can't find a desktop file.
  241. Run in the directory in which the desktop file was sought.
  242. May be used to show a dired buffer."
  243. :type 'hook
  244. :group 'desktop
  245. :version "22.1")
  246. (defcustom desktop-not-loaded-hook nil
  247. "Normal hook run when the user declines to re-use a desktop file.
  248. Run in the directory in which the desktop file was found.
  249. May be used to deal with accidental multiple Emacs jobs."
  250. :type 'hook
  251. :group 'desktop
  252. :options '(desktop-save-mode-off save-buffers-kill-emacs)
  253. :version "22.2")
  254. (defcustom desktop-after-read-hook nil
  255. "Normal hook run after a successful `desktop-read'.
  256. May be used to show a buffer list."
  257. :type 'hook
  258. :group 'desktop
  259. :options '(list-buffers)
  260. :version "22.1")
  261. (defcustom desktop-save-hook nil
  262. "Normal hook run before the desktop is saved in a desktop file.
  263. Run with the desktop buffer current with only the header present.
  264. May be used to add to the desktop code or to truncate history lists,
  265. for example."
  266. :type 'hook
  267. :group 'desktop)
  268. (defcustom desktop-globals-to-save
  269. '(desktop-missing-file-warning
  270. tags-file-name
  271. tags-table-list
  272. search-ring
  273. regexp-search-ring
  274. register-alist
  275. file-name-history)
  276. "List of global variables saved by `desktop-save'.
  277. An element may be variable name (a symbol) or a cons cell of the form
  278. \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
  279. MAX-SIZE elements (if the value is a list) before saving the value.
  280. Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
  281. :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
  282. :group 'desktop)
  283. (defcustom desktop-globals-to-clear
  284. '(kill-ring
  285. kill-ring-yank-pointer
  286. search-ring
  287. search-ring-yank-pointer
  288. regexp-search-ring
  289. regexp-search-ring-yank-pointer)
  290. "List of global variables that `desktop-clear' will clear.
  291. An element may be variable name (a symbol) or a cons cell of the form
  292. \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
  293. to the value obtained by evaluating FORM."
  294. :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
  295. :group 'desktop
  296. :version "22.1")
  297. (defcustom desktop-clear-preserve-buffers
  298. '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
  299. "\\*Warnings\\*")
  300. "List of buffers that `desktop-clear' should not delete.
  301. Each element is a regular expression. Buffers with a name matched by any of
  302. these won't be deleted."
  303. :version "23.3" ; added Warnings - bug#6336
  304. :type '(repeat string)
  305. :group 'desktop)
  306. ;;;###autoload
  307. (defcustom desktop-locals-to-save
  308. '(desktop-locals-to-save ; Itself! Think it over.
  309. truncate-lines
  310. case-fold-search
  311. case-replace
  312. fill-column
  313. overwrite-mode
  314. change-log-default-name
  315. line-number-mode
  316. column-number-mode
  317. size-indication-mode
  318. buffer-file-coding-system
  319. buffer-display-time
  320. indent-tabs-mode
  321. tab-width
  322. indicate-buffer-boundaries
  323. indicate-empty-lines
  324. show-trailing-whitespace)
  325. "List of local variables to save for each buffer.
  326. The variables are saved only when they really are local. Conventional minor
  327. modes are restored automatically; they should not be listed here."
  328. :type '(repeat symbol)
  329. :group 'desktop)
  330. (defcustom desktop-buffers-not-to-save "\\` "
  331. "Regexp identifying buffers that are to be excluded from saving.
  332. This is in effect only for buffers that don't visit files.
  333. To exclude buffers that visit files, use `desktop-files-not-to-save'
  334. or `desktop-modes-not-to-save'."
  335. :type '(choice (const :tag "None" nil)
  336. regexp)
  337. :version "24.4" ; skip invisible temporary buffers
  338. :group 'desktop)
  339. ;; Skip tramp and ange-ftp files
  340. (defcustom desktop-files-not-to-save
  341. "\\(^/[^/:]*:\\|(ftp)$\\)"
  342. "Regexp identifying files whose buffers are to be excluded from saving."
  343. :type '(choice (const :tag "None" nil)
  344. regexp)
  345. :group 'desktop)
  346. ;; We skip TAGS files to save time (tags-file-name is saved instead).
  347. (defcustom desktop-modes-not-to-save
  348. '(tags-table-mode)
  349. "List of major modes whose buffers should not be saved."
  350. :type '(repeat symbol)
  351. :group 'desktop)
  352. (defcustom desktop-restore-frames t
  353. "When non-nil, save and restore the frame and window configuration.
  354. See related options `desktop-restore-reuses-frames',
  355. `desktop-restore-in-current-display', and `desktop-restore-forces-onscreen'."
  356. :type 'boolean
  357. :group 'desktop
  358. :version "24.4")
  359. (defcustom desktop-restore-in-current-display t
  360. "Controls how restoring of frames treats displays.
  361. If t, restores frames into the current display.
  362. If nil, restores frames into their original displays (if possible).
  363. If `delete', deletes frames on other displays instead of restoring them."
  364. :type '(choice (const :tag "Restore in current display" t)
  365. (const :tag "Restore in original display" nil)
  366. (const :tag "Delete frames in other displays" delete))
  367. :group 'desktop
  368. :version "24.4")
  369. (defcustom desktop-restore-forces-onscreen t
  370. "If t, restores frames that are fully offscreen onscreen instead.
  371. If `all', also restores frames that are partially offscreen onscreen.
  372. Note that checking of frame boundaries is only approximate.
  373. It can fail to reliably detect frames whose onscreen/offscreen state
  374. depends on a few pixels, especially near the right / bottom borders
  375. of the screen."
  376. :type '(choice (const :tag "Only fully offscreen frames" t)
  377. (const :tag "Also partially offscreen frames" all)
  378. (const :tag "Do not force frames onscreen" nil))
  379. :group 'desktop
  380. :version "24.4")
  381. (defcustom desktop-restore-reuses-frames t
  382. "If t, restoring frames reuses existing frames.
  383. If nil, deletes existing frames.
  384. If `keep', keeps existing frames and does not reuse them."
  385. :type '(choice (const :tag "Reuse existing frames" t)
  386. (const :tag "Delete existing frames" nil)
  387. (const :tag "Keep existing frames" :keep))
  388. :group 'desktop
  389. :version "24.4")
  390. (defcustom desktop-file-name-format 'absolute
  391. "Format in which desktop file names should be saved.
  392. Possible values are:
  393. absolute -- Absolute file name.
  394. tilde -- Relative to ~.
  395. local -- Relative to directory of desktop file."
  396. :type '(choice (const absolute) (const tilde) (const local))
  397. :group 'desktop
  398. :version "22.1")
  399. (defcustom desktop-restore-eager t
  400. "Number of buffers to restore immediately.
  401. Remaining buffers are restored lazily (when Emacs is idle).
  402. If value is t, all buffers are restored immediately."
  403. :type '(choice (const t) integer)
  404. :group 'desktop
  405. :version "22.1")
  406. (defcustom desktop-lazy-verbose t
  407. "Verbose reporting of lazily created buffers."
  408. :type 'boolean
  409. :group 'desktop
  410. :version "22.1")
  411. (defcustom desktop-lazy-idle-delay 5
  412. "Idle delay before starting to create buffers.
  413. See `desktop-restore-eager'."
  414. :type 'integer
  415. :group 'desktop
  416. :version "22.1")
  417. ;;;###autoload
  418. (defvar-local desktop-save-buffer nil
  419. "When non-nil, save buffer status in desktop file.
  420. If the value is a function, it is called by `desktop-save' with argument
  421. DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
  422. file along with the state of the buffer for which it was called.
  423. When file names are returned, they should be formatted using the call
  424. \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
  425. Later, when `desktop-read' evaluates the desktop file, auxiliary information
  426. is passed as the argument DESKTOP-BUFFER-MISC to functions in
  427. `desktop-buffer-mode-handlers'.")
  428. (make-obsolete-variable 'desktop-buffer-modes-to-save
  429. 'desktop-save-buffer "22.1")
  430. (make-obsolete-variable 'desktop-buffer-misc-functions
  431. 'desktop-save-buffer "22.1")
  432. ;;;###autoload
  433. (defvar desktop-buffer-mode-handlers nil
  434. "Alist of major mode specific functions to restore a desktop buffer.
  435. Functions listed are called by `desktop-create-buffer' when `desktop-read'
  436. evaluates the desktop file. List elements must have the form
  437. (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
  438. Buffers with a major mode not specified here, are restored by the default
  439. handler `desktop-restore-file-buffer'.
  440. Handlers are called with argument list
  441. (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
  442. Furthermore, they may use the following variables:
  443. `desktop-file-version'
  444. `desktop-buffer-major-mode'
  445. `desktop-buffer-minor-modes'
  446. `desktop-buffer-point'
  447. `desktop-buffer-mark'
  448. `desktop-buffer-read-only'
  449. `desktop-buffer-locals'
  450. If a handler returns a buffer, then the saved mode settings
  451. and variable values for that buffer are copied into it.
  452. Modules that define a major mode that needs a special handler should contain
  453. code like
  454. (defun foo-restore-desktop-buffer
  455. ...
  456. (add-to-list \\='desktop-buffer-mode-handlers
  457. \\='(foo-mode . foo-restore-desktop-buffer))
  458. The major mode function must either be autoloaded, or of the form
  459. \"foobar-mode\" and defined in library \"foobar\", so that desktop
  460. can guess how to load the mode's definition.")
  461. ;;;###autoload
  462. (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
  463. (make-obsolete-variable 'desktop-buffer-handlers
  464. 'desktop-buffer-mode-handlers "22.1")
  465. (defcustom desktop-minor-mode-table
  466. '((auto-fill-function auto-fill-mode)
  467. (defining-kbd-macro nil)
  468. (isearch-mode nil)
  469. (vc-mode nil)
  470. (vc-dired-mode nil)
  471. (erc-track-minor-mode nil)
  472. (savehist-mode nil))
  473. "Table mapping minor mode variables to minor mode functions.
  474. Each entry has the form (NAME RESTORE-FUNCTION).
  475. NAME is the name of the buffer-local variable indicating that the minor
  476. mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
  477. RESTORE-FUNCTION nil means don't try to restore the minor mode.
  478. Only minor modes for which the name of the buffer-local variable
  479. and the name of the minor mode function are different have to be added to
  480. this table. See also `desktop-minor-mode-handlers'."
  481. :type '(alist :key-type (symbol :tag "Minor mode")
  482. :value-type (list :tag "Restore function"
  483. (choice (const nil) function)))
  484. :group 'desktop)
  485. ;;;###autoload
  486. (defvar desktop-minor-mode-handlers nil
  487. "Alist of functions to restore non-standard minor modes.
  488. Functions are called by `desktop-create-buffer' to restore minor modes.
  489. List elements must have the form
  490. (MINOR-MODE . RESTORE-FUNCTION).
  491. Minor modes not specified here, are restored by the standard minor mode
  492. function.
  493. Handlers are called with argument list
  494. (DESKTOP-BUFFER-LOCALS)
  495. Furthermore, they may use the following variables:
  496. `desktop-file-version'
  497. `desktop-buffer-file-name'
  498. `desktop-buffer-name'
  499. `desktop-buffer-major-mode'
  500. `desktop-buffer-minor-modes'
  501. `desktop-buffer-point'
  502. `desktop-buffer-mark'
  503. `desktop-buffer-read-only'
  504. `desktop-buffer-misc'
  505. When a handler is called, the buffer has been created and the major mode has
  506. been set, but local variables listed in desktop-buffer-locals has not yet been
  507. created and set.
  508. Modules that define a minor mode that needs a special handler should contain
  509. code like
  510. (defun foo-desktop-restore
  511. ...
  512. (add-to-list \\='desktop-minor-mode-handlers
  513. \\='(foo-mode . foo-desktop-restore))
  514. The minor mode function must either be autoloaded, or of the form
  515. \"foobar-mode\" and defined in library \"foobar\", so that desktop
  516. can guess how to load the mode's definition.
  517. See also `desktop-minor-mode-table'.")
  518. ;;;###autoload
  519. (put 'desktop-minor-mode-handlers 'risky-local-variable t)
  520. ;; ----------------------------------------------------------------------------
  521. (defvar desktop-dirname nil
  522. "The directory in which the desktop file should be saved.")
  523. (defun desktop-full-file-name (&optional dirname)
  524. "Return the full name of the desktop file in DIRNAME.
  525. DIRNAME omitted or nil means use `desktop-dirname'."
  526. (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
  527. (defun desktop-full-lock-name (&optional dirname)
  528. "Return the full name of the desktop lock file in DIRNAME.
  529. DIRNAME omitted or nil means use `desktop-dirname'."
  530. (expand-file-name desktop-base-lock-name (or dirname desktop-dirname)))
  531. (defconst desktop-header
  532. ";; --------------------------------------------------------------------------
  533. ;; Desktop File for Emacs
  534. ;; --------------------------------------------------------------------------
  535. " "*Header to place in Desktop file.")
  536. (defvar desktop-delay-hook nil
  537. "Hooks run after all buffers are loaded; intended for internal use.")
  538. (defvar desktop-file-checksum nil
  539. "Checksum of the last auto-saved contents of the desktop file.
  540. Used to avoid writing contents unchanged between auto-saves.")
  541. (defvar desktop-saved-frameset nil
  542. "Saved state of all frames.
  543. Only valid during frame saving & restoring; intended for internal use.")
  544. ;; ----------------------------------------------------------------------------
  545. ;; Desktop file conflict detection
  546. (defvar desktop-file-modtime nil
  547. "When the desktop file was last modified to the knowledge of this Emacs.
  548. Used to detect desktop file conflicts.")
  549. (defvar desktop-var-serdes-funs
  550. (list (list
  551. 'mark-ring
  552. (lambda (mr)
  553. (mapcar #'marker-position mr))
  554. (lambda (mr)
  555. (mapcar #'copy-marker mr))))
  556. "Table of serialization/deserialization functions for variables.
  557. Each record is a list of form: (var serializer deserializer).
  558. These records can be freely reordered, deleted, or new ones added.
  559. However, for compatibility, don't modify the functions for existing records.")
  560. (defun desktop-owner (&optional dirname)
  561. "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
  562. Return nil if no desktop file found or no Emacs process is using it.
  563. DIRNAME omitted or nil means use `desktop-dirname'."
  564. (let (owner
  565. (file (desktop-full-lock-name dirname)))
  566. (and (file-exists-p file)
  567. (ignore-errors
  568. (with-temp-buffer
  569. (insert-file-contents-literally file)
  570. (goto-char (point-min))
  571. (setq owner (read (current-buffer)))
  572. (integerp owner)))
  573. owner)))
  574. (defun desktop-claim-lock (&optional dirname)
  575. "Record this Emacs process as the owner of the desktop file in DIRNAME.
  576. DIRNAME omitted or nil means use `desktop-dirname'."
  577. (write-region (number-to-string (emacs-pid)) nil
  578. (desktop-full-lock-name dirname)))
  579. (defun desktop-release-lock (&optional dirname)
  580. "Remove the lock file for the desktop in DIRNAME.
  581. DIRNAME omitted or nil means use `desktop-dirname'."
  582. (let ((file (desktop-full-lock-name dirname)))
  583. (when (file-exists-p file) (delete-file file))))
  584. ;; ----------------------------------------------------------------------------
  585. (defun desktop-truncate (list n)
  586. "Truncate LIST to at most N elements destructively."
  587. (let ((here (nthcdr (1- n) list)))
  588. (when (consp here)
  589. (setcdr here nil))))
  590. ;; ----------------------------------------------------------------------------
  591. ;;;###autoload
  592. (defun desktop-clear ()
  593. "Empty the Desktop.
  594. This kills all buffers except for internal ones and those with names matched by
  595. a regular expression in the list `desktop-clear-preserve-buffers'.
  596. Furthermore, it clears the variables listed in `desktop-globals-to-clear'.
  597. When called interactively and `desktop-restore-frames' is non-nil, it also
  598. deletes all frames except the selected one (and its minibuffer frame,
  599. if different)."
  600. (interactive)
  601. (desktop-lazy-abort)
  602. (setq desktop-io-file-version nil)
  603. (dolist (var desktop-globals-to-clear)
  604. (if (symbolp var)
  605. (set-default var nil)
  606. (set-default var (eval (cdr var)))))
  607. (let ((preserve-regexp (concat "^\\("
  608. (mapconcat (lambda (regexp)
  609. (concat "\\(" regexp "\\)"))
  610. desktop-clear-preserve-buffers
  611. "\\|")
  612. "\\)$")))
  613. (dolist (buffer (buffer-list))
  614. (let ((bufname (buffer-name buffer)))
  615. (unless (or (eq (aref bufname 0) ?\s) ;; Don't kill internal buffers
  616. (string-match-p preserve-regexp bufname))
  617. (kill-buffer buffer)))))
  618. (delete-other-windows)
  619. (when (and desktop-restore-frames
  620. ;; Non-interactive calls to desktop-clear happen before desktop-read
  621. ;; which already takes care of frame restoration and deletion.
  622. (called-interactively-p 'any))
  623. (let* ((this (selected-frame))
  624. (mini (window-frame (minibuffer-window this)))) ; in case they differ
  625. (dolist (frame (sort (frame-list) #'frameset-minibufferless-first-p))
  626. (condition-case err
  627. (unless (or (eq frame this)
  628. (eq frame mini)
  629. ;; Don't delete daemon's initial frame, or
  630. ;; we'll never be able to close the last
  631. ;; client's frame (Bug#26912).
  632. (if (daemonp) (not (frame-parameter frame 'client)))
  633. (frame-parameter frame 'desktop-dont-clear))
  634. (delete-frame frame))
  635. (error
  636. (delay-warning 'desktop (error-message-string err))))))))
  637. ;; ----------------------------------------------------------------------------
  638. (unless noninteractive
  639. (add-hook 'kill-emacs-hook 'desktop-kill))
  640. (defun desktop-kill ()
  641. "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
  642. If the desktop should be saved and `desktop-dirname'
  643. is nil, ask the user where to save the desktop."
  644. (when (and desktop-save-mode
  645. (let ((exists (file-exists-p (desktop-full-file-name))))
  646. (or (eq desktop-save t)
  647. (and exists (eq desktop-save 'if-exists))
  648. ;; If it exists, but we aren't using it, we are going
  649. ;; to ask for a new directory below.
  650. (and exists desktop-dirname (eq desktop-save 'ask-if-new))
  651. (and
  652. (or (memq desktop-save '(ask ask-if-new))
  653. (and exists (eq desktop-save 'ask-if-exists)))
  654. (y-or-n-p "Save desktop? ")))))
  655. (unless desktop-dirname
  656. (setq desktop-dirname
  657. (file-name-as-directory
  658. (expand-file-name
  659. (read-directory-name "Directory for desktop file: " nil nil t)))))
  660. (condition-case err
  661. (desktop-save desktop-dirname t)
  662. (file-error
  663. (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
  664. (signal (car err) (cdr err))))))
  665. ;; If we own it, we don't anymore.
  666. (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
  667. ;; ----------------------------------------------------------------------------
  668. (defun desktop-list* (&rest args)
  669. (and args (apply #'cl-list* args)))
  670. ;; ----------------------------------------------------------------------------
  671. (defun desktop-buffer-info (buffer)
  672. "Return information describing BUFFER.
  673. This function is not pure, as BUFFER is made current with
  674. `set-buffer'.
  675. Returns a list of all the necessary information to recreate the
  676. buffer, which is (in order):
  677. `uniquify-buffer-base-name';
  678. `buffer-file-name';
  679. `buffer-name';
  680. `major-mode';
  681. list of minor-modes,;
  682. `point';
  683. `mark';
  684. `buffer-read-only';
  685. auxiliary information given by `desktop-save-buffer';
  686. local variables;
  687. auxiliary information given by `desktop-var-serdes-funs'."
  688. (set-buffer buffer)
  689. `(
  690. ;; base name of the buffer; replaces the buffer name if managed by uniquify
  691. ,(and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name))
  692. ;; basic information
  693. ,(desktop-file-name (buffer-file-name) desktop-dirname)
  694. ,(buffer-name)
  695. ,major-mode
  696. ;; minor modes
  697. ,(let (ret)
  698. (dolist (minor-mode (mapcar #'car minor-mode-alist) ret)
  699. (and (boundp minor-mode)
  700. (symbol-value minor-mode)
  701. (let* ((special (assq minor-mode desktop-minor-mode-table))
  702. (value (cond (special (cadr special))
  703. ((functionp minor-mode) minor-mode))))
  704. (when value (cl-pushnew value ret))))))
  705. ;; point and mark, and read-only status
  706. ,(point)
  707. ,(list (mark t) mark-active)
  708. ,buffer-read-only
  709. ;; auxiliary information
  710. ,(when (functionp desktop-save-buffer)
  711. (funcall desktop-save-buffer desktop-dirname))
  712. ;; local variables
  713. ,(let ((loclist (buffer-local-variables))
  714. (ll nil))
  715. (dolist (local desktop-locals-to-save)
  716. (let ((here (assq local loclist)))
  717. (cond (here
  718. (push here ll))
  719. ((member local loclist)
  720. (push local ll)))))
  721. ll)
  722. ,@(when (>= desktop-io-file-version 208)
  723. (list
  724. (mapcar (lambda (record)
  725. (let ((var (car record)))
  726. (list var
  727. (funcall (cadr record) (symbol-value var)))))
  728. desktop-var-serdes-funs)))))
  729. ;; ----------------------------------------------------------------------------
  730. (defun desktop--v2s (value)
  731. "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE.
  732. SEXP is an sexp that when evaluated yields VALUE.
  733. QUOTE may be `may' (value may be quoted),
  734. `must' (value must be quoted), or nil (value must not be quoted)."
  735. (cond
  736. ((or (numberp value) (null value) (eq t value) (keywordp value))
  737. (cons 'may value))
  738. ((stringp value)
  739. (let ((copy (copy-sequence value)))
  740. (set-text-properties 0 (length copy) nil copy)
  741. ;; Get rid of text properties because we cannot read them.
  742. (cons 'may copy)))
  743. ((symbolp value)
  744. (cons 'must value))
  745. ((vectorp value)
  746. (let* ((pass1 (mapcar #'desktop--v2s value))
  747. (special (assq nil pass1)))
  748. (if special
  749. (cons nil `(vector
  750. ,@(mapcar (lambda (el)
  751. (if (eq (car el) 'must)
  752. `',(cdr el) (cdr el)))
  753. pass1)))
  754. (cons 'may `[,@(mapcar #'cdr pass1)]))))
  755. ((consp value)
  756. (let ((p value)
  757. newlist
  758. use-list*)
  759. (while (consp p)
  760. (let ((q.sexp (desktop--v2s (car p))))
  761. (push q.sexp newlist))
  762. (setq p (cdr p)))
  763. (when p
  764. (let ((last (desktop--v2s p)))
  765. (setq use-list* t)
  766. (push last newlist)))
  767. (if (assq nil newlist)
  768. (cons nil
  769. `(,(if use-list* 'desktop-list* 'list)
  770. ,@(mapcar (lambda (el)
  771. (if (eq (car el) 'must)
  772. `',(cdr el) (cdr el)))
  773. (nreverse newlist))))
  774. (cons 'must
  775. `(,@(mapcar #'cdr
  776. (nreverse (if use-list* (cdr newlist) newlist)))
  777. ,@(if use-list* (cdar newlist)))))))
  778. ((subrp value)
  779. (cons nil `(symbol-function
  780. ',(intern-soft (substring (prin1-to-string value) 7 -1)))))
  781. ((markerp value)
  782. (let ((pos (marker-position value))
  783. (buf (buffer-name (marker-buffer value))))
  784. (cons nil
  785. `(let ((mk (make-marker)))
  786. (add-hook 'desktop-delay-hook
  787. `(lambda ()
  788. (set-marker ,mk ,,pos (get-buffer ,,buf))))
  789. mk))))
  790. (t ; Save as text.
  791. (cons 'may "Unprintable entity"))))
  792. ;; ----------------------------------------------------------------------------
  793. (defun desktop-value-to-string (value)
  794. "Convert VALUE to a string that when read evaluates to the same value.
  795. Not all types of values are supported."
  796. (let* ((print-escape-newlines t)
  797. (print-length nil)
  798. (print-level nil)
  799. (float-output-format nil)
  800. (quote.sexp (desktop--v2s value))
  801. (quote (car quote.sexp))
  802. (print-quoted t)
  803. (txt (prin1-to-string (cdr quote.sexp))))
  804. (if (eq quote 'must)
  805. (concat "'" txt)
  806. txt)))
  807. ;; ----------------------------------------------------------------------------
  808. (defun desktop-outvar (varspec)
  809. "Output a setq statement for variable VAR to the desktop file.
  810. The argument VARSPEC may be the variable name VAR (a symbol),
  811. or a cons cell of the form (VAR . MAX-SIZE),
  812. which means to truncate VAR's value to at most MAX-SIZE elements
  813. \(if the value is a list) before saving the value."
  814. (let (var size)
  815. (if (consp varspec)
  816. (setq var (car varspec) size (cdr varspec))
  817. (setq var varspec))
  818. (when (boundp var)
  819. (when (and (integerp size)
  820. (> size 0)
  821. (listp (eval var)))
  822. (desktop-truncate (eval var) size))
  823. (insert "(setq "
  824. (symbol-name var)
  825. " "
  826. (desktop-value-to-string (symbol-value var))
  827. ")\n"))))
  828. ;; ----------------------------------------------------------------------------
  829. (defun desktop-save-buffer-p (filename bufname mode &rest _dummy)
  830. "Return t if buffer should have its state saved in the desktop file.
  831. FILENAME is the visited file name, BUFNAME is the buffer name, and
  832. MODE is the major mode.
  833. \n\(fn FILENAME BUFNAME MODE)"
  834. (let ((case-fold-search nil)
  835. (no-regexp-to-check (not (stringp desktop-files-not-to-save)))
  836. dired-skip)
  837. (and (or filename
  838. (not (stringp desktop-buffers-not-to-save))
  839. (not (string-match-p desktop-buffers-not-to-save bufname)))
  840. (not (memq mode desktop-modes-not-to-save))
  841. (or (and filename
  842. (or no-regexp-to-check
  843. (not (string-match-p desktop-files-not-to-save filename))))
  844. (and (memq mode '(dired-mode vc-dir-mode))
  845. (or no-regexp-to-check
  846. (not (setq dired-skip
  847. (with-current-buffer bufname
  848. (string-match-p desktop-files-not-to-save
  849. default-directory))))))
  850. (and (null filename)
  851. (null dired-skip) ; bug#5755
  852. (with-current-buffer bufname desktop-save-buffer)))
  853. t)))
  854. ;; ----------------------------------------------------------------------------
  855. (defun desktop-file-name (filename dirname)
  856. "Convert FILENAME to format specified in `desktop-file-name-format'.
  857. DIRNAME must be the directory in which the desktop file will be saved."
  858. (cond
  859. ((not filename) nil)
  860. ((eq desktop-file-name-format 'tilde)
  861. (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
  862. (cond
  863. ((file-name-absolute-p relative-name) relative-name)
  864. ((string= "./" relative-name) "~/")
  865. ((string= "." relative-name) "~")
  866. (t (concat "~/" relative-name)))))
  867. ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
  868. (t (expand-file-name filename))))
  869. ;; ----------------------------------------------------------------------------
  870. (defun desktop--check-dont-save (frame)
  871. (not (frame-parameter frame 'desktop-dont-save)))
  872. (defconst desktop--app-id `(desktop . ,desktop-file-version))
  873. (defun desktop-save-frameset ()
  874. "Save the state of existing frames in `desktop-saved-frameset'.
  875. Frames with a non-nil `desktop-dont-save' parameter are not saved."
  876. (setq desktop-saved-frameset
  877. (and desktop-restore-frames
  878. (frameset-save nil
  879. :app desktop--app-id
  880. :name (concat user-login-name "@" (system-name))
  881. :predicate #'desktop--check-dont-save))))
  882. ;;;###autoload
  883. (defun desktop-save (dirname &optional release only-if-changed version)
  884. "Save the desktop in a desktop file.
  885. Parameter DIRNAME specifies where to save the desktop file.
  886. Optional parameter RELEASE says whether we're done with this
  887. desktop. If ONLY-IF-CHANGED is non-nil, compare the current
  888. desktop information to that in the desktop file, and if the
  889. desktop information has not changed since it was last saved then
  890. do not rewrite the file.
  891. This function can save the desktop in either format version
  892. 208 (which only Emacs 25.1 and later can read) or version
  893. 206 (which is readable by any Emacs from version 22.1 onwards).
  894. By default, it will use the same format the desktop file had when
  895. it was last saved, or version 208 when writing a fresh desktop
  896. file.
  897. To upgrade a version 206 file to version 208, call this command
  898. explicitly with a bare prefix argument: C-u M-x desktop-save.
  899. You are recommended to do this once you have firmly upgraded to
  900. Emacs 25.1 (or later). To downgrade a version 208 file to version
  901. 206, use a double command prefix: C-u C-u M-x desktop-save.
  902. Confirmation will be requested in either case. In a non-interactive
  903. call, VERSION can be given as an integer, either 206 or 208, which
  904. will be accepted as the format version in which to save the file
  905. without further confirmation."
  906. (interactive (list
  907. ;; Or should we just use (car desktop-path)?
  908. (let ((default (if (member "." desktop-path)
  909. default-directory
  910. user-emacs-directory)))
  911. (read-directory-name "Directory to save desktop file in: "
  912. default default t))
  913. nil
  914. nil
  915. current-prefix-arg))
  916. (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
  917. (save-excursion
  918. (let ((eager desktop-restore-eager)
  919. (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
  920. (when
  921. (or (not new-modtime) ; nothing to overwrite
  922. (equal desktop-file-modtime new-modtime)
  923. (yes-or-no-p (if desktop-file-modtime
  924. (if (> (float-time new-modtime) (float-time desktop-file-modtime))
  925. "Desktop file is more recent than the one loaded. Save anyway? "
  926. "Desktop file isn't the one loaded. Overwrite it? ")
  927. "Current desktop was not loaded from a file. Overwrite this desktop file? "))
  928. (unless release (error "Desktop file conflict")))
  929. ;; If we're done with it, release the lock.
  930. ;; Otherwise, claim it if it's unclaimed or if we created it.
  931. (if release
  932. (desktop-release-lock)
  933. (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
  934. ;; What format are we going to write the file in?
  935. (setq desktop-io-file-version
  936. (cond
  937. ((equal version '(4))
  938. (if (or (eq desktop-io-file-version 208)
  939. (yes-or-no-p "Save desktop file in format 208 \
  940. \(Readable by Emacs 25.1 and later only)? "))
  941. 208
  942. (or desktop-io-file-version desktop-native-file-version)))
  943. ((equal version '(16))
  944. (if (or (eq desktop-io-file-version 206)
  945. (yes-or-no-p "Save desktop file in format 206 \
  946. \(Readable by all Emacs versions since 22.1)? "))
  947. 206
  948. (or desktop-io-file-version desktop-native-file-version)))
  949. ((memq version '(206 208))
  950. version)
  951. ((null desktop-io-file-version) ; As yet, no desktop file exists.
  952. desktop-native-file-version)
  953. (t
  954. desktop-io-file-version)))
  955. (with-temp-buffer
  956. (insert
  957. ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
  958. desktop-header
  959. ";; Created " (current-time-string) "\n"
  960. ";; Desktop file format version " (format "%d" desktop-io-file-version) "\n"
  961. ";; Emacs version " emacs-version "\n")
  962. (save-excursion (run-hooks 'desktop-save-hook))
  963. (goto-char (point-max))
  964. (insert "\n;; Global section:\n")
  965. ;; Called here because we save the window/frame state as a global
  966. ;; variable for compatibility with previous Emacsen.
  967. (desktop-save-frameset)
  968. (unless (memq 'desktop-saved-frameset desktop-globals-to-save)
  969. (desktop-outvar 'desktop-saved-frameset))
  970. (mapc (function desktop-outvar) desktop-globals-to-save)
  971. (setq desktop-saved-frameset nil) ; after saving desktop-globals-to-save
  972. (when (memq 'kill-ring desktop-globals-to-save)
  973. (insert
  974. "(setq kill-ring-yank-pointer (nthcdr "
  975. (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
  976. " kill-ring))\n"))
  977. (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
  978. (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
  979. (let ((base (pop l)))
  980. (when (apply 'desktop-save-buffer-p l)
  981. (insert "("
  982. (if (or (not (integerp eager))
  983. (if (zerop eager)
  984. nil
  985. (setq eager (1- eager))))
  986. "desktop-create-buffer"
  987. "desktop-append-buffer-args")
  988. " "
  989. (format "%d" desktop-io-file-version))
  990. ;; If there's a non-empty base name, we save it instead of the buffer name
  991. (when (and base (not (string= base "")))
  992. (setcar (nthcdr 1 l) base))
  993. (dolist (e l)
  994. (insert "\n " (desktop-value-to-string e)))
  995. (insert ")\n\n"))))
  996. (setq default-directory desktop-dirname)
  997. ;; When auto-saving, avoid writing if nothing has changed since the last write.
  998. (let* ((beg (and only-if-changed
  999. (save-excursion
  1000. (goto-char (point-min))
  1001. ;; Don't check the header with changing timestamp
  1002. (and (search-forward "Global section" nil t)
  1003. ;; Also skip the timestamp in desktop-saved-frameset
  1004. ;; if it's saved in the first non-header line
  1005. (search-forward "desktop-saved-frameset"
  1006. (line-beginning-position 3) t)
  1007. ;; This is saved after the timestamp
  1008. (search-forward (format "%S" desktop--app-id) nil t))
  1009. (point))))
  1010. (checksum (and beg (md5 (current-buffer) beg (point-max) 'emacs-mule))))
  1011. (unless (and checksum (equal checksum desktop-file-checksum))
  1012. (let ((coding-system-for-write 'emacs-mule))
  1013. (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
  1014. (setq desktop-file-checksum checksum)
  1015. ;; We remember when it was modified (which is presumably just now).
  1016. (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))))
  1017. ;; ----------------------------------------------------------------------------
  1018. ;;;###autoload
  1019. (defun desktop-remove ()
  1020. "Delete desktop file in `desktop-dirname'.
  1021. This function also sets `desktop-dirname' to nil."
  1022. (interactive)
  1023. (when desktop-dirname
  1024. (let ((filename (desktop-full-file-name)))
  1025. (setq desktop-dirname nil)
  1026. (when (file-exists-p filename)
  1027. (delete-file filename)))))
  1028. (defvar desktop-buffer-args-list nil
  1029. "List of args for `desktop-create-buffer'.")
  1030. (defvar desktop-lazy-timer nil)
  1031. ;; ----------------------------------------------------------------------------
  1032. (defun desktop-restoring-frameset-p ()
  1033. "True if calling `desktop-restore-frameset' will actually restore it."
  1034. (and desktop-restore-frames desktop-saved-frameset (display-graphic-p) t))
  1035. (defun desktop-restore-frameset ()
  1036. "Restore the state of a set of frames.
  1037. This function depends on the value of `desktop-saved-frameset'
  1038. being set (usually, by reading it from the desktop)."
  1039. (when (desktop-restoring-frameset-p)
  1040. (frameset-restore desktop-saved-frameset
  1041. :reuse-frames (eq desktop-restore-reuses-frames t)
  1042. :cleanup-frames (not (eq desktop-restore-reuses-frames 'keep))
  1043. :force-display desktop-restore-in-current-display
  1044. :force-onscreen desktop-restore-forces-onscreen)))
  1045. ;; Just to silence the byte compiler.
  1046. ;; Dynamically bound in `desktop-read'.
  1047. (defvar desktop-first-buffer)
  1048. (defvar desktop-buffer-ok-count)
  1049. (defvar desktop-buffer-fail-count)
  1050. ;; FIXME Interactively, this should have the option to prompt for dirname.
  1051. ;;;###autoload
  1052. (defun desktop-read (&optional dirname)
  1053. "Read and process the desktop file in directory DIRNAME.
  1054. Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
  1055. directories listed in `desktop-path'. If a desktop file is found, it
  1056. is processed and `desktop-after-read-hook' is run. If no desktop file
  1057. is found, clear the desktop and run `desktop-no-desktop-file-hook'.
  1058. This function is a no-op when Emacs is running in batch mode.
  1059. It returns t if a desktop file was loaded, nil otherwise."
  1060. (interactive)
  1061. (unless noninteractive
  1062. (setq desktop-dirname
  1063. (file-name-as-directory
  1064. (expand-file-name
  1065. (or
  1066. ;; If DIRNAME is specified, use it.
  1067. (and (< 0 (length dirname)) dirname)
  1068. ;; Otherwise search desktop file in desktop-path.
  1069. (let ((dirs desktop-path))
  1070. (while (and dirs
  1071. (not (file-exists-p
  1072. (desktop-full-file-name (car dirs)))))
  1073. (setq dirs (cdr dirs)))
  1074. (and dirs (car dirs)))
  1075. ;; If not found and `desktop-path' is non-nil, use its first element.
  1076. (and desktop-path (car desktop-path))
  1077. ;; Default: .emacs.d.
  1078. user-emacs-directory))))
  1079. (if (file-exists-p (desktop-full-file-name))
  1080. ;; Desktop file found, but is it already in use?
  1081. (let ((desktop-first-buffer nil)
  1082. (desktop-buffer-ok-count 0)
  1083. (desktop-buffer-fail-count 0)
  1084. (owner (desktop-owner))
  1085. ;; Avoid desktop saving during evaluation of desktop buffer.
  1086. (desktop-save nil)
  1087. (desktop-autosave-was-enabled))
  1088. (if (and owner
  1089. (memq desktop-load-locked-desktop '(nil ask))
  1090. (or (null desktop-load-locked-desktop)
  1091. (daemonp)
  1092. (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
  1093. Using it may cause conflicts. Use it anyway? " owner)))))
  1094. (let ((default-directory desktop-dirname))
  1095. (setq desktop-dirname nil)
  1096. (run-hooks 'desktop-not-loaded-hook)
  1097. (unless desktop-dirname
  1098. (message "Desktop file in use; not loaded.")))
  1099. (desktop-lazy-abort)
  1100. ;; Temporarily disable the autosave that will leave it
  1101. ;; disabled when loading the desktop fails with errors,
  1102. ;; thus not overwriting the desktop with broken contents.
  1103. (setq desktop-autosave-was-enabled
  1104. (memq 'desktop-auto-save-set-timer window-configuration-change-hook))
  1105. (desktop-auto-save-disable)
  1106. ;; Evaluate desktop buffer and remember when it was modified.
  1107. (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
  1108. (load (desktop-full-file-name) t t t)
  1109. ;; If it wasn't already, mark it as in-use, to bother other
  1110. ;; desktop instances.
  1111. (unless (eq (emacs-pid) owner)
  1112. (condition-case nil
  1113. (desktop-claim-lock)
  1114. (file-error (message "Couldn't record use of desktop file")
  1115. (sit-for 1))))
  1116. (unless (desktop-restoring-frameset-p)
  1117. ;; `desktop-create-buffer' puts buffers at end of the buffer list.
  1118. ;; We want buffers existing prior to evaluating the desktop (and
  1119. ;; not reused) to be placed at the end of the buffer list, so we
  1120. ;; move them here.
  1121. (mapc 'bury-buffer
  1122. (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
  1123. (switch-to-buffer (car (buffer-list))))
  1124. (run-hooks 'desktop-delay-hook)
  1125. (setq desktop-delay-hook nil)
  1126. (desktop-restore-frameset)
  1127. (run-hooks 'desktop-after-read-hook)
  1128. (message "Desktop: %s%d buffer%s restored%s%s."
  1129. (if desktop-saved-frameset
  1130. (let ((fn (length (frameset-states desktop-saved-frameset))))
  1131. (format "%d frame%s, "
  1132. fn (if (= fn 1) "" "s")))
  1133. "")
  1134. desktop-buffer-ok-count
  1135. (if (= 1 desktop-buffer-ok-count) "" "s")
  1136. (if (< 0 desktop-buffer-fail-count)
  1137. (format ", %d failed to restore" desktop-buffer-fail-count)
  1138. "")
  1139. (if desktop-buffer-args-list
  1140. (format ", %d to restore lazily"
  1141. (length desktop-buffer-args-list))
  1142. ""))
  1143. (unless (desktop-restoring-frameset-p)
  1144. ;; Bury the *Messages* buffer to not reshow it when burying
  1145. ;; the buffer we switched to above.
  1146. (when (buffer-live-p (get-buffer "*Messages*"))
  1147. (bury-buffer "*Messages*"))
  1148. ;; Clear all windows' previous and next buffers, these have
  1149. ;; been corrupted by the `switch-to-buffer' calls in
  1150. ;; `desktop-restore-file-buffer' (bug#11556). This is a
  1151. ;; brute force fix and should be replaced by a more subtle
  1152. ;; strategy eventually.
  1153. (walk-window-tree (lambda (window)
  1154. (set-window-prev-buffers window nil)
  1155. (set-window-next-buffers window nil))))
  1156. (setq desktop-saved-frameset nil)
  1157. (if desktop-autosave-was-enabled (desktop-auto-save-enable))
  1158. t))
  1159. ;; No desktop file found.
  1160. (let ((default-directory desktop-dirname))
  1161. (run-hooks 'desktop-no-desktop-file-hook))
  1162. (message "No desktop file.")
  1163. nil)))
  1164. ;; ----------------------------------------------------------------------------
  1165. ;; Maintained for backward compatibility
  1166. ;;;###autoload
  1167. (defun desktop-load-default ()
  1168. "Load the `default' start-up library manually.
  1169. Also inhibit further loading of it."
  1170. (declare (obsolete desktop-save-mode "22.1"))
  1171. (unless inhibit-default-init ; safety check
  1172. (load "default" t t)
  1173. (setq inhibit-default-init t)))
  1174. ;; ----------------------------------------------------------------------------
  1175. ;;;###autoload
  1176. (defun desktop-change-dir (dirname)
  1177. "Change to desktop saved in DIRNAME.
  1178. Kill the desktop as specified by variables `desktop-save-mode' and
  1179. `desktop-save', then clear the desktop and load the desktop file in
  1180. directory DIRNAME."
  1181. (interactive "DChange to directory: ")
  1182. (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
  1183. (desktop-kill)
  1184. (desktop-clear)
  1185. (desktop-read dirname))
  1186. ;; ----------------------------------------------------------------------------
  1187. ;;;###autoload
  1188. (defun desktop-save-in-desktop-dir ()
  1189. "Save the desktop in directory `desktop-dirname'."
  1190. (interactive)
  1191. (if desktop-dirname
  1192. (desktop-save desktop-dirname)
  1193. (call-interactively 'desktop-save))
  1194. (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname)))
  1195. ;; ----------------------------------------------------------------------------
  1196. ;; Auto-Saving.
  1197. (defvar desktop-auto-save-timer nil)
  1198. (defun desktop-auto-save-enable (&optional timeout)
  1199. (when (and (integerp (or timeout desktop-auto-save-timeout))
  1200. (> (or timeout desktop-auto-save-timeout) 0))
  1201. (add-hook 'window-configuration-change-hook 'desktop-auto-save-set-timer)))
  1202. (defun desktop-auto-save-disable ()
  1203. (remove-hook 'window-configuration-change-hook 'desktop-auto-save-set-timer)
  1204. (desktop-auto-save-cancel-timer))
  1205. (defun desktop-auto-save ()
  1206. "Save the desktop periodically.
  1207. Called by the timer created in `desktop-auto-save-set-timer'."
  1208. (when (and desktop-save-mode
  1209. (integerp desktop-auto-save-timeout)
  1210. (> desktop-auto-save-timeout 0)
  1211. ;; Avoid desktop saving during lazy loading.
  1212. (not desktop-lazy-timer)
  1213. ;; Save only to own desktop file.
  1214. (eq (emacs-pid) (desktop-owner))
  1215. desktop-dirname)
  1216. (desktop-save desktop-dirname nil t)))
  1217. (defun desktop-auto-save-set-timer ()
  1218. "Set the auto-save timer.
  1219. Cancel any previous timer. When `desktop-auto-save-timeout' is a positive
  1220. integer, start a new idle timer to call `desktop-auto-save' repeatedly
  1221. after that many seconds of idle time."
  1222. (desktop-auto-save-cancel-timer)
  1223. (when (and (integerp desktop-auto-save-timeout)
  1224. (> desktop-auto-save-timeout 0))
  1225. (setq desktop-auto-save-timer
  1226. (run-with-idle-timer desktop-auto-save-timeout nil
  1227. 'desktop-auto-save))))
  1228. (defun desktop-auto-save-cancel-timer ()
  1229. (when desktop-auto-save-timer
  1230. (cancel-timer desktop-auto-save-timer)
  1231. (setq desktop-auto-save-timer nil)))
  1232. ;; ----------------------------------------------------------------------------
  1233. ;;;###autoload
  1234. (defun desktop-revert ()
  1235. "Revert to the last loaded desktop."
  1236. (interactive)
  1237. (unless desktop-dirname
  1238. (error "Unknown desktop directory"))
  1239. (unless (file-exists-p (desktop-full-file-name))
  1240. (error "No desktop file found"))
  1241. (desktop-clear)
  1242. (desktop-read desktop-dirname))
  1243. (defvar desktop-buffer-major-mode)
  1244. (defvar desktop-buffer-locals)
  1245. (defvar auto-insert) ; from autoinsert.el
  1246. ;; ----------------------------------------------------------------------------
  1247. (defun desktop-restore-file-buffer (buffer-filename
  1248. _buffer-name
  1249. _buffer-misc)
  1250. "Restore a file buffer."
  1251. (when buffer-filename
  1252. (if (or (file-exists-p buffer-filename)
  1253. (let ((msg (format "Desktop: File \"%s\" no longer exists."
  1254. buffer-filename)))
  1255. (if desktop-missing-file-warning
  1256. (y-or-n-p (concat msg " Re-create buffer? "))
  1257. (message "%s" msg)
  1258. nil)))
  1259. (let* ((auto-insert nil) ; Disable auto insertion
  1260. (coding-system-for-read
  1261. (or coding-system-for-read
  1262. (cdr (assq 'buffer-file-coding-system
  1263. desktop-buffer-locals))))
  1264. (buf (find-file-noselect buffer-filename :nowarn)))
  1265. (condition-case nil
  1266. (switch-to-buffer buf)
  1267. (error (pop-to-buffer buf)))
  1268. (and (not (eq major-mode desktop-buffer-major-mode))
  1269. (functionp desktop-buffer-major-mode)
  1270. (funcall desktop-buffer-major-mode))
  1271. buf)
  1272. nil)))
  1273. (defun desktop-load-file (function)
  1274. "Load the file where auto loaded FUNCTION is defined.
  1275. If FUNCTION is not currently defined, guess the library that defines it
  1276. and try to load that."
  1277. (if (fboundp function)
  1278. (autoload-do-load (symbol-function function) function)
  1279. ;; Guess that foobar-mode is defined in foobar.
  1280. ;; TODO rather than guessing or requiring an autoload, the desktop
  1281. ;; file should record the name of the library.
  1282. (let ((name (symbol-name function)))
  1283. (if (string-match "\\`\\(.*\\)-mode\\'" name)
  1284. (with-demoted-errors "Require error in desktop-load-file: %S"
  1285. (require (intern (match-string 1 name)) nil t))))))
  1286. ;; ----------------------------------------------------------------------------
  1287. ;; Create a buffer, load its file, set its mode, ...;
  1288. ;; called from Desktop file only.
  1289. (defun desktop-create-buffer
  1290. (file-version
  1291. buffer-filename
  1292. buffer-name
  1293. buffer-majormode
  1294. buffer-minormodes
  1295. buffer-point
  1296. buffer-mark
  1297. buffer-readonly
  1298. buffer-misc
  1299. &optional
  1300. buffer-locals
  1301. compacted-vars
  1302. &rest _unsupported)
  1303. (setq desktop-io-file-version file-version)
  1304. (let ((desktop-file-version file-version)
  1305. (desktop-buffer-file-name buffer-filename)
  1306. (desktop-buffer-name buffer-name)
  1307. (desktop-buffer-major-mode buffer-majormode)
  1308. (desktop-buffer-minor-modes buffer-minormodes)
  1309. (desktop-buffer-point buffer-point)
  1310. (desktop-buffer-mark buffer-mark)
  1311. (desktop-buffer-read-only buffer-readonly)
  1312. (desktop-buffer-misc buffer-misc)
  1313. (desktop-buffer-locals buffer-locals))
  1314. ;; To make desktop files with relative file names possible, we cannot
  1315. ;; allow `default-directory' to change. Therefore we save current buffer.
  1316. (save-current-buffer
  1317. ;; Give major mode module a chance to add a handler.
  1318. (desktop-load-file desktop-buffer-major-mode)
  1319. (let ((buffer-list (buffer-list))
  1320. (result
  1321. (condition-case-unless-debug err
  1322. (funcall (or (cdr (assq desktop-buffer-major-mode
  1323. desktop-buffer-mode-handlers))
  1324. 'desktop-restore-file-buffer)
  1325. desktop-buffer-file-name
  1326. desktop-buffer-name
  1327. desktop-buffer-misc)
  1328. (error
  1329. (message "Desktop: Can't load buffer %s: %s"
  1330. desktop-buffer-name
  1331. (error-message-string err))
  1332. (when desktop-missing-file-warning (sit-for 1))
  1333. nil))))
  1334. (if (bufferp result)
  1335. (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
  1336. (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
  1337. (setq result nil))
  1338. ;; Restore buffer list order with new buffer at end. Don't change
  1339. ;; the order for old desktop files (old desktop module behavior).
  1340. (unless (< desktop-file-version 206)
  1341. (dolist (buf buffer-list)
  1342. (and (buffer-live-p buf)
  1343. (bury-buffer buf)))
  1344. (when result (bury-buffer result)))
  1345. (when result
  1346. (unless (or desktop-first-buffer (< desktop-file-version 206))
  1347. (setq desktop-first-buffer result))
  1348. (set-buffer result)
  1349. (unless (equal (buffer-name) desktop-buffer-name)
  1350. (rename-buffer desktop-buffer-name t))
  1351. ;; minor modes
  1352. (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
  1353. (auto-fill-mode 1))
  1354. ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
  1355. (auto-fill-mode 0))
  1356. (t
  1357. (dolist (minor-mode desktop-buffer-minor-modes)
  1358. ;; Give minor mode module a chance to add a handler.
  1359. (desktop-load-file minor-mode)
  1360. (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
  1361. (if handler
  1362. (funcall handler desktop-buffer-locals)
  1363. (when (functionp minor-mode)
  1364. (funcall minor-mode 1)))))))
  1365. ;; Even though point and mark are non-nil when written by
  1366. ;; `desktop-save', they may be modified by handlers wanting to set
  1367. ;; point or mark themselves.
  1368. (when desktop-buffer-point
  1369. (goto-char
  1370. (condition-case err
  1371. ;; Evaluate point. Thus point can be something like
  1372. ;; '(search-forward ...
  1373. (eval desktop-buffer-point)
  1374. (error (message "%s" (error-message-string err)) 1))))
  1375. (when desktop-buffer-mark
  1376. (if (consp desktop-buffer-mark)
  1377. (progn
  1378. (move-marker (mark-marker) (car desktop-buffer-mark))
  1379. (if (car (cdr desktop-buffer-mark))
  1380. (activate-mark 'dont-touch-tmm)))
  1381. (move-marker (mark-marker) desktop-buffer-mark)))
  1382. ;; Never override file system if the file really is read-only marked.
  1383. (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
  1384. (dolist (this desktop-buffer-locals)
  1385. (if (consp this)
  1386. ;; An entry of this form `(symbol . value)'.
  1387. (progn
  1388. (make-local-variable (car this))
  1389. (set (car this) (cdr this)))
  1390. ;; An entry of the form `symbol'.
  1391. (make-local-variable this)
  1392. (makunbound this)))
  1393. ;; adjust `buffer-display-time' for the downtime. e.g.,
  1394. ;; * if `buffer-display-time' was 8:00
  1395. ;; * and emacs stopped at `desktop-file-modtime' == 11:00
  1396. ;; * and we are loading the desktop file at (current-time) 12:30,
  1397. ;; -> then we restore `buffer-display-time' as 9:30,
  1398. ;; for the sake of `clean-buffer-list': preserving the invariant
  1399. ;; "how much time the user spent in Emacs without looking at this buffer".
  1400. (setq buffer-display-time
  1401. (if buffer-display-time
  1402. (time-add buffer-display-time
  1403. (time-subtract (current-time)
  1404. desktop-file-modtime))
  1405. (current-time)))
  1406. (unless (< desktop-file-version 208) ; Don't misinterpret any old custom args
  1407. (dolist (record compacted-vars)
  1408. (let*
  1409. ((var (car record))
  1410. (deser-fun (nth 2 (assq var desktop-var-serdes-funs))))
  1411. (if deser-fun (set var (funcall deser-fun (cadr record))))))))
  1412. result))))
  1413. ;; ----------------------------------------------------------------------------
  1414. ;; Backward compatibility -- update parameters to 205 standards.
  1415. (defun desktop-buffer (buffer-filename buffer-name buffer-majormode
  1416. mim pt mk ro tl fc cfs cr buffer-misc)
  1417. (desktop-create-buffer 205 buffer-filename buffer-name
  1418. buffer-majormode (cdr mim) pt mk ro
  1419. buffer-misc
  1420. (list (cons 'truncate-lines tl)
  1421. (cons 'fill-column fc)
  1422. (cons 'case-fold-search cfs)
  1423. (cons 'case-replace cr)
  1424. (cons 'overwrite-mode (car mim)))))
  1425. (defun desktop-append-buffer-args (&rest args)
  1426. "Append ARGS at end of `desktop-buffer-args-list'.
  1427. ARGS must be an argument list for `desktop-create-buffer'."
  1428. (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
  1429. (unless desktop-lazy-timer
  1430. (setq desktop-lazy-timer
  1431. (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
  1432. (defun desktop-lazy-create-buffer ()
  1433. "Pop args from `desktop-buffer-args-list', create buffer and bury it."
  1434. (when desktop-buffer-args-list
  1435. (let* ((remaining (length desktop-buffer-args-list))
  1436. (args (pop desktop-buffer-args-list))
  1437. (buffer-name (nth 2 args))
  1438. (msg (format "Desktop lazily opening %s (%s remaining)..."
  1439. buffer-name remaining)))
  1440. (when desktop-lazy-verbose
  1441. (message "%s" msg))
  1442. (let ((desktop-first-buffer nil)
  1443. (desktop-buffer-ok-count 0)
  1444. (desktop-buffer-fail-count 0))
  1445. (apply 'desktop-create-buffer args)
  1446. (run-hooks 'desktop-delay-hook)
  1447. (setq desktop-delay-hook nil)
  1448. (bury-buffer (get-buffer buffer-name))
  1449. (when desktop-lazy-verbose
  1450. (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
  1451. (defun desktop-idle-create-buffers ()
  1452. "Create buffers until the user does something, then stop.
  1453. If there are no buffers left to create, kill the timer."
  1454. (let ((repeat 1))
  1455. (while (and repeat desktop-buffer-args-list)
  1456. (save-window-excursion
  1457. (desktop-lazy-create-buffer))
  1458. (setq repeat (sit-for 0.2))
  1459. (unless desktop-buffer-args-list
  1460. (cancel-timer desktop-lazy-timer)
  1461. (setq desktop-lazy-timer nil)
  1462. (message "Lazy desktop load complete")
  1463. (sit-for 3)
  1464. (message "")))))
  1465. (defun desktop-lazy-complete ()
  1466. "Run the desktop load to completion."
  1467. (interactive)
  1468. (let ((desktop-lazy-verbose t))
  1469. (while desktop-buffer-args-list
  1470. (save-window-excursion
  1471. (desktop-lazy-create-buffer)))
  1472. (message "Lazy desktop load complete")))
  1473. (defun desktop-lazy-abort ()
  1474. "Abort lazy loading of the desktop."
  1475. (interactive)
  1476. (when desktop-lazy-timer
  1477. (cancel-timer desktop-lazy-timer)
  1478. (setq desktop-lazy-timer nil))
  1479. (when desktop-buffer-args-list
  1480. (setq desktop-buffer-args-list nil)
  1481. (when (called-interactively-p 'interactive)
  1482. (message "Lazy desktop load aborted"))))
  1483. ;; ----------------------------------------------------------------------------
  1484. ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
  1485. ;; command line, we do the rest of what it takes to use desktop, but do it
  1486. ;; after finishing loading the init file.
  1487. ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
  1488. ;; functions are processed after `after-init-hook'.
  1489. (add-hook
  1490. 'after-init-hook
  1491. (lambda ()
  1492. (let ((key "--no-desktop"))
  1493. (when (member key command-line-args)
  1494. (setq command-line-args (delete key command-line-args))
  1495. (desktop-save-mode 0)))
  1496. (when desktop-save-mode
  1497. (desktop-read)
  1498. (setq inhibit-startup-screen t))))
  1499. (provide 'desktop)
  1500. ;;; desktop.el ends here