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))
(save-buffer)))
(defadvice switch-to-buffer (before switch-buffer-now activate)
"Invoke `prelude-auto-save-command' before `switch-to-window'."
(prelude-auto-save-command))
(defadvice other-window (before other-window-now activate)
"Invoke `prelude-auto-save-command' before `other-window'."
(prelude-auto-save-command))
(defadvice windmove-up (before other-window-now activate)
"Invoke `prelude-auto-save-command' before `windmove-up'."
(prelude-auto-save-command))
(defadvice windmove-down (before other-window-now activate)
"Invoke `prelude-auto-save-command' before `windmove-down'."
(prelude-auto-save-command))
(defadvice windmove-left (before other-window-now activate)
"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'."
(defmacro advise-commands (advice-name commands &rest body)
"Apply advice named ADVICE-NAME to multiple COMMANDS.
The body of the advice is in BODY."
`(progn
,@(mapcar (lambda (command)
`(defadvice ,command (before ,(intern (concat (symbol-name command) "-" advice-name)) activate)
,@body))
commands)))
;; advise all window switching functions
(advise-commands "auto-save"
(switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
(prelude-auto-save-command))
(add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)