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 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
9 ;; Keywords: mule, multilingual
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
34 (autoload 'widget-value "wid-edit"))
36 (defvar mac-system-coding-system)
37 (defvar mac-system-locale)
39 ;;; MULE related key bindings and menus.
41 (defvar mule-keymap (make-sparse-keymap)
42 "Keymap for Mule (Multilingual environment) specific commands.")
44 ;; Keep "C-x C-m ..." for mule specific commands.
45 (define-key ctl-x-map "\C-m" mule-keymap)
47 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
48 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
49 (define-key mule-keymap "F" 'set-file-name-coding-system)
50 (define-key mule-keymap "t" 'set-terminal-coding-system)
51 (define-key mule-keymap "k" 'set-keyboard-coding-system)
52 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
53 (define-key mule-keymap "x" 'set-selection-coding-system)
54 (define-key mule-keymap "X" 'set-next-selection-coding-system)
55 (define-key mule-keymap "\C-\\" 'set-input-method)
56 (define-key mule-keymap "c" 'universal-coding-system-argument)
57 (define-key mule-keymap "l" 'set-language-environment)
59 (defvar mule-menu-keymap
60 (make-sparse-keymap "Mule (Multilingual Environment)")
61 "Keymap for Mule (Multilingual environment) menu specific commands.")
63 (defvar describe-language-environment-map
64 (make-sparse-keymap "Describe Language Environment"))
66 (defvar setup-language-environment-map
67 (make-sparse-keymap "Set Language Environment"))
69 (defvar set-coding-system-map
70 (make-sparse-keymap "Set Coding System"))
72 (define-key-after mule-menu-keymap [set-language-environment]
73 (list 'menu-item "Set Language Environment" setup-language-environment-map))
74 (define-key-after mule-menu-keymap [separator-mule]
77 (define-key-after mule-menu-keymap [toggle-input-method]
78 '(menu-item "Toggle Input Method" toggle-input-method)
80 (define-key-after mule-menu-keymap [set-input-method]
81 '(menu-item "Select Input Method..." set-input-method)
83 (define-key-after mule-menu-keymap [describe-input-method]
84 '(menu-item "Describe Input Method" describe-input-method))
85 (define-key-after mule-menu-keymap [separator-input-method]
88 (define-key-after mule-menu-keymap [set-various-coding-system]
89 (list 'menu-item "Set Coding Systems" set-coding-system-map
90 :enable 'default-enable-multibyte-characters))
91 (define-key-after mule-menu-keymap [view-hello-file]
92 '(menu-item "Show Multi-lingual Text" view-hello-file
93 :enable (file-readable-p
94 (expand-file-name "HELLO" data-directory))
95 :help "Display file which says HELLO in many languages")
97 (define-key-after mule-menu-keymap [separator-coding-system]
100 (define-key-after mule-menu-keymap [describe-language-environment]
101 (list 'menu-item "Describe Language Environment"
102 describe-language-environment-map
103 :help "Show multilingual settings for a specific language")
105 (define-key-after mule-menu-keymap [describe-input-method]
106 '(menu-item "Describe Input Method..." describe-input-method
107 :help "Keyboard layout for a specific input method")
109 (define-key-after mule-menu-keymap [describe-coding-system]
110 '(menu-item "Describe Coding System..." describe-coding-system)
112 (define-key-after mule-menu-keymap [list-character-sets]
113 '(menu-item "List Character Sets" list-character-sets
114 :help "Show table of available character sets"))
115 (define-key-after mule-menu-keymap [mule-diag]
116 '(menu-item "Show All of Mule Status" mule-diag
117 :help "Display multilingual environment settings")
120 (define-key-after set-coding-system-map [universal-coding-system-argument]
121 '(menu-item "For Next Command" universal-coding-system-argument
122 :help "Coding system to be used by next command")
124 (define-key-after set-coding-system-map [separator-1]
127 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
128 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
129 :help "How to encode this buffer when saved")
131 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
132 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
133 :enable buffer-file-name
134 :help "Revisit this file immediately using specified coding system")
136 (define-key-after set-coding-system-map [set-file-name-coding-system]
137 '(menu-item "For File Name" set-file-name-coding-system
138 :help "How to decode/encode file names")
140 (define-key-after set-coding-system-map [separator-2]
144 (define-key-after set-coding-system-map [set-keyboard-coding-system]
145 '(menu-item "For Keyboard" set-keyboard-coding-system
146 :help "How to decode keyboard input")
148 (define-key-after set-coding-system-map [set-terminal-coding-system]
149 '(menu-item "For Terminal" set-terminal-coding-system
150 :enable (null (memq initial-window-system '(x w32 mac)))
151 :help "How to encode terminal output")
153 (define-key-after set-coding-system-map [separator-3]
156 (define-key-after set-coding-system-map [set-selection-coding-system]
157 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
158 :visible (display-selections-p)
159 :help "How to en/decode data to/from selection/clipboard")
161 (define-key-after set-coding-system-map [set-next-selection-coding-system]
162 '(menu-item "For Next X Selection" set-next-selection-coding-system
163 :visible (display-selections-p)
164 :help "How to en/decode next selection/clipboard operation")
166 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
167 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
168 :visible (fboundp 'start-process)
169 :enable (get-buffer-process (current-buffer))
170 :help "How to en/decode I/O from/to subprocess connected to this buffer")
174 (define-key setup-language-environment-map
175 [Default] '(menu-item "Default" setup-specified-language-environment))
177 (define-key describe-language-environment-map
178 [Default] '(menu-item "Default" describe-specified-language-support))
180 ;; This should be a single character key binding because users use it
181 ;; very frequently while editing multilingual text. Now we can use
182 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
183 ;; convenient because it requires shifting on most keyboards. An
184 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
185 ;; but it won't be used that frequently.
186 (define-key global-map "\C-\\" 'toggle-input-method)
188 ;; This is no good because people often type Shift-SPC
189 ;; meaning to type SPC. -- rms.
190 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
191 ;; (define-key global-map [?\S- ] 'toggle-input-method)
193 ;;; Mule related hyperlinks.
194 (defconst help-xref-mule-regexp-template
195 (purecopy (concat "\\(\\<\\("
196 "\\(coding system\\)\\|"
197 "\\(input method\\)\\|"
198 "\\(character set\\)\\|"
201 ;; Note starting with word-syntax character:
202 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
204 (defun coding-system-change-eol-conversion (coding-system eol-type)
205 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
206 The returned coding system converts end-of-line by EOL-TYPE
207 but text as the same way as CODING-SYSTEM.
208 EOL-TYPE should be `unix', `dos', `mac', or nil.
209 If EOL-TYPE is nil, the returned coding system detects
210 how end-of-line is formatted automatically while decoding.
212 EOL-TYPE can be specified by an integer 0, 1, or 2.
213 They means `unix', `dos', and `mac' respectively."
214 (if (symbolp eol-type)
215 (setq eol-type (cond ((eq eol-type 'unix) 0)
216 ((eq eol-type 'dos) 1)
217 ((eq eol-type 'mac) 2)
219 ;; We call `coding-system-base' before `coding-system-eol-type',
220 ;; because the coding-system may not be initialized until then.
221 (let* ((base (coding-system-base coding-system))
222 (orig-eol-type (coding-system-eol-type coding-system)))
223 (cond ((vectorp orig-eol-type)
226 (aref orig-eol-type eol-type)))
229 ((= eol-type orig-eol-type)
231 ((progn (setq orig-eol-type (coding-system-eol-type base))
232 (vectorp orig-eol-type))
233 (aref orig-eol-type eol-type)))))
235 (defun coding-system-change-text-conversion (coding-system coding)
236 "Return a coding system which differs from CODING-SYSTEM in text conversion.
237 The returned coding system converts text by CODING
238 but end-of-line as the same way as CODING-SYSTEM.
239 If CODING is nil, the returned coding system detects
240 how text is formatted automatically while decoding."
241 (let ((eol-type (coding-system-eol-type coding-system)))
242 (coding-system-change-eol-conversion
243 (if coding coding 'undecided)
244 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
246 (defun toggle-enable-multibyte-characters (&optional arg)
247 "Change whether this buffer uses multibyte characters.
248 With arg, use multibyte characters if the arg is positive.
250 Note that this command does not convert the byte contents of
251 the buffer; it only changes the way those bytes are interpreted.
252 In general, therefore, this command *changes* the sequence of
253 characters that the current buffer contains.
255 We suggest you avoid using use this command unless you know what you
256 are doing. If you use it by mistake, and the buffer is now displayed
257 wrong, use this command again to toggle back to the right mode."
260 (if (null arg) (null enable-multibyte-characters)
261 (> (prefix-numeric-value arg) 0))))
262 (set-buffer-multibyte new-flag))
263 (force-mode-line-update))
265 (defun view-hello-file ()
266 "Display the HELLO file which list up many languages and characters."
268 ;; We have to decode the file in any environment.
269 (let ((default-enable-multibyte-characters t)
270 (coding-system-for-read 'iso-2022-7bit))
271 (view-file (expand-file-name "HELLO" data-directory))))
273 (defun universal-coding-system-argument (coding-system)
274 "Execute an I/O command using the specified coding system."
276 (let ((default (and buffer-file-coding-system
277 (not (eq (coding-system-type buffer-file-coding-system)
279 buffer-file-coding-system)))
280 (list (read-coding-system
282 (format "Coding system for following command (default %s): " default)
283 "Coding system for following command: ")
285 (let* ((keyseq (read-key-sequence
286 (format "Command to execute with %s:" coding-system)))
287 (cmd (key-binding keyseq))
290 (when (eq cmd 'universal-argument)
291 (call-interactively cmd)
293 ;; Process keys bound in `universal-argument-map'.
295 (setq keyseq (read-key-sequence nil t)
296 cmd (key-binding keyseq t))
297 (not (eq cmd 'universal-argument-other-key)))
298 (let ((current-prefix-arg prefix-arg)
299 ;; Have to bind `last-command-char' here so that
300 ;; `digit-argument', for instance, can compute the
302 (last-command-char (aref keyseq 0)))
303 (call-interactively cmd)))
305 ;; This is the final call to `universal-argument-other-key', which
306 ;; set's the final `prefix-arg.
307 (let ((current-prefix-arg prefix-arg))
308 (call-interactively cmd))
310 ;; Read the command to execute with the given prefix arg.
311 (setq prefix prefix-arg
312 keyseq (read-key-sequence nil t)
313 cmd (key-binding keyseq)))
315 (let ((coding-system-for-read coding-system)
316 (coding-system-for-write coding-system)
317 (coding-system-require-warning t)
318 (current-prefix-arg prefix))
320 (call-interactively cmd))))
322 (defun set-default-coding-systems (coding-system)
323 "Set default value of various coding systems to CODING-SYSTEM.
324 This sets the following coding systems:
325 o coding system of a newly created buffer
326 o default coding system for subprocess I/O
327 This also sets the following values:
328 o default value used as `file-name-coding-system' for converting file names
329 if CODING-SYSTEM is ASCII-compatible.
330 o default value for the command `set-terminal-coding-system' (not on MSDOS)
331 o default value for the command `set-keyboard-coding-system'
332 if CODING-SYSTEM is ASCII-compatible.."
333 (check-coding-system coding-system)
334 (setq-default buffer-file-coding-system coding-system)
335 (if (fboundp 'ucs-set-table-for-input)
336 (dolist (buffer (buffer-list))
337 (or (local-variable-p 'buffer-file-coding-system buffer)
338 (ucs-set-table-for-input buffer))))
340 (if (and default-enable-multibyte-characters (not (eq system-type 'darwin))
341 (or (not coding-system)
342 (not (coding-system-get coding-system 'ascii-incompatible))))
343 ;; The file-name coding system on Darwin systems is always utf-8.
344 (setq default-file-name-coding-system coding-system))
345 ;; If coding-system is nil, honor that on MS-DOS as well, so
346 ;; that they could reset the terminal coding system.
347 (unless (and (eq window-system 'pc) coding-system)
348 (setq default-terminal-coding-system coding-system))
349 (if (or (not coding-system)
350 (not (coding-system-get coding-system 'ascii-incompatible)))
351 (setq default-keyboard-coding-system coding-system))
352 ;; Preserve eol-type from existing default-process-coding-systems.
353 ;; On non-unix-like systems in particular, these may have been set
354 ;; carefully by the user, or by the startup code, to deal with the
355 ;; users shell appropriately, so should not be altered by changing
356 ;; language environment.
358 (coding-system-change-text-conversion
359 (car default-process-coding-system) coding-system))
361 (coding-system-change-text-conversion
362 (cdr default-process-coding-system) coding-system)))
363 (setq default-process-coding-system
364 (cons output-coding input-coding))))
366 (defun prefer-coding-system (coding-system)
367 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
368 This also sets the following coding systems:
369 o coding system of a newly created buffer
370 o default coding system for subprocess I/O
371 This also sets the following values:
372 o default value used as `file-name-coding-system' for converting file names.
373 o default value for the command `set-terminal-coding-system' (not on MSDOS)
374 o default value for the command `set-keyboard-coding-system'
376 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
377 systems set by this function will use that type of EOL conversion.
379 This command does not change the default value of terminal coding system
380 for MS-DOS terminal, because DOS terminals only support a single coding
381 system, and Emacs automatically sets the default to that coding system at
384 A coding system that requires automatic detection of text
385 encoding (e.g. undecided, unix) can't be preferred.
387 See also `coding-category-list' and `coding-system-category'."
388 (interactive "zPrefer coding system: ")
389 (if (not (and coding-system (coding-system-p coding-system)))
390 (error "Invalid coding system `%s'" coding-system))
391 (let ((coding-category (coding-system-category coding-system))
392 (base (coding-system-base coding-system))
393 (eol-type (coding-system-eol-type coding-system)))
394 (if (not coding-category)
395 ;; CODING-SYSTEM is no-conversion or undecided.
396 (error "Can't prefer the coding system `%s'" coding-system))
397 (set coding-category (or base coding-system))
398 ;; Changing the binding of a coding category requires this call.
399 (update-coding-systems-internal)
400 (or (eq coding-category (car coding-category-list))
401 ;; We must change the order.
402 (set-coding-priority (list coding-category)))
403 (if (and base (interactive-p))
404 (message "Highest priority is set to %s (base of %s)"
406 ;; If they asked for specific EOL conversion, honor that.
407 (if (memq eol-type '(0 1 2))
409 (coding-system-change-eol-conversion base eol-type))
410 (setq coding-system base))
411 (set-default-coding-systems coding-system)))
413 (defvar sort-coding-systems-predicate nil
414 "If non-nil, a predicate function to sort coding systems.
416 It is called with two coding systems, and should return t if the first
417 one is \"less\" than the second.
419 The function `sort-coding-systems' use it.")
421 (defun sort-coding-systems (codings)
422 "Sort coding system list CODINGS by a priority of each coding system.
423 Returns the sorted list. CODINGS is modified by side effects.
425 If a coding system is most preferred, it has the highest priority.
426 Otherwise, a coding system corresponds to some MIME charset has higher
427 priorities. Among them, a coding system included in `coding-system'
428 key of the current language environment has higher priorities. See
429 also the documentation of `language-info-alist'.
431 If the variable `sort-coding-systems-predicate' (which see) is
432 non-nil, it is used to sort CODINGS in the different way than above."
433 (if sort-coding-systems-predicate
434 (sort codings sort-coding-systems-predicate)
435 (let* ((from-categories (mapcar #'(lambda (x) (symbol-value x))
436 coding-category-list))
437 (most-preferred (car from-categories))
438 (lang-preferred (get-language-info current-language-environment
442 (let ((base (coding-system-base x)))
443 ;; We calculate the priority number 0..255 by
444 ;; using the 8 bits PMMLCEII as this:
445 ;; P: 1 iff most preferred.
446 ;; MM: greater than 0 iff mime-charset.
447 ;; L: 1 iff one of the current lang. env.'s codings.
448 ;; C: 1 iff one of codings listed in the category list.
449 ;; E: 1 iff not XXX-with-esc
450 ;; II: if iso-2022 based, 0..3, else 1.
452 (lsh (if (eq base most-preferred) 1 0) 7)
454 (let ((mime (coding-system-get base 'mime-charset)))
455 ;; Prefer coding systems corresponding to a
458 ;; Lower utf-16 priority so that we
459 ;; normally prefer utf-8 to it, and put
460 ;; x-ctext below that.
461 (cond ((string-match "utf-16"
464 ((string-match "^x-" (symbol-name mime))
469 (lsh (if (memq base lang-preferred) 1 0) 4)
470 (lsh (if (memq base from-categories) 1 0) 3)
471 (lsh (if (string-match "-with-esc\\'"
474 (if (eq (coding-system-type base) 2)
475 ;; For ISO based coding systems, prefer
476 ;; one that doesn't use escape sequences.
477 (let ((flags (coding-system-flags base)))
478 (if (or (consp (aref flags 0))
479 (consp (aref flags 1))
480 (consp (aref flags 2))
481 (consp (aref flags 3)))
482 (if (or (aref flags 8) (aref flags 9))
487 (sort codings (function (lambda (x y)
488 (> (funcall func x) (funcall func y))))))))
490 (defun find-coding-systems-region (from to)
491 "Return a list of proper coding systems to encode a text between FROM and TO.
492 If FROM is a string, find coding systems in that instead of the buffer.
493 All coding systems in the list can safely encode any multibyte characters
496 If the text contains no multibyte characters, return a list of a single
497 element `undecided'."
498 (let ((codings (find-coding-systems-region-internal from to)))
500 ;; The text contains only ASCII characters. Any coding
503 ;; We need copy-sequence because sorting will alter the argument.
504 (sort-coding-systems (copy-sequence codings)))))
506 (defun find-coding-systems-string (string)
507 "Return a list of proper coding systems to encode STRING.
508 All coding systems in the list can safely encode any multibyte characters
511 If STRING contains no multibyte characters, return a list of a single
512 element `undecided'."
513 (find-coding-systems-region string nil))
515 (defun find-coding-systems-for-charsets (charsets)
516 "Return a list of proper coding systems to encode characters of CHARSETS.
517 CHARSETS is a list of character sets.
518 It actually checks at most the first 96 characters of each charset.
519 So, if a charset of dimension two is included in CHARSETS, the value may
520 contain a coding system that can't encode all characters of the charset."
521 (cond ((or (null charsets)
522 (and (= (length charsets) 1)
523 (eq 'ascii (car charsets))))
525 ((or (memq 'eight-bit-control charsets)
526 (memq 'eight-bit-graphic charsets))
527 '(raw-text emacs-mule))
531 (while (and codings charsets)
532 (setq charset (car charsets) charsets (cdr charsets))
533 (unless (eq charset 'ascii)
534 (setq str (make-string 96 32))
535 (if (= (charset-dimension charset) 1)
536 (if (= (charset-chars charset) 96)
538 (aset str i (make-char charset (+ i 32))))
540 (aset str i (make-char charset (+ i 33)))))
541 (if (= (charset-chars charset) 96)
543 (aset str i (make-char charset 32 (+ i 32))))
545 (aset str i (make-char charset 33 (+ i 33))))))
546 (setq l (find-coding-systems-string str))
550 (dolist (elt codings)
552 (setq ll (cons elt ll))))
553 (setq codings ll)))))
556 (defun find-multibyte-characters (from to &optional maxcount excludes)
557 "Find multibyte characters in the region specified by FROM and TO.
558 If FROM is a string, find multibyte characters in the string.
559 The return value is an alist of the following format:
560 ((CHARSET COUNT CHAR ...) ...)
562 CHARSET is a character set,
563 COUNT is a number of characters,
564 CHARs are the characters found from the character set.
565 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
566 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
568 For invalid characters, CHARs are actually strings."
573 (while (setq idx (string-match "[^\000-\177]" from idx))
574 (setq char (aref from idx)
575 charset (char-charset char))
576 (if (eq charset 'unknown)
577 (setq char (match-string 0)))
578 (if (or (memq charset '(unknown
579 eight-bit-control eight-bit-graphic))
580 (not (or (eq excludes t) (memq charset excludes))))
581 (let ((slot (assq charset chars)))
583 (if (not (memq char (nthcdr 2 slot)))
584 (let ((count (nth 1 slot)))
585 (setcar (cdr slot) (1+ count))
586 (if (or (not maxcount) (< count maxcount))
587 (nconc slot (list char)))))
588 (setq chars (cons (list charset 1 char) chars)))))
589 (setq idx (1+ idx))))
592 (while (re-search-forward "[^\000-\177]" to t)
593 (setq char (preceding-char)
594 charset (char-charset char))
595 (if (eq charset 'unknown)
596 (setq char (match-string 0)))
597 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
598 (not (or (eq excludes t) (memq charset excludes))))
599 (let ((slot (assq charset chars)))
601 (if (not (member char (nthcdr 2 slot)))
602 (let ((count (nth 1 slot)))
603 (setcar (cdr slot) (1+ count))
604 (if (or (not maxcount) (< count maxcount))
605 (nconc slot (list char)))))
606 (setq chars (cons (list charset 1 char) chars))))))))
610 (defun search-unencodable-char (coding-system)
611 "Search forward from point for a character that is not encodable.
612 It asks which coding system to check.
613 If such a character is found, set point after that character.
614 Otherwise, don't move point.
616 When called from a program, the value is a position of the found character,
617 or nil if all characters are encodable."
619 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
621 (format "Coding-system (default %s): " default)
623 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
626 (message "All following characters are encodable by %s" coding-system))
630 (defvar last-coding-system-specified nil
631 "Most recent coding system explicitly specified by the user when asked.
632 This variable is set whenever Emacs asks the user which coding system
633 to use in order to write a file. If you set it to nil explicitly,
634 then call `write-region', then afterward this variable will be non-nil
635 only if the user was explicitly asked and specified a coding system.")
637 (defvar select-safe-coding-system-accept-default-p nil
638 "If non-nil, a function to control the behavior of coding system selection.
639 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
640 function `select-safe-coding-system' (which see). This variable
641 overrides that argument.")
643 (defun select-safe-coding-system-interactively (from to codings unsafe
644 &optional rejected default)
645 "Select interactively a coding system for the region FROM ... TO.
646 FROM can be a string, as in `write-region'.
647 CODINGS is the list of base coding systems known to be safe for this region,
648 typically obtained with `find-coding-systems-region'.
649 UNSAFE is a list of coding systems known to be unsafe for this region.
650 REJECTED is a list of coding systems which were safe but for some reason
651 were not recommended in the particular context.
652 DEFAULT is the coding system to use by default in the query."
653 ;; At first, if some defaults are unsafe, record at most 11
654 ;; problematic characters and their positions for them by turning
657 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
660 (mapcar #'(lambda (coding)
663 (mapcar #'(lambda (pos)
664 (cons pos (aref from pos)))
665 (unencodable-char-position
666 0 (length from) coding
668 (mapcar #'(lambda (pos)
669 (cons pos (char-after pos)))
670 (unencodable-char-position
671 from to coding 11)))))
674 ;; Change each safe coding system to the corresponding
675 ;; mime-charset name if it is also a coding system. Such a name
676 ;; is more friendly to users.
680 (setq mime-charset (coding-system-get (car l) 'mime-charset))
681 (if (and mime-charset (coding-system-p mime-charset))
682 (setcar l mime-charset))
685 ;; Don't offer variations with locking shift, which you
686 ;; basically never want.
688 (dolist (elt codings (setq codings (nreverse l)))
689 (unless (or (eq 'coding-category-iso-7-else
690 (coding-system-category elt))
691 (eq 'coding-category-iso-8-else
692 (coding-system-category elt)))
695 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
696 ;; else is available.
700 (delq 'no-conversion codings)))
701 '(raw-text emacs-mule no-conversion)))
703 (let ((window-configuration (current-window-configuration))
704 (bufname (buffer-name))
707 ;; If some defaults are unsafe, make sure the offending
708 ;; buffer is displayed.
709 (when (and unsafe (not (stringp from)))
710 (pop-to-buffer bufname)
711 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
713 ;; Then ask users to select one from CODINGS while showing
714 ;; the reason why none of the defaults are not used.
715 (with-output-to-temp-buffer "*Warning*"
716 (with-current-buffer standard-output
717 (if (and (null rejected) (null unsafe))
718 (insert "No default coding systems to try for "
720 (format "string \"%s\"." from)
721 (format "buffer `%s'." bufname)))
723 "These default coding systems were tried to encode"
725 (concat " \"" (if (> (length from) 10)
726 (concat (substring from 0 10) "...\"")
728 (format " text\nin the buffer `%s'" bufname))
732 (dolist (x (append rejected unsafe))
733 (princ " ") (princ (car x)))
735 (fill-region-as-paragraph pos (point)))
737 (insert "These safely encodes the target text,
738 but it is not recommended for encoding text in this context,
739 e.g., for sending an email message.\n ")
741 (princ " ") (princ x))
744 (insert (if rejected "And the others"
745 "However, each of them")
746 " encountered these problematic characters:\n")
747 (dolist (coding unsafe)
748 (insert (format " %s:" (car coding)))
751 #'(lambda (bufname pos)
752 (when (buffer-live-p (get-buffer bufname))
753 (pop-to-buffer bufname)
756 #'(lambda (bufname pos coding)
757 (when (buffer-live-p (get-buffer bufname))
758 (pop-to-buffer bufname)
762 (search-unencodable-char coding)
763 (forward-char -1))))))
764 (dolist (elt (cdr coding))
767 (insert (if (< i 10) (cdr elt) "..."))
773 "mouse-2, RET: jump to this character"
775 'help-args (list bufname (car elt)))
780 "mouse-2, RET: next unencodable character"
782 'help-args (list bufname (car elt)
787 The first problematic character is at point in the displayed buffer,\n"
788 (substitute-command-keys "\
789 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
791 one of the following safe coding systems, or edit the buffer:\n")
795 (princ " ") (princ x))
797 (fill-region-as-paragraph pos (point)))
798 (insert "Or specify any other coding system
799 at the risk of losing the problematic characters.\n")))
801 ;; Read a coding system.
804 (format "Select coding system (default %s): " default)
806 (setq last-coding-system-specified coding-system))
808 (kill-buffer "*Warning*")
809 (set-window-configuration window-configuration)
812 (defun select-safe-coding-system (from to &optional default-coding-system
813 accept-default-p file)
814 "Ask a user to select a safe coding system from candidates.
815 The candidates of coding systems which can safely encode a text
816 between FROM and TO are shown in a popup window. Among them, the most
817 proper one is suggested as the default.
819 The list of `buffer-file-coding-system' of the current buffer,
820 the `default-buffer-file-coding-system', and the
821 most preferred coding system (if it corresponds to a MIME charset) is
822 treated as the default coding system list. Among them, the first one
823 that safely encodes the text is normally selected silently and
824 returned without any user interaction. See also the command
825 `prefer-coding-system'.
827 However, the user is queried if the chosen coding system is
828 inconsistent with what would be selected by `find-auto-coding' from
829 coding cookies &c. if the contents of the region were read from a
830 file. (That could lead to data corruption in a file subsequently
831 re-visited and edited.)
833 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
834 list of coding systems to be prepended to the default coding system
835 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
836 element is t, the cdr part is used as the default coding system list,
837 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
838 and the most preferred coding system are not used.
840 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
841 determine the acceptability of the silently selected coding system.
842 It is called with that coding system, and should return nil if it
843 should not be silently selected and thus user interaction is required.
845 Optional 5th arg FILE is the file name to use for this purpose.
846 That is different from `buffer-file-name' when handling `write-region'
849 The variable `select-safe-coding-system-accept-default-p', if
850 non-nil, overrides ACCEPT-DEFAULT-P.
852 Kludgy feature: if FROM is a string, the string is the target text,
854 (if (and default-coding-system
855 (not (listp default-coding-system)))
856 (setq default-coding-system (list default-coding-system)))
858 (let ((no-other-defaults nil)
860 (unless (or (stringp from) find-file-literally)
861 ;; Find an auto-coding that is specified for the the current
862 ;; buffer and file from the region FROM and TO.
867 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
870 (if (coding-system-p (car auto-cs))
871 (setq auto-cs (car auto-cs))
875 Invalid coding system `%s' is specified
876 for the current buffer/file by the %s.
877 It is highly recommended to fix it before writing to a file."
879 (if (eq (cdr auto-cs) :coding) ":coding tag"
880 (format "variable `%s'" (cdr auto-cs)))))
881 (or (yes-or-no-p "Really proceed with writing? ")
882 (error "Save aborted"))
883 (setq auto-cs nil))))))
885 (if (eq (car default-coding-system) t)
886 (setq no-other-defaults t
887 default-coding-system (cdr default-coding-system)))
889 ;; Change elements of the list to (coding . base-coding).
890 (setq default-coding-system
891 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
892 default-coding-system))
894 (if (and auto-cs (not no-other-defaults))
895 ;; If the file has a coding cookie, try to use it before anything
896 ;; else (i.e. before default-coding-system which will typically come
897 ;; from file-coding-system-alist).
898 (let ((base (coding-system-base auto-cs)))
899 (or (memq base '(nil undecided))
900 (rassq base default-coding-system)
901 (push (cons auto-cs base) default-coding-system))))
903 (unless no-other-defaults
904 ;; If buffer-file-coding-system is not nil nor undecided, append it
906 (if buffer-file-coding-system
907 (let ((base (coding-system-base buffer-file-coding-system)))
908 (or (eq base 'undecided)
909 (rassq base default-coding-system)
910 (setq default-coding-system
911 (append default-coding-system
912 (list (cons buffer-file-coding-system base)))))))
914 ;; If default-buffer-file-coding-system is not nil nor undecided,
915 ;; append it to the defaults.
916 (if default-buffer-file-coding-system
917 (let ((base (coding-system-base default-buffer-file-coding-system)))
918 (or (eq base 'undecided)
919 (rassq base default-coding-system)
920 (setq default-coding-system
921 (append default-coding-system
922 (list (cons default-buffer-file-coding-system
925 ;; If the most preferred coding system has the property mime-charset,
926 ;; append it to the defaults.
927 (let ((tail coding-category-list)
929 (while (and tail (not (setq preferred (symbol-value (car tail)))))
930 (setq tail (cdr tail)))
931 (and (coding-system-p preferred)
932 (setq base (coding-system-base preferred))
933 (coding-system-get preferred 'mime-charset)
934 (not (rassq base default-coding-system))
935 (setq default-coding-system
936 (append default-coding-system
937 (list (cons preferred base)))))))
939 (if select-safe-coding-system-accept-default-p
940 (setq accept-default-p select-safe-coding-system-accept-default-p))
942 ;; Decide the eol-type from the top of the default codings,
943 ;; buffer-file-coding-system, or
944 ;; default-buffer-file-coding-system.
945 (if default-coding-system
946 (let ((default-eol-type (coding-system-eol-type
947 (caar default-coding-system))))
948 (if (and (vectorp default-eol-type) buffer-file-coding-system)
949 (setq default-eol-type (coding-system-eol-type
950 buffer-file-coding-system)))
951 (if (and (vectorp default-eol-type) default-buffer-file-coding-system)
952 (setq default-eol-type (coding-system-eol-type
953 default-buffer-file-coding-system)))
954 (if (and default-eol-type (not (vectorp default-eol-type)))
955 (dolist (elt default-coding-system)
956 (setcar elt (coding-system-change-eol-conversion
957 (car elt) default-eol-type))))))
959 (let ((codings (find-coding-systems-region from to))
961 safe rejected unsafe)
962 (if (eq (car codings) 'undecided)
963 ;; Any coding system is ok.
964 (setq coding-system (caar default-coding-system))
965 ;; Reverse the list so that elements are accumulated in safe,
966 ;; rejected, and unsafe in the correct order.
967 (setq default-coding-system (nreverse default-coding-system))
969 ;; Classify the defaults into safe, rejected, and unsafe.
970 (dolist (elt default-coding-system)
971 (if (memq (cdr elt) codings)
972 (if (and (functionp accept-default-p)
973 (not (funcall accept-default-p (cdr elt))))
974 (push (car elt) rejected)
975 (push (car elt) safe))
976 (push (car elt) unsafe)))
978 (setq coding-system (car safe))))
980 ;; If all the defaults failed, ask a user.
981 (when (not coding-system)
982 (setq coding-system (select-safe-coding-system-interactively
983 from to codings unsafe rejected (car codings))))
985 ;; Check we're not inconsistent with what `coding:' spec &c would
986 ;; give when file is re-read.
987 ;; But don't do this if we explicitly ignored the cookie
988 ;; by using `find-file-literally'.
992 (memq (coding-system-type coding-system) '(0 5)))))
993 ;; Merge coding-system and auto-cs as far as possible.
994 (if (not coding-system)
995 (setq coding-system auto-cs)
997 (setq auto-cs coding-system)
998 (let ((eol-type-1 (coding-system-eol-type coding-system))
999 (eol-type-2 (coding-system-eol-type auto-cs)))
1000 (if (eq (coding-system-base coding-system) 'undecided)
1001 (setq coding-system (coding-system-change-text-conversion
1002 coding-system auto-cs))
1003 (if (eq (coding-system-base auto-cs) 'undecided)
1004 (setq auto-cs (coding-system-change-text-conversion
1005 auto-cs coding-system))))
1006 (if (vectorp eol-type-1)
1007 (or (vectorp eol-type-2)
1008 (setq coding-system (coding-system-change-eol-conversion
1009 coding-system eol-type-2)))
1010 (if (vectorp eol-type-2)
1011 (setq auto-cs (coding-system-change-eol-conversion
1012 auto-cs eol-type-1)))))))
1015 ;; Don't barf if writing a compressed file, say.
1016 ;; This check perhaps isn't ideal, but is probably
1017 ;; the best thing to do.
1018 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1019 (not (coding-system-equal coding-system auto-cs)))
1020 (unless (yes-or-no-p
1021 (format "Selected encoding %s disagrees with \
1022 %s specified by file contents. Really save (else edit coding cookies \
1023 and try again)? " coding-system auto-cs))
1024 (error "Save aborted"))))
1027 (setq select-safe-coding-system-function 'select-safe-coding-system)
1029 (defun select-message-coding-system ()
1030 "Return a coding system to encode the outgoing message of the current buffer.
1031 It at first tries the first coding system found in these variables
1033 (1) local value of `buffer-file-coding-system'
1034 (2) value of `sendmail-coding-system'
1035 (3) value of `default-sendmail-coding-system'
1036 (4) value of `default-buffer-file-coding-system'
1037 If the found coding system can't encode the current buffer,
1038 or none of them are bound to a coding system,
1039 it asks the user to select a proper coding system."
1040 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1041 buffer-file-coding-system)
1042 sendmail-coding-system
1043 default-sendmail-coding-system
1044 default-buffer-file-coding-system)))
1045 (if (eq coding 'no-conversion)
1046 ;; We should never use no-conversion for outgoing mail.
1048 (if (fboundp select-safe-coding-system-function)
1050 (funcall select-safe-coding-system-function
1051 (point-min) (point-max) coding
1052 (function (lambda (x)
1053 (coding-system-get x 'mime-charset))))))
1055 ;; Be sure to use LF for end-of-line.
1056 (setq coding (coding-system-change-eol-conversion coding 'unix))
1057 ;; No coding system is decided. Usually this is the case that
1058 ;; the current buffer contains only ASCII. So, we hope
1059 ;; iso-8859-1 works.
1060 (setq coding 'iso-8859-1-unix))
1063 ;;; Language support stuff.
1065 (defvar language-info-alist nil
1066 "Alist of language environment definitions.
1067 Each element looks like:
1068 (LANGUAGE-NAME . ((KEY . INFO) ...))
1069 where LANGUAGE-NAME is a string, the name of the language environment,
1070 KEY is a symbol denoting the kind of information, and
1071 INFO is the data associated with KEY.
1072 Meaningful values for KEY include
1074 documentation value is documentation of what this language environment
1075 is meant for, and how to use it.
1076 charset value is a list of the character sets used by this
1077 language environment.
1078 sample-text value is an expression which is evalled to generate
1079 a line of text written using characters appropriate
1080 for this language environment.
1081 setup-function value is a function to call to switch to this
1082 language environment.
1083 exit-function value is a function to call to leave this
1084 language environment.
1085 coding-system value is a list of coding systems that are good
1086 for saving text written in this language environment.
1087 This list serves as suggestions to the user;
1088 in effect, as a kind of documentation.
1089 coding-priority value is a list of coding systems for this language
1090 environment, in order of decreasing priority.
1091 This is used to set up the coding system priority
1092 list when you switch to this language environment.
1093 nonascii-translation
1094 value is a translation table to be set in the
1095 variable `nonascii-translation-table' in this
1096 language environment, or a character set from
1097 which `nonascii-insert-offset' is calculated.
1098 input-method value is a default input method for this language
1100 features value is a list of features requested in this
1101 language environment.
1102 ctext-non-standard-encodings
1103 value is a list of non-standard encoding
1104 names used in extended segments of CTEXT.
1106 `ctext-non-standard-encodings' for more
1109 The following keys take effect only when multibyte characters are
1110 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1113 unibyte-syntax value is a library name to load to set
1114 unibyte 8-bit character syntaxes for this
1115 language environment.
1117 unibyte-display value is a coding system to encode characters
1118 for the terminal. Characters in the range
1119 of 160 to 255 display not as octal escapes,
1120 but as non-ASCII characters in this language
1123 (defun get-language-info (lang-env key)
1124 "Return information listed under KEY for language environment LANG-ENV.
1125 KEY is a symbol denoting the kind of information.
1126 For a list of useful values for KEY and their meanings,
1127 see `language-info-alist'."
1128 (if (symbolp lang-env)
1129 (setq lang-env (symbol-name lang-env)))
1130 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1132 (cdr (assq key (cdr lang-slot))))))
1134 (defun set-language-info (lang-env key info)
1135 "Modify part of the definition of language environment LANG-ENV.
1136 Specifically, this stores the information INFO under KEY
1137 in the definition of this language environment.
1138 KEY is a symbol denoting the kind of information.
1139 INFO is the value for that information.
1141 For a list of useful values for KEY and their meanings,
1142 see `language-info-alist'."
1143 (if (symbolp lang-env)
1144 (setq lang-env (symbol-name lang-env)))
1145 (set-language-info-internal lang-env key info)
1146 (if (equal lang-env current-language-environment)
1147 (cond ((eq key 'coding-priority)
1148 (set-language-environment-coding-systems lang-env))
1149 ((eq key 'input-method)
1150 (set-language-environment-input-method lang-env))
1151 ((eq key 'nonascii-translation)
1152 (set-language-environment-nonascii-translation lang-env))
1154 (set-language-environment-charset lang-env))
1155 ((eq key 'overriding-fontspec)
1156 (set-language-environment-fontset lang-env))
1157 ((and (not default-enable-multibyte-characters)
1158 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1159 (set-language-environment-unibyte lang-env)))))
1161 (defun set-language-info-internal (lang-env key info)
1163 Arguments are the same as `set-language-info'."
1164 (let (lang-slot key-slot)
1165 (setq lang-slot (assoc lang-env language-info-alist))
1166 (if (null lang-slot) ; If no slot for the language, add it.
1167 (setq lang-slot (list lang-env)
1168 language-info-alist (cons lang-slot language-info-alist)))
1169 (setq key-slot (assq key lang-slot))
1170 (if (null key-slot) ; If no slot for the key, add it.
1172 (setq key-slot (list key))
1173 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1174 (setcdr key-slot (purecopy info))
1175 ;; Update the custom-type of `current-language-environment'.
1176 (put 'current-language-environment 'custom-type
1177 (cons 'choice (mapcar
1180 (sort (mapcar 'car language-info-alist) 'string<))))))
1182 (defun set-language-info-alist (lang-env alist &optional parents)
1183 "Store ALIST as the definition of language environment LANG-ENV.
1184 ALIST is an alist of KEY and INFO values. See the documentation of
1185 `language-info-alist' for the meanings of KEY and INFO.
1187 Optional arg PARENTS is a list of parent menu names; it specifies
1188 where to put this language environment in the
1189 Describe Language Environment and Set Language Environment menus.
1190 For example, (\"European\") means to put this language environment
1191 in the European submenu in each of those two menus."
1192 (if (symbolp lang-env)
1193 (setq lang-env (symbol-name lang-env)))
1194 (let ((describe-map describe-language-environment-map)
1195 (setup-map setup-language-environment-map))
1198 map parent-symbol parent prompt)
1200 (if (symbolp (setq parent-symbol (car l)))
1201 (setq parent (symbol-name parent))
1202 (setq parent parent-symbol parent-symbol (intern parent)))
1203 (setq map (lookup-key describe-map (vector parent-symbol)))
1204 ;; This prompt string is for define-prefix-command, so
1205 ;; that the map it creates will be suitable for a menu.
1206 (or map (setq prompt (format "%s Environment" parent)))
1209 (setq map (intern (format "describe-%s-environment-map"
1210 (downcase parent))))
1211 (define-prefix-command map nil prompt)
1212 (define-key-after describe-map (vector parent-symbol)
1213 (cons parent map) t)))
1214 (setq describe-map (symbol-value map))
1215 (setq map (lookup-key setup-map (vector parent-symbol)))
1218 (setq map (intern (format "setup-%s-environment-map"
1219 (downcase parent))))
1220 (define-prefix-command map nil prompt)
1221 (define-key-after setup-map (vector parent-symbol)
1222 (cons parent map) t)))
1223 (setq setup-map (symbol-value map))
1226 ;; Set up menu items for this language env.
1227 (let ((doc (assq 'documentation alist)))
1229 (define-key-after describe-map (vector (intern lang-env))
1230 (cons lang-env 'describe-specified-language-support) t)))
1231 (define-key-after setup-map (vector (intern lang-env))
1232 (cons lang-env 'setup-specified-language-environment) t)
1235 (set-language-info-internal lang-env (car elt) (cdr elt)))
1237 (if (equal lang-env current-language-environment)
1238 (set-language-environment lang-env))))
1240 (defun read-language-name (key prompt &optional default)
1241 "Read a language environment name which has information for KEY.
1242 If KEY is nil, read any language environment.
1243 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1244 This returns a language environment name as a string."
1245 (let* ((completion-ignore-case t)
1246 (name (completing-read prompt
1249 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1250 t nil nil default)))
1251 (if (and (> (length name) 0)
1253 (get-language-info name key)))
1256 ;;; Multilingual input methods.
1258 "LEIM: Libraries of Emacs Input Methods."
1261 (defconst leim-list-file-name "leim-list.el"
1262 "Name of LEIM list file.
1263 This file contains a list of libraries of Emacs input methods (LEIM)
1264 in the format of Lisp expression for registering each input method.
1265 Emacs loads this file at startup time.")
1267 (defvar leim-list-header (format
1268 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1270 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1271 ;; methods in the same directory as this file. Loading this file
1272 ;; registers all the input methods in Emacs.
1274 ;; Each entry has the form:
1275 ;; (register-input-method
1276 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1277 ;; TITLE DESCRIPTION
1279 ;; See the function `register-input-method' for the meanings of the arguments.
1281 ;; If this directory is included in load-path, Emacs automatically
1282 ;; loads this file at startup time.
1285 leim-list-file-name)
1286 "Header to be inserted in LEIM list file.")
1288 (defvar leim-list-entry-regexp "^(register-input-method"
1289 "Regexp matching head of each entry in LEIM list file.
1290 See also the variable `leim-list-header'")
1292 (defvar update-leim-list-functions
1293 '(quail-update-leim-list-file)
1294 "List of functions to call to update LEIM list file.
1295 Each function is called with one arg, LEIM directory name.")
1297 (defun update-leim-list-file (&rest dirs)
1298 "Update LEIM list file in directories DIRS."
1299 (let ((functions update-leim-list-functions))
1301 (apply (car functions) dirs)
1302 (setq functions (cdr functions)))))
1304 (defvar current-input-method nil
1305 "The current input method for multilingual text.
1306 If nil, that means no input method is activated now.")
1307 (make-variable-buffer-local 'current-input-method)
1308 (put 'current-input-method 'permanent-local t)
1310 (defvar current-input-method-title nil
1311 "Title string of the current input method shown in mode line.")
1312 (make-variable-buffer-local 'current-input-method-title)
1313 (put 'current-input-method-title 'permanent-local t)
1315 (defcustom default-input-method nil
1316 "*Default input method for multilingual text (a string).
1317 This is the input method activated automatically by the command
1318 `toggle-input-method' (\\[toggle-input-method])."
1319 :link '(custom-manual "(emacs)Input Methods")
1321 :type '(choice (const nil) (string
1322 :completion-ignore-case t
1323 :complete-function widget-string-complete
1324 :completion-alist input-method-alist
1325 :prompt-history input-method-history))
1326 :set-after '(current-language-environment))
1328 (put 'input-method-function 'permanent-local t)
1330 (defvar input-method-history nil
1331 "History list for some commands that read input methods.")
1332 (make-variable-buffer-local 'input-method-history)
1333 (put 'input-method-history 'permanent-local t)
1335 (defvar inactivate-current-input-method-function nil
1336 "Function to call for inactivating the current input method.
1337 Every input method should set this to an appropriate value when activated.
1338 This function is called with no argument.
1340 This function should never change the value of `current-input-method'.
1341 It is set to nil by the function `inactivate-input-method'.")
1342 (make-variable-buffer-local 'inactivate-current-input-method-function)
1343 (put 'inactivate-current-input-method-function 'permanent-local t)
1345 (defvar describe-current-input-method-function nil
1346 "Function to call for describing the current input method.
1347 This function is called with no argument.")
1348 (make-variable-buffer-local 'describe-current-input-method-function)
1349 (put 'describe-current-input-method-function 'permanent-local t)
1351 (defvar input-method-alist nil
1352 "Alist of input method names vs how to use them.
1353 Each element has the form:
1354 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1355 See the function `register-input-method' for the meanings of the elements.")
1357 (defun register-input-method (input-method lang-env &rest args)
1358 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1359 INPUT-METHOD and LANG-ENV are symbols or strings.
1361 The remaining arguments are:
1362 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1363 ACTIVATE-FUNC is a function to call to activate this method.
1364 TITLE is a string to show in the mode line when this method is active.
1365 DESCRIPTION is a string describing this method and what it is good for.
1366 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1367 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1369 This function is mainly used in the file \"leim-list.el\" which is
1370 created at Emacs build time, registering all Quail input methods
1371 contained in the Emacs distribution.
1373 In case you want to register a new Quail input method by yourself, be
1374 careful to use the same input method title as given in the third
1375 parameter of `quail-define-package'. (If the values are different, the
1376 string specified in this function takes precedence.)
1378 The commands `describe-input-method' and `list-input-methods' need
1379 these duplicated values to show some information about input methods
1380 without loading the relevant Quail packages."
1381 (if (symbolp lang-env)
1382 (setq lang-env (symbol-name lang-env)))
1383 (if (symbolp input-method)
1384 (setq input-method (symbol-name input-method)))
1385 (let ((info (cons lang-env args))
1386 (slot (assoc input-method input-method-alist)))
1389 (setq slot (cons input-method info))
1390 (setq input-method-alist (cons slot input-method-alist)))))
1392 (defun read-input-method-name (prompt &optional default inhibit-null)
1393 "Read a name of input method from a minibuffer prompting with PROMPT.
1394 If DEFAULT is non-nil, use that as the default,
1395 and substitute it into PROMPT at the first `%s'.
1396 If INHIBIT-NULL is non-nil, null input signals an error.
1398 The return value is a string."
1400 (setq prompt (format prompt default)))
1401 (let* ((completion-ignore-case t)
1402 ;; As it is quite normal to change input method in the
1403 ;; minibuffer, we must enable it even if
1404 ;; enable-recursive-minibuffers is currently nil.
1405 (enable-recursive-minibuffers t)
1406 ;; This binding is necessary because input-method-history is
1408 (input-method (completing-read prompt input-method-alist
1409 nil t nil 'input-method-history
1411 (if (and input-method (symbolp input-method))
1412 (setq input-method (symbol-name input-method)))
1413 (if (> (length input-method) 0)
1416 (error "No valid input method is specified")))))
1418 (defun activate-input-method (input-method)
1419 "Switch to input method INPUT-METHOD for the current buffer.
1420 If some other input method is already active, turn it off first.
1421 If INPUT-METHOD is nil, deactivate any current input method."
1422 (if (and input-method (symbolp input-method))
1423 (setq input-method (symbol-name input-method)))
1424 (if (and current-input-method
1425 (not (string= current-input-method input-method)))
1426 (inactivate-input-method))
1427 (unless (or current-input-method (null input-method))
1428 (let ((slot (assoc input-method input-method-alist)))
1430 (error "Can't activate input method `%s'" input-method))
1431 (setq current-input-method-title nil)
1432 (let ((func (nth 2 slot)))
1433 (if (functionp func)
1434 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1435 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1437 (require (cdr func))
1438 (apply (car func) input-method (nthcdr 5 slot)))
1439 (error "Can't activate input method `%s'" input-method))))
1440 (setq current-input-method input-method)
1441 (or (stringp current-input-method-title)
1442 (setq current-input-method-title (nth 3 slot)))
1444 (run-hooks 'input-method-activate-hook)
1445 (force-mode-line-update)))))
1447 (defun inactivate-input-method ()
1448 "Turn off the current input method."
1449 (when current-input-method
1450 (if input-method-history
1451 (unless (string= current-input-method (car input-method-history))
1452 (setq input-method-history
1453 (cons current-input-method
1454 (delete current-input-method input-method-history))))
1455 (setq input-method-history (list current-input-method)))
1457 (funcall inactivate-current-input-method-function)
1459 (run-hooks 'input-method-inactivate-hook)
1460 (setq current-input-method nil
1461 input-method-function nil
1462 current-input-method-title nil)
1463 (force-mode-line-update)))))
1465 (defun set-input-method (input-method &optional interactive)
1466 "Select and activate input method INPUT-METHOD for the current buffer.
1467 This also sets the default input method to the one you specify.
1468 If INPUT-METHOD is nil, this function turns off the input method, and
1469 also causes you to be prompted for a name of an input method the next
1470 time you invoke \\[toggle-input-method].
1471 When called interactively, the optional arg INTERACTIVE is non-nil,
1472 which marks the variable `default-input-method' as set for Custom buffers.
1474 To deactivate the input method interactively, use \\[toggle-input-method].
1475 To deactivate it programmatically, use \\[inactivate-input-method]."
1477 (let* ((default (or (car input-method-history) default-input-method)))
1478 (list (read-input-method-name
1479 (if default "Select input method (default %s): " "Select input method: ")
1482 (activate-input-method input-method)
1483 (setq default-input-method input-method)
1485 (customize-mark-as-set 'default-input-method))
1486 default-input-method)
1488 (defun toggle-input-method (&optional arg interactive)
1489 "Enable or disable multilingual text input method for the current buffer.
1490 Only one input method can be enabled at any time in a given buffer.
1492 The normal action is to enable an input method if none was
1493 enabled, and disable the current one otherwise. Which input method
1494 to enable can be determined in various ways--either the one most
1495 recently used, or the one specified by `default-input-method', or
1496 as a last resort by reading the name of an input method in the
1499 With a prefix argument, read an input method name with the minibuffer
1500 and enable that one. The default is the most recent input method specified
1501 \(not including the currently active input method, if any).
1503 When called interactively, the optional arg INTERACTIVE is non-nil,
1504 which marks the variable `default-input-method' as set for Custom buffers."
1506 (interactive "P\np")
1507 (if (and current-input-method (not arg))
1508 (inactivate-input-method)
1509 (let ((default (or (car input-method-history) default-input-method)))
1510 (if (and arg default (equal current-input-method default)
1511 (> (length input-method-history) 1))
1512 (setq default (nth 1 input-method-history)))
1513 (activate-input-method
1514 (if (or arg (not default))
1516 (read-input-method-name
1517 (if default "Input method (default %s): " "Input method: " )
1520 (unless default-input-method
1522 (setq default-input-method current-input-method)
1524 (customize-mark-as-set 'default-input-method)))))))
1526 (defun describe-input-method (input-method)
1527 "Describe input method INPUT-METHOD."
1529 (list (read-input-method-name
1530 "Describe input method (default current choice): ")))
1531 (if (and input-method (symbolp input-method))
1532 (setq input-method (symbol-name input-method)))
1533 (help-setup-xref (list #'describe-input-method
1534 (or input-method current-input-method))
1537 (if (null input-method)
1538 (describe-current-input-method)
1539 (let ((current current-input-method))
1543 (activate-input-method input-method)
1544 (describe-current-input-method))
1545 (activate-input-method current))
1547 (activate-input-method current)
1548 (help-setup-xref (list #'describe-input-method input-method)
1550 (with-output-to-temp-buffer (help-buffer)
1551 (let ((elt (assoc input-method input-method-alist)))
1553 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1554 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1556 (defun describe-current-input-method ()
1557 "Describe the input method currently in use.
1558 This is a subroutine for `describe-input-method'."
1559 (if current-input-method
1560 (if (and (symbolp describe-current-input-method-function)
1561 (fboundp describe-current-input-method-function))
1562 (funcall describe-current-input-method-function)
1563 (message "No way to describe the current input method `%s'"
1564 current-input-method)
1566 (error "No input method is activated now")))
1568 (defun read-multilingual-string (prompt &optional initial-input input-method)
1569 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1570 The input method selected last time is activated in minibuffer.
1571 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1573 Optional 3rd argument INPUT-METHOD specifies the input method
1574 to be activated instead of the one selected last time. It is a symbol
1578 current-input-method
1579 default-input-method
1580 (read-input-method-name "Input method: " nil t)))
1581 (if (and input-method (symbolp input-method))
1582 (setq input-method (symbol-name input-method)))
1583 (let ((prev-input-method current-input-method))
1586 (activate-input-method input-method)
1587 (read-string prompt initial-input nil nil t))
1588 (activate-input-method prev-input-method))))
1590 ;; Variables to control behavior of input methods. All input methods
1591 ;; should react to these variables.
1593 (defcustom input-method-verbose-flag 'default
1594 "*A flag to control extra guidance given by input methods.
1595 The value should be nil, t, `complex-only', or `default'.
1597 The extra guidance is done by showing list of available keys in echo
1598 area. When you use the input method in the minibuffer, the guidance
1599 is shown at the bottom short window (split from the existing window).
1601 If the value is t, extra guidance is always given, if the value is
1602 nil, extra guidance is always suppressed.
1604 If the value is `complex-only', only complex input methods such as
1605 `chinese-py' and `japanese' give extra guidance.
1607 If the value is `default', complex input methods always give extra
1608 guidance, but simple input methods give it only when you are not in
1611 See also the variable `input-method-highlight-flag'."
1612 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1613 (const complex-only) (const default))
1616 (defcustom input-method-highlight-flag t
1617 "*If this flag is non-nil, input methods highlight partially-entered text.
1618 For instance, while you are in the middle of a Quail input method sequence,
1619 the text inserted so far is temporarily underlined.
1620 The underlining goes away when you finish or abort the input method sequence.
1621 See also the variable `input-method-verbose-flag'."
1625 (defvar input-method-activate-hook nil
1626 "Normal hook run just after an input method is activated.
1628 The variable `current-input-method' keeps the input method name
1631 (defvar input-method-inactivate-hook nil
1632 "Normal hook run just after an input method is inactivated.
1634 The variable `current-input-method' still keeps the input method name
1637 (defvar input-method-after-insert-chunk-hook nil
1638 "Normal hook run just after an input method insert some chunk of text.")
1640 (defvar input-method-exit-on-first-char nil
1641 "This flag controls when an input method returns.
1642 Usually, the input method does not return while there's a possibility
1643 that it may find a different translation if a user types another key.
1644 But, it this flag is non-nil, the input method returns as soon as
1645 the current key sequence gets long enough to have some valid translation.")
1647 (defvar input-method-use-echo-area nil
1648 "This flag controls how an input method shows an intermediate key sequence.
1649 Usually, the input method inserts the intermediate key sequence,
1650 or candidate translations corresponding to the sequence,
1651 at point in the current buffer.
1652 But, if this flag is non-nil, it displays them in echo area instead.")
1654 (defvar input-method-exit-on-invalid-key nil
1655 "This flag controls the behavior of an input method on invalid key input.
1656 Usually, when a user types a key which doesn't start any character
1657 handled by the input method, the key is handled by turning off the
1658 input method temporarily. After that key, the input method is re-enabled.
1659 But, if this flag is non-nil, the input method is never back on.")
1662 (defvar set-language-environment-hook nil
1663 "Normal hook run after some language environment is set.
1665 When you set some hook function here, that effect usually should not
1666 be inherited to another language environment. So, you had better set
1667 another function in `exit-language-environment-hook' (which see) to
1668 cancel the effect.")
1670 (defvar exit-language-environment-hook nil
1671 "Normal hook run after exiting from some language environment.
1672 When this hook is run, the variable `current-language-environment'
1673 is still bound to the language environment being exited.
1675 This hook is mainly used for canceling the effect of
1676 `set-language-environment-hook' (which-see).")
1678 (put 'setup-specified-language-environment 'apropos-inhibit t)
1680 (defun setup-specified-language-environment ()
1681 "Switch to a specified language environment."
1683 (let (language-name)
1684 (if (and (symbolp last-command-event)
1685 (or (not (eq last-command-event 'Default))
1686 (setq last-command-event 'English))
1687 (setq language-name (symbol-name last-command-event)))
1689 (set-language-environment language-name)
1690 (customize-mark-as-set 'current-language-environment))
1691 (error "Bogus calling sequence"))))
1693 (defcustom current-language-environment "English"
1694 "The last language environment specified with `set-language-environment'.
1695 This variable should be set only with \\[customize], which is equivalent
1696 to using the function `set-language-environment'."
1697 :link '(custom-manual "(emacs)Language Environments")
1698 :set (lambda (symbol value) (set-language-environment value))
1700 (or (car-safe (assoc-string
1701 (if (symbolp current-language-environment)
1702 (symbol-name current-language-environment)
1703 current-language-environment)
1704 language-info-alist t))
1706 ;; custom type will be updated with `set-language-info'.
1707 :type (if language-info-alist
1708 (cons 'choice (mapcar
1711 (sort (mapcar 'car language-info-alist) 'string<)))
1713 :initialize 'custom-initialize-default
1716 (defun reset-language-environment ()
1717 "Reset multilingual environment of Emacs to the default status.
1719 The default status is as follows:
1721 The default value of `buffer-file-coding-system' is nil.
1722 The default coding system for process I/O is nil.
1723 The default value for the command `set-terminal-coding-system' is nil.
1724 The default value for the command `set-keyboard-coding-system' is nil.
1726 The order of priorities of coding categories and the coding system
1727 bound to each category are as follows
1728 coding category coding system
1729 --------------------------------------------------
1730 coding-category-iso-8-1 iso-latin-1
1731 coding-category-iso-8-2 iso-latin-1
1732 coding-category-utf-8 mule-utf-8
1733 coding-category-utf-16-be mule-utf-16be-with-signature
1734 coding-category-utf-16-le mule-utf-16le-with-signature
1735 coding-category-iso-7-tight iso-2022-jp
1736 coding-category-iso-7 iso-2022-7bit
1737 coding-category-iso-7-else iso-2022-7bit-lock
1738 coding-category-iso-8-else iso-2022-8bit-ss2
1739 coding-category-emacs-mule emacs-mule
1740 coding-category-raw-text raw-text
1741 coding-category-sjis japanese-shift-jis
1742 coding-category-big5 chinese-big5
1743 coding-category-ccl nil
1744 coding-category-binary no-conversion"
1746 ;; This function formerly set default-enable-multibyte-characters to t,
1747 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1749 (setq coding-category-iso-7-tight 'iso-2022-jp
1750 coding-category-iso-7 'iso-2022-7bit
1751 coding-category-iso-8-1 'iso-latin-1
1752 coding-category-iso-8-2 'iso-latin-1
1753 coding-category-iso-7-else 'iso-2022-7bit-lock
1754 coding-category-iso-8-else 'iso-2022-8bit-ss2
1755 coding-category-emacs-mule 'emacs-mule
1756 coding-category-raw-text 'raw-text
1757 coding-category-sjis 'japanese-shift-jis
1758 coding-category-big5 'chinese-big5
1759 coding-category-utf-16-be 'mule-utf-16be-with-signature
1760 coding-category-utf-16-le 'mule-utf-16le-with-signature
1761 coding-category-utf-8 'mule-utf-8
1762 coding-category-ccl nil
1763 coding-category-binary 'no-conversion)
1765 (set-coding-priority
1766 '(coding-category-iso-8-1
1767 coding-category-iso-8-2
1768 coding-category-utf-8
1769 coding-category-utf-16-be
1770 coding-category-utf-16-le
1771 coding-category-iso-7-tight
1772 coding-category-iso-7
1773 coding-category-iso-7-else
1774 coding-category-iso-8-else
1775 coding-category-emacs-mule
1776 coding-category-raw-text
1777 coding-category-sjis
1778 coding-category-big5
1780 coding-category-binary))
1782 ;; Changing the binding of a coding category requires this call.
1783 (update-coding-systems-internal)
1785 (set-default-coding-systems nil)
1786 (setq default-sendmail-coding-system 'iso-latin-1)
1787 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1788 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1789 (setq default-file-name-coding-system 'iso-latin-1)
1790 ;; Preserve eol-type from existing default-process-coding-systems.
1791 ;; On non-unix-like systems in particular, these may have been set
1792 ;; carefully by the user, or by the startup code, to deal with the
1793 ;; users shell appropriately, so should not be altered by changing
1794 ;; language environment.
1795 (let ((output-coding
1796 ;; When bootstrapping, coding-systems are not defined yet, so
1797 ;; we need to catch the error from check-coding-system.
1799 (coding-system-change-text-conversion
1800 (car default-process-coding-system) 'undecided)
1801 (coding-system-error 'undecided)))
1804 (coding-system-change-text-conversion
1805 (cdr default-process-coding-system) 'iso-latin-1)
1806 (coding-system-error 'iso-latin-1))))
1807 (setq default-process-coding-system
1808 (cons output-coding input-coding)))
1810 ;; Don't alter the terminal and keyboard coding systems here.
1811 ;; The terminal still supports the same coding system
1812 ;; that it supported a minute ago.
1813 ;; (set-terminal-coding-system-internal nil)
1814 ;; (set-keyboard-coding-system-internal nil)
1816 (setq nonascii-translation-table nil
1817 nonascii-insert-offset 0)
1819 ;; Don't invoke fontset-related functions if fontsets aren't
1820 ;; supported in this build of Emacs.
1821 (and (fboundp 'fontset-list)
1822 (set-overriding-fontspec-internal nil)))
1824 (reset-language-environment)
1826 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system display)
1827 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1828 (let ((coding (get-language-info language-name 'unibyte-display)))
1830 (or (not coding-system)
1831 (coding-system-equal coding coding-system)))
1832 (standard-display-european-internal)
1833 ;; The following 2 lines undo the 8-bit display that we set up
1834 ;; in standard-display-european-internal, which see. This is in
1835 ;; case the user has used standard-display-european earlier in
1836 ;; this session. (The MS-DOS port doesn't use that setup, so it
1837 ;; doesn't need to undo it.)
1838 (when standard-display-table
1840 (aset standard-display-table (+ i 128) nil))))
1841 (or (eq window-system 'pc)
1842 (set-terminal-coding-system (or coding-system coding) display))))
1844 (defun set-language-environment (language-name)
1845 "Set up multi-lingual environment for using LANGUAGE-NAME.
1846 This sets the coding system priority and the default input method
1847 and sometimes other things. LANGUAGE-NAME should be a string
1848 which is the name of a language environment. For example, \"Latin-1\"
1849 specifies the character set for the major languages of Western Europe."
1850 (interactive (list (read-language-name
1852 "Set language environment (default English): ")))
1854 (if (symbolp language-name)
1855 (setq language-name (symbol-name language-name)))
1856 (setq language-name "English"))
1857 (let ((slot (assoc-string language-name language-info-alist t)))
1859 (error "Language environment not defined: %S" language-name))
1860 (setq language-name (car slot)))
1861 (if current-language-environment
1862 (let ((func (get-language-info current-language-environment
1864 (run-hooks 'exit-language-environment-hook)
1865 (if (functionp func) (funcall func))))
1867 (reset-language-environment)
1868 ;; The features might set up coding systems.
1869 (let ((required-features (get-language-info language-name 'features)))
1870 (while required-features
1871 (require (car required-features))
1872 (setq required-features (cdr required-features))))
1874 (setq current-language-environment language-name)
1876 (set-language-environment-coding-systems language-name)
1877 (set-language-environment-input-method language-name)
1878 (set-language-environment-nonascii-translation language-name)
1879 (set-language-environment-charset language-name)
1880 (set-language-environment-fontset language-name)
1881 ;; Unibyte setups if necessary.
1882 (unless default-enable-multibyte-characters
1883 (set-language-environment-unibyte language-name))
1885 (let ((func (get-language-info language-name 'setup-function)))
1886 (if (functionp func)
1889 (run-hooks 'set-language-environment-hook)
1890 (force-mode-line-update t))
1892 (defun standard-display-european-internal ()
1893 ;; Actually set up direct output of non-ASCII characters.
1894 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1895 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1896 ;; the native font, and codes 160 and 146 stand for something very
1898 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1900 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1901 (unless (and (eq window-system 'x)
1902 ;; XFree86 4 has fixed the fonts.
1903 (string= "The XFree86 Project, Inc" (x-server-vendor))
1904 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1906 ;; Make non-line-break space display as a plain space.
1907 (aset standard-display-table 160 [32]))
1908 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1909 ;; don't contain a character at that position. Map it to the ASCII
1910 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1911 ;; U+2019, normally from the windows-1252 character set. XFree 4
1912 ;; fonts probably have the appropriate glyph at this position,
1913 ;; so they could use standard-display-8bit. It's better to use a
1914 ;; proper windows-1252 coding system. --fx]
1915 (aset standard-display-table 146 [39]))))
1917 (defun set-language-environment-coding-systems (language-name)
1918 "Do various coding system setups for language environment LANGUAGE-NAME."
1919 (let* ((priority (get-language-info language-name 'coding-priority))
1920 (default-coding (car priority))
1921 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1923 (let ((categories (mapcar 'coding-system-category priority)))
1924 (set-default-coding-systems
1925 (if (memq eol-type '(0 1 2 unix dos mac))
1926 (coding-system-change-eol-conversion default-coding eol-type)
1928 (setq default-sendmail-coding-system default-coding)
1929 (set-coding-priority categories)
1931 (set (car categories) (car priority))
1932 (setq priority (cdr priority) categories (cdr categories)))
1933 ;; Changing the binding of a coding category requires this call.
1934 (update-coding-systems-internal)))))
1936 (defun set-language-environment-input-method (language-name)
1937 "Do various input method setups for language environment LANGUAGE-NAME."
1938 (let ((input-method (get-language-info language-name 'input-method)))
1940 (setq default-input-method input-method)
1941 (if input-method-history
1942 (setq input-method-history
1944 (delete input-method input-method-history)))))))
1946 (defun set-language-environment-nonascii-translation (language-name)
1947 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1948 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1950 (if (eq window-system 'pc)
1952 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1954 ((char-table-p nonascii)
1955 (setq nonascii-translation-table nonascii))
1956 ((and (eq window-system 'pc) (boundp dos-table))
1957 ;; DOS terminals' default is to use a special non-ASCII translation
1958 ;; table as appropriate for the installed codepage.
1959 (setq nonascii-translation-table (symbol-value dos-table)))
1960 ((charsetp nonascii)
1961 (setq nonascii-insert-offset (- (make-char nonascii) 128))))))
1963 (defun set-language-environment-charset (language-name)
1964 "Do various charset setups for language environment LANGUAGE-NAME."
1965 (if (and utf-translate-cjk-mode
1966 (not (eq utf-translate-cjk-lang-env language-name))
1968 (dolist (charset (get-language-info language-name 'charset))
1969 (if (memq charset utf-translate-cjk-charsets)
1972 (utf-translate-cjk-load-tables)))
1974 (defun set-language-environment-fontset (language-name)
1975 "Do various fontset setups for language environment LANGUAGE-NAME."
1976 ;; Don't invoke fontset-related functions if fontsets aren't
1977 ;; supported in this build of Emacs.
1978 (if (fboundp 'fontset-list)
1979 (set-overriding-fontspec-internal
1980 (get-language-info language-name 'overriding-fontspec))))
1982 (defun set-language-environment-unibyte (language-name)
1983 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1984 ;; Syntax and case table.
1985 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1987 (let ((set-case-syntax-set-multibyte nil))
1988 (load syntax nil t))
1989 ;; No information for syntax and case. Reset to the defaults.
1990 (let ((syntax-table (standard-syntax-table))
1991 (standard-table (standard-case-table))
1992 (case-table (make-char-table 'case-table))
1993 (ch (if (eq window-system 'pc) 128 160)))
1995 (modify-syntax-entry ch " " syntax-table)
1998 (aset case-table i (aref standard-table i)))
1999 (set-char-table-extra-slot case-table 0 nil)
2000 (set-char-table-extra-slot case-table 1 nil)
2001 (set-char-table-extra-slot case-table 2 nil)
2002 (set-standard-case-table case-table))
2003 (let ((list (buffer-list)))
2005 (with-current-buffer (car list)
2006 (set-case-table (standard-case-table)))
2007 (setq list (cdr list))))))
2008 (set-display-table-and-terminal-coding-system language-name))
2010 (defsubst princ-list (&rest args)
2011 "Print all arguments with `princ', then print \"\n\"."
2012 (while args (princ (car args)) (setq args (cdr args)))
2015 (put 'describe-specified-language-support 'apropos-inhibit t)
2017 ;; Print language-specific information such as input methods,
2018 ;; charsets, and coding systems. This function is intended to be
2019 ;; called from the menu:
2020 ;; [menu-bar mule describe-language-environment LANGUAGE]
2021 ;; and should not run it by `M-x describe-current-input-method-function'.
2022 (defun describe-specified-language-support ()
2023 "Describe how Emacs supports the specified language environment."
2025 (let (language-name)
2026 (if (not (and (symbolp last-command-event)
2027 (or (not (eq last-command-event 'Default))
2028 (setq last-command-event 'English))
2029 (setq language-name (symbol-name last-command-event))))
2030 (error "Bogus calling sequence"))
2031 (describe-language-environment language-name)))
2033 (defun describe-language-environment (language-name)
2034 "Describe how Emacs supports language environment LANGUAGE-NAME."
2036 (list (read-language-name
2038 "Describe language environment (default current choice): ")))
2039 (if (null language-name)
2040 (setq language-name current-language-environment))
2041 (if (or (null language-name)
2042 (null (get-language-info language-name 'documentation)))
2043 (error "No documentation for the specified language"))
2044 (if (symbolp language-name)
2045 (setq language-name (symbol-name language-name)))
2046 (dolist (feature (get-language-info language-name 'features))
2048 (let ((doc (get-language-info language-name 'documentation)))
2049 (help-setup-xref (list #'describe-language-environment language-name)
2051 (with-output-to-temp-buffer (help-buffer)
2053 (set-buffer standard-output)
2054 (insert language-name " language environment\n\n")
2056 (insert doc "\n\n"))
2058 (let ((str (eval (get-language-info language-name 'sample-text))))
2060 (insert "Sample text:\n " str "\n\n")))
2062 (let ((input-method (get-language-info language-name 'input-method))
2063 (l (copy-sequence input-method-alist)))
2064 (insert "Input methods")
2066 (insert " (default " input-method ")")
2067 (setq input-method (assoc input-method input-method-alist))
2068 (setq l (cons input-method (delete input-method l))))
2071 (when (string= language-name (nth 1 (car l)))
2072 (insert " " (car (car l)))
2073 (search-backward (car (car l)))
2074 (help-xref-button 0 'help-input-method (car (car l)))
2075 (goto-char (point-max))
2077 (if (stringp (nth 3 (car l)))
2079 (car (nth 3 (car l))))
2080 "\" in mode line)\n"))
2083 (insert "Character sets:\n")
2084 (let ((l (get-language-info language-name 'charset)))
2086 (insert " nothing specific to " language-name "\n")
2088 (insert " " (symbol-name (car l)))
2089 (search-backward (symbol-name (car l)))
2090 (help-xref-button 0 'help-character-set (car l))
2091 (goto-char (point-max))
2092 (insert ": " (charset-description (car l)) "\n")
2095 (insert "Coding systems:\n")
2096 (let ((l (get-language-info language-name 'coding-system)))
2098 (insert " nothing specific to " language-name "\n")
2100 (insert " " (symbol-name (car l)))
2101 (search-backward (symbol-name (car l)))
2102 (help-xref-button 0 'help-coding-system (car l))
2103 (goto-char (point-max))
2105 (coding-system-mnemonic (car l))
2106 "' in mode line):\n\t"
2107 (coding-system-doc-string (car l))
2109 (let ((aliases (coding-system-get (car l)
2110 'alias-coding-systems)))
2112 (insert "\t(alias:")
2114 (insert " " (symbol-name (car aliases)))
2115 (setq aliases (cdr aliases)))
2117 (setq l (cdr l)))))))))
2121 (defvar locale-translation-file-name nil
2122 "File name for the system's file of locale-name aliases, or nil if none.")
2124 ;; The following definitions might as well be marked as constants and
2125 ;; purecopied, since they're normally used on startup, and probably
2126 ;; should reflect the facilities of the base Emacs.
2127 (defconst locale-language-names
2130 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2131 ;; as specified in the Single Unix Spec, Version 2.
2132 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2133 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2134 ;; see Internet RFC 2165 (1997-06) and
2135 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2136 ;; TERRITORY is a country code taken from ISO 3166
2137 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2138 ;; CODESET and MODIFIER are implementation-dependent.
2140 ;; jasonr comments: MS Windows uses three letter codes for
2141 ;; languages instead of the two letter ISO codes that POSIX
2142 ;; uses. In most cases the first two letters are the same, so
2143 ;; most of the regexps in locale-language-names work. Japanese
2144 ;; and Chinese are exceptions, which are listed in the
2145 ;; non-standard section at the bottom of locale-language-names.
2147 ("aa_DJ" . "Latin-1") ; Afar
2150 ("af" . "Latin-1") ; Afrikaans
2151 ("am" "Ethiopic" utf-8) ; Amharic
2152 ("an" . "Latin-9") ; Aragonese
2153 ; ar Arabic glibc uses 8859-6
2156 ("az" . "UTF-8") ; Azerbaijani
2158 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2159 ("bg" "Bulgarian" cp1251) ; Bulgarian
2162 ("bn" . "UTF-8") ; Bengali, Bangla
2164 ("br" . "Latin-1") ; Breton
2165 ("bs" . "Latin-2") ; Bosnian
2166 ("byn" . "UTF-8") ; Bilin; Blin
2167 ("ca" . "Latin-1") ; Catalan
2169 ("cs" "Czech" iso-8859-2)
2170 ("cy" "Welsh" iso-8859-14)
2171 ("da" . "Latin-1") ; Danish
2172 ("de" "German" iso-8859-1)
2175 ("el" "Greek" iso-8859-7)
2176 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2177 ;; That's actually what the GNU locales define, modulo things like
2179 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2180 ("en" "English" iso-8859-1) ; English
2181 ("eo" . "Latin-3") ; Esperanto
2182 ("es" "Spanish" iso-8859-1)
2183 ("et" . "Latin-1") ; Estonian
2184 ("eu" . "Latin-1") ; Basque
2185 ("fa" . "UTF-8") ; Persian
2186 ("fi" . "Latin-1") ; Finnish
2187 ("fj" . "Latin-1") ; Fiji
2188 ("fo" . "Latin-1") ; Faroese
2189 ("fr" "French" iso-8859-1) ; French
2190 ("fy" . "Latin-1") ; Frisian
2191 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2192 ("gd" . "Latin-9") ; Scots Gaelic
2193 ("gez" "Ethiopic" utf-8) ; Geez
2194 ("gl" . "Latin-1") ; Gallegan; Galician
2196 ("gu" . "UTF-8") ; Gujarati
2197 ("gv" . "Latin-1") ; Manx Gaelic
2199 ("he" "Hebrew" iso-8859-8)
2200 ("hi" "Devanagari" utf-8) ; Hindi
2201 ("hr" "Croatian" iso-8859-2) ; Croatian
2202 ("hu" . "Latin-2") ; Hungarian
2205 ("id" . "Latin-1") ; Indonesian
2208 ("is" . "Latin-1") ; Icelandic
2209 ("it" "Italian" iso-8859-1) ; Italian
2211 ("iw" "Hebrew" iso-8859-8)
2212 ("ja" "Japanese" euc-jp)
2214 ("ka" "Georgian" georgian-ps) ; Georgian
2216 ("kl" . "Latin-1") ; Greenlandic
2218 ("kn" "Kannada" utf-8)
2219 ("ko" "Korean" euc-kr)
2222 ("kw" . "Latin-1") ; Cornish
2224 ("la" . "Latin-1") ; Latin
2225 ("lb" . "Latin-1") ; Luxemburgish
2226 ("lg" . "Laint-6") ; Ganda
2228 ("lo" "Lao" utf-8) ; Laothian
2229 ("lt" "Lithuanian" iso-8859-13)
2230 ("lv" . "Latvian") ; Latvian, Lettish
2232 ("mi" . "Latin-7") ; Maori
2233 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2234 ("ml" "Malayalam" utf-8)
2235 ("mn" . "UTF-8") ; Mongolian
2237 ("mr" "Devanagari" utf-8) ; Marathi
2238 ("ms" . "Latin-1") ; Malay
2239 ("mt" . "Latin-3") ; Maltese
2242 ("nb" . "Latin-1") ; Norwegian
2243 ("ne" "Devanagari" utf-8) ; Nepali
2244 ("nl" "Dutch" iso-8859-1)
2245 ("no" . "Latin-1") ; Norwegian
2246 ("oc" . "Latin-1") ; Occitan
2247 ("om_ET" . "UTF-8") ; (Afan) Oromo
2248 ("om" . "Latin-1") ; (Afan) Oromo
2250 ("pa" . "UTF-8") ; Punjabi
2251 ("pl" . "Latin-2") ; Polish
2253 ("pt" . "Latin-1") ; Portuguese
2255 ("rm" . "Latin-1") ; Rhaeto-Romanic
2257 ("ro" "Romanian" iso-8859-2)
2258 ("ru_RU" "Russian" iso-8859-5)
2259 ("ru_UA" "Russian" koi8-u)
2261 ("sa" . "Devanagari") ; Sanskrit
2263 ("se" . "UTF-8") ; Northern Sami
2265 ("sh" . "Latin-2") ; Serbo-Croatian
2267 ("sid" . "UTF-8") ; Sidamo
2268 ("sk" "Slovak" iso-8859-2)
2269 ("sl" "Slovenian" iso-8859-2)
2272 ("so_ET" "UTF-8") ; Somali
2273 ("so" "Latin-1") ; Somali
2274 ("sq" . "Latin-1") ; Albanian
2275 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet)
2276 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2278 ("st" . "Latin-1") ; Sesotho
2280 ("sv" "Swedish" iso-8859-1) ; Swedish
2281 ("sw" . "Latin-1") ; Swahili
2282 ("ta" "Tamil" utf-8)
2283 ("te" . "UTF-8") ; Telugu
2284 ("tg" "Tajik" koi8-t)
2285 ("th" "Thai" tis-620)
2286 ("ti" "Ethiopic" utf-8) ; Tigrinya
2287 ("tig_ER" . "UTF-8") ; Tigre
2289 ("tl" . "Latin-1") ; Tagalog
2292 ("tr" "Turkish" iso-8859-9)
2294 ("tt" . "UTF-8") ; Tatar
2297 ("uk" "Ukrainian" koi8-u)
2298 ("ur" . "UTF-8") ; Urdu
2299 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2300 ("uz" . "Latin-1") ; Uzbek
2301 ("vi" "Vietnamese" utf-8)
2303 ("wa" . "Latin-1") ; Walloon
2305 ("xh" . "Latin-1") ; Xhosa
2306 ("yi" . "Windows-1255") ; Yiddish
2309 ("zh_HK" . "Chinese-Big5")
2310 ("zh_TW" . "Chinese-Big5")
2311 ("zh_CN" . "Chinese-GB")
2312 ("zh" . "Chinese-GB")
2313 ; zh_CN.GB18030/GB18030 \
2315 ; zh_HK/BIG5-HKSCS \
2316 ("zu" . "Latin-1") ; Zulu
2318 ;; ISO standard locales
2320 ("posix$" . "ASCII")
2322 ;; The "IPA" Emacs language environment does not correspond
2323 ;; to any ISO 639 code, so let it stand for itself.
2326 ;; Nonstandard or obsolete language codes
2327 ("cz" . "Czech") ; e.g. Solaris 2.6
2328 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2329 ("iw" . "Hebrew") ; e.g. X11R6.4
2330 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2331 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2332 ("jp" . "Japanese") ; e.g. MS Windows
2333 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2334 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2335 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2336 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2337 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2338 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2340 "Alist of locale regexps vs the corresponding languages and coding systems.
2341 Each element has these form:
2342 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2343 The first element whose LOCALE-REGEXP matches the start of a
2344 downcased locale specifies the LANG-ENV \(language environtment)
2345 and CODING-SYSTEM corresponding to that locale. If there is no
2346 appropriate language environment, the element may have this form:
2347 \(LOCALE-REGEXP . LANG-ENV)
2348 In this case, LANG-ENV is one of generic language environments for an
2349 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2351 (defconst locale-charset-language-names
2353 '((".*8859[-_]?1\\>" . "Latin-1")
2354 (".*8859[-_]?2\\>" . "Latin-2")
2355 (".*8859[-_]?3\\>" . "Latin-3")
2356 (".*8859[-_]?4\\>" . "Latin-4")
2357 (".*8859[-_]?9\\>" . "Latin-5")
2358 (".*8859[-_]?14\\>" . "Latin-8")
2359 (".*8859[-_]?15\\>" . "Latin-9")
2360 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2361 ;; utf-8@euro exists, so put this last. (@euro really specifies
2362 ;; the currency, rather than the charset.)
2363 (".*@euro\\>" . "Latin-9")))
2364 "List of pairs of locale regexps and charset language names.
2365 The first element whose locale regexp matches the start of a downcased locale
2366 specifies the language name whose charset corresponds to that locale.
2367 This language name is used if the locale is not listed in
2368 `locale-language-names'")
2370 (defconst locale-preferred-coding-systems
2372 '((".*8859[-_]?1\\>" . iso-8859-1)
2373 (".*8859[-_]?2\\>" . iso-8859-2)
2374 (".*8859[-_]?3\\>" . iso-8859-3)
2375 (".*8859[-_]?4\\>" . iso-8859-4)
2376 (".*8859[-_]?9\\>" . iso-8859-9)
2377 (".*8859[-_]?14\\>" . iso-8859-14)
2378 (".*8859[-_]?15\\>" . iso-8859-15)
2379 (".*utf\\(?:-?8\\)?" . utf-8)
2380 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2381 ;; specifies the currency, rather than the charset.)
2382 (".*@euro" . iso-8859-15)
2383 ("koi8-?r" . koi8-r)
2384 ("koi8-?u" . koi8-u)
2387 ("euc-?tw" . euc-tw)
2388 ;; We don't support GBK, but as it is upper compatible with
2389 ;; GB-2312, we setup the default coding system to gb2312.
2391 ;; We don't support BIG5-HKSCS, but as it is upper compatible with
2392 ;; BIG5, we setup the default coding system to big5.
2393 ("big5hkscs" . big5)
2394 ("ja.*[._]euc" . japanese-iso-8bit)
2395 ("ja.*[._]jis7" . iso-2022-jp)
2396 ("ja.*[._]pck" . japanese-shift-jis)
2397 ("ja.*[._]sjis" . japanese-shift-jis)
2398 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2400 "List of pairs of locale regexps and preferred coding systems.
2401 The first element whose locale regexp matches the start of a downcased locale
2402 specifies the coding system to prefer when using that locale.
2403 This coding system is used if the locale specifies a specific charset.")
2405 (defun locale-name-match (key alist)
2406 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2407 Return the value corresponding to the first regexp that matches the
2408 start of KEY, or nil if there is no match."
2410 (while (and alist (not element))
2411 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2412 (setq element (car alist)))
2413 (setq alist (cdr alist)))
2416 (defun locale-charset-match-p (charset1 charset2)
2417 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2418 Matching is done ignoring case and any hyphens and underscores in the
2419 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2420 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2421 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2422 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2424 (defvar locale-charset-alist nil
2425 "Coding system alist keyed on locale-style charset name.
2426 Used by `locale-charset-to-coding-system'.")
2428 (defun locale-charset-to-coding-system (charset)
2429 "Find coding system corresponding to CHARSET.
2430 CHARSET is any sort of non-Emacs charset name, such as might be used
2431 in a locale codeset, or elsewhere. It is matched to a coding system
2432 first by case-insensitive lookup in `locale-charset-alist'. Then
2433 matches are looked for in the coding system list, treating case and
2434 the characters `-' and `_' as insignificant. The coding system base
2435 is returned. Thus, for instance, if charset \"ISO8859-2\",
2436 `iso-latin-2' is returned."
2437 (or (car (assoc-string charset locale-charset-alist t))
2438 (let ((cs coding-system-alist)
2440 (while (and (not c) cs)
2441 (if (locale-charset-match-p charset (caar cs))
2442 (setq c (intern (caar cs)))
2444 (if c (coding-system-base c)))))
2446 ;; Fixme: This ought to deal with the territory part of the locale
2447 ;; too, for setting things such as calendar holidays, ps-print paper
2448 ;; size, spelling dictionary.
2450 (defun set-locale-environment (&optional locale-name display)
2451 "Set up multi-lingual environment for using LOCALE-NAME.
2452 This sets the language environment, the coding system priority,
2453 the default input method and sometimes other things.
2455 LOCALE-NAME should be a string which is the name of a locale supported
2456 by the system. Often it is of the form xx_XX.CODE, where xx is a
2457 language, XX is a country, and CODE specifies a character set and
2458 coding system. For example, the locale name \"ja_JP.EUC\" might name
2459 a locale for Japanese in Japan using the `japanese-iso-8bit'
2460 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2463 If LOCALE-NAME is nil, its value is taken from the environment
2464 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2466 The locale names supported by your system can typically be found in a
2467 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2468 will be translated according to the table specified by
2469 `locale-translation-file-name'.
2471 If DISPLAY is non-nil, only set the keyboard coding system and
2472 the terminal coding system for the given display, and don't touch
2473 session-global parameters like the language environment. DISPLAY
2474 may be a display id or a frame.
2476 See also `locale-charset-language-names', `locale-language-names',
2477 `locale-preferred-coding-systems' and `locale-coding-system'."
2478 (interactive "sSet environment for locale: ")
2480 ;; Do this at runtime for the sake of binaries possibly transported
2481 ;; to a system without X.
2482 (setq locale-translation-file-name
2484 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2485 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2486 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2487 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2489 ;; The following name appears after the X-related names above,
2490 ;; since the X-related names are what X actually uses.
2491 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2493 (while (and files (not (file-exists-p (car files))))
2494 (setq files (cdr files)))
2497 (let ((locale locale-name))
2500 ;; Use the first of these three environment variables
2501 ;; that has a nonempty value.
2502 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2504 (= 0 (length locale))) ; nil or empty string
2505 (setq locale (getenv (pop vars) display)))))
2508 ;; The two tests are kept separate so the byte-compiler sees
2509 ;; that mac-get-preference is only called after checking its existence.
2510 (when (fboundp 'mac-get-preference)
2511 (setq locale (mac-get-preference "AppleLocale"))
2513 (let ((languages (mac-get-preference "AppleLanguages")))
2514 (unless (= (length languages) 0) ; nil or empty vector
2515 (setq locale (aref languages 0)))))))
2516 (unless (or locale (not (boundp 'mac-system-locale)))
2517 (setq locale mac-system-locale))
2521 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2522 ;; using the translation file that many systems have.
2523 (when locale-translation-file-name
2525 (insert-file-contents locale-translation-file-name)
2526 (when (re-search-forward
2527 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2528 (setq locale (buffer-substring (point) (line-end-position))))))
2530 ;; Leave the system locales alone if the caller did not specify
2531 ;; an explicit locale name, as their defaults are set from
2532 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2533 ;; want to set them to the same value as LC_CTYPE.
2535 (setq system-messages-locale locale)
2536 (setq system-time-locale locale))
2538 (setq locale (downcase locale))
2540 (let ((language-name
2541 (locale-name-match locale locale-language-names))
2542 (charset-language-name
2543 (locale-name-match locale locale-charset-language-names))
2544 (default-eol-type (coding-system-eol-type
2545 default-buffer-file-coding-system))
2547 (or (locale-name-match locale locale-preferred-coding-systems)
2549 (if (string-match "\\.\\([^@]+\\)" locale)
2550 (locale-charset-to-coding-system
2551 (match-string 1 locale))))
2552 (and (eq system-type 'macos) mac-system-coding-system))))
2554 (if (consp language-name)
2555 ;; locale-language-names specify both lang-env and coding.
2556 ;; But, what specified in locale-preferred-coding-systems
2557 ;; has higher priority.
2558 (setq coding-system (or coding-system
2559 (nth 1 language-name))
2560 language-name (car language-name))
2561 ;; Otherwise, if locale is not listed in locale-language-names,
2562 ;; use what listed in locale-charset-language-names.
2563 (if (not language-name)
2564 (setq language-name charset-language-name)))
2566 ;; If a specific EOL conversion was specified in the default
2567 ;; buffer-file-coding-system, preserve it in the coding system
2568 ;; we will be using from now on.
2569 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2571 (coding-system-p coding-system))
2572 (setq coding-system (coding-system-change-eol-conversion
2573 coding-system default-eol-type)))
2577 ;; Set up for this character set. This is now the right way
2578 ;; to do it for both unibyte and multibyte modes.
2580 (set-language-environment language-name))
2582 ;; If default-enable-multibyte-characters is nil,
2583 ;; we are using single-byte characters,
2584 ;; so the display table and terminal coding system are irrelevant.
2585 (when default-enable-multibyte-characters
2586 (set-display-table-and-terminal-coding-system
2587 language-name coding-system display))
2589 ;; Set the `keyboard-coding-system' if appropriate (tty
2590 ;; only). At least X and MS Windows can generate
2591 ;; multilingual input.
2592 ;; XXX This was disabled unless `window-system', but that
2593 ;; leads to buggy behaviour when a tty frame is opened
2594 ;; later. Setting the keyboard coding system has no adverse
2595 ;; effect on X, so let's do it anyway. -- Lorentey
2596 (let ((kcs (or coding-system
2597 (car (get-language-info language-name
2599 (if kcs (set-keyboard-coding-system kcs display)))
2602 (setq locale-coding-system
2603 (car (get-language-info language-name 'coding-priority)))))
2605 (when (and (not display)
2607 (not (coding-system-equal coding-system
2608 locale-coding-system)))
2609 (prefer-coding-system coding-system)
2610 (setq locale-coding-system coding-system))))
2612 ;; On Windows, override locale-coding-system,
2613 ;; keyboard-coding-system with system codepage. Note:
2614 ;; selection-coding-system is already set in w32select.c.
2615 (when (boundp 'w32-ansi-code-page)
2616 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2617 (when (coding-system-p code-page-coding)
2618 (unless display (setq locale-coding-system code-page-coding))
2619 (set-keyboard-coding-system code-page-coding display)
2620 (set-terminal-coding-system code-page-coding display))))
2622 (when (eq system-type 'darwin)
2623 ;; On Darwin, file names are always encoded in utf-8, no matter
2625 (setq default-file-name-coding-system 'utf-8)
2626 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2628 (when (and (null window-system)
2629 (equal (getenv "TERM_PROGRAM" display) "Apple_Terminal"))
2630 (set-terminal-coding-system 'utf-8)
2631 (set-keyboard-coding-system 'utf-8)))
2633 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2634 ;; (See comments in Flocale_info.)
2636 (let ((locale locale)
2637 (paper (locale-info 'paper)))
2639 ;; This will always be null at the time of writing.
2641 ((equal paper '(216 279))
2642 (setq ps-paper-type 'letter))
2643 ((equal paper '(210 297))
2644 (setq ps-paper-type 'a4)))
2645 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2646 (while (and vars (= 0 (length locale)))
2647 (setq locale (getenv (pop vars) display))))
2649 ;; As of glibc 2.2.5, these are the only US Letter locales,
2650 ;; and the rest are A4.
2652 (or (locale-name-match locale '(("c$" . letter)
2657 ("enu$" . letter) ; Windows
2664 ;;; Charset property
2666 (defun get-charset-property (charset propname)
2667 "Return the value of CHARSET's PROPNAME property.
2668 This is the last value stored with
2669 (put-charset-property CHARSET PROPNAME VALUE)."
2670 (and (not (eq charset 'composition))
2671 (plist-get (charset-plist charset) propname)))
2673 (defun put-charset-property (charset propname value)
2674 "Store CHARSETS's PROPNAME property with value VALUE.
2675 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2676 (or (eq charset 'composition)
2677 (set-charset-plist charset
2678 (plist-put (charset-plist charset) propname value))))
2680 ;;; Character code property
2681 (put 'char-code-property-table 'char-table-extra-slots 0)
2683 (defvar char-code-property-table
2684 (make-char-table 'char-code-property-table)
2685 "Char-table containing a property list of each character code.
2687 See also the documentation of `get-char-code-property' and
2688 `put-char-code-property'.")
2690 (defun get-char-code-property (char propname)
2691 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2692 (let ((plist (aref char-code-property-table char)))
2694 (car (cdr (memq propname plist))))))
2696 (defun put-char-code-property (char propname value)
2697 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2698 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2699 (let ((plist (aref char-code-property-table char)))
2701 (let ((slot (memq propname plist)))
2703 (setcar (cdr slot) value)
2704 (nconc plist (list propname value))))
2705 (aset char-code-property-table char (list propname value)))))
2708 ;; Pretty description of encoded string
2710 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2711 (defvar iso-2022-control-alist
2719 (defun encoded-string-description (str coding-system)
2720 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2721 (setq str (string-as-unibyte str))
2723 (if (and coding-system (eq (coding-system-type coding-system) 2))
2724 ;; Try to get a pretty description for ISO 2022 escape sequences.
2725 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2726 (format "#x%02X" x))))
2727 (function (lambda (x) (format "#x%02X" x))))
2730 (defun encode-coding-char (char coding-system)
2731 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2732 If CODING-SYSTEM can't safely encode CHAR, return nil."
2733 (let ((str1 (string-as-multibyte (char-to-string char)))
2734 (str2 (string-as-multibyte (make-string 2 char)))
2735 (safe-chars (and coding-system
2736 (coding-system-get coding-system 'safe-chars)))
2737 (charset (char-charset char))
2739 (when (or (eq safe-chars t)
2741 (and safe-chars (aref safe-chars char)))
2742 ;; We must find the encoded string of CHAR. But, just encoding
2743 ;; CHAR will put extra control sequences (usually to designate
2744 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2745 ;; To exclude such tailing bytes, we at first encode one-char
2746 ;; string and two-char string, then check how many bytes at the
2747 ;; tail of both encoded strings are the same.
2749 (setq enc1 (encode-coding-string str1 coding-system)
2751 enc2 (encode-coding-string str2 coding-system)
2753 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2754 (setq i1 (1- i1) i2 (1- i2)))
2756 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2757 ;; and they are the extra control sequences at the tail to
2759 (substring enc2 0 i2))))
2762 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2763 ;;; mule-cmds.el ends here