forked from BrettWitty/TADS-3-Mode-for-Emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tads3-mode.el
1655 lines (1514 loc) · 77.3 KB
/
tads3-mode.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
;;; tads3-mode.el --- A mode for editing TADS 3 code -*- lexical-binding: t; -*-
;;;;;;;;;;;
;; Homepage: https://github.com/alexispurslane/tads3-mode
;;
;; This version was modified by Alexis Purslane <[email protected]> and Brett
;; Witty <[email protected]> from Stephen Granade's tads2-mode.el. The
;; main changes made by Witty were a modified regexp to deal with object
;; definitions so that tads3-next-object and tads3-prev-object work nicer, which
;; also helped with indenting TADS 3-style object code. Purslane has added many
;; significant further changes, which can be found on the homepage.
;;
;;
;; Remaining problems:
;; - Multiline C-style comments like:
;; /* This
;; is
;; a comment */
;; still have font-lock problems. Multiline font-locking is known to
;; be difficult.
;; - In such comments, an apostrophe (') will try to match with
;; something nonsense later on.
;; - You cannot move to sub-objects via tads3-next-object.
;;
;; It is VERY strongly recommended that swapq-fill.el is used in
;; conjunction with this mode, to assist in single quote filling.
;;;;;;;;;;;
;; Author: Alexis Purslane <[email protected]>
;; Modified 04 Jun 2024
;; Version 1.5
;; Package-Requires: ((emacs "29.1"))
;; Keywords: languages, tads, text-adventure, interactive-fiction
;; Author: Alexis Purslane <[email protected]>
;; Modified 17 Mar 2023
;; Version 1.4
;; Package-Requires: ((emacs "28.1"))
;; Keywords: languages, tads, text-adventure, interactive-fiction
;; Previous version:
;; Author: Brett Witty <[email protected]>
;; Modified: 4 Feb 2006
;; Version: 1.3
;; Keywords: languages
;; Previous version:
;; Author: Stephen Granade <[email protected]>
;; Created: 3 Apr 1999
;; Version: 1.2
;; Keywords: languages
;; LCD Archive Entry:
;; tads3-mode|Brett Witty|[email protected]|
;; Major mode for editing TADS 3/3 programs|
;; 4-Feb-2006|1.3|~/modes/tads3-mode.el.Z|
;;; Copyright:
;; Portions of this code are Copyright (c) by Stephen Granade 1999. Other
;; portions of this code are Copyright (c) by Brett Witty 2006. Other portions
;; are Copyright (c) by Alexis Purslane 2023. Portions of this code were adapted
;; from GNU Emacs C-mode, and are copyright (c) 1985, 1986, 1987, 1992 by Free
;; Software Foundation, Inc. Other portions of this code were adapted from an
;; earlier TADS mode by Darin Johnson, and are copyright (c) 1994 by Darin
;; Johnson.
;;
;; tads3-mode 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.
;;
;; tads3-mode 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.
;;; Commentary:
;; Version 1.4:
;;
;; This version has been further improved for TADS 3, including more syntax
;; highlighting for various constructions such as function calls, properties,
;; classes, and so on, as well as adding in many keywords that were forgotten
;; the first time around. Additionally, the plugin has been updated to be
;; compatible with Emacs versions past 24, and updated to use more modern Emacs
;; Lisp constructions that were introduced after that version as well. On top of
;; that, several bugfixes have been provided.
;;
;; Thanks to Brett Witty for making the plugin this is based off! I would have
;; never had the courage to start on my own.
;; Version 1.3:
;;
;; This version is a modification on the original TADS 3 mode. It can
;; be used for both TADS 3 and 3.
;; TADS 3 can be found at http://www.ifarchive.org/indexes/if-archiveXprogrammingXtads2.html
;; TADS 3 can be found at http://www.ifarchive.org/indexes/if-archiveXprogrammingXtads3.html
;;
;; Thanks to Stephen Granade for the original version, and all those
;; who assisted in its creation.
;; Version 1.2:
;;
;; TADS is an adventure programming language which can be found via
;; anonymous FTP at
;; /ftp.gmd.de:/if-archive/programming/tads/
;;
;; This major mode is based heavily on the standard EMACS C-mode.
;; Type `C-h m' within TADS mode for details.
;;
;;
;; Special thanks to Matthew Amster-Burton and Dan Shiovitz, who tested
;; early versions of this mode, and to Dan Schmidt, who got filled
;; strings and imenu support working.
;;
;;; Code:
(eval-when-compile
(require 'rx))
(require 'cl-lib)
(require 'xref)
;;; ======General customization variables======
(defconst tads3-mode-version "1.4")
(defgroup tads3
nil
"A customization group for TADS 3 Mode."
:group 'programming)
(defcustom tads3-startup-message t
"*Non-nil means display a message when TADS 3 mode is loaded."
:type 'boolean
:group 'tads3)
(defcustom tads3-no-c++-comments nil
"*If t, C++-style comments \(//\) are not fontified or treated as comments."
:type 'boolean
:group 'tads3)
(defcustom tads3-strip-trailing-whitespace t
"*If t (the default), delete any trailing whitespace when ENTER is pressed."
:type 'boolean
:group 'tads3)
(defcustom tads3-interpreter
(executable-find "qtads")
"The executable for tads3-mode to launch when you run your game.
QTADS is recommended for full freature support, but frobTADS and
Gargoyle will also work if you are writing a text only game."
:type 'string
:group 'tads3)
;;; ======Indentation parameters======
(defcustom tads3-indent-level 4
"*Indentation of lines of block relative to first line of block."
:type 'number
:group 'tads3)
(defcustom tads3-label-offset -2
"*Indentation of label relative to where it should be."
:type 'number
:group 'tads3)
(defcustom tads3-indent-continued-string t
"*If t (the default), strings continued from the previous line
are indented."
:type 'boolean
:group 'tads3)
(defcustom tads3-continued-string-offset 1
"*How much to indent continued strings by compared to the first line
of the string. This is only used if `tads3-indent-continued-string' is
true."
:type 'number
:group 'tads3)
(defcustom tads3-continued-string-offset-from-line 2
"*How much to indent continued strings by compared to the first line
of the command containing the string, if that command is not purely
the string itself. This is only used if `tads3-indent-continued-string'
is false."
:type 'number
:group 'tads3)
(defcustom tads3-brace-imaginary-offset 0
"*Imagined indentation of a TADS open brace that actually follows a statement."
:type 'number
:group 'tads3)
(defcustom tads3-brace-offset 0
"*Extra indentation of braces compared to other text in the same context."
:type 'number
:group 'tads3)
(defcustom tads3-continued-statement-offset 4
"*Extra indentation for lines which do not begin new statements."
:type 'number
:group 'tads3)
(defcustom tads3-continued-brace-offset -4
"*Extra indentation for substatements which begin with an open brace.
This is in addition to `tads3-continued-statement-offset'."
:type 'number
:group 'tads3)
(defcustom tads3-indent-cont-statement 4
"*Indentation of continuation relative to start of statement."
:type 'number
:group 'tads3)
(defcustom tads3-auto-indent-after-newline t
"*If t (the default), automatically indent the next line after
RETURN is pressed."
:type 'number
:group 'tads3)
(defcustom tads3-tab-always-indent t
"*If t (the default), always indent the current line when tab is pressed."
:type 'number
:group 'tads3)
;; I don't know how to fix the original version which also inserts newlines
;; before and after the braces when enabled. - AP
(defcustom tads3-auto-newline t
"*If t, automatically add after semicolons and before braces that
are on the same line as other code in TADS code."
:type 'number
:group 'tads3)
(defcustom tads3--locate-t3m-regexp "\\.t3m$"
"*Regexp for locating t3make files in parent directories."
:type 'number
:group 'tads3)
(defcustom tads3--locate-t3-regexp "\\.t3$"
"*Regexp for locating t3 game files in parent directories."
:type 'number
:group 'tads3)
;;; ======Syntax variables======
(defvar tads3-mode-syntax-table
(let ((tads3-mode-syntax-table (make-syntax-table)))
(modify-syntax-entry ?\\ "\\" tads3-mode-syntax-table)
(modify-syntax-entry ?/ ". 14" tads3-mode-syntax-table)
(modify-syntax-entry ?* ". 23" tads3-mode-syntax-table)
(modify-syntax-entry ?+ "." tads3-mode-syntax-table)
(modify-syntax-entry ?- "." tads3-mode-syntax-table)
(modify-syntax-entry ?= "." tads3-mode-syntax-table)
(modify-syntax-entry ?% "." tads3-mode-syntax-table)
(modify-syntax-entry ?< "." tads3-mode-syntax-table)
(modify-syntax-entry ?> "." tads3-mode-syntax-table)
(modify-syntax-entry ?& "." tads3-mode-syntax-table)
(modify-syntax-entry ?| "." tads3-mode-syntax-table)
(modify-syntax-entry ?\" "\"" tads3-mode-syntax-table)
(modify-syntax-entry ?\' "\'" tads3-mode-syntax-table)
;; any reason NOT to have _ as a word constituent? Makes things simpler.
(modify-syntax-entry ?_ "w" tads3-mode-syntax-table)
;; C++ style comments
(if tads3-no-c++-comments
nil
(modify-syntax-entry ?/ ". 124" tads3-mode-syntax-table)
(modify-syntax-entry ?* ". 23b" tads3-mode-syntax-table)
(modify-syntax-entry ?\n ">" tads3-mode-syntax-table))
tads3-mode-syntax-table)
"Syntax table used in TADS mode.")
(eval-and-compile
(defvar tads3-functions-regexp
"\\<\\(a\\(ddword\\|sk\\(do\\|file\\|io\\)\\)\\|c\\(a\\(ps\\|r\\)\\|dr\\|learscreen\\|vt\\(num\\|str\\)\\)\\|d\\(atatype\\|e\\(fined\\|lword\\)\\)\\|e\\(ndTurn\\|xecCommand\\)\\|f\\(close\\|i\\(nd\\|rst\\(obj\\|sc\\)\\)\\|open\\|seek\\(\\|of\\)\\|write\\)\\|get\\(arg\\|fuse\\|time\\|words\\)\\|i\\(n\\(cturn\\|put\\(\\|dialog\\|event\\|key\\|line\\)\\)\\|sclass\\)\\|l\\(ength\\|o\\(gging\\|wer\\)\\)\\|n\\(extobj\\|o\\(caps\\|tify\\)\\)\\|o\\(bjwords\\|utcapture\\)\\|p\\(arse\\(AskobjIndirect\\|NounList\\|r\\(DictLookup\\|Get\\(Me\\|Obj\\|TokTypes\\)\\|Re\\(placeCommand\\|solveObjects\\)\\|SetMe\\|Tokenize\\)\\)\\|ostAction\\|r\\(eCommand\\|optype\\)\\)\\|quit\\|r\\(and\\(\\|omize\\)\\|e\\(GetGroup\\|Search\\|m\\(daemon\\|fuse\\)\\|s\\(ourceExists\\|t\\(art\\|ore\\)\\)\\)\\|un\\(daemons\\|fuses\\)\\)\\|s\\(a\\(ve\\|y\\)\\|et\\(OutputFilter\\|daemon\\|fuse\\|it\\|score\\|version\\)\\|kipturn\\|ubstr\\|ystemInfo\\)\\|timeDelay\\|u\\(n\\(do\\|notify\\)\\|pper\\)\\|verbInfo\\|yorn\\)\\>"
"Regular expression matching a TADS function")
;; (MODIFIED BY AP)
;; Class is a keyword. That was somehow missed.
(defvar tads3-keywords-regexp
"\\<\\(a\\(bort\\|rgcount\\)\\|break\\|c\\(ontinue\\|lass\\|ase\\)\\|d\\(elete\\|o\\)\\|e\\(lse\\|num\\|xit\\(\\|obj\\)\\)\\|f\\(or\\|unction\\)\\|goto\\|i\\(f\\|nherited\\)\\|local\\|modify\\|n\\(ew\\|il\\)\\|pass\\|re\\(place\\|turn\\)\\|s\\(elf\\|witch\\)\\|t\\(rue\\|ry\\|emplate\\)\\|while\\|static\\|finally\\)\\>"
"Regular expression matching a TADS reserved word"))
;; A note: tads3-label-regexp and tads3-modified-regexp will NOT match
;; function definitions with returns between the label name and colon, like
;; bedroom_door
;; : doorway
;; I don't know of anyone who uses this syntax, but someone might. If you
;; do, remove the '\n' from tads3-label-regexp and tads3-modified-regexp.
;; Regexp for finding a label or class name followed by a colon
;; Note that this should *not* match "default:", nor should it match
;; ":=" (for those of you still using the Pascal-style assignment operator)
(defvar tads3-label-regexp "^[ \t]*\\(class \\)?\\([^:;\"!*(\n ]+ *\\):\\($\\|[^=]\\)")
;; Regexp for finding a modified object
(defvar tads3-modified-regexp "^[ \t]*\\(modify\\|replace\\)\\s-+\\([^:;\"!*(\n ]+\\)")
;; Regexp for some TADS special words
(defvar tads3-specials-regexp
"^[ \t]*\\(compoundWord\\b\\|formatstring\\b\\|specialWords\\b\\)")
;; (MODIFIED BY BW)
;; (Godawful) Regexp for TADS 3 objects, including anonymous
;; ones. This helps the next/previous object commands.
;; This regexp covers all sorts of objects like:
;;
;; (a class)
;; class Something : Class1, Class2
;;
;; (a standard named object)
;; something : Class1
;;
;; (an object using the brace method)
;; something : Class 1 {
;;
;; (an anonymous object)
;; Class1, Class2
;;
;; (any of the above with TADS 3 templates)
;; something : Class1, Class2 'vocab/vocab' 'name' "Description."
;;
;; (anonymous functions)
;; function(arg) { stuff; }
(defvar tads3-regexp
"^\\(class \\|\\++\\s-*\\)?\\([a-zA-Z]+\\s-*:\\s-*\\)?\\([a-zA-Z]+,?\\)+\\s-*.*;?$")
;; A combination of the above three regexps
(defvar tads3-defun-regexp
(concat
"\\("
; The below is replaced by the tads3-regexp which covers all that.
;tads3-label-regexp
;"\\|"
tads3-modified-regexp
"\\|"
tads3-specials-regexp
"\\|"
tads3-regexp
"\\)"))
;; Regexp used internally to recognize labels in switch statements.
(defconst tads3-switch-label-regexp "\\(case[ \t'/\(][^:]+\\|default[ \t]*\\):")
(defconst tads3-class-name-regexp
(rx symbol-start upper (zero-or-more alnum) (or upper lower digit)) symbol-end)
(defconst tads3-property-name-regexp
(rx (or (seq symbol-start (group-n 1 lower (zero-or-more (any "a-z" "A-Z" "_"))) symbol-end " = ")
(seq "." symbol-start (group-n 1 lower (zero-or-more (any "a-z" "A-Z" "_"))) symbol-end))))
(defconst tads3-number-regexp
(rx symbol-start
(or digit (seq "0x" digit))
(zero-or-more digit)
(optional (seq "." (one-or-more digit)))
(optional (seq "E" (or "+" "-") (one-or-more digit)))
symbol-end))
(defconst tads3-description-regexp (rx "\"" (zero-or-more (or "\\\"" (not (any "\"")))) (opt "\"")))
(defconst tads3-substitution-regexp "<<[^>]*\\(?:>>\\)?+")
(defconst tads3-string-regexp (rx "'" (zero-or-more (or "\\'" (not (any "'" "\n" "\r")))) "'"))
(defconst tads3-method-def-regexp "[\t ]*\\(\\_<[a-zA-Z0-9_]+\\_>\\)\\(?:(.*)\\)?\\(?:[\n\t ]*{\\)$")
(defconst tads3-function-call-regexp "\\(?:;\\)?[\t \r]*\\(\\_<[a-zA-Z0-9_]+\\_>\\)([^);]*)\s*;")
;;; ======Font-lock keywords======
(defvar tads3-font-lock-defaults
'(tads3-font-lock-keywords nil nil ((?_ . "w")) tads3-prev-object)
"Font Lock defaults for TADS mode.")
(defgroup tads3-faces nil
"Faces used in TADS 3 mode."
:group 'tads3
:group 'faces)
(defface tads3-description-face
'((t . (:inherit font-lock-string-face :weight bold :foreground "#5FAFFF")))
"Face for Inform 7 strings."
:group 'tads3-faces)
(defface tads3-substitution-face
'((t . (:inherit variable-pitch :slant italic :foreground "#3E9EFF")))
"Face for TADS 3 substitutions embedded in text."
:group 'tads3-faces)
(defface tads3-single-quote-substitution-face
'((t . (:inherit variable-pitch :slant italic :foreground "#FDF4E5")))
"face for TADS 3 substitutions embedded in text."
:group 'tads3-faces)
(defun tads3-match-inside (outer matcher facespec)
"Match inside the match OUTER with MATCHER, fontifying with FACESPEC."
(let ((preform `(progn
(goto-char (match-beginning 0))
(match-end 0))))
`(,outer . '(,matcher ,preform nil (0 ,facespec t)))))
(defvar tads3-font-lock-keywords
(eval-when-compile
`(
;; preprocessor directives as comments.
("^#[ \t]*[a-z]+" . 'font-lock-comment-face)
("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)"
1 'font-lock-string-face)
;; objects and non-TADS functions
("^\\(\\w+[ \t]+\\)*\\+*[ \t]*\\(\\w+\\) *: *\\w+"
2 'font-lock-function-name-face)
("^[ \t]*modify \\(\\w+\\)"
1 'font-lock-function-name-face)
;; TADS keywords.
(,(concat "\\(" tads3-keywords-regexp "\\)") 0 'font-lock-keyword-face)
;; TADS functions.
(,(concat "\\(" tads3-functions-regexp "\\)") . 'font-lock-builtin-face)
;; method def or function call
(,tads3-function-call-regexp 1 'font-lock-function-name-face)
(,tads3-method-def-regexp 1 'font-lock-function-name-face)
;; TADS class names.
(,tads3-class-name-regexp . 'font-lock-type-face)
;; TADS property set or get
(,tads3-property-name-regexp 1 'font-lock-variable-name-face)
;; Integers and bignums
(,tads3-number-regexp . 'font-lock-constant-face)
;; Single quoted strings
(,tads3-string-regexp 0 'font-lock-string-face t)
,(tads3-match-inside tads3-string-regexp tads3-substitution-regexp `'tads3-single-quote-substitution-face)
;; Double quoted strings
(,tads3-description-regexp 0 'tads3-description-face t)
,(tads3-match-inside tads3-description-regexp tads3-substitution-regexp `'tads3-substitution-face)))
"Expressions to fontify in TADS mode.")
;;; ======TADS mode======
(defvar tads3-mode-abbrev-table nil)
(defvar tads3-mode-map
(let ((tads3-mode-map (make-sparse-keymap)))
;; major mode commands
(define-key tads3-mode-map (kbd "M-n") 'tads3-next-object)
(define-key tads3-mode-map (kbd "M-p") 'tads3-prev-object)
(define-key tads3-mode-map (kbd "M-t") 'tads3-inside-block-comment)
(define-key tads3-mode-map (kbd "C-c C-c") 'tads3-build)
(define-key tads3-mode-map (kbd "C-c C-1") 'tads3-run)
;; NOTE: According to "Key Binding Conventions" in the Emacs
;; Lisp manual, these are reserved for the user:
;;(define-key tads3-mode-map (kbd "<f5>") 'tads3-build)
;;(define-key tads3-mode-map (kbd "<f4>") 'tads3-run)
;; Electric keys
(define-key tads3-mode-map "{" 'tads3-electric-brace)
(define-key tads3-mode-map ";" 'tads3-electric-semi)
(define-key tads3-mode-map "#" 'tads3-electric-sharp-sign)
(define-key tads3-mode-map "*" 'tads3-electric-splat)
(define-key tads3-mode-map "\t" 'tads3-indent-command)
(define-key tads3-mode-map "\r" 'tads3-electric-enter)
tads3-mode-map)
"Keymap used in TADS 3 mode.")
;;;###autoload
(define-derived-mode tads3-mode
prog-mode "TADS 3"
"Major mode for TADS 3 programs."
:syntax-table tads3-mode-syntax-table
:aggrev-table tads3-mode-abbrev-table
:group 'tads3
(setq-local paragraph-start (concat "^$\\|" page-delimiter))
(setq-local paragraph-separate paragraph-start)
(setq-local paragraph-ignore-fill-prefix t)
(setq-local indent-line-function 'tads3-indent-line)
(setq-local indent-region-function 'tads3-indent-region)
(setq-local fill-paragraph-function 'tads3-fill-paragraph)
(setq-local imenu-generic-expression
tads3-imenu-generic-expression-regexp)
(setq-local imenu-prev-index-position-function
'tads3-prev-object)
(setq-local require-final-newline t)
;; The block mode comments are default
(setq-local comment-start "/* ")
(setq-local comment-end " */")
(setq-local comment-column 40)
(setq-local comment-start-skip "/\\*+ *\\|// *")
(setq-local comment-indent-function 'tads3-comment-indent)
(setq-local parse-sexp-ignore-comments t)
(setq-local font-lock-defaults tads3-font-lock-defaults)
(add-hook 'tads3-mode-hook 'tads3--reload-identifiers)
(add-hook (make-local-variable 'after-save-hook) 'tads3--reload-identifiers)
(add-hook 'completion-at-point-functions #'tads3--completion-at-point nil t)
(add-hook 'xref-backend-functions #'tads3--xref-backend))
;; This is used by indent-for-comment
;; to decide how much to indent a comment in C code
;; based on its context.
(defun tads3-comment-indent ()
(if (looking-at "^\\(/\\*\\|//\\)")
0 ;Existing comment at bol stays there.
(let ((opoint (point)))
(save-excursion
(beginning-of-line)
(cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\|//\\)")
;; A comment following a solitary close-brace
;; should have only one space.
(search-forward "}")
(1+ (current-column)))
((or (looking-at "^#[ \t]*endif[ \t]*")
(looking-at "^#[ \t]*else[ \t]*"))
7) ;2 spaces after #endif
((progn
(goto-char opoint)
(skip-chars-backward " \t")
(and (= comment-column 0) (bolp)))
;; If comment-column is 0, and nothing but space
;; before the comment, align it at 0 rather than 1.
0)
(t
(max (1+ (current-column)) ;Else indent at comment column
comment-column))))))) ; except leave at least one space.
(defun tads3-indent-command (&optional whole-exp)
"Indent current line as TADS code, or in some cases insert a tab character."
(interactive "P")
(if whole-exp
;; If arg, always indent this line as TADS
;; and shift remaining lines of expression the same amount.
(let ((shift-amt (tads3-indent-line))
beg end)
(save-excursion
(if tads3-tab-always-indent
(beginning-of-line))
;; Find beginning of following line.
(save-excursion
(forward-line 1) (setq beg (point)))
;; Find first beginning-of-sexp for sexp extending past this line.
(while (< (point) beg)
(forward-sexp 1)
(setq end (point))
(skip-chars-forward " \t\n")))
(if (> end beg)
(indent-code-rigidly beg end shift-amt "#")))
;; else just indent the one line
(if (and (not tads3-tab-always-indent)
(save-excursion
(skip-chars-backward " \t")
(not (bolp))))
(insert-tab)
(tads3-indent-line))))
(defun tads3-indent-region (start end)
(save-restriction
(let ((endline (progn (goto-char (max end start))
(or (bolp) (end-of-line))
(point)))
linestart)
(narrow-to-region (point-min) endline)
(goto-char (min start end))
(forward-line 0)
(while (not (eobp))
(tads3-indent-line)
(forward-line 1)))))
(defun tads3-non-indented-string-indentation ()
"Return indentation for the current string."
(save-excursion
(let ((start (1+ (re-search-backward "[^\\]\""))))
(goto-char start)
(+ (current-indentation)
(if (progn (skip-chars-backward " \t")
(bolp))
0
tads3-continued-string-offset-from-line)))))
(defun tads3-indent-line ()
"Indent current line as TADS code. Return the amount the indentation changed by."
(let ((indent (tads3-calculate-indent nil))
beg shift-amt
(case-fold-search nil)
(pos (- (point-max) (point))))
(beginning-of-line)
(setq beg (point))
(cond ((eq indent nil)
;; string
(setq indent
(tads3-calculate-indent-within-string)))
((eq indent t)
;; comment
(setq indent (tads3-calculate-indent-within-comment)))
((looking-at "[ \t]*#")
;; directive
(setq indent 0))
((and (not (looking-at "[ \t]*;"))
(save-excursion
(tads3-backward-to-noncomment 1)
(backward-char)
(looking-at "\"")))
;; "description"
(setq indent tads3-indent-level))
(t
(if (listp indent)
(setq indent (car indent))
;; Check special cases (don't do this if indent was a list,
;; since that means we were at the top level, and these
;; cases are only for C-style code)
(skip-chars-forward " \t")
(cond ((or (looking-at tads3-switch-label-regexp)
(and (looking-at "[A-Za-z]")
(save-excursion
(forward-sexp 1)
(looking-at ":"))))
(setq indent (max 1 (+ indent tads3-label-offset))))
((and (looking-at "else\\b")
(not (looking-at "else\\s_")))
(setq indent (save-excursion
(tads3-backward-to-start-of-if)
(current-indentation))))
((and (looking-at "}[ \t]*else\\b")
(not (looking-at "}[ \t]*else\\s_")))
(setq indent (save-excursion
(forward-char)
(backward-sexp)
(tads3-backward-to-start-of-if)
(current-indentation))))
((and (looking-at "while\\b")
(not (looking-at "while\\s_"))
(save-excursion
(tads3-backward-to-start-of-do)))
;; This is a `while' that ends a do-while.
(setq indent (save-excursion
(tads3-backward-to-start-of-do)
(current-indentation))))
((= (following-char) ?})
(setq indent (- indent tads3-indent-level)))
((= (following-char) ?{)
(setq indent (+ indent tads3-brace-offset)))))))
(skip-chars-forward " \t")
(setq shift-amt (- indent (current-column)))
(if (zerop shift-amt)
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))
(delete-region beg (point))
(indent-to indent)
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))
shift-amt))
;; quite different from the C-mode version
(defun tads3-calculate-indent (&optional parse-start)
"Return appropriate indentation for current line as TADS code.
In usual case returns an integer: the column to indent to.
Returns nil if line starts inside a string, t if in a comment.
If indent is returned inside a list, this means we are at the top
level rather than being C-style code in a function body."
(save-excursion
(beginning-of-line)
(let ((indent-point (point))
(case-fold-search nil)
state
containing-sexp
next-char)
(if parse-start
(goto-char parse-start)
(tads3-beginning-of-defun)
(setq parse-start (point)))
(while (< (point) indent-point)
(setq parse-start (point))
(setq state (parse-partial-sexp (point) indent-point 0))
(setq containing-sexp (car (cdr state))))
;; Now we've got some info, figure out what's up
;; State is: (paren-depth inner-list-start last-sexp instring incomment
;; after-quote min-paren-depth)
(cond
((or (nth 3 state) (nth 4 state))
;; Comment or string
(nth 4 state))
((null containing-sexp)
;; We're at the top level.
(goto-char indent-point)
(skip-chars-forward " \t")
;; returning a list, to flag us as top-level
(setq next-char (following-char))
(list
(cond ((or (= next-char ?\;) ; end of object def
(tads3-looking-at-defun))
0)
((progn
(tads3-backward-to-noncomment parse-start)
(= (preceding-char) ?=)) ; continued property def
(+ (current-indentation)
(if (= next-char ?{)
0 ; starting a method
tads3-continued-statement-offset))) ; continued propy
;; check for start of function def (already checked
;; if we're a continued property def)
((= next-char ?{)
(current-indentation)) ; start of function body
((and (= (current-indentation) 0)
(memq (preceding-char) '(?\; ?})))
;; just after obj def or func def
0)
((save-excursion
(beginning-of-line)
(tads3-looking-at-defun)) ; first line after def'n
tads3-indent-level)
(t
;; Normal, non continued line (we hope)
;; so use indentation of prev line (watching out
;; for things that could span multiple lines)
(if (memq (preceding-char) '(?\} ?\" ?\'))
(progn
(backward-sexp 1)
(skip-chars-backward " \t\n")))
(current-indentation)))))
;; Not at top level - so we go back to doing C stuff
((/= (char-after containing-sexp) ?{)
;; line is expression, not statement (i.e., we're
;; inside parens or square brackets, not curlies),
;; indent to just after the surrounding open.
(goto-char (1+ containing-sexp))
(current-column))
(t
;; We're part of a statement. Continuation or new statement?
;; Find previous non-comment character.
(goto-char indent-point)
(tads3-backward-to-noncomment containing-sexp)
(if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?\{)))
;; This line is continuation of preceding line's statement;
;; indent tads3-continued-statement-offset more than the
;; previous line of the statement.
(progn
(tads3-backward-to-start-of-continued-exp containing-sexp)
(+ tads3-continued-statement-offset (current-column)
(if (save-excursion (goto-char indent-point)
(skip-chars-forward " \t")
(eq (following-char) ?{))
tads3-continued-brace-offset 0)))
;; This line starts a new statement.
;; Position following last unclosed open.
(goto-char containing-sexp)
;; Is line first statement after an open-brace?
(or
;; If no, find that first statement and indent like it.
(save-excursion
(forward-char 1)
(while (progn (skip-chars-forward " \t\n")
(looking-at
(concat
"#\\|/\\*\\|//"
"\\|case[ \t].*:"
"\\|[a-zA-Z0-9_$]*:")))
;; Skip over comments and labels following openbrace.
(cond ((= (following-char) ?\#)
(forward-line 1))
((looking-at "/\\*")
(forward-char 2)
(search-forward "*/" nil 'move))
((looking-at "//")
(forward-line 1))
(t
(search-forward ":"))))
;; The first following code counts
;; if it is before the line we want to indent.
(and (< (point) indent-point)
(current-column)))
;; If no previous statement,
;; indent it relative to line brace is on.
;; For open brace in column zero, don't let statement
;; start there too. If tads3-indent-offset is zero,
;; use tads3-brace-offset + tads3-continued-statement-offset
;; instead.
;; For open-braces not the first thing in a line,
;; add in tads3-brace-imaginary-offset.
(+ (if (and (bolp) (zerop tads3-indent-level))
(+ tads3-brace-offset tads3-continued-statement-offset)
tads3-indent-level)
;; Move back over whitespace before the openbrace.
;; If openbrace is not first nonwhite thing on the line,
;; add the tads3-brace-imaginary-offset.
(progn (skip-chars-backward " \t")
(if (bolp) 0 tads3-brace-imaginary-offset))
;; If the openbrace is preceded by a parenthesized exp,
;; move to the beginning of that;
;; possibly a different line
(progn
(if (eq (preceding-char) ?\))
(forward-sexp -1))
;; Get initial indentation of the line we are on.
(current-indentation))))))))))
(defun tads3-calculate-indent-within-comment (&optional after-star)
"Return the indentation amount for line inside a block comment.
Optional arg AFTER-STAR means, if lines in the comment have a leading star,
return the indentation of the text that would follow this star."
(let (end star-start two-star)
(save-excursion
(beginning-of-line)
(skip-chars-forward " \t")
(setq star-start (= (following-char) ?\*)
two-star (looking-at "\\*\\*"))
(skip-chars-backward " \t\n")
(setq end (point))
(beginning-of-line)
(skip-chars-forward " \t")
(if after-star
(and (looking-at "\\*")
(re-search-forward "\\*[ \t]*")))
(and (re-search-forward "/\\*[ \t]*" end t)
star-start
(not after-star)
(goto-char (1+ (match-beginning 0)))
(if two-star
(backward-char))
(sit-for 1))
(if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
(1+ (current-column))
(current-column)))))
(defun tads3-calculate-indent-within-string ()
"Return the indentation amount for line inside a string."
(if (not tads3-indent-continued-string)
(tads3-non-indented-string-indentation)
(save-excursion
(let ((beg-point (point))
parse-start)
(tads3-beginning-of-defun)
(setq parse-start (point))
(goto-char beg-point)
;; now keep searching backwards until start of string
;; (ugly)
(while (nth 3
(parse-partial-sexp parse-start (point) nil))
(re-search-backward "\\s\"" nil t))
(+ (current-column) tads3-continued-string-offset)))))
(defun tads3-backward-to-start-of-continued-exp (lim)
(if (memq (preceding-char) '(?\) ?\"))
(forward-sexp -1))
(beginning-of-line)
(if (<= (point) lim)
(goto-char (1+ lim)))
(skip-chars-forward " \t"))
(defun tads3-backward-to-start-of-if (&optional limit)
"Move to the start of the last \"unbalanced\" `if'."
(or limit (setq limit (save-excursion (beginning-of-defun) (point))))
(let ((if-level 1)
(case-fold-search nil))
(while (and (not (bobp)) (not (zerop if-level)))
(backward-sexp 1)
(cond ((and (looking-at "else\\b")
(not (looking-at "else\\s_")))
(setq if-level (1+ if-level)))
((and (looking-at "if\\b")
(not (looking-at "if\\s_")))
(setq if-level (1- if-level)))
((< (point) limit)
(setq if-level 0)
(goto-char limit))))))
(defun tads3-backward-to-start-of-do (&optional limit)
"If point follows a `do' statement, move to beginning of it and return t.
Otherwise return nil and don't move point."
(or limit (setq limit (save-excursion (beginning-of-defun) (point))))
(let ((first t)
(startpos (point))
(done nil))
(while (not done)
(let ((next-start (point)))
(condition-case nil
;; Move back one token or one brace or paren group.
(backward-sexp 1)
;; If we find an open-brace, we lose.
(error (setq done 'fail)))
(if done
nil
;; If we reached a `do', we win.
(if (looking-at "do\\b")
(setq done 'succeed)
;; Otherwise, if we skipped a semicolon, we lose.
;; (Exception: we can skip one semicolon before getting
;; to a the last token of the statement, unless that token
;; is a close brace.)
(if (save-excursion
(forward-sexp 1)
(or (and (not first) (= (preceding-char) ?}))
(search-forward ";" next-start t
(if (and first
(/= (preceding-char) ?}))
2 1))))
(setq done 'fail)
(setq first nil)
;; If we go too far back in the buffer, we lose.
(if (< (point) limit)
(setq done 'fail)))))))
(if (eq done 'succeed)
t
(goto-char startpos)
nil)))
(defun tads3-beginning-of-defun ()
"Move either to what we think is start of TADS function or object, or,
if not found, to the start of the buffer."
(interactive)
(beginning-of-line)
(while (not (or (tads3-looking-at-defun) (= (point) (point-min))))
(and (re-search-backward (concat "^" tads3-defun-regexp) nil 'move)
(goto-char (match-beginning 0)))))
(defun tads3-backward-to-noncomment (lim)
(let (opoint stop)
(while (not stop)
(skip-chars-backward " \t\n\r\f" lim)
(setq opoint (point))
(cond ((and (>= (point) (+ 2 lim))
(save-excursion
(forward-char -2)
(looking-at "\\*/")))
(search-backward "/*" lim 'move))
((search-backward "//" (max lim (save-excursion
(beginning-of-line)
(point)))
'move))
(t (beginning-of-line)
(skip-chars-forward " \t")
(if (looking-at "#")
(setq stop (<= (point) lim))
(setq stop t)
(goto-char opoint)))))))
;; tells if we're at top level (or inside braces)
(defun tads3-top-level ()
(save-excursion
(beginning-of-line)
(let ((opoint (point))
state)
(tads3-beginning-of-defun)
(while (< (point) opoint)
(setq state (parse-partial-sexp (point) opoint 0)))
(null (car (cdr state))))))
;; fill a comment or a string
(defun tads3-fill-paragraph (&optional arg)
"Like \\[fill-paragraph] but handle C comments.
If any of the current line is a comment or within a comment,
fill the comment or the paragraph of it that point is in,
preserving the comment indentation or line-starting decorations."
(interactive "P")
(let* (comment-start-place
(first-line
;; Check for obvious entry to comment.
(save-excursion
(beginning-of-line)
(skip-chars-forward " \t\n")
(and (looking-at comment-start-skip)
(setq comment-start-place (point))))))
(if (save-excursion
(beginning-of-line)
(looking-at ".*//")) ;; handle c++-style comments
(let (fill-prefix
(paragraph-start
;; Lines containing just a comment start or just an end
;; should not be filled into paragraphs they are next to.
(concat
paragraph-start
"\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
(paragraph-separate
(concat
paragraph-separate
"\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")))
(save-excursion
(beginning-of-line)
;; Move up to first line of this comment.
(while (and (not (bobp)) (looking-at "[ \t]*//"))
(forward-line -1))
(if (not (looking-at ".*//"))
(forward-line 1))
;; Find the comment start in this line.
(re-search-forward "[ \t]*//[ \t]*")
;; Set the fill-prefix to be what all lines except the first
;; should start with.
(let ((endcol (current-column)))
(skip-chars-backward " \t")
(setq fill-prefix
(concat (make-string (- (current-column) 2) ?\ )
"//"