forked from tbanel/uniline
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniline.el
2131 lines (1979 loc) · 80.8 KB
/
uniline.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
;;; uniline.el --- Draw lines, boxes, & arrows with the keyboard -*- coding:utf-8; lexical-binding: t; -*-
;; Copyright (C) 2024 Thierry Banel
;; Author: Thierry Banel tbanelwebmin at free dot fr
;; Version: 1.0
;; Package-Requires: ((emacs "29.1") (hydra "0.15.0"))
;; Keywords: convenience, text
;; URL: https://github.com/tbanel/uniline
;; Uniline 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.
;; Uniline 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; ┏━━━━━━━┓
;; ╭──────╮ ┃ thick ┣═◁═╗
;; │ thin ┝◀━━━┫ box ┃ ║
;; │ box │ ┗━━━━━━━┛ ║
;; ╰───┬──╯ ╔══════╩═╗
;; ↓ ║ double ║
;; ╰────────────╢ box ║
;; ╚════╤═══╝
;; ▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜ │
;; ▌quadrant-blocks▐─◁─╯
;; ▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟
;;
;; UNICODE characters are available to draw nice boxes
;; and lines.
;; They come in 4 flavours: thin, thick, double, and quadrant-blocks.
;; Uniline makes it easy to draw and combine all 4 flavours.
;; Use the arrows on the keyboard to move around leaving a line behind.
;;
;; Uniline is a minor mode. Enter it with:
;; M-x uniline-mode
;; Leave it with:
;; C-c C-c
;;
;; A font able to displays the needed UNICODE characters have to
;; be used. It works well with the following families:
;; - DejaVu Sans Mono
;; - Unifont
;; - Hack
;; - JetBrains Mono
;; - Cascadia Mono
;; - Agave
;;
;; Also, the encoding of the file must support UNICODE.
;; One way to do that, is to add a line like this one
;; at the top of your file:
;; -*- coding:utf-8; -*-
;;; Requires:
(require 'cl-lib)
(require 'hydra)
(require 'rect)
(cl-proclaim '(optimize (speed 3) (safety 1)))
(eval-when-compile
;; temporarily fix a bug about Hydra generating too long docstrings
(setq byte-compile-docstring-max-column 2000))
;;; Code:
;;;╭────────────────╮
;;;│Coding decisions│
;;;╰────────────────╯
;; Why so much use of (defmacro)?
;; Because we handle 4 directions: north, east, south, west.
;; We end up writing slightly the same algorithms 4 times.
;; It is easier to centralise the algorithms in a set
;; of (defmacro) to write them just once.
;; Isn't (defun) enough and more readable than (defmacro)?
;; Yes, absolutely!
;; We can centralize algorithms in a set of (defun).
;; However a (defun) generically handling all four directions
;; misses nice code-folding.
;; For instance somewhere we have:
;; (ash 3 (* 2 dir))
;; If the compiler sees `dir' as `uniline--direction-dw↓',
;; for example, then it can fold this expression to just 48,
;; which is nice©
;; We call four times a (defmacro) with hard-coded directions.
;; The hard-coded parameter must go all the way down
;; to the last instructions needing that direction.
;; So we have (defmacro) calling other (defmacro).
;; What is the purpose `eval-when-compile'?
;; It folds down a numerical expression to just one number
;; for instance
;; (eval-when-compile (ash 3 (* 2 uniline--direction-lf←)))
;; is converted to just 192
;; This happens both in interpreted and byte-compiled code
;; Otherwise the operations ash, multiplication,
;; and retrieval from `uniline--direction-lf←'
;; would be computed over and over at runtime,
;; with always the same 192 result.
;; We could put directly 192 in the source code,
;; but this would defeat maintenance and readability.
;; What is the purpose `eval-and-compile'?
;; The byte-compiler expands all defmacro' called in defun's.
;; In turn, those defmacro's need to access some defconst's,
;; notably `uniline--direction-up↑' and sisters.
;; So those defconst's are made available to the byte-compiler
;; as well as to the runtime, by embedding them in a
;; `eval-and-compile' declaration.
;; When a decision makes the byte-compiled code better
;; (faster to load and run), at the expense of a slower
;; interpreted counterpart, then go ahead, bias toward
;; byte-compiled code.
;;;╭────────────────────────────────────────────────────────╮
;;;│4 directions, infinite buffer in right & down directions│
;;;╰────────────────────────────────────────────────────────╯
(eval-and-compile
(defconst uniline--direction-up↑ 0)
(defconst uniline--direction-ri→ 1)
(defconst uniline--direction-dw↓ 2)
(defconst uniline--direction-lf← 3))
(eval-when-compile ; not needed at runtime
(defun uniline--reverse-direction (dir)
"Reverse DIR.
DIR is any of the 4 `uniline--direction-*'.
Exchange left with right, up with down."
(pcase dir
('uniline--direction-up↑ 'uniline--direction-dw↓)
('uniline--direction-ri→ 'uniline--direction-lf←)
('uniline--direction-dw↓ 'uniline--direction-up↑)
('uniline--direction-lf← 'uniline--direction-ri→)
(_ (error "Bad direction")))))
(defun uniline--move-to-column (x)
"Move to column X staying on the same line.
Add blanks if line is too short: ensure that cursor
points to a character, not \\n.
Move to 0 if X negative."
(move-to-column (1+ (max x 0)) t)
(forward-char -1))
(defun uniline--move-to-delta-column (x)
"Move X characters, staying on the same line.
Add blanks if line is too short.
X may be negative to move backward.
Move to 0 if target is beyond the left border of buffer."
(uniline--move-to-column (+ (current-column) x)))
(defsubst uniline--forward-line-force (y)
"Helper function to move cursor Y lines.
Create lines at the end of the buffer if there
are not enough lines to honor Y.
Y may be negative.
Does not preserve current column."
;; here we fix a bug in the return of (forward-line):
;; when (forward-line) cannot move enough lines
;; because it is stuck at the end of buffer,
;; it erroneously returns one less missing forwards
;; but the bug does not appears if the end-of-buffer
;; is at the beginning-of-line
;; so here we get out of the corner-case of the
;; (forward-line) bug, by ensuring that there is an empty
;; line at the end of buffer
(goto-char
(prog1 (point)
(goto-char (point-max))
(unless (bolp)
(self-insert-command 1 ?\n))))
(let ((y (forward-line y))) ; faster than (setq y (forward-line y))
(when (> y 0)
(self-insert-command y ?\n)
(forward-line y))))
(defun uniline--move-to-line (y)
"Move to line Y, while staying on the same column.
Create blank lines at the end of the buffer if needed,
or blank characters at the end of target line.
Y=0 means first line in buffer."
(move-to-column
(prog1
(1+ (current-column))
(goto-char (point-min))
(uniline--forward-line-force y))
t)
(forward-char -1))
(defun uniline--move-to-delta-line (y)
"Move Y lines while staying on the same column.
Create blank lines at the end of the buffer if needed,
or blank characters at the end of target line.
Y may be negative to move backward."
(move-to-column
(prog1
(1+ (current-column))
(uniline--forward-line-force y))
t)
(forward-char -1))
(defun uniline--move-to-col-lin (y x)
"Move to line Y and column X.
Create blank lines at the end of the buffer if needed,
or blank characters at the end of target line if needed.
Y=0 means first line of buffer.
X=0 means first column of buffer."
(goto-char (point-min))
(uniline--forward-line-force y)
(move-to-column (1+ x) t)
(forward-char -1))
(eval-when-compile ; not needed at runtime
(defmacro uniline--move-in-direction (dir &optional nb)
"Move NB char in direction DIR.
NB defaults to 1.
This is a macro, therefore it is as if writing
directly (uniline--move-to-delta-line -1) and such,
with no overhead."
(declare (debug (form)))
(unless nb (setq nb 1))
(pcase dir
('uniline--direction-up↑ `(uniline--move-to-delta-line (- ,nb)))
('uniline--direction-ri→ `(uniline--move-to-delta-column ,nb) )
('uniline--direction-dw↓ `(uniline--move-to-delta-line ,nb) )
('uniline--direction-lf← `(uniline--move-to-delta-column (- ,nb)))
(_ (error "Bad direction")))))
(eval-when-compile ; not needed at runtime
(defmacro uniline--at-border-p (dir)
"Test if at a non-trespass-able border of buffer.
This happens at the first line or at the first column,
when trying to go further when DIR is up or left:
`uniline--direction-up↑' or `uniline--direction-lf←'.
In the bottom & right directions the buffer is infinite."
(declare (debug (form)))
(pcase dir
('uniline--direction-up↑ '(= (pos-bol) 1))
('uniline--direction-ri→ 'nil)
('uniline--direction-dw↓ 'nil)
('uniline--direction-lf← '(bolp))
(_ (error "Bad direction")))))
;;;╭─────────────────────────────────────────────────────╮
;;;│Reference tables of ┼ 4 half-lines UNICODE characters│
;;;╰─────────────────────────────────────────────────────╯
;; Hereafter `4halfs' means a representation of a UNICODE character
;; made of half-lines, like ┖ or ┶, as a single number.
;; There are 4 half-lines, one in each direction
;; (north, east, west, south). Each half line may have one of
;; 4 styles: blank ( ), thin (─), thick (━), double (═).
;; So a `4halfs' number goes from 0 to 4x4x4x4 = 256.
;; This representation is great because it is easily handled by
;; the bits manipulation primitives: `logior', `logand', `ash'.
(eval-when-compile ; not needed at runtime
(defun uniline--pack-4halfs (urld)
"Encode a description of lines into a single number.
A character contains half lines upward, right, downward,
left.
Each of those half lines may have one of 4 styles:
0: no line
1: thin line
2: thick line
3: double line
Example: ╀ has description 2 1 1 1
thick=2 upward, and thin=1 in the other direction
The parameter URLD is a list of 4 numbers in [0..3]
for the 4 directions.
A single number encoding all possible combinations has a
range of [0..256). It is handy to index vectors rather than
4 dimensions matrices."
(logior
(ash (car urld) (eval-when-compile (* 2 uniline--direction-up↑)))
(ash (cadr urld) (eval-when-compile (* 2 uniline--direction-ri→)))
(ash (caddr urld) (eval-when-compile (* 2 uniline--direction-dw↓)))
(ash (cadddr urld) (eval-when-compile (* 2 uniline--direction-lf←))))))
(eval-when-compile ; not used at runtime
(defconst uniline--list-of-available-halflines
'(;;╭──unicode char
;;│ ╭───4half description
;;▽ ▽
( ? 0 0 0 0 )
( ?╵ 1 0 0 0 )
( ?╹ 2 0 0 0 )
( ?╶ 0 1 0 0 )
;;( ?└ 1 1 0 0 ) ;; prefer rounded corner
( ?╰ 1 1 0 0 )
( ?┖ 2 1 0 0 )
( ?╺ 0 2 0 0 )
( ?┕ 1 2 0 0 )
( ?┗ 2 2 0 0 )
( ?╷ 0 0 1 0 )
( ?│ 1 0 1 0 )
( ?┃ 2 0 2 0 )
( ?╿ 2 0 1 0 )
;;( ?┌ 0 1 1 0 ) ;; prefer rounded corner
( ?╭ 0 1 1 0 )
( ?├ 1 1 1 0 )
( ?┞ 2 1 1 0 )
( ?┍ 0 2 1 0 )
( ?┝ 1 2 1 0 )
( ?┡ 2 2 1 0 )
( ?╻ 0 0 2 0 )
( ?╽ 1 0 2 0 )
( ?┎ 0 1 2 0 )
( ?┟ 1 1 2 0 )
( ?┠ 2 1 2 0 )
( ?┏ 0 2 2 0 )
( ?┢ 1 2 2 0 )
( ?┣ 2 2 2 0 )
( ?╴ 0 0 0 1 )
;;( ?┘ 1 0 0 1 ) ;; prefer rounded corner
( ?╯ 1 0 0 1 )
( ?┚ 2 0 0 1 )
( ?─ 0 1 0 1 )
( ?┴ 1 1 0 1 )
( ?┸ 2 1 0 1 )
( ?╼ 0 2 0 1 )
( ?┶ 1 2 0 1 )
( ?┺ 2 2 0 1 )
;;( ?┐ 0 0 1 1 ) ;; prefer rounded corner
( ?╮ 0 0 1 1 )
( ?┤ 1 0 1 1 )
( ?┦ 2 0 1 1 )
( ?┬ 0 1 1 1 )
( ?┼ 1 1 1 1 )
( ?╀ 2 1 1 1 )
( ?┮ 0 2 1 1 )
( ?┾ 1 2 1 1 )
( ?╄ 2 2 1 1 )
( ?┒ 0 0 2 1 )
( ?┧ 1 0 2 1 )
( ?┨ 2 0 2 1 )
( ?┰ 0 1 2 1 )
( ?╁ 1 1 2 1 )
( ?╂ 2 1 2 1 )
( ?┲ 0 2 2 1 )
( ?╆ 1 2 2 1 )
( ?╊ 2 2 2 1 )
( ?╸ 0 0 0 2 )
( ?┙ 1 0 0 2 )
( ?┛ 2 0 0 2 )
( ?╾ 0 1 0 2 )
( ?┵ 1 1 0 2 )
( ?┹ 2 1 0 2 )
( ?━ 0 2 0 2 )
( ?┷ 1 2 0 2 )
( ?┻ 2 2 0 2 )
( ?┑ 0 0 1 2 )
( ?┥ 1 0 1 2 )
( ?┩ 2 0 1 2 )
( ?┭ 0 1 1 2 )
( ?┽ 1 1 1 2 )
( ?╃ 2 1 1 2 )
( ?┯ 0 2 1 2 )
( ?┿ 1 2 1 2 )
( ?╇ 2 2 1 2 )
( ?┓ 0 0 2 2 )
( ?┪ 1 0 2 2 )
( ?┫ 2 0 2 2 )
( ?┱ 0 1 2 2 )
( ?╅ 1 1 2 2 )
( ?╉ 2 1 2 2 )
( ?┳ 0 2 2 2 )
( ?╈ 1 2 2 2 )
( ?╋ 2 2 2 2 )
( ?╒ 0 3 1 0 )
( ?╞ 1 3 1 0 )
( ?║ 3 0 3 0 )
( ?╓ 0 1 3 0 )
( ?╟ 3 1 3 0 )
( ?╔ 0 3 3 0 )
( ?╠ 3 3 3 0 )
( ?╜ 3 0 0 1 )
( ?╨ 3 1 0 1 )
( ?╖ 0 0 3 1 )
( ?╢ 3 0 3 1 )
( ?╥ 0 1 3 1 )
( ?╫ 3 1 3 1 )
( ?╛ 1 0 0 3 )
( ?╝ 3 0 0 3 )
( ?═ 0 3 0 3 )
( ?╧ 1 3 0 3 )
( ?╩ 3 3 0 3 )
( ?╕ 0 0 1 3 )
( ?╡ 1 0 1 3 )
( ?╤ 0 3 1 3 )
( ?╪ 1 3 1 3 )
( ?╗ 0 0 3 3 )
( ?╣ 3 0 3 3 )
( ?╦ 0 3 3 3 )
( ?╬ 3 3 3 3 )
( ?╙ 3 1 0 0 )
( ?╘ 1 3 0 0 )
( ?╚ 3 3 0 0 ))))
(eval-when-compile ; not used at runtime
(defconst uniline--list-of-double-halflines
'(;; ╭─missing ╭───replacement
;; │ │ │
;; ▽ ▽ ▽
((0 0 0 3) (0 0 0 2)) ;; ╸
((0 0 2 3) (0 0 3 3)) ;; ╗
((0 0 3 0) (0 0 2 0)) ;; ╻
((0 0 3 2) (0 0 3 3)) ;; ╗
((0 1 0 3) (0 3 0 3)) ;; ═
((0 1 1 3) (0 3 1 3)) ;; ╤
((0 1 2 3) (0 1 2 2)) ;; ┱
((0 1 3 2) (0 1 2 2)) ;; ┱
((0 1 3 3) (0 3 3 3)) ;; ╦
((0 2 0 3) (0 3 0 3)) ;; ═
((0 2 1 3) (0 3 1 3)) ;; ╤
((0 2 2 3) (0 3 3 3)) ;; ╦
((0 2 3 0) (0 3 3 0)) ;; ╔
((0 2 3 1) (0 1 3 1)) ;; ╥
((0 2 3 2) (0 3 3 3)) ;; ╦
((0 2 3 3) (0 3 3 3)) ;; ╦
((0 3 0 0) (0 2 0 0)) ;; ╺
((0 3 0 1) (0 3 0 3)) ;; ═
((0 3 0 2) (0 3 0 3)) ;; ═
((0 3 1 1) (0 3 1 3)) ;; ╤
((0 3 1 2) (0 3 1 3)) ;; ╤
((0 3 2 0) (0 3 3 0)) ;; ╔
((0 3 2 1) (0 2 2 1)) ;; ┲
((0 3 2 2) (0 3 3 3)) ;; ╦
((0 3 2 3) (0 3 3 3)) ;; ╦
((0 3 3 1) (0 3 3 3)) ;; ╦
((0 3 3 2) (0 3 3 3)) ;; ╦
((1 0 2 3) (1 0 1 3)) ;; ╡
((1 0 3 0) (3 0 3 0)) ;; ║
((1 0 3 1) (3 0 3 1)) ;; ╢
((1 0 3 2) (1 0 2 2)) ;; ┪
((1 0 3 3) (3 0 3 3)) ;; ╣
((1 1 0 3) (1 3 0 3)) ;; ╧
((1 1 1 3) (1 3 1 3)) ;; ╪
((1 1 2 3) (1 1 2 2)) ;; ╅
((1 1 3 0) (3 1 3 0)) ;; ╟
((1 1 3 1) (3 1 3 1)) ;; ╫
((1 1 3 2) (1 1 2 2)) ;; ╅
((1 1 3 3) (3 3 3 3)) ;; ╬
((1 2 0 3) (1 3 0 3)) ;; ╧
((1 2 1 3) (1 3 1 3)) ;; ╪
((1 2 2 3) (1 2 2 2)) ;; ╈
((1 2 3 0) (1 2 2 0)) ;; ┢
((1 2 3 1) (1 2 2 1)) ;; ╆
((1 2 3 2) (1 2 2 2)) ;; ╈
((1 2 3 3) (3 3 3 3)) ;; ╬
((1 3 0 1) (1 3 0 3)) ;; ╧
((1 3 0 2) (1 3 0 3)) ;; ╧
((1 3 1 1) (1 3 1 3)) ;; ╪
((1 3 1 2) (1 3 1 3)) ;; ╪
((1 3 2 0) (1 2 2 0)) ;; ┢
((1 3 2 1) (1 2 2 1)) ;; ╆
((1 3 2 2) (1 2 2 2)) ;; ╈
((1 3 2 3) (1 3 1 3)) ;; ╪
((1 3 3 0) (3 3 3 0)) ;; ╠
((1 3 3 1) (3 3 3 3)) ;; ╬
((1 3 3 2) (3 3 3 3)) ;; ╬
((1 3 3 3) (3 3 3 3)) ;; ╬
((2 0 0 3) (3 0 0 3)) ;; ╝
((2 0 1 3) (1 0 1 3)) ;; ╡
((2 0 2 3) (3 0 3 3)) ;; ╣
((2 0 3 0) (3 0 3 0)) ;; ║
((2 0 3 1) (3 0 3 1)) ;; ╢
((2 0 3 2) (3 0 3 3)) ;; ╣
((2 0 3 3) (3 0 3 3)) ;; ╣
((2 1 0 3) (2 1 0 2)) ;; ┹
((2 1 1 3) (2 1 1 2)) ;; ╃
((2 1 2 3) (2 1 2 2)) ;; ╉
((2 1 3 0) (3 1 3 0)) ;; ╟
((2 1 3 1) (3 1 3 1)) ;; ╫
((2 1 3 2) (2 1 2 2)) ;; ╉
((2 1 3 3) (3 3 3 3)) ;; ╬
((2 2 0 3) (3 3 0 3)) ;; ╩
((2 2 1 3) (2 2 1 2)) ;; ╇
((2 2 2 3) (2 2 2 2)) ;; ╋
((2 2 3 0) (3 3 3 0)) ;; ╠
((2 2 3 1) (2 2 2 1)) ;; ╊
((2 2 3 2) (2 2 2 2)) ;; ╋
((2 2 3 3) (3 3 3 3)) ;; ╬
((2 3 0 0) (3 3 0 0)) ;; ╚
((2 3 0 1) (2 2 0 1)) ;; ┺
((2 3 0 2) (3 3 0 3)) ;; ╩
((2 3 0 3) (3 3 0 3)) ;; ╩
((2 3 1 0) (1 3 1 0)) ;; ╞
((2 3 1 1) (2 2 1 1)) ;; ╄
((2 3 1 2) (2 2 1 2)) ;; ╇
((2 3 1 3) (1 3 1 3)) ;; ╪
((2 3 2 0) (3 3 3 0)) ;; ╠
((2 3 2 1) (2 2 2 1)) ;; ╊
((2 3 2 2) (2 2 2 2)) ;; ╋
((2 3 2 3) (3 3 3 3)) ;; ╬
((2 3 3 0) (3 3 3 0)) ;; ╠
((2 3 3 1) (3 3 3 3)) ;; ╬
((2 3 3 2) (3 3 3 3)) ;; ╬
((2 3 3 3) (3 3 3 3)) ;; ╬
((3 0 0 0) (2 0 0 0)) ;; ╹
((3 0 0 2) (3 0 0 3)) ;; ╝
((3 0 1 0) (3 0 3 0)) ;; ║
((3 0 1 1) (3 0 3 1)) ;; ╢
((3 0 1 2) (2 0 1 2)) ;; ┩
((3 0 1 3) (3 0 3 3)) ;; ╣
((3 0 2 0) (3 0 3 0)) ;; ║
((3 0 2 1) (3 0 3 1)) ;; ╢
((3 0 2 2) (3 0 3 3)) ;; ╣
((3 0 2 3) (3 0 3 3)) ;; ╣
((3 0 3 2) (3 0 3 3)) ;; ╣
((3 1 0 2) (2 1 0 2)) ;; ┹
((3 1 0 3) (3 3 0 3)) ;; ╩
((3 1 1 0) (3 1 3 0)) ;; ╟
((3 1 1 1) (3 1 3 1)) ;; ╫
((3 1 1 2) (2 1 1 2)) ;; ╃
((3 1 1 3) (3 3 3 3)) ;; ╬
((3 1 2 0) (3 1 3 0)) ;; ╟
((3 1 2 1) (3 1 3 1)) ;; ╫
((3 1 2 2) (2 1 2 2)) ;; ╉
((3 1 2 3) (3 3 3 3)) ;; ╬
((3 1 3 2) (3 1 3 1)) ;; ╫
((3 1 3 3) (3 3 3 3)) ;; ╬
((3 2 0 0) (3 3 0 0)) ;; ╚
((3 2 0 1) (2 2 0 1)) ;; ┺
((3 2 0 2) (3 3 0 3)) ;; ╩
((3 2 0 3) (3 3 0 3)) ;; ╩
((3 2 1 0) (2 2 1 0)) ;; ┡
((3 2 1 1) (2 2 1 1)) ;; ╄
((3 2 1 2) (2 2 1 2)) ;; ╇
((3 2 1 3) (3 3 3 3)) ;; ╬
((3 2 2 0) (3 3 3 0)) ;; ╠
((3 2 2 1) (2 2 2 1)) ;; ╊
((3 2 2 2) (2 2 2 2)) ;; ╋
((3 2 2 3) (3 3 3 3)) ;; ╬
((3 2 3 0) (3 3 3 0)) ;; ╠
((3 2 3 1) (3 1 3 1)) ;; ╫
((3 2 3 2) (3 3 3 3)) ;; ╬
((3 2 3 3) (3 3 3 3)) ;; ╬
((3 3 0 1) (3 3 0 3)) ;; ╩
((3 3 0 2) (3 3 0 3)) ;; ╩
((3 3 1 0) (3 3 3 0)) ;; ╠
((3 3 1 1) (3 3 3 3)) ;; ╬
((3 3 1 2) (3 3 3 3)) ;; ╬
((3 3 1 3) (3 3 3 3)) ;; ╬
((3 3 2 0) (3 3 3 0)) ;; ╠
((3 3 2 1) (3 3 3 3)) ;; ╬
((3 3 2 2) (3 3 3 3)) ;; ╬
((3 3 2 3) (3 3 3 3)) ;; ╬
((3 3 3 1) (3 3 3 3)) ;; ╬
((3 3 3 2) (3 3 3 3)) ;; ╬
)))
(defconst uniline--4halfs-to-char
(eval-when-compile
(let ((table (make-vector (* 4 4 4 4) nil)))
(cl-loop
for x in uniline--list-of-available-halflines
do
(aset table
(uniline--pack-4halfs (cdr x))
(car x)))
(cl-loop
for x in uniline--list-of-double-halflines
do
(aset table
(uniline--pack-4halfs (car x))
(aref table
(uniline--pack-4halfs (cadr x)))))
table))
"Convert a 4halfs description to a UNICODE character.
The 4halfs description is (UP RI DW LF)
packed into a single integer.
As there are no UNICODE character for every combination,
the visually closest UNICODE character is retrieved.
So for instance
up=1 (thin up),
ri=2 (thick right),
dw=0 (blank down),
lf=0 (blank left),
is encoded into up +4*ri +16*dw +64*lf = 9,
which in turn is converted to ┕.")
(defconst uniline--char-to-4halfs
(eval-when-compile
(let ((table (make-char-table 'uniline--char-to-4halfs)))
(cl-loop
for x in uniline--list-of-available-halflines
do
(aset table
(car x)
(uniline--pack-4halfs (cdr x))))
table))
"Convert a UNICODE character to a 4halfs description.
The UNICODE character is supposed to represent
a combination of half lines in 4 directions
and in 4 brush styles.
The retrieved value is a4halfs description is (UP RI DW LF)
packed into a single integer.
If the UNICODE character is not a box-drawing one, nil
is returned.
So for instance, the character ┸ is converted to (2 1 0 1)
meaning:
2 = thick up
1 = thin right
0 = blank down
1 = thin left
Values (2 1 0 1) are encoded into 2 + 4*1 + 0*16 + 1*64 = 70
This table is the reverse of `uniline--4halfs-to-char'
without the fall-back characters.")
(when nil
;; This inactive code was used to generate the
;; `uniline--list-of-double-halflines' list above.
;; As the ╬ double line glyphs combinations were not all defined
;; in the UNICODE standard, a penalty system was applied to look
;; for the closest possible alternate glyph.
;; For example, ├ and ╠ exist, but a mixture of both do no exit,
;; hence a line like this one
;; ((3 3 1 0) (3 3 3 0)) ;; ╠
;; which says that, when this ├ is needed, with the upward and
;; rightward branches in double line style, then fallback to ╠
;; There is no need to re-run it, except if one wants to change
;; the penalties applied.
;;
;; To run it re-compute `uniline--4halfs-to-char'
;; - but with only the first cl-loop over
;; `uniline--list-of-available-halflines'
;; - not the loop over
;; `uniline--list-of-double-halflines'
(defun uniline--penalty1 (a b)
(pcase a
(0 (if (eq b 0) 0 1000))
(1 (pcase b
(0 1000)
(1 0)
(2 3)
(3 2)))
(2 (pcase b
(0 1000)
(1 3)
(2 0)
(3 1)))
(3 (pcase b
(0 1000)
(1 4)
(2 3)
(3 0)))))
(defun uniline--penalty (u1 r1 d1 l1 u2 r2 d2 l2)
(+
(uniline--penalty1 u1 u2)
(uniline--penalty1 r1 r2)
(uniline--penalty1 d1 d2)
(uniline--penalty1 l1 l2)))
(switch-to-buffer "replace.el")
(dotimes (u1 4)
(dotimes (r1 4)
(dotimes (d1 4)
(dotimes (l1 4)
(unless (aref uniline--4halfs-to-char
(uniline--pack-4halfs (list u1 r1 d1 l1)))
(let ((m 9999)
m0
u3 r3 d3 l3)
(dotimes (u2 4)
(dotimes (r2 4)
(dotimes (d2 4)
(dotimes (l2 4)
(when (aref uniline--4halfs-to-char
(uniline--pack-4halfs (list u2 r2 d2 l2)))
(setq m0 (uniline--penalty u1 r1 d1 l1 u2 r2 d2 l2))
(if (< m0 m)
(setq m m0
u3 u2
r3 r2
d3 d2
l3 l2)))))))
(insert
(format "((%d %d %d %d) (%d %d %d %d)) ;; %c\n"
u1 r1 d1 l1
u3 r3 d3 l3
(aref uniline--4halfs-to-char
(uniline--pack-4halfs (list u3 r3 d3 l3))))))))))))
;;;╭────────────────────────────────────────────────────────╮
;;;│Reference tables of △▶↓□◆● arrows & other UNICODE glyphs│
;;;╰────────────────────────────────────────────────────────╯
(eval-when-compile ; helper constant not needed at runtime
(defconst uniline--glyphs-tmp
'(
;; arrows
(a ?△ ?▷ ?▽ ?◁) ;; white *-pointing triangle
(a ?▲ ?▶ ?▼ ?◀) ;; black *-pointing triangle
(a ?↑ ?→ ?↓ ?←) ;; *wards arrow
(a ?▵ ?▹ ?▿ ?◃) ;; white *-pointing small triangle"
(a ?▴ ?▸ ?▾ ?◂) ;; black *-pointing small triangle
;; Those commented-out arrows are monospaces and supported
;; by the 6 fonts. But they do not have 4 directions.
;;(a ?↕ ?↔ ?↕ ?↔) ;; up-dw-lf-ri arrow
;;(a ?‹ ?› ?› ?‹) ;; single *-pointing angle quotation mark
;; squares
(s ?□) ;; white square
(s ?■) ;; black square
(s ?▫) ;; white small square
(s ?▪) ;; black small square
(s ?◇) ;; white diamond
(s ?◆) ;; black diamond
(s ?◊) ;; lozenge
;; o shapes
(o ?·) ;; middle dot
(o ?∙) ;; bullet operator
(o ?•) ;; bullet
(o ?●) ;; black circle
(o ?◦) ;; white bullet
(o ?Ø) ;; latin capital letter o with stroke
(o ?ø) ;; latin small letter o with stroke
;; crosses
(x ?╳) ;; box drawings light diagonal cross
(x ?÷) ;; division sign
(x ?×) ;; multiplication sign
(x ?±) ;; plus-minus sign
(x ?¤) ;; currency sign
;; The following commented-out glyphs are possible additions
;; when using the DejaVu Sans Mono font
;; Other fonts either do not support those glyphs
;; or do not make them monospaced
;;(t ?⋏ ?≻ ?⋎ ?≺) ;; precedes
;;(t ?⊥ ?⊢ ?⊤ ?⊣) ;; * tack
;;(t ?⋂ ?⊃ ?⋃ ?⊂) ;; subset of
;;(a ?⇡ ?⇢ ?⇣ ?⇠) ;; *wards dashed arrow
;;(a ?⇑ ?⇒ ?⇓ ?⇐) ;; *wards double arrow
;;(a ?⇧ ?⇨ ?⇩ ?⇦) ;; *wards white arrow
;;(b ?↥ ?↦ ?↧ ?↤) ;; *wards arrow from bar
;;(b ?↟ ?↠ ?↡ ?↞) ;; *wards two headed arrow
;;(b ?⇈ ?⇉ ?⇊ ?⇇) ;; *wards paired arrows
;;(b ?☝ ?☞ ?☟ ?☜) ;; white * pointing index
;;(b ?⇞ ?⇻ ?⇟ ?⇺) ;; *wards arrow with double vertical stroke
;;(c ?⬘ ?⬗ ?⬙ ?⬖) ;; diamond with * half black
;;(c ?◓ ?◑ ?◒ ?◐) ;; circle with * half black
;;(b ?⍐ ?⍈ ?⍗ ?⍇) ;; apl functional symbol quad *wards arrow
;;(s ?▢) ;; white square with rounded corners
;;(s ?▣) ;; white square containing black small square
;;(s ?▩) ;; square with diagonal crosshatch fill
;;(s ?▤) ;; square with horizontal fill
;;(s ?▥) ;; square with vertical fill
;;(s ?▦) ;; square with orthogonal crosshatch fill
;;(s ?▧) ;; square with upper left to lower right fill
;;(s ?▨) ;; square with upper right to lower left fill
;;(o ?○) ;; white circle
;;(o ?◎) ;; bullseye
;;(o ?✪) ;; circled white star
;;(o ?o) ;; latin small letter o
;;(o ?O) ;; latin capital letter o
;;(o ?◍) ;; circle with vertical fill
;;(o ?◉) ;; fisheye
;;(o ?❂) ;; circled open centre eight pointed star
;;(o ?⚙) ;; gear
;;(o ?☹) ;; white frowning face
;;(o ?☺) ;; white smiling face
;;(o ?✆) ;; telephone location sign
;;(o ?✇) ;; tape drive
;;(x ?☓) ;; saltire
;;(x ?+) ;; plus sign
;;(x ?✔) ;; heavy check mark
;;(x ?✖) ;; heavy multiplication x
;;(x ?✚) ;; heavy greek cross
;;(x ?✜) ;; heavy open centre cross
;;(x ?x) ;; latin small letter x
;;(x ?X) ;; latin capital letter x
;;(x ?✙) ;; outlined greek cross
;;(x ?✛) ;; open centre cross
;;(d ?⚀) ;; die face-1
;;(d ?⚁) ;; die face-2
;;(d ?⚂) ;; die face-3
;;(d ?⚃) ;; die face-4
;;(d ?⚄) ;; die face-5
;;(d ?⚅) ;; die face-6
;;(d ?⊡) ;; squared dot operator
;;(d ?☒) ;; ballot box with x
;;(d ?☑) ;; ballot box with check
;;(d ?⊞) ;; squared plus
;;(l ?◇) ;; white diamond
;;(l ?◆) ;; black diamond
;;(l ?✦) ;; black four pointed star
;;(l ?✧) ;; white four pointed star
;;(l ?◈) ;; white diamond containing black small diamond
;;(l ?♠) ;; black spade suit
;;(l ?♥) ;; black heart suit
;;(l ?♦) ;; black diamond suit
;;(l ?♣) ;; black club suit
;;(l ?♤) ;; white spade suit
;;(l ?♡) ;; white heart suit
;;(l ?♢) ;; white diamond suit
;;(l ?♧) ;; white club suit
;;(f ?✿) ;; black florette
;;(f ?❀) ;; white florette
;;(f ?❄) ;; snowflake
;;(f ?✾) ;; six petalled black and white florette
;;(f ?❁) ;; eight petalled outlined black florette
;;(f ?❅) ;; tight trifoliate snowflake
;;(f ?❆) ;; heavy chevron snowflake
;;(w ?☼) ;; white sun with rays
;;(w ?☀) ;; black sun with rays
;;(w ?✫) ;; open centre black star
;;(w ?✭) ;; outlined black star
;;(w ?✩) ;; stress outlined white star
;;(w ?✪) ;; circled white star
;;(w ?✬) ;; black centre white star
;;(w ?✮) ;; heavy outlined black star
;;(w ?✯) ;; pinwheel star
;;(w ?✱) ;; heavy asterisk
;;(w ?✲) ;; open centre asterisk
;;(w ?✳) ;; eight spoked asterisk
;;(w ?✴) ;; eight pointed black star
;;(w ?✵) ;; eight pointed pinwheel star
;;(w ?✶) ;; six pointed black star
;;(w ?✷) ;; eight pointed rectilinear black star
;;(w ?✸) ;; heavy eight pointed rectilinear black star
;;(w ?✹) ;; twelve pointed black star
;;(w ?❂) ;; circled open centre eight pointed star
;;(k ?✻) ;; teardrop-spoked asterisk
;;(k ?✽) ;; heavy teardrop-spoked asterisk
;;(k ?❉) ;; balloon-spoked asterisk
;;(k ?❊) ;; eight teardrop-spoked propeller asterisk
;;(k ?✺) ;; sixteen pointed asterisk
;;(k ?✼) ;; open centre teardrop-spoked asterisk
;;(k ?❃) ;; heavy teardrop-spoked pinwheel asterisk
;;(k ?❋) ;; heavy eight teardrop-spoked propeller asterisk
;;(k ?❇) ;; sparkle
;;(k ?❈) ;; heavy sparkle
))
"List of good looking UNICODE glyphs.
Those are:
- arrows in 4 directions,
- lists of symmetric-in-4-directions characters.
There are hundred of UNICODEs, but most of them are not
fixed-width or height, even in mono-spaced fonts.
Here we selected only the fixed-size ones.")
(eval-and-compile
(defconst uniline--glyphs-bw
(eval-when-compile
(let ((r (reverse uniline--glyphs-tmp)))
;; nconc is used to create a circular list on purpose
(nconc r r)))
"List of good looking UNICODE glyphs.
Those are:
- arrows in 4 directions,
- lists of symmetric-in-4-directions characters.
There are hundred of UNICODEs, but most of them are not
fixed-width or height, even in mono-spaced fonts.
Here we selected only the fixed-size ones.
This list is ciurcular in backward order."))
(eval-and-compile
(defconst uniline--glyphs-fw
(eval-when-compile
;; nconc is used to create a circular list on purpose
(nconc uniline--glyphs-tmp uniline--glyphs-tmp))
"List of good looking UNICODE glyphs.
Those are:
- arrows in 4 directions,
- lists of symmetric-in-4-directions characters.
There are hundred of UNICODEs, but most of them are not
fixed-width or height, even in mono-spaced fonts.
Here we selected only the fixed-size ones.
This list is ciurcular in forward order."))
(eval-when-compile ; not needed at runtime
(defun uniline--make-hash (list)
"Helper function to build `uniline--glyphs-reverse-hash-*'.
Used only at package initialization.
LIST is `uniline--glyphs-fbw'."
(let ((hh (make-hash-table)))
(cl-loop
for ll on list
do
(if (cddar ll)
;; glyph is directional, like ▲ ▶ ▼ ◀
(cl-loop
for cc in (cdar ll)
for i from 0
do (puthash cc (cons i ll) hh))
;; glyph is not directional, like ■ ● ◆
(puthash (cadar ll) (cons nil ll) hh))
;; explicitly break out of circular list
if (eq (cdr ll) list)
return nil)
hh)))
(defconst uniline--glyphs-reverse-hash-fw
(eval-when-compile
(uniline--make-hash uniline--glyphs-fw))
"Same as `uniline--glyphs-fw' reversing keys & values.")
(defconst uniline--glyphs-reverse-hash-bw
(eval-when-compile
(uniline--make-hash uniline--glyphs-bw))
"Same as `uniline--glyphs-bw' reversing keys & values.")
;;;╭───────────────────────────────────────────────────────────╮
;;;│Reference tables of ▙▄▟▀ quadrant-blocks UNICODE characters│
;;;╰───────────────────────────────────────────────────────────╯
;; Hereafter `4quadb' means a representation of a quadrant-block
;; UNICODE character as a single number. This number must hold
;; all combinations of the 4 quarter-of-a-blocks. Each of the 4
;; may be present or absent. Therfore `4quadb' is a number from 0
;; to 2x2x2x2 = 16.
(eval-and-compile
(defconst uniline--block-4quadb-to-char
[ ? ?▘ ?▝ ?▀
?▖ ?▌ ?▞ ?▛
?▗ ?▚ ?▐ ?▜
?▄ ?▙ ?▟ ?█
]
"Convert a quadrant bitmap into a UNICODE character.
A quadrant bitmap is a number in [0..16) made of 4 bits.
Each bit says whether or not there is a quadrant-block
at a position.
Reverse of `uniline--block-char-to-4quadb'"))
(eval-and-compile
(defconst uniline--block-char-to-4quadb
(eval-when-compile
(let ((table (make-char-table 'uniline--block-char-to-4quadb)))
(cl-loop
for c across uniline--block-4quadb-to-char
for i from 0
do (aset table c i))
table))
"Convert a UNICODE character to a quadrant bitmap.
Reverse of `uniline--block-4quadb-to-char'"))
(defvar-local uniline--block-which-quadrant 0
"Where is the quadrant cursor.
To draw lines with quadrant-blocks like this ▙▄▟▀,
it is required to keep track where is the
quadrant-cursor. It can be at 4 sub-locations:
north-east, south-west, etc.
1: here──→───╮
╭─┴╮
0: here──→─┤▘▝│
2: here──→─┤▖▗│
╰─┬╯
3: here──→───╯")
;;;╭────────────────────────────────────────────────────────╮
;;;│Low level management of ┏╾╯half-lines UNICODE characters│
;;;╰────────────────────────────────────────────────────────╯
(defvar-local uniline--brush 1
"Controls the style of line.
Possible values are as follows:
nil: no action except cursor movements
0: erase,
1: simple line like ╰─┤
2: thick line like ┗━┫
3: double line like ╚═╣
:block block drawing like ▙▄▟▀")
(eval-when-compile ; not needed at runtime