Commit 876daebc authored by Luc Teirlinck's avatar Luc Teirlinck
Browse files

(define-global-minor-mode): Make it keep track of which major mode it

enabled the minor mode for.  Use find-file-hook again.  Update docstring.
parent 2f7e1f5a
...@@ -271,14 +271,26 @@ With zero or negative ARG turn mode off. ...@@ -271,14 +271,26 @@ With zero or negative ARG turn mode off.
TURN-ON is a function that will be called with no args in every buffer 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. and that should try to turn MODE on if applicable for that buffer.
KEYS is a list of CL-style keyword arguments: KEYS is a list of CL-style keyword arguments:
:group to specify the custom group." :group to specify the custom group.
If MODE's set-up depends on the major mode in effect when it was
enabled, then disabling and reenabling MODE should make MODE work
correctly with the current major mode. This is important to
prevent problems with derived modes, that is, major modes that
call another major mode in their body."
(let* ((global-mode-name (symbol-name global-mode)) (let* ((global-mode-name (symbol-name global-mode))
(pretty-name (easy-mmode-pretty-mode-name mode)) (pretty-name (easy-mmode-pretty-mode-name mode))
(pretty-global-name (easy-mmode-pretty-mode-name global-mode)) (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
(group nil) (group nil)
(extra-args nil) (extra-args nil)
(buffers (intern (concat global-mode-name "-buffers"))) (MODE-buffers (intern (concat global-mode-name "-buffers")))
(cmmh (intern (concat global-mode-name "-cmmh")))) (MODE-enable-in-buffers
(intern (concat global-mode-name "-enable-in-buffers")))
(MODE-check-buffers
(intern (concat global-mode-name "-check-buffers")))
(MODE-cmhh (intern (concat global-mode-name "-cmhh")))
(MODE-major-mode (intern (concat (symbol-name mode) "-major-mode"))))
;; Check keys. ;; Check keys.
(while (keywordp (car keys)) (while (keywordp (car keys))
...@@ -294,6 +306,8 @@ KEYS is a list of CL-style keyword arguments: ...@@ -294,6 +306,8 @@ KEYS is a list of CL-style keyword arguments:
"-mode\\'" "" (symbol-name mode)))))) "-mode\\'" "" (symbol-name mode))))))
`(progn `(progn
(defvar ,MODE-major-mode nil)
(make-variable-buffer-local ',MODE-major-mode)
;; The actual global minor-mode ;; The actual global minor-mode
(define-minor-mode ,global-mode (define-minor-mode ,global-mode
,(format "Toggle %s in every buffer. ,(format "Toggle %s in every buffer.
...@@ -306,10 +320,13 @@ in which `%s' turns it on." ...@@ -306,10 +320,13 @@ in which `%s' turns it on."
;; Setup hook to handle future mode changes and new buffers. ;; Setup hook to handle future mode changes and new buffers.
(if ,global-mode (if ,global-mode
(progn (progn
(add-hook 'after-change-major-mode-hook ',buffers) (add-hook 'after-change-major-mode-hook
(add-hook 'change-major-mode-hook ',cmmh)) ',MODE-enable-in-buffers)
(remove-hook 'after-change-major-mode-hook ',buffers) (add-hook 'find-file-hook ',MODE-check-buffers)
(remove-hook 'change-major-mode-hook ',cmmh)) (add-hook 'change-major-mode-hook ',MODE-cmhh))
(remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
(remove-hook 'find-file-hook ',MODE-check-buffers)
(remove-hook 'change-major-mode-hook ',MODE-cmhh))
;; Go through existing buffers. ;; Go through existing buffers.
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
...@@ -321,22 +338,33 @@ in which `%s' turns it on." ...@@ -321,22 +338,33 @@ in which `%s' turns it on."
:autoload-end :autoload-end
;; List of buffers left to process. ;; List of buffers left to process.
(defvar ,buffers nil) (defvar ,MODE-buffers nil)
;; The function that calls TURN-ON in each buffer. ;; The function that calls TURN-ON in each buffer.
(defun ,buffers () (defun ,MODE-enable-in-buffers ()
(remove-hook 'post-command-hook ',buffers) (dolist (buf ,MODE-buffers)
(while ,buffers (when (buffer-live-p buf)
(let ((buf (pop ,buffers))) (with-current-buffer buf
(when (buffer-live-p buf) (if ,mode
(with-current-buffer buf (,turn-on)))))) (unless (eq ,MODE-major-mode major-mode)
(put ',buffers 'definition-name ',global-mode) (,mode -1)
(,turn-on)
(setq ,MODE-major-mode major-mode))
(,turn-on)
(setq ,MODE-major-mode major-mode))))))
(put ',MODE-enable-in-buffers 'definition-name ',global-mode)
(defun ,MODE-check-buffers ()
(,MODE-enable-in-buffers)
(setq ,MODE-buffers nil)
(remove-hook 'post-command-hook ',MODE-check-buffers))
(put ',MODE-check-buffers 'definition-name ',global-mode)
;; The function that catches kill-all-local-variables. ;; The function that catches kill-all-local-variables.
(defun ,cmmh () (defun ,MODE-cmhh ()
(add-to-list ',buffers (current-buffer)) (add-to-list ',MODE-buffers (current-buffer))
(add-hook 'post-command-hook ',buffers)) (add-hook 'post-command-hook ',MODE-check-buffers))
(put ',cmmh 'definition-name ',global-mode)))) (put ',MODE-cmhh 'definition-name ',global-mode))))
;;; ;;;
;;; easy-mmode-defmap ;;; easy-mmode-defmap
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment