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 (eww-update-header-line-format))
355 (defun eww-tag-body (cont)
356 (let* ((start (point))
357 (fgcolor (cdr (or (assq :fgcolor cont
)
359 (bgcolor (cdr (assq :bgcolor cont
)))
360 (shr-stylesheet (list (cons 'color fgcolor
)
361 (cons 'background-color bgcolor
))))
363 (eww-colorize-region start
(point) fgcolor bgcolor
)))
365 (defun eww-colorize-region (start end fg
&optional bg
)
367 (let ((new-colors (shr-color-check fg bg
)))
370 (add-face-text-property start end
371 (list :foreground
(cadr new-colors
))
374 (add-face-text-property start end
375 (list :background
(car new-colors
))
378 (defun eww-display-raw ()
379 (let ((data (buffer-substring (point) (point-max))))
381 (let ((inhibit-read-only t
))
383 (goto-char (point-min))))
385 (defun eww-display-image ()
386 (let ((data (shr-parse-image-data)))
388 (let ((inhibit-read-only t
))
389 (shr-put-image data nil
))
390 (goto-char (point-min))))
392 (defun eww-setup-buffer ()
393 (switch-to-buffer (get-buffer-create "*eww*"))
394 (let ((inhibit-read-only t
))
397 (unless (eq major-mode
'eww-mode
)
399 (setq-local eww-next-url nil
)
400 (setq-local eww-previous-url nil
)
401 (setq-local eww-up-url nil
)
402 (setq-local eww-home-url nil
)
403 (setq-local eww-start-url nil
)
404 (setq-local eww-contents-url nil
))
406 (defun eww-view-source ()
407 "View the HTML source code of the current page."
409 (let ((buf (get-buffer-create "*eww-source*"))
410 (source eww-current-source
))
411 (with-current-buffer buf
412 (delete-region (point-min) (point-max))
413 (insert (or source
"no source"))
414 (goto-char (point-min))
415 (when (fboundp 'html-mode
)
419 (defun eww-readable ()
420 "View the main \"readable\" parts of the current web page.
421 This command uses heuristics to find the parts of the web page that
422 contains the main textual portion, leaving out navigation menus and
425 (let* ((source eww-current-source
)
426 (dom (shr-transform-dom
430 (decode-coding-region (point-min) (point-max) 'utf-8
)
431 (coding-system-error nil
))
432 (libxml-parse-html-region (point-min) (point-max))))))
433 (eww-score-readability dom
)
435 (eww-display-html nil nil
437 (eww-highest-readability dom
)))
438 (setq eww-current-source source
)))
440 (defun eww-score-readability (node)
443 ((memq (car node
) '(script head
))
445 ((eq (car node
) 'meta
)
447 ((eq (car node
) 'img
)
450 (setq score
(- (length (split-string
451 (or (cdr (assoc 'text
(cdr node
))) ""))))))
453 (dolist (elem (cdr node
))
455 ((eq (car elem
) 'text
)
456 (setq score
(+ score
(length (split-string (cdr elem
))))))
459 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
460 (eww-score-readability elem
)))))))))
461 ;; Cache the score of the node to avoid recomputing all the time.
462 (setcdr node
(cons (cons :eww-readability-score score
) (cdr node
)))
465 (defun eww-highest-readability (node)
468 (dolist (elem (cdr node
))
469 (when (and (consp (cdr elem
))
471 :eww-readability-score
473 (eww-highest-readability elem
))))
474 most-negative-fixnum
)
475 (or (cdr (assoc :eww-readability-score
(cdr result
)))
476 most-negative-fixnum
)))
477 (setq result highest
)))
481 (let ((map (make-sparse-keymap)))
482 (suppress-keymap map
)
483 (define-key map
"q" 'quit-window
)
484 (define-key map
"g" 'eww-reload
)
485 (define-key map
[?
\t] 'shr-next-link
)
486 (define-key map
[?\M-
\t] 'shr-previous-link
)
487 (define-key map
[delete] 'scroll-down-command)
488 (define-key map [?\S-\ ] 'scroll-down-command)
489 (define-key map "\177" 'scroll-down-command)
490 (define-key map " " 'scroll-up-command)
491 (define-key map "l" 'eww-back-url)
492 (define-key map "r" 'eww-forward-url)
493 (define-key map "n" 'eww-next-url)
494 (define-key map "p" 'eww-previous-url)
495 (define-key map "u" 'eww-up-url)
496 (define-key map "t" 'eww-top-url)
497 (define-key map "&" 'eww-browse-with-external-browser)
498 (define-key map "d" 'eww-download)
499 (define-key map "w" 'eww-copy-page-url)
500 (define-key map "C" 'url-cookie-list)
501 (define-key map "v" 'eww-view-source)
502 (define-key map "R" 'eww-readable)
503 (define-key map "H" 'eww-list-histories)
505 (define-key map "b" 'eww-add-bookmark)
506 (define-key map "B" 'eww-list-bookmarks)
507 (define-key map [(meta n)] 'eww-next-bookmark)
508 (define-key map [(meta p)] 'eww-previous-bookmark)
510 (easy-menu-define nil map ""
512 ["Exit" quit-window t]
513 ["Close browser" quit-window t]
514 ["Reload" eww-reload t]
515 ["Back to previous page" eww-back-url
516 :active (not (zerop (length eww-history)))]
517 ["Forward to next page" eww-forward-url
518 :active (not (zerop eww-history-position))]
519 ["Browse with external browser" eww-browse-with-external-browser t]
520 ["Download" eww-download t]
521 ["View page source" eww-view-source]
522 ["Copy page URL" eww-copy-page-url t]
523 ["List histories" eww-list-histories t]
524 ["Add bookmark" eww-add-bookmark t]
525 ["List bookmarks" eww-list-bookmarks t]
526 ["List cookies" url-cookie-list t]))
529 (defvar eww-tool-bar-map
530 (let ((map (make-sparse-keymap)))
531 (dolist (tool-bar-item
532 '((quit-window . "close")
533 (eww-reload . "refresh")
534 (eww-back-url . "left-arrow")
535 (eww-forward-url . "right-arrow")
536 (eww-view-source . "show")
537 (eww-copy-page-url . "copy")
538 (eww-add-bookmark . "bookmark_add"))) ;; ...
539 (tool-bar-local-item-from-menu
540 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
542 "Tool bar for `eww-mode'.")
544 (define-derived-mode eww-mode nil "eww"
545 "Mode for browsing the web.
548 ;; FIXME? This seems a strange default.
549 (setq-local eww-current-url 'author)
550 (setq-local eww-current-dom nil)
551 (setq-local eww-current-source nil)
552 (setq-local eww-current-title "")
553 (setq-local browse-url-browser-function 'eww-browse-url)
554 (setq-local after-change-functions 'eww-process-text-input)
555 (setq-local eww-history nil)
556 (setq-local eww-history-position 0)
557 (when (boundp 'tool-bar-map)
558 (setq-local tool-bar-map eww-tool-bar-map))
559 (buffer-disable-undo)
560 ;;(setq buffer-read-only t)
564 (defun eww-browse-url (url &optional _new-window)
565 (when (and (equal major-mode 'eww-mode)
570 (defun eww-back-url ()
571 "Go to the previously displayed page."
573 (when (>= eww-history-position (length eww-history))
574 (user-error "No previous page"))
576 (setq eww-history-position (+ eww-history-position 2))
577 (eww-restore-history (elt eww-history (1- eww-history-position))))
579 (defun eww-forward-url ()
580 "Go to the next displayed page."
582 (when (zerop eww-history-position)
583 (user-error "No next page"))
585 (eww-restore-history (elt eww-history (1- eww-history-position))))
587 (defun eww-restore-history (elem)
588 (let ((inhibit-read-only t))
590 (insert (plist-get elem :text))
591 (setq eww-current-source (plist-get elem :source)
592 eww-current-dom (plist-get elem :dom))
593 (goto-char (plist-get elem :point))
594 (setq eww-current-url (plist-get elem :url)
595 eww-current-title (plist-get elem :title))
596 (eww-update-header-line-format)))
598 (defun eww-next-url ()
599 "Go to the page marked `next'.
600 A page is marked `next' if rel=\"next\" appears in a <link>
604 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
605 (user-error "No `next' on this page")))
607 (defun eww-previous-url ()
608 "Go to the page marked `previous'.
609 A page is marked `previous' if rel=\"previous\" appears in a <link>
613 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
614 (user-error "No `previous' on this page")))
617 "Go to the page marked `up'.
618 A page is marked `up' if rel=\"up\" appears in a <link>
622 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
623 (user-error "No `up' on this page")))
625 (defun eww-top-url ()
626 "Go to the page marked `top'.
627 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
628 appears in a <link> or <a> tag."
630 (let ((best-url (or eww-start-url
634 (eww-browse-url (shr-expand-url best-url eww-current-url))
635 (user-error "No `top' for this page"))))
638 "Reload the current page."
640 (url-retrieve eww-current-url 'eww-render
641 (list eww-current-url (point))))
645 (defvar eww-form nil)
647 (defvar eww-submit-map
648 (let ((map (make-sparse-keymap)))
649 (define-key map "\r" 'eww-submit)
650 (define-key map [(control c) (control c)] 'eww-submit)
653 (defvar eww-checkbox-map
654 (let ((map (make-sparse-keymap)))
655 (define-key map " " 'eww-toggle-checkbox)
656 (define-key map "\r" 'eww-toggle-checkbox)
657 (define-key map [(control c) (control c)] 'eww-submit)
661 (let ((map (make-keymap)))
662 (set-keymap-parent map text-mode-map)
663 (define-key map "\r" 'eww-submit)
664 (define-key map [(control a)] 'eww-beginning-of-text)
665 (define-key map [(control c) (control c)] 'eww-submit)
666 (define-key map [(control e)] 'eww-end-of-text)
667 (define-key map [?\t] 'shr-next-link)
668 (define-key map [?\M-\t] 'shr-previous-link)
671 (defvar eww-textarea-map
672 (let ((map (make-keymap)))
673 (set-keymap-parent map text-mode-map)
674 (define-key map "\r" 'forward-line)
675 (define-key map [(control c) (control c)] 'eww-submit)
676 (define-key map [?\t] 'shr-next-link)
677 (define-key map [?\M-\t] 'shr-previous-link)
680 (defvar eww-select-map
681 (let ((map (make-sparse-keymap)))
682 (define-key map "\r" 'eww-change-select)
683 (define-key map [(control c) (control c)] 'eww-submit)
686 (defun eww-beginning-of-text ()
687 "Move to the start of the input field."
689 (goto-char (eww-beginning-of-field)))
691 (defun eww-end-of-text ()
692 "Move to the end of the text in the input field."
694 (goto-char (eww-end-of-field))
695 (let ((start (eww-beginning-of-field)))
696 (while (and (equal (following-char) ? )
699 (when (> (point) start)
702 (defun eww-beginning-of-field ()
706 ((not (eq (get-text-property (point) 'eww-form)
707 (get-text-property (1- (point)) 'eww-form)))
710 (previous-single-property-change
711 (point) 'eww-form nil (point-min)))))
713 (defun eww-end-of-field ()
714 (1- (next-single-property-change
715 (point) 'eww-form nil (point-max))))
717 (defun eww-tag-form (cont)
719 (list (assq :method cont)
720 (assq :action cont)))
722 (shr-ensure-paragraph)
727 (when (> (point) start)
728 (put-text-property start (1+ start)
729 'eww-form eww-form))))
731 (defun eww-form-submit (cont)
732 (let ((start (point))
733 (value (cdr (assq :value cont))))
735 (if (zerop (length value))
739 (add-face-text-property start (point) 'eww-form-submit)
740 (put-text-property start (point) 'eww-form
741 (list :eww-form eww-form
744 :name (cdr (assq :name cont))))
745 (put-text-property start (point) 'keymap eww-submit-map)
748 (defun eww-form-checkbox (cont)
749 (let ((start (point)))
750 (if (cdr (assq :checked cont))
751 (insert eww-form-checkbox-selected-symbol)
752 (insert eww-form-checkbox-symbol))
753 (add-face-text-property start (point) 'eww-form-checkbox)
754 (put-text-property start (point) 'eww-form
755 (list :eww-form eww-form
756 :value (cdr (assq :value cont))
757 :type (downcase (cdr (assq :type cont)))
758 :checked (cdr (assq :checked cont))
759 :name (cdr (assq :name cont))))
760 (put-text-property start (point) 'keymap eww-checkbox-map)
763 (defun eww-form-text (cont)
764 (let ((start (point))
765 (type (downcase (or (cdr (assq :type cont))
767 (value (or (cdr (assq :value cont)) ""))
768 (width (string-to-number
769 (or (cdr (assq :size cont))
771 (readonly-property (if (or (cdr (assq :disabled cont))
772 (cdr (assq :readonly cont)))
774 'inhibit-read-only)))
776 (when (< (length value) width)
777 (insert (make-string (- width (length value)) ? )))
778 (put-text-property start (point) 'face 'eww-form-text)
779 (put-text-property start (point) 'local-map eww-text-map)
780 (put-text-property start (point) readonly-property t)
781 (put-text-property start (point) 'eww-form
782 (list :eww-form eww-form
785 :name (cdr (assq :name cont))))
788 (defconst eww-text-input-types '("text" "password" "textarea"
789 "color" "date" "datetime" "datetime-local"
790 "email" "month" "number" "search" "tel"
792 "List of input types which represent a text input.
793 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
795 (defun eww-process-text-input (beg end length)
796 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
797 (properties (text-properties-at end))
798 (type (plist-get form :type)))
800 (member type eww-text-input-types))
803 ;; Delete some space at the end.
806 (if (equal type "textarea")
807 (1- (line-end-position))
809 (let ((new (- end beg)))
810 (while (and (> new 0)
811 (eql (following-char) ? ))
812 (delete-region (point) (1+ (point)))
813 (setq new (1- new))))
814 (set-text-properties beg end properties)))
819 (if (equal type "textarea")
820 (1- (line-end-position))
822 (let ((start (point)))
823 (insert (make-string length ? ))
824 (set-text-properties start (point) properties)))))
825 (let ((value (buffer-substring-no-properties
826 (eww-beginning-of-field)
827 (eww-end-of-field))))
828 (when (string-match " +\\'" value)
829 (setq value (substring value 0 (match-beginning 0))))
830 (plist-put form :value value)
831 (when (equal type "password")
832 ;; Display passwords as asterisks.
833 (let ((start (eww-beginning-of-field)))
834 (put-text-property start (+ start (length value))
835 'display (make-string (length value) ?*))))))))
837 (defun eww-tag-textarea (cont)
838 (let ((start (point))
839 (value (or (cdr (assq :value cont)) ""))
840 (lines (string-to-number
841 (or (cdr (assq :rows cont))
843 (width (string-to-number
844 (or (cdr (assq :cols cont))
850 (when (< (count-lines start (point)) lines)
851 (dotimes (i (- lines (count-lines start (point))))
853 (setq end (point-marker))
855 (while (< (point) end)
857 (let ((pad (- width (- (point) (line-beginning-position)))))
859 (insert (make-string pad ? ))))
860 (add-face-text-property (line-beginning-position)
861 (point) 'eww-form-textarea)
862 (put-text-property (line-beginning-position) (point)
863 'local-map eww-textarea-map)
865 (put-text-property start (point) 'eww-form
866 (list :eww-form eww-form
869 :name (cdr (assq :name cont))))))
871 (defun eww-tag-input (cont)
872 (let ((type (downcase (or (cdr (assq :type cont))
876 ((or (equal type "checkbox")
877 (equal type "radio"))
878 (eww-form-checkbox cont))
879 ((equal type "submit")
880 (eww-form-submit cont))
881 ((equal type "hidden")
882 (let ((form eww-form)
883 (name (cdr (assq :name cont))))
884 ;; Don't add <input type=hidden> elements repeatedly.
886 (or (not (consp (car form)))
887 (not (eq (caar form) 'hidden))
888 (not (equal (plist-get (cdr (car form)) :name)
890 (setq form (cdr form)))
892 (nconc eww-form (list
895 :value (cdr (assq :value cont))))))))
897 (eww-form-text cont)))
898 (unless (= start (point))
899 (put-text-property start (1+ start) 'help-echo "Input field"))))
901 (defun eww-tag-select (cont)
902 (shr-ensure-paragraph)
903 (let ((menu (list :name (cdr (assq :name cont))
909 (if (eq (car (car cont)) 'optgroup)
910 (dolist (groupelem cont)
911 (unless (cdr (assq :disabled (cdr groupelem)))
912 (setq opelem (append opelem (cdr (cdr groupelem))))))
914 (dolist (elem opelem)
915 (when (eq (car elem) 'option)
916 (when (cdr (assq :selected (cdr elem)))
917 (nconc menu (list :value
918 (cdr (assq :value (cdr elem))))))
919 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
920 (setq max (max max (length display)))
922 :value (cdr (assq :value (cdr elem)))
926 (setq options (nreverse options))
927 ;; If we have no selected values, default to the first value.
928 (unless (plist-get menu :value)
929 (nconc menu (list :value (nth 2 (car options)))))
931 (let ((selected (eww-select-display menu)))
933 (make-string (- max (length selected)) ? )))
934 (put-text-property start (point) 'eww-form menu)
935 (add-face-text-property start (point) 'eww-form-select)
936 (put-text-property start (point) 'keymap eww-select-map)
937 (unless (= start (point))
938 (put-text-property start (1+ start) 'help-echo "select field"))
939 (shr-ensure-paragraph))))
941 (defun eww-select-display (select)
942 (let ((value (plist-get select :value))
944 (dolist (elem select)
945 (when (and (consp elem)
946 (eq (car elem) 'item)
947 (equal value (plist-get (cdr elem) :value)))
948 (setq display (plist-get (cdr elem) :display))))
951 (defun eww-change-select ()
952 "Change the value of the select drop-down menu under point."
954 (let* ((input (get-text-property (point) 'eww-form))
955 (completion-ignore-case t)
958 (mapcar (lambda (elem)
960 (eq (car elem) 'item)
961 (cons (plist-get (cdr elem) :display)
962 (plist-get (cdr elem) :value))))
965 (completing-read "Change value: " options nil 'require-match))
966 (inhibit-read-only t))
967 (plist-put input :value (cdr (assoc-string display options t)))
969 (eww-update-field display))))
971 (defun eww-update-field (string)
972 (let ((properties (text-properties-at (point)))
973 (start (eww-beginning-of-field))
974 (end (1+ (eww-end-of-field))))
975 (delete-region start end)
977 (make-string (- (- end start) (length string)) ? ))
978 (set-text-properties start end properties)
981 (defun eww-toggle-checkbox ()
982 "Toggle the value of the checkbox under point."
984 (let* ((input (get-text-property (point) 'eww-form))
985 (type (plist-get input :type)))
986 (if (equal type "checkbox")
989 (if (plist-get input :checked)
991 (plist-put input :checked nil)
992 (eww-update-field eww-form-checkbox-symbol))
993 (plist-put input :checked t)
994 (eww-update-field eww-form-checkbox-selected-symbol))))
995 ;; Radio button. Switch all other buttons off.
996 (let ((name (plist-get input :name)))
998 (dolist (elem (eww-inputs (plist-get input :eww-form)))
999 (when (equal (plist-get (cdr elem) :name) name)
1000 (goto-char (car elem))
1001 (if (not (eq (cdr elem) input))
1003 (plist-put input :checked nil)
1004 (eww-update-field eww-form-checkbox-symbol))
1005 (plist-put input :checked t)
1006 (eww-update-field eww-form-checkbox-selected-symbol)))))
1007 (forward-char 1)))))
1009 (defun eww-inputs (form)
1010 (let ((start (point-min))
1013 (< start (point-max)))
1014 (when (or (get-text-property start 'eww-form)
1015 (setq start (next-single-property-change start 'eww-form)))
1016 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1018 (push (cons start (get-text-property start 'eww-form))
1020 (setq start (next-single-property-change start 'eww-form))))
1023 (defun eww-input-value (input)
1024 (let ((type (plist-get input :type))
1025 (value (plist-get input :value)))
1027 ((equal type "textarea")
1030 (goto-char (point-min))
1031 (while (re-search-forward "^ +\n\\| +$" nil t)
1032 (replace-match "" t t))
1035 (if (string-match " +\\'" value)
1036 (substring value 0 (match-beginning 0))
1039 (defun eww-submit ()
1040 "Submit the current form."
1042 (let* ((this-input (get-text-property (point) 'eww-form))
1043 (form (plist-get this-input :eww-form))
1045 (dolist (elem (sort (eww-inputs form)
1047 (< (car o1) (car o2)))))
1048 (let* ((input (cdr elem))
1049 (input-start (car elem))
1050 (name (plist-get input :name)))
1053 ((member (plist-get input :type) '("checkbox" "radio"))
1054 (when (plist-get input :checked)
1055 (push (cons name (plist-get input :value))
1057 ((equal (plist-get input :type) "submit")
1058 ;; We want the values from buttons if we hit a button if
1059 ;; we hit enter on it, or if it's the first button after
1060 ;; the field we did hit return on.
1061 (when (or (eq input this-input)
1062 (and (not (eq input this-input))
1064 (> input-start (point))))
1065 (setq next-submit t)
1066 (push (cons name (plist-get input :value))
1069 (push (cons name (eww-input-value input))
1072 (when (and (consp elem)
1073 (eq (car elem) 'hidden))
1074 (push (cons (plist-get (cdr elem) :name)
1075 (or (plist-get (cdr elem) :value) ""))
1077 (if (and (stringp (cdr (assq :method form)))
1078 (equal (downcase (cdr (assq :method form))) "post"))
1079 (let ((url-request-method "POST")
1080 (url-request-extra-headers
1081 '(("Content-Type" . "application/x-www-form-urlencoded")))
1082 (url-request-data (mm-url-encode-www-form-urlencoded values)))
1083 (eww-browse-url (shr-expand-url (cdr (assq :action form))
1087 (if (cdr (assq :action form))
1088 (shr-expand-url (cdr (assq :action form))
1092 (mm-url-encode-www-form-urlencoded values))))))
1094 (defun eww-browse-with-external-browser (&optional url)
1095 "Browse the current URL with an external browser.
1096 The browser to used is specified by the `shr-external-browser' variable."
1098 (funcall shr-external-browser (or url eww-current-url)))
1100 (defun eww-follow-link (&optional external mouse-event)
1101 "Browse the URL under point.
1102 If EXTERNAL, browse the URL using `shr-external-browser'."
1103 (interactive (list current-prefix-arg last-nonmenu-event))
1104 (mouse-set-point mouse-event)
1105 (let ((url (get-text-property (point) 'shr-url)))
1108 (message "No link under point"))
1109 ((string-match "^mailto:" url)
1110 (browse-url-mail url))
1112 (funcall shr-external-browser url))
1113 ;; This is a #target url in the same page as the current one.
1114 ((and (url-target (url-generic-parse-url url))
1115 (eww-same-page-p url eww-current-url))
1117 (eww-display-html 'utf-8 url eww-current-dom))
1119 (eww-browse-url url)))))
1121 (defun eww-same-page-p (url1 url2)
1122 "Return non-nil if both URLs represent the same page.
1123 Differences in #targets are ignored."
1124 (let ((obj1 (url-generic-parse-url url1))
1125 (obj2 (url-generic-parse-url url2)))
1126 (setf (url-target obj1) nil)
1127 (setf (url-target obj2) nil)
1128 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1130 (defun eww-copy-page-url ()
1132 (message "%s" eww-current-url)
1133 (kill-new eww-current-url))
1135 (defun eww-download ()
1136 "Download URL under point to `eww-download-directory'."
1138 (let ((url (get-text-property (point) 'shr-url)))
1140 (message "No URL under point")
1141 (url-retrieve url 'eww-download-callback (list url)))))
1143 (defun eww-download-callback (status url)
1144 (unless (plist-get status :error)
1145 (let* ((obj (url-generic-parse-url url))
1146 (path (car (url-path-and-query obj)))
1147 (file (eww-make-unique-file-name (file-name-nondirectory path)
1148 eww-download-directory)))
1150 (message "Saved %s" file))))
1152 (defun eww-make-unique-file-name (file directory)
1154 ((zerop (length file))
1156 ((string-match "\\`[.]" file)
1157 (setq file (concat "!" file))))
1159 (while (file-exists-p (expand-file-name file directory))
1161 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1162 (format "%s(%d)%s" (match-string 1 file)
1163 count (match-string 2 file))
1164 (format "%s(%d)" file count)))
1165 (setq count (1+ count)))
1166 (expand-file-name file directory)))
1170 (defvar eww-bookmarks nil)
1172 (defun eww-add-bookmark ()
1173 "Add the current page to the bookmarks."
1175 (eww-read-bookmarks)
1176 (dolist (bookmark eww-bookmarks)
1177 (when (equal eww-current-url
1178 (plist-get bookmark :url))
1179 (user-error "Already bookmarked")))
1180 (if (y-or-n-p "bookmark this page? ")
1182 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1183 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1184 (push (list :url eww-current-url
1186 :time (current-time-string))
1188 (eww-write-bookmarks)
1189 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1191 (defun eww-write-bookmarks ()
1192 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1193 (insert ";; Auto-generated file; don't edit\n")
1194 (pp eww-bookmarks (current-buffer))))
1196 (defun eww-read-bookmarks ()
1197 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1199 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1201 (insert-file-contents file)
1202 (read (current-buffer)))))))
1204 (defun eww-list-bookmarks ()
1205 "Display the bookmarks."
1207 (eww-bookmark-prepare)
1208 (pop-to-buffer "*eww bookmarks*"))
1210 (defun eww-bookmark-prepare ()
1211 (eww-read-bookmarks)
1212 (unless eww-bookmarks
1213 (user-error "No bookmarks are defined"))
1214 (set-buffer (get-buffer-create "*eww bookmarks*"))
1216 (let ((format "%-40s %s")
1217 (inhibit-read-only t)
1220 (setq header-line-format (concat " " (format format "URL" "Title")))
1221 (dolist (bookmark eww-bookmarks)
1222 (setq start (point))
1223 (setq url (plist-get bookmark :url))
1224 (when (> (length url) 40)
1225 (setq url (substring url 0 40)))
1226 (insert (format format url
1227 (plist-get bookmark :title))
1229 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1230 (goto-char (point-min))))
1232 (defvar eww-bookmark-kill-ring nil)
1234 (defun eww-bookmark-kill ()
1235 "Kill the current bookmark."
1237 (let* ((start (line-beginning-position))
1238 (bookmark (get-text-property start 'eww-bookmark))
1239 (inhibit-read-only t))
1241 (user-error "No bookmark on the current line"))
1243 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1244 (delete-region start (point))
1245 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1246 (eww-write-bookmarks)))
1248 (defun eww-bookmark-yank ()
1249 "Yank a previously killed bookmark to the current line."
1251 (unless eww-bookmark-kill-ring
1252 (user-error "No previously killed bookmark"))
1254 (let ((inhibit-read-only t)
1257 (insert (pop eww-bookmark-kill-ring))
1258 (setq bookmark (get-text-property start 'eww-bookmark))
1259 (if (= start (point-min))
1260 (push bookmark eww-bookmarks)
1261 (let ((line (count-lines start (point))))
1262 (setcdr (nthcdr (1- line) eww-bookmarks)
1263 (cons bookmark (nthcdr line eww-bookmarks)))))
1264 (eww-write-bookmarks)))
1266 (defun eww-bookmark-browse ()
1267 "Browse the bookmark under point in eww."
1269 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1271 (user-error "No bookmark on the current line"))
1273 (eww-browse-url (plist-get bookmark :url))))
1275 (defun eww-next-bookmark ()
1276 "Go to the next bookmark in the list."
1280 (unless (get-buffer "*eww bookmarks*")
1282 (eww-bookmark-prepare))
1283 (with-current-buffer (get-buffer "*eww bookmarks*")
1284 (when (and (not first)
1287 (setq bookmark (get-text-property (line-beginning-position)
1290 (user-error "No next bookmark")))
1291 (eww-browse-url (plist-get bookmark :url))))
1293 (defun eww-previous-bookmark ()
1294 "Go to the previous bookmark in the list."
1298 (unless (get-buffer "*eww bookmarks*")
1300 (eww-bookmark-prepare))
1301 (with-current-buffer (get-buffer "*eww bookmarks*")
1303 (goto-char (point-max))
1304 (beginning-of-line))
1305 ;; On the final line.
1309 (user-error "No previous bookmark")
1311 (setq bookmark (get-text-property (line-beginning-position)
1313 (eww-browse-url (plist-get bookmark :url))))
1315 (defvar eww-bookmark-mode-map
1316 (let ((map (make-sparse-keymap)))
1317 (suppress-keymap map)
1318 (define-key map "q" 'quit-window)
1319 (define-key map [(control k)] 'eww-bookmark-kill)
1320 (define-key map [(control y)] 'eww-bookmark-yank)
1321 (define-key map "\r" 'eww-bookmark-browse)
1323 (easy-menu-define nil map
1324 "Menu for `eww-bookmark-mode-map'."
1326 ["Exit" quit-window t]
1327 ["Browse" eww-bookmark-browse
1328 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1329 ["Kill" eww-bookmark-kill
1330 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1331 ["Yank" eww-bookmark-yank
1332 :active eww-bookmark-kill-ring]))
1335 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1336 "Mode for listing bookmarks.
1338 \\{eww-bookmark-mode-map}"
1339 (buffer-disable-undo)
1340 (setq buffer-read-only t
1345 (defun eww-save-history ()
1346 (push (list :url eww-current-url
1347 :title eww-current-title
1349 :dom eww-current-dom
1350 :source eww-current-source
1351 :text (buffer-string))
1354 (defun eww-list-histories ()
1355 "List the eww-histories."
1357 (when (null eww-history)
1358 (error "No eww-histories are defined"))
1359 (let ((eww-history-trans eww-history))
1360 (set-buffer (get-buffer-create "*eww history*"))
1362 (let ((inhibit-read-only t)
1365 url title format start)
1367 (dolist (history eww-history-trans)
1368 (setq start (point))
1369 (setq domain-length (max domain-length (length (plist-get history :url))))
1370 (setq title-length (max title-length (length (plist-get history :title)))))
1371 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1373 (concat " " (format format "Title" "URL")))
1374 (dolist (history eww-history-trans)
1375 (setq start (point))
1376 (setq url (plist-get history :url))
1377 (setq title (plist-get history :title))
1378 (insert (format format title url))
1380 (put-text-property start (1+ start) 'eww-history history))
1381 (goto-char (point-min)))
1382 (pop-to-buffer "*eww history*")))
1384 (defun eww-history-browse ()
1385 "Browse the history under point in eww."
1387 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1389 (error "No history on the current line"))
1391 (eww-restore-history history)))
1393 (defvar eww-history-mode-map
1394 (let ((map (make-sparse-keymap)))
1395 (suppress-keymap map)
1396 (define-key map "q" 'quit-window)
1397 (define-key map "\r" 'eww-history-browse)
1398 ;; (define-key map "n" 'next-error-no-select)
1399 ;; (define-key map "p" 'previous-error-no-select)
1401 (easy-menu-define nil map
1402 "Menu for `eww-history-mode-map'."
1404 ["Exit" quit-window t]
1405 ["Browse" eww-history-browse
1406 :active (get-text-property (line-beginning-position) 'eww-history)]))
1409 (define-derived-mode eww-history-mode nil "eww history"
1410 "Mode for listing eww-histories.
1412 \\{eww-history-mode-map}"
1413 (buffer-disable-undo)
1414 (setq buffer-read-only t
1419 ;;; eww.el ends here