From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id kIImJoTQlGJSjAAAbAwnHQ (envelope-from ) for ; Mon, 30 May 2022 16:11:16 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id aH0nJYTQlGJ3DQAAG6o9tA (envelope-from ) for ; Mon, 30 May 2022 16:11:16 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id F37369220 for ; Mon, 30 May 2022 16:11:15 +0200 (CEST) Received: from localhost ([::1]:39674 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nvg71-00026k-2V for larch@yhetil.org; Mon, 30 May 2022 10:11:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39142) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nvfw0-0002Hr-4m for emacs-orgmode@gnu.org; Mon, 30 May 2022 09:59:52 -0400 Received: from mail-qv1-xf2e.google.com ([2607:f8b0:4864:20::f2e]:37628) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nvfvx-0002qV-Lb for emacs-orgmode@gnu.org; Mon, 30 May 2022 09:59:51 -0400 Received: by mail-qv1-xf2e.google.com with SMTP id b11so10094366qvv.4 for ; Mon, 30 May 2022 06:59:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=ci+kSmtEGNTD5wlbFKbaWDd81fNP43qiigxTvN3Eyek=; b=IYtcbj29tfsfuZ3jC0eLP3xme0DG7ntxYmYvS4J5AVAaPNINFPjU70A3svxQRXyVuw g1honQkFxar3HC8LKeIhCka5MJjBDa7V/LPUcQ1y+sRskzZaOrmQT1qu1pDuL/XGm+Am Boe7XLMw55GENguwhUVI5/BvsPfhkwmEbXHAB8DTnF0MPgh1kl7wJaFYIjN3PiDJC3Eg uAmtNHFwwByt3p47UvXNtuWSJ6mdKlyJu1PjpMknhX1bKKwMNV8WDILASvWBmm2Yhm6s 0+de4B+zzcxhBKv46fdHmc4cCBapag/6fzFWEhkBQob4xLzUrIBAQPDqGIjhC6irWXaQ j+xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=ci+kSmtEGNTD5wlbFKbaWDd81fNP43qiigxTvN3Eyek=; b=VFI2lZ5qBGwTvAe5E7fRxAyeXjnP/v9xqceotrX4vTnlKveMJ0mpv5w18w4UOlnrhy t4OfPAlb6J3KEM6i0fjUZ6i+OlGKJNThcnCEFuPrNCmtLaUL/sWfw/B28O+xaqB9yKzu nFTR/GSoPQVbz3H0o2Eh2szH9rWdDiN/06x4oVaW9/UcG5BXgzi9ZdKpebY3jDK6mtUh yFJ7ubLd42zhAW4VjPwCT1NZgdCI0KZbVPypX49CfnvQwuPu7tLa+0K/xqIcSxeg33sH 9amG6iuzU9XH9PzN4VwXwPmoI0Mh4jbgE/6SHXs/b8VlvoXPhfdVeU2ZZSAb7v/5gRkY z+gA== X-Gm-Message-State: AOAM530PCDSir/KWhm2e4UbFmHDJRWH4VMZLHLRt4hI6NWDdqOtRKtME RKorY5mCNkiVLf6KqiE0Lpgfy1DoBi0= X-Google-Smtp-Source: ABdhPJwx5J2O2uMX1ykHucyCJpfZDH2RqQx3J4UCkRQm94dRVm7/W3OQdY/+tHmBa0LTgoKd1ODnjA== X-Received: by 2002:a05:6214:4006:b0:432:ea2b:5aad with SMTP id kd6-20020a056214400600b00432ea2b5aadmr46282815qvb.39.1653919188532; Mon, 30 May 2022 06:59:48 -0700 (PDT) Received: from localhost (22.b2.a86c.ip4.static.sl-reverse.com. [108.168.178.34]) by smtp.gmail.com with ESMTPSA id 72-20020a370c4b000000b0069fc13ce1e1sm7970088qkm.18.2022.05.30.06.59.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 May 2022 06:59:47 -0700 (PDT) From: Ihor Radchenko To: Max Nikulin Cc: emacs-orgmode@gnu.org Subject: [PATCH v3] Re: Bug in 9.5.3 org--file-default-apps In-Reply-To: References: <963d5f94-3fdf-a01b-bc91-edc99222cb34@gmail.com> <87czgeaxir.fsf@localhost> <6615610d-93ae-171f-b554-3f4cc79354cc@gmail.com> <87a6bhc1w6.fsf@localhost> <86692975-4d5f-6933-3227-c6b208f76862@gmail.com> <877d6lbsg5.fsf@localhost> <7c75b724-1ea2-5e3e-cbe6-e1895fd35bd3@gmail.com> <877d6j2htv.fsf@localhost> <87ilq14p6p.fsf@localhost> <87v8u0396t.fsf@localhost> <87ilpz3bi0.fsf@localhost> <87h75ip5r6.fsf@localhost> <87zgj46hwo.fsf@localhost> Date: Mon, 30 May 2022 22:00:27 +0800 Message-ID: <8735gr15ok.fsf@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::f2e; envelope-from=yantar92@gmail.com; helo=mail-qv1-xf2e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1653919876; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=ci+kSmtEGNTD5wlbFKbaWDd81fNP43qiigxTvN3Eyek=; b=BdjluovM6r50QkTzECBvIRJqeBiT5+/OS1bFMtECsIP/dOP+pV9PB+48UPchzPsYCcew4d 3kMJkEe4RmlvxCgTeQLWWuROAdwYc4PARL/g+0LkaJD//kkTLikNnOGjwkNoZ6buQtx7jO CvmdAPYJgbv4BEWpxjpZ59p8WXI0FwzX3YLl2NSw5hGIeBrFo9P0cYkix3om2Rh98WGy0U MNLpt2gqZzxSAtToSEzSBRHrl2WCB+ounj9YsukNBgI+Ql9X7JLu9T+g2bb+uMEiVtdoW4 WwNlVUi/j9pVvzWOIHwpdoQF/NhH/9FK66OehWuXvEERwSPFyPsgOu5YX/UqoQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1653919876; a=rsa-sha256; cv=none; b=IIbUKfloKDN6Q73ghrKleS1WCMShb54nSZxA4YW+D0zfn/clc7qtpq5xSj+M8LMAIOrxNk rQoYkWPMoutMAob521nxiFXLQEVSKQhi9HLt4Wn1E7ZlygYSxJTUP4gRBz1Q1iXLSL4elU pIbRznfxl0c8S4bxR5IFfxWDUmg0dYx1UJaNh3rpDmOyFqtW4Xiu2ezhHJj+XIeBO6tCzA 9CUuItxVbPpwyIjsof+MUIcCDHCUy22oCADZey35FsNPtmXrSZ2Wzy9Q1s1PftvSZIKlMg IslIV1ymo3OJd/HdvUJYgikQxhvVF7mcTIJPZbScNR62O55dupT+g7oLx+dtFw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IYtcbj29; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -8.33 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IYtcbj29; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: F37369220 X-Spam-Score: -8.33 X-Migadu-Scanner: scn0.migadu.com X-TUID: grnWTrBXIm+W --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Max Nikulin writes: > However, in my opinion, it does not address original Craig's issue. The=20 > patch improves handling of *non-text* files requiring *external*=20 > viewers, while Craig complained that behavior for shell script is=20 > incorrect and his problem is tightly bound to erased `mailcap-mime-data'. Ideally, we need a feedback from him. For emacs-27 specifically, we might need to work around the bug. However, I am not sure what would be the best way to do it. The easiest can be changing the default value of org-file-apps-gnu on Emacs 27 specifically to not use mailcap at all. But I am pretty sure that we can do better. > I can not reproduce behavior he observed exactly, Org does not opens=20 > shell scripts by less, but it tries and silently (it is expected) fails. > > My results (emacs-27): > 1. If there is no mailcap files at all, the script is opened > in the same emacs session that is correct from my point of view. > 2. If I add ~/.mailcap > text/plain; less '%s'; needsterminal > then I get silent failures > Running less /etc/profile...done > 3. With your patch and the following additional entry in ~/.mailcap > (notice "text" instead of "application" and just "emacs") > text/x-shellscript; emacs %s; test=3Dtest -n "$DISPLAY" > new Emacs session is started. It is a problem but partially > it is caused by incorrect mailcap configuration. > Unlike "text/plain" that would be handled by view-mode > unless `mailcap-mime-data' were erased in Emacs-27, > "text/x-shellscript" is handled by less on my main system > due to mailcap while I would expect same Emacs session. I am confused here. org-file-apps-gnu says that we rely on mailcap: ((remote . emacs) (system . mailcap) (t . mailcap)) So, is (3) following what you would expect from mailcap (regardless whether it is incorrectly configured or not)? Wrong configuration of mailcap is none of Org business - we need not to be "smart" and fix user "errors". They may be deliberate. > I read implementation of `org-open-file' once more and now I see that=20 > currently remote files can not be processed by mailcap code path even=20 > with custom `org-file-apps', so thank you for explanation. In some cases= =20 > it may be handy to launch remote viewer from a host accessed through=20 > ssh, but let's leave it aside. This is exactly why you can always customize org-file-apps.=20 >> - (let* ((mime-type (mailcap-extension-to-mime (or ext ""))) >> + (let* ((mime-type (if (executable-find "file") > > I would consider (and ... (not remp)) despite currently it is redundant.= =20 > Just to mitigate consequences if other parts of this complicated=20 > function will be modified. On the other hand=20 > `start-process-shell-command' is not ready for remote files anyway, so=20 > major cleanup would be required. I would be in favor of a cleanup (by someone=E2=84=A2), but I am against redundancy. Such redundancy may mask bugs making them difficult to debug. Not to mention code readability. >> + (shell-command-to-string >> + (format "%s --brief --mime-type %s" >> + (executable-find "file") >> + (shell-quote-argument (convert-sta= ndard-filename file)))) > > It is not enough to cure my paranoia. However the following case is=20 > rather pathological > > mkdir 'Program Files' > ln -s /usr/bin/file 'Program Files'/ > PATH=3D"$HOME/Program Files:$PATH" \ > emacs -Q -L ~/src/org-mode/lisp/ ~/examples/org/open-script.org & > > Debugger entered--Lisp error: (wrong-type-argument stringp nil) > string-match("/" nil 0) > split-string(nil "/") > mailcap-mime-info("/bin/bash: line 1: /home/ubuntu/Program: No such=20 > f...") > (let* ((mime-type (if (executable-find "file")=20 > (shell-command-to-string (format "%s --brief --mime-type %s"=20 > (executable-find "file") (shell-quote-argument=20 > (convert-standard-filename file)))) (mailcap-extension-to-mime (or ext=20 > "")))) (command (mailcap-mime-info mime-type))) (if (stringp command)=20 > (setq cmd command) (setq cmd 'emacs))) Well. If we want to be this paranoid, could you write a generic safe shell-command wrapper that takes care of various edge cases? Then, we can add that wrapper to org-macs and reuse it in various places where we need to run external command. > Another corner case: > > file --brief --mime-type tstorg-sh-symlink > inode/symlink > file --brief --mime-type --dereference tstorg-sh-symlink > text/x-shellscript I added the extra argument as you suggested. See the new version of the patch. Though my man tells me that --dereference is the default. Not on your system apparently. >> + (executable-find "file") >> + (shell-quote-argument (convert-sta= ndard-filename file)))) >> + (mailcap-extension-to-mime (or ext "")))) > > Actually MIME type for shell scripts varies a lot > > (mailcap-extension-to-mime "sh") =3D> "text/x-sh" > > run-mailcap --norun examples/org/script/tstorg.sh > Error: no "view" mailcap rules found for type "application/x-sh" > > And "text/x-shellscript" as above. This should not matter for us. As long as mailcap-mime-info returns something meaningful, we should be good to go. Unless mailcap-mime-info itself is buggy. Best, Ihor --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=v3-0001-org-open-file-Use-file-command-to-determine-mime-.patch >From 3dac7d62b7c85bde31d27ef48569df9d07ae6eec Mon Sep 17 00:00:00 2001 Message-Id: <3dac7d62b7c85bde31d27ef48569df9d07ae6eec.1653918246.git.yantar92@gmail.com> From: Ihor Radchenko Date: Sun, 22 May 2022 12:04:35 +0800 Subject: [PATCH v3] org-open-file: Use file command to determine mime type, when available * lisp/org.el (org-open-file): Prefer file command to determine file type instead of relying on `mailcap-extension-to-mime'. Only fallback to the latter when file command is not available on the system. Fixes https://list.orgmode.org/874k1n2hpv.fsf@localhost/T/#mcc10df4ea7937360671e6dea8061153dee518807 --- lisp/org.el | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lisp/org.el b/lisp/org.el index 95dff27ad..b9a7b4b2e 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -7981,7 +7981,12 @@ (defun org-open-file (path &optional in-emacs line search) (when (eq cmd 'mailcap) (require 'mailcap) (mailcap-parse-mailcaps) - (let* ((mime-type (mailcap-extension-to-mime (or ext ""))) + (let* ((mime-type (if (executable-find "file") + (shell-command-to-string + (format "%s --brief --mime-type --dereference %s" + (executable-find "file") + (shell-quote-argument (convert-standard-filename file)))) + (mailcap-extension-to-mime (or ext "")))) (command (mailcap-mime-info mime-type))) (if (stringp command) (setq cmd command) -- 2.35.1 --=-=-=--