Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save buffer on focus lost #2376

Closed
murtaza52 opened this issue Jul 21, 2015 · 10 comments
Closed

save buffer on focus lost #2376

murtaza52 opened this issue Jul 21, 2015 · 10 comments

Comments

@murtaza52
Copy link

Hi,

One feature I sorely miss from emacs prelude is the ability to save buffers whenever focus changes to another buffer or window. This is very valuable as the file is automatically saved, and I dont have to manually save them.

Is there such a feature in sapcemacs ?

Thanks,
Murtaza

@murtaza52 murtaza52 changed the title save buffer on focus last save buffer on focus lost Jul 21, 2015
@murtaza52 murtaza52 reopened this Jul 21, 2015
@sooheon
Copy link

sooheon commented Jul 21, 2015

No, afaik, but you can add a naive version yourself very easily:

(defun sooheon--save-all ()
  (interactive)
  (save-some-buffers t))
;; Auto-save on focus out
(add-hook 'focus-out-hook 'sooheon--save-all)

@EricCrosson
Copy link

I have very similar code

  (add-hook 'focus-out-hook
            (defun save-current-buffer-if-needed ()
              (interactive)
              (when (and (buffer-file-name) (buffer-modified-p))
                (save-buffer))))

This only saves the current buffer, if it belongs to a file and has changes that need to be saved.
Is this issue closed?

@sooheon
Copy link

sooheon commented Jul 23, 2015

Nice improvement, will be using it. Thanks.

@Profpatsch
Copy link
Contributor

I looked at my config and it’s just one line there:

  (add-hook 'focus-out-hook (lambda () (save-some-buffers t)))

Maybe it makes sense to include the function from Emacs Prelude, too.

@StreakyCobra
Copy link
Contributor

The main question of this issue has been answered. If someone thinks it's used widely enough to be included in Spacemacs, feel free to open a PR with this change, it will be better discussed there 😄 Closing this issue in the meantime

@martin-liu
Copy link

Hi guys, I have the same requirement and I don't think this question is answered.

The documentation of focus-out-hook says

Normal hook run when a frame loses input focus.

It's not when switch buffer/window.

I found a solution here http://batsov.com/articles/2012/03/08/emacs-tip-number-5-save-buffers-automatically-on-buffer-or-window-switch/

Code like below:

(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (save-buffer)))

It works for me. So I paste it here.

@steshaw
Copy link

steshaw commented Sep 9, 2016

Did a pull request ever get raised for this?

In Emacs, I use:

(ensure-packages-installed 'focus-autosave-mode)
(focus-autosave-mode)

I've only just downloaded Spacemacs today! It seems that I may expect to have a configuration variable in my ~/.spacemacs file:

dotspacemacs-save-all-buffers-on-lost-focus nil

which I could toggle to t. It arguably should default to t.

@xged
Copy link

xged commented May 19, 2017

@martin-liu It auto-saves after every paste as well :(

@adius
Copy link

adius commented Jan 26, 2018

Pull request for this highly appreciated! 😅

@well1791
Copy link

Hi everyone,

Shouldn't this solution be also in the documentation? At least until #4964 is done.

@syl20bnr If you don't mind I can open a PR updating the docs including this snippet.

;; see: http://batsov.com/articles/2012/03/08/emacs-tip-number-5-save-buffers-automatically-on-buffer-or-window-switch/
(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (save-buffer)))

Thanks @martin-liu.

Also, this one is also great, however, not sure how to install it (any help will be pretty much appreciated).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants