Subject: bug#63911: Dired Open With

From: Juri Linkov <[email-masked]>
Date: Thu, 23 Nov 2023 19:54:12 +0200

>> After using this for a while I noticed that most of the time
>> mailcap suggestions are useless - they don't contain the same
>> items as in the context menus of a File Manager.  For example,
>> clicking the right mouse button on a ScreamTracker .stm file
>> opens a menu with Rhythmbox, and clicking on a ScreamTracker3
>> .s3m file opens a menu where the first item is Celluloid
>> and the second is Rhythmbox.
>>
>> So I implemented support for xdg commands that now displays exactly
>> the same menus in Emacs as in the File Manager, and with the same order.
>
> Agreed, it's better to use the xdg commands for this.

Here is a new patch that uses a new generalized function
'shell-command-guess' from bug#18132:

diff --git a/lisp/dired.el b/lisp/dired.el
index c212e3094f8..bb55add9d33 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2533,13 +2533,30 @@ dired-context-menu
   "Populate MENU with Dired mode commands at CLICK."
   (when (mouse-posn-property (event-start click) 'dired-filename)
     (define-key menu [dired-separator] menu-bar-separator)
-    (let ((easy-menu (make-sparse-keymap "Immediate")))
+    (require 'mailcap)
+    (require 'xdg)
+    (let* ((filename (save-excursion
+                       (mouse-set-point click)
+                       (dired-get-filename nil t)))
+           (commands (shell-command-guess (list filename)))
+           (easy-menu (make-sparse-keymap "Immediate")))
       (easy-menu-define nil easy-menu nil
-        '("Immediate"
+        `("Immediate"
           ["Find This File" dired-mouse-find-file
            :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"]))
+           :help "Edit file at mouse click in other window"]
+          ,@(when commands
+              (list (cons "Open With"
+                          (append
+                           (mapcar (lambda (command)
+                                     `[,(or (get-text-property 0 'name command)
+                                            command)
+                                       (lambda ()
+                                         (interactive)
+                                         (dired-do-async-shell-command
+                                          ,command nil (list ,filename)))])
+                                   commands)))))))
       (dolist (item (reverse (lookup-key easy-menu [menu-bar immediate])))
         (when (consp item)
           (define-key menu (vector (car item)) (cdr item))))))