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