mirrored from git://git.sv.gnu.org/emacs.git
/
mule.el
2549 lines (2249 loc) · 98.6 KB
/
mule.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; mule.el --- basic commands for multilingual environment -*- lexical-binding: t; -*-
;; Copyright (C) 1997-2021 Free Software Foundation, Inc.
;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
;; National Institute of Advanced Industrial Science and Technology (AIST)
;; Registration Number H14PRO021
;; Copyright (C) 2003
;; National Institute of Advanced Industrial Science and Technology (AIST)
;; Registration Number H13PRO009
;; 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 3 of the License, 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
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(defconst mule-version "6.0 (HANACHIRUSATO)" "\
Version number and name of this version of MULE (multilingual environment).")
(make-obsolete-variable 'mule-version nil "28.1")
(defconst mule-version-date "2003.9.1" "\
Distribution date of this version of MULE (multilingual environment).")
(make-obsolete-variable 'mule-version-date nil "28.1")
;;; CHARSET
;; Backward compatibility code for handling emacs-mule charsets.
(defvar private-char-area-1-min #xF0000)
(defvar private-char-area-1-max #xFFFFE)
(defvar private-char-area-2-min #x100000)
(defvar private-char-area-2-max #x10FFFE)
;; Table of emacs-mule charsets indexed by their emacs-mule ID.
(defvar emacs-mule-charset-table (make-vector 256 nil))
(aset emacs-mule-charset-table 0 'ascii)
;; Convert the argument of old-style call of define-charset to a
;; property list used by the new-style.
;; INFO-VECTOR is a vector of the format:
;; [DIMENSION CHARS WIDTH DIRECTION ISO-FINAL-CHAR ISO-GRAPHIC-PLANE
;; SHORT-NAME LONG-NAME DESCRIPTION]
(defun convert-define-charset-argument (emacs-mule-id info-vector)
(let* ((dim (aref info-vector 0))
(chars (aref info-vector 1))
(total (if (= dim 1) chars (* chars chars)))
(code-space (if (= dim 1) (if (= chars 96) [32 127] [33 126])
(if (= chars 96) [32 127 32 127] [33 126 33 126])))
code-offset)
(if (integerp emacs-mule-id)
(or (= emacs-mule-id 0)
(and (>= emacs-mule-id 129) (< emacs-mule-id 256))
(error "Invalid CHARSET-ID: %d" emacs-mule-id))
(let (from-id to-id)
(if (= dim 1) (setq from-id 160 to-id 224)
(setq from-id 224 to-id 255))
(while (and (< from-id to-id)
(not (aref emacs-mule-charset-table from-id)))
(setq from-id (1+ from-id)))
(if (= from-id to-id)
(error "No more room for the new Emacs-mule charset"))
(setq emacs-mule-id from-id)))
(if (> (- private-char-area-1-max private-char-area-1-min) total)
(setq code-offset private-char-area-1-min
private-char-area-1-min (+ private-char-area-1-min total))
(if (> (- private-char-area-2-max private-char-area-2-min) total)
(setq code-offset private-char-area-2-min
private-char-area-2-min (+ private-char-area-2-min total))
(error "No more space for a new charset")))
(list :dimension dim
:code-space code-space
:iso-final-char (aref info-vector 4)
:code-offset code-offset
:emacs-mule-id emacs-mule-id)))
(defun define-charset (name docstring &rest props)
"Define NAME (symbol) as a charset with DOCSTRING.
The remaining arguments must come in pairs ATTRIBUTE VALUE. ATTRIBUTE
may be any symbol. The following have special meanings, and one of
`:code-offset', `:map', `:subset', `:superset' must be specified.
`:short-name'
VALUE must be a short string to identify the charset. If omitted,
NAME is used.
`:long-name'
VALUE must be a string longer than `:short-name' to identify the
charset. If omitted, the value of the `:short-name' attribute is used.
`:dimension'
VALUE must be an integer 0, 1, 2, or 3, specifying the dimension of
code-points of the charsets. If omitted, it is calculated from the
value of the `:code-space' attribute.
`:code-space'
VALUE must be a vector of length at most 8 specifying the byte code
range of each dimension in this format:
[ MIN-1 MAX-1 MIN-2 MAX-2 ... ]
where MIN-N is the minimum byte value of Nth dimension of code-point,
MAX-N is the maximum byte value of that.
`:min-code'
VALUE must be an integer specifying the minimum code point of the
charset. If omitted, it is calculated from `:code-space'. VALUE may
be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of
the code point and LOW is the least significant 16 bits.
`:max-code'
VALUE must be an integer specifying the maximum code point of the
charset. If omitted, it is calculated from `:code-space'. VALUE may
be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of
the code point and LOW is the least significant 16 bits.
`:iso-final-char'
VALUE must be a character in the range 32 to 127 (inclusive)
specifying the final char of the charset for ISO-2022 encoding. If
omitted, the charset can't be encoded by ISO-2022 based
coding-systems.
`:iso-revision-number'
VALUE must be an integer in the range 0..63, specifying the revision
number of the charset for ISO-2022 encoding.
`:emacs-mule-id'
VALUE must be an integer of 0, 129..255. If omitted, the charset
can't be encoded by coding-systems of type `emacs-mule'.
`:ascii-compatible-p'
VALUE must be nil or t (default nil). If VALUE is t, the charset is
compatible with ASCII, i.e. the first 128 code points map to ASCII.
`:supplementary-p'
VALUE must be nil or t. If the VALUE is t, the charset is
supplementary, which means it is used only as a parent or a
subset of some other charset, or it is provided just for backward
compatibility.
`:invalid-code'
VALUE must be a nonnegative integer that can be used as an invalid
code point of the charset. If the minimum code is 0 and the maximum
code is greater than Emacs's maximum integer value, `:invalid-code'
should not be omitted.
`:code-offset'
VALUE must be an integer added to the index number of a character to
get the corresponding character code.
`:map'
VALUE must be vector or string.
If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ],
where CODE-n is a code-point of the charset, and CHAR-n is the
corresponding character code.
If it is a string, it is a name of file that contains the above
information. Each line of the file must be this format:
0xXXX 0xYYY
where XXX is a hexadecimal representation of CODE-n and YYY is a
hexadecimal representation of CHAR-n. A line starting with `#' is a
comment line.
`:subset'
VALUE must be a list:
( PARENT MIN-CODE MAX-CODE OFFSET )
PARENT is a parent charset. MIN-CODE and MAX-CODE specify the range
of characters inherited from the parent. OFFSET is an integer value
to add to a code point of the parent charset to get the corresponding
code point of this charset.
`:superset'
VALUE must be a list of parent charsets. The charset inherits
characters from them. Each element of the list may be a cons (PARENT
. OFFSET), where PARENT is a parent charset, and OFFSET is an offset
value to add to a code point of PARENT to get the corresponding code
point of this charset.
`:unify-map'
VALUE must be vector or string.
If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ],
where CODE-n is a code-point of the charset, and CHAR-n is the
corresponding Unicode character code.
If it is a string, it is a name of file that contains the above
information. The file format is the same as what described for `:map'
attribute."
(when (vectorp (car props))
;; Old style code:
;; (define-charset CHARSET-ID CHARSET-SYMBOL INFO-VECTOR)
;; Convert the argument to make it fit with the current style.
(let ((vec (car props)))
(setq props (convert-define-charset-argument name vec)
name docstring
docstring (aref vec 8))))
(let ((attrs (mapcar 'list '(:dimension
:code-space
:min-code
:max-code
:iso-final-char
:iso-revision-number
:emacs-mule-id
:ascii-compatible-p
:supplementary-p
:invalid-code
:code-offset
:map
:subset
:superset
:unify-map
:plist))))
;; If :dimension is omitted, get the dimension from :code-space.
(let ((dimension (plist-get props :dimension)))
(or dimension
(let ((code-space (plist-get props :code-space)))
(setq dimension (if code-space (/ (length code-space) 2) 4))
(setq props (plist-put props :dimension dimension)))))
(let ((code-space (plist-get props :code-space)))
(or code-space
(let ((dimension (plist-get props :dimension)))
(setq code-space (make-vector 8 0))
(dotimes (i dimension)
(aset code-space (1+ (* i 2)) #xFF))
(setq props (plist-put props :code-space code-space)))))
;; If :emacs-mule-id is specified, update emacs-mule-charset-table.
(let ((emacs-mule-id (plist-get props :emacs-mule-id)))
(if (integerp emacs-mule-id)
(aset emacs-mule-charset-table emacs-mule-id name)))
(dolist (slot attrs)
(setcdr slot (purecopy (plist-get props (car slot)))))
;; Make sure that the value of :code-space is a vector of 8
;; elements.
(let* ((slot (assq :code-space attrs))
(val (cdr slot))
(len (length val)))
(if (< len 8)
(setcdr slot
(vconcat val (make-vector (- 8 len) 0)))))
;; Add :name and :docstring properties to PROPS.
(setq props
(cons :name (cons name (cons :docstring (cons (purecopy docstring) props)))))
(or (plist-get props :short-name)
(plist-put props :short-name (symbol-name name)))
(or (plist-get props :long-name)
(plist-put props :long-name (plist-get props :short-name)))
(plist-put props :base name)
;; We can probably get a worthwhile amount in purespace.
(setq props
(mapcar (lambda (elt)
(if (stringp elt)
(purecopy elt)
elt))
props))
(setcdr (assq :plist attrs) props)
(apply 'define-charset-internal name (mapcar 'cdr attrs))))
(defun load-with-code-conversion (fullname file &optional noerror nomessage)
"Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
The file contents are decoded before evaluation if necessary.
If optional third arg NOERROR is non-nil,
report no error if FILE doesn't exist.
Print messages at start and end of loading unless
optional fourth 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 (generate-new-buffer " *load*"))
(load-in-progress t)
(source (string-suffix-p ".el" fullname)))
(unless nomessage
(if source
(message "Loading %s (source)..." file)
(message "Loading %s..." file)))
(when purify-flag
(push (purecopy file) preloaded-file-list))
(unwind-protect
(let ((load-file-name fullname)
(set-auto-coding-for-load t)
(inhibit-file-name-operation nil))
(with-current-buffer buffer
;; 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.
(set-buffer-multibyte t)
;; Don't let deactivate-mark remain set.
(let (deactivate-mark)
(insert-file-contents fullname))
;; 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)
'raw-text)))
(set-buffer-multibyte nil))
;; Make `kill-buffer' quiet.
(set-buffer-modified-p nil))
;; Have the original buffer current while we eval.
(eval-buffer buffer nil
;; This is compatible with what `load' does.
(if dump-mode file fullname)
nil t))
(let (kill-buffer-hook kill-buffer-query-functions)
(kill-buffer buffer)))
(do-after-load-evaluation fullname)
(unless (or nomessage noninteractive)
(if source
(message "Loading %s (source)...done" file)
(message "Loading %s...done" file)))
t)))
(defun charset-info (charset)
"Return a vector of information of CHARSET.
This function is provided for backward compatibility.
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 is always 0.
BYTES is always 0.
DIMENSION is the number of bytes of a code-point of the charset:
1, 2, 3, or 4.
CHARS is the number of characters in a dimension:
94, 96, 128, or 256.
WIDTH is always 0.
DIRECTION is always 0.
LEADING-CODE-BASE is always 0.
LEADING-CODE-EXT is always 0.
ISO-FINAL-CHAR (character) is the final character of the
corresponding ISO 2022 charset. If the charset is not assigned
any final character, the value is -1.
ISO-GRAPHIC-PLANE is always 0.
REVERSE-CHARSET is always -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."
(vector 0
0
(charset-dimension charset)
(charset-chars charset)
0
0
0
0
(charset-iso-final-char charset)
0
-1
(get-charset-property charset :short-name)
(get-charset-property charset :short-name)
(charset-description charset)
(charset-plist charset)))
;; 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.
(defun get-charset-property (charset propname)
"Return the value of CHARSET's PROPNAME property.
This is the last value stored with
(put-charset-property CHARSET PROPNAME VALUE)."
(plist-get (charset-plist charset) propname))
(defun put-charset-property (charset propname value)
"Set CHARSETS's PROPNAME property to value VALUE.
It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
(set-charset-plist charset
(plist-put (charset-plist charset) propname
(if (stringp value)
(purecopy value)
value))))
(defun charset-description (charset)
"Return description string of CHARSET."
(plist-get (charset-plist charset) :docstring))
(defun charset-dimension (charset)
"Return dimension of CHARSET."
(plist-get (charset-plist charset) :dimension))
(defun charset-chars (charset &optional dimension)
"Return number of characters contained in DIMENSION of CHARSET.
DIMENSION defaults to the first dimension."
(unless dimension (setq dimension 1))
(let ((code-space (plist-get (charset-plist charset) :code-space)))
(1+ (- (aref code-space (1- (* 2 dimension)))
(aref code-space (- (* 2 dimension) 2))))))
(defun charset-iso-final-char (charset)
"Return ISO-2022 final character of CHARSET.
Return -1 if charset isn't an ISO 2022 one."
(or (plist-get (charset-plist charset) :iso-final-char)
-1))
(defmacro charset-short-name (charset)
"Return short name of CHARSET."
(plist-get (charset-plist charset) :short-name))
(defmacro charset-long-name (charset)
"Return long name of CHARSET."
(plist-get (charset-plist charset) :long-name))
;;; CHARACTER
(defun make-char-internal (charset-id &optional code1 code2)
(let ((charset (aref emacs-mule-charset-table charset-id)))
(or charset
(error "Invalid Emacs-mule charset ID: %d" charset-id))
(make-char charset code1 code2)))
(defun char-displayable-p (char)
"Return non-nil if we should be able to display CHAR.
On a multi-font display, the test is only whether there is an
appropriate font from the selected frame's fontset to display
CHAR's charset in general. Since fonts may be specified on a
per-character basis, this may not be accurate."
(cond ((< char 128)
;; ASCII characters are always displayable.
t)
((not enable-multibyte-characters)
;; Maybe there's a font for it, but we can't put it in the buffer.
nil)
(t
(let ((font-glyph (internal-char-font nil char)))
(if font-glyph
(if (consp font-glyph)
;; On a window system, a character is displayable
;; if a font for that character is in the default
;; face of the currently selected frame.
(car font-glyph)
;; On a text terminal supporting glyph codes, CHAR is
;; displayable if its glyph code is nonnegative.
(<= 0 font-glyph))
;; On a text terminal without glyph codes, CHAR is displayable
;; if the coding system for the terminal can encode it.
(let ((coding (terminal-coding-system)))
(when coding
(let ((cs-list (coding-system-get coding :charset-list)))
(cond
((listp cs-list)
(catch 'tag
(mapc #'(lambda (charset)
(if (encode-char char charset)
(throw 'tag charset)))
cs-list)
nil))
((eq cs-list 'iso-2022)
(catch 'tag2
(mapc #'(lambda (charset)
(if (and (plist-get (charset-plist charset)
:iso-final-char)
(encode-char char charset))
(throw 'tag2 charset)))
charset-list)
nil))
((eq cs-list 'emacs-mule)
(catch 'tag3
(mapc #'(lambda (charset)
(if (and (plist-get (charset-plist charset)
:emacs-mule-id)
(encode-char char charset))
(throw 'tag3 charset)))
charset-list)
nil)))))))))))
;; Save the ASCII case table in case we need it later. Some locales
;; (such as Turkish) modify the case behavior of ASCII characters,
;; which can interfere with networking code that uses ASCII strings.
(defvar ascii-case-table
;; Code copied from copy-case-table to avoid requiring case-table.el
(let ((tbl (copy-sequence (standard-case-table)))
(up (char-table-extra-slot (standard-case-table) 0)))
(if up (set-char-table-extra-slot tbl 0 (copy-sequence up)))
(set-char-table-extra-slot tbl 1 nil)
(set-char-table-extra-slot tbl 2 nil)
tbl)
"Case table for the ASCII character set.")
;; Coding system stuff
;; Coding system is a symbol that has been defined by the function
;; `define-coding-system'.
(defconst coding-system-iso-2022-flags
'(long-form
ascii-at-eol
ascii-at-cntl
7-bit
locking-shift
single-shift
designation
revision
direction
init-at-bol
designate-at-bol
safe
latin-extra
composition
euc-tw-shift
use-roman
use-oldjis
8-bit-level-4)
"List of symbols that control ISO-2022 encoder/decoder.
The value of the `:flags' attribute in the argument of the function
`define-coding-system' must be one of them.
If `long-form' is specified, use a long designation sequence on
encoding for the charsets `japanese-jisx0208-1978', `chinese-gb2312',
and `japanese-jisx0208'. The long designation sequence doesn't
conform to ISO 2022, but is used by such coding systems as
`compound-text'.
If `ascii-at-eol' is specified, designate ASCII to g0 at end of line
on encoding.
If `ascii-at-cntl' is specified, designate ASCII to g0 before control
codes and SPC on encoding.
If `7-bit' is specified, use 7-bit code only on encoding.
If `locking-shift' is specified, decode locking-shift code correctly
on decoding, and use locking-shift to invoke a graphic element on
encoding.
If `single-shift' is specified, decode single-shift code
correctly on decoding, and use single-shift to invoke a graphic
element on encoding. See also `8-bit-level-4' specification.
If `designation' is specified, decode designation code correctly on
decoding, and use designation to designate a charset to a graphic
element on encoding.
If `revision' is specified, produce an escape sequence to specify
revision number of a charset on encoding. Such an escape sequence is
always correctly decoded on decoding.
If `direction' is specified, decode ISO6429's code for specifying
direction correctly, and produce the code on encoding.
If `init-at-bol' is specified, on encoding, it is assumed that
invocation and designation statuses are reset at each beginning of
line even if `ascii-at-eol' is not specified; thus no codes for
resetting them are produced.
If `safe' is specified, on encoding, characters not supported by a
coding are replaced with `?'.
If `latin-extra' is specified, the code-detection routine assumes that a
code specified in `latin-extra-code-table' (which see) is valid.
If `composition' is specified, an escape sequence to specify
composition sequence is correctly decoded on decoding, and is produced
on encoding.
If `euc-tw-shift' is specified, the EUC-TW specific shifting code is
correctly decoded on decoding, and is produced on encoding.
If `use-roman' is specified, JIS0201-1976-Roman is designated instead
of ASCII.
If `use-oldjis' is specified, JIS0208-1976 is designated instead of
JIS0208-1983.
If `8-bit-level-4' is specified, the decoder assumes the
implementation level \"4\" for 8-bit codes which means that GL is
identified as the single-shift area. The default implementation
level for 8-bit code is \"4A\" which means that GR is identified
as the single-shift area.")
(defun define-coding-system (name docstring &rest props)
"Define NAME (a symbol) as a coding system with DOCSTRING and attributes.
The remaining arguments must come in pairs ATTRIBUTE VALUE. ATTRIBUTE
may be any symbol.
A coding system specifies a rule to decode (i.e. to convert a
byte sequence to a character sequence) and a rule to encode (the
opposite of decoding).
The decoding is done by at most 3 steps; the first is to convert
a byte sequence to a character sequence by one of Emacs'
internal routines specified by `:coding-type' attribute. The
optional second step is to convert the character sequence (the
result of the first step) by a translation table specified
by `:decode-translation-table' attribute. The optional third step
is to convert the above result by a Lisp function specified
by `:post-read-conversion' attribute.
The encoding is done by at most 3 steps, which are the reverse
of the decoding steps. The optional first step converts a
character sequence to another character sequence by a Lisp
function specified by `:pre-write-conversion' attribute. The
optional second step converts the above result by a translation
table specified by `:encode-translation-table' attribute. The
third step converts the above result to a byte sequence by one
of the Emacs's internal routines specified by the `:coding-type'
attribute.
The following attributes have special meanings. Those labeled as
\"(required)\" should not be omitted.
`:mnemonic' (required)
VALUE is a character to display on mode line for the coding system.
`:coding-type' (required)
VALUE specifies the format of byte sequence the coding system
decodes and encodes to. It must be one of `charset', `utf-8',
`utf-16', `iso-2022', `emacs-mule', `shift-jis', `ccl',
`raw-text', `undecided'.
If VALUE is `charset', the coding system is for handling a
byte sequence in which each byte or every two- to four-byte
sequence represents a character code of a charset specified
by the `:charset-list' attribute.
If VALUE is `utf-8', the coding system is for handling Unicode
UTF-8 byte sequences. See also the documentation of the
attribute `:bom'.
If VALUE is `utf-16', the coding system is for handling Unicode
UTF-16 byte sequences. See also the documentation of the
attributes :bom and `:endian'.
If VALUE is `iso-2022', the coding system is for handling byte
sequences conforming to ISO/IEC 2022. See also the documentation
of the attributes `:charset-list', `:flags', and `:designation'.
If VALUE is `emacs-mule', the coding system is for handling
byte sequences which Emacs 20 and 21 used for their internal
representation of characters.
If VALUE is `shift-jis', the coding system is for handling byte
sequences of Shift_JIS format. See also the attribute `:charset-list'.
If VALUE is `ccl', the coding system uses CCL programs to decode
and encode byte sequences. The CCL programs must be
specified by the attributes `:ccl-decoder' and `:ccl-encoder'.
If VALUE is `raw-text', the coding system decodes byte sequences
without any conversions.
`:eol-type'
VALUE is the EOL (end-of-line) format of the coding system. It must be
one of `unix', `dos', `mac'. The symbol `unix' means Unix-like EOL
\(i.e., a single LF character), `dos' means DOS-like EOL \(i.e., a sequence
of CR followed by LF), and `mac' means Mac-like EOL \(i.e., a single CR).
If omitted, Emacs detects the EOL format automatically when decoding.
`:charset-list' (required if `:coding-type' is `charset' or `shift-jis')
VALUE must be a list of charsets supported by the coding system.
If `coding-type:' is `charset', then on decoding and encoding by the
coding system, if a character belongs to multiple charsets in the
list, a charset that comes first in the list is selected.
If `:coding-type' is `iso-2022', VALUE may be `iso-2022', which
indicates that the coding system supports all ISO-2022 based
charsets.
If `:coding-type' is `shift-jis', VALUE must be a list of three
to four charsets supported by Shift_JIS encoding scheme. The
first charset (one dimension) is for code space 0x00..0x7F, the
second (one dimension) for 0xA1..0xDF, the third (two dimension)
for 0x8140..0xEFFC, the optional fourth (three dimension) for
0xF040..0xFCFC.
If `:coding-type' is `emacs-mule', VALUE may be `emacs-mule',
which indicates that the coding system supports all charsets that
have the `:emacs-mule-id' property.
`:ascii-compatible-p'
If VALUE is non-nil, the coding system decodes all 7-bit bytes into
the corresponding ASCII characters, and encodes all ASCII characters
back to the corresponding 7-bit bytes. VALUE defaults to nil.
`:decode-translation-table'
VALUE must be a translation table to use on decoding.
`:encode-translation-table'
VALUE must be a translation table to use on encoding.
`:post-read-conversion'
VALUE must be 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. This function is passed one
argument: the number of characters in the text to convert, with
point at the start of the text. The function should leave point
and the match data unchanged, and should return the new character
count. Note that this function should avoid reading from files
or receiving text from subprocesses -- anything that could invoke
decoding; if it must do so, it should bind
`coding-system-for-read' to a value other than the current
coding-system, to avoid infinite recursion.
`:pre-write-conversion'
VALUE must be 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. This function should convert the whole text in the
current buffer, and leave the match data unchanged. For backward
compatibility, this function is passed two arguments which can be
ignored. Note that this function should avoid writing to files
or sending text to subprocesses -- anything that could invoke
encoding; if it must do so, it should bind
`coding-system-for-write' to a value other than the current
coding-system, to avoid infinite recursion.
`:default-char'
VALUE must be a character. On encoding, a character not supported by
the coding system is replaced with VALUE.
`:for-unibyte'
VALUE non-nil means that visiting a file with the coding system
results in a unibyte buffer.
`:mime-charset'
VALUE must be a symbol whose name is that of a MIME charset converted
to lower case.
`:mime-text-unsuitable'
VALUE non-nil means the `:mime-charset' property names a charset which
is unsuitable for the top-level media of type \"text\".
`:flags'
VALUE must be a list of symbols that control the ISO-2022 converter.
Each must be a member of the list `coding-system-iso-2022-flags'
\(which see). This attribute is meaningful only when `:coding-type'
is `iso-2022'.
`:designation'
VALUE must be a vector [G0-USAGE G1-USAGE G2-USAGE G3-USAGE].
GN-USAGE specifies the usage of graphic register GN as follows.
If it is nil, no charset can be designated to GN.
If it is a charset, the charset is initially designated to GN, and
never used by the other charsets.
If it is a list, the elements must be charsets, nil, 94, or 96. GN
can be used by all the listed charsets. If the list contains 94, any
iso-2022 charset whose code-space ranges are 94 long can be designated
to GN. If the list contains 96, any charsets whose ranges are
96 long can be designated to GN. If the first element is a charset,
that charset is initially designated to GN.
This attribute is meaningful only when `:coding-type' is `iso-2022'.
`:bom'
This attributes specifies whether the coding system uses a \"byte order
mark\". VALUE must be nil, t, or a cons cell of coding systems whose
`:coding-type' is `utf-16' or `utf-8'.
If the value is nil, on decoding, don't treat the first two-byte as
BOM, and on encoding, don't produce BOM bytes.
If the value is t, on decoding, skip the first two-byte as BOM, and on
encoding, produce BOM bytes according to the value of `:endian'.
If the value is a cons cell, on decoding, check the first two bytes.
If they are 0xFE 0xFF, use the car part coding system of the value.
If they are 0xFF 0xFE, use the cdr part coding system of the value.
Otherwise, treat them as bytes for a normal character. On encoding,
produce BOM bytes according to the value of `:endian'.
This attribute is meaningful only when `:coding-type' is `utf-16' or
`utf-8'.
`:endian'
VALUE must be `big' or `little' specifying big-endian and
little-endian respectively. The default value is `big'.
This attribute is meaningful only when `:coding-type' is `utf-16'.
`:ccl-decoder' (required if :coding-type is `ccl')
VALUE is a CCL program name defined by `define-ccl-program'. The
CCL program reads a byte sequence and writes a character sequence
as a decoding result.
`:ccl-encoder' (required if :coding-type is `ccl')
VALUE is a CCL program name defined by `define-ccl-program'. The
CCL program reads a character sequence and writes a byte sequence
as an encoding result.
`:inhibit-null-byte-detection'
VALUE non-nil means Emacs should ignore null bytes on code detection.
See the variable `inhibit-null-byte-detection'. This attribute
is meaningful only when `:coding-type' is `undecided'.
If VALUE is t, Emacs will ignore null bytes unconditionally while
detecting encoding. If VALUE is non-nil and not t, Emacs will
ignore null bytes if `inhibit-null-byte-detection' is non-nil.
`:inhibit-iso-escape-detection'
VALUE non-nil means Emacs should ignore ISO-2022 escape sequences on
code detection. See the variable `inhibit-iso-escape-detection'.
This attribute is meaningful only when `:coding-type' is
`undecided'.
If VALUE is t, Emacs will ignore escape sequences unconditionally
while detecting encoding. If VALUE is non-nil and not t, Emacs
will ignore escape sequences if `inhibit-iso-escape-detection' is
non-nil.
`:prefer-utf-8'
VALUE non-nil means Emacs prefers UTF-8 on code detection for
non-ASCII files. This attribute is meaningful only when
`:coding-type' is `undecided'."
(let* ((common-attrs (mapcar 'list
'(:mnemonic
:coding-type
:charset-list
:ascii-compatible-p
:decode-translation-table
:encode-translation-table
:post-read-conversion
:pre-write-conversion
:default-char
:for-unibyte
:plist
:eol-type)))
(coding-type (plist-get props :coding-type))
(spec-attrs (mapcar 'list
(cond ((eq coding-type 'iso-2022)
'(:initial
:reg-usage
:request
:flags))
((eq coding-type 'utf-8)
'(:bom))
((eq coding-type 'utf-16)
'(:bom
:endian))
((eq coding-type 'ccl)
'(:ccl-decoder
:ccl-encoder
:valids))
((eq coding-type 'undecided)
'(:inhibit-null-byte-detection
:inhibit-iso-escape-detection
:prefer-utf-8))))))
(dolist (slot common-attrs)
(setcdr slot (plist-get props (car slot))))
(dolist (slot spec-attrs)
(setcdr slot (plist-get props (car slot))))
(if (eq coding-type 'iso-2022)
(let ((designation (plist-get props :designation))
(flags (plist-get props :flags))
(initial (make-vector 4 nil))
(reg-usage (cons 4 4))
request elt)
(dotimes (i 4)
(setq elt (aref designation i))
(cond ((charsetp elt)
(aset initial i elt)
(setq request (cons (cons elt i) request)))
((consp elt)
(aset initial i (car elt))
(if (charsetp (car elt))
(setq request (cons (cons (car elt) i) request)))
(dolist (e (cdr elt))
(cond ((charsetp e)
(setq request (cons (cons e i) request)))
((eq e 94)
(setcar reg-usage i))
((eq e 96)
(setcdr reg-usage i))
((eq e t)
(setcar reg-usage i)
(setcdr reg-usage i)))))))
(setcdr (assq :initial spec-attrs) initial)
(setcdr (assq :reg-usage spec-attrs) reg-usage)
(setcdr (assq :request spec-attrs) request)
;; Change :flags value from a list to a bit-mask.
(let ((bits 0)
(i 0))
(dolist (elt coding-system-iso-2022-flags)
(if (memq elt flags)
(setq bits (logior bits (ash 1 i))))
(setq i (1+ i)))
(setcdr (assq :flags spec-attrs) bits))))
;; Add :name and :docstring properties to PROPS.
(setq props
(cons :name (cons name (cons :docstring (cons (purecopy docstring)
props)))))
(setcdr (assq :plist common-attrs) props)
(apply #'define-coding-system-internal
name (mapcar #'cdr (append common-attrs spec-attrs)))))
(defun coding-system-doc-string (coding-system)
"Return the documentation string for CODING-SYSTEM."
(plist-get (coding-system-plist coding-system) :docstring))
(defun coding-system-mnemonic (coding-system)
"Return the mnemonic character of CODING-SYSTEM.
The mnemonic character of a coding system is used in mode line to
indicate the coding system. If CODING-SYSTEM is nil, return ?=."
(plist-get (coding-system-plist coding-system) :mnemonic))
(defun coding-system-type (coding-system)
"Return the coding type of CODING-SYSTEM.
A coding type is a symbol indicating the encoding method of CODING-SYSTEM.
See the function `define-coding-system' for more detail."
(plist-get (coding-system-plist coding-system) :coding-type))
(defun coding-system-charset-list (coding-system)
"Return list of charsets supported by CODING-SYSTEM.
If CODING-SYSTEM supports all ISO-2022 charsets, return `iso-2022'.
If CODING-SYSTEM supports all emacs-mule charsets, return `emacs-mule'."
(plist-get (coding-system-plist coding-system) :charset-list))
(defun coding-system-category (coding-system)
"Return a category symbol of CODING-SYSTEM."
(plist-get (coding-system-plist coding-system) :category))
(defun coding-system-get (coding-system prop)
"Extract a value from CODING-SYSTEM's property list for property PROP.
For compatibility with Emacs 20/21, this accepts old-style symbols
like `mime-charset' as well as the current style like `:mime-charset'."
(or (plist-get (coding-system-plist coding-system) prop)
(if (not (keywordp prop))
;; For backward compatibility.
(if (eq prop 'ascii-incompatible)
(not (plist-get (coding-system-plist coding-system)