emacs-devel
[Top][All Lists]
Advanced

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

Re: enriched-mode and switching major modes.


From: Luc Teirlinck
Subject: Re: enriched-mode and switching major modes.
Date: Fri, 10 Sep 2004 21:14:36 -0500 (CDT)

Richard Stallman wrote:

   I think you are on the right track, except for the detail that
   post-command-hook does the job too late.  enriched-mode ought to get
   reenabled as soon as the new major mode is set up; it should not wait
   until the command finishes.

   One way to make this work is by adding a feature to run-mode-hooks.
   enriched-mode's change-major-mode-hook could then do someting 
   to tell run-mode-hooks to reenable enriched-mode.

   Want to try that?

My previous patch somehow forgot that the command could switch
buffers.  The new patch below fixes that.  I believe that we still
have to use post-command-hook to avoid possibly very annoying and
confusing inconsistencies that could otherwise arise in the case of
error or quit (as well as for major modes that do not use
`run-mode-hooks').  With the new function `with-local-quit', there can
actually be several quits and hence several inconsistencies as a
result of executing a single command.  The patches below take all of
this into account.

Note that `post-command-hook' will only have some non-trivial work to
do in case of really exceptional situations, like errors or quits at
the wrong moment.  In normal circumstances, all it does is a dolist
over an empty list.

Of course, if we would make Enriched mode into a major mode, then none
of this would be necessary.

===File ~/subr.el-diff======================================
*** subr.el     08 Sep 2004 11:29:29 -0500      1.407
--- subr.el     10 Sep 2004 14:39:21 -0500      
***************
*** 1937,1942 ****
--- 1937,1946 ----
  (make-variable-buffer-local 'delayed-mode-hooks)
  (put 'delay-mode-hooks 'permanent-local t)
  
+ (defvar after-change-major-mode-hook nil
+   "Mode independent hook run after changing major modes.
+ This is run just before the mode dependent hooks.")
+ 
  (defun run-mode-hooks (&rest hooks)
    "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
  Execution is delayed if `delay-mode-hooks' is non-nil.
***************
*** 1948,1953 ****
--- 1952,1958 ----
      ;; Normal case, just run the hook as before plus any delayed hooks.
      (setq hooks (nconc (nreverse delayed-mode-hooks) hooks))
      (setq delayed-mode-hooks nil)
+     (run-hooks 'after-change-major-mode-hook)
      (apply 'run-hooks hooks)))
  
  (defmacro delay-mode-hooks (&rest body)
============================================================

===File ~/enriched.el-diff==================================
*** enriched.el 02 Jul 2004 21:39:16 -0500      1.4
--- enriched.el 10 Sep 2004 20:25:21 -0500      
***************
*** 1,6 ****
  ;;; enriched.el --- read and save files in text/enriched format
  
! ;; Copyright (c) 1994, 1995, 1996, 2002 Free Software Foundation, Inc.
  
  ;; Author: Boris Goldowsky <address@hidden>
  ;; Keywords: wp, faces
--- 1,6 ----
  ;;; enriched.el --- read and save files in text/enriched format
  
! ;; Copyright (c) 1994, 1995, 1996, 2002, 2004 Free Software Foundation, Inc.
  
  ;; Author: Boris Goldowsky <address@hidden>
  ;; Keywords: wp, faces
***************
*** 141,147 ****
  
  ;;; Internal variables
  
- 
  (defcustom enriched-mode-hook nil
    "Hook run after entering/leaving Enriched mode.
  If you set variables in this hook, you should arrange for them to be restored
--- 141,146 ----
***************
*** 155,160 ****
--- 154,170 ----
  The value is a list of \(VAR VALUE VAR VALUE...).")
  (make-variable-buffer-local 'enriched-old-bindings)
  
+ ;; A non-nil value indicates that Enriched mode was temporarily disabled
+ ;; by `enriched-before-change-major-mode' while switching major modes.
+ (defvar enriched-change-major-mode-flag nil)
+ (make-local-variable 'enriched-change-major-mode-flag)
+ (put 'enriched-change-major-mode-flag 'permanent-local t)
+ 
+ ;; List of buffers in which Enriched mode was disabled by
+ ;; `enriched-before-change-major-mode' and was not properly re-enabled.
+ ;; `post-command-hook' checks this.
+ (defvar enriched-marked-buffers nil)
+ 
  ;;;
  ;;; Define the mode
  ;;;
***************
*** 197,203 ****
         (make-local-variable 'default-text-properties)
         (setq indent-line-function 'indent-to-left-margin ;WHY??  -sm
               buffer-display-table  enriched-display-table)
!        (use-hard-newlines 1 nil)
         (let ((sticky (plist-get default-text-properties 'front-sticky))
               (p enriched-par-props))
           (dolist (x p)
--- 207,213 ----
         (make-local-variable 'default-text-properties)
         (setq indent-line-function 'indent-to-left-margin ;WHY??  -sm
               buffer-display-table  enriched-display-table)
!        (use-hard-newlines 1 (if enriched-change-major-mode-flag 'never nil))
         (let ((sticky (plist-get default-text-properties 'front-sticky))
               (p enriched-par-props))
           (dolist (x p)
***************
*** 205,211 ****
           (if sticky
               (setq default-text-properties
                     (plist-put default-text-properties
!                               'front-sticky sticky)))))))
  
  ;;;
  ;;; Keybindings
--- 215,254 ----
           (if sticky
               (setq default-text-properties
                     (plist-put default-text-properties
!                               'front-sticky sticky))))
!        (add-hook 'change-major-mode-hook
!                  'enriched-before-change-major-mode nil t)))
!   (setq enriched-change-major-mode-flag nil)
!   (setq enriched-marked-buffers
!       (delete (current-buffer) enriched-marked-buffers)))
! 
! ;; Enriched mode sets several local variables that are killed when
! ;; changing major modes.  They need to be reset.  Also, when the major
! ;; mode changes, `enriched-old-bindings' needs to be correctly
! ;; updated.  Therefore, we disable Enriched mode before changing the
! ;; major mode and enable it back afterward.
! (defun enriched-before-change-major-mode ()
!   (when enriched-mode
!     (let ((inhibit-quit t))
!       (enriched-mode 0)
!       (setq enriched-change-major-mode-flag t)
!       (add-to-list 'enriched-marked-buffers (current-buffer)))))
! 
! (defun enriched-after-change-major-mode ()
!   (when enriched-change-major-mode-flag (enriched-mode 1)))
! 
! (add-hook 'after-change-major-mode-hook 'enriched-after-change-major-mode)
! 
! ;; Have `post-command-hook' check for buffers left in an inconsistent
! ;; state as a result of an error, quit or "local quit" (that is, by
! ;; using `with-local-quit').
! (defun enriched-check-mode-change-buffers ()
!   (dolist (buf enriched-marked-buffers)
!     (with-current-buffer buf
!       (when enriched-change-major-mode-flag (enriched-mode 1))))
!   (setq enriched-marked-buffers nil))
! 
! (add-hook 'post-command-hook 'enriched-check-mode-change-buffers)
  
  ;;;
  ;;; Keybindings
============================================================




reply via email to

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