From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: [Patch/Question] Likely bugs in mailcap docs and mailcap-view-file Date: Fri, 14 Jan 2022 22:24:43 +0100 Message-ID: <875yqmvvx1.fsf@gnu.org> 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="10489"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.7.5; emacs 29.0.50 To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jan 14 22:41:37 2022 Return-path: Envelope-to: ged-emacs-devel@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 1n8UKG-0002YI-Rj for ged-emacs-devel@m.gmane-mx.org; Fri, 14 Jan 2022 22:41:37 +0100 Original-Received: from localhost ([::1]:34196 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n8UKF-0001AB-8B for ged-emacs-devel@m.gmane-mx.org; Fri, 14 Jan 2022 16:41:35 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:58066) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n8UIf-0000Mv-UJ for emacs-devel@gnu.org; Fri, 14 Jan 2022 16:39:57 -0500 Original-Received: from [2001:470:142:3::e] (port=56760 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n8UIf-0005Gt-JJ for emacs-devel@gnu.org; Fri, 14 Jan 2022 16:39:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=dWVZfYvCpEuycRQBphKzVVOQeJE7BlvFOBLSMrz5kjw=; b=T2BnTnEL7Ml2eP 88SUYKSU1be6arLnqO46v4ocxaDqPExq/raE8IN39p9CI4jTzcoJu0gQbe61r/+ptOmJGM7XIYwfo uWG+7KJGLtXOX9phf9nPQD5bPCMIsJZV/Yj0yKaBWaO1bbArMU7bmGTsGD8Ti1rAjShmgqfRzZu71 1QMNakFXYu951NJrWYhY4lUdnSORIljYyQiJpkKEPh/ePmRCizN5FRF/Lz/oXrw8Cx2aZH//Wrtw5 TVGhFG0Ko2ug2N6fyPd7D/nr5VC5Yxb/WLO0ejLrTh2Jjvn46BU4JFBdAEsUHtDIJIcxGRzCdbTXg WF679zD8uffviBr36Wug==; Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:41649) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n8UIf-0001DL-VZ for emacs-devel@gnu.org; Fri, 14 Jan 2022 16:39:57 -0500 Original-Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id 20EEE27C005C for ; Fri, 14 Jan 2022 16:39:57 -0500 (EST) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 14 Jan 2022 16:39:57 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrtdehgdduheduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpegfhffvufffkfggtgesmhdtreertd erjeenucfhrhhomhepvfgrshhsihhlohcujfhorhhnuceothhsughhsehgnhhurdhorhhg qeenucggtffrrghtthgvrhhnpedvvdefkeegffektdeileekhefhtdffgfettdevvdeuke ejvedvfefggefhleehudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpehthhhorhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqd ekieejfeekjeekgedqieefhedvleekqdhtshguhheppehgnhhurdhorhhgsehfrghsthhm rghilhdrfhhm X-ME-Proxy: Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Fri, 14 Jan 2022 16:39:56 -0500 (EST) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:284760 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi all, I wanted to use `mailcap-view-file' in some package (mu4e) in order to view some attachment using some appropriate viewer. I've tested with some pdf attachment which triggered an error because I use pdf-tools which adds an entry to `mailcap-mime-data' where the viewer is the symbol `pdf-view-mode' (a major-mode) whereas `mailcap-view-file' currently only handles the "viewer is a string naming some executable" case. The default value of `mailcap-mime-data' has tons of viewers being major-mode symbols, so that can't be wrong. It's docstring also says: Where VIEWERINFO specifies how the content-type is viewed. Can be a string, in which case it is run through a shell, with appropriate parameters, or a symbol, in which case the symbol is =E2=80=98funcall= =E2=80=99ed if and only if it exists as a function, with the buffer as an argument. Well, that's strange. All symbol-valued VIEWERINFOs are functions of zero args, mostly major-modes. They can't get the buffer (which buffer?!) as an argument. So I propose the attached patch which teaches `mailcap-view-file' the symbol/function-valued viewer case and adjust the docs to match what's already there. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=mailcap.diff diff --git a/lisp/net/mailcap.el b/lisp/net/mailcap.el index bf3c8edd1e..151c6e0509 100644 --- a/lisp/net/mailcap.el +++ b/lisp/net/mailcap.el @@ -319,8 +319,9 @@ mailcap-mime-data Where VIEWERINFO specifies how the content-type is viewed. Can be a string, in which case it is run through a shell, with appropriate -parameters, or a symbol, in which case the symbol is `funcall'ed if -and only if it exists as a function, with the buffer as an argument. +parameters, or a symbol, in which case the symbol must name a function +of zero arguments which is called in a buffer holding the MIME part's +content. TESTINFO is a test for the viewer's applicability, or nil. If nil, it means the viewer is always valid. If it is a Lisp function, it is @@ -1174,34 +1175,45 @@ mailcap-view-file (mailcap-parse-mailcaps) (let ((command (mailcap-mime-info (mailcap-extension-to-mime (file-name-extension file))))) - (unless command - (error "No viewer for %s" (file-name-extension file))) - ;; Remove quotes around the file name - we'll use shell-quote-argument. - (while (string-match "['\"]%s['\"]" command) - (setq command (replace-match "%s" t t command))) - (setq command (replace-regexp-in-string - "%s" - (shell-quote-argument (convert-standard-filename file)) - command - nil t)) - ;; Handlers such as "gio open" and kde-open5 start viewer in background - ;; and exit immediately. Avoid `start-process' since it assumes - ;; :connection-type `pty' and kills children processes with SIGHUP - ;; when temporary terminal session is finished (Bug#44824). - ;; An alternative is `process-connection-type' let-bound to nil for - ;; `start-process-shell-command' call (with no chance to report failure). - (make-process - :name "mailcap-view-file" - :connection-type 'pipe - :buffer nil ; "*Messages*" may be suitable for debugging - :sentinel (lambda (proc event) - (when (and (memq (process-status proc) '(exit signal)) - (/= (process-exit-status proc) 0)) - (message - "Command %s: %s." - (mapconcat #'identity (process-command proc) " ") - (substring event 0 -1)))) - :command (list shell-file-name shell-command-switch command)))) + (if (functionp command) + ;; command is a viewer function (a mode) expecting the file + ;; contents to be in the current buffer. + (let ((buf (generate-new-buffer (file-name-nondirectory file)))) + (set-buffer buf) + (insert-file-contents file) + (setq buffer-file-name file) + (funcall command) + (set-buffer-modified-p nil) + (pop-to-buffer buf)) + ;; command is a program to run with file as an argument. + (unless command + (error "No viewer for %s" (file-name-extension file))) + ;; Remove quotes around the file name - we'll use shell-quote-argument. + (while (string-match "['\"]%s['\"]" command) + (setq command (replace-match "%s" t t command))) + (setq command (replace-regexp-in-string + "%s" + (shell-quote-argument (convert-standard-filename file)) + command + nil t)) + ;; Handlers such as "gio open" and kde-open5 start viewer in background + ;; and exit immediately. Avoid `start-process' since it assumes + ;; :connection-type `pty' and kills children processes with SIGHUP + ;; when temporary terminal session is finished (Bug#44824). + ;; An alternative is `process-connection-type' let-bound to nil for + ;; `start-process-shell-command' call (with no chance to report failure). + (make-process + :name "mailcap-view-file" + :connection-type 'pipe + :buffer nil ; "*Messages*" may be suitable for debugging + :sentinel (lambda (proc event) + (when (and (memq (process-status proc) '(exit signal)) + (/= (process-exit-status proc) 0)) + (message + "Command %s: %s." + (mapconcat #'identity (process-command proc) " ") + (substring event 0 -1)))) + :command (list shell-file-name shell-command-switch command))))) (provide 'mailcap) --=-=-= Content-Type: text/plain Does anyone see a problem with that? I'm not very familiar with that code. Bye, Tassilo --=-=-=--