]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / international / mule-cmds.el
1 ;;; mule-cmds.el --- commands for multilingual environment -*-coding: iso-2022-7bit -*-
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
9 ;; Copyright (C) 2003
10 ;; National Institute of Advanced Industrial Science and Technology (AIST)
11 ;; Registration Number H13PRO009
12
13 ;; Keywords: mule, i18n
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (eval-when-compile
35 (defvar dos-codepage)
36 (autoload 'widget-value "wid-edit"))
37
38 (defvar mac-system-coding-system)
39 (defvar mac-system-locale)
40
41 ;;; MULE related key bindings and menus.
42
43 (defvar mule-keymap (make-sparse-keymap)
44 "Keymap for Mule (Multilingual environment) specific commands.")
45
46 ;; Keep "C-x C-m ..." for mule specific commands.
47 (define-key ctl-x-map "\C-m" mule-keymap)
48
49 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
50 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
51 (define-key mule-keymap "F" 'set-file-name-coding-system)
52 (define-key mule-keymap "t" 'set-terminal-coding-system)
53 (define-key mule-keymap "k" 'set-keyboard-coding-system)
54 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
55 (define-key mule-keymap "x" 'set-selection-coding-system)
56 (define-key mule-keymap "X" 'set-next-selection-coding-system)
57 (define-key mule-keymap "\C-\\" 'set-input-method)
58 (define-key mule-keymap "c" 'universal-coding-system-argument)
59 (define-key mule-keymap "l" 'set-language-environment)
60
61 (defvar mule-menu-keymap
62 (make-sparse-keymap "Mule (Multilingual Environment)")
63 "Keymap for Mule (Multilingual environment) menu specific commands.")
64
65 (defvar describe-language-environment-map
66 (make-sparse-keymap "Describe Language Environment"))
67
68 (defvar setup-language-environment-map
69 (make-sparse-keymap "Set Language Environment"))
70
71 (defvar set-coding-system-map
72 (make-sparse-keymap "Set Coding System"))
73
74 (define-key-after mule-menu-keymap [set-language-environment]
75 (list 'menu-item "Set Language Environment" setup-language-environment-map))
76 (define-key-after mule-menu-keymap [separator-mule]
77 '("--")
78 t)
79 (define-key-after mule-menu-keymap [toggle-input-method]
80 '(menu-item "Toggle Input Method" toggle-input-method)
81 t)
82 (define-key-after mule-menu-keymap [set-input-method]
83 '(menu-item "Select Input Method..." set-input-method)
84 t)
85 (define-key-after mule-menu-keymap [describe-input-method]
86 '(menu-item "Describe Input Method" describe-input-method))
87 (define-key-after mule-menu-keymap [separator-input-method]
88 '("--")
89 t)
90 (define-key-after mule-menu-keymap [set-various-coding-system]
91 (list 'menu-item "Set Coding Systems" set-coding-system-map
92 :enable 'default-enable-multibyte-characters))
93 (define-key-after mule-menu-keymap [view-hello-file]
94 '(menu-item "Show Multi-lingual Text" view-hello-file
95 :enable (file-readable-p
96 (expand-file-name "HELLO" data-directory))
97 :help "Display file which says HELLO in many languages")
98 t)
99 (define-key-after mule-menu-keymap [separator-coding-system]
100 '("--")
101 t)
102 (define-key-after mule-menu-keymap [describe-language-environment]
103 (list 'menu-item "Describe Language Environment"
104 describe-language-environment-map
105 :help "Show multilingual settings for a specific language")
106 t)
107 (define-key-after mule-menu-keymap [describe-input-method]
108 '(menu-item "Describe Input Method..." describe-input-method
109 :help "Keyboard layout for a specific input method")
110 t)
111 (define-key-after mule-menu-keymap [describe-coding-system]
112 '(menu-item "Describe Coding System..." describe-coding-system)
113 t)
114 (define-key-after mule-menu-keymap [list-character-sets]
115 '(menu-item "List Character Sets" list-character-sets
116 :help "Show table of available character sets"))
117 (define-key-after mule-menu-keymap [mule-diag]
118 '(menu-item "Show All of Mule Status" mule-diag
119 :help "Display multilingual environment settings")
120 t)
121
122 (define-key-after set-coding-system-map [universal-coding-system-argument]
123 '(menu-item "For Next Command" universal-coding-system-argument
124 :help "Coding system to be used by next command")
125 t)
126 (define-key-after set-coding-system-map [separator-1]
127 '("--")
128 t)
129 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
130 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
131 :help "How to encode this buffer when saved")
132 t)
133 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
134 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
135 :enable buffer-file-name
136 :help "Revisit this file immediately using specified coding system")
137 t)
138 (define-key-after set-coding-system-map [set-file-name-coding-system]
139 '(menu-item "For File Name" set-file-name-coding-system
140 :help "How to decode/encode file names")
141 t)
142 (define-key-after set-coding-system-map [separator-2]
143 '("--")
144 t)
145
146 (define-key-after set-coding-system-map [set-keyboard-coding-system]
147 '(menu-item "For Keyboard" set-keyboard-coding-system
148 :help "How to decode keyboard input")
149 t)
150 (define-key-after set-coding-system-map [set-terminal-coding-system]
151 '(menu-item "For Terminal" set-terminal-coding-system
152 :enable (null (memq initial-window-system '(x w32 mac)))
153 :help "How to encode terminal output")
154 t)
155 (define-key-after set-coding-system-map [separator-3]
156 '("--")
157 t)
158 (define-key-after set-coding-system-map [set-selection-coding-system]
159 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
160 :visible (display-selections-p)
161 :help "How to en/decode data to/from selection/clipboard")
162 t)
163 (define-key-after set-coding-system-map [set-next-selection-coding-system]
164 '(menu-item "For Next X Selection" set-next-selection-coding-system
165 :visible (display-selections-p)
166 :help "How to en/decode next selection/clipboard operation")
167 t)
168 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
169 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
170 :visible (fboundp 'start-process)
171 :enable (get-buffer-process (current-buffer))
172 :help "How to en/decode I/O from/to subprocess connected to this buffer")
173 t)
174
175
176 (define-key setup-language-environment-map
177 [Default] '(menu-item "Default" setup-specified-language-environment))
178
179 (define-key describe-language-environment-map
180 [Default] '(menu-item "Default" describe-specified-language-support))
181
182 ;; This should be a single character key binding because users use it
183 ;; very frequently while editing multilingual text. Now we can use
184 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
185 ;; convenient because it requires shifting on most keyboards. An
186 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
187 ;; but it won't be used that frequently.
188 (define-key global-map "\C-\\" 'toggle-input-method)
189
190 ;; This is no good because people often type Shift-SPC
191 ;; meaning to type SPC. -- rms.
192 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
193 ;; (define-key global-map [?\S- ] 'toggle-input-method)
194
195 ;;; Mule related hyperlinks.
196 (defconst help-xref-mule-regexp-template
197 (purecopy (concat "\\(\\<\\("
198 "\\(coding system\\)\\|"
199 "\\(input method\\)\\|"
200 "\\(character set\\)\\|"
201 "\\(charset\\)"
202 "\\)\\s-+\\)?"
203 ;; Note starting with word-syntax character:
204 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
205
206 (defun coding-system-change-eol-conversion (coding-system eol-type)
207 "Return a coding system which differs from CODING-SYSTEM in EOL conversion.
208 The returned coding system converts end-of-line by EOL-TYPE
209 but text as the same way as CODING-SYSTEM.
210 EOL-TYPE should be `unix', `dos', `mac', or nil.
211 If EOL-TYPE is nil, the returned coding system detects
212 how end-of-line is formatted automatically while decoding.
213
214 EOL-TYPE can be specified by an integer 0, 1, or 2.
215 They means `unix', `dos', and `mac' respectively."
216 (if (symbolp eol-type)
217 (setq eol-type (cond ((eq eol-type 'unix) 0)
218 ((eq eol-type 'dos) 1)
219 ((eq eol-type 'mac) 2)
220 (t eol-type))))
221 ;; We call `coding-system-base' before `coding-system-eol-type',
222 ;; because the coding-system may not be initialized until then.
223 (let* ((base (coding-system-base coding-system))
224 (orig-eol-type (coding-system-eol-type coding-system)))
225 (cond ((vectorp orig-eol-type)
226 (if (not eol-type)
227 coding-system
228 (aref orig-eol-type eol-type)))
229 ((not eol-type)
230 base)
231 ((= eol-type orig-eol-type)
232 coding-system)
233 ((progn (setq orig-eol-type (coding-system-eol-type base))
234 (vectorp orig-eol-type))
235 (aref orig-eol-type eol-type)))))
236
237 (defun coding-system-change-text-conversion (coding-system coding)
238 "Return a coding system which differs from CODING-SYSTEM in text conversion.
239 The returned coding system converts text by CODING
240 but end-of-line as the same way as CODING-SYSTEM.
241 If CODING is nil, the returned coding system detects
242 how text is formatted automatically while decoding."
243 (let ((eol-type (coding-system-eol-type coding-system)))
244 (coding-system-change-eol-conversion
245 (if coding coding 'undecided)
246 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
247
248 (defun toggle-enable-multibyte-characters (&optional arg)
249 "Change whether this buffer uses multibyte characters.
250 With arg, use multibyte characters if the arg is positive.
251
252 Note that this command does not convert the byte contents of
253 the buffer; it only changes the way those bytes are interpreted.
254 In general, therefore, this command *changes* the sequence of
255 characters that the current buffer contains.
256
257 We suggest you avoid using this command unless you know what you are
258 doing. If you use it by mistake, and the buffer is now displayed
259 wrong, use this command again to toggle back to the right mode."
260 (interactive "P")
261 (let ((new-flag
262 (if (null arg) (null enable-multibyte-characters)
263 (> (prefix-numeric-value arg) 0))))
264 (set-buffer-multibyte new-flag))
265 (force-mode-line-update))
266
267 (defun view-hello-file ()
268 "Display the HELLO file which list up many languages and characters."
269 (interactive)
270 ;; We have to decode the file in any environment.
271 (let ((default-enable-multibyte-characters t)
272 (coding-system-for-read 'iso-2022-7bit))
273 (view-file (expand-file-name "HELLO" data-directory))))
274
275 (defun universal-coding-system-argument (coding-system)
276 "Execute an I/O command using the specified coding system."
277 (interactive
278 (let ((default (and buffer-file-coding-system
279 (not (eq (coding-system-type buffer-file-coding-system)
280 'undecided))
281 buffer-file-coding-system)))
282 (list (read-coding-system
283 (if default
284 (format "Coding system for following command (default %s): " default)
285 "Coding system for following command: ")
286 default))))
287 (let* ((keyseq (read-key-sequence
288 (format "Command to execute with %s:" coding-system)))
289 (cmd (key-binding keyseq))
290 prefix)
291
292 (when (eq cmd 'universal-argument)
293 (call-interactively cmd)
294
295 ;; Process keys bound in `universal-argument-map'.
296 (while (progn
297 (setq keyseq (read-key-sequence nil t)
298 cmd (key-binding keyseq t))
299 (not (eq cmd 'universal-argument-other-key)))
300 (let ((current-prefix-arg prefix-arg)
301 ;; Have to bind `last-command-char' here so that
302 ;; `digit-argument', for instance, can compute the
303 ;; prefix arg.
304 (last-command-char (aref keyseq 0)))
305 (call-interactively cmd)))
306
307 ;; This is the final call to `universal-argument-other-key', which
308 ;; set's the final `prefix-arg.
309 (let ((current-prefix-arg prefix-arg))
310 (call-interactively cmd))
311
312 ;; Read the command to execute with the given prefix arg.
313 (setq prefix prefix-arg
314 keyseq (read-key-sequence nil t)
315 cmd (key-binding keyseq)))
316
317 (let ((coding-system-for-read coding-system)
318 (coding-system-for-write coding-system)
319 (coding-system-require-warning t)
320 (current-prefix-arg prefix))
321 (message "")
322 (call-interactively cmd))))
323
324 (defun set-default-coding-systems (coding-system)
325 "Set default value of various coding systems to CODING-SYSTEM.
326 This sets the following coding systems:
327 o coding system of a newly created buffer
328 o default coding system for subprocess I/O
329 This also sets the following values:
330 o default value used as `file-name-coding-system' for converting file names
331 if CODING-SYSTEM is ASCII-compatible
332 o default value for the command `set-terminal-coding-system' (not on MSDOS)
333 o default value for the command `set-keyboard-coding-system'
334 if CODING-SYSTEM is ASCII-compatible"
335 (check-coding-system coding-system)
336 (setq-default buffer-file-coding-system coding-system)
337 (if (fboundp 'ucs-set-table-for-input)
338 (dolist (buffer (buffer-list))
339 (or (local-variable-p 'buffer-file-coding-system buffer)
340 (ucs-set-table-for-input buffer))))
341
342 (if (eq system-type 'darwin)
343 ;; The file-name coding system on Darwin systems is always utf-8.
344 (setq default-file-name-coding-system 'utf-8)
345 (if (and default-enable-multibyte-characters
346 (or (not coding-system)
347 (coding-system-get coding-system 'ascii-compatible-p)))
348 (setq default-file-name-coding-system coding-system)))
349 ;; If coding-system is nil, honor that on MS-DOS as well, so
350 ;; that they could reset the terminal coding system.
351 (unless (and (eq window-system 'pc) coding-system)
352 (setq default-terminal-coding-system coding-system))
353 (setq default-keyboard-coding-system coding-system)
354 ;; Preserve eol-type from existing default-process-coding-systems.
355 ;; On non-unix-like systems in particular, these may have been set
356 ;; carefully by the user, or by the startup code, to deal with the
357 ;; users shell appropriately, so should not be altered by changing
358 ;; language environment.
359 (let ((output-coding
360 (coding-system-change-text-conversion
361 (car default-process-coding-system) coding-system))
362 (input-coding
363 (coding-system-change-text-conversion
364 (cdr default-process-coding-system) coding-system)))
365 (setq default-process-coding-system
366 (cons output-coding input-coding))))
367
368 (defun prefer-coding-system (coding-system)
369 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
370 This also sets the following coding systems:
371 o coding system of a newly created buffer
372 o default coding system for subprocess I/O
373 This also sets the following values:
374 o default value used as `file-name-coding-system' for converting file names
375 o default value for the command `set-terminal-coding-system' (not on MSDOS)
376 o default value for the command `set-keyboard-coding-system'
377
378 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
379 systems set by this function will use that type of EOL conversion.
380
381 This command does not change the default value of terminal coding system
382 for MS-DOS terminal, because DOS terminals only support a single coding
383 system, and Emacs automatically sets the default to that coding system at
384 startup.
385
386 A coding system that requires automatic detection of text
387 +encoding (e.g. undecided, unix) can't be preferred.."
388 (interactive "zPrefer coding system: ")
389 (if (not (and coding-system (coding-system-p coding-system)))
390 (error "Invalid coding system `%s'" coding-system))
391 (if (memq (coding-system-type coding-system) '(raw-text undecided))
392 (error "Can't prefer the coding system `%s'" coding-system))
393 (let ((base (coding-system-base coding-system))
394 (eol-type (coding-system-eol-type coding-system)))
395 (set-coding-system-priority base)
396 (and (interactive-p)
397 (or (eq base coding-system)
398 (message "Highest priority is set to %s (base of %s)"
399 base coding-system)))
400 ;; If they asked for specific EOL conversion, honor that.
401 (if (memq eol-type '(0 1 2))
402 (setq base
403 (coding-system-change-eol-conversion base eol-type)))
404 (set-default-coding-systems base)))
405
406 (defvar sort-coding-systems-predicate nil
407 "If non-nil, a predicate function to sort coding systems.
408
409 It is called with two coding systems, and should return t if the first
410 one is \"less\" than the second.
411
412 The function `sort-coding-systems' use it.")
413
414 (defun sort-coding-systems (codings)
415 "Sort coding system list CODINGS by a priority of each coding system.
416 Return the sorted list. CODINGS is modified by side effects.
417
418 If a coding system is most preferred, it has the highest priority.
419 Otherwise, coding systems that correspond to MIME charsets have
420 higher priorities. Among them, a coding system included in the
421 `coding-system' key of the current language environment has higher
422 priority. See also the documentation of `language-info-alist'.
423
424 If the variable `sort-coding-systems-predicate' (which see) is
425 non-nil, it is used to sort CODINGS instead."
426 (if sort-coding-systems-predicate
427 (sort codings sort-coding-systems-predicate)
428 (let* ((from-priority (coding-system-priority-list))
429 (most-preferred (car from-priority))
430 (lang-preferred (get-language-info current-language-environment
431 'coding-system))
432 (func (function
433 (lambda (x)
434 (let ((base (coding-system-base x)))
435 ;; We calculate the priority number 0..255 by
436 ;; using the 8 bits PMMLCEII as this:
437 ;; P: 1 if most preferred.
438 ;; MM: greater than 0 if mime-charset.
439 ;; L: 1 if one of the current lang. env.'s codings.
440 ;; C: 1 if one of codings listed in the category list.
441 ;; E: 1 if not XXX-with-esc
442 ;; II: if iso-2022 based, 0..3, else 1.
443 (logior
444 (lsh (if (eq base most-preferred) 1 0) 7)
445 (lsh
446 (let ((mime (coding-system-get base :mime-charset)))
447 ;; Prefer coding systems corresponding to a
448 ;; MIME charset.
449 (if mime
450 ;; Lower utf-16 priority so that we
451 ;; normally prefer utf-8 to it, and put
452 ;; x-ctext below that.
453 (cond ((string-match "utf-16"
454 (symbol-name mime))
455 2)
456 ((string-match "^x-" (symbol-name mime))
457 1)
458 (t 3))
459 0))
460 5)
461 (lsh (if (memq base lang-preferred) 1 0) 4)
462 (lsh (if (memq base from-priority) 1 0) 3)
463 (lsh (if (string-match "-with-esc\\'"
464 (symbol-name base))
465 0 1) 2)
466 (if (eq (coding-system-type base) 'iso-2022)
467 (let ((category (coding-system-category base)))
468 ;; For ISO based coding systems, prefer
469 ;; one that doesn't use designation nor
470 ;; locking/single shifting.
471 (cond
472 ((or (eq category 'coding-category-iso-8-1)
473 (eq category 'coding-category-iso-8-2))
474 2)
475 ((or (eq category 'coding-category-iso-7-tight)
476 (eq category 'coding-category-iso-7))
477 1)
478 (t
479 0)))
480 1)
481 ))))))
482 (sort codings (function (lambda (x y)
483 (> (funcall func x) (funcall func y))))))))
484
485 (defun find-coding-systems-region (from to)
486 "Return a list of proper coding systems to encode a text between FROM and TO.
487
488 If FROM is a string, find coding systems in that instead of the buffer.
489 All coding systems in the list can safely encode any multibyte characters
490 in the text.
491
492 If the text contains no multibyte characters, return a list of a single
493 element `undecided'."
494 (let ((codings (find-coding-systems-region-internal from to)))
495 (if (eq codings t)
496 ;; The text contains only ASCII characters. Any coding
497 ;; systems are safe.
498 '(undecided)
499 ;; We need copy-sequence because sorting will alter the argument.
500 (sort-coding-systems (copy-sequence codings)))))
501
502 (defun find-coding-systems-string (string)
503 "Return a list of proper coding systems to encode STRING.
504 All coding systems in the list can safely encode any multibyte characters
505 in STRING.
506
507 If STRING contains no multibyte characters, return a list of a single
508 element `undecided'."
509 (find-coding-systems-region string nil))
510
511 (defun find-coding-systems-for-charsets (charsets)
512 "Return a list of proper coding systems to encode characters of CHARSETS.
513 CHARSETS is a list of character sets.
514
515 This only finds coding systems of type `charset', whose
516 `:charset-list' property includes all of CHARSETS (plus `ascii' for
517 ascii-compatible coding systems). It was used in older versions of
518 Emacs, but is unlikely to be what you really want now."
519 ;; Deal with aliases.
520 (setq charsets (mapcar (lambda (c)
521 (get-charset-property c :name))
522 charsets))
523 (cond ((or (null charsets)
524 (and (= (length charsets) 1)
525 (eq 'ascii (car charsets))))
526 '(undecided))
527 ((or (memq 'eight-bit-control charsets)
528 (memq 'eight-bit-graphic charsets))
529 '(raw-text utf-8-emacs))
530 (t
531 (let (codings)
532 (dolist (cs (coding-system-list t))
533 (let ((cs-charsets (and (eq (coding-system-type cs) 'charset)
534 (coding-system-charset-list cs)))
535 (charsets charsets))
536 (if (coding-system-get cs :ascii-compatible-p)
537 (add-to-list 'cs-charsets 'ascii))
538 (if (catch 'ok
539 (when cs-charsets
540 (while charsets
541 (unless (memq (pop charsets) cs-charsets)
542 (throw 'ok nil)))
543 t))
544 (push cs codings))))
545 (nreverse codings)))))
546
547 (defun find-multibyte-characters (from to &optional maxcount excludes)
548 "Find multibyte characters in the region specified by FROM and TO.
549 If FROM is a string, find multibyte characters in the string.
550 The return value is an alist of the following format:
551 ((CHARSET COUNT CHAR ...) ...)
552 where
553 CHARSET is a character set,
554 COUNT is a number of characters,
555 CHARs are the characters found from the character set.
556 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
557 Optional 4th arg EXCLUDES is a list of character sets to be ignored."
558 (let ((chars nil)
559 charset char)
560 (if (stringp from)
561 (if (multibyte-string-p from)
562 (let ((idx 0))
563 (while (setq idx (string-match "[^\000-\177]" from idx))
564 (setq char (aref from idx)
565 charset (char-charset char))
566 (unless (memq charset excludes)
567 (let ((slot (assq charset chars)))
568 (if slot
569 (if (not (memq char (nthcdr 2 slot)))
570 (let ((count (nth 1 slot)))
571 (setcar (cdr slot) (1+ count))
572 (if (or (not maxcount) (< count maxcount))
573 (nconc slot (list char)))))
574 (setq chars (cons (list charset 1 char) chars)))))
575 (setq idx (1+ idx)))))
576 (if enable-multibyte-characters
577 (save-excursion
578 (goto-char from)
579 (while (re-search-forward "[^\000-\177]" to t)
580 (setq char (preceding-char)
581 charset (char-charset char))
582 (unless (memq charset excludes)
583 (let ((slot (assq charset chars)))
584 (if slot
585 (if (not (member char (nthcdr 2 slot)))
586 (let ((count (nth 1 slot)))
587 (setcar (cdr slot) (1+ count))
588 (if (or (not maxcount) (< count maxcount))
589 (nconc slot (list char)))))
590 (setq chars (cons (list charset 1 char) chars)))))))))
591 (nreverse chars)))
592
593 (defun search-unencodable-char (coding-system)
594 "Search forward from point for a character that is not encodable.
595 It asks which coding system to check.
596 If such a character is found, set point after that character.
597 Otherwise, don't move point.
598
599 When called from a program, the value is the position of the unencodable
600 character found, or nil if all characters are encodable."
601 (interactive
602 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
603 (read-coding-system
604 (format "Coding-system (default %s): " default)
605 default))))
606 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
607 (if pos
608 (goto-char (1+ pos))
609 (message "All following characters are encodable by %s" coding-system))
610 pos))
611
612 (defvar last-coding-system-specified nil
613 "Most recent coding system explicitly specified by the user when asked.
614 This variable is set whenever Emacs asks the user which coding system
615 to use in order to write a file. If you set it to nil explicitly,
616 then call `write-region', then afterward this variable will be non-nil
617 only if the user was explicitly asked and specified a coding system.")
618
619 (defvar select-safe-coding-system-accept-default-p nil
620 "If non-nil, a function to control the behavior of coding system selection.
621 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
622 function `select-safe-coding-system' (which see). This variable
623 overrides that argument.")
624
625 (defun select-safe-coding-system-interactively (from to codings unsafe
626 &optional rejected default)
627 "Select interactively a coding system for the region FROM ... TO.
628 FROM can be a string, as in `write-region'.
629 CODINGS is the list of base coding systems known to be safe for this region,
630 typically obtained with `find-coding-systems-region'.
631 UNSAFE is a list of coding systems known to be unsafe for this region.
632 REJECTED is a list of coding systems which were safe but for some reason
633 were not recommended in the particular context.
634 DEFAULT is the coding system to use by default in the query."
635 ;; At first, if some defaults are unsafe, record at most 11
636 ;; problematic characters and their positions for them by turning
637 ;; (CODING ...)
638 ;; into
639 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
640 (if unsafe
641 (setq unsafe
642 (mapcar #'(lambda (coding)
643 (cons coding
644 (if (stringp from)
645 (mapcar #'(lambda (pos)
646 (cons pos (aref from pos)))
647 (unencodable-char-position
648 0 (length from) coding
649 11 from))
650 (mapcar #'(lambda (pos)
651 (cons pos (char-after pos)))
652 (unencodable-char-position
653 from to coding 11)))))
654 unsafe)))
655
656 ;; Change each safe coding system to the corresponding
657 ;; mime-charset name if it is also a coding system. Such a name
658 ;; is more friendly to users.
659 (let ((l codings)
660 mime-charset)
661 (while l
662 (setq mime-charset (coding-system-get (car l) :mime-charset))
663 (if (and mime-charset (coding-system-p mime-charset)
664 (coding-system-equal (car l) mime-charset))
665 (setcar l mime-charset))
666 (setq l (cdr l))))
667
668 ;; Don't offer variations with locking shift, which you
669 ;; basically never want.
670 (let (l)
671 (dolist (elt codings (setq codings (nreverse l)))
672 (unless (or (eq 'coding-category-iso-7-else
673 (coding-system-category elt))
674 (eq 'coding-category-iso-8-else
675 (coding-system-category elt)))
676 (push elt l))))
677
678 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
679 ;; else is available.
680 (setq codings
681 (or (delq 'raw-text
682 (delq 'emacs-mule
683 (delq 'no-conversion codings)))
684 '(raw-text emacs-mule no-conversion)))
685
686 (let ((window-configuration (current-window-configuration))
687 (bufname (buffer-name))
688 coding-system)
689 (save-excursion
690 ;; If some defaults are unsafe, make sure the offending
691 ;; buffer is displayed.
692 (when (and unsafe (not (stringp from)))
693 (pop-to-buffer bufname)
694 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
695 unsafe))))
696 ;; Then ask users to select one from CODINGS while showing
697 ;; the reason why none of the defaults are not used.
698 (with-output-to-temp-buffer "*Warning*"
699 (with-current-buffer standard-output
700 (if (and (null rejected) (null unsafe))
701 (insert "No default coding systems to try for "
702 (if (stringp from)
703 (format "string \"%s\"." from)
704 (format "buffer `%s'." bufname)))
705 (insert
706 "These default coding systems were tried to encode"
707 (if (stringp from)
708 (concat " \"" (if (> (length from) 10)
709 (concat (substring from 0 10) "...\"")
710 (concat from "\"")))
711 (format " text\nin the buffer `%s'" bufname))
712 ":\n")
713 (let ((pos (point))
714 (fill-prefix " "))
715 (dolist (x (append rejected unsafe))
716 (princ " ") (princ x))
717 (insert "\n")
718 (fill-region-as-paragraph pos (point)))
719 (when rejected
720 (insert "These safely encode the text in the buffer,
721 but are not recommended for encoding text in this context,
722 e.g., for sending an email message.\n ")
723 (dolist (x rejected)
724 (princ " ") (princ x))
725 (insert "\n"))
726 (when unsafe
727 (insert (if rejected "The other coding systems"
728 "However, each of them")
729 " encountered characters it couldn't encode:\n")
730 (dolist (coding unsafe)
731 (insert (format " %s cannot encode these:" (car coding)))
732 (let ((i 0)
733 (func1
734 #'(lambda (bufname pos)
735 (when (buffer-live-p (get-buffer bufname))
736 (pop-to-buffer bufname)
737 (goto-char pos))))
738 (func2
739 #'(lambda (bufname pos coding)
740 (when (buffer-live-p (get-buffer bufname))
741 (pop-to-buffer bufname)
742 (if (< (point) pos)
743 (goto-char pos)
744 (forward-char 1)
745 (search-unencodable-char coding)
746 (forward-char -1))))))
747 (dolist (elt (cdr coding))
748 (insert " ")
749 (if (stringp from)
750 (insert (if (< i 10) (cdr elt) "..."))
751 (if (< i 10)
752 (insert-text-button
753 (cdr elt)
754 :type 'help-xref
755 'face 'link
756 'help-echo
757 "mouse-2, RET: jump to this character"
758 'help-function func1
759 'help-args (list bufname (car elt)))
760 (insert-text-button
761 "..."
762 :type 'help-xref
763 'face 'link
764 'help-echo
765 "mouse-2, RET: next unencodable character"
766 'help-function func2
767 'help-args (list bufname (car elt)
768 (car coding)))))
769 (setq i (1+ i))))
770 (insert "\n"))
771 (insert (substitute-command-keys "\
772
773 Click on a character (or switch to this window by `\\[other-window]'\n\
774 and select the characters by RET) to jump to the place it appears,\n\
775 where `\\[universal-argument] \\[what-cursor-position]' will give information about it.\n"))))
776 (insert (substitute-command-keys "\nSelect \
777 one of the safe coding systems listed below,\n\
778 or cancel the writing with \\[keyboard-quit] and edit the buffer\n\
779 to remove or modify the problematic characters,\n\
780 or specify any other coding system (and risk losing\n\
781 the problematic characters).\n\n"))
782 (let ((pos (point))
783 (fill-prefix " "))
784 (dolist (x codings)
785 (princ " ") (princ x))
786 (insert "\n")
787 (fill-region-as-paragraph pos (point)))))
788
789 ;; Read a coding system.
790 (setq coding-system
791 (read-coding-system
792 (format "Select coding system (default %s): " default)
793 default))
794 (setq last-coding-system-specified coding-system))
795
796 (kill-buffer "*Warning*")
797 (set-window-configuration window-configuration)
798 coding-system))
799
800 (defun select-safe-coding-system (from to &optional default-coding-system
801 accept-default-p file)
802 "Ask a user to select a safe coding system from candidates.
803 The candidates of coding systems which can safely encode a text
804 between FROM and TO are shown in a popup window. Among them, the most
805 proper one is suggested as the default.
806
807 The list of `buffer-file-coding-system' of the current buffer,
808 the `default-buffer-file-coding-system', and the
809 most preferred coding system (if it corresponds to a MIME charset) is
810 treated as the default coding system list. Among them, the first one
811 that safely encodes the text is normally selected silently and
812 returned without any user interaction. See also the command
813 `prefer-coding-system'.
814
815 However, the user is queried if the chosen coding system is
816 inconsistent with what would be selected by `find-auto-coding' from
817 coding cookies &c. if the contents of the region were read from a
818 file. (That could lead to data corruption in a file subsequently
819 re-visited and edited.)
820
821 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
822 list of coding systems to be prepended to the default coding system
823 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
824 element is t, the cdr part is used as the default coding system list,
825 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
826 and the most preferred coding system are not used.
827
828 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
829 determine the acceptability of the silently selected coding system.
830 It is called with that coding system, and should return nil if it
831 should not be silently selected and thus user interaction is required.
832
833 Optional 5th arg FILE is the file name to use for this purpose.
834 That is different from `buffer-file-name' when handling `write-region'
835 \(for example).
836
837 The variable `select-safe-coding-system-accept-default-p', if
838 non-nil, overrides ACCEPT-DEFAULT-P.
839
840 Kludgy feature: if FROM is a string, the string is the target text,
841 and TO is ignored."
842 (if (and default-coding-system
843 (not (listp default-coding-system)))
844 (setq default-coding-system (list default-coding-system)))
845
846 (let ((no-other-defaults nil)
847 auto-cs)
848 (unless (or (stringp from) find-file-literally)
849 ;; Find an auto-coding that is specified for the the current
850 ;; buffer and file from the region FROM and TO.
851 (save-excursion
852 (save-restriction
853 (widen)
854 (goto-char from)
855 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
856 (- to from)))
857 (if auto-cs
858 (if (coding-system-p (car auto-cs))
859 (setq auto-cs (car auto-cs))
860 (display-warning
861 :warning
862 (format "\
863 Invalid coding system `%s' is specified
864 for the current buffer/file by the %s.
865 It is highly recommended to fix it before writing to a file."
866 (car auto-cs)
867 (if (eq (cdr auto-cs) :coding) ":coding tag"
868 (format "variable `%s'" (cdr auto-cs)))))
869 (or (yes-or-no-p "Really proceed with writing? ")
870 (error "Save aborted"))
871 (setq auto-cs nil))))))
872
873 (if (eq (car default-coding-system) t)
874 (setq no-other-defaults t
875 default-coding-system (cdr default-coding-system)))
876
877 ;; Change elements of the list to (coding . base-coding).
878 (setq default-coding-system
879 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
880 default-coding-system))
881
882 (if (and auto-cs (not no-other-defaults))
883 ;; If the file has a coding cookie, try to use it before anything
884 ;; else (i.e. before default-coding-system which will typically come
885 ;; from file-coding-system-alist).
886 (let ((base (coding-system-base auto-cs)))
887 (or (memq base '(nil undecided))
888 (rassq base default-coding-system)
889 (push (cons auto-cs base) default-coding-system))))
890
891 (unless no-other-defaults
892 ;; If buffer-file-coding-system is not nil nor undecided, append it
893 ;; to the defaults.
894 (if buffer-file-coding-system
895 (let ((base (coding-system-base buffer-file-coding-system)))
896 (or (eq base 'undecided)
897 (rassq base default-coding-system)
898 (setq default-coding-system
899 (append default-coding-system
900 (list (cons buffer-file-coding-system base)))))))
901
902 ;; If default-buffer-file-coding-system is not nil nor undecided,
903 ;; append it to the defaults.
904 (if default-buffer-file-coding-system
905 (let ((base (coding-system-base default-buffer-file-coding-system)))
906 (or (eq base 'undecided)
907 (rassq base default-coding-system)
908 (setq default-coding-system
909 (append default-coding-system
910 (list (cons default-buffer-file-coding-system
911 base)))))))
912
913 ;; If the most preferred coding system has the property mime-charset,
914 ;; append it to the defaults.
915 (let ((preferred (coding-system-priority-list t))
916 base)
917 (and (coding-system-p preferred)
918 (setq base (coding-system-base preferred))
919 (coding-system-get preferred :mime-charset)
920 (not (rassq base default-coding-system))
921 (setq default-coding-system
922 (append default-coding-system
923 (list (cons preferred base)))))))
924
925 (if select-safe-coding-system-accept-default-p
926 (setq accept-default-p select-safe-coding-system-accept-default-p))
927
928 ;; Decide the eol-type from the top of the default codings,
929 ;; buffer-file-coding-system, or
930 ;; default-buffer-file-coding-system.
931 (if default-coding-system
932 (let ((default-eol-type (coding-system-eol-type
933 (caar default-coding-system))))
934 (if (and (vectorp default-eol-type) buffer-file-coding-system)
935 (setq default-eol-type (coding-system-eol-type
936 buffer-file-coding-system)))
937 (if (and (vectorp default-eol-type) default-buffer-file-coding-system)
938 (setq default-eol-type (coding-system-eol-type
939 default-buffer-file-coding-system)))
940 (if (and default-eol-type (not (vectorp default-eol-type)))
941 (dolist (elt default-coding-system)
942 (setcar elt (coding-system-change-eol-conversion
943 (car elt) default-eol-type))))))
944
945 (let ((codings (find-coding-systems-region from to))
946 (coding-system nil)
947 (tick (if (not (stringp from)) (buffer-modified-tick)))
948 safe rejected unsafe)
949 (if (eq (car codings) 'undecided)
950 ;; Any coding system is ok.
951 (setq coding-system (caar default-coding-system))
952 ;; Reverse the list so that elements are accumulated in safe,
953 ;; rejected, and unsafe in the correct order.
954 (setq default-coding-system (nreverse default-coding-system))
955
956 ;; Classify the defaults into safe, rejected, and unsafe.
957 (dolist (elt default-coding-system)
958 (if (or (eq (car codings) 'undecided)
959 (memq (cdr elt) codings))
960 (if (and (functionp accept-default-p)
961 (not (funcall accept-default-p (cdr elt))))
962 (push (car elt) rejected)
963 (push (car elt) safe))
964 (push (car elt) unsafe)))
965 (if safe
966 (setq coding-system (car safe))))
967
968 ;; If all the defaults failed, ask a user.
969 (when (not coding-system)
970 (setq coding-system (select-safe-coding-system-interactively
971 from to codings unsafe rejected (car codings))))
972
973 ;; Check we're not inconsistent with what `coding:' spec &c would
974 ;; give when file is re-read.
975 ;; But don't do this if we explicitly ignored the cookie
976 ;; by using `find-file-literally'.
977 (when (and auto-cs
978 (not (and
979 coding-system
980 (memq (coding-system-type coding-system) '(0 5)))))
981 ;; Merge coding-system and auto-cs as far as possible.
982 (if (not coding-system)
983 (setq coding-system auto-cs)
984 (if (not auto-cs)
985 (setq auto-cs coding-system)
986 (let ((eol-type-1 (coding-system-eol-type coding-system))
987 (eol-type-2 (coding-system-eol-type auto-cs)))
988 (if (eq (coding-system-base coding-system) 'undecided)
989 (setq coding-system (coding-system-change-text-conversion
990 coding-system auto-cs))
991 (if (eq (coding-system-base auto-cs) 'undecided)
992 (setq auto-cs (coding-system-change-text-conversion
993 auto-cs coding-system))))
994 (if (vectorp eol-type-1)
995 (or (vectorp eol-type-2)
996 (setq coding-system (coding-system-change-eol-conversion
997 coding-system eol-type-2)))
998 (if (vectorp eol-type-2)
999 (setq auto-cs (coding-system-change-eol-conversion
1000 auto-cs eol-type-1)))))))
1001
1002 (if (and auto-cs
1003 ;; Don't barf if writing a compressed file, say.
1004 ;; This check perhaps isn't ideal, but is probably
1005 ;; the best thing to do.
1006 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1007 (not (coding-system-equal coding-system auto-cs)))
1008 (unless (yes-or-no-p
1009 (format "Selected encoding %s disagrees with \
1010 %s specified by file contents. Really save (else edit coding cookies \
1011 and try again)? " coding-system auto-cs))
1012 (error "Save aborted"))))
1013 (when (and tick (/= tick (buffer-modified-tick)))
1014 (error "Cancelled because the buffer was modified"))
1015 coding-system)))
1016
1017 (setq select-safe-coding-system-function 'select-safe-coding-system)
1018
1019 (defun select-message-coding-system ()
1020 "Return a coding system to encode the outgoing message of the current buffer.
1021 It at first tries the first coding system found in these variables
1022 in this order:
1023 (1) local value of `buffer-file-coding-system'
1024 (2) value of `sendmail-coding-system'
1025 (3) value of `default-sendmail-coding-system'
1026 (4) value of `default-buffer-file-coding-system'
1027 If the found coding system can't encode the current buffer,
1028 or none of them are bound to a coding system,
1029 it asks the user to select a proper coding system."
1030 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1031 buffer-file-coding-system)
1032 sendmail-coding-system
1033 default-sendmail-coding-system
1034 default-buffer-file-coding-system)))
1035 (if (eq coding 'no-conversion)
1036 ;; We should never use no-conversion for outgoing mail.
1037 (setq coding nil))
1038 (if (fboundp select-safe-coding-system-function)
1039 (funcall select-safe-coding-system-function
1040 (point-min) (point-max) coding
1041 (function (lambda (x) (coding-system-get x :mime-charset))))
1042 coding)))
1043 \f
1044 ;;; Language support stuff.
1045
1046 (defvar language-info-alist nil
1047 "Alist of language environment definitions.
1048 Each element looks like:
1049 (LANGUAGE-NAME . ((KEY . INFO) ...))
1050 where LANGUAGE-NAME is a string, the name of the language environment,
1051 KEY is a symbol denoting the kind of information, and
1052 INFO is the data associated with KEY.
1053 Meaningful values for KEY include
1054
1055 documentation value is documentation of what this language environment
1056 is meant for, and how to use it.
1057 charset value is a list of the character sets mainly used
1058 by this language environment.
1059 sample-text value is an expression which is evalled to generate
1060 a line of text written using characters appropriate
1061 for this language environment.
1062 setup-function value is a function to call to switch to this
1063 language environment.
1064 exit-function value is a function to call to leave this
1065 language environment.
1066 coding-system value is a list of coding systems that are good
1067 for saving text written in this language environment.
1068 This list serves as suggestions to the user;
1069 in effect, as a kind of documentation.
1070 coding-priority value is a list of coding systems for this language
1071 environment, in order of decreasing priority.
1072 This is used to set up the coding system priority
1073 list when you switch to this language environment.
1074 nonascii-translation
1075 value is a charset of dimension one to use for
1076 converting a unibyte character to multibyte
1077 and vice versa.
1078 input-method value is a default input method for this language
1079 environment.
1080 features value is a list of features requested in this
1081 language environment.
1082 ctext-non-standard-encodings
1083 value is a list of non-standard encoding
1084 names used in extended segments of CTEXT.
1085 See the variable
1086 `ctext-non-standard-encodings' for more
1087 detail.
1088
1089 The following keys take effect only when multibyte characters are
1090 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1091 is nil.
1092
1093 unibyte-display value is a coding system to encode characters
1094 for the terminal. Characters in the range
1095 of 160 to 255 display not as octal escapes,
1096 but as non-ASCII characters in this language
1097 environment.")
1098
1099 (defun get-language-info (lang-env key)
1100 "Return information listed under KEY for language environment LANG-ENV.
1101 KEY is a symbol denoting the kind of information.
1102 For a list of useful values for KEY and their meanings,
1103 see `language-info-alist'."
1104 (if (symbolp lang-env)
1105 (setq lang-env (symbol-name lang-env)))
1106 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1107 (if lang-slot
1108 (cdr (assq key (cdr lang-slot))))))
1109
1110 (defun set-language-info (lang-env key info)
1111 "Modify part of the definition of language environment LANG-ENV.
1112 Specifically, this stores the information INFO under KEY
1113 in the definition of this language environment.
1114 KEY is a symbol denoting the kind of information.
1115 INFO is the value for that information.
1116
1117 For a list of useful values for KEY and their meanings,
1118 see `language-info-alist'."
1119 (if (symbolp lang-env)
1120 (setq lang-env (symbol-name lang-env)))
1121 (set-language-info-internal lang-env key info)
1122 (if (equal lang-env current-language-environment)
1123 (cond ((eq key 'coding-priority)
1124 (set-language-environment-coding-systems lang-env)
1125 (set-language-environment-charset lang-env))
1126 ((eq key 'input-method)
1127 (set-language-environment-input-method lang-env))
1128 ((eq key 'nonascii-translation)
1129 (set-language-environment-nonascii-translation lang-env))
1130 ((eq key 'charset)
1131 (set-language-environment-charset lang-env))
1132 ((and (not default-enable-multibyte-characters)
1133 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1134 (set-language-environment-unibyte lang-env)))))
1135
1136 (defun set-language-info-internal (lang-env key info)
1137 "Internal use only.
1138 Arguments are the same as `set-language-info'."
1139 (let (lang-slot key-slot)
1140 (setq lang-slot (assoc lang-env language-info-alist))
1141 (if (null lang-slot) ; If no slot for the language, add it.
1142 (setq lang-slot (list lang-env)
1143 language-info-alist (cons lang-slot language-info-alist)))
1144 (setq key-slot (assq key lang-slot))
1145 (if (null key-slot) ; If no slot for the key, add it.
1146 (progn
1147 (setq key-slot (list key))
1148 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1149 (setcdr key-slot (purecopy info))
1150 ;; Update the custom-type of `current-language-environment'.
1151 (put 'current-language-environment 'custom-type
1152 (cons 'choice (mapcar
1153 (lambda (lang)
1154 (list 'const lang))
1155 (sort (mapcar 'car language-info-alist) 'string<))))))
1156
1157 (defun set-language-info-alist (lang-env alist &optional parents)
1158 "Store ALIST as the definition of language environment LANG-ENV.
1159 ALIST is an alist of KEY and INFO values. See the documentation of
1160 `language-info-alist' for the meanings of KEY and INFO.
1161
1162 Optional arg PARENTS is a list of parent menu names; it specifies
1163 where to put this language environment in the
1164 Describe Language Environment and Set Language Environment menus.
1165 For example, (\"European\") means to put this language environment
1166 in the European submenu in each of those two menus."
1167 (if (symbolp lang-env)
1168 (setq lang-env (symbol-name lang-env)))
1169 (let ((describe-map describe-language-environment-map)
1170 (setup-map setup-language-environment-map))
1171 (if parents
1172 (let ((l parents)
1173 map parent-symbol parent prompt)
1174 (while l
1175 (if (symbolp (setq parent-symbol (car l)))
1176 (setq parent (symbol-name parent))
1177 (setq parent parent-symbol parent-symbol (intern parent)))
1178 (setq map (lookup-key describe-map (vector parent-symbol)))
1179 ;; This prompt string is for define-prefix-command, so
1180 ;; that the map it creates will be suitable for a menu.
1181 (or map (setq prompt (format "%s Environment" parent)))
1182 (if (not map)
1183 (progn
1184 (setq map (intern (format "describe-%s-environment-map"
1185 (downcase parent))))
1186 (define-prefix-command map nil prompt)
1187 (define-key-after describe-map (vector parent-symbol)
1188 (cons parent map) t)))
1189 (setq describe-map (symbol-value map))
1190 (setq map (lookup-key setup-map (vector parent-symbol)))
1191 (if (not map)
1192 (progn
1193 (setq map (intern (format "setup-%s-environment-map"
1194 (downcase parent))))
1195 (define-prefix-command map nil prompt)
1196 (define-key-after setup-map (vector parent-symbol)
1197 (cons parent map) t)))
1198 (setq setup-map (symbol-value map))
1199 (setq l (cdr l)))))
1200
1201 ;; Set up menu items for this language env.
1202 (let ((doc (assq 'documentation alist)))
1203 (when doc
1204 (define-key-after describe-map (vector (intern lang-env))
1205 (cons lang-env 'describe-specified-language-support) t)))
1206 (define-key-after setup-map (vector (intern lang-env))
1207 (cons lang-env 'setup-specified-language-environment) t)
1208
1209 (dolist (elt alist)
1210 (set-language-info-internal lang-env (car elt) (cdr elt)))
1211
1212 (if (equal lang-env current-language-environment)
1213 (set-language-environment lang-env))))
1214
1215 (defun read-language-name (key prompt &optional default)
1216 "Read a language environment name which has information for KEY.
1217 If KEY is nil, read any language environment.
1218 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1219 This returns a language environment name as a string."
1220 (let* ((completion-ignore-case t)
1221 (name (completing-read prompt
1222 language-info-alist
1223 (and key
1224 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1225 t nil nil default)))
1226 (if (and (> (length name) 0)
1227 (or (not key)
1228 (get-language-info name key)))
1229 name)))
1230 \f
1231 ;;; Multilingual input methods.
1232 (defgroup leim nil
1233 "LEIM: Libraries of Emacs Input Methods."
1234 :group 'mule)
1235
1236 (defconst leim-list-file-name "leim-list.el"
1237 "Name of LEIM list file.
1238 This file contains a list of libraries of Emacs input methods (LEIM)
1239 in the format of Lisp expression for registering each input method.
1240 Emacs loads this file at startup time.")
1241
1242 (defvar leim-list-header (format
1243 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1244 ;;
1245 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1246 ;; methods in the same directory as this file. Loading this file
1247 ;; registers all the input methods in Emacs.
1248 ;;
1249 ;; Each entry has the form:
1250 ;; (register-input-method
1251 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1252 ;; TITLE DESCRIPTION
1253 ;; ARG ...)
1254 ;; See the function `register-input-method' for the meanings of the arguments.
1255 ;;
1256 ;; If this directory is included in load-path, Emacs automatically
1257 ;; loads this file at startup time.
1258
1259 "
1260 leim-list-file-name)
1261 "Header to be inserted in LEIM list file.")
1262
1263 (defvar leim-list-entry-regexp "^(register-input-method"
1264 "Regexp matching head of each entry in LEIM list file.
1265 See also the variable `leim-list-header'.")
1266
1267 (defvar update-leim-list-functions
1268 '(quail-update-leim-list-file)
1269 "List of functions to call to update LEIM list file.
1270 Each function is called with one arg, LEIM directory name.")
1271
1272 (defun update-leim-list-file (&rest dirs)
1273 "Update LEIM list file in directories DIRS."
1274 (let ((functions update-leim-list-functions))
1275 (while functions
1276 (apply (car functions) dirs)
1277 (setq functions (cdr functions)))))
1278
1279 (defvar current-input-method nil
1280 "The current input method for multilingual text.
1281 If nil, that means no input method is activated now.")
1282 (make-variable-buffer-local 'current-input-method)
1283 (put 'current-input-method 'permanent-local t)
1284
1285 (defvar current-input-method-title nil
1286 "Title string of the current input method shown in mode line.")
1287 (make-variable-buffer-local 'current-input-method-title)
1288 (put 'current-input-method-title 'permanent-local t)
1289
1290 (defcustom default-input-method nil
1291 "Default input method for multilingual text (a string).
1292 This is the input method activated automatically by the command
1293 `toggle-input-method' (\\[toggle-input-method])."
1294 :link '(custom-manual "(emacs)Input Methods")
1295 :group 'mule
1296 :type '(choice (const nil) (string
1297 :completion-ignore-case t
1298 :complete-function widget-string-complete
1299 :completion-alist input-method-alist
1300 :prompt-history input-method-history))
1301 :set-after '(current-language-environment))
1302
1303 (put 'input-method-function 'permanent-local t)
1304
1305 (defvar input-method-history nil
1306 "History list of input methods read from the minibuffer.
1307
1308 Maximum length of the history list is determined by the value
1309 of `history-length', which see.")
1310 (make-variable-buffer-local 'input-method-history)
1311 (put 'input-method-history 'permanent-local t)
1312
1313 (defvar inactivate-current-input-method-function nil
1314 "Function to call for inactivating the current input method.
1315 Every input method should set this to an appropriate value when activated.
1316 This function is called with no argument.
1317
1318 This function should never change the value of `current-input-method'.
1319 It is set to nil by the function `inactivate-input-method'.")
1320 (make-variable-buffer-local 'inactivate-current-input-method-function)
1321 (put 'inactivate-current-input-method-function 'permanent-local t)
1322
1323 (defvar describe-current-input-method-function nil
1324 "Function to call for describing the current input method.
1325 This function is called with no argument.")
1326 (make-variable-buffer-local 'describe-current-input-method-function)
1327 (put 'describe-current-input-method-function 'permanent-local t)
1328
1329 (defvar input-method-alist nil
1330 "Alist of input method names vs how to use them.
1331 Each element has the form:
1332 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1333 See the function `register-input-method' for the meanings of the elements.")
1334
1335 (defun register-input-method (input-method lang-env &rest args)
1336 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1337
1338 INPUT-METHOD and LANG-ENV are symbols or strings.
1339 ACTIVATE-FUNC is a function to call to activate this method.
1340 TITLE is a string to show in the mode line when this method is active.
1341 DESCRIPTION is a string describing this method and what it is good for.
1342 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1343 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1344
1345 This function is mainly used in the file \"leim-list.el\" which is
1346 created at Emacs build time, registering all Quail input methods
1347 contained in the Emacs distribution.
1348
1349 In case you want to register a new Quail input method by yourself, be
1350 careful to use the same input method title as given in the third
1351 parameter of `quail-define-package'. (If the values are different, the
1352 string specified in this function takes precedence.)
1353
1354 The commands `describe-input-method' and `list-input-methods' need
1355 these duplicated values to show some information about input methods
1356 without loading the relevant Quail packages.
1357 \n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
1358 (if (symbolp lang-env)
1359 (setq lang-env (symbol-name lang-env)))
1360 (if (symbolp input-method)
1361 (setq input-method (symbol-name input-method)))
1362 (let ((info (cons lang-env args))
1363 (slot (assoc input-method input-method-alist)))
1364 (if slot
1365 (setcdr slot info)
1366 (setq slot (cons input-method info))
1367 (setq input-method-alist (cons slot input-method-alist)))))
1368
1369 (defun read-input-method-name (prompt &optional default inhibit-null)
1370 "Read a name of input method from a minibuffer prompting with PROMPT.
1371 If DEFAULT is non-nil, use that as the default,
1372 and substitute it into PROMPT at the first `%s'.
1373 If INHIBIT-NULL is non-nil, null input signals an error.
1374
1375 The return value is a string."
1376 (if default
1377 (setq prompt (format prompt default)))
1378 (let* ((completion-ignore-case t)
1379 ;; As it is quite normal to change input method in the
1380 ;; minibuffer, we must enable it even if
1381 ;; enable-recursive-minibuffers is currently nil.
1382 (enable-recursive-minibuffers t)
1383 ;; This binding is necessary because input-method-history is
1384 ;; buffer local.
1385 (input-method (completing-read prompt input-method-alist
1386 nil t nil 'input-method-history
1387 default)))
1388 (if (and input-method (symbolp input-method))
1389 (setq input-method (symbol-name input-method)))
1390 (if (> (length input-method) 0)
1391 input-method
1392 (if inhibit-null
1393 (error "No valid input method is specified")))))
1394
1395 (defun activate-input-method (input-method)
1396 "Switch to input method INPUT-METHOD for the current buffer.
1397 If some other input method is already active, turn it off first.
1398 If INPUT-METHOD is nil, deactivate any current input method."
1399 (if (and input-method (symbolp input-method))
1400 (setq input-method (symbol-name input-method)))
1401 (if (and current-input-method
1402 (not (string= current-input-method input-method)))
1403 (inactivate-input-method))
1404 (unless (or current-input-method (null input-method))
1405 (let ((slot (assoc input-method input-method-alist)))
1406 (if (null slot)
1407 (error "Can't activate input method `%s'" input-method))
1408 (setq current-input-method-title nil)
1409 (let ((func (nth 2 slot)))
1410 (if (functionp func)
1411 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1412 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1413 (progn
1414 (require (cdr func))
1415 (apply (car func) input-method (nthcdr 5 slot)))
1416 (error "Can't activate input method `%s'" input-method))))
1417 (setq current-input-method input-method)
1418 (or (stringp current-input-method-title)
1419 (setq current-input-method-title (nth 3 slot)))
1420 (unwind-protect
1421 (run-hooks 'input-method-activate-hook)
1422 (force-mode-line-update)))))
1423
1424 (defun inactivate-input-method ()
1425 "Turn off the current input method."
1426 (when current-input-method
1427 (if input-method-history
1428 (unless (string= current-input-method (car input-method-history))
1429 (setq input-method-history
1430 (cons current-input-method
1431 (delete current-input-method input-method-history))))
1432 (setq input-method-history (list current-input-method)))
1433 (unwind-protect
1434 (progn
1435 (setq input-method-function nil
1436 current-input-method-title nil)
1437 (funcall inactivate-current-input-method-function))
1438 (unwind-protect
1439 (run-hooks 'input-method-inactivate-hook)
1440 (setq current-input-method nil)
1441 (force-mode-line-update)))))
1442
1443 (defun set-input-method (input-method &optional interactive)
1444 "Select and activate input method INPUT-METHOD for the current buffer.
1445 This also sets the default input method to the one you specify.
1446 If INPUT-METHOD is nil, this function turns off the input method, and
1447 also causes you to be prompted for a name of an input method the next
1448 time you invoke \\[toggle-input-method].
1449 When called interactively, the optional arg INTERACTIVE is non-nil,
1450 which marks the variable `default-input-method' as set for Custom buffers.
1451
1452 To deactivate the input method interactively, use \\[toggle-input-method].
1453 To deactivate it programmatically, use `inactivate-input-method'."
1454 (interactive
1455 (let* ((default (or (car input-method-history) default-input-method)))
1456 (list (read-input-method-name
1457 (if default "Select input method (default %s): " "Select input method: ")
1458 default t)
1459 t)))
1460 (activate-input-method input-method)
1461 (setq default-input-method input-method)
1462 (when interactive
1463 (customize-mark-as-set 'default-input-method))
1464 default-input-method)
1465
1466 (defvar toggle-input-method-active nil
1467 "Non-nil inside `toggle-input-method'.")
1468
1469 (defun toggle-input-method (&optional arg interactive)
1470 "Enable or disable multilingual text input method for the current buffer.
1471 Only one input method can be enabled at any time in a given buffer.
1472
1473 The normal action is to enable an input method if none was
1474 enabled, and disable the current one otherwise. Which input method
1475 to enable can be determined in various ways--either the one most
1476 recently used, or the one specified by `default-input-method', or
1477 as a last resort by reading the name of an input method in the
1478 minibuffer.
1479
1480 With a prefix argument, read an input method name with the minibuffer
1481 and enable that one. The default is the most recent input method specified
1482 \(not including the currently active input method, if any).
1483
1484 When called interactively, the optional arg INTERACTIVE is non-nil,
1485 which marks the variable `default-input-method' as set for Custom buffers."
1486
1487 (interactive "P\np")
1488 (if toggle-input-method-active
1489 (error "Recursive use of `toggle-input-method'"))
1490 (if (and current-input-method (not arg))
1491 (inactivate-input-method)
1492 (let ((toggle-input-method-active t)
1493 (default (or (car input-method-history) default-input-method)))
1494 (if (and arg default (equal current-input-method default)
1495 (> (length input-method-history) 1))
1496 (setq default (nth 1 input-method-history)))
1497 (activate-input-method
1498 (if (or arg (not default))
1499 (progn
1500 (read-input-method-name
1501 (if default "Input method (default %s): " "Input method: " )
1502 default t))
1503 default))
1504 (unless default-input-method
1505 (prog1
1506 (setq default-input-method current-input-method)
1507 (when interactive
1508 (customize-mark-as-set 'default-input-method)))))))
1509
1510 (eval-when-compile (autoload 'help-buffer "help-mode"))
1511
1512 (defun describe-input-method (input-method)
1513 "Describe input method INPUT-METHOD."
1514 (interactive
1515 (list (read-input-method-name
1516 "Describe input method (default current choice): ")))
1517 (if (and input-method (symbolp input-method))
1518 (setq input-method (symbol-name input-method)))
1519 (help-setup-xref (list #'describe-input-method
1520 (or input-method current-input-method))
1521 (interactive-p))
1522
1523 (if (null input-method)
1524 (describe-current-input-method)
1525 (let ((current current-input-method))
1526 (condition-case nil
1527 (progn
1528 (save-excursion
1529 (activate-input-method input-method)
1530 (describe-current-input-method))
1531 (activate-input-method current))
1532 (error
1533 (activate-input-method current)
1534 (help-setup-xref (list #'describe-input-method input-method)
1535 (interactive-p))
1536 (with-output-to-temp-buffer (help-buffer)
1537 (let ((elt (assoc input-method input-method-alist)))
1538 (princ (format
1539 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1540 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1541
1542 (defun describe-current-input-method ()
1543 "Describe the input method currently in use.
1544 This is a subroutine for `describe-input-method'."
1545 (if current-input-method
1546 (if (and (symbolp describe-current-input-method-function)
1547 (fboundp describe-current-input-method-function))
1548 (funcall describe-current-input-method-function)
1549 (message "No way to describe the current input method `%s'"
1550 current-input-method)
1551 (ding))
1552 (error "No input method is activated now")))
1553
1554 (defun read-multilingual-string (prompt &optional initial-input input-method)
1555 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1556 The input method selected last time is activated in minibuffer.
1557 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1558 initially.
1559 Optional 3rd argument INPUT-METHOD specifies the input method
1560 to be activated instead of the one selected last time. It is a symbol
1561 or a string."
1562 (setq input-method
1563 (or input-method
1564 current-input-method
1565 default-input-method
1566 (read-input-method-name "Input method: " nil t)))
1567 (if (and input-method (symbolp input-method))
1568 (setq input-method (symbol-name input-method)))
1569 (let ((prev-input-method current-input-method))
1570 (unwind-protect
1571 (progn
1572 (activate-input-method input-method)
1573 (read-string prompt initial-input nil nil t))
1574 (activate-input-method prev-input-method))))
1575
1576 ;; Variables to control behavior of input methods. All input methods
1577 ;; should react to these variables.
1578
1579 (defcustom input-method-verbose-flag 'default
1580 "A flag to control extra guidance given by input methods.
1581 The value should be nil, t, `complex-only', or `default'.
1582
1583 The extra guidance is done by showing list of available keys in echo
1584 area. When you use the input method in the minibuffer, the guidance
1585 is shown at the bottom short window (split from the existing window).
1586
1587 If the value is t, extra guidance is always given, if the value is
1588 nil, extra guidance is always suppressed.
1589
1590 If the value is `complex-only', only complex input methods such as
1591 `chinese-py' and `japanese' give extra guidance.
1592
1593 If the value is `default', complex input methods always give extra
1594 guidance, but simple input methods give it only when you are not in
1595 the minibuffer.
1596
1597 See also the variable `input-method-highlight-flag'."
1598 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1599 (const complex-only) (const default))
1600 :group 'mule)
1601
1602 (defcustom input-method-highlight-flag t
1603 "If this flag is non-nil, input methods highlight partially-entered text.
1604 For instance, while you are in the middle of a Quail input method sequence,
1605 the text inserted so far is temporarily underlined.
1606 The underlining goes away when you finish or abort the input method sequence.
1607 See also the variable `input-method-verbose-flag'."
1608 :type 'boolean
1609 :group 'mule)
1610
1611 (defcustom input-method-activate-hook nil
1612 "Normal hook run just after an input method is activated.
1613
1614 The variable `current-input-method' keeps the input method name
1615 just activated."
1616 :type 'hook
1617 :group 'mule)
1618
1619 (defcustom input-method-inactivate-hook nil
1620 "Normal hook run just after an input method is inactivated.
1621
1622 The variable `current-input-method' still keeps the input method name
1623 just inactivated."
1624 :type 'hook
1625 :group 'mule)
1626
1627 (defcustom input-method-after-insert-chunk-hook nil
1628 "Normal hook run just after an input method insert some chunk of text."
1629 :type 'hook
1630 :group 'mule)
1631
1632 (defvar input-method-exit-on-first-char nil
1633 "This flag controls when an input method returns.
1634 Usually, the input method does not return while there's a possibility
1635 that it may find a different translation if a user types another key.
1636 But, if this flag is non-nil, the input method returns as soon as
1637 the current key sequence gets long enough to have some valid translation.")
1638
1639 (defcustom input-method-use-echo-area nil
1640 "This flag controls how an input method shows an intermediate key sequence.
1641 Usually, the input method inserts the intermediate key sequence,
1642 or candidate translations corresponding to the sequence,
1643 at point in the current buffer.
1644 But, if this flag is non-nil, it displays them in echo area instead."
1645 :type 'hook
1646 :group 'mule)
1647
1648 (defvar input-method-exit-on-invalid-key nil
1649 "This flag controls the behavior of an input method on invalid key input.
1650 Usually, when a user types a key which doesn't start any character
1651 handled by the input method, the key is handled by turning off the
1652 input method temporarily. After that key, the input method is re-enabled.
1653 But, if this flag is non-nil, the input method is never back on.")
1654
1655 \f
1656 (defcustom set-language-environment-hook nil
1657 "Normal hook run after some language environment is set.
1658
1659 When you set some hook function here, that effect usually should not
1660 be inherited to another language environment. So, you had better set
1661 another function in `exit-language-environment-hook' (which see) to
1662 cancel the effect."
1663 :type 'hook
1664 :group 'mule)
1665
1666 (defcustom exit-language-environment-hook nil
1667 "Normal hook run after exiting from some language environment.
1668 When this hook is run, the variable `current-language-environment'
1669 is still bound to the language environment being exited.
1670
1671 This hook is mainly used for canceling the effect of
1672 `set-language-environment-hook' (which see)."
1673 :type 'hook
1674 :group 'mule)
1675
1676 (put 'setup-specified-language-environment 'apropos-inhibit t)
1677
1678 (defun setup-specified-language-environment ()
1679 "Switch to a specified language environment."
1680 (interactive)
1681 (let (language-name)
1682 (if (and (symbolp last-command-event)
1683 (or (not (eq last-command-event 'Default))
1684 (setq last-command-event 'English))
1685 (setq language-name (symbol-name last-command-event)))
1686 (prog1
1687 (set-language-environment language-name)
1688 (customize-mark-as-set 'current-language-environment))
1689 (error "Bogus calling sequence"))))
1690
1691 (defcustom current-language-environment "English"
1692 "The last language environment specified with `set-language-environment'.
1693 This variable should be set only with \\[customize], which is equivalent
1694 to using the function `set-language-environment'."
1695 :link '(custom-manual "(emacs)Language Environments")
1696 :set (lambda (symbol value) (set-language-environment value))
1697 :get (lambda (x)
1698 (or (car-safe (assoc-string
1699 (if (symbolp current-language-environment)
1700 (symbol-name current-language-environment)
1701 current-language-environment)
1702 language-info-alist t))
1703 "English"))
1704 ;; custom type will be updated with `set-language-info'.
1705 :type (if language-info-alist
1706 (cons 'choice (mapcar
1707 (lambda (lang)
1708 (list 'const lang))
1709 (sort (mapcar 'car language-info-alist) 'string<)))
1710 'string)
1711 :initialize 'custom-initialize-default
1712 :group 'mule)
1713
1714 (defun reset-language-environment ()
1715 "Reset multilingual environment of Emacs to the default status.
1716
1717 The default status is as follows:
1718
1719 The default value of `buffer-file-coding-system' is nil.
1720 The default coding system for process I/O is nil.
1721 The default value for the command `set-terminal-coding-system' is nil.
1722 The default value for the command `set-keyboard-coding-system' is nil.
1723
1724 The order of priorities of coding systems are as follows:
1725 utf-8
1726 iso-2022-7bit
1727 iso-latin-1
1728 iso-2022-7bit-lock
1729 iso-2022-8bit-ss2
1730 emacs-mule
1731 raw-text"
1732 (interactive)
1733 ;; This function formerly set default-enable-multibyte-characters to t,
1734 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1735
1736 (set-coding-system-priority
1737 'utf-8
1738 'iso-2022-7bit
1739 'iso-latin-1
1740 'iso-2022-7bit-lock
1741 'iso-2022-8bit-ss2
1742 'emacs-mule
1743 'raw-text)
1744
1745 (set-default-coding-systems nil)
1746 (setq default-sendmail-coding-system 'iso-latin-1)
1747 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1748 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1749 (setq default-file-name-coding-system 'iso-latin-1)
1750 ;; Preserve eol-type from existing default-process-coding-systems.
1751 ;; On non-unix-like systems in particular, these may have been set
1752 ;; carefully by the user, or by the startup code, to deal with the
1753 ;; users shell appropriately, so should not be altered by changing
1754 ;; language environment.
1755 (let ((output-coding
1756 ;; When bootstrapping, coding-systems are not defined yet, so
1757 ;; we need to catch the error from check-coding-system.
1758 (condition-case nil
1759 (coding-system-change-text-conversion
1760 (car default-process-coding-system) 'undecided)
1761 (coding-system-error 'undecided)))
1762 (input-coding
1763 (condition-case nil
1764 (coding-system-change-text-conversion
1765 (cdr default-process-coding-system) 'iso-latin-1)
1766 (coding-system-error 'iso-latin-1))))
1767 (setq default-process-coding-system
1768 (cons output-coding input-coding)))
1769
1770 ;; Don't alter the terminal and keyboard coding systems here.
1771 ;; The terminal still supports the same coding system
1772 ;; that it supported a minute ago.
1773 ;; (set-terminal-coding-system-internal nil)
1774 ;; (set-keyboard-coding-system-internal nil)
1775
1776 ;; Back in Emacs-20, it was necessary to provide some fallback implicit
1777 ;; conversion, because almost no packages handled coding-system issues.
1778 ;; Nowadays it'd just paper over bugs.
1779 ;; (set-unibyte-charset 'iso-8859-1)
1780 )
1781
1782 (reset-language-environment)
1783
1784 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system display)
1785 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1786 (let ((coding (get-language-info language-name 'unibyte-display)))
1787 (if (and coding
1788 (or (not coding-system)
1789 (coding-system-equal coding coding-system)))
1790 (standard-display-european-internal)
1791 ;; The following 2 lines undo the 8-bit display that we set up
1792 ;; in standard-display-european-internal, which see. This is in
1793 ;; case the user has used standard-display-european earlier in
1794 ;; this session. (The MS-DOS port doesn't use that setup, so it
1795 ;; doesn't need to undo it.)
1796 (when standard-display-table
1797 (dotimes (i 128)
1798 (aset standard-display-table (+ i 128) nil))))
1799 (or (eq window-system 'pc)
1800 (set-terminal-coding-system (or coding-system coding) display))))
1801
1802 (defun set-language-environment (language-name)
1803 "Set up multi-lingual environment for using LANGUAGE-NAME.
1804 This sets the coding system priority and the default input method
1805 and sometimes other things. LANGUAGE-NAME should be a string
1806 which is the name of a language environment. For example, \"Latin-1\"
1807 specifies the character set for the major languages of Western Europe."
1808 (interactive (list (read-language-name
1809 nil
1810 "Set language environment (default English): ")))
1811 (if language-name
1812 (if (symbolp language-name)
1813 (setq language-name (symbol-name language-name)))
1814 (setq language-name "English"))
1815 (let ((slot (assoc-string language-name language-info-alist t)))
1816 (unless slot
1817 (error "Language environment not defined: %S" language-name))
1818 (setq language-name (car slot)))
1819 (if current-language-environment
1820 (let ((func (get-language-info current-language-environment
1821 'exit-function)))
1822 (run-hooks 'exit-language-environment-hook)
1823 (if (functionp func) (funcall func))))
1824
1825 (reset-language-environment)
1826 ;; The features might set up coding systems.
1827 (let ((required-features (get-language-info language-name 'features)))
1828 (while required-features
1829 (require (car required-features))
1830 (setq required-features (cdr required-features))))
1831
1832 (setq current-language-environment language-name)
1833
1834 (set-language-environment-coding-systems language-name)
1835 (set-language-environment-input-method language-name)
1836 (set-language-environment-nonascii-translation language-name)
1837 (set-language-environment-charset language-name)
1838 ;; Unibyte setups if necessary.
1839 (unless default-enable-multibyte-characters
1840 (set-language-environment-unibyte language-name))
1841
1842 (let ((func (get-language-info language-name 'setup-function)))
1843 (if (functionp func)
1844 (funcall func)))
1845
1846 (setq current-iso639-language
1847 (get-language-info language-name 'iso639-language))
1848
1849 (run-hooks 'set-language-environment-hook)
1850 (force-mode-line-update t))
1851
1852 (define-widget 'charset 'symbol
1853 "An Emacs charset."
1854 :tag "Charset"
1855 :complete-function (lambda ()
1856 (interactive)
1857 (lisp-complete-symbol 'charsetp))
1858 :completion-ignore-case t
1859 :value 'ascii
1860 :validate (lambda (widget)
1861 (unless (charsetp (widget-value widget))
1862 (widget-put widget :error (format "Invalid charset: %S"
1863 (widget-value widget)))
1864 widget))
1865 :prompt-history 'charset-history)
1866
1867 (defcustom language-info-custom-alist nil
1868 "Customizations of language environment parameters.
1869 Value is an alist with elements like those of `language-info-alist'.
1870 These are used to set values in `language-info-alist' which replace
1871 the defaults. A typical use is replacing the default input method for
1872 the environment. Use \\[describe-language-environment] to find the environment's settings.
1873
1874 This option is intended for use at startup. Removing items doesn't
1875 remove them from the language info until you next restart Emacs.
1876
1877 Setting this variable directly does not take effect. See
1878 `set-language-info-alist' for use in programs."
1879 :group 'mule
1880 :version "23.1"
1881 :set (lambda (s v)
1882 (custom-set-default s v)
1883 ;; Can't do this before language environments are set up.
1884 (when v
1885 ;; modify language-info-alist
1886 (dolist (elt v)
1887 (set-language-info-alist (car elt) (cdr elt)))
1888 ;; re-set the environment in case its parameters changed
1889 (set-language-environment current-language-environment)))
1890 :type `(alist
1891 :key-type (string :tag "Language environment"
1892 :completion-ignore-case t
1893 :complete-function widget-string-complete
1894 :completion-alist language-info-alist)
1895 :value-type
1896 (alist :key-type symbol
1897 :options ((documentation string)
1898 (charset (repeat charset))
1899 (sample-text string)
1900 (setup-function function)
1901 (exit-function function)
1902 (coding-system (repeat coding-system))
1903 (coding-priority (repeat coding-system))
1904 (nonascii-translation charset)
1905 (input-method
1906 (string
1907 :completion-ignore-case t
1908 :complete-function widget-string-complete
1909 :completion-alist input-method-alist
1910 :prompt-history input-method-history))
1911 (features (repeat symbol))
1912 (unibyte-display coding-system)))))
1913
1914 (defun standard-display-european-internal ()
1915 ;; Actually set up direct output of non-ASCII characters.
1916 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1917 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1918 ;; the native font, and codes 160 and 146 stand for something very
1919 ;; different there.
1920 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1921 (progn
1922 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1923 (unless (and (eq window-system 'x)
1924 ;; XFree86 4 has fixed the fonts.
1925 (string= "The XFree86 Project, Inc" (x-server-vendor))
1926 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1927 ?3))
1928 ;; Make non-line-break space display as a plain space.
1929 (aset standard-display-table 160 [32]))
1930 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1931 ;; don't contain a character at that position. Map it to the ASCII
1932 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1933 ;; U+2019, normally from the windows-1252 character set. XFree 4
1934 ;; fonts probably have the appropriate glyph at this position,
1935 ;; so they could use standard-display-8bit. It's better to use a
1936 ;; proper windows-1252 coding system. --fx]
1937 (aset standard-display-table 146 [39]))))
1938
1939 (defun set-language-environment-coding-systems (language-name)
1940 "Do various coding system setups for language environment LANGUAGE-NAME."
1941 (let* ((priority (get-language-info language-name 'coding-priority))
1942 (default-coding (car priority))
1943 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1944 (when priority
1945 (set-default-coding-systems
1946 (if (memq eol-type '(0 1 2 unix dos mac))
1947 (coding-system-change-eol-conversion default-coding eol-type)
1948 default-coding))
1949 (setq default-sendmail-coding-system default-coding)
1950 (apply 'set-coding-system-priority priority))))
1951
1952 (defun set-language-environment-input-method (language-name)
1953 "Do various input method setups for language environment LANGUAGE-NAME."
1954 (let ((input-method (get-language-info language-name 'input-method)))
1955 (when input-method
1956 (setq default-input-method input-method)
1957 (if input-method-history
1958 (setq input-method-history
1959 (cons input-method
1960 (delete input-method input-method-history)))))))
1961
1962 (defun set-language-environment-nonascii-translation (language-name)
1963 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1964 ;; Note: For DOS, we assumed that the charset cpXXX is already
1965 ;; defined.
1966 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1967 (if (eq window-system 'pc)
1968 (setq nonascii (intern "cp%d" dos-codepage)))
1969 (or (and (charsetp nonascii)
1970 (get-charset-property nonascii :ascii-compatible-p))
1971 (setq nonascii 'iso-8859-1))
1972 ;; Back in Emacs-20, it was necessary to provide some fallback implicit
1973 ;; conversion, because almost no packages handled coding-system issues.
1974 ;; Nowadays it'd just paper over bugs.
1975 ;; (set-unibyte-charset nonascii)
1976 ))
1977
1978 (defun set-language-environment-charset (language-name)
1979 "Do various charset setups for language environment LANGUAGE-NAME."
1980 ;; Put higher priorities to such charsets that are supported by the
1981 ;; coding systems of higher priorities in this environment.
1982 (let ((charsets (get-language-info language-name 'charset)))
1983 (dolist (coding (get-language-info language-name 'coding-priority))
1984 (setq charsets (append charsets (coding-system-charset-list coding))))
1985 (if charsets
1986 (apply 'set-charset-priority charsets))))
1987
1988 (defun set-language-environment-unibyte (language-name)
1989 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1990 (set-display-table-and-terminal-coding-system language-name))
1991
1992 (defsubst princ-list (&rest args)
1993 "Print all arguments with `princ', then print \"\\n\"."
1994 (while args (princ (car args)) (setq args (cdr args)))
1995 (princ "\n"))
1996
1997 (put 'describe-specified-language-support 'apropos-inhibit t)
1998
1999 ;; Print language-specific information such as input methods,
2000 ;; charsets, and coding systems. This function is intended to be
2001 ;; called from the menu:
2002 ;; [menu-bar mule describe-language-environment LANGUAGE]
2003 ;; and should not run it by `M-x describe-current-input-method-function'.
2004 (defun describe-specified-language-support ()
2005 "Describe how Emacs supports the specified language environment."
2006 (interactive)
2007 (let (language-name)
2008 (if (not (and (symbolp last-command-event)
2009 (or (not (eq last-command-event 'Default))
2010 (setq last-command-event 'English))
2011 (setq language-name (symbol-name last-command-event))))
2012 (error "Bogus calling sequence"))
2013 (describe-language-environment language-name)))
2014
2015 (defun describe-language-environment (language-name)
2016 "Describe how Emacs supports language environment LANGUAGE-NAME."
2017 (interactive
2018 (list (read-language-name
2019 'documentation
2020 "Describe language environment (default current choice): ")))
2021 (if (null language-name)
2022 (setq language-name current-language-environment))
2023 (if (or (null language-name)
2024 (null (get-language-info language-name 'documentation)))
2025 (error "No documentation for the specified language"))
2026 (if (symbolp language-name)
2027 (setq language-name (symbol-name language-name)))
2028 (dolist (feature (get-language-info language-name 'features))
2029 (require feature))
2030 (let ((doc (get-language-info language-name 'documentation)))
2031 (help-setup-xref (list #'describe-language-environment language-name)
2032 (interactive-p))
2033 (with-output-to-temp-buffer (help-buffer)
2034 (save-excursion
2035 (set-buffer standard-output)
2036 (insert language-name " language environment\n\n")
2037 (if (stringp doc)
2038 (insert doc "\n\n"))
2039 (condition-case nil
2040 (let ((str (eval (get-language-info language-name 'sample-text))))
2041 (if (stringp str)
2042 (insert "Sample text:\n " str "\n\n")))
2043 (error nil))
2044 (let ((input-method (get-language-info language-name 'input-method))
2045 (l (copy-sequence input-method-alist))
2046 (first t))
2047 (when (and input-method
2048 (setq input-method (assoc input-method l)))
2049 (insert "Input methods (default " (car input-method) ")\n")
2050 (setq l (cons input-method (delete input-method l))
2051 first nil))
2052 (dolist (elt l)
2053 (when (or (eq input-method elt)
2054 (eq t (compare-strings language-name nil nil
2055 (nth 1 elt) nil nil t)))
2056 (when first
2057 (insert "Input methods:\n")
2058 (setq first nil))
2059 (insert " " (car elt))
2060 (search-backward (car elt))
2061 (help-xref-button 0 'help-input-method (car elt))
2062 (goto-char (point-max))
2063 (insert " (\""
2064 (if (stringp (nth 3 elt)) (nth 3 elt) (car (nth 3 elt)))
2065 "\" in mode line)\n")))
2066 (or first
2067 (insert "\n")))
2068 (insert "Character sets:\n")
2069 (let ((l (get-language-info language-name 'charset)))
2070 (if (null l)
2071 (insert " nothing specific to " language-name "\n")
2072 (while l
2073 (insert " " (symbol-name (car l)))
2074 (search-backward (symbol-name (car l)))
2075 (help-xref-button 0 'help-character-set (car l))
2076 (goto-char (point-max))
2077 (insert ": " (charset-description (car l)) "\n")
2078 (setq l (cdr l)))))
2079 (insert "\n")
2080 (insert "Coding systems:\n")
2081 (let ((l (get-language-info language-name 'coding-system)))
2082 (if (null l)
2083 (insert " nothing specific to " language-name "\n")
2084 (while l
2085 (insert " " (symbol-name (car l)))
2086 (search-backward (symbol-name (car l)))
2087 (help-xref-button 0 'help-coding-system (car l))
2088 (goto-char (point-max))
2089 (insert " (`"
2090 (coding-system-mnemonic (car l))
2091 "' in mode line):\n\t"
2092 (coding-system-doc-string (car l))
2093 "\n")
2094 (let ((aliases (coding-system-aliases (car l))))
2095 (when aliases
2096 (insert "\t(alias:")
2097 (while aliases
2098 (insert " " (symbol-name (car aliases)))
2099 (setq aliases (cdr aliases)))
2100 (insert ")\n")))
2101 (setq l (cdr l)))))))))
2102 \f
2103 ;;; Locales.
2104
2105 (defvar locale-translation-file-name nil
2106 "File name for the system's file of locale-name aliases, or nil if none.")
2107
2108 ;; The following definitions might as well be marked as constants and
2109 ;; purecopied, since they're normally used on startup, and probably
2110 ;; should reflect the facilities of the base Emacs.
2111 (defconst locale-language-names
2112 (purecopy
2113 '(
2114 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2115 ;; as specified in the Single Unix Spec, Version 2.
2116 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2117 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2118 ;; see Internet RFC 2165 (1997-06) and
2119 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2120 ;; TERRITORY is a country code taken from ISO 3166
2121 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2122 ;; CODESET and MODIFIER are implementation-dependent.
2123
2124 ;; jasonr comments: MS Windows uses three letter codes for
2125 ;; languages instead of the two letter ISO codes that POSIX
2126 ;; uses. In most cases the first two letters are the same, so
2127 ;; most of the regexps in locale-language-names work. Japanese
2128 ;; and Chinese are exceptions, which are listed in the
2129 ;; non-standard section at the bottom of locale-language-names.
2130
2131 ("aa_DJ" . "Latin-1") ; Afar
2132 ("aa" . "UTF-8")
2133 ;; ab Abkhazian
2134 ("af" . "Latin-1") ; Afrikaans
2135 ("am" "Ethiopic" utf-8) ; Amharic
2136 ("an" . "Latin-9") ; Aragonese
2137 ; ar Arabic glibc uses 8859-6
2138 ; as Assamese
2139 ; ay Aymara
2140 ("az" . "UTF-8") ; Azerbaijani
2141 ; ba Bashkir
2142 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2143 ("bg" "Bulgarian" cp1251) ; Bulgarian
2144 ; bh Bihari
2145 ; bi Bislama
2146 ("bn" . "UTF-8") ; Bengali, Bangla
2147 ("bo" . "Tibetan")
2148 ("br" . "Latin-1") ; Breton
2149 ("bs" . "Latin-2") ; Bosnian
2150 ("byn" . "UTF-8") ; Bilin; Blin
2151 ("ca" . "Latin-1") ; Catalan
2152 ; co Corsican
2153 ("cs" "Czech" iso-8859-2)
2154 ("cy" "Welsh" iso-8859-14)
2155 ("da" . "Latin-1") ; Danish
2156 ("de" "German" iso-8859-1)
2157 ; dv Divehi
2158 ; dz Bhutani
2159 ("el" "Greek" iso-8859-7)
2160 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2161 ;; That's actually what the GNU locales define, modulo things like
2162 ;; en_IN -- fx.
2163 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2164 ("en" "English" iso-8859-1) ; English
2165 ("eo" . "Esperanto") ; Esperanto
2166 ("es" "Spanish" iso-8859-1)
2167 ("et" . "Latin-1") ; Estonian
2168 ("eu" . "Latin-1") ; Basque
2169 ("fa" . "UTF-8") ; Persian
2170 ("fi" . "Latin-1") ; Finnish
2171 ("fj" . "Latin-1") ; Fiji
2172 ("fo" . "Latin-1") ; Faroese
2173 ("fr" "French" iso-8859-1) ; French
2174 ("fy" . "Latin-1") ; Frisian
2175 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2176 ("gd" . "Latin-9") ; Scots Gaelic
2177 ("gez" "Ethiopic" utf-8) ; Geez
2178 ("gl" . "Latin-1") ; Gallegan; Galician
2179 ; gn Guarani
2180 ("gu" . "UTF-8") ; Gujarati
2181 ("gv" . "Latin-1") ; Manx Gaelic
2182 ; ha Hausa
2183 ("he" "Hebrew" iso-8859-8)
2184 ("hi" "Devanagari" utf-8) ; Hindi
2185 ("hr" "Croatian" iso-8859-2) ; Croatian
2186 ("hu" . "Latin-2") ; Hungarian
2187 ; hy Armenian
2188 ; ia Interlingua
2189 ("id" . "Latin-1") ; Indonesian
2190 ; ie Interlingue
2191 ; ik Inupiak
2192 ("is" . "Latin-1") ; Icelandic
2193 ("it" "Italian" iso-8859-1) ; Italian
2194 ; iu Inuktitut
2195 ("iw" "Hebrew" iso-8859-8)
2196 ("ja" "Japanese" euc-jp)
2197 ; jw Javanese
2198 ("ka" "Georgian" georgian-ps) ; Georgian
2199 ; kk Kazakh
2200 ("kl" . "Latin-1") ; Greenlandic
2201 ; km Cambodian
2202 ("kn" "Kannada" utf-8)
2203 ("ko" "Korean" euc-kr)
2204 ; ks Kashmiri
2205 ; ku Kurdish
2206 ("kw" . "Latin-1") ; Cornish
2207 ; ky Kirghiz
2208 ("la" . "Latin-1") ; Latin
2209 ("lb" . "Latin-1") ; Luxemburgish
2210 ("lg" . "Laint-6") ; Ganda
2211 ; ln Lingala
2212 ("lo" "Lao" utf-8) ; Laothian
2213 ("lt" "Lithuanian" iso-8859-13)
2214 ("lv" . "Latvian") ; Latvian, Lettish
2215 ; mg Malagasy
2216 ("mi" . "Latin-7") ; Maori
2217 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2218 ("ml" "Malayalam" utf-8)
2219 ("mn" . "UTF-8") ; Mongolian
2220 ; mo Moldavian
2221 ("mr" "Devanagari" utf-8) ; Marathi
2222 ("ms" . "Latin-1") ; Malay
2223 ("mt" . "Latin-3") ; Maltese
2224 ; my Burmese
2225 ; na Nauru
2226 ("nb" . "Latin-1") ; Norwegian
2227 ("ne" "Devanagari" utf-8) ; Nepali
2228 ("nl" "Dutch" iso-8859-1)
2229 ("no" . "Latin-1") ; Norwegian
2230 ("oc" . "Latin-1") ; Occitan
2231 ("om_ET" . "UTF-8") ; (Afan) Oromo
2232 ("om" . "Latin-1") ; (Afan) Oromo
2233 ; or Oriya
2234 ("pa" . "UTF-8") ; Punjabi
2235 ("pl" . "Latin-2") ; Polish
2236 ; ps Pashto, Pushto
2237 ("pt" . "Latin-1") ; Portuguese
2238 ; qu Quechua
2239 ("rm" . "Latin-1") ; Rhaeto-Romanic
2240 ; rn Kirundi
2241 ("ro" "Romanian" iso-8859-2)
2242 ("ru_RU" "Russian" iso-8859-5)
2243 ("ru_UA" "Russian" koi8-u)
2244 ; rw Kinyarwanda
2245 ("sa" . "Devanagari") ; Sanskrit
2246 ; sd Sindhi
2247 ("se" . "UTF-8") ; Northern Sami
2248 ; sg Sangho
2249 ("sh" . "Latin-2") ; Serbo-Croatian
2250 ; si Sinhalese
2251 ("sid" . "UTF-8") ; Sidamo
2252 ("sk" "Slovak" iso-8859-2)
2253 ("sl" "Slovenian" iso-8859-2)
2254 ; sm Samoan
2255 ; sn Shona
2256 ("so_ET" "UTF-8") ; Somali
2257 ("so" "Latin-1") ; Somali
2258 ("sq" . "Latin-1") ; Albanian
2259 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2260 ; ss Siswati
2261 ("st" . "Latin-1") ; Sesotho
2262 ; su Sundanese
2263 ("sv" "Swedish" iso-8859-1) ; Swedish
2264 ("sw" . "Latin-1") ; Swahili
2265 ("ta" "Tamil" utf-8)
2266 ("te" . "UTF-8") ; Telugu
2267 ("tg" "Tajik" koi8-t)
2268 ("th" "Thai" tis-620)
2269 ("ti" "Ethiopic" utf-8) ; Tigrinya
2270 ("tig_ER" . "UTF-8") ; Tigre
2271 ; tk Turkmen
2272 ("tl" . "Latin-1") ; Tagalog
2273 ; tn Setswana
2274 ; to Tonga
2275 ("tr" "Turkish" iso-8859-9)
2276 ; ts Tsonga
2277 ("tt" . "UTF-8") ; Tatar
2278 ; tw Twi
2279 ; ug Uighur
2280 ("uk" "Ukrainian" koi8-u)
2281 ("ur" . "UTF-8") ; Urdu
2282 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2283 ("uz" . "Latin-1") ; Uzbek
2284 ("vi" "Vietnamese" utf-8)
2285 ; vo Volapuk
2286 ("wa" . "Latin-1") ; Walloon
2287 ; wo Wolof
2288 ("xh" . "Latin-1") ; Xhosa
2289 ("yi" . "Windows-1255") ; Yiddish
2290 ; yo Yoruba
2291 ; za Zhuang
2292 ("zh_HK" . "Chinese-Big5")
2293 ; zh_HK/BIG5-HKSCS \
2294 ("zh_TW" . "Chinese-Big5")
2295 ("zh_CN.GB2312" "Chinese-GB")
2296 ("zh_CN.GBK" "Chinese-GBK")
2297 ("zh_CN.GB18030" "Chinese-GB18030")
2298 ("zh_CN.UTF-8" . "Chinese-GBK")
2299 ("zh_CN" . "Chinese-GB")
2300 ("zh" . "Chinese-GB")
2301 ("zu" . "Latin-1") ; Zulu
2302
2303 ;; ISO standard locales
2304 ("c$" . "ASCII")
2305 ("posix$" . "ASCII")
2306
2307 ;; The "IPA" Emacs language environment does not correspond
2308 ;; to any ISO 639 code, so let it stand for itself.
2309 ("ipa$" . "IPA")
2310
2311 ;; Nonstandard or obsolete language codes
2312 ("cz" . "Czech") ; e.g. Solaris 2.6
2313 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2314 ("iw" . "Hebrew") ; e.g. X11R6.4
2315 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2316 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2317 ("jp" . "Japanese") ; e.g. MS Windows
2318 ("chs" . "Chinese-GBK") ; MS Windows Chinese Simplified
2319 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2320 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2321 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2322 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2323 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2324 ))
2325 "Alist of locale regexps vs the corresponding languages and coding systems.
2326 Each element has this form:
2327 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2328 The first element whose LOCALE-REGEXP matches the start of a
2329 downcased locale specifies the LANG-ENV \(language environment)
2330 and CODING-SYSTEM corresponding to that locale. If there is no
2331 appropriate language environment, the element may have this form:
2332 \(LOCALE-REGEXP . LANG-ENV)
2333 In this case, LANG-ENV is one of generic language environments for an
2334 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2335
2336 (defconst locale-charset-language-names
2337 (purecopy
2338 '((".*8859[-_]?1\\>" . "Latin-1")
2339 (".*8859[-_]?2\\>" . "Latin-2")
2340 (".*8859[-_]?3\\>" . "Latin-3")
2341 (".*8859[-_]?4\\>" . "Latin-4")
2342 (".*8859[-_]?9\\>" . "Latin-5")
2343 (".*8859[-_]?14\\>" . "Latin-8")
2344 (".*8859[-_]?15\\>" . "Latin-9")
2345 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2346 ;; utf-8@euro exists, so put this last. (@euro really specifies
2347 ;; the currency, rather than the charset.)
2348 (".*@euro\\>" . "Latin-9")))
2349 "List of pairs of locale regexps and charset language names.
2350 The first element whose locale regexp matches the start of a downcased locale
2351 specifies the language name whose charset corresponds to that locale.
2352 This language name is used if the locale is not listed in
2353 `locale-language-names'.")
2354
2355 (defconst locale-preferred-coding-systems
2356 (purecopy
2357 '((".*8859[-_]?1\\>" . iso-8859-1)
2358 (".*8859[-_]?2\\>" . iso-8859-2)
2359 (".*8859[-_]?3\\>" . iso-8859-3)
2360 (".*8859[-_]?4\\>" . iso-8859-4)
2361 (".*8859[-_]?9\\>" . iso-8859-9)
2362 (".*8859[-_]?14\\>" . iso-8859-14)
2363 (".*8859[-_]?15\\>" . iso-8859-15)
2364 (".*utf\\(?:-?8\\)?" . utf-8)
2365 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2366 ;; specifies the currency, rather than the charset.)
2367 (".*@euro" . iso-8859-15)
2368 ("koi8-?r" . koi8-r)
2369 ("koi8-?u" . koi8-u)
2370 ("tcvn" . tcvn)
2371 ("big5[-_]?hkscs" . big5-hkscs)
2372 ("big5" . big5)
2373 ("euc-?tw" . euc-tw)
2374 ("euc-?cn" .euc-cn)
2375 ("gb2312" . gb2312)
2376 ("gbk" . gbk)
2377 ("gb18030" . gb18030)
2378 ("ja.*[._]euc" . japanese-iso-8bit)
2379 ("ja.*[._]jis7" . iso-2022-jp)
2380 ("ja.*[._]pck" . japanese-shift-jis)
2381 ("ja.*[._]sjis" . japanese-shift-jis)
2382 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2383 ))
2384 "List of pairs of locale regexps and preferred coding systems.
2385 The first element whose locale regexp matches the start of a downcased locale
2386 specifies the coding system to prefer when using that locale.
2387 This coding system is used if the locale specifies a specific charset.")
2388
2389 (defun locale-name-match (key alist)
2390 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2391 Return the value corresponding to the first regexp that matches the
2392 start of KEY, or nil if there is no match."
2393 (let (element)
2394 (while (and alist (not element))
2395 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2396 (setq element (car alist)))
2397 (setq alist (cdr alist)))
2398 (cdr element)))
2399
2400 (defun locale-charset-match-p (charset1 charset2)
2401 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2402 Matching is done ignoring case and any hyphens and underscores in the
2403 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2404 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2405 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2406 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2407
2408 (defvar locale-charset-alist nil
2409 "Coding system alist keyed on locale-style charset name.
2410 Used by `locale-charset-to-coding-system'.")
2411
2412 (defun locale-charset-to-coding-system (charset)
2413 "Find coding system corresponding to CHARSET.
2414 CHARSET is any sort of non-Emacs charset name, such as might be used
2415 in a locale codeset, or elsewhere. It is matched to a coding system
2416 first by case-insensitive lookup in `locale-charset-alist'. Then
2417 matches are looked for in the coding system list, treating case and
2418 the characters `-' and `_' as insignificant. The coding system base
2419 is returned. Thus, for instance, if charset \"ISO8859-2\",
2420 `iso-latin-2' is returned."
2421 (or (car (assoc-string charset locale-charset-alist t))
2422 (let ((cs coding-system-alist)
2423 c)
2424 (while (and (not c) cs)
2425 (if (locale-charset-match-p charset (caar cs))
2426 (setq c (intern (caar cs)))
2427 (pop cs)))
2428 (if c (coding-system-base c)))))
2429
2430 ;; Fixme: This ought to deal with the territory part of the locale
2431 ;; too, for setting things such as calendar holidays, ps-print paper
2432 ;; size, spelling dictionary.
2433
2434 (defun locale-translate (locale)
2435 "Expand LOCALE according to `locale-translation-file-name', if possible.
2436 For example, translate \"swedish\" into \"sv_SE.ISO8859-1\"."
2437 (if locale-translation-file-name
2438 (with-temp-buffer
2439 (set-buffer-multibyte nil)
2440 (insert-file-contents locale-translation-file-name)
2441 (if (re-search-forward
2442 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2443 (buffer-substring (point) (line-end-position))
2444 locale))
2445 locale))
2446
2447 (defun set-locale-environment (&optional locale-name frame)
2448 "Set up multi-lingual environment for using LOCALE-NAME.
2449 This sets the language environment, the coding system priority,
2450 the default input method and sometimes other things.
2451
2452 LOCALE-NAME should be a string which is the name of a locale supported
2453 by the system. Often it is of the form xx_XX.CODE, where xx is a
2454 language, XX is a country, and CODE specifies a character set and
2455 coding system. For example, the locale name \"ja_JP.EUC\" might name
2456 a locale for Japanese in Japan using the `japanese-iso-8bit'
2457 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2458 or `@cyrillic'.
2459
2460 If LOCALE-NAME is nil, its value is taken from the environment
2461 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2462
2463 The locale names supported by your system can typically be found in a
2464 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2465 will be translated according to the table specified by
2466 `locale-translation-file-name'.
2467
2468 If FRAME is non-nil, only set the keyboard coding system and the
2469 terminal coding system for the terminal of that frame, and don't
2470 touch session-global parameters like the language environment.
2471
2472 See also `locale-charset-language-names', `locale-language-names',
2473 `locale-preferred-coding-systems' and `locale-coding-system'."
2474 (interactive "sSet environment for locale: ")
2475
2476 ;; Do this at runtime for the sake of binaries possibly transported
2477 ;; to a system without X.
2478 (setq locale-translation-file-name
2479 (let ((files
2480 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2481 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2482 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2483 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2484 ;;
2485 ;; The following name appears after the X-related names above,
2486 ;; since the X-related names are what X actually uses.
2487 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2488 )))
2489 (while (and files (not (file-exists-p (car files))))
2490 (setq files (cdr files)))
2491 (car files)))
2492
2493 (let ((locale locale-name))
2494
2495 (unless locale
2496 ;; Use the first of these three environment variables
2497 ;; that has a nonempty value.
2498 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2499 (while (and vars
2500 (= 0 (length locale))) ; nil or empty string
2501 (setq locale (getenv (pop vars) frame)))))
2502
2503 (unless locale
2504 ;; The two tests are kept separate so the byte-compiler sees
2505 ;; that mac-get-preference is only called after checking its existence.
2506 (when (fboundp 'mac-get-preference)
2507 (setq locale (mac-get-preference "AppleLocale"))
2508 (unless locale
2509 (let ((languages (mac-get-preference "AppleLanguages")))
2510 (unless (= (length languages) 0) ; nil or empty vector
2511 (setq locale (aref languages 0)))))))
2512 (unless (or locale (not (boundp 'mac-system-locale)))
2513 (setq locale mac-system-locale))
2514
2515 (when locale
2516 (setq locale (locale-translate locale))
2517
2518 ;; Leave the system locales alone if the caller did not specify
2519 ;; an explicit locale name, as their defaults are set from
2520 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2521 ;; want to set them to the same value as LC_CTYPE.
2522 (when locale-name
2523 (setq system-messages-locale locale)
2524 (setq system-time-locale locale)))
2525
2526 (setq woman-locale
2527 (or system-messages-locale
2528 (let ((msglocale (getenv "LC_MESSAGES" frame)))
2529 (if (zerop (length msglocale))
2530 locale
2531 (locale-translate msglocale)))))
2532
2533 (when locale
2534 (setq locale (downcase locale))
2535
2536 (let ((language-name
2537 (locale-name-match locale locale-language-names))
2538 (charset-language-name
2539 (locale-name-match locale locale-charset-language-names))
2540 (default-eol-type (coding-system-eol-type
2541 default-buffer-file-coding-system))
2542 (coding-system
2543 (or (locale-name-match locale locale-preferred-coding-systems)
2544 (when locale
2545 (if (string-match "\\.\\([^@]+\\)" locale)
2546 (locale-charset-to-coding-system
2547 (match-string 1 locale))))
2548 (and (eq system-type 'macos) mac-system-coding-system))))
2549
2550 (if (consp language-name)
2551 ;; locale-language-names specify both lang-env and coding.
2552 ;; But, what specified in locale-preferred-coding-systems
2553 ;; has higher priority.
2554 (setq coding-system (or coding-system
2555 (nth 1 language-name))
2556 language-name (car language-name))
2557 ;; Otherwise, if locale is not listed in locale-language-names,
2558 ;; use what listed in locale-charset-language-names.
2559 (if (not language-name)
2560 (setq language-name charset-language-name)))
2561
2562 ;; If a specific EOL conversion was specified in the default
2563 ;; buffer-file-coding-system, preserve it in the coding system
2564 ;; we will be using from now on.
2565 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2566 coding-system
2567 (coding-system-p coding-system))
2568 (setq coding-system (coding-system-change-eol-conversion
2569 coding-system default-eol-type)))
2570
2571 (when language-name
2572
2573 ;; Set up for this character set. This is now the right way
2574 ;; to do it for both unibyte and multibyte modes.
2575 (unless frame
2576 (set-language-environment language-name))
2577
2578 ;; If default-enable-multibyte-characters is nil,
2579 ;; we are using single-byte characters,
2580 ;; so the display table and terminal coding system are irrelevant.
2581 (when default-enable-multibyte-characters
2582 (set-display-table-and-terminal-coding-system
2583 language-name coding-system frame))
2584
2585 ;; Set the `keyboard-coding-system' if appropriate (tty
2586 ;; only). At least X and MS Windows can generate
2587 ;; multilingual input.
2588 ;; XXX This was disabled unless `window-system', but that
2589 ;; leads to buggy behaviour when a tty frame is opened
2590 ;; later. Setting the keyboard coding system has no adverse
2591 ;; effect on X, so let's do it anyway. -- Lorentey
2592 (let ((kcs (or coding-system
2593 (car (get-language-info language-name
2594 'coding-system)))))
2595 (if kcs (set-keyboard-coding-system kcs frame)))
2596
2597 (unless frame
2598 (setq locale-coding-system
2599 (car (get-language-info language-name 'coding-priority)))))
2600
2601 (when (and (not frame)
2602 coding-system
2603 (not (coding-system-equal coding-system
2604 locale-coding-system)))
2605 (prefer-coding-system coding-system)
2606 ;; Fixme: perhaps prefer-coding-system should set this too.
2607 ;; But it's not the time to do such a fundamental change.
2608 (setq default-sendmail-coding-system coding-system)
2609 (setq locale-coding-system coding-system))))
2610
2611 ;; On Windows, override locale-coding-system,
2612 ;; default-file-name-coding-system, keyboard-coding-system,
2613 ;; terminal-coding-system with system codepage.
2614 (when (boundp 'w32-ansi-code-page)
2615 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2616 (when (coding-system-p code-page-coding)
2617 (unless frame (setq locale-coding-system code-page-coding))
2618 (set-keyboard-coding-system code-page-coding frame)
2619 (set-terminal-coding-system code-page-coding frame)
2620 ;; Set default-file-name-coding-system last, so that Emacs
2621 ;; doesn't try to use cpNNNN when it defines keyboard and
2622 ;; terminal encoding. That's because the above two lines
2623 ;; will want to load code-pages.el, where cpNNNN are
2624 ;; defined; if default-file-name-coding-system were set to
2625 ;; cpNNNN while these two lines run, Emacs will want to use
2626 ;; it for encoding the file name it wants to load. And that
2627 ;; will fail, since cpNNNN is not yet usable until
2628 ;; code-pages.el finishes loading.
2629 (setq default-file-name-coding-system code-page-coding))))
2630
2631 (when (eq system-type 'darwin)
2632 ;; On Darwin, file names are always encoded in utf-8, no matter
2633 ;; the locale.
2634 (setq default-file-name-coding-system 'utf-8)
2635 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2636 ;; the locale.
2637 (when (and (null window-system)
2638 (equal (getenv "TERM_PROGRAM" frame) "Apple_Terminal"))
2639 (set-terminal-coding-system 'utf-8)
2640 (set-keyboard-coding-system 'utf-8)))
2641
2642 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2643 ;; (See comments in Flocale_info.)
2644 (unless frame
2645 (let ((locale locale)
2646 (paper (locale-info 'paper)))
2647 (if paper
2648 ;; This will always be null at the time of writing.
2649 (cond
2650 ((equal paper '(216 279))
2651 (setq ps-paper-type 'letter))
2652 ((equal paper '(210 297))
2653 (setq ps-paper-type 'a4)))
2654 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2655 (while (and vars (= 0 (length locale)))
2656 (setq locale (getenv (pop vars) frame))))
2657 (when locale
2658 ;; As of glibc 2.2.5, these are the only US Letter locales,
2659 ;; and the rest are A4.
2660 (setq ps-paper-type
2661 (or (locale-name-match locale '(("c$" . letter)
2662 ("posix$" . letter)
2663 (".._us" . letter)
2664 (".._pr" . letter)
2665 (".._ca" . letter)
2666 ("enu$" . letter) ; Windows
2667 ("esu$" . letter)
2668 ("enc$" . letter)
2669 ("frc$" . letter)))
2670 'a4)))))))
2671 nil)
2672 \f
2673 ;;; Character property
2674
2675 ;; Each element has the form (PROP . TABLE).
2676 ;; PROP is a symbol representing a character property.
2677 ;; TABLE is a char-table containing the property value for each character.
2678 ;; TABLE may be a name of file to load to build a char-table.
2679 ;; Don't modify this variable directly but use `define-char-code-property'.
2680
2681 (defvar char-code-property-alist nil
2682 "Alist of character property name vs char-table containing property values.
2683 Internal use only.")
2684
2685 (put 'char-code-property-table 'char-table-extra-slots 5)
2686
2687 (defun define-char-code-property (name table &optional docstring)
2688 "Define NAME as a character code property given by TABLE.
2689 TABLE is a char-table of purpose `char-code-property-table' with
2690 these extra slots:
2691 1st: NAME.
2692 2nd: Function to call to get a property value of a character.
2693 It is called with three arguments CHAR, VAL, and TABLE, where
2694 CHAR is a character, VAL is the value of (aref TABLE CHAR).
2695 3rd: Function to call to put a property value of a character.
2696 It is called with the same arguments as above.
2697 4th: Function to call to get a description string of a property value.
2698 It is called with one argument VALUE, a property value.
2699 5th: Data used by the above functions.
2700
2701 TABLE may be a name of file to load to build a char-table. The
2702 file should contain a call of `define-char-code-property' with a
2703 char-table of the above format as the argument TABLE.
2704
2705 TABLE may also be nil, in which case no property value is pre-assigned.
2706
2707 Optional 3rd argment DOCSTRING is a documentation string of the property.
2708
2709 See also the documentation of `get-char-code-property' and
2710 `put-char-code-property'."
2711 (or (symbolp name)
2712 (error "Not a symbol: %s" name))
2713 (if (char-table-p table)
2714 (or (and (eq (char-table-subtype table) 'char-code-property-table)
2715 (eq (char-table-extra-slot table 0) name))
2716 (error "Invalid char-table: %s" table))
2717 (or (stringp table)
2718 (error "Not a char-table nor a file name: %s" table)))
2719 (let ((slot (assq name char-code-property-alist)))
2720 (if slot
2721 (setcdr slot table)
2722 (setq char-code-property-alist
2723 (cons (cons name table) char-code-property-alist))))
2724 (put name 'char-code-property-documentation docstring))
2725
2726 (defvar char-code-property-table
2727 (make-char-table 'char-code-property-table)
2728 "Char-table containing a property list of each character code.
2729 This table is used for properties not listed in `char-code-property-alist'.
2730 See also the documentation of `get-char-code-property' and
2731 `put-char-code-property'.")
2732
2733 (defun get-char-code-property (char propname)
2734 "Return the value of CHAR's PROPNAME property."
2735 (let ((slot (assq propname char-code-property-alist)))
2736 (if slot
2737 (let (table value func)
2738 (if (stringp (cdr slot))
2739 (load (cdr slot)))
2740 (setq table (cdr slot)
2741 value (aref table char)
2742 func (char-table-extra-slot table 1))
2743 (if (functionp func)
2744 (setq value (funcall func char value table)))
2745 value)
2746 (plist-get (aref char-code-property-table char) propname))))
2747
2748 (defun put-char-code-property (char propname value)
2749 "Store CHAR's PROPNAME property with VALUE.
2750 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2751 (let ((slot (assq propname char-code-property-alist)))
2752 (if slot
2753 (let (table func)
2754 (if (stringp (cdr slot))
2755 (load (cdr slot)))
2756 (setq table (cdr slot)
2757 func (char-table-extra-slot table 2))
2758 (if (functionp func)
2759 (funcall func char value table)
2760 (aset table char value)))
2761 (let* ((plist (aref char-code-property-table char))
2762 (x (plist-put plist propname value)))
2763 (or (eq x plist)
2764 (aset char-code-property-table char x))))
2765 value))
2766
2767 (defun char-code-property-description (prop value)
2768 "Return a description string of character property PROP's value VALUE.
2769 If there's no description string for VALUE, return nil."
2770 (let ((slot (assq prop char-code-property-alist)))
2771 (if slot
2772 (let (table func)
2773 (if (stringp (cdr slot))
2774 (load (cdr slot)))
2775 (setq table (cdr slot)
2776 func (char-table-extra-slot table 3))
2777 (if (functionp func)
2778 (funcall func value))))))
2779
2780 \f
2781 ;; Pretty description of encoded string
2782
2783 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2784 (defvar iso-2022-control-alist
2785 '((?\x1b . "ESC")
2786 (?\x0e . "SO")
2787 (?\x0f . "SI")
2788 (?\x8e . "SS2")
2789 (?\x8f . "SS3")
2790 (?\x9b . "CSI")))
2791
2792 (defun encoded-string-description (str coding-system)
2793 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2794 (setq str (string-as-unibyte str))
2795 (mapconcat
2796 (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
2797 ;; Try to get a pretty description for ISO 2022 escape sequences.
2798 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2799 (format "#x%02X" x))))
2800 (function (lambda (x) (format "#x%02X" x))))
2801 str " "))
2802
2803 (defun encode-coding-char (char coding-system)
2804 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2805 If CODING-SYSTEM can't safely encode CHAR, return nil."
2806 (let ((str1 (string-as-multibyte (string char)))
2807 (str2 (string-as-multibyte (string char char)))
2808 enc1 enc2 i1 i2)
2809 (when (memq (coding-system-base coding-system)
2810 (find-coding-systems-string str1))
2811 ;; We must find the encoded string of CHAR. But, just encoding
2812 ;; CHAR will put extra control sequences (usually to designate
2813 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2814 ;; To exclude such tailing bytes, we at first encode one-char
2815 ;; string and two-char string, then check how many bytes at the
2816 ;; tail of both encoded strings are the same.
2817
2818 (setq enc1 (encode-coding-string str1 coding-system)
2819 i1 (length enc1)
2820 enc2 (encode-coding-string str2 coding-system)
2821 i2 (length enc2))
2822 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2823 (setq i1 (1- i1) i2 (1- i2)))
2824
2825 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2826 ;; and they are the extra control sequences at the tail to
2827 ;; exclude.
2828 (substring enc2 0 i2))))
2829
2830 ;; Backwards compatibility. These might be better with :init-value t,
2831 ;; but that breaks loadup.
2832 (define-minor-mode unify-8859-on-encoding-mode
2833 "Obsolete."
2834 :group 'mule
2835 :global t)
2836 (define-minor-mode unify-8859-on-decoding-mode
2837 "Obsolete."
2838 :group 'mule
2839 :global t)
2840
2841 (defvar nonascii-insert-offset 0 "This variable is obsolete.")
2842 (defvar nonascii-translation-table nil "This variable is obsolete.")
2843
2844 (defun ucs-insert (arg)
2845 "Insert a character of the given Unicode code point.
2846 Interactively, prompts for a hex string giving the code."
2847 (interactive "sUnicode (hex): ")
2848 (or (integerp arg)
2849 (setq arg (string-to-number arg 16)))
2850 (if (or (< arg 0) (> arg #x10FFFF))
2851 (error "Not a Unicode character code: 0x%X" arg))
2852 (insert-and-inherit arg))
2853
2854
2855 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2856 ;;; mule-cmds.el ends here