home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / desktop.el < prev    next >
Encoding:
Text File  |  1995-05-13  |  18.8 KB  |  540 lines

  1. ;;; desktop.el --- save partial status of Emacs when killed
  2.  
  3. ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Morten Welinder <terra@diku.dk>
  6. ;; Version: 2.09
  7. ;; Keywords: customization
  8. ;; Favourite-brand-of-beer: None, I hate beer.
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Synched up with: FSF 19.28.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; Save the Desktop, i.e.,
  31. ;;    - some global variables
  32. ;;     - the list of buffers with associated files.  For each buffer also
  33. ;;        - the major mode
  34. ;;        - the default directory
  35. ;;        - the point
  36. ;;        - the mark & mark-active
  37. ;;        - buffer-read-only
  38. ;;        - some local variables
  39.  
  40. ;; To use this, first put these three lines in the bottom of your .emacs
  41. ;; file (the later the better):
  42. ;;
  43. ;;    (load "desktop")
  44. ;;    (desktop-load-default)
  45. ;;    (desktop-read)
  46. ;;
  47. ;; Between the second and the third line you may wish to add something that
  48. ;; updates the variables `desktop-globals-to-save' and/or
  49. ;; `desktop-locals-to-save'.  If for instance you want to save the local
  50. ;; variable `foobar' for every buffer in which it is local, you could add
  51. ;; the line
  52. ;;
  53. ;;    (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
  54. ;;
  55. ;; To avoid saving excessive amounts of data you may also with to add
  56. ;; something like the following
  57. ;;
  58. ;;    (add-hook 'kill-emacs-hook
  59. ;;          '(lambda ()
  60. ;;             (desktop-truncate search-ring 3)
  61. ;;             (desktop-truncate regexp-search-ring 3)))
  62. ;;
  63. ;; which will make sure that no more than three search items are saved.  You
  64. ;; must place this line *after* the (load "desktop") line.  See also the
  65. ;; variable desktop-save-hook.
  66.  
  67. ;; Start Emacs in the root directory of your "project". The desktop saver
  68. ;; is inactive by default.  You activate it by M-x desktop-save RET.  When
  69. ;; you exit the next time the above data will be saved.  This ensures that
  70. ;; all the files you were editing will be reloaded the next time you start
  71. ;; Emacs from the same directory and that points will be set where you
  72. ;; left them.  If you save a desktop file in your home directory it will
  73. ;; act as a default desktop when you start Emacs from a directory that
  74. ;; doesn't have its own.  I never do this, but you may want to.
  75.  
  76. ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
  77. ;; in your home directory is used for that.  Saving global default values
  78. ;; for buffers is an example of misuse.
  79.  
  80. ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
  81. ;; `desktop-globals-to-save' (by default it isn't).  This may result in saving
  82. ;; things you did not mean to keep.  Use M-x desktop-clear RET.
  83.  
  84. ;; Thanks to  hetrick@phys.uva.nl (Jim Hetrick)      for useful ideas.
  85. ;;            avk@rtsg.mot.com (Andrew V. Klein)     for a dired tip.
  86. ;;            chris@tecc.co.uk (Chris Boucher)       for a mark tip.
  87. ;;            f89-kam@nada.kth.se (Klas Mellbourn)   for a mh-e tip.
  88. ;;            kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
  89. ;;            treese@lcs.mit.edu (Win Treese)        for ange-ftp ftps.
  90. ;; ---------------------------------------------------------------------------
  91. ;; TODO:
  92. ;;
  93. ;; Save window configuration.
  94. ;; Recognize more minor modes.
  95. ;; Save mark rings.
  96. ;; Start-up with buffer-menu???
  97.  
  98. ;;; Code:
  99.  
  100. ;; Make the compilation more silent
  101. (eval-when-compile
  102.   ;; We use functions from these modules
  103.   ;; We can't (require 'mh-e) since that wants to load something.
  104.   (mapcar 'require '(info dired reporter)))
  105. ;; ----------------------------------------------------------------------------
  106. ;; USER OPTIONS -- settings you might want to play with.
  107. ;; ----------------------------------------------------------------------------
  108. (defconst desktop-basefilename
  109.   (if (eq system-type 'ms-dos)
  110.       "emacs.dsk" ; Ms-Dos does not support multiple dots in file name
  111.     ".emacs.desktop")
  112.   "File for Emacs desktop.  A directory name will be prepended to this name.")
  113.  
  114. (defvar desktop-missing-file-warning t
  115.   "*If non-nil then issue warning if a file no longer exists.
  116. Otherwise simply ignore the file.")
  117.  
  118. (defvar desktop-globals-to-save
  119.   (list 'desktop-missing-file-warning
  120.     ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
  121.     ;; 'kill-ring
  122.     'tags-file-name
  123.     'tags-table-list
  124.     'search-ring
  125.     'regexp-search-ring
  126.     'register-alist
  127.     ;; 'desktop-globals-to-save    ; Itself!
  128.     )
  129.   "List of global variables to save when killing Emacs.")
  130.  
  131. (defvar desktop-locals-to-save
  132.   (list 'desktop-locals-to-save        ; Itself!  Think it over.
  133.         'truncate-lines
  134.     'case-fold-search
  135.     'case-replace
  136.     'fill-column
  137.     'overwrite-mode
  138.     'change-log-default-name
  139.     'line-number-mode
  140.     )
  141.   "List of local variables to save for each buffer.  The variables are saved
  142. only when they really are local.")
  143. (make-variable-buffer-local 'desktop-locals-to-save)
  144.  
  145. ;; We skip .log files because they are normally temporary.
  146. ;;         (ftp) files because they require passwords and whatsnot.
  147. ;;         TAGS files to save time (tags-file-name is saved instead).
  148. (defvar desktop-buffers-not-to-save
  149.  "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
  150.  "Regexp identifying buffers that are to be excluded from saving.")
  151.  
  152. ;; Skip ange-ftp files
  153. (defvar desktop-files-not-to-save
  154.   "^/[^/:]*:"
  155.   "Regexp identifying files whose buffers are to be excluded from saving.")
  156.  
  157. (defvar desktop-buffer-handlers
  158.   '(desktop-buffer-dired
  159.     desktop-buffer-rmail
  160.     desktop-buffer-mh
  161.     desktop-buffer-info
  162.     desktop-buffer-file)
  163.   "*List of functions to call in order to create a buffer.  The functions are
  164. called without explicit parameters but may access the the major mode as `mam',
  165. the file name as `fn', the buffer name as `bn', the default directory as
  166. `dd'.  If some function returns non-nil no further functions are called.
  167. If the function returns t then the buffer is considered created.")
  168.  
  169. (defvar desktop-create-buffer-form "(desktop-create-buffer 205"
  170.   "Opening of form for creation of new buffers.")
  171.  
  172. (defvar desktop-save-hook nil
  173.   "Hook run before saving the desktop to allow you to cut history lists and
  174. the like shorter.")
  175. ;; ----------------------------------------------------------------------------
  176. (defvar desktop-dirname nil
  177.   "The directory in which the current desktop file resides.")
  178.  
  179. (defconst desktop-header
  180. ";; --------------------------------------------------------------------------
  181. ;; Desktop File for Emacs
  182. ;; --------------------------------------------------------------------------
  183. " "*Header to place in Desktop file.")
  184.  
  185. (defvar desktop-delay-hook nil
  186.   "Hooks run after all buffers are loaded; intended for internal use.")
  187. ;; ----------------------------------------------------------------------------
  188. (defun desktop-truncate (l n)
  189.   "Truncate LIST to at most N elements destructively."
  190.   (let ((here (nthcdr (1- n) l)))
  191.     (if (consp here)
  192.     (setcdr here nil))))
  193. ;; ----------------------------------------------------------------------------
  194. (defun desktop-clear () "Empty the Desktop."
  195.   (interactive)
  196.   (setq kill-ring nil
  197.     kill-ring-yank-pointer nil
  198.     search-ring nil
  199.     search-ring-yank-pointer nil
  200.     regexp-search-ring nil
  201.     regexp-search-ring-yank-pointer nil)
  202.   (mapcar (function (lambda (x)
  203.               ;; XEmacs change
  204.               (if (not (equal (buffer-name x) "*Warnings*"))
  205.               (kill-buffer x)))) (buffer-list))
  206.   (delete-other-windows))
  207. ;; ----------------------------------------------------------------------------
  208. (add-hook 'kill-emacs-hook 'desktop-kill)
  209.  
  210. (defun desktop-kill ()
  211.   (if desktop-dirname
  212.       (condition-case err
  213.       (desktop-save desktop-dirname)
  214.     (file-error
  215.      (if (yes-or-no-p "Error while saving the desktop.  Quit anyway? ")
  216.          nil
  217.        (signal (car err) (cdr err)))))))
  218. ;; ----------------------------------------------------------------------------
  219. (defun desktop-internal-v2s (val)
  220.   "Convert VALUE to a pair (quote . txt) where txt is a string that when read
  221. and evaluated yields value.  quote may be 'may (value may be quoted),
  222. 'must (values must be quoted), or nil (value may not be quoted)."
  223.   (cond
  224.    ((or (numberp val) (stringp val) (null val) (eq t val))
  225.     (cons 'may (prin1-to-string val)))
  226.    ((symbolp val)
  227.     (cons 'must (prin1-to-string val)))
  228.    ((vectorp val)
  229.     (let* ((special nil)
  230.        (pass1 (mapcar
  231.            (lambda (el)
  232.              (let ((res (desktop-internal-v2s el)))
  233.                (if (null (car res))
  234.                (setq special t))
  235.                res))
  236.            val)))
  237.       (if special
  238.       (cons nil (concat "(vector "
  239.                 (mapconcat (lambda (el)
  240.                      (if (eq (car el) 'must)
  241.                          (concat "'" (cdr el))
  242.                        (cdr el)))
  243.                        pass1
  244.                        " ")
  245.                 ")"))
  246.     (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
  247.    ((consp val)
  248.     (let ((p val)
  249.       newlist
  250.       anynil)
  251.       (while (consp p)
  252.     (let ((q.txt (desktop-internal-v2s (car p))))
  253.       (or anynil (setq anynil (null (car q.txt))))
  254.       (setq newlist (cons q.txt newlist)))
  255.     (setq p (cdr p)))
  256.       (if p
  257.       (let ((last (desktop-internal-v2s p))
  258.         (el (car newlist)))
  259.         (setcar newlist
  260.             (if (or anynil (setq anynil (null (car last))))
  261.             (cons nil
  262.                   (concat "(cons "
  263.                       (if (eq (car el) 'must) "'" "")
  264.                       (cdr el)
  265.                       " "
  266.                       (if (eq (car last) 'must) "'" "")
  267.                       (cdr last)
  268.                       ")"))
  269.               (cons 'must
  270.                 (concat (cdr el) " . " (cdr last)))))))
  271.       (setq newlist (nreverse newlist))
  272.       (if anynil
  273.       (cons nil
  274.         (concat "(list "
  275.             (mapconcat (lambda (el)
  276.                      (if (eq (car el) 'must)
  277.                      (concat "'" (cdr el))
  278.                        (cdr el)))
  279.                    newlist
  280.                    " ")
  281.             ")"))
  282.     (cons 'must
  283.           (concat "(" (mapconcat 'cdr newlist " ") ")")))))
  284.    ((subrp val)
  285.     (cons nil (concat "(symbol-function '"
  286.               (substring (prin1-to-string val) 7 -1)
  287.               ")")))
  288.    ((markerp val)
  289.     (let ((pos (prin1-to-string (marker-position val)))
  290.       (buf (prin1-to-string (buffer-name (marker-buffer val)))))
  291.       (cons nil (concat "(let ((mk (make-marker)))"
  292.             " (add-hook 'desktop-delay-hook"
  293.             " (list 'lambda '() (list 'set-marker mk "
  294.             pos " (get-buffer " buf ")))) mk)"))))
  295.    (t                    ; save as text
  296.     (cons 'may "\"Unprintable entity\""))))
  297.  
  298. (defun desktop-value-to-string (val)
  299.   "Convert VALUE to a string that when read evaluates to the same value.  Not
  300. all types of values are supported."
  301.   (let* ((print-escape-newlines t)
  302.      (float-output-format nil)
  303.      (quote.txt (desktop-internal-v2s val))
  304.      (quote (car quote.txt))
  305.      (txt (cdr quote.txt)))
  306.     (if (eq quote 'must)
  307.     (concat "'" txt)
  308.       txt)))
  309. ;; ----------------------------------------------------------------------------
  310. (defun desktop-outvar (var)
  311.   "Output a setq statement for VAR to the desktop file."
  312.   (if (boundp var)
  313.       (insert "(setq "
  314.           (symbol-name var)
  315.           " "
  316.           (desktop-value-to-string (symbol-value var))
  317.           ")\n")))
  318. ;; ----------------------------------------------------------------------------
  319. (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
  320.   "Return t if the desktop should record a particular buffer for next startup.
  321. FILENAME is the visited file name, BUFNAME is the buffer name, and
  322. MODE is the major mode."
  323.   (let ((case-fold-search nil))
  324.     (or (and filename
  325.          (not (string-match desktop-buffers-not-to-save bufname))
  326.          (not (string-match desktop-files-not-to-save filename)))
  327.     (and (eq mode 'dired-mode)
  328.          (save-excursion
  329.            (set-buffer (get-buffer bufname))
  330.            (not (string-match desktop-files-not-to-save
  331.                   default-directory))))
  332.     (and (null filename)
  333.          (memq mode '(Info-mode rmail-mode))))))
  334. ;; ----------------------------------------------------------------------------
  335. (defun desktop-save (dirname)
  336.   "Save the Desktop file.  Parameter DIRNAME specifies where to save desktop."
  337.   (interactive "DDirectory to save desktop file in: ")
  338.   (run-hooks 'desktop-save-hook)
  339.   (save-excursion
  340.     (let ((filename (expand-file-name
  341.              (concat dirname desktop-basefilename)))
  342.       (info (nreverse
  343.          (mapcar
  344.           (function (lambda (b)
  345.                   (set-buffer b)
  346.                   (list
  347.                    (buffer-file-name)
  348.                    (buffer-name)
  349.                    major-mode
  350.                    (list    ; list explaining minor modes
  351.                 (not (null auto-fill-function)))
  352.                    (point)
  353.                    (list (mark t) ;; mark-active
  354.                      (not (null (mark)))) ; XEmacs
  355.                    buffer-read-only
  356.                    (cond ((eq major-mode 'Info-mode)
  357.                       (list Info-current-file
  358.                         Info-current-node))
  359.                      ((eq major-mode 'dired-mode)
  360.                       (nreverse
  361.                        (mapcar
  362.                     (function car)
  363.                     dired-subdir-alist)))
  364.                      )
  365.                    (let ((locals desktop-locals-to-save)
  366.                      (loclist (buffer-local-variables))
  367.                      (ll))
  368.                  (while locals
  369.                    (let ((here (assq (car locals) loclist)))
  370.                      (if here
  371.                      (setq ll (cons here ll))
  372.                        (if (member (car locals) loclist)
  373.                        (setq ll (cons (car locals) ll)))))
  374.                    (setq locals (cdr locals)))
  375.                  ll)
  376.                    )))
  377.           (buffer-list))))
  378.       (buf (get-buffer-create "*desktop*")))
  379.       (set-buffer buf)
  380.       (erase-buffer)
  381.  
  382.       (insert desktop-header
  383.           ";; Created " (current-time-string) "\n"
  384.           ";; Emacs version " emacs-version "\n\n"
  385.           ";; Global section:\n")
  386.       (mapcar (function desktop-outvar) desktop-globals-to-save)
  387.       (if (memq 'kill-ring desktop-globals-to-save)
  388.       (insert "(setq kill-ring-yank-pointer (nthcdr "
  389.           (int-to-string
  390.            (- (length kill-ring) (length kill-ring-yank-pointer)))
  391.           " kill-ring))\n"))
  392.  
  393.       (insert "\n;; Buffer section:\n")
  394.       (mapcar
  395.        (function (lambda (l)
  396.            (if (apply 'desktop-save-buffer-p l)
  397.                (progn
  398.              (insert desktop-create-buffer-form)
  399.              (mapcar
  400.               (function (lambda (e)
  401.                       (insert "\n  "
  402.                           (desktop-value-to-string e))))
  403.               l)
  404.              (insert ")\n\n")))))
  405.        info)
  406.       (setq default-directory dirname)
  407.       (if (file-exists-p filename) (delete-file filename))
  408.       (write-region (point-min) (point-max) filename nil 'nomessage)))
  409.   (setq desktop-dirname dirname))
  410. ;; ----------------------------------------------------------------------------
  411. (defun desktop-remove ()
  412.   "Delete the Desktop file and inactivate the desktop system."
  413.   (interactive)
  414.   (if desktop-dirname
  415.       (let ((filename (concat desktop-dirname desktop-basefilename)))
  416.     (setq desktop-dirname nil)
  417.     (if (file-exists-p filename)
  418.         (delete-file filename)))))
  419. ;; ----------------------------------------------------------------------------
  420. (defun desktop-read ()
  421.   "Read the Desktop file and the files it specifies."
  422.   (interactive)
  423.   (let ((filename))
  424.     (if (file-exists-p (concat "./" desktop-basefilename))
  425.     (setq desktop-dirname (expand-file-name "./"))
  426.       (if (file-exists-p (concat "~/" desktop-basefilename))
  427.       (setq desktop-dirname (expand-file-name "~/"))
  428.     (setq desktop-dirname nil)))
  429.     (if desktop-dirname
  430.     (progn
  431.       (load (concat desktop-dirname desktop-basefilename) t t t)
  432.       (run-hooks 'desktop-delay-hook)
  433.       (message "Desktop loaded."))
  434.       (desktop-clear))))
  435. ;; ----------------------------------------------------------------------------
  436. (defun desktop-load-default ()
  437.   "Load the `default' start-up library manually.  Also inhibit further loading
  438. of it.  Call this from your `.emacs' file to provide correct modes for
  439. autoloaded files."
  440.   (if (not inhibit-default-init)    ; safety check
  441.       (progn
  442.     (load "default" t t)
  443.     (setq inhibit-default-init t))))
  444. ;; ----------------------------------------------------------------------------
  445. ;; Note: the following functions use the dynamic variable binding in Lisp.
  446. ;;
  447. (defun desktop-buffer-info () "Load an info file."
  448.   (if (eq 'Info-mode mam)
  449.       (progn
  450.     (require 'info)
  451.     (Info-find-node (nth 0 misc) (nth 1 misc))
  452.     t)))
  453. ;; ----------------------------------------------------------------------------
  454. (defun desktop-buffer-rmail () "Load an RMAIL file."
  455.   (if (eq 'rmail-mode mam)
  456.       (condition-case error
  457.         (progn (rmail-input fn) t)
  458.       (file-locked
  459.        (kill-buffer (current-buffer))
  460.        'ignored))))
  461. ;; ----------------------------------------------------------------------------
  462. (defun desktop-buffer-mh () "Load a folder in the mh system."
  463.   (if (eq 'mh-folder-mode mam)
  464.       (progn
  465.     (require 'mh-e)
  466.     (mh-find-path)
  467.     (mh-visit-folder bn)
  468.     t)))
  469. ;; ----------------------------------------------------------------------------
  470. (defun desktop-buffer-dired () "Load a directory using dired."
  471.   (if (eq 'dired-mode mam)
  472.       (if (file-directory-p (directory-file-name (car misc)))
  473.       (progn
  474.         (dired (car misc))
  475.         (mapcar (function dired-maybe-insert-subdir) (cdr misc))
  476.         t)
  477.     (message "Directory %s no longer exists." (car misc))
  478.     (sit-for 1)
  479.     'ignored)))
  480. ;; ----------------------------------------------------------------------------
  481. (defun desktop-buffer-file () "Load a file."
  482.   (if fn
  483.       (if (or (file-exists-p fn)
  484.           (and desktop-missing-file-warning
  485.            (y-or-n-p (format
  486.                   "File \"%s\" no longer exists. Re-create? "
  487.                   fn))))
  488.       (progn (find-file fn) t)
  489.     'ignored)))
  490. ;; ----------------------------------------------------------------------------
  491. ;; Create a buffer, load its file, set is mode, ...;  called from Desktop file
  492. ;; only.
  493. (defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc &optional locals)
  494.   (let ((hlist desktop-buffer-handlers)
  495.     (result)
  496.     (handler))
  497.     (while (and (not result) hlist)
  498.       (setq handler (car hlist))
  499.       (setq result (funcall handler))
  500.       (setq hlist (cdr hlist)))
  501.     (if (eq result t)
  502.     (progn
  503.       (if (not (equal (buffer-name) bn))
  504.           (rename-buffer bn))
  505.       (auto-fill-mode (if (nth 0 mim) 1 0))
  506.       (goto-char pt)
  507.       (if (consp mk)
  508.           (progn
  509.         (set-mark (car mk))
  510. ;;        (setq mark-active (car (cdr mk))))
  511.                 (if (car (cdr mk)) (zmacs-activate-region))) ; XEmacs
  512.         (set-mark mk))
  513.       ;; Never override file system if the file really is read-only marked.
  514.       (if ro (setq buffer-read-only ro))
  515.       (while locals
  516.         (let ((this (car locals)))
  517.           (if (consp this)
  518.           ;; an entry of this form `(symbol . value)'
  519.           (progn
  520.             (make-local-variable (car this))
  521.             (set (car this) (cdr this)))
  522.         ;; an entry of the form `symbol'
  523.         (make-local-variable this)
  524.         (makunbound this)))
  525.         (setq locals (cdr locals)))
  526.       ))))
  527.  
  528. ;; Backward compatibility -- update parameters to 205 standards.
  529. (defun desktop-buffer (fn bn mam mim pt mk ro tl fc cfs cr misc)
  530.   (desktop-create-buffer 205 fn bn mam (cdr mim) pt mk ro misc
  531.              (list (cons 'truncate-lines tl)
  532.                    (cons 'fill-column fc)
  533.                    (cons 'case-fold-search cfs)
  534.                    (cons 'case-replace cr)
  535.                    (cons 'overwrite-mode (car mim)))))
  536. ;; ----------------------------------------------------------------------------
  537. (provide 'desktop)
  538.  
  539. ;; desktop.el ends here.
  540.