forked from harsman/dyalog-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyalog-mode.el
3208 lines (2960 loc) · 134 KB
/
dyalog-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
;;; dyalog-mode.el --- Major mode for editing Dyalog APL source code -*- coding: utf-8 lexical-binding: t -*-
;; Copyright (C) 2008, 2009, 2010, 2011 Joakim Hårsman
;; Author: Joakim Hårsman <[email protected]>
;; Version: 0.7
;; Package-Requires: ((cl-lib "0.2")(emacs "24.3"))
;; Keywords: languages
;; URL: https://github.com/harsman/dyalog-mode.git
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Dyalog-mode is a major mode for editing Dyalog APL source code.
;;
;; It supports syntax highlighting, indentation and convenience function like
;; toggling localization of variables. It can communicate with Dyalog
;; processes over a socket, allowing Emacs to be used as the editor for a
;; Dyalog session.
;;
;; Get the latest version at https://github.com/harsman/dyalog-mode
;;; Code:
(require 'cl-lib)
(require 'json)
(require 'comint)
;; Set up mode specific keys below
(defvar dyalog-mode-map
(let ((map(make-keymap)))
(define-key map (kbd"M-RET") 'comment-indent-new-line)
(define-key map (kbd"C-c C-c") 'dyalog-editor-fix)
(define-key map (kbd"C-c C-q") 'dyalog-editor-fix-and-quit)
(define-key map (kbd"C-c C-e") 'dyalog-editor-edit-symbol-at-point)
(define-key map (kbd"C-c C-l") 'dyalog-toggle-local)
(define-key map (kbd"C-C C-h") 'dyalog-help-for-symbol-at-point)
map)
"Keymap for Dyalog APL mode.")
(defvar dyalog-array-mode-map
(let ((map(make-sparse-keymap)))
;;(define-key map (kbd"C-c C-c") 'dyalog-array-fix)
(define-key map (kbd"C-c C-e") 'dyalog-editor-edit-symbol-at-point)
map)
"Keymap for Dyalog Array edit mode.")
;;;###autoload
(defun dyalog-fix-altgr-chars (keymap aplchars regularchars)
"Fix a key map so AltGr+char isn't confused with C-M-char.
KEYMAP is an Emacs keymap.
APLCHARS is a string of APL-characters produced by pressing AltGr together
with some character.
REGULARCHARS is a string of the characters that when pressed
together with AltGr produce the corresponding apl character in APLCHARS."
(dolist (pair (cl-mapcar #'cons aplchars regularchars))
(let* ((aplchar (car pair))
(char (cdr pair))
(aplkey (vector (list 'control 'meta aplchar)))
(regkey (vector (list 'control 'meta char)))
(fun (lookup-key (current-global-map) regkey)))
(when fun
(define-key keymap aplkey fun)))))
(defconst dyalog-label-regex
"^\\s-*\\([A-Za-z_][A-Za-z0-9_]*:\\)")
(defconst dyalog-keyword-regex
(concat "\\(\\(?:^\\s-*\\|\\(?5:" dyalog-label-regex " *\\)\\)"
"\\(?2::[A-Za-z]+\\)\\)\\|\\(⋄\\s-*\\(?2::[A-Za-z]+\\)\\)"))
(defconst dyalog-middle-keyword-regex
"\\(?: \\|\\_>\\)\\(:\\(In\\|InEach\\)\\)\\_>")
(defconst dyalog-comment-regex
"^\\s-*⍝")
(defvar dyalog-ascii-chars "][<>+---=/¨~\\?*(){}&|.;@!"
"APL symbols also present in ASCII.")
(defvar dyalog-keyword-chars
"×≤≥≠∨∧÷∊⍴↑↓⍳○←→⌈⌊∘⍎⍕⊂⊃⊆⊇∩∪⊥⊤⍨⍒⍋⌽⍉⊖⍟⍱⍲⍬⌹≡≢⍪⌿⍀⍺⍵⎕⍞⋄⍷⍸⌷⍣⊣⊢⌶⌺⍥⍠")
(defconst dyalog-name "[A-Za-z∆_][A-Za-z∆_0-9]*")
(defconst dyalog-real-number-regex
"¯?\\([0-9]+\\.?[0-9]*\\|\\.[0-9]+\\)\\([Ee]¯?[0-9]+\\.?[0-9]*\\)?")
(defvar dyalog-number
(concat "[^A-Za-z_∆0-9]\\(" dyalog-real-number-regex
"\\([Jj]" dyalog-real-number-regex "\\)?\\)"))
(defconst dyalog-access-type
"^\\s-*:Access +\\(WebMethod\\|\\(?:\\(Public\\|Private\\)\\)?\\(?: +\\(Instance\\( +Override\\|Overridable\\)\\|Shared\\)\\)?\\)")
(defconst dyalog-field-def
(concat "^\\s-*:Field"
"\\(?: +\\(Public\\|Private\\)\\)?"
"\\(?: +\\(Instance\\|Shared\\)\\)?"
"\\(?: +\\(ReadOnly\\)\\)?"
" +" "\\(" dyalog-name "\\)"))
(defconst dyalog-naked-nabla "^\\s-*∇\\s-*$")
(defconst dyalog-func-start "\\(?:\\`\\|∇[\r\n]*\\)\\s-*")
(defun dyalog-name-list (id)
"Return a regex with group ID matching a dyalog name list.
Name lists are (optionally) used for naming the elements of the
return value or right argument of a traditional defined function."
(concat "( *\\(?" id ":" dyalog-name "\\(?: +" dyalog-name "\\)+\\)"
"*)"))
(defconst dyalog-func-retval
(concat "\\(?:"
"\\(?:" "\\(?2:" dyalog-name "\\)" "\\|"
"\\(?:" (dyalog-name-list "2") "\\)" "\\|"
"\\(?:" "{\\(?2:" dyalog-name "\\)}\\)" "\\|"
"\\(?:" "{ *" (dyalog-name-list "2") " *}\\)"
"\\) *← *\\)?"))
(defconst dyalog-func-larg
(concat
"\\(?:"
"\\(?3:" dyalog-name "\\)\\(?:\\_>\\| +\\)" "\\|"
"{\\(?3:" dyalog-name "+\\)}"
"*\\)"))
(defconst dyalog-func-name (concat "\\(?1:" dyalog-name "\\)"))
(defconst dyalog-op-def (concat "\\(?:" "( *"
"\\(?6:" dyalog-name "\\)" ; left operand
" +"
"\\(?7:" dyalog-name "\\)" ; operator name
"\\(?:" " +"
"\\(?8:" dyalog-name "\\)" ; right operand
"\\)?" " *)" "\\)"))
(defconst dyalog-func-def (concat "\\(?:" dyalog-func-name "\\|"
dyalog-op-def "\\)"))
(defvar dyalog-func-rarg (concat "\\(?:\\(?:\\(?: +\\|\\_<\\)\\(?4:"
dyalog-name "\\)\\)\\|"
"\\(?: *" (dyalog-name-list "4") "\\)\\)"))
(defconst dyalog-func-header-end "\\s-*\\(?5:;\\|$\\)")
(defconst dyalog-tradfn-header (concat dyalog-func-start dyalog-func-retval
"\\(?:"
"\\(?:" dyalog-func-larg dyalog-func-def
dyalog-func-rarg "\\)" "\\|"
"\\(?:" dyalog-func-def dyalog-func-rarg "?"
"\\)" "\\)"
dyalog-func-header-end))
(defface dyalog-apl-char
'((t (:inherit font-lock-keyword-face)))
"Face used for APL characters and punctuation."
:group 'dyalog)
(defface dyalog-local-name
'((t (:inherit font-lock-constant-face)))
"Face used for localized names inside APL functions."
:group 'dyalog)
(defface dyalog-local-system-name
'((t (:inherit font-lock-variable-name-face)))
"Face used for localized system variables inside APL functions."
:group 'dyalog)
(defface dyalog-label-definition-face
'((t (:inherit font-lock-keyword-face)))
"Face used for label definitions inside APL functions"
:group 'dyalog)
(defvar dyalog-font-lock-keywords
(list
;; See emacs help for `font-lock-keywords' for a description of how the
;; below values work
;; System functions
'("⎕[A-Za-z]*" . font-lock-builtin-face)
;; Keywords
`(,dyalog-keyword-regex
. (2 font-lock-keyword-face nil))
`(,dyalog-middle-keyword-regex . (2 font-lock-keyword-face nil))
;; Labels
`(,dyalog-label-regex . (1 'dyalog-label-definition-face t))
;; Numeric constans
`(,dyalog-number (1 font-lock-constant-face nil))
;; APL chars
(cons (concat "[" dyalog-ascii-chars dyalog-keyword-chars ":" "]")
''dyalog-apl-char)
;; Localizations
'(";\\([A-Za-z0-9_∆]+\\)" (1 font-lock-constant-face nil))
;; Illegal chars (and del/nabla)
'("[∇$\"%]+" . font-lock-warning-face)
;; Local names. Note that the face specified here doesn't matter since
;; dyalog-fontify-locals-matcher always returns nil and sets the face on
;; its own.
`(dyalog-fontify-locals-matcher (1 font-lock-keyword-face nil))
`(,dyalog-access-type (1 font-lock-keyword-face))
`(,dyalog-field-def (1 font-lock-keyword-face t t)
(4 font-lock-variable-name-face)
(2 font-lock-keyword-face t t)
(3 font-lock-keyword-face t t)))
"Default highlighting mode for Dyalog mode.")
(defvar dyalog-mode-syntax-table
(let ((st (make-syntax-table)))
;; Make various APL chars punctuation
(dolist (char
(string-to-list (concat dyalog-keyword-chars dyalog-ascii-chars)))
(modify-syntax-entry char "." st))
;; Make sure delta, quad and underscore are part of symbol names
;; This doesn't seem to work for delta and quad?
(modify-syntax-entry ?_ "_" st)
(modify-syntax-entry ?∆ "_" st)
(modify-syntax-entry ?⎕ "_" st)
;; Comments
(modify-syntax-entry ?⍝ "<" st)
(modify-syntax-entry ?\n">" st)
;; Strings
(modify-syntax-entry ?' "\"" st)
(modify-syntax-entry ?\" "." st)
;; Delimiters
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
(modify-syntax-entry ?\( "()" st)
(modify-syntax-entry ?\) ")(" st)
(modify-syntax-entry ?{ "(}" st)
(modify-syntax-entry ?\} "){" st)
st)
"Syntax table for `dyalog-mode'.")
(defvar dyalog-array-mode-syntax-table
(let ((st (make-syntax-table)))
(dolist (char
(string-to-list (concat dyalog-keyword-chars dyalog-ascii-chars)))
(modify-syntax-entry char "." st))
(modify-syntax-entry ?_ "_" st)
(modify-syntax-entry ?∆ "_" st)
(modify-syntax-entry ?⎕ "_" st)
;; Delimiters
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
(modify-syntax-entry ?\( "()" st)
(modify-syntax-entry ?\) ")(" st)
(modify-syntax-entry ?{ "(}" st)
(modify-syntax-entry ?\} "){" st)
st)
"Syntax table for `dyalog-array-mode'.")
(defconst dyalog-dfun-syntax-table
(let ((st (copy-syntax-table dyalog-mode-syntax-table)))
(modify-syntax-entry ?\( "." st)
(modify-syntax-entry ?\) "." st)
(modify-syntax-entry ?\[ "." st)
(modify-syntax-entry ?\] "." st)
st)
"Syntax table to only consider {} as parens.")
;;;###autoload
(defun dyalog-ediff-forward-word ()
"Move point forward one word."
(interactive)
(or (> (skip-chars-forward "A-Za-z_∆0-9") 0) ; name
(> (skip-chars-forward "⎕:A-Za-z") 0) ; sys name/keyword
(> (skip-chars-forward "0-9E¯.") 0) ; numbers
(> (skip-chars-forward "⍺⍵∇") 0) ; meta chars
(> (skip-chars-forward " ") 0) ; white space
(forward-char))) ; fallback
(defconst dyalog-delimiter-match
(let ((h (make-hash-table :test 'equal)))
(dolist (e '((":if" . ":endif")("{"."}")
(":for" . ":endfor")(":repeat" . ":until")
(":while" . ":endwhile")(":trap" . ":endtrap")
(":hold" . ":endhold")(":with" . ":endwith")
(":namespace" . ":endnamespace")(":class" . ":endclass")
(":select" . ":endselect")(":interface" . ":endinterface")
(":property" . ":endproperty")))
(puthash (car e) (list (cdr e) 'block-start) h)
(puthash (cdr e) (list (car e) 'block-end) h))
(dolist (e '((":andif". ":if")(":orif".":if")(":elseif".":if")))
(puthash (car e) (list (cdr e) 'block-pause) h))
(dolist (e '((":else" . ":\\(if\\|select\\|trap\\|hold\\)")
(":case" . ":\\(select\\|trap\\)")
(":caselist" . ":\\(select\\|trap\\)")))
(puthash (car e) (list (cdr e) 'block-pause) h))
(dolist (e '((":field" . ":\\(class\\|interface\\)")))
(puthash (car e) (list (cdr e) nil) h))
(dolist (e '(":access" ":using"))
(puthash e (list "" nil) h))
(puthash ":endrepeat" (list ":repeat" 'block-end) h)
(puthash ":end" (list nil 'block-end) h)
(puthash ":section" (list ":endsection" nil) h)
(puthash ":endsection" (list ":section" nil) h)
h))
(defconst dyalog-any-delimiter
":*"
"A bogus keyword used to indicate a match with any keyword.")
(defgroup dyalog nil
"Major mode `dyalog-mode' for editing Dyalog APL code."
:group 'languages
:prefix "dyalog-")
(defcustom dyalog-mode-hook nil
"List of functions to be executed on entry to `dyalog-mode'."
:type 'hook
:group 'dyalog)
(defcustom dyalog-leading-spaces 1
"The number of leading spaces to use for unknown buffer types.
Namespaces, classes and interfaces have 0 leading spaces in the left margin, and
functions have 1, but for buffers that cannot be qualified into one of these types,
the number of leading spaces defined here is used."
:type 'integer
:group 'dyalog)
(defcustom dyalog-indent-comments t
"True if comments should be indented according to the surrounding scope."
:type 'boolean
:group 'dyalog)
(defcustom dyalog-fix-whitespace-before-save nil
"If true, indent and delete redundant whitespace before saving."
:type 'boolean
:group 'dyalog)
(defvar dyalog-buffer-type nil
"Whether a buffer contains a function, namespace or something else.
This affects indentation, functions have a leading space on each
line, but namespaces don't. Valid values are 'space-or-class
'function and 'unknown.")
;;; Indentation
(defun dyalog-matching-delimiter (delimiter)
"Return the match for the given DELIMITER.
For example, if ':EndIf' is provided, return ':If' and vice versa."
(car (gethash (downcase delimiter) dyalog-delimiter-match nil)))
(defun dyalog-keyword-indent-type (keyword)
"Return a symbol indicating how a KEYWORD affects indentation.
If KEYWORD introduces a new block, (e.g :If), return
'block-start. If it ends a block (e.g. :EndIf), return
'block-end. If it ends a block and immediately starts a new
block (e.g. :Else or :Case), return 'block-pause. If the keyword
should be indented the same way as everything else, return nil."
(let ((d (gethash (downcase keyword) dyalog-delimiter-match nil)))
(and d (nth 1 d))))
(defun dyalog-specific-keyword-regex (keyword)
"Return a regex mathcing KEYWORD when point is at bol."
(concat "\\(\\(?:^\\s-*\\|\\(?:" dyalog-label-regex " *\\)\\)"
keyword "\\)\\|\\(⋄\\s-*" keyword "\\)"))
(defun dyalog-relative-indent (n)
"Return the no spaces to indent N tabstops relative to the current line."
(max (+ (current-indentation) (* tab-width n))
(dyalog-leading-indentation)))
(defun dyalog-previous-logical-line ()
"Move backwards to the start of the previous logical line.
Assumes point is at the beginning of a logical line."
(let ((bol (line-beginning-position))
(done nil))
(if (eq (point) bol)
(progn
(forward-line -1)
(end-of-line))
(when (eq (char-before) ?⋄)
(backward-char)))
(while (not done)
(skip-chars-backward "^⋄\r\n")
(if (eq (char-before) ?⋄)
(progn
(setq done (not (dyalog-in-comment-or-string)))
(when (not done)
(backward-char)))
(setq done t)))))
(defun dyalog-next-logical-line ()
"Move forward to the start of the next logical line.
Assumes point is at the start of a logical line."
(let ((done nil))
(when (eq (char-after) ?⋄)
(forward-char))
(while (not done)
(skip-chars-forward "^⋄\r\n")
(if (eq (char-after) ?⋄)
(setq done (not (dyalog-in-comment-or-string)))
(setq done t))
(if (eobp)
nil
(forward-char)))))
(defun dyalog-indent-parse-line (dfunstack on-tradfn-header)
"Parse the current logical line for indentation purposes.
DFUNSTACK is a list of delimiters of currently open dfun blocks.
This affects the parsing of :. ON-TRADFN-HEADER is true if the
line is a tradfn header, this affects the parsing of { and }.
Return a plist with properties :keyword, the keyword at the head
of the line, :label which is the label at the start of the line
if any, :dfunstack which is a list of dfun delimiters open at end
of line, and finally :next-line which is the character position
the next logical line starts at."
(let ((done nil)
(eol (line-end-position))
(in-dfun (equal "{" (car dfunstack)))
(dfun-count nil)
(label nil)
(keyword nil)
(indent-type nil)
(start nil))
(save-excursion
(if (eq (char-after) ?⋄)
(forward-char)
(if (and (not in-dfun) (looking-at dyalog-label-regex))
(progn
(setq label (match-string-no-properties 1))
(goto-char (match-end 0)))))
(setq start (point))
(cond
((looking-at-p "[ \t]*$")
(setq indent-type 'blank))
((looking-at-p "[ \t]*⍝")
(setq indent-type 'comment)))
(while (not done)
(skip-chars-forward "^⋄\r\n'{}⍝:")
(pcase (char-after)
(?'
(progn
(condition-case nil
(forward-sexp)
(scan-error (goto-char eol)))
(when (> (point) eol)
(goto-char eol)
(setq done t))))
(?⍝
(progn
(goto-char eol)
(setq done t)))
(?{
(if on-tradfn-header
(forward-char)
(progn
(push "{" dfunstack)
(setq dfun-count (1+ (or dfun-count 0)))
(if (eobp)
(setq done t)
(forward-char))
(setq in-dfun t))))
(?}
(if on-tradfn-header
(forward-char)
(progn
(when dfunstack
(pop dfunstack)
(setq dfun-count (1- (or dfun-count 0)))
(setq in-dfun (equal (car dfunstack) "{")))
(if (eobp)
(setq done t)
(forward-char)))))
(?:
(if (or in-dfun keyword)
(forward-char)
(progn
(setq keyword
(buffer-substring-no-properties
(point)
(progn
(skip-chars-forward ":A-Za-z")
(point)))
indent-type
(dyalog-keyword-indent-type keyword)))))
(_
(setq done t))))
(cond
((and dfun-count (> dfun-count 0))
(setq indent-type 'dfun-start))
((and dfun-count (< dfun-count 0))
(setq indent-type
(if (eq (save-excursion
(goto-char start)
(skip-syntax-forward " ")
(char-after))
?})
'dfun-end-and-dedent
'dfun-end))))
(unless (eobp)
(forward-char))
(list :label label :keyword keyword :dfunstack dfunstack
:indent-type indent-type :next-line (point)))))
(defun dyalog-indent-stop-block-end (match blockstack indent-status _funcount)
"Return whether we have found root for a block end, and amount of to indent.
MATCH is the keyword that matches the block end (e.g. :For
matches :EndFor), BLOCKSTACK is a stack of currently open blocks,
INDENT-STATUS is the indentation status of the current line (the
return value from `dyalog-indent-status', and FUNCOUNT is the
number of currently open tradfn definitions."
(let ((indent-type (plist-get indent-status :indent-type)))
(cond
((and (not blockstack)
(if match
(looking-at-p (dyalog-specific-keyword-regex match))
(memq indent-type '(block-start block-pause))))
(list t (dyalog-relative-indent 0)))
((and (memq indent-type '(tradfn-start tradfn-end))
(not (string-match ":\\(End\\)?\\(Namespace\\|Class\\|Section\\)" match)))
(list t (skip-chars-forward " ∇"))))))
(defun dyalog-indent-stop-tradfn (blockstack indent-status _funcount)
"Return whether we have found root for a tradfn, and chars to indent.
BLOCKSTACK is a stack of currently open blocks, INDENT-STATUS is
the indentation status of the current line (the return value from
`dyalog-indent-status', and FUNCOUNT is the number of currently
open tradfn definitions."
(cond ((and (not blockstack)
(looking-at-p (dyalog-specific-keyword-regex
":\\(Class\\|Namespace\\)")))
(list t (dyalog-relative-indent 1)))
((and (not blockstack)
(memq (plist-get indent-status :indent-type)
'(tradfn-start tradfn-end)))
(list t (current-indentation)))))
(defun dyalog-indent-search-stop-function (keyword
&optional match_ indent-type_)
"Given a KEYWORD, return a function to check for indentation root.
Optional argument MATCH_ is the matching keyword (e.g. :If
for :EndIf) and only needs to be supplied if it differs from the
default. INDENT-TYPE_ is also optional, and is the indentation
type for the given keyword (see `dyalog-keyword-indent-type') and
only needs to be supplied if it differs from the default."
(let* ((match (or match_ (dyalog-matching-delimiter keyword)))
(indent-type (or indent-type_ (dyalog-keyword-indent-type keyword))))
(cond
((eq 'block-start indent-type)
#'dyalog-indent-search-stop-generic)
((memq indent-type '(block-end block-pause))
(apply-partially 'dyalog-indent-stop-block-end match))
((member (downcase keyword) '(":access" ":using"))
#'dyalog-indent-search-stop-access)
(t
#'dyalog-indent-search-stop-generic))))
(defun dyalog-indent-search-stop-access (blockstack indent-status funcount)
"Return if we have found an indentation root and no chars to indent.
:Access keywords are a special case since they are aligned
either to a tradfn or at the same level as their parent :Property or :Class block."
(let ((indent-type (plist-get indent-status :indent-type))
(delimiter (plist-get indent-status :delimiter))
(label-at-bol (plist-get indent-status :label-at-bol)))
(cond
((member (downcase (or delimiter "")) '(":class" ":property"))
(list t (current-indentation)))
((and (eq indent-type 'tradfn-start)
(eq funcount 0))
(list t (skip-chars-forward " ∇")))
((bobp)
(list t (dyalog-leading-indentation)))
(t
(list nil 0)))))
(defun dyalog-indent-search-stop-generic (blockstack indent-status funcount)
"Return if we have found an indentation root, and no chars to indent.
BLOCKSTACK is a stack of currently open blocks, INDENT-STATUS is
the indentation status of the current keyword (if any), and
FUNCOUNT is the number of currently open tradfn definitions."
(let ((indent-type (plist-get indent-status :indent-type))
(label-at-bol (plist-get indent-status :label-at-bol)))
(cond
((and (eq indent-type 'block-start) (not blockstack) (eq funcount 0))
(list t (+ (dyalog-relative-indent 1)
(if label-at-bol 1 0))))
((and (eq indent-type 'block-end) (not blockstack) (eq funcount 0))
(list t (+ (current-indentation)
(if label-at-bol 1 0))))
((and (eq indent-type 'tradfn-start)
(eq funcount 0))
(list t (skip-chars-forward " \t∇")))
((eq indent-type 'tradfn-end)
(list t (skip-chars-forward " \t")))
((bobp)
(list t (dyalog-leading-indentation)))
(t
(list nil 0)))))
(defun dyalog-indent-status (dfunstack)
"Return a list of information on the current indentation status.
DFUNSTACK is a list of open dfun blocks at point. The list of
information returned includes whether we are at the start of a
block, or the end (or at a pause inside a block), and the name of
the delimiter that triggers the starting or ending of a
block (e.g. \":If\" or \"∇\"."
(let ((next-line (min (point-max) (1+ (line-end-position)))))
(cond
((and (not dfunstack) (dyalog-on-tradfn-header))
(list :indent-type 'tradfn-start :delimiter nil :label-at-bol nil
:next-line next-line))
((looking-at dyalog-naked-nabla)
(list :indent-type 'tradfn-end :delimiter nil :label-at-bol nil
:next-line next-line))
(t
(let* ((indent-parse (dyalog-indent-parse-line dfunstack nil))
(keyword (plist-get indent-parse :keyword))
(indent-type (plist-get indent-parse :indent-type))
(label (plist-get indent-parse :label))
(next-line (plist-get indent-parse :next-line)))
(list :indent-type indent-type :delimiter keyword
:label-at-bol label :next-line next-line
:dfunstack (plist-get indent-parse :dfunstack)))))))
(defun dyalog-search-indent-root (at-root-function)
"Given function AT-ROOT-FUNCTION, search backwards for the root indent.
AT-ROOT-FUNCTION assumes point is at the beginning of a logical
line and returns t when point is at the line containing the
indentation root. For example if we are indenting a :EndFor,
AT-ROOT-FUNCTION returns t when we have reached the corresponding :For."
(let* ((indentation nil)
(blockstack ())
(funcount 0))
(save-excursion
(while (not indentation)
;; TODO: We should probably skip past d-funs
(dyalog-previous-logical-line)
(let* ((in-dfun (dyalog-in-dfun))
(status (dyalog-indent-status nil))
(keyword (plist-get status :delimiter))
(indent-type (plist-get status :indent-type))
(root (apply at-root-function
(list blockstack status funcount)))
(at-root (car root)))
(setq indentation
(cond
(in-dfun
(goto-char (plist-get in-dfun :start))
(dyalog-next-logical-line)
(dyalog-previous-logical-line)
nil)
(at-root
(nth 1 root))
((eq 'block-end indent-type)
(progn
(push (or (dyalog-matching-delimiter keyword)
dyalog-any-delimiter)
blockstack)
nil))
((eq 'block-start indent-type)
(progn
(when (or (equal dyalog-any-delimiter (car blockstack))
(compare-strings keyword nil nil
(or (car blockstack) "") nil nil
'ignore-case))
(pop blockstack))
nil))
((eq 'tradfn-end indent-type)
(setq funcount (1+ funcount))
nil)
((eq 'tradfn-start indent-type)
(setq funcount (1- funcount))
nil)))
(when (and (not indentation) (bobp))
(setq indentation (dyalog-leading-indentation)))))
(list :indent indentation :has-label nil
:funcount funcount :blockstack blockstack))))
(defun dyalog-calculate-dfun-indent ()
"Calculate the indentation amount for a line in a dfun."
(let* ((start (point))
(line-start (+ start (skip-syntax-forward "-"))))
(save-excursion
(let ((containing-brace (scan-lists start -1 1)))
(if (< containing-brace line-start)
(progn
(goto-char containing-brace)
(dyalog-relative-indent
(if (equal (char-after line-start) ?})
0 1)))
(dyalog-leading-indentation))))))
(defun dyalog-calculate-indent ()
"Calculate the amount of indentation for the current line.
Return a plist with the indent in spaces, and whether the current
line has a label."
(save-excursion
(move-beginning-of-line nil)
(let* ((dfunstack (dyalog-current-dfun-stack))
(indent-status (dyalog-indent-status dfunstack))
(indent-type (plist-get indent-status :indent-type))
(label (plist-get indent-status :label-at-bol))
(keyword (plist-get indent-status :delimiter))
(indent-info nil)
(current-line-indent-info nil))
(setq indent-info
(cond
((bobp)
(list :indent (dyalog-leading-indentation)
:has-label nil
:is-comment nil
:funcount 0
:blockstack nil))
(dfunstack
(list :indent (dyalog-calculate-dfun-indent)
:has-label nil
:is-comment nil
:funcount 0
:dfunstack dfunstack))
(label
(let* ((label-indent-info (dyalog-search-indent-root
#'dyalog-indent-stop-tradfn))
(label-indent (plist-get label-indent-info :indent))
(old-label (dyalog-remove-label))
(rest-indent-info (dyalog-calculate-indent)))
;; A label is always aligned 1 space to the left of the
;; surrounding tradfn, and since we search for tradfn
;; delimiters, we align to the nabla. So if we've reached the
;; beginning of the buffer, we subtract one and if we've
;; aligned to the nabla we add one.
(setq label-indent (max 0 (+ label-indent
(if (= label-indent
(dyalog-leading-indentation))
-1
1))))
(insert old-label)
(plist-put rest-indent-info :has-label t)
(plist-put rest-indent-info :label-indent label-indent)
rest-indent-info))
((eq indent-type 'comment)
(if dyalog-indent-comments
(let ((l (dyalog-search-indent-root
#'dyalog-indent-search-stop-generic)))
(plist-put l :is-comment t))
(list :indent (current-indentation)
:has-label nil
:is-comment t
:funcount 0
:blockstack nil)))
(keyword
(dyalog-search-indent-root
(dyalog-indent-search-stop-function keyword)))
((eq 'tradfn-end indent-type)
(dyalog-search-indent-root #'dyalog-indent-stop-tradfn))
((eq 'tradfn-start indent-type)
(dyalog-search-indent-root #'dyalog-indent-stop-tradfn))
(t
(dyalog-search-indent-root #'dyalog-indent-search-stop-generic))))
(setq current-line-indent-info
(dyalog-indent-from-indent-type indent-status indent-info
(current-indentation)))
(unless (eq 'blank indent-type)
(plist-put indent-info :next-indent
(plist-get current-line-indent-info :next-indent)))
indent-info)))
(defun dyalog-leading-indentation ()
"Return the number of spaces to indent by in the current buffer.
This varies depending of the type of object being edited,
namespaces or classes have no extra leading indentation, but functions have
one extra space, to be consistent with separating multiple
functions with ∇."
(pcase (or dyalog-buffer-type (dyalog-guess-buffer-type))
(`space-or-class 0)
(`function 1)
(`unknown dyalog-leading-spaces)))
(defun dyalog-indent-line-with (indent-info)
"Indent the current line according to INDENT-INFO.
INDENT-INFO is the return value from `dyalog-calculate-indent'."
(let* ((indent (plist-get indent-info :indent))
(has-label (plist-get indent-info :has-label))
(is-comment (plist-get indent-info :is-comment)))
(if has-label
(let* ((old-label (dyalog-remove-label))
(label-length (length old-label))
(label-indent (plist-get indent-info :label-indent)))
(if (and (not dyalog-indent-comments) is-comment)
(setq indent (- indent label-indent))
(if (> label-length indent)
;; Label is longer than required indentation, so line
;; should be flush with label
(setq indent 0)
(setq indent (max 0
(- indent (+ label-length label-indent))))))
;; Keywords are never flush with the label, since they start with
;; a colon, and the label ends with one
(beginning-of-line)
(when (looking-at-p "^ *:")
(setq indent (max 1 indent)))
(indent-line-to indent)
(beginning-of-line)
(insert (make-string label-indent ? ))
(insert old-label)
(back-to-indentation))
(indent-line-to indent))))
(defun dyalog-indent-line ()
"Indent the current line."
(interactive)
(let* ((restore-pos (> (current-column) (current-indentation)))
(old-pos (point))
(indent-info (dyalog-calculate-indent)))
(dyalog-indent-line-with indent-info)
(when restore-pos
(goto-char (min old-pos (line-end-position))))))
(defun dyalog-current-tradfn-indentation ()
"Return the column 0 indentation of the tradfn point is in, otherwise nil."
(let* ((tradfn-info (dyalog-tradfn-info))
(tradfn-name (car tradfn-info))
(end-of-header (nth 3 tradfn-info)))
(when (not (zerop (length tradfn-name)))
(save-excursion
(goto-char end-of-header)
(beginning-of-line)
(skip-chars-forward " ∇")
(current-column)))))
(defun dyalog-current-dfun-stack ()
"Return a list of open dynamic functions delimiters."
(let ((in-dfun nil)
(dfunstack ()))
(save-excursion
(while (setq in-dfun (dyalog-in-dfun))
(push "{" dfunstack)
(goto-char (plist-get in-dfun :start)))
dfunstack)))
(defun dyalog-indent-region (start end)
"Indent every line in the current region.
START and END specify the region to indent."
(let ((deactivate-mark nil)
(indent-info nil))
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char start)
(goto-char (setq start (line-beginning-position)))
(forward-line -1)
(setq indent-info (dyalog-calculate-indent))
(plist-put indent-info :tradfn-indent
(dyalog-current-tradfn-indentation))
(plist-put indent-info :nabla-indent
(dyalog-current-nabla-indent))
(when (= (point) start)
;; if start is on the first line of the buffer, we zero
;; next-indent, since we haven't actually initialized indent-info
;; with values from a previous line.
(plist-put indent-info :next-indent 0))
(goto-char start)
(while (< (point) end)
(setq indent-info (dyalog-indent-update indent-info))
(when (bolp)
(save-excursion
(dyalog-indent-line-with indent-info)))
(dyalog-next-logical-line))
(move-marker end nil))
nil))
(defun dyalog-indent-update (indent-info)
"Calculate an updated indentation after the current logical line.
INDENT-INFO is a plist of indentation information, on the same
form as the return value from `dyalog-calculate-indent'. Return
the updated plist of indentation information."
(let* ((dfunstack (plist-get indent-info :dfunstack))
(indent-status (dyalog-indent-status dfunstack))
(label (plist-get indent-status :label-at-bol))
(current-indent nil))
(plist-put indent-info :is-comment nil)
(plist-put indent-info :dfunstack (plist-get indent-status
:dfunstack))
(if label
(let* ((label-indent (max 0 (1- (or (plist-get indent-info
:tradfn-indent)
(dyalog-leading-indentation))))))
(plist-put indent-info :has-label t)
(plist-put indent-info :label-indent label-indent)
(setq current-indent
(save-excursion
(max
(- (+ (skip-chars-forward "^:")
(skip-chars-forward ":")
(skip-chars-forward " \t"))
(length label))
0))))
(progn
(plist-put indent-info :has-label nil)
(setq current-indent (current-indentation))))
(setq indent-info (dyalog-indent-from-indent-type indent-status
indent-info
current-indent))
indent-info))
(defun dyalog-indent-from-indent-type (indent-status indent-info
current-indent)
"Calculate an updated indentation, disregarding any label.
INDENT-STATUS is the indentation status of the current logical
line (as returned by `dyalog-indent-status'). INDENT-INFO is a
plist of indentation information, in the same form as the return
value from `dyalog-calculate-indent'. CURRENT-INDENT is the
current indentation in spaces, disregarding any label. Return the
updated plist of indentation information."
(let* ((indent-type (plist-get indent-status :indent-type))
(delimiter (plist-get indent-status :delimiter))
(blockstack (plist-get indent-info :blockstack))
(next-indent (or (plist-get indent-info :next-indent) 0))
(previous-indent (plist-get indent-info :indent))
(indent (+ previous-indent
next-indent))
(temp-indent 0)
(tradfn-indent (plist-get indent-info :tradfn-indent))
(nabla-indent (plist-get indent-info :nabla-indent))
(ret (copy-sequence indent-info)))
(cond
((eq 'comment indent-type)
(if (not dyalog-indent-comments)
(progn
(setq next-indent (- indent current-indent)
indent current-indent)
(plist-put ret :is-comment t))
(setq next-indent 0)))
((eq 'block-end indent-type)
(progn
;; (unless (string-equal (car blockstack)
;; (dyalog-matching-delimiter delimiter))
;; (error "Non matching delimiter"))
;; We assume delimiters match, since the region might cover
;; only part of matched delimiters
(when blockstack
(pop blockstack))
(setq indent (- indent tab-width)
next-indent 0)))
((eq 'block-start indent-type)
(progn
(push delimiter blockstack)
(setq next-indent tab-width)))
((eq 'block-pause indent-type)
(setq indent (- indent tab-width)
next-indent tab-width))
((eq 'dfun-start indent-type)
(setq next-indent tab-width))
((eq 'dfun-end indent-type)
(setq next-indent (- tab-width)))
((eq 'dfun-end-and-dedent indent-type)
(setq indent (- indent tab-width)
next-indent 0))
((eq 'tradfn-end indent-type)
(setq tradfn-indent nil
indent (or nabla-indent indent)
next-indent 0
nabla-indent nil))
((eq 'tradfn-start indent-type)
(let ((nabla-at-bol (looking-at-p " *∇")))
(setq tradfn-indent (if nabla-at-bol
(+ (save-excursion
(skip-chars-forward "^∇")
(skip-chars-forward " ∇"))
indent)
(save-excursion
(skip-chars-forward " ")))
next-indent (- tradfn-indent indent)
nabla-indent (if nabla-at-bol
indent
previous-indent))))
((eq 'blank indent-type)
(setq next-indent indent
indent 0))
((member (downcase (or delimiter "")) '(":access" ":using"))
(setq temp-indent indent
indent (or tradfn-indent (- indent tab-width))
next-indent (- temp-indent indent)))
;; TODO: dfuns
(t
(setq next-indent 0)))
(plist-put ret :blockstack blockstack)
(plist-put ret :indent indent)