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"))
alper
  • 1,238
  • 11
  • 30
  • 1
    Does this answer your question? [Changing confirmation style when deleting files in dired](https://emacs.stackexchange.com/questions/16891/changing-confirmation-style-when-deleting-files-in-dired) – Drew Jul 21 '21 at 15:34
  • 1
    There are also other questions closely related: https://emacs.stackexchange.com/q/22569/105. https://emacs.stackexchange.com/q/17269/105 – Drew Jul 21 '21 at 15:36
  • Would it be possible to apply `Changing confirmation style when deleting files in dired ` into save file cases? @Drew – alper Jul 21 '21 at 17:51
  • I guess not. Sorry; I misunderstood your question, and thought it was more or less asking for how to do `(fset 'yes-or-no-p #'y-or-n-p)`. – Drew Jul 22 '21 at 01:53

2 Answers2

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"))
Firmin Martin
  • 1,265
  • 7
  • 23
  • Is it possible to install auto-answer from package-install? – alper Jul 21 '21 at 17:46
  • @alper It's currently not on Melpa, but you can install it with [quelpa-use-package](https://github.com/quelpa/quelpa-use-package). See the update of the answer. – Firmin Martin Jul 21 '21 at 18:02
  • it says: `Debugger entered--Lisp error: (error "Package ‘auto-answer-’ is unavailable")` while using quelpa – alper Jul 21 '21 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 ? – Firmin Martin Jul 21 '21 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 Jul 21 '21 at 18:32
  • I verify that `(quelpa '(auto-answer :fetcher github :repo "YoungFrog/auto-answer"))` also works – alper Jul 21 '21 at 18:33
  • @alper Yeah, that is faster. I also have issue with `quelpa` in the past... – Firmin Martin Jul 21 '21 at 18:33
  • After applying this solution, once in a while emacs freezes. Do you think would it be possible that `auto-answer` does not respond back and emacs just endlessly waits response . – alper Aug 10 '21 at 21:14
  • @alper It's unlikely as `auto-answer` replace the prompt function call by the answer whenever a regexp matches the prompt or call the prompt function as before, otherwise. So, at best, there is less function calls; at worst, it's the same as before. Moreover, `auto-answer` cannot loop endlessly, as it only loops on the `(regexp . answer)` alist and potentially the `read-multiple-choice`'s alist. You might want to debug it to find where the problem arises exactly. – Firmin Martin Aug 10 '21 at 22:16
  • Like I am using in emacs-daemon, and respone is not entered in minibuffer as `y` or `n` I cannot write any character into the buffer. I just assume in some cased auto-answer might not apply `y` or `n` which leads emacs daemon to halt. – alper Aug 11 '21 at 09:23
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))
Hugo Ideler
  • 111
  • 2