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