   (when (and ycmd-mode
              (or (not condition)
                  (memq condition ycmd-parse-conditions)))
-    (unless ycmd--buffer-visit-flag
-      (ycmd--notify-server "BufferVisit")
-      (setq ycmd--buffer-visit-flag t))
+    (ycmd--on-visit-buffer)
     (ycmd-notify-file-ready-to-parse)))
 
 (defun ycmd--on-save ()

  '((after-save-hook                  . ycmd--on-save)
    (after-change-functions           . ycmd--on-change)
    (window-configuration-change-hook . ycmd--on-window-configuration-change)
    (kill-buffer-hook                 . ycmd--close-buffer)
    (before-revert-hook               . ycmd--teardown))
  "Hooks which ycmd hooks in.")


       (when ycmd-mode
         ,@body))))

(defun ycmd--on-visit-buffer ()
  "If `ycmd--buffer-visit-flag' is nil send BufferVisit event."
  (unless ycmd--buffer-visit-flag
    (ycmd--notify-server "BufferVisit")
    (setq ycmd--buffer-visit-flag t)))

(defun ycmd--on-close-buffer ()
  "Notify server that the current buffer is no longer open, and cleanup emacs-ycmd variables."
  (when (ycmd-running?)
    (ycmd--notify-server "BufferUnload"
