EDIT:
As @hatschipuh pointed out in the comments, an flet
-type construction is more straightforward and doesn't rely on my idiosyncratic advice macro. At least on my Emacs (24.5.1), cl-flet
uses lexical scoping, so you'll need the noflet
package for the following snippet to work. Scroll down for the original, idiosyncratic answer.
(defun my/bypass-confirmation (function &rest args)
"Call FUNCTION with ARGS, bypassing all `y-or-n-p' prompts."
(require 'noflet)
(noflet
((y-or-n-p (prompt) t))
(apply function args)))
(defun my/bypass-confirmation-all (function &rest args)
"Call FUNCTION with ARGS, bypassing all prompts.
This includes both `y-or-n-p' and `yes-or-no-p'."
(require 'noflet)
(noflet
((y-or-n-p (prompt) t)
(yes-or-no-p (prompt) t))
(apply function args)))
EDIT: This is the original answer, except that I fixed my/bypass-confirmation-all
to work as advertised and changed the calling conventions a bit.
Here's a general wrapper that should work for any function.
(defun my/bypass-confirmation (function &rest args)
"Call FUNCTION with ARGS, bypassing all `y-or-n-p' prompts."
(my/with-advice
((#'y-or-n-p :override (lambda (prompt) t)))
(apply function args)))
(defun my/bypass-confirmation-all (function &rest args)
"Call FUNCTION with ARGS, bypassing all prompts.
This includes both `y-or-n-p' and `yes-or-no-p'."
(my/with-advice
((#'y-or-n-p :override (lambda (prompt) t))
(#'yes-or-no-p :override (lambda (prompt) t)))
(apply function args)))
This code depends on this macro, which seems to be my go-to solution for everything on Stackexchange.
(defmacro my/with-advice (adlist &rest body)
"Execute BODY with temporary advice in ADLIST.
Each element of ADLIST should be a list of the form
(SYMBOL WHERE FUNCTION [PROPS])
suitable for passing to `advice-add'. The BODY is wrapped in an
`unwind-protect' form, so the advice will be removed even in the
event of an error or nonlocal exit."
(declare (debug ((&rest (&rest form)) body))
(indent 1))
`(progn
,@(mapcar (lambda (adform)
(cons 'advice-add adform))
adlist)
(unwind-protect (progn ,@body)
,@(mapcar (lambda (adform)
`(advice-remove ,(car adform) ,(nth 2 adform)))
adlist))))
Whether this code is the smartest way to handle this situation, I don't know. In general, I like using this sort of temporary advice to make modifications of existing functions instead of duplicating the original functions' code, in part because it helps to future-proof your modified function against unrelated changes in the original. But in this case you may need to take additional care, because if twittering-mode
updates and changes the prompts or adds an additional one, you won't see the changes. Since a y-or-n prompt indicates a controversial choice, that could have unfortunate consequences.
EDIT: It occurred to me that an example of use might be helpful. This example works with either implementation.
(defun my/twittering-function ()
;; This will bypass `y-or-n-p' in both commands.
(my/bypass-confirmation #'twittering-favorite arg)
(my/bypass-confirmation #'twittering-native-retweet)
;; This will bypass both `y-or-n-p' and `yes-or-no-p' in this command.
(my/bypass-confirmation-all #'twittering-favorite arg)
;; Prompts in this call are not bypassed.
(twittering-native-retweet)