 (defun smartwin--get-smart-window ()
   "Find smart window among all live windows.
 If found, return the window, else return nil."
-  (catch 'break
-    (dolist (window (window-list))
-      (if (smartwin--smart-window-p window)
-          (throw 'break window)))))
+  (let ((root-tree (car (window-tree (selected-frame)))))
+    (if (and (listp root-tree) (car root-tree))
+        (let ((bottom-window (car (last root-tree))))
+          (if (smartwin--smart-window-p bottom-window)
+              bottom-window
+            nil))
+      nil)))
 
 (defun smartwin--get-befitting-window ()
   "Try to get an existing befitting bottom window as smart window."

      (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))))



(defun smartwin--match-buffer (buffer-or-name)
  "Judge whether to use smartwin to show BUFFER-OR-NAME."
  (let ((buffer (get-buffer buffer-or-name)))
    (if buffer
        (let ((name (buffer-name buffer))
              (mode (buffer-local-value 'major-mode buffer)))
