;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; ltx-dead.el -- dead keys for LaTeX format accented characters. 
;; 
;; $Id: ltx-dead.el,v 5.1 1992/03/31 03:19:13 krab Exp $
;; 
;; Author          : Per Abrahamsen
;; Created On      : Tue Mar 31 05:15:39 1992
;; Last Modified By: Kresten Krab Thorup
;; Last Modified On: Tue Mar 31 05:17:29 1992
;; 
;; Update Count    : 3
;; Buffer Position : 1369
;; Minor Modes     : ()
;; 
;; $Log: ltx-dead.el,v $
; Revision 5.1  1992/03/31  03:19:13  krab
; Tja...
;
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;
;; This module implements dead keys for LaTeX format accented characters.
;;
;; Here is one way to support accents.  The following file implements
;; accents as ``dead keys'' in emacs.  To use it, you must set the
;; variable LaTeX-dead-keys to a list of accents you are interested in.
;; 
;; For example:
;;	 (setq LaTeX-dead-keys '(?` ?\" ?^))
;; will define `, ", and ^ as dead keys.
;; 
;; You must then activate the dead keys with M-x LaTeX-dead-mode.
;; 
;; When you press a dead key, emacs will wait for you to enter a letter.
;; The letter will then be inserted with the appropriate accent.  If you
;; press the dead key twice, the accent itself will be inserted.
;; 
;; This mode should be useful if you use relatively many accented
;; letters.  If you only use few accents, you will probably not like it.
;; In that case, use C-c m instead.
;;

(require 'min-map)

(make-variable-buffer-local 'LaTeX-dead-mode)
(setq-default LaTeX-dead-mode nil)

(setq minor-mode-alist (cons '(LaTeX-dead-mode " Dead") 
			     minor-mode-alist))

(defvar LaTeX-dead-keys '(?' ?` ?\" ?^ ?~)
  "*List of characters used as dead keys.")

(defun LaTeX-dead-mode (arg) "\
Toggle LaTeX-dead-mode.
With arg, turn LaTeX-dead-mode on iff arg is positive.

Thsi mode allows users to conveniently enter characters with accents,
as found in some European languages.

The supported accents are defined by LaTeX-dead-keys.  Modify that
variable to change the list of dead keys. (You most likely want to
do that).

When a dead key is pressed, the system waits for the user to enter
another character.  This character will be accented.  To enter the
accent itself, press the dead key twice.  For example:

Key  |	Text
-----+--------
' e  |  \'{e}
' '  |  '
' i  |  \'{\i}

As shown by the example, this mode knows that i and j must be treated
specially.

This mode allow users to get the pseudo danish chars directly from the
keyboard, and the corresponding english chars by pressing a key twice."

  (interactive "P")
  (if (or (and (null arg) LaTeX-dead-mode)
	  (<= (prefix-numeric-value arg) 0))
      (if LaTeX-dead-mode
	  (progn			;Turn it off
	    (unbind-minor-mode 'LaTeX-dead-mode)
	    (setq LaTeX-dead-mode nil)))
    (if LaTeX-dead-mode		;Turn it on
	()
      (mapcar 'LaTeX-dead-bind LaTeX-dead-keys)
      (setq LaTeX-dead-mode t)))
  (set-buffer-modified-p (buffer-modified-p)))

(defun LaTeX-dead-bind (key)
  "Mark KEY as dead."
  (minor-set-key (char-to-string key) 'LaTeX-dead-function 'LaTeX-dead-mode))

(defun LaTeX-dead-function (char)
  "Insert CHAR, and insert it with an accent if it is a letter.  If it
is the accent itself, insert it.  Otherwise, insert the accent
character followed by the other character."
  (interactive "c")
  (cond ((eq (aref (this-command-keys) 0) char)
	 (if (eq char ?\")
	     (TeX-insert-quote nil)
	   (insert char)))
	((eq (char-syntax char) ?w)
	 (insert TeX-esc)
	 (self-insert-command 1)
	 (insert TeX-grop)
	 (if (or (eq char ?i) (eq char ?j))
	     (insert TeX-esc))
	 (insert char TeX-grcl))
	(t
	 (if (eq (aref (this-command-keys) 0) ?\")
	     (progn (TeX-insert-quote nil)
		    (insert char))
	   (insert (this-command-keys))))))
