comparison lisp/emacs-lisp/easy-mmode.el @ 63023:cc9dd228e2e6

(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.
author Luc Teirlinck <teirllm@auburn.edu>
date Sat, 04 Jun 2005 22:13:57 +0000
parents a33f6ed6e52e
children c531a8daedc6 01137c1fdbe9
comparison
equal deleted inserted replaced
63022:5e1ac4f79779 63023:cc9dd228e2e6
269 (defmacro define-global-minor-mode (global-mode mode turn-on &rest keys) 269 (defmacro define-global-minor-mode (global-mode mode turn-on &rest keys)
270 "Make GLOBAL-MODE out of the buffer-local minor MODE. 270 "Make GLOBAL-MODE out of the buffer-local minor MODE.
271 TURN-ON is a function that will be called with no args in every buffer 271 TURN-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.
273 KEYS is a list of CL-style keyword arguments: 273 KEYS is a list of CL-style keyword arguments:
274 :group to specify the custom group." 274 :group to specify the custom group.
275
276 If MODE's set-up depends on the major mode in effect when it was
277 enabled, then disabling and reenabling MODE should make MODE work
278 correctly with the current major mode. This is important to
279 prevent problems with derived modes, that is, major modes that
280 call 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))
285 (case (pop keys) 297 (case (pop keys)
286 (:extra-args (setq extra-args (pop keys))) 298 (:extra-args (setq extra-args (pop keys)))
292 (setq group 304 (setq group
293 `(:group ',(intern (replace-regexp-in-string 305 `(:group ',(intern (replace-regexp-in-string
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.
300 With prefix ARG, turn %s on if and only if ARG is positive. 314 With prefix ARG, turn %s on if and only if ARG is positive.
301 %s is actually not turned on in every buffer but only in those 315 %s is actually not turned on in every buffer but only in those
304 :global t :extra-args ,extra-args ,@group 318 :global t :extra-args ,extra-args ,@group
305 319
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))
316 (with-current-buffer buf 333 (with-current-buffer buf
317 (if ,global-mode (,turn-on) (when ,mode (,mode -1)))))) 334 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
319 ;; Autoloading define-global-minor-mode autoloads everything 336 ;; Autoloading define-global-minor-mode autoloads everything
320 ;; up-to-here. 337 ;; up-to-here.
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
343 ;;; 371 ;;;
344 372