]> code.delx.au - gnu-emacs/blobdiff - lisp/desktop.el
** offby1@blarg.net, Nov 5: calendar gets wrong end for Daylight Savings Time
[gnu-emacs] / lisp / desktop.el
index 8e37d5d0d3605440d2d08df892d5b195cbd1abd8..d2b2271d3061ec208c13ed967692b7457d9321b3 100644 (file)
 ;; are supplied to handle special major and minor modes respectively.
 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
 ;; to restore a desktop buffer. Elements must have the form
-;; 
+;;
 ;;    (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
-;; 
+;;
 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
 ;; evaluates the desktop file. Buffers with a major mode not specified here,
 ;; are restored by the default handler `desktop-restore-file-buffer'.
 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
 ;; non-standard minor modes.  Elements must have the form
-;; 
+;;
 ;;    (MINOR-MODE . RESTORE-FUNCTION).
-;; 
+;;
 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
 ;; Minor modes not specified here, are restored by the standard minor mode
 ;; function.  If you write a module that defines a major or minor mode that
@@ -153,8 +153,9 @@ backward compatibility.")
 (define-minor-mode desktop-save-mode
   "Toggle desktop saving mode.
 With numeric ARG, turn desktop saving on if ARG is positive, off
-otherwise.  See variable `desktop-save' for a description of when the
-desktop is saved."
+otherwise.  If desktop saving is turned on, the state of Emacs is
+saved from one session to another.  See variable `desktop-save'
+and function `desktop-read' for details."
   :global t
   :group 'desktop)
 
@@ -175,7 +176,8 @@ Possible values are:
 The desktop is never saved when `desktop-save-mode' is nil.
 The variables `desktop-dirname' and `desktop-base-file-name'
 determine where the desktop is saved."
-  :type '(choice
+  :type
+  '(choice
     (const :tag "Always save" t)
     (const :tag "Always ask" ask)
     (const :tag "Ask if desktop file is new, else do save" ask-if-new)
@@ -201,7 +203,7 @@ The base name of the file is specified in `desktop-base-file-name'."
   :version "22.1")
 
 (defcustom desktop-missing-file-warning nil
-  "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
+  "If non-nil, offer to recreate the buffer of a deleted file.
 Also pause for a moment to display message about errors signaled in
 `desktop-buffer-mode-handlers'.
 
@@ -212,6 +214,7 @@ If nil, just print error messages in the message buffer."
 
 (defcustom desktop-no-desktop-file-hook nil
   "Normal hook run when `desktop-read' can't find a desktop file.
+Run in the directory in which the desktop file was sought.
 May be used to show a dired buffer."
   :type 'hook
   :group 'desktop
@@ -222,11 +225,14 @@ May be used to show a dired buffer."
 May be used to show a buffer list."
   :type 'hook
   :group 'desktop
+  :options '(list-buffers)
   :version "22.1")
 
 (defcustom desktop-save-hook nil
   "Normal hook run before the desktop is saved in a desktop file.
-This is useful for truncating history lists, for example."
+Run with the desktop buffer current with only the header present.
+May be used to add to the desktop code or to truncate history lists,
+for example."
   :type 'hook
   :group 'desktop)
 
@@ -282,6 +288,7 @@ these won't be deleted."
     size-indication-mode
     buffer-file-coding-system
     indent-tabs-mode
+    tab-width
     indicate-buffer-boundaries
     indicate-empty-lines
     show-trailing-whitespace)
@@ -475,6 +482,11 @@ See also `desktop-minor-mode-table'.")
 (defvar desktop-dirname nil
   "The directory in which the desktop file should be saved.")
 
+(defun desktop-full-file-name (&optional dirname)
+  "Return the full name of the desktop file in DIRNAME.
+DIRNAME omitted or nil means use `desktop-dirname'."
+  (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
+
 (defconst desktop-header
 ";; --------------------------------------------------------------------------
 ;; Desktop File for Emacs
@@ -492,6 +504,7 @@ See also `desktop-minor-mode-table'.")
        (setcdr here nil))))
 
 ;; ----------------------------------------------------------------------------
+;;;###autoload
 (defun desktop-clear ()
   "Empty the Desktop.
 This kills all buffers except for internal ones and those with names matched by
@@ -528,29 +541,26 @@ Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
   "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
 If the desktop should be saved and `desktop-dirname'
 is nil, ask the user where to save the desktop."
-  (when
-    (and
-      desktop-save-mode
-      (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
-        (or
-          (eq desktop-save t)
-          (and exists (memq desktop-save '(ask-if-new if-exists)))
-          (and
-            (or
-              (memq desktop-save '(ask ask-if-new))
-              (and exists (eq desktop-save 'ask-if-exists)))
-            (y-or-n-p "Save desktop? ")))))
+  (when (and desktop-save-mode
+             (let ((exists (file-exists-p (desktop-full-file-name))))
+               (or (eq desktop-save t)
+                   (and exists (memq desktop-save '(ask-if-new if-exists)))
+                   (and
+                    (or (memq desktop-save '(ask ask-if-new))
+                        (and exists (eq desktop-save 'ask-if-exists)))
+                    (y-or-n-p "Save desktop? ")))))
     (unless desktop-dirname
       (setq desktop-dirname
-        (file-name-as-directory
-          (expand-file-name
-            (call-interactively
-              (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
+            (file-name-as-directory
+             (expand-file-name
+              (call-interactively
+               (lambda (dir)
+                 (interactive "DDirectory for desktop file: ") dir))))))
     (condition-case err
-      (desktop-save desktop-dirname)
+        (desktop-save desktop-dirname)
       (file-error
-        (unless (yes-or-no-p "Error while saving the desktop.  Ignore? ")
-          (signal (car err) (cdr err)))))))
+       (unless (yes-or-no-p "Error while saving the desktop.  Ignore? ")
+         (signal (car err) (cdr err)))))))
 
 ;; ----------------------------------------------------------------------------
 (defun desktop-list* (&rest args)
@@ -661,7 +671,7 @@ Not all types of values are supported."
 (defun desktop-outvar (varspec)
   "Output a setq statement for variable VAR to the desktop file.
 The argument VARSPEC may be the variable name VAR (a symbol),
-or a cons cell of the form  (VAR . MAX-SIZE),
+or a cons cell of the form (VAR . MAX-SIZE),
 which means to truncate VAR's value to at most MAX-SIZE elements
 \(if the value is a list) before saving the value."
   (let (var size)
@@ -684,7 +694,8 @@ which means to truncate VAR's value to at most MAX-SIZE elements
 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
   "Return t if buffer should have its state saved in the desktop file.
 FILENAME is the visited file name, BUFNAME is the buffer name, and
-MODE is the major mode."
+MODE is the major mode.
+\n\(fn FILENAME BUFNAME MODE)"
   (let ((case-fold-search nil))
     (and (not (string-match desktop-buffers-not-to-save bufname))
          (not (memq mode desktop-modes-not-to-save))
@@ -714,6 +725,7 @@ DIRNAME must be the directory in which the desktop file will be saved."
     (t (expand-file-name filename))))
 
 ;; ----------------------------------------------------------------------------
+;;;###autoload
 (defun desktop-save (dirname)
   "Save the desktop in a desktop file.
 Parameter DIRNAME specifies where to save the desktop file.
@@ -722,7 +734,7 @@ See also `desktop-base-file-name'."
   (run-hooks 'desktop-save-hook)
   (setq dirname (file-name-as-directory (expand-file-name dirname)))
   (save-excursion
-    (let ((filename (expand-file-name desktop-base-file-name dirname))
+    (let ((filename (desktop-full-file-name dirname))
           (info
             (mapcar
               #'(lambda (b)
@@ -762,54 +774,52 @@ See also `desktop-base-file-name'."
                         (setq locals (cdr locals)))
                       ll)))
               (buffer-list)))
-          (eager desktop-restore-eager)
-          (buf (get-buffer-create "*desktop*")))
-      (set-buffer buf)
-      (erase-buffer)
-
-      (insert
-        ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
-        desktop-header
-        ";; Created " (current-time-string) "\n"
-        ";; Desktop file format version " desktop-file-version "\n"
-        ";; Emacs version " emacs-version "\n\n"
-        ";; Global section:\n")
-      (mapc (function desktop-outvar) desktop-globals-to-save)
-      (if (memq 'kill-ring desktop-globals-to-save)
+          (eager desktop-restore-eager))
+      (with-temp-buffer
         (insert
-          "(setq kill-ring-yank-pointer (nthcdr "
-          (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
-          " kill-ring))\n"))
-
-      (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
-      (mapc #'(lambda (l)
-                (when (apply 'desktop-save-buffer-p l)
-                  (insert "("
-                          (if (or (not (integerp eager))
-                                  (unless (zerop eager)
-                                    (setq eager (1- eager))
-                                    t))
-                              "desktop-create-buffer"
-                            "desktop-append-buffer-args")
-                          " "
-                          desktop-file-version)
-                  (mapc #'(lambda (e)
-                            (insert "\n  " (desktop-value-to-string e)))
-                        l)
-                  (insert ")\n\n")))
-            info)
-      (setq default-directory dirname)
-      (let ((coding-system-for-write 'emacs-mule))
-        (write-region (point-min) (point-max) filename nil 'nomessage))))
+         ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
+         desktop-header
+         ";; Created " (current-time-string) "\n"
+         ";; Desktop file format version " desktop-file-version "\n"
+         ";; Emacs version " emacs-version "\n\n"
+         ";; Global section:\n")
+        (mapc (function desktop-outvar) desktop-globals-to-save)
+        (if (memq 'kill-ring desktop-globals-to-save)
+            (insert
+             "(setq kill-ring-yank-pointer (nthcdr "
+             (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
+             " kill-ring))\n"))
+
+        (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
+        (mapc #'(lambda (l)
+                  (when (apply 'desktop-save-buffer-p l)
+                    (insert "("
+                            (if (or (not (integerp eager))
+                                    (unless (zerop eager)
+                                      (setq eager (1- eager))
+                                      t))
+                                "desktop-create-buffer"
+                              "desktop-append-buffer-args")
+                            " "
+                            desktop-file-version)
+                    (mapc #'(lambda (e)
+                              (insert "\n  " (desktop-value-to-string e)))
+                          l)
+                    (insert ")\n\n")))
+              info)
+        (setq default-directory dirname)
+        (let ((coding-system-for-write 'emacs-mule))
+          (write-region (point-min) (point-max) filename nil 'nomessage)))))
   (setq desktop-dirname dirname))
 
 ;; ----------------------------------------------------------------------------
+;;;###autoload
 (defun desktop-remove ()
   "Delete desktop file in `desktop-dirname'.
 This function also sets `desktop-dirname' to nil."
   (interactive)
   (when desktop-dirname
-    (let ((filename (expand-file-name desktop-base-file-name desktop-dirname)))
+    (let ((filename (desktop-full-file-name)))
       (setq desktop-dirname nil)
       (when (file-exists-p filename)
         (delete-file filename)))))
@@ -832,32 +842,32 @@ It returns t if a desktop file was loaded, nil otherwise."
   (interactive)
   (unless noninteractive
     (setq desktop-dirname
-      (file-name-as-directory
-        (expand-file-name
-          (or
-            ;; If DIRNAME is specified, use it.
-            (and (< 0 (length dirname)) dirname)
-            ;; Otherwise search desktop file in desktop-path.
-            (let ((dirs desktop-path))
-              (while
-                (and
-                  dirs
-                  (not
-                    (file-exists-p (expand-file-name desktop-base-file-name (car dirs)))))
-                (setq dirs (cdr dirs)))
-              (and dirs (car dirs)))
-            ;; If not found and `desktop-path' is non-nil, use its first element.
-            (and desktop-path (car desktop-path))
-            ;; Default: Home directory.
-            "~"))))
-    (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
+          (file-name-as-directory
+           (expand-file-name
+            (or
+             ;; If DIRNAME is specified, use it.
+             (and (< 0 (length dirname)) dirname)
+             ;; Otherwise search desktop file in desktop-path.
+             (let ((dirs desktop-path))
+               (while (and dirs
+                           (not (file-exists-p
+                                 (desktop-full-file-name (car dirs)))))
+                 (setq dirs (cdr dirs)))
+               (and dirs (car dirs)))
+             ;; If not found and `desktop-path' is non-nil, use its first element.
+             (and desktop-path (car desktop-path))
+             ;; Default: Home directory.
+             "~"))))
+    (if (file-exists-p (desktop-full-file-name))
       ;; Desktop file found, process it.
       (let ((desktop-first-buffer nil)
             (desktop-buffer-ok-count 0)
-            (desktop-buffer-fail-count 0))
-        (setq desktop-lazy-timer nil)
+            (desktop-buffer-fail-count 0)
+            ;; Avoid desktop saving during evaluation of desktop buffer.
+           (desktop-save nil))
+       (desktop-lazy-abort)
         ;; Evaluate desktop buffer.
-        (load (expand-file-name desktop-base-file-name desktop-dirname) t t t)
+        (load (desktop-full-file-name) t t t)
         ;; `desktop-create-buffer' puts buffers at end of the buffer list.
         ;; We want buffers existing prior to evaluating the desktop (and not reused)
         ;; to be placed at the end of the buffer list, so we move them here.
@@ -927,7 +937,7 @@ directory DIRNAME."
   (interactive)
   (unless desktop-dirname
     (error "Unknown desktop directory"))
-  (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
+  (unless (file-exists-p (desktop-full-file-name))
     (error "No desktop file found"))
   (desktop-clear)
   (desktop-read desktop-dirname))
@@ -945,7 +955,7 @@ directory DIRNAME."
               (let ((msg (format "Desktop: File \"%s\" no longer exists."
                                  desktop-buffer-file-name)))
                  (if desktop-missing-file-warning
-                    (y-or-n-p (concat msg " Re-create? "))
+                    (y-or-n-p (concat msg " Re-create buffer? "))
                    (message "%s" msg)
                    nil)))
          (let* ((auto-insert nil) ; Disable auto insertion