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