aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Teirlinck2005-06-04 22:13:57 +0000
committerLuc Teirlinck2005-06-04 22:13:57 +0000
commit876daebc85940a3c6ff11c7077e1d74c87d87705 (patch)
treef52c199e838e2812b7c3348c3b00ec6ef55ee442
parent2f7e1f5aa99b55a8dcd0df4fe9c7e46964c44a9d (diff)
downloademacs-876daebc85940a3c6ff11c7077e1d74c87d87705.tar.gz
emacs-876daebc85940a3c6ff11c7077e1d74c87d87705.zip
(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.
-rw-r--r--lisp/emacs-lisp/easy-mmode.el66
1 files changed, 47 insertions, 19 deletions
diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el
index 188dc172e07..bb0fa666217 100644
--- a/lisp/emacs-lisp/easy-mmode.el
+++ b/lisp/emacs-lisp/easy-mmode.el
@@ -271,14 +271,26 @@ With zero or negative ARG turn mode off.
271TURN-ON is a function that will be called with no args in every buffer 271TURN-ON is a function that will be called with no args in every buffer
272 and that should try to turn MODE on if applicable for that buffer. 272 and that should try to turn MODE on if applicable for that buffer.
273KEYS is a list of CL-style keyword arguments: 273KEYS is a list of CL-style keyword arguments:
274:group to specify the custom group." 274:group to specify the custom group.
275
276If MODE's set-up depends on the major mode in effect when it was
277enabled, then disabling and reenabling MODE should make MODE work
278correctly with the current major mode. This is important to
279prevent problems with derived modes, that is, major modes that
280call another major mode in their body."
281
275 (let* ((global-mode-name (symbol-name global-mode)) 282 (let* ((global-mode-name (symbol-name global-mode))
276 (pretty-name (easy-mmode-pretty-mode-name mode)) 283 (pretty-name (easy-mmode-pretty-mode-name mode))
277 (pretty-global-name (easy-mmode-pretty-mode-name global-mode)) 284 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
278 (group nil) 285 (group nil)
279 (extra-args nil) 286 (extra-args nil)
280 (buffers (intern (concat global-mode-name "-buffers"))) 287 (MODE-buffers (intern (concat global-mode-name "-buffers")))
281 (cmmh (intern (concat global-mode-name "-cmmh")))) 288 (MODE-enable-in-buffers
289 (intern (concat global-mode-name "-enable-in-buffers")))
290 (MODE-check-buffers
291 (intern (concat global-mode-name "-check-buffers")))
292 (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
293 (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode"))))
282 294
283 ;; Check keys. 295 ;; Check keys.
284 (while (keywordp (car keys)) 296 (while (keywordp (car keys))
@@ -294,6 +306,8 @@ KEYS is a list of CL-style keyword arguments:
294 "-mode\\'" "" (symbol-name mode)))))) 306 "-mode\\'" "" (symbol-name mode))))))
295 307
296 `(progn 308 `(progn
309 (defvar ,MODE-major-mode nil)
310 (make-variable-buffer-local ',MODE-major-mode)
297 ;; The actual global minor-mode 311 ;; The actual global minor-mode
298 (define-minor-mode ,global-mode 312 (define-minor-mode ,global-mode
299 ,(format "Toggle %s in every buffer. 313 ,(format "Toggle %s in every buffer.
@@ -306,10 +320,13 @@ in which `%s' turns it on."
306 ;; Setup hook to handle future mode changes and new buffers. 320 ;; Setup hook to handle future mode changes and new buffers.
307 (if ,global-mode 321 (if ,global-mode
308 (progn 322 (progn
309 (add-hook 'after-change-major-mode-hook ',buffers) 323 (add-hook 'after-change-major-mode-hook
310 (add-hook 'change-major-mode-hook ',cmmh)) 324 ',MODE-enable-in-buffers)
311 (remove-hook 'after-change-major-mode-hook ',buffers) 325 (add-hook 'find-file-hook ',MODE-check-buffers)
312 (remove-hook 'change-major-mode-hook ',cmmh)) 326 (add-hook 'change-major-mode-hook ',MODE-cmhh))
327 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
328 (remove-hook 'find-file-hook ',MODE-check-buffers)
329 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
313 330
314 ;; Go through existing buffers. 331 ;; Go through existing buffers.
315 (dolist (buf (buffer-list)) 332 (dolist (buf (buffer-list))
@@ -321,22 +338,33 @@ in which `%s' turns it on."
321 :autoload-end 338 :autoload-end
322 339
323 ;; List of buffers left to process. 340 ;; List of buffers left to process.
324 (defvar ,buffers nil) 341 (defvar ,MODE-buffers nil)
325 342
326 ;; The function that calls TURN-ON in each buffer. 343 ;; The function that calls TURN-ON in each buffer.
327 (defun ,buffers () 344 (defun ,MODE-enable-in-buffers ()
328 (remove-hook 'post-command-hook ',buffers) 345 (dolist (buf ,MODE-buffers)
329 (while ,buffers 346 (when (buffer-live-p buf)
330 (let ((buf (pop ,buffers))) 347 (with-current-buffer buf
331 (when (buffer-live-p buf) 348 (if ,mode
332 (with-current-buffer buf (,turn-on)))))) 349 (unless (eq ,MODE-major-mode major-mode)
333 (put ',buffers 'definition-name ',global-mode) 350 (,mode -1)
351 (,turn-on)
352 (setq ,MODE-major-mode major-mode))
353 (,turn-on)
354 (setq ,MODE-major-mode major-mode))))))
355 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
356
357 (defun ,MODE-check-buffers ()
358 (,MODE-enable-in-buffers)
359 (setq ,MODE-buffers nil)
360 (remove-hook 'post-command-hook ',MODE-check-buffers))
361 (put ',MODE-check-buffers 'definition-name ',global-mode)
334 362
335 ;; The function that catches kill-all-local-variables. 363 ;; The function that catches kill-all-local-variables.
336 (defun ,cmmh () 364 (defun ,MODE-cmhh ()
337 (add-to-list ',buffers (current-buffer)) 365 (add-to-list ',MODE-buffers (current-buffer))
338 (add-hook 'post-command-hook ',buffers)) 366 (add-hook 'post-command-hook ',MODE-check-buffers))
339 (put ',cmmh 'definition-name ',global-mode)))) 367 (put ',MODE-cmhh 'definition-name ',global-mode))))
340 368
341;;; 369;;;
342;;; easy-mmode-defmap 370;;; easy-mmode-defmap