;;
;; $Id$
;; $Source$
;; $Author$
;;
;; Minor mode for QMINORQ
;; (Replace QMINORQ with the name of your minor mode.)
;;

(provide 'QMINORQ)

(defvar QMINORQ-mode nil)
(make-variable-buffer-local 'QMINORQ-mode)

(defvar QMINORQ-mode-map nil
  "Keymap used for editing MODE files.")
(if (null QMINORQ-mode-map)
    (progn
      (setq QMINORQ-mode-map (make-sparse-keymap))
      (define-key QMINORQ-mode-map "\C-c\C-m" 'QMINORQ-XXX)
      )
  )

(defun QMINORQ-XXX ()
  (interactive)
  (message "QMINORQ MODE")
  (sit-for 1)
  )

;;
;; This variable is used to keep track of whether each of several variables
;; are buffer-local and what their original values were.  This is used
;; so that we set then upon entering the mode and restore them upon
;; leaving the mode.  Each element of this association list's car is
;; a variable name and cdr is a cons cell whose car is whether
;; the variable was local to the buffer and whose cdr was the variable's
;; value upon entry to the mode.
;;
(defvar QMINORQ-save-vars nil)
(make-variable-buffer-local 'QMINORQ-save-vars)

(defun QMINORQ-local-set (var val)
  "Set VAR to VAL keeping information about its previous status."
  (let ((save-info (cdr (assq var QMINORQ-save-vars))))
    (if (null save-info)
	(progn
	  (setq QMINORQ-save-vars
		(cons (cons var (cons nil nil))
		      QMINORQ-save-vars)
		)
	  (setq save-info (cdr (assq var QMINORQ-save-vars)))
	  )
      )
    (setcar save-info (local-variable-p var))
    (setcdr save-info (eval var))
    (make-local-variable var)
    (set var val)
    )
  )

(defun QMINORQ-local-restore (var)
  "Restore VAR to its previous local-variable status and value"
  (let ((save-info (cdr (assq var QMINORQ-save-vars))))
    (if save-info
	(progn
	  (if (null (car save-info))
	      (kill-local-variable var)
	    (set var (cdr save-info))
	    )
	  )
      )
    )
  )

(defun QMINORQ-setup (arg)
  "Set up upon entering or clean up upon leaving QMINORQ mode."
  (if arg
      (progn
	;;
	;; Do any other minor mode initialization here
	;;

	)
    ;; else
    ;;
    ;; Do any other minor cleanup here
    ;;

    )
  )

(defun QMINORQ-mode (arg)
  "Toggle on QMINORQ mode."
  (interactive "P")
  (if (null arg)
      (setq QMINORQ-mode (not QMINORQ-mode))
    (setq QMINORQ-mode t))
  (if QMINORQ-mode
      (progn
	;; Turn on minor mode
	(or (not (boundp 'minor-mode-map-alist))
	    (assq 'QMINORQ-mode minor-mode-map-alist)
	    (setq minor-mode-map-alist
		  (cons (cons 'QMINORQ-mode QMINORQ-mode-map)
			minor-mode-map-alist)
		  )
	    )
	(or (assq 'QMINORQ-mode minor-mode-alist)
	    (setq minor-mode-alist
		  (append '((QMINORQ-mode " QMINORQ"))
			  minor-mode-alist)
		  )
	    )

	(QMINORQ-setup t)
	)
    ;; else
    (mapcar (lambda (elem) (QMINORQ-local-restore (car elem)))
	    QMINORQ-save-vars
	    )
    (setq QMINORQ-save-vars nil)
    (QMINORQ-setup nil)
    )
  (force-mode-line-update)
  )
