Subject: Re: bug#18132: Time for a smarter dired-guess-shell-alist-default? (dired-x.el)

From: Juri Linkov <[email-masked]>
Date: Sat, 02 Dec 2023 19:44:38 +0200

>> Usually context menus on file managers include two items:
>>
>>   "Open" that opens immediately
>>   "Open With" with a list of commands
>>
>> So "start" will be used for "Open With" if someone prefers this.
>> Then a new command 'dired-do-open' bound to the "Open" menu item
>> will hard-code 'w32-shell-execute' call with the "open" command.
>
> SGTM, thanks.

Here is this new command 'dired-do-open':

diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 1a17ed749e8..7887cc358aa 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1401,6 +1401,32 @@ shell-command-guess-open
   "Populate COMMANDS by the `open' command."
   (append (ensure-list shell-command-guess-open) commands))
 
+(declare-function w32-shell-execute "w32fns.c")
+
+(defun dired-do-open (&optional arg)
+  "Open the marked files or at click/point externally."
+  (interactive "P" dired-mode)
+  (let ((files (if arg
+                   (dired-get-marked-files nil arg)
+                 (save-excursion
+                   (mouse-set-point last-nonmenu-event)
+                   (list (dired-get-filename nil t)))))
+        (command shell-command-guess-open))
+    (when command
+      (cond
+       ((memq system-type '(gnu/linux))
+        (apply #'call-process command nil 0 nil files))
+       ((memq system-type '(ms-dos))
+        (shell-command (concat command " " (mapconcat #'shell-quote-argument files " "))))
+       ((memq system-type '(windows-nt))
+        (w32-shell-execute (or (and (eq command "start") "open") command) (mapconcat #'convert-standard-filename files " ")))
+       ((memq system-type '(cygwin))
+        (apply #'call-process command nil nil nil files))
+       ((memq system-type '(darwin))
+        (apply #'start-process (concat command " " files) nil command files))
+       (t
+        (error "Open not supported on this system"))))))
+
 
 ;;; Commands that delete or redisplay part of the dired buffer
 
diff --git a/lisp/dired.el b/lisp/dired.el
index 97645c731c8..7f4b96353ee 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2591,6 +2591,9 @@ dired-mode-operate-menu
     ["Delete Image Tag..." image-dired-delete-tag
      :help "Delete image tag from current or marked files"]))
 
+(declare-function shell-command-guess "dired-aux" (files))
+(defvar shell-command-guess-open)
+
 (defun dired-context-menu (menu click)
   "Populate MENU with Dired mode commands at CLICK."
   (when (mouse-posn-property (event-start click) 'dired-filename)
@@ -2606,6 +2609,9 @@ dired-context-menu
            :help "Edit file at mouse click"]
           ["Find in Other Window" dired-mouse-find-file-other-window
            :help "Edit file at mouse click in other window"]
+          ,@(when shell-command-guess-open
+              '(["Open" dired-do-open
+                 :help "Open externally"]))
           ,@(when commands
               (list (cons "Open With"
                           (append