 (put 'ocamlatom 'bounds-of-thing-at-point
      'bounds-of-ocaml-atom-at-point)
 
-(defun merlin-ident-under-point ()
-  "Returns the ident under point in the current buffer"
-  (let ((x (bounds-of-thing-at-point 'ocamlatom)))
-    (if x
-	(buffer-substring-no-properties (car x) (cdr x))
-      nil)))
 ; overlay management
 (defun merlin-create-overlay (var start end face timer)
   "Creates an overlay in the current buffer starting at `start', ending at `end',

  (interactive "p")
  (if (> arg 1)
      (merlin-show-type-of-region)
    (let ((ident (merlin-ident-under-point)))
      (if ident
	  (merlin-show-type ident (merlin-type-of-expression ident))))))

;; .merlin parsing
(defun merlin-add-path (kind dir)

              (end (merlin-make-point (cdr (assoc 'end ret))))
              (type (cdr (assoc 'type ret))))
          (merlin-create-overlay 'merlin-type-overlay start end 'next-error "1 sec")
          (cons (cons start end) type)))))
          

        
    

(defun merlin-type-of-expression-local (bounds exp)
  "Get the type of an expression inside the local context"
  (cons 
   bounds
   (merlin-is-return 
    (merlin-send-command "type"
                         (list "expression" exp "at" 
                               (merlin-unmake-point (point)))))))

(defun merlin-type-of-expression-global (bounds exp)
  "Get the type of an expression globally"
  (cons
   bounds
   (merlin-is-return (merlin-send-command "type" (list "expression" exp)))))


(defun merlin-type-of-expression (bounds exp)
  "Get the type of `exp' whose bounds in the buffer is `bounds'. It uses three techniques to do so:
- type at (node)
- type-expression at (local)
- type expression (global)"
  (or
   (merlin-type-of-expression-node)
   (merlin-type-of-expression-local bounds exp)
   (merlin-type-of-expression-global bounds exp)))

(defun merlin-show-type (bounds &optional quiet)
  "This functions shows the type of the expression inside
`bounds' in the current buffer. If `quiet' is non nil then an
overlay is displayed and module types are displayed in another
buffer. Otherwise only value type are displayed, and without
overlay"
  (let ((result (merlin-type-of-expression bounds
                                        (buffer-substring-no-properties 
                                         (car bounds) (cdr bounds)))))
    (if (not (merlin-is-long (cdr result)))
        (progn
          (if (not quiet)
              (merlin-create-overlay 'merlin-type-overlay 
                                     (caar result) (cdar result) 
                                     'next-error "1 sec"))
          (message "%s" (cdr result)))
      (if (not quiet)
          (progn
            (display-buffer merlin-type-buffer)
              (with-current-buffer merlin-type-buffer
                (erase-buffer)
                (insert (cdr result))))))))


(defun merlin-show-type-of-region ()
  "Show the type of the region"
  (interactive)
  (merlin-show-type ((region-beginning) . (region-end))))

(defun merlin-show-type-of-point-quiet ()
  "Show the type of the identifier under the point if it is short (a value)"
  (merlin-check-synchronize t)
  (merlin-show-type (bounds-of-thing-at-point 'ocamlatom) &optional t))

(defun merlin-show-type-of-point (arg) 
  "Show the type of the identifier under the point. If it is called with a prefix argument, then show the type of the region."
