forked from ocaml/caml-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caml.el
1868 lines (1632 loc) · 67.9 KB
/
caml.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
;;; caml.el --- Caml mode for GNU Emacs -*- lexical-binding: t; -*-
;; Copyright (C) 1997-2017 Institut National de Recherche en Informatique et en Automatique.
;; Author: Jacques Garrigue <[email protected]>
;; Ian T Zimmerman <[email protected]>
;; Damien Doligez <[email protected]>
;; Maintainer: Christophe Troestler <[email protected]>
;; Created: July 1993
;; Package-Requires: ((emacs "24.3"))
;; Version: 4.9
;; Keywords: OCaml
;; Homepage: https://github.com/ocaml/caml-mode
;; 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 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; A major mode for editing OCaml code (see <http://ocaml.org/>) in Emacs.
;; Some of its major features include:
;; - syntax highlighting (font lock);
;; - automatic indentation;
;; - querying the type of expressions (using compiler generated annot files);
;; - running an OCaml REPL within Emacs;
;; - scans declarations and places them in a menu.
;; The original indentation code was the work of Ian T Zimmerman and
;; was adapted for OCaml by Jacques Garrigue in July 1997.
;;; Code:
;;user customizable variables
(defvar caml-quote-char "'"
"*Quote for character constants. \"'\" for OCaml, \"`\" for Caml-Light.")
(defvar caml-imenu-enable nil
"*Enable Imenu support.")
(defvar caml-mode-indentation 2
"*Used for \\[caml-unindent-command].")
(defvar caml-lookback-limit 5000
"*How far to look back for syntax things in caml mode.")
(defvar caml-max-indent-priority 8
"*Bounds priority of operators permitted to affect caml indentation.
Priorities are assigned to `interesting' caml operators as follows:
all keywords 0 to 7 8
type, val, ... + 0 7
:: ^ 6
@ 5
:= <- 4
if 3
fun, let, match ... 2
module 1
opening keywords 0.")
(defvar caml-apply-extra-indent 2
"*How many spaces to add to indentation for an application in caml mode.")
(make-variable-buffer-local 'caml-apply-extra-indent)
(defvar caml-begin-indent 2
"*How many spaces to indent from a \"begin\" keyword in caml mode.")
(make-variable-buffer-local 'caml-begin-indent)
(defvar caml-class-indent 2
"*How many spaces to indent from a \"class\" keyword in caml mode.")
(make-variable-buffer-local 'caml-class-indent)
(defvar caml-exception-indent 2
"*How many spaces to indent from an \"exception\" keyword in caml mode.")
(make-variable-buffer-local 'caml-exception-indent)
(defvar caml-for-indent 2
"*How many spaces to indent from a \"for\" keyword in caml mode.")
(make-variable-buffer-local 'caml-for-indent)
(defvar caml-fun-indent 2
"*How many spaces to indent from a \"fun\" keyword in caml mode.")
(make-variable-buffer-local 'caml-fun-indent)
(defvar caml-function-indent 4
"*How many spaces to indent from a \"function\" keyword in caml mode.")
(make-variable-buffer-local 'caml-function-indent)
(defvar caml-if-indent 2
"*How many spaces to indent from an \"if\" keyword in caml mode.")
(make-variable-buffer-local 'caml-if-indent)
(defvar caml-if-else-indent 0
"*How many spaces to indent from an \"if .. else\" line in caml mode.")
(make-variable-buffer-local 'caml-if-else-indent)
(defvar caml-inherit-indent 2
"*How many spaces to indent from an \"inherit\" keyword in caml mode.")
(make-variable-buffer-local 'caml-inherit-indent)
(defvar caml-initializer-indent 2
"*How many spaces to indent from an \"initializer\" keyword in caml mode.")
(make-variable-buffer-local 'caml-initializer-indent)
(defvar caml-include-indent 2
"*How many spaces to indent from an \"include\" keyword in caml mode.")
(make-variable-buffer-local 'caml-include-indent)
(defvar caml-let-indent 2
"*How many spaces to indent from a \"let\" keyword in caml mode.")
(make-variable-buffer-local 'caml-let-indent)
(defvar caml-let-in-indent 0
"*How many spaces to indent from a \"let .. in\" keyword in caml mode.")
(make-variable-buffer-local 'caml-let-in-indent)
(defvar caml-match-indent 2
"*How many spaces to indent from a \"match\" keyword in caml mode.")
(make-variable-buffer-local 'caml-match-indent)
(defvar caml-method-indent 2
"*How many spaces to indent from a \"method\" keyword in caml mode.")
(make-variable-buffer-local 'caml-method-indent)
(defvar caml-module-indent 2
"*How many spaces to indent from a \"module\" keyword in caml mode.")
(make-variable-buffer-local 'caml-module-indent)
(defvar caml-object-indent 2
"*How many spaces to indent from an \"object\" keyword in caml mode.")
(make-variable-buffer-local 'caml-object-indent)
(defvar caml-of-indent 2
"*How many spaces to indent from an \"of\" keyword in caml mode.")
(make-variable-buffer-local 'caml-of-indent)
(defvar caml-parser-indent 4
"*How many spaces to indent from a \"parser\" keyword in caml mode.")
(make-variable-buffer-local 'caml-parser-indent)
(defvar caml-sig-indent 2
"*How many spaces to indent from a \"sig\" keyword in caml mode.")
(make-variable-buffer-local 'caml-sig-indent)
(defvar caml-struct-indent 2
"*How many spaces to indent from a \"struct\" keyword in caml mode.")
(make-variable-buffer-local 'caml-struct-indent)
(defvar caml-try-indent 2
"*How many spaces to indent from a \"try\" keyword in caml mode.")
(make-variable-buffer-local 'caml-try-indent)
(defvar caml-type-indent 4
"*How many spaces to indent from a \"type\" keyword in caml mode.")
(make-variable-buffer-local 'caml-type-indent)
(defvar caml-val-indent 2
"*How many spaces to indent from a \"val\" keyword in caml mode.")
(make-variable-buffer-local 'caml-val-indent)
(defvar caml-while-indent 2
"*How many spaces to indent from a \"while\" keyword in caml mode.")
(make-variable-buffer-local 'caml-while-indent)
(defvar caml-::-indent 2
"*How many spaces to indent from a \"::\" operator in caml mode.")
(make-variable-buffer-local 'caml-::-indent)
(defvar caml-@-indent 2
"*How many spaces to indent from a \"@\" operator in caml mode.")
(make-variable-buffer-local 'caml-@-indent)
(defvar caml-:=-indent 2
"*How many spaces to indent from a \":=\" operator in caml mode.")
(make-variable-buffer-local 'caml-:=-indent)
(defvar caml-<--indent 2
"*How many spaces to indent from a \"<-\" operator in caml mode.")
(make-variable-buffer-local 'caml-<--indent)
(defvar caml-->-indent 2
"*How many spaces to indent from a \"->\" operator in caml mode.")
(make-variable-buffer-local 'caml-->-indent)
(defvar caml-lb-indent 2
"*How many spaces to indent from a \"\[\" operator in caml mode.")
(make-variable-buffer-local 'caml-lb-indent)
(defvar caml-lc-indent 2
"*How many spaces to indent from a \"\{\" operator in caml mode.")
(make-variable-buffer-local 'caml-lc-indent)
(defvar caml-lp-indent 1
"*How many spaces to indent from a \"\(\" operator in caml mode.")
(make-variable-buffer-local 'caml-lp-indent)
(defvar caml-and-extra-indent nil
"*Extra indent for caml lines starting with the \"and\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-and-extra-indent)
(defvar caml-do-extra-indent nil
"*Extra indent for caml lines starting with the \"do\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-do-extra-indent)
(defvar caml-done-extra-indent nil
"*Extra indent for caml lines starting with the \"done\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-done-extra-indent)
(defvar caml-else-extra-indent nil
"*Extra indent for caml lines starting with the \"else\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-else-extra-indent)
(defvar caml-end-extra-indent nil
"*Extra indent for caml lines starting with the \"end\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-end-extra-indent)
(defvar caml-in-extra-indent nil
"*Extra indent for caml lines starting with the \"in\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-in-extra-indent)
(defvar caml-then-extra-indent nil
"*Extra indent for caml lines starting with the \"then\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-then-extra-indent)
(defvar caml-to-extra-indent -1
"*Extra indent for caml lines starting with the \"to\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-to-extra-indent)
(defvar caml-with-extra-indent nil
"*Extra indent for caml lines starting with the \"with\" keyword.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-with-extra-indent)
(defvar caml-comment-indent 3
"*Indent inside comments.")
(make-variable-buffer-local 'caml-comment-indent)
(defvar caml-|-extra-indent -2
"*Extra indent for caml lines starting with the | operator.
Usually negative. nil is align on master.")
(make-variable-buffer-local 'caml-|-extra-indent)
(defvar caml-rb-extra-indent -2
"*Extra indent for caml lines starting with ].
Usually negative. nil is align on master.")
(defvar caml-rc-extra-indent -2
"*Extra indent for caml lines starting with }.
Usually negative. nil is align on master.")
(defvar caml-rp-extra-indent -1
"*Extra indent for caml lines starting with ).
Usually negative. nil is align on master.")
(defvar caml-electric-indent t
"*Non-nil means electrically indent lines starting with |, ] or }.
Many people find electric keys irritating, so you can disable them if
you are one.")
(defvar caml-electric-close-vector t
"*Non-nil means electrically insert a | before a vector-closing ].
Many people find electric keys irritating, so you can disable them if
you are one. You should probably have this on, though, if you also
have `caml-electric-indent' on, which see.")
;;code
(defvar caml-shell-active nil
"Non nil when a subshell is running.")
(defvar caml-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "|" 'caml-electric-pipe)
(define-key map "}" 'caml-electric-pipe)
(define-key map "]" 'caml-electric-rb)
(define-key map "\t" 'caml-indent-command)
(define-key map [backtab] 'caml-unindent-command)
;;itz 04-21-96 instead of defining a new function, use defadvice
;;that way we get out effect even when we do \C-x` in compilation buffer
;; (define-key map "\C-x`" 'caml-next-error)
;; caml-types
(define-key map [?\C-c?\C-t] 'caml-types-show-type) ; "type"
(define-key map [?\C-c?\C-f] 'caml-types-show-call) ; "function"
(define-key map [?\C-c?\C-l] 'caml-types-show-ident) ; "let"
;; must be a mouse-down event. Can be any button and any prefix
(define-key map [?\C-c down-mouse-1] 'caml-types-explore)
;; caml-help
(define-key map [?\C-c?i] 'ocaml-add-path)
(define-key map [?\C-c?\]] 'ocaml-close-module)
(define-key map [?\C-c?\[] 'ocaml-open-module)
(define-key map [?\C-c?\C-h] 'caml-help)
(define-key map [?\C-c?\t] 'caml-complete)
;; others
(define-key map "\C-cb" 'caml-insert-begin-form)
(define-key map "\C-cf" 'caml-insert-for-form)
(define-key map "\C-ci" 'caml-insert-if-form)
(define-key map "\C-cl" 'caml-insert-let-form)
(define-key map "\C-cm" 'caml-insert-match-form)
(define-key map "\C-ct" 'caml-insert-try-form)
(define-key map "\C-cw" 'caml-insert-while-form)
(define-key map "\C-c`" 'caml-goto-phrase-error)
(define-key map "\C-c\C-a" 'caml-find-alternate-file)
(define-key map "\C-c\C-c" 'compile)
(define-key map "\C-c\C-e" 'caml-eval-phrase)
(define-key map "\C-c\C-[" 'caml-backward-to-less-indent)
(define-key map "\C-c\C-]" 'caml-forward-to-less-indent)
(define-key map "\C-c\C-q" 'caml-indent-phrase)
(define-key map "\C-c\C-r" 'caml-eval-region)
(define-key map "\C-c\C-s" 'caml-show-subshell)
(define-key map "\M-\C-h" 'caml-mark-phrase)
(define-key map "\M-\C-q" 'caml-indent-phrase)
(define-key map "\M-\C-x" 'caml-eval-phrase)
(let ((menu (make-sparse-keymap "Caml"))
(forms (make-sparse-keymap "Forms")))
(define-key map "\C-c\C-d" 'caml-show-imenu)
(define-key map [menu-bar] (make-sparse-keymap))
(define-key map [menu-bar caml] (cons "Caml" menu))
;; caml-help
(define-key menu [open] '("Open add path" . ocaml-add-path ))
(define-key menu [close]
'("Close module for help" . ocaml-close-module))
(define-key menu [open] '("Open module for help" . ocaml-open-module))
(define-key menu [help] '("Help for identifier" . caml-help))
(define-key menu [complete] '("Complete identifier" . caml-complete))
(define-key menu [separator-help] '("---"))
;; caml-types
(define-key menu [show-type]
'("Show type at point" . caml-types-show-type ))
(define-key menu [separator-types] '("---"))
;; others
(define-key menu [camldebug] '("Call debugger..." . camldebug))
(define-key menu [run-caml] '("Start subshell..." . run-caml))
(define-key menu [compile] '("Compile..." . compile))
(define-key menu [switch-view]
'("Switch view" . caml-find-alternate-file))
(define-key menu [separator-format] '("--"))
(define-key menu [forms] (cons "Forms" forms))
(define-key menu [show-imenu] '("Show index" . caml-show-imenu))
(put 'caml-show-imenu 'menu-enable '(not caml-imenu-shown))
(define-key menu [show-subshell] '("Show subshell" . caml-show-subshell))
(put 'caml-show-subshell 'menu-enable 'caml-shell-active)
(define-key menu [eval-phrase] '("Eval phrase" . caml-eval-phrase))
(put 'caml-eval-phrase 'menu-enable 'caml-shell-active)
(define-key menu [indent-phrase] '("Indent phrase" . caml-indent-phrase))
(define-key forms [while]
'("while .. do .. done" . caml-insert-while-form))
(define-key forms [try] '("try .. with .." . caml-insert-try-form))
(define-key forms [match] '("match .. with .." . caml-insert-match-form))
(define-key forms [let] '("let .. in .." . caml-insert-let-form))
(define-key forms [if] '("if .. then .. else .." . caml-insert-if-form))
(define-key forms [begin] '("for .. do .. done" . caml-insert-for-form))
(define-key forms [begin] '("begin .. end" . caml-insert-begin-form)))
map)
"Keymap used in Caml mode.")
(defvar caml-mode-syntax-table
(let ((st (make-syntax-table)))
;; backslash is an escape sequence
(modify-syntax-entry ?\\ "\\" st)
;; ( is first character of comment start
(modify-syntax-entry ?\( "()1n" st)
;; * is second character of comment start,
;; and first character of comment end
(modify-syntax-entry ?* ". 23n" st)
;; ) is last character of comment end
(modify-syntax-entry ?\) ")(4" st)
;; backquote was a string-like delimiter (for character literals)
;; (modify-syntax-entry ?` "\"" st)
;; quote and underscore are part of words
(modify-syntax-entry ?' "w" st)
(modify-syntax-entry ?_ "w" st)
;; ISO-latin accented letters and EUC kanjis are part of words
(let ((i 160))
(while (< i 256)
(modify-syntax-entry i "w" st)
(setq i (1+ i))))
st)
"Syntax table in use in Caml mode buffers.")
(defvar caml-mode-abbrev-table nil
"Abbrev table used for Caml mode buffers.")
(if caml-mode-abbrev-table nil
(define-abbrev-table 'caml-mode-abbrev-table
(mapcar (lambda (keyword)
`(,keyword ,keyword caml-abbrev-hook nil t))
'("and" "do" "done" "else" "end" "in" "then" "with"))))
;; Other internal variables
(defvar caml-imenu-shown nil
"Non-nil if we have computed definition list.")
(make-variable-buffer-local 'caml-imenu-shown)
(defconst caml-imenu-search-regexp
(concat "\\_<in\\_>\\|"
"^[ \t]*\\(let\\|class\\|type\\|m\\(odule\\|ethod\\)"
"\\|functor\\|and\\|val\\)[ \t]+"
"\\(\\('[a-zA-Z0-9]+\\|([^)]+)"
"\\|mutable\\|private\\|rec\\|type\\)[ \t]+\\)?"
"\\([a-zA-Z][a-zA-Z0-9_']*\\)"))
;;; The major mode
(eval-when-compile
(require 'imenu))
;;
(defvar caml-mode-hook nil
"Hook for `caml-mode'.")
(define-derived-mode caml-mode prog-mode "caml"
"Major mode for editing OCaml code."
(setq local-abbrev-table caml-mode-abbrev-table)
(make-local-variable 'paragraph-start)
(setq paragraph-start (concat "^$\\|" page-delimiter))
(make-local-variable 'paragraph-separate)
(setq paragraph-separate paragraph-start)
(make-local-variable 'paragraph-ignore-fill-prefix)
(setq paragraph-ignore-fill-prefix t)
(make-local-variable 'require-final-newline)
(setq require-final-newline t)
(make-local-variable 'comment-start)
(setq comment-start "(*")
(make-local-variable 'comment-end)
(setq comment-end "*)")
(make-local-variable 'comment-column)
(setq comment-column 40)
(make-local-variable 'comment-start-skip)
(setq comment-start-skip "(\\*+ *")
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments nil)
(make-local-variable 'indent-line-function)
(setq indent-line-function #'caml-indent-command)
;itz Fri Sep 25 13:23:49 PDT 1998
(make-local-variable 'add-log-current-defun-function)
(setq add-log-current-defun-function #'caml-current-defun)
;garrigue 27-11-96
(setq case-fold-search nil)
;garrigue july 97
;; imenu support
(make-local-variable 'imenu-create-index-function)
(setq imenu-create-index-function #'caml-create-index-function)
(make-local-variable 'imenu-generic-expression)
(setq imenu-generic-expression caml-imenu-search-regexp)
(if (and caml-imenu-enable (< (buffer-size) 10000))
(caml-show-imenu)))
;; Disabled because it assumes make and does not play well with ocamlbuild.
;; See PR#4469 for details.
;; (defun caml-set-compile-command ()
;; "Hook to set compile-command locally, unless there is a Makefile or
;; a _build directory or a _tags file in the current directory."
;; (interactive)
;; (unless (or (null buffer-file-name)
;; (file-exists-p "makefile")
;; (file-exists-p "Makefile")
;; (file-exists-p "_build")
;; (file-exists-p "_tags"))
;; (let* ((filename (file-name-nondirectory buffer-file-name))
;; (basename (file-name-sans-extension filename))
;; (command nil))
;; (cond
;; ((string-match ".*\\.mli\$" filename)
;; (setq command "ocamlc -c"))
;; ((string-match ".*\\.ml\$" filename)
;; (setq command "ocamlc -c") ; (concat "ocamlc -o " basename)
;; )
;; ((string-match ".*\\.mll\$" filename)
;; (setq command "ocamllex"))
;; ((string-match ".*\\.mll\$" filename)
;; (setq command "ocamlyacc"))
;; )
;; (if command
;; (progn
;; (make-local-variable 'compile-command)
;; (setq compile-command (concat command " " filename))))
;; )))
;; (add-hook 'caml-mode-hook #'caml-set-compile-command)
;;; Auxiliary function. Garrigue 96-11-01.
(defun caml-find-alternate-file ()
"Find the `.mli' file for the open `.ml' file, or vice versa."
(interactive)
(let ((name (buffer-file-name)))
(if (string-match "^\\(.*\\)\\.\\(ml\\|mli\\)$" name)
(find-file
(concat
(caml-match-string 1 name)
(if (string= "ml" (caml-match-string 2 name)) ".mli" ".ml"))))))
;;; subshell support
(defun caml-eval-region (start end)
"Evaluate the region.
Send the current region bounded by START and END to the inferior
OCaml process."
(interactive"r")
(require 'inf-caml)
(declare-function inferior-caml-eval-region "inf-caml")
(inferior-caml-eval-region start end))
;; old version ---to be deleted later
;
; (defun caml-eval-phrase ()
; "Send the current OCaml phrase to the inferior Caml process."
; (interactive)
; (save-excursion
; (let ((bounds (caml-mark-phrase)))
; (inferior-caml-eval-region (car bounds) (cdr bounds)))))
(defun caml-eval-phrase (arg &optional min max)
"Send the phrase containing the point to the CAML process.
With a prefix argument send as many phrases as its numeric value,
If an error occurs during evaluation, stop at this phrase and
report the error.
Return nil if noerror and position of error if any.
If ARG's numeric value is zero or negative, evaluate the current phrase
or as many as prefix arg, ignoring evaluation errors.
This allows to jump other erroneous phrases.
Optional arguments MIN MAX defines a region within which the phrase
should lies."
(interactive "p")
(require 'inf-caml)
(declare-function inferior-caml-eval-phrase "inf-caml")
(inferior-caml-eval-phrase arg min max))
(defun caml-eval-buffer (arg)
"Evaluate the buffer from the beginning to the phrase under the point.
With a prefix ARG, evaluate past the whole buffer, no
stopping at the current point."
(interactive "p")
(let ((here (point)) err)
(goto-char (point-min))
(setq err
(caml-eval-phrase 500 (point-min) (if arg (point-max) here)))
(if err (set-mark err))
(goto-char here)))
(defun caml-show-subshell ()
"Start an inferior subshell."
(interactive)
(require 'inf-caml)
(declare-function inferior-caml-show-subshell "inf-caml")
(inferior-caml-show-subshell))
;;; Imenu support
(defun caml-show-imenu ()
"Open `imenu'."
(interactive)
(require 'imenu)
(switch-to-buffer (current-buffer))
(imenu-add-to-menubar "Defs")
(setq caml-imenu-shown t))
(defun caml-prev-index-position-function ()
"Locate the previous imenu entry."
(let (found data)
(while (and (setq found
(re-search-backward caml-imenu-search-regexp nil 'move))
(progn (setq data (match-data)) t)
(or (caml-in-literal-p)
(caml-in-comment-p)
(if (looking-at "in") (caml-find-in-match)))))
(set-match-data data)
found))
(defun caml-create-index-function ()
"Create an index alist for OCaml files.
See `imenu-create-index-function'."
(let (value-alist
type-alist
class-alist
method-alist
module-alist
and-alist
all-alist
menu-alist
(prev-pos (point-max))
index)
(goto-char prev-pos)
(imenu-progress-message prev-pos 0 t)
;; collect definitions
(while (caml-prev-index-position-function)
(setq index (cons (caml-match-string 5) (point)))
(imenu-progress-message prev-pos nil t)
(setq all-alist (cons index all-alist))
(cond
((looking-at "[ \t]*and")
(setq and-alist (cons index and-alist)))
((looking-at "[ \t]*let")
(setq value-alist (cons index (append and-alist value-alist)))
(setq and-alist nil))
((looking-at "[ \t]*type")
(setq type-alist (cons index (append and-alist type-alist)))
(setq and-alist nil))
((looking-at "[ \t]*class")
(setq class-alist (cons index (append and-alist class-alist)))
(setq and-alist nil))
((looking-at "[ \t]*val")
(setq value-alist (cons index value-alist)))
((looking-at "[ \t]*\\(module\\|functor\\)")
(setq module-alist (cons index module-alist)))
((looking-at "[ \t]*method")
(setq method-alist (cons index method-alist)))))
;; build menu
(mapc
(lambda (pair)
(if (not (null (cdr pair)))
(setq menu-alist
(cons
(cons (car pair)
(sort (cdr pair) 'imenu--sort-by-name))
menu-alist))))
`(("Values" . ,value-alist)
("Types" . ,type-alist)
("Modules" . ,module-alist)
("Methods" . ,method-alist)
("Classes" . ,class-alist)))
(if all-alist (setq menu-alist (cons (cons "Index" all-alist) menu-alist)))
(imenu-progress-message prev-pos 100 t)
menu-alist))
;;; Indentation stuff
(defun caml-in-indentation ()
"Test if inside indentation.
This function tests whether all characters between beginning of
line and point are blanks."
(save-excursion
(skip-chars-backward " \t")
(bolp)))
;;; The command
;;; Sorry, I didn't like the previous behaviour... Garrigue 96/11/01
(defun caml-indent-command (&optional p)
"Indent the current line in Caml mode.
Compute new indentation based on Caml syntax. If prefixed P,
indent the line all the way to where point is."
(interactive "*p")
(cond
((and p (> p 1)) (indent-line-to (current-column)))
((caml-in-indentation) (indent-line-to (caml-compute-final-indent)))
(t (save-excursion
(indent-line-to
(caml-compute-final-indent))))))
(defun caml-unindent-command ()
"Decrease indentation by one level in Caml mode.
Works only if the point is at the beginning of an indented line
\(i.e., all characters between beginning of line and point are
blanks\). Does nothing otherwise. The unindent size is given by the
variable `caml-mode-indentation'."
(interactive "*")
(let* ((begline
(save-excursion
(beginning-of-line)
(point)))
(current-offset
(- (point) begline)))
(if (and (>= current-offset caml-mode-indentation)
(caml-in-indentation))
(backward-delete-char-untabify caml-mode-indentation))))
;;;
;;; Error processing
;;;
;; Error positions are given in bytes, not in characters
;; This function switches to monobyte mode
(require 'compile)
(defconst caml--error-regexp
(rx bol
(* " ")
(group ; 1: HIGHLIGHT
(or "File "
;; Exception backtrace.
(seq
(or "Raised at" "Re-raised at" "Raised by primitive operation at"
"Called from")
(* nonl) ; OCaml ≥4.11: " FUNCTION in"
" file "))
(group (? "\"")) ; 2
(group (+ (not (in "\t\n \",<>")))) ; 3: FILE
(backref 2)
(? " (inlined)")
", line" (? "s") " "
(group (+ (in "0-9"))) ; 4: LINE-START
(? "-" (group (+ (in "0-9")))) ; 5; LINE-END
(? ", character" (? "s") " "
(group (+ (in "0-9"))) ; 6: COL-START
(? "-" (group (+ (in "0-9"))))) ; 7: COL-END
;; Colon not present in backtraces.
(? ":"))
(? "\n"
(* (in "\t "))
(* (or (seq (+ (in "0-9"))
" | "
(* nonl))
(+ "^"))
"\n"
(* (in "\t ")))
(group "Warning" ; 8: WARNING
(? " " (+ (in "0-9")))
(? " [" (+ (in "a-z0-9-")) "]")
":")))
"Regular expression matching the error messages produced by ocamlc/ocamlopt.
Also matches source references in exception backtraces.")
(defun caml--end-column ()
"Return the end-column number in a parsed OCaml message.
OCaml uses exclusive end-columns but Emacs wants them to be inclusive."
(and (match-beginning 7)
(+ (string-to-number (match-string 7))
;; Prior to Emacs 28, the end-column function value was incorrectly
;; off by one.
(if (>= emacs-major-version 28) -1 0))))
(when (boundp 'compilation-error-regexp-alist-alist)
(push `(ocaml ,caml--error-regexp 3 (4 . 5) (6 . caml--end-column) (8) 1
(8 font-lock-function-name-face))
compilation-error-regexp-alist-alist))
(when (boundp 'compilation-error-regexp-alist)
(push 'ocaml compilation-error-regexp-alist))
;; A regexp to extract the range info
(defconst caml-error-chars-regexp
".*, .*, [A-\377]+ \\([0-9]+\\)-\\([0-9]+\\):?"
"Regular expression used by `next-error'.
This regular expression extracts the character numbers from an
error message produced by ocamlc.")
;; Wrapper around next-error.
(defvar caml-error-overlay nil)
(defvar caml-next-error-skip-warnings-flag nil)
;;itz 04-21-96 somebody didn't get the documentation for next-error
;;right. When the optional argument is a number n, it should move
;;forward n errors, not reparse.
;itz 04-21-96 instead of defining a new function, use defadvice
;that way we get our effect even when we do \C-x` in compilation buffer
(defadvice next-error (after caml-next-error activate)
"Read the extra positional information provided by the OCaml compiler.
Puts the point and the mark exactly around the erroneous program
fragment. The erroneous fragment is also temporarily highlighted if
possible."
(if (eq major-mode 'caml-mode)
(let (skip bol beg end)
(save-excursion
(with-current-buffer
(if (boundp 'compilation-last-buffer)
compilation-last-buffer ;Emacs 19
"*compilation*") ;Emacs 18
(save-excursion
(goto-char (window-point (get-buffer-window (current-buffer))))
(if (looking-at caml-error-chars-regexp)
(setq beg
(string-to-number
(buffer-substring (match-beginning 1) (match-end 1)))
end
(string-to-number
(buffer-substring (match-beginning 2) (match-end 2)))))
(forward-line 1)
(beginning-of-line)
(if (and (looking-at "Warning")
caml-next-error-skip-warnings-flag)
(setq skip 't)))))
(cond
(skip (next-error))
(beg
(setq end (- end beg))
(beginning-of-line)
(forward-char beg)
(setq beg (point))
(forward-char end)
(setq end (point))
(goto-char beg)
(push-mark end t)
(cond ((fboundp 'make-overlay)
(if caml-error-overlay ()
(setq caml-error-overlay (make-overlay 1 1))
(overlay-put caml-error-overlay 'face 'region))
(unwind-protect
(progn
(move-overlay caml-error-overlay
beg end (current-buffer))
(sit-for 60))
(delete-overlay caml-error-overlay)))))))))
(defun caml-next-error-skip-warnings (&rest args)
"Same as `next-error' but skip warnings.
For the arguments ARGS, see `next-error'."
(let ((old-flag caml-next-error-skip-warnings-flag))
(unwind-protect
(progn (setq caml-next-error-skip-warnings-flag 't)
(apply #'next-error args))
(setq caml-next-error-skip-warnings-flag old-flag))))
;; Usual match-string doesn't work properly with font-lock-mode
;; on some emacs.
(defun caml-match-string (num &optional string)
"Return string of text matched by last search, without properties.
NUM specifies which parenthesized expression in the last regexp.
Value is nil if NUMth pair didn't match, or there were less than
NUM pairs. Zero means the entire text matched by the whole regexp
or whole string. Uses STRING is given and otherwise extracts from
buffer."
(let* ((data (match-data))
(begin (nth (* 2 num) data))
(end (nth (1+ (* 2 num)) data)))
(if string (substring string begin end)
(buffer-substring-no-properties begin end))))
;; itz Thu Sep 24 19:02:42 PDT 1998 this is to have some level of
;; comfort when sending phrases to the toplevel and getting errors.
(defun caml-goto-phrase-error ()
"Find the error location in current OCaml phrase."
(interactive)
(require 'inf-caml)
(declare-function inferior-caml-goto-error "inf-caml")
(let ((bounds (save-excursion (caml-mark-phrase))))
(inferior-caml-goto-error (car bounds) (cdr bounds))))
;;; Phrases
;itz the heuristics used to see if we're `between two phrases'
;didn't seem right to me.
(defconst caml-phrase-start-keywords
(concat "\\_<\\(class\\|ex\\(ternal\\|ception\\)\\|functor"
"\\|let\\|module\\|open\\|type\\|val\\)\\_>")
"Keywords starting phrases in files.")
(defun caml-at-phrase-start-p ()
"Check if at the start of a phrase.
A phrase starts when a toplevel keyword is at the beginning of a
line."
(and (bolp)
(or (looking-at "#")
(looking-at caml-phrase-start-keywords))))
(defun caml-skip-comments-forward ()
"Skip forward past comments."
(skip-chars-forward " \n\t")
(while (or (looking-at comment-start-skip) (caml-in-comment-p))
(if (= (following-char) ?\)) (forward-char)
(search-forward comment-end))
(skip-chars-forward " \n\t")))
(defun caml-skip-comments-backward ()
"Skip backward past comments."
(skip-chars-backward " \n\t")
(while (and (eq (preceding-char) ?\)) (eq (char-after (- (point) 2)) ?*))
(backward-char)
(while (caml-in-comment-p) (search-backward comment-start))
(skip-chars-backward " \n\t")))
(defconst caml-phrase-sep-keywords (concat ";;\\|" caml-phrase-start-keywords))
(defun caml-find-phrase (&optional min-pos max-pos)
"Find the CAML phrase containing the point.
Return the position of the beginning of the phrase, and move
point to the end. Optionally operates between MIN-POS and
MAX-POS."
(interactive)
(if (not min-pos) (setq min-pos (point-min)))
(if (not max-pos) (setq max-pos (point-max)))
(let (beg end kwop)
;(caml-skip-comments-backward)
(cond
; shall we have special processing for semicolons?
;((and (eq (char-before (- (point) 1)) ?\;) (eq (char-before) ?\;))
; (forward-char)
; (caml-skip-comments-forward)
; (setq beg (point))
; (while (and (search-forward ";;" max-pos 'move)
; (or (caml-in-comment-p) (caml-in-literal-p)))))
(t
(caml-skip-comments-forward)
(if (caml-at-phrase-start-p) (forward-char))
(while (and (cond
((re-search-forward caml-phrase-sep-keywords max-pos 'move)
(goto-char (match-beginning 0)) t))
(or (not (or (bolp) (looking-at ";;")))
(caml-in-comment-p)
(caml-in-literal-p)))
(forward-char))
(setq end (+ (point) (if (looking-at ";;") 2 0)))
(while (and
(setq kwop (caml-find-kwop caml-phrase-sep-keywords min-pos))
(not (string= kwop ";;"))
(not (bolp))))
(if (string= kwop ";;") (forward-char 2))
(if (not kwop) (goto-char min-pos))
(caml-skip-comments-forward)
(setq beg (point))
(if (>= beg end) (error "No phrase before point"))
(goto-char end)))
(caml-skip-comments-forward)
beg))
(defun caml-mark-phrase (&optional min-pos max-pos)
"Put mark at end of this OCaml phrase, point at beginning.
Optionally operates between MIN-POS and MAX-POS."
(interactive)
(let* ((beg (caml-find-phrase min-pos max-pos)) (end (point)))
(push-mark)
(goto-char beg)
(cons beg end)))
;;itz Fri Sep 25 12:58:13 PDT 1998 support for adding change-log entries
(defun caml-current-defun ()
"Return the location of the definition around the point."
(save-excursion
(caml-mark-phrase)
(if (not (looking-at caml-phrase-start-keywords)) nil
(re-search-forward caml-phrase-start-keywords)
(let ((done nil))
(while (not done)
(cond
((looking-at "\\s ")
(skip-syntax-forward " "))
((char-equal (following-char) ?\( )
(forward-sexp 1))
((char-equal (following-char) ?')
(skip-syntax-forward "w_"))
(t (setq done t)))))
(re-search-forward "\\(\\sw\\|\\s_\\)+")
(match-string 0))))
(defun caml-overlap (b1 e1 b2 e2)
"Return non-nil if the closed ranges B1..E1 and B2..E2 overlap."
(<= (max b1 b2) (min e1 e2)))
(defun caml-in-literal-p ()
"Return non-nil if point is inside a caml literal."
(let* ((start-literal (concat "[\"" caml-quote-char "]"))
(char-literal
(concat "\\([^\\]\\|\\\\\\.\\|\\\\[0-9][0-9][0-9]\\)"
caml-quote-char))
(pos (point))
(eol (progn (end-of-line 1) (point)))
state in-str)
(beginning-of-line 1)