         (display-buffer caml-types-buffer))
     (unwind-protect
         (sit-for 60)
-      (delete-overlay caml-types-expr-ovl))))
+      (delete-overlay caml-types-expr-ovl)
+      )))
 
 (defun caml-types-preprocess (type-file)
   (let* ((type-date (nth 5 (file-attributes type-file)))

(if (not (face-differs-from-default-p 'caml-types-face))
    (set-face-background 'caml-types-face "#88FF44"))

(make-face 'caml-typed-face)
(set-face-doc-string 'caml-typed-face
                     "face for hilighting typed expressions")
(if (not (face-differs-from-default-p 'caml-typed-face))
    (set-face-background 'caml-typed-face "#FF8844"))

(overlay-put caml-types-expr-ovl 'face 'caml-types-face)


(defvar caml-types-annotation-tree nil)

         target-pos
         Left Right limits cnum node mes type
         (tree caml-types-annotation-tree)
         region
         )
    (caml-types-preprocess type-file)
    (unless caml-types-buffer 
      (setq caml-types-buffer (get-buffer-create caml-types-buffer-name)))
      ;; (message "Drag the mouse to explore types")
    (unwind-protect
        (caml-track-mouse
         (setq region
               (caml-types-typed-make-overlay target-buf
                                        (caml-event-point-start event)))
         (while (and event
                     (integer-or-marker-p
                      (setq cnum (caml-event-point-end event))))
           (if (and region (<= (car region) cnum) (<= cnum (cdr region)))
               (if (and limits (>= cnum (car limits)) (< cnum (cdr limits)))
                   (message mes)
                 (setq target-bol
                       (save-excursion (goto-char cnum)
                                       (caml-line-beginning-position)))
                 (setq target-line
                       (1+ (count-lines (point-min) target-bol)))
                 (setq target-pos (vector target-file target-line target-bol cnum))
                 (save-excursion
                   (setq node (caml-types-find-location target-pos () tree))
                   (set-buffer caml-types-buffer)
                   (erase-buffer)
                   (cond
                    (node
                     (setq Left (caml-types-get-pos target-buf (elt node 0)))
                     (setq Right (caml-types-get-pos target-buf (elt node 1)))
                     (move-overlay caml-types-expr-ovl Left Right target-buf)
                     (setq limits (caml-types-find-interval target-buf target-pos
                                                            node))
                     (setq type (elt node 2))
                     )
                    (t
                     (delete-overlay caml-types-expr-ovl)
                     (setq type "*no type information*")
                     (setq limits (caml-types-find-interval target-buf target-pos
                                                            tree))
                     ))
                   (message (setq mes (format "type: %s" type)))
                   (insert type)
                   )))
             (setq event (caml-read-event))
             (unless (mouse-movement-p event) (setq event nil))
             )
         )
      (delete-overlay caml-types-expr-ovl)
      (delete-overlay caml-types-typed-ovl)
      )))

(defun caml-types-typed-make-overlay (target-buf pos)
  (interactive "p")
  (let ((start pos) (end pos) len node left right)
      (setq len (length caml-types-annotation-tree))
      (while (> len 3)
        (setq len (- len 1))
        (setq node (aref caml-types-annotation-tree len))
        (if (and (equal target-buf (current-buffer))
                 (setq left (caml-types-get-pos target-buf (elt node 0))
                       right (caml-types-get-pos target-buf (elt node 1)))
                 (<= left pos) (>= right pos)
                 )
            (setq start (min start left)
                  end (max end right))
             ))
      (move-overlay caml-types-typed-ovl
                    (max (point-min) (- start 1))
                    (min (point-max) (+ end 1)) target-buf)
    (cons start end)))

(provide 'caml-types)
