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
)
37 :link
'(custom-manual "(eww) Top")
41 (defcustom eww-header-line-format
"%t: %u"
43 - %t is replaced by the title.
44 - %u is replaced by the URL."
49 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
50 "Prefix URL to search engine"
55 (defcustom eww-download-directory
"~/Downloads/"
56 "Directory where files will downloaded."
61 (defcustom eww-bookmarks-directory user-emacs-directory
62 "Directory where bookmark files will be stored."
67 (defcustom eww-use-external-browser-for-content-type
68 "\\`\\(video/\\|audio/\\|application/ogg\\)"
69 "Always use external browser for specified content-type."
72 :type
'(choice (const :tag
"Never" nil
)
75 (defcustom eww-form-checkbox-selected-symbol
"[X]"
76 "Symbol used to represent a selected checkbox.
77 See also `eww-form-checkbox-symbol'."
80 :type
'(choice (const "[X]")
81 (const "☒") ; Unicode BALLOT BOX WITH X
82 (const "☑") ; Unicode BALLOT BOX WITH CHECK
85 (defcustom eww-form-checkbox-symbol
"[ ]"
86 "Symbol used to represent a checkbox.
87 See also `eww-form-checkbox-selected-symbol'."
90 :type
'(choice (const "[ ]")
91 (const "☐") ; Unicode BALLOT BOX
94 (defface eww-form-submit
95 '((((type x w32 ns
) (class color
)) ; Like default mode line
96 :box
(:line-width
2 :style released-button
)
97 :background
"#808080" :foreground
"black"))
98 "Face for eww buffer buttons."
102 (defface eww-form-checkbox
103 '((((type x w32 ns
) (class color
)) ; Like default mode line
104 :box
(:line-width
2 :style released-button
)
105 :background
"lightgrey" :foreground
"black"))
106 "Face for eww buffer buttons."
110 (defface eww-form-select
111 '((((type x w32 ns
) (class color
)) ; Like default mode line
112 :box
(:line-width
2 :style released-button
)
113 :background
"lightgrey" :foreground
"black"))
114 "Face for eww buffer buttons."
118 (defface eww-form-text
119 '((t (:background
"#505050"
121 :box
(:line-width
1))))
122 "Face for eww text inputs."
126 (defface eww-form-textarea
127 '((t (:background
"#C0C0C0"
129 :box
(:line-width
1))))
130 "Face for eww textarea inputs."
134 (defvar eww-current-url nil
)
135 (defvar eww-current-dom nil
)
136 (defvar eww-current-source nil
)
137 (defvar eww-current-title
""
138 "Title of current page.")
139 (defvar eww-history nil
)
140 (defvar eww-history-position
0)
142 (defvar eww-next-url nil
)
143 (defvar eww-previous-url nil
)
144 (defvar eww-up-url nil
)
145 (defvar eww-home-url nil
)
146 (defvar eww-start-url nil
)
147 (defvar eww-contents-url nil
)
149 (defvar eww-local-regex
"localhost"
150 "When this regex is found in the URL, it's not a keyword but an address.")
152 (defvar eww-link-keymap
153 (let ((map (copy-keymap shr-map
)))
154 (define-key map
"\r" 'eww-follow-link
)
159 "Fetch URL and render the page.
160 If the input doesn't look like an URL or a domain name, the
161 word(s) will be searched for via `eww-search-prefix'."
162 (interactive "sEnter URL or keywords: ")
163 (cond ((string-match-p "\\`file:/" url
))
164 ((string-match-p "\\`ftp://" url
)
165 (user-error "FTP is not supported."))
167 (if (and (= (length (split-string url
)) 1)
168 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
169 (> (length (split-string url
"[.:]")) 1))
170 (string-match eww-local-regex url
)))
172 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
173 (setq url
(concat "http://" url
)))
174 ;; some site don't redirect final /
175 (when (string= (url-filename (url-generic-parse-url url
)) "")
176 (setq url
(concat url
"/"))))
177 (setq url
(concat eww-search-prefix
178 (replace-regexp-in-string " " "+" url
))))))
179 (url-retrieve url
'eww-render
(list url
)))
181 ;;;###autoload (defalias 'browse-web 'eww)
184 (defun eww-open-file (file)
185 "Render a file using EWW."
186 (interactive "fFile: ")
187 (eww (concat "file://"
188 (and (memq system-type
'(windows-nt ms-dos
))
190 (expand-file-name file
))))
192 (defun eww-render (status url
&optional point
)
193 (let ((redirect (plist-get status
:redirect
)))
195 (setq url redirect
)))
196 (let* ((headers (eww-parse-headers))
198 (mail-header-parse-content-type
199 (or (cdr (assoc "content-type" headers
))
203 (or (cdr (assq 'charset
(cdr content-type
)))
204 (eww-detect-charset (equal (car content-type
)
207 (data-buffer (current-buffer)))
210 (setq eww-current-title
"")
212 ((and eww-use-external-browser-for-content-type
213 (string-match-p eww-use-external-browser-for-content-type
215 (eww-browse-with-external-browser url
))
216 ((equal (car content-type
) "text/html")
217 (eww-display-html charset url nil point
))
218 ((equal (car content-type
) "application/pdf")
220 ((string-match-p "\\`image/" (car content-type
))
222 (eww-update-header-line-format))
225 (eww-update-header-line-format)))
226 (setq eww-current-url url
227 eww-history-position
0))
228 (kill-buffer data-buffer
))))
230 (defun eww-parse-headers ()
232 (goto-char (point-min))
233 (while (and (not (eobp))
235 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
236 (push (cons (downcase (match-string 1))
244 (defun eww-detect-charset (html-p)
245 (let ((case-fold-search t
)
249 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
253 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
256 (declare-function libxml-parse-html-region
"xml.c"
257 (start end
&optional base-url
))
259 (defun eww-display-html (charset url
&optional document point
)
260 (or (fboundp 'libxml-parse-html-region
)
261 (error "This function requires Emacs to be compiled with libxml2"))
262 ;; There should be a better way to abort loading images
268 'base
(list (cons 'href url
))
270 (unless (eq charset
'utf-8
)
272 (decode-coding-region (point) (point-max) charset
)
273 (coding-system-error nil
)))
274 (libxml-parse-html-region (point) (point-max))))))
275 (source (and (null document
)
276 (buffer-substring (point) (point-max)))))
278 (setq eww-current-source source
279 eww-current-dom document
)
280 (let ((inhibit-read-only t
)
281 (after-change-functions nil
)
282 (shr-target-id (url-target (url-generic-parse-url url
)))
283 (shr-external-rendering-functions
284 '((title . eww-tag-title
)
285 (form . eww-tag-form
)
286 (input . eww-tag-input
)
287 (textarea . eww-tag-textarea
)
288 (body . eww-tag-body
)
289 (select . eww-tag-select
)
290 (link . eww-tag-link
)
292 (shr-insert-document document
)
297 (goto-char (point-min))
298 (let ((point (next-single-property-change
299 (point-min) 'shr-target-id
)))
303 (goto-char (point-min))
304 ;; Don't leave point inside forms, because the normal eww
305 ;; commands aren't available there.
306 (while (and (not (eobp))
307 (get-text-property (point) 'eww-form
))
309 (setq eww-current-url url
310 eww-history-position
0)
311 (eww-update-header-line-format)))
313 (defun eww-handle-link (cont)
314 (let* ((rel (assq :rel cont
))
315 (href (assq :href cont
))
317 ;; The text associated with :rel is case-insensitive.
318 (if rel
(downcase (cdr rel
)))
319 '(("next" . eww-next-url
)
320 ;; Texinfo uses "previous", but HTML specifies
321 ;; "prev", so recognize both.
322 ("previous" . eww-previous-url
)
323 ("prev" . eww-previous-url
)
324 ;; HTML specifies "start" but also "contents",
325 ;; and Gtk seems to use "home". Recognize
326 ;; them all; but store them in different
327 ;; variables so that we can readily choose the
329 ("start" . eww-start-url
)
330 ("home" . eww-home-url
)
331 ("contents" . eww-contents-url
)
332 ("up" . eww-up-url
)))))
335 (set (cdr where
) (cdr href
)))))
337 (defun eww-tag-link (cont)
338 (eww-handle-link cont
)
341 (defun eww-tag-a (cont)
342 (eww-handle-link cont
)
343 (let ((start (point)))
345 (put-text-property start
(point) 'keymap eww-link-keymap
)))
347 (defun eww-update-header-line-format ()
348 (if eww-header-line-format
349 (setq header-line-format
350 (replace-regexp-in-string
352 ;; FIXME? Title can be blank. Default to, eg, last component
354 (format-spec eww-header-line-format
355 `((?u .
,eww-current-url
)
356 (?t .
,eww-current-title
)))))
357 (setq header-line-format nil
)))
359 (defun eww-tag-title (cont)
360 (setq eww-current-title
"")
362 (when (eq (car sub
) 'text
)
363 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
364 (setq eww-current-title
365 (replace-regexp-in-string
367 (replace-regexp-in-string "[ \t\r\n]+" " " eww-current-title
)))
368 (eww-update-header-line-format))
370 (defun eww-tag-body (cont)
371 (let* ((start (point))
372 (fgcolor (cdr (or (assq :fgcolor cont
)
374 (bgcolor (cdr (assq :bgcolor cont
)))
375 (shr-stylesheet (list (cons 'color fgcolor
)
376 (cons 'background-color bgcolor
))))
378 (shr-colorize-region start
(point) fgcolor bgcolor
)))
380 (defun eww-display-raw ()
381 (let ((data (buffer-substring (point) (point-max))))
383 (let ((inhibit-read-only t
))
385 (goto-char (point-min))))
387 (defun eww-display-image ()
388 (let ((data (shr-parse-image-data)))
390 (let ((inhibit-read-only t
))
391 (shr-put-image data nil
))
392 (goto-char (point-min))))
394 (defun eww-display-pdf ()
395 (let ((data (buffer-substring (point) (point-max))))
396 (switch-to-buffer (get-buffer-create "*eww pdf*"))
397 (let ((coding-system-for-write 'raw-text
)
398 (inhibit-read-only t
))
402 (goto-char (point-min)))
404 (defun eww-setup-buffer ()
405 (switch-to-buffer (get-buffer-create "*eww*"))
406 (let ((inhibit-read-only t
))
409 (unless (eq major-mode
'eww-mode
)
411 (setq-local eww-next-url nil
)
412 (setq-local eww-previous-url nil
)
413 (setq-local eww-up-url nil
)
414 (setq-local eww-home-url nil
)
415 (setq-local eww-start-url nil
)
416 (setq-local eww-contents-url nil
))
418 (defun eww-view-source ()
419 "View the HTML source code of the current page."
421 (let ((buf (get-buffer-create "*eww-source*"))
422 (source eww-current-source
))
423 (with-current-buffer buf
424 (delete-region (point-min) (point-max))
425 (insert (or source
"no source"))
426 (goto-char (point-min))
427 (when (fboundp 'html-mode
)
431 (defun eww-readable ()
432 "View the main \"readable\" parts of the current web page.
433 This command uses heuristics to find the parts of the web page that
434 contains the main textual portion, leaving out navigation menus and
437 (let* ((source eww-current-source
)
438 (dom (shr-transform-dom
442 (decode-coding-region (point-min) (point-max) 'utf-8
)
443 (coding-system-error nil
))
444 (libxml-parse-html-region (point-min) (point-max))))))
445 (eww-score-readability dom
)
447 (eww-display-html nil nil
449 (eww-highest-readability dom
)))
450 (setq eww-current-source source
)))
452 (defun eww-score-readability (node)
455 ((memq (car node
) '(script head
))
457 ((eq (car node
) 'meta
)
459 ((eq (car node
) 'img
)
462 (setq score
(- (length (split-string
463 (or (cdr (assoc 'text
(cdr node
))) ""))))))
465 (dolist (elem (cdr node
))
467 ((and (stringp (cdr elem
))
468 (eq (car elem
) 'text
))
469 (setq score
(+ score
(length (split-string (cdr elem
))))))
472 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
473 (eww-score-readability elem
)))))))))
474 ;; Cache the score of the node to avoid recomputing all the time.
475 (setcdr node
(cons (cons :eww-readability-score score
) (cdr node
)))
478 (defun eww-highest-readability (node)
481 (dolist (elem (cdr node
))
482 (when (and (consp (cdr elem
))
484 :eww-readability-score
486 (eww-highest-readability elem
))))
487 most-negative-fixnum
)
488 (or (cdr (assoc :eww-readability-score
(cdr result
)))
489 most-negative-fixnum
)))
490 (setq result highest
)))
494 (let ((map (make-sparse-keymap)))
495 (suppress-keymap map
)
496 (define-key map
"q" 'quit-window
)
497 (define-key map
"g" 'eww-reload
)
498 (define-key map
[?
\t] 'shr-next-link
)
499 (define-key map
[?\M-
\t] 'shr-previous-link
)
500 (define-key map
[delete] 'scroll-down-command)
501 (define-key map [?\S-\ ] 'scroll-down-command)
502 (define-key map "\177" 'scroll-down-command)
503 (define-key map " " 'scroll-up-command)
504 (define-key map "l" 'eww-back-url)
505 (define-key map "r" 'eww-forward-url)
506 (define-key map "n" 'eww-next-url)
507 (define-key map "p" 'eww-previous-url)
508 (define-key map "u" 'eww-up-url)
509 (define-key map "t" 'eww-top-url)
510 (define-key map "&" 'eww-browse-with-external-browser)
511 (define-key map "d" 'eww-download)
512 (define-key map "w" 'eww-copy-page-url)
513 (define-key map "C" 'url-cookie-list)
514 (define-key map "v" 'eww-view-source)
515 (define-key map "R" 'eww-readable)
516 (define-key map "H" 'eww-list-histories)
518 (define-key map "b" 'eww-add-bookmark)
519 (define-key map "B" 'eww-list-bookmarks)
520 (define-key map [(meta n)] 'eww-next-bookmark)
521 (define-key map [(meta p)] 'eww-previous-bookmark)
523 (easy-menu-define nil map ""
525 ["Exit" quit-window t]
526 ["Close browser" quit-window t]
527 ["Reload" eww-reload t]
528 ["Back to previous page" eww-back-url
529 :active (not (zerop (length eww-history)))]
530 ["Forward to next page" eww-forward-url
531 :active (not (zerop eww-history-position))]
532 ["Browse with external browser" eww-browse-with-external-browser t]
533 ["Download" eww-download t]
534 ["View page source" eww-view-source]
535 ["Copy page URL" eww-copy-page-url t]
536 ["List histories" eww-list-histories t]
537 ["Add bookmark" eww-add-bookmark t]
538 ["List bookmarks" eww-list-bookmarks t]
539 ["List cookies" url-cookie-list t]))
542 (defvar eww-tool-bar-map
543 (let ((map (make-sparse-keymap)))
544 (dolist (tool-bar-item
545 '((quit-window . "close")
546 (eww-reload . "refresh")
547 (eww-back-url . "left-arrow")
548 (eww-forward-url . "right-arrow")
549 (eww-view-source . "show")
550 (eww-copy-page-url . "copy")
551 (eww-add-bookmark . "bookmark_add"))) ;; ...
552 (tool-bar-local-item-from-menu
553 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
555 "Tool bar for `eww-mode'.")
557 (define-derived-mode eww-mode nil "eww"
558 "Mode for browsing the web.
561 ;; FIXME? This seems a strange default.
562 (setq-local eww-current-url 'author)
563 (setq-local eww-current-dom nil)
564 (setq-local eww-current-source nil)
565 (setq-local eww-current-title "")
566 (setq-local browse-url-browser-function 'eww-browse-url)
567 (setq-local after-change-functions 'eww-process-text-input)
568 (setq-local eww-history nil)
569 (setq-local eww-history-position 0)
570 (when (boundp 'tool-bar-map)
571 (setq-local tool-bar-map eww-tool-bar-map))
572 (buffer-disable-undo)
573 ;;(setq buffer-read-only t)
577 (defun eww-browse-url (url &optional _new-window)
578 (when (and (equal major-mode 'eww-mode)
583 (defun eww-back-url ()
584 "Go to the previously displayed page."
586 (when (>= eww-history-position (length eww-history))
587 (user-error "No previous page"))
589 (setq eww-history-position (+ eww-history-position 2))
590 (eww-restore-history (elt eww-history (1- eww-history-position))))
592 (defun eww-forward-url ()
593 "Go to the next displayed page."
595 (when (zerop eww-history-position)
596 (user-error "No next page"))
598 (eww-restore-history (elt eww-history (1- eww-history-position))))
600 (defun eww-restore-history (elem)
601 (let ((inhibit-read-only t))
603 (insert (plist-get elem :text))
604 (setq eww-current-source (plist-get elem :source)
605 eww-current-dom (plist-get elem :dom))
606 (goto-char (plist-get elem :point))
607 (setq eww-current-url (plist-get elem :url)
608 eww-current-title (plist-get elem :title))
609 (eww-update-header-line-format)))
611 (defun eww-next-url ()
612 "Go to the page marked `next'.
613 A page is marked `next' if rel=\"next\" appears in a <link>
617 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
618 (user-error "No `next' on this page")))
620 (defun eww-previous-url ()
621 "Go to the page marked `previous'.
622 A page is marked `previous' if rel=\"previous\" appears in a <link>
626 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
627 (user-error "No `previous' on this page")))
630 "Go to the page marked `up'.
631 A page is marked `up' if rel=\"up\" appears in a <link>
635 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
636 (user-error "No `up' on this page")))
638 (defun eww-top-url ()
639 "Go to the page marked `top'.
640 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
641 appears in a <link> or <a> tag."
643 (let ((best-url (or eww-start-url
647 (eww-browse-url (shr-expand-url best-url eww-current-url))
648 (user-error "No `top' for this page"))))
651 "Reload the current page."
653 (url-retrieve eww-current-url 'eww-render
654 (list eww-current-url (point))))
658 (defvar eww-form nil)
660 (defvar eww-submit-map
661 (let ((map (make-sparse-keymap)))
662 (define-key map "\r" 'eww-submit)
663 (define-key map [(control c) (control c)] 'eww-submit)
666 (defvar eww-checkbox-map
667 (let ((map (make-sparse-keymap)))
668 (define-key map " " 'eww-toggle-checkbox)
669 (define-key map "\r" 'eww-toggle-checkbox)
670 (define-key map [(control c) (control c)] 'eww-submit)
674 (let ((map (make-keymap)))
675 (set-keymap-parent map text-mode-map)
676 (define-key map "\r" 'eww-submit)
677 (define-key map [(control a)] 'eww-beginning-of-text)
678 (define-key map [(control c) (control c)] 'eww-submit)
679 (define-key map [(control e)] 'eww-end-of-text)
680 (define-key map [?\t] 'shr-next-link)
681 (define-key map [?\M-\t] 'shr-previous-link)
684 (defvar eww-textarea-map
685 (let ((map (make-keymap)))
686 (set-keymap-parent map text-mode-map)
687 (define-key map "\r" 'forward-line)
688 (define-key map [(control c) (control c)] 'eww-submit)
689 (define-key map [?\t] 'shr-next-link)
690 (define-key map [?\M-\t] 'shr-previous-link)
693 (defvar eww-select-map
694 (let ((map (make-sparse-keymap)))
695 (define-key map "\r" 'eww-change-select)
696 (define-key map [(control c) (control c)] 'eww-submit)
699 (defun eww-beginning-of-text ()
700 "Move to the start of the input field."
702 (goto-char (eww-beginning-of-field)))
704 (defun eww-end-of-text ()
705 "Move to the end of the text in the input field."
707 (goto-char (eww-end-of-field))
708 (let ((start (eww-beginning-of-field)))
709 (while (and (equal (following-char) ? )
712 (when (> (point) start)
715 (defun eww-beginning-of-field ()
719 ((not (eq (get-text-property (point) 'eww-form)
720 (get-text-property (1- (point)) 'eww-form)))
723 (previous-single-property-change
724 (point) 'eww-form nil (point-min)))))
726 (defun eww-end-of-field ()
727 (1- (next-single-property-change
728 (point) 'eww-form nil (point-max))))
730 (defun eww-tag-form (cont)
732 (list (assq :method cont)
733 (assq :action cont)))
735 (shr-ensure-paragraph)
740 (when (> (point) start)
741 (put-text-property start (1+ start)
742 'eww-form eww-form))))
744 (defun eww-form-submit (cont)
745 (let ((start (point))
746 (value (cdr (assq :value cont))))
748 (if (zerop (length value))
752 (add-face-text-property start (point) 'eww-form-submit)
753 (put-text-property start (point) 'eww-form
754 (list :eww-form eww-form
757 :name (cdr (assq :name cont))))
758 (put-text-property start (point) 'keymap eww-submit-map)
761 (defun eww-form-checkbox (cont)
762 (let ((start (point)))
763 (if (cdr (assq :checked cont))
764 (insert eww-form-checkbox-selected-symbol)
765 (insert eww-form-checkbox-symbol))
766 (add-face-text-property start (point) 'eww-form-checkbox)
767 (put-text-property start (point) 'eww-form
768 (list :eww-form eww-form
769 :value (cdr (assq :value cont))
770 :type (downcase (cdr (assq :type cont)))
771 :checked (cdr (assq :checked cont))
772 :name (cdr (assq :name cont))))
773 (put-text-property start (point) 'keymap eww-checkbox-map)
776 (defun eww-form-text (cont)
777 (let ((start (point))
778 (type (downcase (or (cdr (assq :type cont))
780 (value (or (cdr (assq :value cont)) ""))
781 (width (string-to-number
782 (or (cdr (assq :size cont))
784 (readonly-property (if (or (cdr (assq :disabled cont))
785 (cdr (assq :readonly cont)))
787 'inhibit-read-only)))
789 (when (< (length value) width)
790 (insert (make-string (- width (length value)) ? )))
791 (put-text-property start (point) 'face 'eww-form-text)
792 (put-text-property start (point) 'local-map eww-text-map)
793 (put-text-property start (point) readonly-property t)
794 (put-text-property start (point) 'eww-form
795 (list :eww-form eww-form
798 :name (cdr (assq :name cont))))
801 (defconst eww-text-input-types '("text" "password" "textarea"
802 "color" "date" "datetime" "datetime-local"
803 "email" "month" "number" "search" "tel"
805 "List of input types which represent a text input.
806 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
808 (defun eww-process-text-input (beg end length)
809 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
810 (properties (text-properties-at end))
811 (type (plist-get form :type)))
813 (member type eww-text-input-types))
816 ;; Delete some space at the end.
819 (if (equal type "textarea")
820 (1- (line-end-position))
822 (let ((new (- end beg)))
823 (while (and (> new 0)
824 (eql (following-char) ? ))
825 (delete-region (point) (1+ (point)))
826 (setq new (1- new))))
827 (set-text-properties beg end properties)))
832 (if (equal type "textarea")
833 (1- (line-end-position))
835 (let ((start (point)))
836 (insert (make-string length ? ))
837 (set-text-properties start (point) properties)))))
838 (let ((value (buffer-substring-no-properties
839 (eww-beginning-of-field)
840 (eww-end-of-field))))
841 (when (string-match " +\\'" value)
842 (setq value (substring value 0 (match-beginning 0))))
843 (plist-put form :value value)
844 (when (equal type "password")
845 ;; Display passwords as asterisks.
846 (let ((start (eww-beginning-of-field)))
847 (put-text-property start (+ start (length value))
848 'display (make-string (length value) ?*))))))))
850 (defun eww-tag-textarea (cont)
851 (let ((start (point))
852 (value (or (cdr (assq :value cont)) ""))
853 (lines (string-to-number
854 (or (cdr (assq :rows cont))
856 (width (string-to-number
857 (or (cdr (assq :cols cont))
863 (when (< (count-lines start (point)) lines)
864 (dotimes (i (- lines (count-lines start (point))))
866 (setq end (point-marker))
868 (while (< (point) end)
870 (let ((pad (- width (- (point) (line-beginning-position)))))
872 (insert (make-string pad ? ))))
873 (add-face-text-property (line-beginning-position)
874 (point) 'eww-form-textarea)
875 (put-text-property (line-beginning-position) (point)
876 'local-map eww-textarea-map)
878 (put-text-property start (point) 'eww-form
879 (list :eww-form eww-form
882 :name (cdr (assq :name cont))))))
884 (defun eww-tag-input (cont)
885 (let ((type (downcase (or (cdr (assq :type cont))
889 ((or (equal type "checkbox")
890 (equal type "radio"))
891 (eww-form-checkbox cont))
892 ((equal type "submit")
893 (eww-form-submit cont))
894 ((equal type "hidden")
895 (let ((form eww-form)
896 (name (cdr (assq :name cont))))
897 ;; Don't add <input type=hidden> elements repeatedly.
899 (or (not (consp (car form)))
900 (not (eq (caar form) 'hidden))
901 (not (equal (plist-get (cdr (car form)) :name)
903 (setq form (cdr form)))
905 (nconc eww-form (list
908 :value (cdr (assq :value cont))))))))
910 (eww-form-text cont)))
911 (unless (= start (point))
912 (put-text-property start (1+ start) 'help-echo "Input field"))))
914 (defun eww-tag-select (cont)
915 (shr-ensure-paragraph)
916 (let ((menu (list :name (cdr (assq :name cont))
922 (if (eq (car (car cont)) 'optgroup)
923 (dolist (groupelem cont)
924 (unless (cdr (assq :disabled (cdr groupelem)))
925 (setq opelem (append opelem (cdr (cdr groupelem))))))
927 (dolist (elem opelem)
928 (when (eq (car elem) 'option)
929 (when (cdr (assq :selected (cdr elem)))
930 (nconc menu (list :value
931 (cdr (assq :value (cdr elem))))))
932 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
933 (setq max (max max (length display)))
935 :value (cdr (assq :value (cdr elem)))
939 (setq options (nreverse options))
940 ;; If we have no selected values, default to the first value.
941 (unless (plist-get menu :value)
942 (nconc menu (list :value (nth 2 (car options)))))
944 (let ((selected (eww-select-display menu)))
946 (make-string (- max (length selected)) ? )))
947 (put-text-property start (point) 'eww-form menu)
948 (add-face-text-property start (point) 'eww-form-select)
949 (put-text-property start (point) 'keymap eww-select-map)
950 (unless (= start (point))
951 (put-text-property start (1+ start) 'help-echo "select field"))
952 (shr-ensure-paragraph))))
954 (defun eww-select-display (select)
955 (let ((value (plist-get select :value))
957 (dolist (elem select)
958 (when (and (consp elem)
959 (eq (car elem) 'item)
960 (equal value (plist-get (cdr elem) :value)))
961 (setq display (plist-get (cdr elem) :display))))
964 (defun eww-change-select ()
965 "Change the value of the select drop-down menu under point."
967 (let* ((input (get-text-property (point) 'eww-form))
968 (completion-ignore-case t)
971 (mapcar (lambda (elem)
973 (eq (car elem) 'item)
974 (cons (plist-get (cdr elem) :display)
975 (plist-get (cdr elem) :value))))
978 (completing-read "Change value: " options nil 'require-match))
979 (inhibit-read-only t))
980 (plist-put input :value (cdr (assoc-string display options t)))
982 (eww-update-field display))))
984 (defun eww-update-field (string)
985 (let ((properties (text-properties-at (point)))
986 (start (eww-beginning-of-field))
987 (end (1+ (eww-end-of-field))))
988 (delete-region start end)
990 (make-string (- (- end start) (length string)) ? ))
991 (set-text-properties start end properties)
994 (defun eww-toggle-checkbox ()
995 "Toggle the value of the checkbox under point."
997 (let* ((input (get-text-property (point) 'eww-form))
998 (type (plist-get input :type)))
999 (if (equal type "checkbox")
1002 (if (plist-get input :checked)
1004 (plist-put input :checked nil)
1005 (eww-update-field eww-form-checkbox-symbol))
1006 (plist-put input :checked t)
1007 (eww-update-field eww-form-checkbox-selected-symbol))))
1008 ;; Radio button. Switch all other buttons off.
1009 (let ((name (plist-get input :name)))
1011 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1012 (when (equal (plist-get (cdr elem) :name) name)
1013 (goto-char (car elem))
1014 (if (not (eq (cdr elem) input))
1016 (plist-put input :checked nil)
1017 (eww-update-field eww-form-checkbox-symbol))
1018 (plist-put input :checked t)
1019 (eww-update-field eww-form-checkbox-selected-symbol)))))
1020 (forward-char 1)))))
1022 (defun eww-inputs (form)
1023 (let ((start (point-min))
1026 (< start (point-max)))
1027 (when (or (get-text-property start 'eww-form)
1028 (setq start (next-single-property-change start 'eww-form)))
1029 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1031 (push (cons start (get-text-property start 'eww-form))
1033 (setq start (next-single-property-change start 'eww-form))))
1036 (defun eww-input-value (input)
1037 (let ((type (plist-get input :type))
1038 (value (plist-get input :value)))
1040 ((equal type "textarea")
1043 (goto-char (point-min))
1044 (while (re-search-forward "^ +\n\\| +$" nil t)
1045 (replace-match "" t t))
1048 (if (string-match " +\\'" value)
1049 (substring value 0 (match-beginning 0))
1052 (defun eww-submit ()
1053 "Submit the current form."
1055 (let* ((this-input (get-text-property (point) 'eww-form))
1056 (form (plist-get this-input :eww-form))
1058 (dolist (elem (sort (eww-inputs form)
1060 (< (car o1) (car o2)))))
1061 (let* ((input (cdr elem))
1062 (input-start (car elem))
1063 (name (plist-get input :name)))
1066 ((member (plist-get input :type) '("checkbox" "radio"))
1067 (when (plist-get input :checked)
1068 (push (cons name (plist-get input :value))
1070 ((equal (plist-get input :type) "submit")
1071 ;; We want the values from buttons if we hit a button if
1072 ;; we hit enter on it, or if it's the first button after
1073 ;; the field we did hit return on.
1074 (when (or (eq input this-input)
1075 (and (not (eq input this-input))
1077 (> input-start (point))))
1078 (setq next-submit t)
1079 (push (cons name (plist-get input :value))
1082 (push (cons name (eww-input-value input))
1085 (when (and (consp elem)
1086 (eq (car elem) 'hidden))
1087 (push (cons (plist-get (cdr elem) :name)
1088 (or (plist-get (cdr elem) :value) ""))
1090 (if (and (stringp (cdr (assq :method form)))
1091 (equal (downcase (cdr (assq :method form))) "post"))
1092 (let ((url-request-method "POST")
1093 (url-request-extra-headers
1094 '(("Content-Type" . "application/x-www-form-urlencoded")))
1095 (url-request-data (mm-url-encode-www-form-urlencoded values)))
1096 (eww-browse-url (shr-expand-url (cdr (assq :action form))
1100 (if (cdr (assq :action form))
1101 (shr-expand-url (cdr (assq :action form))
1105 (mm-url-encode-www-form-urlencoded values))))))
1107 (defun eww-browse-with-external-browser (&optional url)
1108 "Browse the current URL with an external browser.
1109 The browser to used is specified by the `shr-external-browser' variable."
1111 (funcall shr-external-browser (or url eww-current-url)))
1113 (defun eww-follow-link (&optional external mouse-event)
1114 "Browse the URL under point.
1115 If EXTERNAL, browse the URL using `shr-external-browser'."
1116 (interactive (list current-prefix-arg last-nonmenu-event))
1117 (mouse-set-point mouse-event)
1118 (let ((url (get-text-property (point) 'shr-url)))
1121 (message "No link under point"))
1122 ((string-match "^mailto:" url)
1123 (browse-url-mail url))
1125 (funcall shr-external-browser url))
1126 ;; This is a #target url in the same page as the current one.
1127 ((and (url-target (url-generic-parse-url url))
1128 (eww-same-page-p url eww-current-url))
1130 (eww-display-html 'utf-8 url eww-current-dom))
1132 (eww-browse-url url)))))
1134 (defun eww-same-page-p (url1 url2)
1135 "Return non-nil if both URLs represent the same page.
1136 Differences in #targets are ignored."
1137 (let ((obj1 (url-generic-parse-url url1))
1138 (obj2 (url-generic-parse-url url2)))
1139 (setf (url-target obj1) nil)
1140 (setf (url-target obj2) nil)
1141 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1143 (defun eww-copy-page-url ()
1145 (message "%s" eww-current-url)
1146 (kill-new eww-current-url))
1148 (defun eww-download ()
1149 "Download URL under point to `eww-download-directory'."
1151 (let ((url (get-text-property (point) 'shr-url)))
1153 (message "No URL under point")
1154 (url-retrieve url 'eww-download-callback (list url)))))
1156 (defun eww-download-callback (status url)
1157 (unless (plist-get status :error)
1158 (let* ((obj (url-generic-parse-url url))
1159 (path (car (url-path-and-query obj)))
1160 (file (eww-make-unique-file-name (file-name-nondirectory path)
1161 eww-download-directory)))
1163 (message "Saved %s" file))))
1165 (defun eww-make-unique-file-name (file directory)
1167 ((zerop (length file))
1169 ((string-match "\\`[.]" file)
1170 (setq file (concat "!" file))))
1172 (while (file-exists-p (expand-file-name file directory))
1174 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1175 (format "%s(%d)%s" (match-string 1 file)
1176 count (match-string 2 file))
1177 (format "%s(%d)" file count)))
1178 (setq count (1+ count)))
1179 (expand-file-name file directory)))
1183 (defvar eww-bookmarks nil)
1185 (defun eww-add-bookmark ()
1186 "Add the current page to the bookmarks."
1188 (eww-read-bookmarks)
1189 (dolist (bookmark eww-bookmarks)
1190 (when (equal eww-current-url
1191 (plist-get bookmark :url))
1192 (user-error "Already bookmarked")))
1193 (if (y-or-n-p "bookmark this page? ")
1195 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1196 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1197 (push (list :url eww-current-url
1199 :time (current-time-string))
1201 (eww-write-bookmarks)
1202 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1204 (defun eww-write-bookmarks ()
1205 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1206 (insert ";; Auto-generated file; don't edit\n")
1207 (pp eww-bookmarks (current-buffer))))
1209 (defun eww-read-bookmarks ()
1210 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1212 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1214 (insert-file-contents file)
1215 (read (current-buffer)))))))
1218 (defun eww-list-bookmarks ()
1219 "Display the bookmarks."
1221 (eww-bookmark-prepare)
1222 (pop-to-buffer "*eww bookmarks*"))
1224 (defun eww-bookmark-prepare ()
1225 (eww-read-bookmarks)
1226 (unless eww-bookmarks
1227 (user-error "No bookmarks are defined"))
1228 (set-buffer (get-buffer-create "*eww bookmarks*"))
1230 (let ((format "%-40s %s")
1231 (inhibit-read-only t)
1234 (setq header-line-format (concat " " (format format "URL" "Title")))
1235 (dolist (bookmark eww-bookmarks)
1236 (setq start (point))
1237 (setq url (plist-get bookmark :url))
1238 (when (> (length url) 40)
1239 (setq url (substring url 0 40)))
1240 (insert (format format url
1241 (plist-get bookmark :title))
1243 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1244 (goto-char (point-min))))
1246 (defvar eww-bookmark-kill-ring nil)
1248 (defun eww-bookmark-kill ()
1249 "Kill the current bookmark."
1251 (let* ((start (line-beginning-position))
1252 (bookmark (get-text-property start 'eww-bookmark))
1253 (inhibit-read-only t))
1255 (user-error "No bookmark on the current line"))
1257 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1258 (delete-region start (point))
1259 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1260 (eww-write-bookmarks)))
1262 (defun eww-bookmark-yank ()
1263 "Yank a previously killed bookmark to the current line."
1265 (unless eww-bookmark-kill-ring
1266 (user-error "No previously killed bookmark"))
1268 (let ((inhibit-read-only t)
1271 (insert (pop eww-bookmark-kill-ring))
1272 (setq bookmark (get-text-property start 'eww-bookmark))
1273 (if (= start (point-min))
1274 (push bookmark eww-bookmarks)
1275 (let ((line (count-lines start (point))))
1276 (setcdr (nthcdr (1- line) eww-bookmarks)
1277 (cons bookmark (nthcdr line eww-bookmarks)))))
1278 (eww-write-bookmarks)))
1280 (defun eww-bookmark-browse ()
1281 "Browse the bookmark under point in eww."
1283 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1285 (user-error "No bookmark on the current line"))
1287 (eww-browse-url (plist-get bookmark :url))))
1289 (defun eww-next-bookmark ()
1290 "Go to the next bookmark in the list."
1294 (unless (get-buffer "*eww bookmarks*")
1296 (eww-bookmark-prepare))
1297 (with-current-buffer (get-buffer "*eww bookmarks*")
1298 (when (and (not first)
1301 (setq bookmark (get-text-property (line-beginning-position)
1304 (user-error "No next bookmark")))
1305 (eww-browse-url (plist-get bookmark :url))))
1307 (defun eww-previous-bookmark ()
1308 "Go to the previous bookmark in the list."
1312 (unless (get-buffer "*eww bookmarks*")
1314 (eww-bookmark-prepare))
1315 (with-current-buffer (get-buffer "*eww bookmarks*")
1317 (goto-char (point-max))
1318 (beginning-of-line))
1319 ;; On the final line.
1323 (user-error "No previous bookmark")
1325 (setq bookmark (get-text-property (line-beginning-position)
1327 (eww-browse-url (plist-get bookmark :url))))
1329 (defvar eww-bookmark-mode-map
1330 (let ((map (make-sparse-keymap)))
1331 (suppress-keymap map)
1332 (define-key map "q" 'quit-window)
1333 (define-key map [(control k)] 'eww-bookmark-kill)
1334 (define-key map [(control y)] 'eww-bookmark-yank)
1335 (define-key map "\r" 'eww-bookmark-browse)
1337 (easy-menu-define nil map
1338 "Menu for `eww-bookmark-mode-map'."
1340 ["Exit" quit-window t]
1341 ["Browse" eww-bookmark-browse
1342 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1343 ["Kill" eww-bookmark-kill
1344 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1345 ["Yank" eww-bookmark-yank
1346 :active eww-bookmark-kill-ring]))
1349 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1350 "Mode for listing bookmarks.
1352 \\{eww-bookmark-mode-map}"
1353 (buffer-disable-undo)
1354 (setq buffer-read-only t
1359 (defun eww-save-history ()
1360 (push (list :url eww-current-url
1361 :title eww-current-title
1363 :dom eww-current-dom
1364 :source eww-current-source
1365 :text (buffer-string))
1368 (defun eww-list-histories ()
1369 "List the eww-histories."
1371 (when (null eww-history)
1372 (error "No eww-histories are defined"))
1373 (let ((eww-history-trans eww-history))
1374 (set-buffer (get-buffer-create "*eww history*"))
1376 (let ((inhibit-read-only t)
1379 url title format start)
1381 (dolist (history eww-history-trans)
1382 (setq start (point))
1383 (setq domain-length (max domain-length (length (plist-get history :url))))
1384 (setq title-length (max title-length (length (plist-get history :title)))))
1385 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1387 (concat " " (format format "Title" "URL")))
1388 (dolist (history eww-history-trans)
1389 (setq start (point))
1390 (setq url (plist-get history :url))
1391 (setq title (plist-get history :title))
1392 (insert (format format title url))
1394 (put-text-property start (1+ start) 'eww-history history))
1395 (goto-char (point-min)))
1396 (pop-to-buffer "*eww history*")))
1398 (defun eww-history-browse ()
1399 "Browse the history under point in eww."
1401 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1403 (error "No history on the current line"))
1405 (eww-restore-history history)))
1407 (defvar eww-history-mode-map
1408 (let ((map (make-sparse-keymap)))
1409 (suppress-keymap map)
1410 (define-key map "q" 'quit-window)
1411 (define-key map "\r" 'eww-history-browse)
1412 ;; (define-key map "n" 'next-error-no-select)
1413 ;; (define-key map "p" 'previous-error-no-select)
1415 (easy-menu-define nil map
1416 "Menu for `eww-history-mode-map'."
1418 ["Exit" quit-window t]
1419 ["Browse" eww-history-browse
1420 :active (get-text-property (line-beginning-position) 'eww-history)]))
1423 (define-derived-mode eww-history-mode nil "eww history"
1424 "Mode for listing eww-histories.
1426 \\{eww-history-mode-map}"
1427 (buffer-disable-undo)
1428 (setq buffer-read-only t
1433 ;;; eww.el ends here