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