-
Notifications
You must be signed in to change notification settings - Fork 1
/
bidi.el
1403 lines (1294 loc) · 47.1 KB
/
bidi.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
;;; bidi.el --- bidi support for Emacs
;; Copyright (C) 2001 Alex Schroeder <alex@gnu.org>
;; Version: $Id: bidi.el,v 1.3 2001/11/21 11:59:58 alex Exp $
;; Keywords: wp
;; Author: Alex Schroeder <alex@gnu.org>
;; Maintainer: Alex Schroeder <alex@gnu.org>
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?CategoryBiDi
;; This file is not part of GNU Emacs.
;; This 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.
;;
;; This 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
;;; Commentary:
;; This is modelled after characters.el. At the moment, however, we
;; don't have categories assigned, so we must create them ourselves.
;; The new categories are identified by a character, like all other
;; categories. We store them in the following variables.
;; The existing categories and syntax tables are not enough to resolve
;; bidi issues: Some of these categories specify that the "real"
;; category must be determined from context. See the Unicode Standard
;; Annex #9, available from http://www.unicode.org/unicode/reports/tr9/.
(defvar bidi-testing t
"Non-nil will use bidi-table-test.el to setup up bidi categories.
Furthermore, while loading this file, some tests are run.")
(defvar bidi-category-l nil
"Strong Left-to-Right: Most alphabetic, syllabic, Han ideographic
characters, digits that are neither European nor Arabic, all unassigned
characters except in the ranges (0590-05FF, FB1D-FB4F) and (0600-07BF,
FB50-FDFF, FE70-FEFF).")
(defvar bidi-category-lre nil
"Strong Left-to-Right Embedding: LRE.")
(defvar bidi-category-lro nil
"Strong Left-to-Right Override: LRO.")
(defvar bidi-category-r nil
"Strong Right-to-Left: RLM, Hebrew alphabet, most punctuation specific
to that script, all unassigned characters in the ranges (0590-05FF,
FB1D-FB4F)")
(defvar bidi-category-al nil
"Strong Right-to-Left Arabic: Arabic, Thaana, and Syriac alphabets,
most punctuation specific to those scripts, all unassigned characters in
the ranges (0600-07BF, FB50-FDFF, FE70-FEFF).")
(defvar bidi-category-rle nil
"Strong Right-to-Left Embedding: RLE.")
(defvar bidi-category-rlo nil
"Strong Right-to-Left Override: RLO.")
(defvar bidi-category-pdf nil
"Weak Pop Directional Format: PDF.")
(defvar bidi-category-en nil
"Weak European Number: European digits, Eastern Arabic-Indic digits.")
(defvar bidi-category-es nil
"Weak European Number Separator: Solidus (Slash).")
(defvar bidi-category-et nil
"Weak European Number Terminator: Plus Sign, Minus Sign, Degree,
Currency symbols.")
(defvar bidi-category-an nil
"Weak Arabic Number: Arabic-Indic digits, Arabic decimal & thousands
separators.")
(defvar bidi-category-cs nil
"Weak Common Number Separator: Colon, Comma, Full Stop (Period),
Non-breaking space.")
(defvar bidi-category-nsm nil
"Weak Non-Spacing Mark: Characters marked Mn (Non-Spacing Mark) and Me
\(Enclosing Mark) in the Unicode Character Database.")
(defvar bidi-category-bn nil
"Weak Boundary Neutral: Formatting and control characters, other than
those explicitly given types above. (These are to be ignored in
processing bidirectional text.)")
(defvar bidi-category-b nil
"Neutral Paragraph Separator: Paragraph Separator, appropriate Newline
Functions, higher-protocol paragraph determination.")
(defvar bidi-category-s nil
"Neutral Segment Separator: Tab")
(defvar bidi-category-ws nil
"Neutral Whitespace: Space, Figure Space, Line Separator, Form Feed,
General Punctuation Spaces, ...")
(defvar bidi-category-on nil
"Other Neutrals: All other characters, including OBJECT REPLACEMENT
CHARACTER.")
(defvar bidi-categories
'(bidi-category-l
bidi-category-lre
bidi-category-lro
bidi-category-r
bidi-category-al
bidi-category-rle
bidi-category-rlo
bidi-category-pdf
bidi-category-en
bidi-category-es
bidi-category-et
bidi-category-an
bidi-category-cs
bidi-category-nsm
bidi-category-bn
bidi-category-b
bidi-category-s
bidi-category-ws
bidi-category-on)
"List of categories variables used by bidi algorithms.
Each category variable holds a character. This character
identifies the category.")
(defun bidi-setup-categories ()
"Create new categories for bidi according to UAX#9.
This sets all the categories defined in `bidi-categories'."
(let ((table (standard-category-table)))
(mapc (lambda (var)
(let ((cat (get-unused-category table))
(doc (get var 'variable-documentation)))
(when (symbol-value var)
(error "%S is already set" var))
(unless cat
(error "No more unused categories available"))
(set var cat)
(define-category cat doc table)))
bidi-categories)))
;; Do category setup once.
(bidi-setup-categories)
(when bidi-testing
;; Fake category used in the test code.
;; UAX#9 also uses this in the examples.
(defvar bidi-category-n bidi-category-ws
"Neutral or Separator (B, S, WS, ON), not used as a category."))
;; Assign the categories to the characters. Choose either the real
;; table or the testing table (for ASCII only and using capital letters
;; for right-to-left).
(if bidi-testing
(load-file "bidi-table-test.el")
(load-file "bidi-table.el"))
;; Here's how the translation will work for a string STR: First, produce
;; a string/array of bidi categories TYPES used in STR. Then apply some
;; rules to TYPES and based on the result, invert selected parts of STR.
;; Note that we don't have explicit levels in visual-to-logical
;; transformation, so levels will be ignored. Levels are important for
;; logical-to-visual transformation, ie. for display. This is done
;; elsewhere.
;; Note that eventually we will also want to call this code to process
;; chunks of text such as received by comint. For this to work we will
;; have to keep a data structure which remembers starting position of
;; the current embedding level. When the level is zero, text can be
;; inserted and the starting position can be incrememted. When the
;; level was non-zero and returns to zero, the entire text between the
;; starting position and the current position will have to be reversed
;; recursively. The issues are not clear, yet. FIXME
;; Note that some text in visual order may still contain the explicit
;; directional codes. Wether these ought to be used while reversing is
;; not clear, yet. FIXME
(defun bidi-get-category (char)
"Return category for CHAR."
(let ((categories bidi-categories)
(set (char-category-set char))
result)
(while (and (not result) categories)
(let ((category (symbol-value (car categories))))
(if (aref set category)
(setq result category)
(setq categories (cdr categories)))))
result))
(when bidi-testing
(assert (eq bidi-category-l (bidi-get-category ?a)))
(assert (eq bidi-category-r (bidi-get-category ?A))))
(defun bidi-get-types (str)
"Return bidi categories for STR in a list."
(mapcar 'bidi-get-category str))
(when bidi-testing
(assert (string=
(string bidi-category-l
bidi-category-ws
bidi-category-r)
(apply 'string (bidi-get-types "a A")))))
(defun bidi-get-mnemonics (types)
"Return mnemonics for all bidi categories in TYPES."
(let ((table (mapcar
(lambda (sym)
(cons (symbol-value sym)
(intern
(upcase
(nth 2 (split-string
(symbol-name sym) "-"))))))
bidi-categories)))
(mapcar (lambda (type)
(cdr (assq type table)))
types)))
(when bidi-testing
(assert (equal
(bidi-get-mnemonics
(list bidi-category-l
bidi-category-ws
bidi-category-r))
'(L WS R))))
(defun bidi-apply-w1 (sor types)
"Apply UAX#9 rule W1.
SOR is the start-of-level-run bidi type.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
Examine each non-spacing mark (NSM) in the level run, and change the
type of the NSM to the type of the previous character. If the NSM is at
the start of the level run, it will get the type of sor."
;; Skip this if no NSM types are found.
(if (null (memq bidi-category-nsm types))
types
(let ((previous-type sor)
(lst types))
(while lst
(if (eq (car lst) bidi-category-nsm)
(setcar lst previous-type)
(setq previous-type (car lst)))
(setq lst (cdr lst)))
types)))
(when bidi-testing
(assert (eq (bidi-apply-w1 nil nil) nil))
(assert (equal (bidi-apply-w1
nil
(list bidi-category-al
bidi-category-nsm
bidi-category-nsm))
(list bidi-category-al
bidi-category-al
bidi-category-al)))
(assert (equal (bidi-apply-w1
bidi-category-r
(list bidi-category-nsm))
(list bidi-category-r))))
(defun bidi-apply-w2 (sor types)
"Apply UAX#9 rule W2.
SOR is the start-of-level-run bidi type.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
Search backwards from each instance of a European number until the first
strong type (R, L, AL, or sor) is found. If an AL is found, change the type of
the European number to Arabic number."
(if (null (memq bidi-category-en types))
types
(let* ((previous-type sor)
(lst types)
type)
(while lst
(setq type (car lst))
(if (eq type bidi-category-en)
(when (eq previous-type bidi-category-al)
(setcar lst bidi-category-al))
(when (or (eq type bidi-category-r)
(eq type bidi-category-l)
(eq type bidi-category-al))
(setq previous-type type)))
(setq lst (cdr lst)))
types)))
(when bidi-testing
(assert (equal (bidi-apply-w2
nil
(list bidi-category-al
bidi-category-en))
(list bidi-category-al
bidi-category-al)))
(assert (equal (bidi-apply-w2
nil
(list bidi-category-al
bidi-category-n
bidi-category-en))
(list bidi-category-al
bidi-category-n
bidi-category-al)))
(assert (equal (bidi-apply-w2
bidi-category-r
(list bidi-category-n
bidi-category-en))
(list bidi-category-n
bidi-category-en)))
(assert (equal (bidi-apply-w2
nil
(list bidi-category-l
bidi-category-n
bidi-category-en))
(list bidi-category-l
bidi-category-n
bidi-category-en)))
(assert (equal (bidi-apply-w2
nil
(list bidi-category-r
bidi-category-n
bidi-category-en))
(list bidi-category-r
bidi-category-n
bidi-category-en))))
(defun bidi-apply-replacement (list old new)
"Replace all instances of OLD with NEW in LIST.
LIST will be modified in place."
(let ((lst (memq old list)))
(while lst
(setcar lst new)
(setq lst (memq old (cdr lst)))))
list)
(when bidi-testing
(assert (equal (bidi-apply-replacement '(1 2 3) 2 5)
'(1 5 3))))
(defun bidi-apply-w3 (types)
"Apply UAX#9 rule W3.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
Change all ALs to R."
(bidi-apply-replacement types bidi-category-al bidi-category-r))
(when bidi-testing
(assert (equal (bidi-apply-w3
(list bidi-category-al
bidi-category-en))
(list bidi-category-r
bidi-category-en)))
(assert (equal (bidi-apply-w3
(list bidi-category-en
bidi-category-al))
(list bidi-category-en
bidi-category-r)))
(assert (equal (bidi-apply-w3
(list bidi-category-en))
(list bidi-category-en))))
(defun bidi-apply-w4 (types)
"Apply UAX#9 rule W4.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
A single European separator between two European numbers changes to a
European number. A single common separator between two numbers of the
same type changes to that type."
;; I don't want to use string-match, here, because while the
;; categories are characters, they may have special meanings in
;; regexps, so I'd have to escape all special characters and then call
;; string-match. That seems like too much overhead.
(let ((lst (memq bidi-category-en types)))
(while lst
(when (and (or (eq (nth 1 lst) bidi-category-cs)
(eq (nth 1 lst) bidi-category-es))
(eq (nth 2 lst) bidi-category-en))
(setq lst (cdr lst))
(setcar lst bidi-category-en)
(setq lst (cdr lst)))
(setq lst (memq bidi-category-en (cdr lst)))))
(let ((lst (memq bidi-category-an types)))
(while lst
(when (and (eq (nth 1 lst) bidi-category-cs)
(eq (nth 2 lst) bidi-category-an))
(setq lst (cdr lst))
(setcar lst bidi-category-an)
(setq lst (cdr lst)))
(setq lst (memq bidi-category-en (cdr lst)))))
types)
(when bidi-testing
(assert (equal (bidi-apply-w4
(list bidi-category-en
bidi-category-es
bidi-category-en))
(list bidi-category-en
bidi-category-en
bidi-category-en)))
(assert (equal (bidi-apply-w4
(list bidi-category-en
bidi-category-cs
bidi-category-en))
(list bidi-category-en
bidi-category-en
bidi-category-en)))
(assert (equal (bidi-apply-w4
(list bidi-category-an
bidi-category-cs
bidi-category-an))
(list bidi-category-an
bidi-category-an
bidi-category-an)))
(assert (equal (bidi-apply-w4
(list bidi-category-en
bidi-category-cs
bidi-category-cs
bidi-category-en))
(list bidi-category-en
bidi-category-cs
bidi-category-cs
bidi-category-en))))
(defun bidi-apply-w5-sub (types)
"Change all ET following EN to EN.
Called by `bidi-apply-w5', once for the real list, once for the reversed list."
(let ((lst (memq bidi-category-en types)))
(while lst
(setq lst (cdr lst))
(while (and lst
(eq (car lst) bidi-category-et))
(setcar lst bidi-category-en)
(setq lst (cdr lst)))
(setq lst (memq bidi-category-en lst))))
types)
(defun bidi-apply-w5 (types)
"Apply UAX#9 rule W5.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
A sequence of European terminators adjacent to European numbers changes
to all European numbers."
;; We use the following trick, here. First we search for any ET
;; following EN, then we reverse the list, and repeat.
(if (not (memq bidi-category-et types))
types
(setq types (bidi-apply-w5-sub types))
(if (not (memq bidi-category-et types))
types
(nreverse (bidi-apply-w5-sub (nreverse types))))))
(when bidi-testing
(assert (equal (bidi-apply-w5
(list bidi-category-et
bidi-category-et
bidi-category-en))
(list bidi-category-en
bidi-category-en
bidi-category-en)))
(assert (equal (bidi-apply-w5
(list bidi-category-en
bidi-category-et
bidi-category-et))
(list bidi-category-en
bidi-category-en
bidi-category-en)))
(assert (equal (bidi-apply-w5
(list bidi-category-an
bidi-category-et
bidi-category-en))
(list bidi-category-an
bidi-category-en
bidi-category-en))))
(defun bidi-apply-w6 (types)
"Apply UAX#9 rule W6.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
Separators and terminators change to Other Neutral."
;; Difficult decision: Is it faster to search the list three times
;; using memq or is it faster to step through the list on our own?
;; Better to reuse code. And it looks good.
(bidi-apply-replacement
(bidi-apply-replacement
(bidi-apply-replacement
types
bidi-category-cs bidi-category-on)
bidi-category-es bidi-category-on)
bidi-category-et bidi-category-on))
(when bidi-testing
(assert (equal (bidi-apply-w6
(list bidi-category-an
bidi-category-et))
(list bidi-category-an
bidi-category-on)))
(assert (equal (bidi-apply-w6
(list bidi-category-l
bidi-category-es
bidi-category-en))
(list bidi-category-l
bidi-category-on
bidi-category-en)))
(assert (equal (bidi-apply-w6
(list bidi-category-en
bidi-category-cs
bidi-category-an))
(list bidi-category-en
bidi-category-on
bidi-category-an)))
(assert (equal (bidi-apply-w6
(list bidi-category-et
bidi-category-an))
(list bidi-category-on
bidi-category-an))))
(defun bidi-apply-w7 (sor types)
"Apply UAX#9 rule W7.
SOR is the start-of-level-run bidi type.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
Search backwards from each instance of a European number until the first
strong type (R, L, or sor) is found. If an L is found, then change the
type of the European number to L."
;; Looks very similar to `bidi-apply-w7'. Does it make sense to
;; factor this out? I don't think so. That would be "method
;; stealing".
(if (null (memq bidi-category-en types))
types
(let* ((previous-type sor)
(lst types)
type)
(while lst
(setq type (car lst))
(if (eq type bidi-category-en)
(when (eq previous-type bidi-category-l)
(setcar lst bidi-category-l))
(when (or (eq type bidi-category-r)
(eq type bidi-category-l))
(setq previous-type type)))
(setq lst (cdr lst)))
types)))
(when bidi-testing
(assert (equal (bidi-apply-w7
nil
(list bidi-category-l
bidi-category-n
bidi-category-en))
(list bidi-category-l
bidi-category-n
bidi-category-l)))
(assert (equal (bidi-apply-w7
nil
(list bidi-category-r
bidi-category-n
bidi-category-en))
(list bidi-category-r
bidi-category-n
bidi-category-en))))
(defun bidi-apply-conversion (start-lst end-lst new)
"Transform elements from START-LST to END-LST to NEW.
START-LST is a list, and END-LST is a sublist, a tail, in START-LST.
Every car in START-LST will then be changed to NEW until END-LST is
reached. END-LST remains unmodified. START-LST is modified in place."
;; A potential error check has been left out. start-lst ought never
;; to be nil.
(let ((start start-lst))
(while (and start-lst
(not (eq start-lst end-lst)))
(setcar start-lst new)
(setq start-lst (cdr start-lst)))
start))
(when bidi-testing
(assert (equal
(let* ((lst '(a b c d e))
(end (nthcdr 3 lst)))
(bidi-apply-conversion lst end 'foo))
'(foo foo foo d e))))
(defun bidi-apply-n1 (sor eor types)
"Apply UAX#9 rule N1.
SOR is the start-of-level-run bidi type.
EOR is the end-of-level-run bidi type.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
A sequence of neutrals takes the direction of the surrounding strong
text if the text on both sides has the same direction. European and
Arabic numbers are treated as though they were R. Start-of-level-run
\(sor) and end-of-level-run \(eor) are used at level run boundaries."
;; The idea is this: We step through the list. start-lst points to
;; the last strong type we encountered. This marks the start of a
;; "stretch of neutrals". lst points to the current position in the
;; list. The elements between start-lst and lst may be switched.
(let ((start-lst types)
(lst types)
(start-type sor)
(type (car types)))
(while lst
(cond ((eq type bidi-category-l); potential end or start of L stretch
(if (eq start-type bidi-category-l)
(bidi-apply-conversion start-lst lst bidi-category-l)
(setq start-type bidi-category-l))
(setq start-lst (cdr lst)))
((or (eq type bidi-category-r); potential end or start of R stretch
(eq type bidi-category-en)
(eq type bidi-category-an))
(if (eq start-type bidi-category-r)
(bidi-apply-conversion start-lst lst bidi-category-r)
(setq start-type bidi-category-r))
(setq start-lst (cdr lst)))
;; is the following clause ever needed?
((not (or (eq type bidi-category-b); definitely not part of a stretch
(eq type bidi-category-s)
(eq type bidi-category-ws)
(eq type bidi-category-on)))
(setq start-type nil
start-lst nil)))
(setq lst (cdr lst)
type (car lst)))
;; final stretch
(when (and start-lst
(eq start-type eor))
(bidi-apply-conversion start-lst nil start-type)))
types)
(when bidi-testing
(assert (equal (bidi-apply-n1
nil nil
(list bidi-category-r
bidi-category-n
bidi-category-r))
(list bidi-category-r
bidi-category-r
bidi-category-r)))
(assert (equal (bidi-apply-n1
nil nil
(list bidi-category-l
bidi-category-n
bidi-category-l))
(list bidi-category-l
bidi-category-l
bidi-category-l)))
(assert (equal (bidi-apply-n1
nil nil
(list bidi-category-r
bidi-category-n
bidi-category-an))
(list bidi-category-r
bidi-category-r
bidi-category-an)))
(assert (equal (bidi-apply-n1
nil nil
(list bidi-category-an
bidi-category-n
bidi-category-r))
(list bidi-category-an
bidi-category-r
bidi-category-r)))
(assert (equal (bidi-apply-n1
nil nil
(list bidi-category-r
bidi-category-n
bidi-category-en))
(list bidi-category-r
bidi-category-r
bidi-category-en)))
(assert (equal (bidi-apply-n1
nil nil
(list bidi-category-en
bidi-category-n
bidi-category-r))
(list bidi-category-en
bidi-category-r
bidi-category-r))))
(defun bidi-apply-n2 (e types)
"Apply UAX#9 rule N2.
TYPES is a list of bidi types to operate on.
E is the embedding level to use.
TYPES will be modified in place.
Any remaining neutrals take the embedding direction."
;; Looks very similar to `bidi-apply-w6'.
(bidi-apply-replacement
(bidi-apply-replacement
(bidi-apply-replacement
(bidi-apply-replacement
types
bidi-category-b e)
bidi-category-s e)
bidi-category-ws e)
bidi-category-on e))
;; Note that the following tests provided for the N2 rule in UAX#9 seem
;; to work only if both N1 and N2 are applied.
;;
;; From UAX#9 (skipped in the test code below):
;; "Assume in this example that eor is L, and sor is R:
;; L N eor => L L eor
;; sor N R => sor R R"
(when bidi-testing
(assert (equal (bidi-apply-n2
bidi-category-r
(list bidi-category-r
bidi-category-n
bidi-category-l))
(list bidi-category-r
bidi-category-r
bidi-category-l)))
(assert (equal (bidi-apply-n2
bidi-category-l
(list bidi-category-r
bidi-category-n
bidi-category-l))
(list bidi-category-r
bidi-category-l
bidi-category-l))))
(defun bidi-resolve-weak-types (types &optional r2l-context)
"Resolve weak bidi types in TYPES, a list of bidi categories.
TYPES is a list of bidi types to operate on.
TYPES will be modified in place.
This uses rules W1 to W7 and rules N1 and N2 from UAX#9.
Since this function doesn't take care of explicit codes, all types are
considered to be at the same level. If you decomposed a string into
stretches of differing embedding levels, you can call this function for
every such stretch.
The resulting list will have the same number of elements and all weak
types will be replaced by strong types. If the optional argument
R2L-CONTEXT is given, the context will be right-to-left. If R2L-CONTEXT
is nil, a left-to-right context will be assumed.
In UAX#9 parlance: Each paragraph has a text ordering type E. If
R2L-CONTEXT is given, E is set to R, otherwise E is set to L.
Furthermore, we set the start-of-level-run type SOR and the
end-of-level-run type EOR to E. E, SOR, and EOR are used for rules W1,
W2, W7, N1, and N2.
For more information in the various rules, see the functions:
`bidi-apply-w1'
`bidi-apply-w2'
`bidi-apply-w3'
`bidi-apply-w4'
`bidi-apply-w5'
`bidi-apply-w6'
`bidi-apply-w7'
`bidi-apply-n1'
`bidi-apply-n2'"
(let* ((e (if r2l-context bidi-category-r bidi-category-l))
(sor e)
(eor e))
(bidi-apply-n2 e
(bidi-apply-n1 sor eor
(bidi-apply-w7 sor
(bidi-apply-w6
(bidi-apply-w5
(bidi-apply-w4
(bidi-apply-w3
(bidi-apply-w2 sor
(bidi-apply-w1 sor types)))))))))))
(when bidi-testing
(assert (equal
(bidi-resolve-weak-types
(bidi-get-types "he said \"THE VALUES ARE 123, 456, 789, OK\"."))
(nconc
(make-list 9 bidi-category-l)
(make-list 15 bidi-category-r)
(make-list 3 bidi-category-en)
(make-list 2 bidi-category-r)
(make-list 3 bidi-category-en)
(make-list 2 bidi-category-r)
(make-list 3 bidi-category-en)
(make-list 4 bidi-category-r)
(make-list 2 bidi-category-l)))))
(defun bidi-resolve-implicit-levels (types &optional levels)
"Resolve the implicit levels in TYPES.
TYPES is a list of bidi types to operate on.
LEVELS is an optional list of explicit levels. If it is provided,
the list should have the same length as TYPES.
LEVELS will be modified in place, if it is provided.
This uses rules I1 and I2 from UAX#9.
For all characters with an even (left-to-right) embedding direction,
those of type R go up one level and those of type AN or EN go up two
levels. For all characters with an odd (right-to-left) embedding
direction, those of type L, EN or AN go up one level."
(setq levels (or levels (make-list (length types) 0)))
(let ((result levels))
(while types
(let* ((type (car types))
(level (car levels)))
(if (evenp level)
;; I1: even (left-to-right) embedding direction
(cond ((eq type bidi-category-r)
(setcar levels (1+ level)))
((or (eq type bidi-category-an)
(eq type bidi-category-en))
(setcar levels (+ 2 level))))
;; I2: odd (right-to-left) embedding direction
(when (or (eq type bidi-category-l)
(eq type bidi-category-en)
(eq type bidi-category-an))
(setcar levels (1+ level))))
(setq types (cdr types)
levels (cdr levels))))
result))
(when bidi-testing
(assert (equal
(bidi-resolve-implicit-levels
(bidi-resolve-weak-types
(bidi-get-types "car means CAR.")))
'(0 0 0 0 0 0 0 0 0 0 1 1 1 0)))
;; The number embedding works.
(assert (equal
(bidi-resolve-implicit-levels
(bidi-resolve-weak-types
(bidi-get-types "he said \"THIS IS 123, RIGHT?\".")))
'(0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 0 0 0)))
(assert (equal
(bidi-resolve-implicit-levels
(bidi-resolve-weak-types
(bidi-get-types "car MEANS CAR."))
(make-list (length "car MEANS CAR.") 1))
;; FIXME: is the 2 at the end wrong?
'(2 2 2 2 1 1 1 1 1 1 1 1 1 2))))
(defun bidi-find-pos (list elem &optional invert)
"Return position of element ELEM in LIST.
With optional argument INVERT, return the position of the first element
which is not eq to ELEM."
(let ((pos 0)
result)
(while list
(if (or (and (not invert) (eq (car list) elem))
(and invert (not (eq (car list) elem))))
(setq list nil
result pos)
(setq pos (1+ pos)))
(setq list (cdr list)))
result))
(when bidi-testing
(assert (eq 2 (bidi-find-pos '(0 0 2 3 4) 0 t)))
(assert (eq 2 (bidi-find-pos '(0 1 2 3 4) 2)))
(assert (eq 2 (car (nthcdr 2 '(0 1 2 3 4))))))
(defun bidi-apply-l2 (str levels &optional limit)
"Apply UAX#9 rule L2.
STR is the string to reorder.
LEVELS is a list of resolved levels.
Both STR and LEVELS must have the same length.
The optional argument LIMIT prevents the reversal of characters on level
LIMIT and below.
From the highest level found in the text to the lowest odd level on each
line, reverse any contiguous sequence of characters that are at that
level or higher."
;; Here's the idea: We will work with lists instead of strings and use
;; nreverse at will.
(setq limit (or limit 0))
(let* ((orig-levels levels)
(level (apply 'max levels))
(char-list (append '(foo) str nil))
(head (cdr char-list))
stretch tail
(pos (bidi-find-pos levels level)))
(while (> level limit)
;; Find beginning of stretch. The stuff before is the head. It
;; must be separated from the stretch. Keep the list of levels up
;; to date. Take care when the stretch starts right at the
;; beginning.
(while pos
(setq levels (nthcdr pos levels)
stretch (nthcdr pos head))
;; Separate head from body.
(if (> pos 0)
(setcdr (nthcdr (1- pos) head) nil)
;; When there is no head, the stretch starts at the first
;; character. This can only happen at the beginning of str,
;; because later, this would require two adjacent stretches
;; with the same level. Those would be treated as one
;; contiguous stretch, therefore this can't happen. We can't
;; set head to nil, because then we would only keep the last
;; character of the stretch (a problem with nconc futher
;; down). That is why we can point head to the beginning of
;; char-list.
(setq head char-list)
(setcdr head nil))
;; Determine where stretch ends. The stuff after the stretch is
;; the tail. The stretch must be separated from the tail. Keep
;; the list of levels up to date. Take care when the stretch
;; covers the entire rest of the list.
(setq pos (bidi-find-pos levels level t))
(if (not pos)
(setq levels nil
tail nil)
(setq levels (nthcdr pos levels)
tail (nthcdr pos stretch))
(setcdr (nthcdr (1- pos) stretch) nil))
;; Reverse stretch
(setq stretch (nreverse stretch))
;; Stitch them together again
(nconc head stretch tail)
;; Prepare next round
(setq head tail
pos (bidi-find-pos levels level)))
;; Decrease level and repeat
(let ((new-level (1- level)))
;; make sure new level actually exists
(while (and (not (memq new-level orig-levels))
(> new-level 0))
(setq new-level (1- new-level)))
;; Reset lists and start again
(setq levels (bidi-apply-replacement orig-levels level new-level)
level new-level
head (cdr char-list)
pos (bidi-find-pos levels level))))
;; Return a string
(apply 'string (cdr char-list))))
(when bidi-testing
(assert (string=
(bidi-apply-l2
"car means CAR."
'(0 0 0 0 0 0 0 0 0 0 1 1 1 0))
"car means RAC."))
(assert (string=
(bidi-apply-l2
"it means THIS and THAT."
(bidi-resolve-implicit-levels
(bidi-resolve-weak-types
(bidi-get-types "it means THIS and THAT."))))
"it means SIHT and TAHT."))
(assert (string=
(bidi-apply-l2
"he said \"THIS IS 123, RIGHT?\"."
(bidi-resolve-implicit-levels
(bidi-resolve-weak-types
(bidi-get-types "he said \"THIS IS 123, RIGHT?\"."))))
"he said \"THGIR ,123 SI SIHT?\"."))
(assert (string=
(bidi-apply-l2
"FOO is a word for FOOLS."
(bidi-resolve-implicit-levels
(bidi-resolve-weak-types
(bidi-get-types "FOO is a word for FOOLS."))))
"OOF is a word for SLOOF.")))
(defun bidi-get-mirror-char (char)
"Return the mirror character of CHAR or nil if there is none.
See `bidi-mirroring-table'."
(cdr (assq char bidi-mirroring-table)))
(when bidi-testing
(assert (eq ?\) (bidi-get-mirror-char ?\()))
(assert (eq ?\] (bidi-get-mirror-char ?\[))))
(defun bidi-apply-l4 (str levels)
"Replace inverted glyphs with their mirrors if necessary.
STR is the string to reorder. STR will be modified in place.
LEVELS is a list of resolved levels.
Both STR and LEVELS must have the same length.
This uses rule L4 from UAX#9.
A character that possesses the mirrored property as specified by Section
4.7, Mirrored, must be depicted by a mirrored glyph if the resolved
directionality of that character is R."
(let ((i 0)
(len (length str))
mirror-char)
(while (< i len)
(setq mirror-char (bidi-get-mirror-char (aref str i)))
(when (and mirror-char (oddp (nth i levels)))
(aset str i mirror-char))
(setq i (1+ i)))
str))
(when bidi-testing
(assert (string= "(abc)" (bidi-apply-l4 "(abc)" '(0 0 0 0 0))))
(assert (string= ")abc(" (bidi-apply-l4 "(abc)" '(1 1 1 1 1))))
(assert (string= "(abc(" (bidi-apply-l4 "(abc)" '(0 0 1 1 3)))))
(defun bidi-reorder-string (str levels)
"Reorder string STR according to resolved LEVELS.