-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuareg.el
3448 lines (3137 loc) · 126 KB
/
tuareg.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
;;; tuareg.el --- Caml mode for (X)Emacs. -*- coding: latin-1 -*-
;; Copyright © by INRIA, Albert Cohen, 2010.
;; Licensed under the GNU General Public License.
;;
;; 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 2 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.
;;; Commentary:
;;; Code:
(require 'cl)
(require 'easymenu)
(defconst tuareg-mode-version "Tuareg Version 1.45.7"
" Copyright © by INRIA, Albert Cohen, 2010.
Copying is covered by the GNU General Public License.
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 2 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.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Emacs versions support
(defconst tuareg-with-xemacs (featurep 'xemacs))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compatibility functions
(defalias 'tuareg-match-string
(if (fboundp 'match-string-no-properties)
'match-string-no-properties
'match-string))
(if (not (fboundp 'read-shell-command))
(defun read-shell-command (prompt &optional initial-input history)
"Read a string from the minibuffer, using `shell-command-history'."
(read-from-minibuffer prompt initial-input nil nil
(or history 'shell-command-history))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Import types and help features
(defvar tuareg-with-caml-mode-p
(condition-case nil
(and (require 'caml-types) (require 'caml-help))
(error nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User customizable variables
;; Use the standard `customize' interface or `tuareg-mode-hook' to
;; Configure these variables
(require 'custom)
(defgroup tuareg nil
"Support for the Objective Caml language."
:group 'languages)
;; Comments
(defcustom tuareg-indent-leading-comments t
"*If true, indent leading comment lines (starting with `(*') like others."
:group 'tuareg :type 'boolean)
(defcustom tuareg-indent-comments t
"*If true, automatically align multi-line comments."
:group 'tuareg :type 'boolean)
(defcustom tuareg-comment-end-extra-indent 0
"*How many spaces to indent a leading comment end `*)'.
If you expect comments to be indented like
(*
...
*)
even without leading `*', use `tuareg-comment-end-extra-indent' = 1."
:group 'tuareg
:type '(radio :extra-offset 8
:format "%{Comment End Extra Indent%}:
Comment alignment:\n%v"
(const :tag "align with `(' in comment opening" 0)
(const :tag "align with `*' in comment opening" 1)
(integer :tag "custom alignment" 0)))
(defcustom tuareg-support-leading-star-comments t
"*Enable automatic intentation of comments of the form
(*
* ...
*)
Documentation comments (** *) are not concerned by this variable
unless `tuareg-leading-star-in-doc' is also set.
If you do not set this variable and still expect comments to be
indented like
(*
...
*)
\(without leading `*'), set `tuareg-comment-end-extra-indent' to 1."
:group 'tuareg :type 'boolean)
(defcustom tuareg-leading-star-in-doc nil
"*Enable automatic intentation of documentation comments of the form
(**
* ...
*)"
:group 'tuareg :type 'boolean)
;; Indentation defaults
(defcustom tuareg-default-indent 2
"*Default indentation.
Global indentation variable (large values may lead to indentation overflows).
When no governing keyword is found, this value is used to indent the line
if it has to."
:group 'tuareg :type 'integer)
(defcustom tuareg-lazy-paren nil
"*If true, indent parentheses like a standard keyword."
:group 'tuareg :type 'boolean)
(defcustom tuareg-support-camllight nil
"*If true, handle Caml Light character syntax (incompatible with labels)."
:group 'tuareg :type 'boolean
:set '(lambda (var val)
(setq tuareg-support-camllight val)
(if (boundp 'tuareg-mode-syntax-table)
(modify-syntax-entry ?` (if val "\"" ".")
tuareg-mode-syntax-table))))
(defcustom tuareg-support-metaocaml nil
"*If true, handle MetaOCaml character syntax."
:group 'tuareg :type 'boolean
:set '(lambda (var val)
(setq tuareg-support-metaocaml val)
(if (boundp 'tuareg-font-lock-keywords)
(tuareg-install-font-lock))))
(defcustom tuareg-let-always-indent t
"*If true, enforce indentation is at least `tuareg-let-indent' after a `let'.
As an example, set it to false when you have `tuareg-with-indent' set to 0,
and you want `let x = match ... with' and `match ... with' indent the
same way."
:group 'tuareg :type 'boolean)
(defcustom tuareg-|-extra-unindent tuareg-default-indent
"*Extra backward indent for Caml lines starting with the `|' operator.
It is NOT the variable controlling the indentation of the `|' itself:
this value is automatically added to `function', `with', `parse' and
some cases of `type' keywords to leave enough space for `|' backward
indentation.
For exemple, setting this variable to 0 leads to the following indentation:
match ... with
X -> ...
| Y -> ...
| Z -> ...
To modify the indentation of lines lead by `|' you need to modify the
indentation variables for `with', `function' and `parse', and possibly
for `type' as well. For example, setting them to 0 (and leaving
`tuareg-|-extra-unindent' to its default value) yields:
match ... with
X -> ...
| Y -> ...
| Z -> ..."
:group 'tuareg :type 'integer)
(defcustom tuareg-class-indent tuareg-default-indent
"*How many spaces to indent from a `class' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-sig-struct-align t
"*Align `sig' and `struct' keywords with `module'."
:group 'tuareg :type 'boolean)
(defcustom tuareg-sig-struct-indent tuareg-default-indent
"*How many spaces to indent from a `sig' or `struct' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-method-indent tuareg-default-indent
"*How many spaces to indent from a `method' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-begin-indent tuareg-default-indent
"*How many spaces to indent from a `begin' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-for-while-indent tuareg-default-indent
"*How many spaces to indent from a `for' or `while' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-do-indent tuareg-default-indent
"*How many spaces to indent from a `do' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-fun-indent tuareg-default-indent
"*How many spaces to indent from a `fun' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-function-indent tuareg-default-indent
"*How many spaces to indent from a `function' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-if-then-else-indent tuareg-default-indent
"*How many spaces to indent from an `if', `then' or `else' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-let-indent tuareg-default-indent
"*How many spaces to indent from a `let' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-in-indent tuareg-default-indent
"*How many spaces to indent from a `in' keyword.
A lot of people like formatting `let' ... `in' expressions whithout
indentation:
let x = 0 in
blah x
Set this variable to 0 to get this behaviour.
However, nested declarations are always correctly handled:
let x = 0 in let x = 0
let y = 0 in or in let y = 0
let z = 0 ... in let z = 0 ..."
:group 'tuareg :type 'integer)
(defcustom tuareg-match-indent tuareg-default-indent
"*How many spaces to indent from a `match' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-try-indent tuareg-default-indent
"*How many spaces to indent from a `try' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-with-indent tuareg-default-indent
"*How many spaces to indent from a `with' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-rule-indent tuareg-default-indent
"*How many spaces to indent from a `rule' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-parse-indent tuareg-default-indent
"*How many spaces to indent from a `parse' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-parser-indent tuareg-default-indent
"*How many spaces to indent from a `parser' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-type-indent tuareg-default-indent
"*How many spaces to indent from a `type' keyword."
:group 'tuareg :type 'integer)
(defcustom tuareg-val-indent tuareg-default-indent
"*How many spaces to indent from a `val' keyword."
:group 'tuareg :type 'integer)
;; Automatic indentation
;; Using abbrev-mode and electric keys
(defcustom tuareg-use-abbrev-mode t
"*Non-nil means electrically indent lines starting with leading keywords.
Leading keywords are such as `end', `done', `else' etc.
It makes use of `abbrev-mode'.
Many people find eletric keywords irritating, so you can disable them by
setting this variable to nil."
:group 'tuareg :type 'boolean
:set '(lambda (var val)
(setq tuareg-use-abbrev-mode val)
(abbrev-mode val)))
(defcustom tuareg-electric-indent t
"*Non-nil means electrically indent lines starting with `|', `)', `]' or `}'.
Many people find eletric keys irritating, so you can disable them in
setting this variable to nil."
:group 'tuareg :type 'boolean)
(defcustom tuareg-electric-close-vector t
"*Non-nil means electrically insert `|' before a vector-closing `]' or
`>' before an object-closing `}'.
Many people find eletric keys irritating, so you can disable them in
setting this variable to nil. You should probably have this on,
though, if you also have `tuareg-electric-indent' on."
:group 'tuareg :type 'boolean)
;; Tuareg-Interactive
;; Configure via `tuareg-mode-hook'
(defcustom tuareg-skip-after-eval-phrase t
"*Non-nil means skip to the end of the phrase after evaluation in the
Caml toplevel."
:group 'tuareg :type 'boolean)
(defcustom tuareg-interactive-read-only-input nil
"*Non-nil means input sent to the Caml toplevel is read-only."
:group 'tuareg :type 'boolean)
(defcustom tuareg-interactive-echo-phrase t
"*Non-nil means echo phrases in the toplevel buffer when sending
them to the Caml toplevel."
:group 'tuareg :type 'boolean)
(defcustom tuareg-interactive-input-font-lock t
"*Non nil means Font-Lock for toplevel input phrases."
:group 'tuareg :type 'boolean)
(defcustom tuareg-interactive-output-font-lock t
"*Non nil means Font-Lock for toplevel output messages."
:group 'tuareg :type 'boolean)
(defcustom tuareg-interactive-error-font-lock t
"*Non nil means Font-Lock for toplevel error messages."
:group 'tuareg :type 'boolean)
(defcustom tuareg-display-buffer-on-eval t
"*Non nil means pop up the Caml toplevel when evaluating code."
:group 'tuareg :type 'boolean)
(defcustom tuareg-manual-url "http://pauillac.inria.fr/ocaml/htmlman/index.html"
"*URL to the Caml reference manual."
:group 'tuareg :type 'string)
(defcustom tuareg-browser 'tuareg-netscape-manual
"*Name of function that displays the Caml reference manual.
Valid names are `tuareg-netscape-manual', `tuareg-mmm-manual'
and `tuareg-xemacs-w3-manual' (XEmacs only)."
:group 'tuareg)
(defcustom tuareg-library-path "/usr/local/lib/ocaml/"
"*Path to the Caml library."
:group 'tuareg :type 'string)
(defcustom tuareg-definitions-max-items 30
"*Maximum number of items a definitions menu can contain."
:group 'tuareg :type 'integer)
(defvar tuareg-options-list
'(("Lazy parentheses indentation" . 'tuareg-lazy-paren)
("Force indentation after `let'" . 'tuareg-let-always-indent)
"---"
("Automatic indentation of leading keywords" . 'tuareg-use-abbrev-mode)
("Electric indentation of ), ] and }" . 'tuareg-electric-indent)
("Electric matching of [| and {<" . 'tuareg-electric-close-vector)
"---"
("Indent body of comments" . 'tuareg-indent-comments)
("Indent first line of comments" . 'tuareg-indent-leading-comments)
("Leading-`*' comment style" . 'tuareg-support-leading-star-comments))
"*List of menu-configurable Tuareg options.")
(defvar tuareg-interactive-options-list
'(("Skip phrase after evaluation" . 'tuareg-skip-after-eval-phrase)
("Echo phrase in interactive buffer" . 'tuareg-interactive-echo-phrase)
"---"
("Font-lock interactive input" . 'tuareg-interactive-input-font-lock)
("Font-lock interactive output" . 'tuareg-interactive-output-font-lock)
("Font-lock interactive error" . 'tuareg-interactive-error-font-lock)
"---"
("Read only input" . 'tuareg-interactive-read-only-input))
"*List of menu-configurable Tuareg options.")
(defvar tuareg-interactive-program "ocaml"
"*Default program name for invoking a Caml toplevel from Emacs.")
;; Could be interesting to have this variable buffer-local
;; (e.g., ocaml vs. metaocaml buffers)
;; (make-variable-buffer-local 'tuareg-interactive-program)
;; Backtrack to custom parsing and caching by default, until stable
;;(defvar tuareg-use-syntax-ppss (fboundp 'syntax-ppss)
(defconst tuareg-use-syntax-ppss nil
"*If nil, use our own parsing and caching.")
(defgroup tuareg-faces nil
"Special faces for the Tuareg mode."
:group 'tuareg)
(defconst tuareg-faces-inherit-p
(if (boundp 'face-attribute-name-alist)
(assq :inherit face-attribute-name-alist)))
(defface tuareg-font-lock-governing-face
(if tuareg-faces-inherit-p
'((t :inherit font-lock-keyword-face))
'((((background light)) (:foreground "darkorange3" :bold t))
(t (:foreground "orange" :bold t))))
"Face description for governing/leading keywords."
:group 'tuareg-faces)
(defvar tuareg-font-lock-governing-face
'tuareg-font-lock-governing-face)
(defface tuareg-font-lock-multistage-face
'((((background light))
(:foreground "darkblue" :background "lightgray" :bold t))
(t (:foreground "steelblue" :background "darkgray" :bold t)))
"Face description for MetaOCaml staging operators."
:group 'tuareg-faces)
(defvar tuareg-font-lock-multistage-face
'tuareg-font-lock-multistage-face)
(defface tuareg-font-lock-operator-face
(if tuareg-faces-inherit-p
'((t :inherit font-lock-keyword-face))
'((((background light)) (:foreground "brown"))
(t (:foreground "khaki"))))
"Face description for all operators."
:group 'tuareg-faces)
(defvar tuareg-font-lock-operator-face
'tuareg-font-lock-operator-face)
(defface tuareg-font-lock-error-face
'((t (:foreground "yellow" :background "red" :bold t)))
"Face description for all errors reported to the source."
:group 'tuareg-faces)
(defvar tuareg-font-lock-error-face
'tuareg-font-lock-error-face)
(defface tuareg-font-lock-interactive-output-face
'((((background light))
(:foreground "blue4"))
(t (:foreground "cyan")))
"Face description for all toplevel outputs."
:group 'tuareg-faces)
(defvar tuareg-font-lock-interactive-output-face
'tuareg-font-lock-interactive-output-face)
(defface tuareg-font-lock-interactive-error-face
(if tuareg-faces-inherit-p
'((t :inherit font-lock-warning-face))
'((((background light)) (:foreground "red3"))
(t (:foreground "red2"))))
"Face description for all toplevel errors."
:group 'tuareg-faces)
(defvar tuareg-font-lock-interactive-error-face
'tuareg-font-lock-interactive-error-face)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Support definitions
(defun tuareg-leading-star-p ()
(and tuareg-support-leading-star-comments
(save-excursion ; this function does not make sense outside of a comment
(tuareg-beginning-of-literal-or-comment)
(and (or tuareg-leading-star-in-doc
(not (looking-at "(\\*[Tt][Ee][Xx]\\|(\\*\\*")))
(progn
(forward-line 1)
(back-to-indentation)
(looking-at "\\*[^)]"))))))
(defun tuareg-auto-fill-insert-leading-star (&optional leading-star)
(let ((point-leading-comment (looking-at "(\\*")) (return-leading nil))
(save-excursion
(back-to-indentation)
(if tuareg-electric-indent
(progn
(if (and (tuareg-in-comment-p)
(or leading-star
(tuareg-leading-star-p)))
(progn
(if (not (looking-at "(?\\*"))
(insert-before-markers "* "))
(setq return-leading t)))
(if (not point-leading-comment)
;; Use optional argument to break recursion
(tuareg-indent-command t)))))
return-leading))
(defun tuareg-auto-fill-function ()
(if (tuareg-in-literal-p) ()
(let ((leading-star
(if (not (char-equal ?\n last-command-char))
(tuareg-auto-fill-insert-leading-star)
nil)))
(do-auto-fill)
(if (not (char-equal ?\n last-command-char))
(tuareg-auto-fill-insert-leading-star leading-star)))))
(defun tuareg-forward-char (&optional step)
(if step (goto-char (+ (point) step))
(goto-char (1+ (point)))))
(defun tuareg-backward-char (&optional step)
(if step (goto-char (- (point) step))
(goto-char (1- (point)))))
(defun tuareg-in-indentation-p ()
"Return non-nil if all chars between beginning of line and point are blanks."
(save-excursion
(skip-chars-backward " \t")
(bolp)))
(defvar tuareg-cache-stop (point-min))
(make-variable-buffer-local 'tuareg-cache-stop)
(defvar tuareg-cache nil)
(make-variable-buffer-local 'tuareg-cache)
(defvar tuareg-cache-local nil)
(make-variable-buffer-local 'tuareg-cache-local)
(defvar tuareg-cache-last-local nil)
(make-variable-buffer-local 'tuareg-cache-last-local)
(defvar tuareg-last-loc (cons nil nil))
(if tuareg-use-syntax-ppss
(progn
(defun tuareg-in-literal-p ()
"Returns non-nil if point is inside a Caml literal."
(nth 3 (syntax-ppss)))
(defun tuareg-in-comment-p ()
"Returns non-nil if point is inside a Caml comment."
(nth 4 (syntax-ppss)))
(defun tuareg-in-literal-or-comment-p ()
"Returns non-nil if point is inside a Caml literal or comment."
(nth 8 (syntax-ppss)))
(defun tuareg-beginning-of-literal-or-comment ()
"Skips to the beginning of the current literal or comment (or buffer)."
(interactive)
(goto-char (or (nth 8 (syntax-ppss)) (point))))
(defun tuareg-beginning-of-literal-or-comment-fast ()
(goto-char (or (nth 8 (syntax-ppss)) (point-min))))
;; FIXME: not clear if moving out of a string/comment counts as 1 or no.
(defalias 'tuareg-backward-up-list 'backward-up-list))
(defun tuareg-before-change-function (begin end)
(setq tuareg-cache-stop
(if (save-excursion (beginning-of-line) (= (point) (point-min)))
(point-min)
(min tuareg-cache-stop (1- begin)))))
(defun tuareg-in-literal-p ()
"Return non-nil if point is inside a Caml literal."
(car (tuareg-in-literal-or-comment)))
(defun tuareg-in-comment-p ()
"Return non-nil if point is inside a Caml comment."
(cdr (tuareg-in-literal-or-comment)))
(defun tuareg-in-literal-or-comment-p ()
"Return non-nil if point is inside a Caml literal or comment."
(tuareg-in-literal-or-comment)
(or (car tuareg-last-loc) (cdr tuareg-last-loc)))
(defun tuareg-in-literal-or-comment ()
"Return the pair `((tuareg-in-literal-p) . (tuareg-in-comment-p))'."
(if (and (<= (point) tuareg-cache-stop) tuareg-cache)
(progn
(if (or (not tuareg-cache-local) (not tuareg-cache-last-local)
(and (>= (point) (caar tuareg-cache-last-local))))
(setq tuareg-cache-local tuareg-cache))
(while (and tuareg-cache-local (< (point) (caar tuareg-cache-local)))
(setq tuareg-cache-last-local tuareg-cache-local
tuareg-cache-local (cdr tuareg-cache-local)))
(setq tuareg-last-loc
(if tuareg-cache-local
(cons (eq (cadar tuareg-cache-local) 'b)
(> (cddar tuareg-cache-local) 0))
(cons nil nil))))
(let ((flag t) (op (point)) (mp (min (point) (1- (point-max))))
(balance 0) (end-of-comment nil))
(while (and tuareg-cache (<= tuareg-cache-stop (caar tuareg-cache)))
(setq tuareg-cache (cdr tuareg-cache)))
(if tuareg-cache
(if (eq (cadar tuareg-cache) 'b)
(progn
(setq tuareg-cache-stop (1- (caar tuareg-cache)))
(goto-char tuareg-cache-stop)
(setq balance (cddar tuareg-cache))
(setq tuareg-cache (cdr tuareg-cache)))
(setq balance (cddar tuareg-cache))
(setq tuareg-cache-stop (caar tuareg-cache))
(goto-char tuareg-cache-stop)
(skip-chars-forward "("))
(goto-char (point-min)))
(skip-chars-backward "\\\\*")
(while flag
(if end-of-comment (setq balance 0 end-of-comment nil))
(skip-chars-forward "^\\\\'`\"(\\*")
(cond
((looking-at "\\\\")
(tuareg-forward-char 2))
((looking-at "'\\([^\n\\']\\|\\\\[^ \t\n][^ \t\n]?[^ \t\n]?\\)'")
(setq tuareg-cache (cons (cons (1+ (point)) (cons 'b balance))
tuareg-cache))
(goto-char (match-end 0))
(setq tuareg-cache (cons (cons (point) (cons 'e balance))
tuareg-cache)))
((and
tuareg-support-camllight
(looking-at "`\\([^\n\\']\\|\\\\[^ \t\n][^ \t\n]?[^ \t\n]?\\)`"))
(setq tuareg-cache (cons (cons (1+ (point)) (cons 'b balance))
tuareg-cache))
(goto-char (match-end 0))
(setq tuareg-cache (cons (cons (point) (cons 'e balance))
tuareg-cache)))
((looking-at "\"")
(tuareg-forward-char)
(setq tuareg-cache (cons (cons (point) (cons 'b balance))
tuareg-cache))
(skip-chars-forward "^\\\\\"")
(while (looking-at "\\\\")
(tuareg-forward-char 2) (skip-chars-forward "^\\\\\""))
(tuareg-forward-char)
(setq tuareg-cache (cons (cons (point) (cons 'e balance))
tuareg-cache)))
((looking-at "(\\*")
(setq balance (1+ balance))
(setq tuareg-cache (cons (cons (point) (cons nil balance))
tuareg-cache))
(tuareg-forward-char 2))
((looking-at "\\*)")
(tuareg-forward-char 2)
(if (> balance 1)
(progn
(setq balance (1- balance))
(setq tuareg-cache (cons (cons (point) (cons nil balance))
tuareg-cache)))
(setq end-of-comment t)
(setq tuareg-cache (cons (cons (point) (cons nil 0))
tuareg-cache))))
(t (tuareg-forward-char)))
(setq flag (<= (point) mp)))
(setq tuareg-cache-local tuareg-cache
tuareg-cache-stop (point))
(goto-char op)
(if tuareg-cache (tuareg-in-literal-or-comment)
(setq tuareg-last-loc (cons nil nil))
tuareg-last-loc))))
(defun tuareg-beginning-of-literal-or-comment ()
"Skips to the beginning of the current literal or comment (or buffer)."
(interactive)
(if (tuareg-in-literal-or-comment-p)
(tuareg-beginning-of-literal-or-comment-fast)))
(defun tuareg-beginning-of-literal-or-comment-fast ()
(while (and tuareg-cache-local
(or (eq 'b (cadar tuareg-cache-local))
(> (cddar tuareg-cache-local) 0)))
(setq tuareg-cache-last-local tuareg-cache-local
tuareg-cache-local (cdr tuareg-cache-local)))
(if tuareg-cache-last-local
(goto-char (caar tuareg-cache-last-local))
(goto-char (point-min)))
(if (eq 'b (cadar tuareg-cache-last-local)) (tuareg-backward-char)))
(defun tuareg-backward-up-list ()
"Safe up-list regarding comments, literals and errors."
(let ((balance 1) (op (point)) (oc nil))
(tuareg-in-literal-or-comment)
(while (and (> (point) (point-min)) (> balance 0))
(setq oc (if tuareg-cache-local (caar tuareg-cache-local) (point-min)))
(condition-case nil (up-list -1) (error (goto-char (point-min))))
(if (>= (point) oc) (setq balance (1- balance))
(goto-char op)
(skip-chars-backward "^[]{}()") (tuareg-backward-char)
(if (not (tuareg-in-literal-or-comment-p))
(cond
((looking-at "[[{(]")
(setq balance (1- balance)))
((looking-at "[]})]")
(setq balance (1+ balance))))
(tuareg-beginning-of-literal-or-comment-fast)))
(setq op (point)))))) ;; End of (if tuareg-use-syntax-ppss
(defun tuareg-false-=-p ()
"Is the underlying `=' the first/second letter of an operator?"
(or (memq (preceding-char) '(?: ?> ?< ?=))
(char-equal ?= (char-after (1+ (point))))))
(defun tuareg-at-phrase-break-p ()
"Is the underlying `;' a phrase break?"
(and (char-equal ?\; (following-char))
(or (and (not (eobp))
(char-equal ?\; (char-after (1+ (point)))))
(char-equal ?\; (preceding-char)))))
(defun tuareg-assoc-indent (kwop &optional look-for-let-or-and)
"Return relative indentation of the keyword given in argument."
(let ((ind (symbol-value (cdr (assoc kwop tuareg-keyword-alist))))
(looking-let-or-and (and look-for-let-or-and
(looking-at "\\<\\(let\\|and\\)\\>"))))
(if (string-match "\\<\\(with\\|function\\|parser?\\)\\>" kwop)
(+ (if (and tuareg-let-always-indent
looking-let-or-and (< ind tuareg-let-indent))
tuareg-let-indent ind)
tuareg-|-extra-unindent)
ind)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sym-lock in Emacs
;; By Stefan Monnier
(defcustom tuareg-font-lock-symbols nil
"Display fun and -> and such using symbols in fonts.
This may sound like a neat trick, but note that it can change the
alignment and can thus lead to surprises."
:type 'bool)
(defvar tuareg-font-lock-symbols-alist
(append
;; The symbols can come from a JIS0208 font.
(and (fboundp 'make-char) (fboundp 'charsetp) (charsetp 'japanese-jisx0208)
(list (cons "fun" (make-char 'japanese-jisx0208 38 75))
(cons "sqrt" (make-char 'japanese-jisx0208 34 101))
(cons "not" (make-char 'japanese-jisx0208 34 76))
(cons "or" (make-char 'japanese-jisx0208 34 75))
(cons "||" (make-char 'japanese-jisx0208 34 75))
(cons "&&" (make-char 'japanese-jisx0208 34 74))
;; (cons "*." (make-char 'japanese-jisx0208 33 95))
;; (cons "/." (make-char 'japanese-jisx0208 33 96))
(cons "->" (make-char 'japanese-jisx0208 34 42))
(cons "=>" (make-char 'japanese-jisx0208 34 77))
(cons "<-" (make-char 'japanese-jisx0208 34 43))
(cons "<>" (make-char 'japanese-jisx0208 33 98))
(cons "==" (make-char 'japanese-jisx0208 34 97))
(cons ">=" (make-char 'japanese-jisx0208 33 102))
(cons "<=" (make-char 'japanese-jisx0208 33 101))
;; Some greek letters for type parameters.
(cons "'a" (make-char 'japanese-jisx0208 38 65))
(cons "'b" (make-char 'japanese-jisx0208 38 66))
(cons "'c" (make-char 'japanese-jisx0208 38 67))
(cons "'d" (make-char 'japanese-jisx0208 38 68))))
;; Or a unicode font.
(and (fboundp 'decode-char)
(list (cons "fun" (decode-char 'ucs 955))
(cons "sqrt" (decode-char 'ucs 8730))
(cons "not" (decode-char 'ucs 172))
(cons "or" (decode-char 'ucs 8897))
(cons "&&" (decode-char 'ucs 8896))
(cons "||" (decode-char 'ucs 8897))
;; (cons "*." (decode-char 'ucs 215))
;; (cons "/." (decode-char 'ucs 247))
(cons "->" (decode-char 'ucs 8594))
(cons "<-" (decode-char 'ucs 8592))
(cons "<=" (decode-char 'ucs 8804))
(cons ">=" (decode-char 'ucs 8805))
(cons "<>" (decode-char 'ucs 8800))
(cons "==" (decode-char 'ucs 8801))
;; Some greek letters for type parameters.
(cons "'a" (decode-char 'ucs 945))
(cons "'b" (decode-char 'ucs 946))
(cons "'c" (decode-char 'ucs 947))
(cons "'d" (decode-char 'ucs 948))
))))
(defun tuareg-font-lock-compose-symbol (alist)
"Compose a sequence of ascii chars into a symbol.
Regexp match data 0 points to the chars."
;; Check that the chars should really be composed into a symbol.
(let* ((start (match-beginning 0))
(end (match-end 0))
(syntaxes (if (eq (char-syntax (char-after start)) ?w)
'(?w) '(?. ?\\))))
(if (or (memq (char-syntax (or (char-before start) ?\ )) syntaxes)
(memq (char-syntax (or (char-after end) ?\ )) syntaxes)
(memq (get-text-property start 'face)
'(tuareg-doc-face font-lock-string-face
font-lock-comment-face)))
;; No composition for you. Let's actually remove any composition
;; we may have added earlier and which is now incorrect.
(remove-text-properties start end '(composition))
;; That's a symbol alright, so add the composition.
(compose-region start end (cdr (assoc (match-string 0) alist)))))
;; Return nil because we're not adding any face property.
nil)
(defun tuareg-font-lock-symbols-keywords ()
(when (fboundp 'compose-region)
(let ((alist nil))
(dolist (x tuareg-font-lock-symbols-alist)
(when (and (if (fboundp 'char-displayable-p)
(char-displayable-p (cdr x))
t)
(not (assoc (car x) alist))) ;Not yet in alist.
(push x alist)))
(when alist
`((,(regexp-opt (mapcar 'car alist) t)
(0 (tuareg-font-lock-compose-symbol ',alist))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Font-Lock
(unless tuareg-use-syntax-ppss
(defun tuareg-fontify-buffer ()
(font-lock-default-fontify-buffer)
(tuareg-fontify (point-min) (point-max)))
(defun tuareg-fontify-region (begin end &optional verbose)
(font-lock-default-fontify-region begin end verbose)
(tuareg-fontify begin end))
(defun tuareg-fontify (begin end)
(if (eq major-mode 'tuareg-mode)
(save-excursion
(let ((modified (buffer-modified-p))) ; Emacs hack (see below)
(goto-char begin)
(beginning-of-line)
(setq begin (point))
(goto-char (1- end))
(end-of-line)
;; Dirty hack to trick `font-lock-default-unfontify-region'
(if (not tuareg-with-xemacs) (forward-line 2))
(setq end (point))
(while (> end begin)
(goto-char (1- end))
(tuareg-in-literal-or-comment)
(cond
((cdr tuareg-last-loc)
(tuareg-beginning-of-literal-or-comment)
(put-text-property (max begin (point)) end 'face
(if (looking-at
"(\\*[Tt][Ee][Xx]\\|(\\*\\*[^*]")
tuareg-doc-face
'font-lock-comment-face))
(setq end (1- (point))))
((car tuareg-last-loc)
(tuareg-beginning-of-literal-or-comment)
(put-text-property (max begin (point)) end 'face
'font-lock-string-face)
(setq end (point)))
(t (while (and tuareg-cache-local
(or (> (caar tuareg-cache-local) end)
(eq 'b (cadar tuareg-cache-local))))
(setq tuareg-cache-local (cdr tuareg-cache-local)))
(setq end (if tuareg-cache-local
(caar tuareg-cache-local) begin)))))
(if (not (or tuareg-with-xemacs modified)) ; properties taken
(set-buffer-modified-p nil)))))) ; too seriously...
;; XEmacs and Emacs have different documentation faces...
(defvar tuareg-doc-face (if (facep 'font-lock-doc-face)
'font-lock-doc-face
'font-lock-doc-string-face))
) ;; End of (unless tuareg-use-syntax-ppss
;; By Stefan Monnier: redesigned font-lock installation and use char classes
;; When char classes are not available, character ranges only span
;; ASCII characters for MULE compatibility
(defconst tuareg-use-char-classes (string-match "[[:alpha:]]" "x"))
(defconst tuareg-lower (if tuareg-use-char-classes "[:lower:]" "a-z"))
(defconst tuareg-alpha (if tuareg-use-char-classes "[:alpha:]" "a-zA-Z"))
(defconst tuareg-font-lock-syntactic-keywords
;; Char constants start with ' but ' can also appear in identifiers.
;; Beware not to match things like '*)hel' or '"hel' since the first '
;; might be inside a string or comment.
'(("\\<\\('\\)\\([^'\\\n]\\|\\\\.[^\\'\n \")]*\\)\\('\\)"
(1 '(7)) (3 '(7)))))
(defun tuareg-font-lock-syntactic-face-function (state)
(if (nth 3 state) font-lock-string-face
(let ((start (nth 8 state)))
(if (and (> (point-max) (+ start 2))
(eq (char-after (+ start 2)) ?*)
(not (eq (char-after (+ start 3)) ?*)))
;; This is a documentation comment
tuareg-doc-face
font-lock-comment-face))))
(when (facep 'font-lock-reference-face)
(defvar font-lock-constant-face)
(if (facep 'font-lock-constant-face) ()
(defvar font-lock-constant-face font-lock-reference-face)
(copy-face font-lock-reference-face 'font-lock-constant-face)))
(when (facep 'font-lock-keyword-face)
(defvar font-lock-preprocessor-face)
(if (facep 'font-lock-preprocessor-face) ()
(defvar font-lock-preprocessor-face font-lock-keyword-face)
(copy-face font-lock-keyword-face 'font-lock-preprocessor-face)))
;; Initially empty, set in `tuareg-install-font-lock'
(defvar tuareg-font-lock-keywords
()
"Font-Lock patterns for Tuareg mode.")
(when (featurep 'sym-lock)
(make-face 'tuareg-font-lock-lambda-face
"Face description for fun keywords (lambda operator).")
(set-face-parent 'tuareg-font-lock-lambda-face
font-lock-function-name-face)
(set-face-font 'tuareg-font-lock-lambda-face
sym-lock-font-name)
;; To change this table, xfd -fn '-adobe-symbol-*--12-*' may be
;; used to determine the symbol character codes.
(defvar tuareg-sym-lock-keywords
'(("<-" 0 1 172 nil)
("->" 0 1 174 nil)
("<=" 0 1 163 nil)
(">=" 0 1 179 nil)
("<>" 0 1 185 nil)
("==" 0 1 186 nil)
("||" 0 1 218 nil)
("&&" 0 1 217 nil)
("[^*]\\(\\*\\)\\." 1 8 180 nil)
("\\(/\\)\\." 1 3 184 nil)
(";;" 0 1 191 nil)
("\\<sqrt\\>" 0 3 214 nil)
("\\<fun\\>" 0 3 108 tuareg-font-lock-lambda-face)
("\\<or\\>" 0 3 218 nil)
("\\<not\\>" 0 3 216 nil))
"If non nil: Overrides default Sym-Lock patterns for Tuareg."))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Keymap
(defvar tuareg-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "|" 'tuareg-electric)
(define-key map ")" 'tuareg-electric-rp)
(define-key map "}" 'tuareg-electric-rc)
(define-key map "]" 'tuareg-electric-rb)
(define-key map "\M-q" 'tuareg-indent-phrase)
(define-key map "\C-c\C-q" 'tuareg-indent-phrase)
(define-key map "\M-\C-\\" 'indent-region)
(define-key map "\C-c\C-a" 'tuareg-find-alternate-file)
(define-key map "\C-c\C-c" 'compile)
(define-key map "\C-xnd" 'tuareg-narrow-to-phrase)
(define-key map "\M-\C-x" 'tuareg-eval-phrase)
(define-key map "\C-x\C-e" 'tuareg-eval-phrase)
(define-key map "\C-c\C-e" 'tuareg-eval-phrase)
(define-key map "\C-c\C-r" 'tuareg-eval-region)
(define-key map "\C-c\C-b" 'tuareg-eval-buffer)
(define-key map "\C-c\C-s" 'tuareg-run-caml)
(define-key map "\C-c\C-i" 'tuareg-interrupt-caml)
(define-key map "\C-c\C-k" 'tuareg-kill-caml)
(define-key map "\C-c\C-n" 'tuareg-next-phrase)
(define-key map "\C-c\C-p" 'tuareg-previous-phrase)
(define-key map [(control c) (home)] 'tuareg-move-inside-block-opening)
(define-key map [(control c) (control down)] 'tuareg-next-phrase)
(define-key map [(control c) (control up)] 'tuareg-previous-phrase)
(define-key map [(meta control down)] 'tuareg-next-phrase)
(define-key map [(meta control up)] 'tuareg-previous-phrase)
(define-key map [(meta control h)] 'tuareg-mark-phrase)
(define-key map "\C-c`" 'tuareg-interactive-next-error-source)
(define-key map "\C-c?" 'tuareg-interactive-next-error-source)
(define-key map "\C-c.c" 'tuareg-insert-class-form)
(define-key map "\C-c.b" 'tuareg-insert-begin-form)
(define-key map "\C-c.f" 'tuareg-insert-for-form)
(define-key map "\C-c.w" 'tuareg-insert-while-form)
(define-key map "\C-c.i" 'tuareg-insert-if-form)
(define-key map "\C-c.l" 'tuareg-insert-let-form)
(define-key map "\C-c.m" 'tuareg-insert-match-form)
(define-key map "\C-c.t" 'tuareg-insert-try-form)
(when tuareg-with-caml-mode-p
;; Trigger caml-types
(define-key map [?\C-c ?\C-t] 'caml-types-show-type)
;; To prevent misbehavior in case of error during exploration.
(define-key map [(control mouse-2)] 'caml-types-mouse-ignore)
(define-key map [(control down-mouse-2)] 'caml-types-explore)
;; Trigger caml-help
(define-key map [?\C-c ?i] 'ocaml-add-path)
(define-key map [?\C-c ?\[] 'ocaml-open-module)
(define-key map [?\C-c ?\]] 'ocaml-close-module)
(define-key map [?\C-c ?h] 'caml-help)
(define-key map [?\C-c ?\t] 'caml-complete))
map)
"Keymap used in Tuareg mode.")
(defvar tuareg-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?_ "_" st)
(modify-syntax-entry ?? ". p" st)
(modify-syntax-entry ?~ ". p" st)
(modify-syntax-entry ?: "." st)
(modify-syntax-entry ?' "w" st) ; ' is part of words (for primes).
(modify-syntax-entry
;; ` is punctuation or character delimiter (Caml Light compatibility).
?` (if tuareg-support-camllight "\"" ".") st)
(modify-syntax-entry ?\" "\"" st) ; " is a string delimiter
(modify-syntax-entry ?\\ "\\" st)
(modify-syntax-entry ?* ". 23" st)
(condition-case nil
(progn
(modify-syntax-entry ?\( "()1n" st)
(modify-syntax-entry ?\) ")(4n" st))
(error ;XEmacs signals an error instead of ignoring `n'.
(modify-syntax-entry ?\( "()1" st)
(modify-syntax-entry ?\) ")(4" st)))
st)
"Syntax table in use in Tuareg mode buffers.")