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 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
48 "Prefix URL to search engine"
53 (defface eww-form-submit
54 '((((type x w32 ns
) (class color
)) ; Like default mode line
55 :box
(:line-width
2 :style released-button
)
56 :background
"#808080" :foreground
"black"))
57 "Face for eww buffer buttons."
61 (defface eww-form-checkbox
62 '((((type x w32 ns
) (class color
)) ; Like default mode line
63 :box
(:line-width
2 :style released-button
)
64 :background
"lightgrey" :foreground
"black"))
65 "Face for eww buffer buttons."
69 (defface eww-form-select
70 '((((type x w32 ns
) (class color
)) ; Like default mode line
71 :box
(:line-width
2 :style released-button
)
72 :background
"lightgrey" :foreground
"black"))
73 "Face for eww buffer buttons."
77 (defface eww-form-text
78 '((t (:background
"#505050"
80 :box
(:line-width
1))))
81 "Face for eww text inputs."
85 (defvar eww-current-url nil
)
86 (defvar eww-current-title
""
87 "Title of current page.")
88 (defvar eww-history nil
)
90 (defvar eww-next-url nil
)
91 (defvar eww-previous-url nil
)
92 (defvar eww-up-url nil
)
93 (defvar eww-home-url nil
)
94 (defvar eww-start-url nil
)
95 (defvar eww-contents-url nil
)
99 "Fetch URL and render the page.
100 If the input doesn't look like an URL or a domain name, the
101 word(s) will be searched for via `eww-search-prefix'."
102 (interactive "sEnter URL or keywords: ")
103 (if (and (= (length (split-string url
)) 1)
104 (> (length (split-string url
"\\.")) 1))
105 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
106 (setq url
(concat "http://" url
)))
107 (setq url
(concat eww-search-prefix
108 (replace-regexp-in-string " " "+" url
))))
109 (url-retrieve url
'eww-render
(list url
)))
112 (defun eww-open-file (file)
113 "Render a file using EWW."
114 (interactive "fFile: ")
115 (eww (concat "file://" (expand-file-name file
))))
117 (defun eww-render (status url
&optional point
)
118 (let ((redirect (plist-get status
:redirect
)))
120 (setq url redirect
)))
121 (set (make-local-variable 'eww-next-url
) nil
)
122 (set (make-local-variable 'eww-previous-url
) nil
)
123 (set (make-local-variable 'eww-up-url
) nil
)
124 (set (make-local-variable 'eww-home-url
) nil
)
125 (set (make-local-variable 'eww-start-url
) nil
)
126 (set (make-local-variable 'eww-contents-url
) nil
)
127 (let* ((headers (eww-parse-headers))
129 (and (string-match "#\\(.*\\)" url
)
130 (match-string 1 url
)))
132 (mail-header-parse-content-type
133 (or (cdr (assoc "content-type" headers
))
137 (or (cdr (assq 'charset
(cdr content-type
)))
138 (eww-detect-charset (equal (car content-type
)
141 (data-buffer (current-buffer)))
145 ((equal (car content-type
) "text/html")
146 (eww-display-html charset url
))
147 ((string-match "^image/" (car content-type
))
150 (eww-display-raw charset
)))
155 (let ((point (next-single-property-change
156 (point-min) 'shr-target-id
)))
158 (goto-char (1+ point
)))))))
159 (kill-buffer data-buffer
))))
161 (defun eww-parse-headers ()
163 (goto-char (point-min))
164 (while (and (not (eobp))
166 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
167 (push (cons (downcase (match-string 1))
175 (defun eww-detect-charset (html-p)
176 (let ((case-fold-search t
)
180 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t
)
184 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
187 (defun eww-display-html (charset url
)
188 (unless (eq charset
'utf8
)
189 (decode-coding-region (point) (point-max) charset
))
192 'base
(list (cons 'href url
))
193 (libxml-parse-html-region (point) (point-max)))))
195 (setq eww-current-url url
)
196 (eww-update-header-line-format)
197 (let ((inhibit-read-only t
)
198 (after-change-functions nil
)
200 (shr-external-rendering-functions
201 '((title . eww-tag-title
)
202 (form . eww-tag-form
)
203 (input . eww-tag-input
)
204 (textarea . eww-tag-textarea
)
205 (body . eww-tag-body
)
206 (select . eww-tag-select
)
207 (link . eww-tag-link
)
209 (shr-insert-document document
))
210 (goto-char (point-min))))
212 (defun eww-handle-link (cont)
213 (let* ((rel (assq :rel cont
))
214 (href (assq :href cont
))
216 ;; The text associated with :rel is case-insensitive.
217 (if rel
(downcase (cdr rel
)))
218 '(("next" . eww-next-url
)
219 ;; Texinfo uses "previous", but HTML specifies
220 ;; "prev", so recognize both.
221 ("previous" . eww-previous-url
)
222 ("prev" . eww-previous-url
)
223 ;; HTML specifies "start" but also "contents",
224 ;; and Gtk seems to use "home". Recognize
225 ;; them all; but store them in different
226 ;; variables so that we can readily choose the
228 ("start" . eww-start-url
)
229 ("home" . eww-home-url
)
230 ("contents" . eww-contents-url
)
231 ("up" . eww-up-url
)))))
234 (set (cdr where
) (cdr href
)))))
236 (defun eww-tag-link (cont)
237 (eww-handle-link cont
)
240 (defun eww-tag-a (cont)
241 (eww-handle-link cont
)
244 (defun eww-update-header-line-format ()
245 (if eww-header-line-format
246 (setq header-line-format
247 (replace-regexp-in-string
249 (format-spec eww-header-line-format
250 `((?u .
,eww-current-url
)
251 (?t .
,eww-current-title
)))))
252 (setq header-line-format nil
)))
254 (defun eww-tag-title (cont)
255 (setq eww-current-title
"")
257 (when (eq (car sub
) 'text
)
258 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
259 (eww-update-header-line-format))
261 (defun eww-tag-body (cont)
262 (let* ((start (point))
263 (fgcolor (cdr (or (assq :fgcolor cont
)
265 (bgcolor (cdr (assq :bgcolor cont
)))
266 (shr-stylesheet (list (cons 'color fgcolor
)
267 (cons 'background-color bgcolor
))))
269 (eww-colorize-region start
(point) fgcolor bgcolor
)))
271 (defun eww-colorize-region (start end fg
&optional bg
)
273 (let ((new-colors (shr-color-check fg bg
)))
276 (add-face-text-property start end
277 (list :foreground
(cadr new-colors
))
280 (add-face-text-property start end
281 (list :background
(car new-colors
))
284 (defun eww-display-raw (charset)
285 (let ((data (buffer-substring (point) (point-max))))
287 (let ((inhibit-read-only t
))
289 (goto-char (point-min))))
291 (defun eww-display-image ()
292 (let ((data (buffer-substring (point) (point-max))))
294 (let ((inhibit-read-only t
))
295 (shr-put-image data nil
))
296 (goto-char (point-min))))
298 (defun eww-setup-buffer ()
299 (pop-to-buffer (get-buffer-create "*eww*"))
301 (let ((inhibit-read-only t
))
306 (let ((map (make-sparse-keymap)))
307 (suppress-keymap map
)
308 (define-key map
"q" 'eww-quit
)
309 (define-key map
"g" 'eww-reload
)
310 (define-key map
[tab] 'shr-next-link)
311 (define-key map [backtab] 'shr-previous-link)
312 (define-key map [delete] 'scroll-down-command)
313 (define-key map "\177" 'scroll-down-command)
314 (define-key map " " 'scroll-up-command)
315 (define-key map "l" 'eww-back-url)
316 (define-key map "n" 'eww-next-url)
317 (define-key map "p" 'eww-previous-url)
318 (define-key map "u" 'eww-up-url)
319 (define-key map "t" 'eww-top-url)
322 (define-derived-mode eww-mode nil "eww"
323 "Mode for browsing the web.
326 (set (make-local-variable 'eww-current-url) 'author)
327 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
328 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
329 ;;(setq buffer-read-only t)
332 (defun eww-browse-url (url &optional new-window)
333 (when (and (equal major-mode 'eww-mode)
335 (push (list eww-current-url (point))
340 "Exit the Emacs Web Wowser."
342 (setq eww-history nil)
343 (kill-buffer (current-buffer)))
345 (defun eww-back-url ()
346 "Go to the previously displayed page."
348 (when (zerop (length eww-history))
349 (error "No previous page"))
350 (let ((prev (pop eww-history)))
351 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
353 (defun eww-next-url ()
354 "Go to the page marked `next'.
355 A page is marked `next' if rel=\"next\" appears in a <link>
359 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
360 (error "No `next' on this page")))
362 (defun eww-previous-url ()
363 "Go to the page marked `previous'.
364 A page is marked `previous' if rel=\"previous\" appears in a <link>
368 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
369 (error "No `previous' on this page")))
372 "Go to the page marked `up'.
373 A page is marked `up' if rel=\"up\" appears in a <link>
377 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
378 (error "No `up' on this page")))
380 (defun eww-top-url ()
381 "Go to the page marked `top'.
382 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
383 appears in a <link> or <a> tag."
385 (let ((best-url (or eww-start-url
389 (eww-browse-url (shr-expand-url best-url eww-current-url))
390 (error "No `top' for this page"))))
393 "Reload the current page."
395 (url-retrieve eww-current-url 'eww-render
396 (list eww-current-url (point))))
400 (defvar eww-form nil)
402 (defvar eww-submit-map
403 (let ((map (make-sparse-keymap)))
404 (define-key map "\r" 'eww-submit)
407 (defvar eww-checkbox-map
408 (let ((map (make-sparse-keymap)))
409 (define-key map [space] 'eww-toggle-checkbox)
410 (define-key map "\r" 'eww-toggle-checkbox)
414 (let ((map (make-keymap)))
415 (set-keymap-parent map text-mode-map)
416 (define-key map "\r" 'eww-submit)
417 (define-key map [(control a)] 'eww-beginning-of-text)
418 (define-key map [(control e)] 'eww-end-of-text)
419 (define-key map [tab] 'shr-next-link
)
420 (define-key map
[backtab] 'shr-previous-link)
423 (defvar eww-textarea-map
424 (let ((map (make-keymap)))
425 (set-keymap-parent map text-mode-map)
426 (define-key map "\r" 'forward-line)
427 (define-key map [tab] 'shr-next-link)
428 (define-key map [backtab] 'shr-previous-link
)
431 (defvar eww-select-map
432 (let ((map (make-sparse-keymap)))
433 (define-key map
"\r" 'eww-change-select
)
436 (defun eww-beginning-of-text ()
437 "Move to the start of the input field."
439 (goto-char (eww-beginning-of-field)))
441 (defun eww-end-of-text ()
442 "Move to the end of the text in the input field."
444 (goto-char (eww-end-of-field))
445 (let ((start (eww-beginning-of-field)))
446 (while (and (equal (following-char) ?
)
449 (when (> (point) start
)
452 (defun eww-beginning-of-field ()
456 ((not (eq (get-text-property (point) 'eww-form
)
457 (get-text-property (1- (point)) 'eww-form
)))
460 (previous-single-property-change
461 (point) 'eww-form nil
(point-min)))))
463 (defun eww-end-of-field ()
464 (1- (next-single-property-change
465 (point) 'eww-form nil
(point-max))))
467 (defun eww-tag-form (cont)
469 (list (assq :method cont
)
470 (assq :action cont
)))
472 (shr-ensure-paragraph)
477 (when (> (point) start
)
478 (put-text-property start
(1+ start
)
479 'eww-form eww-form
))))
481 (defun eww-form-submit (cont)
482 (let ((start (point))
483 (value (cdr (assq :value cont
))))
485 (if (zerop (length value
))
489 (add-face-text-property start
(point) 'eww-form-submit
)
490 (put-text-property start
(point) 'eww-form
491 (list :eww-form eww-form
494 :name
(cdr (assq :name cont
))))
495 (put-text-property start
(point) 'keymap eww-submit-map
)
498 (defun eww-form-checkbox (cont)
499 (let ((start (point)))
500 (if (cdr (assq :checked cont
))
503 (add-face-text-property start
(point) 'eww-form-checkbox
)
504 (put-text-property start
(point) 'eww-form
505 (list :eww-form eww-form
506 :value
(cdr (assq :value cont
))
507 :type
(downcase (cdr (assq :type cont
)))
508 :checked
(cdr (assq :checked cont
))
509 :name
(cdr (assq :name cont
))))
510 (put-text-property start
(point) 'keymap eww-checkbox-map
)
513 (defun eww-form-text (cont)
514 (let ((start (point))
515 (type (downcase (or (cdr (assq :type cont
))
517 (value (or (cdr (assq :value cont
)) ""))
518 (width (string-to-number
519 (or (cdr (assq :size cont
))
522 (when (< (length value
) width
)
523 (insert (make-string (- width
(length value
)) ?
)))
524 (put-text-property start
(point) 'face
'eww-form-text
)
525 (put-text-property start
(point) 'local-map eww-text-map
)
526 (put-text-property start
(point) 'inhibit-read-only t
)
527 (put-text-property start
(point) 'eww-form
528 (list :eww-form eww-form
531 :name
(cdr (assq :name cont
))))
534 (defun eww-process-text-input (beg end length
)
535 (let* ((form (get-text-property end
'eww-form
))
536 (properties (text-properties-at end
))
537 (type (plist-get form
:type
)))
539 (member type
'("text" "password" "textarea")))
542 ;; Delete some space at the end.
545 (if (equal type
"textarea")
546 (1- (line-end-position))
548 (let ((new (- end beg
)))
549 (while (and (> new
0)
550 (eql (following-char) ?
))
551 (delete-region (point) (1+ (point)))
552 (setq new
(1- new
))))
553 (set-text-properties beg end properties
)))
558 (if (equal type
"textarea")
559 (1- (line-end-position))
561 (let ((start (point)))
562 (insert (make-string length ?
))
563 (set-text-properties start
(point) properties
)))))
564 (let ((value (buffer-substring-no-properties
565 (eww-beginning-of-field)
566 (eww-end-of-field))))
567 (when (string-match " +\\'" value
)
568 (setq value
(substring value
0 (match-beginning 0))))
569 (plist-put form
:value value
)
570 (when (equal type
"password")
571 ;; Display passwords as asterisks.
572 (let ((start (eww-beginning-of-field)))
573 (put-text-property start
(+ start
(length value
))
574 'display
(make-string (length value
) ?
*))))))))
576 (defun eww-tag-textarea (cont)
577 (let ((start (point))
578 (value (or (cdr (assq :value cont
)) ""))
579 (lines (string-to-number
580 (or (cdr (assq :rows cont
))
582 (width (string-to-number
583 (or (cdr (assq :cols cont
))
589 (when (< (count-lines start
(point)) lines
)
590 (dotimes (i (- lines
(count-lines start
(point))))
592 (setq end
(point-marker))
594 (while (< (point) end
)
596 (let ((pad (- width
(- (point) (line-beginning-position)))))
598 (insert (make-string pad ?
))))
599 (add-face-text-property (line-beginning-position)
600 (point) 'eww-form-text
)
601 (put-text-property (line-beginning-position) (point)
602 'local-map eww-textarea-map
)
604 (put-text-property start
(point) 'eww-form
605 (list :eww-form eww-form
608 :name
(cdr (assq :name cont
))))))
610 (defun eww-tag-input (cont)
611 (let ((type (downcase (or (cdr (assq :type cont
))
615 ((or (equal type
"checkbox")
616 (equal type
"radio"))
617 (eww-form-checkbox cont
))
618 ((equal type
"submit")
619 (eww-form-submit cont
))
620 ((equal type
"hidden")
621 (let ((form eww-form
)
622 (name (cdr (assq :name cont
))))
623 ;; Don't add <input type=hidden> elements repeatedly.
625 (or (not (consp (car form
)))
626 (not (eq (caar form
) 'hidden
))
627 (not (equal (plist-get (cdr (car form
)) :name
)
629 (setq form
(cdr form
)))
631 (nconc eww-form
(list
634 :value
(cdr (assq :value cont
))))))))
636 (eww-form-text cont
)))
637 (unless (= start
(point))
638 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
640 (defun eww-tag-select (cont)
641 (shr-ensure-paragraph)
642 (let ((menu (list :name
(cdr (assq :name cont
))
648 (when (eq (car elem
) 'option
)
649 (when (cdr (assq :selected
(cdr elem
)))
650 (nconc menu
(list :value
651 (cdr (assq :value
(cdr elem
))))))
652 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
653 (setq max
(max max
(length display
)))
655 :value
(cdr (assq :value
(cdr elem
)))
659 (setq options
(nreverse options
))
660 ;; If we have no selected values, default to the first value.
661 (unless (plist-get menu
:value
)
662 (nconc menu
(list :value
(nth 2 (car options
)))))
664 (let ((selected (eww-select-display menu
)))
666 (make-string (- max
(length selected
)) ?
)))
667 (put-text-property start
(point) 'eww-form menu
)
668 (add-face-text-property start
(point) 'eww-form-select
)
669 (put-text-property start
(point) 'keymap eww-select-map
)
670 (shr-ensure-paragraph))))
672 (defun eww-select-display (select)
673 (let ((value (plist-get select
:value
))
675 (dolist (elem select
)
676 (when (and (consp elem
)
677 (eq (car elem
) 'item
)
678 (equal value
(plist-get (cdr elem
) :value
)))
679 (setq display
(plist-get (cdr elem
) :display
))))
682 (defun eww-change-select ()
683 "Change the value of the select drop-down menu under point."
685 (let* ((input (get-text-property (point) 'eww-form
))
686 (properties (text-properties-at (point)))
687 (completion-ignore-case t
)
690 (mapcar (lambda (elem)
692 (eq (car elem
) 'item
)
693 (cons (plist-get (cdr elem
) :display
)
694 (plist-get (cdr elem
) :value
))))
697 (completing-read "Change value: " options nil
'require-match
))
698 (inhibit-read-only t
))
699 (plist-put input
:value
(cdr (assoc-string display options t
)))
701 (eww-update-field display
))))
703 (defun eww-update-field (string)
704 (let ((properties (text-properties-at (point)))
705 (start (eww-beginning-of-field))
706 (end (1+ (eww-end-of-field))))
707 (delete-region start end
)
709 (make-string (- (- end start
) (length string
)) ?
))
710 (set-text-properties start end properties
)
713 (defun eww-toggle-checkbox ()
714 "Toggle the value of the checkbox under point."
716 (let* ((input (get-text-property (point) 'eww-form
))
717 (type (plist-get input
:type
)))
718 (if (equal type
"checkbox")
721 (if (plist-get input
:checked
)
723 (plist-put input
:checked nil
)
724 (eww-update-field "[ ]"))
725 (plist-put input
:checked t
)
726 (eww-update-field "[X]"))))
727 ;; Radio button. Switch all other buttons off.
728 (let ((name (plist-get input
:name
)))
730 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
731 (when (equal (plist-get (cdr elem
) :name
) name
)
732 (goto-char (car elem
))
733 (if (not (eq (cdr elem
) input
))
735 (plist-put input
:checked nil
)
736 (eww-update-field "[ ]"))
737 (plist-put input
:checked t
)
738 (eww-update-field "[X]")))))
741 (defun eww-inputs (form)
742 (let ((start (point-min))
745 (< start
(point-max)))
746 (when (or (get-text-property start
'eww-form
)
747 (setq start
(next-single-property-change start
'eww-form
)))
748 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
750 (push (cons start
(get-text-property start
'eww-form
))
752 (setq start
(next-single-property-change start
'eww-form
))))
755 (defun eww-input-value (input)
756 (let ((type (plist-get input
:type
))
757 (value (plist-get input
:value
)))
759 ((equal type
"textarea")
762 (goto-char (point-min))
763 (while (re-search-forward "^ +\n\\| +$" nil t
)
764 (replace-match "" t t
))
767 (if (string-match " +\\'" value
)
768 (substring value
0 (match-beginning 0))
772 "Submit the current form."
774 (let* ((this-input (get-text-property (point) 'eww-form
))
775 (form (plist-get this-input
:eww-form
))
777 (dolist (elem (sort (eww-inputs form
)
779 (< (car o1
) (car o2
)))))
780 (let* ((input (cdr elem
))
781 (input-start (car elem
))
782 (name (plist-get input
:name
)))
785 ((member (plist-get input
:type
) '("checkbox" "radio"))
786 (when (plist-get input
:checked
)
787 (push (cons name
(plist-get input
:value
))
789 ((equal (plist-get input
:type
) "submit")
790 ;; We want the values from buttons if we hit a button if
791 ;; we hit enter on it, or if it's the first button after
792 ;; the field we did hit return on.
793 (when (or (eq input this-input
)
794 (and (not (eq input this-input
))
796 (> input-start
(point))))
798 (push (cons name
(plist-get input
:value
))
801 (push (cons name
(eww-input-value input
))
804 (when (and (consp elem
)
805 (eq (car elem
) 'hidden
))
806 (push (cons (plist-get (cdr elem
) :name
)
807 (plist-get (cdr elem
) :value
))
809 (if (and (stringp (cdr (assq :method form
)))
810 (equal (downcase (cdr (assq :method form
))) "post"))
811 (let ((url-request-method "POST")
812 (url-request-extra-headers
813 '(("Content-Type" .
"application/x-www-form-urlencoded")))
814 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
815 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
819 (if (cdr (assq :action form
))
820 (shr-expand-url (cdr (assq :action form
))
824 (mm-url-encode-www-form-urlencoded values
))))))