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