Refactor the window switching advice code

This commit is contained in:
Bozhidar Batsov 2013-07-17 15:28:30 +03:00
parent 3b5ac377a0
commit b7d8bf7126

View file

@ -155,23 +155,19 @@ Will only occur if prelude-whitespace is also enabled."
(file-writable-p buffer-file-name)) (file-writable-p buffer-file-name))
(save-buffer))) (save-buffer)))
(defadvice switch-to-buffer (before switch-buffer-now activate) (defmacro advise-commands (advice-name commands &rest body)
"Invoke `prelude-auto-save-command' before `switch-to-window'." "Apply advice named ADVICE-NAME to multiple COMMANDS.
(prelude-auto-save-command))
(defadvice other-window (before other-window-now activate) The body of the advice is in BODY."
"Invoke `prelude-auto-save-command' before `other-window'." `(progn
(prelude-auto-save-command)) ,@(mapcar (lambda (command)
(defadvice windmove-up (before other-window-now activate) `(defadvice ,command (before ,(intern (concat (symbol-name command) "-" advice-name)) activate)
"Invoke `prelude-auto-save-command' before `windmove-up'." ,@body))
(prelude-auto-save-command)) commands)))
(defadvice windmove-down (before other-window-now activate)
"Invoke `prelude-auto-save-command' before `windmove-down'." ;; advise all window switching functions
(prelude-auto-save-command)) (advise-commands "auto-save"
(defadvice windmove-left (before other-window-now activate) (switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
"Invoke `prelude-auto-save-command' before `windmove-left'."
(prelude-auto-save-command))
(defadvice windmove-right (before other-window-now activate)
"Invoke `prelude-auto-save-command' before `windmove-right'."
(prelude-auto-save-command)) (prelude-auto-save-command))
(add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command) (add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)