3

I am working in a git-repo using zsh shell.

On the background, git-auto-fetch plugin is running in order to automatically fetch all changes from all remotes while I am working in a git-initialized directory.

Due to this, if the buffer I am using in emacs is touched or changed the file behind my back:

  • I am first asked: emacs has changed since visited or saved. Save anyway? #> Yes

  • Than asked again: example_file.py changed on disk; really edit the buffer? (y, n, r or C-h) #> y


Would it be possible to prevent:

  • emacs has changed since visited or saved. Save anyway?
  • example_file.py changed on disk; really edit the buffer? (y, n, r or C-h)

messages to show up by default and force to make their answer Yes/y?


my save function:

(defun my-save-all ()
       (interactive)
       (let ((message-log-max nil)
             (inhibit-message t))
         (save-some-buffers t)))

(defun save-all ()
  (interactive)
  (my-save-all)
  (bk-kill-buffers "__init__.py")
  (bk-kill-buffers "*helm"))
4

2 Answers 2

3

You might want to check the auto-answer package. It works on functions whose the underlying prompt is yes-or-no-p, read-string, read-from-minibuffer, read-key-sequence, read-key-sequence-vector, read-event , read-passwd or lastly read-multiple-choice.

It's pretty easy to use. Just bind auto-answer to a list of (REGEXP ANSWER):

;; Example usage:
;; (let ((auto-answer '(("\\`What's your name\\? \\'" "jack")
;;                      ("\\`What's your password\\? \\'" "secr3t"))))
;;   (list
;;    (read-string "What's your name? ")
;;    (read-passwd "What's your password? ")))
;; => ("jack" "secr3t")

Hence, in your case, you could do something like this (not tested):

(let ((auto-answer
       '((".*has changed since visited or saved. Save anyway\\?" t)
         (".*changed on disk; really edit the buffer\\?.*" ?y))
     ...
)))

I have read the source code of save-some-buffers, so I'm sure that the first prompt is a yes-or-no-p (thus the answer t), though it's rather obvious. I'm not sure about the second prompt, but it is most likely a read-multiple-choice. If not, you have to figure it out and change the answer accordingly.

auto-answer is currently not on Melpa. You can install it with quelpa-use-package:

(use-package auto-answer
  :quelpa (auto-answer :fetcher github :repo "YoungFrog/auto-answer")
  :ensure t)

or just with quelpa (which is available on Melpa):

(quelpa '(auto-answer :fetcher github :repo "YoungFrog/auto-answer"))
10
  • Is it possible to install auto-answer from package-install?
    – alper
    Commented Jul 21, 2021 at 17:46
  • @alper It's currently not on Melpa, but you can install it with quelpa-use-package. See the update of the answer. Commented Jul 21, 2021 at 18:02
  • it says: Debugger entered--Lisp error: (error "Package ‘auto-answer-’ is unavailable") while using quelpa
    – alper
    Commented Jul 21, 2021 at 18:20
  • 1
    @alper My bad, quelpa-use-package is not on Melpa, but quelpa is. I also assumed you have installed use-package. With quelpa alone, (quelpa '(auto-answer :fetcher github :repo "YoungFrog/auto-answer")) should work. Is that what you tried ? Commented Jul 21, 2021 at 18:29
  • I gave up quelpa and just created its file under a folder and loaded using: (add-to-list 'load-path "~/.emacs.d/lisp/auto-answer") (require 'auto-answer)
    – alper
    Commented Jul 21, 2021 at 18:32
1

Based on an example from the emacs wiki, the following works for me to force Emacs to overwrite external changes to the file:

(defadvice basic-save-buffer (around auto-confirm compile activate)
  (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest args) t))
            ((symbol-function 'y-or-n-p) (lambda (&rest args) t)))
    ad-do-it))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.