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