From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ivan Shmakov Newsgroups: gmane.emacs.bugs Subject: bug#19556: eww: make URI rewriting fully customizable Date: Sat, 10 Jan 2015 13:17:55 +0000 Message-ID: <87sifi22rg.fsf@violet.siamics.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1420895957 15135 80.91.229.3 (10 Jan 2015 13:19:17 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 10 Jan 2015 13:19:17 +0000 (UTC) To: 19556@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Jan 10 14:19:12 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Y9vwd-00083e-VF for geb-bug-gnu-emacs@m.gmane.org; Sat, 10 Jan 2015 14:19:12 +0100 Original-Received: from localhost ([::1]:55074 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y9vwc-0005IE-Ou for geb-bug-gnu-emacs@m.gmane.org; Sat, 10 Jan 2015 08:19:10 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41332) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y9vwY-0005I7-7o for bug-gnu-emacs@gnu.org; Sat, 10 Jan 2015 08:19:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y9vwU-000465-Q9 for bug-gnu-emacs@gnu.org; Sat, 10 Jan 2015 08:19:06 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:60449) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y9vwU-00045i-Gn for bug-gnu-emacs@gnu.org; Sat, 10 Jan 2015 08:19:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1Y9vwU-00023K-3R for bug-gnu-emacs@gnu.org; Sat, 10 Jan 2015 08:19:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Ivan Shmakov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 10 Jan 2015 13:19:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 19556 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: submit@debbugs.gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.14208958927818 (code B ref -1); Sat, 10 Jan 2015 13:19:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 10 Jan 2015 13:18:12 +0000 Original-Received: from localhost ([127.0.0.1]:41582 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Y9vvf-000221-IL for submit@debbugs.gnu.org; Sat, 10 Jan 2015 08:18:12 -0500 Original-Received: from fely.am-1.org ([78.47.74.50]:48749) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Y9vvc-00021n-2B for submit@debbugs.gnu.org; Sat, 10 Jan 2015 08:18:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=siamics.net; s=a2013295; h=Content-Type:MIME-Version:Message-ID:Date:Sender:Subject:To:From; bh=UMid1qCLNy3yl9Lezuidg1s3QXi/kfDZvKycqmH3U0Y=; b=HVWlRLceQaDDzAGvqtCPEg8wupQkD5E/kaYwXWO6g2WDOWu/xId6mpGxidrmlc7R3btNSzsOTpSovJezWyGYut1GG6SfhVy+p4tyKzH1PdCGCqN8f8rPqnfQaesgIlK6ed+iJDVDMsSy+mBrRuGHB3ke+bntJB8ksE+7P9DJsqI=; Original-Received: from [2a02:2560:6d4:26ca::1:1d] (helo=violet.siamics.net) by fely.am-1.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1Y9vvX-000144-Mf for submit@debbugs.gnu.org; Sat, 10 Jan 2015 13:18:04 +0000 Original-Received: from localhost ([::1] helo=violet.siamics.net) by violet.siamics.net with esmtps (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1Y9vvQ-0002Ri-87 for submit@debbugs.gnu.org; Sat, 10 Jan 2015 20:17:56 +0700 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 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.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:98170 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Package: emacs Severity: minor As of 88bc8332eb14 (2015-01-07 13:51:41 +0000), the =E2=80=98eww=E2=80=99 command follows a hard-coded set of rules to rewrite the argument given into a URI suitable for url-retrieve. I hereby suggest to make these rules fully customizable, per the patch MIMEd. * lisp/net/eww.el (eww-uri-rewrite-hook): New customizable variable. (eww): Use eww-uri-rewrite-hook. (eww-search-words): Use eww-uri-search. (eww-uri-file, eww-uri-not-supported, eww-uri-search) (eww-uri-remote-prepend-http): New functions, split off eww. FWIW, removing eww-search-words from the hook allows one to type something like https://en.wikipedia.org/wiki/Free software RET (note the blank) at the prompt and be with that. On top of this, eww-uri-file may then be changed slightly to deal with #19472. --=20 FSF associate member #7257 np. Tree of Love =E2=80=94 Jami Sieber =E2=80= =A6 B6A0 230E 334A --=-=-= Content-Type: text/diff Content-Disposition: inline --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -75,6 +75,26 @@ defcustom eww-suggest-uris url-get-url-at-point eww-current-url)) +(defcustom eww-uri-rewrite-hook + '(eww-uri-file + eww-uri-not-supported + eww-uri-remote-prepend-http + eww-uri-search) + "List of functions called to deal with the argument to `eww'. +These functions will be called in order, with the argument given to +`eww' passed as their only argument, until one returns non-nil. The +value returned will be used as the URI to fetch. + +Should no function of those listed return non-nil, `eww' will use its +argument unaltered." + :version "25.1" + :group 'eww + :type 'hook + :options '(eww-uri-file + eww-uri-not-supported + eww-uri-remote-prepend-http + eww-uri-search)) + (defcustom eww-bookmarks-directory user-emacs-directory "Directory where bookmark files will be stored." :version "25.1" @@ -250,23 +270,9 @@ defun eww (url) ": "))) (list (read-string prompt nil nil uris)))) (setq url (string-trim url)) - (cond ((string-match-p "\\`file:/" url)) - ;; Don't mangle file: URLs at all. - ((string-match-p "\\`ftp://" url) - (user-error "FTP is not supported.")) - (t - (if (and (= (length (split-string url)) 1) - (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url)) - (> (length (split-string url "[.:]")) 1)) - (string-match eww-local-regex url))) - (progn - (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url) - (setq url (concat "http://" url))) - ;; some site don't redirect final / - (when (string= (url-filename (url-generic-parse-url url)) "") - (setq url (concat url "/")))) - (setq url (concat eww-search-prefix - (replace-regexp-in-string " " "+" url)))))) + (when-let ((new (run-hook-with-args-until-success + 'eww-uri-rewrite-hook url))) + (setq url new)) (if (eq major-mode 'eww-mode) (when (or (plist-get eww-data :url) (plist-get eww-data :dom)) @@ -292,11 +299,11 @@ defun eww-open-file (file) (expand-file-name file)))) ;;;###autoload -(defun eww-search-words (&optional beg end) +(defun eww-search-words (beg end) "Search the web for the text between the point and marker. See the `eww-search-prefix' variable for the search engine used." (interactive "r") - (eww (buffer-substring beg end))) + (eww (eww-uri-search (buffer-substring beg end)))) (defun eww-render (status url &optional point buffer encode) (let ((redirect (plist-get status :redirect))) @@ -551,6 +559,46 @@ defun eww-links-at-point (&optional pt) (list (get-text-property (point) 'shr-url) (get-text-property (point) 'image-url)))) +(defun eww-uri-file (uri) + "Return URI unaltered if it looks like a valid file: scheme URI." + (and (string-match-p "\\`file:/" uri) + uri)) + +(defun eww-uri-not-supported (uri) + "Raise an error if URI uses the ftp: scheme." + (when (string-match-p "\\`ftp://" url) + (user-error "FTP is not supported."))) + +(defun eww-uri-remote-prepend-http (uri) + "Return URI if it looks like a reference to a remote resource. +Specifically, return URI if it contains no whitespace characters + (except for leading and trailing whitespace, which is ignored) and +either matches `eww-local-regex', or contains a . or :, but no single- +or double-quoted parts. + +Prepend http:// to URI unless it already has a scheme." + (when (and (= (length (split-string uri)) 1) + (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" uri)) + (> (length (split-string uri "[.:]")) 1)) + (string-match eww-local-regex uri))) + (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" uri) + (setq uri (concat "http://" uri))) + (setq uri (replace-regexp-in-string " " "%20" uri)) + ;; Some sites don't redirect final /. + (if (string= (url-filename (url-generic-parse-url uri)) "") + (concat uri "/") + uri))) + +(defun eww-uri-search (keywords) + "Return an URI for the search given by KEYWORDS. +KEYWORDS may be either a list or a string. + +See the `eww-search-prefix' variable for the search engine used." + (concat eww-search-prefix + (if (listp keywords) + (mapconcat #'identity keywords "+") + (subst-char-in-string 32 ?+ keywords)))) + (defun eww-view-source () "View the HTML source code of the current page." (interactive) --=-=-=--