1 ;;; mule-cmds.el --- commands for mulitilingual environment
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
7 ;; Keywords: mule, multilingual
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (defvar dos-codepage))
32 ;;; MULE related key bindings and menus.
34 (defvar mule-keymap (make-sparse-keymap)
35 "Keymap for Mule (Multilingual environment) specific commands.")
37 ;; Keep "C-x C-m ..." for mule specific commands.
38 (define-key ctl-x-map "\C-m" mule-keymap)
40 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
41 (define-key mule-keymap "t" 'set-terminal-coding-system)
42 (define-key mule-keymap "k" 'set-keyboard-coding-system)
43 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
44 (define-key mule-keymap "x" 'set-selection-coding-system)
45 (define-key mule-keymap "X" 'set-next-selection-coding-system)
46 (define-key mule-keymap "\C-\\" 'set-input-method)
47 (define-key mule-keymap "c" 'universal-coding-system-argument)
48 (define-key mule-keymap "l" 'set-language-environment)
50 (defvar mule-menu-keymap
51 (make-sparse-keymap "Mule (Multilingual Environment)")
52 "Keymap for Mule (Multilingual environment) menu specific commands.")
54 (defvar describe-language-environment-map
55 (make-sparse-keymap "Describe Language Environment"))
57 (defvar setup-language-environment-map
58 (make-sparse-keymap "Set Language Environment"))
60 (defvar set-coding-system-map
61 (make-sparse-keymap "Set Coding System"))
63 (define-key-after mule-menu-keymap [set-language-environment]
64 (list 'menu-item "Set Language Environment" setup-language-environment-map
65 :help "Multilingual environment suitable for a specific language"))
66 (define-key-after mule-menu-keymap [mouse-set-font]
67 '(menu-item "Set Font/Fontset" mouse-set-font
68 :visible (fboundp 'generate-fontset-menu)
69 :help "Select a font from list of known fonts/fontsets"))
70 (define-key-after mule-menu-keymap [separator-mule]
73 (define-key-after mule-menu-keymap [toggle-input-method]
74 '(menu-item "Toggle Input Method" toggle-input-method)
76 (define-key-after mule-menu-keymap [set-input-method]
77 '(menu-item "Select Input Method..." set-input-method)
79 (define-key-after mule-menu-keymap [describe-input-method]
80 '(menu-item "Describe Input Method" describe-input-method))
81 (define-key-after mule-menu-keymap [separator-input-method]
84 (define-key-after mule-menu-keymap [set-various-coding-system]
85 (list 'menu-item "Set Coding Systems" set-coding-system-map
86 :enable 'enable-multibyte-characters))
87 (define-key-after mule-menu-keymap [view-hello-file]
88 '(menu-item "Show Multi-lingual Text" view-hello-file
89 :enable (file-readable-p
90 (expand-file-name "HELLO" data-directory))
91 :help "Display file which says HELLO in many languages")
93 (define-key-after mule-menu-keymap [separator-coding-system]
96 (define-key-after mule-menu-keymap [describe-language-environment]
97 (list 'menu-item "Describe Language Environment"
98 describe-language-environment-map
99 :help "Show multilingual settings for a specific language")
101 (define-key-after mule-menu-keymap [describe-input-method]
102 '(menu-item "Describe Input Method..." describe-input-method
103 :help "Keyboard layout for a specific input method")
105 (define-key-after mule-menu-keymap [describe-coding-system]
106 '(menu-item "Describe Coding System..." describe-coding-system)
108 (define-key-after mule-menu-keymap [list-character-sets]
109 '(menu-item "List Character Sets" list-character-sets
110 :help "Show table of available character sets"))
111 (define-key-after mule-menu-keymap [mule-diag]
112 '(menu-item "Show All of Mule Status" mule-diag
113 :help "Display multilingual environment settings")
116 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
117 '(menu-item "For Saving this Buffer" set-buffer-file-coding-system
118 :help "How to encode this buffer on disk")
120 (define-key-after set-coding-system-map [universal-coding-system-argument]
121 '(menu-item "For Next Command" universal-coding-system-argument
122 :help "Coding system to be used by next command")
124 (define-key-after set-coding-system-map [set-terminal-coding-system]
125 '(menu-item "For Terminal" set-terminal-coding-system
126 :enable (null (memq window-system '(x w32 mac)))
127 :help "How to encode terminal output")
129 (define-key-after set-coding-system-map [set-keyboard-coding-system]
130 '(menu-item "For Keyboard" set-keyboard-coding-system
131 :help "How to decode keyboard input")
133 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
134 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
135 :visible (fboundp 'start-process)
136 :enable (get-buffer-process (current-buffer))
137 :help "How to en/decode I/O from/to subprocess connected to this buffer")
139 (define-key-after set-coding-system-map [set-selection-coding-system]
140 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
141 :visible (display-selections-p)
142 :help "How to en/decode data to/from selection/clipboard")
144 (define-key-after set-coding-system-map [set-next-selection-coding-system]
145 '(menu-item "For Next X Selection" set-next-selection-coding-system
146 :visible (display-selections-p)
147 :help "How to en/decode next selection/clipboard operation")
149 (define-key setup-language-environment-map
150 [Default] '(menu-item "Default" setup-specified-language-environment))
152 (define-key describe-language-environment-map
153 [Default] '(menu-item "Default" describe-specified-language-support))
155 ;; This should be a single character key binding because users use it
156 ;; very frequently while editing multilingual text. Now we can use
157 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
158 ;; convenient because it requires shifting on most keyboards. An
159 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
160 ;; but it won't be used that frequently.
161 (define-key global-map "\C-\\" 'toggle-input-method)
163 ;; This is no good because people often type Shift-SPC
164 ;; meaning to type SPC. -- rms.
165 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
166 ;; (define-key global-map [?\S- ] 'toggle-input-method)
168 ;;; Mule related hyperlinks.
169 (defconst help-xref-mule-regexp-template
170 (purecopy (concat "\\(\\<\\("
171 "\\(coding system\\)\\|"
172 "\\(input method\\)\\|"
173 "\\(character set\\)\\|"
176 ;; Note starting with word-syntax character:
177 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
179 (defun coding-system-change-eol-conversion (coding-system eol-type)
180 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
181 The returned coding system converts end-of-line by EOL-TYPE
182 but text as the same way as CODING-SYSTEM.
183 EOL-TYPE should be `unix', `dos', `mac', or nil.
184 If EOL-TYPE is nil, the returned coding system detects
185 how end-of-line is formatted automatically while decoding.
187 EOL-TYPE can be specified by an integer 0, 1, or 2.
188 They means `unix', `dos', and `mac' respectively."
189 (if (symbolp eol-type)
190 (setq eol-type (cond ((eq eol-type 'unix) 0)
191 ((eq eol-type 'dos) 1)
192 ((eq eol-type 'mac) 2)
194 (let ((orig-eol-type (coding-system-eol-type coding-system)))
195 (if (vectorp orig-eol-type)
198 (aref orig-eol-type eol-type))
199 (let ((base (coding-system-base coding-system)))
202 (if (= eol-type orig-eol-type)
204 (setq orig-eol-type (coding-system-eol-type base))
205 (if (vectorp orig-eol-type)
206 (aref orig-eol-type eol-type))))))))
208 (defun coding-system-change-text-conversion (coding-system coding)
209 "Return a coding system which differs from CODING-SYSTEM in text conversion.
210 The returned coding system converts text by CODING
211 but end-of-line as the same way as CODING-SYSTEM.
212 If CODING is nil, the returned coding system detects
213 how text is formatted automatically while decoding."
214 (let ((eol-type (coding-system-eol-type coding-system)))
215 (coding-system-change-eol-conversion
216 (if coding coding 'undecided)
217 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
219 (defun toggle-enable-multibyte-characters (&optional arg)
220 "Change whether this buffer uses multibyte characters.
221 With arg, use multibyte characters if the arg is positive.
223 Note that this command does not convert the byte contents of
224 the buffer; it only changes the way those bytes are interpreted.
225 In general, therefore, this command *changes* the sequence of
226 characters that the current buffer contains.
228 We suggest you avoid using use this command unless you know what you
229 are doing. If you use it by mistake, and the buffer is now displayed
230 wrong, use this command again to toggle back to the right mode."
233 (if (null arg) (null enable-multibyte-characters)
234 (> (prefix-numeric-value arg) 0))))
235 (set-buffer-multibyte new-flag))
236 (force-mode-line-update))
238 (defun view-hello-file ()
239 "Display the HELLO file which list up many languages and characters."
241 ;; We have to decode the file in any environment.
242 (let ((default-enable-multibyte-characters t)
243 (coding-system-for-read 'iso-2022-7bit))
244 (find-file-read-only (expand-file-name "HELLO" data-directory))))
246 (defun universal-coding-system-argument ()
247 "Execute an I/O command using the specified coding system."
249 (let* ((default (and buffer-file-coding-system
250 (not (eq (coding-system-type buffer-file-coding-system)
252 buffer-file-coding-system))
253 (coding-system (read-coding-system
255 (format "Coding system for following command (default, %s): " default)
256 "Coding system for following command: ")
258 (keyseq (read-key-sequence
259 (format "Command to execute with %s:" coding-system)))
260 (cmd (key-binding keyseq))
263 (when (eq cmd 'universal-argument)
264 (call-interactively cmd)
266 ;; Process keys bound in `universal-argument-map'.
268 (setq keyseq (read-key-sequence nil t)
269 cmd (key-binding keyseq t))
270 (not (eq cmd 'universal-argument-other-key)))
271 (let ((current-prefix-arg prefix-arg)
272 ;; Have to bind `last-command-char' here so that
273 ;; `digit-argument', for isntance, can compute the
275 (last-command-char (aref keyseq 0)))
276 (call-interactively cmd)))
278 ;; This is the final call to `univeral-argument-other-key', which
279 ;; set's the final `prefix-arg.
280 (let ((current-prefix-arg prefix-arg))
281 (call-interactively cmd))
283 ;; Read the command to execute with the given prefix arg.
284 (setq prefix prefix-arg
285 keyseq (read-key-sequence nil t)
286 cmd (key-binding keyseq)))
288 (let ((coding-system-for-read coding-system)
289 (coding-system-for-write coding-system)
290 (current-prefix-arg prefix))
292 (call-interactively cmd))))
294 (defun set-default-coding-systems (coding-system)
295 "Set default value of various coding systems to CODING-SYSTEM.
296 This sets the following coding systems:
297 o coding system of a newly created buffer
298 o default coding system for subprocess I/O
299 This also sets the following values:
300 o default value used as file-name-coding-system for converting file names.
301 o default value for the command `set-terminal-coding-system' (not on MSDOS)
302 o default value for the command `set-keyboard-coding-system'."
303 (check-coding-system coding-system)
304 (setq-default buffer-file-coding-system coding-system)
305 (if default-enable-multibyte-characters
306 (setq default-file-name-coding-system coding-system))
307 ;; If coding-system is nil, honor that on MS-DOS as well, so
308 ;; that they could reset the terminal coding system.
309 (unless (and (eq window-system 'pc) coding-system)
310 (setq default-terminal-coding-system coding-system))
311 (setq default-keyboard-coding-system coding-system)
312 ;; Preserve eol-type from existing default-process-coding-systems.
313 ;; On non-unix-like systems in particular, these may have been set
314 ;; carefully by the user, or by the startup code, to deal with the
315 ;; users shell appropriately, so should not be altered by changing
316 ;; language environment.
318 (coding-system-change-text-conversion
319 (car default-process-coding-system) coding-system))
321 (coding-system-change-text-conversion
322 (cdr default-process-coding-system) coding-system)))
323 (setq default-process-coding-system
324 (cons output-coding input-coding))))
326 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
327 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal "20.3")
329 (defun prefer-coding-system (coding-system)
330 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
331 This also sets the following coding systems:
332 o coding system of a newly created buffer
333 o default coding system for subprocess I/O
334 This also sets the following values:
335 o default value used as `file-name-coding-system' for converting file names.
336 o default value for the command `set-terminal-coding-system' (not on MSDOS)
337 o default value for the command `set-keyboard-coding-system'
339 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
340 systems set by this function will use that type of EOL conversion.
342 This command does not change the default value of terminal coding system
343 for MS-DOS terminal, because DOS terminals only support a single coding
344 system, and Emacs automatically sets the default to that coding system at
347 A coding system that requires automatic detection of text
348 encoding (e.g. undecided, unix) can't be preferred.
350 See also `coding-category-list' and `coding-system-category'."
351 (interactive "zPrefer coding system: ")
352 (if (not (and coding-system (coding-system-p coding-system)))
353 (error "Invalid coding system `%s'" coding-system))
354 (let ((coding-category (coding-system-category coding-system))
355 (base (coding-system-base coding-system))
356 (eol-type (coding-system-eol-type coding-system)))
357 (if (not coding-category)
358 ;; CODING-SYSTEM is no-conversion or undecided.
359 (error "Can't prefer the coding system `%s'" coding-system))
360 (set coding-category (or base coding-system))
361 (update-coding-systems-internal)
362 (or (eq coding-category (car coding-category-list))
363 ;; We must change the order.
364 (set-coding-priority (list coding-category)))
365 (if (and base (interactive-p))
366 (message "Highest priority is set to %s (base of %s)"
368 ;; If they asked for specific EOL conversion, honor that.
369 (if (memq eol-type '(0 1 2))
371 (coding-system-change-eol-conversion base eol-type))
372 (setq coding-system base))
373 (set-default-coding-systems coding-system)))
375 (defvar sort-coding-systems-predicate nil
376 "If non-nil, a predicate function to sort coding systems.
378 It is called with two coding systems, and should return t if the first
379 one is \"less\" than the second.
381 The function `sort-coding-systems' use it.")
383 (defun sort-coding-systems (codings)
384 "Sort coding system list CODINGS by a priority of each coding system.
386 If a coding system is most preferred, it has the highest priority.
387 Otherwise, a coding system corresponds to some MIME charset has higher
388 priorities. Among them, a coding system included in `coding-system'
389 key of the current language environment has higher priorities. See
390 also the documentation of `language-info-alist'.
392 If the variable `sort-coding-systems-predicate' (which see) is
393 non-nil, it is used to sort CODINGS in the different way than above."
394 (if sort-coding-systems-predicate
395 (sort codings sort-coding-systems-predicate)
396 (let* ((most-preferred (symbol-value (car coding-category-list)))
397 (lang-preferred (get-language-info current-language-environment
401 (let ((base (coding-system-base x)))
402 (+ (if (eq base most-preferred) 64 0)
403 (let ((mime (coding-system-get base 'mime-charset)))
405 (if (string-match "^x-" (symbol-name mime))
408 (if (memq base lang-preferred) 8 0)
409 (if (string-match "-with-esc$" (symbol-name base))
411 (if (eq (coding-system-type base) 2)
412 ;; For ISO based coding systems, prefer
413 ;; one that doesn't use escape sequences.
414 (let ((flags (coding-system-flags base)))
415 (if (or (consp (aref flags 0))
416 (consp (aref flags 1))
417 (consp (aref flags 2))
418 (consp (aref flags 3)))
419 (if (or (aref flags 8) (aref flags 9))
424 (sort codings (function (lambda (x y)
425 (> (funcall func x) (funcall func y))))))))
427 (defun find-coding-systems-region (from to)
428 "Return a list of proper coding systems to encode a text between FROM and TO.
429 All coding systems in the list can safely encode any multibyte characters
432 If the text contains no multibyte characters, return a list of a single
433 element `undecided'."
434 (let ((codings (find-coding-systems-region-internal from to)))
436 ;; The text contains only ASCII characters. Any coding
439 ;; We need copy-sequence because sorting will alter the argument.
440 (sort-coding-systems (copy-sequence codings)))))
442 (defun find-coding-systems-string (string)
443 "Return a list of proper coding systems to encode STRING.
444 All coding systems in the list can safely encode any multibyte characters
447 If STRING contains no multibyte characters, return a list of a single
448 element `undecided'."
449 (find-coding-systems-region string nil))
451 (defun find-coding-systems-for-charsets (charsets)
452 "Return a list of proper coding systems to encode characters of CHARSETS.
453 CHARSETS is a list of character sets."
454 (cond ((or (null charsets)
455 (and (= (length charsets) 1)
456 (eq 'ascii (car charsets))))
458 ((or (memq 'eight-bit-control charsets)
459 (memq 'eight-bit-graphic charsets))
460 '(raw-text emacs-mule))
464 (while (and codings charsets)
465 (setq charset (car charsets) charsets (cdr charsets))
466 (unless (eq charset 'ascii)
467 (setq l (aref char-coding-system-table (make-char charset)))
472 (if (memq (car codings) l)
473 (setq ll (cons (car codings) ll)))
474 (setq codings (cdr codings)))
475 (setq codings ll)))))
477 (char-table-extra-slot char-coding-system-table 0))))))
479 (defun find-multibyte-characters (from to &optional maxcount excludes)
480 "Find multibyte characters in the region specified by FROM and TO.
481 If FROM is a string, find multibyte characters in the string.
482 The return value is an alist of the following format:
483 ((CHARSET COUNT CHAR ...) ...)
485 CHARSET is a character set,
486 COUNT is a number of characters,
487 CHARs are found characters of the character set.
488 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
489 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
491 For invalid characters, CHARs are actually strings."
496 (while (setq idx (string-match "[^\000-\177]" from idx))
497 (setq char (aref from idx)
498 charset (char-charset char))
499 (if (eq charset 'unknown)
500 (setq char (match-string 0)))
501 (if (or (memq charset '(unknown
502 eight-bit-control eight-bit-graphic))
503 (not (or (eq excludes t) (memq charset excludes))))
504 (let ((slot (assq charset chars)))
506 (if (not (memq char (nthcdr 2 slot)))
507 (let ((count (nth 1 slot)))
508 (setcar (cdr slot) (1+ count))
509 (if (or (not maxcount) (< count maxcount))
510 (nconc slot (list char)))))
511 (setq chars (cons (list charset 1 char) chars)))))
512 (setq idx (1+ idx))))
515 (while (re-search-forward "[^\000-\177]" to t)
516 (setq char (preceding-char)
517 charset (char-charset char))
518 (if (eq charset 'unknown)
519 (setq char (match-string 0)))
520 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
521 (not (or (eq excludes t) (memq charset excludes))))
522 (let ((slot (assq charset chars)))
524 (if (not (member char (nthcdr 2 slot)))
525 (let ((count (nth 1 slot)))
526 (setcar (cdr slot) (1+ count))
527 (if (or (not maxcount) (< count maxcount))
528 (nconc slot (list char)))))
529 (setq chars (cons (list charset 1 char) chars))))))))
532 (defvar last-coding-system-specified nil
533 "Most recent coding system explicitly specified by the user when asked.
534 This variable is set whenever Emacs asks the user which coding system
535 to use in order to write a file. If you set it to nil explicitly,
536 then call `write-region', then afterward this variable will be non-nil
537 only if the user was explicitly asked and specified a coding system.")
539 (defvar select-safe-coding-system-accept-default-p nil
540 "If non-nil, a function to control the behaviour of coding system selection.
541 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
542 function `select-safe-coding-system' (which see). This variable
543 overrides that argument.")
545 (defun select-safe-coding-system (from to &optional default-coding-system
547 "Ask a user to select a safe coding system from candidates.
548 The candidates of coding systems which can safely encode a text
549 between FROM and TO are shown in a popup window. Among them, the most
550 proper one is suggested as the default.
552 The list of `buffer-file-coding-system' of the current buffer and the
553 most preferred coding system (if it corresponds to a MIME charset) is
554 treated as the default coding system list. Among them, the first one
555 that safely encodes the text is silently selected and returned without
556 any user interaction. See also the command `prefer-coding-system'.
558 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
559 list of coding systems to be prepended to the default coding system
562 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
563 determine the acceptability of the silently selected coding system.
564 It is called with that coding system, and should return nil if it
565 should not be silently selected and thus user interaction is required.
567 The variable `select-safe-coding-system-accept-default-p', if
568 non-nil, overrides ACCEPT-DEFAULT-P.
570 Kludgy feature: if FROM is a string, the string is the target text,
572 (if (and default-coding-system
573 (not (listp default-coding-system)))
574 (setq default-coding-system (list default-coding-system)))
576 ;; Change elements of the list to (coding . base-coding).
577 (setq default-coding-system
578 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
579 default-coding-system))
581 ;; If buffer-file-coding-system is not nil nor undecided, append it
583 (if buffer-file-coding-system
584 (let ((base (coding-system-base buffer-file-coding-system)))
585 (or (eq base 'undecided)
586 (assq buffer-file-coding-system default-coding-system)
587 (rassq base default-coding-system)
588 (setq default-coding-system
589 (append default-coding-system
590 (list (cons buffer-file-coding-system base)))))))
592 ;; If the most preferred coding system has the property mime-charset,
593 ;; append it to the defaults.
594 (let ((tail coding-category-list)
597 (not (setq preferred (symbol-value (car tail)))))
598 (setq tail (cdr tail)))
599 (and (coding-system-p preferred)
600 (setq base (coding-system-base preferred))
601 (coding-system-get preferred 'mime-charset)
602 (not (assq preferred default-coding-system))
603 (not (rassq base default-coding-system))
604 (setq default-coding-system
605 (append default-coding-system (list (cons preferred base))))))
607 (if select-safe-coding-system-accept-default-p
608 (setq accept-default-p select-safe-coding-system-accept-default-p))
610 (let ((codings (find-coding-systems-region from to))
612 (bufname (buffer-name))
613 (l default-coding-system))
614 (if (eq (car codings) 'undecided)
615 ;; Any coding system is ok.
616 (setq coding-system t)
618 (while (and l (not coding-system))
619 (if (memq (cdr (car l)) codings)
620 (setq coding-system (car (car l)))
622 (if (and coding-system accept-default-p)
623 (or (funcall accept-default-p coding-system)
624 (setq coding-system (list coding-system)))))
626 ;; If all the defaults failed, ask a user.
627 (when (or (not coding-system) (consp coding-system))
628 ;; At first, change each coding system to the corresponding
629 ;; mime-charset name if it is also a coding system. Such a name
630 ;; is more friendly to users.
634 (setq mime-charset (coding-system-get (car l) 'mime-charset))
635 (if (and mime-charset (coding-system-p mime-charset))
636 (setcar l mime-charset))
639 ;; Make sure the offending buffer is displayed.
641 (pop-to-buffer bufname))
642 ;; Then ask users to select one form CODINGS.
644 (save-window-excursion
645 (with-output-to-temp-buffer "*Warning*"
647 (set-buffer standard-output)
648 (if (not default-coding-system)
649 (insert "No default coding systems to try for "
651 (format "string \"%s\"." from)
652 (format "buffer `%s'." bufname)))
654 "These default coding systems were tried to encode"
656 (concat " \"" (if (> (length from) 10)
657 (concat (substring from 0 10) "...\"")
659 (format " text\nin the buffer `%s'" bufname))
663 (mapcar (function (lambda (x)
664 (princ " ") (princ (car x))))
665 default-coding-system)
667 (fill-region-as-paragraph pos (point)))
669 (if (consp coding-system)
670 (concat (format "%s safely encodes the target text,\n"
672 "but it is not recommended for encoding text in this context,\n"
673 "e.g., for sending an email message.\n")
674 "However, none of them safely encodes the target text.\n")))
675 (insert (if (consp coding-system)
676 "\nSelect the above, or "
678 "one of the following safe coding systems:\n")
681 (mapcar (function (lambda (x) (princ " ") (princ x)))
684 (fill-region-as-paragraph pos (point)))))
686 ;; Read a coding system.
687 (if (consp coding-system)
688 (setq codings (cons (car coding-system) codings)))
689 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
691 (name (completing-read
692 (format "Select coding system (default %s): "
694 safe-names nil t nil nil
695 (car (car safe-names)))))
696 (setq last-coding-system-specified (intern name)
697 coding-system last-coding-system-specified)))
698 (kill-buffer "*Warning*")))
700 (if (vectorp (coding-system-eol-type coding-system))
701 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
704 (coding-system-change-eol-conversion coding-system eol)))))
706 (if (eq coding-system t)
707 (setq coding-system buffer-file-coding-system))
710 (setq select-safe-coding-system-function 'select-safe-coding-system)
712 (defun select-message-coding-system ()
713 "Return a coding system to encode the outgoing message of the current buffer.
714 It at first tries the first coding system found in these variables
716 (1) local value of `buffer-file-coding-system'
717 (2) value of `sendmail-coding-system'
718 (3) value of `default-sendmail-coding-system'
719 (4) value of `default-buffer-file-coding-system'
720 If the found coding system can't encode the current buffer,
721 or none of them are bound to a coding system,
722 it asks the user to select a proper coding system."
723 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
724 buffer-file-coding-system)
725 sendmail-coding-system
726 default-sendmail-coding-system
727 default-buffer-file-coding-system)))
728 (if (eq coding 'no-conversion)
729 ;; We should never use no-conversion for outgoing mails.
731 (if (fboundp select-safe-coding-system-function)
732 (funcall select-safe-coding-system-function
733 (point-min) (point-max) coding
734 (function (lambda (x) (coding-system-get x 'mime-charset))))
737 ;;; Language support stuff.
739 (defvar language-info-alist nil
740 "Alist of language environment definitions.
741 Each element looks like:
742 (LANGUAGE-NAME . ((KEY . INFO) ...))
743 where LANGUAGE-NAME is a string, the name of the language environment,
744 KEY is a symbol denoting the kind of information, and
745 INFO is the data associated with KEY.
746 Meaningful values for KEY include
748 documentation value is documentation of what this language environment
749 is meant for, and how to use it.
750 charset value is a list of the character sets used by this
751 language environment.
752 sample-text value is one line of text,
753 written using those character sets,
754 appropriate for this language environment.
755 setup-function value is a function to call to switch to this
756 language environment.
757 exit-function value is a function to call to leave this
758 language environment.
759 coding-system value is a list of coding systems that are good
760 for saving text written in this language environment.
761 This list serves as suggestions to the user;
762 in effect, as a kind of documentation.
763 coding-priority value is a list of coding systems for this language
764 environment, in order of decreasing priority.
765 This is used to set up the coding system priority
766 list when you switch to this language environment.
768 value is a translation table to be set in the
769 variable `nonascii-translation-table' in this
770 language environment, or a character set from
771 which `nonascii-insert-offset' is calculated.
772 input-method value is a default input method for this language
774 features value is a list of features requested in this
775 language environment.
777 The following keys take effect only when multibyte characters are
778 globally disabled, i.e. the value of `default-enable-multibyte-characters'
781 unibyte-syntax value is a library name to load to set
782 unibyte 8-bit character syntaxes for this
783 language environment.
785 unibyte-display value is a coding system to encode characters
786 for the terminal. Characters in the range
787 of 160 to 255 display not as octal escapes,
788 but as non-ASCII characters in this language
791 (defun get-language-info (lang-env key)
792 "Return information listed under KEY for language environment LANG-ENV.
793 KEY is a symbol denoting the kind of information.
794 For a list of useful values for KEY and their meanings,
795 see `language-info-alist'."
796 (if (symbolp lang-env)
797 (setq lang-env (symbol-name lang-env)))
798 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
800 (cdr (assq key (cdr lang-slot))))))
802 (defun set-language-info (lang-env key info)
803 "Modify part of the definition of language environment LANG-ENV.
804 Specifically, this stores the information INFO under KEY
805 in the definition of this language environment.
806 KEY is a symbol denoting the kind of information.
807 INFO is the value for that information.
809 For a list of useful values for KEY and their meanings,
810 see `language-info-alist'."
811 (if (symbolp lang-env)
812 (setq lang-env (symbol-name lang-env)))
813 (let (lang-slot key-slot)
814 (setq lang-slot (assoc lang-env language-info-alist))
815 (if (null lang-slot) ; If no slot for the language, add it.
816 (setq lang-slot (list lang-env)
817 language-info-alist (cons lang-slot language-info-alist)))
818 (setq key-slot (assq key lang-slot))
819 (if (null key-slot) ; If no slot for the key, add it.
821 (setq key-slot (list key))
822 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
823 (setcdr key-slot (purecopy info))))
825 (defun set-language-info-alist (lang-env alist &optional parents)
826 "Store ALIST as the definition of language environment LANG-ENV.
827 ALIST is an alist of KEY and INFO values. See the documentation of
828 `language-info-alist' for the meanings of KEY and INFO.
830 Optional arg PARENTS is a list of parent menu names; it specifies
831 where to put this language environment in the
832 Describe Language Environment and Set Language Environment menus.
833 For example, (\"European\") means to put this language environment
834 in the European submenu in each of those two menus."
835 (if (symbolp lang-env)
836 (setq lang-env (symbol-name lang-env)))
837 (let ((describe-map describe-language-environment-map)
838 (setup-map setup-language-environment-map))
841 map parent-symbol parent prompt)
843 (if (symbolp (setq parent-symbol (car l)))
844 (setq parent (symbol-name parent))
845 (setq parent parent-symbol parent-symbol (intern parent)))
846 (setq map (lookup-key describe-map (vector parent-symbol)))
847 ;; This prompt string is for define-prefix-command, so
848 ;; that the map it creates will be suitable for a menu.
849 (or map (setq prompt (format "%s Environment" parent)))
852 (setq map (intern (format "describe-%s-environment-map"
854 (define-prefix-command map nil prompt)
855 (define-key-after describe-map (vector parent-symbol)
856 (cons parent map) t)))
857 (setq describe-map (symbol-value map))
858 (setq map (lookup-key setup-map (vector parent-symbol)))
861 (setq map (intern (format "setup-%s-environment-map"
863 (define-prefix-command map nil prompt)
864 (define-key-after setup-map (vector parent-symbol)
865 (cons parent map) t)))
866 (setq setup-map (symbol-value map))
869 ;; Set up menu items for this language env.
870 (let ((doc (assq 'documentation alist)))
872 (define-key-after describe-map (vector (intern lang-env))
873 (cons lang-env 'describe-specified-language-support) t)))
874 (define-key-after setup-map (vector (intern lang-env))
875 (cons lang-env 'setup-specified-language-environment) t)
878 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
879 (setq alist (cdr alist)))))
881 (defun read-language-name (key prompt &optional default)
882 "Read a language environment name which has information for KEY.
883 If KEY is nil, read any language environment.
884 Prompt with PROMPT. DEFAULT is the default choice of language environment.
885 This returns a language environment name as a string."
886 (let* ((completion-ignore-case t)
887 (name (completing-read prompt
890 (function (lambda (elm) (assq key elm))))
892 (if (and (> (length name) 0)
894 (get-language-info name key)))
897 ;;; Multilingual input methods.
899 "LEIM: Libraries of Emacs Input Methods."
902 (defconst leim-list-file-name "leim-list.el"
903 "Name of LEIM list file.
904 This file contains a list of libraries of Emacs input methods (LEIM)
905 in the format of Lisp expression for registering each input method.
906 Emacs loads this file at startup time.")
908 (defvar leim-list-header (format
909 ";;; %s -- list of LEIM (Library of Emacs Input Method)
911 ;; This file contains a list of LEIM (Library of Emacs Input Method)
912 ;; methods in the same directory as this file. Loading this file
913 ;; registers all the input methods in Emacs.
915 ;; Each entry has the form:
916 ;; (register-input-method
917 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
920 ;; See the function `register-input-method' for the meanings of the arguments.
922 ;; If this directory is included in load-path, Emacs automatically
923 ;; loads this file at startup time.
927 "Header to be inserted in LEIM list file.")
929 (defvar leim-list-entry-regexp "^(register-input-method"
930 "Regexp matching head of each entry in LEIM list file.
931 See also the variable `leim-list-header'")
933 (defvar update-leim-list-functions
934 '(quail-update-leim-list-file)
935 "List of functions to call to update LEIM list file.
936 Each function is called with one arg, LEIM directory name.")
938 (defun update-leim-list-file (&rest dirs)
939 "Update LEIM list file in directories DIRS."
940 (let ((functions update-leim-list-functions))
942 (apply (car functions) dirs)
943 (setq functions (cdr functions)))))
945 (defvar current-input-method nil
946 "The current input method for multilingual text.
947 If nil, that means no input method is activated now.")
948 (make-variable-buffer-local 'current-input-method)
949 (put 'current-input-method 'permanent-local t)
951 (defvar current-input-method-title nil
952 "Title string of the current input method shown in mode line.")
953 (make-variable-buffer-local 'current-input-method-title)
954 (put 'current-input-method-title 'permanent-local t)
956 (defcustom default-input-method nil
957 "*Default input method for multilingual text (a string).
958 This is the input method activated automatically by the command
959 `toggle-input-method' (\\[toggle-input-method])."
961 :type '(choice (const nil) string)
962 :set-after '(current-language-environment))
964 (put 'input-method-function 'permanent-local t)
966 (defvar input-method-history nil
967 "History list for some commands that read input methods.")
968 (make-variable-buffer-local 'input-method-history)
969 (put 'input-method-history 'permanent-local t)
971 (defvar inactivate-current-input-method-function nil
972 "Function to call for inactivating the current input method.
973 Every input method should set this to an appropriate value when activated.
974 This function is called with no argument.
976 This function should never change the value of `current-input-method'.
977 It is set to nil by the function `inactivate-input-method'.")
978 (make-variable-buffer-local 'inactivate-current-input-method-function)
979 (put 'inactivate-current-input-method-function 'permanent-local t)
981 (defvar describe-current-input-method-function nil
982 "Function to call for describing the current input method.
983 This function is called with no argument.")
984 (make-variable-buffer-local 'describe-current-input-method-function)
985 (put 'describe-current-input-method-function 'permanent-local t)
987 (defvar input-method-alist nil
988 "Alist of input method names vs how to use them.
989 Each element has the form:
990 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
991 See the function `register-input-method' for the meanings of the elements.")
993 (defun register-input-method (input-method lang-env &rest args)
994 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
995 INPUT-METHOD and LANG-ENV are symbols or strings.
997 The remaining arguments are:
998 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
999 ACTIVATE-FUNC is a function to call to activate this method.
1000 TITLE is a string to show in the mode line when this method is active.
1001 DESCRIPTION is a string describing this method and what it is good for.
1002 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1003 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1005 This function is mainly used in the file \"leim-list.el\" which is
1006 created at Emacs build time, registering all Quail input methods
1007 contained in the Emacs distribution.
1009 In case you want to register a new Quail input method by yourself, be
1010 careful to use the same input method title as given in the third
1011 parameter of `quail-define-package'. (If the values are different, the
1012 string specified in this function takes precedence.)
1014 The commands `describe-input-method' and `list-input-methods' need
1015 these duplicated values to show some information about input methods
1016 without loading the relevant Quail packages."
1017 (if (symbolp lang-env)
1018 (setq lang-env (symbol-name lang-env)))
1019 (if (symbolp input-method)
1020 (setq input-method (symbol-name input-method)))
1021 (let ((info (cons lang-env args))
1022 (slot (assoc input-method input-method-alist)))
1025 (setq slot (cons input-method info))
1026 (setq input-method-alist (cons slot input-method-alist)))))
1028 (defun read-input-method-name (prompt &optional default inhibit-null)
1029 "Read a name of input method from a minibuffer prompting with PROMPT.
1030 If DEFAULT is non-nil, use that as the default,
1031 and substitute it into PROMPT at the first `%s'.
1032 If INHIBIT-NULL is non-nil, null input signals an error.
1034 The return value is a string."
1036 (setq prompt (format prompt default)))
1037 (let* ((completion-ignore-case t)
1038 ;; As it is quite normal to change input method in the
1039 ;; minibuffer, we must enable it even if
1040 ;; enable-recursive-minibuffers is currently nil.
1041 (enable-recursive-minibuffers t)
1042 ;; This binding is necessary because input-method-history is
1044 (input-method (completing-read prompt input-method-alist
1045 nil t nil 'input-method-history
1047 (if (and input-method (symbolp input-method))
1048 (setq input-method (symbol-name input-method)))
1049 (if (> (length input-method) 0)
1052 (error "No valid input method is specified")))))
1054 (defun activate-input-method (input-method)
1055 "Switch to input method INPUT-METHOD for the current buffer.
1056 If some other input method is already active, turn it off first.
1057 If INPUT-METHOD is nil, deactivate any current input method."
1058 (if (and input-method (symbolp input-method))
1059 (setq input-method (symbol-name input-method)))
1060 (if (and current-input-method
1061 (not (string= current-input-method input-method)))
1062 (inactivate-input-method))
1063 (unless (or current-input-method (null input-method))
1064 (let ((slot (assoc input-method input-method-alist)))
1066 (error "Can't activate input method `%s'" input-method))
1067 (setq current-input-method-title nil)
1068 (let ((func (nth 2 slot)))
1069 (if (functionp func)
1070 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1071 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1073 (require (cdr func))
1074 (apply (car func) input-method (nthcdr 5 slot)))
1075 (error "Can't activate input method `%s'" input-method))))
1076 (setq current-input-method input-method)
1077 (or (stringp current-input-method-title)
1078 (setq current-input-method-title (nth 3 slot)))
1080 (run-hooks 'input-method-activate-hook)
1081 (force-mode-line-update)))))
1083 (defun inactivate-input-method ()
1084 "Turn off the current input method."
1085 (when current-input-method
1086 (if input-method-history
1087 (unless (string= current-input-method (car input-method-history))
1088 (setq input-method-history
1089 (cons current-input-method
1090 (delete current-input-method input-method-history))))
1091 (setq input-method-history (list current-input-method)))
1093 (funcall inactivate-current-input-method-function)
1095 (run-hooks 'input-method-inactivate-hook)
1096 (setq current-input-method nil
1097 input-method-function nil
1098 current-input-method-title nil)
1099 (force-mode-line-update)))))
1101 (defun set-input-method (input-method)
1102 "Select and activate input method INPUT-METHOD for the current buffer.
1103 This also sets the default input method to the one you specify.
1104 If INPUT-METHOD is nil, this function turns off the input method, and
1105 also causes you to be prompted for a name of an input method the next
1106 time you invoke \\[toggle-input-method].
1108 To deactivate the input method interactively, use \\[toggle-input-method].
1109 To deactivate it programmatically, use \\[inactivate-input-method]."
1111 (let* ((default (or (car input-method-history) default-input-method)))
1112 (list (read-input-method-name
1113 (if default "Select input method (default %s): " "Select input method: ")
1115 (activate-input-method input-method)
1116 (setq default-input-method input-method)
1117 (when (interactive-p)
1118 (customize-mark-as-set 'default-input-method))
1119 default-input-method)
1121 (defun toggle-input-method (&optional arg)
1122 "Enable or disable multilingual text input method for the current buffer.
1123 Only one input method can be enabled at any time in a given buffer.
1125 The normal action is to enable an input method if none was
1126 enabled, and disable the current one otherwise. Which input method
1127 to enable can be determined in various ways--either the one most
1128 recently used, or the one specified by `default-input-method', or
1129 as a last resort by reading the name of an input method in the
1132 With a prefix argument, read an input method name with the minibuffer
1133 and enable that one. The default is the most recent input method specified
1134 \(not including the currently active input method, if any)."
1137 (if (and current-input-method (not arg))
1138 (inactivate-input-method)
1139 (let ((default (or (car input-method-history) default-input-method)))
1140 (if (and arg default (equal current-input-method default)
1141 (> (length input-method-history) 1))
1142 (setq default (nth 1 input-method-history)))
1143 (activate-input-method
1144 (if (or arg (not default))
1146 (read-input-method-name
1147 (if default "Input method (default %s): " "Input method: " )
1150 (unless default-input-method
1152 (setq default-input-method current-input-method)
1153 (when (interactive-p)
1154 (customize-mark-as-set 'default-input-method)))))))
1156 (defun describe-input-method (input-method)
1157 "Describe input method INPUT-METHOD."
1159 (list (read-input-method-name
1160 "Describe input method (default, current choice): ")))
1161 (if (and input-method (symbolp input-method))
1162 (setq input-method (symbol-name input-method)))
1163 (help-setup-xref (list #'describe-input-method
1164 (or input-method current-input-method))
1167 (if (null input-method)
1168 (describe-current-input-method)
1169 (let ((current current-input-method))
1173 (activate-input-method input-method)
1174 (describe-current-input-method))
1175 (activate-input-method current))
1177 (activate-input-method current)
1178 (help-setup-xref (list #'describe-input-method input-method)
1180 (with-output-to-temp-buffer (help-buffer)
1181 (let ((elt (assoc input-method input-method-alist)))
1183 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1184 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1186 (defun describe-current-input-method ()
1187 "Describe the input method currently in use.
1188 This is a subroutine for `describe-input-method'."
1189 (if current-input-method
1190 (if (and (symbolp describe-current-input-method-function)
1191 (fboundp describe-current-input-method-function))
1192 (funcall describe-current-input-method-function)
1193 (message "No way to describe the current input method `%s'"
1194 current-input-method)
1196 (error "No input method is activated now")))
1198 (defun read-multilingual-string (prompt &optional initial-input input-method)
1199 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1200 The input method selected last time is activated in minibuffer.
1201 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1203 Optional 3rd argument INPUT-METHOD specifies the input method
1204 to be activated instead of the one selected last time. It is a symbol
1208 current-input-method
1209 default-input-method
1210 (read-input-method-name "Input method: " nil t)))
1211 (if (and input-method (symbolp input-method))
1212 (setq input-method (symbol-name input-method)))
1213 (let ((prev-input-method current-input-method))
1216 (activate-input-method input-method)
1217 (read-string prompt initial-input nil nil t))
1218 (activate-input-method prev-input-method))))
1220 ;; Variables to control behavior of input methods. All input methods
1221 ;; should react to these variables.
1223 (defcustom input-method-verbose-flag 'default
1224 "*A flag to control extra guidance given by input methods.
1225 The value should be nil, t, `complex-only', or `default'.
1227 The extra guidance is done by showing list of available keys in echo
1228 area. When you use the input method in the minibuffer, the guidance
1229 is shown at the bottom short window (split from the existing window).
1231 If the value is t, extra guidance is always given, if the value is
1232 nil, extra guidance is always suppressed.
1234 If the value is `complex-only', only complex input methods such as
1235 `chinese-py' and `japanese' give extra guidance.
1237 If the value is `default', complex input methods always give extra
1238 guidance, but simple input methods give it only when you are not in
1241 See also the variable `input-method-highlight-flag'."
1242 :type '(choice (const t) (const nil) (const complex-only) (const default))
1245 (defcustom input-method-highlight-flag t
1246 "*If this flag is non-nil, input methods highlight partially-entered text.
1247 For instance, while you are in the middle of a Quail input method sequence,
1248 the text inserted so far is temporarily underlined.
1249 The underlining goes away when you finish or abort the input method sequence.
1250 See also the variable `input-method-verbose-flag'."
1254 (defvar input-method-activate-hook nil
1255 "Normal hook run just after an input method is activated.
1257 The variable `current-input-method' keeps the input method name
1260 (defvar input-method-inactivate-hook nil
1261 "Normal hook run just after an input method is inactivated.
1263 The variable `current-input-method' still keeps the input method name
1266 (defvar input-method-after-insert-chunk-hook nil
1267 "Normal hook run just after an input method insert some chunk of text.")
1269 (defvar input-method-exit-on-first-char nil
1270 "This flag controls when an input method returns.
1271 Usually, the input method does not return while there's a possibility
1272 that it may find a different translation if a user types another key.
1273 But, it this flag is non-nil, the input method returns as soon as
1274 the current key sequence gets long enough to have some valid translation.")
1276 (defvar input-method-use-echo-area nil
1277 "This flag controls how an input method shows an intermediate key sequence.
1278 Usually, the input method inserts the intermediate key sequence,
1279 or candidate translations corresponding to the sequence,
1280 at point in the current buffer.
1281 But, if this flag is non-nil, it displays them in echo area instead.")
1283 (defvar input-method-exit-on-invalid-key nil
1284 "This flag controls the behaviour of an input method on invalid key input.
1285 Usually, when a user types a key which doesn't start any character
1286 handled by the input method, the key is handled by turning off the
1287 input method temporarily. After that key, the input method is re-enabled.
1288 But, if this flag is non-nil, the input method is never back on.")
1291 (defvar set-language-environment-hook nil
1292 "Normal hook run after some language environment is set.
1294 When you set some hook function here, that effect usually should not
1295 be inherited to another language environment. So, you had better set
1296 another function in `exit-language-environment-hook' (which see) to
1297 cancel the effect.")
1299 (defvar exit-language-environment-hook nil
1300 "Normal hook run after exiting from some language environment.
1301 When this hook is run, the variable `current-language-environment'
1302 is still bound to the language environment being exited.
1304 This hook is mainly used for canceling the effect of
1305 `set-language-environment-hook' (which-see).")
1307 (put 'setup-specified-language-environment 'apropos-inhibit t)
1309 (defun setup-specified-language-environment ()
1310 "Switch to a specified language environment."
1312 (let (language-name)
1313 (if (and (symbolp last-command-event)
1314 (or (not (eq last-command-event 'Default))
1315 (setq last-command-event 'English))
1316 (setq language-name (symbol-name last-command-event)))
1318 (set-language-environment language-name)
1319 (customize-mark-as-set 'current-language-environment))
1320 (error "Bogus calling sequence"))))
1322 (defcustom current-language-environment "English"
1323 "The last language environment specified with `set-language-environment'.
1324 This variable should be set only with \\[customize], which is equivalent
1325 to using the function `set-language-environment'."
1326 :link '(custom-manual "(emacs)Language Environments")
1327 :set (lambda (symbol value) (set-language-environment value))
1329 (or (car-safe (assoc-ignore-case
1330 (if (symbolp current-language-environment)
1331 (symbol-name current-language-environment)
1332 current-language-environment)
1333 language-info-alist))
1335 :type (cons 'choice (mapcar (lambda (lang)
1336 (list 'const (car lang)))
1337 language-info-alist))
1338 :initialize 'custom-initialize-default
1342 (defun reset-language-environment ()
1343 "Reset multilingual environment of Emacs to the default status.
1345 The default status is as follows:
1347 The default value of buffer-file-coding-system is nil.
1348 The default coding system for process I/O is nil.
1349 The default value for the command `set-terminal-coding-system' is nil.
1350 The default value for the command `set-keyboard-coding-system' is nil.
1352 The order of priorities of coding categories and the coding system
1353 bound to each category are as follows
1354 coding category coding system
1355 --------------------------------------------------
1356 coding-category-iso-8-2 iso-latin-1
1357 coding-category-iso-8-1 iso-latin-1
1358 coding-category-iso-7-tight iso-2022-jp
1359 coding-category-iso-7 iso-2022-7bit
1360 coding-category-iso-7-else iso-2022-7bit-lock
1361 coding-category-iso-8-else iso-2022-8bit-ss2
1362 coding-category-emacs-mule emacs-mule
1363 coding-category-raw-text raw-text
1364 coding-category-sjis japanese-shift-jis
1365 coding-category-big5 chinese-big5
1366 coding-category-ccl nil
1367 coding-category-binary no-conversion
1368 coding-category-utf-16-be nil
1369 coding-category-utf-16-le nil
1370 coding-category-utf-8 mule-utf-8"
1372 ;; This function formerly set default-enable-multibyte-characters to t,
1373 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1375 (setq coding-category-iso-7-tight 'iso-2022-jp
1376 coding-category-iso-7 'iso-2022-7bit
1377 coding-category-iso-8-1 'iso-latin-1
1378 coding-category-iso-8-2 'iso-latin-1
1379 coding-category-iso-7-else 'iso-2022-7bit-lock
1380 coding-category-iso-8-else 'iso-2022-8bit-ss2
1381 coding-category-emacs-mule 'emacs-mule
1382 coding-category-raw-text 'raw-text
1383 coding-category-sjis 'japanese-shift-jis
1384 coding-category-big5 'chinese-big5
1385 coding-category-utf-16-be nil
1386 coding-category-utf-16-le nil
1387 coding-category-utf-8 'mule-utf-8
1388 coding-category-ccl nil
1389 coding-category-binary 'no-conversion)
1391 (set-coding-priority
1392 '(coding-category-iso-8-1
1393 coding-category-iso-8-2
1394 coding-category-iso-7-tight
1395 coding-category-iso-7
1396 coding-category-iso-7-else
1397 coding-category-iso-8-else
1398 coding-category-emacs-mule
1399 coding-category-raw-text
1400 coding-category-sjis
1401 coding-category-big5
1403 coding-category-binary
1404 coding-category-utf-16-be
1405 coding-category-utf-16-le
1406 coding-category-utf-8))
1408 (update-coding-systems-internal)
1410 (set-default-coding-systems nil)
1411 (setq default-sendmail-coding-system 'iso-latin-1)
1412 ;; Preserve eol-type from existing default-process-coding-systems.
1413 ;; On non-unix-like systems in particular, these may have been set
1414 ;; carefully by the user, or by the startup code, to deal with the
1415 ;; users shell appropriately, so should not be altered by changing
1416 ;; language environment.
1417 (let ((output-coding
1418 ;; When bootstrapping, coding-systems are not defined yet, so
1419 ;; we need to catch the error from check-coding-system.
1421 (coding-system-change-text-conversion
1422 (car default-process-coding-system) 'undecided)
1423 (coding-system-error 'undecided)))
1426 (coding-system-change-text-conversion
1427 (cdr default-process-coding-system) 'iso-latin-1)
1428 (coding-system-error 'iso-latin-1))))
1429 (setq default-process-coding-system
1430 (cons output-coding input-coding)))
1432 ;; Don't alter the terminal and keyboard coding systems here.
1433 ;; The terminal still supports the same coding system
1434 ;; that it supported a minute ago.
1435 ;; (set-terminal-coding-system-internal nil)
1436 ;; (set-keyboard-coding-system-internal nil)
1438 (setq nonascii-translation-table nil
1439 nonascii-insert-offset 0))
1441 (reset-language-environment)
1443 (defun set-display-table-and-terminal-coding-system (language-name)
1444 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1445 (let ((coding (get-language-info language-name 'unibyte-display)))
1447 (standard-display-european-internal)
1448 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1449 (aset standard-display-table 146 nil))
1450 (or (eq window-system 'pc)
1451 (set-terminal-coding-system coding))))
1453 (defun set-language-environment (language-name)
1454 "Set up multi-lingual environment for using LANGUAGE-NAME.
1455 This sets the coding system priority and the default input method
1456 and sometimes other things. LANGUAGE-NAME should be a string
1457 which is the name of a language environment. For example, \"Latin-1\"
1458 specifies the character set for the major languages of Western Europe."
1459 (interactive (list (read-language-name
1461 "Set language environment (default, English): ")))
1463 (if (symbolp language-name)
1464 (setq language-name (symbol-name language-name)))
1465 (setq language-name "English"))
1466 (or (assoc-ignore-case language-name language-info-alist)
1467 (error "Language environment not defined: %S" language-name))
1468 (if current-language-environment
1469 (let ((func (get-language-info current-language-environment
1471 (run-hooks 'exit-language-environment-hook)
1472 (if (functionp func) (funcall func))))
1473 (let ((default-eol-type (coding-system-eol-type
1474 default-buffer-file-coding-system)))
1475 (reset-language-environment)
1477 ;; The fetaures might set up coding systems.
1478 (let ((required-features (get-language-info language-name 'features)))
1479 (while required-features
1480 (require (car required-features))
1481 (setq required-features (cdr required-features))))
1483 (setq current-language-environment language-name)
1484 (set-language-environment-coding-systems language-name default-eol-type))
1485 (let ((input-method (get-language-info language-name 'input-method)))
1487 (setq default-input-method input-method)
1488 (if input-method-history
1489 (setq input-method-history
1491 (delete input-method input-method-history))))))
1492 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1494 (if (eq window-system 'pc)
1496 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1498 ((char-table-p nonascii)
1499 (setq nonascii-translation-table nonascii))
1500 ((and (eq window-system 'pc) (boundp dos-table))
1501 ;; DOS terminals' default is to use a special non-ASCII translation
1502 ;; table as appropriate for the installed codepage.
1503 (setq nonascii-translation-table (symbol-value dos-table)))
1504 ((charsetp nonascii)
1505 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1507 ;; Unibyte setups if necessary.
1508 (unless default-enable-multibyte-characters
1509 ;; Syntax and case table.
1510 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1512 (let ((set-case-syntax-set-multibyte nil))
1513 (load syntax nil t))
1514 ;; No information for syntax and case. Reset to the defaults.
1515 (let ((syntax-table (standard-syntax-table))
1516 (case-table (standard-case-table))
1517 (ch (if (eq window-system 'pc) 128 160)))
1519 (modify-syntax-entry ch " " syntax-table)
1520 (aset case-table ch ch)
1522 (set-char-table-extra-slot case-table 0 nil)
1523 (set-char-table-extra-slot case-table 1 nil)
1524 (set-char-table-extra-slot case-table 2 nil))
1525 (set-standard-case-table (standard-case-table))
1526 (let ((list (buffer-list)))
1528 (with-current-buffer (car list)
1529 (set-case-table (standard-case-table)))
1530 (setq list (cdr list))))))
1531 (set-display-table-and-terminal-coding-system language-name))
1533 (let ((required-features (get-language-info language-name 'features)))
1534 (while required-features
1535 (require (car required-features))
1536 (setq required-features (cdr required-features))))
1537 (let ((func (get-language-info language-name 'setup-function)))
1538 (if (functionp func)
1540 (run-hooks 'set-language-environment-hook)
1541 (force-mode-line-update t))
1543 (defun standard-display-european-internal ()
1544 ;; Actually set up direct output of non-ASCII characters.
1545 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1546 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1547 ;; the native font, and codes 160 and 146 stand for something very
1549 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1551 ;; Make non-line-break space display as a plain space.
1552 ;; Most X fonts do the wrong thing for code 160.
1553 (aset standard-display-table 160 [32])
1554 ;; With luck, non-Latin-1 fonts are more recent and so don't
1556 (aset standard-display-table 2208 [32]) ; Latin-1 NBSP
1557 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1558 ;; don't contain a character at that position. Map it to the ASCII
1559 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1560 ;; U+2019, normally from the windows-1252 character set. XFree 4
1561 ;; fonts probably have the appropriate glyph at this position,
1562 ;; so they could use standard-display-8bit. It's better to use a
1563 ;; proper windows-1252 coding system. --fx]
1564 (aset standard-display-table 146 [39]))))
1566 (defun set-language-environment-coding-systems (language-name
1568 "Do various coding system setups for language environment LANGUAGE-NAME.
1570 The optional arg EOL-TYPE specifies the eol-type of the default value
1571 of buffer-file-coding-system set by this function."
1572 (let* ((priority (get-language-info language-name 'coding-priority))
1573 (default-coding (car priority)))
1575 (let ((categories (mapcar 'coding-system-category priority)))
1576 (set-default-coding-systems
1577 (if (memq eol-type '(0 1 2 unix dos mac))
1578 (coding-system-change-eol-conversion default-coding eol-type)
1580 (setq default-sendmail-coding-system default-coding)
1581 (set-coding-priority categories)
1583 (set (car categories) (car priority))
1584 (setq priority (cdr priority) categories (cdr categories)))
1585 (update-coding-systems-internal)))))
1587 ;; Print all arguments with `princ', then print "\n".
1588 (defsubst princ-list (&rest args)
1589 (while args (princ (car args)) (setq args (cdr args)))
1592 (put 'describe-specified-language-support 'apropos-inhibit t)
1594 ;; Print a language specific information such as input methods,
1595 ;; charsets, and coding systems. This function is intended to be
1596 ;; called from the menu:
1597 ;; [menu-bar mule describe-language-environment LANGUAGE]
1598 ;; and should not run it by `M-x describe-current-input-method-function'.
1599 (defun describe-specified-language-support ()
1600 "Describe how Emacs supports the specified language environment."
1602 (let (language-name)
1603 (if (not (and (symbolp last-command-event)
1604 (or (not (eq last-command-event 'Default))
1605 (setq last-command-event 'English))
1606 (setq language-name (symbol-name last-command-event))))
1607 (error "Bogus calling sequence"))
1608 (describe-language-environment language-name)))
1610 (defun describe-language-environment (language-name)
1611 "Describe how Emacs supports language environment LANGUAGE-NAME."
1613 (list (read-language-name
1615 "Describe language environment (default, current choice): ")))
1616 (if (null language-name)
1617 (setq language-name current-language-environment))
1618 (if (or (null language-name)
1619 (null (get-language-info language-name 'documentation)))
1620 (error "No documentation for the specified language"))
1621 (if (symbolp language-name)
1622 (setq language-name (symbol-name language-name)))
1623 (let ((doc (get-language-info language-name 'documentation))
1625 (help-setup-xref (list #'describe-language-environment language-name)
1627 (with-output-to-temp-buffer (help-buffer)
1629 (set-buffer standard-output)
1630 (insert language-name " language environment\n\n")
1632 (insert doc "\n\n"))
1634 (let ((str (eval (get-language-info language-name 'sample-text))))
1636 (insert "Sample text:\n " str "\n\n")))
1638 (let ((input-method (get-language-info language-name 'input-method))
1639 (l (copy-sequence input-method-alist)))
1640 (insert "Input methods")
1642 (insert " (default, " input-method ")")
1643 (setq input-method (assoc input-method input-method-alist))
1644 (setq l (cons input-method (delete input-method l))))
1647 (when (string= language-name (nth 1 (car l)))
1648 (insert " " (car (car l)))
1649 (search-backward (car (car l)))
1650 (help-xref-button 0 'help-input-method (car (car l)))
1651 (goto-char (point-max))
1653 (if (stringp (nth 3 (car l)))
1655 (car (nth 3 (car l))))
1656 "\" in mode line)\n"))
1659 (insert "Character sets:\n")
1660 (let ((l (get-language-info language-name 'charset)))
1662 (insert " nothing specific to " language-name "\n")
1664 (insert " " (symbol-name (car l)))
1665 (search-backward (symbol-name (car l)))
1666 (help-xref-button 0 'help-character-set (car l))
1667 (goto-char (point-max))
1668 (insert ": " (charset-description (car l)) "\n")
1671 (insert "Coding systems:\n")
1672 (let ((l (get-language-info language-name 'coding-system)))
1674 (insert " nothing specific to " language-name "\n")
1676 (insert " " (symbol-name (car l)))
1677 (search-backward (symbol-name (car l)))
1678 (help-xref-button 0 'help-coding-system (car l))
1679 (goto-char (point-max))
1681 (coding-system-mnemonic (car l))
1682 "' in mode line):\n\t"
1683 (coding-system-doc-string (car l))
1685 (let ((aliases (coding-system-get (car l)
1686 'alias-coding-systems)))
1688 (insert "\t(alias:")
1690 (insert " " (symbol-name (car aliases)))
1691 (setq aliases (cdr aliases)))
1693 (setq l (cdr l)))))))))
1697 (defvar locale-translation-file-name nil
1698 "File name for the system's file of locale-name aliases, or nil if none.")
1700 ;; The following definitions might as well be marked as constants and
1701 ;; purecopied, since they're normally used on startup, and probably
1702 ;; should reflect the facilities of the base Emacs.
1703 (defconst locale-language-names
1706 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1707 ;; as specified in the Single Unix Spec, Version 2.
1708 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1709 ;; with additions from ISO 639/RA Newsletter No.1/1989;
1710 ;; see Internet RFC 2165 (1997-06) and
1711 ;; http://www.evertype.com/standards/iso639/iso639-en.html
1712 ;; TERRITORY is a country code taken from ISO 3166
1713 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
1714 ;; CODESET and MODIFIER are implementation-dependent.
1718 ("af" . "Latin-1") ; Afrikaans
1719 ("am" . "Ethiopic") ; Amharic
1720 ; ar Arabic glibc uses 8859-6
1725 ("be" . "Belarussian") ; Belarussian [Byelorussian]
1726 ("bg" . "Bulgarian") ; Bulgarian
1729 ; bn Bengali, Bangla
1731 ("br" . "Latin-1") ; Breton
1732 ("ca" . "Latin-1") ; Catalan
1735 ("cy" . "Latin-8") ; Welsh
1736 ("da" . "Latin-1") ; Danish
1740 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
1741 ("en" . "Latin-1") ; English
1742 ("eo" . "Latin-3") ; Esperanto
1744 ("et" . "Latin-4") ; Estonian
1745 ("eu" . "Latin-1") ; Basque
1747 ("fi" . "Latin-1") ; Finnish
1749 ("fo" . "Latin-1") ; Faroese
1750 ("fr" . "French") ; French
1751 ("fy" . "Latin-1") ; Frisian
1752 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
1753 ("gd" . "Latin-1") ; Scots Gaelic
1754 ("gl" . "Latin-1") ; Galician
1757 ("gv" . "Latin-8") ; Manx Gaelic
1760 ("hi" . "Devanagari") ; Hindi glibc uses utf-8
1761 ("hr" . "Latin-2") ; Croatian
1762 ("hu" . "Latin-2") ; Hungarian
1765 ("id" . "Latin-1") ; Indonesian
1768 ("is" . "Latin-1") ; Icelandic
1769 ("it" . "Latin-1") ; Italian
1773 ("ka" . "Georgian") ; Georgian
1775 ("kl" . "Latin-1") ; Greenlandic
1781 ("kw" . "Latin-1") ; Cornish
1783 ("la" . "Latin-1") ; Latin
1784 ("lb" . "Latin-1") ; Luxemburgish
1786 ("lo" . "Lao") ; Laothian
1787 ("lt" . "Lithuanian")
1788 ("lv" . "Latvian") ; Latvian, Lettish
1790 ("mi" . "Latin-7") ; Maori
1791 ("mk" . "Latin-5") ; Macedonian
1795 ("mr" . "Devanagari") ; Marathi glibc uses utf-8
1796 ("ms" . "Latin-1") ; Malay
1797 ("mt" . "Latin-3") ; Maltese
1800 ("ne" . "Devanagari") ; Nepali
1802 ("no" . "Latin-1") ; Norwegian
1803 ("oc" . "Latin-1") ; Occitan
1807 ("pl" . "Latin-2") ; Polish
1809 ("pt" . "Latin-1") ; Portuguese
1811 ("rm" . "Latin-1") ; Rhaeto-Romanic
1814 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
1815 ("ru" . "Latin-5") ; Russian
1817 ("sa" . "Devanagari") ; Sanskrit
1821 ("sh" . "Latin-2") ; Serbo-Croatian
1824 ("sl" . "Slovenian")
1828 ("sq" . "Latin-1") ; Albanian
1829 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
1833 ("sv" . "Latin-1") ; Swedish
1834 ("sw" . "Latin-1") ; Swahili
1835 ; ta Tamil glibc uses utf-8
1836 ; te Telugu glibc uses utf-8
1837 ("tg" . "Cyrillic-KOI8-T") ; Tajik
1841 ("tl" . "Latin-1") ; Tagalog
1844 ("tr" . "Latin-5") ; Turkish
1849 ("uk" . "Ukrainian") ; Ukrainian
1850 ; ur Urdu glibc uses utf-8
1851 ("uz" . "Latin-1") ; Uzbek
1852 ("vi" . "Vietnamese") ; glibc uses utf-8
1856 ("yi" . "Windows-1255") ; Yiddish
1861 ; zh_CN.GB18030/GB18030 \
1863 ; zh_HK/BIG5-HKSCS \
1865 ; zh_TW.EUC-TW/EUC-TW \
1867 ("zh.*[._]big5" . "Chinese-BIG5")
1868 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
1869 ("zh_tw" . "Chinese-CNS")
1870 ("zh" . "Chinese-GB")
1873 ;; ISO standard locales
1875 ("posix$" . "ASCII")
1877 ;; The "IPA" Emacs language environment does not correspond
1878 ;; to any ISO 639 code, so let it stand for itself.
1881 ;; Nonstandard or obsolete language codes
1882 ("cz" . "Czech") ; e.g. Solaris 2.6
1883 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
1884 ("iw" . "Hebrew") ; e.g. X11R6.4
1885 ("sp" . "Latin-5") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
1886 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
1887 ("jp" . "Japanese") ; e.g. MS Windows
1888 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
1889 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
1891 "List of pairs of locale regexps and language names.
1892 The first element whose locale regexp matches the start of a downcased locale
1893 specifies the language name corresponding to that locale.
1894 If the language name is nil, there is no corresponding language environment.")
1896 (defconst locale-charset-language-names
1898 '((".*8859[-_]?1\\>" . "Latin-1")
1899 (".*8859[-_]?2\\>" . "Latin-2")
1900 (".*8859[-_]?3\\>" . "Latin-3")
1901 (".*8859[-_]?4\\>" . "Latin-4")
1902 (".*8859[-_]?9\\>" . "Latin-5")
1903 (".*8859[-_]?14\\>" . "Latin-8")
1904 (".*8859[-_]?15\\>" . "Latin-9")
1905 (".*@euro\\>" . "Latin-9")
1906 (".*utf\\(-?8\\)\\>" . "UTF-8")))
1907 "List of pairs of locale regexps and charset language names.
1908 The first element whose locale regexp matches the start of a downcased locale
1909 specifies the language name whose charsets corresponds to that locale.
1910 This language name is used if its charsets disagree with the charsets of
1911 the language name that would otherwise be used for this locale.")
1913 (defconst locale-preferred-coding-systems
1915 '(("ja.*[._]euc" . japanese-iso-8bit)
1916 ("ja.*[._]jis7" . iso-2022-jp)
1917 ("ja.*[._]pck" . japanese-shift-jis)
1918 ("ja.*[._]sjis" . japanese-shift-jis)
1919 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
1920 (".*[._]utf" . utf-8)))
1921 "List of pairs of locale regexps and preferred coding systems.
1922 The first element whose locale regexp matches the start of a downcased locale
1923 specifies the coding system to prefer when using that locale.")
1925 (defconst standard-keyboard-coding-systems
1927 '(iso-latin-1 iso-latin-2 iso-latin-3 iso-latin-4 iso-latin-5
1928 iso-latin-6 iso-latin-7 iso-latin-8 iso-latin-9 koi8-u koi8-r))
1929 "Coding systems that are commonly used for keyboards.
1930 `set-locale-environment' will set the `keyboard-coding-system' if the
1931 coding-system specified by the locale setting is a member of this list.")
1933 (defun locale-name-match (key alist)
1934 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
1935 Return the value corresponding to the first regexp that matches the
1936 start of KEY, or nil if there is no match."
1938 (while (and alist (not element))
1939 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
1940 (setq element (car alist)))
1941 (setq alist (cdr alist)))
1944 (defun set-locale-environment (&optional locale-name)
1945 "Set up multi-lingual environment for using LOCALE-NAME.
1946 This sets the language environment, the coding system priority,
1947 the default input method and sometimes other things.
1949 LOCALE-NAME should be a string
1950 which is the name of a locale supported by the system;
1951 often it is of the form xx_XX.CODE, where xx is a language,
1952 XX is a country, and CODE specifies a character set and coding system.
1953 For example, the locale name \"ja_JP.EUC\" might name a locale
1954 for Japanese in Japan using the `japanese-iso-8bit' coding-system.
1956 If LOCALE-NAME is nil, its value is taken from the environment
1957 variables LC_ALL, LC_CTYLE and LANG (the first one that is set).
1959 The locale names supported by your system can typically be found in a
1960 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
1961 will be translated according to the table specified by
1962 `locale-translation-file-name'.
1964 See also `locale-charset-language-names', `locale-language-names',
1965 `locale-preferred-coding-systems' and `locale-coding-system'."
1966 (interactive "sSet environment for locale: ")
1968 ;; Do this at runtime for the sake of binaries possibly transported
1969 ;; to a system without X.
1970 (setq locale-translation-file-name
1972 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
1973 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
1974 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
1976 ;; The following name appears after the X-related names above,
1977 ;; since the X-related names are what X actually uses.
1978 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
1980 (while (and files (not (file-exists-p (car files))))
1981 (setq files (cdr files)))
1984 (let ((locale locale-name))
1987 ;; Use the first of these three environment variables
1988 ;; that has a nonempty value.
1989 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
1990 (while (and vars (not (setq locale (getenv (car vars)))))
1991 (setq vars (cdr vars)))))
1995 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
1996 ;; using the translation file that many systems have.
1997 (when locale-translation-file-name
1999 (insert-file-contents locale-translation-file-name)
2000 (when (re-search-forward
2001 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2002 (setq locale (buffer-substring (point) (line-end-position))))))
2004 ;; Leave the system locales alone if the caller did not specify
2005 ;; an explicit locale name, as their defaults are set from
2006 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2007 ;; want to set them to the same value as LC_CTYPE.
2009 (setq system-messages-locale locale)
2010 (setq system-time-locale locale))
2012 (setq locale (downcase locale))
2014 (let ((language-name
2015 (locale-name-match locale locale-language-names))
2016 (charset-language-name
2017 (locale-name-match locale locale-charset-language-names))
2019 (locale-name-match locale locale-preferred-coding-systems)))
2021 ;; Give preference to charset-language-name over language-name.
2022 (if (and charset-language-name
2024 (equal (get-language-info language-name 'charset)
2025 (get-language-info charset-language-name 'charset))))
2026 (setq language-name charset-language-name))
2030 ;; Set up for this character set. This is now the right way
2031 ;; to do it for both unibyte and multibyte modes.
2032 (set-language-environment language-name)
2034 ;; If default-enable-multibyte-characters is nil,
2035 ;; we are using single-byte characters,
2036 ;; so the display table and terminal coding system are irrelevant.
2037 (when default-enable-multibyte-characters
2038 (set-display-table-and-terminal-coding-system language-name))
2040 ;; Set the `keyboard-coding-system' if appropriate.
2041 (let ((kcs (or coding-system
2042 (car (get-language-info language-name
2044 (if (memq kcs standard-keyboard-coding-systems)
2045 (set-keyboard-coding-system kcs)))
2047 (setq locale-coding-system
2048 (car (get-language-info language-name 'coding-priority))))
2051 (prefer-coding-system coding-system)
2052 (setq locale-coding-system coding-system))))))
2054 ;;; Charset property
2056 (defun get-charset-property (charset propname)
2057 "Return the value of CHARSET's PROPNAME property.
2058 This is the last value stored with
2059 (put-charset-property CHARSET PROPNAME VALUE)."
2060 (and (not (eq charset 'composition))
2061 (plist-get (charset-plist charset) propname)))
2063 (defun put-charset-property (charset propname value)
2064 "Store CHARSETS's PROPNAME property with value VALUE.
2065 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2066 (or (eq charset 'composition)
2067 (set-charset-plist charset
2068 (plist-put (charset-plist charset) propname value))))
2070 ;;; Character code property
2071 (put 'char-code-property-table 'char-table-extra-slots 0)
2073 (defvar char-code-property-table
2074 (make-char-table 'char-code-property-table)
2075 "Char-table containing a property list of each character code.
2077 See also the documentation of `get-char-code-property' and
2078 `put-char-code-property'.")
2080 (defun get-char-code-property (char propname)
2081 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2082 (let ((plist (aref char-code-property-table char)))
2084 (car (cdr (memq propname plist))))))
2086 (defun put-char-code-property (char propname value)
2087 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2088 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2089 (let ((plist (aref char-code-property-table char)))
2091 (let ((slot (memq propname plist)))
2093 (setcar (cdr slot) value)
2094 (nconc plist (list propname value))))
2095 (aset char-code-property-table char (list propname value)))))
2098 ;; Pretty description of encoded string
2100 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2101 (defvar iso-2022-control-alist
2109 (defun encoded-string-description (str coding-system)
2110 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2111 (setq str (string-as-unibyte str))
2113 (if (and coding-system (eq (coding-system-type coding-system) 2))
2114 ;; Try to get a pretty description for ISO 2022 escape sequences.
2115 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2116 (format "%02X" x))))
2117 (function (lambda (x) (format "0x%02X" x))))
2120 (defun encode-coding-char (char coding-system)
2121 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2122 If CODING-SYSTEM can't safely encode CHAR, return nil."
2123 (let ((str1 (string-as-multibyte (char-to-string char)))
2124 (str2 (string-as-multibyte (make-string 2 char)))
2125 (safe-chars (and coding-system
2126 (coding-system-get coding-system 'safe-chars)))
2127 (charset (char-charset char))
2129 (when (or (eq safe-chars t)
2131 (and safe-chars (aref safe-chars char)))
2132 ;; We must find the encoded string of CHAR. But, just encoding
2133 ;; CHAR will put extra control sequences (usually to designate
2134 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
2135 ;; To exclude such tailing bytes, we at first encode one-char
2136 ;; string and two-char string, then check how many bytes at the
2137 ;; tail of both encoded strings are the same.
2139 (setq enc1 (encode-coding-string str1 coding-system)
2141 enc2 (encode-coding-string str2 coding-system)
2143 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2144 (setq i1 (1- i1) i2 (1- i2)))
2146 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2147 ;; and they are the extra control sequences at the tail to
2149 (substring enc2 0 i2))))
2152 ;;; mule-cmds.el ends here