-
Notifications
You must be signed in to change notification settings - Fork 10
/
purescript-indent.el
1590 lines (1465 loc) · 69.6 KB
/
purescript-indent.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
;;; purescript-indent.el --- "semi-intelligent" indentation module for PureScript Mode -*- lexical-binding: t -*-
;; Copyright 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
;; Copyright 1997-1998 Guy Lapalme
;; Author: 1997-1998 Guy Lapalme <[email protected]>
;; Keywords: indentation PureScript layout-rule
;; Version: 1.2
;; URL: http://www.iro.umontreal.ca/~lapalme/layout/index.html
;; This file is not 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 3, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Purpose:
;;
;; To support automatic indentation of PureScript programs using
;; the layout rule described in section 1.5 and appendix B.3 of the
;; the PureScript report. The rationale and the implementation principles
;; are described in an article to appear in Journal of Functional Programming.
;; "Dynamic tabbing for automatic indentation with the layout rule"
;;
;; It supports literate scripts.
;; PureScript indentation is performed
;; within \begin{code}...\end{code} sections of a literate script
;; and in lines beginning with > with Bird style literate script
;; TAB aligns to the left column outside of these sections.
;;
;; Installation:
;;
;; To turn indentation on for all PureScript buffers under the PureScript
;; mode of Moss&Thorn <http://www.purescript.org/purescript-mode/>
;; add this to .emacs:
;;
;; (add-hook 'purescript-mode-hook 'turn-on-purescript-indent)
;;
;; Otherwise, call `turn-on-purescript-indent'.
;;
;;
;; Customisation:
;; The "standard" offset for statements is 4 spaces.
;; It can be changed by setting the variable "purescript-indent-offset" to
;; another value
;;
;; The default number of blanks after > in a Bird style literate script
;; is 1; it can be changed by setting the variable
;; "purescript-indent-literate-Bird-default-offset"
;;
;; `purescript-indent-hook' is invoked if not nil.
;;
;; All functions/variables start with
;; `(turn-(on/off)-)purescript-indent' or `purescript-indent-'.
;; This file can also be used as a hook for the Hugs Mode developed by
;; Chris Van Humbeeck <[email protected]>
;; It can be obtained at:
;; http://www-i2.informatik.rwth-aachen.de/Forschung/FP/PureScript/hugs-mode.el
;;
;; For the Hugs mode put the following in your .emacs
;;
;;(setq auto-mode-alist (append auto-mode-alist '(("\\.purs\\'" . hugs-mode))))
;;(autoload 'hugs-mode "hugs-mode" "Go into hugs mode" t)
;;
;; If only the indentation mode is used then replace the two
;; preceding lines with
;;(setq auto-mode-alist (append auto-mode-alist
;; '(("\\.purs\\'" . turn-on-purescript-indent))))
;;(autoload 'turn-on-purescript-indent "hindent" "Indentation mode for PureScript" t)
;;
;; For indentation in both cases then add the following to your .emacs
;;(add-hook 'hugs-mode-hook 'turn-on-purescript-indent)
;;(autoload 'purescript-indent-cycle "hindent" "Indentation cycle for PureScript" t)
;;
;;; Code:
(require 'purescript-string)
(require 'cl-lib)
(defvar purescript-literate)
(defgroup purescript-indent nil
"PureScript indentation."
:group 'purescript
:link '(custom-manual "(purescript-mode)Indentation")
:prefix "purescript-indent-")
(defcustom purescript-indent-offset 4
"Indentation of PureScript statements with respect to containing block."
:type 'integer
:group 'purescript-indent)
(defcustom purescript-indent-literate-Bird-default-offset 1
"Default number of blanks after > in a Bird style literate script."
:type 'integer
:group 'purescript-indent)
(defcustom purescript-indent-rpurs-align-column 0
"Column on which to align right-hand sides (use 0 for ad-hoc alignment)."
:type 'integer
:group 'purescript-indent)
(defun purescript-indent-point-to-col (apoint)
"Return the column number of APOINT."
(save-excursion
(goto-char apoint)
(current-column)))
(defconst purescript-indent-start-keywords-re
(concat "\\<"
(regexp-opt '("class" "data" "import" "infix" "infixl" "infixr"
"instance" "module" "newtype" "primitive" "type") t)
"\\>")
"Regexp for keywords to complete when standing at the first word of a line.")
;; Customizations for different kinds of environments
;; in which dealing with low-level events are different.
(defun purescript-indent-mark-active ()
(if (featurep 'xemacs)
(if zmacs-regions
zmacs-region-active-p
t)
mark-active))
;; for pushing indentation information
(defvar purescript-indent-info) ;Used with dynamic scoping.
(defun purescript-indent-push-col (col &optional name)
"Push indentation information for the column COL.
The info is followed by NAME (if present).
Makes sure that the same indentation info is not pushed twice.
Uses free var `purescript-indent-info'."
(let ((tmp (cons col name)))
(if (member tmp purescript-indent-info)
purescript-indent-info
(push tmp purescript-indent-info))))
(defun purescript-indent-push-pos (pos &optional name)
"Push indentation information for POS followed by NAME (if present)."
(purescript-indent-push-col (purescript-indent-point-to-col pos) name))
;; (defvar purescript-indent-tab-align nil
;; "Align all indentations on TAB stops.")
(defun purescript-indent-column+offset (column offset)
(unless offset (setq offset purescript-indent-offset))
(setq column (+ column offset))
;; (if (and purescript-indent-tab-align (> offset 0))
;; (* 8 (/ (+ column 7) 8))
column) ;; )
(defun purescript-indent-push-pos-offset (pos &optional offset)
"Pushes indentation information for the column corresponding to POS
followed by an OFFSET (if present use its value otherwise use
`purescript-indent-offset')."
(purescript-indent-push-col (purescript-indent-column+offset
(purescript-indent-point-to-col pos)
offset)))
;; redefinition of some Emacs function for dealing with
;; Bird Style literate scripts
(defun purescript-indent-bolp ()
"`bolp' but dealing with Bird-style literate scripts."
(or (bolp)
(and (eq purescript-literate 'bird)
(<= (current-column) (1+ purescript-indent-literate-Bird-default-offset))
(eq (char-after (line-beginning-position)) ?\>))))
(defun purescript-indent-empty-line-p ()
"Checks if the current line is empty; deals with Bird style scripts."
(save-excursion
(beginning-of-line)
(if (and (eq purescript-literate 'bird)
(eq (following-char) ?\>))
(forward-char 1))
(looking-at "[ \t]*$")))
(defun purescript-indent-back-to-indentation ()
"`back-to-indentation' function but dealing with Bird-style literate scripts."
(if (and (eq purescript-literate 'bird)
(progn (beginning-of-line) (eq (following-char) ?\>)))
(progn
(forward-char 1)
(skip-chars-forward " \t"))
(back-to-indentation)))
(defun purescript-indent-current-indentation ()
"`current-indentation' function dealing with Bird-style literate scripts."
(if (eq purescript-literate 'bird)
(save-excursion
(purescript-indent-back-to-indentation)
(current-column))
(current-indentation)))
(defun purescript-indent-backward-to-indentation (n)
"`backward-to-indentation' function dealing with Bird-style literate scripts."
(if (eq purescript-literate 'bird)
(progn
(forward-line (- n))
(purescript-indent-back-to-indentation))
(backward-to-indentation n)))
(defun purescript-indent-forward-line (&optional n)
"`forward-line' function but dealing with Bird-style literate scripts."
(prog1
(forward-line n)
(if (and (eq purescript-literate 'bird) (eq (following-char) ?\>))
(progn (forward-char 1) ; skip > and initial blanks...
(skip-chars-forward " \t")))))
(defun purescript-indent-line-to (n)
"`indent-line-to' function but dealing with Bird-style literate scripts."
(if (eq purescript-literate 'bird)
(progn
(beginning-of-line)
(if (eq (following-char) ?\>)
(delete-char 1))
(delete-horizontal-space) ; remove any starting TABs so
(indent-line-to n) ; that indent-line only adds spaces
(save-excursion
(beginning-of-line)
(if (> n 0) (delete-char 1)) ; delete the first space before
(insert ?\>))) ; inserting a >
(indent-line-to n)))
(defun purescript-indent-skip-blanks-and-newlines-forward (end)
"Skip forward blanks, tabs and newlines until END.
Take account of Bird-style literate scripts."
(skip-chars-forward " \t\n" end)
(if (eq purescript-literate 'bird)
(while (and (bolp) (eq (following-char) ?\>))
(forward-char 1) ; skip >
(skip-chars-forward " \t\n" end))))
(defun purescript-indent-skip-blanks-and-newlines-backward (start)
"Skip backward blanks, tabs and newlines up to START.
Take account of Bird-style literate scripts."
(skip-chars-backward " \t\n" start)
(if (eq purescript-literate 'bird)
(while (and (eq (current-column) 1)
(eq (preceding-char) ?\>))
(forward-char -1) ; skip back >
(skip-chars-backward " \t\n" start))))
;; specific functions for literate code
(defun purescript-indent-within-literate-code ()
"Check if point is within a part of literate PureScript code.
If so, return its start; otherwise return nil:
If it is Bird-style, then return the position of the >;
otherwise return the ending position of \\begin{code}."
(save-excursion
(cl-case purescript-literate
(bird
(beginning-of-line)
(if (or (eq (following-char) ?\>)
(and (bolp) (forward-line -1) (eq (following-char) ?\>)))
(progn
(while (and (zerop (forward-line -1))
(eq (following-char) ?\>)))
(if (not (eq (following-char) ?\>))
(forward-line))
(point))))
;; Look for a \begin{code} or \end{code} line.
((latex tex)
(if (re-search-backward
"^\\(\\\\begin{code}$\\)\\|\\(\\\\end{code}$\\)" nil t)
;; within a literate code part if it was a \\begin{code}.
(match-end 1)))
(t (error "purescript-indent-within-literate-code: should not happen!")))))
(defun purescript-indent-put-region-in-literate (beg end &optional arg)
"Put lines of the region as a piece of literate code.
With prefix arg, remove indication that the region is literate code.
It deals with both Bird style and non Bird-style scripts."
(interactive "r\nP")
(unless purescript-literate
(error "Cannot put a region in literate in a non literate script"))
(if (eq purescript-literate 'bird)
(let ((comment-start "> ") ; Change dynamic bindings for
(comment-start-skip "^> ?") ; comment-region.
(comment-end "")
(comment-end-skip "\n")
(comment-style 'plain))
(comment-region beg end arg))
;; Not Bird style.
(if arg ; Remove the literate indication.
(save-excursion
(goto-char end) ; Remove end.
(if (re-search-backward "^\\\\end{code}[ \t\n]*\\="
(line-beginning-position -2) t)
(delete-region (point) (line-beginning-position 2)))
(goto-char beg) ; Remove end.
(beginning-of-line)
(if (looking-at "\\\\begin{code}")
(kill-line 1)))
(save-excursion ; Add the literate indication.
(goto-char end)
(unless (bolp) (newline))
(insert "\\end{code}\n")
(goto-char beg)
(unless (bolp) (newline))
(insert "\\begin{code}\n")))))
;;; Start of indentation code
(defcustom purescript-indent-look-past-empty-line t
"If nil, indentation engine will not look past an empty line for layout points."
:group 'purescript-indent
:type 'boolean)
(defun purescript-indent-start-of-def ()
"Return the position of the start of a definition.
The start of a def is expected to be recognizable by starting in column 0,
unless `purescript-indent-look-past-empty-line' is nil, in which case we
take a coarser approximation and stop at the first empty line."
(save-excursion
(let ((start-code (and purescript-literate
(purescript-indent-within-literate-code)))
(top-col (if (eq purescript-literate 'bird) 2 0))
(save-point (point)))
;; determine the starting point of the current piece of code
(setq start-code (if start-code (1+ start-code) (point-min)))
;; go backward until the first preceding empty line
(purescript-indent-forward-line -1)
(while (and (if purescript-indent-look-past-empty-line
(or (> (purescript-indent-current-indentation) top-col)
(purescript-indent-empty-line-p))
(and (> (purescript-indent-current-indentation) top-col)
(not (purescript-indent-empty-line-p))))
(> (point) start-code)
(= 0 (purescript-indent-forward-line -1))))
;; go forward after the empty line
(if (purescript-indent-empty-line-p)
(purescript-indent-forward-line 1))
(setq start-code (point))
;; find the first line of code which is not a comment
(forward-comment (point-max))
(if (> (point) save-point)
start-code
(point)))))
(defun purescript-indent-open-structure (start end)
"If any structure (list or tuple) is not closed, between START and END,
returns the location of the opening symbol, nil otherwise."
(save-excursion
(nth 1 (parse-partial-sexp start end))))
(defun purescript-indent-in-string (start end)
"If a string is not closed , between START and END, returns the
location of the opening symbol, nil otherwise."
(save-excursion
(let ((pps (parse-partial-sexp start end)))
(if (nth 3 pps) (nth 8 pps)))))
(defun purescript-indent-in-comment (start end)
"Check, starting from START, if END is at or within a comment.
Returns the location of the start of the comment, nil otherwise."
(let (pps)
(cl-assert (<= start end))
(cond ((= start end) nil)
((nth 4 (save-excursion (setq pps (parse-partial-sexp start end))))
(nth 8 pps))
;; We also want to say that we are *at* the beginning of a comment.
((and (not (nth 8 pps))
(>= (point-max) (+ end 2))
(nth 4 (save-excursion
(setq pps (parse-partial-sexp end (+ end 2))))))
(nth 8 pps)))))
(defvar purescript-indent-off-side-keywords-re
"\\<\\(do\\|let\\|of\\|where\\|mdo\\|rec\\)\\>[ \t]*")
(defun purescript-indent-type-at-point ()
"Return the type of the line (also puts information in `match-data')."
(cond
((purescript-indent-empty-line-p) 'empty)
((purescript-indent-in-comment (point-min) (point)) 'comment)
((looking-at "\\(\\([[:alpha:]]\\(\\sw\\|'\\)*\\)\\|_\\)[ \t\n]*")
'ident)
((looking-at "\\(|[^|]\\)[ \t\n]*") 'guard)
((looking-at "\\(=[^>=]\\|::\\|->\\|<-\\)[ \t\n]*") 'rpurs)
(t 'other)))
(defvar purescript-indent-current-line-first-ident ""
"Global variable that keeps track of the first ident of the line to indent.")
(defun purescript-indent-contour-line (start end)
"Generate contour information between START and END points."
(if (< start end)
(save-excursion
(goto-char end)
(purescript-indent-skip-blanks-and-newlines-backward start)
(let ((cur-col (current-column)) ; maximum column number
(fl 0) ; number of lines that forward-line could not advance
contour)
(while (and (> cur-col 0) (= fl 0) (>= (point) start))
(purescript-indent-back-to-indentation)
(if (< (point) start) (goto-char start))
(and (not (member (purescript-indent-type-at-point)
'(empty comment))) ; skip empty and comment lines
(< (current-column) cur-col) ; less indented column found
(push (point) contour) ; new contour point found
(setq cur-col (current-column)))
(setq fl (purescript-indent-forward-line -1)))
contour))))
(defun purescript-indent-next-symbol (end)
"Move point to the next symbol."
(skip-syntax-forward ")" end)
(if (< (point) end)
(progn
(forward-sexp 1)
(purescript-indent-skip-blanks-and-newlines-forward end))))
(defun purescript-indent-next-symbol-safe (end)
"Puts point to the next following symbol, or to end if there are no more
symbols in the sexp."
(condition-case nil (purescript-indent-next-symbol end)
(error (goto-char end))))
(defun purescript-indent-separate-valdef (start end)
"Return a list of positions for important parts of a valdef."
(save-excursion
(let (valname valname-string aft-valname
guard aft-guard
rpurs-sign aft-rpurs-sign
type)
;; "parse" a valdef separating important parts
(goto-char start)
(setq type (purescript-indent-type-at-point))
(if (or (memq type '(ident other))) ; possible start of a value def
(progn
(if (eq type 'ident)
(progn
(setq valname (match-beginning 0))
(setq valname-string (match-string 0))
(goto-char (match-end 0)))
(skip-chars-forward " \t" end)
(setq valname (point)) ; type = other
(purescript-indent-next-symbol-safe end))
(while (and (< (point) end)
(setq type (purescript-indent-type-at-point))
(or (memq type '(ident other))))
(if (null aft-valname)
(setq aft-valname (point)))
(purescript-indent-next-symbol-safe end))))
(if (and (< (point) end) (eq type 'guard)) ; start of a guard
(progn
(setq guard (match-beginning 0))
(goto-char (match-end 0))
(while (and (< (point) end)
(setq type (purescript-indent-type-at-point))
(not (eq type 'rpurs)))
(if (null aft-guard)
(setq aft-guard (point)))
(purescript-indent-next-symbol-safe end))))
(if (and (< (point) end) (eq type 'rpurs)) ; start of a rpurs
(progn
(setq rpurs-sign (match-beginning 0))
(goto-char (match-end 0))
(if (< (point) end)
(setq aft-rpurs-sign (point)))))
(list valname valname-string aft-valname
guard aft-guard rpurs-sign aft-rpurs-sign))))
(defsubst purescript-indent-no-otherwise (guard)
"Check if there is no otherwise at GUARD."
(save-excursion
(goto-char guard)
(not (looking-at "|[ \t]*otherwise\\>"))))
(defun purescript-indent-guard (start end end-visible indent-info)
"Find indentation information for a line starting with a guard."
(save-excursion
(let* ((purescript-indent-info indent-info)
(sep (purescript-indent-separate-valdef start end))
(valname (nth 0 sep))
(guard (nth 3 sep))
(rpurs-sign (nth 5 sep)))
;; push information indentation for the visible part
(if (and guard (< guard end-visible) (purescript-indent-no-otherwise guard))
(purescript-indent-push-pos guard)
(if rpurs-sign
(purescript-indent-push-pos rpurs-sign) ; probably within a data definition...
(if valname
(purescript-indent-push-pos-offset valname))))
purescript-indent-info)))
(defun purescript-indent-rpurs (start end end-visible indent-info)
"Find indentation information for a line starting with a rpurs."
(save-excursion
(let* ((purescript-indent-info indent-info)
(sep (purescript-indent-separate-valdef start end))
(valname (nth 0 sep))
(guard (nth 3 sep))
(rpurs-sign (nth 5 sep)))
;; push information indentation for the visible part
(if (and rpurs-sign (< rpurs-sign end-visible))
(purescript-indent-push-pos rpurs-sign)
(if (and guard (< guard end-visible))
(purescript-indent-push-pos-offset guard)
(if valname ; always visible !!
(purescript-indent-push-pos-offset valname))))
purescript-indent-info)))
(defconst purescript-indent-decision-table
(let ((or "\\)\\|\\("))
(concat "\\("
"1.1.11" or ; 1= vn gd rh arh
"1.1.10" or ; 2= vn gd rh
"1.1100" or ; 3= vn gd agd
"1.1000" or ; 4= vn gd
"1.0011" or ; 5= vn rh arh
"1.0010" or ; 6= vn rh
"110000" or ; 7= vn avn
"100000" or ; 8= vn
"001.11" or ; 9= gd rh arh
"001.10" or ;10= gd rh
"001100" or ;11= gd agd
"001000" or ;12= gd
"000011" or ;13= rh arh
"000010" or ;14= rh
"000000" ;15=
"\\)")))
(defun purescript-indent-find-case (test)
"Find the index that matches TEST in the decision table."
(if (string-match purescript-indent-decision-table test)
;; use the fact that the resulting match-data is a list of the form
;; (0 6 [2*(n-1) nil] 0 6) where n is the number of the matching regexp
;; so n= ((length match-data)/2)-1
(- (/ (length (match-data 'integers)) 2) 1)
(error "purescript-indent-find-case: impossible case: %s" test)))
(defun purescript-indent-empty (start end end-visible indent-info)
"Find indentation points for an empty line."
(save-excursion
(let* ((purescript-indent-info indent-info)
(sep (purescript-indent-separate-valdef start end))
(valname (pop sep))
(valname-string (pop sep))
(aft-valname (pop sep))
(guard (pop sep))
(aft-guard (pop sep))
(rpurs-sign (pop sep))
(aft-rpurs-sign (pop sep))
(last-line (= end end-visible))
(test (string
(if valname ?1 ?0)
(if (and aft-valname (< aft-valname end-visible)) ?1 ?0)
(if (and guard (< guard end-visible)) ?1 ?0)
(if (and aft-guard (< aft-guard end-visible)) ?1 ?0)
(if (and rpurs-sign (< rpurs-sign end-visible)) ?1 ?0)
(if (and aft-rpurs-sign (< aft-rpurs-sign end-visible)) ?1 ?0))))
(if (and valname-string ; special case for start keywords
(string-match purescript-indent-start-keywords-re valname-string))
(progn
(purescript-indent-push-pos valname)
;; very special for data keyword
(if (string-match "\\<data\\>" valname-string)
(if rpurs-sign (purescript-indent-push-pos rpurs-sign)
(purescript-indent-push-pos-offset valname))
(purescript-indent-push-pos-offset valname)))
(cl-case ; general case
(purescript-indent-find-case test)
;; "1.1.11" 1= vn gd rh arh
(1 (purescript-indent-push-pos valname)
(purescript-indent-push-pos valname valname-string)
(if (purescript-indent-no-otherwise guard) (purescript-indent-push-pos guard "| "))
(purescript-indent-push-pos aft-rpurs-sign))
;; "1.1.10" 2= vn gd rh
(2 (purescript-indent-push-pos valname)
(purescript-indent-push-pos valname valname-string)
(if last-line
(purescript-indent-push-pos-offset guard)
(if (purescript-indent-no-otherwise guard) (purescript-indent-push-pos guard "| "))))
;; "1.1100" 3= vn gd agd
(3 (purescript-indent-push-pos valname)
(purescript-indent-push-pos aft-guard)
(if last-line (purescript-indent-push-pos-offset valname)))
;; "1.1000" 4= vn gd
(4 (purescript-indent-push-pos valname)
(if last-line (purescript-indent-push-pos-offset guard 2)))
;; "1.0011" 5= vn rh arh
(5 (purescript-indent-push-pos valname)
(if (or (and aft-valname (= (char-after rpurs-sign) ?\=))
(= (char-after rpurs-sign) ?\:))
(purescript-indent-push-pos valname valname-string))
(purescript-indent-push-pos aft-rpurs-sign))
;; "1.0010" 6= vn rh
(6 (purescript-indent-push-pos valname)
(purescript-indent-push-pos valname valname-string)
(if last-line (purescript-indent-push-pos-offset valname)))
;; "110000" 7= vn avn
(7 (purescript-indent-push-pos valname)
(if last-line
(purescript-indent-push-pos aft-valname)
(purescript-indent-push-pos valname valname-string)))
;; "100000" 8= vn
(8 (purescript-indent-push-pos valname))
;; "001.11" 9= gd rh arh
(9 (if (purescript-indent-no-otherwise guard) (purescript-indent-push-pos guard "| "))
(purescript-indent-push-pos aft-rpurs-sign))
;; "001.10" 10= gd rh
(10 (if (purescript-indent-no-otherwise guard) (purescript-indent-push-pos guard "| "))
(if last-line (purescript-indent-push-pos-offset guard)))
;; "001100" 11= gd agd
(11 (if (purescript-indent-no-otherwise guard) (purescript-indent-push-pos guard "| "))
(purescript-indent-push-pos aft-guard))
;; "001000" 12= gd
(12 (if (purescript-indent-no-otherwise guard) (purescript-indent-push-pos guard "| "))
(if last-line (purescript-indent-push-pos-offset guard 2)))
;; "000011" 13= rh arh
(13 (purescript-indent-push-pos aft-rpurs-sign))
;; "000010" 14= rh
(14 (if last-line (purescript-indent-push-pos-offset rpurs-sign 2 )))
;; "000000" 15=
(t (error "purescript-indent-empty: %s impossible case" test ))))
purescript-indent-info)))
(defun purescript-indent-ident (start end end-visible indent-info)
"Find indentation points for a line starting with an identifier."
(save-excursion
(let*
((purescript-indent-info indent-info)
(sep (purescript-indent-separate-valdef start end))
(valname (pop sep))
(valname-string (pop sep))
(aft-valname (pop sep))
(guard (pop sep))
(aft-guard (pop sep))
(rpurs-sign (pop sep))
(aft-rpurs-sign (pop sep))
(last-line (= end end-visible))
(is-where
(string-match "where[ \t]*" purescript-indent-current-line-first-ident))
(diff-first ; not a function def with the same name
(or (null valname-string)
(not (string= (purescript-trim valname-string)
(purescript-trim purescript-indent-current-line-first-ident)))))
;; (is-type-def
;; (and rpurs-sign (eq (char-after rpurs-sign) ?\:)))
(test (string
(if valname ?1 ?0)
(if (and aft-valname (< aft-valname end-visible)) ?1 ?0)
(if (and guard (< guard end-visible)) ?1 ?0)
(if (and aft-guard (< aft-guard end-visible)) ?1 ?0)
(if (and rpurs-sign (< rpurs-sign end-visible)) ?1 ?0)
(if (and aft-rpurs-sign (< aft-rpurs-sign end-visible)) ?1 ?0))))
(if (and valname-string ; special case for start keywords
(string-match purescript-indent-start-keywords-re valname-string))
(progn
(purescript-indent-push-pos valname)
(if (string-match "\\<data\\>" valname-string)
;; very special for data keyword
(if aft-rpurs-sign (purescript-indent-push-pos aft-rpurs-sign)
(purescript-indent-push-pos-offset valname))
(if (not (string-match
purescript-indent-start-keywords-re
purescript-indent-current-line-first-ident))
(purescript-indent-push-pos-offset valname))))
(if (string= purescript-indent-current-line-first-ident "::")
(if valname (purescript-indent-push-pos valname))
(cl-case ; general case
(purescript-indent-find-case test)
;; "1.1.11" 1= vn gd rh arh
(1 (if is-where
(purescript-indent-push-pos guard)
(purescript-indent-push-pos valname)
(if diff-first (purescript-indent-push-pos aft-rpurs-sign))))
;; "1.1.10" 2= vn gd rh
(2 (if is-where
(purescript-indent-push-pos guard)
(purescript-indent-push-pos valname)
(if last-line
(purescript-indent-push-pos-offset guard))))
;; "1.1100" 3= vn gd agd
(3 (if is-where
(purescript-indent-push-pos-offset guard)
(purescript-indent-push-pos valname)
(if diff-first
(purescript-indent-push-pos aft-guard))))
;; "1.1000" 4= vn gd
(4 (if is-where
(purescript-indent-push-pos guard)
(purescript-indent-push-pos valname)
(if last-line
(purescript-indent-push-pos-offset guard 2))))
;; "1.0011" 5= vn rh arh
(5 (if is-where
(purescript-indent-push-pos-offset valname)
(purescript-indent-push-pos valname)
(if diff-first
(purescript-indent-push-pos aft-rpurs-sign))))
;; "1.0010" 6= vn rh
(6 (if is-where
(purescript-indent-push-pos-offset valname)
(purescript-indent-push-pos valname)
(if last-line
(purescript-indent-push-pos-offset valname))))
;; "110000" 7= vn avn
(7 (if is-where
(purescript-indent-push-pos-offset valname)
(purescript-indent-push-pos valname)
(if last-line
(purescript-indent-push-pos aft-valname))))
;; "100000" 8= vn
(8 (if is-where
(purescript-indent-push-pos-offset valname)
(purescript-indent-push-pos valname)))
;; "001.11" 9= gd rh arh
(9 (if is-where
(purescript-indent-push-pos guard)
(purescript-indent-push-pos aft-rpurs-sign)))
;; "001.10" 10= gd rh
(10 (if is-where
(purescript-indent-push-pos guard)
(if last-line
(purescript-indent-push-pos-offset guard))))
;; "001100" 11= gd agd
(11 (if is-where
(purescript-indent-push-pos guard)
(if (purescript-indent-no-otherwise guard)
(purescript-indent-push-pos aft-guard))))
;; "001000" 12= gd
(12 (if last-line (purescript-indent-push-pos-offset guard 2)))
;; "000011" 13= rh arh
(13 (purescript-indent-push-pos aft-rpurs-sign))
;; "000010" 14= rh
(14 (if last-line (purescript-indent-push-pos-offset rpurs-sign 2)))
;; "000000" 15=
(t (error "purescript-indent-ident: %s impossible case" test )))))
purescript-indent-info)))
(defun purescript-indent-other (start end end-visible indent-info)
"Find indentation points for a non-empty line starting with something other
than an identifier, a guard or rpurs."
(save-excursion
(let* ((purescript-indent-info indent-info)
(sep (purescript-indent-separate-valdef start end))
(valname (pop sep))
(valname-string (pop sep))
(aft-valname (pop sep))
(guard (pop sep))
(aft-guard (pop sep))
(rpurs-sign (pop sep))
(aft-rpurs-sign (pop sep))
(last-line (= end end-visible))
(test (string
(if valname ?1 ?0)
(if (and aft-valname (< aft-valname end-visible)) ?1 ?0)
(if (and guard (< guard end-visible)) ?1 ?0)
(if (and aft-guard (< aft-guard end-visible)) ?1 ?0)
(if (and rpurs-sign (< rpurs-sign end-visible)) ?1 ?0)
(if (and aft-rpurs-sign (< aft-rpurs-sign end-visible)) ?1 ?0))))
(if (and valname-string ; special case for start keywords
(string-match purescript-indent-start-keywords-re valname-string))
(purescript-indent-push-pos-offset valname)
(cl-case ; general case
(purescript-indent-find-case test)
;; "1.1.11" 1= vn gd rh arh
(1 (purescript-indent-push-pos aft-rpurs-sign))
;; "1.1.10" 2= vn gd rh
(2 (if last-line
(purescript-indent-push-pos-offset guard)
(purescript-indent-push-pos-offset rpurs-sign 2)))
;; "1.1100" 3= vn gd agd
(3 (purescript-indent-push-pos aft-guard))
;; "1.1000" 4= vn gd
(4 (purescript-indent-push-pos-offset guard 2))
;; "1.0011" 5= vn rh arh
(5 (purescript-indent-push-pos valname)
(purescript-indent-push-pos aft-rpurs-sign))
;; "1.0010" 6= vn rh
(6 (if last-line
(purescript-indent-push-pos-offset valname)
(purescript-indent-push-pos-offset rpurs-sign 2)))
;; "110000" 7= vn avn
(7 (purescript-indent-push-pos-offset aft-valname))
;; "100000" 8= vn
(8 (purescript-indent-push-pos valname))
;; "001.11" 9= gd rh arh
(9 (purescript-indent-push-pos aft-rpurs-sign))
;; "001.10" 10= gd rh
(10 (if last-line
(purescript-indent-push-pos-offset guard)
(purescript-indent-push-pos-offset rpurs-sign 2)))
;; "001100" 11= gd agd
(11 (if (purescript-indent-no-otherwise guard)
(purescript-indent-push-pos aft-guard)))
;; "001000" 12= gd
(12 (if last-line (purescript-indent-push-pos-offset guard 2)))
;; "000011" 13= rh arh
(13 (purescript-indent-push-pos aft-rpurs-sign))
;; "000010" 14= rh
(14 (if last-line (purescript-indent-push-pos-offset rpurs-sign 2)))
;; "000000" 15=
(t (error "purescript-indent-other: %s impossible case" test ))))
purescript-indent-info)))
(defun purescript-indent-valdef-indentation (start end end-visible curr-line-type
indent-info)
"Find indentation information for a value definition."
(let ((purescript-indent-info indent-info))
(if (< start end-visible)
(cl-case curr-line-type
(empty (purescript-indent-empty start end end-visible indent-info))
(ident (purescript-indent-ident start end end-visible indent-info))
(guard (purescript-indent-guard start end end-visible indent-info))
(rpurs (purescript-indent-rpurs start end end-visible indent-info))
(comment (error "Comment indent should never happen"))
(other (purescript-indent-other start end end-visible indent-info)))
purescript-indent-info)))
(defun purescript-indent-line-indentation (line-start line-end end-visible
curr-line-type indent-info)
"Compute indentation info between LINE-START and END-VISIBLE.
Separate a line of program into valdefs between offside keywords
and find indentation info for each part."
(save-excursion
;; point is (already) at line-start
(cl-assert (eq (point) line-start))
(let ((purescript-indent-info indent-info)
(start (or (purescript-indent-in-comment line-start line-end)
(purescript-indent-in-string line-start line-end))))
(if start ; if comment at the end
(setq line-end start)) ; end line before it
;; loop on all parts separated by off-side-keywords
(while (and (re-search-forward purescript-indent-off-side-keywords-re
line-end t)
(not (or (purescript-indent-in-comment line-start (point))
(purescript-indent-in-string line-start (point)))))
(let ((beg-match (match-beginning 0)) ; save beginning of match
(end-match (match-end 0))) ; save end of match
;; Do not try to find indentation points if off-side-keyword at
;; the start...
(if (or (< line-start beg-match)
;; Actually, if we're looking at a "let" inside a "do", we
;; should add the corresponding indentation point.
(eq (char-after beg-match) ?l))
(setq purescript-indent-info
(purescript-indent-valdef-indentation line-start beg-match
end-visible
curr-line-type
purescript-indent-info)))
;; ...but keep the start of the line if keyword alone on the line
(if (= line-end end-match)
(purescript-indent-push-pos beg-match))
(setq line-start end-match)
(goto-char line-start)))
(purescript-indent-valdef-indentation line-start line-end end-visible
curr-line-type purescript-indent-info))))
(defun purescript-indent-layout-indent-info (start contour-line)
(let ((purescript-indent-info nil)
(curr-line-type (purescript-indent-type-at-point))
line-start line-end end-visible)
(save-excursion
(if (eq curr-line-type 'ident)
(let ; guess the type of line
((sep
(purescript-indent-separate-valdef
(point) (line-end-position))))
;; if the first ident is where or the start of a def
;; keep it in a global variable
(setq purescript-indent-current-line-first-ident
(if (string-match "where[ \t]*" (nth 1 sep))
(nth 1 sep)
(if (nth 5 sep) ; is there a rpurs-sign
(if (= (char-after (nth 5 sep)) ?\:) ;is it a typdef
"::" (nth 1 sep))
"")))))
(while contour-line ; explore the contour points
(setq line-start (pop contour-line))
(goto-char line-start)
(setq line-end (line-end-position))
(setq end-visible ; visible until the column of the
(if contour-line ; next contour point
(save-excursion
(move-to-column
(purescript-indent-point-to-col (car contour-line)))
(point))
line-end))
(unless (or (purescript-indent-open-structure start line-start)
(purescript-indent-in-comment start line-start))
(setq purescript-indent-info
(purescript-indent-line-indentation line-start line-end
end-visible curr-line-type
purescript-indent-info)))))
purescript-indent-info))
(defun purescript-indent-find-matching-start (regexp limit &optional pred start)
(let ((open (purescript-indent-open-structure limit (point))))
(if open (setq limit (1+ open))))
(unless start (setq start (point)))
(when (re-search-backward regexp limit t)
(let ((nestedcase (match-end 1))
(outer (or (purescript-indent-in-string limit (point))
(purescript-indent-in-comment limit (point))
(purescript-indent-open-structure limit (point))
(if (and pred (funcall pred start)) (point)))))
(cond
(outer
(goto-char outer)
(purescript-indent-find-matching-start regexp limit pred start))
(nestedcase
;; Nested case.
(and (purescript-indent-find-matching-start regexp limit pred)
(purescript-indent-find-matching-start regexp limit pred start)))
(t (point))))))
(defun purescript-indent-filter-let-no-in (start)
"Return non-nil if point is in front of a `let' that has no `in'.
START is the position of the presumed `in'."
;; We're looking at either `in' or `let'.
(when (looking-at "let")
(ignore-errors
(save-excursion
(forward-word 1)
(forward-comment (point-max))
(if (looking-at "{")
(progn
(forward-sexp 1)
(forward-comment (point-max))
(< (point) start))
;; Use the layout rule to see whether this let is already closed
;; without an `in'.
(let ((col (current-column)))
(while (progn (forward-line 1) (purescript-indent-back-to-indentation)
(< (point) start))
(when (< (current-column) col)
(setq col nil)
(goto-char start)))
(null col)))))))
(defun purescript-indent-comment (open start)
"Compute indent info for comments and text inside comments.
OPEN is the start position of the comment in which point is."
;; Ideally we'd want to guess whether it's commented out code or
;; whether it's text. Instead, we'll assume it's text.
(save-excursion
(if (= open (point))
;; We're actually just in front of a comment: align with following
;; code or with comment on previous line.
(let ((prev-line-info
(cond
((eq (char-after) ?\{) nil) ;Align as if it were code.
((and (forward-comment -1)
(> (line-beginning-position 3) open))
;; We're after another comment and there's no empty line
;; between us.
(list (list (purescript-indent-point-to-col (point)))))
(t nil)))) ;Else align as if it were code
;; Align with following code.
(forward-comment (point-max))
;; There are several possible indentation points for this code-line,
;; but the only valid indentation point for the comment is the one
;; that the user will select for the code-line. Obviously we can't
;; know that, so we just assume that the code-line is already at its
;; proper place.
;; Strictly speaking "assume it's at its proper place" would mean
;; we'd just use (current-column), but since this is using info from
;; lines further down and it's common to reindent line-by-line,
;; we'll align not with the current indentation, but with the
;; one that auto-indentation "will" select.
(append
prev-line-info
(let ((indent-info (save-excursion
(purescript-indent-indentation-info start)))
(col (current-column)))
;; Sort the indent-info so that the current indentation comes
;; out first.
(setq indent-info
(sort indent-info
(lambda (x y)
(<= (abs (- col (car x))) (abs (- col (car y)))))))
indent-info)))