1 ;;; mule-cmds.el --- commands for mulitilingual environment -*-coding: iso-2022-7bit -*-
2 ;; Copyright (C) 1995, 2003 Electrotechnical Laboratory, JAPAN.
3 ;; Licensed to the Free Software Foundation.
4 ;; Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H13PRO009
9 ;; Keywords: mule, i18n
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., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
36 ;;; MULE related key bindings and menus.
38 (defvar mule-keymap (make-sparse-keymap)
39 "Keymap for Mule (Multilingual environment) specific commands.")
41 ;; Keep "C-x C-m ..." for mule specific commands.
42 (define-key ctl-x-map "\C-m" mule-keymap)
44 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
45 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
46 (define-key mule-keymap "F" 'set-file-name-coding-system)
47 (define-key mule-keymap "t" 'set-terminal-coding-system)
48 (define-key mule-keymap "k" 'set-keyboard-coding-system)
49 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
50 (define-key mule-keymap "x" 'set-selection-coding-system)
51 (define-key mule-keymap "X" 'set-next-selection-coding-system)
52 (define-key mule-keymap "\C-\\" 'set-input-method)
53 (define-key mule-keymap "c" 'universal-coding-system-argument)
54 (define-key mule-keymap "l" 'set-language-environment)
56 (defvar mule-menu-keymap
57 (make-sparse-keymap "Mule (Multilingual Environment)")
58 "Keymap for Mule (Multilingual environment) menu specific commands.")
60 (defvar describe-language-environment-map
61 (make-sparse-keymap "Describe Language Environment"))
63 (defvar setup-language-environment-map
64 (make-sparse-keymap "Set Language Environment"))
66 (defvar set-coding-system-map
67 (make-sparse-keymap "Set Coding System"))
69 (define-key-after mule-menu-keymap [set-language-environment]
70 (list 'menu-item "Set Language Environment" setup-language-environment-map
71 :help "Multilingual environment suitable for a specific language"))
72 (define-key-after mule-menu-keymap [separator-mule]
75 (define-key-after mule-menu-keymap [toggle-input-method]
76 '(menu-item "Toggle Input Method" toggle-input-method)
78 (define-key-after mule-menu-keymap [set-input-method]
79 '(menu-item "Select Input Method..." set-input-method)
81 (define-key-after mule-menu-keymap [describe-input-method]
82 '(menu-item "Describe Input Method" describe-input-method))
83 (define-key-after mule-menu-keymap [separator-input-method]
86 (define-key-after mule-menu-keymap [set-various-coding-system]
87 (list 'menu-item "Set Coding Systems" set-coding-system-map
88 :enable 'default-enable-multibyte-characters))
89 (define-key-after mule-menu-keymap [view-hello-file]
90 '(menu-item "Show Multi-lingual Text" view-hello-file
91 :enable (file-readable-p
92 (expand-file-name "HELLO" data-directory))
93 :help "Display file which says HELLO in many languages")
95 (define-key-after mule-menu-keymap [separator-coding-system]
98 (define-key-after mule-menu-keymap [describe-language-environment]
99 (list 'menu-item "Describe Language Environment"
100 describe-language-environment-map
101 :help "Show multilingual settings for a specific language")
103 (define-key-after mule-menu-keymap [describe-input-method]
104 '(menu-item "Describe Input Method..." describe-input-method
105 :help "Keyboard layout for a specific input method")
107 (define-key-after mule-menu-keymap [describe-coding-system]
108 '(menu-item "Describe Coding System..." describe-coding-system)
110 (define-key-after mule-menu-keymap [list-character-sets]
111 '(menu-item "List Character Sets" list-character-sets
112 :help "Show table of available character sets"))
113 (define-key-after mule-menu-keymap [mule-diag]
114 '(menu-item "Show All of Mule Status" mule-diag
115 :help "Display multilingual environment settings")
118 (define-key-after set-coding-system-map [universal-coding-system-argument]
119 '(menu-item "For Next Command" universal-coding-system-argument
120 :help "Coding system to be used by next command")
122 (define-key-after set-coding-system-map [separator-1]
125 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
126 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
127 :help "How to encode this buffer when saved")
129 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
130 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
131 :enable buffer-file-name
132 :help "Revisit this file immediately using specified coding system")
134 (define-key-after set-coding-system-map [set-file-name-coding-system]
135 '(menu-item "For File Name" set-file-name-coding-system
136 :help "How to decode/encode file names")
138 (define-key-after set-coding-system-map [separator-2]
142 (define-key-after set-coding-system-map [set-keyboard-coding-system]
143 '(menu-item "For Keyboard" set-keyboard-coding-system
144 :help "How to decode keyboard input")
146 (define-key-after set-coding-system-map [set-terminal-coding-system]
147 '(menu-item "For Terminal" set-terminal-coding-system
148 :enable (null (memq window-system '(x w32 mac)))
149 :help "How to encode terminal output")
151 (define-key-after set-coding-system-map [separator-3]
154 (define-key-after set-coding-system-map [set-selection-coding-system]
155 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
156 :visible (display-selections-p)
157 :help "How to en/decode data to/from selection/clipboard")
159 (define-key-after set-coding-system-map [set-next-selection-coding-system]
160 '(menu-item "For Next X Selection" set-next-selection-coding-system
161 :visible (display-selections-p)
162 :help "How to en/decode next selection/clipboard operation")
164 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
165 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
166 :visible (fboundp 'start-process)
167 :enable (get-buffer-process (current-buffer))
168 :help "How to en/decode I/O from/to subprocess connected to this buffer")
172 (define-key setup-language-environment-map
173 [Default] '(menu-item "Default" setup-specified-language-environment))
175 (define-key describe-language-environment-map
176 [Default] '(menu-item "Default" describe-specified-language-support))
178 ;; This should be a single character key binding because users use it
179 ;; very frequently while editing multilingual text. Now we can use
180 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
181 ;; convenient because it requires shifting on most keyboards. An
182 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
183 ;; but it won't be used that frequently.
184 (define-key global-map "\C-\\" 'toggle-input-method)
186 ;; This is no good because people often type Shift-SPC
187 ;; meaning to type SPC. -- rms.
188 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
189 ;; (define-key global-map [?\S- ] 'toggle-input-method)
191 ;;; Mule related hyperlinks.
192 (defconst help-xref-mule-regexp-template
193 (purecopy (concat "\\(\\<\\("
194 "\\(coding system\\)\\|"
195 "\\(input method\\)\\|"
196 "\\(character set\\)\\|"
199 ;; Note starting with word-syntax character:
200 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
202 (defun coding-system-change-eol-conversion (coding-system eol-type)
203 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
204 The returned coding system converts end-of-line by EOL-TYPE
205 but text as the same way as CODING-SYSTEM.
206 EOL-TYPE should be `unix', `dos', `mac', or nil.
207 If EOL-TYPE is nil, the returned coding system detects
208 how end-of-line is formatted automatically while decoding.
210 EOL-TYPE can be specified by an integer 0, 1, or 2.
211 They means `unix', `dos', and `mac' respectively."
212 (if (symbolp eol-type)
213 (setq eol-type (cond ((eq eol-type 'unix) 0)
214 ((eq eol-type 'dos) 1)
215 ((eq eol-type 'mac) 2)
217 (let ((orig-eol-type (coding-system-eol-type coding-system)))
218 (if (vectorp orig-eol-type)
221 (aref orig-eol-type eol-type))
222 (let ((base (coding-system-base coding-system)))
225 (if (= eol-type orig-eol-type)
227 (setq orig-eol-type (coding-system-eol-type base))
228 (if (vectorp orig-eol-type)
229 (aref orig-eol-type eol-type))))))))
231 (defun coding-system-change-text-conversion (coding-system coding)
232 "Return a coding system which differs from CODING-SYSTEM in text conversion.
233 The returned coding system converts text by CODING
234 but end-of-line as the same way as CODING-SYSTEM.
235 If CODING is nil, the returned coding system detects
236 how text is formatted automatically while decoding."
237 (let ((eol-type (coding-system-eol-type coding-system)))
238 (coding-system-change-eol-conversion
239 (if coding coding 'undecided)
240 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
242 (defun toggle-enable-multibyte-characters (&optional arg)
243 "Change whether this buffer uses multibyte characters.
244 With arg, use multibyte characters if the arg is positive.
246 Note that this command does not convert the byte contents of
247 the buffer; it only changes the way those bytes are interpreted.
248 In general, therefore, this command *changes* the sequence of
249 characters that the current buffer contains.
251 We suggest you avoid using use this command unless you know what you
252 are doing. If you use it by mistake, and the buffer is now displayed
253 wrong, use this command again to toggle back to the right mode."
256 (if (null arg) (null enable-multibyte-characters)
257 (> (prefix-numeric-value arg) 0))))
258 (set-buffer-multibyte new-flag))
259 (force-mode-line-update))
261 (defun view-hello-file ()
262 "Display the HELLO file which list up many languages and characters."
264 ;; We have to decode the file in any environment.
265 (let ((default-enable-multibyte-characters t)
266 (coding-system-for-read 'iso-2022-7bit))
267 (view-file (expand-file-name "HELLO" data-directory))))
269 (defun universal-coding-system-argument (coding-system)
270 "Execute an I/O command using the specified coding system."
272 (let ((default (and buffer-file-coding-system
273 (not (eq (coding-system-type buffer-file-coding-system)
275 buffer-file-coding-system)))
276 (list (read-coding-system
278 (format "Coding system for following command (default, %s): " default)
279 "Coding system for following command: ")
281 (let* ((keyseq (read-key-sequence
282 (format "Command to execute with %s:" coding-system)))
283 (cmd (key-binding keyseq))
286 (when (eq cmd 'universal-argument)
287 (call-interactively cmd)
289 ;; Process keys bound in `universal-argument-map'.
291 (setq keyseq (read-key-sequence nil t)
292 cmd (key-binding keyseq t))
293 (not (eq cmd 'universal-argument-other-key)))
294 (let ((current-prefix-arg prefix-arg)
295 ;; Have to bind `last-command-char' here so that
296 ;; `digit-argument', for instance, can compute the
298 (last-command-char (aref keyseq 0)))
299 (call-interactively cmd)))
301 ;; This is the final call to `universal-argument-other-key', which
302 ;; set's the final `prefix-arg.
303 (let ((current-prefix-arg prefix-arg))
304 (call-interactively cmd))
306 ;; Read the command to execute with the given prefix arg.
307 (setq prefix prefix-arg
308 keyseq (read-key-sequence nil t)
309 cmd (key-binding keyseq)))
311 (let ((coding-system-for-read coding-system)
312 (coding-system-for-write coding-system)
313 (coding-system-require-warning t)
314 (current-prefix-arg prefix))
316 (call-interactively cmd))))
318 (defun set-default-coding-systems (coding-system)
319 "Set default value of various coding systems to CODING-SYSTEM.
320 This sets the following coding systems:
321 o coding system of a newly created buffer
322 o default coding system for subprocess I/O
323 This also sets the following values:
324 o default value used as `file-name-coding-system' for converting file names.
325 o default value for the command `set-terminal-coding-system' (not on MSDOS)
326 o default value for the command `set-keyboard-coding-system'."
327 (check-coding-system coding-system)
328 (setq-default buffer-file-coding-system coding-system)
329 (if (fboundp 'ucs-set-table-for-input)
330 (dolist (buffer (buffer-list))
331 (or (local-variable-p 'buffer-file-coding-system buffer)
332 (ucs-set-table-for-input buffer))))
334 (if default-enable-multibyte-characters
335 (setq default-file-name-coding-system coding-system))
336 ;; If coding-system is nil, honor that on MS-DOS as well, so
337 ;; that they could reset the terminal coding system.
338 (unless (and (eq window-system 'pc) coding-system)
339 (setq default-terminal-coding-system coding-system))
340 (setq default-keyboard-coding-system coding-system)
341 ;; Preserve eol-type from existing default-process-coding-systems.
342 ;; On non-unix-like systems in particular, these may have been set
343 ;; carefully by the user, or by the startup code, to deal with the
344 ;; users shell appropriately, so should not be altered by changing
345 ;; language environment.
347 (coding-system-change-text-conversion
348 (car default-process-coding-system) coding-system))
350 (coding-system-change-text-conversion
351 (cdr default-process-coding-system) coding-system)))
352 (setq default-process-coding-system
353 (cons output-coding input-coding))))
355 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
356 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal "20.3")
358 (defun prefer-coding-system (coding-system)
359 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
360 This also sets the following coding systems:
361 o coding system of a newly created buffer
362 o default coding system for subprocess I/O
363 This also sets the following values:
364 o default value used as `file-name-coding-system' for converting file names.
365 o default value for the command `set-terminal-coding-system' (not on MSDOS)
366 o default value for the command `set-keyboard-coding-system'
368 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
369 systems set by this function will use that type of EOL conversion.
371 This command does not change the default value of terminal coding system
372 for MS-DOS terminal, because DOS terminals only support a single coding
373 system, and Emacs automatically sets the default to that coding system at
376 A coding system that requires automatic detection of text
377 +encoding (e.g. undecided, unix) can't be preferred.."
378 (interactive "zPrefer coding system: ")
379 (if (not (and coding-system (coding-system-p coding-system)))
380 (error "Invalid coding system `%s'" coding-system))
381 (if (memq (coding-system-type coding-system) '(raw-text undecided))
382 (error "Can't prefer the coding system `%s'" coding-system))
383 (let ((base (coding-system-base coding-system))
384 (eol-type (coding-system-eol-type coding-system)))
385 (set-coding-system-priority base)
387 (or (eq base coding-system)
388 (message "Highest priority is set to %s (base of %s)"
389 base coding-system)))
390 ;; If they asked for specific EOL conversion, honor that.
391 (if (memq eol-type '(0 1 2))
393 (coding-system-change-eol-conversion base eol-type)))
394 (set-default-coding-systems base)))
396 (defvar sort-coding-systems-predicate nil
397 "If non-nil, a predicate function to sort coding systems.
399 It is called with two coding systems, and should return t if the first
400 one is \"less\" than the second.
402 The function `sort-coding-systems' use it.")
404 (defun sort-coding-systems (codings)
405 "Sort coding system list CODINGS by a priority of each coding system.
407 If a coding system is most preferred, it has the highest priority.
408 Otherwise, a coding system corresponds to some MIME charset has higher
409 priorities. Among them, a coding system included in `coding-system'
410 key of the current language environment has higher priorities. See
411 also the documentation of `language-info-alist'.
413 If the variable `sort-coding-systems-predicate' (which see) is
414 non-nil, it is used to sort CODINGS in the different way than above."
415 (if sort-coding-systems-predicate
416 (sort codings sort-coding-systems-predicate)
417 (let* ((from-priority (coding-system-priority-list))
418 (most-preferred (car from-priority))
419 (lang-preferred (get-language-info current-language-environment
423 (let ((base (coding-system-base x)))
424 ;; We calculate the priority number 0..255 by
425 ;; using the 8 bits PMMLCEII as this:
426 ;; P: 1 iff most preferred.
427 ;; MM: greater than 0 iff mime-charset.
428 ;; L: 1 iff one of the current lang. env.'s codings.
429 ;; C: 1 iff one of codings listed in the category list.
430 ;; E: 1 iff not XXX-with-esc
431 ;; II: if iso-2022 based, 0..3, else 1.
433 (lsh (if (eq base most-preferred) 1 0) 7)
435 (let ((mime (coding-system-get base :mime-charset)))
436 ;; Prefer coding systems corresponding to a
439 ;; Lower utf-16 priority so that we
440 ;; normally prefer utf-8 to it, and put
441 ;; x-ctext below that.
442 (cond ((string-match "utf-16"
445 ((string-match "^x-" (symbol-name mime))
450 (lsh (if (memq base lang-preferred) 1 0) 4)
451 (lsh (if (memq base from-priority) 1 0) 3)
452 (lsh (if (string-match "-with-esc\\'"
455 (if (eq (coding-system-type base) 'iso-2022)
456 (let ((category (coding-system-category base)))
457 ;; For ISO based coding systems, prefer
458 ;; one that doesn't use designation nor
459 ;; locking/single shifting.
461 ((or (eq category 'coding-category-iso-8-1)
462 (eq category 'coding-category-iso-8-2))
464 ((or (eq category 'coding-category-iso-7-tight)
465 (eq category 'coding-category-iso-7))
471 (sort codings (function (lambda (x y)
472 (> (funcall func x) (funcall func y))))))))
474 (defun find-coding-systems-region (from to)
475 "Return a list of proper coding systems to encode a text between FROM and TO.
477 If FROM is a string, find coding systems in that instead of the buffer.
478 All coding systems in the list can safely encode any multibyte characters
481 If the text contains no multibyte characters, return a list of a single
482 element `undecided'."
483 (let ((codings (find-coding-systems-region-internal from to)))
485 ;; The text contains only ASCII characters. Any coding
488 ;; We need copy-sequence because sorting will alter the argument.
489 (sort-coding-systems (copy-sequence codings)))))
491 (defun find-coding-systems-string (string)
492 "Return a list of proper coding systems to encode STRING.
493 All coding systems in the list can safely encode any multibyte characters
496 If STRING contains no multibyte characters, return a list of a single
497 element `undecided'."
498 (find-coding-systems-region string nil))
500 (defun find-coding-systems-for-charsets (charsets)
501 "Return a list of proper coding systems to encode characters of CHARSETS.
502 CHARSETS is a list of character sets.
504 This only finds coding systems of type `charset', whose
505 `:charset-list' property includes all of CHARSETS (plus `ascii' for
506 ascii-compatible coding systems). It was used in older versions of
507 Emacs, but is unlikely to be what you really want now."
508 ;; Deal with aliases.
509 (setq charsets (mapcar (lambda (c)
510 (get-charset-property c :name))
512 (cond ((or (null charsets)
513 (and (= (length charsets) 1)
514 (eq 'ascii (car charsets))))
516 ((or (memq 'eight-bit-control charsets)
517 (memq 'eight-bit-graphic charsets))
518 '(raw-text utf-8-emacs))
521 (dolist (cs (coding-system-list t))
522 (let ((cs-charsets (and (eq (coding-system-type cs) 'charset)
523 (coding-system-charset-list cs)))
525 (if (coding-system-get cs :ascii-compatible-p)
526 (add-to-list 'cs-charsets 'ascii))
530 (unless (memq (pop charsets) cs-charsets)
534 (nreverse codings)))))
536 (defun find-multibyte-characters (from to &optional maxcount excludes)
537 "Find multibyte characters in the region specified by FROM and TO.
538 If FROM is a string, find multibyte characters in the string.
539 The return value is an alist of the following format:
540 ((CHARSET COUNT CHAR ...) ...)
542 CHARSET is a character set,
543 COUNT is a number of characters,
544 CHARs are the characters found from the character set.
545 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
546 Optional 4th arg EXCLUDE is a list of character sets to be ignored."
550 (if (multibyte-string-p from)
552 (while (setq idx (string-match "[^\000-\177]" from idx))
553 (setq char (aref from idx)
554 charset (char-charset char))
555 (unless (memq charset excludes)
556 (let ((slot (assq charset chars)))
558 (if (not (memq char (nthcdr 2 slot)))
559 (let ((count (nth 1 slot)))
560 (setcar (cdr slot) (1+ count))
561 (if (or (not maxcount) (< count maxcount))
562 (nconc slot (list char)))))
563 (setq chars (cons (list charset 1 char) chars)))))
564 (setq idx (1+ idx)))))
565 (if enable-multibyte-characters
568 (while (re-search-forward "[^\000-\177]" to t)
569 (setq char (preceding-char)
570 charset (char-charset char))
571 (unless (memq charset excludes)
572 (let ((slot (assq charset chars)))
574 (if (not (member char (nthcdr 2 slot)))
575 (let ((count (nth 1 slot)))
576 (setcar (cdr slot) (1+ count))
577 (if (or (not maxcount) (< count maxcount))
578 (nconc slot (list char)))))
579 (setq chars (cons (list charset 1 char) chars)))))))))
582 (defun search-unencodable-char (coding-system)
583 "Search forward from point for a character that is not encodable.
584 It asks which coding system to check.
585 If such a character is found, set point after that character.
586 Otherwise, don't move point.
588 When called from a program, the value is a position of the found character,
589 or nil if all characters are encodable."
591 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
593 (format "Coding-system (default, %s): " default)
595 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
598 (message "All following characters are encodable by %s" coding-system))
601 (defvar last-coding-system-specified nil
602 "Most recent coding system explicitly specified by the user when asked.
603 This variable is set whenever Emacs asks the user which coding system
604 to use in order to write a file. If you set it to nil explicitly,
605 then call `write-region', then afterward this variable will be non-nil
606 only if the user was explicitly asked and specified a coding system.")
608 (defvar select-safe-coding-system-accept-default-p nil
609 "If non-nil, a function to control the behaviour of coding system selection.
610 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
611 function `select-safe-coding-system' (which see). This variable
612 overrides that argument.")
614 (defun select-safe-coding-system (from to &optional default-coding-system
615 accept-default-p file)
616 "Ask a user to select a safe coding system from candidates.
617 The candidates of coding systems which can safely encode a text
618 between FROM and TO are shown in a popup window. Among them, the most
619 proper one is suggested as the default.
621 The list of `buffer-file-coding-system' of the current buffer,
622 the `default-buffer-file-coding-system', and the
623 most preferred coding system (if it corresponds to a MIME charset) is
624 treated as the default coding system list. Among them, the first one
625 that safely encodes the text is normally selected silently and
626 returned without any user interaction. See also the command
627 `prefer-coding-system'.
629 However, the user is queried if the chosen coding system is
630 inconsistent with what would be selected by `set-auto-coding' from
631 coding cookies &c. if the contents of the region were read from a
632 file. (That could lead to data corruption in a file subsequently
633 re-visited and edited.)
635 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
636 list of coding systems to be prepended to the default coding system
637 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
638 element is t, the cdr part is used as the defualt coding system list,
639 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
640 and the most preferred coding system are not used.
642 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
643 determine the acceptability of the silently selected coding system.
644 It is called with that coding system, and should return nil if it
645 should not be silently selected and thus user interaction is required.
647 Optional 5th arg FILE is the file name to use for this purpose.
648 That is different from `buffer-file-name' when handling `write-region'
651 The variable `select-safe-coding-system-accept-default-p', if
652 non-nil, overrides ACCEPT-DEFAULT-P.
654 Kludgy feature: if FROM is a string, the string is the target text,
656 (if (and default-coding-system
657 (not (listp default-coding-system)))
658 (setq default-coding-system (list default-coding-system)))
660 (let ((no-other-defaults nil))
661 (if (eq (car default-coding-system) t)
662 (setq no-other-defaults t
663 default-coding-system (cdr default-coding-system)))
665 ;; Change elements of the list to (coding . base-coding).
666 (setq default-coding-system
667 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
668 default-coding-system))
670 ;; From now on, the list of defaults is reversed.
671 (setq default-coding-system (nreverse default-coding-system))
673 (unless no-other-defaults
674 ;; If buffer-file-coding-system is not nil nor undecided, append it
676 (if buffer-file-coding-system
677 (let ((base (coding-system-base buffer-file-coding-system)))
678 (or (eq base 'undecided)
679 (rassq base default-coding-system)
680 (push (cons buffer-file-coding-system base)
681 default-coding-system))))
683 ;; If default-buffer-file-coding-system is not nil nor undecided,
684 ;; append it to the defaults.
685 (if default-buffer-file-coding-system
686 (let ((base (coding-system-base default-buffer-file-coding-system)))
687 (or (eq base 'undecided)
688 (rassq base default-coding-system)
689 (push (cons default-buffer-file-coding-system base)
690 default-coding-system))))
692 ;; If the most preferred coding system has the property mime-charset,
693 ;; append it to the defaults.
694 (let ((preferred (coding-system-priority-list t))
696 (and (coding-system-p preferred)
697 (setq base (coding-system-base preferred))
698 (coding-system-get preferred :mime-charset)
699 (not (rassq base default-coding-system))
700 (push (cons preferred base)
701 default-coding-system)))))
703 (if select-safe-coding-system-accept-default-p
704 (setq accept-default-p select-safe-coding-system-accept-default-p))
706 (let ((codings (find-coding-systems-region from to))
708 (bufname (buffer-name))
709 safe rejected unsafe)
710 ;; Classify the defaults into safe, rejected, and unsafe.
711 (dolist (elt default-coding-system)
712 (if (or (eq (car codings) 'undecided)
713 (memq (cdr elt) codings))
714 (if (and (functionp accept-default-p)
715 (not (funcall accept-default-p (cdr elt))))
716 (push (car elt) rejected)
717 (push (car elt) safe))
718 (push (car elt) unsafe)))
720 (setq coding-system (car safe)))
722 ;; If all the defaults failed, ask a user.
723 (unless coding-system
724 ;; At first, if some defaults are unsafe, record at most 11
725 ;; problematic characters and their positions for them by turning
728 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
732 (mapcar #'(lambda (coding)
734 (mapcar #'(lambda (pos)
735 (cons pos (aref from pos)))
736 (unencodable-char-position
737 0 (length from) coding
741 (mapcar #'(lambda (coding)
743 (mapcar #'(lambda (pos)
744 (cons pos (char-after pos)))
745 (unencodable-char-position
746 from to coding 11))))
749 ;; Change each safe coding system to the corresponding
750 ;; mime-charset name if it is also a coding system. Such a name
751 ;; is more friendly to users.
755 (setq mime-charset (coding-system-get (car l) :mime-charset))
756 (if (and mime-charset (coding-system-p mime-charset)
757 (coding-system-equal (car l) mime-charset))
758 (setcar l mime-charset))
761 ;; Don't offer variations with locking shift, which you
762 ;; basically never want.
764 (dolist (elt codings (setq codings (nreverse l)))
765 (unless (or (eq 'coding-category-iso-7-else
766 (coding-system-category elt))
767 (eq 'coding-category-iso-8-else
768 (coding-system-category elt)))
771 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
772 ;; else is available.
776 (delq 'no-conversion codings)))
777 '(raw-text emacs-mule no-conversion)))
779 (let ((window-configuration (current-window-configuration)))
781 ;; If some defaults are unsafe, make sure the offending
782 ;; buffer is displayed.
783 (when (and unsafe (not (stringp from)))
784 (pop-to-buffer bufname)
785 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
787 ;; Then ask users to select one from CODINGS while showing
788 ;; the reason why none of the defaults are not used.
789 (with-output-to-temp-buffer "*Warning*"
791 (set-buffer standard-output)
792 (if (not default-coding-system)
793 (insert "No default coding systems to try for "
795 (format "string \"%s\"." from)
796 (format "buffer `%s'." bufname)))
798 "These default coding systems were tried to encode"
800 (concat " \"" (if (> (length from) 10)
801 (concat (substring from 0 10) "...\"")
803 (format " text\nin the buffer `%s'" bufname))
807 (mapc #'(lambda (x) (princ " ") (princ (car x)))
808 default-coding-system)
810 (fill-region-as-paragraph pos (point)))
812 (insert "These safely encodes the target text,
813 but it is not recommended for encoding text in this context,
814 e.g., for sending an email message.\n ")
815 (mapc #'(lambda (x) (princ " ") (princ x)) rejected)
818 (insert (if rejected "And the others"
819 "However, each of them")
820 " encountered these problematic characters:\n")
823 (insert (format " %s:" (car coding)))
826 #'(lambda (bufname pos)
827 (when (buffer-live-p (get-buffer bufname))
828 (pop-to-buffer bufname)
831 #'(lambda (bufname pos coding)
832 (when (buffer-live-p (get-buffer bufname))
833 (pop-to-buffer bufname)
837 (search-unencodable-char coding)
838 (forward-char -1))))))
839 (dolist (elt (cdr coding))
842 (insert (if (< i 10) (cdr elt) "..."))
848 "mouse-2, RET: jump to this character"
850 'help-args (list bufname (car elt)))
855 "mouse-2, RET: next unencodable character"
857 'help-args (list bufname (car elt)
863 The first problematic character is at point in the displayed buffer,\n"
864 (substitute-command-keys "\
865 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
867 "\nSelect the above, or "
870 one of the following safe coding systems, or edit the buffer:\n")
873 (mapcar (function (lambda (x) (princ " ") (princ x)))
876 (fill-region-as-paragraph pos (point)))
877 (insert "Or specify any other coding system
878 at the risk of losing the problematic characters.\n")))
880 ;; Read a coding system.
881 (setq default-coding-system (or (car safe) (car codings)))
884 (format "Select coding system (default %s): "
885 default-coding-system)
886 default-coding-system))
887 (setq last-coding-system-specified coding-system))
889 (kill-buffer "*Warning*")
890 (set-window-configuration window-configuration)))
892 (if (and coding-system (vectorp (coding-system-eol-type coding-system)))
893 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
896 (coding-system-change-eol-conversion coding-system eol)))))
898 ;; Check we're not inconsistent with what `coding:' spec &c would
899 ;; give when file is re-read.
900 ;; But don't do this if we explicitly ignored the cookie
901 ;; by using `find-file-literally'.
902 (unless (or (stringp from) find-file-literally)
903 (let ((auto-cs (save-excursion
906 (narrow-to-region from to)
907 (goto-char (point-min))
908 (set-auto-coding (or file buffer-file-name "")
910 ;; Merge coding-system and auto-cs as far as possible.
911 (if (not coding-system)
912 (setq coding-system auto-cs)
914 (setq auto-cs coding-system)
915 (let ((eol-type-1 (coding-system-eol-type coding-system))
916 (eol-type-2 (coding-system-eol-type auto-cs)))
917 (if (eq (coding-system-base coding-system) 'undecided)
918 (setq coding-system (coding-system-change-text-conversion
919 coding-system auto-cs))
920 (if (eq (coding-system-base auto-cs) 'undecided)
921 (setq auto-cs (coding-system-change-text-conversion
922 auto-cs coding-system))))
923 (if (vectorp eol-type-1)
924 (or (vectorp eol-type-2)
925 (setq coding-system (coding-system-change-eol-conversion
926 coding-system eol-type-2)))
927 (if (vectorp eol-type-2)
928 (setq auto-cs (coding-system-change-eol-conversion
929 auto-cs eol-type-1)))))))
932 ;; Don't barf if writing a compressed file, say.
933 ;; This check perhaps isn't ideal, but is probably
934 ;; the best thing to do.
935 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
936 (not (coding-system-equal coding-system auto-cs)))
938 (format "Selected encoding %s disagrees with \
939 %s specified by file contents. Really save (else edit coding cookies \
940 and try again)? " coding-system auto-cs))
941 (error "Save aborted")))))
944 (setq select-safe-coding-system-function 'select-safe-coding-system)
946 (defun select-message-coding-system ()
947 "Return a coding system to encode the outgoing message of the current buffer.
948 It at first tries the first coding system found in these variables
950 (1) local value of `buffer-file-coding-system'
951 (2) value of `sendmail-coding-system'
952 (3) value of `default-sendmail-coding-system'
953 (4) value of `default-buffer-file-coding-system'
954 If the found coding system can't encode the current buffer,
955 or none of them are bound to a coding system,
956 it asks the user to select a proper coding system."
957 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
958 buffer-file-coding-system)
959 sendmail-coding-system
960 default-sendmail-coding-system
961 default-buffer-file-coding-system)))
962 (if (eq coding 'no-conversion)
963 ;; We should never use no-conversion for outgoing mail.
965 (if (fboundp select-safe-coding-system-function)
966 (funcall select-safe-coding-system-function
967 (point-min) (point-max) coding
968 (function (lambda (x) (coding-system-get x :mime-charset))))
971 ;;; Language support stuff.
973 (defvar language-info-alist nil
974 "Alist of language environment definitions.
975 Each element looks like:
976 (LANGUAGE-NAME . ((KEY . INFO) ...))
977 where LANGUAGE-NAME is a string, the name of the language environment,
978 KEY is a symbol denoting the kind of information, and
979 INFO is the data associated with KEY.
980 Meaningful values for KEY include
982 documentation value is documentation of what this language environment
983 is meant for, and how to use it.
984 charset value is a list of the character sets mainly used
985 by this language environment.
986 sample-text value is an expression which is evalled to generate
987 a line of text written using characters appropriate
988 for this language environment.
989 setup-function value is a function to call to switch to this
990 language environment.
991 exit-function value is a function to call to leave this
992 language environment.
993 coding-system value is a list of coding systems that are good
994 for saving text written in this language environment.
995 This list serves as suggestions to the user;
996 in effect, as a kind of documentation.
997 coding-priority value is a list of coding systems for this language
998 environment, in order of decreasing priority.
999 This is used to set up the coding system priority
1000 list when you switch to this language environment.
1001 nonascii-translation
1002 value is a charset of dimension one to use for
1003 converting a unibyte character to multibyte
1005 input-method value is a default input method for this language
1007 features value is a list of features requested in this
1008 language environment.
1009 ctext-non-standard-encodings
1010 value is a list of non-standard encoding
1011 names used in extended segments of CTEXT.
1013 `ctext-non-standard-encodings' for more
1016 The following keys take effect only when multibyte characters are
1017 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1020 unibyte-display value is a coding system to encode characters
1021 for the terminal. Characters in the range
1022 of 160 to 255 display not as octal escapes,
1023 but as non-ASCII characters in this language
1026 (defun get-language-info (lang-env key)
1027 "Return information listed under KEY for language environment LANG-ENV.
1028 KEY is a symbol denoting the kind of information.
1029 For a list of useful values for KEY and their meanings,
1030 see `language-info-alist'."
1031 (if (symbolp lang-env)
1032 (setq lang-env (symbol-name lang-env)))
1033 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1035 (cdr (assq key (cdr lang-slot))))))
1037 (defun set-language-info (lang-env key info)
1038 "Modify part of the definition of language environment LANG-ENV.
1039 Specifically, this stores the information INFO under KEY
1040 in the definition of this language environment.
1041 KEY is a symbol denoting the kind of information.
1042 INFO is the value for that information.
1044 For a list of useful values for KEY and their meanings,
1045 see `language-info-alist'."
1046 (if (symbolp lang-env)
1047 (setq lang-env (symbol-name lang-env)))
1048 (let (lang-slot key-slot)
1049 (setq lang-slot (assoc lang-env language-info-alist))
1050 (if (null lang-slot) ; If no slot for the language, add it.
1051 (setq lang-slot (list lang-env)
1052 language-info-alist (cons lang-slot language-info-alist)))
1053 (setq key-slot (assq key lang-slot))
1054 (if (null key-slot) ; If no slot for the key, add it.
1056 (setq key-slot (list key))
1057 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1058 (setcdr key-slot (purecopy info))
1059 ;; Update the custom-type of `current-language-environment'.
1060 (put 'current-language-environment 'custom-type
1061 (cons 'choice (mapcar
1064 (sort (mapcar 'car language-info-alist) 'string<))))))
1066 (defun set-language-info-alist (lang-env alist &optional parents)
1067 "Store ALIST as the definition of language environment LANG-ENV.
1068 ALIST is an alist of KEY and INFO values. See the documentation of
1069 `language-info-alist' for the meanings of KEY and INFO.
1071 Optional arg PARENTS is a list of parent menu names; it specifies
1072 where to put this language environment in the
1073 Describe Language Environment and Set Language Environment menus.
1074 For example, (\"European\") means to put this language environment
1075 in the European submenu in each of those two menus."
1076 (if (symbolp lang-env)
1077 (setq lang-env (symbol-name lang-env)))
1078 (let ((describe-map describe-language-environment-map)
1079 (setup-map setup-language-environment-map))
1082 map parent-symbol parent prompt)
1084 (if (symbolp (setq parent-symbol (car l)))
1085 (setq parent (symbol-name parent))
1086 (setq parent parent-symbol parent-symbol (intern parent)))
1087 (setq map (lookup-key describe-map (vector parent-symbol)))
1088 ;; This prompt string is for define-prefix-command, so
1089 ;; that the map it creates will be suitable for a menu.
1090 (or map (setq prompt (format "%s Environment" parent)))
1093 (setq map (intern (format "describe-%s-environment-map"
1094 (downcase parent))))
1095 (define-prefix-command map nil prompt)
1096 (define-key-after describe-map (vector parent-symbol)
1097 (cons parent map) t)))
1098 (setq describe-map (symbol-value map))
1099 (setq map (lookup-key setup-map (vector parent-symbol)))
1102 (setq map (intern (format "setup-%s-environment-map"
1103 (downcase parent))))
1104 (define-prefix-command map nil prompt)
1105 (define-key-after setup-map (vector parent-symbol)
1106 (cons parent map) t)))
1107 (setq setup-map (symbol-value map))
1110 ;; Set up menu items for this language env.
1111 (let ((doc (assq 'documentation alist)))
1113 (define-key-after describe-map (vector (intern lang-env))
1114 (cons lang-env 'describe-specified-language-support) t)))
1115 (define-key-after setup-map (vector (intern lang-env))
1116 (cons lang-env 'setup-specified-language-environment) t)
1119 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
1120 (setq alist (cdr alist)))))
1122 (defun read-language-name (key prompt &optional default)
1123 "Read a language environment name which has information for KEY.
1124 If KEY is nil, read any language environment.
1125 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1126 This returns a language environment name as a string."
1127 (let* ((completion-ignore-case t)
1128 (name (completing-read prompt
1131 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1132 t nil nil default)))
1133 (if (and (> (length name) 0)
1135 (get-language-info name key)))
1138 ;;; Multilingual input methods.
1140 "LEIM: Libraries of Emacs Input Methods."
1143 (defconst leim-list-file-name "leim-list.el"
1144 "Name of LEIM list file.
1145 This file contains a list of libraries of Emacs input methods (LEIM)
1146 in the format of Lisp expression for registering each input method.
1147 Emacs loads this file at startup time.")
1149 (defvar leim-list-header (format
1150 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1152 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1153 ;; methods in the same directory as this file. Loading this file
1154 ;; registers all the input methods in Emacs.
1156 ;; Each entry has the form:
1157 ;; (register-input-method
1158 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1159 ;; TITLE DESCRIPTION
1161 ;; See the function `register-input-method' for the meanings of the arguments.
1163 ;; If this directory is included in load-path, Emacs automatically
1164 ;; loads this file at startup time.
1167 leim-list-file-name)
1168 "Header to be inserted in LEIM list file.")
1170 (defvar leim-list-entry-regexp "^(register-input-method"
1171 "Regexp matching head of each entry in LEIM list file.
1172 See also the variable `leim-list-header'")
1174 (defvar update-leim-list-functions
1175 '(quail-update-leim-list-file)
1176 "List of functions to call to update LEIM list file.
1177 Each function is called with one arg, LEIM directory name.")
1179 (defun update-leim-list-file (&rest dirs)
1180 "Update LEIM list file in directories DIRS."
1181 (let ((functions update-leim-list-functions))
1183 (apply (car functions) dirs)
1184 (setq functions (cdr functions)))))
1186 (defvar current-input-method nil
1187 "The current input method for multilingual text.
1188 If nil, that means no input method is activated now.")
1189 (make-variable-buffer-local 'current-input-method)
1190 (put 'current-input-method 'permanent-local t)
1192 (defvar current-input-method-title nil
1193 "Title string of the current input method shown in mode line.")
1194 (make-variable-buffer-local 'current-input-method-title)
1195 (put 'current-input-method-title 'permanent-local t)
1197 (defcustom default-input-method nil
1198 "*Default input method for multilingual text (a string).
1199 This is the input method activated automatically by the command
1200 `toggle-input-method' (\\[toggle-input-method])."
1201 :link '(custom-manual "(emacs)Input Methods")
1203 :type '(choice (const nil) (string
1204 :completion-ignore-case t
1205 :complete-function widget-string-complete
1206 :completion-alist input-method-alist
1207 :prompt-history input-method-history))
1208 :set-after '(current-language-environment))
1210 (put 'input-method-function 'permanent-local t)
1212 (defvar input-method-history nil
1213 "History list for some commands that read input methods.")
1214 (make-variable-buffer-local 'input-method-history)
1215 (put 'input-method-history 'permanent-local t)
1217 (defvar inactivate-current-input-method-function nil
1218 "Function to call for inactivating the current input method.
1219 Every input method should set this to an appropriate value when activated.
1220 This function is called with no argument.
1222 This function should never change the value of `current-input-method'.
1223 It is set to nil by the function `inactivate-input-method'.")
1224 (make-variable-buffer-local 'inactivate-current-input-method-function)
1225 (put 'inactivate-current-input-method-function 'permanent-local t)
1227 (defvar describe-current-input-method-function nil
1228 "Function to call for describing the current input method.
1229 This function is called with no argument.")
1230 (make-variable-buffer-local 'describe-current-input-method-function)
1231 (put 'describe-current-input-method-function 'permanent-local t)
1233 (defvar input-method-alist nil
1234 "Alist of input method names vs how to use them.
1235 Each element has the form:
1236 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1237 See the function `register-input-method' for the meanings of the elements.")
1239 (defun register-input-method (input-method lang-env &rest args)
1240 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1241 INPUT-METHOD and LANG-ENV are symbols or strings.
1243 The remaining arguments are:
1244 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1245 ACTIVATE-FUNC is a function to call to activate this method.
1246 TITLE is a string to show in the mode line when this method is active.
1247 DESCRIPTION is a string describing this method and what it is good for.
1248 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1249 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1251 This function is mainly used in the file \"leim-list.el\" which is
1252 created at Emacs build time, registering all Quail input methods
1253 contained in the Emacs distribution.
1255 In case you want to register a new Quail input method by yourself, be
1256 careful to use the same input method title as given in the third
1257 parameter of `quail-define-package'. (If the values are different, the
1258 string specified in this function takes precedence.)
1260 The commands `describe-input-method' and `list-input-methods' need
1261 these duplicated values to show some information about input methods
1262 without loading the relevant Quail packages."
1263 (if (symbolp lang-env)
1264 (setq lang-env (symbol-name lang-env)))
1265 (if (symbolp input-method)
1266 (setq input-method (symbol-name input-method)))
1267 (let ((info (cons lang-env args))
1268 (slot (assoc input-method input-method-alist)))
1271 (setq slot (cons input-method info))
1272 (setq input-method-alist (cons slot input-method-alist)))))
1274 (defun read-input-method-name (prompt &optional default inhibit-null)
1275 "Read a name of input method from a minibuffer prompting with PROMPT.
1276 If DEFAULT is non-nil, use that as the default,
1277 and substitute it into PROMPT at the first `%s'.
1278 If INHIBIT-NULL is non-nil, null input signals an error.
1280 The return value is a string."
1282 (setq prompt (format prompt default)))
1283 (let* ((completion-ignore-case t)
1284 ;; As it is quite normal to change input method in the
1285 ;; minibuffer, we must enable it even if
1286 ;; enable-recursive-minibuffers is currently nil.
1287 (enable-recursive-minibuffers t)
1288 ;; This binding is necessary because input-method-history is
1290 (input-method (completing-read prompt input-method-alist
1291 nil t nil 'input-method-history
1293 (if (and input-method (symbolp input-method))
1294 (setq input-method (symbol-name input-method)))
1295 (if (> (length input-method) 0)
1298 (error "No valid input method is specified")))))
1300 (defun activate-input-method (input-method)
1301 "Switch to input method INPUT-METHOD for the current buffer.
1302 If some other input method is already active, turn it off first.
1303 If INPUT-METHOD is nil, deactivate any current input method."
1304 (if (and input-method (symbolp input-method))
1305 (setq input-method (symbol-name input-method)))
1306 (if (and current-input-method
1307 (not (string= current-input-method input-method)))
1308 (inactivate-input-method))
1309 (unless (or current-input-method (null input-method))
1310 (let ((slot (assoc input-method input-method-alist)))
1312 (error "Can't activate input method `%s'" input-method))
1313 (setq current-input-method-title nil)
1314 (let ((func (nth 2 slot)))
1315 (if (functionp func)
1316 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1317 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1319 (require (cdr func))
1320 (apply (car func) input-method (nthcdr 5 slot)))
1321 (error "Can't activate input method `%s'" input-method))))
1322 (setq current-input-method input-method)
1323 (or (stringp current-input-method-title)
1324 (setq current-input-method-title (nth 3 slot)))
1326 (run-hooks 'input-method-activate-hook)
1327 (force-mode-line-update)))))
1329 (defun inactivate-input-method ()
1330 "Turn off the current input method."
1331 (when current-input-method
1332 (if input-method-history
1333 (unless (string= current-input-method (car input-method-history))
1334 (setq input-method-history
1335 (cons current-input-method
1336 (delete current-input-method input-method-history))))
1337 (setq input-method-history (list current-input-method)))
1339 (funcall inactivate-current-input-method-function)
1341 (run-hooks 'input-method-inactivate-hook)
1342 (setq current-input-method nil
1343 input-method-function nil
1344 current-input-method-title nil)
1345 (force-mode-line-update)))))
1347 (defun set-input-method (input-method)
1348 "Select and activate input method INPUT-METHOD for the current buffer.
1349 This also sets the default input method to the one you specify.
1350 If INPUT-METHOD is nil, this function turns off the input method, and
1351 also causes you to be prompted for a name of an input method the next
1352 time you invoke \\[toggle-input-method].
1354 To deactivate the input method interactively, use \\[toggle-input-method].
1355 To deactivate it programmatically, use \\[inactivate-input-method]."
1357 (let* ((default (or (car input-method-history) default-input-method)))
1358 (list (read-input-method-name
1359 (if default "Select input method (default %s): " "Select input method: ")
1361 (activate-input-method input-method)
1362 (setq default-input-method input-method)
1363 (when (interactive-p)
1364 (customize-mark-as-set 'default-input-method))
1365 default-input-method)
1367 (defun toggle-input-method (&optional arg)
1368 "Enable or disable multilingual text input method for the current buffer.
1369 Only one input method can be enabled at any time in a given buffer.
1371 The normal action is to enable an input method if none was
1372 enabled, and disable the current one otherwise. Which input method
1373 to enable can be determined in various ways--either the one most
1374 recently used, or the one specified by `default-input-method', or
1375 as a last resort by reading the name of an input method in the
1378 With a prefix argument, read an input method name with the minibuffer
1379 and enable that one. The default is the most recent input method specified
1380 \(not including the currently active input method, if any)."
1383 (if (and current-input-method (not arg))
1384 (inactivate-input-method)
1385 (let ((default (or (car input-method-history) default-input-method)))
1386 (if (and arg default (equal current-input-method default)
1387 (> (length input-method-history) 1))
1388 (setq default (nth 1 input-method-history)))
1389 (activate-input-method
1390 (if (or arg (not default))
1392 (read-input-method-name
1393 (if default "Input method (default %s): " "Input method: " )
1396 (unless default-input-method
1398 (setq default-input-method current-input-method)
1399 (when (interactive-p)
1400 (customize-mark-as-set 'default-input-method)))))))
1402 (eval-when-compile (autoload 'help-buffer "help-mode"))
1404 (defun describe-input-method (input-method)
1405 "Describe input method INPUT-METHOD."
1407 (list (read-input-method-name
1408 "Describe input method (default, current choice): ")))
1409 (if (and input-method (symbolp input-method))
1410 (setq input-method (symbol-name input-method)))
1411 (help-setup-xref (list #'describe-input-method
1412 (or input-method current-input-method))
1415 (if (null input-method)
1416 (describe-current-input-method)
1417 (let ((current current-input-method))
1421 (activate-input-method input-method)
1422 (describe-current-input-method))
1423 (activate-input-method current))
1425 (activate-input-method current)
1426 (help-setup-xref (list #'describe-input-method input-method)
1428 (with-output-to-temp-buffer (help-buffer)
1429 (let ((elt (assoc input-method input-method-alist)))
1431 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1432 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1434 (defun describe-current-input-method ()
1435 "Describe the input method currently in use.
1436 This is a subroutine for `describe-input-method'."
1437 (if current-input-method
1438 (if (and (symbolp describe-current-input-method-function)
1439 (fboundp describe-current-input-method-function))
1440 (funcall describe-current-input-method-function)
1441 (message "No way to describe the current input method `%s'"
1442 current-input-method)
1444 (error "No input method is activated now")))
1446 (defun read-multilingual-string (prompt &optional initial-input input-method)
1447 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1448 The input method selected last time is activated in minibuffer.
1449 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1451 Optional 3rd argument INPUT-METHOD specifies the input method
1452 to be activated instead of the one selected last time. It is a symbol
1456 current-input-method
1457 default-input-method
1458 (read-input-method-name "Input method: " nil t)))
1459 (if (and input-method (symbolp input-method))
1460 (setq input-method (symbol-name input-method)))
1461 (let ((prev-input-method current-input-method))
1464 (activate-input-method input-method)
1465 (read-string prompt initial-input nil nil t))
1466 (activate-input-method prev-input-method))))
1468 ;; Variables to control behavior of input methods. All input methods
1469 ;; should react to these variables.
1471 (defcustom input-method-verbose-flag 'default
1472 "*A flag to control extra guidance given by input methods.
1473 The value should be nil, t, `complex-only', or `default'.
1475 The extra guidance is done by showing list of available keys in echo
1476 area. When you use the input method in the minibuffer, the guidance
1477 is shown at the bottom short window (split from the existing window).
1479 If the value is t, extra guidance is always given, if the value is
1480 nil, extra guidance is always suppressed.
1482 If the value is `complex-only', only complex input methods such as
1483 `chinese-py' and `japanese' give extra guidance.
1485 If the value is `default', complex input methods always give extra
1486 guidance, but simple input methods give it only when you are not in
1489 See also the variable `input-method-highlight-flag'."
1490 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1491 (const complex-only) (const default))
1494 (defcustom input-method-highlight-flag t
1495 "*If this flag is non-nil, input methods highlight partially-entered text.
1496 For instance, while you are in the middle of a Quail input method sequence,
1497 the text inserted so far is temporarily underlined.
1498 The underlining goes away when you finish or abort the input method sequence.
1499 See also the variable `input-method-verbose-flag'."
1503 (defcustom input-method-activate-hook nil
1504 "Normal hook run just after an input method is activated.
1506 The variable `current-input-method' keeps the input method name
1511 (defcustom input-method-inactivate-hook nil
1512 "Normal hook run just after an input method is inactivated.
1514 The variable `current-input-method' still keeps the input method name
1519 (defcustom input-method-after-insert-chunk-hook nil
1520 "Normal hook run just after an input method insert some chunk of text."
1524 (defvar input-method-exit-on-first-char nil
1525 "This flag controls when an input method returns.
1526 Usually, the input method does not return while there's a possibility
1527 that it may find a different translation if a user types another key.
1528 But, it this flag is non-nil, the input method returns as soon as
1529 the current key sequence gets long enough to have some valid translation.")
1531 (defcustom input-method-use-echo-area nil
1532 "This flag controls how an input method shows an intermediate key sequence.
1533 Usually, the input method inserts the intermediate key sequence,
1534 or candidate translations corresponding to the sequence,
1535 at point in the current buffer.
1536 But, if this flag is non-nil, it displays them in echo area instead."
1540 (defvar input-method-exit-on-invalid-key nil
1541 "This flag controls the behaviour of an input method on invalid key input.
1542 Usually, when a user types a key which doesn't start any character
1543 handled by the input method, the key is handled by turning off the
1544 input method temporarily. After that key, the input method is re-enabled.
1545 But, if this flag is non-nil, the input method is never back on.")
1548 (defcustom set-language-environment-hook nil
1549 "Normal hook run after some language environment is set.
1551 When you set some hook function here, that effect usually should not
1552 be inherited to another language environment. So, you had better set
1553 another function in `exit-language-environment-hook' (which see) to
1558 (defcustom exit-language-environment-hook nil
1559 "Normal hook run after exiting from some language environment.
1560 When this hook is run, the variable `current-language-environment'
1561 is still bound to the language environment being exited.
1563 This hook is mainly used for canceling the effect of
1564 `set-language-environment-hook' (which-see)."
1568 (put 'setup-specified-language-environment 'apropos-inhibit t)
1570 (defun setup-specified-language-environment ()
1571 "Switch to a specified language environment."
1573 (let (language-name)
1574 (if (and (symbolp last-command-event)
1575 (or (not (eq last-command-event 'Default))
1576 (setq last-command-event 'English))
1577 (setq language-name (symbol-name last-command-event)))
1579 (set-language-environment language-name)
1580 (customize-mark-as-set 'current-language-environment))
1581 (error "Bogus calling sequence"))))
1583 (defcustom current-language-environment "English"
1584 "The last language environment specified with `set-language-environment'.
1585 This variable should be set only with \\[customize], which is equivalent
1586 to using the function `set-language-environment'."
1587 :link '(custom-manual "(emacs)Language Environments")
1588 :set (lambda (symbol value) (set-language-environment value))
1590 (or (car-safe (assoc-string
1591 (if (symbolp current-language-environment)
1592 (symbol-name current-language-environment)
1593 current-language-environment)
1594 language-info-alist t))
1596 ;; custom type will be updated with `set-language-info'.
1597 :type (if language-info-alist
1598 (cons 'choice (mapcar
1601 (sort (mapcar 'car language-info-alist) 'string<)))
1603 :initialize 'custom-initialize-default
1606 (defun reset-language-environment ()
1607 "Reset multilingual environment of Emacs to the default status.
1609 The default status is as follows:
1611 The default value of `buffer-file-coding-system' is nil.
1612 The default coding system for process I/O is nil.
1613 The default value for the command `set-terminal-coding-system' is nil.
1614 The default value for the command `set-keyboard-coding-system' is nil.
1616 The order of priorities of coding systems are as follows:
1625 ;; This function formerly set default-enable-multibyte-characters to t,
1626 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1628 (set-coding-system-priority
1637 (set-default-coding-systems nil)
1638 (setq default-sendmail-coding-system 'iso-latin-1)
1639 (setq default-file-name-coding-system 'iso-latin-1)
1640 ;; Preserve eol-type from existing default-process-coding-systems.
1641 ;; On non-unix-like systems in particular, these may have been set
1642 ;; carefully by the user, or by the startup code, to deal with the
1643 ;; users shell appropriately, so should not be altered by changing
1644 ;; language environment.
1645 (let ((output-coding
1646 ;; When bootstrapping, coding-systems are not defined yet, so
1647 ;; we need to catch the error from check-coding-system.
1649 (coding-system-change-text-conversion
1650 (car default-process-coding-system) 'undecided)
1651 (coding-system-error 'undecided)))
1654 (coding-system-change-text-conversion
1655 (cdr default-process-coding-system) 'iso-latin-1)
1656 (coding-system-error 'iso-latin-1))))
1657 (setq default-process-coding-system
1658 (cons output-coding input-coding)))
1660 ;; Don't alter the terminal and keyboard coding systems here.
1661 ;; The terminal still supports the same coding system
1662 ;; that it supported a minute ago.
1663 ;; (set-terminal-coding-system-internal nil)
1664 ;; (set-keyboard-coding-system-internal nil)
1666 (set-unibyte-charset 'iso-8859-1))
1668 (reset-language-environment)
1670 (defun set-display-table-and-terminal-coding-system (language-name)
1671 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1672 (let ((coding (get-language-info language-name 'unibyte-display)))
1674 (standard-display-european-internal)
1675 ;; The following 2 lines undo the 8-bit display that we set up
1676 ;; in standard-display-european-internal, which see. This is in
1677 ;; case the user has used standard-display-european earlier in
1678 ;; this session. (The MS-DOS port doesn't use that setup, so it
1679 ;; doesn't need to undo it.)
1680 (when standard-display-table
1682 (aset standard-display-table (+ i 128) nil))))
1683 (or (eq window-system 'pc)
1684 (set-terminal-coding-system coding))))
1686 (defun set-language-environment (language-name)
1687 "Set up multi-lingual environment for using LANGUAGE-NAME.
1688 This sets the coding system priority and the default input method
1689 and sometimes other things. LANGUAGE-NAME should be a string
1690 which is the name of a language environment. For example, \"Latin-1\"
1691 specifies the character set for the major languages of Western Europe."
1692 (interactive (list (read-language-name
1694 "Set language environment (default, English): ")))
1696 (if (symbolp language-name)
1697 (setq language-name (symbol-name language-name)))
1698 (setq language-name "English"))
1699 (let ((slot (assoc-string language-name language-info-alist t)))
1701 (error "Language environment not defined: %S" language-name))
1702 (setq language-name (car slot)))
1703 (if current-language-environment
1704 (let ((func (get-language-info current-language-environment
1706 (run-hooks 'exit-language-environment-hook)
1707 (if (functionp func) (funcall func))))
1708 (let ((default-eol-type (coding-system-eol-type
1709 default-buffer-file-coding-system)))
1710 (reset-language-environment)
1712 ;; The features might set up coding systems.
1713 (let ((required-features (get-language-info language-name 'features)))
1714 (while required-features
1715 (require (car required-features))
1716 (setq required-features (cdr required-features))))
1718 (setq current-language-environment language-name)
1719 (set-language-environment-coding-systems language-name default-eol-type))
1720 (let ((input-method (get-language-info language-name 'input-method)))
1722 (setq default-input-method input-method)
1723 (if input-method-history
1724 (setq input-method-history
1726 (delete input-method input-method-history))))))
1728 ;; Put higher priorities to such charsets that are supported by the
1729 ;; coding systems of higher priorities in this environment.
1730 (let ((charsets (get-language-info language-name 'charset)))
1731 (dolist (coding (get-language-info language-name 'coding-priority))
1732 (setq charsets (append charsets (coding-system-charset-list coding))))
1734 (apply 'set-charset-priority charsets)))
1736 ;; Note: For DOS, we assumed that the charset cpXXX is already
1738 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1739 (if (eq window-system 'pc)
1740 (setq nonascii (intern "cp%d" dos-codepage)))
1741 (or (and (charsetp nonascii)
1742 (= (charset-dimension nonascii) 1))
1743 (setq nonascii 'iso-8859-1))
1744 (set-unibyte-charset nonascii))
1746 ;; Unibyte setups if necessary.
1747 (or default-enable-multibyte-characters
1748 (set-display-table-and-terminal-coding-system language-name))
1750 (let ((required-features (get-language-info language-name 'features)))
1751 (while required-features
1752 (require (car required-features))
1753 (setq required-features (cdr required-features))))
1755 ;; Don't invoke fontset-related functions if fontsets aren't
1756 ;; supported in this build of Emacs.
1757 (when (fboundp 'fontset-list)
1758 (let ((overriding-fontspec (get-language-info language-name
1759 'overriding-fontspec)))
1760 (if overriding-fontspec
1761 (set-overriding-fontspec-internal overriding-fontspec))))
1763 (let ((func (get-language-info language-name 'setup-function)))
1764 (if (functionp func)
1766 (run-hooks 'set-language-environment-hook)
1767 (force-mode-line-update t))
1769 (define-widget 'charset 'symbol
1772 :complete-function (lambda ()
1774 (lisp-complete-symbol 'charsetp))
1775 :completion-ignore-case t
1777 :validate (lambda (widget)
1778 (unless (charsetp (widget-value widget))
1779 (widget-put widget :error (format "Invalid charset: %S"
1780 (widget-value widget)))
1782 :prompt-history 'charset-history)
1784 (defcustom language-info-custom-alist nil
1785 "Customizations of language environment parameters.
1786 Value is an alist with elements like those of `language-info-alist'.
1787 These are used to set values in `language-info-alist' which replace
1788 the defaults. A typical use is replacing the default input method for
1789 the environment. Use \\[describe-language-environment] to find the environment's settings.
1791 This option is intended for use at startup. Removing items doesn't
1792 remove them from the language info until you next restart Emacs.
1794 Setting this variable directly does not take effect. See
1795 `set-language-info-alist' for use in programs."
1799 (custom-set-default s v)
1800 ;; Can't do this before language environments are set up.
1802 ;; modify language-info-alist
1804 (set-language-info-alist (car elt) (cdr elt)))
1805 ;; re-set the environment in case its parameters changed
1806 (set-language-environment current-language-environment)))
1808 :key-type (string :tag "Language environment"
1809 :completion-ignore-case t
1810 :complete-function widget-string-complete
1811 :completion-alist language-info-alist)
1813 (alist :key-type symbol
1814 :options ((documentation string)
1815 (charset (repeat charset))
1816 (sample-text string)
1817 (setup-function function)
1818 (exit-function function)
1819 (coding-system (repeat coding-system))
1820 (coding-priority (repeat coding-system))
1821 (nonascii-translation charset)
1824 :completion-ignore-case t
1825 :complete-function widget-string-complete
1826 :completion-alist input-method-alist
1827 :prompt-history input-method-history))
1828 (features (repeat symbol))
1829 (unibyte-display coding-system)))))
1831 (defun standard-display-european-internal ()
1832 ;; Actually set up direct output of non-ASCII characters.
1833 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1834 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1835 ;; the native font, and codes 160 and 146 stand for something very
1837 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1839 ;; Make non-line-break space display as a plain space.
1840 ;; Most X fonts do the wrong thing for code 160.
1841 (aset standard-display-table 160 [32])
1842 ;; With luck, non-Latin-1 fonts are more recent and so don't
1844 (aset standard-display-table (make-char 'latin-iso8859-1 160) [32])
1845 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1846 ;; don't contain a character at that position. Map it to the ASCII
1847 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1848 ;; U+2019, normally from the windows-1252 character set. XFree 4
1849 ;; fonts probably have the appropriate glyph at this position,
1850 ;; so they could use standard-display-8bit. It's better to use a
1851 ;; proper windows-1252 coding system. --fx]
1852 (aset standard-display-table 146 [39])
1853 ;; XFree86 4 has changed most of the fonts from their designed
1854 ;; versions such that `' no longer appears as balanced quotes.
1855 ;; Assume it has iso10646 fonts installed, so we can display
1857 (when (and (eq window-system 'x)
1858 (string= "The XFree86 Project, Inc" (x-server-vendor))
1859 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1861 ;; We suppress these setting for the moment because the
1862 ;; above assumption is wrong.
1863 ;; (aset standard-display-table ?' [?
\e,F"
\e(B])
1864 ;; (aset standard-display-table ?` [?
\e,F!
\e(B])
1865 ;; The fonts don't have the relevant bug.
1866 (aset standard-display-table 160 nil)
1867 (aset standard-display-table (make-char 'latin-iso8859-1 160)
1870 (defun set-language-environment-coding-systems (language-name
1872 "Do various coding system setups for language environment LANGUAGE-NAME.
1874 The optional arg EOL-TYPE specifies the eol-type of the default value
1875 of `buffer-file-coding-system' set by this function."
1876 (let* ((priority (get-language-info language-name 'coding-priority))
1877 (default-coding (car priority)))
1879 (set-default-coding-systems
1880 (if (memq eol-type '(0 1 2 unix dos mac))
1881 (coding-system-change-eol-conversion default-coding eol-type)
1883 (setq default-sendmail-coding-system default-coding)
1884 (apply 'set-coding-system-priority priority))))
1886 (defsubst princ-list (&rest args)
1887 "Print all arguments with `princ', then print \"\n\"."
1888 (while args (princ (car args)) (setq args (cdr args)))
1891 (put 'describe-specified-language-support 'apropos-inhibit t)
1893 ;; Print language-specific information such as input methods,
1894 ;; charsets, and coding systems. This function is intended to be
1895 ;; called from the menu:
1896 ;; [menu-bar mule describe-language-environment LANGUAGE]
1897 ;; and should not run it by `M-x describe-current-input-method-function'.
1898 (defun describe-specified-language-support ()
1899 "Describe how Emacs supports the specified language environment."
1901 (let (language-name)
1902 (if (not (and (symbolp last-command-event)
1903 (or (not (eq last-command-event 'Default))
1904 (setq last-command-event 'English))
1905 (setq language-name (symbol-name last-command-event))))
1906 (error "Bogus calling sequence"))
1907 (describe-language-environment language-name)))
1909 (defun describe-language-environment (language-name)
1910 "Describe how Emacs supports language environment LANGUAGE-NAME."
1912 (list (read-language-name
1914 "Describe language environment (default, current choice): ")))
1915 (if (null language-name)
1916 (setq language-name current-language-environment))
1917 (if (or (null language-name)
1918 (null (get-language-info language-name 'documentation)))
1919 (error "No documentation for the specified language"))
1920 (if (symbolp language-name)
1921 (setq language-name (symbol-name language-name)))
1922 (dolist (feature (get-language-info language-name 'features))
1924 (let ((doc (get-language-info language-name 'documentation))
1926 (help-setup-xref (list #'describe-language-environment language-name)
1928 (with-output-to-temp-buffer (help-buffer)
1930 (set-buffer standard-output)
1931 (insert language-name " language environment\n\n")
1933 (insert doc "\n\n"))
1935 (let ((str (eval (get-language-info language-name 'sample-text))))
1937 (insert "Sample text:\n " str "\n\n")))
1939 (let ((input-method (get-language-info language-name 'input-method))
1940 (l (copy-sequence input-method-alist)))
1941 (insert "Input methods")
1943 (insert " (default, " input-method ")")
1944 (setq input-method (assoc input-method input-method-alist))
1945 (setq l (cons input-method (delete input-method l))))
1948 (when (eq t (compare-strings language-name nil nil
1949 (nth 1 (car l)) nil nil t))
1950 (insert " " (car (car l)))
1951 (search-backward (car (car l)))
1952 (help-xref-button 0 'help-input-method (car (car l)))
1953 (goto-char (point-max))
1955 (if (stringp (nth 3 (car l)))
1957 (car (nth 3 (car l))))
1958 "\" in mode line)\n"))
1961 (insert "Character sets:\n")
1962 (let ((l (get-language-info language-name 'charset)))
1964 (insert " nothing specific to " language-name "\n")
1966 (insert " " (symbol-name (car l)))
1967 (search-backward (symbol-name (car l)))
1968 (help-xref-button 0 'help-character-set (car l))
1969 (goto-char (point-max))
1970 (insert ": " (charset-description (car l)) "\n")
1973 (insert "Coding systems:\n")
1974 (let ((l (get-language-info language-name 'coding-system)))
1976 (insert " nothing specific to " language-name "\n")
1978 (insert " " (symbol-name (car l)))
1979 (search-backward (symbol-name (car l)))
1980 (help-xref-button 0 'help-coding-system (car l))
1981 (goto-char (point-max))
1983 (coding-system-mnemonic (car l))
1984 "' in mode line):\n\t"
1985 (coding-system-doc-string (car l))
1987 (let ((aliases (coding-system-aliases (car l))))
1989 (insert "\t(alias:")
1991 (insert " " (symbol-name (car aliases)))
1992 (setq aliases (cdr aliases)))
1994 (setq l (cdr l)))))))))
1998 (defvar locale-translation-file-name nil
1999 "File name for the system's file of locale-name aliases, or nil if none.")
2001 ;; The following definitions might as well be marked as constants and
2002 ;; purecopied, since they're normally used on startup, and probably
2003 ;; should reflect the facilities of the base Emacs.
2004 (defconst locale-language-names
2007 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2008 ;; as specified in the Single Unix Spec, Version 2.
2009 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2010 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2011 ;; see Internet RFC 2165 (1997-06) and
2012 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2013 ;; TERRITORY is a country code taken from ISO 3166
2014 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2015 ;; CODESET and MODIFIER are implementation-dependent.
2017 ;; jasonr comments: MS Windows uses three letter codes for
2018 ;; languages instead of the two letter ISO codes that POSIX
2019 ;; uses. In most cases the first two letters are the same, so
2020 ;; most of the regexps in locale-language-names work. Japanese
2021 ;; and Chinese are exceptions, which are listed in the
2022 ;; non-standard section at the bottom of locale-language-names.
2026 ("af" . "Latin-1") ; Afrikaans
2027 ("am" . "Ethiopic") ; Amharic
2028 ; ar Arabic glibc uses 8859-6
2033 ("be" . "Belarusian") ; Belarusian [Byelorussian until early 1990s]
2034 ("bg" . "Bulgarian") ; Bulgarian
2037 ; bn Bengali, Bangla
2039 ("br" . "Latin-1") ; Breton
2040 ("bs" . "Latin-2") ; Bosnian
2041 ("ca" . "Latin-1") ; Catalan
2044 ("cy" . "Welsh") ; Welsh [glibc uses Latin-8. Did this change?]
2045 ("da" . "Latin-1") ; Danish
2049 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2050 ;; That's actually what the GNU locales define, modulo things like
2052 ("en" . "Latin-1") ; English
2053 ("eo" . "Latin-3") ; Esperanto
2055 ("et" . "Latin-4") ; Estonian
2056 ("eu" . "Latin-1") ; Basque
2057 ; fa Persian glibc uses utf-8
2058 ("fi" . "Latin-1") ; Finnish
2060 ("fo" . "Latin-1") ; Faroese
2061 ("fr" . "French") ; French
2062 ("fy" . "Latin-1") ; Frisian
2063 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2064 ("gd" . "Latin-1") ; Scots Gaelic
2065 ("gl" . "Latin-1") ; Galician
2068 ("gv" . "Latin-8") ; Manx Gaelic glibc uses 8859-1
2071 ("hi" . "Devanagari") ; Hindi glibc uses utf-8
2072 ("hr" . "Croatian") ; Croatian
2073 ("hu" . "Latin-2") ; Hungarian
2076 ("id" . "Latin-1") ; Indonesian
2079 ("is" . "Latin-1") ; Icelandic
2080 ("it" . "Italian") ; Italian
2084 ("ka" . "Georgian") ; Georgian
2086 ("kl" . "Latin-1") ; Greenlandic
2092 ("kw" . "Latin-1") ; Cornish
2094 ("la" . "Latin-1") ; Latin
2095 ("lb" . "Latin-1") ; Luxemburgish
2097 ("lo" . "Lao") ; Laothian
2098 ("lt" . "Lithuanian")
2099 ("lv" . "Latvian") ; Latvian, Lettish
2101 ("mi" . "Latin-7") ; Maori
2102 ("mk" . "Cyrillic-ISO") ; Macedonian
2106 ("mr" . "Devanagari") ; Marathi glibc uses utf-8
2107 ("ms" . "Latin-1") ; Malay
2108 ("mt" . "Latin-3") ; Maltese
2111 ("ne" . "Devanagari") ; Nepali
2113 ("no" . "Latin-1") ; Norwegian
2114 ("oc" . "Latin-1") ; Occitan
2118 ("pl" . "Latin-2") ; Polish
2120 ("pt" . "Latin-1") ; Portuguese
2122 ("rm" . "Latin-1") ; Rhaeto-Romanic
2125 ("ru.*[_.]koi8\\(?:-r\\)?\\'" . "Cyrillic-KOI8") ; Russian
2126 ("ru" . "Cyrillic-ISO") ; Russian
2128 ("sa" . "Devanagari") ; Sanskrit
2132 ("sh" . "Latin-2") ; Serbo-Croatian
2135 ("sl" . "Slovenian")
2139 ("sq" . "Latin-1") ; Albanian
2140 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2141 ("sr.*@cyrillic" . "Cyrillic-ISO") ; per glibc
2145 ("sv" . "Swedish") ; Swedish
2146 ("sw" . "Latin-1") ; Swahili
2147 ; ta Tamil glibc uses utf-8
2148 ; te Telugu glibc uses utf-8
2153 ("tl" . "Latin-1") ; Tagalog
2161 ("uk" . "Ukrainian") ; Ukrainian
2162 ; ur Urdu glibc uses utf-8
2163 ("uz" . "Latin-1") ; Uzbek
2164 ("vi" . "Vietnamese") ; glibc uses utf-8
2166 ("wa" . "Latin-1") ; Walloon
2169 ("yi" . "Windows-1255") ; Yiddish
2174 ; zh_HK/BIG5-HKSCS \
2176 ("zh.*[._]big5" . "Chinese-BIG5")
2177 ("zh.*[._]gb18030" . "Chinese-GB18030") ; zh_CN.GB18030/GB18030 in glibc
2178 ("zh.*[._]gbk" . "Chinese-GBK")
2179 ;; glibc has zh_TW.EUC-TW, with zh_TW defaulting to Big5
2180 ("zh_tw" . "Chinese-CNS") ; glibc uses big5
2181 ("zh_tw[._]euc-tw" . "Chinese-EUC-TW")
2182 ("zh" . "Chinese-GB")
2185 ;; ISO standard locales
2187 ("posix$" . "ASCII")
2189 ;; The "IPA" Emacs language environment does not correspond
2190 ;; to any ISO 639 code, so let it stand for itself.
2193 ;; Nonstandard or obsolete language codes
2194 ("cz" . "Czech") ; e.g. Solaris 2.6
2195 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2196 ("iw" . "Hebrew") ; e.g. X11R6.4
2197 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2198 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2199 ("jp" . "Japanese") ; e.g. MS Windows
2200 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2201 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2203 "List of pairs of locale regexps and language names.
2204 The first element whose locale regexp matches the start of a downcased locale
2205 specifies the language name corresponding to that locale.
2206 If the language name is nil, there is no corresponding language environment.")
2208 (defconst locale-charset-language-names
2210 '((".*8859[-_]?1\\>" . "Latin-1")
2211 (".*8859[-_]?2\\>" . "Latin-2")
2212 (".*8859[-_]?3\\>" . "Latin-3")
2213 (".*8859[-_]?4\\>" . "Latin-4")
2214 (".*8859[-_]?9\\>" . "Latin-5")
2215 (".*8859[-_]?14\\>" . "Latin-8")
2216 (".*8859[-_]?15\\>" . "Latin-9")
2217 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2218 ;; utf-8@euro exists, so put this last. (@euro really specifies
2219 ;; the currency, rather than the charset.)
2220 (".*@euro\\>" . "Latin-9")))
2221 "List of pairs of locale regexps and charset language names.
2222 The first element whose locale regexp matches the start of a downcased locale
2223 specifies the language name whose charset corresponds to that locale.
2224 This language name is used if its charsets disagree with the charsets of
2225 the language name that would otherwise be used for this locale.")
2227 (defconst locale-preferred-coding-systems
2229 '(("ja.*[._]euc" . japanese-iso-8bit)
2230 ("ja.*[._]jis7" . iso-2022-jp)
2231 ("ja.*[._]pck" . japanese-shift-jis)
2232 ("ja.*[._]sjis" . japanese-shift-jis)
2233 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2234 (".*[._]utf" . utf-8)))
2235 "List of pairs of locale regexps and preferred coding systems.
2236 The first element whose locale regexp matches the start of a downcased locale
2237 specifies the coding system to prefer when using that locale.")
2239 (defun locale-name-match (key alist)
2240 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2241 Return the value corresponding to the first regexp that matches the
2242 start of KEY, or nil if there is no match."
2244 (while (and alist (not element))
2245 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2246 (setq element (car alist)))
2247 (setq alist (cdr alist)))
2250 (defun locale-charset-match-p (charset1 charset2)
2251 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2252 Matching is done ignoring case and any hyphens and underscores in the
2253 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2254 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2255 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2256 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2258 (defvar locale-charset-alist nil
2259 "Coding system alist keyed on locale-style charset name.
2260 Used by `locale-charset-to-coding-system'.")
2262 (defun locale-charset-to-coding-system (charset)
2263 "Find coding system corresponding to CHARSET.
2264 CHARSET is any sort of non-Emacs charset name, such as might be used
2265 in a locale codeset, or elsewhere. It is matched to a coding system
2266 first by case-insensitive lookup in `locale-charset-alist'. Then
2267 matches are looked for in the coding system list, treating case and
2268 the characters `-' and `_' as insignificant. The coding system base
2269 is returned. Thus, for instance, if charset \"ISO8859-2\",
2270 `iso-latin-2' is returned."
2271 (or (car (assoc-string charset locale-charset-alist t))
2272 (let ((cs coding-system-alist)
2274 (while (and (not c) cs)
2275 (if (locale-charset-match-p charset (caar cs))
2276 (setq c (intern (caar cs)))
2278 (if c (coding-system-base c)))))
2280 ;; Fixme: This ought to deal with the territory part of the locale
2281 ;; too, for setting things such as calendar holidays, ps-print paper
2282 ;; size, spelling dictionary.
2284 (defun set-locale-environment (&optional locale-name)
2285 "Set up multi-lingual environment for using LOCALE-NAME.
2286 This sets the language environment, the coding system priority,
2287 the default input method and sometimes other things.
2289 LOCALE-NAME should be a string which is the name of a locale supported
2290 by the system. Often it is of the form xx_XX.CODE, where xx is a
2291 language, XX is a country, and CODE specifies a character set and
2292 coding system. For example, the locale name \"ja_JP.EUC\" might name
2293 a locale for Japanese in Japan using the `japanese-iso-8bit'
2294 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2297 If LOCALE-NAME is nil, its value is taken from the environment
2298 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2300 The locale names supported by your system can typically be found in a
2301 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2302 will be translated according to the table specified by
2303 `locale-translation-file-name'.
2305 See also `locale-charset-language-names', `locale-language-names',
2306 `locale-preferred-coding-systems' and `locale-coding-system'."
2307 (interactive "sSet environment for locale: ")
2309 ;; Do this at runtime for the sake of binaries possibly transported
2310 ;; to a system without X.
2311 (setq locale-translation-file-name
2313 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2314 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2315 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2317 ;; The following name appears after the X-related names above,
2318 ;; since the X-related names are what X actually uses.
2319 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2321 (while (and files (not (file-exists-p (car files))))
2322 (setq files (cdr files)))
2325 (let ((locale locale-name))
2328 ;; Use the first of these three environment variables
2329 ;; that has a nonempty value.
2330 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2332 (= 0 (length locale))) ; nil or empty string
2333 (setq locale (getenv (pop vars))))))
2337 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2338 ;; using the translation file that many systems have.
2339 (when locale-translation-file-name
2341 (set-buffer-multibyte nil)
2342 (insert-file-contents locale-translation-file-name)
2343 (when (re-search-forward
2344 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2345 (setq locale (buffer-substring (point) (line-end-position))))))
2347 ;; Leave the system locales alone if the caller did not specify
2348 ;; an explicit locale name, as their defaults are set from
2349 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2350 ;; want to set them to the same value as LC_CTYPE.
2352 (setq system-messages-locale locale)
2353 (setq system-time-locale locale))
2355 (setq locale (downcase locale))
2357 (let ((language-name
2358 (locale-name-match locale locale-language-names))
2359 (charset-language-name
2360 (locale-name-match locale locale-charset-language-names))
2362 (or (locale-name-match locale locale-preferred-coding-systems)
2364 (if (string-match "\\.\\([^@]+\\)" locale)
2365 (locale-charset-to-coding-system
2366 (match-string 1 locale)))))))
2368 ;; Give preference to charset-language-name over language-name.
2369 (if (and charset-language-name
2371 (equal (get-language-info language-name 'charset)
2372 (get-language-info charset-language-name 'charset))))
2373 (setq language-name charset-language-name))
2377 ;; Set up for this character set. This is now the right way
2378 ;; to do it for both unibyte and multibyte modes.
2379 (set-language-environment language-name)
2381 ;; If default-enable-multibyte-characters is nil,
2382 ;; we are using single-byte characters,
2383 ;; so the display table and terminal coding system are irrelevant.
2384 (when default-enable-multibyte-characters
2385 (set-display-table-and-terminal-coding-system language-name))
2387 ;; Set the `keyboard-coding-system' if appropriate (tty
2388 ;; only). At least X and MS Windows can generate
2389 ;; multilingual input.
2390 (unless window-system
2391 (let ((kcs (or coding-system
2392 (car (get-language-info language-name
2394 (if kcs (set-keyboard-coding-system kcs))))
2396 (setq locale-coding-system
2397 (car (get-language-info language-name 'coding-priority))))
2400 (prefer-coding-system coding-system)
2401 (setq locale-coding-system coding-system))
2402 (when (get-language-info current-language-environment 'coding-priority)
2403 (let ((codeset (locale-info 'codeset))
2404 (coding-system (car (coding-system-priority-list))))
2406 (let ((cs (coding-system-aliases coding-system))
2408 (while (and cs (not result))
2410 (locale-charset-match-p (symbol-name (pop cs))
2411 (locale-info 'codeset))))
2413 (message "Warning: Default coding system `%s' disagrees with
2414 system codeset `%s' for this locale." coding-system codeset))))))))
2416 ;; On Windows, override locale-coding-system, keyboard-coding-system,
2417 ;; selection-coding-system with system codepage.
2418 (when (boundp 'w32-ansi-code-page)
2419 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2420 (when (coding-system-p code-page-coding)
2421 (setq locale-coding-system code-page-coding)
2422 (set-selection-coding-system code-page-coding)
2423 (set-keyboard-coding-system code-page-coding)
2424 (set-terminal-coding-system code-page-coding))))
2426 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2427 ;; (See comments in Flocale_info.)
2428 (let ((locale locale)
2429 (paper (locale-info 'paper)))
2431 ;; This will always be null at the time of writing.
2433 ((equal paper '(216 279))
2434 (setq ps-paper-type 'letter))
2435 ((equal paper '(210 297))
2436 (setq ps-paper-type 'a4)))
2437 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2438 (while (and vars (= 0 (length locale)))
2439 (setq locale (getenv (pop vars)))))
2441 ;; As of glibc 2.2.5, these are the only US Letter locales,
2442 ;; and the rest are A4.
2444 (or (locale-name-match locale '(("c$" . letter)
2449 ("enu$" . letter) ; Windows
2456 ;;; Character code property
2457 (put 'char-code-property-table 'char-table-extra-slots 0)
2459 (defvar char-code-property-table
2460 (make-char-table 'char-code-property-table)
2461 "Char-table containing a property list of each character code.
2463 See also the documentation of `get-char-code-property' and
2464 `put-char-code-property'.")
2466 (defun get-char-code-property (char propname)
2467 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2468 (let ((plist (aref char-code-property-table char)))
2470 (car (cdr (memq propname plist))))))
2472 (defun put-char-code-property (char propname value)
2473 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2474 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2475 (let ((plist (aref char-code-property-table char)))
2477 (let ((slot (memq propname plist)))
2479 (setcar (cdr slot) value)
2480 (nconc plist (list propname value))))
2481 (aset char-code-property-table char (list propname value)))))
2484 ;; Pretty description of encoded string
2486 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2487 (defvar iso-2022-control-alist
2495 (defun encoded-string-description (str coding-system)
2496 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2497 (setq str (string-as-unibyte str))
2499 (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
2500 ;; Try to get a pretty description for ISO 2022 escape sequences.
2501 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2502 (format "0x%02X" x))))
2503 (function (lambda (x) (format "0x%02X" x))))
2506 (defun encode-coding-char (char coding-system)
2507 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2508 If CODING-SYSTEM can't safely encode CHAR, return nil."
2509 (let ((str1 (string-as-multibyte (string char)))
2510 (str2 (string-as-multibyte (string char char)))
2512 (when (memq (coding-system-base coding-system)
2513 (find-coding-systems-string str1))
2514 ;; We must find the encoded string of CHAR. But, just encoding
2515 ;; CHAR will put extra control sequences (usually to designate
2516 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2517 ;; To exclude such tailing bytes, we at first encode one-char
2518 ;; string and two-char string, then check how many bytes at the
2519 ;; tail of both encoded strings are the same.
2521 (setq enc1 (encode-coding-string str1 coding-system)
2523 enc2 (encode-coding-string str2 coding-system)
2525 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2526 (setq i1 (1- i1) i2 (1- i2)))
2528 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2529 ;; and they are the extra control sequences at the tail to
2531 (substring enc2 0 i2))))
2533 ;; Backwards compatibility. These might be better with :init-value t,
2534 ;; but that breaks loadup.
2535 (define-minor-mode unify-8859-on-encoding-mode
2539 (define-minor-mode unify-8859-on-decoding-mode
2544 (defvar nonascii-insert-offset 0 "This variable is obsolete.")
2545 (defvar nonascii-translation-table nil "This variable is obsolete.")
2548 ;;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2549 ;;; mule-cmds.el ends here