1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2015 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
)
32 (require 'url-util
) ; for url-get-url-at-point
34 (eval-when-compile (require 'subr-x
)) ;; for string-trim
39 :link
'(custom-manual "(eww) Top")
43 (defcustom eww-header-line-format
"%t: %u"
45 - %t is replaced by the title.
46 - %u is replaced by the URL."
51 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
52 "Prefix URL to search engine"
57 (defcustom eww-download-directory
"~/Downloads/"
58 "Directory where files will downloaded."
63 (defcustom eww-suggest-uris
67 "List of functions called to form the list of default URIs for `eww'.
68 Each of the elements is a function returning either a string or a list
69 of strings. The results will be joined into a single list with
70 duplicate entries (if any) removed."
74 :options
'(eww-links-at-point
78 (defcustom eww-bookmarks-directory user-emacs-directory
79 "Directory where bookmark files will be stored."
84 (defcustom eww-desktop-remove-duplicates t
85 "Whether to remove duplicates from the history when saving desktop data.
86 If non-nil, repetitive EWW history entries (comprising of the URI, the
87 title, and the point position) will not be saved as part of the Emacs
88 desktop. Otherwise, such entries will be retained."
93 (defcustom eww-restore-desktop nil
94 "How to restore EWW buffers on `desktop-restore'.
95 If t or 'auto, the buffers will be reloaded automatically.
96 If nil, buffers will require manual reload, and will contain the text
97 specified in `eww-restore-reload-prompt' instead of the actual Web
101 :type
'(choice (const :tag
"Restore all automatically" t
)
102 (const :tag
"Require manual reload" nil
)))
104 (defcustom eww-restore-reload-prompt
105 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
106 "The string to put in the buffers not reloaded on `desktop-restore'.
107 This prompt will be used if `eww-restore-desktop' is nil.
109 The string will be passed through `substitute-command-keys'."
114 (defcustom eww-history-limit
50
115 "Maximum number of entries to retain in the history."
118 :type
'(choice (const :tag
"Unlimited" nil
)
121 (defcustom eww-use-external-browser-for-content-type
122 "\\`\\(video/\\|audio/\\|application/ogg\\)"
123 "Always use external browser for specified content-type."
126 :type
'(choice (const :tag
"Never" nil
)
129 (defcustom eww-after-render-hook nil
130 "A hook called after eww has finished rendering the buffer."
135 (defcustom eww-form-checkbox-selected-symbol
"[X]"
136 "Symbol used to represent a selected checkbox.
137 See also `eww-form-checkbox-symbol'."
140 :type
'(choice (const "[X]")
141 (const "☒") ; Unicode BALLOT BOX WITH X
142 (const "☑") ; Unicode BALLOT BOX WITH CHECK
145 (defcustom eww-form-checkbox-symbol
"[ ]"
146 "Symbol used to represent a checkbox.
147 See also `eww-form-checkbox-selected-symbol'."
150 :type
'(choice (const "[ ]")
151 (const "☐") ; Unicode BALLOT BOX
154 (defface eww-form-submit
155 '((((type x w32 ns
) (class color
)) ; Like default mode line
156 :box
(:line-width
2 :style released-button
)
157 :background
"#808080" :foreground
"black"))
158 "Face for eww buffer buttons."
162 (defface eww-form-file
163 '((((type x w32 ns
) (class color
)) ; Like default mode line
164 :box
(:line-width
2 :style released-button
)
165 :background
"#808080" :foreground
"black"))
166 "Face for eww buffer buttons."
170 (defface eww-form-checkbox
171 '((((type x w32 ns
) (class color
)) ; Like default mode line
172 :box
(:line-width
2 :style released-button
)
173 :background
"lightgrey" :foreground
"black"))
174 "Face for eww buffer buttons."
178 (defface eww-form-select
179 '((((type x w32 ns
) (class color
)) ; Like default mode line
180 :box
(:line-width
2 :style released-button
)
181 :background
"lightgrey" :foreground
"black"))
182 "Face for eww buffer buttons."
186 (defface eww-form-text
187 '((t (:background
"#505050"
189 :box
(:line-width
1))))
190 "Face for eww text inputs."
194 (defface eww-form-textarea
195 '((t (:background
"#C0C0C0"
197 :box
(:line-width
1))))
198 "Face for eww textarea inputs."
202 (defface eww-invalid-certificate
203 '((default :weight bold
)
204 (((class color
)) :foreground
"red"))
205 "Face for web pages with invalid certificates."
209 (defface eww-valid-certificate
210 '((default :weight bold
)
211 (((class color
)) :foreground
"ForestGreen"))
212 "Face for web pages with valid certificates."
216 (defvar eww-data nil
)
217 (defvar eww-history nil
)
218 (defvar eww-history-position
0)
220 (defvar eww-local-regex
"localhost"
221 "When this regex is found in the URL, it's not a keyword but an address.")
223 (defvar eww-link-keymap
224 (let ((map (copy-keymap shr-map
)))
225 (define-key map
"\r" 'eww-follow-link
)
228 (defun eww-suggested-uris nil
229 "Return the list of URIs to suggest at the `eww' prompt.
230 This list can be customized via `eww-suggest-uris'."
231 (let ((obseen (make-vector 42 0))
233 (dolist (fun eww-suggest-uris
)
234 (let ((ret (funcall fun
)))
235 (dolist (uri (if (stringp ret
) (list ret
) ret
))
236 (when (and uri
(not (intern-soft uri obseen
)))
243 "Fetch URL and render the page.
244 If the input doesn't look like an URL or a domain name, the
245 word(s) will be searched for via `eww-search-prefix'."
247 (let* ((uris (eww-suggested-uris))
248 (prompt (concat "Enter URL or keywords"
249 (if uris
(format " (default %s)" (car uris
)) "")
251 (list (read-string prompt nil nil uris
))))
252 (setq url
(string-trim url
))
253 (cond ((string-match-p "\\`file:/" url
))
254 ;; Don't mangle file: URLs at all.
255 ((string-match-p "\\`ftp://" url
)
256 (user-error "FTP is not supported."))
258 ;; Anything that starts with something that vaguely looks
259 ;; like a protocol designator is interpreted as a full URL.
260 (if (or (string-match "\\`[A-Za-z]+:" url
)
261 ;; Also try to match "naked" URLs like
262 ;; en.wikipedia.org/wiki/Free software
263 (string-match "\\`[A-Za-z_]+\\.[A-Za-z._]+/" url
)
264 (and (= (length (split-string url
)) 1)
265 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
266 (> (length (split-string url
"[.:]")) 1))
267 (string-match eww-local-regex url
))))
269 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
270 (setq url
(concat "http://" url
)))
271 ;; Some sites do not redirect final /
272 (when (string= (url-filename (url-generic-parse-url url
)) "")
273 (setq url
(concat url
"/"))))
274 (setq url
(concat eww-search-prefix
275 (replace-regexp-in-string " " "+" url
))))))
276 (if (eq major-mode
'eww-mode
)
277 (when (or (plist-get eww-data
:url
)
278 (plist-get eww-data
:dom
))
281 (plist-put eww-data
:url url
)
282 (plist-put eww-data
:title
"")
283 (eww-update-header-line-format)
284 (let ((inhibit-read-only t
))
285 (insert (format "Loading %s..." url
))
286 (goto-char (point-min))))
287 (url-retrieve url
'eww-render
288 (list url nil
(current-buffer))))
290 ;;;###autoload (defalias 'browse-web 'eww)
293 (defun eww-open-file (file)
294 "Render a file using EWW."
295 (interactive "fFile: ")
296 (eww (concat "file://"
297 (and (memq system-type
'(windows-nt ms-dos
))
299 (expand-file-name file
))))
302 (defun eww-search-words (&optional beg end
)
303 "Search the web for the text between the point and marker.
304 See the `eww-search-prefix' variable for the search engine used."
306 (eww (buffer-substring beg end
)))
308 (defun eww-render (status url
&optional point buffer encode
)
309 (let ((redirect (plist-get status
:redirect
)))
311 (setq url redirect
)))
312 (let* ((headers (eww-parse-headers))
314 (mail-header-parse-content-type
315 (or (cdr (assoc "content-type" headers
))
319 (or (cdr (assq 'charset
(cdr content-type
)))
320 (eww-detect-charset (equal (car content-type
)
323 (data-buffer (current-buffer)))
324 ;; Save the https peer status.
325 (with-current-buffer buffer
326 (plist-put eww-data
:peer
(plist-get status
:peer
)))
330 ((and eww-use-external-browser-for-content-type
331 (string-match-p eww-use-external-browser-for-content-type
333 (eww-browse-with-external-browser url
))
334 ((equal (car content-type
) "text/html")
335 (eww-display-html charset url nil point buffer encode
))
336 ((equal (car content-type
) "application/pdf")
338 ((string-match-p "\\`image/" (car content-type
))
339 (eww-display-image buffer
))
341 (eww-display-raw buffer encode
)))
342 (with-current-buffer buffer
343 (plist-put eww-data
:url url
)
344 (eww-update-header-line-format)
345 (setq eww-history-position
0)
346 (run-hooks 'eww-after-render-hook
)))
347 (kill-buffer data-buffer
))))
349 (defun eww-parse-headers ()
351 (goto-char (point-min))
352 (while (and (not (eobp))
354 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
355 (push (cons (downcase (match-string 1))
363 (defun eww-detect-charset (html-p)
364 (let ((case-fold-search t
)
368 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
372 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
375 (declare-function libxml-parse-html-region
"xml.c"
376 (start end
&optional base-url
))
378 (defun eww-display-html (charset url
&optional document point buffer encode
)
379 (unless (fboundp 'libxml-parse-html-region
)
380 (error "This function requires Emacs to be compiled with libxml2"))
381 (unless (buffer-live-p buffer
)
382 (error "Buffer %s doesn't exist" buffer
))
383 ;; There should be a better way to abort loading images
389 'base
(list (cons 'href url
))
391 (when (or (and encode
392 (not (eq charset encode
)))
393 (not (eq charset
'utf-8
)))
395 (decode-coding-region (point) (point-max)
397 (coding-system-error nil
)))
398 (libxml-parse-html-region (point) (point-max))))))
399 (source (and (null document
)
400 (buffer-substring (point) (point-max)))))
401 (with-current-buffer buffer
402 (plist-put eww-data
:source source
)
403 (plist-put eww-data
:dom document
)
404 (let ((inhibit-read-only t
)
405 (inhibit-modification-hooks t
)
406 (shr-target-id (url-target (url-generic-parse-url url
)))
407 (shr-external-rendering-functions
408 '((title . eww-tag-title
)
409 (form . eww-tag-form
)
410 (input . eww-tag-input
)
411 (textarea . eww-tag-textarea
)
412 (body . eww-tag-body
)
413 (select . eww-tag-select
)
414 (link . eww-tag-link
)
417 (shr-insert-document document
)
422 (goto-char (point-min))
423 (let ((point (next-single-property-change
424 (point-min) 'shr-target-id
)))
428 (goto-char (point-min))
429 ;; Don't leave point inside forms, because the normal eww
430 ;; commands aren't available there.
431 (while (and (not (eobp))
432 (get-text-property (point) 'eww-form
))
434 (eww-size-text-inputs))))
436 (defun eww-handle-link (dom)
437 (let* ((rel (dom-attr dom
'rel
))
438 (href (dom-attr dom
'href
))
440 ;; The text associated with :rel is case-insensitive.
441 (if rel
(downcase rel
))
443 ;; Texinfo uses "previous", but HTML specifies
444 ;; "prev", so recognize both.
445 ("previous" .
:previous
)
447 ;; HTML specifies "start" but also "contents",
448 ;; and Gtk seems to use "home". Recognize
449 ;; them all; but store them in different
450 ;; variables so that we can readily choose the
454 ("contents" .
:contents
)
458 (plist-put eww-data
(cdr where
) href
))))
460 (defun eww-tag-link (dom)
461 (eww-handle-link dom
)
464 (defun eww-tag-a (dom)
465 (eww-handle-link dom
)
466 (let ((start (point)))
468 (put-text-property start
(point) 'keymap eww-link-keymap
)))
470 (defun eww-update-header-line-format ()
471 (setq header-line-format
472 (and eww-header-line-format
473 (let ((title (plist-get eww-data
:title
))
474 (peer (plist-get eww-data
:peer
)))
475 (when (zerop (length title
))
476 (setq title
"[untitled]"))
477 ;; This connection has is https.
480 (propertize title
'face
481 (if (plist-get peer
:warnings
)
482 'eww-invalid-certificate
483 'eww-valid-certificate
))))
484 (replace-regexp-in-string
487 eww-header-line-format
488 `((?u .
,(or (plist-get eww-data
:url
) ""))
491 (defun eww-tag-title (dom)
492 (plist-put eww-data
:title
493 (replace-regexp-in-string
495 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom
))))
496 (eww-update-header-line-format))
498 (defun eww-tag-body (dom)
499 (let* ((start (point))
500 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
501 (bgcolor (dom-attr dom
'bgcolor
))
502 (shr-stylesheet (list (cons 'color fgcolor
)
503 (cons 'background-color bgcolor
))))
505 (shr-colorize-region start
(point) fgcolor bgcolor
)))
507 (defun eww-display-raw (buffer &optional encode
)
508 (let ((data (buffer-substring (point) (point-max))))
509 (unless (buffer-live-p buffer
)
510 (error "Buffer %s doesn't exist" buffer
))
511 (with-current-buffer buffer
512 (let ((inhibit-read-only t
))
515 (unless (eq encode
'utf-8
)
516 (encode-coding-region (point-min) (1+ (length data
)) 'utf-8
)
518 (decode-coding-region (point-min) (1+ (length data
)) encode
)
519 (coding-system-error nil
))))
520 (goto-char (point-min)))))
522 (defun eww-display-image (buffer)
523 (let ((data (shr-parse-image-data)))
524 (unless (buffer-live-p buffer
)
525 (error "Buffer %s doesn't exist" buffer
))
526 (with-current-buffer buffer
527 (let ((inhibit-read-only t
))
529 (shr-put-image data nil
))
530 (goto-char (point-min)))))
532 (declare-function mailcap-view-mime
"mailcap" (type))
533 (defun eww-display-pdf ()
534 (let ((data (buffer-substring (point) (point-max))))
535 (switch-to-buffer (get-buffer-create "*eww pdf*"))
536 (let ((coding-system-for-write 'raw-text
)
537 (inhibit-read-only t
))
540 (mailcap-view-mime "application/pdf")))
541 (goto-char (point-min)))
543 (defun eww-setup-buffer ()
544 (switch-to-buffer (get-buffer-create "*eww*"))
545 (let ((inhibit-read-only t
))
548 (unless (eq major-mode
'eww-mode
)
551 (defun eww-current-url nil
552 "Return URI of the Web page the current EWW buffer is visiting."
553 (plist-get eww-data
:url
))
555 (defun eww-links-at-point ()
556 "Return list of URIs, if any, linked at point."
558 (list (get-text-property (point) 'shr-url
)
559 (get-text-property (point) 'image-url
))))
561 (defun eww-view-source ()
562 "View the HTML source code of the current page."
564 (let ((buf (get-buffer-create "*eww-source*"))
565 (source (plist-get eww-data
:source
)))
566 (with-current-buffer buf
567 (let ((inhibit-read-only t
))
568 (delete-region (point-min) (point-max))
569 (insert (or source
"no source"))
570 (goto-char (point-min))
571 (when (fboundp 'html-mode
)
575 (defun eww-readable ()
576 "View the main \"readable\" parts of the current web page.
577 This command uses heuristics to find the parts of the web page that
578 contains the main textual portion, leaving out navigation menus and
581 (let* ((old-data eww-data
)
582 (dom (with-temp-buffer
583 (insert (plist-get old-data
:source
))
585 (decode-coding-region (point-min) (point-max) 'utf-8
)
586 (coding-system-error nil
))
587 (libxml-parse-html-region (point-min) (point-max)))))
588 (eww-score-readability dom
)
590 (eww-display-html nil nil
591 (eww-highest-readability dom
)
592 nil
(current-buffer))
593 (dolist (elem '(:source
:url
:title
:next
:previous
:up
))
594 (plist-put eww-data elem
(plist-get old-data elem
)))
595 (eww-update-header-line-format)))
597 (defun eww-score-readability (node)
600 ((memq (dom-tag node
) '(script head comment
))
602 ((eq (dom-tag node
) 'meta
)
604 ((eq (dom-tag node
) 'img
)
606 ((eq (dom-tag node
) 'a
)
607 (setq score
(- (length (split-string (dom-text node
))))))
609 (dolist (elem (dom-children node
))
611 (setq score
(+ score
(length (split-string elem
))))
613 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
614 (eww-score-readability elem
))))))))
615 ;; Cache the score of the node to avoid recomputing all the time.
616 (dom-set-attribute node
:eww-readability-score score
)
619 (defun eww-highest-readability (node)
622 (dolist (elem (dom-non-text-children node
))
623 (when (> (or (dom-attr
624 (setq highest
(eww-highest-readability elem
))
625 :eww-readability-score
)
626 most-negative-fixnum
)
627 (or (dom-attr result
:eww-readability-score
)
628 most-negative-fixnum
))
629 (setq result highest
)))
633 (let ((map (make-sparse-keymap)))
634 (set-keymap-parent map special-mode-map
)
635 (define-key map
"g" 'eww-reload
) ;FIXME: revert-buffer-function instead!
636 (define-key map
"G" 'eww
)
637 (define-key map
[?
\t] 'shr-next-link
)
638 (define-key map
[?\M-
\t] 'shr-previous-link
)
639 (define-key map
[backtab] 'shr-previous-link)
640 (define-key map [delete] 'scroll-down-command)
641 (define-key map "l" 'eww-back-url)
642 (define-key map "r" 'eww-forward-url)
643 (define-key map "n" 'eww-next-url)
644 (define-key map "p" 'eww-previous-url)
645 (define-key map "u" 'eww-up-url)
646 (define-key map "t" 'eww-top-url)
647 (define-key map "&" 'eww-browse-with-external-browser)
648 (define-key map "d" 'eww-download)
649 (define-key map "w" 'eww-copy-page-url)
650 (define-key map "C" 'url-cookie-list)
651 (define-key map "v" 'eww-view-source)
652 (define-key map "R" 'eww-readable)
653 (define-key map "H" 'eww-list-histories)
654 (define-key map "E" 'eww-set-character-encoding)
655 (define-key map "S" 'eww-list-buffers)
657 (define-key map "b" 'eww-add-bookmark)
658 (define-key map "B" 'eww-list-bookmarks)
659 (define-key map [(meta n)] 'eww-next-bookmark)
660 (define-key map [(meta p)] 'eww-previous-bookmark)
662 (easy-menu-define nil map ""
664 ["Exit" quit-window t]
665 ["Close browser" quit-window t]
666 ["Reload" eww-reload t]
667 ["Back to previous page" eww-back-url
668 :active (not (zerop (length eww-history)))]
669 ["Forward to next page" eww-forward-url
670 :active (not (zerop eww-history-position))]
671 ["Browse with external browser" eww-browse-with-external-browser t]
672 ["Download" eww-download t]
673 ["View page source" eww-view-source]
674 ["Copy page URL" eww-copy-page-url t]
675 ["List histories" eww-list-histories t]
676 ["List buffers" eww-list-buffers t]
677 ["Add bookmark" eww-add-bookmark t]
678 ["List bookmarks" eww-list-bookmarks t]
679 ["List cookies" url-cookie-list t]
680 ["Character Encoding" eww-set-character-encoding]))
683 (defvar eww-tool-bar-map
684 (let ((map (make-sparse-keymap)))
685 (dolist (tool-bar-item
686 '((quit-window . "close")
687 (eww-reload . "refresh")
688 (eww-back-url . "left-arrow")
689 (eww-forward-url . "right-arrow")
690 (eww-view-source . "show")
691 (eww-copy-page-url . "copy")
692 (eww-add-bookmark . "bookmark_add"))) ;; ...
693 (tool-bar-local-item-from-menu
694 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
696 "Tool bar for `eww-mode'.")
698 (define-derived-mode eww-mode special-mode "eww"
699 "Mode for browsing the web."
700 (setq-local eww-data (list :title ""))
701 (setq-local browse-url-browser-function #'eww-browse-url)
702 (add-hook 'after-change-functions #'eww-process-text-input nil t)
703 (setq-local eww-history nil)
704 (setq-local eww-history-position 0)
705 (when (boundp 'tool-bar-map)
706 (setq-local tool-bar-map eww-tool-bar-map))
708 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
709 ;; multi-page isearch support
710 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
711 (setq truncate-lines t)
712 (buffer-disable-undo)
713 (setq buffer-read-only t))
716 (defun eww-browse-url (url &optional new-window)
718 (switch-to-buffer (generate-new-buffer "*eww*"))
722 (defun eww-back-url ()
723 "Go to the previously displayed page."
725 (when (>= eww-history-position (length eww-history))
726 (user-error "No previous page"))
728 (setq eww-history-position (+ eww-history-position 2))
729 (eww-restore-history (elt eww-history (1- eww-history-position))))
731 (defun eww-forward-url ()
732 "Go to the next displayed page."
734 (when (zerop eww-history-position)
735 (user-error "No next page"))
737 (eww-restore-history (elt eww-history (1- eww-history-position))))
739 (defun eww-restore-history (elem)
740 (let ((inhibit-read-only t)
741 (inhibit-modification-hooks t)
742 (text (plist-get elem :text)))
745 (eww-reload) ; FIXME: restore :point?
748 (goto-char (plist-get elem :point))
749 (eww-update-header-line-format))))
751 (defun eww-next-url ()
752 "Go to the page marked `next'.
753 A page is marked `next' if rel=\"next\" appears in a <link>
756 (if (plist-get eww-data :next)
757 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
758 (plist-get eww-data :url)))
759 (user-error "No `next' on this page")))
761 (defun eww-previous-url ()
762 "Go to the page marked `previous'.
763 A page is marked `previous' if rel=\"previous\" appears in a <link>
766 (if (plist-get eww-data :previous)
767 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
768 (plist-get eww-data :url)))
769 (user-error "No `previous' on this page")))
772 "Go to the page marked `up'.
773 A page is marked `up' if rel=\"up\" appears in a <link>
776 (if (plist-get eww-data :up)
777 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
778 (plist-get eww-data :url)))
779 (user-error "No `up' on this page")))
781 (defun eww-top-url ()
782 "Go to the page marked `top'.
783 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
784 appears in a <link> or <a> tag."
786 (let ((best-url (or (plist-get eww-data :start)
787 (plist-get eww-data :contents)
788 (plist-get eww-data :home))))
790 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
791 (user-error "No `top' for this page"))))
793 (defun eww-reload (&optional local encode)
794 "Reload the current page.
795 If LOCAL (the command prefix), don't reload the page from the
796 network, but just re-display the HTML already fetched."
798 (let ((url (plist-get eww-data :url)))
800 (if (null (plist-get eww-data :dom))
801 (error "No current HTML data")
802 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
803 (point) (current-buffer)))
804 (url-retrieve url 'eww-render
805 (list url (point) (current-buffer) encode)))))
809 (defvar eww-form nil)
811 (defvar eww-submit-map
812 (let ((map (make-sparse-keymap)))
813 (define-key map "\r" 'eww-submit)
814 (define-key map [(control c) (control c)] 'eww-submit)
817 (defvar eww-submit-file
818 (let ((map (make-sparse-keymap)))
819 (define-key map "\r" 'eww-select-file)
820 (define-key map [(control c) (control c)] 'eww-submit)
823 (defvar eww-checkbox-map
824 (let ((map (make-sparse-keymap)))
825 (define-key map " " 'eww-toggle-checkbox)
826 (define-key map "\r" 'eww-toggle-checkbox)
827 (define-key map [(control c) (control c)] 'eww-submit)
831 (let ((map (make-keymap)))
832 (set-keymap-parent map text-mode-map)
833 (define-key map "\r" 'eww-submit)
834 (define-key map [(control a)] 'eww-beginning-of-text)
835 (define-key map [(control c) (control c)] 'eww-submit)
836 (define-key map [(control e)] 'eww-end-of-text)
837 (define-key map [?\t] 'shr-next-link)
838 (define-key map [?\M-\t] 'shr-previous-link)
841 (defvar eww-textarea-map
842 (let ((map (make-keymap)))
843 (set-keymap-parent map text-mode-map)
844 (define-key map "\r" 'forward-line)
845 (define-key map [(control c) (control c)] 'eww-submit)
846 (define-key map [?\t] 'shr-next-link)
847 (define-key map [?\M-\t] 'shr-previous-link)
850 (defvar eww-select-map
851 (let ((map (make-sparse-keymap)))
852 (define-key map "\r" 'eww-change-select)
853 (define-key map [(control c) (control c)] 'eww-submit)
856 (defun eww-beginning-of-text ()
857 "Move to the start of the input field."
859 (goto-char (eww-beginning-of-field)))
861 (defun eww-end-of-text ()
862 "Move to the end of the text in the input field."
864 (goto-char (eww-end-of-field))
865 (let ((start (eww-beginning-of-field)))
866 (while (and (equal (following-char) ? )
869 (when (> (point) start)
872 (defun eww-beginning-of-field ()
876 ((not (eq (get-text-property (point) 'eww-form)
877 (get-text-property (1- (point)) 'eww-form)))
880 (previous-single-property-change
881 (point) 'eww-form nil (point-min)))))
883 (defun eww-end-of-field ()
884 (1- (next-single-property-change
885 (point) 'eww-form nil (point-max))))
887 (defun eww-tag-form (dom)
888 (let ((eww-form (list (cons :method (dom-attr dom 'method))
889 (cons :action (dom-attr dom 'action))))
891 (shr-ensure-paragraph)
896 (when (> (point) start)
897 (put-text-property start (1+ start)
898 'eww-form eww-form))))
900 (defun eww-form-submit (dom)
901 (let ((start (point))
902 (value (dom-attr dom 'value)))
904 (if (zerop (length value))
908 (add-face-text-property start (point) 'eww-form-submit)
909 (put-text-property start (point) 'eww-form
910 (list :eww-form eww-form
913 :name (dom-attr dom 'name)))
914 (put-text-property start (point) 'keymap eww-submit-map)
917 (defun eww-form-checkbox (dom)
918 (let ((start (point)))
919 (if (dom-attr dom 'checked)
920 (insert eww-form-checkbox-selected-symbol)
921 (insert eww-form-checkbox-symbol))
922 (add-face-text-property start (point) 'eww-form-checkbox)
923 (put-text-property start (point) 'eww-form
924 (list :eww-form eww-form
925 :value (dom-attr dom 'value)
926 :type (downcase (dom-attr dom 'type))
927 :checked (dom-attr dom 'checked)
928 :name (dom-attr dom 'name)))
929 (put-text-property start (point) 'keymap eww-checkbox-map)
932 (defun eww-form-file (dom)
933 (let ((start (point))
934 (value (dom-attr dom 'value)))
936 (if (zerop (length value))
940 (add-face-text-property start (point) 'eww-form-file)
942 (put-text-property start (point) 'eww-form
943 (list :eww-form eww-form
944 :value (dom-attr dom 'value)
945 :type (downcase (dom-attr dom 'type))
946 :name (dom-attr dom 'name)))
947 (put-text-property start (point) 'keymap eww-submit-file)
950 (defun eww-select-file ()
951 "Change the value of the upload file menu under point."
953 (let* ((input (get-text-property (point) 'eww-form)))
955 (let ((insert-default-directory t))
956 (read-file-name "filename: "))))
957 (eww-update-field filename (length "Browse"))
958 (plist-put input :filename filename))))
960 (defun eww-form-text (dom)
961 (let ((start (point))
962 (type (downcase (or (dom-attr dom 'type) "text")))
963 (value (or (dom-attr dom 'value) ""))
964 (width (string-to-number (or (dom-attr dom 'size) "40")))
965 (readonly-property (if (or (dom-attr dom 'disabled)
966 (dom-attr dom 'readonly))
968 'inhibit-read-only)))
970 (when (< (length value) width)
971 (insert (make-string (- width (length value)) ? )))
972 (put-text-property start (point) 'face 'eww-form-text)
973 (put-text-property start (point) 'inhibit-read-only t)
974 (put-text-property start (point) 'local-map eww-text-map)
975 (put-text-property start (point) readonly-property t)
976 (put-text-property start (point) 'eww-form
977 (list :eww-form eww-form
980 :name (dom-attr dom 'name)))
983 (defconst eww-text-input-types '("text" "password" "textarea"
984 "color" "date" "datetime" "datetime-local"
985 "email" "month" "number" "search" "tel"
987 "List of input types which represent a text input.
988 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
990 (defun eww-process-text-input (beg end replace-length)
991 (when-let (pos (and (< (1+ end) (point-max))
992 (> (1- end) (point-min))
994 ((get-text-property (1+ end) 'eww-form)
996 ((get-text-property (1- end) 'eww-form)
998 (let* ((form (get-text-property pos 'eww-form))
999 (properties (text-properties-at pos))
1000 (inhibit-read-only t)
1001 (length (- end beg replace-length))
1002 (type (plist-get form :type)))
1004 (member type eww-text-input-types))
1007 ;; Delete some space at the end.
1010 (if (equal type "textarea")
1011 (1- (line-end-position))
1012 (eww-end-of-field)))
1013 (while (and (> length 0)
1014 (eql (following-char) ? ))
1015 (delete-region (1- (point)) (point))
1020 (goto-char (1- end))
1022 (if (equal type "textarea")
1023 (1- (line-end-position))
1024 (1+ (eww-end-of-field))))
1025 (let ((start (point)))
1026 (insert (make-string (abs length) ? ))
1027 (set-text-properties start (point) properties))
1028 (goto-char (1- end)))))
1029 (set-text-properties (plist-get form :start) (plist-get form :end)
1031 (let ((value (buffer-substring-no-properties
1032 (eww-beginning-of-field)
1033 (eww-end-of-field))))
1034 (when (string-match " +\\'" value)
1035 (setq value (substring value 0 (match-beginning 0))))
1036 (plist-put form :value value)
1037 (when (equal type "password")
1038 ;; Display passwords as asterisks.
1039 (let ((start (eww-beginning-of-field)))
1040 (put-text-property start (+ start (length value))
1041 'display (make-string (length value) ?*)))))))))
1043 (defun eww-tag-textarea (dom)
1044 (let ((start (point))
1045 (value (or (dom-attr dom 'value) ""))
1046 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1047 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1049 (shr-ensure-newline)
1051 (shr-ensure-newline)
1052 (when (< (count-lines start (point)) lines)
1053 (dotimes (_ (- lines (count-lines start (point))))
1055 (setq end (point-marker))
1057 (while (< (point) end)
1059 (let ((pad (- width (- (point) (line-beginning-position)))))
1061 (insert (make-string pad ? ))))
1062 (add-face-text-property (line-beginning-position)
1063 (point) 'eww-form-textarea)
1064 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1065 (put-text-property (line-beginning-position) (point)
1066 'local-map eww-textarea-map)
1068 (put-text-property start (point) 'eww-form
1069 (list :eww-form eww-form
1072 :name (dom-attr dom 'name)))))
1074 (defun eww-tag-input (dom)
1075 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1078 ((or (equal type "checkbox")
1079 (equal type "radio"))
1080 (eww-form-checkbox dom))
1081 ((equal type "file")
1082 (eww-form-file dom))
1083 ((equal type "submit")
1084 (eww-form-submit dom))
1085 ((equal type "hidden")
1086 (let ((form eww-form)
1087 (name (dom-attr dom 'name)))
1088 ;; Don't add <input type=hidden> elements repeatedly.
1090 (or (not (consp (car form)))
1091 (not (eq (caar form) 'hidden))
1092 (not (equal (plist-get (cdr (car form)) :name)
1094 (setq form (cdr form)))
1096 (nconc eww-form (list
1099 :value (dom-attr dom 'value)))))))
1101 (eww-form-text dom)))
1102 (unless (= start (point))
1103 (put-text-property start (1+ start) 'help-echo "Input field"))))
1105 (defun eww-tag-select (dom)
1106 (shr-ensure-paragraph)
1107 (let ((menu (list :name (dom-attr dom 'name)
1108 :eww-form eww-form))
1113 (if (eq (dom-tag dom) 'optgroup)
1114 (dolist (groupelem (dom-children dom))
1115 (unless (dom-attr groupelem 'disabled)
1116 (setq opelem (append opelem (list groupelem)))))
1117 (setq opelem (list dom)))
1118 (dolist (elem opelem)
1119 (when (eq (dom-tag elem) 'option)
1120 (when (dom-attr elem 'selected)
1121 (nconc menu (list :value (dom-attr elem 'value))))
1122 (let ((display (dom-text elem)))
1123 (setq max (max max (length display)))
1125 :value (dom-attr elem 'value)
1129 (setq options (nreverse options))
1130 ;; If we have no selected values, default to the first value.
1131 (unless (plist-get menu :value)
1132 (nconc menu (list :value (nth 2 (car options)))))
1133 (nconc menu options)
1134 (let ((selected (eww-select-display menu)))
1136 (make-string (- max (length selected)) ? )))
1137 (put-text-property start (point) 'eww-form menu)
1138 (add-face-text-property start (point) 'eww-form-select)
1139 (put-text-property start (point) 'keymap eww-select-map)
1140 (unless (= start (point))
1141 (put-text-property start (1+ start) 'help-echo "select field"))
1142 (shr-ensure-paragraph))))
1144 (defun eww-select-display (select)
1145 (let ((value (plist-get select :value))
1147 (dolist (elem select)
1148 (when (and (consp elem)
1149 (eq (car elem) 'item)
1150 (equal value (plist-get (cdr elem) :value)))
1151 (setq display (plist-get (cdr elem) :display))))
1154 (defun eww-change-select ()
1155 "Change the value of the select drop-down menu under point."
1157 (let* ((input (get-text-property (point) 'eww-form))
1158 (completion-ignore-case t)
1161 (mapcar (lambda (elem)
1163 (eq (car elem) 'item)
1164 (cons (plist-get (cdr elem) :display)
1165 (plist-get (cdr elem) :value))))
1168 (completing-read "Change value: " options nil 'require-match))
1169 (inhibit-read-only t))
1170 (plist-put input :value (cdr (assoc-string display options t)))
1172 (eww-update-field display))))
1174 (defun eww-update-field (string &optional offset)
1175 (if (not offset) (setq offset 0))
1176 (let ((properties (text-properties-at (point)))
1177 (start (+ (eww-beginning-of-field) offset))
1178 (current-end (1+ (eww-end-of-field)))
1179 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1180 (delete-region start current-end)
1181 (forward-char offset)
1183 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1184 (if (= 0 offset) (set-text-properties start new-end properties))
1187 (defun eww-toggle-checkbox ()
1188 "Toggle the value of the checkbox under point."
1190 (let* ((input (get-text-property (point) 'eww-form))
1191 (type (plist-get input :type)))
1192 (if (equal type "checkbox")
1195 (if (plist-get input :checked)
1197 (plist-put input :checked nil)
1198 (eww-update-field eww-form-checkbox-symbol))
1199 (plist-put input :checked t)
1200 (eww-update-field eww-form-checkbox-selected-symbol))))
1201 ;; Radio button. Switch all other buttons off.
1202 (let ((name (plist-get input :name)))
1204 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1205 (when (equal (plist-get (cdr elem) :name) name)
1206 (goto-char (car elem))
1207 (if (not (eq (cdr elem) input))
1209 (plist-put input :checked nil)
1210 (eww-update-field eww-form-checkbox-symbol))
1211 (plist-put input :checked t)
1212 (eww-update-field eww-form-checkbox-selected-symbol)))))
1213 (forward-char 1)))))
1215 (defun eww-inputs (form)
1216 (let ((start (point-min))
1219 (< start (point-max)))
1220 (when (or (get-text-property start 'eww-form)
1221 (setq start (next-single-property-change start 'eww-form)))
1222 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1224 (push (cons start (get-text-property start 'eww-form))
1226 (setq start (next-single-property-change start 'eww-form))))
1229 (defun eww-size-text-inputs ()
1230 (let ((start (point-min)))
1232 (< start (point-max)))
1233 (when (or (get-text-property start 'eww-form)
1234 (setq start (next-single-property-change start 'eww-form)))
1235 (let ((props (get-text-property start 'eww-form)))
1236 (plist-put props :start start)
1237 (setq start (next-single-property-change
1238 start 'eww-form nil (point-max)))
1239 (plist-put props :end start))))))
1241 (defun eww-input-value (input)
1242 (let ((type (plist-get input :type))
1243 (value (plist-get input :value)))
1245 ((equal type "textarea")
1248 (goto-char (point-min))
1249 (while (re-search-forward "^ +\n\\| +$" nil t)
1250 (replace-match "" t t))
1253 (if (string-match " +\\'" value)
1254 (substring value 0 (match-beginning 0))
1257 (defun eww-submit ()
1258 "Submit the current form."
1260 (let* ((this-input (get-text-property (point) 'eww-form))
1261 (form (plist-get this-input :eww-form))
1263 (dolist (elem (sort (eww-inputs form)
1265 (< (car o1) (car o2)))))
1266 (let* ((input (cdr elem))
1267 (input-start (car elem))
1268 (name (plist-get input :name)))
1271 ((member (plist-get input :type) '("checkbox" "radio"))
1272 (when (plist-get input :checked)
1273 (push (cons name (plist-get input :value))
1275 ((equal (plist-get input :type) "file")
1277 (list (cons "filedata"
1279 (insert-file-contents
1280 (plist-get input :filename))
1282 (cons "name" (plist-get input :name))
1283 (cons "filename" (plist-get input :filename))))
1285 ((equal (plist-get input :type) "submit")
1286 ;; We want the values from buttons if we hit a button if
1287 ;; we hit enter on it, or if it's the first button after
1288 ;; the field we did hit return on.
1289 (when (or (eq input this-input)
1290 (and (not (eq input this-input))
1292 (> input-start (point))))
1293 (setq next-submit t)
1294 (push (cons name (plist-get input :value))
1297 (push (cons name (eww-input-value input))
1300 (when (and (consp elem)
1301 (eq (car elem) 'hidden))
1302 (push (cons (plist-get (cdr elem) :name)
1303 (or (plist-get (cdr elem) :value) ""))
1305 (if (and (stringp (cdr (assq :method form)))
1306 (equal (downcase (cdr (assq :method form))) "post"))
1308 (dolist (x values mtype)
1309 (if (equal (car x) "file")
1311 (setq mtype "multipart/form-data"))))
1312 (cond ((equal mtype "multipart/form-data")
1313 (let ((boundary (mml-compute-boundary '())))
1314 (let ((url-request-method "POST")
1315 (url-request-extra-headers
1316 (list (cons "Content-Type"
1317 (concat "multipart/form-data; boundary="
1320 (mm-url-encode-multipart-form-data values boundary)))
1321 (eww-browse-url (shr-expand-url
1322 (cdr (assq :action form))
1323 (plist-get eww-data :url))))))
1325 (let ((url-request-method "POST")
1326 (url-request-extra-headers
1328 "application/x-www-form-urlencoded")))
1330 (mm-url-encode-www-form-urlencoded values)))
1331 (eww-browse-url (shr-expand-url
1332 (cdr (assq :action form))
1333 (plist-get eww-data :url)))))))
1336 (if (cdr (assq :action form))
1337 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1338 (plist-get eww-data :url))
1340 (mm-url-encode-www-form-urlencoded values))))))
1342 (defun eww-browse-with-external-browser (&optional url)
1343 "Browse the current URL with an external browser.
1344 The browser to used is specified by the `shr-external-browser' variable."
1346 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1348 (defun eww-follow-link (&optional external mouse-event)
1349 "Browse the URL under point.
1350 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1351 If EXTERNAL is double prefix, browse in new buffer."
1352 (interactive (list current-prefix-arg last-nonmenu-event))
1353 (mouse-set-point mouse-event)
1354 (let ((url (get-text-property (point) 'shr-url)))
1357 (message "No link under point"))
1358 ((string-match "^mailto:" url)
1359 (browse-url-mail url))
1360 ((and (consp external) (<= (car external) 4))
1361 (funcall shr-external-browser url))
1362 ;; This is a #target url in the same page as the current one.
1363 ((and (url-target (url-generic-parse-url url))
1364 (eww-same-page-p url (plist-get eww-data :url)))
1365 (let ((dom (plist-get eww-data :dom)))
1367 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1369 (eww-browse-url url external)))))
1371 (defun eww-same-page-p (url1 url2)
1372 "Return non-nil if both URLs represent the same page.
1373 Differences in #targets are ignored."
1374 (let ((obj1 (url-generic-parse-url url1))
1375 (obj2 (url-generic-parse-url url2)))
1376 (setf (url-target obj1) nil)
1377 (setf (url-target obj2) nil)
1378 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1380 (defun eww-copy-page-url ()
1381 "Copy the URL of the current page into the kill ring."
1383 (message "%s" (plist-get eww-data :url))
1384 (kill-new (plist-get eww-data :url)))
1386 (defun eww-download ()
1387 "Download URL under point to `eww-download-directory'."
1389 (let ((url (get-text-property (point) 'shr-url)))
1391 (message "No URL under point")
1392 (url-retrieve url 'eww-download-callback (list url)))))
1394 (defun eww-download-callback (status url)
1395 (unless (plist-get status :error)
1396 (let* ((obj (url-generic-parse-url url))
1397 (path (car (url-path-and-query obj)))
1398 (file (eww-make-unique-file-name (file-name-nondirectory path)
1399 eww-download-directory)))
1400 (goto-char (point-min))
1401 (re-search-forward "\r?\n\r?\n")
1402 (write-region (point) (point-max) file)
1403 (message "Saved %s" file))))
1405 (defun eww-make-unique-file-name (file directory)
1407 ((zerop (length file))
1409 ((string-match "\\`[.]" file)
1410 (setq file (concat "!" file))))
1412 (while (file-exists-p (expand-file-name file directory))
1414 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1415 (format "%s(%d)%s" (match-string 1 file)
1416 count (match-string 2 file))
1417 (format "%s(%d)" file count)))
1418 (setq count (1+ count)))
1419 (expand-file-name file directory)))
1421 (defun eww-set-character-encoding (charset)
1422 "Set character encoding."
1423 (interactive "zUse character set (default utf-8): ")
1425 (eww-reload nil 'utf-8)
1426 (eww-reload nil charset)))
1430 (defvar eww-bookmarks nil)
1432 (defun eww-add-bookmark ()
1433 "Add the current page to the bookmarks."
1435 (eww-read-bookmarks)
1436 (dolist (bookmark eww-bookmarks)
1437 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1438 (user-error "Already bookmarked")))
1439 (if (y-or-n-p "bookmark this page? ")
1441 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1442 (plist-get eww-data :title))))
1443 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1444 (push (list :url (plist-get eww-data :url)
1446 :time (current-time-string))
1448 (eww-write-bookmarks)
1449 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1450 (plist-get eww-data :title)))))
1452 (defun eww-write-bookmarks ()
1453 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1454 (insert ";; Auto-generated file; don't edit\n")
1455 (pp eww-bookmarks (current-buffer))))
1457 (defun eww-read-bookmarks ()
1458 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1460 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1462 (insert-file-contents file)
1463 (read (current-buffer)))))))
1466 (defun eww-list-bookmarks ()
1467 "Display the bookmarks."
1469 (eww-bookmark-prepare)
1470 (pop-to-buffer "*eww bookmarks*"))
1472 (defun eww-bookmark-prepare ()
1473 (eww-read-bookmarks)
1474 (unless eww-bookmarks
1475 (user-error "No bookmarks are defined"))
1476 (set-buffer (get-buffer-create "*eww bookmarks*"))
1478 (let* ((width (/ (window-width) 2))
1479 (format (format "%%-%ds %%s" width))
1480 (inhibit-read-only t)
1483 (setq header-line-format (concat " " (format format "Title" "URL")))
1484 (dolist (bookmark eww-bookmarks)
1486 title (plist-get bookmark :title))
1487 (when (> (length title) width)
1488 (setq title (substring title 0 width)))
1489 (insert (format format title (plist-get bookmark :url)) "\n")
1490 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1491 (goto-char (point-min))))
1493 (defvar eww-bookmark-kill-ring nil)
1495 (defun eww-bookmark-kill ()
1496 "Kill the current bookmark."
1498 (let* ((start (line-beginning-position))
1499 (bookmark (get-text-property start 'eww-bookmark))
1500 (inhibit-read-only t))
1502 (user-error "No bookmark on the current line"))
1504 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1505 (delete-region start (point))
1506 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1507 (eww-write-bookmarks)))
1509 (defun eww-bookmark-yank ()
1510 "Yank a previously killed bookmark to the current line."
1512 (unless eww-bookmark-kill-ring
1513 (user-error "No previously killed bookmark"))
1515 (let ((inhibit-read-only t)
1518 (insert (pop eww-bookmark-kill-ring))
1519 (setq bookmark (get-text-property start 'eww-bookmark))
1520 (if (= start (point-min))
1521 (push bookmark eww-bookmarks)
1522 (let ((line (count-lines start (point))))
1523 (setcdr (nthcdr (1- line) eww-bookmarks)
1524 (cons bookmark (nthcdr line eww-bookmarks)))))
1525 (eww-write-bookmarks)))
1527 (defun eww-bookmark-browse ()
1528 "Browse the bookmark under point in eww."
1530 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1532 (user-error "No bookmark on the current line"))
1534 (eww-browse-url (plist-get bookmark :url))))
1536 (defun eww-next-bookmark ()
1537 "Go to the next bookmark in the list."
1541 (unless (get-buffer "*eww bookmarks*")
1543 (eww-bookmark-prepare))
1544 (with-current-buffer (get-buffer "*eww bookmarks*")
1545 (when (and (not first)
1548 (setq bookmark (get-text-property (line-beginning-position)
1551 (user-error "No next bookmark")))
1552 (eww-browse-url (plist-get bookmark :url))))
1554 (defun eww-previous-bookmark ()
1555 "Go to the previous bookmark in the list."
1559 (unless (get-buffer "*eww bookmarks*")
1561 (eww-bookmark-prepare))
1562 (with-current-buffer (get-buffer "*eww bookmarks*")
1564 (goto-char (point-max))
1565 (beginning-of-line))
1566 ;; On the final line.
1570 (user-error "No previous bookmark")
1572 (setq bookmark (get-text-property (line-beginning-position)
1574 (eww-browse-url (plist-get bookmark :url))))
1576 (defvar eww-bookmark-mode-map
1577 (let ((map (make-sparse-keymap)))
1578 (suppress-keymap map)
1579 (define-key map "q" 'quit-window)
1580 (define-key map [(control k)] 'eww-bookmark-kill)
1581 (define-key map [(control y)] 'eww-bookmark-yank)
1582 (define-key map "\r" 'eww-bookmark-browse)
1584 (easy-menu-define nil map
1585 "Menu for `eww-bookmark-mode-map'."
1587 ["Exit" quit-window t]
1588 ["Browse" eww-bookmark-browse
1589 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1590 ["Kill" eww-bookmark-kill
1591 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1592 ["Yank" eww-bookmark-yank
1593 :active eww-bookmark-kill-ring]))
1596 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1597 "Mode for listing bookmarks.
1599 \\{eww-bookmark-mode-map}"
1600 (buffer-disable-undo)
1601 (setq buffer-read-only t
1606 (defun eww-save-history ()
1607 (plist-put eww-data :point (point))
1608 (plist-put eww-data :text (buffer-string))
1609 (push eww-data eww-history)
1610 (setq eww-data (list :title ""))
1611 ;; Don't let the history grow infinitely. We store quite a lot of
1613 (when-let (tail (and eww-history-limit
1614 (nthcdr eww-history-limit eww-history)))
1617 (defvar eww-current-buffer)
1619 (defun eww-list-histories ()
1620 "List the eww-histories."
1622 (when (null eww-history)
1623 (error "No eww-histories are defined"))
1624 (let ((eww-history-trans eww-history)
1625 (buffer (current-buffer)))
1626 (set-buffer (get-buffer-create "*eww history*"))
1628 (setq-local eww-current-buffer buffer)
1629 (let ((inhibit-read-only t)
1632 url title format start)
1634 (dolist (history eww-history-trans)
1635 (setq start (point))
1636 (setq domain-length (max domain-length (length (plist-get history :url))))
1637 (setq title-length (max title-length (length (plist-get history :title)))))
1638 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1640 (concat " " (format format "Title" "URL")))
1641 (dolist (history eww-history-trans)
1642 (setq start (point))
1643 (setq url (plist-get history :url))
1644 (setq title (plist-get history :title))
1645 (insert (format format title url))
1647 (put-text-property start (1+ start) 'eww-history history))
1648 (goto-char (point-min)))
1649 (pop-to-buffer "*eww history*")))
1651 (defun eww-history-browse ()
1652 "Browse the history under point in eww."
1654 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1656 (error "No history on the current line"))
1657 (let ((buffer eww-current-buffer))
1660 (switch-to-buffer buffer)))
1661 (eww-restore-history history)))
1663 (defvar eww-history-mode-map
1664 (let ((map (make-sparse-keymap)))
1665 (suppress-keymap map)
1666 (define-key map "q" 'quit-window)
1667 (define-key map "\r" 'eww-history-browse)
1668 ;; (define-key map "n" 'next-error-no-select)
1669 ;; (define-key map "p" 'previous-error-no-select)
1671 (easy-menu-define nil map
1672 "Menu for `eww-history-mode-map'."
1674 ["Exit" quit-window t]
1675 ["Browse" eww-history-browse
1676 :active (get-text-property (line-beginning-position) 'eww-history)]))
1679 (define-derived-mode eww-history-mode nil "eww history"
1680 "Mode for listing eww-histories.
1682 \\{eww-history-mode-map}"
1683 (buffer-disable-undo)
1684 (setq buffer-read-only t
1687 ;;; eww buffers list
1689 (defun eww-list-buffers ()
1690 "Enlist eww buffers."
1693 (current (current-buffer)))
1694 (dolist (buffer (buffer-list))
1695 (with-current-buffer buffer
1696 (when (derived-mode-p 'eww-mode)
1697 (push (vector buffer (plist-get eww-data :title)
1698 (plist-get eww-data :url))
1700 (unless buffers-info
1701 (error "No eww buffers"))
1702 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1703 (set-buffer (get-buffer-create "*eww buffers*"))
1705 (let ((inhibit-read-only t)
1708 url title format start)
1710 (dolist (buffer-info buffers-info)
1711 (setq title-length (max title-length
1712 (length (elt buffer-info 1)))
1713 domain-length (max domain-length
1714 (length (elt buffer-info 2)))))
1715 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1717 (concat " " (format format "Title" "URL")))
1719 (current-buffer-line 1))
1720 (dolist (buffer-info buffers-info)
1722 title (elt buffer-info 1)
1723 url (elt buffer-info 2)
1725 (insert (format format title url))
1727 (let ((buffer (elt buffer-info 0)))
1728 (put-text-property start (1+ start) 'eww-buffer
1730 (when (eq current buffer)
1731 (setq current-buffer-line line))))
1732 (goto-char (point-min))
1733 (forward-line (1- current-buffer-line)))))
1734 (pop-to-buffer "*eww buffers*"))
1736 (defun eww-buffer-select ()
1737 "Switch to eww buffer."
1739 (let ((buffer (get-text-property (line-beginning-position)
1742 (error "No buffer on current line"))
1744 (switch-to-buffer buffer)))
1746 (defun eww-buffer-show ()
1747 "Display buffer under point in eww buffer list."
1748 (let ((buffer (get-text-property (line-beginning-position)
1751 (error "No buffer on current line"))
1753 (switch-to-buffer buffer)
1756 (defun eww-buffer-show-next ()
1757 "Move to next eww buffer in the list and display it."
1761 (goto-char (point-min)))
1764 (defun eww-buffer-show-previous ()
1765 "Move to previous eww buffer in the list and display it."
1769 (goto-char (point-max)))
1773 (defun eww-buffer-kill ()
1774 "Kill buffer from eww list."
1776 (let* ((start (line-beginning-position))
1777 (buffer (get-text-property start 'eww-buffer))
1778 (inhibit-read-only t))
1780 (user-error "No buffer on the current line"))
1781 (kill-buffer buffer)
1783 (delete-region start (point)))
1788 (defvar eww-buffers-mode-map
1789 (let ((map (make-sparse-keymap)))
1790 (suppress-keymap map)
1791 (define-key map "q" 'quit-window)
1792 (define-key map [(control k)] 'eww-buffer-kill)
1793 (define-key map "\r" 'eww-buffer-select)
1794 (define-key map "n" 'eww-buffer-show-next)
1795 (define-key map "p" 'eww-buffer-show-previous)
1797 (easy-menu-define nil map
1798 "Menu for `eww-buffers-mode-map'."
1800 ["Exit" quit-window t]
1801 ["Select" eww-buffer-select
1802 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1803 ["Kill" eww-buffer-kill
1804 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1807 (define-derived-mode eww-buffers-mode nil "eww buffers"
1808 "Mode for listing buffers.
1810 \\{eww-buffers-mode-map}"
1811 (buffer-disable-undo)
1812 (setq buffer-read-only t
1817 (defvar eww-desktop-data-save
1818 '(:url :title :point)
1819 "List of `eww-data' properties to preserve in the desktop file.
1820 Also used when saving `eww-history'.")
1822 (defun eww-desktop-data-1 (alist)
1826 (let ((k (car tail))
1828 (when (memq k eww-desktop-data-save)
1829 (setq acc (cons k (cons v acc)))))
1830 (setq tail (cddr tail)))
1833 (defun eww-desktop-history-duplicate (a b)
1834 (let ((tail a) (r t))
1836 (if (or (memq (car tail) '(:point)) ; ignore :point
1838 (plist-get b (car tail))))
1839 (setq tail (cddr tail))
1845 (defun eww-desktop-misc-data (_directory)
1846 "Return a property list with data used to restore eww buffers.
1847 This list will contain, as :history, the list, whose first element is
1848 the value of `eww-data', and the tail is `eww-history'.
1850 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1851 entries (if any) will be removed from the list.
1853 Only the properties listed in `eww-desktop-data-save' are included.
1854 Generally, the list should not include the (usually overly large)
1855 :dom, :source and :text properties."
1856 (let ((history (mapcar 'eww-desktop-data-1
1857 (cons eww-data eww-history))))
1858 (list :history (if eww-desktop-remove-duplicates
1859 (cl-remove-duplicates
1860 history :test 'eww-desktop-history-duplicate)
1863 (defun eww-restore-desktop (file-name buffer-name misc-data)
1864 "Restore an eww buffer from its desktop file record.
1865 If `eww-restore-desktop' is t or 'auto, this function will also
1866 initiate the retrieval of the respective URI in the background.
1867 Otherwise, the restored buffer will contain a prompt to do so by using
1869 (with-current-buffer (get-buffer-create buffer-name)
1871 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1872 (setq eww-history (cdr (plist-get misc-data :history))
1873 eww-data (or (car (plist-get misc-data :history))
1874 ;; backwards compatibility
1875 (list :url (plist-get misc-data :uri))))
1877 (when (plist-get eww-data :url)
1878 (case eww-restore-desktop
1879 ((t auto) (eww (plist-get eww-data :url)))
1880 ((zerop (buffer-size))
1881 (insert (substitute-command-keys
1882 eww-restore-reload-prompt))))))
1886 (add-to-list 'desktop-locals-to-save
1887 'eww-history-position)
1888 (add-to-list 'desktop-buffer-mode-handlers
1889 '(eww-mode . eww-restore-desktop))
1893 (defun eww-isearch-next-buffer (&optional _buffer wrap)
1894 "Go to the next page to search using `rel' attribute for navigation."
1901 (eww-previous-url)))
1906 ;;; eww.el ends here