From: Ivan Shmakov <ivan@siamics.net>
To: 19556@debbugs.gnu.org
Subject: bug#19556: eww: make URI rewriting fully customizable
Date: Sat, 10 Jan 2015 13:17:55 +0000 [thread overview]
Message-ID: <87sifi22rg.fsf@violet.siamics.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 929 bytes --]
Package: emacs
Severity: minor
As of 88bc8332eb14 (2015-01-07 13:51:41 +0000), the ‘eww’
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.
--
FSF associate member #7257 np. Tree of Love — Jami Sieber … B6A0 230E 334A
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/diff, Size: 4491 bytes --]
--- 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)
next reply other threads:[~2015-01-10 13:17 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-10 13:17 Ivan Shmakov [this message]
2015-01-10 14:19 ` bug#19556: eww: make URI rewriting fully customizable Eli Zaretskii
2015-01-10 14:40 ` Ivan Shmakov
2015-01-10 16:01 ` Eli Zaretskii
2015-01-10 17:20 ` Ivan Shmakov
2015-01-10 17:37 ` Eli Zaretskii
2015-01-10 18:05 ` Ivan Shmakov
2015-01-10 14:29 ` Lars Magne Ingebrigtsen
2015-01-10 14:44 ` Ivan Shmakov
2015-01-10 14:58 ` Lars Magne Ingebrigtsen
2015-01-10 15:59 ` Ivan Shmakov
2015-01-10 16:08 ` Lars Magne Ingebrigtsen
2015-01-10 16:22 ` Lars Magne Ingebrigtsen
2015-01-10 18:24 ` Ivan Shmakov
2015-12-25 7:03 ` Lars Ingebrigtsen
2015-12-26 9:30 ` Ivan Shmakov
2015-01-10 15:02 ` Lars Magne Ingebrigtsen
2015-01-10 18:11 ` Ivan Shmakov
2015-01-11 14:58 ` Lars Magne Ingebrigtsen
2015-01-11 15:06 ` Lars Magne Ingebrigtsen
2015-01-11 18:22 ` Ivan Shmakov
2015-01-11 20:03 ` Eli Zaretskii
2015-01-11 20:17 ` Ivan Shmakov
2015-01-11 20:40 ` Eli Zaretskii
2015-01-11 21:46 ` Lars Magne Ingebrigtsen
2015-01-11 1:59 ` Stefan Monnier
2015-01-11 6:25 ` Ivan Shmakov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sifi22rg.fsf@violet.siamics.net \
--to=ivan@siamics.net \
--cc=19556@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.