Subject: Re: bug#49980: 28.0.50; [PATCH] Should we have project-save-buffers?

From: Arthur Miller <[email-masked]>
Date: Sat, 14 Aug 2021 04:25:43 +0200

Juri Linkov <[email-masked]> writes:

>> I am sorry I am bit late to the party, but I would like to have a
>> command to save all modified buffers belonging to a project directory. I
>> am just about to code something like this for myself and a small project
>> of mine, so I would be happy if you can point me into something that
>> exists.
>>
>> I have coded a little branch switcher with helm. The purpose it so be
>> able to fast switch branch, say you are working on some bigger feature,
>> and than wish for some reason to switch to some other branch and then
>> comeback and continue on the previous one. The workflow is to simply
>> stash everything on change, and apply stash when branch is checked out
>> again. I have it working, minus the part to save all buffers belonging
>> to a project which is root directory wtih .git in my case.
>>
>> I don't know if that fits into your purpose or something, nor do I think
>> it is hard to code, but I always prefer if I can skip duplicating code
>> and per-use soemthing.
>
> Now it's possible just to type 'C-x s' after customizing
> 'save-some-buffers-default-predicate' to 'save-some-buffers-root',
> or after setting this value in project's .dir-locals.el.
>
> If you want to use this new feature programmatically,
> then just call:
>
>   (save-some-buffers t 'save-some-buffers-root)

Hi again, this works just wonderful! :)

Actually it works so good, that I am thinking I am doing something wrong
here.

I created some files in top level and a subdirectory, with some content
and it worked just fine. Literally with just adding the above line:

(save-some-buffers t 'save-some-buffers-root)

Fragment of my use-case:

#+begin_src emacs-lisp

(defun helm-git-branch--checkout (branch)
  (helm-aif (helm-ls-git-root-dir)
      (with-helm-default-directory it
        (with-output-to-string
          (with-current-buffer standard-output
            (save-some-buffers t 'save-some-buffers-root)
            (when (helm-git-branch--dirty-p)
              (helm-git-branch--stash))
            (insert (call-process "git" nil t nil "checkout" branch))
            (helm-git-branch--unstash))))))

#+end_src

So how does it work? save-some-buffers-root auto detects a git repo
root via project-find-functions in project.el or something there? I
havent followed entire call chain. I ask, because I guess this is going
to be part of Emacs 28 but wonder if you have any plans to port it 
into older versions?

Anyway, thank you. That was what I needed, I think you saved me quite
some work.