1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013-2014 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)
36 :link '(custom-manual "(eww) Top")
40 (defcustom eww-header-line-format "%t: %u"
42 - %t is replaced by the title.
43 - %u is replaced by the URL."
48 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
49 "Prefix URL to search engine"
54 (defcustom eww-download-directory "~/Downloads/"
55 "Directory where files will downloaded."
60 (defcustom eww-bookmarks-directory user-emacs-directory
61 "Directory where bookmark files will be stored."
66 (defcustom eww-use-external-browser-for-content-type
67 "\\`\\(video/\\|audio/\\|application/ogg\\)"
68 "Always use external browser for specified content-type."
71 :type '(choice (const :tag "Never" nil)
74 (defcustom eww-form-checkbox-selected-symbol "[X]"
75 "Symbol used to represent a selected checkbox.
76 See also `eww-form-checkbox-symbol'."
79 :type '(choice (const "[X]")
80 (const "☒") ; Unicode BALLOT BOX WITH X
81 (const "☑") ; Unicode BALLOT BOX WITH CHECK
84 (defcustom eww-form-checkbox-symbol "[ ]"
85 "Symbol used to represent a checkbox.
86 See also `eww-form-checkbox-selected-symbol'."
89 :type '(choice (const "[ ]")
90 (const "☐") ; Unicode BALLOT BOX
93 (defface eww-form-submit
94 '((((type x w32 ns) (class color)) ; Like default mode line
95 :box (:line-width 2 :style released-button)
96 :background "#808080" :foreground "black"))
97 "Face for eww buffer buttons."
101 (defface eww-form-checkbox
102 '((((type x w32 ns) (class color)) ; Like default mode line
103 :box (:line-width 2 :style released-button)
104 :background "lightgrey" :foreground "black"))
105 "Face for eww buffer buttons."
109 (defface eww-form-select
110 '((((type x w32 ns) (class color)) ; Like default mode line
111 :box (:line-width 2 :style released-button)
112 :background "lightgrey" :foreground "black"))
113 "Face for eww buffer buttons."
117 (defface eww-form-text
118 '((t (:background "#505050"
120 :box (:line-width 1))))
121 "Face for eww text inputs."
125 (defface eww-form-textarea
126 '((t (:background "#C0C0C0"
128 :box (:line-width 1))))
129 "Face for eww textarea inputs."
133 (defvar eww-current-url nil)
134 (defvar eww-current-dom nil)
135 (defvar eww-current-source nil)
136 (defvar eww-current-title ""
137 "Title of current page.")
138 (defvar eww-history nil)
139 (defvar eww-history-position 0)
141 (defvar eww-next-url nil)
142 (defvar eww-previous-url nil)
143 (defvar eww-up-url nil)
144 (defvar eww-home-url nil)
145 (defvar eww-start-url nil)
146 (defvar eww-contents-url nil)
148 (defvar eww-local-regex "localhost"
149 "When this regex is found in the URL, it's not a keyword but an address.")
151 (defvar eww-link-keymap
152 (let ((map (copy-keymap shr-map)))
153 (define-key map "\r" 'eww-follow-link)
158 "Fetch URL and render the page.
159 If the input doesn't look like an URL or a domain name, the
160 word(s) will be searched for via `eww-search-prefix'."
161 (interactive "sEnter URL or keywords: ")
162 (cond ((string-match-p "\\`file:/" url))
163 ((string-match-p "\\`ftp://" url)
164 (user-error "FTP is not supported."))
166 (if (and (= (length (split-string url)) 1)
167 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url))
168 (> (length (split-string url "[.:]")) 1))
169 (string-match eww-local-regex url)))
171 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
172 (setq url (concat "http://" url)))
173 ;; some site don't redirect final /
174 (when (string= (url-filename (url-generic-parse-url url)) "")
175 (setq url (concat url "/"))))
176 (setq url (concat eww-search-prefix
177 (replace-regexp-in-string " " "+" url))))))
178 (url-retrieve url 'eww-render (list url)))
180 ;;;###autoload (defalias 'browse-web 'eww)
183 (defun eww-open-file (file)
184 "Render a file using EWW."
185 (interactive "fFile: ")
186 (eww (concat "file://"
187 (and (memq system-type '(windows-nt ms-dos))
189 (expand-file-name file))))
191 (defun eww-render (status url &optional point)
192 (let ((redirect (plist-get status :redirect)))
194 (setq url redirect)))
195 (let* ((headers (eww-parse-headers))
197 (mail-header-parse-content-type
198 (or (cdr (assoc "content-type" headers))
202 (or (cdr (assq 'charset (cdr content-type)))
203 (eww-detect-charset (equal (car content-type)
206 (data-buffer (current-buffer)))
209 (setq eww-current-title "")
211 ((and eww-use-external-browser-for-content-type
212 (string-match-p eww-use-external-browser-for-content-type
214 (eww-browse-with-external-browser url))
215 ((equal (car content-type) "text/html")
216 (eww-display-html charset url nil point))
217 ((string-match-p "\\`image/" (car content-type))
219 (eww-update-header-line-format))
222 (eww-update-header-line-format)))
223 (setq eww-current-url url
224 eww-history-position 0))
225 (kill-buffer data-buffer))))
227 (defun eww-parse-headers ()
229 (goto-char (point-min))
230 (while (and (not (eobp))
232 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
233 (push (cons (downcase (match-string 1))
241 (defun eww-detect-charset (html-p)
242 (let ((case-fold-search t)
246 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
250 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
253 (declare-function libxml-parse-html-region "xml.c"
254 (start end &optional base-url))
256 (defun eww-display-html (charset url &optional document point)
257 (or (fboundp 'libxml-parse-html-region)
258 (error "This function requires Emacs to be compiled with libxml2"))
262 'base (list (cons 'href url))
264 (unless (eq charset 'utf-8)
266 (decode-coding-region (point) (point-max) charset)
267 (coding-system-error nil)))
268 (libxml-parse-html-region (point) (point-max))))))
269 (source (and (null document)
270 (buffer-substring (point) (point-max)))))
272 (setq eww-current-source source
273 eww-current-dom document)
274 (let ((inhibit-read-only t)
275 (after-change-functions nil)
276 (shr-target-id (url-target (url-generic-parse-url url)))
277 (shr-external-rendering-functions
278 '((title . eww-tag-title)
279 (form . eww-tag-form)
280 (input . eww-tag-input)
281 (textarea . eww-tag-textarea)
282 (body . eww-tag-body)
283 (select . eww-tag-select)
284 (link . eww-tag-link)
286 (shr-insert-document document)
291 (goto-char (point-min))
292 (let ((point (next-single-property-change
293 (point-min) 'shr-target-id)))
297 (goto-char (point-min)))))
298 (setq eww-current-url url
299 eww-history-position 0)
300 (eww-update-header-line-format)))
302 (defun eww-handle-link (cont)
303 (let* ((rel (assq :rel cont))
304 (href (assq :href cont))
306 ;; The text associated with :rel is case-insensitive.
307 (if rel (downcase (cdr rel)))
308 '(("next" . eww-next-url)
309 ;; Texinfo uses "previous", but HTML specifies
310 ;; "prev", so recognize both.
311 ("previous" . eww-previous-url)
312 ("prev" . eww-previous-url)
313 ;; HTML specifies "start" but also "contents",
314 ;; and Gtk seems to use "home". Recognize
315 ;; them all; but store them in different
316 ;; variables so that we can readily choose the
318 ("start" . eww-start-url)
319 ("home" . eww-home-url)
320 ("contents" . eww-contents-url)
321 ("up" . eww-up-url)))))
324 (set (cdr where) (cdr href)))))
326 (defun eww-tag-link (cont)
327 (eww-handle-link cont)
330 (defun eww-tag-a (cont)
331 (eww-handle-link cont)
332 (let ((start (point)))
334 (put-text-property start (point) 'keymap eww-link-keymap)))
336 (defun eww-update-header-line-format ()
337 (if eww-header-line-format
338 (setq header-line-format
339 (replace-regexp-in-string
341 ;; FIXME? Title can be blank. Default to, eg, last component
343 (format-spec eww-header-line-format
344 `((?u . ,eww-current-url)
345 (?t . ,eww-current-title)))))
346 (setq header-line-format nil)))
348 (defun eww-tag-title (cont)
349 (setq eww-current-title "")
351 (when (eq (car sub) 'text)
352 (setq eww-current-title (concat eww-current-title (cdr sub)))))
353 (setq eww-current-title
354 (replace-regexp-in-string
356 (replace-regexp-in-string "[ \t\r\n]+" " " eww-current-title)))
357 (eww-update-header-line-format))
359 (defun eww-tag-body (cont)
360 (let* ((start (point))
361 (fgcolor (cdr (or (assq :fgcolor cont)
363 (bgcolor (cdr (assq :bgcolor cont)))
364 (shr-stylesheet (list (cons 'color fgcolor)
365 (cons 'background-color bgcolor))))
367 (eww-colorize-region start (point) fgcolor bgcolor)))
369 (defun eww-colorize-region (start end fg &optional bg)
371 (let ((new-colors (shr-color-check fg bg)))
374 (add-face-text-property start end
375 (list :foreground (cadr new-colors))
378 (add-face-text-property start end
379 (list :background (car new-colors))
382 (defun eww-display-raw ()
383 (let ((data (buffer-substring (point) (point-max))))
385 (let ((inhibit-read-only t))
387 (goto-char (point-min))))
389 (defun eww-display-image ()
390 (let ((data (shr-parse-image-data)))
392 (let ((inhibit-read-only t))
393 (shr-put-image data nil))
394 (goto-char (point-min))))
396 (defun eww-setup-buffer ()
397 (switch-to-buffer (get-buffer-create "*eww*"))
398 (let ((inhibit-read-only t))
401 (unless (eq major-mode 'eww-mode)
403 (setq-local eww-next-url nil)
404 (setq-local eww-previous-url nil)
405 (setq-local eww-up-url nil)
406 (setq-local eww-home-url nil)
407 (setq-local eww-start-url nil)
408 (setq-local eww-contents-url nil))
410 (defun eww-view-source ()
411 "View the HTML source code of the current page."
413 (let ((buf (get-buffer-create "*eww-source*"))
414 (source eww-current-source))
415 (with-current-buffer buf
416 (delete-region (point-min) (point-max))
417 (insert (or source "no source"))
418 (goto-char (point-min))
419 (when (fboundp 'html-mode)
423 (defun eww-readable ()
424 "View the main \"readable\" parts of the current web page.
425 This command uses heuristics to find the parts of the web page that
426 contains the main textual portion, leaving out navigation menus and
429 (let* ((source eww-current-source)
430 (dom (shr-transform-dom
434 (decode-coding-region (point-min) (point-max) 'utf-8)
435 (coding-system-error nil))
436 (libxml-parse-html-region (point-min) (point-max))))))
437 (eww-score-readability dom)
439 (eww-display-html nil nil
441 (eww-highest-readability dom)))
442 (setq eww-current-source source)))
444 (defun eww-score-readability (node)
447 ((memq (car node) '(script head))
449 ((eq (car node) 'meta)
451 ((eq (car node) 'img)
454 (setq score (- (length (split-string
455 (or (cdr (assoc 'text (cdr node))) ""))))))
457 (dolist (elem (cdr node))
459 ((eq (car elem) 'text)
460 (setq score (+ score (length (split-string (cdr elem))))))
463 (or (cdr (assoc :eww-readability-score (cdr elem)))
464 (eww-score-readability elem)))))))))
465 ;; Cache the score of the node to avoid recomputing all the time.
466 (setcdr node (cons (cons :eww-readability-score score) (cdr node)))
469 (defun eww-highest-readability (node)
472 (dolist (elem (cdr node))
473 (when (and (consp (cdr elem))
475 :eww-readability-score
477 (eww-highest-readability elem))))
478 most-negative-fixnum)
479 (or (cdr (assoc :eww-readability-score (cdr result)))
480 most-negative-fixnum)))
481 (setq result highest)))
485 (let ((map (make-sparse-keymap)))
486 (suppress-keymap map)
487 (define-key map "q" 'quit-window)
488 (define-key map "g" 'eww-reload)
489 (define-key map [?\t] 'shr-next-link)
490 (define-key map [?\M-\t] 'shr-previous-link)
491 (define-key map [delete] 'scroll-down-command)
492 (define-key map [?\S-\ ] 'scroll-down-command)
493 (define-key map "\177" 'scroll-down-command)
494 (define-key map " " 'scroll-up-command)
495 (define-key map "l" 'eww-back-url)
496 (define-key map "r" 'eww-forward-url)
497 (define-key map "n" 'eww-next-url)
498 (define-key map "p" 'eww-previous-url)
499 (define-key map "u" 'eww-up-url)
500 (define-key map "t" 'eww-top-url)
501 (define-key map "&" 'eww-browse-with-external-browser)
502 (define-key map "d" 'eww-download)
503 (define-key map "w" 'eww-copy-page-url)
504 (define-key map "C" 'url-cookie-list)
505 (define-key map "v" 'eww-view-source)
506 (define-key map "R" 'eww-readable)
507 (define-key map "H" 'eww-list-histories)
509 (define-key map "b" 'eww-add-bookmark)
510 (define-key map "B" 'eww-list-bookmarks)
511 (define-key map [(meta n)] 'eww-next-bookmark)
512 (define-key map [(meta p)] 'eww-previous-bookmark)
514 (easy-menu-define nil map ""
516 ["Exit" quit-window t]
517 ["Close browser" quit-window t]
518 ["Reload" eww-reload t]
519 ["Back to previous page" eww-back-url
520 :active (not (zerop (length eww-history)))]
521 ["Forward to next page" eww-forward-url
522 :active (not (zerop eww-history-position))]
523 ["Browse with external browser" eww-browse-with-external-browser t]
524 ["Download" eww-download t]
525 ["View page source" eww-view-source]
526 ["Copy page URL" eww-copy-page-url t]
527 ["List histories" eww-list-histories t]
528 ["Add bookmark" eww-add-bookmark t]
529 ["List bookmarks" eww-list-bookmarks t]
530 ["List cookies" url-cookie-list t]))
533 (defvar eww-tool-bar-map
534 (let ((map (make-sparse-keymap)))
535 (dolist (tool-bar-item
536 '((quit-window . "close")
537 (eww-reload . "refresh")
538 (eww-back-url . "left-arrow")
539 (eww-forward-url . "right-arrow")
540 (eww-view-source . "show")
541 (eww-copy-page-url . "copy")
542 (eww-add-bookmark . "bookmark_add"))) ;; ...
543 (tool-bar-local-item-from-menu
544 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
546 "Tool bar for `eww-mode'.")
548 (define-derived-mode eww-mode nil "eww"
549 "Mode for browsing the web.
552 ;; FIXME? This seems a strange default.
553 (setq-local eww-current-url 'author)
554 (setq-local eww-current-dom nil)
555 (setq-local eww-current-source nil)
556 (setq-local eww-current-title "")
557 (setq-local browse-url-browser-function 'eww-browse-url)
558 (setq-local after-change-functions 'eww-process-text-input)
559 (setq-local eww-history nil)
560 (setq-local eww-history-position 0)
561 (when (boundp 'tool-bar-map)
562 (setq-local tool-bar-map eww-tool-bar-map))
563 (buffer-disable-undo)
564 ;;(setq buffer-read-only t)
568 (defun eww-browse-url (url &optional _new-window)
569 (when (and (equal major-mode 'eww-mode)
574 (defun eww-back-url ()
575 "Go to the previously displayed page."
577 (when (>= eww-history-position (length eww-history))
578 (user-error "No previous page"))
580 (setq eww-history-position (+ eww-history-position 2))
581 (eww-restore-history (elt eww-history (1- eww-history-position))))
583 (defun eww-forward-url ()
584 "Go to the next displayed page."
586 (when (zerop eww-history-position)
587 (user-error "No next page"))
589 (eww-restore-history (elt eww-history (1- eww-history-position))))
591 (defun eww-restore-history (elem)
592 (let ((inhibit-read-only t))
594 (insert (plist-get elem :text))
595 (setq eww-current-source (plist-get elem :source)
596 eww-current-dom (plist-get elem :dom))
597 (goto-char (plist-get elem :point))
598 (setq eww-current-url (plist-get elem :url)
599 eww-current-title (plist-get elem :title))
600 (eww-update-header-line-format)))
602 (defun eww-next-url ()
603 "Go to the page marked `next'.
604 A page is marked `next' if rel=\"next\" appears in a <link>
608 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
609 (user-error "No `next' on this page")))
611 (defun eww-previous-url ()
612 "Go to the page marked `previous'.
613 A page is marked `previous' if rel=\"previous\" appears in a <link>
617 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
618 (user-error "No `previous' on this page")))
621 "Go to the page marked `up'.
622 A page is marked `up' if rel=\"up\" appears in a <link>
626 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
627 (user-error "No `up' on this page")))
629 (defun eww-top-url ()
630 "Go to the page marked `top'.
631 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
632 appears in a <link> or <a> tag."
634 (let ((best-url (or eww-start-url
638 (eww-browse-url (shr-expand-url best-url eww-current-url))
639 (user-error "No `top' for this page"))))
642 "Reload the current page."
644 (url-retrieve eww-current-url 'eww-render
645 (list eww-current-url (point))))
649 (defvar eww-form nil)
651 (defvar eww-submit-map
652 (let ((map (make-sparse-keymap)))
653 (define-key map "\r" 'eww-submit)
654 (define-key map [(control c) (control c)] 'eww-submit)
657 (defvar eww-checkbox-map
658 (let ((map (make-sparse-keymap)))
659 (define-key map " " 'eww-toggle-checkbox)
660 (define-key map "\r" 'eww-toggle-checkbox)
661 (define-key map [(control c) (control c)] 'eww-submit)
665 (let ((map (make-keymap)))
666 (set-keymap-parent map text-mode-map)
667 (define-key map "\r" 'eww-submit)
668 (define-key map [(control a)] 'eww-beginning-of-text)
669 (define-key map [(control c) (control c)] 'eww-submit)
670 (define-key map [(control e)] 'eww-end-of-text)
671 (define-key map [?\t] 'shr-next-link)
672 (define-key map [?\M-\t] 'shr-previous-link)
675 (defvar eww-textarea-map
676 (let ((map (make-keymap)))
677 (set-keymap-parent map text-mode-map)
678 (define-key map "\r" 'forward-line)
679 (define-key map [(control c) (control c)] 'eww-submit)
680 (define-key map [?\t] 'shr-next-link)
681 (define-key map [?\M-\t] 'shr-previous-link)
684 (defvar eww-select-map
685 (let ((map (make-sparse-keymap)))
686 (define-key map "\r" 'eww-change-select)
687 (define-key map [(control c) (control c)] 'eww-submit)
690 (defun eww-beginning-of-text ()
691 "Move to the start of the input field."
693 (goto-char (eww-beginning-of-field)))
695 (defun eww-end-of-text ()
696 "Move to the end of the text in the input field."
698 (goto-char (eww-end-of-field))
699 (let ((start (eww-beginning-of-field)))
700 (while (and (equal (following-char) ? )
703 (when (> (point) start)
706 (defun eww-beginning-of-field ()
710 ((not (eq (get-text-property (point) 'eww-form)
711 (get-text-property (1- (point)) 'eww-form)))
714 (previous-single-property-change
715 (point) 'eww-form nil (point-min)))))
717 (defun eww-end-of-field ()
718 (1- (next-single-property-change
719 (point) 'eww-form nil (point-max))))
721 (defun eww-tag-form (cont)
723 (list (assq :method cont)
724 (assq :action cont)))
726 (shr-ensure-paragraph)
731 (when (> (point) start)
732 (put-text-property start (1+ start)
733 'eww-form eww-form))))
735 (defun eww-form-submit (cont)
736 (let ((start (point))
737 (value (cdr (assq :value cont))))
739 (if (zerop (length value))
743 (add-face-text-property start (point) 'eww-form-submit)
744 (put-text-property start (point) 'eww-form
745 (list :eww-form eww-form
748 :name (cdr (assq :name cont))))
749 (put-text-property start (point) 'keymap eww-submit-map)
752 (defun eww-form-checkbox (cont)
753 (let ((start (point)))
754 (if (cdr (assq :checked cont))
755 (insert eww-form-checkbox-selected-symbol)
756 (insert eww-form-checkbox-symbol))
757 (add-face-text-property start (point) 'eww-form-checkbox)
758 (put-text-property start (point) 'eww-form
759 (list :eww-form eww-form
760 :value (cdr (assq :value cont))
761 :type (downcase (cdr (assq :type cont)))
762 :checked (cdr (assq :checked cont))
763 :name (cdr (assq :name cont))))
764 (put-text-property start (point) 'keymap eww-checkbox-map)
767 (defun eww-form-text (cont)
768 (let ((start (point))
769 (type (downcase (or (cdr (assq :type cont))
771 (value (or (cdr (assq :value cont)) ""))
772 (width (string-to-number
773 (or (cdr (assq :size cont))
775 (readonly-property (if (or (cdr (assq :disabled cont))
776 (cdr (assq :readonly cont)))
778 'inhibit-read-only)))
780 (when (< (length value) width)
781 (insert (make-string (- width (length value)) ? )))
782 (put-text-property start (point) 'face 'eww-form-text)
783 (put-text-property start (point) 'local-map eww-text-map)
784 (put-text-property start (point) readonly-property t)
785 (put-text-property start (point) 'eww-form
786 (list :eww-form eww-form
789 :name (cdr (assq :name cont))))
792 (defconst eww-text-input-types '("text" "password" "textarea"
793 "color" "date" "datetime" "datetime-local"
794 "email" "month" "number" "search" "tel"
796 "List of input types which represent a text input.
797 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
799 (defun eww-process-text-input (beg end length)
800 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
801 (properties (text-properties-at end))
802 (type (plist-get form :type)))
804 (member type eww-text-input-types))
807 ;; Delete some space at the end.
810 (if (equal type "textarea")
811 (1- (line-end-position))
813 (let ((new (- end beg)))
814 (while (and (> new 0)
815 (eql (following-char) ? ))
816 (delete-region (point) (1+ (point)))
817 (setq new (1- new))))
818 (set-text-properties beg end properties)))
823 (if (equal type "textarea")
824 (1- (line-end-position))
826 (let ((start (point)))
827 (insert (make-string length ? ))
828 (set-text-properties start (point) properties)))))
829 (let ((value (buffer-substring-no-properties
830 (eww-beginning-of-field)
831 (eww-end-of-field))))
832 (when (string-match " +\\'" value)
833 (setq value (substring value 0 (match-beginning 0))))
834 (plist-put form :value value)
835 (when (equal type "password")
836 ;; Display passwords as asterisks.
837 (let ((start (eww-beginning-of-field)))
838 (put-text-property start (+ start (length value))
839 'display (make-string (length value) ?*))))))))
841 (defun eww-tag-textarea (cont)
842 (let ((start (point))
843 (value (or (cdr (assq :value cont)) ""))
844 (lines (string-to-number
845 (or (cdr (assq :rows cont))
847 (width (string-to-number
848 (or (cdr (assq :cols cont))
854 (when (< (count-lines start (point)) lines)
855 (dotimes (i (- lines (count-lines start (point))))
857 (setq end (point-marker))
859 (while (< (point) end)
861 (let ((pad (- width (- (point) (line-beginning-position)))))
863 (insert (make-string pad ? ))))
864 (add-face-text-property (line-beginning-position)
865 (point) 'eww-form-textarea)
866 (put-text-property (line-beginning-position) (point)
867 'local-map eww-textarea-map)
869 (put-text-property start (point) 'eww-form
870 (list :eww-form eww-form
873 :name (cdr (assq :name cont))))))
875 (defun eww-tag-input (cont)
876 (let ((type (downcase (or (cdr (assq :type cont))
880 ((or (equal type "checkbox")
881 (equal type "radio"))
882 (eww-form-checkbox cont))
883 ((equal type "submit")
884 (eww-form-submit cont))
885 ((equal type "hidden")
886 (let ((form eww-form)
887 (name (cdr (assq :name cont))))
888 ;; Don't add <input type=hidden> elements repeatedly.
890 (or (not (consp (car form)))
891 (not (eq (caar form) 'hidden))
892 (not (equal (plist-get (cdr (car form)) :name)
894 (setq form (cdr form)))
896 (nconc eww-form (list
899 :value (cdr (assq :value cont))))))))
901 (eww-form-text cont)))
902 (unless (= start (point))
903 (put-text-property start (1+ start) 'help-echo "Input field"))))
905 (defun eww-tag-select (cont)
906 (shr-ensure-paragraph)
907 (let ((menu (list :name (cdr (assq :name cont))
913 (if (eq (car (car cont)) 'optgroup)
914 (dolist (groupelem cont)
915 (unless (cdr (assq :disabled (cdr groupelem)))
916 (setq opelem (append opelem (cdr (cdr groupelem))))))
918 (dolist (elem opelem)
919 (when (eq (car elem) 'option)
920 (when (cdr (assq :selected (cdr elem)))
921 (nconc menu (list :value
922 (cdr (assq :value (cdr elem))))))
923 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
924 (setq max (max max (length display)))
926 :value (cdr (assq :value (cdr elem)))
930 (setq options (nreverse options))
931 ;; If we have no selected values, default to the first value.
932 (unless (plist-get menu :value)
933 (nconc menu (list :value (nth 2 (car options)))))
935 (let ((selected (eww-select-display menu)))
937 (make-string (- max (length selected)) ? )))
938 (put-text-property start (point) 'eww-form menu)
939 (add-face-text-property start (point) 'eww-form-select)
940 (put-text-property start (point) 'keymap eww-select-map)
941 (unless (= start (point))
942 (put-text-property start (1+ start) 'help-echo "select field"))
943 (shr-ensure-paragraph))))
945 (defun eww-select-display (select)
946 (let ((value (plist-get select :value))
948 (dolist (elem select)
949 (when (and (consp elem)
950 (eq (car elem) 'item)
951 (equal value (plist-get (cdr elem) :value)))
952 (setq display (plist-get (cdr elem) :display))))
955 (defun eww-change-select ()
956 "Change the value of the select drop-down menu under point."
958 (let* ((input (get-text-property (point) 'eww-form))
959 (completion-ignore-case t)
962 (mapcar (lambda (elem)
964 (eq (car elem) 'item)
965 (cons (plist-get (cdr elem) :display)
966 (plist-get (cdr elem) :value))))
969 (completing-read "Change value: " options nil 'require-match))
970 (inhibit-read-only t))
971 (plist-put input :value (cdr (assoc-string display options t)))
973 (eww-update-field display))))
975 (defun eww-update-field (string)
976 (let ((properties (text-properties-at (point)))
977 (start (eww-beginning-of-field))
978 (end (1+ (eww-end-of-field))))
979 (delete-region start end)
981 (make-string (- (- end start) (length string)) ? ))
982 (set-text-properties start end properties)
985 (defun eww-toggle-checkbox ()
986 "Toggle the value of the checkbox under point."
988 (let* ((input (get-text-property (point) 'eww-form))
989 (type (plist-get input :type)))
990 (if (equal type "checkbox")
993 (if (plist-get input :checked)
995 (plist-put input :checked nil)
996 (eww-update-field eww-form-checkbox-symbol))
997 (plist-put input :checked t)
998 (eww-update-field eww-form-checkbox-selected-symbol))))
999 ;; Radio button. Switch all other buttons off.
1000 (let ((name (plist-get input :name)))
1002 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1003 (when (equal (plist-get (cdr elem) :name) name)
1004 (goto-char (car elem))
1005 (if (not (eq (cdr elem) input))
1007 (plist-put input :checked nil)
1008 (eww-update-field eww-form-checkbox-symbol))
1009 (plist-put input :checked t)
1010 (eww-update-field eww-form-checkbox-selected-symbol)))))
1011 (forward-char 1)))))
1013 (defun eww-inputs (form)
1014 (let ((start (point-min))
1017 (< start (point-max)))
1018 (when (or (get-text-property start 'eww-form)
1019 (setq start (next-single-property-change start 'eww-form)))
1020 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1022 (push (cons start (get-text-property start 'eww-form))
1024 (setq start (next-single-property-change start 'eww-form))))
1027 (defun eww-input-value (input)
1028 (let ((type (plist-get input :type))
1029 (value (plist-get input :value)))
1031 ((equal type "textarea")
1034 (goto-char (point-min))
1035 (while (re-search-forward "^ +\n\\| +$" nil t)
1036 (replace-match "" t t))
1039 (if (string-match " +\\'" value)
1040 (substring value 0 (match-beginning 0))
1043 (defun eww-submit ()
1044 "Submit the current form."
1046 (let* ((this-input (get-text-property (point) 'eww-form))
1047 (form (plist-get this-input :eww-form))
1049 (dolist (elem (sort (eww-inputs form)
1051 (< (car o1) (car o2)))))
1052 (let* ((input (cdr elem))
1053 (input-start (car elem))
1054 (name (plist-get input :name)))
1057 ((member (plist-get input :type) '("checkbox" "radio"))
1058 (when (plist-get input :checked)
1059 (push (cons name (plist-get input :value))
1061 ((equal (plist-get input :type) "submit")
1062 ;; We want the values from buttons if we hit a button if
1063 ;; we hit enter on it, or if it's the first button after
1064 ;; the field we did hit return on.
1065 (when (or (eq input this-input)
1066 (and (not (eq input this-input))
1068 (> input-start (point))))
1069 (setq next-submit t)
1070 (push (cons name (plist-get input :value))
1073 (push (cons name (eww-input-value input))
1076 (when (and (consp elem)
1077 (eq (car elem) 'hidden))
1078 (push (cons (plist-get (cdr elem) :name)
1079 (or (plist-get (cdr elem) :value) ""))
1081 (if (and (stringp (cdr (assq :method form)))
1082 (equal (downcase (cdr (assq :method form))) "post"))
1083 (let ((url-request-method "POST")
1084 (url-request-extra-headers
1085 '(("Content-Type" . "application/x-www-form-urlencoded")))
1086 (url-request-data (mm-url-encode-www-form-urlencoded values)))
1087 (eww-browse-url (shr-expand-url (cdr (assq :action form))
1091 (if (cdr (assq :action form))
1092 (shr-expand-url (cdr (assq :action form))
1096 (mm-url-encode-www-form-urlencoded values))))))
1098 (defun eww-browse-with-external-browser (&optional url)
1099 "Browse the current URL with an external browser.
1100 The browser to used is specified by the `shr-external-browser' variable."
1102 (funcall shr-external-browser (or url eww-current-url)))
1104 (defun eww-follow-link (&optional external mouse-event)
1105 "Browse the URL under point.
1106 If EXTERNAL, browse the URL using `shr-external-browser'."
1107 (interactive (list current-prefix-arg last-nonmenu-event))
1108 (mouse-set-point mouse-event)
1109 (let ((url (get-text-property (point) 'shr-url)))
1112 (message "No link under point"))
1113 ((string-match "^mailto:" url)
1114 (browse-url-mail url))
1116 (funcall shr-external-browser url))
1117 ;; This is a #target url in the same page as the current one.
1118 ((and (url-target (url-generic-parse-url url))
1119 (eww-same-page-p url eww-current-url))
1121 (eww-display-html 'utf-8 url eww-current-dom))
1123 (eww-browse-url url)))))
1125 (defun eww-same-page-p (url1 url2)
1126 "Return non-nil if both URLs represent the same page.
1127 Differences in #targets are ignored."
1128 (let ((obj1 (url-generic-parse-url url1))
1129 (obj2 (url-generic-parse-url url2)))
1130 (setf (url-target obj1) nil)
1131 (setf (url-target obj2) nil)
1132 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1134 (defun eww-copy-page-url ()
1136 (message "%s" eww-current-url)
1137 (kill-new eww-current-url))
1139 (defun eww-download ()
1140 "Download URL under point to `eww-download-directory'."
1142 (let ((url (get-text-property (point) 'shr-url)))
1144 (message "No URL under point")
1145 (url-retrieve url 'eww-download-callback (list url)))))
1147 (defun eww-download-callback (status url)
1148 (unless (plist-get status :error)
1149 (let* ((obj (url-generic-parse-url url))
1150 (path (car (url-path-and-query obj)))
1151 (file (eww-make-unique-file-name (file-name-nondirectory path)
1152 eww-download-directory)))
1154 (message "Saved %s" file))))
1156 (defun eww-make-unique-file-name (file directory)
1158 ((zerop (length file))
1160 ((string-match "\\`[.]" file)
1161 (setq file (concat "!" file))))
1163 (while (file-exists-p (expand-file-name file directory))
1165 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1166 (format "%s(%d)%s" (match-string 1 file)
1167 count (match-string 2 file))
1168 (format "%s(%d)" file count)))
1169 (setq count (1+ count)))
1170 (expand-file-name file directory)))
1174 (defvar eww-bookmarks nil)
1176 (defun eww-add-bookmark ()
1177 "Add the current page to the bookmarks."
1179 (eww-read-bookmarks)
1180 (dolist (bookmark eww-bookmarks)
1181 (when (equal eww-current-url
1182 (plist-get bookmark :url))
1183 (user-error "Already bookmarked")))
1184 (if (y-or-n-p "bookmark this page? ")
1186 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1187 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1188 (push (list :url eww-current-url
1190 :time (current-time-string))
1192 (eww-write-bookmarks)
1193 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1195 (defun eww-write-bookmarks ()
1196 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1197 (insert ";; Auto-generated file; don't edit\n")
1198 (pp eww-bookmarks (current-buffer))))
1200 (defun eww-read-bookmarks ()
1201 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1203 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1205 (insert-file-contents file)
1206 (read (current-buffer)))))))
1209 (defun eww-list-bookmarks ()
1210 "Display the bookmarks."
1212 (eww-bookmark-prepare)
1213 (pop-to-buffer "*eww bookmarks*"))
1215 (defun eww-bookmark-prepare ()
1216 (eww-read-bookmarks)
1217 (unless eww-bookmarks
1218 (user-error "No bookmarks are defined"))
1219 (set-buffer (get-buffer-create "*eww bookmarks*"))
1221 (let ((format "%-40s %s")
1222 (inhibit-read-only t)
1225 (setq header-line-format (concat " " (format format "URL" "Title")))
1226 (dolist (bookmark eww-bookmarks)
1227 (setq start (point))
1228 (setq url (plist-get bookmark :url))
1229 (when (> (length url) 40)
1230 (setq url (substring url 0 40)))
1231 (insert (format format url
1232 (plist-get bookmark :title))
1234 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1235 (goto-char (point-min))))
1237 (defvar eww-bookmark-kill-ring nil)
1239 (defun eww-bookmark-kill ()
1240 "Kill the current bookmark."
1242 (let* ((start (line-beginning-position))
1243 (bookmark (get-text-property start 'eww-bookmark))
1244 (inhibit-read-only t))
1246 (user-error "No bookmark on the current line"))
1248 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1249 (delete-region start (point))
1250 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1251 (eww-write-bookmarks)))
1253 (defun eww-bookmark-yank ()
1254 "Yank a previously killed bookmark to the current line."
1256 (unless eww-bookmark-kill-ring
1257 (user-error "No previously killed bookmark"))
1259 (let ((inhibit-read-only t)
1262 (insert (pop eww-bookmark-kill-ring))
1263 (setq bookmark (get-text-property start 'eww-bookmark))
1264 (if (= start (point-min))
1265 (push bookmark eww-bookmarks)
1266 (let ((line (count-lines start (point))))
1267 (setcdr (nthcdr (1- line) eww-bookmarks)
1268 (cons bookmark (nthcdr line eww-bookmarks)))))
1269 (eww-write-bookmarks)))
1271 (defun eww-bookmark-browse ()
1272 "Browse the bookmark under point in eww."
1274 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1276 (user-error "No bookmark on the current line"))
1278 (eww-browse-url (plist-get bookmark :url))))
1280 (defun eww-next-bookmark ()
1281 "Go to the next bookmark in the list."
1285 (unless (get-buffer "*eww bookmarks*")
1287 (eww-bookmark-prepare))
1288 (with-current-buffer (get-buffer "*eww bookmarks*")
1289 (when (and (not first)
1292 (setq bookmark (get-text-property (line-beginning-position)
1295 (user-error "No next bookmark")))
1296 (eww-browse-url (plist-get bookmark :url))))
1298 (defun eww-previous-bookmark ()
1299 "Go to the previous bookmark in the list."
1303 (unless (get-buffer "*eww bookmarks*")
1305 (eww-bookmark-prepare))
1306 (with-current-buffer (get-buffer "*eww bookmarks*")
1308 (goto-char (point-max))
1309 (beginning-of-line))
1310 ;; On the final line.
1314 (user-error "No previous bookmark")
1316 (setq bookmark (get-text-property (line-beginning-position)
1318 (eww-browse-url (plist-get bookmark :url))))
1320 (defvar eww-bookmark-mode-map
1321 (let ((map (make-sparse-keymap)))
1322 (suppress-keymap map)
1323 (define-key map "q" 'quit-window)
1324 (define-key map [(control k)] 'eww-bookmark-kill)
1325 (define-key map [(control y)] 'eww-bookmark-yank)
1326 (define-key map "\r" 'eww-bookmark-browse)
1328 (easy-menu-define nil map
1329 "Menu for `eww-bookmark-mode-map'."
1331 ["Exit" quit-window t]
1332 ["Browse" eww-bookmark-browse
1333 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1334 ["Kill" eww-bookmark-kill
1335 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1336 ["Yank" eww-bookmark-yank
1337 :active eww-bookmark-kill-ring]))
1340 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1341 "Mode for listing bookmarks.
1343 \\{eww-bookmark-mode-map}"
1344 (buffer-disable-undo)
1345 (setq buffer-read-only t
1350 (defun eww-save-history ()
1351 (push (list :url eww-current-url
1352 :title eww-current-title
1354 :dom eww-current-dom
1355 :source eww-current-source
1356 :text (buffer-string))
1359 (defun eww-list-histories ()
1360 "List the eww-histories."
1362 (when (null eww-history)
1363 (error "No eww-histories are defined"))
1364 (let ((eww-history-trans eww-history))
1365 (set-buffer (get-buffer-create "*eww history*"))
1367 (let ((inhibit-read-only t)
1370 url title format start)
1372 (dolist (history eww-history-trans)
1373 (setq start (point))
1374 (setq domain-length (max domain-length (length (plist-get history :url))))
1375 (setq title-length (max title-length (length (plist-get history :title)))))
1376 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1378 (concat " " (format format "Title" "URL")))
1379 (dolist (history eww-history-trans)
1380 (setq start (point))
1381 (setq url (plist-get history :url))
1382 (setq title (plist-get history :title))
1383 (insert (format format title url))
1385 (put-text-property start (1+ start) 'eww-history history))
1386 (goto-char (point-min)))
1387 (pop-to-buffer "*eww history*")))
1389 (defun eww-history-browse ()
1390 "Browse the history under point in eww."
1392 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1394 (error "No history on the current line"))
1396 (eww-restore-history history)))
1398 (defvar eww-history-mode-map
1399 (let ((map (make-sparse-keymap)))
1400 (suppress-keymap map)
1401 (define-key map "q" 'quit-window)
1402 (define-key map "\r" 'eww-history-browse)
1403 ;; (define-key map "n" 'next-error-no-select)
1404 ;; (define-key map "p" 'previous-error-no-select)
1406 (easy-menu-define nil map
1407 "Menu for `eww-history-mode-map'."
1409 ["Exit" quit-window t]
1410 ["Browse" eww-history-browse
1411 :active (get-text-property (line-beginning-position) 'eww-history)]))
1414 (define-derived-mode eww-history-mode nil "eww history"
1415 "Mode for listing eww-histories.
1417 \\{eww-history-mode-map}"
1418 (buffer-disable-undo)
1419 (setq buffer-read-only t
1424 ;;; eww.el ends here