chiark / gitweb /
Merge remote-tracking branch 'staging'
[profile] / el / dot-emacs.el
index 4ad0e6c3d6dea9da2b03708d09364a0f45239daa..0030abde0e9afa7ab78d0fd8fc1d2557f8feb698 100644 (file)
@@ -122,17 +122,6 @@ (defun mdw-config (sym)
                      (concat "(" (buffer-string) ")")))))))
   (cdr (assq sym mdw-config)))
 
-;; Width configuration.
-
-(defcustom mdw-column-width
-  (string-to-number (or (mdw-config 'emacs-width) "77"))
-  "Width of Emacs columns."
-  :type 'integer)
-(defcustom mdw-text-width mdw-column-width
-  "Expected width of text within columns."
-  :type 'integer
-  :safe 'integerp)
-
 ;; Local variables hacking.
 
 (defun run-local-vars-mode-hook ()
@@ -193,6 +182,116 @@ (defadvice backward-page (after mdw-fixup compile activate)
 (defadvice forward-page (after mdw-fixup compile activate)
   (mdw-fixup-page-position))
 
+;; Transient mark mode hacks.
+
+(defadvice exchange-point-and-mark
+    (around mdw-highlight (&optional arg) activate compile)
+  "Maybe don't actually exchange point and mark.
+If `transient-mark-mode' is on and the mark is inactive, then
+just activate it.  A non-trivial prefix argument will force the
+usual behaviour.  A trivial prefix argument (i.e., just C-u) will
+activate the mark and temporarily enable `transient-mark-mode' if
+it's currently off."
+  (cond ((or mark-active
+            (and (not transient-mark-mode) (not arg))
+            (and arg (or (not (consp arg))
+                         (not (= (car arg) 4)))))
+        ad-do-it)
+       (t
+        (or transient-mark-mode (setq transient-mark-mode 'only))
+        (set-mark (mark t)))))
+
+;; Glasses.
+
+(setq glasses-separator "-"
+      glasses-separate-parentheses-p nil
+      glasses-uncapitalize-p t)
+
+;;;--------------------------------------------------------------------------
+;;; Rename buffers along with files.
+
+(defvar mdw-inhibit-rename-buffer nil
+  "If non-nil, `rename-file' won't rename the buffer visiting the file.")
+
+(defmacro mdw-advise-to-inhibit-rename-buffer (function)
+  "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
+
+This will prevent `rename-file' from renaming the buffer."
+  `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
+     "Don't rename the buffer when renaming the underlying file."
+     (let ((mdw-inhibit-rename-buffer t))
+       ad-do-it)))
+(mdw-advise-to-inhibit-rename-buffer recode-file-name)
+(mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
+(mdw-advise-to-inhibit-rename-buffer backup-buffer)
+
+(defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
+                       compile activate)
+  "If a buffer is visiting the file, rename it to match the new name.
+
+Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
+  (unless mdw-inhibit-rename-buffer
+    (let ((buffer (get-file-buffer from)))
+      (when buffer
+       (let ((to (if (not (string= (file-name-nondirectory to) "")) to
+                   (concat to (file-name-nondirectory from)))))
+         (with-current-buffer buffer
+           (set-visited-file-name to nil t)))))))
+
+;; Character width table hacking.
+(dolist (ch '(?🙀))
+  (aset char-width-table ch 2))
+
+;;;--------------------------------------------------------------------------
+;;; Miscellaneous bug fixes.
+
+;; Bug fix for markdown-mode, which breaks point positioning during
+;; `query-replace'.
+(defadvice markdown-check-change-for-wiki-link
+    (around mdw-save-match activate compile)
+  "Save match data around the `markdown-mode' `after-change-functions' hook."
+  (save-match-data ad-do-it))
+
+;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
+;; always returns nil, with the result that all email addresses are lost.
+;; Replace the function entirely.
+(defadvice bbdb-canonicalize-address
+    (around mdw-bug-fix activate compile)
+  "Don't use `run-hook-with-args', because that doesn't work."
+  (let ((net (ad-get-arg 0)))
+
+    ;; Make sure this is a proper hook list.
+    (if (functionp bbdb-canonicalize-net-hook)
+       (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
+
+    ;; Iterate over the hooks until things converge.
+    (let ((donep nil))
+      (while (not donep)
+       (let (next (changep nil)
+             hook (hooks bbdb-canonicalize-net-hook))
+         (while hooks
+           (setq hook (pop hooks))
+           (setq next (funcall hook net))
+           (if (not (equal next net))
+               (setq changep t
+                     net next)))
+         (setq donep (not changep)))))
+    (setq ad-return-value net)))
+
+;;;--------------------------------------------------------------------------
+;;; Window management.
+
+;; Width configuration.
+
+(defcustom mdw-column-width
+  (string-to-number (or (mdw-config 'emacs-width) "77"))
+  "Width of Emacs columns."
+  :type 'integer)
+(defcustom mdw-text-width mdw-column-width
+  "Expected width of text within columns."
+  :type 'integer
+  :safe 'integerp)
+
 ;; Splitting windows.
 
 (unless (fboundp 'scroll-bar-columns)
@@ -393,86 +492,239 @@ (mdw-advise-to-inhibit-raise-frame select-frame-set-input-focus)
 (mdw-advise-to-inhibit-raise-frame appt-disp-window)
 (mdw-advise-to-inhibit-raise-frame mouse-select-window)
 
-;; Bug fix for markdown-mode, which breaks point positioning during
-;; `query-replace'.
-(defadvice markdown-check-change-for-wiki-link
-    (around mdw-save-match activate compile)
-  "Save match data around the `markdown-mode' `after-change-functions' hook."
-  (save-match-data ad-do-it))
-
-;; Bug fix for `bbdb-canonicalize-address': on Emacs 24, `run-hook-with-args'
-;; always returns nil, with the result that all email addresses are lost.
-;; Replace the function entirely.
-(defadvice bbdb-canonicalize-address
-    (around mdw-bug-fix activate compile)
-  "Don't use `run-hook-with-args', because that doesn't work."
-  (let ((net (ad-get-arg 0)))
+;; Window selection for `display-buffer'.
 
-    ;; Make sure this is a proper hook list.
-    (if (functionp bbdb-canonicalize-net-hook)
-       (setq bbdb-canonicalize-net-hook (list bbdb-canonicalize-net-hook)))
+(defvar mdw-designated-window nil
+  "The window chosen by `mdw-designate-window', or nil.")
 
-    ;; Iterate over the hooks until things converge.
-    (let ((donep nil))
-      (while (not donep)
-       (let (next (changep nil)
-             hook (hooks bbdb-canonicalize-net-hook))
-         (while hooks
-           (setq hook (pop hooks))
-           (setq next (funcall hook net))
-           (if (not (equal next net))
-               (setq changep t
-                     net next)))
-         (setq donep (not changep)))))
-    (setq ad-return-value net)))
+(defun mdw-designated-window-display-buffer-function (buffer not-this-window)
+  "Display buffer function to use the designated window."
+  (unless mdw-designated-window (error "No designated window!"))
+  (prog1 mdw-designated-window
+    (with-selected-window mdw-designated-window (switch-to-buffer buffer))
+    (setq mdw-designated-window nil
+         display-buffer-function nil)))
 
-;; Transient mark mode hacks.
+(defun mdw-display-buffer-in-designated-window (buffer alist)
+  "Display function to use the designated window."
+  (prog1 mdw-designated-window
+    (when mdw-designated-window
+      (with-selected-window mdw-designated-window
+       (switch-to-buffer buffer nil t)))
+    (setq mdw-designated-window nil)))
 
-(defadvice exchange-point-and-mark
-    (around mdw-highlight (&optional arg) activate compile)
-  "Maybe don't actually exchange point and mark.
-If `transient-mark-mode' is on and the mark is inactive, then
-just activate it.  A non-trivial prefix argument will force the
-usual behaviour.  A trivial prefix argument (i.e., just C-u) will
-activate the mark and temporarily enable `transient-mark-mode' if
-it's currently off."
-  (cond ((or mark-active
-            (and (not transient-mark-mode) (not arg))
-            (and arg (or (not (consp arg))
-                         (not (= (car arg) 4)))))
-        ad-do-it)
-       (t
-        (or transient-mark-mode (setq transient-mark-mode 'only))
-        (set-mark (mark t)))))
+(defun mdw-designate-window (cancel)
+  "Use the selected window for the next pop-up buffer.
+With a prefix argument, clear the designated window."
+  (interactive "P")
+  (let ((window (selected-window)))
+    (cond (cancel
+          (cond (mdw-designated-window
+                 (setq mdw-designated-window nil)
+                 (unless (mdw-emacs-version-p 24)
+                   (setq display-buffer-function nil))
+                 (message "Window designation cleared."))
+                (t
+                 (message "No designated window active."))))
+         ((window-dedicated-p window)
+          (error "Window is dedicated to its buffer."))
+         (t
+          (setq mdw-designated-window window)
+          (unless (mdw-emacs-version-p 24)
+            (setq display-buffer-function
+                    #'mdw-designated-window-display-buffer-function))
+          (message "Window designated.")))))
 
-;; Functions for sexp diary entries.
+(when (mdw-emacs-version-p 24)
+  (setq display-buffer-base-action
+         (let* ((action display-buffer-base-action)
+                (funcs (car action))
+                (alist (cdr action)))
+           (cons (cons 'mdw-display-buffer-in-designated-window funcs)
+                 alist))))
 
-(defvar mdw-diary-for-org-mode-p nil
-  "Display diary along with the agenda?")
+(defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
+  "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
+  (interactive "bBuffer: ")
+  (let ((home-frame (selected-frame))
+       (buffer (get-buffer buffer-or-name))
+       (safe-buffer (get-buffer "*scratch*")))
+    (dolist (frame (frame-list))
+      (unless (eq frame home-frame)
+       (dolist (window (window-list frame))
+         (when (eq (window-buffer window) buffer)
+           (set-window-buffer window safe-buffer)))))))
 
-(defun mdw-not-org-mode (form)
-  "As FORM, but not in Org mode agenda."
-  (and (not mdw-diary-for-org-mode-p)
-       (eval form)))
+(defvar mdw-inhibit-walk-windows nil
+  "If non-nil, then `walk-windows' does nothing.
+This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
+buffers in random frames.")
 
-(defun mdw-weekday (l)
-  "Return non-nil if `date' falls on one of the days of the week in L.
-L is a list of day numbers (from 0 to 6 for Sunday through to
-Saturday) or symbols `sunday', `monday', etc. (or a mixture).  If
-the date stored in `date' falls on a listed day, then the
-function returns non-nil."
-  (let ((d (calendar-day-of-week date)))
-    (or (memq d l)
-       (memq (nth d '(sunday monday tuesday wednesday
-                             thursday friday saturday)) l))))
+(setq display-buffer--other-frame-action
+       '((display-buffer-reuse-window display-buffer-pop-up-frame)
+         (reusable-frames . nil)
+         (inhibit-same-window . t)))
 
-(defun mdw-discordian-date (date)
-  "Return the Discordian calendar date corresponding to DATE.
+(defadvice walk-windows (around mdw-inhibit activate)
+  "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
+  (and (not mdw-inhibit-walk-windows)
+       ad-do-it))
 
-The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
+(defadvice switch-to-buffer-other-frame
+    (around mdw-always-new-frame activate)
+  "Always make a new frame.
+Even if an existing window in some random frame looks tempting."
+  (let ((mdw-inhibit-walk-windows t)) ad-do-it))
 
-The original is by David Pearson.  I modified it to produce date components
-as output rather than a string."
+(defadvice display-buffer (before mdw-inhibit-other-frames activate)
+  "Don't try to do anything fancy with other frames.
+Pretend they don't exist.  They might be on other display devices."
+  (ad-set-arg 2 nil))
+
+(setq even-window-sizes nil
+      even-window-heights nil
+      display-buffer-reuse-frames nil)
+
+(defvar mdw-fallback-window-alist nil
+  "Alist mapping frames to fallback windows.")
+
+(defun mdw-cleanup-fallback-window-alist ()
+  "Remove entries for dead frames and windows from the fallback alist."
+  (let ((prev nil)
+       (cursor mdw-fallback-window-alist))
+    (while cursor
+      (let* ((assoc (car cursor))
+            (tail (cdr cursor)))
+       (cond ((and (frame-live-p (car assoc))
+                   (window-live-p (cdr assoc)))
+              (setq prev cursor))
+             ((null prev)
+              (setq mdw-fallback-window-alist tail))
+             (t
+              (setcdr prev tail)))
+       (setq cursor tail)))))
+
+(defun mdw-set-fallback-window (cancel)
+  "Prefer the selected window for pop-up buffers in this frame.
+With a prefix argument, clear the fallback window."
+  (interactive "P")
+  (let* ((frame (selected-frame)) (window (selected-window))
+        (assoc (assq (selected-frame) mdw-fallback-window-alist)))
+    (cond (cancel
+          (cond (assoc
+                 (setcdr assoc nil)
+                 (message "Fallback window cleared."))
+                (t
+                 (message "No fallback window active in this frame."))))
+         ((window-dedicated-p window)
+          (error "Window is dedicated to its buffer."))
+         (t
+          (if assoc (setcdr assoc window)
+            (push (cons frame window) mdw-fallback-window-alist))
+          (message "Fallback window set.")))
+    (mdw-cleanup-fallback-window-alist)))
+
+(defun mdw-last-window-in-frame-p (window)
+  "Return whether WINDOW is the last in its frame."
+  (catch 'done
+    (while window
+      (let ((next (window-next-sibling window)))
+       (while (and next (window-minibuffer-p next))
+         (setq next (window-next-sibling next)))
+       (if next (throw 'done nil)))
+      (setq window (window-parent window)))
+    t))
+
+(defun mdw-display-buffer-in-tolerable-window (buffer alist)
+  "Try finding a tolerable window in which to display BUFFER.
+Begone, foul DWIMmerlaik!
+
+This is all totally subject to arbitrary change in the future, but the
+emphasis is on predictability rather than crazy DWIMmery."
+  (let* ((selected (selected-window)) chosen
+        (fallback (assq (selected-frame) mdw-fallback-window-alist))
+        (full-height-p (window-full-height-p selected))
+        (full-width-p (window-full-width-p selected)))
+    (cond
+
+     ((and fallback (window-live-p (cdr fallback)))
+      ;; There's a fallback window set for this frame.  Use it.
+
+      (setq chosen (cdr fallback)
+           selected nil)
+      (display-buffer-record-window 'window chosen buffer))
+
+     ((and full-height-p full-width-p)
+      ;; We're basically the only window in the frame.  If we want to get
+      ;; anywhere, we'll have to split the window.
+
+      (let ((width (window-width selected))
+           (preferred-width (mdw-preferred-column-width)))
+       (if (and (>= width (mdw-frame-width-for-columns 2 preferred-width))
+                (mdw-frame-width-quantized-p width preferred-width))
+           (setq chosen (split-window-right preferred-width))
+         (setq chosen (split-window-below)))
+       (display-buffer-record-window 'window chosen buffer)))
+
+     ((mdw-last-window-in-frame-p selected)
+      ;; This is the last window in the frame.  I don't think I want to
+      ;; clobber the first window, so rebound and clobber the previous one
+      ;; instead.  (This obviously has the same effect if there are only two
+      ;; windows, but seems more useful if there are three.)
+
+      (setq chosen (previous-window selected 'never nil))
+      (display-buffer-record-window 'reuse chosen buffer))
+
+     (t
+      ;; There's another window in front of us.  Let's use that one.
+      (setq chosen (next-window selected 'never nil)))
+      (display-buffer-record-window 'reuse chosen buffer))
+
+    (if (eq chosen selected)
+       (error "Failed to select a different window!"))
+
+    (when chosen
+      (with-selected-window chosen (switch-to-buffer buffer)))
+    chosen))
+
+;; Hack the display actions so that they do something sensible.
+(setq display-buffer-fallback-action
+       '((display-buffer--maybe-same-window
+          display-buffer-reuse-window
+          display-buffer-pop-up-window
+          mdw-display-buffer-in-tolerable-window)))
+
+;;;--------------------------------------------------------------------------
+;;; Calendar and diary hacking.
+
+;; Functions for sexp diary entries.
+
+(defvar mdw-diary-for-org-mode-p nil
+  "Display diary along with the agenda?")
+
+(defun mdw-not-org-mode (form)
+  "As FORM, but not in Org mode agenda."
+  (and (not mdw-diary-for-org-mode-p)
+       (eval form)))
+
+(defun mdw-weekday (l)
+  "Return non-nil if `date' falls on one of the days of the week in L.
+L is a list of day numbers (from 0 to 6 for Sunday through to
+Saturday) or symbols `sunday', `monday', etc. (or a mixture).  If
+the date stored in `date' falls on a listed day, then the
+function returns non-nil."
+  (let ((d (calendar-day-of-week date)))
+    (or (memq d l)
+       (memq (nth d '(sunday monday tuesday wednesday
+                             thursday friday saturday)) l))))
+
+(defun mdw-discordian-date (date)
+  "Return the Discordian calendar date corresponding to DATE.
+
+The return value is (YOLD . st-tibs-day) or (YOLD SEASON DAYNUM DOW).
+
+The original is by David Pearson.  I modified it to produce date components
+as output rather than a string."
   (let* ((days ["Sweetmorn" "Boomtime" "Pungenday"
                "Prickle-Prickle" "Setting Orange"])
         (months ["Chaos" "Discord" "Confusion"
@@ -529,10 +781,6 @@ (defun mdw-todo (&optional when)
                                (nth 2 when))))))))
     (eq w d)))
 
-(defadvice org-agenda-list (around mdw-preserve-links activate)
-  (let ((mdw-diary-for-org-mode-p t))
-    ad-do-it))
-
 (defcustom diary-time-regexp nil
   "Regexp matching times in the diary buffer."
   :type 'regexp)
@@ -564,6 +812,13 @@ (defadvice diary-add-to-list (before mdw-trim-leading-space compile activate)
        (if (equal str old) (setq done t)))
       (ad-set-arg 1 str))))
 
+;;;--------------------------------------------------------------------------
+;;; Org-mode hacking.
+
+(defadvice org-agenda-list (around mdw-preserve-links activate)
+  (let ((mdw-diary-for-org-mode-p t))
+    ad-do-it))
+
 (defadvice org-bbdb-anniversaries (after mdw-fixup-list compile activate)
   "Return a string rather than a list."
   (with-temp-buffer
@@ -673,244 +928,6 @@ (setq org-export-docbook-xslt-proc-command "xsltproc --output %o %s %i"
       org-export-docbook-xslt-stylesheet
        "/usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl")
 
-;; Glasses.
-
-(setq glasses-separator "-"
-      glasses-separate-parentheses-p nil
-      glasses-uncapitalize-p t)
-
-;; Some hacks to do with window placement.
-
-(defvar mdw-designated-window nil
-  "The window chosen by `mdw-designate-window', or nil.")
-
-(defun mdw-designated-window-display-buffer-function (buffer not-this-window)
-  "Display buffer function to use the designated window."
-  (unless mdw-designated-window (error "No designated window!"))
-  (prog1 mdw-designated-window
-    (with-selected-window mdw-designated-window (switch-to-buffer buffer))
-    (setq mdw-designated-window nil
-         display-buffer-function nil)))
-
-(defun mdw-display-buffer-in-designated-window (buffer alist)
-  "Display function to use the designated window."
-  (prog1 mdw-designated-window
-    (when mdw-designated-window
-      (with-selected-window mdw-designated-window
-       (switch-to-buffer buffer nil t)))
-    (setq mdw-designated-window nil)))
-
-(defun mdw-designate-window (cancel)
-  "Use the selected window for the next pop-up buffer.
-With a prefix argument, clear the designated window."
-  (interactive "P")
-  (let ((window (selected-window)))
-    (cond (cancel
-          (cond (mdw-designated-window
-                 (setq mdw-designated-window nil)
-                 (unless (mdw-emacs-version-p 24)
-                   (setq display-buffer-function nil))
-                 (message "Window designation cleared."))
-                (t
-                 (message "No designated window active."))))
-         ((window-dedicated-p window)
-          (error "Window is dedicated to its buffer."))
-         (t
-          (setq mdw-designated-window window)
-          (unless (mdw-emacs-version-p 24)
-            (setq display-buffer-function
-                    #'mdw-designated-window-display-buffer-function))
-          (message "Window designated.")))))
-
-(when (mdw-emacs-version-p 24)
-  (setq display-buffer-base-action
-         (let* ((action display-buffer-base-action)
-                (funcs (car action))
-                (alist (cdr action)))
-           (cons (cons 'mdw-display-buffer-in-designated-window funcs)
-                 alist))))
-
-(defun mdw-clobber-other-windows-showing-buffer (buffer-or-name)
-  "Arrange that no windows on other frames are showing BUFFER-OR-NAME."
-  (interactive "bBuffer: ")
-  (let ((home-frame (selected-frame))
-       (buffer (get-buffer buffer-or-name))
-       (safe-buffer (get-buffer "*scratch*")))
-    (dolist (frame (frame-list))
-      (unless (eq frame home-frame)
-       (dolist (window (window-list frame))
-         (when (eq (window-buffer window) buffer)
-           (set-window-buffer window safe-buffer)))))))
-
-(defvar mdw-inhibit-walk-windows nil
-  "If non-nil, then `walk-windows' does nothing.
-This is used by advice on `switch-to-buffer-other-frame' to inhibit finding
-buffers in random frames.")
-
-(setq display-buffer--other-frame-action
-       '((display-buffer-reuse-window display-buffer-pop-up-frame)
-         (reusable-frames . nil)
-         (inhibit-same-window . t)))
-
-(defadvice walk-windows (around mdw-inhibit activate)
-  "If `mdw-inhibit-walk-windows' is non-nil, then do nothing."
-  (and (not mdw-inhibit-walk-windows)
-       ad-do-it))
-
-(defadvice switch-to-buffer-other-frame
-    (around mdw-always-new-frame activate)
-  "Always make a new frame.
-Even if an existing window in some random frame looks tempting."
-  (let ((mdw-inhibit-walk-windows t)) ad-do-it))
-
-(defadvice display-buffer (before mdw-inhibit-other-frames activate)
-  "Don't try to do anything fancy with other frames.
-Pretend they don't exist.  They might be on other display devices."
-  (ad-set-arg 2 nil))
-
-(setq even-window-sizes nil
-      even-window-heights nil
-      display-buffer-reuse-frames nil)
-
-(defvar mdw-fallback-window-alist nil
-  "Alist mapping frames to fallback windows.")
-
-(defun mdw-cleanup-fallback-window-alist ()
-  "Remove entries for dead frames and windows from the fallback alist."
-  (let ((prev nil)
-       (cursor mdw-fallback-window-alist))
-    (while cursor
-      (let* ((assoc (car cursor))
-            (tail (cdr cursor)))
-       (cond ((and (frame-live-p (car assoc))
-                   (window-live-p (cdr assoc)))
-              (setq prev cursor))
-             ((null prev)
-              (setq mdw-fallback-window-alist tail))
-             (t
-              (setcdr prev tail)))
-       (setq cursor tail)))))
-
-(defun mdw-set-fallback-window (cancel)
-  "Prefer the selected window for pop-up buffers in this frame.
-With a prefix argument, clear the fallback window."
-  (interactive "P")
-  (let* ((frame (selected-frame)) (window (selected-window))
-        (assoc (assq (selected-frame) mdw-fallback-window-alist)))
-    (cond (cancel
-          (cond (assoc
-                 (setcdr assoc nil)
-                 (message "Fallback window cleared."))
-                (t
-                 (message "No fallback window active in this frame."))))
-         ((window-dedicated-p window)
-          (error "Window is dedicated to its buffer."))
-         (t
-          (if assoc (setcdr assoc window)
-            (push (cons frame window) mdw-fallback-window-alist))
-          (message "Fallback window set.")))
-    (mdw-cleanup-fallback-window-alist)))
-
-(defun mdw-last-window-in-frame-p (window)
-  "Return whether WINDOW is the last in its frame."
-  (catch 'done
-    (while window
-      (let ((next (window-next-sibling window)))
-       (while (and next (window-minibuffer-p next))
-         (setq next (window-next-sibling next)))
-       (if next (throw 'done nil)))
-      (setq window (window-parent window)))
-    t))
-
-(defun mdw-display-buffer-in-tolerable-window (buffer alist)
-  "Try finding a tolerable window in which to display BUFFER.
-Begone, foul DWIMmerlaik!
-
-This is all totally subject to arbitrary change in the future, but the
-emphasis is on predictability rather than crazy DWIMmery."
-  (let* ((selected (selected-window)) chosen
-        (fallback (assq (selected-frame) mdw-fallback-window-alist))
-        (full-height-p (window-full-height-p selected))
-        (full-width-p (window-full-width-p selected)))
-    (cond
-
-     ((and fallback (window-live-p (cdr fallback)))
-      ;; There's a fallback window set for this frame.  Use it.
-
-      (setq chosen (cdr fallback)
-           selected nil)
-      (display-buffer-record-window 'window chosen buffer))
-
-     ((and full-height-p full-width-p)
-      ;; We're basically the only window in the frame.  If we want to get
-      ;; anywhere, we'll have to split the window.
-
-      (let ((width (window-width selected))
-           (preferred-width (mdw-preferred-column-width)))
-       (if (and (>= width (mdw-frame-width-for-columns 2 preferred-width))
-                (mdw-frame-width-quantized-p width preferred-width))
-           (setq chosen (split-window-right preferred-width))
-         (setq chosen (split-window-below)))
-       (display-buffer-record-window 'window chosen buffer)))
-
-     ((mdw-last-window-in-frame-p selected)
-      ;; This is the last window in the frame.  I don't think I want to
-      ;; clobber the first window, so rebound and clobber the previous one
-      ;; instead.  (This obviously has the same effect if there are only two
-      ;; windows, but seems more useful if there are three.)
-
-      (setq chosen (previous-window selected 'never nil))
-      (display-buffer-record-window 'reuse chosen buffer))
-
-     (t
-      ;; There's another window in front of us.  Let's use that one.
-      (setq chosen (next-window selected 'never nil)))
-      (display-buffer-record-window 'reuse chosen buffer))
-
-    (if (eq chosen selected)
-       (error "Failed to select a different window!"))
-
-    (when chosen
-      (with-selected-window chosen (switch-to-buffer buffer)))
-    chosen))
-
-;; Hack the display actions so that they do something sensible.
-(setq display-buffer-fallback-action
-       '((display-buffer--maybe-same-window
-          display-buffer-reuse-window
-          display-buffer-pop-up-window
-          mdw-display-buffer-in-tolerable-window)))
-
-;; Rename buffers along with files.
-
-(defvar mdw-inhibit-rename-buffer nil
-  "If non-nil, `rename-file' won't rename the buffer visiting the file.")
-
-(defmacro mdw-advise-to-inhibit-rename-buffer (function)
-  "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
-
-This will prevent `rename-file' from renaming the buffer."
-  `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
-     "Don't rename the buffer when renaming the underlying file."
-     (let ((mdw-inhibit-rename-buffer t))
-       ad-do-it)))
-(mdw-advise-to-inhibit-rename-buffer recode-file-name)
-(mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
-(mdw-advise-to-inhibit-rename-buffer backup-buffer)
-
-(defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
-                       compile activate)
-  "If a buffer is visiting the file, rename it to match the new name.
-
-Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
-  (unless mdw-inhibit-rename-buffer
-    (let ((buffer (get-file-buffer from)))
-      (when buffer
-       (let ((to (if (not (string= (file-name-nondirectory to) "")) to
-                   (concat to (file-name-nondirectory from)))))
-         (with-current-buffer buffer
-           (set-visited-file-name to nil t)))))))
-
 ;;;--------------------------------------------------------------------------
 ;;; Improved compilation machinery.