This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tads3.el
1406 lines (1266 loc) · 50.5 KB
/
tads3.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 --- TADS 2/3 mode for GNU Emacs v19 or later
;;;;;;;;;;;
;; This version was modified by Brett Witty <[email protected]>
;; from Stephen Granade's tads2-mode.el. The only real changes was a
;; modified regexp to deal with object definitions so that
;; tads-next-object and tads-prev-object work nicer. It also helps
;; with indenting TADS 3-style object code.
;;
;; 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 tads-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: 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 2/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.
;; 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 for version 1.3:
;; This version is a modification on the original TADS 2 mode. It can
;; be used for both TADS 2 and 3.
;; TADS 2 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.
;;; Commentary for 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.
;;
;; Put this file somewhere on your load-path, and the following code in
;; your .emacs file:
;;
;; (autoload 'tads3-mode "tads3-mode" "TADS 2 editing mode." t)
;; (setq auto-mode-alist
;; (append (list (cons "\\.t$" 'tads2-mode))
;; auto-mode-alist))
;;; Code:
;;; General variables: --------------------------------------------------------
(defconst tads-mode-version "1.2")
(defvar tads-startup-message t
"*Non-nil means display a message when TADS 2 mode is loaded.")
(defvar tads-no-c++-comments nil
"*If t, C++-style comments \(//\) are not fontified or treated as comments.")
(defvar tads-strip-trailing-whitespace t
"*If t (the default), delete any trailing whitespace when ENTER is pressed.")
(defvar tads-mode-abbrev-table nil)
(defvar tads-mode-map nil
"Keymap used in TADS 2 mode.")
(if tads-mode-map nil
(let ((map (make-sparse-keymap "TADS")))
(setq tads-mode-map (make-sparse-keymap))
(define-key tads-mode-map "\M-n" 'tads-next-object)
(define-key tads-mode-map "\M-p" 'tads-prev-object)
(define-key tads-mode-map "{" 'electric-tads-brace)
(define-key tads-mode-map "}" 'electric-tads-brace)
(define-key tads-mode-map ";" 'electric-tads-semi)
(define-key tads-mode-map "#" 'electric-tads-sharp-sign)
(define-key tads-mode-map "*" 'electric-tads-splat)
(define-key tads-mode-map "\r" 'electric-tads-enter)
(define-key tads-mode-map "\M-t" 'tads-inside-block-comment)
;; REMOVED BY BW because of problemaatic behaviour. Uncomment it if you like.
;(define-key tads-mode-map "\177" 'backward-delete-char-untabify)
(define-key tads-mode-map "\t" 'tads-indent-command)
(define-key tads-mode-map [menu-bar] (make-sparse-keymap))
(define-key tads-mode-map [menu-bar tads] (cons "TADS" map))
(define-key map [next-object] '("Next object" . tads-next-object))
(define-key map [prev-object] '("Previous object" . tads-prev-object))
(define-key map [separator1] '("--" . nil))
(define-key map [comment-region] '("Comment Out Region" . comment-region))
(put 'comment-region 'menu-enable 'mark-active)
(define-key map [indent-region] '("Indent Region" . indent-region))
(put 'indent-region 'menu-enable 'mark-active)
(define-key map [indent-line] '("Indent Line" . indent-for-tab-command))))
;;; Indentation parameters: ---------------------------------------------------
(defvar tads-indent-level 4
"*Indentation of lines of block relative to first line of block.")
(defvar tads-label-offset -2
"*Indentation of label relative to where it should be.")
(defvar tads-indent-continued-string t
"*If t (the default), strings continued from the previous line
are indented.")
(defvar tads-continued-string-offset 1
"*How much to indent continued strings by compared to the first line
of the string. This is only used if `tads-indent-continued-string' is
true.")
(defvar tads-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 `tads-indent-continued-string'
is false.")
(defvar tads-brace-imaginary-offset 0
"*Imagined indentation of a TADS open brace that actually follows
a statement.")
(defvar tads-brace-offset 0
"*Extra indentation of braces compared to other text in the same context.")
(defvar tads-continued-statement-offset 4
"*Extra indentation for lines which do not begin new statements.")
(defvar tads-continued-brace-offset -4
"*Extra indentation for substatements which begin with an open brace.
This is in addition to `tads-continued-statement-offset'.")
(defvar tads-indent-cont-statement 4
"*Indentation of continuation relative to start of statement.")
(defvar tads-auto-indent-after-newline t
"*If t (the default), automatically indent the next line after
RETURN is pressed.")
(defvar tads-tab-always-indent t
"*If t (the default), always indent the current line when tab is pressed.")
(defvar tads-auto-newline nil
"*If t, automatically add newlines before and after braces,
and after semicolons in TADS code. If you don't want a leading
newline before braces then use:
(define-key tads-mode-map \"{\" 'electric-tads-semi)")
;;; Syntax variables: ---------------------------------------------------------
(defvar tads-mode-syntax-table nil
"Syntax table used in TADS mode.")
(if tads-mode-syntax-table
nil
(setq tads-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\\ "\\" tads-mode-syntax-table)
(modify-syntax-entry ?/ ". 14" tads-mode-syntax-table)
(modify-syntax-entry ?* ". 23" tads-mode-syntax-table)
(modify-syntax-entry ?+ "." tads-mode-syntax-table)
(modify-syntax-entry ?- "." tads-mode-syntax-table)
(modify-syntax-entry ?= "." tads-mode-syntax-table)
(modify-syntax-entry ?% "." tads-mode-syntax-table)
(modify-syntax-entry ?< "." tads-mode-syntax-table)
(modify-syntax-entry ?> "." tads-mode-syntax-table)
(modify-syntax-entry ?& "." tads-mode-syntax-table)
(modify-syntax-entry ?| "." tads-mode-syntax-table)
(modify-syntax-entry ?\' "\"" tads-mode-syntax-table)
;; any reason NOT to have _ as a word constituent? Makes things simpler.
(modify-syntax-entry ?_ "w" tads-mode-syntax-table)
;; C++ style comments
(if tads-no-c++-comments
()
(modify-syntax-entry ?/ ". 124" tads-mode-syntax-table)
(modify-syntax-entry ?* ". 23b" tads-mode-syntax-table)
(modify-syntax-entry ?\n ">" tads-mode-syntax-table)))
;(defvar tads-functions-list
; '("addword" "askdo" "askfile" "askio" "caps" "car" "cdr"
; "clearscreen" "cvtnum" "cvtstr" "datatype" "defined" "delword"
; "endTurn" "execCommand" "fclose" "find" "firstobj" "firstsc" "fopen"
; "fseek" "fseekof" "fwrite" "getarg" "getfuse" "gettime" "getwords"
; "incturn" "input" "inputdialog" "inputevent" "inputkey" "inputline"
; "isclass" "length" "logging" "lower" "nextobj" "nocaps" "notify"
; "objwords" "outcapture" "parseAskobjIndirect" "parseNounList"
; "parserDictLookup" "parserGetMe" "parserGetObj" "parserGetTokTypes"
; "parserReplaceCommand" "parserResolveObjects" "parserSetMe"
; "parserTokenize" "postAction" "preCommand" "proptype" "quit" "rand"
; "randomize" "reGetGroup" "remdaemon" "remfuse" "reSearch"
; "resourceExists" "restart" "restore" "rundaemons" "runfuses" "save"
; "say" "setdaemon" "setfuse" "setit" "setOutputFilter" "setscore"
; "setversion" "skipturn" "substr" "systemInfo" "timeDelay" "undo"
; "unnotify" "upper" "verbInfo" "yorn")
; "List of TADS built-in functions.")
;; A function to aid my own failing memory of how to print objects
;(defun tads-make-regexp ()
; (interactive)
; (insert (make-regexp tads-functions-list)))
;(defvar tads-keywords-list
; '("abort" "argcount" "break" "continue" "delete" "do" "else" "exit"
; "exitobj" "for" "goto" "if" "inherited" "local" "modify" "new" "nil"
; "pass" "replace" "return" "self" "switch" "true" "while")
; "List of TADS keywords.")
;; The following regexps were made from the above commented lists using
;; Simon Marshall's make-regexp package (thanks, Gareth!).
(eval-and-compile
(defvar tads-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")
(defvar tads-keywords-regexp
"\\<\\(a\\(bort\\|rgcount\\)\\|break\\|continue\\|d\\(elete\\|o\\)\\|e\\(lse\\|xit\\(\\|obj\\)\\)\\|for\\|goto\\|i\\(f\\|nherited\\)\\|local\\|modify\\|n\\(ew\\|il\\)\\|pass\\|re\\(place\\|turn\\)\\|s\\(elf\\|witch\\)\\|true\\|while\\)\\>"
"Regular expression matching a TADS reserved word"))
;; A note: tads-label-regexp and tads-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 tads-label-regexp and tads-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 tads-label-regexp "^[ \t]*\\(class \\)?\\([^:;\"!*(\n ]+ *\\):\\($\\|[^=]\\)")
;; Regexp for finding a modified object
(defvar tads-modified-regexp "^[ \t]*\\(modify\\|replace\\)\\s-+\\([^:;\"!*(\n ]+\\)")
;; Regexp for some TADS special words
(defvar tads-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 tads-defun-regexp
(concat
"\\("
; The below is replaced by the tads3-regexp which covers all that.
;tads-label-regexp
;"\\|"
tads-modified-regexp
"\\|"
tads-specials-regexp
"\\|"
tads3-regexp
"\\)"))
;; Regexp used internally to recognize labels in switch statements.
(defconst tads-switch-label-regexp "\\(case[ \t'/\(][^:]+\\|default[ \t]*\\):")
;;; Font-lock keywords: -------------------------------------------------------
(defvar tads-font-lock-defaults
'(tads-font-lock-keywords nil t ((?_ . "w")) tads-prev-object)
"Font Lock defaults for TADS mode.")
(defvar tads-font-lock-keywords
(eval-when-compile
(list
;; 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.
(cons (concat "\\(" tads-keywords-regexp "\\)")
'font-lock-keyword-face)
;; TADS functions.
(cons (concat "\\(" tads-functions-regexp "\\)")
'font-lock-builtin-face)
;; Single quoted strings
'("'[-a-zA-Z0-9.|/\\*<>\\? ]*'" . font-lock-string-face)
;'("'[^'\\"$]*'" . font-lock-string-face)
))
"Expressions to fontify in TADS mode.")
;;; TADS mode: ----------------------------------------------------------------
(defun tads-mode ()
"Major mode for editing TADS programs.
* TADS syntax:
Type \\[indent-for-tab-command] to indent the current line.
Type \\[indent-region] to indent the region.
* Navigating in a file:
Type \\[tads-prev-object] to go to the previous object/class declaration.
Type \\[tads-next-object] to go to the next one.
* Font-lock support:
Put \(add-hook 'tads-mode-hook 'turn-on-font-lock) in your .emacs file.
*Key definitions:
\\{tads-mode-map}
* Miscellaneous user options:
tads-startup-message
Set to nil to inhibit the message printed the first time TADS
mode is used.
tads-auto-newline
If true, automatically insert a newline before and after
braces, and after colons and semicolons.
tads-no-c++-comments
Set to true to not treat C++-style comments \(//\) as comments.
tads-strip-trailing-whitespace
If true (the default), all whitespace at the end of a line will
be removed when RETURN is pressed.
tads-mode-hook
The hook that is run after entering TADS mode.
* User options controlling indentation style:
Values in parentheses are the default indentation style.
tads-indent-level \(4\)
Indentation of code inside an object relative to the first
line of the block.
tads-brace-offset \(0\)
Extra indentation for a brace as compared to text in the same
context.
tads-brace-imaginary-offset \(0\)
Imagined indentation for an open brace that follows a statement.
tads-indent-cont-statement \(4\)
Indentation of continuation relative to start of statement.
tads-continued-statement-offset \(4\)
Extra indentation for lines which do not begin new statements
tads-continued-brace-offset \(-4\)
Extra indentation for substatements which start with an open brace.
This is in addition to `tads-continued-statement-offset'.
tads-label-offset \(-2\)
Extra indentation for line that is a label, or case or default.
tads-indent-continued-string \(t\)
If true, strings which span more than one line are all indented
the same amount.
tads-continued-string-offset \(1\)
How much to indent continued strings by compared to the first line
of the string. This is only used if `tads-indent-continued-string'
is true.
tads-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 `tads-indent-continued-string'
is false.
tads-auto-indent-after-newline \(t\)
If true, then pressing RETURN also indents the new line that is
created.
tads-tab-always-indents \(t\)
If true, TAB always indents the current line when pressed.
tads-auto-newline \(nil\)
If true, automatically add newlines before and after braces,
and after semicolons in TADS code. If you don't want a leading
newline before braces then use:
(define-key tads-mode-map \"{\" 'electric-tads-semi)"
(interactive)
(if tads-startup-message
(message "Emacs TADS mode version %s by Stephen Granade."
tads-mode-version))
(kill-all-local-variables)
(use-local-map tads-mode-map)
(setq major-mode 'tads-mode)
(setq mode-name "TADS")
(setq local-abbrev-table tads-mode-abbrev-table)
(set-syntax-table tads-mode-syntax-table)
(set (make-local-variable 'paragraph-start) (concat "^$\\|" page-delimiter))
(set (make-local-variable 'paragraph-separate) paragraph-start)
(set (make-local-variable 'paragraph-ignore-fill-prefix) t)
(set (make-local-variable 'indent-line-function) 'tads-indent-line)
(set (make-local-variable 'indent-region-function) 'tads-indent-region)
(set (make-local-variable 'fill-paragraph-function) 'tads-fill-paragraph)
(set (make-local-variable 'imenu-extract-index-name-function)
'tads-imenu-extract-name)
(set (make-local-variable 'imenu-prev-index-position-function)
'tads-prev-object)
(set (make-local-variable 'require-final-newline) t)
;; The block mode comments are default
(set (make-local-variable 'comment-start) "/* ")
(set (make-local-variable 'comment-end) " */")
(set (make-local-variable 'comment-column) 40)
(set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
(set (make-local-variable 'comment-indent-function) 'tads-comment-indent)
(set (make-local-variable 'parse-sexp-ignore-comments) t)
(set (make-local-variable 'font-lock-defaults) tads-font-lock-defaults)
(run-hooks 'tads-mode-hook))
;; This is used by indent-for-comment
;; to decide how much to indent a comment in C code
;; based on its context.
(defun tads-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 tads-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 (tads-indent-line))
beg end)
(save-excursion
(if tads-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 tads-tab-always-indent)
(save-excursion
(skip-chars-backward " \t")
(not (bolp))))
(insert-tab)
(tads-indent-line))))
(defun tads-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))
(tads-indent-line)
(forward-line 1)))))
(defun tads-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
tads-continued-string-offset-from-line)))))
(defun tads-indent-line ()
"Indent current line as TADS code.
Return the amount the indentation changed by."
(let ((indent (calculate-tads-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
(calculate-tads-indent-within-string)
))
((eq indent t)
;; comment
(setq indent (calculate-tads-indent-within-comment)))
((looking-at "[ \t]*#")
;; directive
(setq indent 0))
((and (not (looking-at "[ \t]*;"))
(save-excursion
(tads-backward-to-noncomment 1)
(backward-char)
(looking-at "\"")))
;; "description"
(setq indent tads-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 tads-switch-label-regexp)
(and (looking-at "[A-Za-z]")
(save-excursion
(forward-sexp 1)
(looking-at ":"))))
(setq indent (max 1 (+ indent tads-label-offset))))
((and (looking-at "else\\b")
(not (looking-at "else\\s_")))
(setq indent (save-excursion
(tads-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)
(tads-backward-to-start-of-if)
(current-indentation))))
((and (looking-at "while\\b")
(not (looking-at "while\\s_"))
(save-excursion
(tads-backward-to-start-of-do)))
;; This is a `while' that ends a do-while.
(setq indent (save-excursion
(tads-backward-to-start-of-do)
(current-indentation))))
((= (following-char) ?})
(setq indent (- indent tads-indent-level)))
((= (following-char) ?{)
(setq indent (+ indent tads-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 calculate-tads-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)
(tads-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))
(nth 4 state)) ; Comment or string
((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
(tads-looking-at-defun))
0)
((progn
(tads-backward-to-noncomment parse-start)
(= (preceding-char) ?=)) ; continued property def
(+ (current-indentation)
(if (= next-char ?{)
0 ; starting a method
tads-continued-statement-offset))) ; continued propy
;; check for start of function def (already checked
;; if we're a continued property def)
((= next-char ?{)
0) ; start of function body
((and (= (current-indentation) 0)
(memq (preceding-char) '(?\; ?})))
;; just after obj def or func def
0)
((save-excursion
(beginning-of-line)
(tads-looking-at-defun)) ; first line after def'n
tads-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)
(tads-backward-to-noncomment containing-sexp)
(if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?\{)))
;; This line is continuation of preceding line's statement;
;; indent tads-continued-statement-offset more than the
;; previous line of the statement.
(progn
(tads-backward-to-start-of-continued-exp containing-sexp)
(+ tads-continued-statement-offset (current-column)
(if (save-excursion (goto-char indent-point)
(skip-chars-forward " \t")
(eq (following-char) ?{))
tads-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 tads-indent-offset is zero,
;; use tads-brace-offset + tads-continued-statement-offset
;; instead.
;; For open-braces not the first thing in a line,
;; add in tads-brace-imaginary-offset.
(+ (if (and (bolp) (zerop tads-indent-level))
(+ tads-brace-offset tads-continued-statement-offset)
tads-indent-level)
;; Move back over whitespace before the openbrace.
;; If openbrace is not first nonwhite thing on the line,
;; add the tads-brace-imaginary-offset.
(progn (skip-chars-backward " \t")
(if (bolp) 0 tads-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 calculate-tads-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 calculate-tads-indent-within-string ()
"Return the indentation amount for line inside a string."
(if (not tads-indent-continued-string)
(tads-non-indented-string-indentation)
(save-excursion
(let ((beg-point (point))
parse-start)
(tads-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) tads-continued-string-offset)))))
(defun tads-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 tads-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 tads-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 tads-beginning-of-defun ()
(interactive)
"Move either to what we think is start of TADS function or object, or,
if not found, to the start of the buffer."
(beginning-of-line)
(while (not (or (tads-looking-at-defun) (= (point) (point-min))))
(and (re-search-backward (concat "^" tads-defun-regexp) nil 'move)
(goto-char (match-beginning 0)))))
(defun tads-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 tads-top-level ()
(save-excursion
(beginning-of-line)
(let ((opoint (point))
state)
(tads-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 tads-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) ?\ )
"//"
(make-string (- endcol (current-column)) ?\ ))))
(save-restriction
;; Narrow down to just the lines of this comment.
(narrow-to-region (point)
(save-excursion
(forward-line 1)
(while (looking-at "[ \t]*//")
(forward-line 1))
(point)))
(insert fill-prefix)
(fill-paragraph arg)