unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#63911: Dired Open With
@ 2023-06-05 16:07 Juri Linkov
  2023-06-23 16:27 ` Juri Linkov
  0 siblings, 1 reply; 8+ messages in thread
From: Juri Linkov @ 2023-06-05 16:07 UTC (permalink / raw)
  To: 63911

[-- Attachment #1: Type: text/plain, Size: 93 bytes --]

Tags: patch

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: dired-open-with.patch --]
[-- Type: text/x-diff, Size: 1951 bytes --]

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

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-06-05 16:07 bug#63911: Dired Open With Juri Linkov
@ 2023-06-23 16:27 ` Juri Linkov
  2023-09-01 22:33   ` Stefan Kangas
  0 siblings, 1 reply; 8+ messages in thread
From: Juri Linkov @ 2023-06-23 16:27 UTC (permalink / raw)
  To: 63911

[-- Attachment #1: Type: text/plain, Size: 960 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: dired-context-menu-desktop-commands.patch --]
[-- Type: text/x-diff, Size: 3386 bytes --]

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

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-06-23 16:27 ` Juri Linkov
@ 2023-09-01 22:33   ` Stefan Kangas
  2023-09-02 18:40     ` Howard Melman
  2023-11-23 17:54     ` Juri Linkov
  0 siblings, 2 replies; 8+ messages in thread
From: Stefan Kangas @ 2023-09-01 22:33 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 63911

Juri Linkov <juri@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.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-09-01 22:33   ` Stefan Kangas
@ 2023-09-02 18:40     ` Howard Melman
  2023-09-02 19:01       ` Eli Zaretskii
  2023-11-23 17:54     ` Juri Linkov
  1 sibling, 1 reply; 8+ messages in thread
From: Howard Melman @ 2023-09-02 18:40 UTC (permalink / raw)
  To: 63911

Stefan Kangas <stefankangas@gmail.com> writes:

> Juri Linkov <juri@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






^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-09-02 18:40     ` Howard Melman
@ 2023-09-02 19:01       ` Eli Zaretskii
  2023-09-23  1:10         ` Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2023-09-02 19:01 UTC (permalink / raw)
  To: Howard Melman; +Cc: 63911

> From: Howard Melman <hmelman@gmail.com>
> Date: Sat, 02 Sep 2023 14:40:13 -0400
> 
> Stefan Kangas <stefankangas@gmail.com> writes:
> 
> > Juri Linkov <juri@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.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-09-02 19:01       ` Eli Zaretskii
@ 2023-09-23  1:10         ` Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 8+ messages in thread
From: Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-23  1:10 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 63911, Howard Melman

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Howard Melman <hmelman@gmail.com>
>> Date: Sat, 02 Sep 2023 14:40:13 -0400
>> 
>> Stefan Kangas <stefankangas@gmail.com> writes:
>> 
>> > Juri Linkov <juri@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/





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-09-01 22:33   ` Stefan Kangas
  2023-09-02 18:40     ` Howard Melman
@ 2023-11-23 17:54     ` Juri Linkov
  2023-11-27 17:36       ` Juri Linkov
  1 sibling, 1 reply; 8+ messages in thread
From: Juri Linkov @ 2023-11-23 17:54 UTC (permalink / raw)
  To: 63911

[-- Attachment #1: Type: text/plain, Size: 721 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: dired-context-menu-shell-command-guess.patch --]
[-- Type: text/x-diff, Size: 1853 bytes --]

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

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#63911: Dired Open With
  2023-11-23 17:54     ` Juri Linkov
@ 2023-11-27 17:36       ` Juri Linkov
  0 siblings, 0 replies; 8+ messages in thread
From: Juri Linkov @ 2023-11-27 17:36 UTC (permalink / raw)
  To: 63911

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.





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-11-27 17:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-05 16:07 bug#63911: Dired Open With Juri Linkov
2023-06-23 16:27 ` Juri Linkov
2023-09-01 22:33   ` Stefan Kangas
2023-09-02 18:40     ` Howard Melman
2023-09-02 19:01       ` Eli Zaretskii
2023-09-23  1:10         ` Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-23 17:54     ` Juri Linkov
2023-11-27 17:36       ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).