emacs-devel
[Top][All Lists]
Advanced

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

Re: org-mode and mode hooks.


From: Luc Teirlinck
Subject: Re: org-mode and mode hooks.
Date: Fri, 27 May 2005 12:17:16 -0500 (CDT)

Here are my proposed changes to easy-mmode.el.  Unlike my previous
proposed patch, they require some cooperation from the individual 
minor modes, but this seemed necessary to catch (and attempt to work
around) all possible ways to mess up the major mode for which the
minor mode is set up.  The only two involved modes I could find using
grepping were font-lock-mode and cwarn mode.  The patches below adapt
both.

Michael could check whether or not after my patches (the one to
cwarn.el is irrelevant for him), just disabling and enabling font-lock
is sufficient for him.  If not, then I have the impression that I
should not even try to correct this, as maybe certain font-lock-face
properties might be mode independent.

When testing this patch, do not rely on stuff like `C-h v
font-lock-set-defaults', because this is currently buggy.
Do `M-: font-lock-set-defaults', or similar, instead.

To test the error message, one could use something like:

(defun foo-mode ()
  (interactive)
  (outline-mode)
  (setq major-mode 'foo-mode)
  (setq mode-name "FOO"))

M-x foo-mode

I can install the patches below if desired.

===File ~/easy-mmode-diff===================================
*** easy-mmode.el       22 May 2005 16:50:33 -0500      1.63
--- easy-mmode.el       27 May 2005 11:00:47 -0500      
***************
*** 271,284 ****
  TURN-ON is a function that will be called with no args in every buffer
    and that should try to turn MODE on if applicable for that buffer.
  KEYS is a list of CL-style keyword arguments:
! :group to specify the custom group."
    (let* ((global-mode-name (symbol-name global-mode))
         (pretty-name (easy-mmode-pretty-mode-name mode))
         (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
         (group nil)
         (extra-args nil)
         (buffers (intern (concat global-mode-name "-buffers")))
!        (cmmh (intern (concat global-mode-name "-cmmh"))))
  
      ;; Check keys.
      (while (keywordp (car keys))
--- 271,294 ----
  TURN-ON is a function that will be called with no args in every buffer
    and that should try to turn MODE on if applicable for that buffer.
  KEYS is a list of CL-style keyword arguments:
! :group to specify the custom group.
! 
! If the minor mode's set-up depends on the major mode, the function that
! does the initial set-up should record the value of the variable `major-mode'
! in the variable MODE-stored-mode.  This is used to catch bugs where MODE is
! set up for the wrong mode. `define-global-minor-mode' provides a defvar
! for MODE-stored-hook, with initial value nil, and makes it automatically
! buffer-local.  If MODE's set-up is independent of the major mode,
! the value of the variable should stay nil."
    (let* ((global-mode-name (symbol-name global-mode))
         (pretty-name (easy-mmode-pretty-mode-name mode))
         (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
         (group nil)
         (extra-args nil)
         (buffers (intern (concat global-mode-name "-buffers")))
!        (buffers-check (intern (concat global-mode-name "-check-buffers")))
!        (cmmh (intern (concat global-mode-name "-cmmh")))
!        (stored-mode (intern (concat (symbol-name mode) "-stored-mode"))))
  
      ;; Check keys.
      (while (keywordp (car keys))
***************
*** 294,299 ****
--- 304,311 ----
                                "-mode\\'" "" (symbol-name mode))))))
  
      `(progn
+        (defvar ,stored-mode nil)
+        (make-variable-buffer-local ',stored-mode)
         ;; The actual global minor-mode
         (define-minor-mode ,global-mode
         ,(format "Toggle %s in every buffer.
***************
*** 307,314 ****
--- 319,328 ----
         (if ,global-mode
             (progn
               (add-hook 'after-change-major-mode-hook ',buffers)
+              (add-hook 'find-file-hook ',buffers-check)
               (add-hook 'change-major-mode-hook ',cmmh))
           (remove-hook 'after-change-major-mode-hook ',buffers)
+          (remove-hook 'find-file-hook ',buffers-check)
           (remove-hook 'change-major-mode-hook ',cmmh))
  
         ;; Go through existing buffers.
***************
*** 325,341 ****
  
         ;; The function that calls TURN-ON in each buffer.
         (defun ,buffers ()
!        (remove-hook 'post-command-hook ',buffers)
         (while ,buffers
           (let ((buf (pop ,buffers)))
             (when (buffer-live-p buf)
!              (with-current-buffer buf (,turn-on))))))
!        (put ',buffers 'definition-name ',global-mode)
  
         ;; The function that catches kill-all-local-variables.
         (defun ,cmmh ()
         (add-to-list ',buffers (current-buffer))
!        (add-hook 'post-command-hook ',buffers))
         (put ',cmmh 'definition-name ',global-mode))))
  
  ;;;
--- 339,372 ----
  
         ;; The function that calls TURN-ON in each buffer.
         (defun ,buffers ()
!        (dolist (buf ,buffers)
!          (when (buffer-live-p buf)
!            (with-current-buffer buf
!              (unless ,mode
!                (,turn-on))))))
!        (put ',buffers 'definition-name ',global-mode)
! 
!        (defun ,buffers-check ()
!        (remove-hook 'post-command-hook ',buffers-check)
         (while ,buffers
           (let ((buf (pop ,buffers)))
             (when (buffer-live-p buf)
!              (with-current-buffer buf
!                (if (or (not ,stored-mode) (eq ,stored-mode major-mode))
!                    (,turn-on)
!                  (let ((old-stored-mode ,stored-mode))
!                    (,mode -1)
!                    (,turn-on)
!                    (message
!                     "%s was set up for wrong major mode: %s.
! We tried to correct.  This is a bug.  Please report it."
!                     (symbol-name ',mode) old-stored-mode))
!                  (sit-for 1)))))))
  
         ;; The function that catches kill-all-local-variables.
         (defun ,cmmh ()
         (add-to-list ',buffers (current-buffer))
!        (add-hook 'post-command-hook ',buffers-check))
         (put ',cmmh 'definition-name ',global-mode))))
  
  ;;;
============================================================

===File ~/font-lock-diff====================================
*** font-lock.el        22 May 2005 19:04:51 -0500      1.258
--- font-lock.el        26 May 2005 19:17:56 -0500      
***************
*** 1569,1583 ****
        (t
         (car keywords))))
  
! (defvar font-lock-set-defaults nil)   ; Whether we have set up defaults.
  
  (defun font-lock-set-defaults ()
    "Set fontification defaults appropriately for this mode.
  Sets various variables using `font-lock-defaults' (or, if nil, using
  `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
    ;; Set fontification defaults iff not previously set.
!   (unless font-lock-set-defaults
!     (set (make-local-variable 'font-lock-set-defaults) t)
      (make-local-variable 'font-lock-fontified)
      (make-local-variable 'font-lock-multiline)
      (let* ((defaults (or font-lock-defaults
--- 1569,1585 ----
        (t
         (car keywords))))
  
! ;; The mode for which font-lock was initialized, or nil if none.
! (defvar font-lock-set-defaults)
! (defvaralias 'font-lock-set-defaults 'font-lock-mode-stored-mode)
  
  (defun font-lock-set-defaults ()
    "Set fontification defaults appropriately for this mode.
  Sets various variables using `font-lock-defaults' (or, if nil, using
  `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
    ;; Set fontification defaults iff not previously set.
!   (unless (eq font-lock-set-defaults major-mode)
!     (setq font-lock-set-defaults major-mode)
      (make-local-variable 'font-lock-fontified)
      (make-local-variable 'font-lock-multiline)
      (let* ((defaults (or font-lock-defaults
============================================================

===File ~/cwarn-diff========================================
*** cwarn.el    04 Apr 2005 07:03:09 -0500      1.9
--- cwarn.el    26 May 2005 19:19:59 -0500      
***************
*** 234,242 ****
--- 234,244 ----
      (back-to-indentation)
      (eq (char-after) ?#)))
  
+ (defvar cwarn-mode-stored-mode)
  (defun cwarn-font-lock-keywords (addp)
    "Install/Remove keywords into current buffer.
  If ADDP is non-nil, install else remove."
+   (when addp (setq cwarn-mode-stored-mode major-mode))
    (dolist (pair cwarn-font-lock-feature-keywords-alist)
      (let ((feature (car pair))
          (keywords (cdr pair)))
============================================================




reply via email to

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