-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyperex.el
executable file
·5506 lines (4937 loc) · 218 KB
/
typerex.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; TypeRex OCaml Studio ;
; ;
; Tiphaine Turpin ;
; ;
; Copyright 2011-2012 INRIA Saclay - Ile-de-France / OCamlPro ;
; All rights reserved. This file is distributed under the terms of ;
; the GNU Public License version 3.0. ;
; ;
; TypeRex 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. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This file defines a set of faces for use with new TypeRex syntax
;; coloring, both on dark or light background.
(defgroup typerex-new-faces nil
"Special faces for the new TypeRex syntax coloring."
:group 'typerex-syntax-coloring)
(defun ocp-face-name (name)
(intern (concat "ocp-face-" (symbol-name name))))
(defun ocp-face-same (name desc)
(progn
(eval `(defface ,(ocp-face-name name) `(
(t
,desc
)) ,(format "Face for %s tokens" name)
:group 'typerex-new-faces))
; (eval `(defvar ,name `,name))
))
(defun ocp-face-light-dark (name light dark)
(eval `(defface ,(ocp-face-name name) `(
(((background light))
,light
)(t
,dark
)) ,(format "Face for %s tokens" name)
:group 'typerex-new-faces)))
(defun ocp-face (f)
(if (eq (length f) 3)
(ocp-face-light-dark (car f) (cadr f) (cadr (cdr f)))
(ocp-face-same (car f) (cadr f))))
;; The following defines a list of faces by means of entries of the
;; following format: (<name> <light background> [<dark background>])
(defconst ocp-faces `(
;; Keywords
(governing
(:weight ultra-bold :foreground "#3C323C")
(:weight ultra-bold :foreground "gold"))
(keyword
(:inherit font-lock-keyword-face)
(:weight ultra-bold :foreground "firebrick"))
(open-include
(:foreground "red1" :weight ultra-bold))
; (:inherit font-lock-warning-face))
;; Symbols and punctuation
(punctuation
(:weight ultra-bold)
(:foreground "gray85" :weight ultra-bold))
(bar-case
(:weight ultra-bold :foreground "purple3" :background "linen")
(:weight ultra-bold :foreground "MediumOrchid3" :background "purple4"))
(func
(:weight ultra-bold :foreground "purple3"))
(op-field
(:weight ultra-bold)
(:foreground "gray85" :weight ultra-bold))
;; Operators
(op
(:weight ultra-bold :foreground "sienna"))
(op-bool
(:weight ultra-bold :foreground "brown"))
;; Literals
(constant
(:inherit font-lock-constant-face))
(string
(:inherit font-lock-string-face))
;; ', `, ~, ?
(tag
(:weight ultra-bold :foreground "sienna"))
;; Idents
(type-occ
(:foreground "OliveDrab4"))
(type-def
(:weight bold :inherit ,(ocp-face-name 'type-occ)))
(type-variable
(:weight bold :foreground "dark violet"))
(cstr-occ
(:inherit font-lock-constant-face)
(:foreground "medium sea green"))
(cstr-def
(:weight bold :inherit ,(ocp-face-name 'cstr-occ))
(:weight bold :foreground "medium sea green"))
(field-occ
(:inherit font-lock-constant-face)
(:foreground "medium sea green"))
(field-def
(:weight bold :inherit ,(ocp-face-name 'field-occ))
(:weight bold :foreground "medium sea green"))
(variant
(:inherit font-lock-constant-face))
(val-occ-fun
(:foreground "blue1")
(:foreground "light sky blue"))
(val-occ
(:inherit font-lock-variable-name-face)
(:foreground "peru"))
(val-def-fun
(:weight bold :inherit ,(ocp-face-name 'val-occ-fun))
(:weight bold :foreground "light sky blue"))
(val-def
(:weight bold :inherit ,(ocp-face-name 'val-occ)))
(method-occ
(:inherit ,(ocp-face-name 'val-occ)))
(method-def
(:weight bold :inherit ,(ocp-face-name 'method-occ)))
(mod-occ
(:foreground "dark orange"))
(mod-def
(:weight bold :inherit ,(ocp-face-name 'mod-occ)))
(builtin
(:inherit font-lock-builtin-face))
;; Comments
(comment
(:foreground "gray50"))
(doc
(:foreground "steel blue"))
(doc-title
(:weight bold :foreground "black" :background "light blue"))
(doc-italic
(:slant italic :inherit ,(ocp-face-name 'doc)))
(doc-bold
(:weight bold :inherit ,(ocp-face-name 'doc)))
(doc-keyword
(:foreground "magenta4"))
(doc-stop
(:inherit font-lock-warning-face))
;; Lexing error
(error
(:inherit typerex-font-lock-error-face))
(highlight-occ
(:background "green yellow")
(:background "coral4"))
(highlight-def
(:background "green")
(:background "purple4"))
))
(mapc 'ocp-face ocp-faces)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; TypeRex OCaml Studio ;
; ;
; Thomas Gazagnaire, Fabrice Le Fessant ;
; ;
; Copyright 2011-2012 OCamlPro ;
; All rights reserved. This file is distributed under the terms of ;
; the GNU Public License version 3.0. ;
; ;
; TypeRex 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. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "LOADING TYPEREX")
;;(setq debug-on-error t)
(defun exec-in-buffer (tmpbuf command)
(let ((commandstring (mapconcat 'identity command " ")))
(message commandstring)
(let* ((process-name (car command))
(process-args (cdr command))
(exit-status
(apply
'call-process
(append
(list process-name nil tmpbuf nil)
process-args))))
(message "command exited with code %d (output=%s)" exit-status
(if (equal tmpbuf nil) "<none>" tmpbuf))
(if (not (eq exit-status 0))
(if (equal tmpbuf nil)
(error process-name)
(error (with-current-buffer tmpbuf
(concat commandstring "\n" (buffer-string)))))))))
(defun check-position (pos)
(min (point-max) (max (point-min) pos)))
(defun check-byte (pos)
(min (position-bytes (point-max)) (max (position-bytes (point-min)) pos)))
(defun line-column-bytes ()
(let ((l (line-number-at-pos))
(c (- (position-bytes (point))
(position-bytes (line-beginning-position)))))
(format "%d %d" l c)))
(defun line-column-to-pos (l c)
(save-excursion
(goto-char (point-min))
(byte-to-position (+ (position-bytes (line-beginning-position l)) c))))
(defun highlight-overlay (face start end)
"put an overlay on the given range, in the current buffer"
(let ((overlay (make-overlay (check-position start) (check-position end))))
(overlay-put overlay 'face face)
overlay))
(defun highlight-region (pos)
(highlight-overlay (car pos) (cadr pos) (cadr (cdr pos))))
(defun do-at-next-input (f arg)
(let ((inhibited inhibit-quit))
(setq inhibit-quit t)
(sit-for 10000)
(apply f (list arg))
(setq inhibit-quit inhibited)))
(defun highlight (face start end)
(let ((overlay (highlight-region (list face start end))))
(do-at-next-input
'delete-overlay overlay)))
(defun highlight-regions (forever regions)
(let ((overlays (mapcar 'highlight-region regions)))
(unless (eq forever t)
(do-at-next-input
(lambda (ovs) (mapc 'delete-overlay ovs)) overlays))
nil))
(defun display-temp (buffer-name contents)
"like display-message-or-buffer, but not permanent"
(display-message-or-buffer contents buffer-name)
(do-at-next-input
(lambda (buffer-name)
(condition-case nil
(kill-buffer buffer-name)
(error ())))
buffer-name))
;; Copied from Emacs itself (simple.el) !
(defun fits-in-echo-area ()
"tests whether a buffer's contents fits in the echo area"
(let ((lines
(if (= (buffer-size) 0)
0
(count-screen-lines nil nil nil (minibuffer-window)))))
(or (<= lines 1)
(<= lines
(if resize-mini-windows
(cond ((floatp max-mini-window-height)
(* (frame-height)
max-mini-window-height))
((integerp max-mini-window-height)
max-mini-window-height)
(t
1))
1)))))
(defun y-or-n-check-height (prompt)
"prompt for a single character answer as y-or-n-p, but use a
buffer to display the question if it is too long."
(with-temp-buffer
(insert prompt)
(if (fits-in-echo-area)
(y-or-n-p prompt)
(display-buffer (current-buffer))
(let ((answer (y-or-n-p prompt)))
(kill-buffer (current-buffer))
answer))))
(defun set-text-properties-region (pos)
(add-text-properties (car pos) (cadr pos) (cadr (cdr pos))))
(defun propertize-regions (regions)
(mapc 'set-text-properties-region regions)
nil)
(defun propertize-region-list (regions)
(let ((props (car regions)))
(mapc
(lambda (pos)
(add-text-properties (car pos) (cadr pos) props))
(cadr regions)))
nil)
(defun propertize-region-list-byte (regions)
(let ((props (car regions)))
(mapc
(lambda (pos)
(let ((start (check-byte (car pos)))
(end (check-byte (cadr pos))))
(add-text-properties
(byte-to-position start) (byte-to-position end) props)))
(cadr regions)))
nil)
(defun propertize-region-lists-char (regions)
(mapc 'propertize-region-list regions)
nil)
(defun propertize-region-lists-byte (regions)
(mapc 'propertize-region-list-byte regions)
nil)
(defun get-buffer-create-clear (name)
"return a buffer with the given name, clearing it if necessary"
(let ((buffer (get-buffer-create name)))
(with-current-buffer buffer
(widen)
(delete-region (point-min) (point-max)))
buffer))
(defun create-buffer (buffer-name) (get-buffer-create-clear buffer-name))
(defun set-cleared-buffer (buffer-name)
(set-buffer (get-buffer-create-clear buffer-name)))
(defun string-of-buffer (buffer)
(with-current-buffer buffer
(buffer-string)))
(defun int-of-string (str)
(if (equal str "0")
0
(let ((i (string-to-number str)))
(if (equal i 0)
nil
i))))
(defun current-line ()
"Return the vertical position of point..."
(+ (count-lines (window-start) (point))
(if (= (current-column) 0) 1 0)
-1))
(defun strings-of-buffer (buffer)
(with-current-buffer buffer
(split-string (buffer-string) "[\n]+")))
(defun goto-position (pos)
(forward-char (- (+ 1 pos) (point))))
(defun current-ident ()
(save-excursion
(let (start end)
(search-backward-regexp "[^a-zA-Z._0-9]")
(forward-char 1)
(setq start (point))
(search-forward-regexp "[^a-zA-Z._0-9]" nil 0)
(if (eq (point) (point-max))
(setq end (point))
(setq end (- (point) 1)))
(buffer-substring start end))))
(defun delete-current-ident ()
(let (start end end-buffer)
(search-backward-regexp "[^a-zA-Z._0-9]")
(forward-char 1)
(setq start (point))
(search-forward-regexp "[^a-zA-Z._0-9]" nil 0)
(if (eq (point) (point-max))
(progn
(setq end (point))
(setq end-buffer 1))
(progn
(setq end (- (point) 1))
(setq end-buffer 0)))
(delete-region start end)
(if (equal end-buffer 0)
(backward-char 1))))
(defun revert-with-history ()
"revert the current buffer while keeping its history"
(let ((pos (point)))
(save-excursion
;; (setq scroll (window-vscroll))
;; (setq auto-window-vscroll nil)
(clear-visited-file-modtime)
(widen)
(setq inhibit-modification-hooks t)
(delete-region (point-min) (point-max))
(setq inhibit-modification-hooks nil)
(insert-file-contents (buffer-file-name))
)
(goto-char pos)
(set-buffer-modified-p nil)
(set-visited-file-modtime)
))
(defun revert-buffer-visiting (filename)
"revert the buffer visiting filename, if any"
(let ((buffer (find-buffer-visiting filename)))
(unless (eq buffer nil)
(with-current-buffer buffer
;; workaround for the duplicate insertion bug
;; (revert-buffer t t t))
(revert-with-history)
(setq buffer-undo-list nil)
(push '(apply ocp-undo) buffer-undo-list)
))))
(defun save-buffer-visiting (filename)
"revert the buffer visiting filename, if any"
(let ((buffer (find-buffer-visiting filename)))
(unless (eq buffer nil)
(with-current-buffer buffer
(save-buffer)))))
(defun renamed-file (old-filename new-filename)
"update the buffer visiting a renamed filename, if any"
(let ((buffer (find-buffer-visiting old-filename)))
(unless (eq buffer nil)
(with-current-buffer buffer
(set-visited-file-name new-filename t t)
;; workaround for the duplicate insertion bug
;; (revert-buffer t t t))
(revert-with-history)
(setq buffer-undo-list nil)
(push '(apply ocp-undo) buffer-undo-list)
(reset-tokenization)
))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; TypeRex OCaml Studio ;
; ;
; Tiphaine Turpin ;
; ;
; Copyright 2011-2012 INRIA Saclay - Ile-de-France / OCamlPro ;
; All rights reserved. This file is distributed under the terms of ;
; the GNU Public License version 3.0. ;
; ;
; TypeRex 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 lisp implementation of our RPC protocol.
;; Usage:
;; - Connect to the listening server using by evaluating
;; (start-connection port-number)
;; or setup a server with
;; (start-rpc-server)
;; The protocol is the one implemented by class
;; Server.tagged_connection
;; - Then use
;; (ocp-rpc-string-command command)
;; which behaves as method send_tagged_command, to execute requests.
;; - Callbacks are processed by calling the function
;; ocp-rpc-process-callback on the string representing the
;; callback. The ocp-rpc-process-callback function is responsible
;; for error encoding.
;; - Accepting requests is not implemented, since we don't use it in
;; TypeRex because Emacs initiates the requests.
(require 'tq)
(defconst ocp-rpc-end-of-message "END_OF_MESSAGE"
"mark for the client to detect the end of the answer")
(defconst ocp-rpc-request-start "REQUEST_START"
"mark for the client to detect the beginning of a request")
(defconst ocp-rpc-request-start-length (+ 1 (length ocp-rpc-request-start)))
(defvar ocp-rpc-connection nil
"connection process, (used, e.g. for sending data)")
(defvar ocp-rpc-queue nil
"transaction queue on ocp-rpc-connection")
(defvar ocp-rpc-connection-server nil
"server process")
(defun ocp-rpc-make-transaction (c)
"send a message and get the expected reply. Both messages can
be either requests or answers"
;; (message "sending message %s" c)
(let ((reply (make-vector 1 nil)))
(tq-enqueue
ocp-rpc-queue
(concat c "\n" ocp-rpc-end-of-message "\n")
(concat "\n" ocp-rpc-end-of-message "\n")
reply
(lambda (reply answer)
(setq answer (substring answer 0 (- (+ 2 (length ocp-rpc-end-of-message)))))
;; (message "received message %s" answer)
(aset reply 0 answer))
t)
(let ((quit-inhibited inhibit-quit))
(setq inhibit-quit t)
(while
(and (eq (aref reply 0) nil)
(eq (process-status ocp-rpc-connection) 'open))
(accept-process-output ocp-rpc-connection 1 0))
(unless (eq (process-status ocp-rpc-connection) 'open)
(signal 'error '("Error: connection closed")))
(setq inhibit-quit quit-inhibited)
(aref reply 0)))
)
(defun ocp-rpc-get-answer (c)
"send a message and get a result"
(let ((answer (ocp-rpc-make-transaction c)))
(if (and (>= (length answer) ocp-rpc-request-start-length)
(string=
(substring answer 0 ocp-rpc-request-start-length)
(concat ocp-rpc-request-start "\n")))
(let ((callback (substring answer (length ocp-rpc-request-start))))
;; (message "received callback %s" callback)
(let ((result (ocp-rpc-process-callback callback)))
;; (message "sending result %s" result)
(ocp-rpc-get-answer result)))
answer)))
(defun ocp-rpc-string-command (c)
"send a command and get the expected result"
(ocp-rpc-get-answer (concat ocp-rpc-request-start "\n" c)))
(defun ocp-rpc-start-connection (port)
"start connection by listening on specified port"
(setq max-lisp-eval-depth 10000)
(setq ocp-rpc-connection
(open-network-stream "ocp-wizard-client" nil 'local port))
(set-process-filter ocp-rpc-connection 'ocp-rpc-connection-filter)
(set-process-query-on-exit-flag ocp-rpc-connection nil)
)
(defun ocp-rpc-connection-sentinel (process event)
(if (string= (substring event 0 4) "open")
(progn
;; (message "connected")
(setq ocp-rpc-connection process)
(setq ocp-rpc-queue (tq-create ocp-rpc-connection))
(set-process-query-on-exit-flag ocp-rpc-connection nil))
))
(defun ocp-rpc-start-server ()
"start a server connection and return the listening port"
(setq max-lisp-eval-depth 10000)
(setq ocp-rpc-connection nil)
(setq ocp-rpc-connection-server
(make-network-process
:name "ocp-connection"
:server 0 :host "127.0.0.1" :family 'ipv4 :service t :reuseaddr))
(set-process-sentinel ocp-rpc-connection-server 'ocp-rpc-connection-sentinel)
(set-process-query-on-exit-flag ocp-rpc-connection-server nil)
(let ((internal
;; try to be compatible with older Emacs
(if (coding-system-p 'emacs-internal) 'emacs-internal 'emacs-mule)))
(set-process-coding-system ocp-rpc-connection-server internal internal))
(process-contact ocp-rpc-connection-server :service)
)
(defconst typerex-version "1.0.1"
"Version of TypeRex")
;;; ocp.el --- Caml mode for (X)Emacs.
;; Copyright (C) 1997-2006 Albert Cohen, all rights reserved.
;; Copyright (C) 2009-2010 Jane Street Holding, LLC.
;; Copyright (C) 2011 OCamlPro SAS
;; 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:
(eval-when-compile (require 'cl))
(require 'easymenu)
(defconst typerex-mode-version
(concat "TypeRex Version " typerex-version " ("
(eval-when-compile
(let ((file (or (and (boundp 'byte-compile-current-file)
byte-compile-current-file)
load-file-name)))
(when file
(setq file (expand-file-name "version"
(file-name-directory file))))
(with-temp-buffer
(if (and file (file-exists-p file))
(insert-file-contents-literally file)
(let ((default-directory
(if file
(file-name-directory file)
default-directory)))
(cond ((file-directory-p ".hg")
(call-process "hg" nil t nil "id" "-i" "--debug"))
((file-directory-p ".svn")
(shell-command "svn info | grep Revision: | sed 's/Revision: //'" t))
((file-directory-p ".bzr")
(shell-command "bzr log -l -1 | grep revno:" t))
(t (insert "unknown\n")))))
(buffer-substring-no-properties
(point-min) (1- (point-max))))))
")")
" Copyright (C) 1997-2006 Albert Cohen, all rights reserved.
Copyright (C) 2009-2010 Jane Street Holding, LLC.
Copyright (C) 2011 OCamlPro SAS
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 typerex-with-xemacs (featurep 'xemacs))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compatibility functions
(defun typerex-editing-ls3 ()
"Tells whether we are editing Lucid Synchrone syntax."
(string-match "\\.ls" (buffer-name)))
(defun typerex-editing-camllex ()
"Tells whether we are editing CamlLex syntax."
(string-match "\\.mll" (buffer-name)))
(defalias 'typerex-match-string
(if (fboundp 'match-string-no-properties)
'match-string-no-properties
'match-string))
(or (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 typerex-with-caml-mode-p
(and (require 'caml-types nil t) (require 'caml-help nil t)))
(eval-when-compile
(autoload 'caml-complete "caml-help"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User customizable variables
;; Use the standard `customize' interface or `typerex-mode-hook' to
;; Configure these variables
(require 'custom)
(defgroup ocp nil
"Support for the OCaml language."
:group 'languages)
(defgroup typerex-syntax-coloring nil
"Syntax coloring using Font Lock."
:group 'ocp)
(defgroup typerex-tuareg-faces nil
"Special faces for Tuareg syntax coloring."
:group 'typerex-syntax-coloring)
(defgroup typerex-indent nil
"TypeRex indentation configuration."
:group 'ocp)
(defgroup typerex-auto-complete nil
"TypeRex auto completion."
:group 'ocp)
(defgroup typerex-interactive nil
"TypeRex interactive interpreter configuration."
:group 'ocp)
(defgroup typerex-misc nil
"TypeRex miscellaneous configuration."
:group 'ocp)
;; Comments
(defcustom typerex-indent-leading-comments t
"*If true, indent leading comment lines (starting with `(*') like others."
:group 'typerex-indent :type 'boolean)
(defcustom typerex-indent-comments t
"*If true, automatically align multi-line comments."
:group 'typerex-indent :type 'boolean)
(defcustom typerex-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 `typerex-comment-end-extra-indent' = 1."
:group 'typerex-indent
: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 typerex-support-leading-star-comments t
"*Enable automatic intentation of comments of the form
(*
* ...
*)
Documentation comments (** *) are not concerned by this variable
unless `typerex-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 `typerex-comment-end-extra-indent' to 1."
:group 'typerex-indent :type 'boolean)
(defcustom typerex-leading-star-in-doc nil
"*Enable automatic intentation of documentation comments of the form
(**
* ...
*)"
:group 'typerex-indent :type 'boolean)
;; Indentation defaults
(defcustom typerex-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 'typerex-indent :type 'integer)
(defcustom typerex-support-camllight nil
"*If true, handle Caml Light character syntax (incompatible with labels)."
:group 'typerex-indent :type 'boolean
:set '(lambda (var val)
(setq typerex-support-camllight val)
(when (boundp 'typerex-mode-syntax-table)
(modify-syntax-entry ?` (if val "\"" ".")
typerex-mode-syntax-table))))
(defcustom typerex-support-metaocaml nil
"*If true, handle MetaOCaml syntax."
:group 'typerex-indent :type 'boolean
:set '(lambda (var val)
(setq typerex-support-metaocaml val)
(when (boundp 'typerex-font-lock-keywords)
(typerex-make-indentation-regexps)
(typerex-install-font-lock))))
(defcustom typerex-let-always-indent t
"*If true, enforce indentation is at least `typerex-let-indent' after a `let'.
As an example, set it to false when you have `typerex-with-indent' set to 0,
and you want `let x = match ... with' and `match ... with' indent the
same way."
:group 'typerex-indent :type 'boolean)
(defcustom typerex-pipe-extra-unindent typerex-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 example, 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
`typerex-pipe-extra-unindent' to its default value) yields:
match ... with
X -> ...
| Y -> ...
| Z -> ..."
:group 'typerex-indent :type 'integer)
(defcustom typerex-class-indent typerex-default-indent
"*How many spaces to indent from a `class' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-sig-struct-align t
"*Align `sig' and `struct' keywords with `module'."
:group 'typerex-indent :type 'boolean)
(defcustom typerex-sig-struct-indent typerex-default-indent
"*How many spaces to indent from a `sig' or `struct' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-method-indent typerex-default-indent
"*How many spaces to indent from a `method' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-begin-indent typerex-default-indent
"*How many spaces to indent from a `begin' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-for-while-indent typerex-default-indent
"*How many spaces to indent from a `for' or `while' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-do-indent typerex-default-indent
"*How many spaces to indent from a `do' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-fun-indent typerex-default-indent
"*How many spaces to indent from a `fun' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-function-indent typerex-default-indent
"*How many spaces to indent from a `function' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-if-then-else-indent typerex-default-indent
"*How many spaces to indent from an `if', `then' or `else' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-let-indent typerex-default-indent
"*How many spaces to indent from a `let' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-in-indent 0 ; typerex-default-indent
"*How many spaces to indent from a `in' keyword.
Upstream <http://caml.inria.fr/resources/doc/guides/guidelines.en.html>
recommends 0, and this is what we default to since 2.0.1
instead of the historical `typerex-default-indent'."
:group 'typerex-indent :type 'integer)
(defcustom typerex-match-indent typerex-default-indent
"*How many spaces to indent from a `match' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-try-indent typerex-default-indent
"*How many spaces to indent from a `try' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-with-indent typerex-default-indent
"*How many spaces to indent from a `with' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-rule-indent typerex-default-indent
"*How many spaces to indent from a `rule' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-type-indent typerex-default-indent
"*How many spaces to indent from a `type' keyword."
:group 'typerex-indent :type 'integer)
(defcustom typerex-val-indent typerex-default-indent
"*How many spaces to indent from a `val' keyword."
:group 'typerex-indent :type 'integer)
;; Automatic indentation
;; Using abbrev-mode and electric keys
(defcustom typerex-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 'typerex-indent :type 'boolean
:set '(lambda (var val)
(setq typerex-use-abbrev-mode val)
(abbrev-mode val)))
(defcustom typerex-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 'typerex-indent :type 'boolean)
(defcustom typerex-case-arrow-extra-indent 2
"*How many spaces to indent from a `->' keyword in a pattern match case."
:group 'typerex-indent :type 'integer)
(defcustom typerex-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 `typerex-electric-indent' on."
:group 'typerex-misc :type 'boolean)
;; TypeRex-Interactive
;; Configure via `typerex-mode-hook'
(defcustom typerex-interactive-scroll-to-bottom-on-output nil
"*Controls when to scroll to the bottom of the interactive buffer
upon evaluating an expression.
See `comint-scroll-to-bottom-on-output' for details."
:group 'typerex-interactive :type 'boolean
:set '(lambda (var val)
(setq typerex-interactive-scroll-to-bottom-on-output val)
(when (boundp 'comint-scroll-to-bottom-on-output)
(setq comint-scroll-to-bottom-on-output val))))
(defcustom typerex-skip-after-eval-phrase t
"*Non-nil means skip to the end of the phrase after evaluation in the
Caml toplevel."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-interactive-read-only-input nil
"*Non-nil means input sent to the Caml toplevel is read-only."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-interactive-echo-phrase t
"*Non-nil means echo phrases in the toplevel buffer when sending
them to the Caml toplevel."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-interactive-input-font-lock t
"*Non nil means Font-Lock for toplevel input phrases."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-interactive-output-font-lock t
"*Non nil means Font-Lock for toplevel output messages."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-interactive-error-font-lock t
"*Non nil means Font-Lock for toplevel error messages."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-display-buffer-on-eval t
"*Non nil means pop up the Caml toplevel when evaluating code."
:group 'typerex-interactive :type 'boolean)
(defcustom typerex-manual-url "http://caml.inria.fr/pub/docs/manual-ocaml/index.html"
"*URL to the OCaml reference manual."
:group 'typerex-misc :type 'string)
(defcustom typerex-typerex-manual-url "http://www.typerex.org/manual-ide.html"
"*URL to the TypeRex user manual."
:group 'typerex-misc :type 'string)
(defcustom typerex-browser 'browse-url
"*Name of function that displays the Caml reference manual.
Valid names are `browse-url', `browse-url-firefox', etc."
:group 'typerex-misc)
(defcustom typerex-library-path nil
"*Path to the Caml library. If nil, get it from the TypeRex server"
:group 'typerex-misc :type 'string)