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."
46 (defface eww-form-submit
47 '((((type x w32 ns) (class color)) ; Like default mode line
48 :box (:line-width 2 :style released-button)
49 :background "#808080" :foreground "black"))
50 "Face for eww buffer buttons."
54 (defface eww-form-checkbox
55 '((((type x w32 ns) (class color)) ; Like default mode line
56 :box (:line-width 2 :style released-button)
57 :background "lightgrey" :foreground "black"))
58 "Face for eww buffer buttons."
62 (defface eww-form-select
63 '((((type x w32 ns) (class color)) ; Like default mode line
64 :box (:line-width 2 :style released-button)
65 :background "lightgrey" :foreground "black"))
66 "Face for eww buffer buttons."
70 (defface eww-form-text
71 '((t (:background "#505050"
73 :box (:line-width 1))))
74 "Face for eww text inputs."
78 (defvar eww-current-url nil)
79 (defvar eww-current-title ""
80 "Title of current page.")
81 (defvar eww-history nil)
83 (defvar eww-next-url nil)
84 (defvar eww-previous-url nil)
85 (defvar eww-up-url nil)
86 (defvar eww-top-url nil)
90 "Fetch URL and render the page."
91 (interactive "sUrl: ")
92 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
93 (setq url (concat "http://" url)))
94 (url-retrieve url 'eww-render (list url)))
97 (defun eww-open-file (file)
98 "Render a file using EWW."
99 (interactive "fFile: ")
100 (eww (concat "file://" (expand-file-name file))))
102 (defun eww-render (status url &optional point)
103 (let ((redirect (plist-get status :redirect)))
105 (setq url redirect)))
106 (set (make-local-variable 'eww-next-url) nil)
107 (set (make-local-variable 'eww-previous-url) nil)
108 (set (make-local-variable 'eww-up-url) nil)
109 (set (make-local-variable 'eww-top-url) nil)
110 (let* ((headers (eww-parse-headers))
112 (and (string-match "#\\(.*\\)" url)
113 (match-string 1 url)))
115 (mail-header-parse-content-type
116 (or (cdr (assoc "content-type" headers))
120 (or (cdr (assq 'charset (cdr content-type)))
121 (eww-detect-charset (equal (car content-type)
124 (data-buffer (current-buffer)))
128 ((equal (car content-type) "text/html")
129 (eww-display-html charset url))
130 ((string-match "^image/" (car content-type))
133 (eww-display-raw charset)))
138 (let ((point (next-single-property-change
139 (point-min) 'shr-target-id)))
141 (goto-char (1+ point)))))))
142 (kill-buffer data-buffer))))
144 (defun eww-parse-headers ()
146 (goto-char (point-min))
147 (while (and (not (eobp))
149 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
150 (push (cons (downcase (match-string 1))
158 (defun eww-detect-charset (html-p)
159 (let ((case-fold-search t)
163 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t)
167 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
170 (defun eww-display-html (charset url)
171 (unless (eq charset 'utf8)
172 (decode-coding-region (point) (point-max) charset))
175 'base (list (cons 'href url))
176 (libxml-parse-html-region (point) (point-max)))))
178 (setq eww-current-url url)
179 (eww-update-header-line-format)
180 (let ((inhibit-read-only t)
181 (after-change-functions nil)
183 (shr-external-rendering-functions
184 '((title . eww-tag-title)
185 (form . eww-tag-form)
186 (input . eww-tag-input)
187 (textarea . eww-tag-textarea)
188 (body . eww-tag-body)
189 (select . eww-tag-select)
190 (link . eww-tag-link)
192 (shr-insert-document document))
193 (goto-char (point-min))))
195 (defun eww-handle-link (cont)
196 (let* ((rel (assq :rel cont))
197 (href (assq :href cont))
198 (where (assoc (cdr rel)
199 '(("next" . eww-next-url)
200 ("previous" . eww-previous-url)
201 ("start" . eww-top-url)
202 ("up" . eww-up-url)))))
205 (set (cdr where) (cdr href)))))
207 (defun eww-tag-link (cont)
208 (eww-handle-link cont)
211 (defun eww-tag-a (cont)
212 (eww-handle-link cont)
215 (defun eww-update-header-line-format ()
216 (if eww-header-line-format
217 (setq header-line-format (format-spec eww-header-line-format
218 `((?u . ,eww-current-url)
219 (?t . ,eww-current-title))))
220 (setq header-line-format nil)))
222 (defun eww-tag-title (cont)
223 (setq eww-current-title "")
225 (when (eq (car sub) 'text)
226 (setq eww-current-title (concat eww-current-title (cdr sub)))))
227 (eww-update-header-line-format))
229 (defun eww-tag-body (cont)
230 (let* ((start (point))
231 (fgcolor (cdr (or (assq :fgcolor cont)
233 (bgcolor (cdr (assq :bgcolor cont)))
234 (shr-stylesheet (list (cons 'color fgcolor)
235 (cons 'background-color bgcolor))))
237 (eww-colorize-region start (point) fgcolor bgcolor)))
239 (defun eww-colorize-region (start end fg &optional bg)
241 (let ((new-colors (shr-color-check fg bg)))
244 (add-face-text-property start end
245 (list :foreground (cadr new-colors))
248 (add-face-text-property start end
249 (list :background (car new-colors))
252 (defun eww-display-raw (charset)
253 (let ((data (buffer-substring (point) (point-max))))
255 (let ((inhibit-read-only t))
257 (goto-char (point-min))))
259 (defun eww-display-image ()
260 (let ((data (buffer-substring (point) (point-max))))
262 (let ((inhibit-read-only t))
263 (shr-put-image data nil))
264 (goto-char (point-min))))
266 (defun eww-setup-buffer ()
267 (pop-to-buffer (get-buffer-create "*eww*"))
269 (let ((inhibit-read-only t))
274 (let ((map (make-sparse-keymap)))
275 (suppress-keymap map)
276 (define-key map "q" 'eww-quit)
277 (define-key map "g" 'eww-reload)
278 (define-key map [tab] 'shr-next-link)
279 (define-key map [backtab] 'shr-previous-link)
280 (define-key map [delete] 'scroll-down-command)
281 (define-key map "\177" 'scroll-down-command)
282 (define-key map " " 'scroll-up-command)
283 (define-key map "l" 'eww-back-url)
284 (define-key map "n" 'eww-next-url)
285 (define-key map "p" 'eww-previous-url)
286 (define-key map "u" 'eww-up-url)
287 (define-key map "t" 'eww-top-url)
290 (define-derived-mode eww-mode nil "eww"
291 "Mode for browsing the web.
294 (set (make-local-variable 'eww-current-url) 'author)
295 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
296 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
297 ;;(setq buffer-read-only t)
300 (defun eww-browse-url (url &optional new-window)
301 (when (and (equal major-mode 'eww-mode)
303 (push (list eww-current-url (point))
308 "Exit the Emacs Web Wowser."
310 (setq eww-history nil)
311 (kill-buffer (current-buffer)))
313 (defun eww-back-url ()
314 "Go to the previously displayed page."
316 (when (zerop (length eww-history))
317 (error "No previous page"))
318 (let ((prev (pop eww-history)))
319 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
321 (defun eww-next-url ()
322 "Go to the page marked `next'.
323 A page is marked `next' if rel=\"next\" appears in a <link>
327 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
328 (error "No `next' on this page")))
330 (defun eww-previous-url ()
331 "Go to the page marked `previous'.
332 A page is marked `previous' if rel=\"previous\" appears in a <link>
336 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
337 (error "No `previous' on this page")))
340 "Go to the page marked `up'.
341 A page is marked `up' if rel=\"up\" appears in a <link>
345 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
346 (error "No `up' on this page")))
348 (defun eww-top-url ()
349 "Go to the page marked `top'.
350 A page is marked `top' if rel=\"start\" appears in a <link>
354 (eww-browse-url (shr-expand-url eww-top-url eww-current-url))
355 (error "No `top' on this page")))
358 "Reload the current page."
360 (url-retrieve eww-current-url 'eww-render
361 (list eww-current-url (point))))
365 (defvar eww-form nil)
367 (defvar eww-submit-map
368 (let ((map (make-sparse-keymap)))
369 (define-key map "\r" 'eww-submit)
372 (defvar eww-checkbox-map
373 (let ((map (make-sparse-keymap)))
374 (define-key map [space] 'eww-toggle-checkbox)
375 (define-key map "\r" 'eww-toggle-checkbox)
379 (let ((map (make-keymap)))
380 (set-keymap-parent map text-mode-map)
381 (define-key map "\r" 'eww-submit)
382 (define-key map [(control a)] 'eww-beginning-of-text)
383 (define-key map [(control e)] 'eww-end-of-text)
384 (define-key map [tab] 'shr-next-link)
385 (define-key map [backtab] 'shr-previous-link)
388 (defun eww-beginning-of-text ()
389 "Move to the start of the input field."
391 (goto-char (eww-beginning-of-field)))
393 (defun eww-end-of-text ()
394 "Move to the end of the text in the input field."
396 (goto-char (eww-end-of-field))
397 (let ((start (eww-beginning-of-field)))
398 (while (and (equal (following-char) ? )
401 (when (> (point) start)
404 (defun eww-beginning-of-field ()
405 (previous-single-property-change
406 (point) 'eww-form nil (point-min)))
408 (defun eww-end-of-field ()
409 (1- (next-single-property-change
410 (point) 'eww-form nil (point-max))))
412 (defvar eww-textarea-map
413 (let ((map (make-sparse-keymap)))
414 (set-keymap-parent map text-mode-map)
417 (defvar eww-select-map
418 (let ((map (make-sparse-keymap)))
419 (define-key map "\r" 'eww-change-select)
422 (defun eww-tag-form (cont)
424 (list (assq :method cont)
425 (assq :action cont)))
427 (shr-ensure-paragraph)
432 (when (> (point) start)
433 (put-text-property start (1+ start)
434 'eww-form eww-form))))
436 (defun eww-form-submit (cont)
437 (let ((start (point))
438 (value (cdr (assq :value cont))))
440 (if (zerop (length value))
444 (add-face-text-property start (point) 'eww-form-submit)
445 (put-text-property start (point) 'eww-form
446 (list :eww-form eww-form
448 :name (cdr (assq :name cont))))
449 (put-text-property start (point) 'keymap eww-submit-map)
452 (defun eww-form-checkbox (cont)
453 (let ((start (point)))
454 (if (cdr (assq :checked cont))
457 (add-face-text-property start (point) 'eww-form-checkbox)
458 (put-text-property start (point) 'eww-form
459 (list :eww-form eww-form
460 :value (cdr (assq :value cont))
461 :type (downcase (cdr (assq :type cont)))
462 :name (cdr (assq :name cont))))
463 (put-text-property start (point) 'keymap eww-checkbox-map)
466 (defun eww-form-text (cont)
467 (let ((start (point))
468 (type (downcase (or (cdr (assq :type cont))
470 (value (or (cdr (assq :value cont)) ""))
471 (width (string-to-number
472 (or (cdr (assq :size cont))
475 (when (< (length value) width)
476 (insert (make-string (- width (length value)) ? )))
477 (put-text-property start (point) 'face 'eww-form-text)
478 (put-text-property start (point) 'local-map eww-text-map)
479 (put-text-property start (point) 'inhibit-read-only t)
480 (put-text-property start (point) 'eww-form
481 (list :eww-form eww-form
484 :name (cdr (assq :name cont))))))
486 (defun eww-process-text-input (beg end length)
487 (let ((form (get-text-property end 'eww-form))
488 (properties (text-properties-at end)))
494 (goto-char (eww-end-of-field))
495 (let ((new (- end beg)))
496 (while (and (> new 0)
497 (eql (following-char) ? ))
498 (delete-region (point) (1+ (point)))
499 (setq new (1- new))))
500 (set-text-properties beg end properties)))
504 (goto-char (1+ (eww-end-of-field)))
505 (let ((start (point)))
506 (insert (make-string length ? ))
507 (set-text-properties start (point) properties)))))
508 (plist-put form :value (buffer-substring-no-properties
509 (eww-beginning-of-field)
510 (eww-end-of-field))))))
512 (defun eww-form-textarea (cont)
513 (let ((start (point))
514 (value (or (cdr (assq :value cont)) ""))
515 (lines (string-to-number
516 (or (cdr (assq :rows cont))
518 (width (string-to-number
519 (or (cdr (assq :cols cont))
525 (when (< (count-lines start (point)) lines)
526 (dotimes (i (- lines (count-lines start (point))))
530 (while (< (point) end)
532 (let ((pad (- width (- (point) (line-beginning-position)))))
534 (insert (make-string pad ? ))))
535 (add-face-text-property (line-beginning-position)
536 (point) 'eww-form-text)
537 (put-text-property (line-beginning-position) (point)
538 'keymap eww-text-map)))
539 (put-text-property start (point) 'eww-form
540 (list :eww-form eww-form
542 :type (downcase (cdr (assq :type cont)))
543 :name (cdr (assq :name cont)))))
545 (defun eww-tag-input (cont)
546 (let ((type (downcase (or (cdr (assq :type cont))
549 ((or (equal type "checkbox")
550 (equal type "radio"))
551 (eww-form-checkbox cont))
552 ((equal type "submit")
553 (eww-form-submit cont))
554 ((equal type "hidden")
555 (let ((form eww-form)
556 (name (cdr (assq :name cont))))
557 ;; Don't add <input type=hidden> elements repeatedly.
559 (or (not (consp (car form)))
560 (not (eq (caar form) 'hidden))
561 (not (equal (plist-get (cdr (car form)) :name)
563 (setq form (cdr form)))
565 (nconc eww-form (list
568 :value (cdr (assq :value cont))))))))
570 (eww-form-text cont)))))
572 (defun eww-tag-textarea (cont)
573 (eww-form-textarea cont))
575 (defun eww-tag-select (cont)
576 (shr-ensure-paragraph)
577 (let ((menu (list :name (cdr (assq :name cont))
583 (when (eq (car elem) 'option)
584 (when (cdr (assq :selected (cdr elem)))
585 (nconc menu (list :value
586 (cdr (assq :value (cdr elem))))))
587 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
588 (setq max (max max (length display))))
590 :value (cdr (assq :value (cdr elem)))
594 ;; If we have no selected values, default to the first value.
595 (unless (plist-get menu :value)
596 (nconc menu (list :value (nth 2 (car options)))))
598 (let ((selected (eww-element-value menu)))
600 (make-string (- max (length selected)) ? )))
601 (put-text-property start (point) 'eww-form menu)
602 (add-face-text-property start (point) 'eww-form-select)
603 (put-text-property start (point) 'keymap eww-select-map)
604 (shr-ensure-paragraph))))
606 (defun eww-click-radio (widget &rest ignore)
607 (let ((form (plist-get (cdr widget) :eww-form))
608 (name (plist-get (cdr widget) :name)))
609 (when (equal (plist-get (cdr widget) :type) "radio")
610 (if (widget-value widget)
611 ;; Switch all the other radio buttons off.
612 (dolist (overlay (overlays-in (point-min) (point-max)))
613 (let ((field (plist-get (overlay-properties overlay) 'button)))
614 (when (and (eq (plist-get (cdr field) :eww-form) form)
615 (equal name (plist-get (cdr field) :name)))
616 (unless (eq field widget)
617 (widget-value-set field nil)))))
618 (widget-value-set widget t)))
619 (eww-fix-widget-keymap)))
621 (defun eww-inputs (form)
622 (let ((start (point-min))
625 (< start (point-max)))
626 (when (or (get-text-property start 'eww-form)
627 (setq start (next-single-property-change start 'eww-form)))
628 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
630 (push (cons start (get-text-property start 'eww-form))
632 (setq start (next-single-property-change start 'eww-form))))
635 (defun eww-input-value (input)
636 (let ((type (plist-get input :type)))
638 ((equal type "select")
641 (let ((value (plist-get input :value)))
642 (if (string-match " +" value)
643 (substring value 0 (match-beginning 0))
647 "Submit the current form."
649 (let* ((this-input (get-text-property (point) 'eww-form))
650 (form (plist-get this-input :eww-form))
652 (dolist (elem (sort (eww-inputs form)
654 (< (car o1) (car o2)))))
655 (let* ((input (cdr elem))
656 (input-start (car elem))
657 (name (plist-get input :name)))
660 ((equal (plist-get input :type) "checkbox")
661 (when (plist-get input :checked)
662 (push (cons name (plist-get input :value))
664 ((equal (plist-get input :type) "submit")
665 ;; We want the values from buttons if we hit a button if
666 ;; we hit enter on it, or if it's the first button after
667 ;; the field we did hit return on.
668 (when (or (eq input this-input)
669 (and (not (eq input this-input))
671 (> input-start (point))))
672 (push (cons name (plist-get input :value))
675 (push (cons name (eww-input-value input))
678 (when (and (consp elem)
679 (eq (car elem) 'hidden))
680 (push (cons (plist-get (cdr elem) :name)
681 (plist-get (cdr elem) :value))
683 (if (and (stringp (cdr (assq :method form)))
684 (equal (downcase (cdr (assq :method form))) "post"))
685 (let ((url-request-method "POST")
686 (url-request-extra-headers
687 '(("Content-Type" . "application/x-www-form-urlencoded")))
688 (url-request-data (mm-url-encode-www-form-urlencoded values)))
689 (eww-browse-url (shr-expand-url (cdr (assq :action form))
693 (if (cdr (assq :action form))
694 (shr-expand-url (cdr (assq :action form))
698 (mm-url-encode-www-form-urlencoded values))))))