   (merlin-view-errors)
 )
 
-
-(defun merlin-extract-complete (l)
+;; COMPLETION
+(defun merlin-extract-complete (prefix l)
+  "Parses and format completion results"
   (mapcar '(lambda (c) 
-	     (concat merlin-prefix 
+	     (concat prefix
 		     (cdr (assoc 'name c)) 
 		     ": " 
 		     (cdr (assoc 'desc c))
 		     ))
 	  (append l nil)))
-;(cdr (assoc 'name c))
 
 (defun merlin-source-action ()
+  "Called when the user has pressed RET on a completion candidate. Remove the garbage"
   (save-excursion
     (let ((endpoint (point)))
       (goto-char merlin-point)

    (process-send-string (merlin-get-process) string)
    (merlin-wait-for-answer)
))
	
(defun merlin-rewind ()
  "Rewind the knowledge of merlin of the current buffer to zero"
  (merlin-send-command "reset" nil nil))

(defun merlin-make-point (line col)
  "Creates a point from a couple line / col"
  (save-excursion
    (beginning-of-line)
    (goto-line line)
    (forward-char col)
    (point)))
(defun merlin-dump-env ()
  "Dump the environment"
  (merlin-send-command "dump" '("env")
		       '(lambda (sexp) 
			  (message "Env: %s" (append (elt sexp 1) nil)))))


(defun merlin-tell-string (mode string)
  "Tell a string to merlin using `mode'"

;; Mode for Merlin


;; json is mandatory
(require 'json)
;; auto-complete is not
(require 'auto-complete nil 'noerror)

;;
;; Variables
;;
(defvar merlin-command "ocamlmerlin"
  "The command for ocamlmerlin in your installation")

(defvar merlin-process nil
  "The merlin process associated to the current buffer")
(defvar merlin-continuation nil
  "The callback to be called with the result of the next command")
(defvar merlin-point nil
  "Stores the point of last completion (beginning of the prefix)")
(defvar merlin-result nil
  "Temporary variables to store command results")
(defvar merlin-debug t
  "If true, logs the data sent and received from merlin")
(defvar merlin-type-buffer
  (get-buffer-create "*merlin types*")
  "The buffer to use to display types"
  )

(defvar merlin-cache nil "Merlin cache for completion")
(defvar merlin-prefix nil "Merlin prefix")
(defvar merlin-name nil "Merlin name")
(defvar merlin-error-overlay nil "Merlin overlay used for errors")

;; 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 \"\""
  (let ((l (butlast (split-string ident "\\."))))
    (let ((s (mapconcat 'identity l ".")))
      (if (string-equal s "") s (concat s ".")))))
(defun merlin-make-point (line col)
  "Creates a point from a couple line / col"
  (save-excursion
    (beginning-of-line)
    (goto-line line)
    (forward-char col)
    (point)))

(defun merlin-ident-under-point ()
  "Returns the ident under point in the current buffer"
  (let ((x (bounds-of-thing-at-point 'symbol)))
    (buffer-substring-no-properties (car x) (cdr x))))

;; PROCESS MANAGEMENT
(defun merlin-make-buffer-name ()
  "Returns the buffer name associated to the current buffer, for the merlin process"
  (concat "*"
	  (buffer-file-name nil)
	  " merlin *"))

(defun merlin-make-process-name ()
  "Returns the process name for the current buffer"
  (concat "merlin-" (buffer-file-name nil)))

(defun merlin-get-process ()
  "Returns the process of the current buffer"
  merlin-process)

(defun merlin-filter (process output)
  "The filter on merlin's output"
