;;;* Last edited: Jun 13 01:34 1992 (krab)
;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; TeX-smart-abbrev - smart abbrevation for common math expressions
;; 
;; Copyright (C) 1991 Kresten Krab Thorup (krab@iesd.auc.dk).
;; 
;; 
;; 
;; $Id: tex-math.el,v 5.20 1992/07/22 12:24:38 krab Exp $
;; Author          : Kresten Krab Thorup
;; Created On      : Thu May 30 22:30:22 1991
;; Last Modified By: Kresten Krab Thorup
;; Last Modified On: Sat Jun 13 01:34:51 1992
;; Update Count    : 73
;; 
;; HISTORY
;; 03-Feb-92  (Last Mod: Mon Feb  3 12:18:48 1992) Masayuki Kuwada
;; Supported LaTeX-math-mode-help (M-?).
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; AUC TeX is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing. 
;;
;; Everyone is granted permission to copy, modify and redistribute
;; this file, but only under the conditions described in the
;; document "GNU Emacs copying permission notice".   An exact copy
;; of the document is supposed to have been given to you along with
;; this file so that you can know how you may redistribute it all.
;; It should be in a file named COPYING.  Among other things, the
;; copyright notice and this notice must be preserved on all copies.
;;
;; This software was written as part of the author's official duty as
;; an employee of the University of Aalborg (denmark) and is in the
;; public domain.  You are free to use this software as you wish, but
;; WITHOUT ANY WARRANTY WHATSOEVER.  It would be nice, though if when
;; you use this code, you give due credit to the author.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; TO DO LIST
;;
;; Make the documentation be updated on changes to the list
;;

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customization
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar LaTeX-math-abbrev-prefix "`"
  "*Prefix for LaTeX-math-abbrev.")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'min-map)

(setq TeX-math-keymap (make-keymap))

(defvar TeX-math-mode nil
  "Is LaTeX-math-mode on or off? nonil means on.")

(if (not (assoc 'TeX-math-mode minor-mode-alist))
    (progn
      (setq minor-mode-alist (cons '(TeX-math-mode " Math") minor-mode-alist))
      (make-variable-buffer-local 'TeX-math-mode)))

(defun LaTeX-math-mode ()
  "A minor mode, with easy acces to math symbols. Further documentation will
be present when the mode has been set."
  (interactive)
  (cond (TeX-math-mode
	 (setq TeX-math-mode nil)
	 (unbind-minor-mode 'TeX-math-keymap))
	(t
	 (setq TeX-math-mode t)
	 (minor-set-key LaTeX-math-abbrev-prefix
			'LaTeX-abbrev-math
			'TeX-math-keymap)
	 (minor-set-key "\M-?"
			'LaTeX-math-mode-help
			'TeX-math-keymap)))
  (set-docstring 'LaTeX-math-mode
		 (concat
		  "A minor mode, in which " LaTeX-math-abbrev-prefix "-char is"
		  " bound to a list of standard math symbols.\n "
		  "With prefix (C-u) the symbol is inserted in single dollar signs.\n"
		  " The prefix char may be customized by changing the\n"
		  "value of the string variable LaTeX-math-abbrev-prefix.\n\n"
		  "Current keybindings are:\n\n"
		  
		  (let ((list LaTeX-mathlist)
			(endlist ""))
		    (while (cdr list)
		      (progn
			(setq endlist
			      (concat
			       endlist
			       LaTeX-math-abbrev-prefix
			       "-"
			       (char-to-string (car (car list)))
			       "   \\"
			       (cdr (car list))
			       "\n"))
			(setq list (cdr list))))
		    endlist)))
  ;; force modeline update soon
  (set-buffer-modified-p (buffer-modified-p)))

(defun set-docstring (symbol string)
  "Set the documentation string for symbol SYMBOL to STRING."
  (let* ((symfunc (symbol-function symbol))
	 (doc-cdr (nthcdr 2 symfunc)))
    (condition-case nil 
	  (setcar doc-cdr string)
	(error
	 (setq symfunc (copy-sequence (symbol-function
				       symbol))
	       doc-cdr (nthcdr 2 symfunc))
	 (setcar doc-cdr string)
	 (fset symbol symfunc)))))

(defvar LaTeX-mathlist '(( ?a . "alpha" )
		      ( ?b . "beta" )
		      ( ?d . "delta" )
		      ( ?e . "epsilon" )
		      ( ?f . "phi" )
		      ( ?g . "gamma" )
		      ( ?h . "eta" )
		      ( ?k . "kappa" )
		      ( ?l . "lambda" )
		      ( ?m . "mu" )
		      ( ?n . "nu" )
		      ( ?o . "omega" )
		      ( ?p . "pi" )
		      ( ?q . "theta" )
		      ( ?r . "rho" )
		      ( ?s . "sigma" )
		      ( ?t . "tau" )
		      ( ?u . "upsilon" )
		      ( ?x . "chi" )
		      ( ?y . "psi" )
		      ( ?z . "zeta" )
		      ( ?D . "Delta" )
		      ( ?G . "Gamma" )
		      ( ?Q . "Theta" )
		      ( ?L . "Lambda" )
		      ( ?Y . "Psi" )
		      ( ?P . "Pi" )
		      ( ?S . "Sigma" )
		      ( ?U . "Upsilon" )
		      ( ?V . "Phi" )
		      ( ?O . "Omega" )
		      ( ?\C-f . "rightarrow" )
		      ( ?\C-b . "leftarrow" )
		      ( ?\C-p . "uparrow" )
		      ( ?\C-n . "downarrow" )
		      ( ?< . "leq" )
		      ( ?> . "geq" )
		      ( ?~ . "tilde" )
		      ( ?I . "infty" )
		      ( ?A . "forall" )
		      ( ?E . "exists" )
		      ( ?! . "not" )
		      ( ?i . "in" )
		      ( ?* . "times" )
		      ( ?. . "cdot" )
		      ( ?{ . "subset" )
		      ( ?} . "supset" )
		      ( ?[ . "subseteq" )
		      ( ?] . "supseteq" )
		      ( ?\\ . "backslash" )
		      ( ?/ . "setminus" )
		      ( ?+ . "cup" )
		      ( ?- . "cap" )
		      ( ?( . "langle")
		      ( ?) . "rangle")
		      ( ?\C-e . "exp")
		      ( ?\C-s . "sin")
		      ( ?\C-c . "cos")  
		      ( ?\C-^ . "sup" )
		      ( ?\C-_ . "inf" )
		      ( ?\C-d . "det" )
		      ( ?\C-l . "lim" )
		      ( ?\C-t . "tan" )
		      ( ?^ . "hat")
		      ( ?v . "vee")
		      ( ?0 . "emptyset" ))
  "Alist of key-symbol mappings to use in LaTeX-math-mode.
The user may define new mapping by redefining this variable.

To expand the current mapping by binding \"\\alpha\" to `-a you may insert the followin in your .emacs:

   (setq LaTeX-mathlist (cons '( ?a . \"alpha\")
			      LaTeX-mathlist))")

(defun LaTeX-abbrev-math (p char)
  "Smart bindings of math symbols. See LaTeX-math-mode for documentation..."
  (interactive "p\nc")
  (message "value %d" p)
  (if (= (string-to-char LaTeX-math-abbrev-prefix) char)
      (insert-string LaTeX-math-abbrev-prefix)
    (if (assoc char LaTeX-mathlist)
	(insert (concat (if (= p 1 ) "" "$")
			       TeX-esc (cdr (assoc char LaTeX-mathlist))
			       (if (= p 1 ) "" "$")))
      (error "no such LaTeX-math-abbrev defined"))))


(defun LaTeX-math-mode-help ()
  "Put up window describing LaTeX-math-mode."
  (interactive)
  (describe-function 'LaTeX-math-mode))

