summaryrefslogtreecommitdiff
path: root/src/modes.py
blob: efb697371e7c365da17ee2b17f45c078a2f19a23 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
#!/usr/bin/env python3
#
# Copyright © 2018, 2020 Christian Persch
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <https://www.gnu.org/licenses/>.


import argparse
import enum
import inspect
import pathlib
import sys
import typing

from dataclasses import dataclass, field

# Types

class Type(enum.IntEnum):
    ECMA = enum.auto() # CSI n [h|l]
    WHAT = enum.auto() # CSI ? n [h|l]
    GT   = enum.auto() # CSI > n [h|l]

class Flags(enum.Flag):
    NONE = 0
    WRITABLE = enum.auto()

class Source(enum.Enum):
    CONTOUR  = enum.auto()
    DEC      = enum.auto(),
    DRCSTERM = enum.auto(),
    ECMA48   = enum.auto() # eq ISO 6429
    HP       = enum.auto()
    KITTY    = enum.auto()
    ITERM2   = enum.auto()
    RLOGIN   = enum.auto()
    SCO      = enum.auto()
    VTE      = enum.auto()
    WYSE     = enum.auto()
    XDG      = enum.auto()
    XTERM    = enum.auto()

    @classmethod
    def from_name(cls, name):
        if name.startswith('CONTOUR'):
            return cls.CONTOUR
        elif name.startswith('DEC') or name.endswith('_DEC'):
            return cls.DEC
        elif name.startswith('DRCS'):
            return cls.DRCSTERM
        elif name.endswith('_ECMA'):
            return cls.ECMA35
        elif name.startswith('HP') or name.endswith('_HP'):
             return cls.HP
        elif name.startswith('KITTY'):
            return cls.KITTY
        elif name.startswith('ITERM'):
            return cls.ITERM2
        elif name.startswith('RLOGIN'):
            return cls.RLOGIN
        elif name.startswith('SCO'):
            return cls.SCO
        elif name.startswith('VTE'):
            return cls.VTE
        elif name.startswith('WY'):
            return cls.WYSE
        elif name.startswith('XDG'):
            return cls.XDG
        elif name.startswith('XTERM'):
            return cls.XTERM
        else:
            return cls.ECMA48
            #raise ValueError(f'Could not determine source for mode {name}')

# Control Sequence

@dataclass(eq=True, order=True)
class NamedMode:
    ''' A named mode '''
    mtype: Type
    number: int
    name: str
    default: bool
    flags: Flags=Flags.NONE
    source: typing.Optional[Source]=None
    alias: typing.Optional[typing.List[str]]=None
    comment: str=None
    sloc_file: str=None
    sloc_line: int=-1

    def __post_init__(self):

        if self.source is None:
            self.source = Source.from_name(self.name)

        if self.sloc_file is None or self.sloc_line == -1:
            fname = f'mode_{self.mtype.name}'
            stack = inspect.stack()
            depth = -1
            for _frame in stack:
                depth += 1
                if _frame.function == fname:
                    depth += 1
                    break

            if depth == -1 or depth >= len(stack):
                raise ValueError('{self.name} source location not found')
            else:
                frame = stack[depth]
                self.sloc_file = frame.filename
                self.sloc_line = frame.lineno

            del stack


def mode_ECMA(name, number, **kwargs):
    return NamedMode(mtype=Type.ECMA,
                     name=name,
                     number=number,
                     **kwargs)

def mode_WHAT(name, number, **kwargs):
    return NamedMode(mtype=Type.WHAT,
                     name=name,
                     number=number,
                     **kwargs)

def mode_GT(name, number, **kwargs):
    return NamedMode(mtype=Type.GT,
                     name=name,
                     number=number,
                     **kwargs)

# All known modes, ordered by type, source, and number

modes = [

    # Modes for SM_ECMA/RM_ECMA (CSI n [h|l])
    #
    # Most of these are not implemented in VTE.
    #
    # References: ECMA-48 § 7
    #             WY370

    mode_ECMA('GATM', 1, default=False),
    mode_ECMA('KAM', 2, default=False),
    mode_ECMA('CRM', 3, default=False),

    # IRM - insertion replacement mode
    #
    # Default: reset
    #
    # References: ECMA-48 § 7.2.10
    #             VT525
    #
    mode_ECMA('IRM', 4, default=False, flags=Flags.WRITABLE),

    mode_ECMA('SRTM', 5, default=False),
    mode_ECMA('ERM', 6, default=False),
    mode_ECMA('VEM', 7, default=False),

    # BDSM - Bi-Directional Support Mode
    #
    # Reset state is explicit mode, set state is implicit mode
    #
    # References: ECMA-48
    #             ECMA TR/53
    #             Terminal-wg/bidi
    #
    # Default in ECMA: reset
    # Default in Terminal-wg/bidi and VTE: set
    #
    mode_ECMA('BDSM', 8, default=True, flags=Flags.WRITABLE),

    # DCSM defaults to RESET in ECMA, forced to SET in Terminal-wg/bidi#
    mode_ECMA('DCSM', 9, default=True),

    mode_ECMA('HEM', 10, default=False),

    # ECMA-48 § F.4.1 Deprecated
    mode_ECMA('PUM', 11, default=False),

    # SRM - local echo send/receive mode
    # If reset, characters entered by the keyboard are shown on the
    # screen as well as being sent to the host; if set, the
    # keyboard input is only sent to the host.
    #
    # Default: set
    #
    # References: ECMA-48 § 7.2.15
    #             VT525
    #
    # Removed in VTE 0.60: issue #69
    #
    mode_ECMA('SRM', 12, default=True),

    mode_ECMA('FEAM', 13, default=False),
    mode_ECMA('FETM', 14, default=False),
    mode_ECMA('MATM', 15, default=False),
    mode_ECMA('TTM', 16, default=False),
    mode_ECMA('SATM', 17, default=False),
    mode_ECMA('TSM', 18, default=False),

    # ECMA-48 § F.5.1 Removed
    mode_ECMA('EBM', 19, default=False),

    # LNM - line feed/newline mode
    # If set, the cursor moves to the first column on LF, FF, VT,
    # and a Return key press sends CRLF.
    # If reset, the cursor column is unchanged by LF, FF, VT,
    # and a Return key press sends CR only.
    #
    # Default: reset
    #
    # References: ECMA-48 § F.5.2 Removed!
    #             VT525
    #
    mode_ECMA('LNM', 20, default=False),

    mode_ECMA('GRCM', 21, default=True),

    # ECMA-48 § F.4.2 Deprecated
    mode_ECMA('ZDM', 22, default=False),

    # WYDSCM - display disable mode
    # If set, blanks the screen; if reset, shows the data.
    #
    # Default: reset
    #
    # References: WY370
    #
    mode_ECMA('WYDSCM', 30, default=False),

    # WHYSTLINM - status line display mode
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYSTLINM', 31, default=False),

    # WYCRTSAVM - screen saver mode
    # Like DECCRTSM.
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYCRTSAVM', 32, default=False),

    # WYSTCURM - steady cursor mode
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYSTCURM', 33, default=False),

    # WYULCURM - underline cursor mode
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYULCURM', 34, default=False),

    # WYCLRM - width change clear disable mode
    # If set, the screen is not cleared when the column mode changes
    # by DECCOLM or WY161.
    # Note that this does not affect DECSCPP.
    # This is the same as DECNCSM mode.
    #
    # Default: set (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYCLRM', 35, default=True),

    # WYDELKM - delete key definition
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    # Note: Same as DECBKM
    mode_ECMA('WYDELKM', 36, default=False),

    # WYGATM - send characters mode
    # If set, sends all characters; if reset, only erasable characters.
    # Like GATM above.
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYGATM', 37, default=False),

    # WYTEXM - send full screen/scrolling region to printer
    # Like DECPEX mode.
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYTEXM', 38, default=False),

    # WYEXTDM - extra data line
    # If set, the last line of the screen is used as data line and not
    # a status line; if reset, the last line of the screen is used
    # as a status line.
    #
    # Default: reset
    #
    # References: WY370
    #
    mode_ECMA('WYEXTDM', 40, default=True),

    # WYASCII - WY350 personality mode
    # If set, switches to WY350 personality.
    #
    # Default: reset (set-up)
    #
    # References: WY370
    #
    mode_ECMA('WYASCII', 42, default=True),

    # ************************************************************************

    # Modes for SM_DEC/RM_DEC (CSI ? n [h|l])
    #
    # Most of these are not implemented in VTE.
    #
    # References: VT525
    #             XTERM
    #             KITTY
    #             MINTTY
    #             MLTERM
    #             RLogin
    #             URXVT
    #             WY370
    #

    # DEC:

    # DECCKM - cursor keys mode
    #
    # Controls whether the cursor keys send cursor sequences, or application
    # sequences.
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DEC_APPLICATION_CURSOR_KEYS', 1, default=False, flags=Flags.WRITABLE),

    # DECCOLM: 132 column mode
    #
    # Sets page width to 132 (set) or 80 (reset) columns.
    #
    # Changing this mode resets the top, bottom, left, right margins;
    # clears the screen (unless DECNCSM is set); resets DECLRMM; and clears
    # the status line if host-writable.
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DEC_132_COLUMN', 3, default=False, flags=Flags.WRITABLE),

    # DECANM - ansi-mode
    # Resetting this puts the terminal into VT52 compatibility mode.
    # To return to ECMA-48 mode, use ESC < (1/11 3/12).
    #
    # Default: set
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECANM', 2, default=True),

    # DECSCLM - scrolling mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECSCLM', 4, default=False),

    # DECSCNM - screen mode
    # If set, displays reverse; if reset, normal.
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DEC_REVERSE_IMAGE', 5, default=False, flags=Flags.WRITABLE),

    # DECOM - origin mode
    # If set, the cursor is restricted to within the page margins.
    #
    # On terminal reset, DECOM is reset.
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DEC_ORIGIN', 6, default=False, flags=Flags.WRITABLE),

    # DECAWM - auto wrap mode
    #
    # Controls whether text wraps to the next line when the
    # cursor reaches the right margin.
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DEC_AUTOWRAP', 7, default=False, flags=Flags.WRITABLE),

    # DECARM - autorepeat mode
    # Controls whether keys auytomatically repeat while held pressed
    # for more than 0.5s.
    # Note that /some/ keys do not repeat regardless of this setting.
    #
    # Default: set
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECARM', 8, default=True),

    mode_WHAT('XTERM_MOUSE_X10', 9, default=False, flags=Flags.WRITABLE),
    mode_WHAT('DECLTM', 11, default=False),
    mode_WHAT('DECEKEM', 16, default=False),

    # DECPFF - print FF mode
    # Controls whether the terminal terminates a print command by
    # sending a FF to the printer.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECCPFF', 18, default=False),

    # DECPEX - print extent mode
    # If set, print page prints only the scrolling region;
    # if reset, the complete page.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECPEX', 19, default=False),

    # DECTCEM - text cursor enable
    # If set, the text cursor is visible; if reset, invisible.
    #
    # Default: set
    #
    # References: VT525
    #
    mode_WHAT('DEC_TEXT_CURSOR', 25, default=True, flags=Flags.WRITABLE),

    # DECLRM - RTL mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECRLM', 34, default=False),

    # DECHEBM - hebrew/north-american keyboard mapping mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECHEBM', 35, default=False),

    # DECHEM - hebrew encoding mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECHEM', 36, default=False),

    mode_WHAT('XTERM_DECCOLM', 40, default=False, flags=Flags.WRITABLE),

    # DECNRCM - NRCS mode
    # Operates in 7-bit (set) or 8-bit (reset) mode.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECNRCM', 42, default=False),

    # DECGEPM - Graphics Expanded Print Mode
    #
    # Default: reset
    #
    # References: VT330
    #
    mode_WHAT('DECGEPM', 43, default=False),

    # DECGPCM - Graphics Print Colour Mode
    #
    # Default: reset
    #
    # References: VT330
    #
    # Note: Conflicts with XTERM_MARGIN_BELL
    #
    #mode_WHAT('DECGPCM', 44, default=False),

    # DECGCPS - Graphics Print Colour Syntax
    # If set, uses RGB colour format; if reset, uses HLS colour format.
    #
    # Default: reset
    #
    # References: VT330
    #
    # Note: conflicts with XTERM_REVERSE_WRAP
    #
    #mode_WHAT('DECGPCS', 45, default=False),

    # DECGPBM - Graphics Print Background Mode
    #
    # Default: reset
    #
    # References: VT330
    #
    # Note: conflicts with XTERM_LOGGING (which VTE does not implement)
    #
    #mode_WHAT('DECGPBM', 46, default=False),

    # DECGRPM - Graphics Rotated Print Mode
    #
    # Default: reset
    #
    # References: VT330
    #
    # Note: conflicts with XTERM_ALTBUF
    #
    #mode_WHAT('DECGRPM', 47, default=False),

    mode_WHAT('XTERM_ALTBUF', 47, default=False, flags=Flags.WRITABLE),

    mode_WHAT('DEC131TM', 53, default=False),

    # DECNAKB - greek/north-american keyboard mapping mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECNAKB', 57, default=False),

    # DECIPEM - enter/return to/from pro-printer emulation mode
    # Switches the terminal to (set)/from (reset) the ibm pro
    # printer protocol.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECIPEM', 58, default=False),

    # Kanji/Katakana Display Mode, from VT382-Kanji
    mode_WHAT('DECKKDM', 59, default=True),

    # DECHCCM - horizontal cursor coupling mode
    # Controls what happens when the cursor moves out of the left or
    # right margins of the window.
    # If set, the window pans to keep the cursor in view; if reset,
    # the cursor disappears.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECHCCM', 60, default=False),

    # DECVCCM - vertical cursor coupling mode
    # Controls what happens when the cursor moves out of the top or
    # bottom of the window, When the height of the window is smaller
    # than the page.
    # If set, the window pans to keep the cursor in view; if reset,
    # the cursor disappears.
    #
    # Default: set
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECVCCM', 61, default=True),

    # DECPCCM - page cursor coupling mode
    #
    # Default: set
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECPCCM', 64, default=True),

    # DECNKM - numeric/application keypad mode
    # Controls whether the numeric keypad sends application (set)
    # or keypad (reset) sequences.
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DEC_APPLICATION_KEYPAD', 66, default=False, flags=Flags.WRITABLE),

    # DECBKM - backarrow key mode
    # WYDELKM
    #
    # If set, the Backspace key works as a backspace key
    # sending the BS control; if reset, it works as a Delete
    # key sending the DEL control.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECBKM', 67, default=False, alias=['WYDELKM']),

    # DECKBUM - typewriter/data rpocessing keys mode
    #
    # If set, the keyboard keys act as data processing keys;
    # if reset, as typewriter keys.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECKBUM', 68, default=False),

    # DECLRMM - vertical split-screen mode
    # Controls whether a DECSLRM is executed.
    # On set, resets line attributes to single width and single height,
    # and while set, the terminal ignores any changes to line attributes.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Needs to be implemented if DECSLRM is implemented, to resolve a
    # conflict between DECSLRM and SCOSC.
    #
    # aka DECVSSM
    #
    mode_WHAT('DECLRMM', 69, default=False),

    # DECXRLM - transmit rate limit
    # If set, limits the transmit rate; if reset, the rate is
    # unlimited.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECXRLM', 73, default=False),

    # DECSDM - sixel display mode (scrolling)
    #
    # Default: set
    #
    # References: ?
    #
    # Note: Conflicts with WY161
    #
    mode_WHAT('DECSDM', 80, default=True, flags=Flags.WRITABLE),

    # DECKPM - key position mode
    # If set, the keyboard sends extended reports (DECEKBD) that include
    # the key position and modifier state; if reset, it sends character codes.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECKPM', 81, default=False),

    # Thai Space Compensating Mode, from VT382-Thai
    mode_WHAT('DECTHAISCM', 90, default=False),

    # DECNCSM - no clear screen on DECOLM
    # If set, the screen is not cleared when the column mode changes
    # by DECCOLM.
    # Note that this does not affect DECSCPP.
    #
    # Default: set
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECNCSM', 95, default=False),

    # DECRLCM - RTL copy mode
    # If set, copy/paste from RTL; if reset, from LTR.
    # Only enabled when the keyboard language is set to hebrew.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECRLCM', 96, default=False),

    # DECCRTSM - CRT save mode
    # When set, blanks the terminal after the inactivity timeout
    # (set with DECCRTST).
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECRCRTSM', 97, default=False),

    # DECARSM - auto resize mode
    # Sets whether changing page arrangements automatically
    # changes the lines per screen.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECARSM', 98, default=False),

    # DECMCM - modem control mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECMCM', 99, default=False),

    # DECAAM - auto answerback mode
    #
    # Default: reset
    #
    # References: VT525
    #
    mode_WHAT('DECAAM', 100, default=False),

    # DECCANSM - conceal answerback message mode
    #
    # Default: reset
    #
    # References: VT525
    #
    # Unimplemented, since we don't support answerback at all.
    #
    mode_WHAT('DECANSM', 101, default=False),

    # DECNULM - null mode
    # If set, pass NUL to the printer; if reset, discard NUL.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECNULM', 102, default=False),

    # DECHDPXM - half-duplex mode
    # Whether to use half-duplex (set) or full-duplex (reset) mode.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECHDPXM', 103, default=False),

    # DECESKM - enable secondary keyboard language mode
    # If set, use the secondary keyboard mapping (group 2); if reset,
    # use the primary (group 1).
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECESKM', 104, default=False),

    # DECOSCNM - overscan mode
    # (monochrome terminal only)
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECOSCNM', 106, default=False),

    # DECNUMLK - num lock mode
    #
    # Set the num lock state as if by acting the NumLock key.
    # Set means NumLock on; reset means off.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECNUMLK', 108, default=False),

    # DECCAPSLK - caps lock mode
    #
    # Set the caps lock state as if by acting the CapsLock key.
    # Set means CapsLock on; reset means off.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECCAPSLK', 109, default=False),

    # DECKLHIM - keyboard LED host indicator mode
    # If set, the keyboard LEDs show the state from the host
    # (see DECLL); if reset, the local state.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECKLHIM', 110, default=False),

    # DECFWM - framed window mode
    # If set, session window frames are drawn with frame border and icon.
    #
    # Default: reset
    #
    # References: VT525
    #
    # VTE does not support sessions.
    #
    mode_WHAT('DECFWM', 111, default=False),

    # DECRPL - review previous lines mode
    # If set, allows to view the scrollback.
    #
    # Default: set (VTE)
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECRPL', 112, default=True),

    # DECHWUM - host wake-up mode
    # If set, the terminal exits CRT save and energy save mode
    # when a character is received from the host.
    #
    # Default: ?
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECHWUM', 113, default=False),

    # DECTCUM - alternate text color underline mode
    #
    # If set, text with the undeerline attribute is underlined as
    # well as being displayed in the alternate coolor (if
    # specified); if reset, it is only displayed in the
    # alternate color.
    #
    # Default: ?
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECATCUM', 114, default=False),

    # DECTCBM - alternate text color blink mode
    #
    # If set, text with the blink attribute blinks as well
    # as being displayed in the alternate color (if
    # specified); if reset, it is only displayed in the
    # alternate color.
    #
    # Default: ?
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECATCBM', 115, default=False),

    # DECBBSM - bold and blink style mode
    #
    # If set, the bold or blink attributes affect both foreground
    # and background color; if reset, those affect only the foreground
    # color.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECBBSM', 116, default=False),

    # DECECM - erase color mode
    #
    # If set, erased text or new cells appearing on the screen by scrolling
    # are assigned the screen background color; if reset, they are assigned
    # the text background color.
    #
    # Default: reset
    #
    # References: VT525
    #
    # Probably not worth implementing.
    #
    mode_WHAT('DECECM', 117, default=False),

    # Contour:

    mode_WHAT('CONTOUR_BATCHED_RENDERING', 2026, default=False),

    # DRCSTerm:

    # DRCSMM_V1
    # Whether to enable DRCSMMv1 unicode mapping
    #
    # Default: reset
    #
    # References: DRCSTerm
    #
    mode_WHAT('DRCSMM_V1', 8800, default=False),

    # KiTTY:

    mode_WHAT('KITTY_STYLED_UNDERLINES', 2016, default=True),
    mode_WHAT('KITTY_EXTENDED_KEYBOARD', 2017, default=False),

    # MinTTY:

    mode_WHAT('MINTTY_REPORT_CJK_AMBIGUOUS_WIDTH', 7700, default=False),
    mode_WHAT('MINTTY_REPORT_SCROLL_MARKER_IN_CURRENT_LINE', 7711, default=False),
    mode_WHAT('MINTTY_APPLICATION_ESCAPE', 7727, default=False),
    mode_WHAT('MINTTY_ESCAPE_SENDS_FS',7728, default=False),

    # MINTTY_SIXEL_SCROLL_END_POSITION:
    # If set, sixel scrolling moves the cursor to the left margin on the
    # next line; if reset, moves the cursor to the right of the inserted
    # graphic.
    #
    # Default: reset
    #
    # References: MinTTY
    mode_WHAT('MINTTY_SIXEL_SCROLL_END_POSITION',7730, default=False),

    mode_WHAT('MINTTY_SCROLLBAR', 7766, default=False),
    mode_WHAT('MINTTY_REPORT_FONT_CHANGES', 7767, default=False),
    mode_WHAT('MINTTY_SHORTCUT_OVERRIDE', 7783, default=False),
    mode_WHAT('MINTTY_ALTBUF_MOUSEWHEEL_TO_CURSORKEYS', 7786, default=False),
    mode_WHAT('MINTTY_MOUSEWHEEL_APPLICATION_KEYS', 7787, default=False),
    mode_WHAT('MINTTY_BIDI_DISABLE_IN_CURRENT_LINE', 7796, default=False),

    # MINTTY_SIXEL_SCROLL_CURSOR_RIGHT:
    # If set, sixel scrolling moves the cursor to the right of the
    # inserted graphic; if reset, MINTTY_SIXEL_SCROLL_END_POSITION
    # takes effect.
    #
    # Default: reset
    #
    # References: MinTTY
    mode_WHAT('MINTTY_SIXEL_SCROLL_CURSOR_RIGHT', 8452, default=False,
              alias=['RLOGIN_SIXEL_SCROLL_CURSOR_RIGHT']),

    # MinTTY also knows mode 77096 "BIDI disable", and 77000..77031
    # "Application control key", all of  which are outside of the supported
    # range CSI parameters in VTE, so we don't list them here and VTE will
    # never support them.

    # RLogin:

    # RLogin has many private modes
    # [https://github.com/kmiya-culti/RLogin/blob/master/RLogin/TextRam.h#L131]:
    # 1406..1415, 1420..1425, 1430..1434, 1436, 1452..1481,
    # 8400..8406, 8416..8417, 8428..8429, 8435, 8437..8443,
    # 8446..8458,
    # and modes 7727, 7786, 8200 (home cursor on [ED 2]),
    # 8800 (DRCSMM_V1), 8840 (same as 8428).
    #
    # We're not going to implement them, but avoid these ranges
    # when assigning new mode numbers.
    #
    # The following are the ones from RLogin that MLTerm knows about:

    #mode_WHAT('RLOGIN_APPLICATION_ESCAPE', 7727, default=False)
    #mode_WHAT('RLOGIN_MOUSEWHEEL_TO_CURSORKEYS', 7786, default=False)

    # Ambiguous-width characters are wide (reset) or narrow (set)
    mode_WHAT('RLOGIN_AMBIGUOUS_WIDTH_CHARACTERS_NARROW', 8428, default=False),

    # XTERM also knows this one
    #mode_WHAT('RLOGIN_SIXEL_SCROLL_CURSOR_RIGHT', 8452, default=False),

    # [u]RXVT:

    mode_WHAT('RXVT_TOOLBAR', 10, default=False),
    mode_WHAT('RXVT_SCROLLBAR', 30, default=False),

    # Conflicts with DECHEBM
    #mode_WHAT('RXVT_SHIFT_KEYS', 35, default=False),

    mode_WHAT('RXVT_SCROLL_OUTPUT', 1010, default=False),
    mode_WHAT('RXVT_SCROLL_KEYPRESS', 1011, default=False),
    mode_WHAT('RXVT_MOUSE_EXT', 1015, default=False),

    # Bold/blink uses normal (reset) or high intensity (set) colour
    mode_WHAT('RXVT_INTENSITY_STYLES', 1021, default=True),

    # WYSE:

    # WYTEK - TEK 4010/4014 personality
    # If set, switches to TEK 4010/4014 personality.
    #
    # Default: reset
    #
    # References: WY370
    #
    mode_WHAT('WYTEK', 38, default=False, alias=['DECTEK']),

    # WY161 - 161 column mode
    # If set, switches the terminal to 161 columns; if reset,
    # to 80 columns.
    #
    # Default: reset
    #
    # References: WY370
    #
    # Note: Conflicts with DECSDM
    #mode_WHAT('WY161', 80, default=False),

    # WY52 - 52 lines mode
    # If set, switches the terminal to 52 lines; if reset,
    # to 24 lines.
    #
    # Default: reset
    #
    # References: WY370
    #
    mode_WHAT('WY52', 83, default=False),

    # WYENAT - enable separate attributes
    # If set, SGR attributes may be set separately for eraseable
    # and noneraseable characters. If reset, the same SGR attributes
    # apply to both eraseable and noneraseable characters.
    #
    #
    # Default: reset
    #
    # References: WY370
    #
    mode_WHAT('WYENAT', 84, default=False),

    # WYREPL - replacement character color
    #
    # Default: reset
    #
    # References: WY370
    #
    mode_WHAT('WYREPL', 85, default=False),

    # VTE:

    # Whether to swap the Left and Right arrow keys if the cursor
    # stands over an RTL paragraph.
    #
    # Default:  set
    #
    # Reference: Terminal-wg/bidi
    #
    mode_WHAT('VTE_BIDI_SWAP_ARROW_KEYS', 1243, default=True, flags=Flags.WRITABLE),

    # Whether box drawing characters in the U+2500..U+257F range
    # are to be mirrored in RTL context.
    #
    # Default: reset
    #
    # Reference: Terminal-wg/bidi
    #
    mode_WHAT('VTE_BIDI_BOX_MIRROR', 2500, default=False, flags=Flags.WRITABLE),

    # Whether BiDi paragraph direction is autodetected.
    #
    # Default: reset
    #
    # Reference: Terminal-wg/bidi
    #
    mode_WHAT('VTE_BIDI_AUTO', 2501, default=False, flags=Flags.WRITABLE),

    # XTERM:

    mode_WHAT('XTERM_ATT610_BLINK', 12, default=False),
    mode_WHAT('XTERM_CURSOR_BLINK', 13, default=False),
    mode_WHAT('XTERM_CURSOR_BLINK_XOR', 14, default=False),
    mode_WHAT('XTERM_CURSES_HACK', 41, default=False),
    mode_WHAT('XTERM_MARGIN_BELL', 44, default=False),
    mode_WHAT('XTERM_REVERSE_WRAP', 45, default=False),
    mode_WHAT('XTERM_LOGGING', 46, default=False),

    mode_WHAT('XTERM_MOUSE_VT220', 1000, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_MOUSE_VT220_HIGHLIGHT', 1001, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_MOUSE_BUTTON_EVENT', 1002, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_MOUSE_ANY_EVENT', 1003, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_FOCUS', 1004, default=False, flags=Flags.WRITABLE),

    mode_WHAT('XTERM_MOUSE_EXT', 1005, default=False),

    mode_WHAT('XTERM_MOUSE_EXT_SGR', 1006, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_ALTBUF_SCROLL', 1007, default=True, flags=Flags.WRITABLE),

    mode_WHAT('XTERM_8BIT_META', 1034, default=False),
    mode_WHAT('XTERM_NUMLOCK', 1035, default=False),

    mode_WHAT('XTERM_META_SENDS_ESCAPE', 1036, default=True, flags=Flags.WRITABLE),

    mode_WHAT('XTERM_DELETE_IS_DEL', 1037, default=False),
    mode_WHAT('XTERM_ALT_SENDS_ESCAPE', 1039, default=False),
    mode_WHAT('XTERM_KEEP_SELECTION', 1040, default=False),
    mode_WHAT('XTERM_SELECT_TO_CLIPBOARD', 1041, default=False),
    mode_WHAT('XTERM_BELL_URGENT', 1042, default=False),
    mode_WHAT('XTERM_PRESENT_ON_BELL', 1043, default=False),
    mode_WHAT('XTERM_KEEP_CLIPBOARD', 1044, default=False),
    mode_WHAT('XTERM_ALLOW_ALTBUF', 1046, default=True),

    mode_WHAT('XTERM_OPT_ALTBUF', 1047, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_SAVE_CURSOR', 1048, default=False, flags=Flags.WRITABLE),
    mode_WHAT('XTERM_OPT_ALTBUF_SAVE_CURSOR', 1049, default=False, flags=Flags.WRITABLE),

    mode_WHAT('XTERM_FKEYS_TERMCAP', 1050, default=False),
    mode_WHAT('XTERM_FKEYS_SUN', 1051, default=False),
    mode_WHAT('XTERM_FKEYS_HP', 1052, default=False),
    mode_WHAT('XTERM_FKEYS_SCO', 1053, default=False),
    mode_WHAT('XTERM_FKEYS_LEGACY', 1060, default=False),
    mode_WHAT('XTERM_FKEYS_VT220', 1061, default=False),

    # XTERM_SIXEL_PRIVATE_COLOR_REGISTERS:
    # When set, each SIXEL graphic uses newly initialised colour registers.
    # When reset, changes to colour registers from one SIXEL image are
    # saved and used for the next SIXEL graphic.
    #
    # Default: set
    #
    # References: XTERM
    #
    mode_WHAT('XTERM_SIXEL_PRIVATE_COLOR_REGISTERS', 1070, default=True, flags=Flags.WRITABLE),

    mode_WHAT('XTERM_READLINE_BUTTON1_MOVE_POINT', 2001, default=False),
    mode_WHAT('XTERM_READLINE_BUTTON2_MOVE_POINT', 2002, default=False),
    mode_WHAT('XTERM_READLINE_DBLBUTTON3_DELETE', 2003, default=False),

    # Whether to surround pasted text with CSI ~ sequences
    #
    # Default: reset
    #
    # References: XTERM
    #
    mode_WHAT('XTERM_READLINE_BRACKETED_PASTE', 2004, default=False, flags=Flags.WRITABLE),

    mode_WHAT('XTERM_READLINE_PASTE_QUOTE', 2005, default=False),
    mode_WHAT('XTERM_READLINE_PASTE_LITERAL_NL', 2006, default=False),

    # ************************************************************************

    # Modes for SM_HP/RM_HP (CSI > n [h|l])
    #
    # None of these are not implemented in VTE.
    #
    # References: HP 2397A

    # HP:

    # HP_MULTIPAGE:
    # If set, the terminal has multiple pages of 24 lines of display memory.
    # If reset, the terminal only has one page of 24 lines of display memory
    #
    # Default: reset
    #
    # References: HP 2397A
    mode_GT('HP_MULTIPAGE', 1, default=False),

    # HP_MEMLOCK:
    #
    # Default: reset
    #
    # References: HP 2397A
    mode_GT('HP_MEMLOCK', 2, default=False),

]

# Output generator

''' Write copyright header '''
def write_header(outfile):
    outfile.write('''
/* Generated by modes.py; do not edit! */

/*
 * Copyright © 2018, 2020 Christian Persch
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 */
''')


''' Write sequences '''
def write_modes(output, mtype):
    outfile = open(output.as_posix(), 'w')
    write_header(outfile)
    outfile.write('''
#if !defined(MODE) || !defined(MODE_FIXED)
#error "Must define MODE and MODE_FIXED before including this file"
#endif

''')

    for m in sorted([m for m in modes if m.mtype == mtype], key=lambda m: m.number):

        if m.flags & Flags.WRITABLE:
            outfile.write(f'MODE('
                          f'{m.name}, '
                          f'{m.number})\n')
        else:
            value = 'ALWAYS_SET' if m.default else 'ALWAYS_RESET'
            outfile.write(f'MODE_FIXED('
                          f'{m.name}, '
                          f'{m.number}, '
                          f'{value})\n')


# main

''' main '''
if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='modes include file generator')
    parser.add_argument('--destdir',
                        type=pathlib.Path,
                        default=pathlib.PosixPath('.'),
                        help='Output directory')

    try:
        args = parser.parse_args()
    except Exception as e:
        print(f'Failed to parse arguments: {e}')
        sys.exit(1)

    write_modes(args.destdir / "modes-ecma.hh", Type.ECMA)
    write_modes(args.destdir / "modes-dec.hh", Type.WHAT)
    # write_modes(args.destdir / "modes-hp.hh", Type.GT)