GNU bug report logs - #63911
Dired Open With

Previous Next

Package: emacs;

Reported by: Juri Linkov <juri <at> linkov.net>

Date: Mon, 5 Jun 2023 16:40:01 UTC

Severity: normal

Tags: patch

Fixed in version 30.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 63911 in the body.
You can then email your comments to 63911 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Mon, 05 Jun 2023 16:40:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Juri Linkov <juri <at> linkov.net>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 05 Jun 2023 16:40:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: bug-gnu-emacs <at> gnu.org
Subject: Dired Open With
Date: Mon, 05 Jun 2023 19:07:49 +0300
[Message part 1 (text/plain, inline)]
Tags: patch

As requested in bug#63875, here is a new context submenu "Open With" in Dired:

[dired-open-with.patch (text/x-diff, inline)]
diff --git a/lisp/dired.el b/lisp/dired.el
index e70467ca53b..a295ece128d 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2504,17 +2504,33 @@ dired-mode-operate-menu
     ["Delete Image Tag..." image-dired-delete-tag
      :help "Delete image tag from current or marked files"]))
 
+(declare-function mailcap-file-default-commands "mailcap" (files))
+
 (defun dired-context-menu (menu click)
   "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 nil t)
+    (let* ((filename (save-excursion
+                       (mouse-set-point click)
+                       (dired-get-filename nil t)))
+           (commands (mailcap-file-default-commands (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"
+                          (mapcar (lambda (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))))))

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Fri, 23 Jun 2023 17:00:02 GMT) Full text and rfc822 format available.

Message #8 received at 63911 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: 63911 <at> debbugs.gnu.org
Subject: Re: bug#63911: Dired Open With
Date: Fri, 23 Jun 2023 19:27:10 +0300
[Message part 1 (text/plain, inline)]
> As requested in bug#63875, here is a new context submenu "Open With" in Dired:
>
> +    (require 'mailcap nil t)
> +    (let* ((filename (save-excursion
> +                       (mouse-set-point click)
> +                       (dired-get-filename nil t)))
> +           (commands (mailcap-file-default-commands (list filename)))

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.

This is not the final patch, maybe more customization needed
before pushing to master.

[dired-context-menu-desktop-commands.patch (text/x-diff, inline)]
diff --git a/lisp/dired.el b/lisp/dired.el
index 914d0a0e783..7d5691df352 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2504,17 +2504,58 @@ dired-mode-operate-menu
     ["Delete Image Tag..." image-dired-delete-tag
      :help "Delete image tag from current or marked files"]))
 
+(declare-function mailcap-file-default-commands "mailcap" (files))
+(declare-function xdg-mime-apps "xdg" (mime))
+(declare-function xdg-desktop-read-file "xdg" (filename &optional group))
+
 (defun dired-context-menu (menu click)
   "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)))
+           (mailcap-commands (mailcap-file-default-commands (list filename)))
+           (xdg-mime (when (executable-find "xdg-mime")
+                       (string-trim-right
+                        (shell-command-to-string
+                         (concat "xdg-mime query filetype " filename)))))
+           (xdg-mime-apps (unless (string-empty-p xdg-mime)
+                            (xdg-mime-apps xdg-mime)))
+           (desktop-commands
+            (mapcar (lambda (desktop)
+                      (setq desktop (xdg-desktop-read-file desktop))
+                      (cons (gethash "Name" desktop)
+                            (replace-regexp-in-string
+                             " .*" "" (gethash "Exec" desktop))))
+                    xdg-mime-apps))
+           (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 (or desktop-commands mailcap-commands)
+              (list (cons "Open With"
+                          (append
+                           (mapcar (lambda (desktop-command)
+                                     `[,(car desktop-command)
+                                       (lambda ()
+                                         (interactive)
+                                         (dired-do-async-shell-command
+                                          ,(cdr desktop-command) nil
+                                          (list ,filename)))])
+                                   desktop-commands)
+                           (mapcar (lambda (mailcap-command)
+                                     `[,mailcap-command
+                                       (lambda ()
+                                         (interactive)
+                                         (dired-do-async-shell-command
+                                          ,mailcap-command nil (list ,filename)))])
+                                   mailcap-commands)))))))
       (dolist (item (reverse (lookup-key easy-menu [menu-bar immediate])))
         (when (consp item)
           (define-key menu (vector (car item)) (cdr item))))))

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Fri, 01 Sep 2023 22:34:01 GMT) Full text and rfc822 format available.

Message #11 received at 63911 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 63911 <at> debbugs.gnu.org
Subject: Re: bug#63911: Dired Open With
Date: Sat, 2 Sep 2023 00:33:29 +0200
Juri Linkov <juri <at> linkov.net> writes:

> 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.

> This is not the final patch, maybe more customization needed
> before pushing to master.

I guess a NEWS item is in order, too.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Sat, 02 Sep 2023 18:41:01 GMT) Full text and rfc822 format available.

Message #14 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Howard Melman <hmelman <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#63911: Dired Open With
Date: Sat, 02 Sep 2023 14:40:13 -0400
Stefan Kangas <stefankangas <at> gmail.com> writes:

> Juri Linkov <juri <at> linkov.net> writes:
>
>> 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.

Will this work (or at least not break) on mac or windows or
other non-xgd systems?

-- 

Howard





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Sat, 02 Sep 2023 19:02:02 GMT) Full text and rfc822 format available.

Message #17 received at 63911 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Howard Melman <hmelman <at> gmail.com>
Cc: 63911 <at> debbugs.gnu.org
Subject: Re: bug#63911: Dired Open With
Date: Sat, 02 Sep 2023 22:01:16 +0300
> From: Howard Melman <hmelman <at> gmail.com>
> Date: Sat, 02 Sep 2023 14:40:13 -0400
> 
> Stefan Kangas <stefankangas <at> gmail.com> writes:
> 
> > Juri Linkov <juri <at> linkov.net> writes:
> >
> >> 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.
> 
> Will this work (or at least not break) on mac or windows or
> other non-xgd systems?

The MS-Windows port already has a function that's the equivalent of
xdg-open: w32-shell-execute.  Patches to add this to the menu
discussed here are welcome.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Sat, 23 Sep 2023 01:11:02 GMT) Full text and rfc822 format available.

Message #20 received at 63911 <at> debbugs.gnu.org (full text, mbox):

From: Björn Bidar <bjorn.bidar <at> thaodan.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 63911 <at> debbugs.gnu.org, Howard Melman <hmelman <at> gmail.com>
Subject: Re: bug#63911: Dired Open With
Date: Sat, 23 Sep 2023 04:10:28 +0300
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Howard Melman <hmelman <at> gmail.com>
>> Date: Sat, 02 Sep 2023 14:40:13 -0400
>> 
>> Stefan Kangas <stefankangas <at> gmail.com> writes:
>> 
>> > Juri Linkov <juri <at> linkov.net> writes:
>> >
>> >> 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.
>> 
>> Will this work (or at least not break) on mac or windows or
>> other non-xgd systems?
>
> The MS-Windows port already has a function that's the equivalent of
> xdg-open: w32-shell-execute.  Patches to add this to the menu
> discussed here are welcome.

For macOS there's man 1 open which behaves very similar to xdg-open.
https://www.unix.com/man-page/osx/1/open/




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Thu, 23 Nov 2023 18:05:02 GMT) Full text and rfc822 format available.

Message #23 received at 63911 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: 63911 <at> debbugs.gnu.org
Subject: Re: bug#63911: Dired Open With
Date: Thu, 23 Nov 2023 19:54:12 +0200
[Message part 1 (text/plain, inline)]
>> 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:

[dired-context-menu-shell-command-guess.patch (text/x-diff, inline)]
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))))))

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63911; Package emacs. (Mon, 27 Nov 2023 17:38:02 GMT) Full text and rfc822 format available.

Message #26 received at 63911 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: 63911 <at> debbugs.gnu.org
Subject: Re: bug#63911: Dired Open With
Date: Mon, 27 Nov 2023 19:36:02 +0200
close 63911 30.0.50
quit

>> 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:

Now pushed to master, and closed.




bug marked as fixed in version 30.0.50, send any further explanations to 63911 <at> debbugs.gnu.org and Juri Linkov <juri <at> linkov.net> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Mon, 27 Nov 2023 17:38:03 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 26 Dec 2023 12:24:05 GMT) Full text and rfc822 format available.

This bug report was last modified 115 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.