;;; eev-compose.el -- typing accents and mathematical chars using a compose key.

;; Copyright (C) 2001,2002,2003,2004,2005,2008 Free Software Foundation, Inc.
;;
;; This file is part of GNU eev.
;;
;; GNU eev is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; GNU eev is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU eev; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;; Author:     Eduardo Ochs <eduardoochs@gmail.com>
;; Maintainer: Eduardo Ochs <eduardoochs@gmail.com>
;; Version:    2008jul07
;; Keywords:   i18n, mathematical chars, glyphs
;;
;; Latest version: <http://angg.twu.net/eev-current/eev-compose.el>
;;       htmlized: <http://angg.twu.net/eev-current/eev-compose.el.html>

;;; Comment:
;;
;; This file implements a very primitive way to insert accented
;; characters and glyphs - including mathematical glyphs, like the
;; ones defined in "eev-math-glyphs.el". Here's how it works: by
;; default, when `eev-mode' is on, the key `M-,' is bound to
;; `eev-compose-two-keys'; when we type, for example, `M-, ^ o', we
;; get an "\364".
;;
;; Here's how to type the main glyphs that I use in my e-script files:
;;
;;   "*":   C-q C-o
;;   "\253":   M-, < <
;;   "\273":   M-, > >
;;
;; The plist that says how pairs of characters are to be composed by
;; `M-,' is stored in the variable `eev-composes-all'. That variable
;; is not set directly; rather, the function `eev-composes-update'
;; sets it to:
;;   
;;   (append eev-composes-localmath     ; default: nil
;;           eev-composes-globalmath    ; default: nil
;;           eev-composes-accents
;;           eev-composes-otheriso)
;;
;; When the function `eev-math-glyphs-edrx' from "eev-math-glyphs.el"
;; sets compose pairs for mathematical characters it sets just
;; `eev-composes-localmath' and `eev-composes-globalmath', not
;; `eev-composes-accents' or `eev-composes-otheriso'.
;;
;; See: <http://angg.twu.net/eev-current/eev-glyphs.el.html>,
;;      <http://angg.twu.net/eev-current/eev-math-glyphs.el.html>,
;;      <http://angg.twu.net/eev-article.html#glyphs>.
;;      <http://angg.twu.net/eev-article.html#compose-pairs>.
;;      (find-elnode "Reading One Event")
;;      (find-es "emacs" "key_name")

;; Known bugs: this only works reliably in unibyte/raw-text buffers;
;; as the lisp function invoked to insert the new character is
;; `insert' the "composed character" is always inserted, even in
;; `overwrite-mode' and `picture-mode'; and this does not always work
;; for inserting special characters in the minibuffer.


(defun eev-compose-pair (pair) (interactive "sTwo-character code: ")
"Convert PAIR (a two-character string) to a single character and insert it.
The conversion is done by looking up PAIR in the the plist `eev-composes-all'.
If an entry for PAIR is not found, raise an error."
  (let ((sublist (member pair eev-composes-all)))
    (if sublist (insert (nth 1 sublist))
      (error "Pair \"%s\" not in `eev-composes-all'" pair))))

(defun eev-compose-two-keys ()
"Read two characters with `read-event' and insert their \"composition\".
For example: `\\[eev-compose-two-keys] ^ A' inserts an \"A\" with a hat.
The list of composable pairs is stored in the variable `eev-composes-all'.
See also `eev-composes-update'."
  (interactive)
  (eev-compose-pair (format "%c%c"
                           (read-event "Compose key 1: " t)
                           (read-event "Compose key 2: " t))))

(defvar eev-composes-accents '(
   "`A" ?\300   "`E" ?\310   "`I" ?\314   "`O" ?\322   "`U" ?\331
   "`a" ?\340   "`e" ?\350   "`i" ?\354   "`o" ?\362   "`u" ?\371
   "'A" ?\301   "'E" ?\311   "'I" ?\315   "'O" ?\323   "'U" ?\332
   "'a" ?\341   "'e" ?\351   "'i" ?\355   "'o" ?\363   "'u" ?\372
   "^A" ?\302   "^E" ?\312   "^I" ?\316   "^O" ?\324   "^U" ?\333
   "^a" ?\342   "^e" ?\352   "^i" ?\356   "^o" ?\364   "^u" ?\373
   "~A" ?\303                       "~O" ?\325
   "~a" ?\343                       "~o" ?\365
  "\"A" ?\304  "\"E" ?\313  "\"I" ?\317  "\"O" ?\326  "\"U" ?\334
  "\"a" ?\344  "\"e" ?\353  "\"i" ?\357  "\"o" ?\366  "\"u" ?\374
   "'C" ?\307   "CC" ?\307   "~N" ?\321
   "'c" ?\347   "cc" ?\347   "~n" ?\361
))
(defvar eev-composes-otheriso '(
   "_a" ?\252   "_o" ?\272   "AE" ?\306   "ae" ?\346   "ss" ?\337
   "!!" ?\241   "??" ?\277   "SS" ?\247   "<<" ?\253   ">>" ?\273
   "00" ?\260   "11" ?\271   "22" ?\262   "33" ?\263
   "14" ?\274   "12" ?\275   "34" ?\276
   "+-" ?\261   ":-" ?\367   "cd" ?\267   "xx" ?\327   "nt" ?\254
))
(defvar eev-composes-globalmath nil)
(defvar eev-composes-localmath nil)
(defvar eev-composes-all nil)

(defun eev-composes-update ()
  "Update the variable `eev-composes-all'. See the source code."
  (setq eev-composes-all
        (append eev-composes-localmath eev-composes-globalmath
                eev-composes-accents   eev-composes-otheriso)))

(eev-composes-update)

(provide 'eev-compose)


;; Local Variables:
;; coding:            raw-text-unix
;; ee-anchor-format:  "\253%s\273"
;; ee-comment-prefix: ";;"
;; End: