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