mule.el 57.7 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1 2 3
;;; mule.el --- basic commands for mulitilingual environment

;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
Richard M. Stallman's avatar
Richard M. Stallman committed
4
;; Licensed to the Free Software Foundation.
Karl Heuer's avatar
Karl Heuer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; Keywords: mule, multilingual, character set, coding system

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Karl Heuer's avatar
Karl Heuer committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Karl Heuer's avatar
Karl Heuer committed
24 25 26

;;; Code:

27
(defconst mule-version "5.0 (SAKAKI)" "\
Karl Heuer's avatar
Karl Heuer committed
28 29
Version number and name of this version of MULE (multilingual environment).")

30
(defconst mule-version-date "1999.12.7" "\
Karl Heuer's avatar
Karl Heuer committed
31 32 33
Distribution date of this version of MULE (multilingual environment).")

(defun load-with-code-conversion (fullname file &optional noerror nomessage)
34 35
  "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
The file contents are decoded before evaluation if necessary.
Karl Heuer's avatar
Karl Heuer committed
36 37 38 39 40 41 42 43 44 45 46 47
If optional second arg NOERROR is non-nil,
 report no error if FILE doesn't exist.
Print messages at start and end of loading unless
 optional third arg NOMESSAGE is non-nil.
Return t if file exists."
  (if (null (file-readable-p fullname))
      (and (null noerror)
	   (signal 'file-error (list "Cannot open load file" file)))
    ;; Read file with code conversion, and then eval.
    (let* ((buffer
	    ;; To avoid any autoloading, set default-major-mode to
	    ;; fundamental-mode.
48 49 50 51 52 53
	    ;; So that we don't get completely screwed if the
	    ;; file is encoded in some complicated character set,
	    ;; read it with real decoding, as a multibyte buffer,
	    ;; even if this is a --unibyte Emacs session.
	    (let ((default-major-mode 'fundamental-mode)
		  (default-enable-multibyte-characters t))
Karl Heuer's avatar
Karl Heuer committed
54 55 56
	      ;; We can't use `generate-new-buffer' because files.el
	      ;; is not yet loaded.
	      (get-buffer-create (generate-new-buffer-name " *load*"))))
57 58 59 60 61 62 63 64
	   (load-in-progress t)
	   (source (save-match-data (string-match "\\.el\\'" fullname))))
      (unless nomessage
	(if source
	    (message "Loading %s (source)..." file)
	  (message "Loading %s..." file)))
      (when purify-flag
	(setq preloaded-file-list (cons file preloaded-file-list)))
Karl Heuer's avatar
Karl Heuer committed
65
      (unwind-protect
66
	  (let ((load-file-name fullname)
67
		(set-auto-coding-for-load t)
68
		(inhibit-file-name-operation nil))
Karl Heuer's avatar
Karl Heuer committed
69 70 71
	    (save-excursion
	      (set-buffer buffer)
	      (insert-file-contents fullname)
72 73 74 75 76 77 78 79
	      ;; If the loaded file was inserted with no-conversion or
	      ;; raw-text coding system, make the buffer unibyte.
	      ;; Otherwise, eval-buffer might try to interpret random
	      ;; binary junk as multibyte characters.
	      (if (and enable-multibyte-characters
		       (or (eq (coding-system-type last-coding-system-used) 5)
			   (eq last-coding-system-used 'no-conversion)))
		  (set-buffer-multibyte nil))
Karl Heuer's avatar
Karl Heuer committed
80 81
	      ;; Make `kill-buffer' quiet.
	      (set-buffer-modified-p nil))
82
	    ;; Have the original buffer current while we eval.
83 84 85 86
	    (eval-buffer buffer nil file
			 ;; If this Emacs is running with --unibyte,
			 ;; convert multibyte strings to unibyte
			 ;; after reading them.
87
;;			 (not default-enable-multibyte-characters)
88
			 nil t
89
			 ))
90 91
	(let (kill-buffer-hook kill-buffer-query-functions)
	  (kill-buffer buffer)))
Karl Heuer's avatar
Karl Heuer committed
92
      (let ((hook (assoc file after-load-alist)))
93 94 95 96 97 98
	(when hook
	  (mapcar (function eval) (cdr hook))))
      (unless (or nomessage noninteractive)
	(if source
	    (message "Loading %s (source)...done" file)
	  (message "Loading %s...done" file)))
Karl Heuer's avatar
Karl Heuer committed
99 100 101 102
      t)))

;; API (Application Program Interface) for charsets.

103 104 105 106 107 108 109 110
;; Return t if OBJ is a quoted symbol
;; and the symbol is the name of a standard charset.
(defsubst charset-quoted-standard-p (obj)
  (and (listp obj) (eq (car obj) 'quote)
       (symbolp (car-safe (cdr obj)))
       (let ((vector (get (car-safe (cdr obj)) 'charset)))
	 (and (vectorp vector)
	      (< (aref vector 0) 160)))))
Karl Heuer's avatar
Karl Heuer committed
111 112

(defsubst charsetp (object)
113
  "T if OBJECT is a charset."
Karl Heuer's avatar
Karl Heuer committed
114 115 116 117 118 119 120 121 122 123 124 125
  (and (symbolp object) (vectorp (get object 'charset))))

(defsubst charset-info (charset)
  "Return a vector of information of CHARSET.
The elements of the vector are:
	CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
	LEADING-CODE-BASE, LEADING-CODE-EXT,
	ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
	REVERSE-CHARSET, SHORT-NAME, LONG-NAME,	DESCRIPTION,
	PLIST,
where
CHARSET-ID (integer) is the identification number of the charset.
126 127
BYTES (integer) is the length of multi-byte form of a character in
  the charset: one of 1, 2, 3, and 4.
Karl Heuer's avatar
Karl Heuer committed
128 129 130 131 132 133
DIMENSION (integer) is the number of bytes to represent a character of
the charset: 1 or 2.
CHARS (integer) is the number of characters in a dimension: 94 or 96.
WIDTH (integer) is the number of columns a character in the charset
  occupies on the screen: one of 0, 1, and 2.
DIRECTION (integer) is the rendering direction of characters in the
134 135
  charset when rendering.  If 0, render from left to right, else
  render from right to left.
Karl Heuer's avatar
Karl Heuer committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
LEADING-CODE-BASE (integer) is the base leading-code for the
  charset.
LEADING-CODE-EXT (integer) is the extended leading-code for the
  charset.  All charsets of less than 0xA0 has the value 0.
ISO-FINAL-CHAR (character) is the final character of the
  corresponding ISO 2022 charset.
ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
  while encoding to variants of ISO 2022 coding system, one of the
  following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
REVERSE-CHARSET (integer) is the charset which differs only in
  LEFT-TO-RIGHT value from the charset.  If there's no such a
  charset, the value is -1.
SHORT-NAME (string) is the short name to refer to the charset.
LONG-NAME (string) is the long name to refer to the charset
DESCRIPTION (string) is the description string of the charset.
PLIST (property list) may contain any type of information a user
  want to put and get by functions `put-charset-property' and
  `get-charset-property' respectively."
  (get charset 'charset))

156 157 158 159
;; It is better not to use backquote in this file,
;; because that makes a bootstrapping problem
;; if you need to recompile all the Lisp files using interpreted code.

Karl Heuer's avatar
Karl Heuer committed
160 161
(defmacro charset-id (charset)
  "Return charset identification number of CHARSET."
162
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
163
      (aref (charset-info (nth 1 charset)) 0)
164
    (list 'aref (list 'charset-info charset) 0)))
Karl Heuer's avatar
Karl Heuer committed
165 166

(defmacro charset-bytes (charset)
167 168
  "Return bytes of CHARSET.
See the function `charset-info' for more detail."
169
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
170
      (aref (charset-info (nth 1 charset)) 1)
171
    (list 'aref (list 'charset-info charset) 1)))
Karl Heuer's avatar
Karl Heuer committed
172 173

(defmacro charset-dimension (charset)
174 175
  "Return dimension of CHARSET.
See the function `charset-info' for more detail."
176
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
177
      (aref (charset-info (nth 1 charset)) 2)
178
    (list 'aref (list 'charset-info charset) 2)))
Karl Heuer's avatar
Karl Heuer committed
179 180

(defmacro charset-chars (charset)
181 182
  "Return character numbers contained in a dimension of CHARSET.
See the function `charset-info' for more detail."
183
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
184
      (aref (charset-info (nth 1 charset)) 3)
185
    (list 'aref (list 'charset-info charset) 3)))
Karl Heuer's avatar
Karl Heuer committed
186 187

(defmacro charset-width (charset)
188 189
  "Return width (how many column occupied on a screen) of CHARSET.
See the function `charset-info' for more detail."
190
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
191
      (aref (charset-info (nth 1 charset)) 4)
192
    (list 'aref (list 'charset-info charset) 4)))
Karl Heuer's avatar
Karl Heuer committed
193 194

(defmacro charset-direction (charset)
195 196
  "Return direction of CHARSET.
See the function `charset-info' for more detail."
197
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
198
      (aref (charset-info (nth 1 charset)) 5)
199
    (list 'aref (list 'charset-info charset) 5)))
Karl Heuer's avatar
Karl Heuer committed
200 201

(defmacro charset-iso-final-char (charset)
202 203
  "Return final char of CHARSET.
See the function `charset-info' for more detail."
204
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
205
      (aref (charset-info (nth 1 charset)) 8)
206
    (list 'aref (list 'charset-info charset) 8)))
Karl Heuer's avatar
Karl Heuer committed
207 208

(defmacro charset-iso-graphic-plane (charset)
209 210
  "Return graphic plane of CHARSET.
See the function `charset-info' for more detail."
211
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
212
      (aref (charset-info (nth 1 charset)) 9)
213
    (list 'aref (list 'charset-info charset) 9)))
Karl Heuer's avatar
Karl Heuer committed
214 215

(defmacro charset-reverse-charset (charset)
216 217
  "Return reverse charset of CHARSET.
See the function `charset-info' for more detail."
218
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
219
      (aref (charset-info (nth 1 charset)) 10)
220
    (list 'aref (list 'charset-info charset) 10)))
Karl Heuer's avatar
Karl Heuer committed
221 222

(defmacro charset-short-name (charset)
223 224
  "Return short name of CHARSET.
See the function `charset-info' for more detail."
225
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
226
      (aref (charset-info (nth 1 charset)) 11)
227
    (list 'aref (list 'charset-info charset) 11)))
Karl Heuer's avatar
Karl Heuer committed
228 229

(defmacro charset-long-name (charset)
230 231
  "Return long name of CHARSET.
See the function `charset-info' for more detail."
232
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
233
      (aref (charset-info (nth 1 charset)) 12)
234
    (list 'aref (list 'charset-info charset) 12)))
Karl Heuer's avatar
Karl Heuer committed
235 236

(defmacro charset-description (charset)
Paul Eggert's avatar
Paul Eggert committed
237
  "Return description of CHARSET.
238
See the function `charset-info' for more detail."
239
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
240
      (aref (charset-info (nth 1 charset)) 13)
241
    (list 'aref (list 'charset-info charset) 13)))
Karl Heuer's avatar
Karl Heuer committed
242 243

(defmacro charset-plist (charset)
244 245
  "Return list charset property of CHARSET.
See the function `charset-info' for more detail."
246 247 248 249 250
  (list 'aref
	(if (charset-quoted-standard-p charset)
	    (charset-info (nth 1 charset))
	  (list 'charset-info charset))
	14))
Karl Heuer's avatar
Karl Heuer committed
251 252

(defun set-charset-plist (charset plist)
Karl Heuer's avatar
Karl Heuer committed
253
  "Set CHARSET's property list to PLIST, and return PLIST."
Karl Heuer's avatar
Karl Heuer committed
254 255
  (aset (charset-info  charset) 14 plist))

Eli Zaretskii's avatar
Eli Zaretskii committed
256 257
(defun make-char (charset &optional code1 code2)
  "Return a character of CHARSET whose position codes are CODE1 and CODE2.
Kenichi Handa's avatar
Kenichi Handa committed
258
CODE1 and CODE2 are optional, but if you don't supply
259 260
sufficient position codes, return a generic character which stands for
all characters or group of characters in the character set.
Eli Zaretskii's avatar
Eli Zaretskii committed
261 262
A generic character can be used to index a char table (e.g. syntax-table).
If CODE1 or CODE2 are invalid (out of range), this function signals an error."
263 264 265 266 267 268 269 270 271 272 273 274 275
  (make-char-internal (charset-id charset) c1 c2))

(put 'make-char 'byte-compile
     (function 
      (lambda (form)
	(let ((charset (nth 1 form)))
	  (if (charset-quoted-standard-p charset)
	      (byte-compile-normal-call
	       (cons 'make-char-internal
		     (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
	    (byte-compile-normal-call
	     (cons 'make-char-internal
		   (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
Karl Heuer's avatar
Karl Heuer committed
276

277
(defun charset-list ()
278 279
  "Return list of charsets ever defined.

280
This function is provided for backward compatibility.
281
Now we have the variable `charset-list'."
282 283
  charset-list)

284 285 286
(defsubst generic-char-p (char)
  "Return t if and only if CHAR is a generic character.
See also the documentation of make-char."
287 288 289 290
  (and (>= char 0400)
       (let ((l (split-char char)))
	 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
	      (not (eq (car l) 'composition))))))
291

292
(defun decode-char (ccs code-point &optional restriction)
293
  "Return character specified by coded character set CCS and CODE-POINT in it.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
Return nil if such a character is not supported.
Currently, supported coded character set is `ucs' (ISO/IEC
10646: Universal Multi-Octet Coded Character Set) only.

Optional argument RESTRICTION specifies a way to map the pair of CCS
and CODE-POINT to a chracter.   Currently not supported and just ignored."
  (cond ((eq ccs 'ucs)
	 (cond ((< code-point 128)
		code-point)
	       ((< code-point 256)
		(make-char 'latin-iso8859-1 code-point))
	       ((< code-point #x2500)
		(setq code-point (- code-point #x0100))
		(make-char 'mule-unicode-0100-24ff 
			   (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
	       ((< code-point #x33ff)
		(setq code-point (- code-point #x2500))
		(make-char 'mule-unicode-2500-33ff
			   (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
	       ((and (>= code-point #xe000) (< code-point #x10000))
		(setq code-point (- code-point #xe000))
		(make-char 'mule-unicode-e000-ffff
			   (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
	       ))))

(defun encode-char (char ccs &optional restriction)
320
  "Return code-point in coded character set CCS that corresponds to CHAR.
321 322 323
Return nil if CHAR is not included in CCS.
Currently, supported coded character set is `ucs' (ISO/IEC
10646: Universal Multi-Octet Coded Character Set) only.
324 325

CHAR should be in one of these charsets:
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
  mule-unicode-e000-ffff
Otherwise, return nil.

Optional argument RESTRICTION specifies a way to map CHAR to a
code-point in CCS.  Currently not supported and just ignored."
  (let* ((split (split-char char))
	 (charset (car split)))
    (cond ((eq ccs 'ucs)
	   (cond ((eq charset 'ascii)
		  char)
		 ((eq charset 'latin-iso8859-1)
		  (+ (nth 1 split) 128))
		 ((eq charset 'mule-unicode-0100-24ff)
		  (+ #x0100 (+ (* (- (nth 1 split) 32) 96)
			       (- (nth 2 split) 32))))
		 ((eq charset 'mule-unicode-2500-33ff)
		  (+ #x2500 (+ (* (- (nth 1 split) 32) 96)
			       (- (nth 2 split) 32))))
		 ((eq charset 'mule-unicode-e000-ffff)
		  (+ #xe000 (+ (* (- (nth 1 split) 32) 96)
			       (- (nth 2 split) 32)))))))))

349

350
;; Coding system staffs
Karl Heuer's avatar
Karl Heuer committed
351

352
;; Coding system is a symbol that has the property `coding-system'.
Karl Heuer's avatar
Karl Heuer committed
353
;;
354 355
;; The value of the property `coding-system' is a vector of the
;; following format:
356
;;	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
357
;; We call this vector as coding-spec.  See comments in src/coding.c
358
;; for more detail.  
359 360 361 362

(defconst coding-spec-type-idx 0)
(defconst coding-spec-mnemonic-idx 1)
(defconst coding-spec-doc-string-idx 2)
363
(defconst coding-spec-plist-idx 3)
364 365
(defconst coding-spec-flags-idx 4)

366 367 368 369
;; PLIST is a property list of a coding system.  To share PLIST among
;; alias coding systems, a coding system has PLIST in coding-spec
;; instead of having it in normal property list of Lisp symbol.
;; Here's a list of coding system properties currently being used.
370 371 372 373
;;
;; o coding-category
;;
;; The value is a coding category the coding system belongs to.  The
374 375
;; function `make-coding-system' sets this value automatically 
;; unless its argument PROPERTIES specifies this property.
Karl Heuer's avatar
Karl Heuer committed
376
;;
377
;; o alias-coding-systems
Karl Heuer's avatar
Karl Heuer committed
378
;;
379 380
;; The value is a list of coding systems of the same alias group.  The
;; first element is the coding system made at first, which we call as
381 382
;; `base coding system'.  The function `make-coding-system' sets this
;; value automatically and `define-coding-system-alias' updates it.
383 384 385 386 387 388 389
;;
;; o post-read-conversion
;;
;; The value is a function to call after some text is inserted and
;; decoded by the coding system itself and before any functions in
;; `after-insert-functions' are called.  The arguments to this
;; function is the same as those of a function in
Karl Heuer's avatar
Karl Heuer committed
390 391 392
;; `after-insert-functions', i.e. LENGTH of a text while putting point
;; at the head of the text to be decoded
;;
393 394 395 396 397 398 399 400 401
;; o pre-write-conversion
;;
;; The value is a function to call after all functions in
;; `write-region-annotate-functions' and `buffer-file-format' are
;; called, and before the text is encoded by the coding system itself.
;; The arguments to this function is the same as those of a function
;; in `write-region-annotate-functions', i.e. FROM and TO specifying
;; region of a text.
;;
402
;; o translation-table-for-decode
403
;;
404 405 406
;; The value is a translation table to be applied on decoding.  See
;; the function `make-translation-table' for the format of translation
;; table.
407
;;
408
;; o translation-table-for-encode
409
;;
410
;; The value is a translation table to be applied on encoding.
411
;;
412 413 414 415 416 417
;; o safe-chars
;;
;; The value is a char table.  If a character has non-nil value in it,
;; the character is safely supported by the coding system.  This
;; overrides the specification of safe-charsets.

418 419 420 421 422 423 424 425
;; o safe-charsets
;;
;; The value is a list of charsets safely supported by the coding
;; system.  The value t means that all charsets Emacs handles are
;; supported.  Even if some charset is not in this list, it doesn't
;; mean that the charset can't be encoded in the coding system,
;; instead, it just means that some other receiver of a text encoded
;; in the coding system won't be able to handle that charset.
426 427 428 429 430
;;
;; o mime-charset
;;
;; The value is a symbol of which name is `MIME-charset' parameter of
;; the coding system.
431
;;
432 433 434 435 436 437 438
;; o charset-origin-alist
;;
;; The value is a list of this form:
;;	(CHARSET EXTERNAL-CHARSET-NAME ENCODING-FUNCTION).
;; ENCODING-FUNCTION is a function to encode a character in CHARSET
;; to the code in EXTERNAL-CHARSET-NAME.  The command what-cursor-position
;; uses this information of the buffer-file-coding-system.
439 440 441
;; ENCODING-FUNCTION may be a translation table or a symbol whose
;; property `translation-table' is a translation table.  In these case,
;; the translation table is used to encode the character.
442
;;
443 444 445 446 447
;; o valid-codes (meaningful only for a coding system based on CCL)
;;
;; The value is a list to indicate valid byte ranges of the encoded
;; file.  Each element of the list is an integer or a cons of integer.
;; In the former case, the integer value is a valid byte code.  In the
Karl Heuer's avatar
Karl Heuer committed
448
;; latter case, the integers specifies the range of valid byte codes.
449

450 451 452 453

;; Return coding-spec of CODING-SYSTEM
(defsubst coding-system-spec (coding-system)
  (get (check-coding-system coding-system) 'coding-system))
Karl Heuer's avatar
Karl Heuer committed
454 455

(defun coding-system-type (coding-system)
456 457 458 459
  "Return the coding type of CODING-SYSTEM.
A coding type is an integer value indicating the encoding method
of CODING-SYSTEM.  See the function `make-coding-system' for more detail."
  (aref (coding-system-spec coding-system) coding-spec-type-idx))
Karl Heuer's avatar
Karl Heuer committed
460 461

(defun coding-system-mnemonic (coding-system)
462
  "Return the mnemonic character of CODING-SYSTEM.
463 464 465 466
The mnemonic character of a coding system is used in mode line
to indicate the coding system.  If the arg is nil, return ?-."
  (let ((spec (coding-system-spec coding-system)))
    (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
Karl Heuer's avatar
Karl Heuer committed
467

468
(defun coding-system-doc-string (coding-system)
469 470
  "Return the documentation string for CODING-SYSTEM."
  (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
Karl Heuer's avatar
Karl Heuer committed
471

472
(defun coding-system-plist (coding-system)
473 474
  "Return the property list of CODING-SYSTEM."
  (aref (coding-system-spec coding-system) coding-spec-plist-idx))
475

Karl Heuer's avatar
Karl Heuer committed
476
(defun coding-system-flags (coding-system)
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  "Return `flags' of CODING-SYSTEM.
A `flags' of a coding system is a vector of length 32 indicating detailed
information of a coding system.  See the function `make-coding-system'
for more detail."
  (aref (coding-system-spec coding-system) coding-spec-flags-idx))

(defun coding-system-get (coding-system prop)
  "Extract a value from CODING-SYSTEM's property list for property PROP."
  (plist-get (coding-system-plist coding-system) prop))

(defun coding-system-put (coding-system prop val)
  "Change value in CODING-SYSTEM's property list PROP to VAL."
  (let ((plist (coding-system-plist coding-system)))
    (if plist
	(plist-put plist prop val)
      (aset (coding-system-spec coding-system) coding-spec-plist-idx
	    (list prop val)))))

(defun coding-system-category (coding-system)
  "Return the coding category of CODING-SYSTEM."
  (coding-system-get coding-system 'coding-category))

(defun coding-system-base (coding-system)
  "Return the base coding system of CODING-SYSTEM.
501 502
A base coding system is what made by `make-coding-system'.
Any alias nor subsidiary coding systems are not base coding system."
503 504 505
  (car (coding-system-get coding-system 'alias-coding-systems)))

(defalias 'coding-system-parent 'coding-system-base)
506
(make-obsolete 'coding-system-parent 'coding-system-base "20.3")
507 508 509 510 511 512 513 514 515 516

;; Coding system also has a property `eol-type'.
;;
;; This property indicates how the coding system handles end-of-line
;; format.  The value is integer 0, 1, 2, or a vector of three coding
;; systems.  Each integer value 0, 1, and 2 indicates the format of
;; end-of-line LF, CRLF, and CR respectively.  A vector value
;; indicates that the format of end-of-line should be detected
;; automatically.  Nth element of the vector is the subsidiary coding
;; system whose `eol-type' property is N.
Karl Heuer's avatar
Karl Heuer committed
517

518
(defun coding-system-eol-type (coding-system)
519 520
  "Return eol-type of CODING-SYSTEM.
An eol-type is integer 0, 1, 2, or a vector of coding systems.
Karl Heuer's avatar
Karl Heuer committed
521

522 523 524 525 526 527 528
Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
CRLF, and CR respectively.

A vector value indicates that a format of end-of-line should be
detected automatically.  Nth element of the vector is the subsidiary
coding system whose eol-type is N."
  (get coding-system 'eol-type))
529

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
(defun coding-system-lessp (x y)
  (cond ((eq x 'no-conversion) t)
	((eq y 'no-conversion) nil)
	((eq x 'emacs-mule) t)
	((eq y 'emacs-mule) nil)
	((eq x 'undecided) t)
	((eq y 'undecided) nil)
	(t (let ((c1 (coding-system-mnemonic x))
		 (c2 (coding-system-mnemonic y)))
	     (or (< (downcase c1) (downcase c2))
		 (and (not (> (downcase c1) (downcase c2)))
		      (< c1 c2)))))))

;; Add CODING-SYSTEM to coding-system-list while keeping it sorted.
(defun add-to-coding-system-list (coding-system)
  (if (or (null coding-system-list)
	  (coding-system-lessp coding-system (car coding-system-list)))
      (setq coding-system-list (cons coding-system coding-system-list))
    (let ((len (length coding-system-list))
	  mid (tem coding-system-list))
      (while (> len 1)
	(setq mid (nthcdr (/ len 2) tem))
	(if (coding-system-lessp (car mid) coding-system)
	    (setq tem mid
		  len (- len (/ len 2)))
	  (setq len (/ len 2))))
      (setcdr tem (cons coding-system (cdr tem))))))

558
(defun coding-system-list (&optional base-only)
559 560 561 562 563
  "Return a list of all existing non-subsidiary coding systems.
If optional arg BASE-ONLY is non-nil, only base coding systems are listed.
The value doesn't include subsidiary coding systems which are what
made from bases and aliases automatically for various end-of-line
formats (e.g. iso-latin-1-unix, koi8-r-dos)."
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  (let* ((codings (copy-sequence coding-system-list))
	 (tail (cons nil codings)))
    ;; Remove subsidiary coding systems (eol variants) and alias
    ;; coding systems (if necessary).
    (while (cdr tail)
      (let* ((coding (car (cdr tail)))
	     (aliases (coding-system-get coding 'alias-coding-systems)))
	(if (or
	     ;; CODING is an eol variant if not in ALIASES.
	     (not (memq coding aliases))
	     ;; CODING is an alias if it is not car of ALIASES.
	     (and base-only (not (eq coding (car aliases)))))
	    (setcdr tail (cdr (cdr tail)))
	  (setq tail (cdr tail)))))
    codings))

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
(defun register-char-codings (coding-system safe-chars)
  (let ((general (char-table-extra-slot char-coding-system-table 0)))
    (if (eq safe-chars t)
	(or (memq coding-system general)
	    (set-char-table-extra-slot char-coding-system-table 0
				       (cons coding-system general)))
      (map-char-table
       (function
	(lambda (key val)
	  (if (and (>= key 128) val)
	      (let ((codings (aref char-coding-system-table key)))
		(or (memq coding-system codings)
		    (aset char-coding-system-table key
			  (cons coding-system codings)))))))
       safe-chars))))


597 598
;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
(defun make-subsidiary-coding-system (coding-system)
599 600
  (let ((coding-spec (coding-system-spec coding-system))
	(subsidiaries (vector (intern (format "%s-unix" coding-system))
601 602
			      (intern (format "%s-dos" coding-system))
			      (intern (format "%s-mac" coding-system))))
603 604
	(i 0)
	temp)
605
    (while (< i 3)
606
      (put (aref subsidiaries i) 'coding-system coding-spec)
607
      (put (aref subsidiaries i) 'eol-type i)
608
      (add-to-coding-system-list (aref subsidiaries i))
609 610 611
      (setq coding-system-alist
	    (cons (list (symbol-name (aref subsidiaries i)))
		  coding-system-alist))
612 613
      (setq i (1+ i)))
    subsidiaries))
Karl Heuer's avatar
Karl Heuer committed
614

615
(defun make-coding-system (coding-system type mnemonic doc-string
616 617 618 619
					 &optional
					 flags
					 properties
					 eol-type)
620
  "Define a new coding system CODING-SYSTEM (symbol).
621
Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional), 
622
and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
623
in the following format:
624
	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
625 626

TYPE is an integer value indicating the type of the coding system as follows:
Karl Heuer's avatar
Karl Heuer committed
627 628 629 630
  0: Emacs internal format,
  1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
  2: ISO-2022 including many variants,
  3: Big5 used mainly on Chinese PC,
631 632
  4: private, CCL programs provide encoding/decoding algorithm,
  5: Raw-text, which means that text contains random 8-bit codes. 
633

634
MNEMONIC is a character to be displayed on mode line for the coding system.
635

636
DOC-STRING is a documentation string for the coding system.
637

638
FLAGS specifies more detailed information of the coding system as follows:
639

640
  If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
Karl Heuer's avatar
Karl Heuer committed
641 642
      CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
      ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
643
      USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
644
      SAFE, ACCEPT-LATIN-EXTRA-CODE.
Karl Heuer's avatar
Karl Heuer committed
645 646 647 648 649
    CHARSETn are character sets initially designated to Gn graphic registers.
      If CHARSETn is nil, Gn is never used.
      If CHARSETn is t, Gn can be used but nothing designated initially.
      If CHARSETn is a list of character sets, those character sets are
        designated to Gn on output, but nothing designated to Gn initially.
650
        But, character set `ascii' can be designated only to G0.
Karl Heuer's avatar
Karl Heuer committed
651 652 653 654 655 656 657 658 659 660
    SHORT-FORM non-nil means use short designation sequence on output.
    ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
    ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
      SPACE on output.
    SEVEN non-nil means use 7-bit code only on output.
    LOCKING-SHIFT non-nil means use locking-shift.
    SINGLE-SHIFT non-nil means use single-shift.
    USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
    USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
    NO-ISO6429 non-nil means not use ISO6429's direction specification.
661 662 663 664
    INIT-BOL non-nil means any designation state is assumed to be reset
      to initial at each beginning of line on output.
    DESIGNATION-BOL non-nil means designation sequences should be placed
      at beginning of line on output.
665
    SAFE non-nil means convert unsafe characters to `?' on output.
666 667
      Characters not specified in the property `safe-charsets' nor
      `safe-chars' are unsafe.
668 669 670
    ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
      a code specified in `latin-extra-code-table' (which see) as a valid
      code of the coding system.
671

672 673 674
  If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
    decoding and encoding.  CCL programs should be specified by their
    symbols.
675 676 677 678 679

PROPERTIES is an alist of properties vs the corresponding values.
These properties are set in PLIST, a property list.  This function
also sets properties `coding-category' and `alias-coding-systems'
automatically.
Karl Heuer's avatar
Karl Heuer committed
680

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
EOL-TYPE specifies the EOL type of the coding-system in one of the
following formats:

  o symbol (unix, dos, or mac)

	The symbol `unix' means Unix-like EOL (LF), `dos' means
	DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).

  o number (0, 1, or 2)

	The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
	respectively.

  o vector of coding-systems of length 3

	The EOL type is detected automatically for the coding system.
	And, according to the detected EOL type, one of the coding
	systems in the vector is selected.  Elements of the vector
Stefan Monnier's avatar
Typo.  
Stefan Monnier committed
699
	corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
700 701
	in this order.

702 703 704 705 706 707 708
Kludgy features for backward compatibility:

1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
treated as a compiled CCL code.

2. If PROPERTIES is just a list of character sets, the list is set as
a value of `safe-charsets' in PLIST."
709
  ;; Set a value of `coding-system' property.
710
  (let ((coding-spec (make-vector 5 nil))
711 712
	(no-initial-designation t)
	(no-alternative-designation t)
713
	coding-category)
714
    (if (or (not (integerp type)) (< type 0) (> type 5))
715
	(error "TYPE argument must be 0..5"))
716
    (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
Karl Heuer's avatar
Karl Heuer committed
717
	(error "MNEMONIC argument must be an ASCII printable character."))
718 719 720
    (aset coding-spec coding-spec-type-idx type)
    (aset coding-spec coding-spec-mnemonic-idx mnemonic)
    (aset coding-spec coding-spec-doc-string-idx
721
	  (purecopy (if (stringp doc-string) doc-string "")))
722 723 724 725 726
    (cond ((= type 0)
	   (setq coding-category 'coding-category-emacs-mule))
	  ((= type 1)
	   (setq coding-category 'coding-category-sjis))
	  ((= type 2)			; ISO2022
Karl Heuer's avatar
Karl Heuer committed
727
	   (let ((i 0)
728
		 (vec (make-vector 32 nil))
729 730
		 (g1-designation nil)
		 (fl flags))
Karl Heuer's avatar
Karl Heuer committed
731
	     (while (< i 4)
732
	       (let ((charset (car fl)))
733 734 735 736 737 738 739 740 741 742 743 744 745
		 (if (and no-initial-designation
			  (> i 0)
			  (or (charsetp charset)
			      (and (consp charset)
				   (charsetp (car charset)))))
		     (setq no-initial-designation nil))
		 (if (charsetp charset)
		     (if (= i 1) (setq g1-designation charset))
		   (if (consp charset)
		       (let ((tail charset)
			     elt)
			 (while tail
			   (setq elt (car tail))
746 747 748 749
			   (if (eq elt t)
			       (setq no-alternative-designation nil)
			     (if (and elt (not (charsetp elt)))
				 (error "Invalid charset: %s" elt)))
750 751
			   (setq tail (cdr tail)))
			 (setq g1-designation (car charset)))
752 753 754 755
		     (if charset
			 (if (eq charset t)
			     (setq no-alternative-designation nil)
			   (error "Invalid charset: %s" charset)))))
Karl Heuer's avatar
Karl Heuer committed
756
		 (aset vec i charset))
757 758 759 760
	       (setq fl (cdr fl) i (1+ i)))
	     (while (and (< i 32) fl)
	       (aset vec i (car fl))
	       (setq fl (cdr fl) i (1+ i)))
761 762 763
	     (aset coding-spec 4 vec)
	     (setq coding-category
		   (if (aref vec 8)	; Use locking-shift.
764 765
		       (or (and (aref vec 7) 'coding-category-iso-7-else)
			   'coding-category-iso-8-else)
766 767
		     (if (aref vec 7)	; 7-bit only.
			 (if (aref vec 9) ; Use single-shift.
768
			     'coding-category-iso-7-else
769 770 771
			   (if no-alternative-designation
			       'coding-category-iso-7-tight
			     'coding-category-iso-7))
772 773
		       (if (or no-initial-designation
			       (not no-alternative-designation))
774
			   'coding-category-iso-8-else
775 776 777 778 779 780 781
			 (if (and (charsetp g1-designation)
				  (= (charset-dimension g1-designation) 2))
			     'coding-category-iso-8-2
			   'coding-category-iso-8-1)))))))
	  ((= type 3)
	   (setq coding-category 'coding-category-big5))
	  ((= type 4)			; private
782
	   (setq coding-category 'coding-category-ccl)
783 784 785 786 787 788 789 790 791 792 793
	   (if (not (consp flags))
	       (error "Invalid FLAGS argument for TYPE 4 (CCL)")
	     (let ((decoder (check-ccl-program
			     (car flags)
			     (intern (format "%s-decoder" coding-system))))
		   (encoder (check-ccl-program
			     (cdr flags)
			     (intern (format "%s-encoder" coding-system)))))
	       (if (and decoder encoder)
		   (aset coding-spec 4 (cons decoder encoder))
		 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
794 795
	  (t				; i.e. (= type 5)
	   (setq coding-category 'coding-category-raw-text)))
796 797

    (let ((plist (list 'coding-category coding-category
798
		       'alias-coding-systems (list coding-system))))
799
      (if no-initial-designation
800 801 802 803 804 805
	  (plist-put plist 'no-initial-designation t))
      (if (and properties
	       (or (eq properties t)
		   (not (consp (car properties)))))
	  ;; In the old version, the arg PROPERTIES is a list to be
	  ;; set in PLIST as a value of property `safe-charsets'.
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
	  (setq properties (list (cons 'safe-charsets properties))))
      ;; In the current version PROPERTIES is a property list.
      ;; Reflect it into PLIST one by one while handling safe-chars
      ;; specially.
      (let ((safe-charsets (cdr (assq 'safe-charsets properties)))
	    (safe-chars (cdr (assq 'safe-chars properties)))
	    (l properties)
	    prop val)
	;; If only safe-charsets is specified, make a char-table from
	;; it, and store that char-table as the value of `safe-chars'.
	(if (and (not safe-chars) safe-charsets)
	    (let (charset)
	      (if (eq safe-charsets t)
		  (setq safe-chars t)
		(setq safe-chars (make-char-table 'safe-chars))
		(while safe-charsets
		  (setq charset (car safe-charsets)
			safe-charsets (cdr safe-charsets))
		  (cond ((eq charset 'ascii)) ; just ignore
			((eq charset 'eight-bit-control)
			 (let ((i 128))
			   (while (< i 160)
			     (aset safe-chars i t)
			     (setq i (1+ i)))))
			((eq charset 'eight-bit-graphic)
			 (let ((i 160))
			   (while (< i 256)
			     (aset safe-chars i t)
			     (setq i (1+ i)))))
			(t
			 (aset safe-chars (make-char charset) t)))))
	      (setq l (cons (cons 'safe-chars safe-chars) l))))
	(while l
	  (setq prop (car (car l)) val (cdr (car l)) l (cdr l))
	  (if (eq prop 'safe-chars)
	      (progn
842 843 844 845 846
		(if (and (symbolp val)
			 (get val 'translation-table))
		    (setq safe-chars (get val 'translation-table)))
		(register-char-codings coding-system safe-chars)
		(setq val safe-chars)))
847
	  (plist-put plist prop val)))
848 849 850
      ;; The property `coding-category' may have been set differently
      ;; through PROPERTIES.
      (setq coding-category (plist-get plist 'coding-category))
851
      (aset coding-spec coding-spec-plist-idx plist))
852 853 854
    (put coding-system 'coding-system coding-spec)
    (put coding-category 'coding-systems
	 (cons coding-system (get coding-category 'coding-systems))))
Karl Heuer's avatar
Karl Heuer committed
855

856
  ;; Next, set a value of `eol-type' property.
857
  (if (not eol-type)
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
      ;; If EOL-TYPE is nil, set a vector of subsidiary coding
      ;; systems, each corresponds to a coding system for the detected
      ;; EOL format.
      (setq eol-type (make-subsidiary-coding-system coding-system)))
  (setq eol-type
	(cond ((or (eq eol-type 'unix) (null eol-type))
	       0)
	      ((eq eol-type 'dos)
	       1)
	      ((eq eol-type 'mac)
	       2)
	      ((or (and (vectorp eol-type)
			(= (length eol-type) 3))
		   (and (numberp eol-type)
			(and (>= eol-type 0)
			     (<= eol-type 2))))
	       eol-type)
	      (t
	       (error "Invalid EOL-TYPE spec:%S" eol-type))))
  (put coding-system 'eol-type eol-type)
878 879 880

  ;; At last, register CODING-SYSTEM in `coding-system-list' and
  ;; `coding-system-alist'.
881
  (add-to-coding-system-list coding-system)
882
  (setq coding-system-alist (cons (list (symbol-name coding-system))
883
				  coding-system-alist))
884 885 886 887 888 889 890

  ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
  ;; XXX-with-esc variants.
  (let ((coding-category (coding-system-category coding-system)))
    (if (or (eq coding-category 'coding-category-iso-8-1)
	    (eq coding-category 'coding-category-iso-8-2))
	(let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
891 892 893 894 895 896 897 898
	      (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system))
	      (safe-charsets (assq 'safe-charsets properties))
	      (mime-charset (assq 'mime-charset properties)))
	  (if safe-charsets
	      (setcdr safe-charsets t)
	    (setq properties (cons (cons 'safe-charsets t) properties)))
	  (if mime-charset
	      (setcdr mime-charset nil))
899 900 901 902
	  (make-coding-system esc type mnemonic doc
			      (if (listp (car flags))
				  (cons (append (car flags) '(t)) (cdr flags))
				(cons (list (car flags) t) (cdr flags)))
903
			      properties))))
904

905
  coding-system)
906

907
(defun define-coding-system-alias (alias coding-system)
Richard M. Stallman's avatar
Richard M. Stallman committed
908
  "Define ALIAS as an alias for coding system CODING-SYSTEM."
909 910
  (put alias 'coding-system (coding-system-spec coding-system))
  (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
911
  (add-to-coding-system-list alias)
912 913 914 915 916 917
  (setq coding-system-alist (cons (list (symbol-name alias))
				  coding-system-alist))
  (let ((eol-type (coding-system-eol-type coding-system)))
    (if (vectorp eol-type)
	(put alias 'eol-type (make-subsidiary-coding-system alias))
      (put alias 'eol-type eol-type))))
Karl Heuer's avatar
Karl Heuer committed
918 919

(defun set-buffer-file-coding-system (coding-system &optional force)
Richard M. Stallman's avatar
Richard M. Stallman committed
920 921 922 923 924 925 926 927
  "Set the file coding-system of the current buffer to CODING-SYSTEM.
This means that when you save the buffer, it will be converted
according to CODING-SYSTEM.  For a list of possible values of CODING-SYSTEM,
use \\[list-coding-systems].

If the buffer's previous file coding-system value specifies end-of-line
conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
merged with the already-specified end-of-line conversion.
928 929 930 931 932 933 934

If the buffer's previous file coding-system value specifies text
conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
merged with the already-specified text conversion.

However, if the optional prefix argument FORCE is non-nil, then
CODING-SYSTEM is used exactly as specified.
Kenichi Handa's avatar
Kenichi Handa committed
935 936 937 938 939

This marks the buffer modified so that the succeeding \\[save-buffer]
surely saves the buffer with CODING-SYSTEM.  From a program, if you
don't want to mark the buffer modified, just set the variable
`buffer-file-coding-system' directly."
940
  (interactive "zCoding system for visited file (default, nil): \nP")
Karl Heuer's avatar
Karl Heuer committed
941
  (check-coding-system coding-system)
942
  (if (and coding-system buffer-file-coding-system (null force))
943 944 945 946 947 948 949 950 951 952 953 954 955
      (let ((base (coding-system-base buffer-file-coding-system))
	    (eol (coding-system-eol-type buffer-file-coding-system)))
	;; If CODING-SYSTEM doesn't specify text conversion, merge
	;; with that of buffer-file-coding-system.
	(if (eq (coding-system-base coding-system) 'undecided)
	    (setq coding-system (coding-system-change-text-conversion
				 coding-system base)))
	;; If CODING-SYSTEM doesn't specify eol conversion, merge with
	;; that of buffer-file-coding-system.
	(if (and (vectorp (coding-system-eol-type coding-system))
		 (numberp eol) (>= eol 0) (<= eol 2))
	    (setq coding-system (coding-system-change-eol-conversion
				 coding-system eol)))))
Karl Heuer's avatar
Karl Heuer committed
956 957 958 959
  (setq buffer-file-coding-system coding-system)
  (set-buffer-modified-p t)
  (force-mode-line-update))

Richard M. Stallman's avatar
Richard M. Stallman committed
960 961 962 963 964
(defvar default-terminal-coding-system nil
  "Default value for the terminal coding system.
This is normally set according to the selected language environment.
See also the command `set-terminal-coding-system'.")

965 966
(defun set-terminal-coding-system (coding-system)
  "Set coding system of your terminal to CODING-SYSTEM.
Richard M. Stallman's avatar
Richard M. Stallman committed
967 968 969 970 971 972
All text output to the terminal will be encoded
with the specified coding system.
For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
The default is determined by the selected language environment
or by the previous use of this command."
  (interactive
973 974 975 976 977 978 979
   (list (let ((default (if (and (not (terminal-coding-system))
				 default-terminal-coding-system)
			    default-terminal-coding-system)))
	   (read-coding-system
	    (format "Coding system for terminal display (default, %s): "
		    default)
	    default))))
Richard M. Stallman's avatar
Richard M. Stallman committed
980 981 982 983 984
  (if (and (not coding-system)
	   (not (terminal-coding-system)))
      (setq coding-system default-terminal-coding-system))
  (if coding-system
      (setq default-terminal-coding-system coding-system))      
985 986 987
  (set-terminal-coding-system-internal coding-system)
  (redraw-frame (selected-frame)))

Richard M. Stallman's avatar
Richard M. Stallman committed
988 989 990 991 992
(defvar default-keyboard-coding-system nil
  "Default value of the keyboard coding system.
This is normally set according to the selected language environment.
See also the command `set-keyboard-coding-system'.")

993
(defun set-keyboard-coding-system (coding-system)
Richard M. Stallman's avatar
Richard M. Stallman committed
994 995
  "Set coding system for keyboard input to CODING-SYSTEM.
In addition, this command enables Encoded-kbd minor mode.
996
\(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
Richard M. Stallman's avatar
Richard M. Stallman committed
997 998 999 1000
For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
The default is determined by the selected language environment
or by the previous use of this command."
  (interactive
1001 1002 1003 1004 1005 1006 1007
   (list (let ((default (if (and (not (keyboard-coding-system))
				 default-keyboard-coding-system)
			    default-keyboard-coding-system)))
	   (read-coding-system
	    (format "Coding system for keyboard input (default, %s): "
		    default)
	    default))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1008 1009 1010 1011 1012
  (if (and (not coding-system)
	   (not (keyboard-coding-system)))
      (setq coding-system default-keyboard-coding-system))
  (if coding-system
      (setq default-keyboard-coding-system coding-system))
1013 1014 1015 1016
  (set-keyboard-coding-system-internal coding-system)
  (encoded-kbd-mode (if coding-system 1 0)))

(defun set-buffer-process-coding-system (decoding encoding)
Richard M. Stallman's avatar
Richard M. Stallman committed
1017
  "Set coding systems for the process associated with the current buffer.
1018
DECODING is the coding system to be used to decode input from the process,
Richard M. Stallman's avatar
Richard M. Stallman committed
1019 1020 1021
ENCODING is the coding system to be used to encode output to the process.

For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
Karl Heuer's avatar
Karl Heuer committed
1022
  (interactive
1023
   "zCoding-system for output from the process: \nzCoding-system for input to the process: ")
Karl Heuer's avatar
Karl Heuer committed
1024 1025 1026
  (let ((proc (get-buffer-process (current-buffer))))
    (if (null proc)
	(error "no process")
1027 1028 1029
      (check-coding-system decoding)
      (check-coding-system encoding)
      (set-process-coding-system proc decoding encoding)))
Karl Heuer's avatar
Karl Heuer committed
1030 1031
  (force-mode-line-update))

1032 1033
(defalias 'set-clipboard-coding-system 'set-selection-coding-system)

1034
(defun set-selection-coding-system (coding-system)
1035 1036 1037
  "Make CODING-SYSTEM used for communicating with other X clients .
When sending or receiving text via cut_buffer, selection, and clipboard,
the text is encoded or decoded by CODING-SYSTEM."
1038
  (interactive "zCoding system for X selection: ")
1039
  (check-coding-system coding-system)
1040
  (setq selection-coding-system coding-system))
1041

Karl Heuer's avatar
Karl Heuer committed
1042
;; Coding system lastly specified by the command
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053