Subject: Re: bug#13625: 24.1; Enable 'package-menu-execute being non-interactive

From: Yves Baumes <[email-masked]>
Date: Mon, 4 Feb 2013 18:59:22 +0100

Le 4 févr. 2013 à 16:13, Stefan Monnier a écrit :

> Maybe the best option here is to add an optional `dont-query' argument.

I've fiddled around a little bit, so tell me if my reasoning is ok.
I've found that whatever you do, an interactive call provide a
numeric value of '1'. Reading the Emacs Lisp Reference Manual,
I didn't find a way to negate the boolean value with `interactive'. As a matter
of fact I would suggest that `dont-query' must not be a good name since its value
would be 'true' when the function is called interactively while we want the user
to be queried in that case.

I made my modification with `prompt-user' as a parameter name.
One could think as another alternative, such as `query-user'.
Would it be more appropriate? I can't tell which one sounds better actually.
I am not fluent in english.. :-/

So here is the diff:

=== modified file 'lisp/emacs-lisp/package.el'
--- lisp/emacs-lisp/package.el	2013-01-01 09:11:05 +0000
+++ lisp/emacs-lisp/package.el	2013-02-04 17:17:41 +0000
@@ -1588,11 +1588,11 @@
 	       (length upgrades)
 	       (if (= (length upgrades) 1) "" "s")))))
 
-(defun package-menu-execute ()
+(defun package-menu-execute (&optional prompt-user)
   "Perform marked Package Menu actions.
 Packages marked for installation are downloaded and installed;
 packages marked for deletion are removed."
-  (interactive)
+  (interactive "p")
   (unless (derived-mode-p 'package-menu-mode)
     (error "The current buffer is not in Package Menu mode"))
   (let (install-list delete-list cmd id)
@@ -1611,26 +1611,30 @@
 		 (push (car id) install-list))))
 	(forward-line)))
     (when install-list
-      (if (yes-or-no-p
-	   (if (= (length install-list) 1)
-	       (format "Install package `%s'? " (car install-list))
-	     (format "Install these %d packages (%s)? "
-		     (length install-list)
-		     (mapconcat 'symbol-name install-list ", "))))
+      (if (or
+           (not prompt-user)
+           (yes-or-no-p
+            (if (= (length install-list) 1)
+                (format "Install package `%s'? " (car install-list))
+              (format "Install these %d packages (%s)? "
+                      (length install-list)
+                      (mapconcat 'symbol-name install-list ", ")))))
 	  (mapc 'package-install install-list)))
     ;; Delete packages, prompting if necessary.
     (when delete-list
-      (if (yes-or-no-p
-	   (if (= (length delete-list) 1)
-	       (format "Delete package `%s-%s'? "
-		       (caar delete-list)
-		       (cdr (car delete-list)))
-	     (format "Delete these %d packages (%s)? "
-		     (length delete-list)
-		     (mapconcat (lambda (elt)
-				  (concat (car elt) "-" (cdr elt)))
-				delete-list
-				", "))))
+      (if (or
+           (not prompt-user)
+           (yes-or-no-p
+            (if (= (length delete-list) 1)
+                (format "Delete package `%s-%s'? "
+                        (caar delete-list)
+                        (cdr (car delete-list)))
+              (format "Delete these %d packages (%s)? "
+                      (length delete-list)
+                      (mapconcat (lambda (elt)
+                                   (concat (car elt) "-" (cdr elt)))
+                                 delete-list
+                                 ", ")))))
 	  (dolist (elt delete-list)
 	    (condition-case-unless-debug err
 		(package-delete (car elt) (cdr elt))

The new code in this patch negates the `prompt-user' value since we want to
apply the changes when the user is not prompted *or* in the case he must be
prompted then he must reply 'yes'.

Maybe a good Changelog line could be:
"* emacs-lisp/package.el(package-menu-execute): Don't prompt for the user when `package-menu-execute' is called non-interactively."

Tell me if I forget something, or if something is wrong for you.

Regards
Yves