Skip to content

Instantly share code, notes, and snippets.

@dabrahams
Created July 29, 2012 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabrahams/9504f1ae730f4db4c741 to your computer and use it in GitHub Desktop.
Save dabrahams/9504f1ae730f4db4c741 to your computer and use it in GitHub Desktop.
=== modified file 'lisp/window.el'
--- lisp/window.el 2012-07-18 10:02:54 +0000
+++ lisp/window.el 2012-07-29 16:59:18 +0000
@@ -2656,10 +2656,12 @@
(old-buffer (window-buffer window))
;; Save this since it's destroyed by `set-window-buffer'.
(next-buffers (window-next-buffers window))
+ (pred (frame-parameter frame 'buffer-predicate))
entry new-buffer killed-buffers visible)
(when (window-dedicated-p window)
(error "Window %s is dedicated to buffer %s" window old-buffer))
+ (unless
(catch 'found
;; Scan WINDOW's previous buffers first, skipping entries of next
;; buffers.
@@ -2669,6 +2671,7 @@
(not (setq killed-buffers
(cons new-buffer killed-buffers))))
(not (eq new-buffer old-buffer))
+ (or (null pred) (funcall pred new-buffer))
(or bury-or-kill
(not (memq new-buffer next-buffers))))
(if (and (not switch-to-visible-buffer)
@@ -2690,6 +2693,7 @@
(when (and (buffer-live-p buffer)
(not (eq buffer old-buffer))
(not (eq (aref (buffer-name buffer) 0) ?\s))
+ (or (null pred) (funcall pred buffer))
(or bury-or-kill (not (memq buffer next-buffers))))
(if (get-buffer-window buffer frame)
;; Try to avoid showing a buffer visible in some other window.
@@ -2708,16 +2712,22 @@
(not (setq killed-buffers
(cons buffer killed-buffers))))
(not (eq buffer old-buffer))
+ (or (null pred) (funcall pred buffer))
(setq entry (assq buffer (window-prev-buffers window))))
(setq new-buffer buffer)
(set-window-buffer-start-and-point
window new-buffer (nth 1 entry) (nth 2 entry))
- (throw 'found t))))
-
- ;; Show a buffer visible in another window.
- (when visible
- (setq new-buffer visible)
- (set-window-buffer-start-and-point window new-buffer)))
+ (throw 'found t)))))
+
+ ;; If we reach this, then either: (1) we have a
+ ;; candidate buffer that was skipped because it was already visible on
+ ;; the frame, in which case we switch to it now, or (2) no candidate
+ ;; was found, in which case we switch to *scratch*.
+ (if visible
+ (setq new-buffer visible)
+ (setq new-buffer (get-buffer-create "*scratch*")))
+ (set-window-buffer-start-and-point window new-buffer))
+
(if bury-or-kill
;; Remove `old-buffer' from WINDOW's previous and (restored list
@@ -2750,10 +2760,12 @@
(frame (window-frame window))
(old-buffer (window-buffer window))
(next-buffers (window-next-buffers window))
+ (pred (frame-parameter frame 'buffer-predicate))
new-buffer entry killed-buffers visible)
(when (window-dedicated-p window)
(error "Window %s is dedicated to buffer %s" window old-buffer))
+ (unless
(catch 'found
;; Scan WINDOW's next buffers first.
(dolist (buffer next-buffers)
@@ -2761,6 +2772,7 @@
(not (setq killed-buffers
(cons buffer killed-buffers))))
(not (eq buffer old-buffer))
+ (or (null pred) (funcall pred buffer))
(setq entry (assq buffer (window-prev-buffers window))))
(setq new-buffer buffer)
(set-window-buffer-start-and-point
@@ -2771,6 +2783,7 @@
(dolist (buffer (buffer-list frame))
(when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
(not (eq (aref (buffer-name buffer) 0) ?\s))
+ (or (null pred) (funcall pred buffer))
(not (assq buffer (window-prev-buffers window))))
(if (get-buffer-window buffer frame)
;; Try to avoid showing a buffer visible in some other window.
@@ -2785,6 +2798,7 @@
(or (buffer-live-p new-buffer)
(not (setq killed-buffers
(cons new-buffer killed-buffers))))
+ (or (null pred) (funcall pred new-buffer))
(not (eq new-buffer old-buffer)))
(if (and (not switch-to-visible-buffer)
(get-buffer-window new-buffer frame))
@@ -2793,12 +2807,17 @@
(setq visible new-buffer))
(set-window-buffer-start-and-point
window new-buffer (nth 1 entry) (nth 2 entry))
- (throw 'found t))))
-
- ;; Show a buffer visible in another window.
- (when visible
- (setq new-buffer visible)
- (set-window-buffer-start-and-point window new-buffer)))
+ (throw 'found t)))))
+
+ ;; If we reach this, then either: (1) we have a candidate buffer that
+ ;; was skipped because it was already visible on the frame, in which
+ ;; case we switch to it now, or (2) no candidate was found, in which
+ ;; case we switch to *scratch*.
+ (if visible
+ (setq new-buffer visible)
+ (setq new-buffer (get-buffer-create "*scratch*")))
+ (set-window-buffer-start-and-point window new-buffer))
+
;; Remove `new-buffer' from and restore WINDOW's next buffers.
(set-window-next-buffers window (delq new-buffer next-buffers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment