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

From: Juri Linkov <[email-masked]>
Date: Sun, 03 Dec 2023 19:04:55 +0200

>> Here is this new command 'dired-do-open':
>
> Apologies for taking only a cursory look.
>
> 1. It doesn't use the marked files, contrary to the doc.
> 2. On MS Windows, at least, it doesn't work for files
>    with embedded space chars.

You are absolutely right.  The previous version used wrong assumptions.
Please try a better one:

diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 1a17ed749e8..23c7a9e0027 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1401,6 +1401,36 @@ 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 a file at click/point externally."
+  (interactive "P" dired-mode)
+  (let ((files (if (mouse-event-p last-nonmenu-event)
+                   (save-excursion
+                     (mouse-set-point last-nonmenu-event)
+                     (dired-get-marked-files nil arg))
+                 (dired-get-marked-files nil arg)))
+        (command shell-command-guess-open))
+    (when (and (memq system-type '(windows-nt))
+               (equal command "start"))
+      (setq command "open"))
+    (when command
+      (dolist (file files)
+        (cond
+         ((memq system-type '(gnu/linux))
+          (call-process command nil 0 nil file))
+         ((memq system-type '(ms-dos))
+          (shell-command (concat command " " (shell-quote-argument file))))
+         ((memq system-type '(windows-nt))
+          (w32-shell-execute command (convert-standard-filename file)))
+         ((memq system-type '(cygwin))
+          (call-process command nil nil nil file))
+         ((memq system-type '(darwin))
+          (start-process (concat command " " file) nil command file))
+         (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