1 ;;; mule-cmds.el --- commands for mulitilingual environment
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
7 ;; Keywords: mule, multilingual
9 ;; This file is part of GNU Emacs.
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)
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.
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.
30 (eval-when-compile (defvar dos-codepage))
32 ;;; MULE related key bindings and menus.
34 (defvar mule-keymap (make-sparse-keymap)
35 "Keymap for Mule (Multilingual environment) specific commands.")
37 ;; Keep "C-x C-m ..." for mule specific commands.
38 (define-key ctl-x-map "\C-m" mule-keymap)
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)
52 (defvar mule-menu-keymap
53 (make-sparse-keymap "Mule (Multilingual Environment)")
54 "Keymap for Mule (Multilingual environment) menu specific commands.")
56 (defvar describe-language-environment-map
57 (make-sparse-keymap "Describe Language Environment"))
59 (defvar setup-language-environment-map
60 (make-sparse-keymap "Set Language Environment"))
62 (defvar set-coding-system-map
63 (make-sparse-keymap "Set Coding System"))
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]
75 (define-key-after mule-menu-keymap [toggle-input-method]
76 '(menu-item "Toggle Input Method" toggle-input-method)
78 (define-key-after mule-menu-keymap [set-input-method]
79 '(menu-item "Select Input Method..." set-input-method)
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]
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")
95 (define-key-after mule-menu-keymap [separator-coding-system]
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")
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")
107 (define-key-after mule-menu-keymap [describe-coding-system]
108 '(menu-item "Describe Coding System..." describe-coding-system)
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")
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")
122 (define-key-after set-coding-system-map [separator-1]
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")
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")
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")
138 (define-key-after set-coding-system-map [separator-2]
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")
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")
151 (define-key-after set-coding-system-map [separator-3]
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")
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")
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")
172 (define-key setup-language-environment-map
173 [Default] '(menu-item "Default" setup-specified-language-environment))
175 (define-key describe-language-environment-map
176 [Default] '(menu-item "Default" describe-specified-language-support))
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)
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)
191 ;;; Mule related hyperlinks.
192 (defconst help-xref-mule-regexp-template
193 (purecopy (concat "\\(\\<\\("
194 "\\(coding system\\)\\|"
195 "\\(input method\\)\\|"
196 "\\(character set\\)\\|"
199 ;; Note starting with word-syntax character:
200 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
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.
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)
217 (let ((orig-eol-type (coding-system-eol-type coding-system)))
218 (if (vectorp orig-eol-type)
221 (aref orig-eol-type eol-type))
222 (let ((base (coding-system-base coding-system)))
225 (if (= eol-type orig-eol-type)
227 (setq orig-eol-type (coding-system-eol-type base))
228 (if (vectorp orig-eol-type)
229 (aref orig-eol-type eol-type))))))))
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)))))
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.
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.
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."
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))
261 (defun view-hello-file ()
262 "Display the HELLO file which list up many languages and characters."
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))))
269 (defun universal-coding-system-argument (coding-system)
270 "Execute an I/O command using the specified coding system."
272 (let ((default (and buffer-file-coding-system
273 (not (eq (coding-system-type buffer-file-coding-system)
275 buffer-file-coding-system)))
276 (list (read-coding-system
278 (format "Coding system for following command (default, %s): " default)
279 "Coding system for following command: ")
281 (let* ((keyseq (read-key-sequence
282 (format "Command to execute with %s:" coding-system)))
283 (cmd (key-binding keyseq))
286 (when (eq cmd 'universal-argument)
287 (call-interactively cmd)
289 ;; Process keys bound in `universal-argument-map'.
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
298 (last-command-char (aref keyseq 0)))
299 (call-interactively cmd)))
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))
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)))
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))
316 (call-interactively cmd))))
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.
342 (coding-system-change-text-conversion
343 (car default-process-coding-system) coding-system))
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))))
350 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
351 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal "20.3")
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'
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.
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
371 A coding system that requires automatic detection of text
372 encoding (e.g. undecided, unix) can't be preferred.
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)"
392 ;; If they asked for specific EOL conversion, honor that.
393 (if (memq eol-type '(0 1 2))
395 (coding-system-change-eol-conversion base eol-type))
396 (setq coding-system base))
397 (set-default-coding-systems coding-system)))
399 (defvar sort-coding-systems-predicate nil
400 "If non-nil, a predicate function to sort coding systems.
402 It is called with two coding systems, and should return t if the first
403 one is \"less\" than the second.
405 The function `sort-coding-systems' use it.")
407 (defun sort-coding-systems (codings)
408 "Sort coding system list CODINGS by a priority of each coding system.
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'.
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
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 ;; Prefer coding systems corresponding to a
431 ;; Lower utf-16 priority so that we
432 ;; normally prefer utf-8 to it, and put
433 ;; x-ctext below that.
434 (cond ((or (eq base 'mule-utf-16-le)
435 (eq base 'mule-utf-16-be))
437 ((string-match "^x-" (symbol-name mime))
441 (if (memq base lang-preferred) 8 0)
442 (if (string-match "-with-esc$" (symbol-name base))
444 (if (eq (coding-system-type base) 2)
445 ;; For ISO based coding systems, prefer
446 ;; one that doesn't use escape sequences.
447 (let ((flags (coding-system-flags base)))
448 (if (or (consp (aref flags 0))
449 (consp (aref flags 1))
450 (consp (aref flags 2))
451 (consp (aref flags 3)))
452 (if (or (aref flags 8) (aref flags 9))
457 (sort codings (function (lambda (x y)
458 (> (funcall func x) (funcall func y))))))))
460 (defun find-coding-systems-region (from to)
461 "Return a list of proper coding systems to encode a text between FROM and TO.
462 If FROM is a string, find coding systems in that instead of the buffer.
463 All coding systems in the list can safely encode any multibyte characters
466 If the text contains no multibyte characters, return a list of a single
467 element `undecided'."
468 (let ((codings (find-coding-systems-region-internal from to)))
470 ;; The text contains only ASCII characters. Any coding
473 ;; We need copy-sequence because sorting will alter the argument.
474 (sort-coding-systems (copy-sequence codings)))))
476 (defun find-coding-systems-string (string)
477 "Return a list of proper coding systems to encode STRING.
478 All coding systems in the list can safely encode any multibyte characters
481 If STRING contains no multibyte characters, return a list of a single
482 element `undecided'."
483 (find-coding-systems-region string nil))
485 (defun find-coding-systems-for-charsets (charsets)
486 "Return a list of proper coding systems to encode characters of CHARSETS.
487 CHARSETS is a list of character sets."
488 (cond ((or (null charsets)
489 (and (= (length charsets) 1)
490 (eq 'ascii (car charsets))))
492 ((or (memq 'eight-bit-control charsets)
493 (memq 'eight-bit-graphic charsets))
494 '(raw-text emacs-mule))
498 (while (and codings charsets)
499 (setq charset (car charsets) charsets (cdr charsets))
500 (unless (eq charset 'ascii)
501 (setq l (aref char-coding-system-table (make-char charset)))
506 (if (memq (car codings) l)
507 (setq ll (cons (car codings) ll)))
508 (setq codings (cdr codings)))
509 (setq codings ll)))))
511 (char-table-extra-slot char-coding-system-table 0))))))
513 (defun find-multibyte-characters (from to &optional maxcount excludes)
514 "Find multibyte characters in the region specified by FROM and TO.
515 If FROM is a string, find multibyte characters in the string.
516 The return value is an alist of the following format:
517 ((CHARSET COUNT CHAR ...) ...)
519 CHARSET is a character set,
520 COUNT is a number of characters,
521 CHARs are found characters of the character set.
522 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
523 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
525 For invalid characters, CHARs are actually strings."
530 (while (setq idx (string-match "[^\000-\177]" from idx))
531 (setq char (aref from idx)
532 charset (char-charset char))
533 (if (eq charset 'unknown)
534 (setq char (match-string 0)))
535 (if (or (memq charset '(unknown
536 eight-bit-control eight-bit-graphic))
537 (not (or (eq excludes t) (memq charset excludes))))
538 (let ((slot (assq charset chars)))
540 (if (not (memq char (nthcdr 2 slot)))
541 (let ((count (nth 1 slot)))
542 (setcar (cdr slot) (1+ count))
543 (if (or (not maxcount) (< count maxcount))
544 (nconc slot (list char)))))
545 (setq chars (cons (list charset 1 char) chars)))))
546 (setq idx (1+ idx))))
549 (while (re-search-forward "[^\000-\177]" to t)
550 (setq char (preceding-char)
551 charset (char-charset char))
552 (if (eq charset 'unknown)
553 (setq char (match-string 0)))
554 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
555 (not (or (eq excludes t) (memq charset excludes))))
556 (let ((slot (assq charset chars)))
558 (if (not (member char (nthcdr 2 slot)))
559 (let ((count (nth 1 slot)))
560 (setcar (cdr slot) (1+ count))
561 (if (or (not maxcount) (< count maxcount))
562 (nconc slot (list char)))))
563 (setq chars (cons (list charset 1 char) chars))))))))
567 (defun search-unencodable-char (coding-system)
568 "Search forward from point for a character that is not encodable.
569 It asks which coding system to check.
570 If such a character is found, set point after that character.
571 Otherwise, don't move point.
573 When called from a program, the value is a position of the found character,
574 or nil if all characters are encodable."
576 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
578 (format "Coding-system (default, %s): " default)
580 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
583 (message "All following characters are encodable by %s" coding-system))
587 (defvar last-coding-system-specified nil
588 "Most recent coding system explicitly specified by the user when asked.
589 This variable is set whenever Emacs asks the user which coding system
590 to use in order to write a file. If you set it to nil explicitly,
591 then call `write-region', then afterward this variable will be non-nil
592 only if the user was explicitly asked and specified a coding system.")
594 (defvar select-safe-coding-system-accept-default-p nil
595 "If non-nil, a function to control the behaviour of coding system selection.
596 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
597 function `select-safe-coding-system' (which see). This variable
598 overrides that argument.")
600 (defun select-safe-coding-system (from to &optional default-coding-system
601 accept-default-p file)
602 "Ask a user to select a safe coding system from candidates.
603 The candidates of coding systems which can safely encode a text
604 between FROM and TO are shown in a popup window. Among them, the most
605 proper one is suggested as the default.
607 The list of `buffer-file-coding-system' of the current buffer and the
608 most preferred coding system (if it corresponds to a MIME charset) is
609 treated as the default coding system list. Among them, the first one
610 that safely encodes the text is normally selected silently and
611 returned without any user interaction. See also the command
612 `prefer-coding-system'.
614 However, the user is queried if the chosen coding system is
615 inconsistent with what would be selected by `set-auto-coding' from
616 coding cookies &c. if the contents of the region were read from a
617 file. (That could lead to data corruption in a file subsequently
618 re-visited and edited.)
620 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
621 list of coding systems to be prepended to the default coding system
622 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
623 element is t, the cdr part is used as the defualt coding system list,
624 i.e. `buffer-file-coding-system' and the most prepended coding system
627 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
628 determine the acceptability of the silently selected coding system.
629 It is called with that coding system, and should return nil if it
630 should not be silently selected and thus user interaction is required.
632 Optional 5th arg FILE is the file name to use for this purpose.
633 That is different from `buffer-file-name' when handling `write-region'
636 The variable `select-safe-coding-system-accept-default-p', if
637 non-nil, overrides ACCEPT-DEFAULT-P.
639 Kludgy feature: if FROM is a string, the string is the target text,
641 (if (and default-coding-system
642 (not (listp default-coding-system)))
643 (setq default-coding-system (list default-coding-system)))
645 (let ((no-other-defaults nil))
646 (if (eq (car default-coding-system) t)
647 (setq no-other-defaults t
648 default-coding-system (cdr default-coding-system)))
650 ;; Change elements of the list to (coding . base-coding).
651 (setq default-coding-system
652 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
653 default-coding-system))
655 (unless no-other-defaults
656 ;; If buffer-file-coding-system is not nil nor undecided, append it
658 (if buffer-file-coding-system
659 (let ((base (coding-system-base buffer-file-coding-system)))
660 (or (eq base 'undecided)
661 (assq buffer-file-coding-system default-coding-system)
662 (rassq base default-coding-system)
663 (setq default-coding-system
664 (append default-coding-system
665 (list (cons buffer-file-coding-system base)))))))
667 ;; If the most preferred coding system has the property mime-charset,
668 ;; append it to the defaults.
669 (let ((tail coding-category-list)
672 (not (setq preferred (symbol-value (car tail)))))
673 (setq tail (cdr tail)))
674 (and (coding-system-p preferred)
675 (setq base (coding-system-base preferred))
676 (coding-system-get preferred 'mime-charset)
677 (not (assq preferred default-coding-system))
678 (not (rassq base default-coding-system))
679 (setq default-coding-system
680 (append default-coding-system
681 (list (cons preferred base))))))))
683 (if select-safe-coding-system-accept-default-p
684 (setq accept-default-p select-safe-coding-system-accept-default-p))
686 (let ((codings (find-coding-systems-region from to))
688 (bufname (buffer-name))
689 safe rejected unsafe)
690 (if (eq (car codings) 'undecided)
691 ;; Any coding system is ok.
692 (setq coding-system t)
693 ;; Classify the defaults into safe, rejected, and unsafe.
694 (dolist (elt default-coding-system)
695 (if (memq (cdr elt) codings)
696 (if (and (functionp accept-default-p)
697 (not (funcall accept-default-p (cdr elt))))
698 (push (car elt) rejected)
699 (push (car elt) safe))
700 (push (car elt) unsafe)))
702 (setq coding-system (car (last safe)))))
704 ;; If all the defaults failed, ask a user.
705 (when (not coding-system)
706 ;; At first, if some defaults are unsafe, record at most 11
707 ;; problematic characters and their positions for them by turning
710 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
714 (mapcar #'(lambda (coding)
716 (mapcar #'(lambda (pos)
717 (cons pos (aref from pos)))
718 (unencodable-char-position
719 0 (length from) coding
723 (mapcar #'(lambda (coding)
725 (mapcar #'(lambda (pos)
726 (cons pos (char-after pos)))
727 (unencodable-char-position
728 from to coding 11))))
731 ;; Change each safe coding system to the corresponding
732 ;; mime-charset name if it is also a coding system. Such a name
733 ;; is more friendly to users.
737 (setq mime-charset (coding-system-get (car l) 'mime-charset))
738 (if (and mime-charset (coding-system-p mime-charset))
739 (setcar l mime-charset))
742 ;; Don't offer variations with locking shift, which you
743 ;; basically never want.
745 (dolist (elt codings (setq codings (nreverse l)))
746 (unless (or (eq 'coding-category-iso-7-else
747 (coding-system-category elt))
748 (eq 'coding-category-iso-8-else
749 (coding-system-category elt)))
752 (let ((window-configuration (current-window-configuration)))
754 ;; If some defaults are unsafe, make sure the offending
755 ;; buffer is displayed.
756 (when (and unsafe (not (stringp from)))
757 (pop-to-buffer bufname)
758 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
760 ;; Then ask users to select one from CODINGS while showing
761 ;; the reason why none of the defaults are not used.
762 (with-output-to-temp-buffer "*Warning*"
764 (set-buffer standard-output)
765 (if (not default-coding-system)
766 (insert "No default coding systems to try for "
768 (format "string \"%s\"." from)
769 (format "buffer `%s'." bufname)))
771 "These default coding systems were tried to encode"
773 (concat " \"" (if (> (length from) 10)
774 (concat (substring from 0 10) "...\"")
776 (format " text\nin the buffer `%s'" bufname))
780 (mapc #'(lambda (x) (princ " ") (princ (car x)))
781 default-coding-system)
783 (fill-region-as-paragraph pos (point)))
785 (insert "These safely encodes the target text,
786 but it is not recommended for encoding text in this context,
787 e.g., for sending an email message.\n ")
788 (mapc #'(lambda (x) (princ " ") (princ x)) rejected)
791 (insert (if rejected "And the others"
792 "However, each of them")
793 " encountered these problematic characters:\n")
796 (insert (format " %s:" (car coding)))
799 #'(lambda (bufname pos)
800 (when (buffer-live-p (get-buffer bufname))
801 (pop-to-buffer bufname)
804 #'(lambda (bufname pos coding)
805 (when (buffer-live-p (get-buffer bufname))
806 (pop-to-buffer bufname)
810 (search-unencodable-char coding)
811 (forward-char -1))))))
812 (dolist (elt (cdr coding))
815 (insert (if (< i 10) (cdr elt) "..."))
821 "mouse-2, RET: jump to this character"
823 'help-args (list bufname (car elt)))
828 "mouse-2, RET: next unencodable character"
830 'help-args (list bufname (car elt)
836 The first problematic character is at point in the displayed buffer,\n"
837 (substitute-command-keys "\
838 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
840 "\nSelect the above, or "
843 one of the following safe coding systems, or edit the buffer:\n")
846 (mapcar (function (lambda (x) (princ " ") (princ x)))
849 (fill-region-as-paragraph pos (point)))
850 (insert "Or specify any other coding system
851 on your risk of losing the problematic characters.\n")))
853 ;; Read a coding system.
854 (setq default-coding-system (or (car safe) (car codings)))
857 (format "Select coding system (default %s): "
858 default-coding-system)
859 default-coding-system))
860 (setq last-coding-system-specified coding-system))
862 (kill-buffer "*Warning*")
863 (set-window-configuration window-configuration)))
865 (if (vectorp (coding-system-eol-type coding-system))
866 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
869 (coding-system-change-eol-conversion coding-system eol)))))
871 (if (eq coding-system t)
872 (setq coding-system buffer-file-coding-system))
873 ;; Check we're not inconsistent with what `coding:' spec &c would
874 ;; give when file is re-read.
875 ;; But don't do this if we explicitly ignored the cookie
876 ;; by using `find-file-literally'.
877 (unless (or (stringp from) find-file-literally)
878 (let ((auto-cs (save-excursion
881 (narrow-to-region from to)
882 (goto-char (point-min))
883 (set-auto-coding (or file buffer-file-name "")
885 (if (and auto-cs coding-system
886 ;; Don't barf if writing a compressed file, say.
887 ;; This check perhaps isn't ideal, but is probably
888 ;; the best thing to do.
889 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
890 (not (coding-system-equal (coding-system-base coding-system)
891 (coding-system-base auto-cs))))
893 (format "Selected encoding %s disagrees with \
894 %s specified by file contents. Really save (else edit coding cookies \
895 and try again)? " coding-system auto-cs))
896 (error "Save aborted")))))
899 (setq select-safe-coding-system-function 'select-safe-coding-system)
901 (defun select-message-coding-system ()
902 "Return a coding system to encode the outgoing message of the current buffer.
903 It at first tries the first coding system found in these variables
905 (1) local value of `buffer-file-coding-system'
906 (2) value of `sendmail-coding-system'
907 (3) value of `default-sendmail-coding-system'
908 (4) value of `default-buffer-file-coding-system'
909 If the found coding system can't encode the current buffer,
910 or none of them are bound to a coding system,
911 it asks the user to select a proper coding system."
912 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
913 buffer-file-coding-system)
914 sendmail-coding-system
915 default-sendmail-coding-system
916 default-buffer-file-coding-system)))
917 (if (eq coding 'no-conversion)
918 ;; We should never use no-conversion for outgoing mail.
920 (if (fboundp select-safe-coding-system-function)
921 (funcall select-safe-coding-system-function
922 (point-min) (point-max) coding
923 (function (lambda (x) (coding-system-get x 'mime-charset))))
926 ;;; Language support stuff.
928 (defvar language-info-alist nil
929 "Alist of language environment definitions.
930 Each element looks like:
931 (LANGUAGE-NAME . ((KEY . INFO) ...))
932 where LANGUAGE-NAME is a string, the name of the language environment,
933 KEY is a symbol denoting the kind of information, and
934 INFO is the data associated with KEY.
935 Meaningful values for KEY include
937 documentation value is documentation of what this language environment
938 is meant for, and how to use it.
939 charset value is a list of the character sets used by this
940 language environment.
941 sample-text value is an expression which is evalled to generate
942 a line of text written using characters appropriate
943 for this language environment.
944 setup-function value is a function to call to switch to this
945 language environment.
946 exit-function value is a function to call to leave this
947 language environment.
948 coding-system value is a list of coding systems that are good
949 for saving text written in this language environment.
950 This list serves as suggestions to the user;
951 in effect, as a kind of documentation.
952 coding-priority value is a list of coding systems for this language
953 environment, in order of decreasing priority.
954 This is used to set up the coding system priority
955 list when you switch to this language environment.
957 value is a translation table to be set in the
958 variable `nonascii-translation-table' in this
959 language environment, or a character set from
960 which `nonascii-insert-offset' is calculated.
961 input-method value is a default input method for this language
963 features value is a list of features requested in this
964 language environment.
966 The following keys take effect only when multibyte characters are
967 globally disabled, i.e. the value of `default-enable-multibyte-characters'
970 unibyte-syntax value is a library name to load to set
971 unibyte 8-bit character syntaxes for this
972 language environment.
974 unibyte-display value is a coding system to encode characters
975 for the terminal. Characters in the range
976 of 160 to 255 display not as octal escapes,
977 but as non-ASCII characters in this language
980 (defun get-language-info (lang-env key)
981 "Return information listed under KEY for language environment LANG-ENV.
982 KEY is a symbol denoting the kind of information.
983 For a list of useful values for KEY and their meanings,
984 see `language-info-alist'."
985 (if (symbolp lang-env)
986 (setq lang-env (symbol-name lang-env)))
987 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
989 (cdr (assq key (cdr lang-slot))))))
991 (defun set-language-info (lang-env key info)
992 "Modify part of the definition of language environment LANG-ENV.
993 Specifically, this stores the information INFO under KEY
994 in the definition of this language environment.
995 KEY is a symbol denoting the kind of information.
996 INFO is the value for that information.
998 For a list of useful values for KEY and their meanings,
999 see `language-info-alist'."
1000 (if (symbolp lang-env)
1001 (setq lang-env (symbol-name lang-env)))
1002 (let (lang-slot key-slot)
1003 (setq lang-slot (assoc lang-env language-info-alist))
1004 (if (null lang-slot) ; If no slot for the language, add it.
1005 (setq lang-slot (list lang-env)
1006 language-info-alist (cons lang-slot language-info-alist)))
1007 (setq key-slot (assq key lang-slot))
1008 (if (null key-slot) ; If no slot for the key, add it.
1010 (setq key-slot (list key))
1011 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1012 (setcdr key-slot (purecopy info))
1013 ;; Update the custom-type of `current-language-environment'.
1014 (put 'current-language-environment 'custom-type
1015 (cons 'choice (mapcar
1017 (list 'const (car lang)))
1018 (sort (copy-sequence language-info-alist)
1019 (lambda (x y) (string< (car x) (car y)))))))))
1021 (defun set-language-info-alist (lang-env alist &optional parents)
1022 "Store ALIST as the definition of language environment LANG-ENV.
1023 ALIST is an alist of KEY and INFO values. See the documentation of
1024 `language-info-alist' for the meanings of KEY and INFO.
1026 Optional arg PARENTS is a list of parent menu names; it specifies
1027 where to put this language environment in the
1028 Describe Language Environment and Set Language Environment menus.
1029 For example, (\"European\") means to put this language environment
1030 in the European submenu in each of those two menus."
1031 (if (symbolp lang-env)
1032 (setq lang-env (symbol-name lang-env)))
1033 (let ((describe-map describe-language-environment-map)
1034 (setup-map setup-language-environment-map))
1037 map parent-symbol parent prompt)
1039 (if (symbolp (setq parent-symbol (car l)))
1040 (setq parent (symbol-name parent))
1041 (setq parent parent-symbol parent-symbol (intern parent)))
1042 (setq map (lookup-key describe-map (vector parent-symbol)))
1043 ;; This prompt string is for define-prefix-command, so
1044 ;; that the map it creates will be suitable for a menu.
1045 (or map (setq prompt (format "%s Environment" parent)))
1048 (setq map (intern (format "describe-%s-environment-map"
1049 (downcase parent))))
1050 (define-prefix-command map nil prompt)
1051 (define-key-after describe-map (vector parent-symbol)
1052 (cons parent map) t)))
1053 (setq describe-map (symbol-value map))
1054 (setq map (lookup-key setup-map (vector parent-symbol)))
1057 (setq map (intern (format "setup-%s-environment-map"
1058 (downcase parent))))
1059 (define-prefix-command map nil prompt)
1060 (define-key-after setup-map (vector parent-symbol)
1061 (cons parent map) t)))
1062 (setq setup-map (symbol-value map))
1065 ;; Set up menu items for this language env.
1066 (let ((doc (assq 'documentation alist)))
1068 (define-key-after describe-map (vector (intern lang-env))
1069 (cons lang-env 'describe-specified-language-support) t)))
1070 (define-key-after setup-map (vector (intern lang-env))
1071 (cons lang-env 'setup-specified-language-environment) t)
1074 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
1075 (setq alist (cdr alist)))))
1077 (defun read-language-name (key prompt &optional default)
1078 "Read a language environment name which has information for KEY.
1079 If KEY is nil, read any language environment.
1080 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1081 This returns a language environment name as a string."
1082 (let* ((completion-ignore-case t)
1083 (name (completing-read prompt
1086 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1087 t nil nil default)))
1088 (if (and (> (length name) 0)
1090 (get-language-info name key)))
1093 ;;; Multilingual input methods.
1095 "LEIM: Libraries of Emacs Input Methods."
1098 (defconst leim-list-file-name "leim-list.el"
1099 "Name of LEIM list file.
1100 This file contains a list of libraries of Emacs input methods (LEIM)
1101 in the format of Lisp expression for registering each input method.
1102 Emacs loads this file at startup time.")
1104 (defvar leim-list-header (format
1105 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1107 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1108 ;; methods in the same directory as this file. Loading this file
1109 ;; registers all the input methods in Emacs.
1111 ;; Each entry has the form:
1112 ;; (register-input-method
1113 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1114 ;; TITLE DESCRIPTION
1116 ;; See the function `register-input-method' for the meanings of the arguments.
1118 ;; If this directory is included in load-path, Emacs automatically
1119 ;; loads this file at startup time.
1122 leim-list-file-name)
1123 "Header to be inserted in LEIM list file.")
1125 (defvar leim-list-entry-regexp "^(register-input-method"
1126 "Regexp matching head of each entry in LEIM list file.
1127 See also the variable `leim-list-header'")
1129 (defvar update-leim-list-functions
1130 '(quail-update-leim-list-file)
1131 "List of functions to call to update LEIM list file.
1132 Each function is called with one arg, LEIM directory name.")
1134 (defun update-leim-list-file (&rest dirs)
1135 "Update LEIM list file in directories DIRS."
1136 (let ((functions update-leim-list-functions))
1138 (apply (car functions) dirs)
1139 (setq functions (cdr functions)))))
1141 (defvar current-input-method nil
1142 "The current input method for multilingual text.
1143 If nil, that means no input method is activated now.")
1144 (make-variable-buffer-local 'current-input-method)
1145 (put 'current-input-method 'permanent-local t)
1147 (defvar current-input-method-title nil
1148 "Title string of the current input method shown in mode line.")
1149 (make-variable-buffer-local 'current-input-method-title)
1150 (put 'current-input-method-title 'permanent-local t)
1152 (defcustom default-input-method nil
1153 "*Default input method for multilingual text (a string).
1154 This is the input method activated automatically by the command
1155 `toggle-input-method' (\\[toggle-input-method])."
1157 :type '(choice (const nil) string)
1158 :set-after '(current-language-environment))
1160 (put 'input-method-function 'permanent-local t)
1162 (defvar input-method-history nil
1163 "History list for some commands that read input methods.")
1164 (make-variable-buffer-local 'input-method-history)
1165 (put 'input-method-history 'permanent-local t)
1167 (defvar inactivate-current-input-method-function nil
1168 "Function to call for inactivating the current input method.
1169 Every input method should set this to an appropriate value when activated.
1170 This function is called with no argument.
1172 This function should never change the value of `current-input-method'.
1173 It is set to nil by the function `inactivate-input-method'.")
1174 (make-variable-buffer-local 'inactivate-current-input-method-function)
1175 (put 'inactivate-current-input-method-function 'permanent-local t)
1177 (defvar describe-current-input-method-function nil
1178 "Function to call for describing the current input method.
1179 This function is called with no argument.")
1180 (make-variable-buffer-local 'describe-current-input-method-function)
1181 (put 'describe-current-input-method-function 'permanent-local t)
1183 (defvar input-method-alist nil
1184 "Alist of input method names vs how to use them.
1185 Each element has the form:
1186 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1187 See the function `register-input-method' for the meanings of the elements.")
1189 (defun register-input-method (input-method lang-env &rest args)
1190 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1191 INPUT-METHOD and LANG-ENV are symbols or strings.
1193 The remaining arguments are:
1194 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1195 ACTIVATE-FUNC is a function to call to activate this method.
1196 TITLE is a string to show in the mode line when this method is active.
1197 DESCRIPTION is a string describing this method and what it is good for.
1198 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1199 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1201 This function is mainly used in the file \"leim-list.el\" which is
1202 created at Emacs build time, registering all Quail input methods
1203 contained in the Emacs distribution.
1205 In case you want to register a new Quail input method by yourself, be
1206 careful to use the same input method title as given in the third
1207 parameter of `quail-define-package'. (If the values are different, the
1208 string specified in this function takes precedence.)
1210 The commands `describe-input-method' and `list-input-methods' need
1211 these duplicated values to show some information about input methods
1212 without loading the relevant Quail packages."
1213 (if (symbolp lang-env)
1214 (setq lang-env (symbol-name lang-env)))
1215 (if (symbolp input-method)
1216 (setq input-method (symbol-name input-method)))
1217 (let ((info (cons lang-env args))
1218 (slot (assoc input-method input-method-alist)))
1221 (setq slot (cons input-method info))
1222 (setq input-method-alist (cons slot input-method-alist)))))
1224 (defun read-input-method-name (prompt &optional default inhibit-null)
1225 "Read a name of input method from a minibuffer prompting with PROMPT.
1226 If DEFAULT is non-nil, use that as the default,
1227 and substitute it into PROMPT at the first `%s'.
1228 If INHIBIT-NULL is non-nil, null input signals an error.
1230 The return value is a string."
1232 (setq prompt (format prompt default)))
1233 (let* ((completion-ignore-case t)
1234 ;; As it is quite normal to change input method in the
1235 ;; minibuffer, we must enable it even if
1236 ;; enable-recursive-minibuffers is currently nil.
1237 (enable-recursive-minibuffers t)
1238 ;; This binding is necessary because input-method-history is
1240 (input-method (completing-read prompt input-method-alist
1241 nil t nil 'input-method-history
1243 (if (and input-method (symbolp input-method))
1244 (setq input-method (symbol-name input-method)))
1245 (if (> (length input-method) 0)
1248 (error "No valid input method is specified")))))
1250 (defun activate-input-method (input-method)
1251 "Switch to input method INPUT-METHOD for the current buffer.
1252 If some other input method is already active, turn it off first.
1253 If INPUT-METHOD is nil, deactivate any current input method."
1254 (if (and input-method (symbolp input-method))
1255 (setq input-method (symbol-name input-method)))
1256 (if (and current-input-method
1257 (not (string= current-input-method input-method)))
1258 (inactivate-input-method))
1259 (unless (or current-input-method (null input-method))
1260 (let ((slot (assoc input-method input-method-alist)))
1262 (error "Can't activate input method `%s'" input-method))
1263 (setq current-input-method-title nil)
1264 (let ((func (nth 2 slot)))
1265 (if (functionp func)
1266 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1267 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1269 (require (cdr func))
1270 (apply (car func) input-method (nthcdr 5 slot)))
1271 (error "Can't activate input method `%s'" input-method))))
1272 (setq current-input-method input-method)
1273 (or (stringp current-input-method-title)
1274 (setq current-input-method-title (nth 3 slot)))
1276 (run-hooks 'input-method-activate-hook)
1277 (force-mode-line-update)))))
1279 (defun inactivate-input-method ()
1280 "Turn off the current input method."
1281 (when current-input-method
1282 (if input-method-history
1283 (unless (string= current-input-method (car input-method-history))
1284 (setq input-method-history
1285 (cons current-input-method
1286 (delete current-input-method input-method-history))))
1287 (setq input-method-history (list current-input-method)))
1289 (funcall inactivate-current-input-method-function)
1291 (run-hooks 'input-method-inactivate-hook)
1292 (setq current-input-method nil
1293 input-method-function nil
1294 current-input-method-title nil)
1295 (force-mode-line-update)))))
1297 (defun set-input-method (input-method)
1298 "Select and activate input method INPUT-METHOD for the current buffer.
1299 This also sets the default input method to the one you specify.
1300 If INPUT-METHOD is nil, this function turns off the input method, and
1301 also causes you to be prompted for a name of an input method the next
1302 time you invoke \\[toggle-input-method].
1304 To deactivate the input method interactively, use \\[toggle-input-method].
1305 To deactivate it programmatically, use \\[inactivate-input-method]."
1307 (let* ((default (or (car input-method-history) default-input-method)))
1308 (list (read-input-method-name
1309 (if default "Select input method (default %s): " "Select input method: ")
1311 (activate-input-method input-method)
1312 (setq default-input-method input-method)
1313 (when (interactive-p)
1314 (customize-mark-as-set 'default-input-method))
1315 default-input-method)
1317 (defun toggle-input-method (&optional arg)
1318 "Enable or disable multilingual text input method for the current buffer.
1319 Only one input method can be enabled at any time in a given buffer.
1321 The normal action is to enable an input method if none was
1322 enabled, and disable the current one otherwise. Which input method
1323 to enable can be determined in various ways--either the one most
1324 recently used, or the one specified by `default-input-method', or
1325 as a last resort by reading the name of an input method in the
1328 With a prefix argument, read an input method name with the minibuffer
1329 and enable that one. The default is the most recent input method specified
1330 \(not including the currently active input method, if any)."
1333 (if (and current-input-method (not arg))
1334 (inactivate-input-method)
1335 (let ((default (or (car input-method-history) default-input-method)))
1336 (if (and arg default (equal current-input-method default)
1337 (> (length input-method-history) 1))
1338 (setq default (nth 1 input-method-history)))
1339 (activate-input-method
1340 (if (or arg (not default))
1342 (read-input-method-name
1343 (if default "Input method (default %s): " "Input method: " )
1346 (unless default-input-method
1348 (setq default-input-method current-input-method)
1349 (when (interactive-p)
1350 (customize-mark-as-set 'default-input-method)))))))
1352 (defun describe-input-method (input-method)
1353 "Describe input method INPUT-METHOD."
1355 (list (read-input-method-name
1356 "Describe input method (default, current choice): ")))
1357 (if (and input-method (symbolp input-method))
1358 (setq input-method (symbol-name input-method)))
1359 (help-setup-xref (list #'describe-input-method
1360 (or input-method current-input-method))
1363 (if (null input-method)
1364 (describe-current-input-method)
1365 (let ((current current-input-method))
1369 (activate-input-method input-method)
1370 (describe-current-input-method))
1371 (activate-input-method current))
1373 (activate-input-method current)
1374 (help-setup-xref (list #'describe-input-method input-method)
1376 (with-output-to-temp-buffer (help-buffer)
1377 (let ((elt (assoc input-method input-method-alist)))
1379 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1380 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1382 (defun describe-current-input-method ()
1383 "Describe the input method currently in use.
1384 This is a subroutine for `describe-input-method'."
1385 (if current-input-method
1386 (if (and (symbolp describe-current-input-method-function)
1387 (fboundp describe-current-input-method-function))
1388 (funcall describe-current-input-method-function)
1389 (message "No way to describe the current input method `%s'"
1390 current-input-method)
1392 (error "No input method is activated now")))
1394 (defun read-multilingual-string (prompt &optional initial-input input-method)
1395 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1396 The input method selected last time is activated in minibuffer.
1397 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1399 Optional 3rd argument INPUT-METHOD specifies the input method
1400 to be activated instead of the one selected last time. It is a symbol
1404 current-input-method
1405 default-input-method
1406 (read-input-method-name "Input method: " nil t)))
1407 (if (and input-method (symbolp input-method))
1408 (setq input-method (symbol-name input-method)))
1409 (let ((prev-input-method current-input-method))
1412 (activate-input-method input-method)
1413 (read-string prompt initial-input nil nil t))
1414 (activate-input-method prev-input-method))))
1416 ;; Variables to control behavior of input methods. All input methods
1417 ;; should react to these variables.
1419 (defcustom input-method-verbose-flag 'default
1420 "*A flag to control extra guidance given by input methods.
1421 The value should be nil, t, `complex-only', or `default'.
1423 The extra guidance is done by showing list of available keys in echo
1424 area. When you use the input method in the minibuffer, the guidance
1425 is shown at the bottom short window (split from the existing window).
1427 If the value is t, extra guidance is always given, if the value is
1428 nil, extra guidance is always suppressed.
1430 If the value is `complex-only', only complex input methods such as
1431 `chinese-py' and `japanese' give extra guidance.
1433 If the value is `default', complex input methods always give extra
1434 guidance, but simple input methods give it only when you are not in
1437 See also the variable `input-method-highlight-flag'."
1438 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1439 (const complex-only) (const default))
1442 (defcustom input-method-highlight-flag t
1443 "*If this flag is non-nil, input methods highlight partially-entered text.
1444 For instance, while you are in the middle of a Quail input method sequence,
1445 the text inserted so far is temporarily underlined.
1446 The underlining goes away when you finish or abort the input method sequence.
1447 See also the variable `input-method-verbose-flag'."
1451 (defvar input-method-activate-hook nil
1452 "Normal hook run just after an input method is activated.
1454 The variable `current-input-method' keeps the input method name
1457 (defvar input-method-inactivate-hook nil
1458 "Normal hook run just after an input method is inactivated.
1460 The variable `current-input-method' still keeps the input method name
1463 (defvar input-method-after-insert-chunk-hook nil
1464 "Normal hook run just after an input method insert some chunk of text.")
1466 (defvar input-method-exit-on-first-char nil
1467 "This flag controls when an input method returns.
1468 Usually, the input method does not return while there's a possibility
1469 that it may find a different translation if a user types another key.
1470 But, it this flag is non-nil, the input method returns as soon as
1471 the current key sequence gets long enough to have some valid translation.")
1473 (defvar input-method-use-echo-area nil
1474 "This flag controls how an input method shows an intermediate key sequence.
1475 Usually, the input method inserts the intermediate key sequence,
1476 or candidate translations corresponding to the sequence,
1477 at point in the current buffer.
1478 But, if this flag is non-nil, it displays them in echo area instead.")
1480 (defvar input-method-exit-on-invalid-key nil
1481 "This flag controls the behaviour of an input method on invalid key input.
1482 Usually, when a user types a key which doesn't start any character
1483 handled by the input method, the key is handled by turning off the
1484 input method temporarily. After that key, the input method is re-enabled.
1485 But, if this flag is non-nil, the input method is never back on.")
1488 (defvar set-language-environment-hook nil
1489 "Normal hook run after some language environment is set.
1491 When you set some hook function here, that effect usually should not
1492 be inherited to another language environment. So, you had better set
1493 another function in `exit-language-environment-hook' (which see) to
1494 cancel the effect.")
1496 (defvar exit-language-environment-hook nil
1497 "Normal hook run after exiting from some language environment.
1498 When this hook is run, the variable `current-language-environment'
1499 is still bound to the language environment being exited.
1501 This hook is mainly used for canceling the effect of
1502 `set-language-environment-hook' (which-see).")
1504 (put 'setup-specified-language-environment 'apropos-inhibit t)
1506 (defun setup-specified-language-environment ()
1507 "Switch to a specified language environment."
1509 (let (language-name)
1510 (if (and (symbolp last-command-event)
1511 (or (not (eq last-command-event 'Default))
1512 (setq last-command-event 'English))
1513 (setq language-name (symbol-name last-command-event)))
1515 (set-language-environment language-name)
1516 (customize-mark-as-set 'current-language-environment))
1517 (error "Bogus calling sequence"))))
1519 (defcustom current-language-environment "English"
1520 "The last language environment specified with `set-language-environment'.
1521 This variable should be set only with \\[customize], which is equivalent
1522 to using the function `set-language-environment'."
1523 :link '(custom-manual "(emacs)Language Environments")
1524 :set (lambda (symbol value) (set-language-environment value))
1526 (or (car-safe (assoc-ignore-case
1527 (if (symbolp current-language-environment)
1528 (symbol-name current-language-environment)
1529 current-language-environment)
1530 language-info-alist))
1532 ;; custom type will be updated with `set-language-info'.
1533 :type (if language-info-alist
1534 (cons 'choice (mapcar
1536 (list 'const (car lang)))
1537 (sort (copy-sequence language-info-alist)
1538 (lambda (x y) (string< (car x) (car y))))))
1540 :initialize 'custom-initialize-default
1543 (defun reset-language-environment ()
1544 "Reset multilingual environment of Emacs to the default status.
1546 The default status is as follows:
1548 The default value of `buffer-file-coding-system' is nil.
1549 The default coding system for process I/O is nil.
1550 The default value for the command `set-terminal-coding-system' is nil.
1551 The default value for the command `set-keyboard-coding-system' is nil.
1553 The order of priorities of coding categories and the coding system
1554 bound to each category are as follows
1555 coding category coding system
1556 --------------------------------------------------
1557 coding-category-iso-8-2 iso-latin-1
1558 coding-category-iso-8-1 iso-latin-1
1559 coding-category-iso-7-tight iso-2022-jp
1560 coding-category-iso-7 iso-2022-7bit
1561 coding-category-iso-7-else iso-2022-7bit-lock
1562 coding-category-iso-8-else iso-2022-8bit-ss2
1563 coding-category-emacs-mule emacs-mule
1564 coding-category-raw-text raw-text
1565 coding-category-sjis japanese-shift-jis
1566 coding-category-big5 chinese-big5
1567 coding-category-ccl nil
1568 coding-category-binary no-conversion
1569 coding-category-utf-16-be nil
1570 coding-category-utf-16-le nil
1571 coding-category-utf-8 mule-utf-8"
1573 ;; This function formerly set default-enable-multibyte-characters to t,
1574 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1576 (setq coding-category-iso-7-tight 'iso-2022-jp
1577 coding-category-iso-7 'iso-2022-7bit
1578 coding-category-iso-8-1 'iso-latin-1
1579 coding-category-iso-8-2 'iso-latin-1
1580 coding-category-iso-7-else 'iso-2022-7bit-lock
1581 coding-category-iso-8-else 'iso-2022-8bit-ss2
1582 coding-category-emacs-mule 'emacs-mule
1583 coding-category-raw-text 'raw-text
1584 coding-category-sjis 'japanese-shift-jis
1585 coding-category-big5 'chinese-big5
1586 coding-category-utf-16-be nil
1587 coding-category-utf-16-le nil
1588 coding-category-utf-8 'mule-utf-8
1589 coding-category-ccl nil
1590 coding-category-binary 'no-conversion)
1592 (set-coding-priority
1593 '(coding-category-iso-8-1
1594 coding-category-iso-8-2
1595 coding-category-iso-7-tight
1596 coding-category-iso-7
1597 coding-category-iso-7-else
1598 coding-category-iso-8-else
1599 coding-category-emacs-mule
1600 coding-category-raw-text
1601 coding-category-sjis
1602 coding-category-big5
1604 coding-category-binary
1605 coding-category-utf-16-be
1606 coding-category-utf-16-le
1607 coding-category-utf-8))
1609 (update-coding-systems-internal)
1611 (set-default-coding-systems nil)
1612 (setq default-sendmail-coding-system 'iso-latin-1)
1613 ;; Preserve eol-type from existing default-process-coding-systems.
1614 ;; On non-unix-like systems in particular, these may have been set
1615 ;; carefully by the user, or by the startup code, to deal with the
1616 ;; users shell appropriately, so should not be altered by changing
1617 ;; language environment.
1618 (let ((output-coding
1619 ;; When bootstrapping, coding-systems are not defined yet, so
1620 ;; we need to catch the error from check-coding-system.
1622 (coding-system-change-text-conversion
1623 (car default-process-coding-system) 'undecided)
1624 (coding-system-error 'undecided)))
1627 (coding-system-change-text-conversion
1628 (cdr default-process-coding-system) 'iso-latin-1)
1629 (coding-system-error 'iso-latin-1))))
1630 (setq default-process-coding-system
1631 (cons output-coding input-coding)))
1633 ;; Don't alter the terminal and keyboard coding systems here.
1634 ;; The terminal still supports the same coding system
1635 ;; that it supported a minute ago.
1636 ;; (set-terminal-coding-system-internal nil)
1637 ;; (set-keyboard-coding-system-internal nil)
1639 (setq nonascii-translation-table nil
1640 nonascii-insert-offset 0))
1642 (reset-language-environment)
1644 (defun set-display-table-and-terminal-coding-system (language-name)
1645 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1646 (let ((coding (get-language-info language-name 'unibyte-display)))
1648 (standard-display-european-internal)
1649 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1650 (aset standard-display-table 146 nil))
1651 (or (eq window-system 'pc)
1652 (set-terminal-coding-system coding))))
1654 (defun set-language-environment (language-name)
1655 "Set up multi-lingual environment for using LANGUAGE-NAME.
1656 This sets the coding system priority and the default input method
1657 and sometimes other things. LANGUAGE-NAME should be a string
1658 which is the name of a language environment. For example, \"Latin-1\"
1659 specifies the character set for the major languages of Western Europe."
1660 (interactive (list (read-language-name
1662 "Set language environment (default, English): ")))
1664 (if (symbolp language-name)
1665 (setq language-name (symbol-name language-name)))
1666 (setq language-name "English"))
1667 (or (assoc-ignore-case language-name language-info-alist)
1668 (error "Language environment not defined: %S" language-name))
1669 (if current-language-environment
1670 (let ((func (get-language-info current-language-environment
1672 (run-hooks 'exit-language-environment-hook)
1673 (if (functionp func) (funcall func))))
1674 (let ((default-eol-type (coding-system-eol-type
1675 default-buffer-file-coding-system)))
1676 (reset-language-environment)
1678 ;; The features might set up coding systems.
1679 (let ((required-features (get-language-info language-name 'features)))
1680 (while required-features
1681 (require (car required-features))
1682 (setq required-features (cdr required-features))))
1684 (setq current-language-environment language-name)
1685 (set-language-environment-coding-systems language-name default-eol-type))
1686 (let ((input-method (get-language-info language-name 'input-method)))
1688 (setq default-input-method input-method)
1689 (if input-method-history
1690 (setq input-method-history
1692 (delete input-method input-method-history))))))
1693 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1695 (if (eq window-system 'pc)
1697 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1699 ((char-table-p nonascii)
1700 (setq nonascii-translation-table nonascii))
1701 ((and (eq window-system 'pc) (boundp dos-table))
1702 ;; DOS terminals' default is to use a special non-ASCII translation
1703 ;; table as appropriate for the installed codepage.
1704 (setq nonascii-translation-table (symbol-value dos-table)))
1705 ((charsetp nonascii)
1706 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1708 ;; Unibyte setups if necessary.
1709 (unless default-enable-multibyte-characters
1710 ;; Syntax and case table.
1711 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1713 (let ((set-case-syntax-set-multibyte nil))
1714 (load syntax nil t))
1715 ;; No information for syntax and case. Reset to the defaults.
1716 (let ((syntax-table (standard-syntax-table))
1717 (case-table (standard-case-table))
1718 (ch (if (eq window-system 'pc) 128 160)))
1720 (modify-syntax-entry ch " " syntax-table)
1721 (aset case-table ch ch)
1723 (set-char-table-extra-slot case-table 0 nil)
1724 (set-char-table-extra-slot case-table 1 nil)
1725 (set-char-table-extra-slot case-table 2 nil))
1726 (set-standard-case-table (standard-case-table))
1727 (let ((list (buffer-list)))
1729 (with-current-buffer (car list)
1730 (set-case-table (standard-case-table)))
1731 (setq list (cdr list))))))
1732 (set-display-table-and-terminal-coding-system language-name))
1734 (let ((required-features (get-language-info language-name 'features)))
1735 (while required-features
1736 (require (car required-features))
1737 (setq required-features (cdr required-features))))
1738 (let ((func (get-language-info language-name 'setup-function)))
1739 (if (functionp func)
1741 (run-hooks 'set-language-environment-hook)
1742 (force-mode-line-update t))
1744 (defun standard-display-european-internal ()
1745 ;; Actually set up direct output of non-ASCII characters.
1746 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1747 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1748 ;; the native font, and codes 160 and 146 stand for something very
1750 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1752 ;; Make non-line-break space display as a plain space.
1753 ;; Most X fonts do the wrong thing for code 160.
1754 (aset standard-display-table 160 [32])
1755 ;; With luck, non-Latin-1 fonts are more recent and so don't
1757 (aset standard-display-table 2208 [32]) ; Latin-1 NBSP
1758 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1759 ;; don't contain a character at that position. Map it to the ASCII
1760 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1761 ;; U+2019, normally from the windows-1252 character set. XFree 4
1762 ;; fonts probably have the appropriate glyph at this position,
1763 ;; so they could use standard-display-8bit. It's better to use a
1764 ;; proper windows-1252 coding system. --fx]
1765 (aset standard-display-table 146 [39]))))
1767 (defun set-language-environment-coding-systems (language-name
1769 "Do various coding system setups for language environment LANGUAGE-NAME.
1771 The optional arg EOL-TYPE specifies the eol-type of the default value
1772 of `buffer-file-coding-system' set by this function."
1773 (let* ((priority (get-language-info language-name 'coding-priority))
1774 (default-coding (car priority)))
1776 (let ((categories (mapcar 'coding-system-category priority)))
1777 (set-default-coding-systems
1778 (if (memq eol-type '(0 1 2 unix dos mac))
1779 (coding-system-change-eol-conversion default-coding eol-type)
1781 (setq default-sendmail-coding-system default-coding)
1782 (set-coding-priority categories)
1784 (set (car categories) (car priority))
1785 (setq priority (cdr priority) categories (cdr categories)))
1786 (update-coding-systems-internal)))))
1788 (defsubst princ-list (&rest args)
1789 "Print all arguments with `princ', then print \"\n\"."
1790 (while args (princ (car args)) (setq args (cdr args)))
1793 (put 'describe-specified-language-support 'apropos-inhibit t)
1795 ;; Print a language specific information such as input methods,
1796 ;; charsets, and coding systems. This function is intended to be
1797 ;; called from the menu:
1798 ;; [menu-bar mule describe-language-environment LANGUAGE]
1799 ;; and should not run it by `M-x describe-current-input-method-function'.
1800 (defun describe-specified-language-support ()
1801 "Describe how Emacs supports the specified language environment."
1803 (let (language-name)
1804 (if (not (and (symbolp last-command-event)
1805 (or (not (eq last-command-event 'Default))
1806 (setq last-command-event 'English))
1807 (setq language-name (symbol-name last-command-event))))
1808 (error "Bogus calling sequence"))
1809 (describe-language-environment language-name)))
1811 (defun describe-language-environment (language-name)
1812 "Describe how Emacs supports language environment LANGUAGE-NAME."
1814 (list (read-language-name
1816 "Describe language environment (default, current choice): ")))
1817 (if (null language-name)
1818 (setq language-name current-language-environment))
1819 (if (or (null language-name)
1820 (null (get-language-info language-name 'documentation)))
1821 (error "No documentation for the specified language"))
1822 (if (symbolp language-name)
1823 (setq language-name (symbol-name language-name)))
1824 (let ((doc (get-language-info language-name 'documentation))
1826 (help-setup-xref (list #'describe-language-environment language-name)
1828 (with-output-to-temp-buffer (help-buffer)
1830 (set-buffer standard-output)
1831 (insert language-name " language environment\n\n")
1833 (insert doc "\n\n"))
1835 (let ((str (eval (get-language-info language-name 'sample-text))))
1837 (insert "Sample text:\n " str "\n\n")))
1839 (let ((input-method (get-language-info language-name 'input-method))
1840 (l (copy-sequence input-method-alist)))
1841 (insert "Input methods")
1843 (insert " (default, " input-method ")")
1844 (setq input-method (assoc input-method input-method-alist))
1845 (setq l (cons input-method (delete input-method l))))
1848 (when (string= language-name (nth 1 (car l)))
1849 (insert " " (car (car l)))
1850 (search-backward (car (car l)))
1851 (help-xref-button 0 'help-input-method (car (car l)))
1852 (goto-char (point-max))
1854 (if (stringp (nth 3 (car l)))
1856 (car (nth 3 (car l))))
1857 "\" in mode line)\n"))
1860 (insert "Character sets:\n")
1861 (let ((l (get-language-info language-name 'charset)))
1863 (insert " nothing specific to " language-name "\n")
1865 (insert " " (symbol-name (car l)))
1866 (search-backward (symbol-name (car l)))
1867 (help-xref-button 0 'help-character-set (car l))
1868 (goto-char (point-max))
1869 (insert ": " (charset-description (car l)) "\n")
1872 (insert "Coding systems:\n")
1873 (let ((l (get-language-info language-name 'coding-system)))
1875 (insert " nothing specific to " language-name "\n")
1877 (insert " " (symbol-name (car l)))
1878 (search-backward (symbol-name (car l)))
1879 (help-xref-button 0 'help-coding-system (car l))
1880 (goto-char (point-max))
1882 (coding-system-mnemonic (car l))
1883 "' in mode line):\n\t"
1884 (coding-system-doc-string (car l))
1886 (let ((aliases (coding-system-get (car l)
1887 'alias-coding-systems)))
1889 (insert "\t(alias:")
1891 (insert " " (symbol-name (car aliases)))
1892 (setq aliases (cdr aliases)))
1894 (setq l (cdr l)))))))))
1898 (defvar locale-translation-file-name nil
1899 "File name for the system's file of locale-name aliases, or nil if none.")
1901 ;; The following definitions might as well be marked as constants and
1902 ;; purecopied, since they're normally used on startup, and probably
1903 ;; should reflect the facilities of the base Emacs.
1904 (defconst locale-language-names
1907 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1908 ;; as specified in the Single Unix Spec, Version 2.
1909 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1910 ;; with additions from ISO 639/RA Newsletter No.1/1989;
1911 ;; see Internet RFC 2165 (1997-06) and
1912 ;; http://www.evertype.com/standards/iso639/iso639-en.html
1913 ;; TERRITORY is a country code taken from ISO 3166
1914 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
1915 ;; CODESET and MODIFIER are implementation-dependent.
1919 ("af" . "Latin-1") ; Afrikaans
1920 ("am" . "Ethiopic") ; Amharic
1921 ; ar Arabic glibc uses 8859-6
1926 ("be" . "Belarusian") ; Belarusian [Byelorussian until early 1990s]
1927 ("bg" . "Bulgarian") ; Bulgarian
1930 ; bn Bengali, Bangla
1932 ("br" . "Latin-1") ; Breton
1933 ("bs" . "Latin-2") ; Bosnian
1934 ("ca" . "Latin-1") ; Catalan
1937 ("cy" . "Welsh") ; Welsh
1938 ("da" . "Latin-1") ; Danish
1942 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
1943 ;; That's actually what the GNU locales define, modulo things like
1945 ("en" . "Latin-1") ; English
1946 ("eo" . "Latin-3") ; Esperanto
1948 ("et" . "Latin-4") ; Estonian
1949 ("eu" . "Latin-1") ; Basque
1950 ; fa Persian glibc uses utf-8
1951 ("fi" . "Latin-1") ; Finnish
1953 ("fo" . "Latin-1") ; Faroese
1954 ("fr" . "French") ; French
1955 ("fy" . "Latin-1") ; Frisian
1956 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
1957 ("gd" . "Latin-1") ; Scots Gaelic
1958 ("gl" . "Latin-1") ; Galician
1961 ("gv" . "Latin-8") ; Manx Gaelic glibc uses 8859-1
1964 ("hi" . "Devanagari") ; Hindi glibc uses utf-8
1965 ("hr" . "Croatian") ; Croatian
1966 ("hu" . "Latin-2") ; Hungarian
1969 ("id" . "Latin-1") ; Indonesian
1972 ("is" . "Latin-1") ; Icelandic
1973 ("it" . "Latin-1") ; Italian
1977 ("ka" . "Georgian") ; Georgian
1979 ("kl" . "Latin-1") ; Greenlandic
1985 ("kw" . "Latin-1") ; Cornish
1987 ("la" . "Latin-1") ; Latin
1988 ("lb" . "Latin-1") ; Luxemburgish
1990 ("lo" . "Lao") ; Laothian
1991 ("lt" . "Lithuanian")
1992 ("lv" . "Latvian") ; Latvian, Lettish
1994 ("mi" . "Latin-7") ; Maori
1995 ("mk" . "Cyrillic-ISO") ; Macedonian
1999 ("mr" . "Devanagari") ; Marathi glibc uses utf-8
2000 ("ms" . "Latin-1") ; Malay
2001 ("mt" . "Latin-3") ; Maltese
2004 ("ne" . "Devanagari") ; Nepali
2006 ("no" . "Latin-1") ; Norwegian
2007 ("oc" . "Latin-1") ; Occitan
2011 ("pl" . "Latin-2") ; Polish
2013 ("pt" . "Latin-1") ; Portuguese
2015 ("rm" . "Latin-1") ; Rhaeto-Romanic
2018 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
2019 ("ru" . "Cyrillic-ISO") ; Russian
2021 ("sa" . "Devanagari") ; Sanskrit
2025 ("sh" . "Latin-2") ; Serbo-Croatian
2028 ("sl" . "Slovenian")
2032 ("sq" . "Latin-1") ; Albanian
2033 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2034 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; per glibc
2038 ("sv" . "Swedish") ; Swedish
2039 ("sw" . "Latin-1") ; Swahili
2040 ; ta Tamil glibc uses utf-8
2041 ; te Telugu glibc uses utf-8
2046 ("tl" . "Latin-1") ; Tagalog
2049 ("tr" . "Latin-5") ; Turkish
2054 ("uk" . "Ukrainian") ; Ukrainian
2055 ; ur Urdu glibc uses utf-8
2056 ("uz" . "Latin-1") ; Uzbek
2057 ("vi" . "Vietnamese") ; glibc uses utf-8
2059 ("wa" . "Latin-1") ; Walloon
2062 ("yi" . "Windows-1255") ; Yiddish
2067 ; zh_CN.GB18030/GB18030 \
2069 ; zh_HK/BIG5-HKSCS \
2071 ; zh_TW.EUC-TW/EUC-TW \
2073 ("zh.*[._]big5" . "Chinese-BIG5")
2074 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
2075 ("zh_tw" . "Chinese-CNS")
2076 ("zh" . "Chinese-GB")
2079 ;; ISO standard locales
2081 ("posix$" . "ASCII")
2083 ;; The "IPA" Emacs language environment does not correspond
2084 ;; to any ISO 639 code, so let it stand for itself.
2087 ;; Nonstandard or obsolete language codes
2088 ("cz" . "Czech") ; e.g. Solaris 2.6
2089 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2090 ("iw" . "Hebrew") ; e.g. X11R6.4
2091 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2092 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2093 ("jp" . "Japanese") ; e.g. MS Windows
2094 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2095 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2097 "List of pairs of locale regexps and language names.
2098 The first element whose locale regexp matches the start of a downcased locale
2099 specifies the language name corresponding to that locale.
2100 If the language name is nil, there is no corresponding language environment.")
2102 (defconst locale-charset-language-names
2104 '((".*8859[-_]?1\\>" . "Latin-1")
2105 (".*8859[-_]?2\\>" . "Latin-2")
2106 (".*8859[-_]?3\\>" . "Latin-3")
2107 (".*8859[-_]?4\\>" . "Latin-4")
2108 (".*8859[-_]?9\\>" . "Latin-5")
2109 (".*8859[-_]?14\\>" . "Latin-8")
2110 (".*8859[-_]?15\\>" . "Latin-9")
2111 (".*utf\\(-?8\\)\\>" . "UTF-8")
2112 ;; utf-8@euro exists, so put this last. (@euro really specifies
2113 ;; the currency, rather than the charset.)
2114 (".*@euro\\>" . "Latin-9")))
2115 "List of pairs of locale regexps and charset language names.
2116 The first element whose locale regexp matches the start of a downcased locale
2117 specifies the language name whose charsets corresponds to that locale.
2118 This language name is used if its charsets disagree with the charsets of
2119 the language name that would otherwise be used for this locale.")
2121 (defconst locale-preferred-coding-systems
2123 '(("ja.*[._]euc" . japanese-iso-8bit)
2124 ("ja.*[._]jis7" . iso-2022-jp)
2125 ("ja.*[._]pck" . japanese-shift-jis)
2126 ("ja.*[._]sjis" . japanese-shift-jis)
2127 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2128 (".*[._]utf" . utf-8)))
2129 "List of pairs of locale regexps and preferred coding systems.
2130 The first element whose locale regexp matches the start of a downcased locale
2131 specifies the coding system to prefer when using that locale.")
2133 (defun locale-name-match (key alist)
2134 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2135 Return the value corresponding to the first regexp that matches the
2136 start of KEY, or nil if there is no match."
2138 (while (and alist (not element))
2139 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2140 (setq element (car alist)))
2141 (setq alist (cdr alist)))
2144 (defun set-locale-environment (&optional locale-name)
2145 "Set up multi-lingual environment for using LOCALE-NAME.
2146 This sets the language environment, the coding system priority,
2147 the default input method and sometimes other things.
2149 LOCALE-NAME should be a string
2150 which is the name of a locale supported by the system;
2151 often it is of the form xx_XX.CODE, where xx is a language,
2152 XX is a country, and CODE specifies a character set and coding system.
2153 For example, the locale name \"ja_JP.EUC\" might name a locale
2154 for Japanese in Japan using the `japanese-iso-8bit' coding-system.
2156 If LOCALE-NAME is nil, its value is taken from the environment
2157 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2159 The locale names supported by your system can typically be found in a
2160 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2161 will be translated according to the table specified by
2162 `locale-translation-file-name'.
2164 See also `locale-charset-language-names', `locale-language-names',
2165 `locale-preferred-coding-systems' and `locale-coding-system'."
2166 (interactive "sSet environment for locale: ")
2168 ;; Do this at runtime for the sake of binaries possibly transported
2169 ;; to a system without X.
2170 (setq locale-translation-file-name
2172 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2173 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
2174 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2176 ;; The following name appears after the X-related names above,
2177 ;; since the X-related names are what X actually uses.
2178 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2180 (while (and files (not (file-exists-p (car files))))
2181 (setq files (cdr files)))
2184 (let ((locale locale-name))
2187 ;; Use the first of these three environment variables
2188 ;; that has a nonempty value.
2189 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2191 (= 0 (length locale))) ; nil or empty string
2192 (setq locale (getenv (pop vars))))))
2196 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2197 ;; using the translation file that many systems have.
2198 (when locale-translation-file-name
2200 (insert-file-contents locale-translation-file-name)
2201 (when (re-search-forward
2202 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2203 (setq locale (buffer-substring (point) (line-end-position))))))
2205 ;; Leave the system locales alone if the caller did not specify
2206 ;; an explicit locale name, as their defaults are set from
2207 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2208 ;; want to set them to the same value as LC_CTYPE.
2210 (setq system-messages-locale locale)
2211 (setq system-time-locale locale))
2213 (setq locale (downcase locale))
2215 (let ((language-name
2216 (locale-name-match locale locale-language-names))
2217 (charset-language-name
2218 (locale-name-match locale locale-charset-language-names))
2220 (locale-name-match locale locale-preferred-coding-systems)))
2222 ;; Give preference to charset-language-name over language-name.
2223 (if (and charset-language-name
2225 (equal (get-language-info language-name 'charset)
2226 (get-language-info charset-language-name 'charset))))
2227 (setq language-name charset-language-name))
2231 ;; Set up for this character set. This is now the right way
2232 ;; to do it for both unibyte and multibyte modes.
2233 (set-language-environment language-name)
2235 ;; If default-enable-multibyte-characters is nil,
2236 ;; we are using single-byte characters,
2237 ;; so the display table and terminal coding system are irrelevant.
2238 (when default-enable-multibyte-characters
2239 (set-display-table-and-terminal-coding-system language-name))
2241 ;; Set the `keyboard-coding-system' if appropriate (tty
2242 ;; only). At least X and MS Windows can generate
2243 ;; multilingual input.
2244 (unless window-system
2245 (let ((kcs (or coding-system
2246 (car (get-language-info language-name
2248 (if kcs (set-keyboard-coding-system kcs))))
2250 (setq locale-coding-system
2251 (car (get-language-info language-name 'coding-priority))))
2254 (prefer-coding-system coding-system)
2255 (setq locale-coding-system coding-system))))))
2257 ;;; Charset property
2259 (defun get-charset-property (charset propname)
2260 "Return the value of CHARSET's PROPNAME property.
2261 This is the last value stored with
2262 (put-charset-property CHARSET PROPNAME VALUE)."
2263 (and (not (eq charset 'composition))
2264 (plist-get (charset-plist charset) propname)))
2266 (defun put-charset-property (charset propname value)
2267 "Store CHARSETS's PROPNAME property with value VALUE.
2268 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2269 (or (eq charset 'composition)
2270 (set-charset-plist charset
2271 (plist-put (charset-plist charset) propname value))))
2273 ;;; Character code property
2274 (put 'char-code-property-table 'char-table-extra-slots 0)
2276 (defvar char-code-property-table
2277 (make-char-table 'char-code-property-table)
2278 "Char-table containing a property list of each character code.
2280 See also the documentation of `get-char-code-property' and
2281 `put-char-code-property'.")
2283 (defun get-char-code-property (char propname)
2284 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2285 (let ((plist (aref char-code-property-table char)))
2287 (car (cdr (memq propname plist))))))
2289 (defun put-char-code-property (char propname value)
2290 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2291 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2292 (let ((plist (aref char-code-property-table char)))
2294 (let ((slot (memq propname plist)))
2296 (setcar (cdr slot) value)
2297 (nconc plist (list propname value))))
2298 (aset char-code-property-table char (list propname value)))))
2301 ;; Pretty description of encoded string
2303 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2304 (defvar iso-2022-control-alist
2312 (defun encoded-string-description (str coding-system)
2313 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2314 (setq str (string-as-unibyte str))
2316 (if (and coding-system (eq (coding-system-type coding-system) 2))
2317 ;; Try to get a pretty description for ISO 2022 escape sequences.
2318 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2319 (format "%02X" x))))
2320 (function (lambda (x) (format "0x%02X" x))))
2323 (defun encode-coding-char (char coding-system)
2324 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2325 If CODING-SYSTEM can't safely encode CHAR, return nil."
2326 (let ((str1 (string-as-multibyte (char-to-string char)))
2327 (str2 (string-as-multibyte (make-string 2 char)))
2328 (safe-chars (and coding-system
2329 (coding-system-get coding-system 'safe-chars)))
2330 (charset (char-charset char))
2332 (when (or (eq safe-chars t)
2334 (and safe-chars (aref safe-chars char)))
2335 ;; We must find the encoded string of CHAR. But, just encoding
2336 ;; CHAR will put extra control sequences (usually to designate
2337 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
2338 ;; To exclude such tailing bytes, we at first encode one-char
2339 ;; string and two-char string, then check how many bytes at the
2340 ;; tail of both encoded strings are the same.
2342 (setq enc1 (encode-coding-string str1 coding-system)
2344 enc2 (encode-coding-string str2 coding-system)
2346 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2347 (setq i1 (1- i1) i2 (1- i2)))
2349 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2350 ;; and they are the extra control sequences at the tail to
2352 (substring enc2 0 i2))))
2355 ;;; mule-cmds.el ends here