comparison lisp/desktop.el @ 71453:bef13fc7f695

(desktop-full-file-name): New function. (desktop-kill, desktop-save, desktop-remove, desktop-read) (desktop-revert): Use it. (desktop-clear, desktop-save, desktop-remove): Add autoload cookie. (desktop-after-read-hook): Add option list-buffers. (desktop-locals-to-save): Add tab-width. (desktop-save-mode, desktop-no-desktop-file-hook, desktop-save-hook): Fix docstring.
author Lars Hansen <larsh@soem.dk>
date Fri, 23 Jun 2006 21:42:04 +0000
parents 019755175634
children 1c7fb3ed1a95 138ce2701550
comparison
equal deleted inserted replaced
71452:dd6b8b8cda6d 71453:bef13fc7f695
151 151
152 ;;;###autoload 152 ;;;###autoload
153 (define-minor-mode desktop-save-mode 153 (define-minor-mode desktop-save-mode
154 "Toggle desktop saving mode. 154 "Toggle desktop saving mode.
155 With numeric ARG, turn desktop saving on if ARG is positive, off 155 With numeric ARG, turn desktop saving on if ARG is positive, off
156 otherwise. See variable `desktop-save' for a description of when the 156 otherwise. If desktop saving is turned on, the state of Emacs is
157 desktop is saved." 157 saved from one session to another. See variable `desktop-save'
158 and function `desktop-read' for details."
158 :global t 159 :global t
159 :group 'desktop) 160 :group 'desktop)
160 161
161 ;; Maintained for backward compatibility 162 ;; Maintained for backward compatibility
162 (define-obsolete-variable-alias 'desktop-enable 163 (define-obsolete-variable-alias 'desktop-enable
173 if-exists -- save if desktop file exists, otherwise don't save. 174 if-exists -- save if desktop file exists, otherwise don't save.
174 nil -- never save. 175 nil -- never save.
175 The desktop is never saved when `desktop-save-mode' is nil. 176 The desktop is never saved when `desktop-save-mode' is nil.
176 The variables `desktop-dirname' and `desktop-base-file-name' 177 The variables `desktop-dirname' and `desktop-base-file-name'
177 determine where the desktop is saved." 178 determine where the desktop is saved."
178 :type '(choice 179 :type
180 '(choice
179 (const :tag "Always save" t) 181 (const :tag "Always save" t)
180 (const :tag "Always ask" ask) 182 (const :tag "Always ask" ask)
181 (const :tag "Ask if desktop file is new, else do save" ask-if-new) 183 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
182 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists) 184 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
183 (const :tag "Save if desktop file exists, else don't" if-exists) 185 (const :tag "Save if desktop file exists, else don't" if-exists)
210 :group 'desktop 212 :group 'desktop
211 :version "22.1") 213 :version "22.1")
212 214
213 (defcustom desktop-no-desktop-file-hook nil 215 (defcustom desktop-no-desktop-file-hook nil
214 "Normal hook run when `desktop-read' can't find a desktop file. 216 "Normal hook run when `desktop-read' can't find a desktop file.
217 Run in the directory in which the desktop file was sought.
215 May be used to show a dired buffer." 218 May be used to show a dired buffer."
216 :type 'hook 219 :type 'hook
217 :group 'desktop 220 :group 'desktop
218 :version "22.1") 221 :version "22.1")
219 222
220 (defcustom desktop-after-read-hook nil 223 (defcustom desktop-after-read-hook nil
221 "Normal hook run after a successful `desktop-read'. 224 "Normal hook run after a successful `desktop-read'.
222 May be used to show a buffer list." 225 May be used to show a buffer list."
223 :type 'hook 226 :type 'hook
224 :group 'desktop 227 :group 'desktop
228 :options '(list-buffers)
225 :version "22.1") 229 :version "22.1")
226 230
227 (defcustom desktop-save-hook nil 231 (defcustom desktop-save-hook nil
228 "Normal hook run before the desktop is saved in a desktop file. 232 "Normal hook run before the desktop is saved in a desktop file.
229 This is useful for truncating history lists, for example." 233 Run with the desktop buffer current with only the header present.
234 May be used to add to the desktop code or to truncate history lists,
235 for example."
230 :type 'hook 236 :type 'hook
231 :group 'desktop) 237 :group 'desktop)
232 238
233 (defcustom desktop-globals-to-save 239 (defcustom desktop-globals-to-save
234 '(desktop-missing-file-warning 240 '(desktop-missing-file-warning
280 line-number-mode 286 line-number-mode
281 column-number-mode 287 column-number-mode
282 size-indication-mode 288 size-indication-mode
283 buffer-file-coding-system 289 buffer-file-coding-system
284 indent-tabs-mode 290 indent-tabs-mode
291 tab-width
285 indicate-buffer-boundaries 292 indicate-buffer-boundaries
286 indicate-empty-lines 293 indicate-empty-lines
287 show-trailing-whitespace) 294 show-trailing-whitespace)
288 "List of local variables to save for each buffer. 295 "List of local variables to save for each buffer.
289 The variables are saved only when they really are local. Conventional minor 296 The variables are saved only when they really are local. Conventional minor
473 480
474 ;; ---------------------------------------------------------------------------- 481 ;; ----------------------------------------------------------------------------
475 (defvar desktop-dirname nil 482 (defvar desktop-dirname nil
476 "The directory in which the desktop file should be saved.") 483 "The directory in which the desktop file should be saved.")
477 484
485 (defun desktop-full-file-name (&optional dirname)
486 "Return the full name of the desktop file in DIRNAME.
487 DIRNAME omitted or nil means use `desktop-dirname'."
488 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
489
478 (defconst desktop-header 490 (defconst desktop-header
479 ";; -------------------------------------------------------------------------- 491 ";; --------------------------------------------------------------------------
480 ;; Desktop File for Emacs 492 ;; Desktop File for Emacs
481 ;; -------------------------------------------------------------------------- 493 ;; --------------------------------------------------------------------------
482 " "*Header to place in Desktop file.") 494 " "*Header to place in Desktop file.")
490 (let ((here (nthcdr (1- n) list))) 502 (let ((here (nthcdr (1- n) list)))
491 (if (consp here) 503 (if (consp here)
492 (setcdr here nil)))) 504 (setcdr here nil))))
493 505
494 ;; ---------------------------------------------------------------------------- 506 ;; ----------------------------------------------------------------------------
507 ;;;###autoload
495 (defun desktop-clear () 508 (defun desktop-clear ()
496 "Empty the Desktop. 509 "Empty the Desktop.
497 This kills all buffers except for internal ones and those with names matched by 510 This kills all buffers except for internal ones and those with names matched by
498 a regular expression in the list `desktop-clear-preserve-buffers'. 511 a regular expression in the list `desktop-clear-preserve-buffers'.
499 Furthermore, it clears the variables listed in `desktop-globals-to-clear'." 512 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
526 539
527 (defun desktop-kill () 540 (defun desktop-kill ()
528 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do. 541 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
529 If the desktop should be saved and `desktop-dirname' 542 If the desktop should be saved and `desktop-dirname'
530 is nil, ask the user where to save the desktop." 543 is nil, ask the user where to save the desktop."
531 (when 544 (when (and desktop-save-mode
532 (and 545 (let ((exists (file-exists-p (desktop-full-file-name))))
533 desktop-save-mode 546 (or (eq desktop-save t)
534 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname)))) 547 (and exists (memq desktop-save '(ask-if-new if-exists)))
535 (or 548 (and
536 (eq desktop-save t) 549 (or (memq desktop-save '(ask ask-if-new))
537 (and exists (memq desktop-save '(ask-if-new if-exists))) 550 (and exists (eq desktop-save 'ask-if-exists)))
538 (and 551 (y-or-n-p "Save desktop? ")))))
539 (or
540 (memq desktop-save '(ask ask-if-new))
541 (and exists (eq desktop-save 'ask-if-exists)))
542 (y-or-n-p "Save desktop? ")))))
543 (unless desktop-dirname 552 (unless desktop-dirname
544 (setq desktop-dirname 553 (setq desktop-dirname
545 (file-name-as-directory 554 (file-name-as-directory
546 (expand-file-name 555 (expand-file-name
547 (call-interactively 556 (call-interactively
548 (lambda (dir) (interactive "DDirectory for desktop file: ") dir)))))) 557 (lambda (dir)
558 (interactive "DDirectory for desktop file: ") dir))))))
549 (condition-case err 559 (condition-case err
550 (desktop-save desktop-dirname) 560 (desktop-save desktop-dirname)
551 (file-error 561 (file-error
552 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ") 562 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
553 (signal (car err) (cdr err))))))) 563 (signal (car err) (cdr err)))))))
554 564
555 ;; ---------------------------------------------------------------------------- 565 ;; ----------------------------------------------------------------------------
556 (defun desktop-list* (&rest args) 566 (defun desktop-list* (&rest args)
557 (if (null (cdr args)) 567 (if (null (cdr args))
558 (car args) 568 (car args)
713 (t (concat "~/" relative-name))))) 723 (t (concat "~/" relative-name)))))
714 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname)) 724 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
715 (t (expand-file-name filename)))) 725 (t (expand-file-name filename))))
716 726
717 ;; ---------------------------------------------------------------------------- 727 ;; ----------------------------------------------------------------------------
728 ;;;###autoload
718 (defun desktop-save (dirname) 729 (defun desktop-save (dirname)
719 "Save the desktop in a desktop file. 730 "Save the desktop in a desktop file.
720 Parameter DIRNAME specifies where to save the desktop file. 731 Parameter DIRNAME specifies where to save the desktop file.
721 See also `desktop-base-file-name'." 732 See also `desktop-base-file-name'."
722 (interactive "DDirectory to save desktop file in: ") 733 (interactive "DDirectory to save desktop file in: ")
723 (run-hooks 'desktop-save-hook) 734 (run-hooks 'desktop-save-hook)
724 (setq dirname (file-name-as-directory (expand-file-name dirname))) 735 (setq dirname (file-name-as-directory (expand-file-name dirname)))
725 (save-excursion 736 (save-excursion
726 (let ((filename (expand-file-name desktop-base-file-name dirname)) 737 (let ((filename (desktop-full-file-name dirname))
727 (info 738 (info
728 (mapcar 739 (mapcar
729 #'(lambda (b) 740 #'(lambda (b)
730 (set-buffer b) 741 (set-buffer b)
731 (list 742 (list
800 (let ((coding-system-for-write 'emacs-mule)) 811 (let ((coding-system-for-write 'emacs-mule))
801 (write-region (point-min) (point-max) filename nil 'nomessage))))) 812 (write-region (point-min) (point-max) filename nil 'nomessage)))))
802 (setq desktop-dirname dirname)) 813 (setq desktop-dirname dirname))
803 814
804 ;; ---------------------------------------------------------------------------- 815 ;; ----------------------------------------------------------------------------
816 ;;;###autoload
805 (defun desktop-remove () 817 (defun desktop-remove ()
806 "Delete desktop file in `desktop-dirname'. 818 "Delete desktop file in `desktop-dirname'.
807 This function also sets `desktop-dirname' to nil." 819 This function also sets `desktop-dirname' to nil."
808 (interactive) 820 (interactive)
809 (when desktop-dirname 821 (when desktop-dirname
810 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname))) 822 (let ((filename (desktop-full-file-name)))
811 (setq desktop-dirname nil) 823 (setq desktop-dirname nil)
812 (when (file-exists-p filename) 824 (when (file-exists-p filename)
813 (delete-file filename))))) 825 (delete-file filename)))))
814 826
815 (defvar desktop-buffer-args-list nil 827 (defvar desktop-buffer-args-list nil
828 This function is a no-op when Emacs is running in batch mode. 840 This function is a no-op when Emacs is running in batch mode.
829 It returns t if a desktop file was loaded, nil otherwise." 841 It returns t if a desktop file was loaded, nil otherwise."
830 (interactive) 842 (interactive)
831 (unless noninteractive 843 (unless noninteractive
832 (setq desktop-dirname 844 (setq desktop-dirname
833 (file-name-as-directory 845 (file-name-as-directory
834 (expand-file-name 846 (expand-file-name
835 (or 847 (or
836 ;; If DIRNAME is specified, use it. 848 ;; If DIRNAME is specified, use it.
837 (and (< 0 (length dirname)) dirname) 849 (and (< 0 (length dirname)) dirname)
838 ;; Otherwise search desktop file in desktop-path. 850 ;; Otherwise search desktop file in desktop-path.
839 (let ((dirs desktop-path)) 851 (let ((dirs desktop-path))
840 (while 852 (while (and dirs
841 (and 853 (not (file-exists-p
842 dirs 854 (desktop-full-file-name (car dirs)))))
843 (not 855 (setq dirs (cdr dirs)))
844 (file-exists-p (expand-file-name desktop-base-file-name (car dirs))))) 856 (and dirs (car dirs)))
845 (setq dirs (cdr dirs))) 857 ;; If not found and `desktop-path' is non-nil, use its first element.
846 (and dirs (car dirs))) 858 (and desktop-path (car desktop-path))
847 ;; If not found and `desktop-path' is non-nil, use its first element. 859 ;; Default: Home directory.
848 (and desktop-path (car desktop-path)) 860 "~"))))
849 ;; Default: Home directory. 861 (if (file-exists-p (desktop-full-file-name))
850 "~"))))
851 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
852 ;; Desktop file found, process it. 862 ;; Desktop file found, process it.
853 (let ((desktop-first-buffer nil) 863 (let ((desktop-first-buffer nil)
854 (desktop-buffer-ok-count 0) 864 (desktop-buffer-ok-count 0)
855 (desktop-buffer-fail-count 0)) 865 (desktop-buffer-fail-count 0))
856 (setq desktop-lazy-timer nil) 866 (setq desktop-lazy-timer nil)
857 ;; Evaluate desktop buffer. 867 ;; Evaluate desktop buffer.
858 (load (expand-file-name desktop-base-file-name desktop-dirname) t t t) 868 (load (desktop-full-file-name) t t t)
859 ;; `desktop-create-buffer' puts buffers at end of the buffer list. 869 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
860 ;; We want buffers existing prior to evaluating the desktop (and not reused) 870 ;; We want buffers existing prior to evaluating the desktop (and not reused)
861 ;; to be placed at the end of the buffer list, so we move them here. 871 ;; to be placed at the end of the buffer list, so we move them here.
862 (mapc 'bury-buffer 872 (mapc 'bury-buffer
863 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list)))))) 873 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
923 (defun desktop-revert () 933 (defun desktop-revert ()
924 "Revert to the last loaded desktop." 934 "Revert to the last loaded desktop."
925 (interactive) 935 (interactive)
926 (unless desktop-dirname 936 (unless desktop-dirname
927 (error "Unknown desktop directory")) 937 (error "Unknown desktop directory"))
928 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname)) 938 (unless (file-exists-p (desktop-full-file-name))
929 (error "No desktop file found")) 939 (error "No desktop file found"))
930 (desktop-clear) 940 (desktop-clear)
931 (desktop-read desktop-dirname)) 941 (desktop-read desktop-dirname))
932 942
933 ;; ---------------------------------------------------------------------------- 943 ;; ----------------------------------------------------------------------------