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