       (if (window-dedicated-p smart-win)
           (set-window-dedicated-p smart-win nil)))))
 
-(defadvice kill-buffer
-    (after smartwin-after-kill-buffer)
+(defadvice kill-buffer (around smartwin-around-kill-buffer)
   "Try to insure smart buffer should not shown in normal window and vice versa."
   (let ((smart-win (smartwin--get-smart-window))
-        (window (selected-window)))
-    (if (eq smart-win window)
-        ;; auto hide smart window if "*scratch*" is the last smart buffer and
-        ;; not been modified
-        (progn
-          (let ((scratch-buffer (get-buffer "*scratch*"))
-                (buffer-list (smartwin--make-smart-buffer-list)))
-            (if (and (eq scratch-buffer (window-buffer smart-win))
-                     (not buffer-list)
-                     (not (buffer-modified-p scratch-buffer)))
-                (smartwin-hide))))
-      ;; to insure smart buffer not shown in normal window
-      (let ((buffer (window-buffer window)))
-        (if (smartwin--match-buffer buffer)
-            (let ((normal-buffer-list (smartwin--make-normal-buffer-list)))
-              (if normal-buffer-list
-                  (set-window-buffer window (car normal-buffer-list))
-                (set-window-buffer window (get-buffer "*scratch*")))
-              ))))))
+        (scratch-buffer (get-buffer "*scratch*"))
+        (window (selected-window))
+        (buffer-to-kill (or (and (ad-get-arg 0) (get-buffer (ad-get-arg 0)))
+                            (current-buffer))))
+    (if (eq buffer-to-kill scratch-buffer) ;; not kill scratch buffer, clear it
+        (clear-scratch-buffer buffer-to-kill)
+      ad-do-it ;; kill buffer
+      (if (eq smart-win window)
+          ;; auto hide smart window if "*scratch*" is the last smart buffer and
+          ;; not been modified
+          (progn
+            (let ((buffer-list (smartwin--make-smart-buffer-list)))
+              (if (and (eq scratch-buffer (window-buffer smart-win))
+                       (not buffer-list)
+                       (not (buffer-modified-p scratch-buffer)))
+                  (smartwin-hide))))
+        ;; to insure smart buffer not shown in normal window
+        (let ((buffer (window-buffer window)))
+          (if (smartwin--match-buffer buffer)
+              (let ((normal-buffer-list (smartwin--make-normal-buffer-list)))
+                (if normal-buffer-list
+                    (set-window-buffer window (car normal-buffer-list))
+                  (set-window-buffer window (get-buffer "*scratch*"))))))))))
 
 ;;; Minor Mode
 

      (ad-deactivate 'get-mru-window)
      (ad-deactivate 'delete-window)
      (ad-deactivate 'delete-other-windows)
      (ad-deactivate 'balance-windows)
      (ad-deactivate 'mwheel-scroll)
      (ad-deactivate 'select-window)
      (ad-deactivate 'kill-buffer)
      (ad-deactivate 'gdb)
      (define-key lisp-interaction-mode-map (kbd "C-x k") nil)

      (smartwin-hide))))


                        (minimize-window smart-win)))))))))

(defadvice balance-windows (around smartwin-around-balance-windows)
  "When do `balance-windows', ignore smart window."
  (let* ((window (smartwin--get-smart-window))
         (in-smartwin (eq (selected-window) window)))
    (if window
