]> code.delx.au - gnu-emacs/blob - lisp/international/iso-acc.el
Change a category of one Japanese character.
[gnu-emacs] / lisp / international / iso-acc.el
1 ;;; iso-acc.el --- minor mode providing electric accent keys
2
3 ;; Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Johan Vromans
6 ;; Maintainer: FSF
7 ;; Keywords: i18n
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Function `iso-accents-mode' activates a minor mode in which
29 ;; typewriter "dead keys" are emulated. The purpose of this emulation
30 ;; is to provide a simple means for inserting accented characters
31 ;; according to the ISO-8859-1 character set.
32 ;;
33 ;; In `iso-accents-mode', pseudo accent characters are used to
34 ;; introduce accented keys. The pseudo-accent characters are:
35 ;;
36 ;; ' (minute) -> grave accent
37 ;; ` (backtick) -> acute accent
38 ;; " (second) -> diaeresis
39 ;; ^ (caret) -> circumflex
40 ;; ~ (tilde) -> tilde over the character
41 ;; / (slash) -> slash through the character.
42 ;; Also: /A is A-with-ring and /E is AE ligature.
43 ;;
44 ;; The action taken depends on the key that follows the pseudo accent.
45 ;; In general:
46 ;;
47 ;; pseudo-accent + appropriate letter -> accented letter
48 ;; pseudo-accent + space -> pseudo-accent
49 ;; pseudo-accent + pseudo-accent -> accent (if available)
50 ;; pseudo-accent + other -> pseudo-accent + other
51 ;;
52 ;; If the pseudo-accent is followed by anything else than a
53 ;; self-insert-command, the dead-key code is terminated, the
54 ;; pseudo-accent inserted 'as is' and the bell is rung to signal this.
55 ;;
56 ;; Function `iso-accents-mode' can be used to enable the iso accents
57 ;; minor mode, or disable it.
58
59 ;; If you want only some of these characters to serve as accents,
60 ;; add a language to `iso-languages' which specifies the accent characters
61 ;; that you want, then select the language with `iso-accents-customize'.
62 \f
63 ;;; Code:
64
65 (provide 'iso-acc)
66
67 (defvar iso-languages
68 '(("catalan"
69 ;; Note this includes some extra characters used in Spanish,
70 ;; on the idea that someone who uses Catalan is likely to use Spanish
71 ;; as well.
72 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
73 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
74 (?\ . ?'))
75 (?` (?A . ?\300) (?E . ?\310) (?O . ?\322)
76 (?a . ?\340) (?e . ?\350) (?o . ?\362) (?\ . ?`))
77 (?\" (?I . ?\317) (?U . ?\334) (?i . ?\357) (?u . ?\374) (?\ . ?\"))
78 (?\~ (?C . ?\307) (?N . ?\321) (?c . ?\347) (?n . ?\361)
79 (?> . ?\273) (?< . ?\253) (?! . ?\241) (?? . ?\277)
80 (?\ . ?\~)))
81
82 ("esperanto"
83 (?^ (?H . ?\246) (?J . ?\254) (?h . ?\266) (?j . ?\274) (?C . ?\306)
84 (?G . ?\330) (?S . ?\336) (?c . ?\346) (?g . ?\370) (?s . ?\376)
85 (?^ . ?^) (?\ . ?^))
86 (?~ (?U . ?\335) (?u . ?\375) (?\ . ?~)))
87
88 ("french"
89 (?' (?E . ?\311) (?C . ?\307) (?e . ?\351) (?c . ?\347) (?\ . ?'))
90 (?` (?A . ?\300) (?E . ?\310) (?U . ?\331)
91 (?a . ?\340) (?e . ?\350) (?u . ?\371) (?\ . ?`))
92 (?^ (?A . ?\302) (?E . ?\312) (?I . ?\316) (?O . ?\324) (?U . ?\333)
93 (?a . ?\342) (?e . ?\352) (?i . ?\356) (?o . ?\364) (?u . ?\373)
94 (?\ . ?^))
95 (?\" (?E . ?\313) (?I . ?\317)
96 (?e . ?\353) (?i . ?\357) (?\ . ?\"))
97 (?\~ (?< . ?\253) (?> . ?\273) (?C . ?\307) (?c . ?\347) (?\ . ?\~)))
98
99 ("german"
100 (?\" (?A . ?\304) (?O . ?\326) (?U . ?\334)
101 (?a . ?\344) (?o . ?\366) (?u . ?\374) (?s . ?\337) (?\ . ?\")))
102
103 ("irish"
104 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
105 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
106 (?\ . ?')))
107
108 ("latin-1"
109 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
110 (?Y . ?\335) (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363)
111 (?u . ?\372) (?y . ?\375) (?' . ?\264) (?\ . ?'))
112 (?` (?A . ?\300) (?E . ?\310) (?I . ?\314) (?O . ?\322) (?U . ?\331)
113 (?a . ?\340) (?e . ?\350) (?i . ?\354) (?o . ?\362) (?u . ?\371)
114 (?` . ?`) (?\ . ?`))
115 (?^ (?A . ?\302) (?E . ?\312) (?I . ?\316) (?O . ?\324) (?U . ?\333)
116 (?a . ?\342) (?e . ?\352) (?i . ?\356) (?o . ?\364) (?u . ?\373)
117 (?^ . ?^) (?\ . ?^))
118 (?\" (?A . ?\304) (?E . ?\313) (?I . ?\317) (?O . ?\326) (?U . ?\334)
119 (?a . ?\344) (?e . ?\353) (?i . ?\357) (?o . ?\366) (?s . ?\337)
120 (?u . ?\374) (?y . ?\377) (?\" . ?\250) (?\ . ?\"))
121 (?\~ (?A . ?\303) (?C . ?\307) (?D . ?\320) (?N . ?\321) (?O . ?\325)
122 (?T . ?\336) (?a . ?\343) (?c . ?\347) (?d . ?\360) (?n . ?\361)
123 (?o . ?\365) (?t . ?\376) (?> . ?\273) (?< . ?\253) (?\~ . ?\270)
124 (?! . ?\241) (?? . ?\277)
125 (?\ . ?\~))
126 (?\/ (?A . ?\305) (?E . ?\306) (?O . ?\330) (?a . ?\345) (?e . ?\346)
127 (?o . ?\370) (?\/ . ?\260) (?\ . ?\/)))
128
129 ("latin-2"
130 (?' (?A . ?\301) (?C . ?\306) (?D . ?\320) (?E . ?\311) (?I . ?\315)
131 (?L . ?\305) (?N . ?\321) (?O . ?\323) (?R . ?\300) (?S . ?\246)
132 (?U . ?\332) (?Y . ?\335) (?Z . ?\254) (?a . ?\341) (?c . ?\346)
133 (?d . ?\360) (?e . ?\351) (?i . ?\355) (?l . ?\345) (?n . ?\361)
134 (?o . ?\363) (?r . ?\340) (?s . ?\266) (?u . ?\372) (?y . ?\375)
135 (?z . ?\274) (?' . ?\264) (?\ . ?'))
136 (?` (?A . ?\241) (?C . ?\307) (?E . ?\312) (?L . ?\243) (?S . ?\252)
137 (?T . ?\336) (?Z . ?\257) (?a . ?\261) (?l . ?\263) (?c . ?\347)
138 (?e . ?\352) (?s . ?\272) (?t . ?\376) (?z . ?\277) (?` . ?\252)
139 (?. . ?\377) (?\ . ?`))
140 (?^ (?A . ?\302) (?O . ?\324) (?a . ?\342) (?o . ?\364)
141 (?^ . ?^) ; no special code?
142 (?\ . ?^))
143 (?\" (?A . ?\304) (?E . ?\313) (?O . ?\326) (?U . ?\334) (?a . ?\344)
144 (?e . ?\353) (?o . ?\366) (?s . ?\337) (?u . ?\374) (?\" . ?\250)
145 (?\ . ?\"))
146 (?\~ (?A . ?\303) (?C . ?\310) (?D . ?\317) (?L . ?\245) (?N . ?\322)
147 (?O . ?\325) (?R . ?\330) (?S . ?\251) (?T . ?\253) (?U . ?\333)
148 (?Z . ?\256) (?a . ?\323) (?c . ?\350) (?d . ?\357) (?l . ?\265)
149 (?n . ?\362) (?o . ?\365) (?r . ?\370) (?s . ?\271) (?t . ?\273)
150 (?u . ?\373) (?z . ?\276)
151 (?v . ?\242) ; v accent
152 (?\~ . ?\242) ; v accent
153 (?\. . ?\270) ; cedilla accent
154 (?\ . ?\~)))
155
156 ("latin-3"
157 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
158 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
159 (?' . ?\264) (?\ . ?'))
160 (?` (?A . ?\300) (?E . ?\310) (?I . ?\314) (?O . ?\322) (?U . ?\331)
161 (?a . ?\340) (?e . ?\350) (?i . ?\354) (?o . ?\362) (?u . ?\371)
162 (?` . ?`) (?\ . ?`))
163 (?^ (?A . ?\302) (?E . ?\312) (?I . ?\316) (?O . ?\324) (?U . ?\333)
164 (?a . ?\342) (?e . ?\352) (?i . ?\356) (?o . ?\364) (?u . ?\373)
165 (?H . ?\246) (?J . ?\254) (?h . ?\266) (?j . ?\274) (?C . ?\306)
166 (?G . ?\330) (?S . ?\336) (?c . ?\346) (?g . ?\370) (?s . ?\376)
167 (?^ . ?^) (?\ . ?^))
168 (?\" (?A . ?\304) (?E . ?\313) (?I . ?\317) (?O . ?\326) (?U . ?\334)
169 (?a . ?\344) (?e . ?\353) (?i . ?\357) (?o . ?\366) (?s . ?\337)
170 (?u . ?\374) (?\" . ?\250) (?\ . ?\"))
171 (?~ (?A . ?\303) (?C . ?\307) (?D . ?\320) (?N . ?\321) (?O . ?\325)
172 (?a . ?\343) (?c . ?\347) (?d . ?\360) (?n . ?\361) (?o . ?\365)
173 (?$ . ?\245) (?S . ?\252) (?s . ?\272) (?G . ?\253) (?g . ?\273)
174 (?U . ?\335) (?u . ?\375) (?` . ?\242) (?~ . ?\270) (?\ . ?~))
175 (?/ (?H . ?\241) (?# . ?\243) (?$ . ?\244) (?r . ?\256) (?h . ?\261)
176 (?I . ?\251) (?Z . ?\257) (?i . ?\271) (?z . ?\277) (?C . ?\305)
177 (?G . ?\325) (?c . ?\345) (?g . ?\365) (?. . ?\377) (?/ . ?\260)
178 (?\ . ?/)))
179
180 ("portuguese"
181 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
182 (?C . ?\307) (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363)
183 (?u . ?\372) (?c . ?\347) (?\ . ?'))
184 (?` (?A . ?\300) (?a . ?\340) (?\ . ?`))
185 (?^ (?A . ?\302) (?E . ?\312) (?O . ?\324) (?a . ?\342) (?e . ?\352)
186 (?o . ?\364) (?\ . ?^))
187 (?\" (?U . ?\334) (?u . ?\374) (?\ . ?\"))
188 (?\~ (?A . ?\303) (?O . ?\325) (?a . ?\343) (?o . ?\365) (?\ . ?\~)))
189
190 ("spanish"
191 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
192 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
193 (?\ . ?'))
194 (?\" (?U . ?\334) (?u . ?\374) (?\ . ?\"))
195 (?\~ (?N . ?\321) (?n . ?\361) (?> . ?\273) (?< . ?\253) (?! . ?\241)
196 (?? . ?\277) (?\ . ?\~))))
197 "List of language-specific customizations for the ISO Accents mode.
198
199 Each element of the list is of the form
200
201 (LANGUAGE
202 (PSEUDO-ACCENT MAPPINGS)
203 (PSEUDO-ACCENT MAPPINGS)
204 ...)
205
206 LANGUAGE is a string naming the language.
207 PSEUDO-ACCENT is a char specifying an accent key.
208 MAPPINGS are cons cells of the form (CHAR . ISO-CHAR).
209
210 The net effect is that the key sequence PSEUDO-ACCENT CHAR is mapped
211 to ISO-CHAR on input.")
212
213 (defvar iso-language nil
214 "Language for which ISO Accents mode is currently customized.
215 Change it with the `iso-accents-customize' function.")
216
217 (defvar iso-accents-list nil
218 "Association list for ISO accent combinations, for the chosen language.")
219
220 (defvar iso-accents-mode nil
221 "*Non-nil enables ISO Accents mode.
222 Setting this variable makes it local to the current buffer.
223 See the function `iso-accents-mode'.")
224 (make-variable-buffer-local 'iso-accents-mode)
225
226 (defvar iso-accents-enable '(?' ?` ?^ ?\" ?~ ?/)
227 "*List of accent keys that become prefixes in ISO Accents mode.
228 The default is (?' ?` ?^ ?\" ?~ ?/), which contains all the supported
229 accent keys. If you set this variable to a list in which some of those
230 characters are missing, the missing ones do not act as accents.
231
232 Note that if you specify a language with `iso-accents-customize',
233 that can also turn off certain prefixes (whichever ones are not needed in
234 the language you choose).")
235
236 (defun iso-accents-accent-key (prompt)
237 "Modify the following character by adding an accent to it."
238 ;; Pick up the accent character.
239 (if (and iso-accents-mode
240 (memq last-input-char iso-accents-enable))
241 (iso-accents-compose prompt)
242 (char-to-string last-input-char)))
243
244 (defun iso-accents-compose (prompt)
245 (let* ((first-char last-input-char)
246 (list (assq first-char iso-accents-list))
247 ;; Wait for the second key and look up the combination.
248 (second-char (if (or prompt
249 (not (eq (key-binding "a")
250 'self-insert-command))
251 ;; Not at start of a key sequence.
252 (> (length (this-single-command-keys)) 1)
253 ;; Called from anything but the command loop.
254 this-command)
255 (progn
256 (message "%s%c"
257 (or prompt "Compose with ")
258 first-char)
259 (read-event))
260 (insert first-char)
261 (prog1 (read-event)
262 (delete-region (1- (point)) (point)))))
263 (entry (cdr (assq second-char list))))
264 (if entry
265 ;; Found it: return the mapped char
266 (vector entry)
267 ;; Otherwise, advance and schedule the second key for execution.
268 (setq unread-command-events (list second-char))
269 (vector first-char))))
270
271 ;; It is a matter of taste if you want the minor mode indicated
272 ;; in the mode line...
273 ;; If so, uncomment the next four lines.
274 ;; (or (assq 'iso-accents-mode minor-mode-map-alist)
275 ;; (setq minor-mode-alist
276 ;; (append minor-mode-alist
277 ;; '((iso-accents-mode " ISO-Acc")))))
278
279 ;;;###autoload
280 (defun iso-accents-mode (&optional arg)
281 "Toggle ISO Accents mode, in which accents modify the following letter.
282 This permits easy insertion of accented characters according to ISO-8859-1.
283 When Iso-accents mode is enabled, accent character keys
284 \(`, ', \", ^, / and ~) do not self-insert; instead, they modify the following
285 letter key so that it inserts an ISO accented letter.
286
287 You can customize ISO Accents mode to a particular language
288 with the command `iso-accents-customize'.
289
290 Special combinations: ~c gives a c with cedilla,
291 ~d gives an Icelandic eth (d with dash).
292 ~t gives an Icelandic thorn.
293 \"s gives German sharp s.
294 /a gives a with ring.
295 /e gives an a-e ligature.
296 ~< and ~> give guillemots.
297 ~! gives an inverted exclamation mark.
298 ~? gives an inverted question mark.
299
300 With an argument, a positive argument enables ISO Accents mode,
301 and a negative argument disables it."
302
303 (interactive "P")
304
305 (if (if arg
306 ;; Negative arg means switch it off.
307 (<= (prefix-numeric-value arg) 0)
308 ;; No arg means toggle.
309 iso-accents-mode)
310 (setq iso-accents-mode nil)
311
312 ;; Enable electric accents.
313 (setq iso-accents-mode t)))
314
315 (defun iso-accents-customize (language)
316 "Customize the ISO accents machinery for a particular language.
317 It selects the customization based on the specifications in the
318 `iso-languages' variable."
319 (interactive (list (completing-read "Language: " iso-languages nil t)))
320 (let ((table (assoc language iso-languages))
321 all-accents tail)
322 (if (not table)
323 (error "Unknown language '%s'" language)
324 (setq iso-language language
325 iso-accents-list (cdr table))
326 (if key-translation-map
327 (substitute-key-definition
328 'iso-accents-accent-key nil key-translation-map)
329 (setq key-translation-map (make-sparse-keymap)))
330 ;; Set up translations for all the characters that are used as
331 ;; accent prefixes in this language.
332 (setq tail iso-accents-list)
333 (while tail
334 (define-key key-translation-map (vector (car (car tail)))
335 'iso-accents-accent-key)
336 (setq tail (cdr tail))))))
337
338 (defun iso-accentuate (start end)
339 "Convert two-character sequences in region into accented characters.
340 Noninteractively, this operates on text from START to END.
341 This uses the same conversion that ISO Accents mode uses for type-in."
342 (interactive "r")
343 (save-excursion
344 (save-restriction
345 (narrow-to-region start end)
346 (goto-char start)
347 (forward-char 1)
348 (let (entry)
349 (while (< (point) end)
350 (if (and (memq (preceding-char) iso-accents-enable)
351 (setq entry (cdr (assq (following-char) (assq (preceding-char) iso-accents-list)))))
352 (progn
353 (forward-char -1)
354 (delete-char 2)
355 (insert entry)
356 (setq end (1- end)))
357 (forward-char 1)))))))
358
359 (defun iso-accent-rassoc-unit (value alist)
360 (let (elt acc)
361 (while (and alist (not elt))
362 (setq acc (car (car alist))
363 elt (car (rassq value (cdr (car alist))))
364 alist (cdr alist)))
365 (if elt
366 (cons acc elt))))
367
368 (defun iso-unaccentuate (start end)
369 "Convert accented characters in the region into two-character sequences.
370 Noninteractively, this operates on text from START to END.
371 This uses the opposite of the conversion done by ISO Accents mode for type-in."
372 (interactive "r")
373 (save-excursion
374 (save-restriction
375 (narrow-to-region start end)
376 (goto-char start)
377 (let (entry)
378 (while (< (point) end)
379 (if (and (> (following-char) 127)
380 (setq entry (iso-accent-rassoc-unit (following-char)
381 iso-accents-list)))
382 (progn
383 (delete-char 1)
384 (insert (car entry) (cdr entry))
385 (setq end (1+ end)))
386 (forward-char 1)))))))
387
388 (defun iso-deaccentuate (start end)
389 "Convert accented characters in the region into unaccented characters.
390 Noninteractively, this operates on text from START to END."
391 (interactive "r")
392 (save-excursion
393 (save-restriction
394 (narrow-to-region start end)
395 (goto-char start)
396 (let (entry)
397 (while (< (point) end)
398 (if (and (> (following-char) 127)
399 (setq entry (iso-accent-rassoc-unit (following-char)
400 iso-accents-list)))
401 (progn
402 (delete-char 1)
403 (insert (cdr entry)))
404 (forward-char 1)))))))
405
406 ;; Set up the default settings.
407 (iso-accents-customize "latin-1")
408
409 ;; Use Iso-Accents mode in the minibuffer
410 ;; if it was in use in the previous buffer.
411 (defun iso-acc-minibuf-setup ()
412 (setq iso-accents-mode
413 (save-excursion
414 (set-buffer (window-buffer minibuffer-scroll-window))
415 iso-accents-mode)))
416
417 (add-hook 'minibuffer-setup-hook 'iso-acc-minibuf-setup)
418
419 ;;; iso-acc.el ends here