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 Free Software Foundation, Inc.
7 ;; Keywords: mule, multilingual
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (defvar dos-codepage))
32 ;;; MULE related key bindings and menus.
34 (defvar mule-keymap (make-sparse-keymap)
35 "Keymap for Mule (Multilingual environment) specific commands.")
37 ;; Keep "C-x C-m ..." for mule specific commands.
38 (define-key ctl-x-map "\C-m" mule-keymap)
40 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
41 (define-key mule-keymap "t" 'set-terminal-coding-system)
42 (define-key mule-keymap "k" 'set-keyboard-coding-system)
43 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
44 (define-key mule-keymap "x" 'set-selection-coding-system)
45 (define-key mule-keymap "X" 'set-next-selection-coding-system)
46 (define-key mule-keymap "\C-\\" 'set-input-method)
47 (define-key mule-keymap "c" 'universal-coding-system-argument)
48 (define-key mule-keymap "l" 'set-language-environment)
50 (define-key help-map "\C-L" 'describe-language-environment)
51 (define-key help-map "L" 'describe-language-environment)
52 (define-key help-map "\C-\\" 'describe-input-method)
53 (define-key help-map "I" 'describe-input-method)
54 (define-key help-map "C" 'describe-coding-system)
55 (define-key help-map "h" 'view-hello-file)
57 (defvar mule-menu-keymap
58 (make-sparse-keymap "Mule (Multilingual Environment)")
59 "Keymap for Mule (Multilingual environment) menu specific commands.")
61 (defvar describe-language-environment-map
62 (make-sparse-keymap "Describe Language Environment"))
64 (defvar setup-language-environment-map
65 (make-sparse-keymap "Set Language Environment"))
67 (defvar set-coding-system-map
68 (make-sparse-keymap "Set Coding System"))
70 (define-key-after mule-menu-keymap [set-language-environment]
71 (list 'menu-item "Set Language Environment" setup-language-environment-map
72 :help "Multilingual environment suitable for a specific language"))
73 (define-key-after mule-menu-keymap [mouse-set-font]
74 '(menu-item "Set Font/Fontset" mouse-set-font
75 :visible (fboundp 'generate-fontset-menu)
76 :help "Select a font from list of known fonts/fontsets"))
77 (define-key-after mule-menu-keymap [separator-mule]
80 (define-key-after mule-menu-keymap [toggle-input-method]
81 '(menu-item "Toggle Input Method" toggle-input-method)
83 (define-key-after mule-menu-keymap [set-input-method]
84 '(menu-item "Select Input Method..." set-input-method)
86 (define-key-after mule-menu-keymap [describe-input-method]
87 '(menu-item "Describe Input Method" describe-input-method))
88 (define-key-after mule-menu-keymap [separator-input-method]
91 (define-key-after mule-menu-keymap [set-various-coding-system]
92 (list 'menu-item "Set Coding Systems" set-coding-system-map
93 :enable 'enable-multibyte-characters))
94 (define-key-after mule-menu-keymap [view-hello-file]
95 '(menu-item "Show Multi-lingual Text" view-hello-file
96 :enable (file-readable-p
97 (expand-file-name "HELLO" data-directory))
98 :help "Display file which says HELLO in many languages")
100 (define-key-after mule-menu-keymap [separator-coding-system]
103 (define-key-after mule-menu-keymap [describe-language-environment]
104 (list 'menu-item "Describe Language Environment"
105 describe-language-environment-map
106 :help "Show multilingual settings for a specific language")
108 (define-key-after mule-menu-keymap [describe-input-method]
109 '(menu-item "Describe Input Method..." describe-input-method
110 :help "Keyboard layout for a specific input method")
112 (define-key-after mule-menu-keymap [describe-coding-system]
113 '(menu-item "Describe Coding System..." describe-coding-system)
115 (define-key-after mule-menu-keymap [list-character-sets]
116 '(menu-item "List Character Sets" list-character-sets
117 :help "Show table of available character sets"))
118 (define-key-after mule-menu-keymap [mule-diag]
119 '(menu-item "Show All of Mule Status" mule-diag
120 :help "Display multilingual environment settings")
123 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
124 '(menu-item "For Saving this Buffer" set-buffer-file-coding-system
125 :help "How to encode this buffer on disk")
127 (define-key-after set-coding-system-map [universal-coding-system-argument]
128 '(menu-item "For Next Command" universal-coding-system-argument
129 :help "Coding system to be used by next command")
131 (define-key-after set-coding-system-map [set-terminal-coding-system]
132 '(menu-item "For Terminal" set-terminal-coding-system
133 :enable (null (memq window-system '(x w32 mac)))
134 :help "How to encode terminal output")
136 (define-key-after set-coding-system-map [set-keyboard-coding-system]
137 '(menu-item "For Keyboard" set-keyboard-coding-system
138 :help "How to decode keyboard input")
140 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
141 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
142 :visible (fboundp 'start-process)
143 :enable (get-buffer-process (current-buffer))
144 :help "How to en/decode I/O from/to subprocess connected to this buffer")
146 (define-key-after set-coding-system-map [set-selection-coding-system]
147 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
148 :visible (display-selections-p)
149 :help "How to en/decode data to/from selection/clipboard")
151 (define-key-after set-coding-system-map [set-next-selection-coding-system]
152 '(menu-item "For Next X Selection" set-next-selection-coding-system
153 :visible (display-selections-p)
154 :help "How to en/decode next selection/clipboard operation")
156 (define-key setup-language-environment-map
157 [Default] '(menu-item "Default" setup-specified-language-environment))
159 (define-key describe-language-environment-map
160 [Default] '(menu-item "Default" describe-specified-language-support))
162 ;; This should be a single character key binding because users use it
163 ;; very frequently while editing multilingual text. Now we can use
164 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
165 ;; convenient because it requires shifting on most keyboards. An
166 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
167 ;; but it won't be used that frequently.
168 (define-key global-map "\C-\\" 'toggle-input-method)
170 ;;; This is no good because people often type Shift-SPC
171 ;;; meaning to type SPC. -- rms.
172 ;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
173 ;;; (define-key global-map [?\S- ] 'toggle-input-method)
175 ;;; Mule related hyperlinks.
176 (defconst help-xref-mule-regexp-template
177 (purecopy (concat "\\(\\<\\("
178 "\\(coding system\\)\\|"
179 "\\(input method\\)\\|"
180 "\\(character set\\)\\|"
183 ;; Note starting with word-syntax character:
184 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
186 (defun coding-system-change-eol-conversion (coding-system eol-type)
187 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
188 The returned coding system converts end-of-line by EOL-TYPE
189 but text as the same way as CODING-SYSTEM.
190 EOL-TYPE should be `unix', `dos', `mac', or nil.
191 If EOL-TYPE is nil, the returned coding system detects
192 how end-of-line is formatted automatically while decoding.
194 EOL-TYPE can be specified by an integer 0, 1, or 2.
195 They means `unix', `dos', and `mac' respectively."
196 (if (symbolp eol-type)
197 (setq eol-type (cond ((eq eol-type 'unix) 0)
198 ((eq eol-type 'dos) 1)
199 ((eq eol-type 'mac) 2)
201 (let ((orig-eol-type (coding-system-eol-type coding-system)))
202 (if (vectorp orig-eol-type)
205 (aref orig-eol-type eol-type))
206 (let ((base (coding-system-base coding-system)))
209 (if (= eol-type orig-eol-type)
211 (setq orig-eol-type (coding-system-eol-type base))
212 (if (vectorp orig-eol-type)
213 (aref orig-eol-type eol-type))))))))
215 (defun coding-system-change-text-conversion (coding-system coding)
216 "Return a coding system which differs from CODING-SYSTEM in text conversion.
217 The returned coding system converts text by CODING
218 but end-of-line as the same way as CODING-SYSTEM.
219 If CODING is nil, the returned coding system detects
220 how text is formatted automatically while decoding."
222 (coding-system-base coding-system)
223 (let ((eol-type (coding-system-eol-type coding-system)))
224 (coding-system-change-eol-conversion
226 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
228 (defun toggle-enable-multibyte-characters (&optional arg)
229 "Change whether this buffer uses multibyte characters.
230 With arg, use multibyte characters if the arg is positive.
232 Note that this command does not convert the byte contents of
233 the buffer; it only changes the way those bytes are interpreted.
234 In general, therefore, this command *changes* the sequence of
235 characters that the current buffer contains.
237 We suggest you avoid using use this command unless you know what you
238 are doing. If you use it by mistake, and the buffer is now displayed
239 wrong, use this command again to toggle back to the right mode."
242 (if (null arg) (null enable-multibyte-characters)
243 (> (prefix-numeric-value arg) 0))))
244 (set-buffer-multibyte new-flag))
245 (force-mode-line-update))
247 (defun view-hello-file ()
248 "Display the HELLO file which list up many languages and characters."
250 ;; We have to decode the file in any environment.
251 (let ((default-enable-multibyte-characters t)
252 (coding-system-for-read 'iso-2022-7bit))
253 (find-file-read-only (expand-file-name "HELLO" data-directory))))
255 (defun universal-coding-system-argument ()
256 "Execute an I/O command using the specified coding system."
258 (let* ((default (and buffer-file-coding-system
259 (not (eq (coding-system-type buffer-file-coding-system)
261 buffer-file-coding-system))
262 (coding-system (read-coding-system
264 (format "Coding system for following command (default, %s): " default)
265 "Coding system for following command: ")
267 (keyseq (read-key-sequence
268 (format "Command to execute with %s:" coding-system)))
269 (cmd (key-binding keyseq))
272 (when (eq cmd 'universal-argument)
273 (call-interactively cmd)
275 ;; Process keys bound in `universal-argument-map'.
277 (setq keyseq (read-key-sequence nil t)
278 cmd (key-binding keyseq t))
279 (not (eq cmd 'universal-argument-other-key)))
280 (let ((current-prefix-arg prefix-arg)
281 ;; Have to bind `last-command-char' here so that
282 ;; `digit-argument', for isntance, can compute the
284 (last-command-char (aref keyseq 0)))
285 (call-interactively cmd)))
287 ;; This is the final call to `univeral-argument-other-key', which
288 ;; set's the final `prefix-arg.
289 (let ((current-prefix-arg prefix-arg))
290 (call-interactively cmd))
292 ;; Read the command to execute with the given prefix arg.
293 (setq prefix prefix-arg
294 keyseq (read-key-sequence nil t)
295 cmd (key-binding keyseq)))
297 (let ((coding-system-for-read coding-system)
298 (coding-system-for-write coding-system)
299 (current-prefix-arg prefix))
301 (call-interactively cmd))))
303 (defun set-default-coding-systems (coding-system)
304 "Set default value of various coding systems to CODING-SYSTEM.
305 This sets the following coding systems:
306 o coding system of a newly created buffer
307 o default coding system for subprocess I/O
308 This also sets the following values:
309 o default value used as file-name-coding-system for converting file names.
310 o default value for the command `set-terminal-coding-system' (not on MSDOS)
311 o default value for the command `set-keyboard-coding-system'."
312 (check-coding-system coding-system)
313 (setq-default buffer-file-coding-system coding-system)
314 (if default-enable-multibyte-characters
315 (setq default-file-name-coding-system coding-system))
316 ;; If coding-system is nil, honor that on MS-DOS as well, so
317 ;; that they could reset the terminal coding system.
318 (unless (and (eq window-system 'pc) coding-system)
319 (setq default-terminal-coding-system coding-system))
320 (setq default-keyboard-coding-system coding-system)
321 (setq default-process-coding-system (cons coding-system coding-system)))
323 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
324 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal "20.3")
326 (defun prefer-coding-system (coding-system)
327 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
328 This also sets the following coding systems:
329 o coding system of a newly created buffer
330 o default coding system for subprocess I/O
331 This also sets the following values:
332 o default value used as `file-name-coding-system' for converting file names.
333 o default value for the command `set-terminal-coding-system' (not on MSDOS)
334 o default value for the command `set-keyboard-coding-system'
336 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
337 systems set by this function will use that type of EOL conversion.
339 This command does not change the default value of terminal coding system
340 for MS-DOS terminal, because DOS terminals only support a single coding
341 system, and Emacs automatically sets the default to that coding system at
344 A coding system that requires automatic detection of text
345 encoding (e.g. undecided, unix) can't be preferred.
347 See also `coding-category-list' and `coding-system-category'."
348 (interactive "zPrefer coding system: ")
349 (if (not (and coding-system (coding-system-p coding-system)))
350 (error "Invalid coding system `%s'" coding-system))
351 (let ((coding-category (coding-system-category coding-system))
352 (base (coding-system-base coding-system))
353 (eol-type (coding-system-eol-type coding-system)))
354 (if (not coding-category)
355 ;; CODING-SYSTEM is no-conversion or undecided.
356 (error "Can't prefer the coding system `%s'" coding-system))
357 (set coding-category (or base coding-system))
358 (update-coding-systems-internal)
359 (or (eq coding-category (car coding-category-list))
360 ;; We must change the order.
361 (set-coding-priority (list coding-category)))
362 (if (and base (interactive-p))
363 (message "Highest priority is set to %s (base of %s)"
365 ;; If they asked for specific EOL conversion, honor that.
366 (if (memq eol-type '(0 1 2))
368 (coding-system-change-eol-conversion base eol-type))
369 (setq coding-system base))
370 (set-default-coding-systems coding-system)))
372 (defvar sort-coding-systems-predicate nil
373 "If non-nil, a predicate function to sort coding systems.
375 It is called with two coding systems, and should return t if the first
376 one is \"less\" than the second.
378 The function `sort-coding-systems' use it.")
380 (defun sort-coding-systems (codings)
381 "Sort coding system list CODINGS by a priority of each coding system.
383 If a coding system is most preferred, it has the highest priority.
384 Otherwise, a coding system corresponds to some MIME charset has higher
385 priorities. Among them, a coding system included in `coding-system'
386 key of the current language environment has higher priorities. See
387 also the documentation of `language-info-alist'.
389 If the variable `sort-coding-systems-predicate' (which see) is
390 non-nil, it is used to sort CODINGS in the different way than above."
391 (if sort-coding-systems-predicate
392 (sort codings sort-coding-systems-predicate)
393 (let* ((most-preferred (symbol-value (car coding-category-list)))
394 (lang-preferred (get-language-info current-language-environment
398 (let ((base (coding-system-base x)))
399 (+ (if (eq base most-preferred) 64 0)
400 (let ((mime (coding-system-get base 'mime-charset)))
402 (if (string-match "^x-" (symbol-name mime))
405 (if (memq base lang-preferred) 8 0)
406 (if (string-match "-with-esc$" (symbol-name base))
408 (if (eq (coding-system-type base) 2)
409 ;; For ISO based coding systems, prefer
410 ;; one that doesn't use escape sequences.
411 (let ((flags (coding-system-flags base)))
412 (if (or (consp (aref flags 0))
413 (consp (aref flags 1))
414 (consp (aref flags 2))
415 (consp (aref flags 3)))
416 (if (or (aref flags 8) (aref flags 9))
421 (sort codings (function (lambda (x y)
422 (> (funcall func x) (funcall func y))))))))
424 (defun find-coding-systems-region (from to)
425 "Return a list of proper coding systems to encode a text between FROM and TO.
426 All coding systems in the list can safely encode any multibyte characters
429 If the text contains no multibyte characters, return a list of a single
430 element `undecided'."
431 (let ((codings (find-coding-systems-region-internal from to)))
433 ;; The text contains only ASCII characters. Any coding
436 ;; We need copy-sequence because sorting will alter the argument.
437 (sort-coding-systems (copy-sequence codings)))))
439 (defun find-coding-systems-string (string)
440 "Return a list of proper coding systems to encode STRING.
441 All coding systems in the list can safely encode any multibyte characters
444 If STRING contains no multibyte characters, return a list of a single
445 element `undecided'."
446 (find-coding-systems-region string nil))
448 (defun find-coding-systems-for-charsets (charsets)
449 "Return a list of proper coding systems to encode characters of CHARSETS.
450 CHARSETS is a list of character sets."
451 (cond ((or (null charsets)
452 (and (= (length charsets) 1)
453 (eq 'ascii (car charsets))))
455 ((or (memq 'eight-bit-control charsets)
456 (memq 'eight-bit-graphic charsets))
457 '(raw-text emacs-mule))
461 (while (and codings charsets)
462 (setq charset (car charsets) charsets (cdr charsets))
463 (unless (eq charset 'ascii)
464 (setq l (aref char-coding-system-table (make-char charset)))
469 (if (memq (car codings) l)
470 (setq ll (cons (car codings) ll)))
471 (setq codings (cdr codings)))
472 (setq codings ll)))))
474 (char-table-extra-slot char-coding-system-table 0))))))
476 (defun find-multibyte-characters (from to &optional maxcount excludes)
477 "Find multibyte characters in the region specified by FROM and TO.
478 If FROM is a string, find multibyte characters in the string.
479 The return value is an alist of the following format:
480 ((CHARSET COUNT CHAR ...) ...)
482 CHARSET is a character set,
483 COUNT is a number of characters,
484 CHARs are found characters of the character set.
485 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
486 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
488 For invalid characters, CHARs are actually strings."
493 (while (setq idx (string-match "[^\000-\177]" from idx))
494 (setq char (aref from idx)
495 charset (char-charset char))
496 (if (eq charset 'unknown)
497 (setq char (match-string 0)))
498 (if (or (memq charset '(unknown
499 eight-bit-control eight-bit-graphic))
500 (not (or (eq excludes t) (memq charset excludes))))
501 (let ((slot (assq charset chars)))
503 (if (not (memq char (nthcdr 2 slot)))
504 (let ((count (nth 1 slot)))
505 (setcar (cdr slot) (1+ count))
506 (if (or (not maxcount) (< count maxcount))
507 (nconc slot (list char)))))
508 (setq chars (cons (list charset 1 char) chars)))))
509 (setq idx (1+ idx))))
512 (while (re-search-forward "[^\000-\177]" to t)
513 (setq char (preceding-char)
514 charset (char-charset char))
515 (if (eq charset 'unknown)
516 (setq char (match-string 0)))
517 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
518 (not (or (eq excludes t) (memq charset excludes))))
519 (let ((slot (assq charset chars)))
521 (if (not (member char (nthcdr 2 slot)))
522 (let ((count (nth 1 slot)))
523 (setcar (cdr slot) (1+ count))
524 (if (or (not maxcount) (< count maxcount))
525 (nconc slot (list char)))))
526 (setq chars (cons (list charset 1 char) chars))))))))
529 (defvar last-coding-system-specified nil
530 "Most recent coding system explicitly specified by the user when asked.
531 This variable is set whenever Emacs asks the user which coding system
532 to use in order to write a file. If you set it to nil explicitly,
533 then call `write-region', then afterward this variable will be non-nil
534 only if the user was explicitly asked and specified a coding system.")
536 (defvar select-safe-coding-system-accept-default-p nil
537 "If non-nil, a function to control the behaviour of coding system selection.
538 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
539 function `select-safe-coding-system' (which see). This variable
540 overrides that argument.")
542 (defun select-safe-coding-system (from to &optional default-coding-system
544 "Ask a user to select a safe coding system from candidates.
545 The candidates of coding systems which can safely encode a text
546 between FROM and TO are shown in a popup window. Among them, the most
547 proper one is suggested as the default.
549 The list of `buffer-file-coding-system' of the current buffer and the
550 most preferred coding system (if it corresponds to a MIME charset) is
551 treated as the default coding system list. Among them, the first one
552 that safely encodes the text is silently selected and returned without
553 any user interaction. See also the command `prefer-coding-system'.
555 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
556 list of coding systems to be prepended to the default coding system
559 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
560 determine the acceptability of the silently selected coding system.
561 It is called with that coding system, and should return nil if it
562 should not be silently selected and thus user interaction is required.
564 The variable `select-safe-coding-system-accept-default-p', if
565 non-nil, overrides ACCEPT-DEFAULT-P.
567 Kludgy feature: if FROM is a string, the string is the target text,
569 (if (and default-coding-system
570 (not (listp default-coding-system)))
571 (setq default-coding-system (list default-coding-system)))
573 ;; Change elements of the list to (coding . base-coding).
574 (setq default-coding-system
575 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
576 default-coding-system))
578 ;; If buffer-file-coding-system is not nil nor undecided, append it
580 (if buffer-file-coding-system
581 (let ((base (coding-system-base buffer-file-coding-system)))
582 (or (eq base 'undecided)
583 (assq buffer-file-coding-system default-coding-system)
584 (rassq base default-coding-system)
585 (setq default-coding-system
586 (append default-coding-system
587 (list (cons buffer-file-coding-system base)))))))
589 ;; If the most preferred coding system has the property mime-charset,
590 ;; append it to the defaults.
591 (let ((tail coding-category-list)
594 (not (setq preferred (symbol-value (car tail)))))
595 (setq tail (cdr tail)))
596 (and (coding-system-p preferred)
597 (setq base (coding-system-base preferred))
598 (coding-system-get preferred 'mime-charset)
599 (not (assq preferred default-coding-system))
600 (not (rassq base default-coding-system))
601 (setq default-coding-system
602 (append default-coding-system (list (cons preferred base))))))
604 (if select-safe-coding-system-accept-default-p
605 (setq accept-default-p select-safe-coding-system-accept-default-p))
607 (let ((codings (find-coding-systems-region from to))
609 (l default-coding-system))
610 (if (eq (car codings) 'undecided)
611 ;; Any coding system is ok.
612 (setq coding-system t)
614 (while (and l (not coding-system))
615 (if (memq (cdr (car l)) codings)
616 (setq coding-system (car (car l)))
618 (if (and coding-system accept-default-p)
619 (or (funcall accept-default-p coding-system)
620 (setq coding-system (list coding-system)))))
622 ;; If all the defaults failed, ask a user.
623 (when (or (not coding-system) (consp coding-system))
624 ;; At first, change each coding system to the corresponding
625 ;; mime-charset name if it is also a coding system. Such a name
626 ;; is more friendly to users.
630 (setq mime-charset (coding-system-get (car l) 'mime-charset))
631 (if (and mime-charset (coding-system-p mime-charset))
632 (setcar l mime-charset))
635 ;; Then ask users to select one form CODINGS.
637 (save-window-excursion
638 (with-output-to-temp-buffer "*Warning*"
640 (set-buffer standard-output)
641 (if (not default-coding-system)
642 (insert "No default coding systems to try.")
643 (insert "These default coding systems were tried")
645 (insert " to encode \""
646 (if (> (length from) 10)
647 (substring from 0 10)
653 (mapcar (function (lambda (x)
654 (princ " ") (princ (car x))))
655 default-coding-system)
657 (fill-region-as-paragraph pos (point)))
659 (if (consp coding-system)
660 (concat (format "%s safely encodes the target text,\n"
662 "but it is not recommended for encoding text in this context,\n"
663 "e.g., for sending an email message.\n")
664 "However, none of them safely encodes the target text.\n")))
665 (insert (if (consp coding-system)
666 "\nSelect the above, or "
668 "one of the following safe coding systems:\n")
671 (mapcar (function (lambda (x) (princ " ") (princ x)))
674 (fill-region-as-paragraph pos (point)))))
676 ;; Read a coding system.
677 (if (consp coding-system)
678 (setq codings (cons (car coding-system) codings)))
679 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
681 (name (completing-read
682 (format "Select coding system (default %s): "
684 safe-names nil t nil nil
685 (car (car safe-names)))))
686 (setq last-coding-system-specified (intern name)
687 coding-system last-coding-system-specified)))
688 (kill-buffer "*Warning*")))
690 (if (vectorp (coding-system-eol-type coding-system))
691 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
694 (coding-system-change-eol-conversion coding-system eol)))))
696 (if (eq coding-system t)
697 (setq coding-system buffer-file-coding-system))
700 (setq select-safe-coding-system-function 'select-safe-coding-system)
702 (defun select-message-coding-system ()
703 "Return a coding system to encode the outgoing message of the current buffer.
704 It at first tries the first coding system found in these variables
706 (1) local value of `buffer-file-coding-system'
707 (2) value of `sendmail-coding-system'
708 (3) value of `default-sendmail-coding-system'
709 (4) value of `default-buffer-file-coding-system'
710 If the found coding system can't encode the current buffer,
711 or none of them are bound to a coding system,
712 it asks the user to select a proper coding system."
713 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
714 buffer-file-coding-system)
715 sendmail-coding-system
716 default-sendmail-coding-system
717 default-buffer-file-coding-system)))
718 (if (eq coding 'no-conversion)
719 ;; We should never use no-conversion for outgoing mails.
721 (if (fboundp select-safe-coding-system-function)
722 (funcall select-safe-coding-system-function
723 (point-min) (point-max) coding
724 (function (lambda (x) (coding-system-get x 'mime-charset))))
727 ;;; Language support stuff.
729 (defvar language-info-alist nil
730 "Alist of language environment definitions.
731 Each element looks like:
732 (LANGUAGE-NAME . ((KEY . INFO) ...))
733 where LANGUAGE-NAME is a string, the name of the language environment,
734 KEY is a symbol denoting the kind of information, and
735 INFO is the data associated with KEY.
736 Meaningful values for KEY include
738 documentation value is documentation of what this language environment
739 is meant for, and how to use it.
740 charset value is a list of the character sets used by this
741 language environment.
742 sample-text value is one line of text,
743 written using those character sets,
744 appropriate for this language environment.
745 setup-function value is a function to call to switch to this
746 language environment.
747 exit-function value is a function to call to leave this
748 language environment.
749 coding-system value is a list of coding systems that are good
750 for saving text written in this language environment.
751 This list serves as suggestions to the user;
752 in effect, as a kind of documentation.
753 coding-priority value is a list of coding systems for this language
754 environment, in order of decreasing priority.
755 This is used to set up the coding system priority
756 list when you switch to this language environment.
758 value is a translation table to be set in the
759 variable `nonascii-translation-table' in this
760 language environment, or a character set from
761 which `nonascii-insert-offset' is calculated.
762 input-method value is a default input method for this language
764 features value is a list of features requested in this
765 language environment.
767 The following keys take effect only when multibyte characters are
768 globally disabled, i.e. the value of `default-enable-multibyte-characters'
771 unibyte-syntax value is a library name to load to set
772 unibyte 8-bit character syntaxes for this
773 language environment.
775 unibyte-display value is a coding system to encode characters
776 for the terminal. Characters in the range
777 of 160 to 255 display not as octal escapes,
778 but as non-ASCII characters in this language
781 (defun get-language-info (lang-env key)
782 "Return information listed under KEY for language environment LANG-ENV.
783 KEY is a symbol denoting the kind of information.
784 For a list of useful values for KEY and their meanings,
785 see `language-info-alist'."
786 (if (symbolp lang-env)
787 (setq lang-env (symbol-name lang-env)))
788 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
790 (cdr (assq key (cdr lang-slot))))))
792 (defun set-language-info (lang-env key info)
793 "Modify part of the definition of language environment LANG-ENV.
794 Specifically, this stores the information INFO under KEY
795 in the definition of this language environment.
796 KEY is a symbol denoting the kind of information.
797 INFO is the value for that information.
799 For a list of useful values for KEY and their meanings,
800 see `language-info-alist'."
801 (if (symbolp lang-env)
802 (setq lang-env (symbol-name lang-env)))
803 (let (lang-slot key-slot)
804 (setq lang-slot (assoc lang-env language-info-alist))
805 (if (null lang-slot) ; If no slot for the language, add it.
806 (setq lang-slot (list lang-env)
807 language-info-alist (cons lang-slot language-info-alist)))
808 (setq key-slot (assq key lang-slot))
809 (if (null key-slot) ; If no slot for the key, add it.
811 (setq key-slot (list key))
812 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
813 (setcdr key-slot (purecopy info))))
815 (defun set-language-info-alist (lang-env alist &optional parents)
816 "Store ALIST as the definition of language environment LANG-ENV.
817 ALIST is an alist of KEY and INFO values. See the documentation of
818 `language-info-alist' for the meanings of KEY and INFO.
820 Optional arg PARENTS is a list of parent menu names; it specifies
821 where to put this language environment in the
822 Describe Language Environment and Set Language Environment menus.
823 For example, (\"European\") means to put this language environment
824 in the European submenu in each of those two menus."
825 (if (symbolp lang-env)
826 (setq lang-env (symbol-name lang-env)))
827 (let ((describe-map describe-language-environment-map)
828 (setup-map setup-language-environment-map))
831 map parent-symbol parent prompt)
833 (if (symbolp (setq parent-symbol (car l)))
834 (setq parent (symbol-name parent))
835 (setq parent parent-symbol parent-symbol (intern parent)))
836 (setq map (lookup-key describe-map (vector parent-symbol)))
837 ;; This prompt string is for define-prefix-command, so
838 ;; that the map it creates will be suitable for a menu.
839 (or map (setq prompt (format "%s Environment" parent)))
842 (setq map (intern (format "describe-%s-environment-map"
844 (define-prefix-command map nil prompt)
845 (define-key-after describe-map (vector parent-symbol)
846 (cons parent map) t)))
847 (setq describe-map (symbol-value map))
848 (setq map (lookup-key setup-map (vector parent-symbol)))
851 (setq map (intern (format "setup-%s-environment-map"
853 (define-prefix-command map nil prompt)
854 (define-key-after setup-map (vector parent-symbol)
855 (cons parent map) t)))
856 (setq setup-map (symbol-value map))
859 ;; Set up menu items for this language env.
860 (let ((doc (assq 'documentation alist)))
862 (define-key-after describe-map (vector (intern lang-env))
863 (cons lang-env 'describe-specified-language-support) t)))
864 (define-key-after setup-map (vector (intern lang-env))
865 (cons lang-env 'setup-specified-language-environment) t)
868 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
869 (setq alist (cdr alist)))))
871 (defun read-language-name (key prompt &optional default)
872 "Read a language environment name which has information for KEY.
873 If KEY is nil, read any language environment.
874 Prompt with PROMPT. DEFAULT is the default choice of language environment.
875 This returns a language environment name as a string."
876 (let* ((completion-ignore-case t)
877 (name (completing-read prompt
880 (function (lambda (elm) (assq key elm))))
882 (if (and (> (length name) 0)
884 (get-language-info name key)))
887 ;;; Multilingual input methods.
889 "LEIM: Libraries of Emacs Input Methods."
892 (defconst leim-list-file-name "leim-list.el"
893 "Name of LEIM list file.
894 This file contains a list of libraries of Emacs input methods (LEIM)
895 in the format of Lisp expression for registering each input method.
896 Emacs loads this file at startup time.")
898 (defvar leim-list-header (format
899 ";;; %s -- list of LEIM (Library of Emacs Input Method)
901 ;; This file contains a list of LEIM (Library of Emacs Input Method)
902 ;; methods in the same directory as this file. Loading this file
903 ;; registers all the input methods in Emacs.
905 ;; Each entry has the form:
906 ;; (register-input-method
907 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
910 ;; See the function `register-input-method' for the meanings of the arguments.
912 ;; If this directory is included in load-path, Emacs automatically
913 ;; loads this file at startup time.
917 "Header to be inserted in LEIM list file.")
919 (defvar leim-list-entry-regexp "^(register-input-method"
920 "Regexp matching head of each entry in LEIM list file.
921 See also the variable `leim-list-header'")
923 (defvar update-leim-list-functions
924 '(quail-update-leim-list-file)
925 "List of functions to call to update LEIM list file.
926 Each function is called with one arg, LEIM directory name.")
928 (defun update-leim-list-file (&rest dirs)
929 "Update LEIM list file in directories DIRS."
930 (let ((functions update-leim-list-functions))
932 (apply (car functions) dirs)
933 (setq functions (cdr functions)))))
935 (defvar current-input-method nil
936 "The current input method for multilingual text.
937 If nil, that means no input method is activated now.")
938 (make-variable-buffer-local 'current-input-method)
939 (put 'current-input-method 'permanent-local t)
941 (defvar current-input-method-title nil
942 "Title string of the current input method shown in mode line.")
943 (make-variable-buffer-local 'current-input-method-title)
944 (put 'current-input-method-title 'permanent-local t)
946 (defcustom default-input-method nil
947 "*Default input method for multilingual text (a string).
948 This is the input method activated automatically by the command
949 `toggle-input-method' (\\[toggle-input-method])."
951 :type '(choice (const nil) string)
952 :set-after '(current-language-environment))
954 (put 'input-method-function 'permanent-local t)
956 (defvar input-method-history nil
957 "History list for some commands that read input methods.")
958 (make-variable-buffer-local 'input-method-history)
959 (put 'input-method-history 'permanent-local t)
961 (defvar inactivate-current-input-method-function nil
962 "Function to call for inactivating the current input method.
963 Every input method should set this to an appropriate value when activated.
964 This function is called with no argument.
966 This function should never change the value of `current-input-method'.
967 It is set to nil by the function `inactivate-input-method'.")
968 (make-variable-buffer-local 'inactivate-current-input-method-function)
969 (put 'inactivate-current-input-method-function 'permanent-local t)
971 (defvar describe-current-input-method-function nil
972 "Function to call for describing the current input method.
973 This function is called with no argument.")
974 (make-variable-buffer-local 'describe-current-input-method-function)
975 (put 'describe-current-input-method-function 'permanent-local t)
977 (defvar input-method-alist nil
978 "Alist of input method names vs how to use them.
979 Each element has the form:
980 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
981 See the function `register-input-method' for the meanings of the elements.")
983 (defun register-input-method (input-method lang-env &rest args)
984 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
985 INPUT-METHOD and LANG-ENV are symbols or strings.
987 The remaining arguments are:
988 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
989 ACTIVATE-FUNC is a function to call to activate this method.
990 TITLE is a string to show in the mode line when this method is active.
991 DESCRIPTION is a string describing this method and what it is good for.
992 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
993 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
995 This function is mainly used in the file \"leim-list.el\" which is
996 created at Emacs build time, registering all Quail input methods
997 contained in the Emacs distribution.
999 In case you want to register a new Quail input method by yourself, be
1000 careful to use the same input method title as given in the third
1001 parameter of `quail-define-package'. (If the values are different, the
1002 string specified in this function takes precedence.)
1004 The commands `describe-input-method' and `list-input-methods' need
1005 these duplicated values to show some information about input methods
1006 without loading the relevant Quail packages."
1007 (if (symbolp lang-env)
1008 (setq lang-env (symbol-name lang-env)))
1009 (if (symbolp input-method)
1010 (setq input-method (symbol-name input-method)))
1011 (let ((info (cons lang-env args))
1012 (slot (assoc input-method input-method-alist)))
1015 (setq slot (cons input-method info))
1016 (setq input-method-alist (cons slot input-method-alist)))))
1018 (defun read-input-method-name (prompt &optional default inhibit-null)
1019 "Read a name of input method from a minibuffer prompting with PROMPT.
1020 If DEFAULT is non-nil, use that as the default,
1021 and substitute it into PROMPT at the first `%s'.
1022 If INHIBIT-NULL is non-nil, null input signals an error.
1024 The return value is a string."
1026 (setq prompt (format prompt default)))
1027 (let* ((completion-ignore-case t)
1028 ;; As it is quite normal to change input method in the
1029 ;; minibuffer, we must enable it even if
1030 ;; enable-recursive-minibuffers is currently nil.
1031 (enable-recursive-minibuffers t)
1032 ;; This binding is necessary because input-method-history is
1034 (input-method (completing-read prompt input-method-alist
1035 nil t nil 'input-method-history
1037 (if (and input-method (symbolp input-method))
1038 (setq input-method (symbol-name input-method)))
1039 (if (> (length input-method) 0)
1042 (error "No valid input method is specified")))))
1044 (defun activate-input-method (input-method)
1045 "Switch to input method INPUT-METHOD for the current buffer.
1046 If some other input method is already active, turn it off first.
1047 If INPUT-METHOD is nil, deactivate any current input method."
1048 (if (and input-method (symbolp input-method))
1049 (setq input-method (symbol-name input-method)))
1050 (if (and current-input-method
1051 (not (string= current-input-method input-method)))
1052 (inactivate-input-method))
1053 (unless (or current-input-method (null input-method))
1054 (let ((slot (assoc input-method input-method-alist)))
1056 (error "Can't activate input method `%s'" input-method))
1057 (setq current-input-method-title nil)
1058 (let ((func (nth 2 slot)))
1059 (if (functionp func)
1060 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1061 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1063 (require (cdr func))
1064 (apply (car func) input-method (nthcdr 5 slot)))
1065 (error "Can't activate input method `%s'" input-method))))
1066 (setq current-input-method input-method)
1067 (or (stringp current-input-method-title)
1068 (setq current-input-method-title (nth 3 slot)))
1070 (run-hooks 'input-method-activate-hook)
1071 (force-mode-line-update)))))
1073 (defun inactivate-input-method ()
1074 "Turn off the current input method."
1075 (when current-input-method
1076 (if input-method-history
1077 (unless (string= current-input-method (car input-method-history))
1078 (setq input-method-history
1079 (cons current-input-method
1080 (delete current-input-method input-method-history))))
1081 (setq input-method-history (list current-input-method)))
1083 (funcall inactivate-current-input-method-function)
1085 (run-hooks 'input-method-inactivate-hook)
1086 (setq current-input-method nil
1087 input-method-function nil
1088 current-input-method-title nil)
1089 (force-mode-line-update)))))
1091 (defun set-input-method (input-method)
1092 "Select and activate input method INPUT-METHOD for the current buffer.
1093 This also sets the default input method to the one you specify.
1094 If INPUT-METHOD is nil, this function turns off the input method, and
1095 also causes you to be prompted for a name of an input method the next
1096 time you invoke \\[toggle-input-method].
1098 To deactivate the input method interactively, use \\[toggle-input-method].
1099 To deactivate it programmatically, use \\[inactivate-input-method]."
1101 (let* ((default (or (car input-method-history) default-input-method)))
1102 (list (read-input-method-name
1103 (if default "Select input method (default %s): " "Select input method: ")
1105 (activate-input-method input-method)
1106 (setq default-input-method input-method))
1108 (defun toggle-input-method (&optional arg)
1109 "Enable or disable multilingual text input method for the current buffer.
1110 Only one input method can be enabled at any time in a given buffer.
1112 The normal action is to enable an input method if none was
1113 enabled, and disable the current one otherwise. Which input method
1114 to enable can be determined in various ways--either the one most
1115 recently used, or the one specified by `default-input-method', or
1116 as a last resort by reading the name of an input method in the
1119 With a prefix argument, read an input method name with the minibuffer
1120 and enable that one. The default is the most recent input method specified
1121 \(not including the currently active input method, if any)."
1124 (if (and current-input-method (not arg))
1125 (inactivate-input-method)
1126 (let ((default (or (car input-method-history) default-input-method)))
1127 (if (and arg default (equal current-input-method default)
1128 (> (length input-method-history) 1))
1129 (setq default (nth 1 input-method-history)))
1130 (activate-input-method
1131 (if (or arg (not default))
1133 (read-input-method-name
1134 (if default "Input method (default %s): " "Input method: " )
1137 (or default-input-method
1138 (setq default-input-method current-input-method)))))
1140 (defun describe-input-method (input-method)
1141 "Describe input method INPUT-METHOD."
1143 (list (read-input-method-name
1144 "Describe input method (default, current choice): ")))
1145 (if (and input-method (symbolp input-method))
1146 (setq input-method (symbol-name input-method)))
1147 (if (null input-method)
1148 (describe-current-input-method)
1149 (let ((current current-input-method))
1153 (activate-input-method input-method)
1154 (describe-current-input-method))
1155 (activate-input-method current))
1157 (activate-input-method current)
1158 (with-output-to-temp-buffer "*Help*"
1159 (let ((elt (assoc input-method input-method-alist)))
1161 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1162 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1164 (defun describe-current-input-method ()
1165 "Describe the input method currently in use."
1166 (if current-input-method
1167 (if (and (symbolp describe-current-input-method-function)
1168 (fboundp describe-current-input-method-function))
1169 (funcall describe-current-input-method-function)
1170 (message "No way to describe the current input method `%s'"
1171 current-input-method)
1173 (error "No input method is activated now")))
1175 (defun read-multilingual-string (prompt &optional initial-input input-method)
1176 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1177 The input method selected last time is activated in minibuffer.
1178 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1180 Optional 3rd argument INPUT-METHOD specifies the input method
1181 to be activated instead of the one selected last time. It is a symbol
1185 current-input-method
1186 default-input-method
1187 (read-input-method-name "Input method: " nil t)))
1188 (if (and input-method (symbolp input-method))
1189 (setq input-method (symbol-name input-method)))
1190 (let ((prev-input-method current-input-method))
1193 (activate-input-method input-method)
1194 (read-string prompt initial-input nil nil t))
1195 (activate-input-method prev-input-method))))
1197 ;; Variables to control behavior of input methods. All input methods
1198 ;; should react to these variables.
1200 (defcustom input-method-verbose-flag 'default
1201 "*A flag to control extra guidance given by input methods.
1202 The value should be nil, t, `complex-only', or `default'.
1204 The extra guidance is done by showing list of available keys in echo
1205 area. When you use the input method in the minibuffer, the guidance
1206 is shown at the bottom short window (split from the existing window).
1208 If the value is t, extra guidance is always given, if the value is
1209 nil, extra guidance is always suppressed.
1211 If the value is `complex-only', only complex input methods such as
1212 `chinese-py' and `japanese' give extra guidance.
1214 If the value is `default', complex input methods always give extra
1215 guidance, but simple input methods give it only when you are not in
1218 See also the variable `input-method-highlight-flag'."
1219 :type '(choice (const t) (const nil) (const complex-only) (const default))
1222 (defcustom input-method-highlight-flag t
1223 "*If this flag is non-nil, input methods highlight partially-entered text.
1224 For instance, while you are in the middle of a Quail input method sequence,
1225 the text inserted so far is temporarily underlined.
1226 The underlining goes away when you finish or abort the input method sequence.
1227 See also the variable `input-method-verbose-flag'."
1231 (defvar input-method-activate-hook nil
1232 "Normal hook run just after an input method is activated.
1234 The variable `current-input-method' keeps the input method name
1237 (defvar input-method-inactivate-hook nil
1238 "Normal hook run just after an input method is inactivated.
1240 The variable `current-input-method' still keeps the input method name
1243 (defvar input-method-after-insert-chunk-hook nil
1244 "Normal hook run just after an input method insert some chunk of text.")
1246 (defvar input-method-exit-on-first-char nil
1247 "This flag controls when an input method returns.
1248 Usually, the input method does not return while there's a possibility
1249 that it may find a different translation if a user types another key.
1250 But, it this flag is non-nil, the input method returns as soon as
1251 the current key sequence gets long enough to have some valid translation.")
1253 (defvar input-method-use-echo-area nil
1254 "This flag controls how an input method shows an intermediate key sequence.
1255 Usually, the input method inserts the intermediate key sequence,
1256 or candidate translations corresponding to the sequence,
1257 at point in the current buffer.
1258 But, if this flag is non-nil, it displays them in echo area instead.")
1260 (defvar input-method-exit-on-invalid-key nil
1261 "This flag controls the behaviour of an input method on invalid key input.
1262 Usually, when a user types a key which doesn't start any character
1263 handled by the input method, the key is handled by turning off the
1264 input method temporarily. After that key, the input method is re-enabled.
1265 But, if this flag is non-nil, the input method is never back on.")
1268 (defvar set-language-environment-hook nil
1269 "Normal hook run after some language environment is set.
1271 When you set some hook function here, that effect usually should not
1272 be inherited to another language environment. So, you had better set
1273 another function in `exit-language-environment-hook' (which see) to
1274 cancel the effect.")
1276 (defvar exit-language-environment-hook nil
1277 "Normal hook run after exiting from some language environment.
1278 When this hook is run, the variable `current-language-environment'
1279 is still bound to the language environment being exited.
1281 This hook is mainly used for canceling the effect of
1282 `set-language-environment-hook' (which-see).")
1284 (put 'setup-specified-language-environment 'apropos-inhibit t)
1286 (defun setup-specified-language-environment ()
1287 "Switch to a specified language environment."
1289 (let (language-name)
1290 (if (and (symbolp last-command-event)
1291 (or (not (eq last-command-event 'Default))
1292 (setq last-command-event 'English))
1293 (setq language-name (symbol-name last-command-event)))
1294 (set-language-environment language-name)
1295 (error "Bogus calling sequence"))))
1297 (defcustom current-language-environment "English"
1298 "The last language environment specified with `set-language-environment'.
1299 This variable should be set only with \\[customize], which is equivalent
1300 to using the function `set-language-environment'."
1301 :link '(custom-manual "(emacs)Language Environments")
1302 :set (lambda (symbol value) (set-language-environment value))
1304 (or (car-safe (assoc-ignore-case
1305 (if (symbolp current-language-environment)
1306 (symbol-name current-language-environment)
1307 current-language-environment)
1308 language-info-alist))
1310 :type (cons 'choice (mapcar (lambda (lang)
1311 (list 'const (car lang)))
1312 language-info-alist))
1313 :initialize 'custom-initialize-default
1317 (defun reset-language-environment ()
1318 "Reset multilingual environment of Emacs to the default status.
1320 The default status is as follows:
1322 The default value of buffer-file-coding-system is nil.
1323 The default coding system for process I/O is nil.
1324 The default value for the command `set-terminal-coding-system' is nil.
1325 The default value for the command `set-keyboard-coding-system' is nil.
1327 The order of priorities of coding categories and the coding system
1328 bound to each category are as follows
1329 coding category coding system
1330 --------------------------------------------------
1331 coding-category-iso-8-2 iso-latin-1
1332 coding-category-iso-8-1 iso-latin-1
1333 coding-category-iso-7-tight iso-2022-jp
1334 coding-category-iso-7 iso-2022-7bit
1335 coding-category-iso-7-else iso-2022-7bit-lock
1336 coding-category-iso-8-else iso-2022-8bit-ss2
1337 coding-category-emacs-mule emacs-mule
1338 coding-category-raw-text raw-text
1339 coding-category-sjis japanese-shift-jis
1340 coding-category-big5 chinese-big5
1341 coding-category-ccl nil
1342 coding-category-binary no-conversion
1343 coding-category-utf-16-be nil
1344 coding-category-utf-16-le nil
1345 coding-category-utf-8 mule-utf-8"
1347 ;; This function formerly set default-enable-multibyte-characters to t,
1348 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1350 (setq coding-category-iso-7-tight 'iso-2022-jp
1351 coding-category-iso-7 'iso-2022-7bit
1352 coding-category-iso-8-1 'iso-latin-1
1353 coding-category-iso-8-2 'iso-latin-1
1354 coding-category-iso-7-else 'iso-2022-7bit-lock
1355 coding-category-iso-8-else 'iso-2022-8bit-ss2
1356 coding-category-emacs-mule 'emacs-mule
1357 coding-category-raw-text 'raw-text
1358 coding-category-sjis 'japanese-shift-jis
1359 coding-category-big5 'chinese-big5
1360 coding-category-utf-16-be nil
1361 coding-category-utf-16-le nil
1362 coding-category-utf-8 'mule-utf-8
1363 coding-category-ccl nil
1364 coding-category-binary 'no-conversion)
1366 (set-coding-priority
1367 '(coding-category-iso-8-1
1368 coding-category-iso-8-2
1369 coding-category-iso-7-tight
1370 coding-category-iso-7
1371 coding-category-iso-7-else
1372 coding-category-iso-8-else
1373 coding-category-emacs-mule
1374 coding-category-raw-text
1375 coding-category-sjis
1376 coding-category-big5
1378 coding-category-binary
1379 coding-category-utf-16-be
1380 coding-category-utf-16-le
1381 coding-category-utf-8))
1383 (update-coding-systems-internal)
1385 (set-default-coding-systems nil)
1386 (setq default-sendmail-coding-system 'iso-latin-1)
1387 (setq default-process-coding-system '(undecided . iso-latin-1))
1389 ;; Don't alter the terminal and keyboard coding systems here.
1390 ;; The terminal still supports the same coding system
1391 ;; that it supported a minute ago.
1392 ;;; (set-terminal-coding-system-internal nil)
1393 ;;; (set-keyboard-coding-system-internal nil)
1395 (setq nonascii-translation-table nil
1396 nonascii-insert-offset 0))
1398 (reset-language-environment)
1400 (defun set-display-table-and-terminal-coding-system (language-name)
1401 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1402 (let ((coding (get-language-info language-name 'unibyte-display)))
1404 (standard-display-european-internal)
1405 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1406 (aset standard-display-table 146 nil))
1407 (or (eq window-system 'pc)
1408 (set-terminal-coding-system coding))))
1410 (defun set-language-environment (language-name)
1411 "Set up multi-lingual environment for using LANGUAGE-NAME.
1412 This sets the coding system priority and the default input method
1413 and sometimes other things. LANGUAGE-NAME should be a string
1414 which is the name of a language environment. For example, \"Latin-1\"
1415 specifies the character set for the major languages of Western Europe."
1416 (interactive (list (read-language-name
1418 "Set language environment (default, English): ")))
1420 (if (symbolp language-name)
1421 (setq language-name (symbol-name language-name)))
1422 (setq language-name "English"))
1423 (or (assoc-ignore-case language-name language-info-alist)
1424 (error "Language environment not defined: %S" language-name))
1425 (if current-language-environment
1426 (let ((func (get-language-info current-language-environment
1428 (run-hooks 'exit-language-environment-hook)
1429 (if (fboundp func) (funcall func))))
1430 (let ((default-eol-type (coding-system-eol-type
1431 default-buffer-file-coding-system)))
1432 (reset-language-environment)
1434 (setq current-language-environment language-name)
1435 (set-language-environment-coding-systems language-name default-eol-type))
1436 (let ((input-method (get-language-info language-name 'input-method)))
1438 (setq default-input-method input-method)
1439 (if input-method-history
1440 (setq input-method-history
1442 (delete input-method input-method-history))))))
1443 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1445 (if (eq window-system 'pc)
1447 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1449 ((char-table-p nonascii)
1450 (setq nonascii-translation-table nonascii))
1451 ((and (eq window-system 'pc) (boundp dos-table))
1452 ;; DOS terminals' default is to use a special non-ASCII translation
1453 ;; table as appropriate for the installed codepage.
1454 (setq nonascii-translation-table (symbol-value dos-table)))
1455 ((charsetp nonascii)
1456 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1458 ;; Unibyte setups if necessary.
1459 (unless default-enable-multibyte-characters
1460 ;; Syntax and case table.
1461 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1463 (let ((set-case-syntax-set-multibyte nil))
1464 (load syntax nil t))
1465 ;; No information for syntax and case. Reset to the defaults.
1466 (let ((syntax-table (standard-syntax-table))
1467 (case-table (standard-case-table))
1468 (ch (if (eq window-system 'pc) 128 160)))
1470 (modify-syntax-entry ch " " syntax-table)
1471 (aset case-table ch ch)
1473 (set-char-table-extra-slot case-table 0 nil)
1474 (set-char-table-extra-slot case-table 1 nil)
1475 (set-char-table-extra-slot case-table 2 nil))
1476 (set-standard-case-table (standard-case-table))
1477 (let ((list (buffer-list)))
1479 (with-current-buffer (car list)
1480 (set-case-table (standard-case-table)))
1481 (setq list (cdr list))))))
1482 (set-display-table-and-terminal-coding-system language-name))
1484 (let ((required-features (get-language-info language-name 'features)))
1485 (while required-features
1486 (require (car required-features))
1487 (setq required-features (cdr required-features))))
1488 (let ((func (get-language-info language-name 'setup-function)))
1491 (run-hooks 'set-language-environment-hook)
1492 (force-mode-line-update t))
1494 (defun standard-display-european-internal ()
1495 ;; Actually set up direct output of non-ASCII characters.
1496 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1497 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1498 ;; the native font, and codes 160 and 146 stand for something very
1500 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1502 ;; Make non-line-break space display as a plain space.
1503 ;; Most X fonts do the wrong thing for code 160.
1504 (aset standard-display-table 160 [32])
1505 ;; With luck, non-Latin-1 fonts are more recent and so don't
1507 (aset standard-display-table 2208 [32]) ; Latin-1 NBSP
1508 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1509 ;; don't contain a character at that position. Map it to the ASCII
1510 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK
1511 ;; from the cp1252, aka Windows-1252 character set. --fx]
1512 (aset standard-display-table 146 [39]))))
1514 (defun set-language-environment-coding-systems (language-name
1516 "Do various coding system setups for language environment LANGUAGE-NAME.
1518 The optional arg EOL-TYPE specifies the eol-type of the default value
1519 of buffer-file-coding-system set by this function."
1520 (let* ((priority (get-language-info language-name 'coding-priority))
1521 (default-coding (car priority)))
1523 (let ((categories (mapcar 'coding-system-category priority)))
1524 (set-default-coding-systems
1525 (if (memq eol-type '(0 1 2 unix dos mac))
1526 (coding-system-change-eol-conversion default-coding eol-type)
1528 (setq default-sendmail-coding-system default-coding)
1529 (set-coding-priority categories)
1531 (set (car categories) (car priority))
1532 (setq priority (cdr priority) categories (cdr categories)))
1533 (update-coding-systems-internal)))))
1535 ;; Print all arguments with `princ', then print "\n".
1536 (defsubst princ-list (&rest args)
1537 (while args (princ (car args)) (setq args (cdr args)))
1540 (put 'describe-specified-language-support 'apropos-inhibit t)
1542 ;; Print a language specific information such as input methods,
1543 ;; charsets, and coding systems. This function is intended to be
1544 ;; called from the menu:
1545 ;; [menu-bar mule describe-language-environment LANGUAGE]
1546 ;; and should not run it by `M-x describe-current-input-method-function'.
1547 (defun describe-specified-language-support ()
1548 "Describe how Emacs supports the specified language environment."
1550 (let (language-name)
1551 (if (not (and (symbolp last-command-event)
1552 (or (not (eq last-command-event 'Default))
1553 (setq last-command-event 'English))
1554 (setq language-name (symbol-name last-command-event))))
1555 (error "Bogus calling sequence"))
1556 (describe-language-environment language-name)))
1558 (defun describe-language-environment (language-name)
1559 "Describe how Emacs supports language environment LANGUAGE-NAME."
1561 (list (read-language-name
1563 "Describe language environment (default, current choice): ")))
1564 (if (null language-name)
1565 (setq language-name current-language-environment))
1566 (if (or (null language-name)
1567 (null (get-language-info language-name 'documentation)))
1568 (error "No documentation for the specified language"))
1569 (if (symbolp language-name)
1570 (setq language-name (symbol-name language-name)))
1571 (let ((doc (get-language-info language-name 'documentation))
1573 (with-output-to-temp-buffer "*Help*"
1575 (set-buffer standard-output)
1576 (insert language-name " language environment\n\n")
1578 (insert doc "\n\n"))
1580 (let ((str (eval (get-language-info language-name 'sample-text))))
1582 (insert "Sample text:\n " str "\n\n")))
1584 (let ((input-method (get-language-info language-name 'input-method))
1585 (l (copy-sequence input-method-alist)))
1586 (insert "Input methods")
1588 (insert " (default, " input-method ")")
1589 (setq input-method (assoc input-method input-method-alist))
1590 (setq l (cons input-method (delete input-method l))))
1593 (when (string= language-name (nth 1 (car l)))
1594 (insert " " (car (car l)))
1595 (search-backward (car (car l)))
1596 (help-xref-button 0 #'describe-input-method (car (car l))
1597 "mouse-2, RET: describe this input method")
1598 (goto-char (point-max))
1600 (if (stringp (nth 3 (car l)))
1602 (car (nth 3 (car l))))
1603 "\" in mode line)\n"))
1606 (insert "Character sets:\n")
1607 (let ((l (get-language-info language-name 'charset)))
1609 (insert " nothing specific to " language-name "\n")
1611 (insert " " (symbol-name (car l)))
1612 (search-backward (symbol-name (car l)))
1613 (help-xref-button 0 #'describe-character-set (car l)
1614 "mouse-2, RET: describe this character set")
1615 (goto-char (point-max))
1616 (insert ": " (charset-description (car l)) "\n")
1619 (insert "Coding systems:\n")
1620 (let ((l (get-language-info language-name 'coding-system)))
1622 (insert " nothing specific to " language-name "\n")
1624 (insert " " (symbol-name (car l)))
1625 (search-backward (symbol-name (car l)))
1626 (help-xref-button 0 #'describe-coding-system (car l)
1627 "mouse-2, RET: describe this coding system")
1628 (goto-char (point-max))
1630 (coding-system-mnemonic (car l))
1631 "' in mode line):\n\t"
1632 (coding-system-doc-string (car l))
1634 (let ((aliases (coding-system-get (car l)
1635 'alias-coding-systems)))
1637 (insert "\t(alias:")
1639 (insert " " (symbol-name (car aliases)))
1640 (setq aliases (cdr aliases)))
1643 (help-setup-xref (list #'describe-language-environment language-name)
1644 (interactive-p))))))
1648 (defvar locale-translation-file-name nil
1649 "File name for the system's file of locale-name aliases, or nil if none.")
1651 (defvar locale-language-names
1653 ;; UTF-8 is not yet implemented.
1654 ;; Put this first, so that e.g. "ko.UTF-8" does not match "ko" below.
1657 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1658 ;; as specified in the Single Unix Spec, Version 2.
1659 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1660 ;; with additions from ISO 639/RA Newsletter No.1/1989;
1661 ;; see Internet RFC 2165 (1997-06).
1662 ;; TERRITORY is a country code taken from ISO 3166.
1663 ;; CODESET and MODIFIER are implementation-dependent.
1667 ("af" . "Latin-1") ; Afrikaans
1668 ("am" . "Ethiopic") ; Amharic
1674 ("be" . "Latin-5") ; Byelorussian
1675 ("bg" . "Latin-5") ; Bulgarian
1678 ; bn Bengali, Bangla
1680 ("br" . "Latin-1") ; Breton
1681 ("ca" . "Latin-1") ; Catalan
1684 ("cy" . "Latin-8") ; Welsh
1685 ("da" . "Latin-1") ; Danish
1689 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
1690 ("en" . "Latin-1") ; English
1691 ("eo" . "Latin-3") ; Esperanto
1693 ("et" . "Latin-4") ; Estonian
1694 ("eu" . "Latin-1") ; Basque
1696 ("fi" . "Latin-1") ; Finnish
1698 ("fo" . "Latin-1") ; Faroese
1699 ("fr" . "Latin-1") ; French
1700 ("fy" . "Latin-1") ; Frisian
1701 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
1702 ("gd" . "Latin-1") ; Scots Gaelic
1703 ("gl" . "Latin-1") ; Galician
1708 ("hi" . "Devanagari") ; Hindi
1709 ("hr" . "Latin-2") ; Croatian
1710 ("hu" . "Latin-2") ; Hungarian
1713 ("id" . "Latin-1") ; Indonesian
1716 ("is" . "Latin-1") ; Icelandic
1717 ("it" . "Latin-1") ; Italian
1723 ("kl" . "Latin-1") ; Greenlandic
1730 ("la" . "Latin-1") ; Latin
1732 ("lo" . "Lao") ; Laothian
1733 ("lt" . "Latin-4") ; Lithuanian
1734 ("lv" . "Latin-4") ; Latvian, Lettish
1737 ("mk" . "Latin-5") ; Macedonian
1741 ("mr" . "Devanagari") ; Marathi
1743 ("mt" . "Latin-3") ; Maltese
1746 ("ne" . "Devanagari") ; Nepali
1748 ("no" . "Latin-1") ; Norwegian
1753 ("pl" . "Latin-2") ; Polish
1755 ("pt" . "Latin-1") ; Portuguese
1757 ("rm" . "Latin-1") ; Rhaeto-Romanic
1760 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
1761 ("ru" . "Latin-5") ; Russian
1763 ("sa" . "Devanagari") ; Sanskrit
1766 ("sh" . "Latin-2") ; Serbo-Croatian
1769 ("sl" . "Slovenian")
1773 ("sq" . "Latin-1") ; Albanian
1774 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
1778 ("sv" . "Latin-1") ; Swedish
1779 ("sw" . "Latin-1") ; Swahili
1786 ("tl" . "Latin-1") ; Tagalog
1789 ("tr" . "Latin-5") ; Turkish
1794 ("uk" . "Latin-5") ; Ukrainian
1797 ("vi" . "Vietnamese")
1804 ("zh.*[._]big5" . "Chinese-BIG5")
1805 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
1806 ("zh_tw" . "Chinese-CNS")
1807 ("zh" . "Chinese-GB")
1810 ;; ISO standard locales
1812 ("posix$" . "ASCII")
1814 ;; The "IPA" Emacs language environment does not correspond
1815 ;; to any ISO 639 code, so let it stand for itself.
1818 ;; Nonstandard or obsolete language codes
1819 ("cz" . "Czech") ; e.g. Solaris 2.6
1820 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
1821 ("iw" . "Hebrew") ; e.g. X11R6.4
1822 ("sp" . "Latin-5") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
1823 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
1824 ("jp" . "Japanese") ; e.g. MS Windows
1826 "List of pairs of locale regexps and language names.
1827 The first element whose locale regexp matches the start of a downcased locale
1828 specifies the language name corresponding to that locale.
1829 If the language name is nil, there is no corresponding language environment.")
1831 (defvar locale-charset-language-names
1832 '((".*8859[-_]?1\\>" . "Latin-1")
1833 (".*8859[-_]?2\\>" . "Latin-2")
1834 (".*8859[-_]?3\\>" . "Latin-3")
1835 (".*8859[-_]?4\\>" . "Latin-4")
1836 (".*8859[-_]?9\\>" . "Latin-5")
1837 (".*8859[-_]?14\\>" . "Latin-8")
1838 (".*8859[-_]?15\\>" . "Latin-9")
1839 (".*@euro\\>" . "Latin-9")
1841 "List of pairs of locale regexps and charset language names.
1842 The first element whose locale regexp matches the start of a downcased locale
1843 specifies the language name whose charsets corresponds to that locale.
1844 This language name is used if its charsets disagree with the charsets of
1845 the language name that would otherwise be used for this locale.")
1847 (defvar locale-preferred-coding-systems
1848 '(("ja.*[._]euc" . japanese-iso-8bit)
1849 ("ja.*[._]jis7" . iso-2022-jp)
1850 ("ja.*[._]pck" . japanese-shift-jis)
1851 ("ja.*[._]sjis" . japanese-shift-jis)
1853 "List of pairs of locale regexps and preferred coding systems.
1854 The first element whose locale regexp matches the start of a downcased locale
1855 specifies the coding system to prefer when using that locale.")
1857 (defun locale-name-match (key alist)
1858 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
1859 Return the value corresponding to the first regexp that matches the
1860 start of KEY, or nil if there is no match."
1862 (while (and alist (not element))
1863 (if (string-match (concat "^\\(" (car (car alist)) "\\)") key)
1864 (setq element (car alist)))
1865 (setq alist (cdr alist)))
1868 (defun set-locale-environment (locale-name)
1869 "Set up multi-lingual environment for using LOCALE-NAME.
1870 This sets the coding system priority and the default input method
1871 and sometimes other things. LOCALE-NAME should be a string
1872 which is the name of a locale supported by the system;
1873 often it is of the form xx_XX.CODE, where xx is a language,
1874 XX is a country, and CODE specifies a character set and coding system.
1875 For example, the locale name \"ja_JP.EUC\" might name a locale
1876 for Japanese in Japan using the `japanese-iso-8bit' coding-system.
1878 If LOCALE-NAME is nil, its value is taken from the environment.
1880 The locale names supported by your system can typically be found in a
1881 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
1882 may be translated according to the table specified by
1883 `locale-translation-file-name'.
1885 See also `locale-charset-language-names', `locale-language-names',
1886 `locale-preferred-coding-systems' and `locale-coding-system'."
1888 ;; Do this at runtime for the sake of binaries possibly transported
1889 ;; to a system without X.
1890 (setq locale-translation-file-name
1892 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
1893 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
1894 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
1896 ;; The following name appears after the X-related names above,
1897 ;; since the X-related names are what X actually uses.
1898 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
1900 (while (and files (not (file-exists-p (car files))))
1901 (setq files (cdr files)))
1904 (let ((locale locale-name))
1907 ;; Use the first of these three environment variables
1908 ;; that has a nonempty value.
1909 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
1910 (while (and vars (not (setq locale (getenv (car vars)))))
1911 (setq vars (cdr vars)))))
1915 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
1916 ;; using the translation file that many systems have.
1917 (when locale-translation-file-name
1919 (insert-file-contents locale-translation-file-name)
1920 (when (re-search-forward
1921 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
1922 (setq locale (buffer-substring (point) (line-end-position))))))
1924 ;; Leave the system locales alone if the caller did not specify
1925 ;; an explicit locale name, as their defaults are set from
1926 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
1927 ;; want to set them to the same value as LC_CTYPE.
1929 (setq system-messages-locale locale)
1930 (setq system-time-locale locale))
1932 (setq locale (downcase locale))
1934 (let ((language-name
1935 (locale-name-match locale locale-language-names))
1936 (charset-language-name
1937 (locale-name-match locale locale-charset-language-names))
1939 (locale-name-match locale locale-preferred-coding-systems)))
1941 (if (and charset-language-name
1943 (equal (get-language-info language-name 'charset)
1944 (get-language-info charset-language-name 'charset))))
1945 (setq language-name charset-language-name))
1949 ;; Set up for this character set. This is now the right way
1950 ;; to do it for both unibyte and multibyte modes.
1951 (set-language-environment language-name)
1953 ;; If default-enable-multibyte-characters is nil,
1954 ;; we are using single-byte characters,
1955 ;; so the display table and terminal coding system are irrelevant.
1956 (when default-enable-multibyte-characters
1957 (set-display-table-and-terminal-coding-system language-name))
1959 (setq locale-coding-system
1960 (car (get-language-info language-name 'coding-priority))))
1963 (prefer-coding-system coding-system)
1964 (setq locale-coding-system coding-system))))))
1966 ;;; Charset property
1968 (defun get-charset-property (charset propname)
1969 "Return the value of CHARSET's PROPNAME property.
1970 This is the last value stored with
1971 (put-charset-property CHARSET PROPNAME VALUE)."
1972 (and (not (eq charset 'composition))
1973 (plist-get (charset-plist charset) propname)))
1975 (defun put-charset-property (charset propname value)
1976 "Store CHARSETS's PROPNAME property with value VALUE.
1977 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1978 (or (eq charset 'composition)
1979 (set-charset-plist charset
1980 (plist-put (charset-plist charset) propname value))))
1982 ;;; Character code property
1983 (put 'char-code-property-table 'char-table-extra-slots 0)
1985 (defvar char-code-property-table
1986 (make-char-table 'char-code-property-table)
1987 "Char-table containing a property list of each character code.
1989 See also the documentation of `get-char-code-property' and
1990 `put-char-code-property'.")
1992 (defun get-char-code-property (char propname)
1993 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1994 (let ((plist (aref char-code-property-table char)))
1996 (car (cdr (memq propname plist))))))
1998 (defun put-char-code-property (char propname value)
1999 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2000 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2001 (let ((plist (aref char-code-property-table char)))
2003 (let ((slot (memq propname plist)))
2005 (setcar (cdr slot) value)
2006 (nconc plist (list propname value))))
2007 (aset char-code-property-table char (list propname value)))))
2010 ;; Pretty description of encoded string
2012 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2013 (defvar iso-2022-control-alist
2021 (defun encoded-string-description (str coding-system)
2022 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2023 (setq str (string-as-unibyte str))
2025 (if (and coding-system (eq (coding-system-type coding-system) 2))
2026 ;; Try to get a pretty description for ISO 2022 escape sequences.
2027 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2028 (format "%02X" x))))
2029 (function (lambda (x) (format "0x%02X" x))))
2032 (defun encode-coding-char (char coding-system)
2033 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2034 If CODING-SYSTEM can't safely encode CHAR, return nil."
2035 (let ((str1 (string-as-multibyte (char-to-string char)))
2036 (str2 (string-as-multibyte (make-string 2 char)))
2037 (safe-chars (and coding-system
2038 (coding-system-get coding-system 'safe-chars)))
2039 (charset (char-charset char))
2041 (when (or (eq safe-chars t)
2043 (and safe-chars (aref safe-chars char)))
2044 ;; We must find the encoded string of CHAR. But, just encoding
2045 ;; CHAR will put extra control sequences (usually to designate
2046 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
2047 ;; To exclude such tailing bytes, we at first encode one-char
2048 ;; string and two-char string, then check how many bytes at the
2049 ;; tail of both encoded strings are the same.
2051 (setq enc1 (encode-coding-string str1 coding-system)
2053 enc2 (encode-coding-string str2 coding-system)
2055 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2056 (setq i1 (1- i1) i2 (1- i2)))
2058 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2059 ;; and they are the extra control sequences at the tail to
2061 (substring enc2 0 i2))))
2064 ;;; mule-cmds.el ends here