From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: Re: bug-reference.el: Allow custom handlers for opening URLs Date: Mon, 04 May 2020 11:41:13 +0200 Message-ID: <87tv0w9gw6.fsf@gnu.org> References: <87h7wx4d24.fsf@gnu.org> <87d07l3utr.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="27147"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: Yuri Khan , emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon May 04 11:41:53 2020 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 1jVXbl-0006yq-03 for ged-emacs-devel@m.gmane-mx.org; Mon, 04 May 2020 11:41:53 +0200 Original-Received: from localhost ([::1]:46990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jVXbk-00072P-3h for ged-emacs-devel@m.gmane-mx.org; Mon, 04 May 2020 05:41:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54054) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jVXbB-0006dE-VQ for emacs-devel@gnu.org; Mon, 04 May 2020 05:41:17 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:53556) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jVXbB-0007AN-AT; Mon, 04 May 2020 05:41:17 -0400 Original-Received: from auth1-smtp.messagingengine.com ([66.111.4.227]:37797) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1jVXbB-00077j-0P; Mon, 04 May 2020 05:41:17 -0400 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id BC12427C0054; Mon, 4 May 2020 05:41:16 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Mon, 04 May 2020 05:41:16 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrjeeggddujecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufhffjgfkfgggtgesthdtredttdertdenucfhrhhomhepvfgrshhsihhl ohcujfhorhhnuceothhsughhsehgnhhurdhorhhgqeenucggtffrrghtthgvrhhnpeelhf duuedvvdduteetgedtffehuedutdejtefhjeetgeethffftdffgfduveekffenucffohhm rghinhepghhnuhdrohhrghenucfkphepkeegrddufedvrddutdekrddvfeehnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepthhhohhrnhdomhgv shhmthhprghuthhhphgvrhhsohhnrghlihhthidqkeeijeefkeejkeegqdeifeehvdelke dqthhsughhpeepghhnuhdrohhrghesfhgrshhtmhgrihhlrdhfmh X-ME-Proxy: Original-Received: from thinkpad-t440p (p54846ceb.dip0.t-ipconnect.de [84.132.108.235]) by mail.messagingengine.com (Postfix) with ESMTPA id 68FB53280060; Mon, 4 May 2020 05:41:15 -0400 (EDT) Mail-Followup-To: Stefan Monnier , emacs-devel@gnu.org, Yuri Khan In-Reply-To: (Stefan Monnier's message of "Sun, 03 May 2020 16:39:40 -0400") X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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:248800 Archived-At: Stefan Monnier writes: >> Yes, that's probably even better/more generic. But again, I wouldn't >> want to hard-code that, so I guess we'd want some >> `browse-url-handlers' in browse-url.el doing basically the same as >> the `bug-reference-url-handlers' I proposed, right? > > Yes. Hopefully that can be used for other purposes such as > redirecting some URLs to EWW? Sure, just add an entry like: ("http://\\(www\\.\\)?gnu\\.org\\b" . eww) >> Looking at it, there're already some special handlers for mailto: and >> man: page URLs. Should I convert those to default entries of the new >> `browse-url-handlers' alist? (IMHO, yes.) > > I think that would make sense, yes. Done so. >> And should the presence of `debbugs-gnu-bugs' create an entry in the >> alist, i.e., should having debbugs installed be enough to open bug >> links with debbugs or should we still be using the browser? > > You mean, should the debbugs package come with autoloads that add > themselves to this new variable? That's up to the debbugs package > maintainers, I'd say ;-) Right, that's their job. I've implemented it on the feature/browse-url-handlers branch (patch also attached below). Please, could you have a look? (I've tested man, mailto, the special gnu.org-eww handler, and web URLs matching no entry and it worked right.) And when hacking that up, I've seen that this feature is already implemented! The value of `browse-url-browser-function' may already be a (REGEXP . FUNCTION) alist itself and then it works the same way as my new `browse-url-handlers'. But I think that this is less flexible design as it doesn't allow other packages to easily hook in those mechanics (in the sense of Yuri's mobile application metaphor which fits well, I think). I would like to remove that entry from `browse-url--browser-defcustom-type' and issue a warning in `browse-url' pointing to the new `browse-url-handlers' when function is a dotted pair (while still supporting that usage for the next 20 years, of course). But removing the alist from `browse-url--browser-defcustom-type' would break the customize interface if the user's value is indeed an alist... So is there a better way to deprecate a single choice of a defcustom type? Just writing that in the docstring? Here's the patch: --8<---------------cut here---------------start------------->8--- >From 8046f46252b5e23a9fa157660efb5c6fc0ea82e2 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Mon, 4 May 2020 11:24:08 +0200 Subject: [PATCH] Allow for custom URL handlers in browse-url. * lisp/net/browse-url.el (browse-url-handlers): New defcustom. (browse-url): Use it. --- lisp/net/browse-url.el | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el index 7aad44b287..ab0be7b913 100644 --- a/lisp/net/browse-url.el +++ b/lisp/net/browse-url.el @@ -175,6 +175,23 @@ browse-url-browser-function :type browse-url--browser-defcustom-type :version "24.1") +;;;#autoload +(defcustom browse-url-handlers + `(("\\`mailto:" . ,browse-url-mailto-function) + ("\\`man:" . ,browse-url-man-function)) + "An alist with elements of the form (REGEXP HANDLER). +Each REGEXP is matched against each URL to be opened in turn and +the first match's HANDLER is invoked with the URL. + +A HANDLER must either be a function with the same arguments as +`browse-url' or a variable whos value is such a function. + +If no REGEXP matches, the bug reference URL is opened using the +value of `browse-url-browser-function'." + :type '(alist :key-type (regexp :tag "Regexp") + :value-type (function :tag "Handler")) + :version "28.1") + (defcustom browse-url-secondary-browser-function 'browse-url-default-browser "Function used to launch an alternative browser. This is usually an external browser (that is, not eww or w3m), @@ -786,12 +803,14 @@ browse-url (not (string-match "\\`[a-z]+:" url))) (setq url (expand-file-name url))) (let ((process-environment (copy-sequence process-environment)) - (function (or (and (string-match "\\`mailto:" url) - browse-url-mailto-function) - (and (string-match "\\`man:" url) - browse-url-man-function) - browse-url-browser-function)) - ;; Ensure that `default-directory' exists and is readable (b#6077). + (function + (catch 'custom-url-handler + (dolist (regex-handler browse-url-handlers) + (when (string-match-p (car regex-handler) url) + (throw 'custom-url-handler (cdr regex-handler)))) + ;; No special handler found. + browse-url-browser-function)) + ;; Ensure that `default-directory' exists and is readable (bug#6077). (default-directory (or (unhandled-file-name-directory default-directory) (expand-file-name "~/")))) ;; When connected to various displays, be careful to use the display of -- 2.26.2 --8<---------------cut here---------------end--------------->8--- Bye, Tassilo