desktop.el 49 KB

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