 	      (merlin-handle-line buf words)
 	      (forward-line))))
 	(switch-to-buffer buf))))
-    
+
+;; Idle 
+(defun merlin-idle-hook ()
+  (if (<= merlin-idle-delay 0.)
+      (cancel-timer merlin-idle-timer)
+    (if merlin-mode
+	(merlin-show-type-minibuffer))))
+
 ;; Mode definition
+(defvar merlin-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "C-c <C-return>") 'merlin-tell-previous-lines)
+    (define-key map (kbd "C-c C-t") 'merlin-show-type)
+    (define-key map (kbd "C-c e") 'merlin-dump-env)
+    map
+    ))
+
 (defun merlin-setup ()
   "Sets up a buffer for use with merlin"
   (interactive)
-  (merlin-start-process)
-  (auto-complete-mode)
-  (setq ac-sources '(merlin-ac-source))
-  (merlin-parse)
-  (with-current-buffer merlin-type-buffer
-    (tuareg-mode))
+  (if (not merlin-mode)
+      (progn
+	(merlin-start-process)
+	(auto-complete-mode)
+	(setq ac-sources '(merlin-ac-source))
+	(merlin-parse)
+	(if (> merlin-idle-delay 0.)
+	    (setq merlin-idle-timer
+		  (run-with-idle-timer merlin-idle-delay t 'merlin-idle-hook)))
+	(with-current-buffer merlin-type-buffer
+	  (tuareg-mode)))
+    
 )
 (define-minor-mode merlin-mode
   "Mode to use merlin tool inside OCaml tools."
   nil
   " merlin"
-  '(("\C-i" . merlin-tell-previous-lines)
-    ("\C-o" . merlin-get-type))
-  :after-hook 'merlin-setup)
+  :keymap merlin-mode-map
+  (if merlin-mode 
+      (merlin-setup)
+    (progn
+      (process-send-eof (merlin-get-process))
+      (cancel-timer merlin-idle-timer)
+      (delete-overlay merlin-overlay)
+)))

(defvar merlin-error-overlay nil "Merlin overlay used for errors")
(defvar merlin-buffer nil "Buffer for merlin input")
(defvar merlin-ready nil "Is reception done?")
;; UTILS
(defun merlin-compute-prefix (ident)
  "Computes the prefix of an identifier. The prefix of Foo.bar is Foo. and the prefix of bar is \"\""

(defun merlin-extract-complete (prefix l)
  "Parses and format completion results"
  (mapcar `(lambda (c) 
	    (concat ,prefix
		    (cdr (assoc 'name c)) 
		    ": " 
