-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopcaml-mode.el
1138 lines (1028 loc) · 37.6 KB
/
gopcaml-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
;;; gopcaml-mode.el --- Structural Ocaml Editing -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Kiran Gopinathan
;; Author: Kiran Gopinathan <[email protected]>
;; Keywords: languages, tools
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
;;;; Requirements and environment setup
;; load in core Gopcaml library
(cond
((and (boundp 'gopcaml-dev-mode) gopcaml-dev-mode)
;; if in dev mode,
(message "loading gopcaml-mode [dev]")
(require 'gopcaml
;; load dynamic module from the build directory
(expand-file-name "./_build/default/gopcaml.so"
(string-trim
(shell-command-to-string (format "dirname %s"
(or (and (boundp 'file) file)
(buffer-file-name (current-buffer))
)))))))
(t
(message "loading gopcaml-mode [normal]")
(let ((opam-lib (ignore-errors (car (process-lines "opam" "var" "lib")))))
(if (and opam-lib (file-directory-p opam-lib))
(require 'gopcaml (expand-file-name "./gopcaml-mode/gopcaml.so" opam-lib))
(error "Could not find opam - please make sure it is installed")
))))
;; load in emacs dependencies
(require 'subr-x)
;; load in merlin/ocp deps
(require 'ocp-indent)
(require 'merlin)
;;;; Customization
;;;;; Faces
(defgroup gopcaml-faces nil
"Faces for gopcaml mode."
:group 'gopcaml-mode
:group 'faces)
(defface gopcaml-highlight-face
'((t :inherit caml-types-expr-face))
"Face for highlighting expr."
:group 'gopcaml-faces)
(defface gopcaml-zipper-face
'((t (:background "dark slate gray")))
"Face for highlighting zipper."
:group 'gopcaml-faces)
;;;;; Options
(defcustom gopcaml-rebuild-delay 1
"Number of idling seconds before rebuilding gopcaml-state."
:type 'integer
:group 'gopcaml)
;;;; Local variables
(defvar-local gopcaml-temporary-highlight-overlays nil
"Maintains an the overlay used for single-element highlights.")
(defvar-local gopcaml-zipper-overlay nil
"Overlay used to highlight the zipper region.")
(defvar-local gopcaml-update-timer nil
"Timer object used to periodically update gopcaml state.")
(defvar-local gopcaml-expand-timer nil
"Timer object used to periodically expand the element under point")
(defvar-local gopcaml-zipper-mode-quit nil
"Function that can be called to quit the zipper mode.")
;;;; Helper functions
(defun gopcaml-remove-stored-overlays (&optional group)
"Remove stored overlays - optionally only those of gopcaml-kind GROUP."
(setq gopcaml-temporary-highlight-overlays
(remove-if (lambda (it) (null it))
(mapcar (lambda (it)
(if (or (not group)
(equal (overlay-get it 'gopcaml-kind)
group))
(delete-overlay it)
nil))
gopcaml-temporary-highlight-overlays))))
(defun gopcaml-store-overlays (overlay &optional group)
"Store an OVERLAY - optionally only those of gopcaml-kind GROUP."
(overlay-put overlay 'gopcaml-kind group)
(push overlay gopcaml-temporary-highlight-overlays))
(defun gopcaml-is-excluded-current-file ()
"Determines whether the current file has been designated as an
ignored file."
(let ((excluded-extensions
(or (and (boundp 'gopcaml-ignored-extensions) gopcaml-ignored-extensions)
'("mll" "mly")))
(file-extension (file-name-extension (buffer-file-name))))
(member file-extension excluded-extensions)))
;;;;; Highlighting functions
(defun gopcaml-temporarily-highlight-region (bounds &optional group face)
"Temporarily highlight region enclosed by BOUNDS using FACE.
removes all existing overlays of type GROUP if present."
(unless face
(setq face 'gopcaml-highlight-face))
;; when group is present, remove all existing overlays of the same group
(gopcaml-remove-stored-overlays group)
(lexical-let ((overlay (make-overlay (car bounds) (cdr bounds))))
(overlay-put overlay 'face face)
(gopcaml-store-overlays overlay group)
;; wait for user input
(unwind-protect
(sit-for 60) (gopcaml-remove-stored-overlays group))))
(defun gopcaml-temporarily-highlight-multiple-region (mbounds &optional group face)
"Temporarily highlight regions listed in MBOUNDS using FACE.
removes all existing overlays of type GROUP if present."
(unless face
(setq face 'gopcaml-highlight-face))
;; when group is present, remove all existing overlays of the same group
(gopcaml-remove-stored-overlays group)
(dolist (bounds mbounds)
(lexical-let ((overlay (make-overlay (car bounds) (cdr bounds))))
(overlay-put overlay 'face face)
(gopcaml-store-overlays overlay group)))
;; wait for user input
(unwind-protect
(sit-for 60) (gopcaml-remove-stored-overlays group))
)
(defun gopcaml-highlight-current-structure-item ()
"Highlight the structure-item enclosing the current point."
(interactive)
(let ((area (gopcaml-get-enclosing-bounds (point)))
start end)
(when area
(setq start (caar area))
(setq end (cadar area))
(gopcaml-temporarily-highlight-region (cons start end)))))
(defun gopcaml-highlight-dirty-region ()
"Highlight the dirty region."
(interactive)
(let ((area (car (gopcaml-get-dirty-region)))
start end)
(when area
(setq start (car area))
(setq end (cdr area))
(gopcaml-temporarily-highlight-region (cons start end)))))
;;;;; Zipper Movement
(defun move-gopcaml-zipper (zipper-fn &optional direction initial)
"Move the zipper using ZIPPER-FN in direction DIRECTION."
(let ((area (car (gopcaml-retrieve-zipper-bounds)))
start end (point (point)) (buf-name (buffer-name)))
(if area
(progn
(setq start (car area))
(setq end (cadr area))
(cond
((and
(not initial)
(equal point start)
(equal direction 'forward)
)
(goto-char end)
t)
((and
(not initial)
(equal point end)
(equal direction 'backward)
)
(goto-char start)
t)
((and
initial
(< point start)
(equal direction 'forward)
)
(goto-char start)
t)
((and
initial
(> point end)
(equal direction 'backward)
)
(goto-char end)
t)
(t
(setq area (car (funcall zipper-fn)))
(if area
(progn
(setq start (car area))
(setq end (cadr area))
(move-overlay gopcaml-zipper-overlay start end)
;; (let ((wind-start (window-start)) (wind-end (window-end)))
;; (if (and wind-start wind-end (< wind-start start wind-end))
;; (set-window-point (get-buffer-window buf-name) start)
;; (goto-char start))
;; )
(if (equal direction 'forward) (if (< end (window-end))
(goto-char end)
(goto-char start))
(if (> start (window-start))
(goto-char start)
(goto-char end)
))
t
)
nil))))
nil)))
(defun gopcaml-zipper-mode-and-move (operation &optional
zipper-constructor selection-mode direction skip-zipper-mode)
"Start gopcaml-zipper mode using ZIPPER-CONSTRUCTOR and perform OPERATION.
SELECTION-MODE indicates whether this was called in the context
of an existing selection, allowing for some slightly more
ergonomic movement when dealing with selections. DIRECTION
indicates the direction of the movement - allows skipping
whitespace in direction before constructing zipper.
SKIP-ZIPPER-MODE if set will prevent the activation zipper mode."
(interactive)
(if (not zipper-constructor)
(setq zipper-constructor #'gopcaml-build-zipper))
(let ((starting-pos (point))
(selection-active (or (region-active-p) selection-mode)))
(if (and selection-active (not (region-active-p)) (not skip-zipper-mode))
;; if selection active and not region active
;; i.e we're holding shift, but haven't selecting anything
(progn
(push-mark (point))
(activate-mark)))
;; if not already in zipper mode
(if (not gopcaml-zipper)
;; build zipper around point
(progn
(cond
((equal direction 'forward)
(skip-chars-forward " \n\t"))
((equal direction 'backward)
(skip-chars-backward " \n\t")))
(let ((area
(car (funcall zipper-constructor
(point)
(line-number-at-pos)
(list direction))))
start end overlay)
;; if successfull then perform operation
(if area
(progn
(setq start (car area))
(setq end (cadr area))
(cond
;; if skip-zipper mode
(skip-zipper-mode
;; just perform the operation and return the range
(setq area (car (funcall operation t)))
(gopcaml-delete-zipper)
(goto-char starting-pos)
area)
(t
(setq overlay (make-overlay start end))
(if (not selection-active)
;; (overlay-put overlay 'face 'gopcaml-selection-face)
(overlay-put overlay 'face 'gopcaml-zipper-face)
)
(overlay-put overlay 'gopcaml-kind 'zipper)
(setq gopcaml-zipper-overlay overlay)
(setq gopcaml-zipper-mode-pred t)
(setq gopcaml-zipper-mode-quit
(set-transient-map
(if (not selection-active)
gopcaml-zipper-mode-map
gopcaml-selection-zipper-mode-map)
t #'gopcaml-on-exit-zipper-mode))
;; (funcall operation t)
(cond
((equal direction 'forward)
(goto-char end)
)
((equal direction 'backward)
(goto-char start)
)))))
(gopcaml-delete-zipper)
(when skip-zipper-mode
(goto-char starting-pos))
nil
)))
;; otherwise just perfom operation
(cond
(skip-zipper-mode
;; just perform the operation and return the range
(let ((area (car (funcall operation t))))
(if gopcaml-zipper-mode-quit
(funcall gopcaml-zipper-mode-quit)
(gopcaml-delete-zipper))
area
))
(t (funcall operation nil)))
)))
(defun gopcaml-zipper-mark-mode ()
"Start gopcaml-zipper mark mode."
(interactive)
(let ((starting-pos (point))
(selection-active (and transient-mark-mode mark-active)))
;; if not already in zipper mode
(if selection-active (setq starting-pos (min (mark) (region-end) (region-beginning) starting-pos)))
(if (not gopcaml-zipper)
;; build zipper aronud point
(progn
(skip-chars-forward " \n\t")
(let ((area
(car (gopcaml-build-zipper
(point)
(line-number-at-pos)
nil)))
start end overlay)
;; if successfull then perform operation
(if area
(progn
(setq start (car area))
(setq end (cadr area))
(setq overlay (make-overlay start end))
(overlay-put overlay 'gopcaml-kind 'zipper)
(setq gopcaml-zipper-overlay overlay)
(setq gopcaml-zipper-mode-quit
(set-transient-map
gopcaml-mark-zipper-mode-map
t #'gopcaml-on-exit-zipper-mode))
;; (funcall operation t)
(if selection-active
(progn
(save-excursion
(goto-char (max end (region-end)))
(push-mark (point))
(activate-mark))
(goto-char (min start starting-pos)))
(progn
(save-excursion
(goto-char end)
(set-mark (point))
(activate-mark))
(goto-char (min start starting-pos))))
)
(gopcaml-delete-zipper)
)))
;; otherwise just perfom operation
(let ((area (car (gopcaml-move-zipper-up))))
(if area
(progn
(setq start (car area))
(setq end (cadr area))
(move-overlay gopcaml-zipper-overlay start end)
;; (funcall operation t)
(save-excursion
(goto-char (max end (region-end)))
(set-mark (point))
(activate-mark))
(goto-char (min start (region-beginning)))
)
)
))))
;;;;; User facing operations
(defun gopcaml-get-current-item-bounds ()
"Return the bounds of the current item."
(gopcaml-zipper-mode-and-move
(lambda (_) (gopcaml-move-zipper-up)) nil nil 'forward t)
)
(defun gopcaml-indent-current-sexp ()
"Indent the current sexp."
(interactive)
(let ((area (gopcaml-get-current-item-bounds)))
(when area
(ocp-indent-region (car area) (cadr area))))
)
(defun gopcaml-beginning-defun ()
"Move backwards to the beginning of the defun."
(interactive)
(let ((area (car (gopcaml-find-defun-start (point) (line-number-at-pos)))))
(if area
(progn (goto-char area))
nil)))
(defun gopcaml-end-defun ()
"Move forwards to the end of the defun."
(interactive)
(let ((area (car (gopcaml-find-defun-end (point) (line-number-at-pos)))))
(if area
(progn (goto-char area))
nil)))
(defun gopcaml-goto-nearest-letdef ()
"Move backwards to the nearest letdef."
(interactive)
(let ((area (car (gopcaml-find-nearest-letdef (point) (line-number-at-pos)))))
(if area
(progn
(push-mark)
(goto-char area))
nil)))
(defun gopcaml-goto-nearest-pattern ()
"Move backwards to the nearest letdef."
(interactive)
(let ((area (car (gopcaml-find-nearest-pattern (point) (line-number-at-pos)))))
(if area
(progn
(push-mark)
(goto-char area)
)
nil)))
(defun gopcaml-move-to-hole ()
"Move to (??) from the current point."
(interactive)
(let ((end (car (gopcaml-find-defun-end (point) (line-number-at-pos)))) (curr (point)))
(when (and end (search-forward "(??)" end t))
(if (equal (point) (+ curr 4)) (gopcaml-move-to-hole) (backward-char 4))
)
))
(defun gopcaml-move-backward-to-hole ()
"Move to (??) backward from the current point."
(interactive)
(let ((start (car (gopcaml-find-defun-start (point) (line-number-at-pos)))) (curr (point)))
(when (and start (search-backward "(??)" start t))
(if (equal (point) curr) (gopcaml-move-to-hole))
)
))
(defun gopcaml-forward-list-full (selection-mode)
"Move the zipper forwards (broadly from current point) in SELECTION-MODE."
(interactive)
(gopcaml-zipper-mode-and-move (lambda (initial)
(move-gopcaml-zipper
#'gopcaml-move-zipper-right
'forward
initial)
)
#'gopcaml-broadly-build-zipper
selection-mode
'forward))
(defun gopcaml-forward-list ()
"Move the zipper forwards (broadly from current point)."
(interactive) (gopcaml-forward-list-full nil))
(defun gopcaml-forward-list-selection ()
"Move the zipper forwards (broadly from current point)."
(interactive) (gopcaml-forward-list-full t))
(defun gopcaml-backward-list-full (selection-mode)
"Move the zipper backwards (broadly from current point) in SELECTION-MODE."
(interactive)
(gopcaml-zipper-mode-and-move (lambda (initial)
(move-gopcaml-zipper
#'gopcaml-move-zipper-left
'backward
initial))
#'gopcaml-broadly-build-zipper
selection-mode
'backward))
(defun gopcaml-backward-list ()
"Move the zipper backwards (broadly from current point)."
(interactive) (gopcaml-backward-list-full nil))
(defun gopcaml-backward-list-selection ()
"Move the zipper backwards (broadly from current point)."
(interactive) (gopcaml-backward-list-full t))
(defun gopcaml-backward-up-list-full (selection-mode)
"Move the zipper up (from expression at point) in SELECTION-MODE."
(interactive)
(gopcaml-zipper-mode-and-move (lambda (initial) (move-gopcaml-zipper
#'gopcaml-move-zipper-up
nil
initial))
nil
selection-mode
'backward))
(defun gopcaml-backward-up-list ()
"Move the zipper up (from expression at point)."
(interactive) (gopcaml-backward-up-list-full nil))
(defun gopcaml-backward-up-list-selection ()
"Move the zipper up (from expression at point)."
(interactive) (gopcaml-backward-up-list-full t))
(defun gopcaml-down-list-full (selection-mode)
"Move the zipper down (from expression at point) in SELECTION-MODE."
(interactive)
(gopcaml-zipper-mode-and-move (lambda (initial) (move-gopcaml-zipper
#'gopcaml-move-zipper-down
nil
initial))
nil
selection-mode
'forward))
(defun gopcaml-down-list ()
"Move the zipper dow (from expression at point)."
(interactive) (gopcaml-down-list-full nil))
(defun gopcaml-down-list-selection ()
"Move the zipper down (from expression at point)."
(interactive) (gopcaml-down-list-full t))
(defun gopcaml-forward-sexp-full (selection-mode &optional arg)
"Move the zipper forward ARG times (from expression at point) in SELECTION-MODE."
(interactive "^p")
(if (equal arg (- 1))
(gopcaml-zipper-mode-and-move (lambda (initial) (move-gopcaml-zipper
#'gopcaml-move-zipper-left
'backward
initial))
nil
selection-mode
'backward)
(gopcaml-zipper-mode-and-move (lambda (initial) (move-gopcaml-zipper
#'gopcaml-move-zipper-right
'forward
initial))
nil
selection-mode
'forward)))
(defun gopcaml-forward-sexp (&optional arg)
"Move the zipper down (from expression at point)."
(interactive "p") (gopcaml-forward-sexp-full nil arg))
(defun gopcaml-forward-sexp-selection (&optional arg)
"Move the zipper down (from expression at point)."
(interactive "p") (gopcaml-forward-sexp-full t arg))
(defun gopcaml-backward-sexp-full (selection-mode &optional arg)
"Move the zipper backwards (from expression at point) in SELECTION-MODE."
(interactive "p")
(gopcaml-zipper-mode-and-move (lambda (initial) (move-gopcaml-zipper
#'gopcaml-move-zipper-left
'backward
initial))
nil
selection-mode
'backward))
(defun gopcaml-backward-sexp (&optional arg)
"Move the zipper down (from expression at point)."
(interactive "p")
(gopcaml-backward-sexp-full nil arg))
(defun gopcaml-backward-sexp-selection (&optional arg)
"Move the zipper down (from expression at point)."
(interactive "p") (gopcaml-backward-sexp-full t arg))
(defun gopcaml-ensure-space-between-backward ()
"Ensures space between points."
(interactive)
(let ((start (point)) end
(start-line (line-number-at-pos))
end-line
(indent (current-column))
)
(skip-chars-backward " \n\t")
(setq end (point))
(setq end-line (line-number-at-pos))
(delete-region start end)
(insert "\n")
(insert "\n")
(insert (make-string indent 32))
(list (- (point) end) (- start end)
(- (line-number-at-pos) end-line)
(- start-line end-line))
))
(defun gopcaml-ensure-space-between-forward ()
"Ensures space between points."
(interactive)
(let ((start (point)) end
(start-line (line-number-at-pos))
end-line
indent
)
(skip-chars-forward " \n\t")
(setq indent (current-column))
(setq end (point))
(setq end-line (line-number-at-pos))
(delete-region start end)
(insert "\n")
(insert "\n")
(insert (make-string indent 32))
(list (- start (point)) (- start end)
(- start-line (line-number-at-pos)) (- start-line end-line))
))
(defun gopcaml-zipper-ensure-space ()
"Ensures-spacing between current element."
(if (and (car (gopcaml-zipper-is-top-level)) (car (gopcaml-zipper-is-top-level-parent)))
(let
((area (car (gopcaml-retrieve-zipper-bounds)))
start end
pre-change
post-change)
(when area
(setq start (car area))
(setq end (cadr area))
(goto-char end)
(setq post-change (gopcaml-ensure-space-between-forward))
(setq post-change
(list
(- (car post-change) (cadr post-change))
(- (caddr post-change) (cadddr post-change) ))
)
(goto-char start)
(setq pre-change (gopcaml-ensure-space-between-backward))
(setq pre-change
(list
(- (cadr pre-change) (car pre-change))
(- (cadddr pre-change) (caddr pre-change))))
(setq area (car (gopcaml-zipper-space-update
(car pre-change) (cadr pre-change)
(car post-change) (cadr post-change))))
(when area
(move-overlay gopcaml-zipper-overlay (car area) (cadr area))
t)))
nil))
(defun gopcaml-zipper-type ()
"Type region enclosed by zipper."
(interactive)
(when gopcaml-zipper-overlay
(let ((start (overlay-start gopcaml-zipper-overlay))
(end (overlay-end gopcaml-zipper-overlay)))
(lexical-let*
((substring (buffer-substring-no-properties start end))
(on-success (lambda (type) (merlin--type-display nil type nil)))
(on-error (lambda (err)
(let ((msg (assoc 'message err))
(typ (assoc type err)))
(cond ((and typ (equal (cdr typ) "parser"))
(message "Error: the content of the region failed to parse."))
(msg (message "Error: %s" (cdr msg)))
(t
(message "Unexpected error")))))))
(merlin--type-expression substring on-success on-error))
)))
(defun gopcaml-zipper-kill-region ()
"Kill the current item using the zipper."
(interactive)
(let ((area (car (gopcaml-begin-zipper-delete))) start end (buf-name (buffer-name)))
(when area
(setq start (car area))
(setq end (cadr area))
(kill-region start end)
(setq area (car (gopcaml-retrieve-zipper-bounds)))
(move-overlay gopcaml-zipper-overlay (car area) (cadr area))
(goto-char (car area)))))
(defun gopcaml-zipper-move-vertical (move-fn)
"Insert a let-def using the zipper MOVE-FN."
(interactive)
(let (area insert-pos start end text)
(setq area (car (funcall move-fn)))
(when area
(setq insert-pos (car area))
(setq start (cadr area))
(setq end (caddr area))
(setq text (buffer-substring-no-properties start end))
(delete-region start end)
(goto-char insert-pos)
(insert "\n\n")
(insert text)
(insert "\n")
(setq area (car (gopcaml-retrieve-zipper-bounds)))
(move-overlay gopcaml-zipper-overlay (car area) (cadr area))
(goto-char (car area))
)
))
(defun gopcaml-zipper-move-up ()
"Move the zipper element up."
(interactive)
(gopcaml-zipper-move-vertical #'gopcaml-zipper-move-elem-up))
(defun gopcaml-zipper-move-down ()
"Move the zipper down."
(interactive)
(gopcaml-zipper-move-vertical #'gopcaml-zipper-move-elem-down))
(defun gopcaml-zipper-insert-letdef ()
"Attempt to insert a let-def using the zipper."
(interactive)
(let (area (column (current-column)) text start-pos)
(setq area (car (gopcaml-zipper-insert-let-def-start column)))
(setq text (car area))
(setq start-pos (cdr area))
(goto-char start-pos)
(insert "\n\n")
(insert (make-string column 32))
(insert text)
(insert "\n")
(setq area (car (gopcaml-retrieve-zipper-bounds)))
(move-overlay gopcaml-zipper-overlay (car area) (cadr area))
(goto-char (car area))
))
(defun gopcaml-copy-region ()
"Copy region encompassed by the zipper."
(when gopcaml-zipper-overlay
(let ((start (overlay-start gopcaml-zipper-overlay))
(end (overlay-end gopcaml-zipper-overlay))
text)
(setq text (buffer-substring-no-properties start end))
(copy-region-as-kill start end)
(message "copied \"%s\" to kill-ring" (truncate-string-to-width
text 40 nil nil t)))))
(defun gopcaml-mode-insert-type-hole ()
"Insert a type hole at the cursor."
(interactive)
(insert "(??)"))
(defun gopcaml-zipper-swap (transform-fn)
"Swap current text using output from zipper function TRANSFORM-FN."
(let ((area (car (funcall transform-fn)))
region1-start
region1-end
region2-start
region2-end)
(when area
(setq region1-start (car area))
(setq region1-end (car (cdr area)))
(setq region2-start (car (cdr (cdr area))))
(setq region2-end (car (cdr (cdr (cdr area)))))
(let ((region1-str (buffer-substring region1-start region1-end))
(region2-str (buffer-substring region2-start region2-end)))
(when (< region2-start region1-start)
(cl-psetq region1-start region2-start
region1-end region2-end
region1-str region2-str
region2-start region1-start
region2-end region1-end
region2-str region1-str))
(progn
(delete-region region2-start region2-end)
(goto-char region2-start)
(insert region1-str))
(progn
(delete-region region1-start region1-end)
(goto-char region1-start)
(insert region2-str)))
(setq area (car (gopcaml-retrieve-zipper-bounds)))
(move-overlay gopcaml-zipper-overlay (car area) (cadr area))
(goto-char (car area))
)))
(defun gopcaml-zipper-transpose ()
"Transpose text using zipper."
(interactive)
(let ((area
(if (not gopcaml-zipper)
;; build zipper aronud point
(let
((area
(car (gopcaml-build-zipper (point) (line-number-at-pos) nil)))
start end overlay)
;; if successfull then perform operation
(if area
(progn
(setq start (car area))
(setq end (cadr area))
(setq overlay (make-overlay start end))
;; (overlay-put overlay 'face 'gopcaml-selection-face)
(overlay-put overlay 'face 'gopcaml-zipper-face)
(overlay-put overlay 'gopcaml-kind 'zipper)
(setq gopcaml-zipper-overlay overlay)
(setq gopcaml-zipper-mode-quit
(set-transient-map
gopcaml-zipper-mode-map
t #'gopcaml-on-exit-zipper-mode))
area)
(gopcaml-delete-zipper)
nil))
;; otherwise just perfom operation
(car (gopcaml-retrieve-zipper-bounds))
))
start end curr)
(if area
(progn
(setq curr (point))
(setq start (car area))
(setq end (cadr area))
(cond
((and start end (equal start curr))
(gopcaml-zipper-swap #'gopcaml-begin-zipper-swap-backwards)
(gopcaml-move-zipper-right)
(setq area (car (gopcaml-retrieve-zipper-bounds))))
((and start end (equal end curr))
(gopcaml-zipper-swap #'gopcaml-begin-zipper-swap-forwards)
(setq area (car (gopcaml-retrieve-zipper-bounds))))
(t
;; failed to set area
(setq area nil)
(gopcaml-on-exit-zipper-mode)
nil))
(when area
(move-overlay gopcaml-zipper-overlay (car area) (cadr area))
(goto-char (cadr area))
t))
nil)))
(defun gopcaml-zipper-move-forwards ()
"Move current element forwards at the same level."
(interactive)
(gopcaml-zipper-swap #'gopcaml-begin-zipper-swap-forwards))
(defun gopcaml-zipper-move-backwards ()
"Move current element backwards at the same level."
(interactive)
(gopcaml-zipper-swap #'gopcaml-begin-zipper-swap-backwards))
(defun gopcaml-state-filter (cmd)
"Determines whether a CMD can be carried out in current Gopcaml mode state."
(when (and gopcaml-state(gopcaml-state-available-filter))
cmd))
(defun gopcaml-zipper-use-current-and-quit (fn)
"Execute FN on region enclosing the selected zipper item and exits zipper mode."
(interactive)
(when (and gopcaml-zipper-overlay gopcaml-zipper-mode-quit)
(let ((area (car (gopcaml-retrieve-zipper-bounds))))
(when area
(funcall fn (car area) (cadr area))
;; sometimes the function being called will call operations
;; that end up quitting the transient mode automatically, so
;; we don't need to do anything
(if gopcaml-zipper-mode-quit
(funcall gopcaml-zipper-mode-quit))))))
(defun gopcaml-zipper-kill ()
"Deletes the current item and exits zipper mode."
(interactive)
(gopcaml-zipper-use-current-and-quit #'kill-region))
;;;; Mode maps
(defvar gopcaml-zipper-mode-map
(let ((gopcaml-map (make-sparse-keymap)))
(define-key gopcaml-map (kbd "C-M-f")
'(menu-item "" gopcaml-forward-sexp ))
(define-key gopcaml-map (kbd "C-M-b")
'(menu-item "" gopcaml-backward-sexp))
(define-key gopcaml-map (kbd "C-M-q")
#'gopcaml-indent-current-sexp)
(define-key gopcaml-map (kbd "C-M-u")
'(menu-item "" gopcaml-backward-up-list))
(define-key gopcaml-map (kbd "C-M-S-u")
'(menu-item "" gopcaml-zipper-move-up))
(define-key gopcaml-map (kbd "C-M-d")
'(menu-item "" gopcaml-down-list ))
(define-key gopcaml-map (kbd "C-M-S-d")
'(menu-item "" gopcaml-zipper-move-down))
(define-key gopcaml-map (kbd "C-M-n")
'(menu-item "" gopcaml-forward-list))
(define-key gopcaml-map (kbd "C-M-p")
'(menu-item "" gopcaml-backward-list))
(define-key gopcaml-map (kbd "C-M-w")
#'gopcaml-zipper-kill)
(define-key gopcaml-map (kbd "C-w")
#'gopcaml-zipper-kill)
(define-key gopcaml-map (kbd "M-w")
'(menu-item "" (lambda () (interactive) (gopcaml-copy-region))
))
(define-key gopcaml-map (kbd "C-M-S-n")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-move-forwards))
))
(define-key gopcaml-map (kbd "C-M-S-f")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-move-forwards))
))
(define-key gopcaml-map (kbd "C-M-S-p")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-move-backwards))
))
(define-key gopcaml-map (kbd "C-M-S-b")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-move-backwards))
))
(define-key gopcaml-map (kbd "C-M-t")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-transpose))
))
(define-key gopcaml-map (kbd "M-SPC")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-ensure-space))))
(define-key gopcaml-map (kbd "C-M-SPC")
'(menu-item "" (lambda () (interactive) (gopcaml-zipper-ensure-space))))
gopcaml-map)
"Map used when in zipper mode. ari ari!")
(defvar gopcaml-selection-zipper-mode-map
(let ((gopcaml-map (make-sparse-keymap)))
(define-key gopcaml-map (kbd "C-M-S-f")
'(menu-item "" gopcaml-forward-sexp-selection))
(define-key gopcaml-map (kbd "C-M-S-b")
'(menu-item "" gopcaml-backward-sexp-selection))
(define-key gopcaml-map (kbd "C-M-S-u")
'(menu-item "" gopcaml-backward-up-list-selection))
(define-key gopcaml-map (kbd "C-M-S-d")
'(menu-item "" gopcaml-down-list-selection))
(define-key gopcaml-map (kbd "C-M-S-n")
'(menu-item "" gopcaml-forward-list-selection))
(define-key gopcaml-map (kbd "C-M-S-p")
'(menu-item "" gopcaml-backward-list-selection))
gopcaml-map)
"Map used when in zipper mode for selections. ari ari!")
(defvar gopcaml-mark-zipper-mode-map
(let ((gopcaml-map (make-sparse-keymap)))
(define-key gopcaml-map
(kbd "C-M-@")
#'gopcaml-zipper-mark-mode)
(define-key gopcaml-map
(kbd "C-M-SPC")
#'gopcaml-zipper-mark-mode)
gopcaml-map)
"Map used when in zipper mode for marking expressions. ari ari!")
(defun gopcaml-on-exit-zipper-mode ()
"Exit gopcaml-zipper-mode."
(gopcaml-delete-zipper)
(delete-overlay gopcaml-zipper-overlay)
(setq gopcaml-zipper-overlay nil)
(setq gopcaml-zipper-mode-quit nil))
;; graciously taken from https://emacs.stackexchange.com/questions/12532/buffer-local-idle-timer
(defun run-with-local-idle-timer (secs repeat function &rest args)
"`run-with-idle-timer' but always run in the `current-buffer'.
Cancels itself, if this buffer was killed.
SECS is the periodicity of the timer.
REPEAT dictates whether the timer should be called repeatedly.
FUNCTION is the function to call on timer.
ARGS are parameters to pass to the function."
(let* (;; Chicken and egg problem.
(fns (make-symbol "local-idle-timer"))
(timer (apply 'run-with-idle-timer secs repeat fns args))
(fn `(lambda (&rest args)
(if (not (buffer-live-p ,(current-buffer)))
(cancel-timer ,timer)
(with-current-buffer ,(current-buffer)
(apply (function ,function) args))))))
(fset fns fn)
timer))
(defun gopcaml-before-change-remove-type-hole (beginning end)
"Before inserting text, attempt to remove type holes.
BEGINNING is the start of the edited text region.
END is the end of the edited text region."
(let ( (point (point)) element)
(when (and (equal beginning end) (< (+ point 4) (point-max)))
(setq element (buffer-substring-no-properties point (+ point 4)))
(if (equal "(??)" element)
(delete-region point (+ point 4))
))))
(defun gopcaml-type-hole-needed ()
"Check whether the current expression expansion needs a type hole."
(save-excursion
(skip-chars-forward " \t\n")