3

Every time when I call M-x projectile-regenate-tags I get bothered with the message

Tags file a:/foobar/TAGS has changed, read new contents? (y or n)

which is quite cumbersome. Especially when I wanted to add the command to a after-save-hook.

So I was thinking about a defadvice, or command to press y in the minbuffer that I could do in a function same with (projectile-regenate-tags).

Any idea in order to do so?

  • 1
    Find the code that invokes the prompt, and act accordingly. Maybe there is a hook or an option that affects the behavior. Or maybe you need to advise or even redefine some function. The devil is in the details. IOW, look at the code. – Drew Aug 11 '15 at 20:28
6

The tags-revert-without-query variable is the generic mechanism for skipping this prompt.

Check whether setting that directly circumvents the problem. If so, you could let-bind it in a wrapper around the function you're calling to get that behaviour for this scenario only.

  • This is a more clever way to do. Thanks. If I might ask, how did you know that such variable exists? – ReneFroger Aug 12 '15 at 12:00
  • 2
    You can (and should) ask Emacs. M-x apropos-user-option RET tag RET is a useful approach for discovering tag-related config options (n.b. use after the library has loaded, or Emacs won't know about all the options). Or you might use the broader apropos-variable. There are many useful apropos-* commands (which, naturally, you can discover with M-x apropos-command RET apropos RET). – phils Aug 12 '15 at 12:47
  • I get it, very useful indeed. Thanks. – ReneFroger Aug 12 '15 at 20:27
1

Here's a way to do that generically for any function (even if it has no convenient variable)

(defun y-or-n-p-test ()
  (message (if (y-or-n-p "is this true?")
             "yes!"
             "no!")))

(defun auto-yes (old-fun &rest args)
  (cl-letf (((symbol-function 'y-or-n-p) (lambda (prompt) t))
             ((symbol-function 'yes-or-no-p) (lambda (prompt) t)))
    (apply old-fun args)))

(defun auto-no (old-fun &rest args)
  (cl-letf (((symbol-function 'y-or-n-p) (lambda (prompt) nil))
             ((symbol-function 'yes-or-no-p) (lambda (prompt) nil)))
    (apply old-fun args)))

(advice-add #'y-or-n-p-test :around #'auto-yes)

;; just testing
(y-or-n-p-test)
  • Thanks Pythonnut for your answer. Technically it's the answer that I was looking for, but I decided to go for phils, because his answer is only targeting the question, while your question applies for all sort of questions, which is not desirable. Nevertheless, your input is really appreciated! – ReneFroger Aug 12 '15 at 12:01
  • I'd rather define an auto-answer advice which would answer automatically whatever a global dynamical variable tells it to answer. Then auto-answering would amount to (let ((that-global-var value)) code-asks-questions). WDYT ? – YoungFrog Aug 16 '16 at 10:37
  • @YoungFrog that would be as simple as replacing the nil or t inside the lambda with your variable. – PythonNut Aug 16 '16 at 15:05
  • @PythonNut Almost, yes. I wrote the code and posted it as an answer (too long to fit here). – YoungFrog Aug 18 '16 at 9:30
1

This is only a variation on PythonNut's answer but it's too long to fit properly in a comment. The main difference is that I advise once, and then the desired behaviour is obtained by binding a dynamical variable.

;; -*- mode: emacs-lisp; lexical-binding: t; -*-

(defvar auto-answer 'ask
  "When bound, yes-or-no questions will not prompt.

When this variable is bound to a boolean value, the functions
`yes-or-no-p' and `y-or-n-p' will return that value without
prompting.  With its default value (the symbol `ask'), the
functions will prompt normally.")

(defun auto-answer (oldfun &rest args)
  (if (booleanp auto-answer)
      auto-answer
    (apply oldfun args)))

(advice-add 'y-or-n-p :around 'auto-answer)
(advice-add 'yes-or-no-p :around 'auto-answer)

;; test it:
(y-or-n-p "foo")
(let ((auto-answer t))
  (y-or-n-p "foo"))
(let ((auto-answer nil))
  (y-or-n-p "foo"))
  • Thanks for sharing. I never expected I would get an useful answer on this exactly 1 year later! Thanks! – ReneFroger Aug 18 '16 at 12:22
  • An idea for improvement would be to make the behaviour dependent on the message in the prompt. – YoungFrog Aug 19 '16 at 18:47
  • I wrote a version which acts depending on the prompt text, it's available at github.com/YoungFrog/auto-answer (slightly different calling convention, look at the example at the top) – YoungFrog Sep 1 '16 at 12:20
  • Thanks, looks interesting. Do you mind if it was posted on reddit.com/r/emacs? – ReneFroger Sep 1 '16 at 20:31
  • @ReneFroger I don't mind – YoungFrog Sep 1 '16 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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