From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#63911: Dired Open With Date: Fri, 23 Jun 2023 19:27:10 +0300 Organization: LINKOV.NET Message-ID: <868rca9map.fsf@mail.linkov.net> References: <86bkhuney2.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38276"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) To: 63911@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jun 23 19:00:07 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qCk8k-0009i2-1K for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 23 Jun 2023 19:00:06 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qCk8h-00029o-TF; Fri, 23 Jun 2023 13:00:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qCk8g-00029S-UF for bug-gnu-emacs@gnu.org; Fri, 23 Jun 2023 13:00:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qCk8g-0007JU-JS for bug-gnu-emacs@gnu.org; Fri, 23 Jun 2023 13:00:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qCk8g-0004Lg-Ee for bug-gnu-emacs@gnu.org; Fri, 23 Jun 2023 13:00:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 23 Jun 2023 17:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63911 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 63911-submit@debbugs.gnu.org id=B63911.168753958216637 (code B ref 63911); Fri, 23 Jun 2023 17:00:02 +0000 Original-Received: (at 63911) by debbugs.gnu.org; 23 Jun 2023 16:59:42 +0000 Original-Received: from localhost ([127.0.0.1]:39487 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qCk8L-0004KH-Gc for submit@debbugs.gnu.org; Fri, 23 Jun 2023 12:59:41 -0400 Original-Received: from relay5-d.mail.gandi.net ([217.70.183.197]:55573) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qCk8J-0004Jz-K8 for 63911@debbugs.gnu.org; Fri, 23 Jun 2023 12:59:40 -0400 X-GND-Sasl: juri@linkov.net Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id E311C1C0008 for <63911@debbugs.gnu.org>; Fri, 23 Jun 2023 16:59:32 +0000 (UTC) In-Reply-To: <86bkhuney2.fsf@mail.linkov.net> (Juri Linkov's message of "Mon, 05 Jun 2023 19:07:49 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:263965 Archived-At: --=-=-= Content-Type: text/plain > 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. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=dired-context-menu-desktop-commands.patch 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)))))) --=-=-=--