1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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/>.
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
39 (defcustom eww-header-line-format "%t: %u"
41 - %t is replaced by the title.
42 - %u is replaced by the URL."
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."
54 (defvar eww-current-url nil)
55 (defvar eww-current-title ""
56 "Title of current page.")
57 (defvar eww-history nil)
59 (defvar eww-next-url nil)
60 (defvar eww-previous-url nil)
61 (defvar eww-up-url nil)
62 (defvar eww-top-url nil)
66 "Fetch URL and render the page."
67 (interactive "sUrl: ")
68 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
69 (setq url (concat "http://" url)))
70 (url-retrieve url 'eww-render (list url)))
73 (defun eww-open-file (file)
74 "Render a file using EWW."
75 (interactive "fFile: ")
76 (eww (concat "file://" (expand-file-name file))))
78 (defun eww-render (status url &optional point)
79 (let ((redirect (plist-get status :redirect)))
82 (set (make-local-variable 'eww-next-url) nil)
83 (set (make-local-variable 'eww-previous-url) nil)
84 (set (make-local-variable 'eww-up-url) nil)
85 (set (make-local-variable 'eww-top-url) nil)
86 (let* ((headers (eww-parse-headers))
88 (and (string-match "#\\(.*\\)" url)
89 (match-string 1 url)))
91 (mail-header-parse-content-type
92 (or (cdr (assoc "content-type" headers))
96 (or (cdr (assq 'charset (cdr content-type)))
97 (eww-detect-charset (equal (car content-type)
100 (data-buffer (current-buffer)))
104 ((equal (car content-type) "text/html")
105 (eww-display-html charset url))
106 ((string-match "^image/" (car content-type))
109 (eww-display-raw charset)))
114 (let ((point (next-single-property-change
115 (point-min) 'shr-target-id)))
117 (goto-char (1+ point)))))))
118 (kill-buffer data-buffer))))
120 (defun eww-parse-headers ()
122 (goto-char (point-min))
123 (while (and (not (eobp))
125 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
126 (push (cons (downcase (match-string 1))
134 (defun eww-detect-charset (html-p)
135 (let ((case-fold-search t)
139 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t)
143 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
146 (defun eww-display-html (charset url)
147 (unless (eq charset 'utf8)
148 (decode-coding-region (point) (point-max) charset))
151 'base (list (cons 'href url))
152 (libxml-parse-html-region (point) (point-max)))))
154 (setq eww-current-url url)
155 (eww-update-header-line-format)
156 (let ((inhibit-read-only t)
158 (shr-external-rendering-functions
159 '((title . eww-tag-title)
160 (form . eww-tag-form)
161 (input . eww-tag-input)
162 (textarea . eww-tag-textarea)
163 (body . eww-tag-body)
164 (select . eww-tag-select)
165 (link . eww-tag-link)
167 (shr-insert-document document)
168 (eww-convert-widgets))
169 (goto-char (point-min))))
171 (defun eww-handle-link (cont)
172 (let* ((rel (assq :rel cont))
173 (href (assq :href cont))
174 (where (assoc (cdr rel)
175 '(("next" . eww-next-url)
176 ("previous" . eww-previous-url)
177 ("start" . eww-top-url)
178 ("up" . eww-up-url)))))
181 (set (cdr where) (cdr href)))))
183 (defun eww-tag-link (cont)
184 (eww-handle-link cont)
187 (defun eww-tag-a (cont)
188 (eww-handle-link cont)
191 (defun eww-update-header-line-format ()
192 (if eww-header-line-format
193 (setq header-line-format (format-spec eww-header-line-format
194 `((?u . ,eww-current-url)
195 (?t . ,eww-current-title))))
196 (setq header-line-format nil)))
198 (defun eww-tag-title (cont)
199 (setq eww-current-title "")
201 (when (eq (car sub) 'text)
202 (setq eww-current-title (concat eww-current-title (cdr sub)))))
203 (eww-update-header-line-format))
205 (defun eww-tag-body (cont)
206 (let* ((start (point))
207 (fgcolor (cdr (or (assq :fgcolor cont)
209 (bgcolor (cdr (assq :bgcolor cont)))
210 (shr-stylesheet (list (cons 'color fgcolor)
211 (cons 'background-color bgcolor))))
213 (eww-colorize-region start (point) fgcolor bgcolor)))
215 (defun eww-colorize-region (start end fg &optional bg)
217 (let ((new-colors (shr-color-check fg bg)))
220 (add-face-text-property start end
221 (list :foreground (cadr new-colors))))
223 (add-face-text-property start end
224 (list :background (car new-colors))))))))
226 (defun eww-display-raw (charset)
227 (let ((data (buffer-substring (point) (point-max))))
229 (let ((inhibit-read-only t))
231 (goto-char (point-min))))
233 (defun eww-display-image ()
234 (let ((data (buffer-substring (point) (point-max))))
236 (let ((inhibit-read-only t))
237 (shr-put-image data nil))
238 (goto-char (point-min))))
240 (defun eww-setup-buffer ()
241 (pop-to-buffer (get-buffer-create "*eww*"))
243 (setq widget-field-list nil)
244 (let ((inhibit-read-only t))
249 (let ((map (make-sparse-keymap)))
250 (suppress-keymap map)
251 (define-key map "q" 'eww-quit)
252 (define-key map "g" 'eww-reload)
253 (define-key map [tab] 'shr-next-link)
254 (define-key map [backtab] 'shr-previous-link)
255 (define-key map [delete] 'scroll-down-command)
256 (define-key map "\177" 'scroll-down-command)
257 (define-key map " " 'scroll-up-command)
258 (define-key map "l" 'eww-back-url)
259 (define-key map "n" 'eww-next-url)
260 (define-key map "p" 'eww-previous-url)
261 (define-key map "u" 'eww-up-url)
262 (define-key map "t" 'eww-top-url)
265 (define-derived-mode eww-mode nil "eww"
266 "Mode for browsing the web.
269 (set (make-local-variable 'eww-current-url) 'author)
270 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
272 (defun eww-browse-url (url &optional new-window)
273 (push (list eww-current-url (point))
278 "Exit the Emacs Web Wowser."
280 (setq eww-history nil)
281 (kill-buffer (current-buffer)))
283 (defun eww-back-url ()
284 "Go to the previously displayed page."
286 (when (zerop (length eww-history))
287 (error "No previous page"))
288 (let ((prev (pop eww-history)))
289 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
291 (defun eww-next-url ()
292 "Go to the page marked `next'.
293 A page is marked `next' if rel=\"next\" appears in a <link>
297 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
298 (error "No `next' on this page")))
300 (defun eww-previous-url ()
301 "Go to the page marked `previous'.
302 A page is marked `previous' if rel=\"previous\" appears in a <link>
306 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
307 (error "No `previous' on this page")))
310 "Go to the page marked `up'.
311 A page is marked `up' if rel=\"up\" appears in a <link>
315 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
316 (error "No `up' on this page")))
318 (defun eww-top-url ()
319 "Go to the page marked `top'.
320 A page is marked `top' if rel=\"start\" appears in a <link>
324 (eww-browse-url (shr-expand-url eww-top-url eww-current-url))
325 (error "No `top' on this page")))
328 "Reload the current page."
330 (url-retrieve eww-current-url 'eww-render
331 (list eww-current-url (point))))
335 (defvar eww-form nil)
337 (defun eww-tag-form (cont)
339 (list (assq :method cont)
340 (assq :action cont)))
342 (shr-ensure-paragraph)
347 (when (> (point) start)
348 (put-text-property start (1+ start)
349 'eww-form eww-form))))
351 (defun eww-tag-input (cont)
352 (let* ((start (point))
353 (type (downcase (or (cdr (assq :type cont))
355 (value (cdr (assq :value cont)))
358 ((or (equal type "submit")
359 (equal type "image"))
362 :name (cdr (assq :name cont))
363 :value (if (zerop (length value))
367 (or (if (zerop (length value))
370 ((or (equal type "radio")
371 (equal type "checkbox"))
373 :notify 'eww-click-radio
374 :name (cdr (assq :name cont))
375 :checkbox-value value
378 (cdr (assq :checked cont))))
379 ((equal type "hidden")
381 :name (cdr (assq :name cont))
384 (list 'editable-field
385 :size (string-to-number
386 (or (cdr (assq :size cont))
389 :secret (and (equal type "password") ?*)
391 :name (cdr (assq :name cont))
392 :eww-form eww-form)))))
393 (nconc eww-form (list widget))
394 (unless (eq (car widget) 'hidden)
395 (apply 'widget-create widget)
396 (put-text-property start (point) 'eww-widget widget)
399 (defun eww-tag-textarea (cont)
400 (let* ((start (point))
403 :size (string-to-number
404 (or (cdr (assq :cols cont))
406 :value (or (cdr (assq 'text cont)) "")
408 :name (cdr (assq :name cont))
409 :eww-form eww-form)))
410 (nconc eww-form (list widget))
411 (apply 'widget-create widget)
412 (put-text-property start (point) 'eww-widget widget)))
414 (defun eww-tag-select (cont)
415 (shr-ensure-paragraph)
416 (let ((menu (list 'menu-choice
417 :name (cdr (assq :name cont))
422 (when (eq (car elem) 'option)
423 (when (cdr (assq :selected (cdr elem)))
424 (nconc menu (list :value
425 (cdr (assq :value (cdr elem))))))
427 :value (cdr (assq :value (cdr elem)))
428 :tag (cdr (assq 'text (cdr elem))))
431 ;; If we have no selected values, default to the first value.
432 (unless (plist-get (cdr menu) :value)
433 (nconc menu (list :value (nth 2 (car options)))))
435 (apply 'widget-create menu)
436 (put-text-property start (point) 'eww-widget menu)
437 (shr-ensure-paragraph))))
439 (defun eww-click-radio (widget &rest ignore)
440 (let ((form (plist-get (cdr widget) :eww-form))
441 (name (plist-get (cdr widget) :name)))
442 (when (equal (plist-get (cdr widget) :type) "radio")
443 (if (widget-value widget)
444 ;; Switch all the other radio buttons off.
445 (dolist (overlay (overlays-in (point-min) (point-max)))
446 (let ((field (plist-get (overlay-properties overlay) 'button)))
447 (when (and (eq (plist-get (cdr field) :eww-form) form)
448 (equal name (plist-get (cdr field) :name)))
449 (unless (eq field widget)
450 (widget-value-set field nil)))))
451 (widget-value-set widget t)))
452 (eww-fix-widget-keymap)))
454 (defun eww-submit (widget &rest ignore)
455 (let ((form (plist-get (cdr widget) :eww-form))
457 (dolist (overlay (sort (overlays-in (point-min) (point-max))
459 (< (overlay-start o1) (overlay-start o2)))))
460 (let ((field (or (plist-get (overlay-properties overlay) 'field)
461 (plist-get (overlay-properties overlay) 'button))))
462 (when (eq (plist-get (cdr field) :eww-form) form)
463 (let ((name (plist-get (cdr field) :name)))
466 ((eq (car field) 'checkbox)
467 (when (widget-value field)
468 (push (cons name (plist-get (cdr field) :checkbox-value))
470 ((eq (car field) 'push-button)
471 ;; We want the values from buttons if we hit a button,
472 ;; if it's the first button in the DOM after the field
474 (when (and (eq (car widget) 'push-button)
476 (push (cons name (widget-value field))
479 (push (cons name (widget-value field))
482 (when (and (consp elem)
483 (eq (car elem) 'hidden))
484 (push (cons (plist-get (cdr elem) :name)
485 (plist-get (cdr elem) :value))
487 ;; If we hit ENTER in a non-button field, include the value of the
488 ;; first submit button after it.
489 (unless (eq (car widget) 'push-button)
491 (name (plist-get (cdr widget) :name)))
494 (or (not (consp (car rest)))
495 (not (equal name (plist-get (cdar rest) :name)))))
498 (let ((elem (pop rest)))
499 (when (and (consp (car rest))
500 (eq (car elem) 'push-button))
501 (push (cons (plist-get (cdr elem) :name)
502 (plist-get (cdr elem) :value))
505 (if (and (stringp (cdr (assq :method form)))
506 (equal (downcase (cdr (assq :method form))) "post"))
507 (let ((url-request-method "POST")
508 (url-request-extra-headers
509 '(("Content-Type" . "application/x-www-form-urlencoded")))
510 (url-request-data (mm-url-encode-www-form-urlencoded values)))
511 (eww-browse-url (shr-expand-url (cdr (assq :action form))
515 (if (cdr (assq :action form))
516 (shr-expand-url (cdr (assq :action form))
520 (mm-url-encode-www-form-urlencoded values))))))
522 (defun eww-convert-widgets ()
523 (let ((start (point-min))
525 ;; Some widgets come from different buffers (rendered for tables),
526 ;; so we need to nix out the list of widgets and recreate them.
527 (setq widget-field-list nil
528 widget-field-new nil)
529 (while (setq start (if (get-text-property start 'eww-widget)
531 (next-single-property-change start 'eww-widget)))
532 (setq widget (get-text-property start 'eww-widget))
534 (let ((end (next-single-property-change start 'eww-widget)))
535 (dolist (overlay (overlays-in start end))
536 (when (or (plist-get (overlay-properties overlay) 'button)
537 (plist-get (overlay-properties overlay) 'field))
538 (delete-overlay overlay)))
539 (delete-region start end))
541 (not (eq (car widget) 'hidden)))
542 (apply 'widget-create widget)
543 (put-text-property start (point) 'help-echo
544 (if (memq (car widget) '(text editable-field))
547 (when (eq (car widget) 'push-button)
548 (add-face-text-property start (point) 'eww-button t))))
550 (eww-fix-widget-keymap)))
552 (defun eww-fix-widget-keymap ()
553 (dolist (overlay (overlays-in (point-min) (point-max)))
554 (when (plist-get (overlay-properties overlay) 'button)
555 (overlay-put overlay 'local-map widget-keymap))))