Move some files from gnus/ to net/
[bpt/emacs.git] / lisp / net / 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 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'mm-url)
32
33 (defgroup eww nil
34 "Emacs Web Wowser"
35 :version "24.4"
36 :group 'hypermedia
37 :prefix "eww-")
38
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)
45
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)
53
54 (defvar eww-current-url nil)
55 (defvar eww-current-title ""
56 "Title of current page.")
57 (defvar eww-history nil)
58
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)))
66
67 (defun eww-render (status url &optional point)
68 (let ((redirect (plist-get status :redirect)))
69 (when redirect
70 (setq url redirect)))
71 (let* ((headers (eww-parse-headers))
72 (shr-target-id
73 (and (string-match "#\\(.*\\)" url)
74 (match-string 1 url)))
75 (content-type
76 (mail-header-parse-content-type
77 (or (cdr (assoc "content-type" headers))
78 "text/plain")))
79 (charset (intern
80 (downcase
81 (or (cdr (assq 'charset (cdr content-type)))
82 (eww-detect-charset (equal (car content-type)
83 "text/html"))
84 "utf8"))))
85 (data-buffer (current-buffer)))
86 (unwind-protect
87 (progn
88 (cond
89 ((equal (car content-type) "text/html")
90 (eww-display-html charset url))
91 ((string-match "^image/" (car content-type))
92 (eww-display-image))
93 (t
94 (eww-display-raw charset)))
95 (cond
96 (point
97 (goto-char point))
98 (shr-target-id
99 (let ((point (next-single-property-change
100 (point-min) 'shr-target-id)))
101 (when point
102 (goto-char (1+ point)))))))
103 (kill-buffer data-buffer))))
104
105 (defun eww-parse-headers ()
106 (let ((headers nil))
107 (goto-char (point-min))
108 (while (and (not (eobp))
109 (not (eolp)))
110 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
111 (push (cons (downcase (match-string 1))
112 (match-string 2))
113 headers))
114 (forward-line 1))
115 (unless (eobp)
116 (forward-line 1))
117 headers))
118
119 (defun eww-detect-charset (html-p)
120 (let ((case-fold-search t)
121 (pt (point)))
122 (or (and html-p
123 (re-search-forward
124 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t)
125 (goto-char pt)
126 (match-string 1))
127 (and (looking-at
128 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
129 (match-string 1)))))
130
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))))
153
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)))
160
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))
167
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)))
177
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))))))))
188
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))))
195
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))))
202
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))
210
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))
224
225 (define-derived-mode eww-mode nil "eww"
226 "Mode for browsing the web.
227
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))
231
232 (defun eww-browse-url (url &optional new-window)
233 (push (list eww-current-url (point))
234 eww-history)
235 (eww url))
236
237 (defun eww-quit ()
238 "Exit the Emacs Web Wowser."
239 (interactive)
240 (setq eww-history nil)
241 (kill-buffer (current-buffer)))
242
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)))))
250
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))))
256
257 ;; Form support.
258
259 (defvar eww-form nil)
260
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))))
274
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 ((or (equal type "submit")
283 (equal type "image"))
284 (list 'push-button
285 :notify 'eww-submit
286 :name (cdr (assq :name cont))
287 :value (if (zerop (length value))
288 "Submit"
289 value)
290 :eww-form eww-form
291 (or (if (zerop (length value))
292 "Submit"
293 value))))
294 ((or (equal type "radio")
295 (equal type "checkbox"))
296 (list 'checkbox
297 :notify 'eww-click-radio
298 :name (cdr (assq :name cont))
299 :checkbox-value value
300 :checkbox-type type
301 :eww-form eww-form
302 (cdr (assq :checked cont))))
303 ((equal type "hidden")
304 (list 'hidden
305 :name (cdr (assq :name cont))
306 :value value))
307 (t
308 (list 'editable-field
309 :size (string-to-number
310 (or (cdr (assq :size cont))
311 "40"))
312 :value (or value "")
313 :secret (and (equal type "password") ?*)
314 :action 'eww-submit
315 :name (cdr (assq :name cont))
316 :eww-form eww-form)))))
317 (nconc eww-form (list widget))
318 (unless (eq (car widget) 'hidden)
319 (apply 'widget-create widget)
320 (put-text-property start (point) 'eww-widget widget)
321 (insert " "))))
322
323 (defun eww-tag-textarea (cont)
324 (let* ((start (point))
325 (widget
326 (list 'text
327 :size (string-to-number
328 (or (cdr (assq :cols cont))
329 "40"))
330 :value (or (cdr (assq 'text cont)) "")
331 :action 'eww-submit
332 :name (cdr (assq :name cont))
333 :eww-form eww-form)))
334 (nconc eww-form (list widget))
335 (apply 'widget-create widget)
336 (put-text-property start (point) 'eww-widget widget)))
337
338 (defun eww-tag-select (cont)
339 (shr-ensure-paragraph)
340 (let ((menu (list 'menu-choice
341 :name (cdr (assq :name cont))
342 :eww-form eww-form))
343 (options nil)
344 (start (point)))
345 (dolist (elem cont)
346 (when (eq (car elem) 'option)
347 (when (cdr (assq :selected (cdr elem)))
348 (nconc menu (list :value
349 (cdr (assq :value (cdr elem))))))
350 (push (list 'item
351 :value (cdr (assq :value (cdr elem)))
352 :tag (cdr (assq 'text (cdr elem))))
353 options)))
354 (when options
355 ;; If we have no selected values, default to the first value.
356 (unless (plist-get (cdr menu) :value)
357 (nconc menu (list :value (nth 2 (car options)))))
358 (nconc menu options)
359 (apply 'widget-create menu)
360 (put-text-property start (point) 'eww-widget menu)
361 (shr-ensure-paragraph))))
362
363 (defun eww-click-radio (widget &rest ignore)
364 (let ((form (plist-get (cdr widget) :eww-form))
365 (name (plist-get (cdr widget) :name)))
366 (when (equal (plist-get (cdr widget) :type) "radio")
367 (if (widget-value widget)
368 ;; Switch all the other radio buttons off.
369 (dolist (overlay (overlays-in (point-min) (point-max)))
370 (let ((field (plist-get (overlay-properties overlay) 'button)))
371 (when (and (eq (plist-get (cdr field) :eww-form) form)
372 (equal name (plist-get (cdr field) :name)))
373 (unless (eq field widget)
374 (widget-value-set field nil)))))
375 (widget-value-set widget t)))
376 (eww-fix-widget-keymap)))
377
378 (defun eww-submit (widget &rest ignore)
379 (let ((form (plist-get (cdr widget) :eww-form))
380 values)
381 (dolist (overlay (sort (overlays-in (point-min) (point-max))
382 (lambda (o1 o2)
383 (< (overlay-start o1) (overlay-start o2)))))
384 (let ((field (or (plist-get (overlay-properties overlay) 'field)
385 (plist-get (overlay-properties overlay) 'button))))
386 (when (eq (plist-get (cdr field) :eww-form) form)
387 (let ((name (plist-get (cdr field) :name)))
388 (when name
389 (cond
390 ((eq (car field) 'checkbox)
391 (when (widget-value field)
392 (push (cons name (plist-get (cdr field) :checkbox-value))
393 values)))
394 ((eq (car field) 'push-button)
395 ;; We want the values from buttons if we hit a button,
396 ;; if it's the first button in the DOM after the field
397 ;; hit ENTER on.
398 (when (and (eq (car widget) 'push-button)
399 (eq widget field))
400 (push (cons name (widget-value field))
401 values)))
402 (t
403 (push (cons name (widget-value field))
404 values))))))))
405 (dolist (elem form)
406 (when (and (consp elem)
407 (eq (car elem) 'hidden))
408 (push (cons (plist-get (cdr elem) :name)
409 (plist-get (cdr elem) :value))
410 values)))
411 ;; If we hit ENTER in a non-button field, include the value of the
412 ;; first submit button after it.
413 (unless (eq (car widget) 'push-button)
414 (let ((rest form)
415 (name (plist-get (cdr widget) :name)))
416 (when rest
417 (while (and rest
418 (or (not (consp (car rest)))
419 (not (equal name (plist-get (cdar rest) :name)))))
420 (pop rest)))
421 (while rest
422 (let ((elem (pop rest)))
423 (when (and (consp (car rest))
424 (eq (car elem) 'push-button))
425 (push (cons (plist-get (cdr elem) :name)
426 (plist-get (cdr elem) :value))
427 values)
428 (setq rest nil))))))
429 (if (and (stringp (cdr (assq :method form)))
430 (equal (downcase (cdr (assq :method form))) "post"))
431 (let ((url-request-method "POST")
432 (url-request-extra-headers
433 '(("Content-Type" . "application/x-www-form-urlencoded")))
434 (url-request-data (mm-url-encode-www-form-urlencoded values)))
435 (eww-browse-url (shr-expand-url (cdr (assq :action form))
436 eww-current-url)))
437 (eww-browse-url
438 (concat
439 (if (cdr (assq :action form))
440 (shr-expand-url (cdr (assq :action form))
441 eww-current-url)
442 eww-current-url)
443 "?"
444 (mm-url-encode-www-form-urlencoded values))))))
445
446 (defun eww-convert-widgets ()
447 (let ((start (point-min))
448 widget)
449 ;; Some widgets come from different buffers (rendered for tables),
450 ;; so we need to nix out the list of widgets and recreate them.
451 (setq widget-field-list nil
452 widget-field-new nil)
453 (while (setq start (if (get-text-property start 'eww-widget)
454 start
455 (next-single-property-change start 'eww-widget)))
456 (setq widget (get-text-property start 'eww-widget))
457 (goto-char start)
458 (let ((end (next-single-property-change start 'eww-widget)))
459 (dolist (overlay (overlays-in start end))
460 (when (or (plist-get (overlay-properties overlay) 'button)
461 (plist-get (overlay-properties overlay) 'field))
462 (delete-overlay overlay)))
463 (delete-region start end))
464 (when (and widget
465 (not (eq (car widget) 'hidden)))
466 (apply 'widget-create widget)
467 (put-text-property start (point) 'help-echo
468 (if (memq (car widget) '(text editable-field))
469 "Input field"
470 "Button"))
471 (when (eq (car widget) 'push-button)
472 (add-face-text-property start (point) 'eww-button t))))
473 (widget-setup)
474 (eww-fix-widget-keymap)))
475
476 (defun eww-fix-widget-keymap ()
477 (dolist (overlay (overlays-in (point-min) (point-max)))
478 (when (plist-get (overlay-properties overlay) 'button)
479 (overlay-put overlay 'local-map widget-keymap))))
480
481 (provide 'eww)
482
483 ;;; eww.el ends here