all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: 63911@debbugs.gnu.org
Subject: bug#63911: Dired Open With
Date: Fri, 23 Jun 2023 19:27:10 +0300	[thread overview]
Message-ID: <868rca9map.fsf@mail.linkov.net> (raw)
In-Reply-To: <86bkhuney2.fsf@mail.linkov.net> (Juri Linkov's message of "Mon,  05 Jun 2023 19:07:49 +0300")

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

  reply	other threads:[~2023-06-23 16:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-05 16:07 bug#63911: Dired Open With Juri Linkov
2023-06-23 16:27 ` Juri Linkov [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=868rca9map.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=63911@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.