use cooper theme -- end of git, I am trying livemesh
[srid.dotfiles.git] / emacs / external / ml / caml-types.el
blob08c9fd0db8eb0c2c4f3e71e95fc4a5d8f053743a
1 ;(***********************************************************************)
2 ;(* *)
3 ;(* Objective Caml *)
4 ;(* *)
5 ;(* Damien Doligez, projet Moscova, INRIA Rocquencourt *)
6 ;(* *)
7 ;(* Copyright 2003 Institut National de Recherche en Informatique et *)
8 ;(* en Automatique. All rights reserved. This file is distributed *)
9 ;(* under the terms of the GNU General Public License. *)
10 ;(* *)
11 ;(***********************************************************************)
13 ;(* $Id: caml-types.el,v 1.29.6.2 2004/11/15 12:50:54 doligez Exp $ *)
15 ; An emacs-lisp complement to the "-dtypes" option of ocamlc and ocamlopt.
17 ;; XEmacs compatibility
19 (eval-and-compile
20 (if (and (boundp 'running-xemacs) running-xemacs)
21 (require 'caml-xemacs)
22 (require 'caml-emacs)))
26 (defvar caml-types-location-re nil "Regexp to parse *.annot files.
28 Annotation files *.annot may be generated with the \"-dtypes\" option
29 of ocamlc and ocamlopt.
31 Their format is:
33 file ::= block *
34 block ::= position <SP> position <LF> annotation *
35 position ::= filename <SP> num <SP> num <SP> num
36 annotation ::= keyword open-paren <LF> <SP> <SP> data <LF> close-paren
38 <SP> is a space character (ASCII 0x20)
39 <LF> is a line-feed character (ASCII 0x0A)
40 num is a sequence of decimal digits
41 filename is a string with the lexical conventions of O'Caml
42 open-paren is an open parenthesis (ASCII 0x28)
43 close-paren is a closed parenthesis (ASCII 0x29)
44 data is any sequence of characters where <LF> is always followed by
45 at least two space characters.
47 - in each block, the two positions are respectively the start and the
48 - end of the range described by the block.
49 - in a position, the filename is the name of the file, the first num
50 is the line number, the second num is the offset of the beginning
51 of the line, the third num is the offset of the position itself.
52 - the char number within the line is the difference between the third
53 and second nums.
55 For the moment, the only possible keyword is \"type\"."
58 (let* ((caml-types-filename-re "\"\\(\\([^\\\"]\\|\\\\.\\)*\\)\"")
59 (caml-types-number-re "\\([0-9]*\\)")
60 (caml-types-position-re
61 (concat caml-types-filename-re " "
62 caml-types-number-re " "
63 caml-types-number-re " "
64 caml-types-number-re)))
65 (setq caml-types-location-re
66 (concat "^" caml-types-position-re " " caml-types-position-re)))
68 (defvar caml-types-expr-ovl (make-overlay 1 1))
70 (make-face 'caml-types-face)
71 (set-face-doc-string 'caml-types-face
72 "face for hilighting expressions and types")
73 (if (not (face-differs-from-default-p 'caml-types-face))
74 (set-face-background 'caml-types-face "#88FF44"))
76 (defvar caml-types-typed-ovl (make-overlay 1 1))
78 (make-face 'caml-types-typed-face)
79 (set-face-doc-string 'caml-types-typed-face
80 "face for hilighting typed expressions")
81 (if (not (face-differs-from-default-p 'caml-types-typed-face))
82 (set-face-background 'caml-types-typed-face "#FF8844"))
84 (overlay-put caml-types-expr-ovl 'face 'caml-types-face)
85 (overlay-put caml-types-typed-ovl 'face 'caml-types-typed-face)
88 (defvar caml-types-annotation-tree nil)
89 (defvar caml-types-annotation-date nil)
90 (make-variable-buffer-local 'caml-types-annotation-tree)
91 (make-variable-buffer-local 'caml-types-annotation-date)
93 (defvar caml-types-buffer-name "*caml-types*"
94 "Name of buffer for diplaying caml types")
95 (defvar caml-types-buffer nil
96 "buffer for diplaying caml types")
98 (defun caml-types-show-type (arg)
99 "Show the type of expression or pattern at point.
100 The smallest expression or pattern that contains point is
101 temporarily highlighted. Its type is highlighted in the .annot
102 file and the mark is set to the beginning of the type.
103 The type is also displayed in the mini-buffer.
105 Hints on using the type display:
106 . If you want the type of an identifier, put point within any
107 occurrence of this identifier.
108 . If you want the result type of a function application, put point
109 at the first space after the function name.
110 . If you want the type of a list, put point on a bracket, on a
111 semicolon, or on the :: constructor.
112 . Even if type checking fails, you can still look at the types
113 in the file, up to where the type checker failed.
115 Types are also displayed in the buffer *caml-types*, which is
116 displayed when the command is called with Prefix argument 4.
118 See also `caml-types-explore' for exploration by mouse dragging.
119 See `caml-types-location-re' for annotation file format.
121 (interactive "p")
122 (let* ((target-buf (current-buffer))
123 (target-file (file-name-nondirectory (buffer-file-name)))
124 (target-line (1+ (count-lines (point-min)
125 (caml-line-beginning-position))))
126 (target-bol (caml-line-beginning-position))
127 (target-cnum (point))
128 (type-file (concat (file-name-sans-extension (buffer-file-name))
129 ".annot")))
130 (caml-types-preprocess type-file)
131 (unless caml-types-buffer
132 (setq caml-types-buffer (get-buffer-create caml-types-buffer-name)))
133 (let* ((targ-loc (vector target-file target-line target-bol target-cnum))
134 (node (caml-types-find-location targ-loc ()
135 caml-types-annotation-tree)))
136 (cond
137 ((null node)
138 (delete-overlay caml-types-expr-ovl)
139 (message "Point is not within a typechecked expression or pattern.")
140 ; (with-current-buffer type-buf (narrow-to-region 1 1))
143 (let ((left (caml-types-get-pos target-buf (elt node 0)))
144 (right (caml-types-get-pos target-buf (elt node 1)))
145 (type (elt node 2)))
146 (move-overlay caml-types-expr-ovl left right target-buf)
147 (with-current-buffer caml-types-buffer
148 (erase-buffer)
149 (insert type)
150 (message (format "type: %s" type)))
151 ))))
152 (if (and (= arg 4)
153 (not (window-live-p (get-buffer-window caml-types-buffer))))
154 (display-buffer caml-types-buffer))
155 (unwind-protect
156 (caml-sit-for 60)
157 (delete-overlay caml-types-expr-ovl)
160 (defun caml-types-preprocess (type-file)
161 (let* ((type-date (nth 5 (file-attributes type-file)))
162 (target-file (file-name-nondirectory (buffer-file-name)))
163 (target-date (nth 5 (file-attributes target-file))))
164 (unless (and caml-types-annotation-tree
165 type-date
166 caml-types-annotation-date
167 (not (caml-types-date< caml-types-annotation-date type-date)))
168 (if (and type-date target-date (caml-types-date< type-date target-date))
169 (error (format "%s is more recent than %s" target-file type-file)))
170 (message "Reading annotation file...")
171 (let* ((type-buf (caml-types-find-file type-file))
172 (tree (with-current-buffer type-buf
173 (widen)
174 (goto-char (point-min))
175 (caml-types-build-tree target-file))))
176 (setq caml-types-annotation-tree tree
177 caml-types-annotation-date type-date)
178 (kill-buffer type-buf)
179 (message ""))
182 (defun caml-types-date< (date1 date2)
183 (or (< (car date1) (car date2))
184 (and (= (car date1) (car date2))
185 (< (nth 1 date1) (nth 1 date2)))))
188 ; we use an obarray for hash-consing the strings within each tree
190 (defun caml-types-make-hash-table ()
191 (make-vector 255 0))
193 (defun caml-types-hcons (elem table)
194 (symbol-name (intern elem table)))
197 ; tree of intervals
198 ; each node is a vector
199 ; [ pos-left pos-right type-info child child child... ]
200 ; type-info =
201 ; () if this node does not correspond to an annotated interval
202 ; (type-start . type-end) address of the annotation in the .annot file
204 (defun caml-types-build-tree (target-file)
205 (let ((stack ())
206 (accu ())
207 (table (caml-types-make-hash-table))
208 (type-info ()))
209 (while (re-search-forward caml-types-location-re () t)
210 (let ((l-file (file-name-nondirectory (match-string 1)))
211 (l-line (string-to-int (match-string 3)))
212 (l-bol (string-to-int (match-string 4)))
213 (l-cnum (string-to-int (match-string 5)))
214 (r-file (file-name-nondirectory (match-string 6)))
215 (r-line (string-to-int (match-string 8)))
216 (r-bol (string-to-int (match-string 9)))
217 (r-cnum (string-to-int (match-string 10))))
218 (unless (caml-types-not-in-file l-file r-file target-file)
219 (while (and (re-search-forward "^" () t)
220 (not (looking-at "type"))
221 (not (looking-at "\\\"")))
222 (forward-char 1))
223 (setq type-info
224 (if (looking-at
225 "^type(\n\\( \\([^\n)]\\|.)\\|\n[^)]\\)*\\)\n)")
226 (caml-types-hcons (match-string 1) table)))
227 (setq accu ())
228 (while (and stack
229 (caml-types-pos-contains l-cnum r-cnum (car stack)))
230 (setq accu (cons (car stack) accu))
231 (setq stack (cdr stack)))
232 (let* ((left-pos (vector l-file l-line l-bol l-cnum))
233 (right-pos (vector r-file r-line r-bol r-cnum))
234 (node (caml-types-make-node left-pos right-pos type-info
235 accu)))
236 (setq stack (cons node stack))))))
237 (if (null stack)
238 (error "no annotations found for this source file")
239 (let* ((left-pos (elt (car (last stack)) 0))
240 (right-pos (elt (car stack) 1)))
241 (if (null (cdr stack))
242 (car stack)
243 (caml-types-make-node left-pos right-pos () (nreverse stack)))))))
245 (defun caml-types-not-in-file (l-file r-file target-file)
246 (or (and (not (string= l-file target-file))
247 (not (string= l-file "")))
248 (and (not (string= r-file target-file))
249 (not (string= r-file "")))))
251 (defun caml-types-make-node (left-pos right-pos type-info children)
252 (let ((result (make-vector (+ 3 (length children)) ()))
253 (i 3))
254 (aset result 0 left-pos)
255 (aset result 1 right-pos)
256 (aset result 2 type-info)
257 (while children
258 (aset result i (car children))
259 (setq children (cdr children))
260 (setq i (1+ i)))
261 result))
263 (defun caml-types-pos-contains (l-cnum r-cnum node)
264 (and (<= l-cnum (elt (elt node 0) 3))
265 (>= r-cnum (elt (elt node 1) 3))))
267 (defun caml-types-find-location (targ-pos curr node)
268 (if (not (caml-types-pos-inside targ-pos node))
269 curr
270 (if (elt node 2)
271 (setq curr node))
272 (let ((i (caml-types-search node targ-pos)))
273 (if (and (> i 3)
274 (caml-types-pos-inside targ-pos (elt node (1- i))))
275 (caml-types-find-location targ-pos curr (elt node (1- i)))
276 curr))))
278 ; trouve le premier fils qui commence apres la position
279 ; ou (length node) si tous commencent avant
280 (defun caml-types-search (node pos)
281 (let ((min 3)
282 (max (length node))
283 med)
284 (while (< min max)
285 (setq med (/ (+ min max) 2))
286 (if (caml-types-pos<= (elt (elt node med) 0) pos)
287 (setq min (1+ med))
288 (setq max med)))
289 min))
291 (defun caml-types-pos-inside (pos node)
292 (let ((left-pos (elt node 0))
293 (right-pos (elt node 1)))
294 (and (caml-types-pos<= left-pos pos)
295 (caml-types-pos> right-pos pos))))
297 (defun caml-types-find-interval (buf targ-pos node)
298 (let ((nleft (elt node 0))
299 (nright (elt node 1))
300 (left ())
301 (right ())
303 (cond
304 ((not (caml-types-pos-inside targ-pos node))
305 (if (not (caml-types-pos<= nleft targ-pos))
306 (setq right nleft))
307 (if (not (caml-types-pos> nright targ-pos))
308 (setq left nright)))
310 (setq left nleft
311 right nright)
312 (setq i (caml-types-search node targ-pos))
313 (if (< i (length node))
314 (setq right (elt (elt node i) 0)))
315 (if (> i 3)
316 (setq left (elt (elt node (1- i)) 1)))))
317 (cons (if left
318 (caml-types-get-pos buf left)
319 (with-current-buffer buf (point-min)))
320 (if right
321 (caml-types-get-pos buf right)
322 (with-current-buffer buf (point-max))))))
325 ;; Warning: these comparison functions are not symmetric.
326 ;; The first argument determines the format:
327 ;; when its file component is empty, only the cnum is compared.
329 (defun caml-types-pos<= (pos1 pos2)
330 (let ((file1 (elt pos1 0))
331 (line1 (elt pos1 1))
332 (bol1 (elt pos1 2))
333 (cnum1 (elt pos1 3))
334 (file2 (elt pos2 0))
335 (line2 (elt pos2 1))
336 (bol2 (elt pos2 2))
337 (cnum2 (elt pos2 3)))
338 (if (string= file1 "")
339 (<= cnum1 cnum2)
340 (and (string= file1 file2)
341 (or (< line1 line2)
342 (and (= line1 line2)
343 (<= (- cnum1 bol1) (- cnum2 bol2))))))))
345 (defun caml-types-pos> (pos1 pos2)
346 (let ((file1 (elt pos1 0))
347 (line1 (elt pos1 1))
348 (bol1 (elt pos1 2))
349 (cnum1 (elt pos1 3))
350 (file2 (elt pos2 0))
351 (line2 (elt pos2 1))
352 (bol2 (elt pos2 2))
353 (cnum2 (elt pos2 3)))
354 (if (string= file1 "")
355 (> cnum1 cnum2)
356 (and (string= file1 file2)
357 (or (> line1 line2)
358 (and (= line1 line2)
359 (> (- cnum1 bol1) (- cnum2 bol2))))))))
361 (defun caml-types-get-pos (buf pos)
362 (save-excursion
363 (set-buffer buf)
364 (goto-line (elt pos 1))
365 (forward-char (- (elt pos 3) (elt pos 2)))
366 (point)))
368 ; find-file-read-only-noselect seems to be missing from emacs...
369 (defun caml-types-find-file (name)
370 (let (buf)
371 (cond
372 ((setq buf (get-file-buffer name))
373 (unless (verify-visited-file-modtime buf)
374 (if (buffer-modified-p buf)
375 (find-file-noselect name)
376 (with-current-buffer buf (revert-buffer t t)))
378 ((and (file-readable-p name)
379 (setq buf (find-file-noselect name)))
380 (with-current-buffer buf (toggle-read-only 1))
383 (error "No annotation file. You should compile with option \"-dtypes\"."))
385 buf))
387 (defun caml-types-mouse-ignore (event)
388 (interactive "e")
389 nil)
391 (defun caml-types-time ()
392 (let ((time (current-time)))
393 (+ (* (mod (cadr time) 1000) 1000)
394 (/ (cadr (cdr time)) 1000))))
396 (defun caml-types-explore (event)
397 "Explore type annotations by mouse dragging.
399 The expression under the mouse is highlighted and its type is displayed
400 in the minibuffer, until the move is released, much as `caml-types-show-type'.
401 The function uses two overlays.
403 . One overlay delimits the largest region whose all subnodes
404 are well-typed.
405 . Another overlay delimits the current node under the mouse (whose type
406 annotation is beeing displayed).
408 (interactive "e")
409 (set-buffer (window-buffer (caml-event-window event)))
410 (let* ((target-buf (current-buffer))
411 (target-file (file-name-nondirectory (buffer-file-name)))
412 (type-file (concat (file-name-sans-extension (buffer-file-name))
413 ".annot"))
414 (target-line) (target-bol)
415 target-pos
416 Left Right limits cnum node mes type
417 region
418 (window (caml-event-window event))
419 target-tree
420 (speed 100)
421 (last-time (caml-types-time))
422 (original-event event)
424 (select-window window)
425 (unwind-protect
426 (progn
427 (caml-types-preprocess type-file)
428 (setq target-tree caml-types-annotation-tree)
429 (unless caml-types-buffer
430 (setq caml-types-buffer
431 (get-buffer-create caml-types-buffer-name)))
432 ;; (message "Drag the mouse to explore types")
433 (unwind-protect
434 (caml-track-mouse
435 (while event
436 (cond
437 ;; we ignore non mouse events
438 ((caml-ignore-event-p event))
439 ;; we stop when the original button is released
440 ((caml-release-event-p original-event event)
441 (setq event nil))
442 ;; we scroll when the motion is outside the window
443 ((and (caml-mouse-movement-p event)
444 (not (and (equal window (caml-event-window event))
445 (integer-or-marker-p
446 (caml-event-point-end event)))))
447 (let* ((win (caml-window-edges window))
448 (top (nth 1 win))
449 (bottom (- (nth 3 win) 1))
450 mouse
451 time
453 (while (and
454 (caml-sit-for 0 (/ 500 speed))
455 (setq time (caml-types-time))
456 (> (- time last-time) (/ 500 speed))
457 (setq mouse (caml-mouse-vertical-position))
458 (or (< mouse top) (>= mouse bottom))
460 (setq last-time time)
461 (cond
462 ((< mouse top)
463 (setq speed (- top mouse))
464 (condition-case nil
465 (scroll-down 1)
466 (error (message "Beginning of buffer!"))))
467 ((>= mouse bottom)
468 (setq speed (+ 1 (- mouse bottom)))
469 (condition-case nil
470 (scroll-up 1)
471 (error (message "End of buffer!"))))
473 (setq speed (* speed speed))
475 ;; main action, when the motion is inside the window
476 ;; or on orginal button down event
477 ((or (caml-mouse-movement-p event)
478 (equal original-event event))
479 (setq cnum (caml-event-point-end event))
480 (if (and region
481 (<= (car region) cnum) (< cnum (cdr region)))
482 ;; mouse remains in outer region
484 ;; otherwise, reset the outer region
485 (setq region
486 (caml-types-typed-make-overlay
487 target-buf (caml-event-point-start event))))
489 (and limits
490 (>= cnum (car limits)) (< cnum (cdr limits)))
491 ;; inner region is unchanged
493 ;; recompute the inner region and type annotation
494 (setq target-bol
495 (save-excursion
496 (goto-char cnum) (caml-line-beginning-position))
497 target-line (1+ (count-lines (point-min)
498 target-bol))
499 target-pos
500 (vector target-file target-line target-bol cnum))
501 (save-excursion
502 (setq node (caml-types-find-location
503 target-pos () target-tree))
504 (set-buffer caml-types-buffer)
505 (erase-buffer)
506 (cond
507 (node
508 (setq Left
509 (caml-types-get-pos target-buf (elt node 0))
510 Right
511 (caml-types-get-pos target-buf (elt node 1)))
512 (move-overlay
513 caml-types-expr-ovl Left Right target-buf)
514 (setq limits
515 (caml-types-find-interval target-buf
516 target-pos node)
517 type (elt node 2))
520 (delete-overlay caml-types-expr-ovl)
521 (setq type "*no type information*")
522 (setq limits
523 (caml-types-find-interval
524 target-buf target-pos target-tree))
526 (setq mes (format "type: %s" type))
527 (insert type)
529 (message mes)
532 ;; we read next event, unless it is nil, and loop back.
533 (if event (setq event (caml-read-event)))
536 ;; delete overlays at end of exploration
537 (delete-overlay caml-types-expr-ovl)
538 (delete-overlay caml-types-typed-ovl)
540 ;; When an error occurs, the mouse release event has not been read.
541 ;; We could wait for mouse release to prevent execution of
542 ;; a binding of mouse release, such as cut or paste.
543 ;; In most common cases, next event will be the mouse release.
544 ;; However, it could also be a key stroke before mouse release.
545 ;; Emacs does not allow to test whether mouse is up or down.
546 ;; Not sure it is robust to loop for mouse release after an error
547 ;; occured, as is done for exploration.
548 ;; So far, we just ignore next event. (Next line also be uncommenting.)
549 (if event (caml-read-event))
552 (defun caml-types-typed-make-overlay (target-buf pos)
553 (interactive "p")
554 (let ((start pos) (end pos) len node left right)
555 (setq len (length caml-types-annotation-tree))
556 (while (> len 3)
557 (setq len (- len 1))
558 (setq node (aref caml-types-annotation-tree len))
559 (if (and (equal target-buf (current-buffer))
560 (setq left (caml-types-get-pos target-buf (elt node 0))
561 right (caml-types-get-pos target-buf (elt node 1)))
562 (<= left pos) (> right pos)
564 (setq start (min start left)
565 end (max end right))
567 (move-overlay caml-types-typed-ovl
568 (max (point-min) (- start 1))
569 (min (point-max) (+ end 1)) target-buf)
570 (cons start end)))
572 (provide 'caml-types)