aa23e97dab8133ec00ab54acb16d2107576a0208
[gnus] / lisp / eww.el
1 ;;; eww.el --- Emacs Web Wowser
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'shr)
29 (require 'url)
30 (require 'mm-url)
31
32 (defgroup eww nil
33   "Emacs Web Wowser"
34   :version "24.4"
35   :group 'hypermedia
36   :prefix "eww-")
37
38 (defcustom eww-header-line-format "%t: %u"
39   "Header line format.
40 - %t is replaced by the title.
41 - %u is replaced by the URL."
42   :group 'eww
43   :type 'string)
44
45 (defvar eww-current-url nil)
46 (defvar eww-current-title ""
47   "Title of current page.")
48 (defvar eww-history nil)
49
50 ;;;###autoload
51 (defun eww (url)
52   "Fetch URL and render the page."
53   (interactive "sUrl: ")
54   (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
55     (setq url (concat "http://" url)))
56   (url-retrieve url 'eww-render (list url)))
57
58 (defun eww-detect-charset (html-p)
59   (let ((case-fold-search t)
60         (pt (point)))
61     (or (and html-p
62              (re-search-forward
63               "<meta[\t\n\r ]+[^>]*charset=\\([^\t\n\r \"/>]+\\)" nil t)
64              (goto-char pt)
65              (match-string 1))
66         (and (looking-at
67               "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
68              (match-string 1)))))
69
70 (defun eww-render (status url &optional point)
71   (let ((redirect (plist-get status :redirect)))
72     (when redirect
73       (setq url redirect)))
74   (let* ((headers (eww-parse-headers))
75          (shr-target-id
76           (and (string-match "#\\(.*\\)" url)
77                (match-string 1 url)))
78          (content-type
79           (mail-header-parse-content-type
80            (or (cdr (assoc "content-type" headers))
81                "text/plain")))
82          (charset (intern
83                    (downcase
84                     (or (cdr (assq 'charset (cdr content-type)))
85                         (eww-detect-charset (equal (car content-type)
86                                                    "text/html"))
87                         "utf8"))))
88          (data-buffer (current-buffer)))
89     (unwind-protect
90         (progn
91           (cond
92            ((equal (car content-type) "text/html")
93             (eww-display-html charset url))
94            ((string-match "^image/" (car content-type))
95             (eww-display-image))
96            (t
97             (eww-display-raw charset)))
98           (cond
99            (point
100             (goto-char point))
101            (shr-target-id
102             (let ((point (next-single-property-change
103                           (point-min) 'shr-target-id)))
104               (when point
105                 (goto-char (1+ point)))))))
106       (kill-buffer data-buffer))))
107
108 (defun eww-parse-headers ()
109   (let ((headers nil))
110     (goto-char (point-min))
111     (while (and (not (eobp))
112                 (not (eolp)))
113       (when (looking-at "\\([^:]+\\): *\\(.*\\)")
114         (push (cons (downcase (match-string 1))
115                     (match-string 2))
116               headers))
117       (forward-line 1))
118     (unless (eobp)
119       (forward-line 1))
120     headers))
121
122 (defun eww-display-html (charset url)
123   (unless (eq charset 'utf8)
124     (decode-coding-region (point) (point-max) charset))
125   (let ((document
126          (list
127           'base (list (cons 'href url))
128           (libxml-parse-html-region (point) (point-max)))))
129     (eww-setup-buffer)
130     (setq eww-current-url url)
131     (eww-update-header-line-format)
132     (let ((inhibit-read-only t)
133           (shr-external-rendering-functions
134            '((title . eww-tag-title)
135              (form . eww-tag-form)
136              (input . eww-tag-input)
137              (textarea . eww-tag-textarea)
138              (body . eww-tag-body)
139              (select . eww-tag-select))))
140       (shr-insert-document document)
141       (eww-convert-widgets))
142     (goto-char (point-min))))
143
144 (defun eww-update-header-line-format ()
145   (if eww-header-line-format
146       (setq header-line-format (format-spec eww-header-line-format
147                                             `((?u . ,eww-current-url)
148                                               (?t . ,eww-current-title))))
149     (setq header-line-format nil)))
150
151 (defun eww-tag-title (cont)
152   (setq eww-current-title "")
153   (dolist (sub cont)
154     (when (eq (car sub) 'text)
155       (setq eww-current-title (concat eww-current-title (cdr sub)))))
156   (eww-update-header-line-format))
157
158 (defun eww-tag-body (cont)
159   (let* ((start (point))
160          (fgcolor (cdr (or (assq :fgcolor cont)
161                            (assq :text cont))))
162          (bgcolor (cdr (assq :bgcolor cont)))
163          (shr-stylesheet (list (cons 'color fgcolor)
164                                (cons 'background-color bgcolor))))
165     (shr-generic cont)
166     (eww-colorize-region start (point) fgcolor bgcolor)))
167
168 (defun eww-colorize-region (start end fg &optional bg)
169   (when (or fg bg)
170     (let ((new-colors (shr-color-check fg bg)))
171       (when new-colors
172         (when fg
173           (eww-put-color start end :foreground (cadr new-colors)))
174         (when bg
175           (eww-put-color start end :background (car new-colors)))))))
176
177 (defun eww-put-color (start end type color)
178   (shr-put-color-1 start end type color))
179
180 (defun eww-display-raw (charset)
181   (let ((data (buffer-substring (point) (point-max))))
182     (eww-setup-buffer)
183     (let ((inhibit-read-only t))
184       (insert data))
185     (goto-char (point-min))))
186
187 (defun eww-display-image ()
188   (let ((data (buffer-substring (point) (point-max))))
189     (eww-setup-buffer)
190     (let ((inhibit-read-only t))
191       (shr-put-image data nil))
192     (goto-char (point-min))))
193
194 (defun eww-setup-buffer ()
195   (pop-to-buffer (get-buffer-create "*eww*"))
196   (remove-overlays)
197   (setq widget-field-list nil)
198   (let ((inhibit-read-only t))
199     (erase-buffer))
200   (eww-mode))
201
202 (defvar eww-mode-map
203   (let ((map (make-sparse-keymap)))
204     (suppress-keymap map)
205     (define-key map "q" 'eww-quit)
206     (define-key map "g" 'eww-reload)
207     (define-key map [tab] 'widget-forward)
208     (define-key map [backtab] 'widget-backward)
209     (define-key map [delete] 'scroll-down-command)
210     (define-key map "\177" 'scroll-down-command)
211     (define-key map " " 'scroll-up-command)
212     (define-key map "p" 'eww-previous-url)
213     ;;(define-key map "n" 'eww-next-url)
214     map))
215
216 (define-derived-mode eww-mode nil "eww"
217   "Mode for browsing the web.
218
219 \\{eww-mode-map}"
220   (set (make-local-variable 'eww-current-url) 'author)
221   (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
222
223 (defun eww-browse-url (url &optional new-window)
224   (let ((url-request-extra-headers
225          (append '(("User-Agent" . "eww/1.0"))
226                  url-request-extra-headers)))
227     (push (list eww-current-url (point))
228           eww-history)
229     (eww url)))
230
231 (defun eww-quit ()
232   "Exit the Emacs Web Wowser."
233   (interactive)
234   (setq eww-history nil)
235   (kill-buffer (current-buffer)))
236
237 (defun eww-previous-url ()
238   "Go to the previously displayed page."
239   (interactive)
240   (when (zerop (length eww-history))
241     (error "No previous page"))
242   (let ((prev (pop eww-history)))
243     (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
244
245 (defun eww-reload ()
246   "Reload the current page."
247   (interactive)
248   (url-retrieve eww-current-url 'eww-render
249                 (list eww-current-url (point))))
250
251 ;; Form support.
252
253 (defvar eww-form nil)
254
255 (defun eww-tag-form (cont)
256   (let ((eww-form
257          (list (assq :method cont)
258                (assq :action cont)))
259         (start (point)))
260     (shr-ensure-paragraph)
261     (shr-generic cont)
262     (unless (bolp)
263       (insert "\n"))
264     (insert "\n")
265     (when (> (point) start)
266       (put-text-property start (1+ start)
267                          'eww-form eww-form))))
268
269 (defun eww-tag-input (cont)
270   (let* ((start (point))
271          (type (downcase (or (cdr (assq :type cont))
272                              "text")))
273          (widget
274           (cond
275            ((equal type "submit")
276             (list 'push-button
277                   :notify 'eww-submit
278                   :name (cdr (assq :name cont))
279                   :value (cdr (assq :value cont))
280                   :eww-form eww-form
281                   (or (cdr (assq :value cont)) "Submit")))
282            ((or (equal type "radio")
283                 (equal type "checkbox"))
284             (list 'checkbox
285                   :notify 'eww-click-radio
286                   :name (cdr (assq :name cont))
287                   :checkbox-value (cdr (assq :value cont))
288                   :checkbox-type type
289                   :eww-form eww-form
290                   (cdr (assq :checked cont))))
291            ((equal type "hidden")
292             (list 'hidden
293                   :name (cdr (assq :name cont))
294                   :value (cdr (assq :value cont))))
295            (t
296             (list 'editable-field
297                   :size (string-to-number
298                          (or (cdr (assq :size cont))
299                              "40"))
300                   :value (or (cdr (assq :value cont)) "")
301                   :secret (and (equal type "password") ?*)
302                   :action 'eww-submit
303                   :name (cdr (assq :name cont))
304                   :eww-form eww-form)))))
305     (nconc eww-form (list widget))
306     (unless (eq (car widget) 'hidden)
307       (apply 'widget-create widget)
308       (put-text-property start (point) 'eww-widget widget))))
309
310 (defun eww-tag-textarea (cont)
311   (let* ((start (point))
312          (widget
313           (list 'text
314                 :size (string-to-number
315                        (or (cdr (assq :cols cont))
316                            "40"))
317                 :value (or (cdr (assq 'text cont)) "")
318                 :action 'eww-submit
319                 :name (cdr (assq :name cont))
320                 :eww-form eww-form)))
321     (nconc eww-form (list widget))
322     (apply 'widget-create widget)
323     (put-text-property start (point) 'eww-widget widget)))
324
325 (defun eww-tag-select (cont)
326   (shr-ensure-paragraph)
327   (let ((menu (list 'menu-choice
328                     :name (cdr (assq :name cont))
329                     :eww-form eww-form))
330         (options nil)
331         (start (point)))
332     (dolist (elem cont)
333       (when (eq (car elem) 'option)
334         (when (cdr (assq :selected (cdr elem)))
335           (nconc menu (list :value
336                             (cdr (assq :value (cdr elem))))))
337         (push (list 'item
338                     :value (cdr (assq :value (cdr elem)))
339                     :tag (cdr (assq 'text (cdr elem))))
340               options)))
341     ;; If we have no selected values, default to the first value.
342     (unless (plist-get (cdr menu) :value)
343       (nconc menu (list :value (nth 2 (car options)))))
344     (nconc menu options)
345     (apply 'widget-create menu)
346     (put-text-property start (point) 'eww-widget menu)
347     (shr-ensure-paragraph)))
348
349 (defun eww-click-radio (widget &rest ignore)
350   (let ((form (plist-get (cdr widget) :eww-form))
351         (name (plist-get (cdr widget) :name)))
352     (when (equal (plist-get (cdr widget) :type) "radio")
353       (if (widget-value widget)
354           ;; Switch all the other radio buttons off.
355           (dolist (overlay (overlays-in (point-min) (point-max)))
356             (let ((field (plist-get (overlay-properties overlay) 'button)))
357               (when (and (eq (plist-get (cdr field) :eww-form) form)
358                          (equal name (plist-get (cdr field) :name)))
359                 (unless (eq field widget)
360                   (widget-value-set field nil)))))
361         (widget-value-set widget t)))
362     (eww-fix-widget-keymap)))
363
364 (defun eww-submit (widget &rest ignore)
365   (let ((form (plist-get (cdr widget) :eww-form))
366         values)
367     (dolist (overlay (sort (overlays-in (point-min) (point-max))
368                            (lambda (o1 o2)
369                              (< (overlay-start o1) (overlay-start o2)))))
370       (let ((field (or (plist-get (overlay-properties overlay) 'field)
371                        (plist-get (overlay-properties overlay) 'button))))
372         (when (eq (plist-get (cdr field) :eww-form) form)
373           (let ((name (plist-get (cdr field) :name)))
374             (when name
375               (cond
376                ((eq (car field) 'checkbox)
377                 (when (widget-value field)
378                   (push (cons name (plist-get (cdr field) :checkbox-value))
379                         values)))
380                ((eq (car field) 'push-button)
381                 ;; We want the values from buttons if we hit a button,
382                 ;; if it's the first button in the DOM after the field
383                 ;; hit ENTER on.
384                 (when (and (eq (car widget) 'push-button)
385                            (eq widget field))
386                   (push (cons name (widget-value field))
387                         values)))
388                (t
389                 (push (cons name (widget-value field))
390                       values))))))))
391     (dolist (elem form)
392       (when (and (consp elem)
393                  (eq (car elem) 'hidden))
394         (push (cons (plist-get (cdr elem) :name)
395                     (plist-get (cdr elem) :value))
396               values)))
397     ;; If we hit ENTER in a non-button field, include the value of the
398     ;; first submit button after it.
399     (unless (eq (car widget) 'push-button)
400       (let ((rest form)
401             (name (plist-get (cdr widget) :name)))
402         (when rest
403           (while (and rest
404                       (or (not (consp (car rest)))
405                           (not (equal name (plist-get (cdar rest) :name)))))
406             (pop rest)))
407         (while rest
408           (let ((elem (pop rest)))
409             (when (and (consp (car rest))
410                        (eq (car elem) 'push-button))
411               (push (cons (plist-get (cdr elem) :name)
412                           (plist-get (cdr elem) :value))
413                     values)
414               (setq rest nil))))))
415     (if (and (stringp (cdr (assq :method form)))
416              (equal (downcase (cdr (assq :method form))) "post"))
417         (let ((url-request-method "POST")
418               (url-request-extra-headers
419                '(("Content-Type" . "application/x-www-form-urlencoded")))
420               (url-request-data (mm-url-encode-www-form-urlencoded values)))
421           (eww-browse-url (shr-expand-url (cdr (assq :action form))
422                                           eww-current-url)))
423       (eww-browse-url
424        (concat
425         (if (cdr (assq :action form))
426             (shr-expand-url (cdr (assq :action form))
427                             eww-current-url)
428           eww-current-url)
429         "?"
430         (mm-url-encode-www-form-urlencoded values))))))
431
432 (defun eww-convert-widgets ()
433   (let ((start (point-min))
434         widget)
435     ;; Some widgets come from different buffers (rendered for tables),
436     ;; so we need to nix out the list of widgets and recreate them.
437     (setq widget-field-list nil
438           widget-field-new nil)
439     (while (setq start (next-single-property-change start 'eww-widget))
440       (setq widget (get-text-property start 'eww-widget))
441       (goto-char start)
442       (let ((end (next-single-property-change start 'eww-widget)))
443         (dolist (overlay (overlays-in start end))
444           (when (or (plist-get (overlay-properties overlay) 'button)
445                     (plist-get (overlay-properties overlay) 'field))
446             (delete-overlay overlay)))
447         (delete-region start end))
448       (when (and widget
449                  (not (eq (car widget) 'hidden)))
450         (apply 'widget-create widget)))
451     (widget-setup)
452     (eww-fix-widget-keymap)))
453
454 (defun eww-fix-widget-keymap ()
455   (dolist (overlay (overlays-in (point-min) (point-max)))
456     (when (plist-get (overlay-properties overlay) 'button)
457       (overlay-put overlay 'local-map widget-keymap))))
458
459 (provide 'eww)
460
461 ;;; eww.el ends here