   (set (make-local-variable 'merlin-ready) nil)
   (set (make-local-variable 'merlin-pending-errors) nil)
   (set (make-local-variable 'merlin-pending-errors-overlays) nil)
-  (set (make-local-variable 'merlin-type-overlay) nil)
   (set (make-local-variable 'merlin-lock-zone-highlight-overlay) nil)
   (set (make-local-variable 'merlin-lock-zone-margin-overlay) nil)
   (set (make-local-variable 'merlin--project-file) nil)
   (set (make-local-variable 'merlin-enclosing-types) nil)
   (set (make-local-variable 'merlin-enclosing-offset) nil)
   (set (make-local-variable 'merlin-last-point-type) nil)
+  ; if there is not yet a buffer for the current buffer, create one
+  (when (not (merlin-process-started-p))
+      (merlin-start-process nil))
+  (set (make-local-variable 'merlin-local-process)
+       (merlin-get-process-variable 'merlin-local-process))
+  (merlin-process-add-user)
+
+  (when (and (fboundp 'auto-complete-mode)
+             merlin-use-auto-complete-mode)
+    (auto-complete-mode 1)
+    (add-to-list 'ac-sources 'merlin-ac-source))
+  (add-hook 'completion-at-point-functions
+            #'merlin-completion-at-point nil 'local)
   (add-to-list 'after-change-functions 'merlin-edit)
   (merlin-load-project-file)
   (with-current-buffer (get-buffer-create merlin-type-buffer-name)


(defun merlin-edit (start end length)
  "Called when an edit is make to retract the locked zone if it is needed."
  (when merlin-type-overlay
      (delete-overlay merlin-type-overlay)
      (setq merlin-type-overlay nil))
  (if (< start merlin-lock-point)
      (progn
        (setq merlin-lock-point (merlin-retract-to start))
        (merlin-update-lock-zone-display))))

    (if (not end-p)
        (merlin-send-command "tell" '("end" nil)))
    (merlin-check-for-errors view-errors-p)
    (setq merlin-lock-point (merlin-get-position))
    (merlin-update-lock-zone-display)))

      
