 
 (ac-define-source "merlin" merlin-ac-source)
 
-(defun merlin-ident-under-point ()
-  (let ((x (bounds-of-thing-at-point 'symbol)))
-    (buffer-substring-no-properties (car x) (cdr x))))
-
+;; Get the type of an element"
 (defun merlin-get-type () 
   (interactive)
-  (merlin-send-command "complete" (list "prefix" (merlin-ident-under-point))
-		       '(lambda (sexp)
-			  (if (= (length (elt sexp 1)) 0) 
-			      (message "nothing found for %s" (merlin-ident-under-point))
-			    (let ((ans (elt (elt sexp 1) 0)))
-			      (if (string-equal (cdr (assoc 'kind ans)) "Value")
-				  (message "%s" (cdr (assoc 'desc ans)))
-				(progn
-				  (display-buffer merlin-type-buffer)
-				  (with-current-buffer merlin-type-buffer
-				    (erase-buffer)
-				    (insert (cdr (assoc 'info ans)))))
-))))))
-
-;; .merlin
-(defun merlin-parse-line (l)
-  (let ((words (split-string (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
-    (push words l)))
+  (let ((sexp (merlin-get-completion (merlin-ident-under-point))))
+    (if (= (length (elt sexp 1)) 0) 
+	(message "nothing found for %s" (merlin-ident-under-point))
+      (let ((ans (elt (elt sexp 1) 0)))
+	(if (string-equal (cdr (assoc 'kind ans)) "Value")
+	    (message "%s" (cdr (assoc 'desc ans)))
+	  (progn
+	    (display-buffer merlin-type-buffer)
+	    (with-current-buffer merlin-type-buffer
+	      (erase-buffer)
+	      (insert (cdr (assoc 'info ans))))))))))
+
+;; .merlin parsing
 (defun merlin-add-path (kind dir)
-  (message "%s ++ %s" kind dir)
+  "Adds an item to a path in merlin"
   (merlin-send-command "path" (list "add" kind dir) nil))
 
 (defun merlin-get-packages ()
+  "Get the list of available findlib package"
   (setq merlin-packages nil)
-  (merlin-send-command "find" '("list")
-		       '(lambda (output)
-			  (setq merlin-packages (append (elt output 1) nil))))
-  (sleep-for 0.05)
-  merlin-packages
-  )
+  (append (elt (merlin-send-command-sync "find" '("list")) 1) nil))
 (defun merlin-use (pkg)
+  "Use a package in the current session of merlin"
   (interactive
    (list (completing-read "Package to use:" (merlin-get-packages))))
   (merlin-send-command "find" (list "use" pkg) nil))
 
 (defun merlin-handle-line (buffer words)
+  "Handles a line in a .merlin file"
   (with-current-buffer buffer
     (cond
      ((string-equal (elt words 0) "S") (merlin-add-path "source" (elt words 1)))
      ((string-equal (elt words 0) "B") (merlin-add-path "build" (elt words 1)))
      ((string-equal (elt words 0) "PKG") (merlin-use (elt words 1))))))
 (defun merlin-parse ()
+  "Parses a .merlin"
   (if (file-exists-p ".merlin")
       (progn
 	(setq lines nil)

      (delete-region (point) endpoint)))
)

(defun merlin-compute-prefix (ident)
  (let ((l (butlast (split-string ident "\\."))))
    (let ((s (mapconcat 'identity l ".")))
      (if (string-equal s "") s (concat s ".")))))
	
    
(defun merlin-complete-identifier (ident)
  (setq merlin-prefix (merlin-compute-prefix ident))
  (merlin-send-command "complete" (list "prefix" ident) 
		       '(lambda (output)
			  (setq merlin-cache 
				(merlin-extract-complete (elt output 1)))
			  )))

(defun merlin-source-init ()
  (setq merlin-cache nil)
  (setq merlin-point ac-point)
  (merlin-complete-identifier ac-prefix)
  (sleep-for 0.05)
  merlin-cache)
  

(defvar merlin-ac-source

			    (message "ok")))))

(defun merlin-tell-previous-lines ()
  "Tell merlin the lines up to the point"
  (interactive)
  (merlin-rewind)
  (save-excursion
