 	(setq best-window window)))
     best-window))
 
+;;; work with others
+;; ediff
+(add-hook 'ediff-before-setup-hook
+	  (lambda ()
+	    (smartwin-hide t)))
+
 ;;; advices
 
-(defadvice quit-window (after smartwin-quit-window)
-  "When run `quit-window' in smart window, select other window."
-  (let ((window (selected-window)))
-    (if (smartwin-smart-window-p window)
-        (smartwin-hide))))
-
-(defadvice kill-buffer (around smartwin-kill-buffer)
-  "When run `kill-buffer' in smart window, find right buffer to show.
-If no buffer can be showed in smart window, hide it."
-  (let ((buffer (ad-get-arg 0))
-        window)
-    (if (not buffer)
-        (setq buffer (current-buffer)))
-    (setq window (get-buffer-window buffer))
-    ad-do-it
-    (if (smartwin-smart-window-p window)
-        (progn
-          (setq buffer
-                (catch 'break
-                  (dolist (b (window-prev-buffers window))
-                    (if (smartwin-match-buffer b)
-                        (throw 'break b)))
-                  (throw 'break nil)))
-          (if buffer
-              (set-window-buffer winodw buffer)
-            (smartwin-hide t))))))
+(defadvice gdb (before smartwin-gdb-hide)
+  "When run `gdb', hide smart window."
+  (smartwin-hide t))
+
+(defadvice mwheel-scroll (around smartwin-scroll-up)
+  "When scroll window by mouse, let smartwin known."
+  (let ((in-smartwin-scroll t))
+    ad-do-it))
 
 (defadvice select-window (after smartwin-select-window)
   "Enlarge or shringe smart window when select window."
-  (if (not (boundp 'in-smartwin-select-window))
+  (if (and (not (boundp 'in-smartwin-select-window))
+           (not (boundp 'in-smartwin-scroll)))
       (let ((in-smartwin-select-window t)
             (window (ad-get-arg 0)))
         (if window

            (smartwin-show))
        ad-do-it)
    (message "Attempt to move smart window")))

(defadvice evil-window-rotate-upwards (around smartwin-rotate-upwards-ignore)
  "When `evil-window-rotate-upwards', ignore smart window."
  (if (not (smartwin-smart-window-p (selected-window)))
      (if (smartwin-find-smart-win)
          (progn
            (smartwin-hide)
            ad-do-it
            (smartwin-show))
        ad-do-it)))

(defadvice evil-window-rotate-downwards (around smartwin-rotate-downwards-ignore)
  "When `evil-window-rotate-downwards', ignore smart window."
  (if (not (smartwin-smart-window-p (selected-window)))
      (if (smartwin-find-smart-win)
          (progn
            (smartwin-hide)
            ad-do-it
            (smartwin-show))
        ad-do-it)))

(defadvice windmove-do-window-select (around smartwin-ignore-smart-window)
  "When move to smart window, enlarge it, when leave it, shringe it."
  (let ((window-before (selected-window)))
    ad-do-it
    (let ((window-after (selected-window)))
      (if (and (smartwin-smart-window-p window-before)
               (neq window-before window-after))
          (minimize-window window-before)
        (if (and (smartwin-smart-window-p window-after)
                 (neq window-before window-after))
            (smartwin-enlarge-window window-after))))))

(defadvice delete-other-windows (around smartwin-delete-other-windows)
  "If in smart window, just enlarge it instead of `delete-other-windows'."

(defun smartwin-enlarge-window (window)
  "Try to enlarge smart WINDOW, but not too large."
  (let ((height-before (window-height window)))
    ;; set smart window start
    (if (> (fit-window-to-buffer window
                                 smartwin-max-window-height
                                 smartwin-min-window-height)
           0)
        (set-window-start
         window
         (save-excursion
           (goto-char (window-start))
           (forward-line (- height-before (window-height window))))
         t))))

(defun smartwin-show ()
  "Show smart window."
