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