-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxl.el
1777 lines (1606 loc) · 64.1 KB
/
xl.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
;;; xl.el --- Basic mode for XL
;; Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
;; Author: Christophe de Dinechin ([email protected])
;; Based on: Python mode by Dave Love <[email protected]>
;; Maintainer: FSF
;; Created: July 2005
;; Keywords: languages
;; WARNING: This file works only on Emacs 22, it fails with 21.x
;; This file is part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Major mode for editing programs written in XL.
;; Successive TABs cycle between possible indentations for the line.
;; There is symbol completion using lookup in XL.
;;; Code:
;; It's messy to autoload the relevant comint functions so that comint
;; is only required when inferior XL is used.
(require 'comint)
(eval-when-compile
(require 'compile)
(autoload 'info-lookup-maybe-add-help "info-look"))
(autoload 'compilation-start "compile")
(defgroup xl nil
"Editing mode for XL programs"
:group 'languages
:version "22.1"
:link '(emacs-commentary-link "xl"))
;;;###autoload
(add-to-list 'interpreter-mode-alist '("xl" . xl-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.xl\\'" . xl-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.xs\\'" . xl-mode))
;;;; Font lock
(defvar xl-keywords
'("generic" "is" "has" "matches"
"import" "use"
"in" "out" "var" "const" "variable" "constant"
"require" "ensure"
"if" "then" "else"
"try" "catch" "retry" "raise"
"while" "until" "for" "do" "loop" "case"
"return" "exit" "restart"
"nil" "anything" "nothing"
"not" "and" "or" "xor"
"with" "without" "of" "to" "on" "into" "as" "any"
"when" "where" "like" "extends")
"List of words highlighted as 'keywords' in XL mode")
(defvar xl-warnings
'("raise" "error")
"List of words prefixing a 'warnings' in XL mode")
(defvar xl-types
'("boolean" "integer" "character" "real" "text"
"ref" "reference" "pointer"
"array" "string" "sequence" "vector" "matrix" "complex" )
"List of words highlighted as 'types' in XL mode")
(defvar xl-functors
'("function" "procedure" "to" "translation" "iterator")
"List of words declaring functions in XL mode")
(defvar xl-type-declarators
'("type" "class")
"List of words declaring types in XL mode")
(defvar xl-module-declarators
'("module" "record" "include" "interface")
"List of words declaring modules or records in XL mode")
(defvar xl-outdent-words
'("else" "into")
"List of words that will cause the next line to be unindented")
(defvar xl-quotes
'(("<<" ">>"))
"List of character sequences used as quotes in XL mode")
(defvar xl-comments
'(("//")
("/*" "*/"))
"List of character sequences used as comments in XL mode")
(defun xl-separators-regexp (sep-list)
"Return a regexp matching the separator list"
(cons 'or (mapcar '(lambda(x)
(if (cadr x)
(list 'and
(car x)
'(0+ (or printing "\f" "\n"))
(cadr x))
(list 'and
(car x)
'(0+ (or printing))
'(or "\n" "\f"))))
sep-list)))
;; Strictly speaking, XL has no keywords, but colorizing special words
;; that have standard uses in normal XL programs still makes sense.
(defvar xl-font-lock-keywords
;; Strings
`((,(rx (group (eval (xl-separators-regexp xl-quotes))))
(1 font-lock-string-face))
;; Comments
(,(rx (group (eval (xl-separators-regexp xl-comments))))
(1 font-lock-comment-face))
;; Pragmas
(,(rx (group (and "{" (0+ blank) (0+ word))))
(1 font-lock-preprocessor-face))
(,(rx (group "}"))
(1 font-lock-preprocessor-face))
;; Keywords
(,(rx (group (and word-start
(eval (cons 'or xl-keywords))
word-end)))
(1 font-lock-keyword-face))
;; Warnings (raise X)
(,(rx (and word-start
(group (eval (cons 'or xl-warnings)))
(1+ space) (group (1+ (or (1+ word)
(eval (xl-separators-regexp xl-quotes))
(1+ (char digit ".eE_")))))))
(1 font-lock-keyword-face) (2 font-lock-warning-face))
;; Constants
(,(rx (and word-start
(group (1+ (char digit "_"))
"#"
(1+ word)
(optional "." (0+ word))
(optional "#")
(optional (and (char "eE")
(optional (char "+-"))
(1+ (char digit "_")))))
word-end))
(1 font-lock-constant-face))
(,(rx (and word-start
(group (1+ (char digit "_"))
(optional "." (0+ (char digit "_")))
(optional (and (char "eE")
(optional (char "+-"))
(1+ (char digit "_")))))
word-end))
(1 font-lock-constant-face))
;; Predefined types
(,(rx (and word-start
(group (eval (cons 'or xl-types)))
word-end))
(1 font-lock-type-face))
;; Executable types (procedures)
(,(rx (and word-start
(group (eval (cons 'or xl-functors)))
(1+ space) (group (and (1+ word)
(0+ (and "." (1+ word)))))))
(1 font-lock-keyword-face) (2 font-lock-function-name-face))
;; Type declarations
(,(rx (and word-start
(group (eval (cons 'or xl-type-declarators)))
(1+ space) (group (and (1+ word)
(0+ (and "." (1+ word)))))))
(1 font-lock-keyword-face) (2 font-lock-type-face))
;; Module declarations (we use the preprocessor face for these)
(,(rx (and word-start
(group (eval (cons 'or xl-module-declarators)))
(1+ space) (group (and (1+ word)
(0+ (and "." (1+ word)))))))
(1 font-lock-keyword-face) (2 font-lock-preprocessor-face))
;; Import directives
(,(rx (and word-start
(group "import")
(1+ space) (group (1+ word))
(0+ space) "=" (0+ space)
(group (and (1+ word))
(0+ (and "." (1+ word))))))
(1 font-lock-keyword-face)
(2 font-lock-variable-name-face)
(3 font-lock-preprocessor-face))
(,(rx (and word-start
(group "import")
(1+ space)
(group (and (1+ word))
(0+ (and "." (1+ word))))))
(1 font-lock-keyword-face)
(2 font-lock-preprocessor-face))
;; Declaration
(,(rx (and word-start
(group (1+ word))
(0+ space) (group (or ":"
(and (1+ space)
(or "as" "like")
(1+ space))
)) (0+ space)
(group (and (1+ word))
(0+ (and "." (1+ word))))))
(1 font-lock-variable-name-face)
(3 font-lock-type-face))
;; Assignment
(,(rx (and word-start
(group (1+ word))
(0+ space) (or ":=" "+=" "-=" "*=" "/=") (0+ space)))
(1 font-lock-variable-name-face)) ))
;; Recognize XL text separators
(defconst xl-font-lock-syntactic-keywords
`(
(,(rx (and "[[" (0+ anything) "]]")))
(,(rx (and (group (syntax string-quote)) anything (backref 1)))) ))
;;;; Keymap and syntax
(defvar xl-mode-map
(let ((map (make-sparse-keymap)))
;; Mostly taken from xl-mode.el.
(define-key map "\177" 'xl-backspace)
(define-key map [(shift tab)] 'xl-unindent-for-tab)
(define-key map [(control tab)] 'xl-shift-right)
(define-key map [(control shift tab)] 'xl-shift-left)
(define-key map "\C-c<" 'xl-shift-left)
(define-key map "\C-c>" 'xl-shift-right)
(define-key map "\C-c4" 'xl-bwc1)
(define-key map "\C-c7" 'xl-bwc2)
(define-key map "\C-c6" 'xl-fwc1)
(define-key map "\C-c9" 'xl-fwc2)
(define-key map "\C-c1" 'xl-beginning-of-statement)
(define-key map "\C-c3" 'xl-end-of-statement)
(define-key map "\C-c\C-k" 'xl-mark-block)
(define-key map "\C-c\C-n" 'xl-next-statement)
(define-key map "\C-c\C-p" 'xl-previous-statement)
(define-key map "\C-c\C-u" 'xl-beginning-of-block)
(define-key map "\C-c\C-f" 'xl-describe-symbol)
(define-key map "\C-c\C-w" 'xl-check)
(define-key map "\C-c\C-v" 'xl-check) ; a la sgml-mode
(define-key map "\C-c\C-s" 'xl-send-string)
(define-key map [?\C-\M-x] 'xl-send-defun)
(define-key map "\C-c\C-r" 'xl-send-region)
(define-key map "\C-c\M-r" 'xl-send-region-and-go)
(define-key map "\C-c\C-c" 'xl-send-buffer)
(define-key map "\C-c\C-z" 'xl-switch-to-xl)
(define-key map "\C-c\C-m" 'xl-load-file)
(define-key map "\C-c\C-l" 'xl-load-file) ; a la cmuscheme
(substitute-key-definition 'complete-symbol 'xl-complete-symbol
map global-map)
;; Fixme: Add :help to menu.
(easy-menu-define xl-menu map "XL Mode menu"
'("XL"
["Shift region left" xl-shift-left :active mark-active]
["Shift region right" xl-shift-right :active mark-active]
"-"
["Mark block" xl-mark-block]
["Mark def/class" mark-defun
:help "Mark innermost definition around point"]
"-"
["Start of block" xl-beginning-of-block]
["End of block" xl-end-of-block]
["Start of definition" beginning-of-defun
:help "Go to start of innermost definition around point"]
["End of definition" end-of-defun
:help "Go to end of innermost definition around point"]
["Previous statement" xl-previous-statement]
["Next statement" xl-next-statement]
"-"
["Start interpreter" run-xl
:help "Run `inferior' XL in separate buffer"]
["Import/reload file" xl-load-file
:help "Load into inferior XL session"]
["Eval buffer" xl-send-buffer
:help "Evaluate buffer en bloc in inferior XL session"]
["Eval region" xl-send-region :active mark-active
:help "Evaluate region en bloc in inferior XL session"]
["Eval def/class" xl-send-defun
:help "Evaluate current definition in inferior XL session"]
["Switch to interpreter" xl-switch-to-xl
:help "Switch to inferior XL buffer"]
["Check file" xl-check :help "Run pychecker"]
["Debugger" pdb :help "Run pdb under GUD"]
"-"
["Help on symbol" xl-describe-symbol
:help "Use pydoc on symbol at point"]))
map))
(defvar xl-mode-syntax-table
(let ((table (make-syntax-table)))
;; Give punctuation syntax to ASCII that normally has symbol
;; syntax or has word syntax and isn't a letter.
(modify-syntax-entry ?% "." table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\' "\"" table)
(modify-syntax-entry ?: "." table)
(modify-syntax-entry ?\; "." table)
(modify-syntax-entry ?& "." table)
(modify-syntax-entry ?\| "." table)
(modify-syntax-entry ?+ "." table)
(modify-syntax-entry ?- "." table)
(modify-syntax-entry ?= "." table)
(modify-syntax-entry ?< "." table)
(modify-syntax-entry ?> "." table)
(modify-syntax-entry ?$ "." table)
(modify-syntax-entry ?\[ "." table)
(modify-syntax-entry ?\] "." table)
(modify-syntax-entry ?\{ "." table)
(modify-syntax-entry ?\} "." table)
(modify-syntax-entry ?. "." table)
(modify-syntax-entry ?\\ "." table)
;; a single slash is punctuation, but a double slash starts a comment
(modify-syntax-entry ?/ ". 124b" table)
(modify-syntax-entry ?* ". 23" table)
;; and \f and \n end a comment
(modify-syntax-entry ?\f "> b" table)
(modify-syntax-entry ?\n "> b" table)
;; Give CR the same syntax as newline, for selective-display
(modify-syntax-entry ?\^m "> b" table)
;; # is a paired delimiter in 16#FFFE#, but we treat it as symbol
(modify-syntax-entry ?# "." table)
;; define what belongs in Ada symbols
(modify-syntax-entry ?_ "_" table)
;; define parentheses to match
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
table))
;;;; Utility stuff
(defsubst xl-in-string/comment ()
"Return non-nil if point is in a XL literal (a comment or string)."
(syntax-ppss-context (syntax-ppss)))
(defun xl-skip-comments/blanks (&optional backward)
"Skip comments and blank lines.
BACKWARD non-nil means go backwards, otherwise go forwards.
Doesn't move out of comments -- should be outside or at end of line."
(forward-comment (if backward most-negative-fixnum most-positive-fixnum)))
(defun xl-skip-comments/blanks2 (&optional backward)
"Same as above"
(let ((regexp (rx (1+ (or (eval (xl-separators-regexp xl-comments))
(1+ (char blank ?\r ?\f)))))))
(if backward
(re-search-backward regexp (point-min) t)
(re-search-forward regexp (point-max) t))))
(defun xl-fwc1 () "" (interactive) (xl-skip-comments/blanks))
(defun xl-bwc1 () "" (interactive) (xl-skip-comments/blanks t))
(defun xl-fwc2 () "" (interactive) (xl-skip-comments/blanks2))
(defun xl-bwc2 () "" (interactive) (xl-skip-comments/blanks2 t))
(defun xl-continuation-line-p ()
"Return non-nil if current line continues a previous one.
The criteria are that the previous line ends in a punctuation (except semicolon)
or that the bracket/paren nesting depth is nonzero."
(or (and (save-excursion
(xl-skip-comments/blanks t)
(and (re-search-backward (rx graph) (point-min) t)
(looking-at (rx (syntax punctuation)))
(not (looking-at (rx (or ";" ")" "]" "}"))))))
(not (syntax-ppss-context (syntax-ppss))))
(/= 0 (syntax-ppss-depth
(save-excursion ; syntax-ppss with arg changes point
(syntax-ppss (line-beginning-position)))))))
(defun xl-comment-line-p (&optional arg)
"Return non-nil iff current line has only a comment.
With optional arg, return non-nil iff current line is empty or only a comment."
(save-excursion
(end-of-line)
(when (or arg (eq 'comment (syntax-ppss-context (syntax-ppss))))
(back-to-indentation)
(looking-at (rx (or (syntax comment-start)
(eval (xl-separators-regexp xl-comments))
line-end))))))
(defun xl-beginning-of-string ()
"Go to beginning of string around point.
Do nothing if not in string."
(let ((state (syntax-ppss)))
(when (eq 'string (syntax-ppss-context state))
(goto-char (nth 8 state)))))
(defun xl-open-block-statement-p ()
"Return non-nil if statement at point opens a block.
In XL, a statement opens a block if next line is more indented"
(let ((indent (current-indentation)))
(if (eobp) nil
(if (xl-outdent-p) t
(save-excursion
(beginning-of-line)
(forward-line)
(if (xl-comment-line-p) (xl-skip-comments/blanks))
(< indent (current-indentation)))))))
(defun xl-close-block-statement-p ()
"Return non-nil if current line is a statement closing a block.
The criteria is that the previous line has a lower indent"
(let ((indent (current-indentation)))
(if (bobp) nil
(save-excursion
(forward-line -1)
(while (and (not (bobp)) (xl-comment-line-p))
(forward-line -1))
(< indent (current-indentation))))))
(defun xl-outdent-p ()
"Return non-nil if current line should outdent a level."
(save-excursion
(back-to-indentation)
(looking-at (rx (eval (cons 'or xl-outdent-words))))))
;;;; Indentation.
(defcustom xl-indent 4
"*Number of columns for a unit of indentation in XL mode.
See also `\\[xl-guess-indent]'"
:group 'xl
:type 'integer)
(defcustom xl-guess-indent t
"*Non-nil means XL mode guesses `xl-indent' for the buffer."
:type 'boolean
:group 'xl)
(defcustom xl-indent-string-contents t
"*Non-nil means indent contents of multi-line strings together.
This means indent them the same as the preceding non-blank line.
Otherwise indent them to column zero."
:type '(choice (const :tag "Align with preceding" t)
(const :tag "Indent to column 0" nil))
:group 'xl)
(defcustom xl-honour-comment-indentation nil
"Non-nil means indent relative to preceding comment line.
Only do this for comments where the leading comment character is followed
by space. This doesn't apply to comment lines, which are always indented
in lines with preceding comments."
:type 'boolean
:group 'xl)
(defcustom xl-continuation-offset 4
"*Number of columns of additional indentation for continuation lines.
Continuation lines follow a line terminated by an operator other than semicolon"
:group 'xl
:type 'integer)
(defun xl-guess-indent ()
"Guess step for indentation of current buffer.
Set `xl-indent' locally to the value guessed."
(interactive)
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(let (done indent)
(while (and (not done) (not (eobp)))
(when (and (re-search-forward (rx (and ?: (0+ space)
(or (syntax comment-start)
line-end)))
nil 'move)
(xl-open-block-statement-p))
(save-excursion
(xl-beginning-of-statement)
(let ((initial (current-indentation)))
(if (zerop (xl-next-statement))
(setq indent (- (current-indentation) initial)))
(if (and (>= indent 2) (<= indent 8)) ; sanity check
(setq done t))))))
(when done
(when (/= indent (default-value 'xl-indent))
(set (make-local-variable 'xl-indent) indent)
(unless (= tab-width xl-indent)
(setq indent-tabs-mode nil)))
indent)))))
(defun xl-calculate-indentation ()
"Calculate XL indentation for line at point."
(save-excursion
(beginning-of-line)
(let ((syntax (syntax-ppss))
start)
(cond
((eq 'string (syntax-ppss-context syntax)) ; multi-line string
(if (not xl-indent-string-contents)
0
(save-excursion
;; Find indentation of preceding non-blank line within string.
(setq start (nth 8 syntax))
(forward-line -1)
(while (and (< start (point)) (looking-at "\\s-*$"))
(forward-line -1))
(current-indentation))))
((xl-continuation-line-p)
(let ((point (point))
(open-start (cadr syntax)))
(if open-start
;; Inside bracketed expression.
(progn
(goto-char (1+ open-start))
;; Look for first item in list (preceding point) and
;; align with it, if found.
(if (let ((parse-sexp-ignore-comments t))
(condition-case ()
(progn (forward-sexp)
(backward-sexp)
(< (point) point))
(error nil)))
(current-column)
;; Otherwise indent relative to statement start, one
;; level per bracketing level.
(goto-char (1+ open-start))
(xl-beginning-of-statement)
(+ (current-indentation) (* (car syntax) xl-indent))))
;; Otherwise backslash-continued.
(forward-line -1)
(if (xl-continuation-line-p)
;; We're past first continuation line. Align with
;; previous line.
(current-indentation)
;; First continuation line. Indent one step, with an
;; extra one if statement opens a block.
(save-excursion
(xl-beginning-of-statement)
(+ (current-indentation) xl-continuation-offset
(if (xl-open-block-statement-p)
xl-indent
0)))))))
((bobp)
0)
(t (let ((point (point)))
(if xl-honour-comment-indentation
;; Back over whitespace, newlines, non-indentable comments.
(catch 'done
(while t
(if (cond ((bobp))
;; not at comment start
((not (forward-comment -1))
(xl-beginning-of-statement)
t)
;; trailing comment
((/= (current-column) (current-indentation))
(xl-beginning-of-statement)
t)
;; indentable comment like xl-mode.el
((and (looking-at (rx (and (syntax comment-start)
(or space line-end))))
(/= 0 (current-column)))))
(throw 'done t))))
;; Else back over all comments.
(xl-skip-comments/blanks t)
(xl-beginning-of-statement))
;; don't lose on bogus outdent
(max 0 (+ (current-indentation)
(or (cond ((xl-open-block-statement-p)
xl-indent)
((xl-outdent-p)
xl-indent))
(progn (goto-char point)
(if (xl-outdent-p)
(- xl-indent)))
0)))))))))
;; (defun xl-comment-indent ()
;; "`comment-indent-function' for XL."
;; ;; If previous non-blank line was a comment, use its indentation.
;; ;; FIXME: This seems unnecessary since the default code delegates to
;; ;; indent-according-to-mode. --Stef
;; (unless (bobp)
;; (save-excursion
;; (forward-comment -1)
;; (if (eq ?# (char-after)) (current-column)))))
;;;; Cycling through the possible indentations with successive TABs.
;; These don't need to be buffer-local since they're only relevant
;; during a cycle.
;; Alist of possible indentations and start of statement they would close.
(defvar xl-indent-list nil
"Internal use.")
;; Length of the above
(defvar xl-indent-list-length nil
"Internal use.")
;; Current index into the alist.
(defvar xl-indent-index nil
"Internal use.")
(defun xl-initial-text ()
"Text of line following indentation and ignoring any trailing comment."
(buffer-substring (+ (line-beginning-position) (current-indentation))
(save-excursion
(end-of-line)
(forward-comment -1)
(point))))
(defun xl-indentation-levels ()
"Return a list of possible indentations for this line.
Includes the default indentation, one extra indent,
and those which would close all enclosing blocks.
Assumes the line has already been indented per
`xl-indent-line'. Elements of the list are actually pairs:
\(INDENTATION . TEXT), where TEXT is the initial text of the
corresponding block opening (or nil)."
(let ((levels (list (cons (current-indentation) nil)))
(unindents '()))
;; Only one possibility if we are in a continuation line.
(unless (xl-continuation-line-p)
(progn
(save-excursion
(while (xl-beginning-of-block)
(push (cons (current-indentation) (xl-initial-text)) unindents)))
(setq levels (append (reverse unindents) levels unindents levels))
(push (cons (+ (current-indentation) xl-indent) t)
levels)))
levels))
;; This is basically what `xl-indent-line' would be if we didn't
;; do the cycling.
(defun xl-indent-line-1 ()
"Subroutine of `xl-indent-line'."
(let ((target (xl-calculate-indentation))
(pos (- (point-max) (point))))
(if (= target (current-indentation))
(if (< (current-column) (current-indentation))
(back-to-indentation))
(beginning-of-line)
(delete-horizontal-space)
(indent-to target)
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))))
(defun xl-unindent-for-tab ()
"Indent for tab with a -1 argument"
(interactive)
(indent-for-tab-command))
(defun xl-indent-line ()
"Indent current line as XL code.
When invoked via `indent-for-tab-command', cycle through possible
indentations for current line. The cycle is broken by a command different
from `indent-for-tab-command', i.e. successive TABs do the cycling."
(interactive)
;; Don't do extra work if invoked via `indent-region', for instance.
(if (not (or (eq this-command 'indent-for-tab-command)
(eq this-command 'xl-unindent-for-tab)))
(xl-indent-line-1)
(if (or (eq last-command 'indent-for-tab-command)
(eq last-command 'xl-unindent-for-tab))
(if (= 1 xl-indent-list-length)
(message "Sole indentation")
(progn (setq xl-indent-index
(mod
(if (eq this-command 'xl-unindent-for-tab)
(1- xl-indent-index)
(1+ xl-indent-index))
xl-indent-list-length))
(beginning-of-line)
(delete-horizontal-space)
(indent-to (car (nth xl-indent-index xl-indent-list)))
(let ((text (cdr (nth xl-indent-index
xl-indent-list))))
(if text
(cond
((eq text t) (message "Indenting"))
((stringp text) (message "Closes: %s" text)))
(message "Current indentation")))))
(xl-indent-line-1)
(setq xl-indent-list (xl-indentation-levels)
xl-indent-list-length (length xl-indent-list)
xl-indent-index (1- xl-indent-list-length)))))
;; Fixme: Define an indent-region-function. It should probably leave
;; lines alone if the indentation is already at one of the allowed
;; levels. Otherwise, M-C-\ typically keeps indenting more deeply
;; down a function.
;;;; Movement.
(defun xl-beginning-of-defun ()
"`beginning-of-defun-function' for XL.
Finds beginning of innermost nested class or method definition.
Returns the name of the definition found at the end, or nil if reached
start of buffer."
(let ((ci (current-indentation))
(def-re (rx (and line-start (0+ space) (eval (cons 'or xl-functors))
(1+ space)
(group (1+ (or word (syntax symbol)))))))
found lep def-line)
(if (xl-comment-line-p)
(setq ci most-positive-fixnum))
(while (and (not (bobp)) (not found))
;; Treat bol at beginning of function as outside function so
;; that successive C-M-a makes progress backwards.
(setq def-line (looking-at def-re))
(unless (bolp) (end-of-line))
(setq lep (line-end-position))
(if (and (re-search-backward def-re nil 'move)
;; Must be less indented or matching top level, or
;; equally indented if we started on a definition line.
(let ((in (current-indentation)))
(or (and (zerop ci) (zerop in))
(= lep (line-end-position)) ; on initial line
(and def-line (= in ci))
(< in ci)))
(not (xl-in-string/comment)))
(setq found t)))
(back-to-indentation)))
(defun xl-end-of-defun ()
"`end-of-defun-function' for XL.
Finds end of innermost nested class or method definition."
(xl-beginning-of-defun)
(xl-end-of-block))
(defun xl-beginning-of-statement ()
"Go to start of current statement.
Accounts for continuation lines and multi-line strings."
(interactive)
(beginning-of-line)
(xl-beginning-of-string)
(while (xl-continuation-line-p)
(beginning-of-line)
(forward-line -1)))
(defun xl-end-of-statement ()
"Go to the end of the current statement and return point.
Usually this is the beginning of the next line, but if there is a continuation,
we need to skip additional lines."
(interactive)
(end-of-line)
(xl-skip-comments/blanks)
(while (xl-continuation-line-p)
(end-of-line)
(xl-skip-comments/blanks))
(point))
(defun xl-previous-statement (&optional count)
"Go to start of previous statement.
With argument COUNT, do it COUNT times. Stop at beginning of buffer.
Return count of statements left to move."
(interactive "p")
(unless count (setq count 1))
(if (< count 0)
(xl-next-statement (- count))
(xl-beginning-of-statement)
(while (and (> count 0) (not (bobp)))
(xl-skip-comments/blanks t)
(xl-beginning-of-statement)
(unless (bobp) (setq count (1- count))))
(back-to-indentation)
count))
(defun xl-next-statement (&optional count)
"Go to start of next statement.
With argument COUNT, do it COUNT times. Stop at end of buffer.
Return count of statements left to move."
(interactive "p")
(unless count (setq count 1))
(if (< count 0)
(xl-previous-statement (- count))
(beginning-of-line)
(while (and (> count 0) (not (eobp)))
(xl-end-of-statement)
(xl-skip-comments/blanks)
(setq count (1- count)))
count))
(defun xl-beginning-of-block (&optional arg)
"Go to start of current block.
With numeric arg, do it that many times. If ARG is negative, call
`xl-end-of-block' instead.
If point is on the first line of a block, use its outer block.
If current statement is in column zero, don't move and return nil.
Otherwise return non-nil."
(interactive "p")
(unless arg (setq arg 1))
(cond
((zerop arg))
((< arg 0) (xl-end-of-block (- arg)))
(t
(let ((point (point)))
(if (xl-comment-line-p)
(xl-skip-comments/blanks t))
(xl-beginning-of-statement)
(let ((ci (current-indentation)))
(if (zerop ci)
(not (goto-char point)) ; return nil
;; Look upwards for less indented statement.
(if (catch 'done
(while (zerop (forward-line -1))
(when (and (< (current-indentation) ci)
(not (xl-comment-line-p t))
;; Move to beginning to save effort in case
;; this is in string.
(progn (xl-beginning-of-statement) t)
(xl-open-block-statement-p))
(beginning-of-line)
(throw 'done t)))
(not (goto-char point))) ; Failed -- return nil
(xl-beginning-of-block (1- arg)))))))))
(defun xl-end-of-block (&optional arg)
"Go to end of current block.
With numeric arg, do it that many times. If ARG is negative, call
`xl-beginning-of-block' instead.
If current statement is in column zero and doesn't open a block, don't
move and return nil. Otherwise return t."
(interactive "p")
(unless arg (setq arg 1))
(if (< arg 0)
(xl-beginning-of-block (- arg)))
(while (and (> arg 0)
(let* ((point (point))
(_ (if (xl-comment-line-p)
(xl-skip-comments/blanks t)))
(ci (current-indentation))
(open (xl-open-block-statement-p)))
(if (and (zerop ci) (not open))
(not (goto-char point))
(catch 'done
(while (zerop (xl-next-statement))
(when (or (and open (<= (current-indentation) ci))
(< (current-indentation) ci))
(xl-skip-comments/blanks t)
(beginning-of-line 2)
(throw 'done t)))
(not (goto-char point))))))
(setq arg (1- arg)))
(zerop arg))
;;;; Imenu.
(defvar xl-recursing)
(defun xl-imenu-create-index ()
"`imenu-create-index-function' for XL.
Makes nested Imenu menus from nested `class' and `def' statements.
The nested menus are headed by an item referencing the outer
definition; it has a space prepended to the name so that it sorts
first with `imenu--sort-by-name' (though, unfortunately, sub-menus
precede it)."
(unless (boundp 'xl-recursing) ; dynamically bound below
(goto-char (point-min))) ; normal call from Imenu
(let (index-alist ; accumulated value to return
name)
(while (re-search-forward
(rx (and line-start (0+ space) ; leading space
(or (group "def") (group "class")) ; type
(1+ space) (group (1+ (or word ?_))))) ; name
nil t)
(unless (xl-in-string/comment)
(let ((pos (match-beginning 0))
(name (match-string-no-properties 3)))
(if (match-beginning 2) ; def or class?
(setq name (concat "class " name)))
(save-restriction
(narrow-to-defun)
(let* ((xl-recursing t)
(sublist (xl-imenu-create-index)))
(if sublist
(progn (push (cons (concat " " name) pos) sublist)
(push (cons name sublist) index-alist))
(push (cons name pos) index-alist)))))))
(nreverse index-alist)))
;;;; `Electric' commands.
(defun xl-backspace (arg)
"Maybe delete a level of indentation on the current line.
If not at the end of line's indentation, or on a comment line, just call
`backward-delete-char-untabify'. With ARG, repeat that many times."
(interactive "*p")
(if (or (/= (current-indentation) (current-column))
(bolp)
(xl-continuation-line-p))
(backward-delete-char-untabify arg)
(let ((indent 0))
(save-excursion
(while (and (> arg 0) (xl-beginning-of-block))
(setq arg (1- arg)))
(when (zerop arg)
(setq indent (current-indentation))
(message "Closes %s" (xl-initial-text))))
(delete-horizontal-space)
(indent-to indent))))
(put 'xl-backspace 'delete-selection 'supersede)
;;;; pychecker
(defcustom xl-check-command "pychecker --stdlib"
"*Command used to check a XL file."
:type 'string
:group 'xl)
(defvar xl-saved-check-command nil
"Internal use.")
;; After `sgml-validate-command'.
(defun xl-check (command)
"Check a XL file (default current buffer's file).
Runs COMMAND, a shell command, as if by `compile'.
See `xl-check-command' for the default."
(interactive
(list (read-string "Checker command: "
(or xl-saved-check-command
(concat xl-check-command " "
(let ((name (buffer-file-name)))
(if name
(file-name-nondirectory name))))))))
(setq xl-saved-check-command command)
(save-some-buffers (not compilation-ask-about-save) nil)
(let ((compilation-error-regexp-alist
(cons '("(\\([^,]+\\), line \\([0-9]+\\))" 1 2)
compilation-error-regexp-alist)))
(compilation-start command)))
;;;; Inferior mode stuff (following cmuscheme).
;; Fixme: Make sure we can work with IXL.
(defcustom xl-xl-command "xl"
"*Shell command to run XL interpreter.
Any arguments can't contain whitespace.
Note that IXL may not work properly; it must at least be used with the
`-cl' flag, i.e. use `ixl -cl'."
:group 'xl
:type 'string)
(defcustom xl-jython-command "jython"
"*Shell command to run Jython interpreter.
Any arguments can't contain whitespace."
:group 'xl
:type 'string)
(defvar xl-command xl-xl-command
"Actual command used to run XL.
May be `xl-xl-command' or `xl-jython-command'.
Additional arguments are added when the command is used by `run-xl'
et al.")
(defvar xl-buffer nil
"The current xl process buffer."
;; Fixme: a single process is currently assumed, so that this doc
;; is misleading.
;; "*The current xl process buffer.
;; To run multiple XL processes, start the first with \\[run-xl].
;; It will be in a buffer named *XL*. Rename that with
;; \\[rename-buffer]. Now start a new process with \\[run-xl]. It
;; will be in a new buffer, named *XL*. Switch between the different
;; process buffers with \\[switch-to-buffer].
;; Commands that send text from source buffers to XL processes have
;; to choose a process to send to. This is determined by global variable
;; `xl-buffer'. Suppose you have three inferior XLs running:
;; Buffer Process
;; foo xl