 ;; PROCESS MANAGEMENT ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;
 
-(defun merlin-get-buffer-instance-name ()
-  "Return the instance name of the current-projet.
-For now it is a constant function (every buffer shares the same instance)."
-  "")
-
-(defun merlin-get-process ()
-  "Return the process of the current buffer."
-  merlin-local-process)
-
-(defun merlin-get-process-buffer-name ()
-  "Return the buffer name of the merlin process associated to the current buffer."
-  (format "*merlin*%s*" (merlin-get-buffer-instance-name)))
-
-(defun merlin-get-process-variable (var)
-  "Return the value of VAR (symbol) inside the process buffer."
-  (when (get-buffer (merlin-get-process-buffer-name))
-    (buffer-local-value var (get-buffer (merlin-get-process-buffer-name)))))
-
-(defun merlin-get-process-name ()
-  "Return the process name for the current buffer."
-  (concat "merlin-" (merlin-get-buffer-instance-name)))
-
-(defun merlin-start-process (flags &optional users)
-  "Start the merlin process for the current buffer.
+(defun merlin-start-process (flags)
+  "Start the merlin process.
 FLAGS are a list of strings denoting the parameters to be passed
-to merlin.  USERS can be used to set the users of this
-buffer.  Return the process created"
-  (get-buffer-create (merlin-get-process-buffer-name))
-  (let ((p (apply #'start-file-process (merlin-get-process-name)
-                         (merlin-get-process-buffer-name)
-                         merlin-command `("-protocol" "sexp" . ,flags)))
-        (name (buffer-name)))
-    (set (make-local-variable 'merlin-local-process) p)
-    (dolist (buffer users)
-      (message "Setting process for buffer %s" buffer)
-      (with-current-buffer buffer
-        (set (make-local-variable 'merlin-local-process) p)))
-    (merlin-debug (format "Running %s with flags %s\n" merlin-command flags))
-    (set-process-query-on-exit-flag p nil)
-    (push p merlin-processes)
-; don't forget to initialize temporary variable
-    (with-current-buffer (merlin-get-process-buffer-name)
-      (set (make-local-variable 'merlin-queue) (tq-create p))
-      (set (make-local-variable 'merlin-process-users) (cons name (delete name users)))
-      (set (make-local-variable 'merlin-local-process) p)
-      (set (make-local-variable 'merlin-process-last-user) name)
-      )
-  p))
-
-(defun merlin-get-current-buffer-users ()
-  "Return the list of users of the merlin instance for this buffer."
-  (when (get-buffer (merlin-get-process-buffer-name))
-    (with-current-buffer (merlin-get-process-buffer-name)
-      merlin-process-users)))
+to merlin.  It returns the created process."
+  (when (not (merlin-process-started-p))
+    (get-buffer-create merlin-buffer-name)
+    (let ((p (apply #'start-file-process "merlin" merlin-buffer-name
+                    merlin-command `("-protocol" "sexp" . ,flags)))
+          (name (buffer-name)))
+      (merlin-debug (format "Running %s with flags %s\n" merlin-command flags))
+      (set-process-query-on-exit-flag p nil)
+      (setq merlin-process p)
+      (setq merlin-process-queue (tq-create p))
+      p)))
 
 (defun merlin-restart-process ()
   "Restart the merlin toplevel for this buffer, taking into account new flags."
   (interactive)
-  (let ((users (merlin-get-current-buffer-users)))
-    (when (merlin-process-started-p)
-      (ignore-errors (merlin-kill-process)))
-    (setq merlin-local-process (merlin-start-process merlin-current-flags users))
-    (setq merlin-pending-errors nil)
-    (merlin-load-project-file)
-    (merlin-to-point)
-    (message "Merlin restarted")))
+  (when (merlin-process-started-p)
+    (ignore-errors (merlin-kill-process)))
+  (merlin-start-process merlin-current-flags)
+  (setq merlin-pending-errors nil)
+  (merlin-load-project-file)
+  (merlin-to-point)
+  (message "Merlin restarted"))
 
 (defun merlin-process-clear-flags ()
   "Clear all flags set up to be passed to merlin.

(defun merlin-debug (s)
  "Output S if the variable `merlin-debug' is non-nil on the process buffer
associated to the current buffer."
  (with-current-buffer (merlin-get-process-buffer-name)
    (goto-char (point-max))
    (insert s)))


    (setq merlin-current-flags flag-list))
  (message "Flag %s added.  Restart ocamlmerlin by `merlin-restart-process' to take it into account." flag-string))

(defun merlin-is-last-user-p ()
  "Return whether the current buffer was the current user of the merlin process."
  (equal merlin-process-last-user (buffer-name)))

(defun merlin-process-started-p ()
  "Return non-nil if the merlin process for the current buffer is already started."
  (get-buffer merlin-buffer-name))

(defun merlin-kill-process ()
  "Kill the merlin process inside the buffer."
  (tq-close merlin-process-queue)
  (kill-process merlin-process))

(defun merlin-wait-for-answer ()
  "Waits for merlin to answer."
  (while (not merlin-ready)
    (accept-process-output merlin-process 0.1 nil t))
  merlin-result)

(defun merlin-send-command-async (command callback-if-success &optional callback-if-exn)
