1 ;;; mule-cmds.el --- commands for mulitilingual environment -*-coding: iso-2022-7bit -*-
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Keywords: mule, multilingual
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
35 (autoload 'widget-value "wid-edit"))
37 (defvar mac-system-coding-system)
38 (defvar mac-system-locale)
40 ;;; MULE related key bindings and menus.
42 (defvar mule-keymap (make-sparse-keymap)
43 "Keymap for Mule (Multilingual environment) specific commands.")
45 ;; Keep "C-x C-m ..." for mule specific commands.
46 (define-key ctl-x-map "\C-m" mule-keymap)
48 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
49 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
50 (define-key mule-keymap "F" 'set-file-name-coding-system)
51 (define-key mule-keymap "t" 'set-terminal-coding-system)
52 (define-key mule-keymap "k" 'set-keyboard-coding-system)
53 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
54 (define-key mule-keymap "x" 'set-selection-coding-system)
55 (define-key mule-keymap "X" 'set-next-selection-coding-system)
56 (define-key mule-keymap "\C-\\" 'set-input-method)
57 (define-key mule-keymap "c" 'universal-coding-system-argument)
58 (define-key mule-keymap "l" 'set-language-environment)
60 (defvar mule-menu-keymap
61 (make-sparse-keymap "Mule (Multilingual Environment)")
62 "Keymap for Mule (Multilingual environment) menu specific commands.")
64 (defvar describe-language-environment-map
65 (make-sparse-keymap "Describe Language Environment"))
67 (defvar setup-language-environment-map
68 (make-sparse-keymap "Set Language Environment"))
70 (defvar set-coding-system-map
71 (make-sparse-keymap "Set Coding System"))
73 (define-key-after mule-menu-keymap [set-language-environment]
74 (list 'menu-item "Set Language Environment" setup-language-environment-map))
75 (define-key-after mule-menu-keymap [separator-mule]
78 (define-key-after mule-menu-keymap [toggle-input-method]
79 '(menu-item "Toggle Input Method" toggle-input-method)
81 (define-key-after mule-menu-keymap [set-input-method]
82 '(menu-item "Select Input Method..." set-input-method)
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]
89 (define-key-after mule-menu-keymap [set-various-coding-system]
90 (list 'menu-item "Set Coding Systems" set-coding-system-map
91 :enable 'default-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")
98 (define-key-after mule-menu-keymap [separator-coding-system]
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")
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")
110 (define-key-after mule-menu-keymap [describe-coding-system]
111 '(menu-item "Describe Coding System..." describe-coding-system)
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")
121 (define-key-after set-coding-system-map [universal-coding-system-argument]
122 '(menu-item "For Next Command" universal-coding-system-argument
123 :help "Coding system to be used by next command")
125 (define-key-after set-coding-system-map [separator-1]
128 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
129 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
130 :help "How to encode this buffer when saved")
132 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
133 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
134 :enable buffer-file-name
135 :help "Revisit this file immediately using specified coding system")
137 (define-key-after set-coding-system-map [set-file-name-coding-system]
138 '(menu-item "For File Name" set-file-name-coding-system
139 :help "How to decode/encode file names")
141 (define-key-after set-coding-system-map [separator-2]
145 (define-key-after set-coding-system-map [set-keyboard-coding-system]
146 '(menu-item "For Keyboard" set-keyboard-coding-system
147 :help "How to decode keyboard input")
149 (define-key-after set-coding-system-map [set-terminal-coding-system]
150 '(menu-item "For Terminal" set-terminal-coding-system
151 :enable (null (memq window-system '(x w32 mac)))
152 :help "How to encode terminal output")
154 (define-key-after set-coding-system-map [separator-3]
157 (define-key-after set-coding-system-map [set-selection-coding-system]
158 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
159 :visible (display-selections-p)
160 :help "How to en/decode data to/from selection/clipboard")
162 (define-key-after set-coding-system-map [set-next-selection-coding-system]
163 '(menu-item "For Next X Selection" set-next-selection-coding-system
164 :visible (display-selections-p)
165 :help "How to en/decode next selection/clipboard operation")
167 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
168 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
169 :visible (fboundp 'start-process)
170 :enable (get-buffer-process (current-buffer))
171 :help "How to en/decode I/O from/to subprocess connected to this buffer")
175 (define-key setup-language-environment-map
176 [Default] '(menu-item "Default" setup-specified-language-environment))
178 (define-key describe-language-environment-map
179 [Default] '(menu-item "Default" describe-specified-language-support))
181 ;; This should be a single character key binding because users use it
182 ;; very frequently while editing multilingual text. Now we can use
183 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
184 ;; convenient because it requires shifting on most keyboards. An
185 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
186 ;; but it won't be used that frequently.
187 (define-key global-map "\C-\\" 'toggle-input-method)
189 ;; This is no good because people often type Shift-SPC
190 ;; meaning to type SPC. -- rms.
191 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
192 ;; (define-key global-map [?\S- ] 'toggle-input-method)
194 ;;; Mule related hyperlinks.
195 (defconst help-xref-mule-regexp-template
196 (purecopy (concat "\\(\\<\\("
197 "\\(coding system\\)\\|"
198 "\\(input method\\)\\|"
199 "\\(character set\\)\\|"
202 ;; Note starting with word-syntax character:
203 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
205 (defun coding-system-change-eol-conversion (coding-system eol-type)
206 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
207 The returned coding system converts end-of-line by EOL-TYPE
208 but text as the same way as CODING-SYSTEM.
209 EOL-TYPE should be `unix', `dos', `mac', or nil.
210 If EOL-TYPE is nil, the returned coding system detects
211 how end-of-line is formatted automatically while decoding.
213 EOL-TYPE can be specified by an integer 0, 1, or 2.
214 They means `unix', `dos', and `mac' respectively."
215 (if (symbolp eol-type)
216 (setq eol-type (cond ((eq eol-type 'unix) 0)
217 ((eq eol-type 'dos) 1)
218 ((eq eol-type 'mac) 2)
220 ;; We call `coding-system-base' before `coding-system-eol-type',
221 ;; because the coding-system may not be initialized until then.
222 (let* ((base (coding-system-base coding-system))
223 (orig-eol-type (coding-system-eol-type coding-system)))
224 (cond ((vectorp orig-eol-type)
227 (aref orig-eol-type eol-type)))
230 ((= eol-type orig-eol-type)
232 ((progn (setq orig-eol-type (coding-system-eol-type base))
233 (vectorp orig-eol-type))
234 (aref orig-eol-type eol-type)))))
236 (defun coding-system-change-text-conversion (coding-system coding)
237 "Return a coding system which differs from CODING-SYSTEM in text conversion.
238 The returned coding system converts text by CODING
239 but end-of-line as the same way as CODING-SYSTEM.
240 If CODING is nil, the returned coding system detects
241 how text is formatted automatically while decoding."
242 (let ((eol-type (coding-system-eol-type coding-system)))
243 (coding-system-change-eol-conversion
244 (if coding coding 'undecided)
245 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
247 (defun toggle-enable-multibyte-characters (&optional arg)
248 "Change whether this buffer uses multibyte characters.
249 With arg, use multibyte characters if the arg is positive.
251 Note that this command does not convert the byte contents of
252 the buffer; it only changes the way those bytes are interpreted.
253 In general, therefore, this command *changes* the sequence of
254 characters that the current buffer contains.
256 We suggest you avoid using use this command unless you know what you
257 are doing. If you use it by mistake, and the buffer is now displayed
258 wrong, use this command again to toggle back to the right mode."
261 (if (null arg) (null enable-multibyte-characters)
262 (> (prefix-numeric-value arg) 0))))
263 (set-buffer-multibyte new-flag))
264 (force-mode-line-update))
266 (defun view-hello-file ()
267 "Display the HELLO file which list up many languages and characters."
269 ;; We have to decode the file in any environment.
270 (let ((default-enable-multibyte-characters t)
271 (coding-system-for-read 'iso-2022-7bit))
272 (view-file (expand-file-name "HELLO" data-directory))))
274 (defun universal-coding-system-argument (coding-system)
275 "Execute an I/O command using the specified coding system."
277 (let ((default (and buffer-file-coding-system
278 (not (eq (coding-system-type buffer-file-coding-system)
280 buffer-file-coding-system)))
281 (list (read-coding-system
283 (format "Coding system for following command (default %s): " default)
284 "Coding system for following command: ")
286 (let* ((keyseq (read-key-sequence
287 (format "Command to execute with %s:" coding-system)))
288 (cmd (key-binding keyseq))
291 (when (eq cmd 'universal-argument)
292 (call-interactively cmd)
294 ;; Process keys bound in `universal-argument-map'.
296 (setq keyseq (read-key-sequence nil t)
297 cmd (key-binding keyseq t))
298 (not (eq cmd 'universal-argument-other-key)))
299 (let ((current-prefix-arg prefix-arg)
300 ;; Have to bind `last-command-char' here so that
301 ;; `digit-argument', for instance, can compute the
303 (last-command-char (aref keyseq 0)))
304 (call-interactively cmd)))
306 ;; This is the final call to `universal-argument-other-key', which
307 ;; set's the final `prefix-arg.
308 (let ((current-prefix-arg prefix-arg))
309 (call-interactively cmd))
311 ;; Read the command to execute with the given prefix arg.
312 (setq prefix prefix-arg
313 keyseq (read-key-sequence nil t)
314 cmd (key-binding keyseq)))
316 (let ((coding-system-for-read coding-system)
317 (coding-system-for-write coding-system)
318 (coding-system-require-warning t)
319 (current-prefix-arg prefix))
321 (call-interactively cmd))))
323 (defun set-default-coding-systems (coding-system)
324 "Set default value of various coding systems to CODING-SYSTEM.
325 This sets the following coding systems:
326 o coding system of a newly created buffer
327 o default coding system for subprocess I/O
328 This also sets the following values:
329 o default value used as `file-name-coding-system' for converting file names
330 if CODING-SYSTEM is ASCII-compatible.
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 if CODING-SYSTEM is ASCII-compatible.."
334 (check-coding-system coding-system)
335 (setq-default buffer-file-coding-system coding-system)
336 (if (fboundp 'ucs-set-table-for-input)
337 (dolist (buffer (buffer-list))
338 (or (local-variable-p 'buffer-file-coding-system buffer)
339 (ucs-set-table-for-input buffer))))
341 (if (eq system-type 'darwin)
342 ;; The file-name coding system on Darwin systems is always utf-8.
343 (setq default-file-name-coding-system 'utf-8)
344 (if (and default-enable-multibyte-characters
345 (or (not coding-system)
346 (not (coding-system-get coding-system 'ascii-incompatible))))
347 (setq default-file-name-coding-system coding-system)))
348 ;; If coding-system is nil, honor that on MS-DOS as well, so
349 ;; that they could reset the terminal coding system.
350 (unless (and (eq window-system 'pc) coding-system)
351 (setq default-terminal-coding-system coding-system))
352 (if (or (not coding-system)
353 (not (coding-system-get coding-system 'ascii-incompatible)))
354 (setq default-keyboard-coding-system coding-system))
355 ;; Preserve eol-type from existing default-process-coding-systems.
356 ;; On non-unix-like systems in particular, these may have been set
357 ;; carefully by the user, or by the startup code, to deal with the
358 ;; users shell appropriately, so should not be altered by changing
359 ;; language environment.
361 (coding-system-change-text-conversion
362 (car default-process-coding-system) coding-system))
364 (coding-system-change-text-conversion
365 (cdr default-process-coding-system) coding-system)))
366 (setq default-process-coding-system
367 (cons output-coding input-coding))))
369 (defun prefer-coding-system (coding-system)
370 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
371 This also sets the following coding systems:
372 o coding system of a newly created buffer
373 o default coding system for subprocess I/O
374 This also sets the following values:
375 o default value used as `file-name-coding-system' for converting file names.
376 o default value for the command `set-terminal-coding-system' (not on MSDOS)
377 o default value for the command `set-keyboard-coding-system'
379 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
380 systems set by this function will use that type of EOL conversion.
382 This command does not change the default value of terminal coding system
383 for MS-DOS terminal, because DOS terminals only support a single coding
384 system, and Emacs automatically sets the default to that coding system at
387 A coding system that requires automatic detection of text
388 encoding (e.g. undecided, unix) can't be preferred.
390 See also `coding-category-list' and `coding-system-category'."
391 (interactive "zPrefer coding system: ")
392 (if (not (and coding-system (coding-system-p coding-system)))
393 (error "Invalid coding system `%s'" coding-system))
394 (let ((coding-category (coding-system-category coding-system))
395 (base (coding-system-base coding-system))
396 (eol-type (coding-system-eol-type coding-system)))
397 (if (not coding-category)
398 ;; CODING-SYSTEM is no-conversion or undecided.
399 (error "Can't prefer the coding system `%s'" coding-system))
400 (set coding-category (or base coding-system))
401 ;; Changing the binding of a coding category requires this call.
402 (update-coding-systems-internal)
403 (or (eq coding-category (car coding-category-list))
404 ;; We must change the order.
405 (set-coding-priority (list coding-category)))
406 (if (and base (interactive-p))
407 (message "Highest priority is set to %s (base of %s)"
409 ;; If they asked for specific EOL conversion, honor that.
410 (if (memq eol-type '(0 1 2))
412 (coding-system-change-eol-conversion base eol-type))
413 (setq coding-system base))
414 (set-default-coding-systems coding-system)))
416 (defvar sort-coding-systems-predicate nil
417 "If non-nil, a predicate function to sort coding systems.
419 It is called with two coding systems, and should return t if the first
420 one is \"less\" than the second.
422 The function `sort-coding-systems' use it.")
424 (defun sort-coding-systems (codings)
425 "Sort coding system list CODINGS by a priority of each coding system.
426 Returns the sorted list. CODINGS is modified by side effects.
428 If a coding system is most preferred, it has the highest priority.
429 Otherwise, a coding system corresponds to some MIME charset has higher
430 priorities. Among them, a coding system included in `coding-system'
431 key of the current language environment has higher priorities. See
432 also the documentation of `language-info-alist'.
434 If the variable `sort-coding-systems-predicate' (which see) is
435 non-nil, it is used to sort CODINGS in the different way than above."
436 (if sort-coding-systems-predicate
437 (sort codings sort-coding-systems-predicate)
438 (let* ((from-categories (mapcar #'(lambda (x) (symbol-value x))
439 coding-category-list))
440 (most-preferred (car from-categories))
441 (lang-preferred (get-language-info current-language-environment
445 (let ((base (coding-system-base x)))
446 ;; We calculate the priority number 0..255 by
447 ;; using the 8 bits PMMLCEII as this:
448 ;; P: 1 iff most preferred.
449 ;; MM: greater than 0 iff mime-charset.
450 ;; L: 1 iff one of the current lang. env.'s codings.
451 ;; C: 1 iff one of codings listed in the category list.
452 ;; E: 1 iff not XXX-with-esc
453 ;; II: if iso-2022 based, 0..3, else 1.
455 (lsh (if (eq base most-preferred) 1 0) 7)
457 (let ((mime (coding-system-get base 'mime-charset)))
458 ;; Prefer coding systems corresponding to a
461 ;; Lower utf-16 priority so that we
462 ;; normally prefer utf-8 to it, and put
463 ;; x-ctext below that.
464 (cond ((string-match "utf-16"
467 ((string-match "^x-" (symbol-name mime))
472 (lsh (if (memq base lang-preferred) 1 0) 4)
473 (lsh (if (memq base from-categories) 1 0) 3)
474 (lsh (if (string-match "-with-esc\\'"
477 (if (eq (coding-system-type base) 2)
478 ;; For ISO based coding systems, prefer
479 ;; one that doesn't use escape sequences.
480 (let ((flags (coding-system-flags base)))
481 (if (or (consp (aref flags 0))
482 (consp (aref flags 1))
483 (consp (aref flags 2))
484 (consp (aref flags 3)))
485 (if (or (aref flags 8) (aref flags 9))
490 (sort codings (function (lambda (x y)
491 (> (funcall func x) (funcall func y))))))))
493 (defun find-coding-systems-region (from to)
494 "Return a list of proper coding systems to encode a text between FROM and TO.
495 If FROM is a string, find coding systems in that instead of the buffer.
496 All coding systems in the list can safely encode any multibyte characters
499 If the text contains no multibyte characters, return a list of a single
500 element `undecided'."
501 (let ((codings (find-coding-systems-region-internal from to)))
503 ;; The text contains only ASCII characters. Any coding
506 ;; We need copy-sequence because sorting will alter the argument.
507 (sort-coding-systems (copy-sequence codings)))))
509 (defun find-coding-systems-string (string)
510 "Return a list of proper coding systems to encode STRING.
511 All coding systems in the list can safely encode any multibyte characters
514 If STRING contains no multibyte characters, return a list of a single
515 element `undecided'."
516 (find-coding-systems-region string nil))
518 (defun find-coding-systems-for-charsets (charsets)
519 "Return a list of proper coding systems to encode characters of CHARSETS.
520 CHARSETS is a list of character sets.
521 It actually checks at most the first 96 characters of each charset.
522 So, if a charset of dimension two is included in CHARSETS, the value may
523 contain a coding system that can't encode all characters of the charset."
524 (cond ((or (null charsets)
525 (and (= (length charsets) 1)
526 (eq 'ascii (car charsets))))
528 ((or (memq 'eight-bit-control charsets)
529 (memq 'eight-bit-graphic charsets))
530 '(raw-text emacs-mule))
534 (while (and codings charsets)
535 (setq charset (car charsets) charsets (cdr charsets))
536 (unless (eq charset 'ascii)
537 (setq str (make-string 96 32))
538 (if (= (charset-dimension charset) 1)
539 (if (= (charset-chars charset) 96)
541 (aset str i (make-char charset (+ i 32))))
543 (aset str i (make-char charset (+ i 33)))))
544 (if (= (charset-chars charset) 96)
546 (aset str i (make-char charset 32 (+ i 32))))
548 (aset str i (make-char charset 33 (+ i 33))))))
549 (setq l (find-coding-systems-string str))
553 (dolist (elt codings)
555 (setq ll (cons elt ll))))
556 (setq codings ll)))))
559 (defun find-multibyte-characters (from to &optional maxcount excludes)
560 "Find multibyte characters in the region specified by FROM and TO.
561 If FROM is a string, find multibyte characters in the string.
562 The return value is an alist of the following format:
563 ((CHARSET COUNT CHAR ...) ...)
565 CHARSET is a character set,
566 COUNT is a number of characters,
567 CHARs are the characters found from the character set.
568 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
569 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
571 For invalid characters, CHARs are actually strings."
576 (while (setq idx (string-match "[^\000-\177]" from idx))
577 (setq char (aref from idx)
578 charset (char-charset char))
579 (if (eq charset 'unknown)
580 (setq char (match-string 0)))
581 (if (or (memq charset '(unknown
582 eight-bit-control eight-bit-graphic))
583 (not (or (eq excludes t) (memq charset excludes))))
584 (let ((slot (assq charset chars)))
586 (if (not (memq char (nthcdr 2 slot)))
587 (let ((count (nth 1 slot)))
588 (setcar (cdr slot) (1+ count))
589 (if (or (not maxcount) (< count maxcount))
590 (nconc slot (list char)))))
591 (setq chars (cons (list charset 1 char) chars)))))
592 (setq idx (1+ idx))))
595 (while (re-search-forward "[^\000-\177]" to t)
596 (setq char (preceding-char)
597 charset (char-charset char))
598 (if (eq charset 'unknown)
599 (setq char (match-string 0)))
600 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
601 (not (or (eq excludes t) (memq charset excludes))))
602 (let ((slot (assq charset chars)))
604 (if (not (member char (nthcdr 2 slot)))
605 (let ((count (nth 1 slot)))
606 (setcar (cdr slot) (1+ count))
607 (if (or (not maxcount) (< count maxcount))
608 (nconc slot (list char)))))
609 (setq chars (cons (list charset 1 char) chars))))))))
613 (defun search-unencodable-char (coding-system)
614 "Search forward from point for a character that is not encodable.
615 It asks which coding system to check.
616 If such a character is found, set point after that character.
617 Otherwise, don't move point.
619 When called from a program, the value is a position of the found character,
620 or nil if all characters are encodable."
622 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
624 (format "Coding-system (default %s): " default)
626 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
629 (message "All following characters are encodable by %s" coding-system))
633 (defvar last-coding-system-specified nil
634 "Most recent coding system explicitly specified by the user when asked.
635 This variable is set whenever Emacs asks the user which coding system
636 to use in order to write a file. If you set it to nil explicitly,
637 then call `write-region', then afterward this variable will be non-nil
638 only if the user was explicitly asked and specified a coding system.")
640 (defvar select-safe-coding-system-accept-default-p nil
641 "If non-nil, a function to control the behavior of coding system selection.
642 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
643 function `select-safe-coding-system' (which see). This variable
644 overrides that argument.")
646 (defun select-safe-coding-system-interactively (from to codings unsafe
647 &optional rejected default)
648 "Select interactively a coding system for the region FROM ... TO.
649 FROM can be a string, as in `write-region'.
650 CODINGS is the list of base coding systems known to be safe for this region,
651 typically obtained with `find-coding-systems-region'.
652 UNSAFE is a list of coding systems known to be unsafe for this region.
653 REJECTED is a list of coding systems which were safe but for some reason
654 were not recommended in the particular context.
655 DEFAULT is the coding system to use by default in the query."
656 ;; At first, if some defaults are unsafe, record at most 11
657 ;; problematic characters and their positions for them by turning
660 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
663 (mapcar #'(lambda (coding)
666 (mapcar #'(lambda (pos)
667 (cons pos (aref from pos)))
668 (unencodable-char-position
669 0 (length from) coding
671 (mapcar #'(lambda (pos)
672 (cons pos (char-after pos)))
673 (unencodable-char-position
674 from to coding 11)))))
677 ;; Change each safe coding system to the corresponding
678 ;; mime-charset name if it is also a coding system. Such a name
679 ;; is more friendly to users.
683 (setq mime-charset (coding-system-get (car l) 'mime-charset))
684 (if (and mime-charset (coding-system-p mime-charset))
685 (setcar l mime-charset))
688 ;; Don't offer variations with locking shift, which you
689 ;; basically never want.
691 (dolist (elt codings (setq codings (nreverse l)))
692 (unless (or (eq 'coding-category-iso-7-else
693 (coding-system-category elt))
694 (eq 'coding-category-iso-8-else
695 (coding-system-category elt)))
698 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
699 ;; else is available.
703 (delq 'no-conversion codings)))
704 '(raw-text emacs-mule no-conversion)))
706 (let ((window-configuration (current-window-configuration))
707 (bufname (buffer-name))
710 ;; If some defaults are unsafe, make sure the offending
711 ;; buffer is displayed.
712 (when (and unsafe (not (stringp from)))
713 (pop-to-buffer bufname)
714 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
716 ;; Then ask users to select one from CODINGS while showing
717 ;; the reason why none of the defaults are not used.
718 (with-output-to-temp-buffer "*Warning*"
719 (with-current-buffer standard-output
720 (if (and (null rejected) (null unsafe))
721 (insert "No default coding systems to try for "
723 (format "string \"%s\"." from)
724 (format "buffer `%s'." bufname)))
726 "These default coding systems were tried to encode"
728 (concat " \"" (if (> (length from) 10)
729 (concat (substring from 0 10) "...\"")
731 (format " text\nin the buffer `%s'" bufname))
735 (dolist (x (append rejected unsafe))
736 (princ " ") (princ (car x)))
738 (fill-region-as-paragraph pos (point)))
740 (insert "These safely encodes the target text,
741 but it is not recommended for encoding text in this context,
742 e.g., for sending an email message.\n ")
744 (princ " ") (princ x))
747 (insert (if rejected "And the others"
748 "However, each of them")
749 " encountered these problematic characters:\n")
750 (dolist (coding unsafe)
751 (insert (format " %s:" (car coding)))
754 #'(lambda (bufname pos)
755 (when (buffer-live-p (get-buffer bufname))
756 (pop-to-buffer bufname)
759 #'(lambda (bufname pos coding)
760 (when (buffer-live-p (get-buffer bufname))
761 (pop-to-buffer bufname)
765 (search-unencodable-char coding)
766 (forward-char -1))))))
767 (dolist (elt (cdr coding))
770 (insert (if (< i 10) (cdr elt) "..."))
776 "mouse-2, RET: jump to this character"
778 'help-args (list bufname (car elt)))
783 "mouse-2, RET: next unencodable character"
785 'help-args (list bufname (car elt)
790 The first problematic character is at point in the displayed buffer,\n"
791 (substitute-command-keys "\
792 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
794 one of the following safe coding systems, or edit the buffer:\n")
798 (princ " ") (princ x))
800 (fill-region-as-paragraph pos (point)))
801 (insert "Or specify any other coding system
802 at the risk of losing the problematic characters.\n")))
804 ;; Read a coding system.
807 (format "Select coding system (default %s): " default)
809 (setq last-coding-system-specified coding-system))
811 (kill-buffer "*Warning*")
812 (set-window-configuration window-configuration)
815 (defun select-safe-coding-system (from to &optional default-coding-system
816 accept-default-p file)
817 "Ask a user to select a safe coding system from candidates.
818 The candidates of coding systems which can safely encode a text
819 between FROM and TO are shown in a popup window. Among them, the most
820 proper one is suggested as the default.
822 The list of `buffer-file-coding-system' of the current buffer,
823 the `default-buffer-file-coding-system', and the
824 most preferred coding system (if it corresponds to a MIME charset) is
825 treated as the default coding system list. Among them, the first one
826 that safely encodes the text is normally selected silently and
827 returned without any user interaction. See also the command
828 `prefer-coding-system'.
830 However, the user is queried if the chosen coding system is
831 inconsistent with what would be selected by `find-auto-coding' from
832 coding cookies &c. if the contents of the region were read from a
833 file. (That could lead to data corruption in a file subsequently
834 re-visited and edited.)
836 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
837 list of coding systems to be prepended to the default coding system
838 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
839 element is t, the cdr part is used as the default coding system list,
840 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
841 and the most preferred coding system are not used.
843 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
844 determine the acceptability of the silently selected coding system.
845 It is called with that coding system, and should return nil if it
846 should not be silently selected and thus user interaction is required.
848 Optional 5th arg FILE is the file name to use for this purpose.
849 That is different from `buffer-file-name' when handling `write-region'
852 The variable `select-safe-coding-system-accept-default-p', if
853 non-nil, overrides ACCEPT-DEFAULT-P.
855 Kludgy feature: if FROM is a string, the string is the target text,
857 (if (and default-coding-system
858 (not (listp default-coding-system)))
859 (setq default-coding-system (list default-coding-system)))
861 (let ((no-other-defaults nil)
863 (unless (or (stringp from) find-file-literally)
864 ;; Find an auto-coding that is specified for the the current
865 ;; buffer and file from the region FROM and TO.
870 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
873 (if (coding-system-p (car auto-cs))
874 (setq auto-cs (car auto-cs))
878 Invalid coding system `%s' is specified
879 for the current buffer/file by the %s.
880 It is highly recommended to fix it before writing to a file."
882 (if (eq (cdr auto-cs) :coding) ":coding tag"
883 (format "variable `%s'" (cdr auto-cs)))))
884 (or (yes-or-no-p "Really proceed with writing? ")
885 (error "Save aborted"))
886 (setq auto-cs nil))))))
888 (if (eq (car default-coding-system) t)
889 (setq no-other-defaults t
890 default-coding-system (cdr default-coding-system)))
892 ;; Change elements of the list to (coding . base-coding).
893 (setq default-coding-system
894 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
895 default-coding-system))
897 (if (and auto-cs (not no-other-defaults))
898 ;; If the file has a coding cookie, try to use it before anything
899 ;; else (i.e. before default-coding-system which will typically come
900 ;; from file-coding-system-alist).
901 (let ((base (coding-system-base auto-cs)))
902 (or (memq base '(nil undecided))
903 (rassq base default-coding-system)
904 (push (cons auto-cs base) default-coding-system))))
906 (unless no-other-defaults
907 ;; If buffer-file-coding-system is not nil nor undecided, append it
909 (if buffer-file-coding-system
910 (let ((base (coding-system-base buffer-file-coding-system)))
911 (or (eq base 'undecided)
912 (rassq base default-coding-system)
913 (setq default-coding-system
914 (append default-coding-system
915 (list (cons buffer-file-coding-system base)))))))
917 ;; If default-buffer-file-coding-system is not nil nor undecided,
918 ;; append it to the defaults.
919 (if default-buffer-file-coding-system
920 (let ((base (coding-system-base default-buffer-file-coding-system)))
921 (or (eq base 'undecided)
922 (rassq base default-coding-system)
923 (setq default-coding-system
924 (append default-coding-system
925 (list (cons default-buffer-file-coding-system
928 ;; If the most preferred coding system has the property mime-charset,
929 ;; append it to the defaults.
930 (let ((tail coding-category-list)
932 (while (and tail (not (setq preferred (symbol-value (car tail)))))
933 (setq tail (cdr tail)))
934 (and (coding-system-p preferred)
935 (setq base (coding-system-base preferred))
936 (coding-system-get preferred 'mime-charset)
937 (not (rassq base default-coding-system))
938 (setq default-coding-system
939 (append default-coding-system
940 (list (cons preferred base)))))))
942 (if select-safe-coding-system-accept-default-p
943 (setq accept-default-p select-safe-coding-system-accept-default-p))
945 ;; Decide the eol-type from the top of the default codings,
946 ;; buffer-file-coding-system, or
947 ;; default-buffer-file-coding-system.
948 (if default-coding-system
949 (let ((default-eol-type (coding-system-eol-type
950 (caar default-coding-system))))
951 (if (and (vectorp default-eol-type) buffer-file-coding-system)
952 (setq default-eol-type (coding-system-eol-type
953 buffer-file-coding-system)))
954 (if (and (vectorp default-eol-type) default-buffer-file-coding-system)
955 (setq default-eol-type (coding-system-eol-type
956 default-buffer-file-coding-system)))
957 (if (and default-eol-type (not (vectorp default-eol-type)))
958 (dolist (elt default-coding-system)
959 (setcar elt (coding-system-change-eol-conversion
960 (car elt) default-eol-type))))))
962 (let ((codings (find-coding-systems-region from to))
964 safe rejected unsafe)
965 (if (eq (car codings) 'undecided)
966 ;; Any coding system is ok.
967 (setq coding-system (caar default-coding-system))
968 ;; Reverse the list so that elements are accumulated in safe,
969 ;; rejected, and unsafe in the correct order.
970 (setq default-coding-system (nreverse default-coding-system))
972 ;; Classify the defaults into safe, rejected, and unsafe.
973 (dolist (elt default-coding-system)
974 (if (memq (cdr elt) codings)
975 (if (and (functionp accept-default-p)
976 (not (funcall accept-default-p (cdr elt))))
977 (push (car elt) rejected)
978 (push (car elt) safe))
979 (push (car elt) unsafe)))
981 (setq coding-system (car safe))))
983 ;; If all the defaults failed, ask a user.
984 (when (not coding-system)
985 (setq coding-system (select-safe-coding-system-interactively
986 from to codings unsafe rejected (car codings))))
988 ;; Check we're not inconsistent with what `coding:' spec &c would
989 ;; give when file is re-read.
990 ;; But don't do this if we explicitly ignored the cookie
991 ;; by using `find-file-literally'.
995 (memq (coding-system-type coding-system) '(0 5)))))
996 ;; Merge coding-system and auto-cs as far as possible.
997 (if (not coding-system)
998 (setq coding-system auto-cs)
1000 (setq auto-cs coding-system)
1001 (let ((eol-type-1 (coding-system-eol-type coding-system))
1002 (eol-type-2 (coding-system-eol-type auto-cs)))
1003 (if (eq (coding-system-base coding-system) 'undecided)
1004 (setq coding-system (coding-system-change-text-conversion
1005 coding-system auto-cs))
1006 (if (eq (coding-system-base auto-cs) 'undecided)
1007 (setq auto-cs (coding-system-change-text-conversion
1008 auto-cs coding-system))))
1009 (if (vectorp eol-type-1)
1010 (or (vectorp eol-type-2)
1011 (setq coding-system (coding-system-change-eol-conversion
1012 coding-system eol-type-2)))
1013 (if (vectorp eol-type-2)
1014 (setq auto-cs (coding-system-change-eol-conversion
1015 auto-cs eol-type-1)))))))
1018 ;; Don't barf if writing a compressed file, say.
1019 ;; This check perhaps isn't ideal, but is probably
1020 ;; the best thing to do.
1021 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1022 (not (coding-system-equal coding-system auto-cs)))
1023 (unless (yes-or-no-p
1024 (format "Selected encoding %s disagrees with \
1025 %s specified by file contents. Really save (else edit coding cookies \
1026 and try again)? " coding-system auto-cs))
1027 (error "Save aborted"))))
1030 (setq select-safe-coding-system-function 'select-safe-coding-system)
1032 (defun select-message-coding-system ()
1033 "Return a coding system to encode the outgoing message of the current buffer.
1034 It at first tries the first coding system found in these variables
1036 (1) local value of `buffer-file-coding-system'
1037 (2) value of `sendmail-coding-system'
1038 (3) value of `default-sendmail-coding-system'
1039 (4) value of `default-buffer-file-coding-system'
1040 If the found coding system can't encode the current buffer,
1041 or none of them are bound to a coding system,
1042 it asks the user to select a proper coding system."
1043 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1044 buffer-file-coding-system)
1045 sendmail-coding-system
1046 default-sendmail-coding-system
1047 default-buffer-file-coding-system)))
1048 (if (eq coding 'no-conversion)
1049 ;; We should never use no-conversion for outgoing mail.
1051 (if (fboundp select-safe-coding-system-function)
1053 (funcall select-safe-coding-system-function
1054 (point-min) (point-max) coding
1055 (function (lambda (x)
1056 (coding-system-get x 'mime-charset))))))
1058 ;; Be sure to use LF for end-of-line.
1059 (setq coding (coding-system-change-eol-conversion coding 'unix))
1060 ;; No coding system is decided. Usually this is the case that
1061 ;; the current buffer contains only ASCII. So, we hope
1062 ;; iso-8859-1 works.
1063 (setq coding 'iso-8859-1-unix))
1066 ;;; Language support stuff.
1068 (defvar language-info-alist nil
1069 "Alist of language environment definitions.
1070 Each element looks like:
1071 (LANGUAGE-NAME . ((KEY . INFO) ...))
1072 where LANGUAGE-NAME is a string, the name of the language environment,
1073 KEY is a symbol denoting the kind of information, and
1074 INFO is the data associated with KEY.
1075 Meaningful values for KEY include
1077 documentation value is documentation of what this language environment
1078 is meant for, and how to use it.
1079 charset value is a list of the character sets used by this
1080 language environment.
1081 sample-text value is an expression which is evalled to generate
1082 a line of text written using characters appropriate
1083 for this language environment.
1084 setup-function value is a function to call to switch to this
1085 language environment.
1086 exit-function value is a function to call to leave this
1087 language environment.
1088 coding-system value is a list of coding systems that are good
1089 for saving text written in this language environment.
1090 This list serves as suggestions to the user;
1091 in effect, as a kind of documentation.
1092 coding-priority value is a list of coding systems for this language
1093 environment, in order of decreasing priority.
1094 This is used to set up the coding system priority
1095 list when you switch to this language environment.
1096 nonascii-translation
1097 value is a translation table to be set in the
1098 variable `nonascii-translation-table' in this
1099 language environment, or a character set from
1100 which `nonascii-insert-offset' is calculated.
1101 input-method value is a default input method for this language
1103 features value is a list of features requested in this
1104 language environment.
1105 ctext-non-standard-encodings
1106 value is a list of non-standard encoding
1107 names used in extended segments of CTEXT.
1109 `ctext-non-standard-encodings' for more
1112 The following keys take effect only when multibyte characters are
1113 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1116 unibyte-syntax value is a library name to load to set
1117 unibyte 8-bit character syntaxes for this
1118 language environment.
1120 unibyte-display value is a coding system to encode characters
1121 for the terminal. Characters in the range
1122 of 160 to 255 display not as octal escapes,
1123 but as non-ASCII characters in this language
1126 (defun get-language-info (lang-env key)
1127 "Return information listed under KEY for language environment LANG-ENV.
1128 KEY is a symbol denoting the kind of information.
1129 For a list of useful values for KEY and their meanings,
1130 see `language-info-alist'."
1131 (if (symbolp lang-env)
1132 (setq lang-env (symbol-name lang-env)))
1133 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1135 (cdr (assq key (cdr lang-slot))))))
1137 (defun set-language-info (lang-env key info)
1138 "Modify part of the definition of language environment LANG-ENV.
1139 Specifically, this stores the information INFO under KEY
1140 in the definition of this language environment.
1141 KEY is a symbol denoting the kind of information.
1142 INFO is the value for that information.
1144 For a list of useful values for KEY and their meanings,
1145 see `language-info-alist'."
1146 (if (symbolp lang-env)
1147 (setq lang-env (symbol-name lang-env)))
1148 (set-language-info-internal lang-env key info)
1149 (if (equal lang-env current-language-environment)
1150 (cond ((eq key 'coding-priority)
1151 (set-language-environment-coding-systems lang-env))
1152 ((eq key 'input-method)
1153 (set-language-environment-input-method lang-env))
1154 ((eq key 'nonascii-translation)
1155 (set-language-environment-nonascii-translation lang-env))
1157 (set-language-environment-charset lang-env))
1158 ((eq key 'overriding-fontspec)
1159 (set-language-environment-fontset lang-env))
1160 ((and (not default-enable-multibyte-characters)
1161 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1162 (set-language-environment-unibyte lang-env)))))
1164 (defun set-language-info-internal (lang-env key info)
1166 Arguments are the same as `set-language-info'."
1167 (let (lang-slot key-slot)
1168 (setq lang-slot (assoc lang-env language-info-alist))
1169 (if (null lang-slot) ; If no slot for the language, add it.
1170 (setq lang-slot (list lang-env)
1171 language-info-alist (cons lang-slot language-info-alist)))
1172 (setq key-slot (assq key lang-slot))
1173 (if (null key-slot) ; If no slot for the key, add it.
1175 (setq key-slot (list key))
1176 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1177 (setcdr key-slot (purecopy info))
1178 ;; Update the custom-type of `current-language-environment'.
1179 (put 'current-language-environment 'custom-type
1180 (cons 'choice (mapcar
1183 (sort (mapcar 'car language-info-alist) 'string<))))))
1185 (defun set-language-info-alist (lang-env alist &optional parents)
1186 "Store ALIST as the definition of language environment LANG-ENV.
1187 ALIST is an alist of KEY and INFO values. See the documentation of
1188 `language-info-alist' for the meanings of KEY and INFO.
1190 Optional arg PARENTS is a list of parent menu names; it specifies
1191 where to put this language environment in the
1192 Describe Language Environment and Set Language Environment menus.
1193 For example, (\"European\") means to put this language environment
1194 in the European submenu in each of those two menus."
1195 (if (symbolp lang-env)
1196 (setq lang-env (symbol-name lang-env)))
1197 (let ((describe-map describe-language-environment-map)
1198 (setup-map setup-language-environment-map))
1201 map parent-symbol parent prompt)
1203 (if (symbolp (setq parent-symbol (car l)))
1204 (setq parent (symbol-name parent))
1205 (setq parent parent-symbol parent-symbol (intern parent)))
1206 (setq map (lookup-key describe-map (vector parent-symbol)))
1207 ;; This prompt string is for define-prefix-command, so
1208 ;; that the map it creates will be suitable for a menu.
1209 (or map (setq prompt (format "%s Environment" parent)))
1212 (setq map (intern (format "describe-%s-environment-map"
1213 (downcase parent))))
1214 (define-prefix-command map nil prompt)
1215 (define-key-after describe-map (vector parent-symbol)
1216 (cons parent map) t)))
1217 (setq describe-map (symbol-value map))
1218 (setq map (lookup-key setup-map (vector parent-symbol)))
1221 (setq map (intern (format "setup-%s-environment-map"
1222 (downcase parent))))
1223 (define-prefix-command map nil prompt)
1224 (define-key-after setup-map (vector parent-symbol)
1225 (cons parent map) t)))
1226 (setq setup-map (symbol-value map))
1229 ;; Set up menu items for this language env.
1230 (let ((doc (assq 'documentation alist)))
1232 (define-key-after describe-map (vector (intern lang-env))
1233 (cons lang-env 'describe-specified-language-support) t)))
1234 (define-key-after setup-map (vector (intern lang-env))
1235 (cons lang-env 'setup-specified-language-environment) t)
1238 (set-language-info-internal lang-env (car elt) (cdr elt)))
1240 (if (equal lang-env current-language-environment)
1241 (set-language-environment lang-env))))
1243 (defun read-language-name (key prompt &optional default)
1244 "Read a language environment name which has information for KEY.
1245 If KEY is nil, read any language environment.
1246 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1247 This returns a language environment name as a string."
1248 (let* ((completion-ignore-case t)
1249 (name (completing-read prompt
1252 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1253 t nil nil default)))
1254 (if (and (> (length name) 0)
1256 (get-language-info name key)))
1259 ;;; Multilingual input methods.
1261 "LEIM: Libraries of Emacs Input Methods."
1264 (defconst leim-list-file-name "leim-list.el"
1265 "Name of LEIM list file.
1266 This file contains a list of libraries of Emacs input methods (LEIM)
1267 in the format of Lisp expression for registering each input method.
1268 Emacs loads this file at startup time.")
1270 (defvar leim-list-header (format
1271 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1273 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1274 ;; methods in the same directory as this file. Loading this file
1275 ;; registers all the input methods in Emacs.
1277 ;; Each entry has the form:
1278 ;; (register-input-method
1279 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1280 ;; TITLE DESCRIPTION
1282 ;; See the function `register-input-method' for the meanings of the arguments.
1284 ;; If this directory is included in load-path, Emacs automatically
1285 ;; loads this file at startup time.
1288 leim-list-file-name)
1289 "Header to be inserted in LEIM list file.")
1291 (defvar leim-list-entry-regexp "^(register-input-method"
1292 "Regexp matching head of each entry in LEIM list file.
1293 See also the variable `leim-list-header'")
1295 (defvar update-leim-list-functions
1296 '(quail-update-leim-list-file)
1297 "List of functions to call to update LEIM list file.
1298 Each function is called with one arg, LEIM directory name.")
1300 (defun update-leim-list-file (&rest dirs)
1301 "Update LEIM list file in directories DIRS."
1302 (let ((functions update-leim-list-functions))
1304 (apply (car functions) dirs)
1305 (setq functions (cdr functions)))))
1307 (defvar current-input-method nil
1308 "The current input method for multilingual text.
1309 If nil, that means no input method is activated now.")
1310 (make-variable-buffer-local 'current-input-method)
1311 (put 'current-input-method 'permanent-local t)
1313 (defvar current-input-method-title nil
1314 "Title string of the current input method shown in mode line.")
1315 (make-variable-buffer-local 'current-input-method-title)
1316 (put 'current-input-method-title 'permanent-local t)
1318 (defcustom default-input-method nil
1319 "*Default input method for multilingual text (a string).
1320 This is the input method activated automatically by the command
1321 `toggle-input-method' (\\[toggle-input-method])."
1322 :link '(custom-manual "(emacs)Input Methods")
1324 :type '(choice (const nil) (string
1325 :completion-ignore-case t
1326 :complete-function widget-string-complete
1327 :completion-alist input-method-alist
1328 :prompt-history input-method-history))
1329 :set-after '(current-language-environment))
1331 (put 'input-method-function 'permanent-local t)
1333 (defvar input-method-history nil
1334 "History list for some commands that read input methods.")
1335 (make-variable-buffer-local 'input-method-history)
1336 (put 'input-method-history 'permanent-local t)
1338 (defvar inactivate-current-input-method-function nil
1339 "Function to call for inactivating the current input method.
1340 Every input method should set this to an appropriate value when activated.
1341 This function is called with no argument.
1343 This function should never change the value of `current-input-method'.
1344 It is set to nil by the function `inactivate-input-method'.")
1345 (make-variable-buffer-local 'inactivate-current-input-method-function)
1346 (put 'inactivate-current-input-method-function 'permanent-local t)
1348 (defvar describe-current-input-method-function nil
1349 "Function to call for describing the current input method.
1350 This function is called with no argument.")
1351 (make-variable-buffer-local 'describe-current-input-method-function)
1352 (put 'describe-current-input-method-function 'permanent-local t)
1354 (defvar input-method-alist nil
1355 "Alist of input method names vs how to use them.
1356 Each element has the form:
1357 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1358 See the function `register-input-method' for the meanings of the elements.")
1360 (defun register-input-method (input-method lang-env &rest args)
1361 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1363 INPUT-METHOD and LANG-ENV are symbols or strings.
1364 ACTIVATE-FUNC is a function to call to activate this method.
1365 TITLE is a string to show in the mode line when this method is active.
1366 DESCRIPTION is a string describing this method and what it is good for.
1367 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1368 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1370 This function is mainly used in the file \"leim-list.el\" which is
1371 created at Emacs build time, registering all Quail input methods
1372 contained in the Emacs distribution.
1374 In case you want to register a new Quail input method by yourself, be
1375 careful to use the same input method title as given in the third
1376 parameter of `quail-define-package'. (If the values are different, the
1377 string specified in this function takes precedence.)
1379 The commands `describe-input-method' and `list-input-methods' need
1380 these duplicated values to show some information about input methods
1381 without loading the relevant Quail packages.
1382 \n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
1383 (if (symbolp lang-env)
1384 (setq lang-env (symbol-name lang-env)))
1385 (if (symbolp input-method)
1386 (setq input-method (symbol-name input-method)))
1387 (let ((info (cons lang-env args))
1388 (slot (assoc input-method input-method-alist)))
1391 (setq slot (cons input-method info))
1392 (setq input-method-alist (cons slot input-method-alist)))))
1394 (defun read-input-method-name (prompt &optional default inhibit-null)
1395 "Read a name of input method from a minibuffer prompting with PROMPT.
1396 If DEFAULT is non-nil, use that as the default,
1397 and substitute it into PROMPT at the first `%s'.
1398 If INHIBIT-NULL is non-nil, null input signals an error.
1400 The return value is a string."
1402 (setq prompt (format prompt default)))
1403 (let* ((completion-ignore-case t)
1404 ;; As it is quite normal to change input method in the
1405 ;; minibuffer, we must enable it even if
1406 ;; enable-recursive-minibuffers is currently nil.
1407 (enable-recursive-minibuffers t)
1408 ;; This binding is necessary because input-method-history is
1410 (input-method (completing-read prompt input-method-alist
1411 nil t nil 'input-method-history
1413 (if (and input-method (symbolp input-method))
1414 (setq input-method (symbol-name input-method)))
1415 (if (> (length input-method) 0)
1418 (error "No valid input method is specified")))))
1420 (defun activate-input-method (input-method)
1421 "Switch to input method INPUT-METHOD for the current buffer.
1422 If some other input method is already active, turn it off first.
1423 If INPUT-METHOD is nil, deactivate any current input method."
1424 (if (and input-method (symbolp input-method))
1425 (setq input-method (symbol-name input-method)))
1426 (if (and current-input-method
1427 (not (string= current-input-method input-method)))
1428 (inactivate-input-method))
1429 (unless (or current-input-method (null input-method))
1430 (let ((slot (assoc input-method input-method-alist)))
1432 (error "Can't activate input method `%s'" input-method))
1433 (setq current-input-method-title nil)
1434 (let ((func (nth 2 slot)))
1435 (if (functionp func)
1436 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1437 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1439 (require (cdr func))
1440 (apply (car func) input-method (nthcdr 5 slot)))
1441 (error "Can't activate input method `%s'" input-method))))
1442 (setq current-input-method input-method)
1443 (or (stringp current-input-method-title)
1444 (setq current-input-method-title (nth 3 slot)))
1446 (run-hooks 'input-method-activate-hook)
1447 (force-mode-line-update)))))
1449 (defun inactivate-input-method ()
1450 "Turn off the current input method."
1451 (when current-input-method
1452 (if input-method-history
1453 (unless (string= current-input-method (car input-method-history))
1454 (setq input-method-history
1455 (cons current-input-method
1456 (delete current-input-method input-method-history))))
1457 (setq input-method-history (list current-input-method)))
1459 (funcall inactivate-current-input-method-function)
1461 (run-hooks 'input-method-inactivate-hook)
1462 (setq current-input-method nil
1463 input-method-function nil
1464 current-input-method-title nil)
1465 (force-mode-line-update)))))
1467 (defun set-input-method (input-method &optional interactive)
1468 "Select and activate input method INPUT-METHOD for the current buffer.
1469 This also sets the default input method to the one you specify.
1470 If INPUT-METHOD is nil, this function turns off the input method, and
1471 also causes you to be prompted for a name of an input method the next
1472 time you invoke \\[toggle-input-method].
1473 When called interactively, the optional arg INTERACTIVE is non-nil,
1474 which marks the variable `default-input-method' as set for Custom buffers.
1476 To deactivate the input method interactively, use \\[toggle-input-method].
1477 To deactivate it programmatically, use \\[inactivate-input-method]."
1479 (let* ((default (or (car input-method-history) default-input-method)))
1480 (list (read-input-method-name
1481 (if default "Select input method (default %s): " "Select input method: ")
1484 (activate-input-method input-method)
1485 (setq default-input-method input-method)
1487 (customize-mark-as-set 'default-input-method))
1488 default-input-method)
1490 (defun toggle-input-method (&optional arg interactive)
1491 "Enable or disable multilingual text input method for the current buffer.
1492 Only one input method can be enabled at any time in a given buffer.
1494 The normal action is to enable an input method if none was
1495 enabled, and disable the current one otherwise. Which input method
1496 to enable can be determined in various ways--either the one most
1497 recently used, or the one specified by `default-input-method', or
1498 as a last resort by reading the name of an input method in the
1501 With a prefix argument, read an input method name with the minibuffer
1502 and enable that one. The default is the most recent input method specified
1503 \(not including the currently active input method, if any).
1505 When called interactively, the optional arg INTERACTIVE is non-nil,
1506 which marks the variable `default-input-method' as set for Custom buffers."
1508 (interactive "P\np")
1509 (if (and current-input-method (not arg))
1510 (inactivate-input-method)
1511 (let ((default (or (car input-method-history) default-input-method)))
1512 (if (and arg default (equal current-input-method default)
1513 (> (length input-method-history) 1))
1514 (setq default (nth 1 input-method-history)))
1515 (activate-input-method
1516 (if (or arg (not default))
1518 (read-input-method-name
1519 (if default "Input method (default %s): " "Input method: " )
1522 (unless default-input-method
1524 (setq default-input-method current-input-method)
1526 (customize-mark-as-set 'default-input-method)))))))
1528 (defun describe-input-method (input-method)
1529 "Describe input method INPUT-METHOD."
1531 (list (read-input-method-name
1532 "Describe input method (default current choice): ")))
1533 (if (and input-method (symbolp input-method))
1534 (setq input-method (symbol-name input-method)))
1535 (help-setup-xref (list #'describe-input-method
1536 (or input-method current-input-method))
1539 (if (null input-method)
1540 (describe-current-input-method)
1541 (let ((current current-input-method))
1545 (activate-input-method input-method)
1546 (describe-current-input-method))
1547 (activate-input-method current))
1549 (activate-input-method current)
1550 (help-setup-xref (list #'describe-input-method input-method)
1552 (with-output-to-temp-buffer (help-buffer)
1553 (let ((elt (assoc input-method input-method-alist)))
1555 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1556 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1558 (defun describe-current-input-method ()
1559 "Describe the input method currently in use.
1560 This is a subroutine for `describe-input-method'."
1561 (if current-input-method
1562 (if (and (symbolp describe-current-input-method-function)
1563 (fboundp describe-current-input-method-function))
1564 (funcall describe-current-input-method-function)
1565 (message "No way to describe the current input method `%s'"
1566 current-input-method)
1568 (error "No input method is activated now")))
1570 (defun read-multilingual-string (prompt &optional initial-input input-method)
1571 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1572 The input method selected last time is activated in minibuffer.
1573 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1575 Optional 3rd argument INPUT-METHOD specifies the input method
1576 to be activated instead of the one selected last time. It is a symbol
1580 current-input-method
1581 default-input-method
1582 (read-input-method-name "Input method: " nil t)))
1583 (if (and input-method (symbolp input-method))
1584 (setq input-method (symbol-name input-method)))
1585 (let ((prev-input-method current-input-method))
1588 (activate-input-method input-method)
1589 (read-string prompt initial-input nil nil t))
1590 (activate-input-method prev-input-method))))
1592 ;; Variables to control behavior of input methods. All input methods
1593 ;; should react to these variables.
1595 (defcustom input-method-verbose-flag 'default
1596 "*A flag to control extra guidance given by input methods.
1597 The value should be nil, t, `complex-only', or `default'.
1599 The extra guidance is done by showing list of available keys in echo
1600 area. When you use the input method in the minibuffer, the guidance
1601 is shown at the bottom short window (split from the existing window).
1603 If the value is t, extra guidance is always given, if the value is
1604 nil, extra guidance is always suppressed.
1606 If the value is `complex-only', only complex input methods such as
1607 `chinese-py' and `japanese' give extra guidance.
1609 If the value is `default', complex input methods always give extra
1610 guidance, but simple input methods give it only when you are not in
1613 See also the variable `input-method-highlight-flag'."
1614 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1615 (const complex-only) (const default))
1618 (defcustom input-method-highlight-flag t
1619 "*If this flag is non-nil, input methods highlight partially-entered text.
1620 For instance, while you are in the middle of a Quail input method sequence,
1621 the text inserted so far is temporarily underlined.
1622 The underlining goes away when you finish or abort the input method sequence.
1623 See also the variable `input-method-verbose-flag'."
1627 (defvar input-method-activate-hook nil
1628 "Normal hook run just after an input method is activated.
1630 The variable `current-input-method' keeps the input method name
1633 (defvar input-method-inactivate-hook nil
1634 "Normal hook run just after an input method is inactivated.
1636 The variable `current-input-method' still keeps the input method name
1639 (defvar input-method-after-insert-chunk-hook nil
1640 "Normal hook run just after an input method insert some chunk of text.")
1642 (defvar input-method-exit-on-first-char nil
1643 "This flag controls when an input method returns.
1644 Usually, the input method does not return while there's a possibility
1645 that it may find a different translation if a user types another key.
1646 But, it this flag is non-nil, the input method returns as soon as
1647 the current key sequence gets long enough to have some valid translation.")
1649 (defvar input-method-use-echo-area nil
1650 "This flag controls how an input method shows an intermediate key sequence.
1651 Usually, the input method inserts the intermediate key sequence,
1652 or candidate translations corresponding to the sequence,
1653 at point in the current buffer.
1654 But, if this flag is non-nil, it displays them in echo area instead.")
1656 (defvar input-method-exit-on-invalid-key nil
1657 "This flag controls the behavior of an input method on invalid key input.
1658 Usually, when a user types a key which doesn't start any character
1659 handled by the input method, the key is handled by turning off the
1660 input method temporarily. After that key, the input method is re-enabled.
1661 But, if this flag is non-nil, the input method is never back on.")
1664 (defvar set-language-environment-hook nil
1665 "Normal hook run after some language environment is set.
1667 When you set some hook function here, that effect usually should not
1668 be inherited to another language environment. So, you had better set
1669 another function in `exit-language-environment-hook' (which see) to
1670 cancel the effect.")
1672 (defvar exit-language-environment-hook nil
1673 "Normal hook run after exiting from some language environment.
1674 When this hook is run, the variable `current-language-environment'
1675 is still bound to the language environment being exited.
1677 This hook is mainly used for canceling the effect of
1678 `set-language-environment-hook' (which-see).")
1680 (put 'setup-specified-language-environment 'apropos-inhibit t)
1682 (defun setup-specified-language-environment ()
1683 "Switch to a specified language environment."
1685 (let (language-name)
1686 (if (and (symbolp last-command-event)
1687 (or (not (eq last-command-event 'Default))
1688 (setq last-command-event 'English))
1689 (setq language-name (symbol-name last-command-event)))
1691 (set-language-environment language-name)
1692 (customize-mark-as-set 'current-language-environment))
1693 (error "Bogus calling sequence"))))
1695 (defcustom current-language-environment "English"
1696 "The last language environment specified with `set-language-environment'.
1697 This variable should be set only with \\[customize], which is equivalent
1698 to using the function `set-language-environment'."
1699 :link '(custom-manual "(emacs)Language Environments")
1700 :set (lambda (symbol value) (set-language-environment value))
1702 (or (car-safe (assoc-string
1703 (if (symbolp current-language-environment)
1704 (symbol-name current-language-environment)
1705 current-language-environment)
1706 language-info-alist t))
1708 ;; custom type will be updated with `set-language-info'.
1709 :type (if language-info-alist
1710 (cons 'choice (mapcar
1713 (sort (mapcar 'car language-info-alist) 'string<)))
1715 :initialize 'custom-initialize-default
1718 (defun reset-language-environment ()
1719 "Reset multilingual environment of Emacs to the default status.
1721 The default status is as follows:
1723 The default value of `buffer-file-coding-system' is nil.
1724 The default coding system for process I/O is nil.
1725 The default value for the command `set-terminal-coding-system' is nil.
1726 The default value for the command `set-keyboard-coding-system' is nil.
1728 The order of priorities of coding categories and the coding system
1729 bound to each category are as follows
1730 coding category coding system
1731 --------------------------------------------------
1732 coding-category-iso-8-1 iso-latin-1
1733 coding-category-iso-8-2 iso-latin-1
1734 coding-category-utf-8 mule-utf-8
1735 coding-category-utf-16-be mule-utf-16be-with-signature
1736 coding-category-utf-16-le mule-utf-16le-with-signature
1737 coding-category-iso-7-tight iso-2022-jp
1738 coding-category-iso-7 iso-2022-7bit
1739 coding-category-iso-7-else iso-2022-7bit-lock
1740 coding-category-iso-8-else iso-2022-8bit-ss2
1741 coding-category-emacs-mule emacs-mule
1742 coding-category-raw-text raw-text
1743 coding-category-sjis japanese-shift-jis
1744 coding-category-big5 chinese-big5
1745 coding-category-ccl nil
1746 coding-category-binary no-conversion"
1748 ;; This function formerly set default-enable-multibyte-characters to t,
1749 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1751 (setq coding-category-iso-7-tight 'iso-2022-jp
1752 coding-category-iso-7 'iso-2022-7bit
1753 coding-category-iso-8-1 'iso-latin-1
1754 coding-category-iso-8-2 'iso-latin-1
1755 coding-category-iso-7-else 'iso-2022-7bit-lock
1756 coding-category-iso-8-else 'iso-2022-8bit-ss2
1757 coding-category-emacs-mule 'emacs-mule
1758 coding-category-raw-text 'raw-text
1759 coding-category-sjis 'japanese-shift-jis
1760 coding-category-big5 'chinese-big5
1761 coding-category-utf-16-be 'mule-utf-16be-with-signature
1762 coding-category-utf-16-le 'mule-utf-16le-with-signature
1763 coding-category-utf-8 'mule-utf-8
1764 coding-category-ccl nil
1765 coding-category-binary 'no-conversion)
1767 (set-coding-priority
1768 '(coding-category-iso-8-1
1769 coding-category-iso-8-2
1770 coding-category-utf-8
1771 coding-category-utf-16-be
1772 coding-category-utf-16-le
1773 coding-category-iso-7-tight
1774 coding-category-iso-7
1775 coding-category-iso-7-else
1776 coding-category-iso-8-else
1777 coding-category-emacs-mule
1778 coding-category-raw-text
1779 coding-category-sjis
1780 coding-category-big5
1782 coding-category-binary))
1784 ;; Changing the binding of a coding category requires this call.
1785 (update-coding-systems-internal)
1787 (set-default-coding-systems nil)
1788 (setq default-sendmail-coding-system 'iso-latin-1)
1789 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1790 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1791 (setq default-file-name-coding-system 'iso-latin-1)
1792 ;; Preserve eol-type from existing default-process-coding-systems.
1793 ;; On non-unix-like systems in particular, these may have been set
1794 ;; carefully by the user, or by the startup code, to deal with the
1795 ;; users shell appropriately, so should not be altered by changing
1796 ;; language environment.
1797 (let ((output-coding
1798 ;; When bootstrapping, coding-systems are not defined yet, so
1799 ;; we need to catch the error from check-coding-system.
1801 (coding-system-change-text-conversion
1802 (car default-process-coding-system) 'undecided)
1803 (coding-system-error 'undecided)))
1806 (coding-system-change-text-conversion
1807 (cdr default-process-coding-system) 'iso-latin-1)
1808 (coding-system-error 'iso-latin-1))))
1809 (setq default-process-coding-system
1810 (cons output-coding input-coding)))
1812 ;; Don't alter the terminal and keyboard coding systems here.
1813 ;; The terminal still supports the same coding system
1814 ;; that it supported a minute ago.
1815 ;; (set-terminal-coding-system-internal nil)
1816 ;; (set-keyboard-coding-system-internal nil)
1818 (setq nonascii-translation-table nil
1819 nonascii-insert-offset 0)
1821 ;; Don't invoke fontset-related functions if fontsets aren't
1822 ;; supported in this build of Emacs.
1823 (and (fboundp 'fontset-list)
1824 (set-overriding-fontspec-internal nil)))
1826 (reset-language-environment)
1828 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system)
1829 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1830 (let ((coding (get-language-info language-name 'unibyte-display)))
1832 (or (not coding-system)
1833 (coding-system-equal coding coding-system)))
1834 (standard-display-european-internal)
1835 ;; The following 2 lines undo the 8-bit display that we set up
1836 ;; in standard-display-european-internal, which see. This is in
1837 ;; case the user has used standard-display-european earlier in
1838 ;; this session. (The MS-DOS port doesn't use that setup, so it
1839 ;; doesn't need to undo it.)
1840 (when standard-display-table
1842 (aset standard-display-table (+ i 128) nil))))
1843 (or (eq window-system 'pc)
1844 (set-terminal-coding-system (or coding-system coding)))))
1846 (defun set-language-environment (language-name)
1847 "Set up multi-lingual environment for using LANGUAGE-NAME.
1848 This sets the coding system priority and the default input method
1849 and sometimes other things. LANGUAGE-NAME should be a string
1850 which is the name of a language environment. For example, \"Latin-1\"
1851 specifies the character set for the major languages of Western Europe."
1852 (interactive (list (read-language-name
1854 "Set language environment (default English): ")))
1856 (if (symbolp language-name)
1857 (setq language-name (symbol-name language-name)))
1858 (setq language-name "English"))
1859 (let ((slot (assoc-string language-name language-info-alist t)))
1861 (error "Language environment not defined: %S" language-name))
1862 (setq language-name (car slot)))
1863 (if current-language-environment
1864 (let ((func (get-language-info current-language-environment
1866 (run-hooks 'exit-language-environment-hook)
1867 (if (functionp func) (funcall func))))
1869 (reset-language-environment)
1870 ;; The features might set up coding systems.
1871 (let ((required-features (get-language-info language-name 'features)))
1872 (while required-features
1873 (require (car required-features))
1874 (setq required-features (cdr required-features))))
1876 (setq current-language-environment language-name)
1878 (set-language-environment-coding-systems language-name)
1879 (set-language-environment-input-method language-name)
1880 (set-language-environment-nonascii-translation language-name)
1881 (set-language-environment-charset language-name)
1882 (set-language-environment-fontset language-name)
1883 ;; Unibyte setups if necessary.
1884 (unless default-enable-multibyte-characters
1885 (set-language-environment-unibyte language-name))
1887 (let ((func (get-language-info language-name 'setup-function)))
1888 (if (functionp func)
1891 (run-hooks 'set-language-environment-hook)
1892 (force-mode-line-update t))
1894 (defun standard-display-european-internal ()
1895 ;; Actually set up direct output of non-ASCII characters.
1896 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1897 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1898 ;; the native font, and codes 160 and 146 stand for something very
1900 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1902 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1903 (unless (and (eq window-system 'x)
1904 ;; XFree86 4 has fixed the fonts.
1905 (string= "The XFree86 Project, Inc" (x-server-vendor))
1906 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1908 ;; Make non-line-break space display as a plain space.
1909 (aset standard-display-table 160 [32]))
1910 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1911 ;; don't contain a character at that position. Map it to the ASCII
1912 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1913 ;; U+2019, normally from the windows-1252 character set. XFree 4
1914 ;; fonts probably have the appropriate glyph at this position,
1915 ;; so they could use standard-display-8bit. It's better to use a
1916 ;; proper windows-1252 coding system. --fx]
1917 (aset standard-display-table 146 [39]))))
1919 (defun set-language-environment-coding-systems (language-name)
1920 "Do various coding system setups for language environment LANGUAGE-NAME."
1921 (let* ((priority (get-language-info language-name 'coding-priority))
1922 (default-coding (car priority))
1923 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1925 (let ((categories (mapcar 'coding-system-category priority)))
1926 (set-default-coding-systems
1927 (if (memq eol-type '(0 1 2 unix dos mac))
1928 (coding-system-change-eol-conversion default-coding eol-type)
1930 (setq default-sendmail-coding-system default-coding)
1931 (set-coding-priority categories)
1933 (set (car categories) (car priority))
1934 (setq priority (cdr priority) categories (cdr categories)))
1935 ;; Changing the binding of a coding category requires this call.
1936 (update-coding-systems-internal)))))
1938 (defun set-language-environment-input-method (language-name)
1939 "Do various input method setups for language environment LANGUAGE-NAME."
1940 (let ((input-method (get-language-info language-name 'input-method)))
1942 (setq default-input-method input-method)
1943 (if input-method-history
1944 (setq input-method-history
1946 (delete input-method input-method-history)))))))
1948 (defun set-language-environment-nonascii-translation (language-name)
1949 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1950 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1952 (if (eq window-system 'pc)
1954 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1956 ((char-table-p nonascii)
1957 (setq nonascii-translation-table nonascii))
1958 ((and (eq window-system 'pc) (boundp dos-table))
1959 ;; DOS terminals' default is to use a special non-ASCII translation
1960 ;; table as appropriate for the installed codepage.
1961 (setq nonascii-translation-table (symbol-value dos-table)))
1962 ((charsetp nonascii)
1963 (setq nonascii-insert-offset (- (make-char nonascii) 128))))))
1965 (defun set-language-environment-charset (language-name)
1966 "Do various charset setups for language environment LANGUAGE-NAME."
1967 (if (and utf-translate-cjk-mode
1968 (not (eq utf-translate-cjk-lang-env language-name))
1970 (dolist (charset (get-language-info language-name 'charset))
1971 (if (memq charset utf-translate-cjk-charsets)
1974 (utf-translate-cjk-load-tables)))
1976 (defun set-language-environment-fontset (language-name)
1977 "Do various fontset setups for language environment LANGUAGE-NAME."
1978 ;; Don't invoke fontset-related functions if fontsets aren't
1979 ;; supported in this build of Emacs.
1980 (if (fboundp 'fontset-list)
1981 (set-overriding-fontspec-internal
1982 (get-language-info language-name 'overriding-fontspec))))
1984 (defun set-language-environment-unibyte (language-name)
1985 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1986 ;; Syntax and case table.
1987 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1989 (let ((set-case-syntax-set-multibyte nil))
1990 (load syntax nil t))
1991 ;; No information for syntax and case. Reset to the defaults.
1992 (let ((syntax-table (standard-syntax-table))
1993 (standard-table (standard-case-table))
1994 (case-table (make-char-table 'case-table))
1995 (ch (if (eq window-system 'pc) 128 160)))
1997 (modify-syntax-entry ch " " syntax-table)
2000 (aset case-table i (aref standard-table i)))
2001 (set-char-table-extra-slot case-table 0 nil)
2002 (set-char-table-extra-slot case-table 1 nil)
2003 (set-char-table-extra-slot case-table 2 nil)
2004 (set-standard-case-table case-table))
2005 (let ((list (buffer-list)))
2007 (with-current-buffer (car list)
2008 (set-case-table (standard-case-table)))
2009 (setq list (cdr list))))))
2010 (set-display-table-and-terminal-coding-system language-name))
2012 (defsubst princ-list (&rest args)
2013 "Print all arguments with `princ', then print \"\n\"."
2014 (while args (princ (car args)) (setq args (cdr args)))
2017 (put 'describe-specified-language-support 'apropos-inhibit t)
2019 ;; Print language-specific information such as input methods,
2020 ;; charsets, and coding systems. This function is intended to be
2021 ;; called from the menu:
2022 ;; [menu-bar mule describe-language-environment LANGUAGE]
2023 ;; and should not run it by `M-x describe-current-input-method-function'.
2024 (defun describe-specified-language-support ()
2025 "Describe how Emacs supports the specified language environment."
2027 (let (language-name)
2028 (if (not (and (symbolp last-command-event)
2029 (or (not (eq last-command-event 'Default))
2030 (setq last-command-event 'English))
2031 (setq language-name (symbol-name last-command-event))))
2032 (error "Bogus calling sequence"))
2033 (describe-language-environment language-name)))
2035 (defun describe-language-environment (language-name)
2036 "Describe how Emacs supports language environment LANGUAGE-NAME."
2038 (list (read-language-name
2040 "Describe language environment (default current choice): ")))
2041 (if (null language-name)
2042 (setq language-name current-language-environment))
2043 (if (or (null language-name)
2044 (null (get-language-info language-name 'documentation)))
2045 (error "No documentation for the specified language"))
2046 (if (symbolp language-name)
2047 (setq language-name (symbol-name language-name)))
2048 (dolist (feature (get-language-info language-name 'features))
2050 (let ((doc (get-language-info language-name 'documentation)))
2051 (help-setup-xref (list #'describe-language-environment language-name)
2053 (with-output-to-temp-buffer (help-buffer)
2055 (set-buffer standard-output)
2056 (insert language-name " language environment\n\n")
2058 (insert doc "\n\n"))
2060 (let ((str (eval (get-language-info language-name 'sample-text))))
2062 (insert "Sample text:\n " str "\n\n")))
2064 (let ((input-method (get-language-info language-name 'input-method))
2065 (l (copy-sequence input-method-alist)))
2066 (insert "Input methods")
2068 (insert " (default " input-method ")")
2069 (setq input-method (assoc input-method input-method-alist))
2070 (setq l (cons input-method (delete input-method l))))
2073 (when (string= language-name (nth 1 (car l)))
2074 (insert " " (car (car l)))
2075 (search-backward (car (car l)))
2076 (help-xref-button 0 'help-input-method (car (car l)))
2077 (goto-char (point-max))
2079 (if (stringp (nth 3 (car l)))
2081 (car (nth 3 (car l))))
2082 "\" in mode line)\n"))
2085 (insert "Character sets:\n")
2086 (let ((l (get-language-info language-name 'charset)))
2088 (insert " nothing specific to " language-name "\n")
2090 (insert " " (symbol-name (car l)))
2091 (search-backward (symbol-name (car l)))
2092 (help-xref-button 0 'help-character-set (car l))
2093 (goto-char (point-max))
2094 (insert ": " (charset-description (car l)) "\n")
2097 (insert "Coding systems:\n")
2098 (let ((l (get-language-info language-name 'coding-system)))
2100 (insert " nothing specific to " language-name "\n")
2102 (insert " " (symbol-name (car l)))
2103 (search-backward (symbol-name (car l)))
2104 (help-xref-button 0 'help-coding-system (car l))
2105 (goto-char (point-max))
2107 (coding-system-mnemonic (car l))
2108 "' in mode line):\n\t"
2109 (coding-system-doc-string (car l))
2111 (let ((aliases (coding-system-get (car l)
2112 'alias-coding-systems)))
2114 (insert "\t(alias:")
2116 (insert " " (symbol-name (car aliases)))
2117 (setq aliases (cdr aliases)))
2119 (setq l (cdr l)))))))))
2123 (defvar locale-translation-file-name nil
2124 "File name for the system's file of locale-name aliases, or nil if none.")
2126 ;; The following definitions might as well be marked as constants and
2127 ;; purecopied, since they're normally used on startup, and probably
2128 ;; should reflect the facilities of the base Emacs.
2129 (defconst locale-language-names
2132 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2133 ;; as specified in the Single Unix Spec, Version 2.
2134 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2135 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2136 ;; see Internet RFC 2165 (1997-06) and
2137 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2138 ;; TERRITORY is a country code taken from ISO 3166
2139 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2140 ;; CODESET and MODIFIER are implementation-dependent.
2142 ;; jasonr comments: MS Windows uses three letter codes for
2143 ;; languages instead of the two letter ISO codes that POSIX
2144 ;; uses. In most cases the first two letters are the same, so
2145 ;; most of the regexps in locale-language-names work. Japanese
2146 ;; and Chinese are exceptions, which are listed in the
2147 ;; non-standard section at the bottom of locale-language-names.
2149 ("aa_DJ" . "Latin-1") ; Afar
2152 ("af" . "Latin-1") ; Afrikaans
2153 ("am" "Ethiopic" utf-8) ; Amharic
2154 ("an" . "Latin-9") ; Aragonese
2155 ; ar Arabic glibc uses 8859-6
2158 ("az" . "UTF-8") ; Azerbaijani
2160 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2161 ("bg" "Bulgarian" cp1251) ; Bulgarian
2164 ("bn" . "UTF-8") ; Bengali, Bangla
2166 ("br" . "Latin-1") ; Breton
2167 ("bs" . "Latin-2") ; Bosnian
2168 ("byn" . "UTF-8") ; Bilin; Blin
2169 ("ca" . "Latin-1") ; Catalan
2171 ("cs" "Czech" iso-8859-2)
2172 ("cy" "Welsh" iso-8859-14)
2173 ("da" . "Latin-1") ; Danish
2174 ("de" "German" iso-8859-1)
2177 ("el" "Greek" iso-8859-7)
2178 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2179 ;; That's actually what the GNU locales define, modulo things like
2181 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2182 ("en" "English" iso-8859-1) ; English
2183 ("eo" . "Latin-3") ; Esperanto
2184 ("es" "Spanish" iso-8859-1)
2185 ("et" . "Latin-1") ; Estonian
2186 ("eu" . "Latin-1") ; Basque
2187 ("fa" . "UTF-8") ; Persian
2188 ("fi" . "Latin-1") ; Finnish
2189 ("fj" . "Latin-1") ; Fiji
2190 ("fo" . "Latin-1") ; Faroese
2191 ("fr" "French" iso-8859-1) ; French
2192 ("fy" . "Latin-1") ; Frisian
2193 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2194 ("gd" . "Latin-9") ; Scots Gaelic
2195 ("gez" "Ethiopic" utf-8) ; Geez
2196 ("gl" . "Latin-1") ; Gallegan; Galician
2198 ("gu" . "UTF-8") ; Gujarati
2199 ("gv" . "Latin-1") ; Manx Gaelic
2201 ("he" "Hebrew" iso-8859-8)
2202 ("hi" "Devanagari" utf-8) ; Hindi
2203 ("hr" "Croatian" iso-8859-2) ; Croatian
2204 ("hu" . "Latin-2") ; Hungarian
2207 ("id" . "Latin-1") ; Indonesian
2210 ("is" . "Latin-1") ; Icelandic
2211 ("it" "Italian" iso-8859-1) ; Italian
2213 ("iw" "Hebrew" iso-8859-8)
2214 ("ja" "Japanese" euc-jp)
2216 ("ka" "Georgian" georgian-ps) ; Georgian
2218 ("kl" . "Latin-1") ; Greenlandic
2220 ("kn" "Kannada" utf-8)
2221 ("ko" "Korean" euc-kr)
2224 ("kw" . "Latin-1") ; Cornish
2226 ("la" . "Latin-1") ; Latin
2227 ("lb" . "Latin-1") ; Luxemburgish
2228 ("lg" . "Laint-6") ; Ganda
2230 ("lo" "Lao" utf-8) ; Laothian
2231 ("lt" "Lithuanian" iso-8859-13)
2232 ("lv" . "Latvian") ; Latvian, Lettish
2234 ("mi" . "Latin-7") ; Maori
2235 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2236 ("ml" "Malayalam" utf-8)
2237 ("mn" . "UTF-8") ; Mongolian
2239 ("mr" "Devanagari" utf-8) ; Marathi
2240 ("ms" . "Latin-1") ; Malay
2241 ("mt" . "Latin-3") ; Maltese
2244 ("nb" . "Latin-1") ; Norwegian
2245 ("ne" "Devanagari" utf-8) ; Nepali
2246 ("nl" "Dutch" iso-8859-1)
2247 ("no" . "Latin-1") ; Norwegian
2248 ("oc" . "Latin-1") ; Occitan
2249 ("om_ET" . "UTF-8") ; (Afan) Oromo
2250 ("om" . "Latin-1") ; (Afan) Oromo
2252 ("pa" . "UTF-8") ; Punjabi
2253 ("pl" . "Latin-2") ; Polish
2255 ("pt" . "Latin-1") ; Portuguese
2257 ("rm" . "Latin-1") ; Rhaeto-Romanic
2259 ("ro" "Romanian" iso-8859-2)
2260 ("ru_RU" "Russian" iso-8859-5)
2261 ("ru_UA" "Russian" koi8-u)
2263 ("sa" . "Devanagari") ; Sanskrit
2265 ("se" . "UTF-8") ; Northern Sami
2267 ("sh" . "Latin-2") ; Serbo-Croatian
2269 ("sid" . "UTF-8") ; Sidamo
2270 ("sk" "Slovak" iso-8859-2)
2271 ("sl" "Slovenian" iso-8859-2)
2274 ("so_ET" "UTF-8") ; Somali
2275 ("so" "Latin-1") ; Somali
2276 ("sq" . "Latin-1") ; Albanian
2277 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet)
2278 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2280 ("st" . "Latin-1") ; Sesotho
2282 ("sv" "Swedish" iso-8859-1) ; Swedish
2283 ("sw" . "Latin-1") ; Swahili
2284 ("ta" "Tamil" utf-8)
2285 ("te" . "UTF-8") ; Telugu
2286 ("tg" "Tajik" koi8-t)
2287 ("th" "Thai" tis-620)
2288 ("ti" "Ethiopic" utf-8) ; Tigrinya
2289 ("tig_ER" . "UTF-8") ; Tigre
2291 ("tl" . "Latin-1") ; Tagalog
2294 ("tr" "Turkish" iso-8859-9)
2296 ("tt" . "UTF-8") ; Tatar
2299 ("uk" "Ukrainian" koi8-u)
2300 ("ur" . "UTF-8") ; Urdu
2301 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2302 ("uz" . "Latin-1") ; Uzbek
2303 ("vi" "Vietnamese" utf-8)
2305 ("wa" . "Latin-1") ; Walloon
2307 ("xh" . "Latin-1") ; Xhosa
2308 ("yi" . "Windows-1255") ; Yiddish
2311 ("zh_HK" . "Chinese-Big5")
2312 ("zh_TW" . "Chinese-Big5")
2313 ("zh_CN" . "Chinese-GB")
2314 ("zh" . "Chinese-GB")
2315 ; zh_CN.GB18030/GB18030 \
2317 ; zh_HK/BIG5-HKSCS \
2318 ("zu" . "Latin-1") ; Zulu
2320 ;; ISO standard locales
2322 ("posix$" . "ASCII")
2324 ;; The "IPA" Emacs language environment does not correspond
2325 ;; to any ISO 639 code, so let it stand for itself.
2328 ;; Nonstandard or obsolete language codes
2329 ("cz" . "Czech") ; e.g. Solaris 2.6
2330 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2331 ("iw" . "Hebrew") ; e.g. X11R6.4
2332 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2333 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2334 ("jp" . "Japanese") ; e.g. MS Windows
2335 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2336 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2337 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2338 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2339 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2340 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2342 "Alist of locale regexps vs the corresponding languages and coding systems.
2343 Each element has these form:
2344 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2345 The first element whose LOCALE-REGEXP matches the start of a
2346 downcased locale specifies the LANG-ENV \(language environtment)
2347 and CODING-SYSTEM corresponding to that locale. If there is no
2348 appropriate language environment, the element may have this form:
2349 \(LOCALE-REGEXP . LANG-ENV)
2350 In this case, LANG-ENV is one of generic language environments for an
2351 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2353 (defconst locale-charset-language-names
2355 '((".*8859[-_]?1\\>" . "Latin-1")
2356 (".*8859[-_]?2\\>" . "Latin-2")
2357 (".*8859[-_]?3\\>" . "Latin-3")
2358 (".*8859[-_]?4\\>" . "Latin-4")
2359 (".*8859[-_]?9\\>" . "Latin-5")
2360 (".*8859[-_]?14\\>" . "Latin-8")
2361 (".*8859[-_]?15\\>" . "Latin-9")
2362 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2363 ;; utf-8@euro exists, so put this last. (@euro really specifies
2364 ;; the currency, rather than the charset.)
2365 (".*@euro\\>" . "Latin-9")))
2366 "List of pairs of locale regexps and charset language names.
2367 The first element whose locale regexp matches the start of a downcased locale
2368 specifies the language name whose charset corresponds to that locale.
2369 This language name is used if the locale is not listed in
2370 `locale-language-names'")
2372 (defconst locale-preferred-coding-systems
2374 '((".*8859[-_]?1\\>" . iso-8859-1)
2375 (".*8859[-_]?2\\>" . iso-8859-2)
2376 (".*8859[-_]?3\\>" . iso-8859-3)
2377 (".*8859[-_]?4\\>" . iso-8859-4)
2378 (".*8859[-_]?9\\>" . iso-8859-9)
2379 (".*8859[-_]?14\\>" . iso-8859-14)
2380 (".*8859[-_]?15\\>" . iso-8859-15)
2381 (".*utf\\(?:-?8\\)?" . utf-8)
2382 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2383 ;; specifies the currency, rather than the charset.)
2384 (".*@euro" . iso-8859-15)
2385 ("koi8-?r" . koi8-r)
2386 ("koi8-?u" . koi8-u)
2389 ("euc-?tw" . euc-tw)
2390 ;; We don't support GBK, but as it is upper compatible with
2391 ;; GB-2312, we setup the default coding system to gb2312.
2393 ;; We don't support BIG5-HKSCS, but as it is upper compatible with
2394 ;; BIG5, we setup the default coding system to big5.
2395 ("big5hkscs" . big5)
2396 ("ja.*[._]euc" . japanese-iso-8bit)
2397 ("ja.*[._]jis7" . iso-2022-jp)
2398 ("ja.*[._]pck" . japanese-shift-jis)
2399 ("ja.*[._]sjis" . japanese-shift-jis)
2400 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2402 "List of pairs of locale regexps and preferred coding systems.
2403 The first element whose locale regexp matches the start of a downcased locale
2404 specifies the coding system to prefer when using that locale.
2405 This coding system is used if the locale specifies a specific charset.")
2407 (defun locale-name-match (key alist)
2408 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2409 Return the value corresponding to the first regexp that matches the
2410 start of KEY, or nil if there is no match."
2412 (while (and alist (not element))
2413 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2414 (setq element (car alist)))
2415 (setq alist (cdr alist)))
2418 (defun locale-charset-match-p (charset1 charset2)
2419 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2420 Matching is done ignoring case and any hyphens and underscores in the
2421 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2422 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2423 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2424 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2426 (defvar locale-charset-alist nil
2427 "Coding system alist keyed on locale-style charset name.
2428 Used by `locale-charset-to-coding-system'.")
2430 (defun locale-charset-to-coding-system (charset)
2431 "Find coding system corresponding to CHARSET.
2432 CHARSET is any sort of non-Emacs charset name, such as might be used
2433 in a locale codeset, or elsewhere. It is matched to a coding system
2434 first by case-insensitive lookup in `locale-charset-alist'. Then
2435 matches are looked for in the coding system list, treating case and
2436 the characters `-' and `_' as insignificant. The coding system base
2437 is returned. Thus, for instance, if charset \"ISO8859-2\",
2438 `iso-latin-2' is returned."
2439 (or (car (assoc-string charset locale-charset-alist t))
2440 (let ((cs coding-system-alist)
2442 (while (and (not c) cs)
2443 (if (locale-charset-match-p charset (caar cs))
2444 (setq c (intern (caar cs)))
2446 (if c (coding-system-base c)))))
2448 ;; Fixme: This ought to deal with the territory part of the locale
2449 ;; too, for setting things such as calendar holidays, ps-print paper
2450 ;; size, spelling dictionary.
2452 (defun set-locale-environment (&optional locale-name)
2453 "Set up multi-lingual environment for using LOCALE-NAME.
2454 This sets the language environment, the coding system priority,
2455 the default input method and sometimes other things.
2457 LOCALE-NAME should be a string which is the name of a locale supported
2458 by the system. Often it is of the form xx_XX.CODE, where xx is a
2459 language, XX is a country, and CODE specifies a character set and
2460 coding system. For example, the locale name \"ja_JP.EUC\" might name
2461 a locale for Japanese in Japan using the `japanese-iso-8bit'
2462 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2465 If LOCALE-NAME is nil, its value is taken from the environment
2466 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2468 The locale names supported by your system can typically be found in a
2469 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2470 will be translated according to the table specified by
2471 `locale-translation-file-name'.
2473 See also `locale-charset-language-names', `locale-language-names',
2474 `locale-preferred-coding-systems' and `locale-coding-system'."
2475 (interactive "sSet environment for locale: ")
2477 ;; Do this at runtime for the sake of binaries possibly transported
2478 ;; to a system without X.
2479 (setq locale-translation-file-name
2481 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2482 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2483 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2484 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2486 ;; The following name appears after the X-related names above,
2487 ;; since the X-related names are what X actually uses.
2488 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2490 (while (and files (not (file-exists-p (car files))))
2491 (setq files (cdr files)))
2494 (let ((locale locale-name))
2497 ;; Use the first of these three environment variables
2498 ;; that has a nonempty value.
2499 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2501 (= 0 (length locale))) ; nil or empty string
2502 (setq locale (getenv (pop vars))))))
2505 ;; The two tests are kept separate so the byte-compiler sees
2506 ;; that mac-get-preference is only called after checking its existence.
2507 (when (fboundp 'mac-get-preference)
2508 (setq locale (mac-get-preference "AppleLocale"))
2510 (let ((languages (mac-get-preference "AppleLanguages")))
2511 (unless (= (length languages) 0) ; nil or empty vector
2512 (setq locale (aref languages 0)))))))
2513 (unless (or locale (not (boundp 'mac-system-locale)))
2514 (setq locale mac-system-locale))
2518 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2519 ;; using the translation file that many systems have.
2520 (when locale-translation-file-name
2522 (insert-file-contents locale-translation-file-name)
2523 (when (re-search-forward
2524 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2525 (setq locale (buffer-substring (point) (line-end-position))))))
2527 ;; Leave the system locales alone if the caller did not specify
2528 ;; an explicit locale name, as their defaults are set from
2529 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2530 ;; want to set them to the same value as LC_CTYPE.
2532 (setq system-messages-locale locale)
2533 (setq system-time-locale locale))
2535 (setq locale (downcase locale))
2537 (let ((language-name
2538 (locale-name-match locale locale-language-names))
2539 (charset-language-name
2540 (locale-name-match locale locale-charset-language-names))
2541 (default-eol-type (coding-system-eol-type
2542 default-buffer-file-coding-system))
2544 (or (locale-name-match locale locale-preferred-coding-systems)
2546 (if (string-match "\\.\\([^@]+\\)" locale)
2547 (locale-charset-to-coding-system
2548 (match-string 1 locale))))
2549 (and (eq system-type 'macos) mac-system-coding-system))))
2551 (if (consp language-name)
2552 ;; locale-language-names specify both lang-env and coding.
2553 ;; But, what specified in locale-preferred-coding-systems
2554 ;; has higher priority.
2555 (setq coding-system (or coding-system
2556 (nth 1 language-name))
2557 language-name (car language-name))
2558 ;; Otherwise, if locale is not listed in locale-language-names,
2559 ;; use what listed in locale-charset-language-names.
2560 (if (not language-name)
2561 (setq language-name charset-language-name)))
2563 ;; If a specific EOL conversion was specified in the default
2564 ;; buffer-file-coding-system, preserve it in the coding system
2565 ;; we will be using from now on.
2566 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2568 (coding-system-p coding-system))
2569 (setq coding-system (coding-system-change-eol-conversion
2570 coding-system default-eol-type)))
2574 ;; Set up for this character set. This is now the right way
2575 ;; to do it for both unibyte and multibyte modes.
2576 (set-language-environment language-name)
2578 ;; If default-enable-multibyte-characters is nil,
2579 ;; we are using single-byte characters,
2580 ;; so the display table and terminal coding system are irrelevant.
2581 (when default-enable-multibyte-characters
2582 (set-display-table-and-terminal-coding-system
2583 language-name coding-system))
2585 ;; Set the `keyboard-coding-system' if appropriate (tty
2586 ;; only). At least X and MS Windows can generate
2587 ;; multilingual input.
2588 (unless window-system
2589 (let ((kcs (or coding-system
2590 (car (get-language-info language-name
2592 (if kcs (set-keyboard-coding-system kcs))))
2594 (setq locale-coding-system
2595 (car (get-language-info language-name 'coding-priority))))
2597 (when (and coding-system
2598 (not (coding-system-equal coding-system
2599 locale-coding-system)))
2600 (prefer-coding-system coding-system)
2601 (setq locale-coding-system coding-system))))
2603 ;; On Windows, override locale-coding-system,
2604 ;; keyboard-coding-system with system codepage. Note:
2605 ;; selection-coding-system is already set in w32select.c.
2606 (when (boundp 'w32-ansi-code-page)
2607 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2608 (when (coding-system-p code-page-coding)
2609 (setq locale-coding-system code-page-coding)
2610 (set-keyboard-coding-system code-page-coding)
2611 (set-terminal-coding-system code-page-coding))))
2613 (when (eq system-type 'darwin)
2614 ;; On Darwin, file names are always encoded in utf-8, no matter
2616 (setq default-file-name-coding-system 'utf-8)
2617 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2619 (when (and (null window-system)
2620 (equal (getenv "TERM_PROGRAM") "Apple_Terminal"))
2621 (set-terminal-coding-system 'utf-8)
2622 (set-keyboard-coding-system 'utf-8)))
2624 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2625 ;; (See comments in Flocale_info.)
2626 (let ((locale locale)
2627 (paper (locale-info 'paper)))
2629 ;; This will always be null at the time of writing.
2631 ((equal paper '(216 279))
2632 (setq ps-paper-type 'letter))
2633 ((equal paper '(210 297))
2634 (setq ps-paper-type 'a4)))
2635 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2636 (while (and vars (= 0 (length locale)))
2637 (setq locale (getenv (pop vars)))))
2639 ;; As of glibc 2.2.5, these are the only US Letter locales,
2640 ;; and the rest are A4.
2642 (or (locale-name-match locale '(("c$" . letter)
2647 ("enu$" . letter) ; Windows
2654 ;;; Charset property
2656 (defun get-charset-property (charset propname)
2657 "Return the value of CHARSET's PROPNAME property.
2658 This is the last value stored with
2659 (put-charset-property CHARSET PROPNAME VALUE)."
2660 (and (not (eq charset 'composition))
2661 (plist-get (charset-plist charset) propname)))
2663 (defun put-charset-property (charset propname value)
2664 "Store CHARSETS's PROPNAME property with value VALUE.
2665 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2666 (or (eq charset 'composition)
2667 (set-charset-plist charset
2668 (plist-put (charset-plist charset) propname value))))
2670 ;;; Character code property
2671 (put 'char-code-property-table 'char-table-extra-slots 0)
2673 (defvar char-code-property-table
2674 (make-char-table 'char-code-property-table)
2675 "Char-table containing a property list of each character code.
2677 See also the documentation of `get-char-code-property' and
2678 `put-char-code-property'.")
2680 (defun get-char-code-property (char propname)
2681 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2682 (let ((plist (aref char-code-property-table char)))
2684 (car (cdr (memq propname plist))))))
2686 (defun put-char-code-property (char propname value)
2687 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2688 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2689 (let ((plist (aref char-code-property-table char)))
2691 (let ((slot (memq propname plist)))
2693 (setcar (cdr slot) value)
2694 (nconc plist (list propname value))))
2695 (aset char-code-property-table char (list propname value)))))
2698 ;; Pretty description of encoded string
2700 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2701 (defvar iso-2022-control-alist
2709 (defun encoded-string-description (str coding-system)
2710 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2711 (setq str (string-as-unibyte str))
2713 (if (and coding-system (eq (coding-system-type coding-system) 2))
2714 ;; Try to get a pretty description for ISO 2022 escape sequences.
2715 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2716 (format "#x%02X" x))))
2717 (function (lambda (x) (format "#x%02X" x))))
2720 (defun encode-coding-char (char coding-system)
2721 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2722 If CODING-SYSTEM can't safely encode CHAR, return nil."
2723 (let ((str1 (string-as-multibyte (char-to-string char)))
2724 (str2 (string-as-multibyte (make-string 2 char)))
2725 (safe-chars (and coding-system
2726 (coding-system-get coding-system 'safe-chars)))
2727 (charset (char-charset char))
2729 (when (or (eq safe-chars t)
2731 (and safe-chars (aref safe-chars char)))
2732 ;; We must find the encoded string of CHAR. But, just encoding
2733 ;; CHAR will put extra control sequences (usually to designate
2734 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2735 ;; To exclude such tailing bytes, we at first encode one-char
2736 ;; string and two-char string, then check how many bytes at the
2737 ;; tail of both encoded strings are the same.
2739 (setq enc1 (encode-coding-string str1 coding-system)
2741 enc2 (encode-coding-string str2 coding-system)
2743 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2744 (setq i1 (1- i1) i2 (1- i2)))
2746 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2747 ;; and they are the extra control sequences at the tail to
2749 (substring enc2 0 i2))))
2752 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2753 ;;; mule-cmds.el ends here