1 ;;; iso-acc.el --- minor mode providing electric accent keys
3 ;; Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
5 ;; Author: Johan Vromans
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
33 ;; In `iso-accents-mode', pseudo accent characters are used to
34 ;; introduce accented keys. The pseudo-accent characters are:
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.
44 ;; The action taken depends on the key that follows the pseudo accent.
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
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.
56 ;; Function `iso-accents-mode' can be used to enable the iso accents
57 ;; minor mode, or disable it.
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'.
69 ;; Note this includes some extra characters used in Spanish,
70 ;; on the idea that someone who uses Catalan is likely to use Spanish
72 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
73 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
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)
83 (?^ (?H . ?\246) (?J . ?\254) (?h . ?\266) (?j . ?\274) (?C . ?\306)
84 (?G . ?\330) (?S . ?\336) (?c . ?\346) (?g . ?\370) (?s . ?\376)
86 (?~ (?U . ?\335) (?u . ?\375) (?\ . ?~)))
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)
95 (?\" (?E . ?\313) (?I . ?\317)
96 (?e . ?\353) (?i . ?\357) (?\ . ?\"))
97 (?\~ (?< . ?\253) (?> . ?\273) (?C . ?\307) (?c . ?\347) (?\ . ?\~)))
100 (?\" (?A . ?\304) (?O . ?\326) (?U . ?\334)
101 (?a . ?\344) (?o . ?\366) (?u . ?\374) (?s . ?\337) (?\ . ?\")))
104 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
105 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
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)
115 (?^ (?A . ?\302) (?E . ?\312) (?I . ?\316) (?O . ?\324) (?U . ?\333)
116 (?a . ?\342) (?e . ?\352) (?i . ?\356) (?o . ?\364) (?u . ?\373)
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)
126 (?\/ (?A . ?\305) (?E . ?\306) (?O . ?\330) (?a . ?\345) (?e . ?\346)
127 (?o . ?\370) (?\/ . ?\260) (?\ . ?\/)))
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?
143 (?\" (?A . ?\304) (?E . ?\313) (?O . ?\326) (?U . ?\334) (?a . ?\344)
144 (?e . ?\353) (?o . ?\366) (?s . ?\337) (?u . ?\374) (?\" . ?\250)
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
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)
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)
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)
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) (?\ . ?\~)))
191 (?' (?A . ?\301) (?E . ?\311) (?I . ?\315) (?O . ?\323) (?U . ?\332)
192 (?a . ?\341) (?e . ?\351) (?i . ?\355) (?o . ?\363) (?u . ?\372)
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.
199 Each element of the list is of the form
202 (PSEUDO-ACCENT MAPPINGS)
203 (PSEUDO-ACCENT MAPPINGS)
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).
210 The net effect is that the key sequence PSEUDO-ACCENT CHAR is mapped
211 to ISO-CHAR on input.")
213 (defvar iso-language nil
214 "Language for which ISO Accents mode is currently customized.
215 Change it with the `iso-accents-customize' function.")
217 (defvar iso-accents-list nil
218 "Association list for ISO accent combinations, for the chosen language.")
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)
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.
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).")
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)))
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.
257 (or prompt "Compose with ")
262 (delete-region (1- (point)) (point)))))
263 (entry (cdr (assq second-char list))))
265 ;; Found it: return the mapped char
267 ;; Otherwise, advance and schedule the second key for execution.
268 (setq unread-command-events (list second-char))
269 (vector first-char))))
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")))))
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.
287 You can customize ISO Accents mode to a particular language
288 with the command `iso-accents-customize'.
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.
300 With an argument, a positive argument enables ISO Accents mode,
301 and a negative argument disables it."
306 ;; Negative arg means switch it off.
307 (<= (prefix-numeric-value arg) 0)
308 ;; No arg means toggle.
310 (setq iso-accents-mode nil)
312 ;; Enable electric accents.
313 (setq iso-accents-mode t)))
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))
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)
334 (define-key key-translation-map (vector (car (car tail)))
335 'iso-accents-accent-key)
336 (setq tail (cdr tail))))))
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."
345 (narrow-to-region start end)
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)))))
357 (forward-char 1)))))))
359 (defun iso-accent-rassoc-unit (value alist)
361 (while (and alist (not elt))
362 (setq acc (car (car alist))
363 elt (car (rassq value (cdr (car alist))))
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."
375 (narrow-to-region start end)
378 (while (< (point) end)
379 (if (and (> (following-char) 127)
380 (setq entry (iso-accent-rassoc-unit (following-char)
384 (insert (car entry) (cdr entry))
386 (forward-char 1)))))))
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."
394 (narrow-to-region start end)
397 (while (< (point) end)
398 (if (and (> (following-char) 127)
399 (setq entry (iso-accent-rassoc-unit (following-char)
403 (insert (cdr entry)))
404 (forward-char 1)))))))
406 ;; Set up the default settings.
407 (iso-accents-customize "latin-1")
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
414 (set-buffer (window-buffer minibuffer-scroll-window))
417 (add-hook 'minibuffer-setup-hook 'iso-acc-minibuf-setup)
419 ;;; iso-acc.el ends here