 			 merlin-command)))
     (set (make-local-variable 'merlin-process) p)
     (set-process-filter p 'merlin-filter)))
-
 
 (defun merlin-send-command (name args callback)
   "Send a command to merlin. Callback is called with the sexp result of the command"


(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))))

(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)))
(defun merlin-add-path (kind dir)
  (message "%s ++ %s" kind dir)
  (merlin-send-command "path" (list "add" kind dir) nil))

(defun merlin-get-packages ()
  (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
  )
(defun merlin-use (pkg)
  (interactive
   (list (completing-read "Package to use:" (merlin-get-packages))))
  (merlin-send-command "find" (list "use" pkg) nil))

(defun merlin-handle-line (buffer words)
  (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 ()
  (if (file-exists-p ".merlin")
      (progn
	(setq lines nil)


(defun merlin-tell-piece (mode start end)
  "Tell part of the current buffer to merlin using `mode'"
  (merlin-tell-string mode (buffer-substring start end)))


;; ERRORS
(defun merlin-remove-error-overlay ()
  "Remove the error overlay"
  (delete-overlay merlin-error-overlay))

(defun merlin-error-highlight (start end)
  "Add an overlay between `start' and `end' for errors"
  (setq merlin-overlay
	(make-overlay start end))
  (overlay-put merlin-overlay 'face 'next-error)
  (run-at-time "5 sec" nil 'merlin-remove-overlay)
  )
(defun merlin-handle-errors (errors)
  "Goes to the location of the first error and adds an overlay"
  (let ((message (cdr (assoc 'message errors)))
	(lbeginning (cdr (assoc 'line (cdr (assoc 'start errors)))))
	(cbeginning (cdr (assoc 'col (cdr (assoc 'start errors)))))
