emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A few questions about desktop.el


From: Lars Hansen
Subject: Re: A few questions about desktop.el
Date: Mon, 08 Aug 2005 16:51:11 +0200
User-agent: Debian Thunderbird 1.0.2 (X11/20050331)

Juanma Barranquero wrote:

>I'm having a few problems with highlight-changes-mode, which is a
>not-very-standard minor mode.
>  
>
Yes, the desktop module expects minor modes to follow the conventions.
The only mechanism
to deal with non-conventional minor modes is `desktop-minor-mode-table'
that allows the
minor mode variable and minor mode function to have different names.

To restore highlight changes mode, the function `highlight-changes-mode'
needs to be
called with the value of `highlight-changes-mode' to get the correct
"sub-mode". So what
is needed is something like `desktop-buffer-mode-handlers', but for
minor modes.
I suggest the patch below.
It allows modules defining minor modes to define their own restore
function without
having to change the desktop module.

>The other question is: why does `desktop-save' save `nil' for active
>minor-modes which do appear in `desktop-minor-mode-table' as
>non-restorable?
>  
>
I don't think there is any good reason. The patch below includes your
suggested change
(the second one).

Now I have a question: To allow modules defining major or minor modes to
add a special
handler to `desktop-buffer-mode-handlers' or
`desktop-minor-mode-handlers', the module
in question must be loaded before the major or minor mode function is
called. To do that,
the patch below defines the following function:

   (defun desktop-load-file (function)
     "Load the file where auto loaded FUNCTION is defined."
     (let ((fcell (symbol-function function)))
       (when (and (listp fcell)
                  (eq 'autoload (car fcell)))
         (load (cadr fcell)))))

This function assumes that the mode function is autoloaded. Is this
requirement ok?
Is there a better solution?


*** /home/lh/cvsroot/emacs/lisp/desktop.el      2005-08-08 14:15:26.000000000 
+0200
--- /home/lh/emacs/LH-work/desktop/new.desktop.el       2005-08-08 
16:27:09.416541856 +0200
***************
*** 51,63 ****
  ;; function is added to the `after-init-hook'. This function is
  ;; responsible for loading the desktop when Emacs is started.
  
! ;; Some words on minor modes: Most minor modes are controlled by
! ;; buffer-local variables, which have a standard save / restore
! ;; mechanism.  To handle all minor modes, we take the following
! ;; approach: (1) check whether the variable name from
! ;; `minor-mode-alist' is also a function; and (2) use translation
! ;; table `desktop-minor-mode-table' in the case where the two names
! ;; are not the same.
  
  ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
  ;; in your home directory is used for that.  Saving global default values
--- 51,116 ----
  ;; function is added to the `after-init-hook'. This function is
  ;; responsible for loading the desktop when Emacs is started.
  
! ;; Special handling.
! ;; -----------------
! ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
! ;; 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
! ;; needs a special handler, then place code like
! 
! ;;    (defun foo-restore-desktop-buffer
! ;;    ...
! ;;    (add-to-list 'desktop-buffer-mode-handlers
! ;;                 '(foo-mode . foo-restore-desktop-buffer))
! 
! ;; or
! 
! ;;    (defun bar-desktop-restore
! ;;    ...
! ;;    (add-to-list 'desktop-minor-mode-handlers
! ;;                 '(bar-mode . bar-desktop-restore))
! 
! ;; in the module itself, and make shure that the mode function is
! ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
! ;; `desktop-minor-mode-handlers' for more info.
! 
! ;; Minor modes.
! ;; ------------
! ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
! ;; manual) are handled in the following way:
! ;; When `desktop-save' saves the state of a buffer to the desktop file, it
! ;; saves as `desktop-minor-modes' the list of names of those variables in
! ;; `minor-mode-alist' that have a non-nil value.
! ;; When `desktop-create' restores the buffer, each of the symbols in
! ;; `desktop-minor-modes' is called as function with parameter 1.
! ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
! ;; are used to handle non-conventional minor modes.  `desktop-save' uses
! ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
! ;; functions before writing `desktop-minor-modes'. If a minor mode has a
! ;; variable name that is different form its function name, an entry
! 
! ;;    (NAME RESTORE-FUNCTION)
! 
! ;; should be added to `desktop-minor-mode-table'.  If a minor mode should not
! ;; be restored, RESTORE-FUNCTION should be set to nil.  `desktop-create' uses
! ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
! ;; function different from the usual minor mode function.
! ;; ---------------------------------------------------------------------------
  
  ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
  ;; in your home directory is used for that.  Saving global default values
***************
*** 221,226 ****
--- 274,280 ----
    :type '(repeat string)
    :group 'desktop)
  
+ ;;;###autoload
  (defcustom desktop-locals-to-save
    '(desktop-locals-to-save  ; Itself!  Think it over.
      truncate-lines
***************
*** 230,238 ****
      overwrite-mode
      change-log-default-name
      line-number-mode
!     buffer-file-coding-system)
    "List of local variables to save for each buffer.
! The variables are saved only when they really are local."
    :type '(repeat symbol)
    :group 'desktop)
  (make-variable-buffer-local 'desktop-locals-to-save)
--- 284,299 ----
      overwrite-mode
      change-log-default-name
      line-number-mode
!     column-number-mode
!     size-indication-mode
!     buffer-file-coding-system
!     indent-tabs-mode
!     indicate-buffer-boundaries
!     indicate-empty-lines
!     show-trailing-whitespace)
    "List of local variables to save for each buffer.
! The variables are saved only when they really are local.  Conventional minor
! modes are restored automatically; they should not be listed here."
    :type '(repeat symbol)
    :group 'desktop)
  (make-variable-buffer-local 'desktop-locals-to-save)
***************
*** 301,323 ****
  When file names are returned, they should be formatted using the call
  \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
  
! Later, when `desktop-read' calls a function in `desktop-buffer-mode-handlers'
! to restore the buffer, the auxiliary information is passed as the argument
! DESKTOP-BUFFER-MISC.")
  (make-variable-buffer-local 'desktop-save-buffer)
  (make-obsolete-variable 'desktop-buffer-modes-to-save
                          'desktop-save-buffer "22.1")
  (make-obsolete-variable 'desktop-buffer-misc-functions
                          'desktop-save-buffer "22.1")
  
! (defcustom desktop-buffer-mode-handlers
!   '((dired-mode . dired-restore-desktop-buffer)
!     (rmail-mode . rmail-restore-desktop-buffer)
!     (mh-folder-mode . mh-restore-desktop-buffer)
!     (Info-mode . Info-restore-desktop-buffer))
    "Alist of major mode specific functions to restore a desktop buffer.
! Functions are called by `desktop-read'.  List elements must have the form
! \(MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
  
  Buffers with a major mode not specified here, are restored by the default
  handler `desktop-restore-file-buffer'.
--- 362,384 ----
  When file names are returned, they should be formatted using the call
  \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
  
! Later, when `desktop-read' evaluates the desktop file, auxiliary information
! is passed as the argument DESKTOP-BUFFER-MISC to functions in
! `desktop-buffer-mode-handlers'.")
  (make-variable-buffer-local 'desktop-save-buffer)
  (make-obsolete-variable 'desktop-buffer-modes-to-save
                          'desktop-save-buffer "22.1")
  (make-obsolete-variable 'desktop-buffer-misc-functions
                          'desktop-save-buffer "22.1")
  
! ;;;###autoload
! (defvar desktop-buffer-mode-handlers
!   nil
    "Alist of major mode specific functions to restore a desktop buffer.
! Functions listed are called by `desktop-create-buffer' when `desktop-read'
! evaluates the desktop file.  List elements must have the form
! 
!    (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
  
  Buffers with a major mode not specified here, are restored by the default
  handler `desktop-restore-file-buffer'.
***************
*** 337,345 ****
     desktop-buffer-locals
  
  If a handler returns a buffer, then the saved mode settings
! and variable values for that buffer are copied into it."
!   :type 'alist
!   :group 'desktop)
  
  (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
  (make-obsolete-variable 'desktop-buffer-handlers
--- 398,414 ----
     desktop-buffer-locals
  
  If a handler returns a buffer, then the saved mode settings
! and variable values for that buffer are copied into it.
! 
! Modules that define a major mode that needs a special handler should contain
! code like
! 
!    (defun foo-restore-desktop-buffer
!    ...
!    (add-to-list 'desktop-buffer-mode-handlers
!                 '(foo-mode . foo-restore-desktop-buffer))
! 
! Furthermore the major mode function must be autoloaded.")
  
  (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
  (make-obsolete-variable 'desktop-buffer-handlers
***************
*** 355,364 ****
  called.  RESTORE-FUNCTION nil means don't try to restore the minor mode.
  Only minor modes for which the name of the buffer-local variable
  and the name of the minor mode function are different have to be added to
! this table."
    :type 'sexp
    :group 'desktop)
  
  ;; 
----------------------------------------------------------------------------
  (defvar desktop-dirname nil
    "The directory in which the desktop file should be saved.")
--- 424,479 ----
  called.  RESTORE-FUNCTION nil means don't try to restore the minor mode.
  Only minor modes for which the name of the buffer-local variable
  and the name of the minor mode function are different have to be added to
! this table.  See also `desktop-minor-mode-handlers'."
    :type 'sexp
    :group 'desktop)
  
+ ;;;###autoload
+ (defvar desktop-minor-mode-handlers
+   nil
+   "Alist of functions to restore non-standard minor modes.
+ Functions are called by `desktop-create-buffer' to restore minor modes.
+ List elements must have the form
+ 
+    (MINOR-MODE . RESTORE-FUNCTION).
+ 
+ Minor modes not specified here, are restored by the standard minor mode
+ function.
+ 
+ Handlers are called with argument list
+ 
+    (DESKTOP-BUFFER-LOCALS)
+ 
+ Furthermore, they may use the following variables:
+ 
+    desktop-file-version
+    desktop-buffer-file-name
+    desktop-buffer-name
+    desktop-buffer-major-mode
+    desktop-buffer-minor-modes
+    desktop-buffer-point
+    desktop-buffer-mark
+    desktop-buffer-read-only
+    desktop-buffer-misc
+ 
+ When a handler is called, the buffer has been created and the major mode has
+ been set, but local variables listed in desktop-buffer-locals has not yet been
+ created and set.
+ 
+ Modules that define a minor mode that needs a special handler should contain
+ code like
+ 
+    (defun foo-desktop-restore
+    ...
+    (add-to-list 'desktop-minor-mode-handlers
+                 '(foo-mode . foo-desktop-restore))
+ 
+ Furthermore the minor mode function must be autoloaded.
+ 
+ See also `desktop-minor-mode-table'.")
+ 
+ (put 'desktop-minor-mode-handlers 'risky-local-variable t)
+ 
  ;; 
----------------------------------------------------------------------------
  (defvar desktop-dirname nil
    "The directory in which the desktop file should be saved.")
***************
*** 622,633 ****
                            (and
                              (boundp minor-mode)
                              (symbol-value minor-mode)
!                             (let ((special (assq minor-mode 
desktop-minor-mode-table)))
!                               (when (or special (functionp minor-mode))
!                                 (setq ret
!                                   (cons
!                                     (if special (cadr special) minor-mode)
!                                     ret))))))
                          (mapcar #'car minor-mode-alist))
                        ret)
                      (point)
--- 737,746 ----
                            (and
                              (boundp minor-mode)
                              (symbol-value minor-mode)
!                             (let* ((special (assq minor-mode 
desktop-minor-mode-table))
!                                    (value (cond (special (cadr special))
!                                                 ((functionp minor-mode) 
minor-mode))))
!                               (when value (add-to-list 'ret value)))))
                          (mapcar #'car minor-mode-alist))
                        ret)
                      (point)
***************
*** 685,691 ****
                    (insert ")\n\n")))
              info)
        (setq default-directory dirname)
-       (when (file-exists-p filename) (delete-file filename))
        (let ((coding-system-for-write 'emacs-mule))
          (write-region (point-min) (point-max) filename nil 'nomessage))))
    (setq desktop-dirname dirname))
--- 798,803 ----
***************
*** 850,862 ****
            buf)
        nil)))
  
  ;; 
----------------------------------------------------------------------------
  ;; Create a buffer, load its file, set its mode, ...;
  ;; called from Desktop file only.
  
! (eval-when-compile ; Just to silence the byte compiler
!    (defvar desktop-first-buffer) ;; Dynamically bound in `desktop-read'
! )
  
  (defun desktop-create-buffer
    (desktop-file-version
--- 962,981 ----
            buf)
        nil)))
  
+ (defun desktop-load-file (function)
+   "Load the file where auto loaded FUNCTION is defined."
+   (let ((fcell (symbol-function function)))
+     (when (and (listp fcell)
+                (eq 'autoload (car fcell)))
+       (load (cadr fcell)))))
+ 
  ;; 
----------------------------------------------------------------------------
  ;; Create a buffer, load its file, set its mode, ...;
  ;; called from Desktop file only.
  
! ;; Just to silence the byte compiler.
! (eval-when-compile
!   (defvar desktop-first-buffer)) ; Dynamically bound in `desktop-read'
  
  (defun desktop-create-buffer
    (desktop-file-version
***************
*** 877,882 ****
--- 996,1003 ----
    ;; To make desktop files with relative file names possible, we cannot
    ;; allow `default-directory' to change. Therefore we save current buffer.
    (save-current-buffer
+     ;; Give major mode module a chance to add a handler.
+     (desktop-load-file desktop-buffer-major-mode)
      (let ((buffer-list (buffer-list))
            (result
             (condition-case err
***************
*** 914,922 ****
                 (auto-fill-mode 0))
                (t
                 (mapc #'(lambda (minor-mode)
!                        (when (functionp minor-mode) (funcall minor-mode 1)))
                     desktop-buffer-minor-modes)))
!         ;; Even though point and mark are non-nil when written by 
`desktop-save'
          ;; they may be modified by handlers wanting to set point or mark 
themselves.
          (when desktop-buffer-point
            (goto-char
--- 1035,1049 ----
                 (auto-fill-mode 0))
                (t
                 (mapc #'(lambda (minor-mode)
!                          ;; Give minor mode module a chance to add a handler.
!                          (desktop-load-file minor-mode)
!                          (let ((handler (cdr (assq minor-mode 
desktop-minor-mode-handlers))))
!                            (if handler
!                                (funcall handler desktop-buffer-locals)
!                              (when (functionp minor-mode)
!                                (funcall minor-mode 1)))))
                     desktop-buffer-minor-modes)))
!         ;; Even though point and mark are non-nil when written by 
`desktop-save',
          ;; they may be modified by handlers wanting to set point or mark 
themselves.
          (when desktop-buffer-point
            (goto-char
*** /home/lh/cvsroot/emacs/lisp/hilit-chg.el    2005-08-08 14:15:26.000000000 
+0200
--- /home/lh/emacs/LH-work/desktop/new.hilit-chg.el     2005-08-08 
16:27:55.003611568 +0200
***************
*** 673,678 ****
--- 673,679 ----
        (if new-highlight-changes-mode
            ;; mode is turned on -- but may be passive
            (progn
+             (add-to-list 'desktop-locals-to-save 'highlight-changes-mode)
              (hilit-chg-set new-highlight-changes-mode)
              (or was-on
                  ;; run highlight-changes-enable-hook once
***************
*** 1154,1159 ****
--- 1155,1170 ----
                 )))
     (buffer-list)))
  
+ ;;;; Desktop support.
+ 
+ ;; Called by `desktop-create-buffer' to restore `highlight-changes-mode'.
+ (defun hilit-chg-desktop-restore (desktop-buffer-locals)
+   (highlight-changes-mode
+    (or (cdr (assq 'highlight-changes-mode desktop-buffer-locals)) 1)))
+ 
+ (add-to-list 'desktop-minor-mode-handlers
+              '(highlight-changes-mode . hilit-chg-desktop-restore))
+ 
  ;; ===================== debug ==================
  ;; For debug & test use:
  ;;
*** /home/lh/cvsroot/emacs/lisp/dired.el        2005-08-08 14:15:26.000000000 
+0200
--- /home/lh/emacs/LH-work/desktop/new.dired.el 2005-08-08 16:28:28.824470016 
+0200
***************
*** 1469,1474 ****
--- 1469,1476 ----
  ;; Dired mode is suitable only for specially formatted data.
  (put 'dired-mode 'mode-class 'special)
  
+ ;; Autoload cookie needed by desktop.el
+ ;;;###autoload
  (defun dired-mode (&optional dirname switches)
    "\
  Mode for \"editing\" directory listings.
***************
*** 3270,3276 ****
           (function (lambda (f) (desktop-file-name (car f) desktop-dirname)))
           dired-subdir-alist)))))
  
- ;;;###autoload
  (defun dired-restore-desktop-buffer (desktop-buffer-file-name
                                       desktop-buffer-name
                                       desktop-buffer-misc)
--- 3272,3277 ----
***************
*** 3291,3296 ****
--- 3292,3300 ----
        (when desktop-missing-file-warning (sit-for 1))
        nil)))
  
+ (add-to-list 'desktop-buffer-mode-handlers
+              '(dired-mode . dired-restore-desktop-buffer))
+ 
  
  (if (eq system-type 'vax-vms)
      (load "dired-vms"))
*** /home/lh/cvsroot/emacs/lisp/info.el 2005-08-08 14:15:26.000000000 +0200
--- /home/lh/emacs/LH-work/desktop/new.info.el  2005-08-08 16:29:18.743881112 
+0200
***************
*** 3199,3204 ****
--- 3199,3206 ----
  
  (defvar tool-bar-map)
  
+ ;; Autoload cookie needed by desktop.el
+ ;;;###autoload
  (defun Info-mode ()
    "Info mode provides commands for browsing through the Info documentation 
tree.
  Documentation in Info is divided into \"nodes\", each of which discusses
***************
*** 4168,4174 ****
    (if (not (member Info-current-file '("apropos" "history" "toc")))
        (list Info-current-file Info-current-node)))
  
- ;;;###autoload
  (defun Info-restore-desktop-buffer (desktop-buffer-file-name
                                      desktop-buffer-name
                                      desktop-buffer-misc)
--- 4170,4175 ----
***************
*** 4182,4187 ****
--- 4183,4191 ----
      (Info-find-node first second)
      (current-buffer))))
  
+ (add-to-list 'desktop-buffer-mode-handlers
+              '(Info-mode . Info-restore-desktop-buffer))
+ 
  (provide 'info)
  
  ;; arch-tag: f2480fe2-2139-40c1-a49b-6314991164ac
*** /home/lh/cvsroot/emacs/lisp/mail/rmail.el   2005-08-08 14:15:19.000000000 
+0200
--- /home/lh/emacs/LH-work/desktop/new.rmail.el 2005-08-08 16:30:28.313304952 
+0200
***************
*** 4058,4064 ****
  
  ;;;;  Desktop support
  
- ;;;###autoload
  (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
                                       desktop-buffer-name
                                       desktop-buffer-misc)
--- 4058,4063 ----
***************
*** 4073,4078 ****
--- 4072,4080 ----
        (kill-buffer (current-buffer))
        nil)))
  
+ (add-to-list 'desktop-buffer-mode-handlers
+              '(rmail-mode . rmail-restore-desktop-buffer))
+ 
  (provide 'rmail)
  
  ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
*** /home/lh/cvsroot/emacs/lisp/mh-e/mh-e.el    2005-07-12 15:25:36.000000000 
+0200
--- /home/lh/emacs/LH-work/desktop/new.mh-e.el  2005-08-06 07:26:37.000000000 
+0200
***************
*** 1507,1512 ****
--- 1507,1514 ----
             (not (member 'mh-folder-mode which-func-modes)))
    (push 'mh-folder-mode which-func-modes))
  
+ ;; Autoload cookie needed by desktop.el
+ ;;;###autoload
  (define-derived-mode mh-folder-mode fundamental-mode "MH-Folder"
    "Major MH-E mode for \"editing\" an MH folder scan 
listing.\\<mh-folder-mode-map>
  
***************
*** 1653,1659 ****
      (set (make-local-variable (car pairs)) (car (cdr pairs)))
      (setq pairs (cdr (cdr pairs)))))
  
- ;;;###autoload
  (defun mh-restore-desktop-buffer (desktop-buffer-file-name
                                    desktop-buffer-name
                                    desktop-buffer-misc)
--- 1655,1660 ----
***************
*** 1666,1671 ****
--- 1667,1675 ----
    (mh-visit-folder desktop-buffer-name)
    (current-buffer))
  
+ (add-to-list 'desktop-buffer-mode-handlers
+              '(mh-folder-mode . mh-restore-desktop-buffer))
+ 
  (defun mh-scan-folder (folder range &optional dont-exec-pending)
    "Scan the FOLDER over the RANGE.
  If the optional argument DONT-EXEC-PENDING is non-nil then pending deletes and

reply via email to

[Prev in Thread] Current Thread [Next in Thread]