]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
Avoid non-ASCII characters to prevent loadup failure.
[gnu-emacs] / lisp / international / mule-cmds.el
1 ;;; mule-cmds.el --- Commands for mulitilingual environment
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 ;;; MULE related key bindings and menus.
28
29 (defvar mule-keymap (make-sparse-keymap)
30 "Keymap for Mule (Multilingual environment) specific commands.")
31
32 ;; Keep "C-x C-m ..." for mule specific commands.
33 (define-key ctl-x-map "\C-m" mule-keymap)
34
35 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
36 (define-key mule-keymap "t" 'set-terminal-coding-system)
37 (define-key mule-keymap "k" 'set-keyboard-coding-system)
38 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
39 (define-key mule-keymap "x" 'set-selection-coding-system)
40 (define-key mule-keymap "X" 'set-next-selection-coding-system)
41 (define-key mule-keymap "\C-\\" 'set-input-method)
42 (define-key mule-keymap "c" 'universal-coding-system-argument)
43 (define-key mule-keymap "l" 'set-language-environment)
44
45 (define-key help-map "\C-L" 'describe-language-environment)
46 (define-key help-map "L" 'describe-language-environment)
47 (define-key help-map "\C-\\" 'describe-input-method)
48 (define-key help-map "I" 'describe-input-method)
49 (define-key help-map "C" 'describe-coding-system)
50 (define-key help-map "h" 'view-hello-file)
51
52 (defvar mule-menu-keymap (make-sparse-keymap "Mule")
53 "Keymap for Mule (Multilingual environment) menu specific commands.")
54
55 (define-key global-map [menu-bar mule]
56 ;; It is better not to use backquote here,
57 ;; because that makes a bootstrapping problem
58 ;; if you need to recompile all the Lisp files using interpreted code.
59 (list 'menu-item "Mule" mule-menu-keymap
60 ':visible 'default-enable-multibyte-characters))
61
62 (setq menu-bar-final-items (cons 'mule menu-bar-final-items))
63
64 (defvar describe-language-environment-map
65 (make-sparse-keymap "Describe Language Environment"))
66
67 (defvar setup-language-environment-map
68 (make-sparse-keymap "Set Language Environment"))
69
70 (defvar set-coding-system-map
71 (make-sparse-keymap "Set Coding System"))
72
73 (define-key-after mule-menu-keymap [describe-language-environment]
74 (cons "Describe Language Environment" describe-language-environment-map)
75 t)
76 (define-key-after mule-menu-keymap [set-language-environment]
77 (cons "Set Language Environment" setup-language-environment-map)
78 t)
79 (define-key-after mule-menu-keymap [mouse-set-font]
80 '("Set Font/Fontset" . mouse-set-font)
81 t)
82 (define-key-after mule-menu-keymap [separator-mule]
83 '("--")
84 t)
85 (define-key-after mule-menu-keymap [toggle-input-method]
86 '("Toggle Input Method" . toggle-input-method)
87 t)
88 (define-key-after mule-menu-keymap [set-input-method]
89 '("Select Input Method" . set-input-method)
90 t)
91 (define-key-after mule-menu-keymap [describe-input-method]
92 '("Describe Input Method" . describe-input-method)
93 t)
94 (define-key-after mule-menu-keymap [separator-input-method]
95 '("--")
96 t)
97 (define-key-after mule-menu-keymap [describe-coding-system]
98 '("Describe Coding Systems" . describe-coding-system)
99 t)
100 (define-key-after mule-menu-keymap [set-various-coding-system]
101 (cons "Set Coding System" set-coding-system-map)
102 t)
103 (define-key-after mule-menu-keymap [separator-coding-system]
104 '("--")
105 t)
106 (define-key-after mule-menu-keymap [mule-diag]
107 '("Show All of Mule Status" . mule-diag)
108 t)
109 (define-key-after mule-menu-keymap [view-hello-file]
110 '("Show Script Examples" . view-hello-file)
111 t)
112
113 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
114 '("Buffer File" . set-buffer-file-coding-system)
115 t)
116 (define-key-after set-coding-system-map [universal-coding-system-argument]
117 '("Next Command" . universal-coding-system-argument)
118 t)
119 (define-key-after set-coding-system-map [set-terminal-coding-system]
120 '("Terminal" . set-terminal-coding-system)
121 t)
122 (define-key-after set-coding-system-map [set-keyboard-coding-system]
123 '("Keyboard" . set-keyboard-coding-system)
124 t)
125 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
126 '("Buffer Process" . set-buffer-process-coding-system)
127 t)
128 (define-key-after set-coding-system-map [set-selection-coding-system]
129 '("X Selection" . set-selection-coding-system)
130 t)
131 (define-key-after set-coding-system-map [set-next-selection-coding-system]
132 '("Next X Selection" . set-next-selection-coding-system)
133 t)
134 (define-key setup-language-environment-map
135 [Default] '("Default" . setup-specified-language-environment))
136
137 ;; These are meaningless when running under X and W32.
138 (put 'set-terminal-coding-system 'menu-enable
139 '(or (not window-system) (eq window-system 'pc)))
140 (put 'set-keyboard-coding-system 'menu-enable
141 '(or (not window-system) (eq window-system 'pc)))
142 ;; This is meaningless when the current buffer has no process.
143 (put 'set-buffer-process-coding-system 'menu-enable
144 '(get-buffer-process (current-buffer)))
145 ;; These are meaningless when running under terminal.
146 (put 'set-selection-coding-system 'menu-enable
147 'window-system)
148 (put 'set-next-selection-coding-system 'menu-enable
149 'window-system)
150
151 ;; This should be a single character key binding because users use it
152 ;; very frequently while editing multilingual text. Now we can use
153 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
154 ;; convenient because it requires shifting on most keyboards. An
155 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
156 ;; but it won't be used that frequently.
157 (define-key global-map "\C-\\" 'toggle-input-method)
158
159 ;;; This is no good because people often type Shift-SPC
160 ;;; meaning to type SPC. -- rms.
161 ;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
162 ;;; (define-key global-map [?\S- ] 'toggle-input-method)
163
164 (defun coding-system-change-eol-conversion (coding-system eol-type)
165 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
166 The returned coding system converts end-of-line by EOL-TYPE
167 but text as the same way as CODING-SYSTEM.
168 EOL-TYPE should be `unix', `dos', `mac', or nil.
169 If EOL-TYPE is nil, the returned coding system detects
170 how end-of-line is formatted automatically while decoding.
171
172 EOL-TYPE can be specified by an integer 0, 1, or 2.
173 They means `unix', `dos', and `mac' respectively."
174 (if (symbolp eol-type)
175 (setq eol-type (cond ((eq eol-type 'unix) 0)
176 ((eq eol-type 'dos) 1)
177 ((eq eol-type 'mac) 2)
178 (t eol-type))))
179 (let ((orig-eol-type (coding-system-eol-type coding-system)))
180 (if (vectorp orig-eol-type)
181 (if (not eol-type)
182 coding-system
183 (aref orig-eol-type eol-type))
184 (let ((base (coding-system-base coding-system)))
185 (if (not eol-type)
186 base
187 (if (= eol-type orig-eol-type)
188 coding-system
189 (setq orig-eol-type (coding-system-eol-type base))
190 (if (vectorp orig-eol-type)
191 (aref orig-eol-type eol-type))))))))
192
193 (defun coding-system-change-text-conversion (coding-system coding)
194 "Return a coding system which differs from CODING-SYSTEM in text conversion.
195 The returned coding system converts text by CODING
196 but end-of-line as the same way as CODING-SYSTEM.
197 If CODING is nil, the returned coding system detects
198 how text is formatted automatically while decoding."
199 (if (not coding)
200 (coding-system-base coding-system)
201 (let ((eol-type (coding-system-eol-type coding-system)))
202 (coding-system-change-eol-conversion
203 coding
204 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
205
206 (defun toggle-enable-multibyte-characters (&optional arg)
207 "Change whether this buffer uses multibyte characters.
208 With arg, use multibyte characters if the arg is positive.
209
210 Note that this command does not convert the byte contents of
211 the buffer; it only changes the way those bytes are interpreted.
212 In general, therefore, this command *changes* the sequence of
213 characters that the current buffer contains.
214
215 We suggest you avoid using use this command unless you know what you
216 are doing. If you use it by mistake, and the buffer is now displayed
217 wrong, use this command again to toggle back to the right mode."
218 (interactive "P")
219 (let ((new-flag
220 (if (null arg) (null enable-multibyte-characters)
221 (> (prefix-numeric-value arg) 0))))
222 (set-buffer-multibyte new-flag))
223 (force-mode-line-update))
224
225 (defun view-hello-file ()
226 "Display the HELLO file which list up many languages and characters."
227 (interactive)
228 ;; We have to decode the file in any environment.
229 (let ((default-enable-multibyte-characters t)
230 (coding-system-for-read 'iso-2022-7bit))
231 (find-file-read-only (expand-file-name "HELLO" data-directory))))
232
233 (defun universal-coding-system-argument ()
234 "Execute an I/O command using the specified coding system."
235 (interactive)
236 (let* ((default (and buffer-file-coding-system
237 (not (eq (coding-system-type buffer-file-coding-system)
238 t))
239 buffer-file-coding-system))
240 (coding-system (read-coding-system
241 (if default
242 (format "Coding system for following command (default, %s): " default)
243 "Coding system for following command: ")
244 default))
245 (keyseq (read-key-sequence
246 (format "Command to execute with %s:" coding-system)))
247 (cmd (key-binding keyseq)))
248 (let ((coding-system-for-read coding-system)
249 (coding-system-for-write coding-system))
250 (message "")
251 (call-interactively cmd))))
252
253 (defun set-default-coding-systems (coding-system)
254 "Set default value of various coding systems to CODING-SYSTEM.
255 This sets the following coding systems:
256 o coding system of a newly created buffer
257 o default coding system for subprocess I/O
258 This also sets the following values:
259 o default value used as file-name-coding-system for converting file names.
260 o default value for the command `set-terminal-coding-system' (not on MSDOS)
261 o default value for the command `set-keyboard-coding-system'."
262 (check-coding-system coding-system)
263 (setq-default buffer-file-coding-system coding-system)
264 (if default-enable-multibyte-characters
265 (setq default-file-name-coding-system coding-system))
266 ;; If coding-system is nil, honor that on MS-DOS as well, so
267 ;; that they could reset the terminal coding system.
268 (unless (and (eq window-system 'pc) coding-system)
269 (setq default-terminal-coding-system coding-system))
270 (setq default-keyboard-coding-system coding-system)
271 (setq default-process-coding-system (cons coding-system coding-system)))
272
273 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
274 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
275
276 (defun prefer-coding-system (coding-system)
277 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
278 This also sets the following coding systems:
279 o coding system of a newly created buffer
280 o default coding system for subprocess I/O
281 This also sets the following values:
282 o default value used as file-name-coding-system for converting file names.
283 o default value for the command `set-terminal-coding-system' (not on MSDOS)
284 o default value for the command `set-keyboard-coding-system'
285
286 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
287 systems set by this function will use that type of EOL conversion.
288
289 This command does not change the default value of terminal coding system
290 for MS-DOS terminal, because DOS terminals only support a single coding
291 system, and Emacs automatically sets the default to that coding system at
292 startup."
293 (interactive "zPrefer coding system: ")
294 (if (not (and coding-system (coding-system-p coding-system)))
295 (error "Invalid coding system `%s'" coding-system))
296 (let ((coding-category (coding-system-category coding-system))
297 (base (coding-system-base coding-system))
298 (eol-type (coding-system-eol-type coding-system)))
299 (if (not coding-category)
300 ;; CODING-SYSTEM is no-conversion or undecided.
301 (error "Can't prefer the coding system `%s'" coding-system))
302 (set coding-category (or base coding-system))
303 (update-coding-systems-internal)
304 (or (eq coding-category (car coding-category-list))
305 ;; We must change the order.
306 (set-coding-priority (list coding-category)))
307 (if (and base (interactive-p))
308 (message "Highest priority is set to %s (base of %s)"
309 base coding-system))
310 ;; If they asked for specific EOL conversion, honor that.
311 (if (memq eol-type '(0 1 2))
312 (setq coding-system
313 (coding-system-change-eol-conversion base eol-type))
314 (setq coding-system base))
315 (set-default-coding-systems coding-system)))
316
317 (defun find-coding-systems-region-subset-p (list1 list2)
318 "Return non-nil if all elements in LIST1 are included in LIST2.
319 Comparison done with EQ."
320 (catch 'tag
321 (while list1
322 (or (memq (car list1) list2)
323 (throw 'tag nil))
324 (setq list1 (cdr list1)))
325 t))
326
327 (defun find-coding-systems-region (from to)
328 "Return a list of proper coding systems to encode a text between FROM and TO.
329 All coding systems in the list can safely encode any multibyte characters
330 in the text.
331
332 If the text contains no multibyte characters, return a list of a single
333 element `undecided'."
334 (find-coding-systems-for-charsets (find-charset-region from to)))
335
336 (defun find-coding-systems-string (string)
337 "Return a list of proper coding systems to encode STRING.
338 All coding systems in the list can safely encode any multibyte characters
339 in STRING.
340
341 If STRING contains no multibyte characters, return a list of a single
342 element `undecided'."
343 (find-coding-systems-for-charsets (find-charset-string string)))
344
345 (defun find-coding-systems-for-charsets (charsets)
346 "Return a list of proper coding systems to encode characters of CHARSETS.
347 CHARSETS is a list of character sets."
348 (if (or (null charsets)
349 (and (= (length charsets) 1)
350 (eq 'ascii (car charsets))))
351 '(undecided)
352 (setq charsets (delq 'composition charsets))
353 (let ((l (coding-system-list 'base-only))
354 (charset-preferred-codings
355 (mapcar (function
356 (lambda (x)
357 (if (eq x 'unknown)
358 'raw-text
359 (get-charset-property x 'preferred-coding-system))))
360 charsets))
361 (priorities (mapcar (function (lambda (x) (symbol-value x)))
362 coding-category-list))
363 codings coding safe)
364 (if (memq 'unknown charsets)
365 ;; The region contains invalid multibyte characters.
366 (setq l '(raw-text)))
367 (while l
368 (setq coding (car l) l (cdr l))
369 (if (and (setq safe (coding-system-get coding 'safe-charsets))
370 (or (eq safe t)
371 (find-coding-systems-region-subset-p charsets safe)))
372 ;; We put the higher priority to coding systems included
373 ;; in CHARSET-PREFERRED-CODINGS, and within them, put the
374 ;; higher priority to coding systems which support smaller
375 ;; number of charsets.
376 (let ((priority
377 (+ (if (coding-system-get coding 'mime-charset) 4096 0)
378 (lsh (length (memq coding priorities)) 7)
379 (if (memq coding charset-preferred-codings) 64 0)
380 (if (> (coding-system-type coding) 0) 32 0)
381 (if (consp safe) (- 32 (length safe)) 0))))
382 (setq codings (cons (cons priority coding) codings)))))
383 (mapcar 'cdr
384 (sort codings (function (lambda (x y) (> (car x) (car y))))))
385 )))
386
387 (defun find-multibyte-characters (from to &optional maxcount excludes)
388 "Find multibyte characters in the region specified by FROM and TO.
389 If FROM is a string, find multibyte characters in the string.
390 The return value is an alist of the following format:
391 ((CHARSET COUNT CHAR ...) ...)
392 where
393 CHARSET is a character set,
394 COUNT is a number of characters,
395 CHARs are found characters of the character set.
396 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
397 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
398
399 For invalid characters, CHARs are actually strings."
400 (let ((chars nil)
401 charset char)
402 (if (stringp from)
403 (let ((idx 0))
404 (while (setq idx (string-match "[^\000-\177]" from idx))
405 (setq char (aref from idx)
406 charset (char-charset char))
407 (if (eq charset 'unknown)
408 (setq char (match-string 0)))
409 (if (or (eq charset 'unknown)
410 (not (or (eq excludes t) (memq charset excludes))))
411 (let ((slot (assq charset chars)))
412 (if slot
413 (if (not (memq char (nthcdr 2 slot)))
414 (let ((count (nth 1 slot)))
415 (setcar (cdr slot) (1+ count))
416 (if (or (not maxcount) (< count maxcount))
417 (nconc slot (list char)))))
418 (setq chars (cons (list charset 1 char) chars)))))
419 (setq idx (1+ idx))))
420 (save-excursion
421 (goto-char from)
422 (while (re-search-forward "[^\000-\177]" to t)
423 (setq char (preceding-char)
424 charset (char-charset char))
425 (if (eq charset 'unknown)
426 (setq char (match-string 0)))
427 (if (or (eq charset 'unknown)
428 (not (or (eq excludes t) (memq charset excludes))))
429 (let ((slot (assq charset chars)))
430 (if slot
431 (if (not (member char (nthcdr 2 slot)))
432 (let ((count (nth 1 slot)))
433 (setcar (cdr slot) (1+ count))
434 (if (or (not maxcount) (< count maxcount))
435 (nconc slot (list char)))))
436 (setq chars (cons (list charset 1 char) chars))))))))
437 (nreverse chars)))
438
439 (defvar last-coding-system-specified nil
440 "Most recent coding system explicitly specified by the user when asked.
441 This variable is set whenever Emacs asks the user which coding system
442 to use in order to write a file. If you set it to nil explicitly,
443 then call `write-region', then afterward this variable will be non-nil
444 only if the user was explicitly asked and specified a coding system.")
445
446 (defun select-safe-coding-system (from to &optional default-coding-system)
447 "Ask a user to select a safe coding system from candidates.
448 The candidates of coding systems which can safely encode a text
449 between FROM and TO are shown in a popup window.
450
451 Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be
452 checked at first. If omitted, buffer-file-coding-system of the
453 current buffer is used.
454
455 If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
456 returned without any user interaction.
457
458 Kludgy feature: if FROM is a string, the string is the target text,
459 and TO is ignored."
460 (or default-coding-system
461 (setq default-coding-system buffer-file-coding-system))
462 (let* ((charsets (if (stringp from) (find-charset-string from)
463 (find-charset-region from to)))
464 (safe-coding-systems (find-coding-systems-for-charsets charsets)))
465 (if (or (not enable-multibyte-characters)
466 (eq (car safe-coding-systems) 'undecided)
467 (eq default-coding-system 'no-conversion)
468 (and default-coding-system
469 (memq (coding-system-base default-coding-system)
470 safe-coding-systems)))
471 default-coding-system
472
473 ;; At first, change each coding system to the corresponding
474 ;; mime-charset name if it is also a coding system.
475 (let ((l safe-coding-systems)
476 mime-charset)
477 (while l
478 (setq mime-charset (coding-system-get (car l) 'mime-charset))
479 (if (and mime-charset (coding-system-p mime-charset))
480 (setcar l mime-charset))
481 (setq l (cdr l))))
482
483 (let ((non-safe-chars (find-multibyte-characters
484 from to 3
485 (and default-coding-system
486 (coding-system-get default-coding-system
487 'safe-charsets))))
488 show-position overlays)
489 (save-excursion
490 ;; Highlight characters that default-coding-system can't encode.
491 (when (integerp from)
492 (goto-char from)
493 (let ((found nil))
494 (while (and (not found)
495 (re-search-forward "[^\000-\177]" to t))
496 (setq found (assq (char-charset (preceding-char))
497 non-safe-chars))))
498 (forward-line -1)
499 (setq show-position (point))
500 (save-excursion
501 (while (and (< (length overlays) 256)
502 (re-search-forward "[^\000-\177]" to t))
503 (let* ((char (preceding-char))
504 (charset (char-charset char)))
505 (when (assq charset non-safe-chars)
506 (setq overlays (cons (make-overlay (1- (point)) (point))
507 overlays))
508 (overlay-put (car overlays) 'face 'highlight))))))
509
510 ;; At last, ask a user to select a proper coding system.
511 (unwind-protect
512 (save-window-excursion
513 (when show-position
514 ;; At first, be sure to show the current buffer.
515 (set-window-buffer (selected-window) (current-buffer))
516 (set-window-start (selected-window) show-position))
517 ;; Then, show a helpful message.
518 (with-output-to-temp-buffer "*Warning*"
519 (save-excursion
520 (set-buffer standard-output)
521 (insert "The target text contains the following non ASCII character(s):\n")
522 (let ((len (length non-safe-chars))
523 (shown 0))
524 (while (and non-safe-chars (< shown 3))
525 (when (> (length (car non-safe-chars)) 2)
526 (setq shown (1+ shown))
527 (insert (format "%25s: " (car (car non-safe-chars))))
528 (let ((l (nthcdr 2 (car non-safe-chars))))
529 (while l
530 (if (or (stringp (car l)) (char-valid-p (car l)))
531 (insert (car l)))
532 (setq l (cdr l))))
533 (if (> (nth 1 (car non-safe-chars)) 3)
534 (insert "..."))
535 (insert "\n"))
536 (setq non-safe-chars (cdr non-safe-chars)))
537 (if (< shown len)
538 (insert (format "%27s\n" "..."))))
539 (insert (format
540 "These can't be encoded safely by the coding system %s.
541
542 Please select one from the following safe coding systems:\n"
543 default-coding-system))
544 (let ((pos (point))
545 (fill-prefix " "))
546 (mapcar (function (lambda (x) (princ " ") (princ x)))
547 safe-coding-systems)
548 (fill-region-as-paragraph pos (point)))))
549
550 ;; Read a coding system.
551 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
552 safe-coding-systems))
553 (name (completing-read
554 (format "Select coding system (default %s): "
555 (car safe-coding-systems))
556 safe-names nil t nil nil
557 (car (car safe-names)))))
558 (setq last-coding-system-specified (intern name))
559 (if (integerp (coding-system-eol-type default-coding-system))
560 (setq last-coding-system-specified
561 (coding-system-change-eol-conversion
562 last-coding-system-specified
563 (coding-system-eol-type default-coding-system))))
564 last-coding-system-specified))
565 (kill-buffer "*Warning*")
566 (while overlays
567 (delete-overlay (car overlays))
568 (setq overlays (cdr overlays)))))))))
569
570 (setq select-safe-coding-system-function 'select-safe-coding-system)
571
572 (defun select-message-coding-system ()
573 "Return a coding system to encode the outgoing message of the current buffer.
574 It at first tries the first coding system found in these variables
575 in this order:
576 (1) local value of `buffer-file-coding-system'
577 (2) value of `sendmail-coding-system'
578 (3) value of `default-buffer-file-coding-system'
579 (4) value of `default-sendmail-coding-system'
580 If the found coding system can't encode the current buffer,
581 or none of them are bound to a coding system,
582 it asks the user to select a proper coding system."
583 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
584 buffer-file-coding-system)
585 sendmail-coding-system
586 default-buffer-file-coding-system
587 default-sendmail-coding-system)))
588 (if (eq coding 'no-conversion)
589 ;; We should never use no-conversion for outgoing mails.
590 (setq coding nil))
591 (if (fboundp select-safe-coding-system-function)
592 (funcall select-safe-coding-system-function
593 (point-min) (point-max) coding)
594 coding)))
595 \f
596 ;;; Language support stuff.
597
598 (defvar language-info-alist nil
599 "Alist of language environment definitions.
600 Each element looks like:
601 (LANGUAGE-NAME . ((KEY . INFO) ...))
602 where LANGUAGE-NAME is a string, the name of the language environment,
603 KEY is a symbol denoting the kind of information, and
604 INFO is the data associated with KEY.
605 Meaningful values for KEY include
606
607 documentation value is documentation of what this language environment
608 is meant for, and how to use it.
609 charset value is a list of the character sets used by this
610 language environment.
611 sample-text value is one line of text,
612 written using those character sets,
613 appropriate for this language environment.
614 setup-function value is a function to call to switch to this
615 language environment.
616 exit-function value is a function to call to leave this
617 language environment.
618 coding-system value is a list of coding systems that are good
619 for saving text written in this language environment.
620 This list serves as suggestions to the user;
621 in effect, as a kind of documentation.
622 coding-priority value is a list of coding systems for this language
623 environment, in order of decreasing priority.
624 This is used to set up the coding system priority
625 list when you switch to this language environment.
626 nonascii-translation
627 value is a translation table to be set in the
628 variable `nonascii-translation-table' in this
629 language environment, or a character set from
630 which `nonascii-insert-offset' is calculated.
631 input-method value is a default input method for this language
632 environment.
633 features value is a list of features requested in this
634 language environment.
635
636 The following keys take effect only when multibyte characters are
637 globally disabled, i.e. the value of `default-enable-multibyte-characters'
638 is nil.
639
640 unibyte-syntax value is a library name to load to set
641 unibyte 8-bit character syntaxes for this
642 language environment.
643
644 unibyte-display value is a coding system to encode characters
645 for the terminal. Characters in the range
646 of 160 to 255 display not as octal escapes,
647 but as non-ASCII characters in this language
648 environment.")
649
650 (defun get-language-info (lang-env key)
651 "Return information listed under KEY for language environment LANG-ENV.
652 KEY is a symbol denoting the kind of information.
653 For a list of useful values for KEY and their meanings,
654 see `language-info-alist'."
655 (if (symbolp lang-env)
656 (setq lang-env (symbol-name lang-env)))
657 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
658 (if lang-slot
659 (cdr (assq key (cdr lang-slot))))))
660
661 (defun set-language-info (lang-env key info)
662 "Modify part of the definition of language environment LANG-ENV.
663 Specifically, this stores the information INFO under KEY
664 in the definition of this language environment.
665 KEY is a symbol denoting the kind of information.
666 INFO is the value for that information.
667
668 For a list of useful values for KEY and their meanings,
669 see `language-info-alist'."
670 (if (symbolp lang-env)
671 (setq lang-env (symbol-name lang-env)))
672 (let (lang-slot key-slot)
673 (setq lang-slot (assoc lang-env language-info-alist))
674 (if (null lang-slot) ; If no slot for the language, add it.
675 (setq lang-slot (list lang-env)
676 language-info-alist (cons lang-slot language-info-alist)))
677 (setq key-slot (assq key lang-slot))
678 (if (null key-slot) ; If no slot for the key, add it.
679 (progn
680 (setq key-slot (list key))
681 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
682 (setcdr key-slot info)))
683
684 (defun set-language-info-alist (lang-env alist &optional parents)
685 "Store ALIST as the definition of language environment LANG-ENV.
686 ALIST is an alist of KEY and INFO values. See the documentation of
687 `set-language-info' for the meanings of KEY and INFO.
688
689 Optional arg PARENTS is a list of parent menu names; it specifies
690 where to put this language environment in the
691 Describe Language Environment and Set Language Environment menus.
692 For example, (\"European\") means to put this language environment
693 in the European submenu in each of those two menus."
694 (if (symbolp lang-env)
695 (setq lang-env (symbol-name lang-env)))
696 (let ((describe-map describe-language-environment-map)
697 (setup-map setup-language-environment-map))
698 (if parents
699 (let ((l parents)
700 map parent-symbol parent)
701 (while l
702 (if (symbolp (setq parent-symbol (car l)))
703 (setq parent (symbol-name parent))
704 (setq parent parent-symbol parent-symbol (intern parent)))
705 (setq map (lookup-key describe-map (vector parent-symbol)))
706 (if (not map)
707 (progn
708 (setq map (intern (format "describe-%s-environment-map"
709 (downcase parent))))
710 (define-prefix-command map)
711 (define-key-after describe-map (vector parent-symbol)
712 (cons parent map) t)))
713 (setq describe-map (symbol-value map))
714 (setq map (lookup-key setup-map (vector parent-symbol)))
715 (if (not map)
716 (progn
717 (setq map (intern (format "setup-%s-environment-map"
718 (downcase parent))))
719 (define-prefix-command map)
720 (define-key-after setup-map (vector parent-symbol)
721 (cons parent map) t)))
722 (setq setup-map (symbol-value map))
723 (setq l (cdr l)))))
724
725 ;; Set up menu items for this language env.
726 (let ((doc (assq 'documentation alist)))
727 (when doc
728 (define-key-after describe-map (vector (intern lang-env))
729 (cons lang-env 'describe-specified-language-support) t)))
730 (define-key-after setup-map (vector (intern lang-env))
731 (cons lang-env 'setup-specified-language-environment) t)
732
733 (while alist
734 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
735 (setq alist (cdr alist)))))
736
737 (defun read-language-name (key prompt &optional default)
738 "Read a language environment name which has information for KEY.
739 If KEY is nil, read any language environment.
740 Prompt with PROMPT. DEFAULT is the default choice of language environment.
741 This returns a language environment name as a string."
742 (let* ((completion-ignore-case t)
743 (name (completing-read prompt
744 language-info-alist
745 (and key
746 (function (lambda (elm) (assq key elm))))
747 t nil nil default)))
748 (if (and (> (length name) 0)
749 (or (not key)
750 (get-language-info name key)))
751 name)))
752 \f
753 ;;; Multilingual input methods.
754
755 (defconst leim-list-file-name "leim-list.el"
756 "Name of LEIM list file.
757 This file contains a list of libraries of Emacs input methods (LEIM)
758 in the format of Lisp expression for registering each input method.
759 Emacs loads this file at startup time.")
760
761 (defvar leim-list-header (format
762 ";;; %s -- list of LEIM (Library of Emacs Input Method)
763 ;;
764 ;; This file contains a list of LEIM (Library of Emacs Input Method)
765 ;; in the same directory as this file. Loading this file registers
766 ;; the whole input methods in Emacs.
767 ;;
768 ;; Each entry has the form:
769 ;; (register-input-method
770 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
771 ;; TITLE DESCRIPTION
772 ;; ARG ...)
773 ;; See the function `register-input-method' for the meanings of arguments.
774 ;;
775 ;; If this directory is included in load-path, Emacs automatically
776 ;; loads this file at startup time.
777
778 "
779 leim-list-file-name)
780 "Header to be inserted in LEIM list file.")
781
782 (defvar leim-list-entry-regexp "^(register-input-method"
783 "Regexp matching head of each entry in LEIM list file.
784 See also the variable `leim-list-header'")
785
786 (defvar update-leim-list-functions
787 '(quail-update-leim-list-file)
788 "List of functions to call to update LEIM list file.
789 Each function is called with one arg, LEIM directory name.")
790
791 (defun update-leim-list-file (&rest dirs)
792 "Update LEIM list file in directories DIRS."
793 (let ((functions update-leim-list-functions))
794 (while functions
795 (apply (car functions) dirs)
796 (setq functions (cdr functions)))))
797
798 (defvar current-input-method nil
799 "The current input method for multilingual text.
800 If nil, that means no input method is activated now.")
801 (make-variable-buffer-local 'current-input-method)
802 (put 'current-input-method 'permanent-local t)
803
804 (defvar current-input-method-title nil
805 "Title string of the current input method shown in mode line.")
806 (make-variable-buffer-local 'current-input-method-title)
807 (put 'current-input-method-title 'permanent-local t)
808
809 (defcustom default-input-method nil
810 "*Default input method for multilingual text (a string).
811 This is the input method activated automatically by the command
812 `toggle-input-method' (\\[toggle-input-method])."
813 :group 'mule
814 :type '(choice (const nil) string))
815
816 (put 'input-method-function 'permanent-local t)
817
818 (defvar input-method-history nil
819 "History list for some commands that read input methods.")
820 (make-variable-buffer-local 'input-method-history)
821 (put 'input-method-history 'permanent-local t)
822
823 (defvar inactivate-current-input-method-function nil
824 "Function to call for inactivating the current input method.
825 Every input method should set this to an appropriate value when activated.
826 This function is called with no argument.
827
828 This function should never change the value of `current-input-method'.
829 It is set to nil by the function `inactivate-input-method'.")
830 (make-variable-buffer-local 'inactivate-current-input-method-function)
831 (put 'inactivate-current-input-method-function 'permanent-local t)
832
833 (defvar describe-current-input-method-function nil
834 "Function to call for describing the current input method.
835 This function is called with no argument.")
836 (make-variable-buffer-local 'describe-current-input-method-function)
837 (put 'describe-current-input-method-function 'permanent-local t)
838
839 (defvar input-method-alist nil
840 "Alist of input method names vs how to use them.
841 Each element has the form:
842 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
843 See the function `register-input-method' for the meanings of the elements.")
844
845 (defun register-input-method (input-method lang-env &rest args)
846 "Register INPUT-METHOD as an input method for language environment ENV.
847 INPUT-METHOD and LANG-ENV are symbols or strings.
848
849 The remaining arguments are:
850 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
851 ACTIVATE-FUNC is a function to call to activate this method.
852 TITLE is a string to show in the mode line when this method is active.
853 DESCRIPTION is a string describing this method and what it is good for.
854 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
855 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
856
857 This function is mainly used in the file \"leim-list.el\" which is
858 created at building time of emacs, registering all quail input methods
859 contained in the emacs distribution.
860
861 In case you want to register a new quail input method by yourself, be
862 careful to use the same input method title as given in the third
863 parameter of `quail-define-package' (if the values are different, the
864 string specified in this function takes precedence).
865
866 The commands `describe-input-method' and `list-input-methods' need
867 this duplicated values to show some information about input methods
868 without loading the affected quail packages."
869 (if (symbolp lang-env)
870 (setq lang-env (symbol-name lang-env)))
871 (if (symbolp input-method)
872 (setq input-method (symbol-name input-method)))
873 (let ((info (cons lang-env args))
874 (slot (assoc input-method input-method-alist)))
875 (if slot
876 (setcdr slot info)
877 (setq slot (cons input-method info))
878 (setq input-method-alist (cons slot input-method-alist)))))
879
880 (defun read-input-method-name (prompt &optional default inhibit-null)
881 "Read a name of input method from a minibuffer prompting with PROMPT.
882 If DEFAULT is non-nil, use that as the default,
883 and substitute it into PROMPT at the first `%s'.
884 If INHIBIT-NULL is non-nil, null input signals an error.
885
886 The return value is a string."
887 (if default
888 (setq prompt (format prompt default)))
889 (let* ((completion-ignore-case t)
890 ;; This binding is necessary because input-method-history is
891 ;; buffer local.
892 (input-method (completing-read prompt input-method-alist
893 nil t nil 'input-method-history
894 default)))
895 (if (> (length input-method) 0)
896 input-method
897 (if inhibit-null
898 (error "No valid input method is specified")))))
899
900 (defun activate-input-method (input-method)
901 "Switch to input method INPUT-METHOD for the current buffer.
902 If some other input method is already active, turn it off first.
903 If INPUT-METHOD is nil, deactivate any current input method."
904 (if (and input-method (symbolp input-method))
905 (setq input-method (symbol-name input-method)))
906 (if (and current-input-method
907 (not (string= current-input-method input-method)))
908 (inactivate-input-method))
909 (unless (or current-input-method (null input-method))
910 (let ((slot (assoc input-method input-method-alist)))
911 (if (null slot)
912 (error "Can't activate input method `%s'" input-method))
913 (let ((func (nth 2 slot)))
914 (if (functionp func)
915 (apply (nth 2 slot) input-method (nthcdr 5 slot))
916 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
917 (progn
918 (require (cdr func))
919 (apply (car func) input-method (nthcdr 5 slot)))
920 (error "Can't activate input method `%s'" input-method))))
921 (setq current-input-method input-method)
922 (setq current-input-method-title (nth 3 slot))
923 (unwind-protect
924 (run-hooks 'input-method-activate-hook)
925 (force-mode-line-update)))))
926
927 (defun inactivate-input-method ()
928 "Turn off the current input method."
929 (when current-input-method
930 (if input-method-history
931 (unless (string= current-input-method (car input-method-history))
932 (setq input-method-history
933 (cons current-input-method
934 (delete current-input-method input-method-history))))
935 (setq input-method-history (list current-input-method)))
936 (unwind-protect
937 (funcall inactivate-current-input-method-function)
938 (unwind-protect
939 (run-hooks 'input-method-inactivate-hook)
940 (setq current-input-method nil
941 current-input-method-title nil)
942 (force-mode-line-update)))))
943
944 (defun set-input-method (input-method)
945 "Select and activate input method INPUT-METHOD for the current buffer.
946 This also sets the default input method to the one you specify."
947 (interactive
948 (let* ((default (or (car input-method-history) default-input-method)))
949 (list (read-input-method-name
950 (if default "Select input method (default %s): " "Select input method: ")
951 default t))))
952 (activate-input-method input-method)
953 (setq default-input-method input-method))
954
955 (defun toggle-input-method (&optional arg)
956 "Turn on or off a multilingual text input method for the current buffer.
957
958 With no prefix argument, if an input method is currently activated,
959 turn it off. Otherwise, activate an input method -- the one most
960 recently used, or the one specified in `default-input-method', or
961 the one read from the minibuffer.
962
963 With a prefix argument, read an input method from the minibuffer and
964 turn it on.
965
966 The default is to use the most recent input method specified
967 \(not including the currently active input method, if any)."
968 (interactive "P")
969 (if (and current-input-method (not arg))
970 (inactivate-input-method)
971 (let ((default (or (car input-method-history) default-input-method)))
972 (if (and arg default (equal current-input-method default)
973 (> (length input-method-history) 1))
974 (setq default (nth 1 input-method-history)))
975 (activate-input-method
976 (if (or arg (not default))
977 (progn
978 (read-input-method-name
979 (if default "Input method (default %s): " "Input method: " )
980 default t))
981 default))
982 (or default-input-method
983 (setq default-input-method current-input-method)))))
984
985 (defun describe-input-method (input-method)
986 "Describe input method INPUT-METHOD."
987 (interactive
988 (list (read-input-method-name
989 "Describe input method (default, current choice): ")))
990 (if (and input-method (symbolp input-method))
991 (setq input-method (symbol-name input-method)))
992 (if (null input-method)
993 (describe-current-input-method)
994 (with-output-to-temp-buffer "*Help*"
995 (let ((elt (assoc input-method input-method-alist)))
996 (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n"
997 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
998
999 (defun describe-current-input-method ()
1000 "Describe the input method currently in use."
1001 (if current-input-method
1002 (if (and (symbolp describe-current-input-method-function)
1003 (fboundp describe-current-input-method-function))
1004 (funcall describe-current-input-method-function)
1005 (message "No way to describe the current input method `%s'"
1006 current-input-method)
1007 (ding))
1008 (error "No input method is activated now")))
1009
1010 (defun read-multilingual-string (prompt &optional initial-input input-method)
1011 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1012 The input method selected last time is activated in minibuffer.
1013 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1014 initially.
1015 Optional 3rd argument INPUT-METHOD specifies the input method
1016 to be activated instead of the one selected last time. It is a symbol
1017 or a string."
1018 (setq input-method
1019 (or input-method
1020 current-input-method
1021 default-input-method
1022 (read-input-method-name "Input method: " nil t)))
1023 (if (and input-method (symbolp input-method))
1024 (setq input-method (symbol-name input-method)))
1025 (let ((prev-input-method current-input-method))
1026 (unwind-protect
1027 (progn
1028 (activate-input-method input-method)
1029 (read-string prompt initial-input nil nil t))
1030 (activate-input-method prev-input-method))))
1031
1032 ;; Variables to control behavior of input methods. All input methods
1033 ;; should react to these variables.
1034
1035 (defcustom input-method-verbose-flag 'default
1036 "*A flag to control extra guidance given by input methods.
1037 The value should be nil, t, `complex-only', or `default'.
1038
1039 The extra guidance is done by showing list of available keys in echo
1040 area. When you use the input method in the minibuffer, the guidance
1041 is shown at the bottom short window (split from the existing window).
1042
1043 If the value is t, extra guidance is always given, if the value is
1044 nil, extra guidance is always suppressed.
1045
1046 If the value is `complex-only', only complex input methods such as
1047 `chinese-py' and `japanese' give extra guidance.
1048
1049 If the value is `default', complex input methods always give extra
1050 guidance, but simple input methods give it only when you are not in
1051 the minibuffer.
1052
1053 See also the variable `input-method-highlight-flag'."
1054 :type '(choice (const t) (const nil) (const complex-only) (const default))
1055 :group 'mule)
1056
1057 (defcustom input-method-highlight-flag t
1058 "*If this flag is non-nil, input methods highlight partially-entered text.
1059 For instance, while you are in the middle of a Quail input method sequence,
1060 the text inserted so far is temporarily underlined.
1061 The underlining goes away when you finish or abort the input method sequence.
1062 See also the variable `input-method-verbose-flag'."
1063 :type 'boolean
1064 :group 'mule)
1065
1066 (defvar input-method-activate-hook nil
1067 "Normal hook run just after an input method is activated.
1068
1069 The variable `current-input-method' keeps the input method name
1070 just activated.")
1071
1072 (defvar input-method-inactivate-hook nil
1073 "Normal hook run just after an input method is inactivated.
1074
1075 The variable `current-input-method' still keeps the input method name
1076 just inactivated.")
1077
1078 (defvar input-method-after-insert-chunk-hook nil
1079 "Normal hook run just after an input method insert some chunk of text.")
1080
1081 (defvar input-method-exit-on-first-char nil
1082 "This flag controls a timing when an input method returns.
1083 Usually, the input method does not return while there's a possibility
1084 that it may find a different translation if a user types another key.
1085 But, it this flag is non-nil, the input method returns as soon as
1086 the current key sequence gets long enough to have some valid translation.")
1087
1088 (defvar input-method-use-echo-area nil
1089 "This flag controls how an input method shows an intermediate key sequence.
1090 Usually, the input method inserts the intermediate key sequence,
1091 or candidate translations corresponding to the sequence,
1092 at point in the current buffer.
1093 But, if this flag is non-nil, it displays them in echo area instead.")
1094
1095 (defvar input-method-exit-on-invalid-key nil
1096 "This flag controls the behaviour of an input method on invalid key input.
1097 Usually, when a user types a key which doesn't start any character
1098 handled by the input method, the key is handled by turning off the
1099 input method temporarily. After that key, the input method is re-enabled.
1100 But, if this flag is non-nil, the input method is never back on.")
1101
1102 \f
1103 (defvar set-language-environment-hook nil
1104 "Normal hook run after some language environment is set.
1105
1106 When you set some hook function here, that effect usually should not
1107 be inherited to another language environment. So, you had better set
1108 another function in `exit-language-environment-hook' (which see) to
1109 cancel the effect.")
1110
1111 (defvar exit-language-environment-hook nil
1112 "Normal hook run after exiting from some language environment.
1113 When this hook is run, the variable `current-language-environment'
1114 is still bound to the language environment being exited.
1115
1116 This hook is mainly used for canceling the effect of
1117 `set-language-environment-hook' (which-see).")
1118
1119 (put 'setup-specified-language-environment 'apropos-inhibit t)
1120
1121 (defun setup-specified-language-environment ()
1122 "Switch to a specified language environment."
1123 (interactive)
1124 (let (language-name)
1125 (if (and (symbolp last-command-event)
1126 (or (not (eq last-command-event 'Default))
1127 (setq last-command-event 'English))
1128 (setq language-name (symbol-name last-command-event)))
1129 (set-language-environment language-name)
1130 (error "Bogus calling sequence"))))
1131
1132 (defcustom current-language-environment "English"
1133 "The last language environment specified with `set-language-environment'.
1134 This variable should be set only with \\[customize], which is equivalent
1135 to using the function `set-language-environment'."
1136 :link '(custom-manual "(emacs)Language Environments")
1137 :set (lambda (symbol value) (set-language-environment value))
1138 :get (lambda (x)
1139 (or (car-safe (assoc-ignore-case
1140 (if (symbolp current-language-environment)
1141 (symbol-name current-language-environment)
1142 current-language-environment)
1143 language-info-alist))
1144 "English"))
1145 :type (cons 'choice (mapcar (lambda (lang)
1146 (list 'const (car lang)))
1147 language-info-alist))
1148 :initialize 'custom-initialize-default
1149 :group 'mule
1150 :type 'string)
1151
1152 (defun reset-language-environment ()
1153 "Reset multilingual environment of Emacs to the default status.
1154
1155 The default status is as follows:
1156
1157 The default value of buffer-file-coding-system is nil.
1158 The default coding system for process I/O is nil.
1159 The default value for the command `set-terminal-coding-system' is nil.
1160 The default value for the command `set-keyboard-coding-system' is nil.
1161
1162 The order of priorities of coding categories and the coding system
1163 bound to each category are as follows
1164 coding category coding system
1165 --------------------------------------------------
1166 coding-category-iso-8-2 iso-latin-1
1167 coding-category-iso-8-1 iso-latin-1
1168 coding-category-iso-7-tight iso-2022-jp
1169 coding-category-iso-7 iso-2022-7bit
1170 coding-category-iso-7-else iso-2022-7bit-lock
1171 coding-category-iso-8-else iso-2022-8bit-ss2
1172 coding-category-emacs-mule emacs-mule
1173 coding-category-raw-text raw-text
1174 coding-category-sjis japanese-shift-jis
1175 coding-category-big5 chinese-big5
1176 coding-category-ccl nil
1177 coding-category-binary no-conversion
1178 "
1179 (interactive)
1180 ;; This function formerly set default-enable-multibyte-characters to t,
1181 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1182
1183 (setq coding-category-iso-7-tight 'iso-2022-jp
1184 coding-category-iso-7 'iso-2022-7bit
1185 coding-category-iso-8-1 'iso-latin-1
1186 coding-category-iso-8-2 'iso-latin-1
1187 coding-category-iso-7-else 'iso-2022-7bit-lock
1188 coding-category-iso-8-else 'iso-2022-8bit-ss2
1189 coding-category-emacs-mule 'emacs-mule
1190 coding-category-raw-text 'raw-text
1191 coding-category-sjis 'japanese-shift-jis
1192 coding-category-big5 'chinese-big5
1193 coding-category-ccl nil
1194 coding-category-binary 'no-conversion)
1195
1196 (set-coding-priority
1197 '(coding-category-iso-8-1
1198 coding-category-iso-8-2
1199 coding-category-iso-7-tight
1200 coding-category-iso-7
1201 coding-category-iso-7-else
1202 coding-category-iso-8-else
1203 coding-category-emacs-mule
1204 coding-category-raw-text
1205 coding-category-sjis
1206 coding-category-big5
1207 coding-category-ccl
1208 coding-category-binary))
1209
1210 (update-coding-systems-internal)
1211
1212 (set-default-coding-systems nil)
1213 ;; Don't alter the terminal and keyboard coding systems here.
1214 ;; The terminal still supports the same coding system
1215 ;; that it supported a minute ago.
1216 ;;; (set-terminal-coding-system-internal nil)
1217 ;;; (set-keyboard-coding-system-internal nil)
1218
1219 (setq nonascii-translation-table nil
1220 nonascii-insert-offset 0))
1221
1222 (defun set-display-table-and-terminal-coding-system (language-name)
1223 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1224 (let ((coding (get-language-info language-name 'unibyte-display)))
1225 (if coding
1226 (standard-display-european-internal)
1227 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1228 (aset standard-display-table 146 nil))
1229 (or (eq window-system 'pc)
1230 (set-terminal-coding-system coding))))
1231
1232 (defun set-language-environment (language-name)
1233 "Set up multi-lingual environment for using LANGUAGE-NAME.
1234 This sets the coding system priority and the default input method
1235 and sometimes other things. LANGUAGE-NAME should be a string
1236 which is the name of a language environment. For example, \"Latin-1\"
1237 specifies the character set for the major languages of Western Europe."
1238 (interactive (list (read-language-name
1239 nil
1240 "Set language environment (default, English): ")))
1241 (if language-name
1242 (if (symbolp language-name)
1243 (setq language-name (symbol-name language-name)))
1244 (setq language-name "English"))
1245 (or (assoc-ignore-case language-name language-info-alist)
1246 (error "Language environment not defined: %S" language-name))
1247 (if current-language-environment
1248 (let ((func (get-language-info current-language-environment
1249 'exit-function)))
1250 (run-hooks 'exit-language-environment-hook)
1251 (if (fboundp func) (funcall func))))
1252 (let ((default-eol-type (coding-system-eol-type
1253 default-buffer-file-coding-system)))
1254 (reset-language-environment)
1255
1256 (setq current-language-environment language-name)
1257 (set-language-environment-coding-systems language-name default-eol-type))
1258 (let ((input-method (get-language-info language-name 'input-method)))
1259 (when input-method
1260 (setq default-input-method input-method)
1261 (if input-method-history
1262 (setq input-method-history
1263 (cons input-method
1264 (delete input-method input-method-history))))))
1265 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1266 (dos-table
1267 (if (eq window-system 'pc)
1268 (intern
1269 (concat "cp" dos-codepage "-nonascii-translation-table")))))
1270 (cond
1271 ((char-table-p nonascii)
1272 (setq nonascii-translation-table nonascii))
1273 ((and (eq window-system 'pc) (boundp dos-table))
1274 ;; DOS terminals' default is to use a special non-ASCII translation
1275 ;; table as appropriate for the installed codepage.
1276 (setq nonascii-translation-table (symbol-value dos-table)))
1277 ((charsetp nonascii)
1278 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1279
1280 (setq charset-origin-alist
1281 (get-language-info language-name 'charset-origin-alist))
1282
1283 ;; Unibyte setups if necessary.
1284 (unless default-enable-multibyte-characters
1285 ;; Syntax and case table.
1286 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1287 (if syntax
1288 (let ((set-case-syntax-set-multibyte nil))
1289 (load syntax nil t))
1290 ;; No information for syntax and case. Reset to the defaults.
1291 (let ((syntax-table (standard-syntax-table))
1292 (case-table (standard-case-table))
1293 (ch (if (eq window-system 'pc) 128 160)))
1294 (while (< ch 256)
1295 (modify-syntax-entry ch " " syntax-table)
1296 (aset case-table ch ch)
1297 (setq ch (1+ ch)))
1298 (set-char-table-extra-slot case-table 0 nil)
1299 (set-char-table-extra-slot case-table 1 nil)
1300 (set-char-table-extra-slot case-table 2 nil))
1301 (set-standard-case-table (standard-case-table))
1302 (let ((list (buffer-list)))
1303 (while list
1304 (with-current-buffer (car list)
1305 (set-case-table (standard-case-table)))
1306 (setq list (cdr list))))))
1307 (set-display-table-and-terminal-coding-system language-name))
1308
1309 (let ((required-features (get-language-info language-name 'features)))
1310 (while required-features
1311 (require (car required-features))
1312 (setq required-features (cdr required-features))))
1313 (let ((func (get-language-info language-name 'setup-function)))
1314 (if (fboundp func)
1315 (funcall func)))
1316 (run-hooks 'set-language-environment-hook)
1317 (force-mode-line-update t))
1318
1319 (defun standard-display-european-internal ()
1320 ;; Actually set up direct output of non-ASCII characters.
1321 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1322 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1323 ;; the native font, and codes 160 and 146 stand for something very
1324 ;; different there.
1325 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1326 (progn
1327 ;; Make non-line-break space display as a plain space.
1328 ;; Most X fonts do the wrong thing for code 160.
1329 (aset standard-display-table 160 [32])
1330 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1331 ;; don't contain a character at that position. Map it to the ASCII
1332 ;; apostrophe.
1333 (aset standard-display-table 146 [39]))))
1334
1335 (defun set-language-environment-coding-systems (language-name
1336 &optional eol-type)
1337 "Do various coding system setups for language environment LANGUAGE-NAME.
1338
1339 The optional arg EOL-TYPE specifies the eol-type of the default value
1340 of buffer-file-coding-system set by this function."
1341 (let* ((priority (get-language-info language-name 'coding-priority))
1342 (default-coding (car priority)))
1343 (if priority
1344 (let ((categories (mapcar 'coding-system-category priority)))
1345 (set-default-coding-systems
1346 (if (memq eol-type '(0 1 2 unix dos mac))
1347 (coding-system-change-eol-conversion default-coding eol-type)
1348 default-coding))
1349 (setq default-sendmail-coding-system default-coding)
1350 (set-coding-priority categories)
1351 (while priority
1352 (set (car categories) (car priority))
1353 (setq priority (cdr priority) categories (cdr categories)))
1354 (update-coding-systems-internal)))))
1355
1356 ;; Print all arguments with `princ', then print "\n".
1357 (defsubst princ-list (&rest args)
1358 (while args (princ (car args)) (setq args (cdr args)))
1359 (princ "\n"))
1360
1361 (put 'describe-specified-language-support 'apropos-inhibit t)
1362
1363 ;; Print a language specific information such as input methods,
1364 ;; charsets, and coding systems. This function is intended to be
1365 ;; called from the menu:
1366 ;; [menu-bar mule describe-language-environment LANGUAGE]
1367 ;; and should not run it by `M-x describe-current-input-method-function'.
1368 (defun describe-specified-language-support ()
1369 "Describe how Emacs supports the specified language environment."
1370 (interactive)
1371 (let (language-name)
1372 (if (not (and (symbolp last-command-event)
1373 (setq language-name (symbol-name last-command-event))))
1374 (error "Bogus calling sequence"))
1375 (describe-language-environment language-name)))
1376
1377 (defun describe-language-environment (language-name)
1378 "Describe how Emacs supports language environment LANGUAGE-NAME."
1379 (interactive
1380 (list (read-language-name
1381 'documentation
1382 "Describe language environment (default, current choice): ")))
1383 (if (null language-name)
1384 (setq language-name current-language-environment))
1385 (if (or (null language-name)
1386 (null (get-language-info language-name 'documentation)))
1387 (error "No documentation for the specified language"))
1388 (if (symbolp language-name)
1389 (setq language-name (symbol-name language-name)))
1390 (let ((doc (get-language-info language-name 'documentation)))
1391 (with-output-to-temp-buffer "*Help*"
1392 (princ-list language-name " language environment" "\n")
1393 (if (stringp doc)
1394 (progn
1395 (princ-list doc)
1396 (terpri)))
1397 (let ((str (get-language-info language-name 'sample-text)))
1398 (if (stringp str)
1399 (progn
1400 (princ "Sample text:\n")
1401 (princ-list " " str)
1402 (terpri))))
1403 (let ((input-method (get-language-info language-name 'input-method))
1404 (l (copy-sequence input-method-alist)))
1405 (princ "Input methods")
1406 (when input-method
1407 (princ (format " (default, %s)" input-method))
1408 (setq input-method (assoc input-method input-method-alist))
1409 (setq l (cons input-method (delete input-method l))))
1410 (princ ":\n")
1411 (while l
1412 (if (string= language-name (nth 1 (car l)))
1413 (princ-list " " (car (car l))
1414 (format " (`%s' in mode line)" (nth 3 (car l)))))
1415 (setq l (cdr l))))
1416 (terpri)
1417 (princ "Character sets:\n")
1418 (let ((l (get-language-info language-name 'charset)))
1419 (if (null l)
1420 (princ-list " nothing specific to " language-name)
1421 (while l
1422 (princ-list " " (car l) ": "
1423 (charset-description (car l)))
1424 (setq l (cdr l)))))
1425 (terpri)
1426 (princ "Coding systems:\n")
1427 (let ((l (get-language-info language-name 'coding-system)))
1428 (if (null l)
1429 (princ-list " nothing specific to " language-name)
1430 (while l
1431 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1432 (car l)
1433 (coding-system-mnemonic (car l))
1434 (coding-system-doc-string (car l))))
1435 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1436 (when aliases
1437 (princ "\t")
1438 (princ (cons 'alias: (cdr aliases)))
1439 (terpri)))
1440 (setq l (cdr l))))))))
1441 \f
1442 ;;; Locales.
1443
1444 (defvar locale-translation-file-name
1445 (let ((files '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
1446 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
1447 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
1448 ;;
1449 ;; The following name appears after the X-related names above,
1450 ;; since the X-related names are what X actually uses.
1451 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
1452 )))
1453 (while (and files (not (file-exists-p (car files))))
1454 (setq files (cdr files)))
1455 (car files))
1456 "*File name for the system's file of locale-name aliases, or nil if none.")
1457
1458 (defvar locale-language-names
1459 '(
1460 ;; UTF-8 is not yet implemented.
1461 ;; Put this first, so that e.g. "ko.UTF-8" does not match "ko" below.
1462 (".*[._]utf" . nil)
1463
1464 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1465 ;; as specified in the Single Unix Spec, Version 2.
1466 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1467 ;; with additions from ISO 639/RA Newsletter No.1/1989;
1468 ;; see Internet RFC 2165 (1997-06).
1469 ;; TERRITORY is a country code taken from ISO 3166.
1470 ;; CODESET and MODIFIER are implementation-dependent.
1471 ;;
1472 ; aa Afar
1473 ; ab Abkhazian
1474 ("af" . "Latin-3") ; Afrikaans
1475 ("am" . "Ethiopic") ; Amharic
1476 ; ar Arabic
1477 ; as Assamese
1478 ; ay Aymara
1479 ; az Azerbaijani
1480 ; ba Bashkir
1481 ("be" . "Cyrillic-ISO") ; Byelorussian
1482 ("bg" . "Cyrillic-ISO") ; Bulgarian
1483 ; bh Bihari
1484 ; bi Bislama
1485 ; bn Bengali, Bangla
1486 ("bo" . "Tibetan")
1487 ("br" . "Latin-1") ; Breton
1488 ("ca" . "Latin-1") ; Catalan
1489 ; co Corsican
1490 ("cs" . "Czech")
1491 ; cy Welsh
1492 ("da" . "Latin-1") ; Danish
1493 ("de" . "German")
1494 ; dz Bhutani
1495 ("el" . "Greek")
1496 ("en" . "English")
1497 ("eo" . "Latin-3") ; Esperanto
1498 ("es" . "Latin-1") ; Spanish
1499 ("et" . "Latin-4") ; Estonian
1500 ("eu" . "Latin-1") ; Basque
1501 ; fa Persian
1502 ("fi" . "Latin-1") ; Finnish
1503 ; fj Fiji
1504 ("fo" . "Latin-1") ; Faroese
1505 ("fr" . "Latin-1") ; French
1506 ("fy" . "Latin-1") ; Frisian
1507 ("ga" . "Latin-1") ; Irish
1508 ; gd Scots Gaelic
1509 ("gl" . "Latin-3") ; Galician
1510 ; gn Guarani
1511 ; gu Gujarati
1512 ; ha Hausa
1513 ("he" . "Hebrew")
1514 ("hi" . "Devanagari") ; Hindi
1515 ("hr" . "Latin-2") ; Croatian
1516 ("hu" . "Latin-2") ; Hungarian
1517 ; hy Armenian
1518 ; ia Interlingua
1519 ("id" . "Latin-1") ; Indonesian
1520 ; ie Interlingue
1521 ; ik Inupiak
1522 ("is" . "Latin-1") ; Icelandic
1523 ("it" . "Latin-1") ; Italian
1524 ; iu Inuktitut
1525 ("ja" . "Japanese")
1526 ; jw Javanese
1527 ; ka Georgian
1528 ; kk Kazakh
1529 ("kl" . "Latin-4") ; Greenlandic
1530 ; km Cambodian
1531 ; kn Kannada
1532 ("ko" . "Korean")
1533 ; ks Kashmiri
1534 ; ku Kurdish
1535 ; ky Kirghiz
1536 ("la" . "Latin-1") ; Latin
1537 ; ln Lingala
1538 ("lo" . "Lao") ; Laothian
1539 ("lt" . "Latin-4") ; Lithuanian
1540 ("lv" . "Latin-4") ; Latvian, Lettish
1541 ; mg Malagasy
1542 ; mi Maori
1543 ("mk" . "Cyrillic-ISO") ; Macedonian
1544 ; ml Malayalam
1545 ; mn Mongolian
1546 ; mo Moldavian
1547 ("mr" . "Devanagari") ; Marathi
1548 ; ms Malay
1549 ("mt" . "Latin-3") ; Maltese
1550 ; my Burmese
1551 ; na Nauru
1552 ("ne" . "Devanagari") ; Nepali
1553 ("nl" . "Latin-1") ; Dutch
1554 ("no" . "Latin-1") ; Norwegian
1555 ; oc Occitan
1556 ; om (Afan) Oromo
1557 ; or Oriya
1558 ; pa Punjabi
1559 ("pl" . "Latin-2") ; Polish
1560 ; ps Pashto, Pushto
1561 ("pt" . "Latin-1") ; Portuguese
1562 ; qu Quechua
1563 ("rm" . "Latin-1") ; Rhaeto-Romance
1564 ; rn Kirundi
1565 ("ro" . "Romanian")
1566 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
1567 ("ru" . "Cyrillic-ISO") ; Russian
1568 ; rw Kinyarwanda
1569 ("sa" . "Devanagari") ; Sanskrit
1570 ; sd Sindhi
1571 ; sg Sangho
1572 ("sh" . "Latin-2") ; Serbo-Croatian
1573 ; si Sinhalese
1574 ("sk" . "Slovak")
1575 ("sl" . "Slovenian")
1576 ; sm Samoan
1577 ; sn Shona
1578 ; so Somali
1579 ("sq" . "Latin-2") ; Albanian
1580 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
1581 ; ss Siswati
1582 ; st Sesotho
1583 ; su Sundanese
1584 ("sv" . "Latin-1") ; Swedish
1585 ("sw" . "Latin-1") ; Swahili
1586 ; ta Tamil
1587 ; te Telugu
1588 ; tg Tajik
1589 ("th" . "Thai")
1590 ; ti Tigrinya
1591 ; tk Turkmen
1592 ; tl Tagalog
1593 ; tn Setswana
1594 ; to Tonga
1595 ("tr" . "Latin-5") ; Turkish
1596 ; ts Tsonga
1597 ; tt Tatar
1598 ; tw Twi
1599 ; ug Uighur
1600 ("uk" . "Cyrillic-ISO") ; Ukrainian
1601 ; ur Urdu
1602 ; uz Uzbek
1603 ("vi" . "Vietnamese")
1604 ; vo Volapuk
1605 ; wo Wolof
1606 ; xh Xhosa
1607 ; yi Yiddish
1608 ; yo Yoruba
1609 ; za Zhuang
1610 ("zh.*[._]big5" . "Chinese-BIG5")
1611 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
1612 ("zh_tw" . "Chinese-CNS")
1613 ("zh" . "Chinese-GB")
1614 ; zu Zulu
1615
1616 ;; ISO standard locales
1617 ("c$" . "ASCII")
1618 ("posix$" . "ASCII")
1619
1620 ;; generic ISO 8859 locales
1621 (".*8859[-_]?1" . "Latin-1")
1622 (".*8859[-_]?2" . "Latin-2")
1623 (".*8859[-_]?3" . "Latin-3")
1624 (".*8859[-_]?4" . "Latin-4")
1625 (".*8859[-_]?9" . "Latin-5")
1626 (".*8859[-_]?14" . "Latin-8")
1627 (".*8859[-_]?15" . "Latin-9")
1628
1629 ;; The "IPA" Emacs language environment does not correspond
1630 ;; to any ISO 639 code, so let it stand for itself.
1631 ("ipa$" . "IPA")
1632
1633 ;; Nonstandard or obsolete language codes
1634 ("cz" . "Czech") ; e.g. Solaris 2.6
1635 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
1636 ("iw" . "Hebrew") ; e.g. X11R6.4
1637 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
1638 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
1639 )
1640 "List of pairs of locale regexps and language names.
1641 The first element whose locale regexp matches the start of a downcased
1642 locale specifies the language name corresponding to that locale.
1643 If the language name is nil, there is no corresponding language environment.")
1644
1645 (defvar locale-preferred-coding-systems
1646 '(("ja.*[._]euc" . japanese-iso-8bit)
1647 ("ja.*[._]jis7" . iso-2022-jp)
1648 ("ja.*[._]pck" . japanese-shift-jis)
1649 ("ja.*[._]sjis" . japanese-shift-jis)
1650 (".*[._].*8859[-_]?1" . iso-8859-1)
1651 (".*[._].*8859[-_]?2" . iso-8859-2)
1652 (".*[._].*8859[-_]?3" . iso-8859-3)
1653 (".*[._].*8859[-_]?4" . iso-8859-4)
1654 (".*[._].*8859[-_]?5" . iso-8859-5)
1655 (".*[._].*8859[-_]?7" . iso-8859-7)
1656 (".*[._].*8859[-_]?8" . iso-8859-8)
1657 (".*[._].*8859[-_]?9" . iso-8859-9)
1658 )
1659 "List of pairs of locale regexps and coding systems.
1660 The first element whose locale regexp matches the start of a downcased
1661 locale specifies the coding system to prefer when using that locale.
1662 If the coding system is nil, there is no special preference.")
1663
1664 (defun locale-name-match (key alist)
1665 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
1666 Return the value corresponding to the first regexp that matches the
1667 start of KEY, or nil if there is no match."
1668 (let (element)
1669 (while (and alist (not element))
1670 (if (string-match (concat "^\\(" (car (car alist)) "\\)") key)
1671 (setq element (car alist)))
1672 (setq alist (cdr alist)))
1673 (cdr element)))
1674
1675 (defun set-locale-environment (locale-name)
1676 "Set up multi-lingual environment for using LOCALE-NAME.
1677 This sets the coding system priority and the default input method
1678 and sometimes other things. LOCALE-NAME should be a string
1679 which is the name of a locale supported by the system;
1680 often it is of the form xx_XX.CODE, where xx is a language,
1681 XX is a country, and CODE specifies a character set and coding system.
1682 For example, the locale name \"ja_JP.EUC\" might name a locale
1683 for Japanese in Japan using the `japanese-iso-8bit' coding-system.
1684
1685 If LOCALE-NAME is nil, its value is taken from the environment.
1686
1687 The locale names supported by your system can typically be found in a
1688 directory named `/usr/share/locale' or `/usr/lib/locale'."
1689
1690 (unless locale-name
1691 ;; Use the first of these three environment variables
1692 ;; that has a nonempty value.
1693 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
1694 (while (and vars (not (setq locale-name (getenv (car vars)))))
1695 (setq vars (cdr vars)))))
1696
1697 (when locale-name
1698
1699 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
1700 ;; using the translation file that many systems have.
1701 (when locale-translation-file-name
1702 (with-temp-buffer
1703 (insert-file-contents locale-translation-file-name)
1704 (when (re-search-forward
1705 (concat "^" (regexp-quote locale-name) ":?[ \t]+") nil t)
1706 (setq locale-name (buffer-substring (point) (line-end-position))))))
1707
1708 (setq locale-name (downcase locale-name))
1709
1710 (let ((language-name (locale-name-match
1711 locale-name locale-language-names))
1712 (coding-system (locale-name-match
1713 locale-name locale-preferred-coding-systems)))
1714 (when language-name
1715
1716 ;; Set up for this character set. This is now the right way
1717 ;; to do it for both unibyte and multibyte modes.
1718 (set-language-environment language-name)
1719
1720 ;; If default-enable-multibyte-characters is nil,
1721 ;; we are using single-byte characters,
1722 ;; so the display table and terminal coding system are irrelevant.
1723 (when default-enable-multibyte-characters
1724 (set-display-table-and-terminal-coding-system language-name))
1725
1726 (setq locale-coding-system
1727 (car (get-language-info language-name 'coding-priority))))
1728
1729 (when coding-system
1730 (prefer-coding-system coding-system)
1731 (setq locale-coding-system coding-system)))))
1732 \f
1733 ;;; Charset property
1734
1735 (defun get-charset-property (charset propname)
1736 "Return the value of CHARSET's PROPNAME property.
1737 This is the last value stored with
1738 (put-charset-property CHARSET PROPNAME VALUE)."
1739 (and (not (eq charset 'composition))
1740 (plist-get (charset-plist charset) propname)))
1741
1742 (defun put-charset-property (charset propname value)
1743 "Store CHARSETS's PROPNAME property with value VALUE.
1744 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1745 (or (eq charset 'composition)
1746 (set-charset-plist charset
1747 (plist-put (charset-plist charset) propname value))))
1748
1749 ;;; Character code property
1750 (put 'char-code-property-table 'char-table-extra-slots 0)
1751
1752 (defvar char-code-property-table
1753 (make-char-table 'char-code-property-table)
1754 "Char-table containing a property list of each character code.
1755
1756 See also the documentation of `get-char-code-property' and
1757 `put-char-code-property'.")
1758
1759 (defun get-char-code-property (char propname)
1760 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1761 (let ((plist (aref char-code-property-table char)))
1762 (if (listp plist)
1763 (car (cdr (memq propname plist))))))
1764
1765 (defun put-char-code-property (char propname value)
1766 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1767 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1768 (let ((plist (aref char-code-property-table char)))
1769 (if plist
1770 (let ((slot (memq propname plist)))
1771 (if slot
1772 (setcar (cdr slot) value)
1773 (nconc plist (list propname value))))
1774 (aset char-code-property-table char (list propname value)))))
1775
1776 \f
1777 ;; Pretty description of encoded string
1778
1779 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
1780 (defvar iso-2022-control-alist
1781 '((?\x1b . "ESC")
1782 (?\x0e . "SO")
1783 (?\x0f . "SI")
1784 (?\x8e . "SS2")
1785 (?\x8f . "SS3")
1786 (?\x9b . "CSI")))
1787
1788 (defun encoded-string-description (str coding-system)
1789 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
1790 (setq str (string-as-unibyte str))
1791 (let ((char (aref str 0))
1792 desc)
1793 (when (< char 128)
1794 (setq desc (or (cdr (assq char iso-2022-control-alist))
1795 (char-to-string char)))
1796 (let ((i 1)
1797 (len (length str)))
1798 (while (< i len)
1799 (setq char (aref str i))
1800 (if (>= char 128)
1801 (setq desc nil i len)
1802 (setq desc (concat desc " "
1803 (or (cdr (assq char iso-2022-control-alist))
1804 (char-to-string char)))
1805 i (1+ i))))))
1806 (or desc
1807 (mapconcat (function (lambda (x) (format "0x%02x" x))) str " "))))
1808
1809 (defun encode-coding-char (char coding-system)
1810 "Encode CHAR by CODING-SYSTEM and return the resulting string.
1811 If CODING-SYSTEM can't safely encode CHAR, return nil."
1812 (if (cmpcharp char)
1813 (setq char (car (decompose-composite-char char 'list))))
1814 (let ((str1 (char-to-string char))
1815 (str2 (make-string 2 char))
1816 (safe-charsets (and coding-system
1817 (coding-system-get coding-system 'safe-charsets)))
1818 enc1 enc2 i1 i2)
1819 (when (or (eq safe-charsets t)
1820 (memq (char-charset char) safe-charsets))
1821 ;; We must find the encoded string of CHAR. But, just encoding
1822 ;; CHAR will put extra control sequences (usually to designate
1823 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
1824 ;; To exclude such tailing bytes, we at first encode one-char
1825 ;; string and two-char string, then check how many bytes at the
1826 ;; tail of both encoded strings are the same.
1827
1828 (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system))
1829 i1 (length enc1)
1830 enc2 (string-as-unibyte (encode-coding-string str2 coding-system))
1831 i2 (length enc2))
1832 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
1833 (setq i1 (1- i1) i2 (1- i2)))
1834
1835 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
1836 ;; and they are the extra control sequences at the tail to
1837 ;; exclude.
1838 (substring enc2 0 i2))))
1839
1840
1841 ;;; mule-cmds.el ends here