Debug message fix
[sxemacs] / lisp / x-compose.el
1 ;;; x-compose.el --- Compose-key processing in SXEmacs
2
3 ;; Copyright (C) 1992, 1993, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Jamie Zawinski <jwz@jwz.org>
6 ;; Maintainer: SXEmacs Development Team
7 ;; Rewritten by Martin Buchholz far too many times.
8 ;;
9 ;; Changed: 11 Jun 1997 by Heiko Muenkel <muenkel@tnt.uni-hannover.de>
10 ;;      The degree sign couldn't be inserted with the old version.
11 ;; Keywords: i18n
12
13 ;; This file is part of SXEmacs.
14
15 ;; SXEmacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; SXEmacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Synched up with: Not in FSF.
29
30 ;;; Commentary:
31
32 ;; created by jwz, 14-jun-92.
33 ;;; changed by Jan Vroonhof, July 1997: Use function-key-map instead
34 ;;;                                     of global map.
35 ;;;                                     Preliminary support for
36 ;;;                                     XFree86 deadkeys
37
38 ;; This file implements DEC-, OpenWindows-, and HP-compatible "Compose"
39 ;; processing for XEmacs.
40
41 ;; If you are running a version of X which already does compose processing,
42 ;; then you don't need this file.  But the MIT R4 and R5 distributions don't
43 ;; do compose processing, so you may want to fake it by using this code.
44
45 ;; The basic idea is that there are several ways to generate keysyms which
46 ;; do not have keys devoted to them on your keyboard.
47
48 ;; The first method is by using "dead" keys.  A dead key is a key which,
49 ;; when typed, does not insert a character.  Instead it modifies the
50 ;; following character typed.  So if you typed "dead-tilde" followed by "A",
51 ;; then "A-tilde" would be inserted.  Of course, this requires you to modify
52 ;; your keyboard to include a "dead-tilde" key on it somewhere.
53
54 ;; The second method is by using a "Compose" key.  With a Compose key, you
55 ;; would type "Compose" then "tilde" then "A" to insert "A-tilde".
56
57 ;; There are a small number of dead keys: acute, grave, cedilla, diaeresis,
58 ;; circumflex, tilde, and ring.  There are a larger number of accented and
59 ;; other characters accessible via the Compose key, so both are useful.
60
61 ;; To use this code, you will need to have a Compose key on your keyboard.
62 ;; The default configuration of most X keyboards doesn't contain one.  You
63 ;; can, for example, turn the right "Meta" key into a "Compose" key with
64 ;; this command:
65
66 ;;    xmodmap -e "remove mod1 = Meta_R" -e "keysym Meta_R = Multi_key"
67
68 ;; Multi-key is the name that X (and emacs) know the "Compose" key by.
69 ;; The "remove..." command is necessary because the "Compose" key must not
70 ;; have any modifier bits associated with it.  This exact command may not
71 ;; work, depending on what system and keyboard you are using.  If it
72 ;; doesn't, you'll have to read the man page for xmodmap.  You might want
73 ;; to get the "xkeycaps" program from
74 ;; <URL:http://www.jwz.org/xkeycaps/>,
75 ;; which is a graphical front end to xmodmap
76 ;; that hides xmodmap's arcane syntax from you.
77
78 ;; If for some reason you don't want to have a dedicated compose key on your
79 ;; keyboard, you can use some other key as the prefix.  For example, to make
80 ;; "Meta-Shift-C" act as a compose key (so that "M-C , c" would insert the
81 ;; character "ccedilla") you could do
82
83 ;;    (global-set-key "\M-C" compose-map)
84
85 ;; I believe the bindings encoded in this file are the same as those used
86 ;; by OpenWindows versions 2 and 3, and DEC VT320 terminals.  Please let me
87 ;; know if you think otherwise.
88
89 ;; Much thanks to Justin Bur <justin@crim.ca> for helping me understand how
90 ;; this stuff is supposed to work.
91
92 ;; You also might want to consider getting Justin's patch for the MIT Xlib
93 ;; that implements compose processing in the library.  This will enable
94 ;; compose processing in applications other than emacs as well.  You can
95 ;; get it from export.lcs.mit.edu in contrib/compose.tar.Z.
96
97 ;; This code has one feature that a more "builtin" Compose mechanism could
98 ;; not have: at any point you can type C-h to get a list of the possible
99 ;; completions of what you have typed so far.
100
101 ;;; Code:
102
103 (require 'x-iso8859-1)
104
105 (macrolet
106     ((define-compose-map (keymap-symbol)
107        `(progn
108           (defconst ,keymap-symbol (make-sparse-keymap ',keymap-symbol))
109           ;; Required to tell XEmacs the keymaps were actually autoloaded.
110           ;; #### Make this unnecessary!
111           (fset ',keymap-symbol ,keymap-symbol))))
112
113   (define-compose-map compose-map)
114   (define-compose-map compose-acute-map)
115   (define-compose-map compose-grave-map)
116   (define-compose-map compose-cedilla-map)
117   (define-compose-map compose-diaeresis-map)
118   (define-compose-map compose-circumflex-map)
119   (define-compose-map compose-tilde-map)
120   (define-compose-map compose-ring-map))
121
122 (define-key compose-map 'acute      compose-acute-map)
123 (define-key compose-map 'grave      compose-grave-map)
124 (define-key compose-map 'cedilla    compose-cedilla-map)
125 (define-key compose-map 'diaeresis  compose-diaeresis-map)
126 (define-key compose-map 'circumflex compose-circumflex-map)
127 (define-key compose-map 'tilde      compose-tilde-map)
128 (define-key compose-map 'degree     compose-ring-map)
129
130 ;;(define-key function-key-map [multi-key] compose-map)
131
132 ;; The following is necessary, because one can't rebind [degree]
133 ;; and use it to insert the degree sign!
134 ;;(defun compose-insert-degree ()
135 ;;  "Inserts a degree sign."
136 ;;  (interactive)
137 ;;  (insert ?\260))
138
139 (define-key compose-map [acute]         compose-acute-map)
140 (define-key compose-map [?']            compose-acute-map)
141 (define-key compose-map [grave]         compose-grave-map)
142 (define-key compose-map [?`]            compose-grave-map)
143 (define-key compose-map [cedilla]       compose-cedilla-map)
144 (define-key compose-map [?,]            compose-cedilla-map)
145 (define-key compose-map [diaeresis]     compose-diaeresis-map)
146 (define-key compose-map [?\"]           compose-diaeresis-map)
147 (define-key compose-map [circumflex]    compose-circumflex-map)
148 (define-key compose-map [?^]            compose-circumflex-map)
149 (define-key compose-map [tilde]         compose-tilde-map)
150 (define-key compose-map [~]             compose-tilde-map)
151 (define-key compose-map [degree]        compose-ring-map)
152 (define-key compose-map [?*]            compose-ring-map)
153
154 \f
155 ;;; The contents of the "dead key" maps.  These are shared by the
156 ;;; compose-map.
157
158 (define-key compose-acute-map [space]   "'")
159 (define-key compose-acute-map [?']      [acute])
160 (define-key compose-acute-map [?A]      [Aacute])
161 (define-key compose-acute-map [E]       [Eacute])
162 (define-key compose-acute-map [I]       [Iacute])
163 (define-key compose-acute-map [O]       [Oacute])
164 (define-key compose-acute-map [U]       [Uacute])
165 (define-key compose-acute-map [Y]       [Yacute])
166 (define-key compose-acute-map [a]       [aacute])
167 (define-key compose-acute-map [e]       [eacute])
168 (define-key compose-acute-map [i]       [iacute])
169 (define-key compose-acute-map [o]       [oacute])
170 (define-key compose-acute-map [u]       [uacute])
171 (define-key compose-acute-map [y]       [yacute])
172
173 (define-key compose-grave-map [space]   "`")
174 (define-key compose-grave-map [?`]      [grave])
175 (define-key compose-grave-map [A]       [Agrave])
176 (define-key compose-grave-map [E]       [Egrave])
177 (define-key compose-grave-map [I]       [Igrave])
178 (define-key compose-grave-map [O]       [Ograve])
179 (define-key compose-grave-map [U]       [Ugrave])
180 (define-key compose-grave-map [a]       [agrave])
181 (define-key compose-grave-map [e]       [egrave])
182 (define-key compose-grave-map [i]       [igrave])
183 (define-key compose-grave-map [o]       [ograve])
184 (define-key compose-grave-map [u]       [ugrave])
185
186 (define-key compose-cedilla-map [space] ",")
187 (define-key compose-cedilla-map [?,]    [cedilla])
188 (define-key compose-cedilla-map [C]     [Ccedilla])
189 (define-key compose-cedilla-map [c]     [ccedilla])
190
191 (define-key compose-diaeresis-map [space] [diaeresis])
192 (define-key compose-diaeresis-map [?\"] [diaeresis])
193 (define-key compose-diaeresis-map [A]   [Adiaeresis])
194 (define-key compose-diaeresis-map [E]   [Ediaeresis])
195 (define-key compose-diaeresis-map [I]   [Idiaeresis])
196 (define-key compose-diaeresis-map [O]   [Odiaeresis])
197 (define-key compose-diaeresis-map [U]   [Udiaeresis])
198 (define-key compose-diaeresis-map [a]   [adiaeresis])
199 (define-key compose-diaeresis-map [e]   [ediaeresis])
200 (define-key compose-diaeresis-map [i]   [idiaeresis])
201 (define-key compose-diaeresis-map [o]   [odiaeresis])
202 (define-key compose-diaeresis-map [u]   [udiaeresis])
203 (define-key compose-diaeresis-map [y]   [ydiaeresis])
204
205 (define-key compose-circumflex-map [space] "^")
206 (define-key compose-circumflex-map [?/] "|")
207 (define-key compose-circumflex-map [?!] [brokenbar])
208 (define-key compose-circumflex-map [?-] [macron])
209 (define-key compose-circumflex-map [?_] [macron])
210 (define-key compose-circumflex-map [?0] [degree])
211 (define-key compose-circumflex-map [?1] [onesuperior])
212 (define-key compose-circumflex-map [?2] [twosuperior])
213 (define-key compose-circumflex-map [?3] [threesuperior])
214 (define-key compose-circumflex-map [?.] [periodcentered])
215 (define-key compose-circumflex-map [A]  [Acircumflex])
216 (define-key compose-circumflex-map [E]  [Ecircumflex])
217 (define-key compose-circumflex-map [I]  [Icircumflex])
218 (define-key compose-circumflex-map [O]  [Ocircumflex])
219 (define-key compose-circumflex-map [U]  [Ucircumflex])
220 (define-key compose-circumflex-map [a]  [acircumflex])
221 (define-key compose-circumflex-map [e]  [ecircumflex])
222 (define-key compose-circumflex-map [i]  [icircumflex])
223 (define-key compose-circumflex-map [o]  [ocircumflex])
224 (define-key compose-circumflex-map [u]  [ucircumflex])
225
226 (define-key compose-tilde-map [space]   "~")
227 (define-key compose-tilde-map [A]       [Atilde])
228 (define-key compose-tilde-map [N]       [Ntilde])
229 (define-key compose-tilde-map [O]       [Otilde])
230 (define-key compose-tilde-map [a]       [atilde])
231 (define-key compose-tilde-map [n]       [ntilde])
232 (define-key compose-tilde-map [o]       [otilde])
233
234 (define-key compose-ring-map [space]    [degree])
235 (define-key compose-ring-map [A]        [Aring])
236 (define-key compose-ring-map [a]        [aring])
237
238 \f
239 ;;; The rest of the compose-map.  These are the composed characters
240 ;;; that are not accessible via "dead" keys.
241
242 (define-key compose-map " '"    "'")
243 (define-key compose-map " ^"    "^")
244 (define-key compose-map " `"    "`")
245 (define-key compose-map " ~"    "~")
246 (define-key compose-map "  "    [nobreakspace])
247 (define-key compose-map " \""   [diaeresis])
248 (define-key compose-map " :"    [diaeresis])
249 (define-key compose-map " *"    [degree])
250
251 (define-key compose-map "!!"    [exclamdown])
252 (define-key compose-map "!^"    [brokenbar])
253 (define-key compose-map "!S"    [section])
254 (define-key compose-map "!s"    [section])
255 (define-key compose-map "!P"    [paragraph])
256 (define-key compose-map "!p"    [paragraph])
257
258 (define-key compose-map "(("    "[")
259 (define-key compose-map "(-"    "{")
260
261 (define-key compose-map "))"    "]")
262 (define-key compose-map ")-"    "}")
263
264 (define-key compose-map "++"    "#")
265 (define-key compose-map "+-"    [plusminus])
266
267 (define-key compose-map "-("    "{")
268 (define-key compose-map "-)"    "}")
269 (define-key compose-map "--"    "-")
270 (define-key compose-map "-L"    [sterling])
271 (define-key compose-map "-l"    [sterling])
272 (define-key compose-map "-Y"    [yen])
273 (define-key compose-map "-y"    [yen])
274 (define-key compose-map "-,"    [notsign])
275 (define-key compose-map "-|"    [notsign])
276 (define-key compose-map "-^"    [macron])
277 (define-key compose-map "-+"    [plusminus])
278 (define-key compose-map "-:"    [division])
279 (define-key compose-map "-D"    [ETH])
280 (define-key compose-map "-d"    [eth])
281 (define-key compose-map "-a"    [ordfeminine])
282
283 (define-key compose-map ".^"    [periodcentered])
284
285 (define-key compose-map "//"    "\\")
286 (define-key compose-map "/<"    "\\")
287 (define-key compose-map "/^"    "|")
288 (define-key compose-map "/C"    [cent])
289 (define-key compose-map "/c"    [cent])
290 (define-key compose-map "/U"    [mu])
291 (define-key compose-map "/u"    [mu])
292 (define-key compose-map "/O"    [Ooblique])
293 (define-key compose-map "/o"    [oslash])
294
295 (define-key compose-map "0X"    [currency])
296 (define-key compose-map "0x"    [currency])
297 (define-key compose-map "0S"    [section])
298 (define-key compose-map "0s"    [section])
299 (define-key compose-map "0C"    [copyright])
300 (define-key compose-map "0c"    [copyright])
301 (define-key compose-map "0R"    [registered])
302 (define-key compose-map "0r"    [registered])
303 (define-key compose-map "0^"    [degree])
304
305 (define-key compose-map "1^"    [onesuperior])
306 (define-key compose-map "14"    [onequarter])
307 (define-key compose-map "12"    [onehalf])
308
309 (define-key compose-map "2^"    [twosuperior])
310
311 (define-key compose-map "3^"    [threesuperior])
312 (define-key compose-map "34"    [threequarters])
313
314 (define-key compose-map ":-"    [division])
315
316 (define-key compose-map "</"    "\\")
317 (define-key compose-map "<<"    [guillemotleft])
318
319 (define-key compose-map "=L"    [sterling])
320 (define-key compose-map "=l"    [sterling])
321 (define-key compose-map "=Y"    [yen])
322 (define-key compose-map "=y"    [yen])
323
324 (define-key compose-map ">>"    [guillemotright])
325
326 (define-key compose-map "??"    [questiondown])
327
328 (define-key compose-map "AA"    "@")
329 (define-key compose-map "Aa"    "@")
330 (define-key compose-map "A_"    [ordfeminine])
331 (define-key compose-map "A`"    [Agrave])
332 (define-key compose-map "A'"    [Aacute])
333 (define-key compose-map "A^"    [Acircumflex])
334 (define-key compose-map "A~"    [Atilde])
335 (define-key compose-map "A\""   [Adiaeresis])
336 (define-key compose-map "A*"    [Aring])
337 (define-key compose-map "AE"    [AE])
338
339 (define-key compose-map "C/"    [cent])
340 (define-key compose-map "C|"    [cent])
341 (define-key compose-map "C0"    [copyright])
342 (define-key compose-map "CO"    [copyright])
343 (define-key compose-map "Co"    [copyright])
344 (define-key compose-map "C,"    [Ccedilla])
345
346 (define-key compose-map "D-"    [ETH])
347
348 (define-key compose-map "E`"    [Egrave])
349 (define-key compose-map "E'"    [Eacute])
350 (define-key compose-map "E^"    [Ecircumflex])
351 (define-key compose-map "E\""   [Ediaeresis])
352
353 (define-key compose-map "I`"    [Igrave])
354 (define-key compose-map "I'"    [Iacute])
355 (define-key compose-map "I^"    [Icircumflex])
356 (define-key compose-map "I\""   [Idiaeresis])
357
358 (define-key compose-map "L-"    [sterling])
359 (define-key compose-map "L="    [sterling])
360
361 (define-key compose-map "N~"    [Ntilde])
362
363 (define-key compose-map "OX"    [currency])
364 (define-key compose-map "Ox"    [currency])
365 (define-key compose-map "OS"    [section])
366 (define-key compose-map "Os"    [section])
367 (define-key compose-map "OC"    [copyright])
368 (define-key compose-map "Oc"    [copyright])
369 (define-key compose-map "OR"    [registered])
370 (define-key compose-map "Or"    [registered])
371 (define-key compose-map "O_"    [masculine])
372 (define-key compose-map "O`"    [Ograve])
373 (define-key compose-map "O'"    [Oacute])
374 (define-key compose-map "O^"    [Ocircumflex])
375 (define-key compose-map "O~"    [Otilde])
376 (define-key compose-map "O\""   [Odiaeresis])
377 (define-key compose-map "O/"    [Ooblique])
378
379 (define-key compose-map "P!"    [paragraph])
380
381 (define-key compose-map "R0"    [registered])
382 (define-key compose-map "RO"    [registered])
383 (define-key compose-map "Ro"    [registered])
384
385 (define-key compose-map "S!"    [section])
386 (define-key compose-map "S0"    [section])
387 (define-key compose-map "SO"    [section])
388 (define-key compose-map "So"    [section])
389 (define-key compose-map "SS"    [ssharp])
390
391 (define-key compose-map "TH"    [THORN])
392
393 (define-key compose-map "U`"    [Ugrave])
394 (define-key compose-map "U'"    [Uacute])
395 (define-key compose-map "U^"    [Ucircumflex])
396 (define-key compose-map "U\""   [Udiaeresis])
397
398 (define-key compose-map "X0"    [currency])
399 (define-key compose-map "XO"    [currency])
400 (define-key compose-map "Xo"    [currency])
401
402 (define-key compose-map "Y-"    [yen])
403 (define-key compose-map "Y="    [yen])
404 (define-key compose-map "Y'"    [Yacute])
405
406 (define-key compose-map "_A"    [ordfeminine])
407 (define-key compose-map "_a"    [ordfeminine])
408 (define-key compose-map "_^"    [macron])
409 (define-key compose-map "_O"    [masculine])
410 (define-key compose-map "_o"    [masculine])
411
412 (define-key compose-map "aA"    "@")
413 (define-key compose-map "aa"    "@")
414 (define-key compose-map "a_"    [ordfeminine])
415 (define-key compose-map "a-"    [ordfeminine])
416 (define-key compose-map "a`"    [agrave])
417 (define-key compose-map "a'"    [aacute])
418 (define-key compose-map "a^"    [acircumflex])
419 (define-key compose-map "a~"    [atilde])
420 (define-key compose-map "a\""   [adiaeresis])
421 (define-key compose-map "a*"    [aring])
422 (define-key compose-map "ae"    [ae])
423
424 (define-key compose-map "c/"    [cent])
425 (define-key compose-map "c|"    [cent])
426 (define-key compose-map "c0"    [copyright])
427 (define-key compose-map "cO"    [copyright])
428 (define-key compose-map "co"    [copyright])
429 (define-key compose-map "c,"    [ccedilla])
430
431 (define-key compose-map "d-"    [eth])
432
433 (define-key compose-map "e`"    [egrave])
434 (define-key compose-map "e'"    [eacute])
435 (define-key compose-map "e^"    [ecircumflex])
436 (define-key compose-map "e\""   [ediaeresis])
437
438 (define-key compose-map "i`"    [igrave])
439 (define-key compose-map "i'"    [iacute])
440 (define-key compose-map "i^"    [icircumflex])
441 (define-key compose-map "i\""   [idiaeresis])
442 (define-key compose-map "i:"    [idiaeresis])
443
444 (define-key compose-map "l-"    [sterling])
445 (define-key compose-map "l="    [sterling])
446
447 (define-key compose-map "n~"    [ntilde])
448
449 (define-key compose-map "oX"    [currency])
450 (define-key compose-map "ox"    [currency])
451 (define-key compose-map "oC"    [copyright])
452 (define-key compose-map "oc"    [copyright])
453 (define-key compose-map "oR"    [registered])
454 (define-key compose-map "or"    [registered])
455 (define-key compose-map "oS"    [section])
456 (define-key compose-map "os"    [section])
457 (define-key compose-map "o_"    [masculine])
458 (define-key compose-map "o`"    [ograve])
459 (define-key compose-map "o'"    [oacute])
460 (define-key compose-map "o^"    [ocircumflex])
461 (define-key compose-map "o~"    [otilde])
462 (define-key compose-map "o\""   [odiaeresis])
463 (define-key compose-map "o/"    [oslash])
464
465 (define-key compose-map "p!"    [paragraph])
466
467 (define-key compose-map "r0"    [registered])
468 (define-key compose-map "rO"    [registered])
469 (define-key compose-map "ro"    [registered])
470
471 (define-key compose-map "s!"    [section])
472 (define-key compose-map "s0"    [section])
473 (define-key compose-map "sO"    [section])
474 (define-key compose-map "so"    [section])
475 (define-key compose-map "ss"    [ssharp])
476
477 (define-key compose-map "th"    [thorn])
478
479 (define-key compose-map "u`"    [ugrave])
480 (define-key compose-map "u'"    [uacute])
481 (define-key compose-map "u^"    [ucircumflex])
482 (define-key compose-map "u\""   [udiaeresis])
483 (define-key compose-map "u/"    [mu])
484
485 (define-key compose-map "x0"    [currency])
486 (define-key compose-map "xO"    [currency])
487 (define-key compose-map "xo"    [currency])
488 (define-key compose-map "xx"    [multiply])
489
490 (define-key compose-map "y-"    [yen])
491 (define-key compose-map "y="    [yen])
492 (define-key compose-map "y'"    [yacute])
493 (define-key compose-map "y\""   [ydiaeresis])
494
495 (define-key compose-map "|C"    [cent])
496 (define-key compose-map "|c"    [cent])
497 (define-key compose-map "||"    [brokenbar])
498
499 \f
500 ;; Suppose we type these three physical keys: [Multi_key " a]
501 ;; Xlib can deliver these keys as the following sequences of keysyms:
502 ;;
503 ;; - [Multi_key " a] (no surprise here)
504 ;; - [adiaeresis] (OK, Xlib is doing compose processing for us)
505 ;; - [Multi_key " adiaeresis] (Huh?)
506 ;;
507 ;; It is the last possibility that is arguably a bug.  Xlib can't
508 ;; decide whether it's really doing compose processing or not (or
509 ;; actually, different parts of Xlib disagree).
510 ;;
511 ;; So we'll just convert [Multi_key " adiaeresis] to [adiaeresis]
512 (defun xlib-input-method-bug-workaround (keymap)
513   (map-keymap
514    (lambda (key value)
515      (cond
516       ((keymapp value)
517        (xlib-input-method-bug-workaround value))
518       ((and (sequencep value)
519             (eq 1 (length value))
520             (null (lookup-key keymap value)))
521        (define-key keymap value value))))
522    keymap
523    ;; #### It is currently not safe to add definitions to a keymap in
524    ;; map-keymap, due to a bug in map-keymap (dangling pointer to freed
525    ;; memory on a rehash).  So we sort, which has the side effect of
526    ;; mapping over a copy of the original hash-table.
527    t))
528 (xlib-input-method-bug-workaround compose-map)
529 (unintern 'xlib-input-method-bug-workaround)
530
531 ;; While we're at it, a similar mechanism will make colon equivalent
532 ;; to doublequote for diaeresis processing.  Some Xlibs do this.
533 (defun alias-colon-to-doublequote (keymap)
534   (map-keymap
535    (lambda (key value)
536      (when (keymapp value)
537        (alias-colon-to-doublequote value))
538      (when (eq key '\")
539        (define-key keymap ":" value)))
540    keymap
541    ;; #### It is currently not safe to add definitions to a keymap in
542    ;; map-keymap, due to a bug in map-keymap (dangling pointer to freed
543    ;; memory on a rehash).  So we sort, which has the side effect of
544    ;; mapping over a copy of the original hash-table.
545    t))
546 (alias-colon-to-doublequote compose-map)
547 (unintern 'alias-colon-to-doublequote)
548
549 ;;; Electric dead keys: making a' mean a-acute.
550
551 \f
552 (defun electric-diacritic (&optional count)
553   "Modify the previous character with an accent.
554 For example, if `:' is bound to this command, then typing `a:'
555 will first insert `a' and then turn it into `\344' (adiaeresis).
556 The keys to which this command may be bound (and the accents
557 which it understands) are:
558
559    '  (acute)       \301\311\315\323\332\335 \341\351\355\363\372\375
560    `  (grave)       \300\310\314\322\331 \340\350\354\362\371
561    :  (diaeresis)   \304\313\317\326\334 \344\353\357\366\374\377
562    ^  (circumflex)  \302\312\316\324\333 \342\352\356\364\373
563    ,  (cedilla)     \307\347
564    .  (ring)        \305\345"
565   (interactive "p")
566   (or count (setq count 1))
567
568   (if (not (eq last-command 'self-insert-command))
569       ;; Only do the magic if the two chars were typed in succession.
570       (self-insert-command count)
571
572     ;; This is so that ``a : C-x u'' will transform `adiaeresis' back into `a:'
573     (self-insert-command count)
574     (undo-boundary)
575     (delete-char (- count))
576
577     (let* ((c last-command-char)
578            (map (cond ((eq c ?') compose-acute-map)
579                       ((eq c ?`) compose-grave-map)
580                       ((eq c ?,) compose-cedilla-map)
581                       ((eq c ?:) compose-diaeresis-map)
582                       ((eq c ?^) compose-circumflex-map)
583                       ((eq c ?~) compose-tilde-map)
584                       ((eq c ?.) compose-ring-map)
585                       (t (error "unknown diacritic: %s (%c)" c c))))
586            (base-char (preceding-char))
587            (mod-char (and (>= (downcase base-char) ?a) ; only do alphabetics?
588                           (<= (downcase base-char) ?z)
589                           (lookup-key map (make-string 1 base-char)))))
590       (if (and (vectorp mod-char) (= (length mod-char) 1))
591           (setq mod-char (aref mod-char 0)))
592       (if (and mod-char (symbolp mod-char))
593           (setq mod-char (or (get mod-char character-set-property) mod-char)))
594       (if (and mod-char (> count 0))
595           (delete-char -1)
596         (setq mod-char c))
597       (while (> count 0)
598         (insert mod-char)
599         (setq count (1- count))))))
600
601 ;; should "::" mean "ยจ" and ": " mean ":"?
602 ;; should we also do
603 ;;    (?~
604 ;;     (?A "\303")
605 ;;     (?C "\307")
606 ;;     (?D "\320")
607 ;;     (?N "\321")
608 ;;     (?O "\325")
609 ;;     (?a "\343")
610 ;;     (?c "\347")
611 ;;     (?d "\360")
612 ;;     (?n "\361")
613 ;;     (?o "\365")
614 ;;     (?> "\273")
615 ;;     (?< "\253")
616 ;;     (?  "~")) ; no special code
617 ;;    (?\/
618 ;;     (?A "\305") ;; A-with-ring (Norwegian and Danish)
619 ;;     (?E "\306") ;; AE-ligature (Norwegian and Danish)
620 ;;     (?O "\330")
621 ;;     (?a "\345") ;; a-with-ring (Norwegian and Danish)
622 ;;     (?e "\346") ;; ae-ligature (Norwegian and Danish)
623 ;;     (?o "\370")
624 ;;     (?  "/")) ; no special code
625
626 \f
627 ;;; Providing help in the middle of a compose sequence.  (Way cool.)
628
629 (eval-when-compile
630   (defsubst next-composable-event ()
631     (let (event)
632       (while (progn
633                (setq event (next-command-event))
634                (not (or (key-press-event-p event)
635                         (button-press-event-p event))))
636         (dispatch-event event))
637       event)))
638
639 (defun compose-help (ignore-prompt)
640   (let* ((keys (apply 'vector (nbutlast (append (this-command-keys) nil))))
641          (map (or (lookup-key function-key-map keys)
642                   (error "can't find map?  %s %s" keys (this-command-keys))))
643          binding)
644     (save-excursion
645       (with-output-to-temp-buffer "*Help*"
646         (set-buffer "*Help*")
647         (erase-buffer)
648         (message "Working...")
649         (setq ctl-arrow 'compose) ; non-t-non-nil
650         (insert "You are typing a compose sequence.  So far you have typed: ")
651         (insert (key-description keys))
652         (insert "\nCompletions from here are:\n\n")
653         (map-keymap 'compose-help-mapper map t)
654         (message "? ")))
655     (while (keymapp map)
656       (setq binding (lookup-key map (vector (next-composable-event))))
657       (if (null binding)
658           (message "No such key in keymap. Try again.")
659         (setq map binding)))
660     binding))
661
662 (put 'compose-help 'isearch-command t)  ; so that it doesn't terminate isearch
663
664 (defun compose-help-mapper (key binding)
665   (if (and (symbolp key)
666            (get key character-set-property))
667       (setq key (get key character-set-property)))
668   (if (eq binding 'compose-help) ; suppress that...
669       nil
670     (if (keymapp binding)
671         (let ((p (point)))
672           (map-keymap 'compose-help-mapper binding t)
673           (goto-char p)
674           (while (not (eobp))
675             (if (characterp key)
676                 (insert (make-string 1 key))
677               (insert (single-key-description key)))
678             (insert " ")
679             (forward-line 1)))
680       (if (characterp key)
681           (insert (make-string 1 key))
682         (insert (single-key-description key)))
683       (indent-to 16)
684       (let ((code (and (vectorp binding)
685                        (= 1 (length binding))
686                        (get (aref binding 0) character-set-property))))
687         (if code
688             (insert (make-string 1 code))
689           (if (stringp binding)
690               (insert binding)
691             (insert (prin1-to-string binding)))))
692       (when (and (vectorp binding) (= 1 (length binding)))
693         (indent-to 32)
694         (insert (symbol-name (aref binding 0)))))
695     (insert "\n")))
696
697 ;; define it at top-level in the compose map...
698 ;;(define-key compose-map [(control h)] 'compose-help)
699 ;;(define-key compose-map [help]        'compose-help)
700 ;; and then define it in each sub-map of the compose map.
701 (map-keymap
702  (lambda (key binding)
703    (when (keymapp binding)
704 ;;     (define-key binding [(control h)] 'compose-help)
705 ;;     (define-key binding [help]        'compose-help)
706      ))
707  compose-map)
708
709 ;; Make redisplay display the accented letters
710 (if (memq (default-value 'ctl-arrow) '(t nil))
711     (setq-default ctl-arrow 'iso-8859/1))
712
713 \f
714 (provide 'x-compose)
715
716 ;;; x-compose.el ends here