From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#18132: Time for a smarter dired-guess-shell-alist-default? (dired-x.el) Date: Sat, 25 Nov 2023 20:10:40 +0200 Organization: LINKOV.NET Message-ID: <86h6l9zorh.fsf@mail.linkov.net> References: <87d2cn67zo.fsf@mail.jurta.org> <87bns6dcul.fsf@mail.jurta.org> <87silbstsc.fsf@mail.jurta.org> <83lf2k5gna.fsf@gnu.org> <83ilxn3qpo.fsf@gnu.org> <878ryizwyi.fsf@mail.linkov.net> <86y1eoxtrr.fsf@mail.linkov.net> <83leaofebt.fsf@gnu.org> <86edgfk37h.fsf@mail.linkov.net> <83zfz3dyae.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30570"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 18132@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 25 19:22:41 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r6xIe-0007p5-C5 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 25 Nov 2023 19:22:40 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r6xHy-00058c-Gp; Sat, 25 Nov 2023 13:21:58 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r6xHx-00057z-9x for bug-gnu-emacs@gnu.org; Sat, 25 Nov 2023 13:21:57 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r6xHx-0004za-29 for bug-gnu-emacs@gnu.org; Sat, 25 Nov 2023 13:21:57 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r6xI2-0002PY-7N for bug-gnu-emacs@gnu.org; Sat, 25 Nov 2023 13:22:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 25 Nov 2023 18:22:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 18132 X-GNU-PR-Package: emacs Original-Received: via spool by 18132-submit@debbugs.gnu.org id=B18132.17009365079245 (code B ref 18132); Sat, 25 Nov 2023 18:22:02 +0000 Original-Received: (at 18132) by debbugs.gnu.org; 25 Nov 2023 18:21:47 +0000 Original-Received: from localhost ([127.0.0.1]:40189 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r6xHm-0002P3-LC for submit@debbugs.gnu.org; Sat, 25 Nov 2023 13:21:47 -0500 Original-Received: from relay8-d.mail.gandi.net ([2001:4b98:dc4:8::228]:51533) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r6xHf-0002OP-8g for 18132@debbugs.gnu.org; Sat, 25 Nov 2023 13:21:40 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 757631BF206; Sat, 25 Nov 2023 18:21:27 +0000 (UTC) In-Reply-To: <83zfz3dyae.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 24 Nov 2023 16:24:41 +0200") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:275006 Archived-At: --=-=-= Content-Type: text/plain >> >> > Then its possible options for such hook could include: >> >> > >> >> > - xdg-open-function >> >> > - mailcap-function that adds mailcap candidates >> >> > - dired-x-guess that adds existing dired-guess-shell-alist-default >> >> > - Windows-specific candidates from w32-shell-execute >> >> > - etc. >> >> >> >> Here is the implementation: >> > >> > Thanks, but please don't put this stuff in simple.el. There's no >> > reason to have this preloaded in Emacs. >> >> I can't find other place to put these 50 lines. I added to simple.el >> near read-shell-command because it's used by read-shell-command. > > IMO, read-shell-command should not by default call these helpers. It doesn't call them by default. > If we cannot find a suitable non-preloaded file to put this stuff (is > subr-x.el okay, for example?), it's okay to start a new file. Ok, then let's add this to dired-aux.el since it's used in dired. >> Also shell-command-guess is used by dired-read-shell-command >> ('!' and '&' on a file in dired). > > Likewise here: users should by default be able to invoke these Dired > commands without automatically loading those helpers, which come with > quite tangible ballast. For Dired, this is perhaps less important, > since Dired itself is an optional package, but still: I think it's > wrong to do this by default. Ok, I removed mailcap and xdg from defaults: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=shell-command-guess.patch diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index 02194e6ff45..0c01b24c2db 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -763,22 +763,6 @@ dired-trample-file-versions ;;; Shell commands -(declare-function mailcap-file-default-commands "mailcap" (files)) - -(defvar dired-aux-files) - -(defun dired-minibuffer-default-add-shell-commands () - "Return a list of all commands associated with current Dired files. -This function is used to add all related commands retrieved by `mailcap' -to the end of the list of defaults just after the default value." - (interactive) - (let ((commands (and (boundp 'dired-aux-files) - (require 'mailcap nil t) - (mailcap-file-default-commands dired-aux-files)))) - (if (listp minibuffer-default) - (append minibuffer-default commands) - (cons minibuffer-default commands)))) - ;; This is an extra function so that you can redefine it, e.g., to use gmhist. (defun dired-read-shell-command (prompt arg files) "Read a Dired shell command. @@ -789,14 +773,9 @@ dired-read-shell-command Use `dired-guess-shell-command' to offer a smarter default choice of shell command." - (minibuffer-with-setup-hook - (lambda () - (setq-local dired-aux-files files) - (setq-local minibuffer-default-add-function - #'dired-minibuffer-default-add-shell-commands)) - (setq prompt (format prompt (dired-mark-prompt arg files))) - (dired-mark-pop-up nil 'shell files - 'dired-guess-shell-command prompt files))) + (setq prompt (format prompt (dired-mark-prompt arg files))) + (dired-mark-pop-up nil 'shell files + 'dired-guess-shell-command prompt files)) ;;;###autoload (defcustom dired-confirm-shell-command t @@ -1316,7 +1295,7 @@ dired-guess-default ;;;###autoload (defun dired-guess-shell-command (prompt files) "Ask user with PROMPT for a shell command, guessing a default from FILES." - (let ((default (dired-guess-default files)) + (let ((default (shell-command-guess files)) default-list val) (if (null default) ;; Nothing to guess @@ -1340,6 +1319,64 @@ dired-guess-shell-command ;; If we got a return, then return default. (if (equal val "") default val)))) +(defcustom shell-command-guess-functions + '(shell-command-guess-dired) + "List of functions that guess shell commands for files. +Each function receives a list of commands and a list of file names +and should return the same list of commands with changes +such as added new commands." + :type '(repeat + (choice (function-item shell-command-guess-dired) + (function-item shell-command-guess-xdg) + (function-item shell-command-guess-mailcap) + (function :tag "Custom function"))) + :group 'dired + :version "30.1") + +;;;###autoload +(defun shell-command-guess (files) + "Return a list of shell commands, appropriate for FILES." + (let ((commands nil)) + (run-hook-wrapped 'shell-command-guess-functions + (lambda (fun) + (setq commands (funcall fun commands files)) + nil)) + commands)) + +(declare-function mailcap-file-default-commands "mailcap" (files)) + +(defun shell-command-guess-mailcap (commands files) + (require 'mailcap) + (append (mailcap-file-default-commands files) commands)) + +(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep)) +(declare-function dired-guess-default "dired-aux" (files)) + +(defun shell-command-guess-dired (commands files) + (require 'dired-aux) + (append (ensure-list (dired-guess-default files)) commands)) + +(declare-function xdg-mime-apps "xdg" (mime)) +(declare-function xdg-desktop-read-file "xdg" (filename &optional group)) + +(defun shell-command-guess-xdg (commands files) + (require 'xdg) + (let* ((xdg-mime (when (executable-find "xdg-mime") + (string-trim-right + (shell-command-to-string + (concat "xdg-mime query filetype " (car files)))))) + (xdg-mime-apps (unless (string-empty-p xdg-mime) + (xdg-mime-apps xdg-mime))) + (xdg-commands + (mapcar (lambda (desktop) + (setq desktop (xdg-desktop-read-file desktop)) + (propertize + (replace-regexp-in-string + " .*" "" (gethash "Exec" desktop)) + 'name (gethash "Name" desktop))) + xdg-mime-apps))) + (append xdg-commands commands))) + ;;; Commands that delete or redisplay part of the dired buffer @@ -3856,9 +3893,6 @@ dired-vc-deduce-fileset (setq model (vc-checkout-model backend only-files-list)))) (list backend files only-files-list state model))) -(define-obsolete-function-alias 'minibuffer-default-add-dired-shell-commands - #'dired-minibuffer-default-add-shell-commands "29.1") - (provide 'dired-aux) diff --git a/lisp/simple.el b/lisp/simple.el index 02c68912dba..f27c3042d9a 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -4273,7 +4273,6 @@ shell-command-default-error-buffer is run interactively. A value of nil means that output to stderr and stdout will be intermixed in the output stream.") -(declare-function mailcap-file-default-commands "mailcap" (files)) (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep)) (defun minibuffer-default-add-shell-commands () @@ -4284,8 +4283,7 @@ minibuffer-default-add-shell-commands (let* ((filename (if (listp minibuffer-default) (car minibuffer-default) minibuffer-default)) - (commands (and filename (require 'mailcap nil t) - (mailcap-file-default-commands (list filename))))) + (commands (and filename (shell-command-guess (list filename))))) (setq commands (mapcar (lambda (command) (concat command " " filename)) commands)) --=-=-=--