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