]> code.delx.au - gnu-emacs/blob - lisp/international/iso-acc.el
(minibuffer-setup-hook): Fix misspelling of hook name.
[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 '(("portuguese"
69 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
70 (?C . ?\307) (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363)
71 (?u . ?\372) (?c . ?\347) (?\ . ?'))
72 (?` (?A . ?\300) (?a . ?\340) (?\ . ?`))
73 (?^ (?A . ?\302) (?E . ?\312) (?O . ?\324) (?a . ?\342) (?e . ?\352)
74 (?o . ?\364) (?\ . ?^))
75 (?\" (?U . ?\334) (?u . ?\374) (?\ . ?\"))
76 (?\~ (?A . ?\303) (?O . ?\325) (?a . ?\343) (?o . ?\365) (?\ . ?\~)))
77
78 ("irish"
79 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
80 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
81 (?\ . ?')))
82
83 ("french"
84 (?' (?E . ?\311) (?C . ?\307) (?e . ?\351) (?c . ?\347) (?\ . ?'))
85 (?` (?A . ?\300) (?E . ?\310) (?U . ?\331)
86 (?a . ?\340) (?e . ?\350) (?u . ?\371) (?\ . ?`))
87 (?^ (?A . ?\302) (?E . ?\312) (?I . ?\316) (?O . ?\324) (?U . ?\333)
88 (?a . ?\342) (?e . ?\352) (?i . ?\356) (?o . ?\364) (?u . ?\373)
89 (?\ . ?^))
90 (?\" (?E . ?\313) (?I . ?\317)
91 (?e . ?\353) (?i . ?\357) (?\ . ?\"))
92 (?\~ (?< . ?\253) (?> . ?\273) (?C . ?\307) (?c . ?\347) (?\ . ?\~)))
93
94 ("latin-2"
95 (?' (?A . ?\301) (?C . ?\306) (?D . ?\320) (?E . ?\311) (?I . ?\315)
96 (?L . ?\305) (?N . ?\321) (?O . ?\323) (?R . ?\300) (?S . ?\246)
97 (?U . ?\332) (?Y . ?\335) (?Z . ?\254) (?a . ?\341) (?c . ?\346)
98 (?d . ?\360) (?e . ?\351) (?i . ?\355) (?l . ?\345) (?n . ?\361)
99 (?o . ?\363) (?r . ?\340) (?s . ?\266) (?u . ?\372) (?y . ?\375)
100 (?z . ?\274) (?' . ?\264) (?\ . ?'))
101 (?` (?A . ?\241) (?C . ?\307) (?E . ?\312) (?L . ?\243) (?S . ?\252)
102 (?T . ?\336) (?Z . ?\257) (?a . ?\261) (?l . ?\263) (?c . ?\347)
103 (?e . ?\352) (?s . ?\272) (?t . ?\376) (?z . ?\277) (?` . ?\252)
104 (?. . ?\377) (?\ . ?`))
105 (?^ (?A . ?\302) (?O . ?\324) (?a . ?\342) (?o . ?\364)
106 (?^ . ?^) ; no special code?
107 (?\ . ?^))
108 (?\" (?A . ?\304) (?E . ?\313) (?O . ?\326) (?U . ?\334) (?a . ?\344)
109 (?e . ?\353) (?o . ?\366) (?s . ?\337) (?u . ?\374) (?\" . ?\250)
110 (?\ . ?\"))
111 (?\~ (?A . ?\303) (?C . ?\310) (?D . ?\317) (?L . ?\245) (?N . ?\322)
112 (?O . ?\325) (?R . ?\330) (?S . ?\251) (?T . ?\253) (?U . ?\333)
113 (?Z . ?\256) (?a . ?\323) (?c . ?\350) (?d . ?\357) (?l . ?\265)
114 (?n . ?\362) (?o . ?\365) (?r . ?\370) (?s . ?\271) (?t . ?\273)
115 (?u . ?\373) (?z . ?\276)
116 (?v . ?\242) ; v accent
117 (?\~ . ?\242) ; v accent
118 (?\. . ?\270) ; cedilla accent
119 (?\ . ?\~)))
120
121 ("latin-1"
122 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
123 (?Y . ?\335) (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363)
124 (?u . ?\372) (?y . ?\375) (?' . ?\264) (?\ . ?'))
125 (?` (?A . ?\300) (?E . ?\310) (?I . ?\314) (?O . ?\322) (?U . ?\331)
126 (?a . ?\340) (?e . ?\350) (?i . ?\354) (?o . ?\362) (?u . ?\371)
127 (?` . ?`) (?\ . ?`))
128 (?^ (?A . ?\302) (?E . ?\312) (?I . ?\316) (?O . ?\324) (?U . ?\333)
129 (?a . ?\342) (?e . ?\352) (?i . ?\356) (?o . ?\364) (?u . ?\373)
130 (?^ . ?^) (?\ . ?^))
131 (?\" (?A . ?\304) (?E . ?\313) (?I . ?\317) (?O . ?\326) (?U . ?\334)
132 (?a . ?\344) (?e . ?\353) (?i . ?\357) (?o . ?\366) (?s . ?\337)
133 (?u . ?\374) (?y . ?\377) (?\" . ?\250) (?\ . ?\"))
134 (?\~ (?A . ?\303) (?C . ?\307) (?D . ?\320) (?N . ?\321) (?O . ?\325)
135 (?T . ?\336) (?a . ?\343) (?c . ?\347) (?d . ?\360) (?n . ?\361)
136 (?o . ?\365) (?t . ?\376) (?> . ?\273) (?< . ?\253) (?\~ . ?\270)
137 (?! . ?\241) (?? . ?\277)
138 (?\ . ?\~))
139 (?\/ (?A . ?\305) (?E . ?\306) (?O . ?\330) (?a . ?\345) (?e . ?\346)
140 (?o . ?\370) (?\/ . ?\260) (?\ . ?\/))))
141 "List of language-specific customizations for the ISO Accents mode.
142
143 Each element of the list is of the form
144
145 (LANGUAGE
146 (PSEUDO-ACCENT MAPPINGS)
147 (PSEUDO-ACCENT MAPPINGS)
148 ...)
149
150 LANGUAGE is a string naming the language.
151 PSEUDO-ACCENT is a char specifying an accent key.
152 MAPPINGS are cons cells of the form (CHAR . ISO-CHAR).
153
154 The net effect is that the key sequence PSEUDO-ACCENT CHAR is mapped
155 to ISO-CHAR on input.")
156
157 (defvar iso-language nil
158 "Language for which ISO Accents mode is currently customized.
159 Change it with the `iso-accents-customize' function.")
160
161 (defvar iso-accents-list nil
162 "Association list for ISO accent combinations, for the chosen language.")
163
164 (defvar iso-accents-mode nil
165 "*Non-nil enables ISO Accents mode.
166 Setting this variable makes it local to the current buffer.
167 See the function `iso-accents-mode'.")
168 (make-variable-buffer-local 'iso-accents-mode)
169
170 (defvar iso-accents-enable '(?' ?` ?^ ?\" ?~ ?/)
171 "*List of accent keys that become prefixes in ISO Accents mode.
172 The default is (?' ?` ?^ ?\" ?~ ?/), which contains all the supported
173 accent keys. If you set this variable to a list in which some of those
174 characters are missing, the missing ones do not act as accents.
175
176 Note that if you specify a language with `iso-accents-customize',
177 that can also turn off certain prefixes (whichever ones are not needed in
178 the language you choose).")
179
180 (defun iso-accents-accent-key (prompt)
181 "Modify the following character by adding an accent to it."
182 ;; Pick up the accent character.
183 (if (and iso-accents-mode
184 (memq last-input-char iso-accents-enable))
185 (iso-accents-compose prompt)
186 (char-to-string last-input-char)))
187
188 (defun iso-accents-compose (prompt)
189 (let* ((first-char last-input-char)
190 (list (assq first-char iso-accents-list))
191 ;; Wait for the second key and look up the combination.
192 (second-char (if (or prompt
193 (not (eq (key-binding "a")
194 'self-insert-command))
195 ;; Not at start of a key sequence.
196 (> (length (this-single-command-keys)) 1)
197 ;; Called from anything but the command loop.
198 this-command)
199 (progn
200 (message "%s%c"
201 (or prompt "Compose with ")
202 first-char)
203 (read-event))
204 (insert first-char)
205 (prog1 (read-event)
206 (delete-region (1- (point)) (point)))))
207 (entry (cdr (assq second-char list))))
208 (if entry
209 ;; Found it: return the mapped char
210 (vector entry)
211 ;; Otherwise, advance and schedule the second key for execution.
212 (setq unread-command-events (list second-char))
213 (vector first-char))))
214
215 ;; It is a matter of taste if you want the minor mode indicated
216 ;; in the mode line...
217 ;; If so, uncomment the next four lines.
218 ;; (or (assq 'iso-accents-mode minor-mode-map-alist)
219 ;; (setq minor-mode-alist
220 ;; (append minor-mode-alist
221 ;; '((iso-accents-mode " ISO-Acc")))))
222
223 ;;;###autoload
224 (defun iso-accents-mode (&optional arg)
225 "Toggle ISO Accents mode, in which accents modify the following letter.
226 This permits easy insertion of accented characters according to ISO-8859-1.
227 When Iso-accents mode is enabled, accent character keys
228 \(`, ', \", ^, / and ~) do not self-insert; instead, they modify the following
229 letter key so that it inserts an ISO accented letter.
230
231 You can customize ISO Accents mode to a particular language
232 with the command `iso-accents-customize'.
233
234 Special combinations: ~c gives a c with cedilla,
235 ~d gives an Icelandic eth (d with dash).
236 ~t gives an Icelandic thorn.
237 \"s gives German sharp s.
238 /a gives a with ring.
239 /e gives an a-e ligature.
240 ~< and ~> give guillemots.
241 ~! gives an inverted exclamation mark.
242 ~? gives an inverted question mark.
243
244 With an argument, a positive argument enables ISO Accents mode,
245 and a negative argument disables it."
246
247 (interactive "P")
248
249 (if (if arg
250 ;; Negative arg means switch it off.
251 (<= (prefix-numeric-value arg) 0)
252 ;; No arg means toggle.
253 iso-accents-mode)
254 (setq iso-accents-mode nil)
255
256 ;; Enable electric accents.
257 (setq iso-accents-mode t)))
258
259 (defun iso-accents-customize (language)
260 "Customize the ISO accents machinery for a particular language.
261 It selects the customization based on the specifications in the
262 `iso-languages' variable."
263 (interactive (list (completing-read "Language: " iso-languages nil t)))
264 (let ((table (assoc language iso-languages))
265 all-accents tail)
266 (if (not table)
267 (error "Unknown language '%s'" language)
268 (setq iso-language language
269 iso-accents-list (cdr table))
270 (if key-translation-map
271 (substitute-key-definition
272 'iso-accents-accent-key nil key-translation-map)
273 (setq key-translation-map (make-sparse-keymap)))
274 ;; Set up translations for all the characters that are used as
275 ;; accent prefixes in this language.
276 (setq tail iso-accents-list)
277 (while tail
278 (define-key key-translation-map (vector (car (car tail)))
279 'iso-accents-accent-key)
280 (setq tail (cdr tail))))))
281
282 (defun iso-accentuate (start end)
283 "Convert two-character sequences in region into accented characters.
284 Noninteractively, this operates on text from START to END.
285 This uses the same conversion that ISO Accents mode uses for type-in."
286 (interactive "r")
287 (save-excursion
288 (save-restriction
289 (narrow-to-region start end)
290 (goto-char start)
291 (forward-char 1)
292 (let (entry)
293 (while (< (point) end)
294 (if (and (memq (preceding-char) iso-accents-enable)
295 (setq entry (cdr (assq (following-char) (assq (preceding-char) iso-accents-list)))))
296 (progn
297 (forward-char -1)
298 (delete-char 2)
299 (insert entry)
300 (setq end (1- end)))
301 (forward-char 1)))))))
302
303 (defun iso-accent-rassoc-unit (value alist)
304 (let (elt acc)
305 (while (and alist (not elt))
306 (setq acc (car (car alist))
307 elt (car (rassq value (cdr (car alist))))
308 alist (cdr alist)))
309 (if elt
310 (cons acc elt))))
311
312 (defun iso-unaccentuate (start end)
313 "Convert accented characters in the region into two-character sequences.
314 Noninteractively, this operates on text from START to END.
315 This uses the opposite of the conversion done by ISO Accents mode for type-in."
316 (interactive "r")
317 (save-excursion
318 (save-restriction
319 (narrow-to-region start end)
320 (goto-char start)
321 (let (entry)
322 (while (< (point) end)
323 (if (and (> (following-char) 127)
324 (setq entry (iso-accent-rassoc-unit (following-char)
325 iso-accents-list)))
326 (progn
327 (delete-char 1)
328 (insert (car entry) (cdr entry))
329 (setq end (1+ end)))
330 (forward-char 1)))))))
331
332 (defun iso-deaccentuate (start end)
333 "Convert accented characters in the region into unaccented characters.
334 Noninteractively, this operates on text from START to END."
335 (interactive "r")
336 (save-excursion
337 (save-restriction
338 (narrow-to-region start end)
339 (goto-char start)
340 (let (entry)
341 (while (< (point) end)
342 (if (and (> (following-char) 127)
343 (setq entry (iso-accent-rassoc-unit (following-char)
344 iso-accents-list)))
345 (progn
346 (delete-char 1)
347 (insert (cdr entry)))
348 (forward-char 1)))))))
349
350 ;; Set up the default settings.
351 (iso-accents-customize "latin-1")
352
353 ;; Use Iso-Accents mode in the minibuffer
354 ;; if it was in use in the previous buffer.
355 (defun iso-acc-minibuf-setup ()
356 (setq iso-accents-mode
357 (save-excursion
358 (set-buffer (window-buffer minibuffer-scroll-window))
359 iso-accents-mode)))
360
361 (add-hook 'minibuffer-setup-hook 'iso-acc-minibuf-setup)
362
363 ;;; iso-acc.el ends here