-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhi2.el
1263 lines (1133 loc) · 46.1 KB
/
hi2.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
;;; hi2.el --- indentation module for Haskell Mode
;; Copyright (C) 2013 Gergely Risko
;; Copyright (C) 2009 Kristof Bastiaensen
;; This file is heavily based on haskell-indentation.el by Kristof.
;; Author: Gergely Risko <[email protected]>
;; Version: 20141005.0
;; Keywords: indentation haskell
;; URL: https://github.com/errge/hi2
;; 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:
;; This is a modified version of haskell-indentation-mode. Currently
;; the semantic parser is not changed, but the UI of tab completion is
;; reworked. Most notably:
;; - DEL and C-d is not mapped: if you want to indent backwards, you can
;; use S-TAB. This means no random jumping on backspace anymore.
;;
;; - TAB steps to the right as before, but when the end is reached, it
;; starts going to the left instead of wrapping around.
;;
;; - TAB stays inside the code, instead of going to the beginning of the
;; line. As in pyhton-mode and perl-mode.
;;
;; - Region indentation common case is supported: TAB and S-TAB is simply
;; moving the whole region to the left/right by 1-column. Can be
;; pressed repeatedly.
;;
;; - The current indentations are shown as underscores in the current
;; line. So you have some visual indication on what's gonna happen.
;; Also useful while hacking on the parser and want to see the results.
;; Can be turned off by setting hi2-show-indentations to nil in your
;; init file or calling hi2-disable-show-indentations from the buffer.
;; If there are collisions with other overlay hacking modes
;; (e.g. fill-column-indicator), try to turn off
;; hi2-show-indentations-after-eol.
;;
;; - The buffer is not changed when indentation is not changed (so there
;; are no undo points created and no dirty flag in the buffer if
;; pressing TAB had no effect).
;;
;; - The code for all this is somewhat commented and cleaned.
;; There is ongoing development on making this better and I invite
;; everyone to participate (report bugs, fix issues, etc.):
;; https://github.com/errge/hi2
;;
;; Once this is stable and better in every way than the default
;; haskell-indentation-mode, I'm happy to merge it with haskell-mode,
;; but until this is moving fast, I want to keep it separate.
;;; Installation:
;; To turn indentation on for all Haskell buffers under Haskell mode
;; <http://www.haskell.org/haskell-mode/> add this to .emacs:
;;
;; (add-hook haskell-mode-hook 'turn-on-hi2)
;;
;; Otherwise, call `hi2-mode'.
;;; TODOs:
;; - hi2-find-indentations should always go to the beginning of the
;; line and compute the indentations there; for this we need to
;; adapt auto-fill and newline-and-indent,
;; - rename every def* to start with hi2-,
;; - research emacs parser writing, maybe we can have something more
;; maintainable than dinamically scoped variables; or write
;; comments at least.
;;; Code:
(require 'hl-line)
(require 'syntax)
(with-no-warnings (require 'cl))
(defvar hi2-dyn-first-position)
(defvar hi2-dyn-last-direction)
(defvar hi2-dyn-last-indentations)
(defgroup hi2 nil
"Haskell hi2 indentation."
:group 'haskell
:prefix "hi2-")
(defcustom hi2-show-indentations t
"If t the current line's indentation points will be showed as
underscore overlays in new haskell-mode buffers. Use
`hi2-enable-show-indentations' and `hi2-disable-show-indentations'
to switch the behavior for already existing buffers."
:type 'boolean
:group 'hi2)
(defcustom hi2-show-indentations-after-eol t
"If t, try to show indentation points after the end of line.
This requires strange overlay hacks and can collide with other
modes (e.g. fill-column-indicator)."
:type 'boolean
:group 'hi2)
(defcustom hi2-show-indentations-delay 0.1
"Number of seconds to wait before showing the indentations.
Since showing the indentations can be expensive on complicated
files, you should set this value higher than your key repeat
speed, so we'll never start computing indentations while you're
moving your cursor around.")
(defface hi2-show-normal-face
'((t :underline (:style wave)))
"Default face for indentations overlay."
:group 'hl2)
(defface hi2-show-hl-line-face
'((t :underline (:style wave) :inherit hl-line))
"Face used for indentations overlay after EOL if hl-line mode is enabled."
:group 'hl2)
(defcustom hi2-layout-offset 2
"Extra indentation to add before expressions in a haskell layout list."
:type 'integer
:group 'hi2)
(defcustom hi2-starter-offset 1
"Extra indentation after an opening keyword (e.g. let)."
:type 'integer
:group 'hi2)
(defcustom hi2-left-offset 2
"Extra indentation after an indentation to the left (e.g. after do)."
:type 'integer
:group 'hi2)
(defcustom hi2-ifte-offset 2
"Extra indentation after the keywords `if' `then' or `else'."
:type 'integer
:group 'hi2)
(defcustom hi2-where-pre-offset 2
"Extra indentation before the keyword `where'."
:type 'integer
:group 'hi2)
(defcustom hi2-where-post-offset 2
"Extra indentation after the keyword `where'."
:type 'integer
:group 'hi2)
(defconst hi2-mode-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap [?\r] 'hi2-newline-and-indent)
(define-key keymap [backtab] 'hi2-indent-backwards)
keymap))
;;;###autoload
(define-minor-mode hi2-mode
"Haskell indentation mode that deals with the layout rule.
It rebinds RET, DEL and BACKSPACE, so that indentations can be
set and deleted as if they were real tabs. It supports
autofill-mode."
:lighter " HI2"
:keymap hi2-mode-map
(kill-local-variable 'indent-line-function)
(kill-local-variable 'indent-region-function)
(kill-local-variable 'normal-auto-fill-function)
(when hi2-mode
(setq max-lisp-eval-depth (max max-lisp-eval-depth 600)) ;; set a higher limit for recursion
(set (make-local-variable 'indent-line-function) 'hi2-indent-line)
(set (make-local-variable 'indent-region-function) 'hi2-indent-region)
(set (make-local-variable 'normal-auto-fill-function) 'hi2-auto-fill-function)
(when hi2-show-indentations (hi2-enable-show-indentations))))
;;;###autoload
(defun turn-on-hi2 ()
"Turn on the hi2 minor mode."
(interactive)
(hi2-mode t))
(put 'parse-error
'error-conditions
'(error parse-error))
(put 'parse-error 'error-message "Parse error")
(defun parse-error (&rest args)
(signal 'parse-error (apply 'format args)))
(defmacro on-parse-error (except &rest body)
`(condition-case parse-error-string
(progn ,@body)
(parse-error
,except
(message "%s" (cdr parse-error-string)))))
(defvar haskell-literate)
(defun hi2-birdp (&optional force)
"Return t if this is a literate haskell buffer in bird style, nil otherwise.
If FORCE is non-nil, throw an error if this is a literate haskell
buffer, but the point is on a non-literate (e.g. comment) line."
(and (eq haskell-literate 'bird)
(if (not force)
t
(save-excursion
(progn
(beginning-of-line)
(when (/= (char-after) ?>) (error "literate bird command used on non-literate line"))
t)))))
;;---------------------------------------- UI starts here
(defun hi2-auto-fill-function ()
(when (> (current-column) fill-column)
(while (> (current-column) fill-column)
(skip-syntax-backward "-")
(skip-syntax-backward "^-"))
(let ((indent (car (last (hi2-find-indentations-safe)))))
(delete-horizontal-space)
(newline)
(when (hi2-birdp) (insert ">"))
(indent-to indent)
(end-of-line))))
(defun hi2-reindent-to (col &optional move)
"Reindent current line to COL, also move the point there if MOVE"
(let* ((cc (current-column))
(ci (hi2-current-indentation)))
;; only untabify if will do something, so we don't pollute undo
;; have to untabify outside of save-excursion, to get good point moving
(when (/= ci col)
(untabify (line-beginning-position) (line-end-position)))
(save-excursion
(move-to-column ci)
(if (<= ci col)
(insert-before-markers (make-string (- col ci) ? ))
(delete-char (- col ci))))
(when move
(move-to-column col))))
(defun hi2-indent-rigidly (start end arg)
"Indent all lines starting in the region sideways by ARG columns.
Called from a program, takes three arguments, START, END and ARG.
You can remove all indentation from a region by giving a large negative ARG.
Handles bird style literate haskell too."
(interactive "r\np")
(save-excursion
(goto-char end)
(let ((end-marker (point-marker)))
(goto-char start)
(or (bolp) (forward-line 0))
(untabify (point) end-marker)
(while (< (point) end-marker)
(let ((ci (hi2-current-indentation)))
(when (and t
(= (char-after) ?>))
(forward-char 1))
(skip-syntax-forward "-")
(unless (eolp)
(hi2-reindent-to (max 0 (+ ci arg))))
(forward-line 1)))
(move-marker end-marker nil))))
(defun hi2-current-indentation ()
"Column position of first non whitespace character in current line"
(save-excursion
(beginning-of-line)
(when (hi2-birdp) (forward-char))
(skip-syntax-forward "-")
(current-column)))
(defun hi2-bird-outside-codep ()
"True iff we are in bird literate mode, but outside of code"
(and (hi2-birdp)
(or (< (current-column) 2)
(save-excursion
(beginning-of-line)
(/= (char-after) ?>)))))
(defun hi2-delete-horizontal-space-and-newline ()
(delete-horizontal-space)
(newline))
(defun hi2-newline-and-indent ()
"Ran on C-j or RET"
(interactive)
;; On RET (or C-j), we:
;; - just jump to the next line if literate haskell, but outside code
(if (hi2-bird-outside-codep)
(hi2-delete-horizontal-space-and-newline)
;; - just jump to the next line if parse-error
(on-parse-error
(hi2-delete-horizontal-space-and-newline)
(let* ((cc (current-column))
(ci (hi2-current-indentation))
(indentations (hi2-find-indentations-safe)))
;; - jump to the next line and reindent to at the least same level
;; if parsing was OK
(skip-syntax-forward "-")
(hi2-delete-horizontal-space-and-newline)
(when (hi2-birdp) (insert "> "))
(hi2-reindent-to
(hi2-next-indentation (- ci 1) indentations 'nofail)
'move)))))
(defun hi2-next-indentation (col indentations &optional nofail)
"Find the leftmost indentation which is greater than COL.
Or returns the last indentation if there are no bigger ones and
NOFAIL is non-nil."
(when (null indentations) (error "hi2-next-indentation called with empty list"))
(or (find-if #'(lambda (i) (> i col)) indentations)
(when nofail (car (last indentations)))))
(defun hi2-previous-indentation (col indentations &optional nofail)
"Find the rightmost indentation which is less than COL."
(when (null indentations) (error "hi2-previous-indentation called with empty list"))
(let ((rev (reverse indentations)))
(or (find-if #'(lambda (i) (< i col)) rev)
(when nofail (car rev)))))
(defun hi2-indent-line ()
(interactive)
"Auto indentation on TAB.
Do nothing inside multiline comments and multiline strings.
Start enumerating the indentation points to the right. The user
can continue by repeatedly pressing TAB. When there is no more
indentation points to the right, we switch going to the left."
;; try to repeat
(when (not (hi2-indent-line-repeat))
(setq hi2-dyn-last-direction nil)
;; do nothing if we're inside a string or comment
(unless (save-excursion
(beginning-of-line)
(nth 8 (syntax-ppss)))
;; parse error is intentionally not catched here, it may come from
;; hi2-find-indentations-safe, but escapes the scope and aborts the
;; opertaion before any moving happens
(let* ((cc (current-column))
(ci (hi2-current-indentation))
(inds (save-excursion
(move-to-column ci)
(hi2-find-indentations-safe)))
(valid (memq ci inds))
(cursor-in-whitespace (< cc ci)))
;; can't happen right now, because of -safe, but we may want to have this in the future
;; (when (null inds)
;; (error "returned indentations empty, but no parse error"))
(if (and valid cursor-in-whitespace)
(move-to-column ci)
(hi2-reindent-to (hi2-next-indentation ci inds 'nofail) cursor-in-whitespace))
(setq hi2-dyn-last-direction 'right)
(setq hi2-dyn-first-position (hi2-current-indentation))
(setq hi2-dyn-last-indentations inds)))))
(defun hi2-indent-line-repeat ()
"Ran if the user repeatedly presses the TAB key"
(cond
((and (memq last-command '(indent-for-tab-command hi2-indent-backwards))
(eq hi2-dyn-last-direction 'region))
(let ((mark-even-if-inactive t))
(hi2-indent-rigidly (region-beginning) (region-end) 1))
t)
((and (eq last-command 'indent-for-tab-command)
(memq hi2-dyn-last-direction '(left right))
hi2-dyn-last-indentations)
(let* ((cc (current-column))
(ci (hi2-current-indentation)))
(if (eq hi2-dyn-last-direction 'left)
(hi2-reindent-to (hi2-previous-indentation ci hi2-dyn-last-indentations 'nofail))
;; right
(if (hi2-next-indentation ci hi2-dyn-last-indentations)
(hi2-reindent-to (hi2-next-indentation ci hi2-dyn-last-indentations 'nofail))
;; but failed, switch to left
(setq hi2-dyn-last-direction 'left)
;; and skip to the point where the user started pressing TABs.
;; except if there are <= 2 indentation points, because this
;; behavior is very confusing in that case
(when (< 2 (length hi2-dyn-last-indentations))
(hi2-reindent-to hi2-dyn-first-position))
(hi2-indent-line-repeat))))
t)
(t nil)))
(defun hi2-indent-region (start end)
(setq hi2-dyn-last-direction 'region)
(hi2-indent-rigidly start end 1)
(message "Press TAB or S-TAB again to indent the region more"))
(defun hi2-indent-backwards ()
"Indent the current line to the previous indentation point"
(interactive)
(cond
((and (memq last-command '(indent-for-tab-command hi2-indent-backwards))
(eq hi2-dyn-last-direction 'region))
(let ((mark-even-if-inactive t))
(hi2-indent-rigidly (region-beginning) (region-end) -1)))
((use-region-p)
(setq hi2-dyn-last-direction 'region)
(hi2-indent-rigidly (region-beginning) (region-end) -1)
(message "Press TAB or S-TAB again to indent the region more"))
(t
(setq hi2-dyn-last-direction nil)
(let* ((cc (current-column))
(ci (hi2-current-indentation))
(inds (save-excursion
(move-to-column ci)
(hi2-find-indentations-safe)))
(cursor-in-whitespace (< cc ci))
(pi (hi2-previous-indentation ci inds)))
(if (null pi)
;; if there are no more indentations to the left, just go to column 0
(hi2-reindent-to (car (hi2-first-indentation)) cursor-in-whitespace)
(hi2-reindent-to pi cursor-in-whitespace))))))
;;---------------------------------------- hi2 show indentations UI starts here
(defvar hi2-dyn-show-indentations
"Whether showing of indentation points is enabled in this buffer.")
(make-variable-buffer-local 'hi2-dyn-show-indentations)
(defvar hi2-dyn-overlays nil
"Overlays used by hi2-enable-show-indentations.")
(make-variable-buffer-local 'hi2-dyn-overlays)
(defun hi2-init-overlays (n)
"Makes sure that hi2-dyn-overlays contains at least N overlays."
(let* ((clen (length hi2-dyn-overlays))
(needed (- n clen)))
(dotimes (n needed hi2-dyn-overlays)
(setq hi2-dyn-overlays
(cons (make-overlay 1 1) hi2-dyn-overlays)))))
(defvar hi2-dyn-show-overlays-timer (timer-create)
"Timer used to schedule hi2-show-overlays. Scheduled when we
go to a new line and canceled in pre-command-hook if the user
very quickly starts a new command.")
(make-variable-buffer-local 'hi2-dyn-show-overlays-timer)
(defun hi2-show-overlays-cancel-timer ()
"Cancels hi2-dyn-show-overlays-timer."
(when hi2-dyn-show-overlays-timer
(cancel-timer hi2-dyn-show-overlays-timer)))
(defun hi2-show-overlays-schedule-timer ()
"Schedule a hi2-show-overlays run."
(setq hi2-dyn-show-overlays-timer
(run-with-timer hi2-show-indentations-delay nil #'hi2-show-overlays)))
(defvar hi2-dyn-show-overlays-cache '(-1 . nil)
"(line-beginning-position . indentations) at the previous
hi2-find-indentations-safe call inside hi2-show-overlays")
(make-variable-buffer-local 'hi2-dyn-show-overlays-cache)
(defun hi2-unshow-overlays ()
"Unshows all the overlays."
(mapc #'delete-overlay hi2-dyn-overlays))
(defun hi2-show-overlays-change-major-mode ()
(hi2-unshow-overlays)
(hi2-show-overlays-cancel-timer))
(defun hi2-show-overlays-pre-command ()
(hi2-unshow-overlays)
(hi2-show-overlays-cancel-timer))
(defun hi2-show-overlays-post-command ()
"Schedules a hi2-show-overlay run and deletes the overlays if
the line is different than previously, so the cache is not
usable. If the cache is usable, just uses the cache to show the
overlays."
(if (= (car hi2-dyn-show-overlays-cache)
(line-beginning-position))
; cache is usable
(progn
(hi2-show-overlays (cdr hi2-dyn-show-overlays-cache)))
; cache is not usable
(hi2-show-overlays-schedule-timer)))
(defun hi2-show-overlays (&optional cached-indentations)
"Put an underscore overlay at all the indentations points in
the current buffer."
(when (and (memq major-mode '(haskell-mode literate-haskell-mode))
(memq 'hi2-mode minor-mode-list)
hi2-dyn-show-indentations)
(save-excursion
(let* ((columns (progn
(end-of-line)
(current-column)))
(ci (hi2-current-indentation))
(allinds (or cached-indentations
(save-excursion
(move-to-column ci); XXX: remove when hi2-find-indentations is fixed
;; don't freak out on parse-error
(condition-case e
(hi2-find-indentations-safe)
(parse-error nil)))))
;; indentations that are easy to show
(inds (remove-if (lambda (i) (>= i columns)) allinds))
;; tricky indentations, that are after the current EOL
(overinds (member-if (lambda (i) (>= i columns)) allinds))
;; +1: leave space for an extra overlay to show overinds
(overlays (hi2-init-overlays (+ 1 (length inds)))))
(while inds
(move-to-column (car inds))
(and
;; if we jump in the middle of a tab, we will be misaligned
(= (current-column) (car inds))
;; if we jump to the beginning, we will look at the tab
(/= (char-after) '?\t)
;; and we only want to display indentations for spaces,
;; because otherwise they'll look messy
(progn
(overlay-put (car overlays) 'face 'hi2-show-normal-face)
(overlay-put (car overlays) 'after-string nil)
(move-overlay (car overlays) (point) (+ 1 (point)))
(setq overlays (cdr overlays))))
(setq inds (cdr inds)))
(when (and overinds
hi2-show-indentations-after-eol)
(let ((o (car overlays))
(s (make-string (+ 1 (- (car (last overinds)) columns)) ? )))
;; needed for the cursor to be in the good position, see:
;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-03/msg00079.html
(put-text-property 0 1 'cursor t s)
;; color the whole line ending overlay with hl-line face if needed
(when (or hl-line-mode global-hl-line-mode)
(put-text-property 0 (length s) 'face 'hl-line s))
;; put in the underlines at the correct positions
(dolist (i overinds)
(put-text-property
(- i columns) (+ 1 (- i columns))
'face (if (or hl-line-mode global-hl-line-mode)
'hi2-show-hl-line-face
'hi2-show-normal-face)
s))
(overlay-put o 'face nil)
(overlay-put o 'after-string s)
(end-of-line)
(move-overlay o (point) (point))))
(setq hi2-dyn-show-overlays-cache
(cons (line-beginning-position) allinds))))))
(defun hi2-enable-show-indentations ()
"Enable showing of indentation points in the current buffer."
(interactive)
(setq hi2-dyn-show-indentations t)
(add-hook 'change-major-mode-hook #'hi2-show-overlays-change-major-mode nil t)
(add-hook 'pre-command-hook #'hi2-show-overlays-pre-command nil t)
(add-hook 'post-command-hook #'hi2-show-overlays-post-command nil t))
(defun hi2-disable-show-indentations ()
"Disable showing of indentation points in the current buffer."
(interactive)
(setq hi2-dyn-show-indentations nil)
(remove-hook 'post-command-hook #'hi2-show-overlays-post-command t)
(hi2-unshow-overlays)
(remove-hook 'change-major-mode-hook #'hi2-show-overlays-change-major-mode t)
(remove-hook 'pre-command-hook #'hi2-show-overlays-pre-command t))
;;---------------------------------------- parser starts here
;; PARSER TODOS:
;; - why is there an indentation point at 2 after an import?
;; because you can write:
;;
;; import A
;; (a, b, c)
;;
;; But at least we should fix this:
;;
;; import A (a)
;; (b) <- why there is an indentation point at 2 here?
;;
;; and maybe have a setq for the first case
;; - why there are no indentation points at all before data in this file:
;; import A
;; data Foo
;; - there should be an indentation point at 2 in the second line:
;;
;; data Person = Person
;; { firstName :: !String -- ^ First name
;; , lastName :: !String -- ^ Last name
;; , age :: !Int -- ^ Age
;; } deriving (Eq, Show)
;;
;; see: https://github.com/errge/hi2/issues/4
;; - module export list indentation is inconsistent, see:
;; https://github.com/errge/hi2/issues/3
;; Dynamically scoped variables.
(defvar following-token)
(defvar current-token)
(defvar left-indent)
(defvar starter-indent)
(defvar current-indent)
(defvar layout-indent)
(defvar parse-line-number)
(defvar possible-indentations)
(defvar indentation-point)
(defun hi2-goto-least-indentation ()
(beginning-of-line)
(if (hi2-birdp)
(catch 'return
(while t
(when (/= (char-after) ?>)
(forward-line)
(forward-char 2)
(throw 'return nil))
(let ((ps (nth 8 (syntax-ppss))))
(when ps ;; inside comment or string
(goto-char ps)
(beginning-of-line)))
(when (and (>= 2 (hi2-current-indentation))
(not (looking-at ">\\s-*$")))
(forward-char 2)
(throw 'return nil))
(when (bobp)
(forward-char 2)
(throw 'return nil))
(forward-line -1)))
;; not bird style
(catch 'return
(while (not (bobp))
(forward-comment (- (buffer-size)))
(beginning-of-line)
(let ((ps (nth 8 (syntax-ppss))))
(when ps ;; inside comment or string
(goto-char ps)))
(when (= 0 (hi2-current-indentation))
(throw 'return nil))))
(beginning-of-line)
(when (bobp)
(forward-comment (buffer-size)))))
(defun hi2-parse-to-indentations ()
(save-excursion
(skip-syntax-forward "-")
(let ((indentation-point (point))
(layout-indent 0)
(parse-line-number 0)
(current-indent hi2-layout-offset)
(starter-indent hi2-layout-offset)
(left-indent hi2-layout-offset)
(case-fold-search nil)
current-token
following-token
possible-indentations)
(hi2-goto-least-indentation)
(if (<= indentation-point (point))
(hi2-first-indentation)
(setq current-token (hi2-peek-token))
(catch 'parse-end
(hi2-toplevel)
(unless (eq current-token 'end-tokens)
(parse-error "Illegal token: %s" current-token)))
possible-indentations))))
(defun hi2-first-indentation ()
(if (hi2-birdp) '(2) '(0)))
(defun hi2-find-indentations ()
(let ((ppss (syntax-ppss)))
(cond
((nth 3 ppss)
(hi2-first-indentation))
((nth 4 ppss)
(if (save-excursion
(and (skip-syntax-forward "-")
(eolp)
(not (> (forward-line 1) 0))
(not (nth 4 (syntax-ppss)))))
(hi2-parse-to-indentations)
(hi2-first-indentation)))
(t
(hi2-parse-to-indentations)))))
;; XXX: this is a hack, the parser shouldn't return nil without parse-error
(defun hi2-find-indentations-safe ()
(let ((ret (hi2-find-indentations)))
(if ret
ret
(hi2-first-indentation))))
(defconst hi2-unicode-tokens
'(("→" . "->") ;; #x2192 RIGHTWARDS ARROW
("∷" . "::") ;; #x2237 PROPORTION
("←" . "<-") ;; #x2190 LEFTWARDS ARROW
("⇒" . "=>") ;; #x21D2 RIGHTWARDS DOUBLE ARROW
("∀" . "forall") ;; #x2200 FOR ALL
("↢" . "-<") ;; #x2919 LEFTWARDS ARROW-TAIL
("↣" . ">-") ;; #x291A RIGHTWARDS ARROW-TAIL
("⤛" . "-<<") ;; #x291B LEFTWARDS DOUBLE ARROW-TAIL
("⤜" . ">>-") ;; #x291C RIGHTWARDS DOUBLE ARROW-TAIL
("★" . "*")) ;; #x2605 BLACK STAR
"Translation dictionary from UnicodeSyntax tokens to their ASCII representation.")
(defconst hi2-toplevel-list
'(("module" . hi2-module)
("data" . (lambda () (hi2-statement-right #'hi2-data)))
("type" . (lambda () (hi2-statement-right #'hi2-data)))
("newtype" . (lambda () (hi2-statement-right #'hi2-data)))
("class" . hi2-class-declaration)
("instance" . hi2-class-declaration )))
(defconst hi2-type-list
'(("::" . (lambda () (hi2-with-starter
(lambda () (hi2-separated #'hi2-type "->" nil)) nil)))
("(" . (lambda () (hi2-list #'hi2-type
")" "," nil)))
("[" . (lambda () (hi2-list #'hi2-type
"]" "," nil)))
("{" . (lambda () (hi2-list #'hi2-type
"}" "," nil)))))
(defconst hi2-expression-list
'(("data" . hi2-data)
("type" . hi2-data)
("newtype" . hi2-data)
("if" . (lambda () (hi2-with-starter #'hi2-if-maybe-multiwayif nil)))
("let" . (lambda () (hi2-phrase
'(hi2-declaration-layout
"in" hi2-expression))))
("do" . (lambda () (hi2-with-starter
#'hi2-expression-layout nil)))
("mdo" . (lambda () (hi2-with-starter
#'hi2-expression-layout nil)))
("rec" . (lambda () (hi2-with-starter
#'hi2-expression-layout nil)))
("case" . (lambda () (hi2-phrase
'(hi2-expression
"of" hi2-case-layout))))
("\\" . (lambda () (hi2-with-starter
#'hi2-lambda-maybe-lambdacase nil)))
("proc" . (lambda () (hi2-phrase
'(hi2-expression
"->" hi2-expression))))
("where" . (lambda () (hi2-with-starter
#'hi2-declaration-layout nil t)))
("::" . (lambda () (hi2-with-starter
(lambda () (hi2-separated #'hi2-type "->" nil)) nil)))
("=" . (lambda () (hi2-statement-right #'hi2-expression)))
("<-" . (lambda () (hi2-statement-right #'hi2-expression)))
("(" . (lambda () (hi2-list #'hi2-expression
")" '(list "," "->") nil)))
("[" . (lambda () (hi2-list #'hi2-expression
"]" "," "|")))
("{" . (lambda () (hi2-list #'hi2-expression
"}" "," nil)))))
(defun hi2-expression-layout ()
(hi2-layout #'hi2-expression))
(defun hi2-declaration-layout ()
(hi2-layout #'hi2-declaration))
(defun hi2-case-layout ()
(hi2-layout #'hi2-case))
;; After a lambda (backslash) there are two possible cases:
;; - the new lambdacase expression, that can be recognized by the
;; next token being "case",
;; - or simply an anonymous function definition in the form of
;; "expression -> expression".
(defun hi2-lambda-maybe-lambdacase ()
(if (string= current-token "case")
(hi2-with-starter
#'hi2-case-layout nil)
(hi2-phrase-rest
'(hi2-expression "->" hi2-expression))))
(defun hi2-if-maybe-multiwayif ()
(cond ((string= current-token "|")
;; Multi-way if
(hi2-implicit-multiwayif-layout #'hi2-multiwayif-phrase))
((string= current-token "{")
;; Multi-way if with explicit layout
(hi2-layout #'hi2-multiwayif-phrase))
(t
;; Regular if
(hi2-phrase-rest '(hi2-expression "then" hi2-expression "else" hi2-expression)))
))
(defun hi2-multiwayif-phrase ()
(cond ((string= current-token "|")
(hi2-phrase '(hi2-expression "->" hi2-expression)))
))
;; TODO(klao): this is an almost verbatim copy of
;; hi2-implicit-layout-list, I was just too afraid to touch that one.
(defun hi2-implicit-multiwayif-layout (parser)
(let* ((layout-indent (current-column))
(current-indent (current-column))
(left-indent (current-column)))
(catch 'return
(while t
(let ((left-indent left-indent))
(funcall parser))
(cond ((member current-token '(layout-next ";"))
(hi2-read-next-token))
((eq current-token 'end-tokens)
(when (or (hi2-expression-token following-token)
(member following-token '(";" "|")))
(hi2-add-layout-indent))
(throw 'return nil))
(t (throw 'return nil))))))
;; put hi2-read-next-token outside the current-indent definition
;; so it will not return 'layout-end again
(when (eq current-token 'layout-end)
(hi2-read-next-token))) ;; leave layout at 'layout-end or illegal token
(defun hi2-fundep ()
(hi2-with-starter
(lambda () (hi2-separated
#'hi2-fundep1 "," nil))
nil))
(defun hi2-fundep1 ()
(let ((current-indent (current-column)))
(while (member current-token '(value "->"))
(hi2-read-next-token))
(when (and (eq current-token 'end-tokens)
(member following-token '(value "->")))
(hi2-add-indentation current-indent))))
(defun hi2-toplevel ()
(hi2-layout
(lambda ()
(let ((parser (assoc current-token hi2-toplevel-list)))
(if parser
(funcall (cdr parser))
(hi2-declaration))))))
(defun hi2-type ()
(let ((current-indent (current-column)))
(catch 'return
(while t
(cond
((member current-token '(value operator "->"))
(hi2-read-next-token))
((eq current-token 'end-tokens)
(when (member following-token
'(value operator no-following-token
"->" "(" "[" "{" "::"))
(hi2-add-indentation current-indent))
(throw 'return nil))
(t (let ((parser (assoc current-token hi2-type-list)))
(if (not parser)
(throw 'return nil)
(funcall (cdr parser))))))))))
(defun hi2-data ()
(hi2-with-starter
(lambda ()
(when (string= current-token "instance")
(hi2-read-next-token))
(hi2-type)
(cond ((string= current-token "=")
(hi2-with-starter
(lambda () (hi2-separated #'hi2-type "|" "deriving"))
nil))
((string= current-token "where")
(hi2-with-starter
#'hi2-expression-layout nil))))
nil))
(defun hi2-class-declaration ()
(hi2-with-starter
(lambda ()
(hi2-type)
(when (string= current-token "|")
(hi2-fundep))
(when (string= current-token "where")
(hi2-with-starter
#'hi2-expression-layout nil)))
nil))
(defun hi2-module ()
(hi2-with-starter
(lambda ()
(let ((current-indent (current-column)))
(hi2-read-next-token)
(when (string= current-token "(")
(hi2-list
#'hi2-module-export
")" "," nil))
(when (eq current-token 'end-tokens)
(hi2-add-indentation current-indent)
(throw 'parse-end nil))
(when (string= current-token "where")
(hi2-read-next-token)
(when (eq current-token 'end-tokens)
(hi2-add-layout-indent)
(throw 'parse-end nil))
(hi2-layout #'hi2-toplevel))))
nil))
(defun hi2-module-export ()
(cond ((string= current-token "module")
(let ((current-indent (current-column)))
(hi2-read-next-token)
(cond ((eq current-token 'end-tokens)
(hi2-add-indentation current-indent))
((eq current-token 'value)
(hi2-read-next-token)))))
(t (hi2-type))))
(defun hi2-list (parser end sep stmt-sep)
(hi2-with-starter
`(lambda () (hi2-separated #',parser
,sep
,stmt-sep))
end))
(defun hi2-with-starter (parser end &optional where-expr?)
(let ((starter-column (current-column))
(current-indent current-indent)
(left-indent (if (= (current-column) (hi2-current-indentation))
(current-column) left-indent)))
(hi2-read-next-token)
(when (eq current-token 'end-tokens)
(if (equal following-token end)
(hi2-add-indentation starter-column)
(if where-expr?
(hi2-add-where-post-indent left-indent)
(hi2-add-indentation
(+ left-indent hi2-left-offset))))
(throw 'parse-end nil))
(let* ((current-indent (current-column))
(starter-indent (min starter-column current-indent))
(left-indent (if end (+ current-indent hi2-starter-offset)
left-indent)))
(funcall parser)
(cond ((eq current-token 'end-tokens)
(when (equal following-token end)
(hi2-add-indentation starter-indent))
(when end (throw 'parse-end nil))) ;; add no indentations
((equal current-token end)
(hi2-read-next-token)) ;; continue
(end (parse-error "Illegal token: %s" current-token))))))
(defun hi2-case ()
(hi2-expression)
(cond ((eq current-token 'end-tokens)
(hi2-add-indentation current-indent))
((string= current-token "|")
(hi2-with-starter
(lambda () (hi2-separated #'hi2-case "|" nil))
nil))
((string= current-token "->")
(hi2-statement-right #'hi2-expression))
;; otherwise fallthrough
))
(defun hi2-statement-right (parser)
(hi2-read-next-token)
(when (eq current-token 'end-tokens)
(hi2-add-indentation
(+ left-indent hi2-left-offset))
(throw 'parse-end nil))
(let ((current-indent (current-column)))
(funcall parser)))
(defun hi2-simple-declaration ()
(hi2-expression)
(cond ((string= current-token "=")
(hi2-statement-right #'hi2-expression))
((string= current-token "::")
(hi2-statement-right #'hi2-type))
((and (eq current-token 'end-tokens)
(string= following-token "="))
(hi2-add-indentation current-indent)
(throw 'parse-end nil))))
(defun hi2-declaration ()
(hi2-expression)
(cond ((string= current-token "|")
(hi2-with-starter
(lambda () (hi2-separated #'hi2-expression "," "|"))
nil))
((eq current-token 'end-tokens)
(when (member following-token '("|" "=" "::" ","))
(hi2-add-indentation current-indent)
(throw 'parse-end nil)))))