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)
33 (eval-when-compile (require 'subr-x)) ;; for string-trim
38 :link '(custom-manual "(eww) Top")
42 (defcustom eww-header-line-format "%t: %u"
44 - %t is replaced by the title.
45 - %u is replaced by the URL."
50 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
51 "Prefix URL to search engine"
56 (defcustom eww-download-directory "~/Downloads/"
57 "Directory where files will downloaded."
62 (defcustom eww-bookmarks-directory user-emacs-directory
63 "Directory where bookmark files will be stored."
68 (defcustom eww-desktop-remove-duplicates t
69 "Whether to remove duplicates from the history when saving desktop data.
70 If non-nil, repetitive EWW history entries (comprising of the URI, the
71 title, and the point position) will not be saved as part of the Emacs
72 desktop. Otherwise, such entries will be retained."
77 (defcustom eww-restore-desktop nil
78 "How to restore EWW buffers on `desktop-restore'.
79 If t or 'auto, the buffers will be reloaded automatically.
80 If nil, buffers will require manual reload, and will contain the text
81 specified in `eww-restore-reload-prompt' instead of the actual Web
85 :type '(choice (const :tag "Restore all automatically" t)
86 (const :tag "Require manual reload" nil)))
88 (defcustom eww-restore-reload-prompt
89 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
90 "The string to put in the buffers not reloaded on `desktop-restore'.
91 This prompt will be used if `eww-restore-desktop' is nil.
93 The string will be passed through `substitute-command-keys'."
98 (defcustom eww-history-limit 50
99 "Maximum number of entries to retain in the history."
102 :type '(choice (const :tag "Unlimited" nil)
104 (defcustom eww-use-external-browser-for-content-type
105 "\\`\\(video/\\|audio/\\|application/ogg\\)"
106 "Always use external browser for specified content-type."
109 :type '(choice (const :tag "Never" nil)
112 (defcustom eww-after-render-hook nil
113 "A hook called after eww has finished rendering the buffer."
118 (defcustom eww-form-checkbox-selected-symbol "[X]"
119 "Symbol used to represent a selected checkbox.
120 See also `eww-form-checkbox-symbol'."
123 :type '(choice (const "[X]")
124 (const "☒") ; Unicode BALLOT BOX WITH X
125 (const "☑") ; Unicode BALLOT BOX WITH CHECK
128 (defcustom eww-form-checkbox-symbol "[ ]"
129 "Symbol used to represent a checkbox.
130 See also `eww-form-checkbox-selected-symbol'."
133 :type '(choice (const "[ ]")
134 (const "☐") ; Unicode BALLOT BOX
137 (defface eww-form-submit
138 '((((type x w32 ns) (class color)) ; Like default mode line
139 :box (:line-width 2 :style released-button)
140 :background "#808080" :foreground "black"))
141 "Face for eww buffer buttons."
145 (defface eww-form-file
146 '((((type x w32 ns) (class color)) ; Like default mode line
147 :box (:line-width 2 :style released-button)
148 :background "#808080" :foreground "black"))
149 "Face for eww buffer buttons."
153 (defface eww-form-checkbox
154 '((((type x w32 ns) (class color)) ; Like default mode line
155 :box (:line-width 2 :style released-button)
156 :background "lightgrey" :foreground "black"))
157 "Face for eww buffer buttons."
161 (defface eww-form-select
162 '((((type x w32 ns) (class color)) ; Like default mode line
163 :box (:line-width 2 :style released-button)
164 :background "lightgrey" :foreground "black"))
165 "Face for eww buffer buttons."
169 (defface eww-form-text
170 '((t (:background "#505050"
172 :box (:line-width 1))))
173 "Face for eww text inputs."
177 (defface eww-form-textarea
178 '((t (:background "#C0C0C0"
180 :box (:line-width 1))))
181 "Face for eww textarea inputs."
185 (defvar eww-data nil)
186 (defvar eww-history nil)
187 (defvar eww-history-position 0)
189 (defvar eww-local-regex "localhost"
190 "When this regex is found in the URL, it's not a keyword but an address.")
192 (defvar eww-link-keymap
193 (let ((map (copy-keymap shr-map)))
194 (define-key map "\r" 'eww-follow-link)
199 "Fetch URL and render the page.
200 If the input doesn't look like an URL or a domain name, the
201 word(s) will be searched for via `eww-search-prefix'."
202 (interactive "sEnter URL or keywords: ")
203 (setq url (string-trim url))
204 (cond ((string-match-p "\\`file:/" url))
205 ;; Don't mangle file: URLs at all.
206 ((string-match-p "\\`ftp://" url)
207 (user-error "FTP is not supported."))
209 (if (and (= (length (split-string url)) 1)
210 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url))
211 (> (length (split-string url "[.:]")) 1))
212 (string-match eww-local-regex url)))
214 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
215 (setq url (concat "http://" url)))
216 ;; some site don't redirect final /
217 (when (string= (url-filename (url-generic-parse-url url)) "")
218 (setq url (concat url "/"))))
219 (setq url (concat eww-search-prefix
220 (replace-regexp-in-string " " "+" url))))))
221 (url-retrieve url 'eww-render
223 (and (eq major-mode 'eww-mode)
226 ;;;###autoload (defalias 'browse-web 'eww)
229 (defun eww-open-file (file)
230 "Render a file using EWW."
231 (interactive "fFile: ")
232 (eww (concat "file://"
233 (and (memq system-type '(windows-nt ms-dos))
235 (expand-file-name file))))
238 (defun eww-search-words (&optional beg end)
239 "Search the web for the text between the point and marker.
240 See the `eww-search-prefix' variable for the search engine used."
242 (eww (buffer-substring beg end)))
244 (defun eww-render (status url &optional point buffer)
245 (let ((redirect (plist-get status :redirect)))
247 (setq url redirect)))
248 (let* ((headers (eww-parse-headers))
250 (mail-header-parse-content-type
251 (or (cdr (assoc "content-type" headers))
255 (or (cdr (assq 'charset (cdr content-type)))
256 (eww-detect-charset (equal (car content-type)
259 (data-buffer (current-buffer)))
262 (plist-put eww-data :title "")
264 ((and eww-use-external-browser-for-content-type
265 (string-match-p eww-use-external-browser-for-content-type
267 (eww-browse-with-external-browser url))
268 ((equal (car content-type) "text/html")
269 (eww-display-html charset url nil point buffer))
270 ((equal (car content-type) "application/pdf")
272 ((string-match-p "\\`image/" (car content-type))
273 (eww-display-image buffer)
274 (eww-update-header-line-format))
276 (eww-display-raw buffer)
277 (eww-update-header-line-format)))
278 (plist-put eww-data :url url)
279 (setq eww-history-position 0)
280 (run-hooks 'eww-after-render-hook))
281 (kill-buffer data-buffer))))
283 (defun eww-parse-headers ()
285 (goto-char (point-min))
286 (while (and (not (eobp))
288 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
289 (push (cons (downcase (match-string 1))
297 (defun eww-detect-charset (html-p)
298 (let ((case-fold-search t)
302 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
306 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
309 (declare-function libxml-parse-html-region "xml.c"
310 (start end &optional base-url))
312 (defun eww-display-html (charset url &optional document point buffer)
313 (or (fboundp 'libxml-parse-html-region)
314 (error "This function requires Emacs to be compiled with libxml2"))
315 ;; There should be a better way to abort loading images
321 'base (list (cons 'href url))
323 (unless (eq charset 'utf-8)
325 (decode-coding-region (point) (point-max) charset)
326 (coding-system-error nil)))
327 (libxml-parse-html-region (point) (point-max))))))
328 (source (and (null document)
329 (buffer-substring (point) (point-max)))))
330 (eww-setup-buffer buffer)
331 (plist-put eww-data :source source)
332 (plist-put eww-data :dom document)
333 (let ((inhibit-read-only t)
334 (after-change-functions nil)
335 (shr-target-id (url-target (url-generic-parse-url url)))
336 (shr-external-rendering-functions
337 '((title . eww-tag-title)
338 (form . eww-tag-form)
339 (input . eww-tag-input)
340 (textarea . eww-tag-textarea)
341 (body . eww-tag-body)
342 (select . eww-tag-select)
343 (link . eww-tag-link)
345 (shr-insert-document document)
350 (goto-char (point-min))
351 (let ((point (next-single-property-change
352 (point-min) 'shr-target-id)))
356 (goto-char (point-min))
357 ;; Don't leave point inside forms, because the normal eww
358 ;; commands aren't available there.
359 (while (and (not (eobp))
360 (get-text-property (point) 'eww-form))
362 (plist-put eww-data :url url)
363 (setq eww-history-position 0)
364 (eww-update-header-line-format)))
366 (defun eww-handle-link (cont)
367 (let* ((rel (assq :rel cont))
368 (href (assq :href cont))
370 ;; The text associated with :rel is case-insensitive.
371 (if rel (downcase (cdr rel)))
373 ;; Texinfo uses "previous", but HTML specifies
374 ;; "prev", so recognize both.
375 ("previous" . :previous)
377 ;; HTML specifies "start" but also "contents",
378 ;; and Gtk seems to use "home". Recognize
379 ;; them all; but store them in different
380 ;; variables so that we can readily choose the
384 ("contents" . :contents)
388 (plist-put eww-data (cdr where) (cdr href)))))
390 (defun eww-tag-link (cont)
391 (eww-handle-link cont)
394 (defun eww-tag-a (cont)
395 (eww-handle-link cont)
396 (let ((start (point)))
398 (put-text-property start (point) 'keymap eww-link-keymap)))
400 (defun eww-update-header-line-format ()
401 (if eww-header-line-format
402 (setq header-line-format
403 (replace-regexp-in-string
405 ;; FIXME? Title can be blank. Default to, eg, last component
407 (format-spec eww-header-line-format
408 `((?u . ,(plist-get eww-data :url))
409 (?t . ,(or (plist-get eww-data :title) ""))))))
410 (setq header-line-format nil)))
412 (defun eww-tag-title (cont)
415 (when (eq (car sub) 'text)
416 (setq title (concat title (cdr sub)))))
417 (plist-put eww-data :title
418 (replace-regexp-in-string
420 (replace-regexp-in-string "[ \t\r\n]+" " " title))))
421 (eww-update-header-line-format))
423 (defun eww-tag-body (cont)
424 (let* ((start (point))
425 (fgcolor (cdr (or (assq :fgcolor cont)
427 (bgcolor (cdr (assq :bgcolor cont)))
428 (shr-stylesheet (list (cons 'color fgcolor)
429 (cons 'background-color bgcolor))))
431 (shr-colorize-region start (point) fgcolor bgcolor)))
433 (defun eww-display-raw (&optional buffer)
434 (let ((data (buffer-substring (point) (point-max))))
435 (eww-setup-buffer buffer)
436 (let ((inhibit-read-only t))
438 (goto-char (point-min))))
440 (defun eww-display-image (&optional buffer)
441 (let ((data (shr-parse-image-data)))
442 (eww-setup-buffer buffer)
443 (let ((inhibit-read-only t))
444 (shr-put-image data nil))
445 (goto-char (point-min))))
447 (defun eww-display-pdf ()
448 (let ((data (buffer-substring (point) (point-max))))
449 (switch-to-buffer (get-buffer-create "*eww pdf*"))
450 (let ((coding-system-for-write 'raw-text)
451 (inhibit-read-only t))
455 (goto-char (point-min)))
457 (defun eww-setup-buffer (&optional buffer)
459 (if (buffer-live-p buffer)
461 (get-buffer-create "*eww*")))
462 (let ((inhibit-read-only t))
465 (unless (eq major-mode 'eww-mode)
468 (defun eww-view-source ()
469 "View the HTML source code of the current page."
471 (let ((buf (get-buffer-create "*eww-source*"))
472 (source (plist-get eww-data :source)))
473 (with-current-buffer buf
474 (let ((inhibit-read-only t))
475 (delete-region (point-min) (point-max))
476 (insert (or source "no source"))
477 (goto-char (point-min))
478 (when (fboundp 'html-mode)
482 (defun eww-readable ()
483 "View the main \"readable\" parts of the current web page.
484 This command uses heuristics to find the parts of the web page that
485 contains the main textual portion, leaving out navigation menus and
488 (let* ((old-data eww-data)
489 (dom (shr-transform-dom
491 (insert (plist-get old-data :source))
493 (decode-coding-region (point-min) (point-max) 'utf-8)
494 (coding-system-error nil))
495 (libxml-parse-html-region (point-min) (point-max))))))
496 (eww-score-readability dom)
498 (eww-display-html nil nil
500 (eww-highest-readability dom))
501 nil (current-buffer))
502 (dolist (elem '(:source :url :title :next :previous :up))
503 (plist-put eww-data elem (plist-get old-data elem)))
504 (eww-update-header-line-format)))
506 (defun eww-score-readability (node)
509 ((memq (car node) '(script head comment))
511 ((eq (car node) 'meta)
513 ((eq (car node) 'img)
516 (setq score (- (length (split-string
517 (or (cdr (assoc 'text (cdr node))) ""))))))
519 (dolist (elem (cdr node))
521 ((and (stringp (cdr elem))
522 (eq (car elem) 'text))
523 (setq score (+ score (length (split-string (cdr elem))))))
526 (or (cdr (assoc :eww-readability-score (cdr elem)))
527 (eww-score-readability elem)))))))))
528 ;; Cache the score of the node to avoid recomputing all the time.
529 (setcdr node (cons (cons :eww-readability-score score) (cdr node)))
532 (defun eww-highest-readability (node)
535 (dolist (elem (cdr node))
536 (when (and (consp (cdr elem))
538 :eww-readability-score
540 (eww-highest-readability elem))))
541 most-negative-fixnum)
542 (or (cdr (assoc :eww-readability-score (cdr result)))
543 most-negative-fixnum)))
544 (setq result highest)))
548 (let ((map (make-sparse-keymap)))
549 (suppress-keymap map)
550 (define-key map "q" 'quit-window)
551 (define-key map "g" 'eww-reload)
552 (define-key map [?\t] 'shr-next-link)
553 (define-key map [?\M-\t] 'shr-previous-link)
554 (define-key map [delete] 'scroll-down-command)
555 (define-key map [?\S-\ ] 'scroll-down-command)
556 (define-key map "\177" 'scroll-down-command)
557 (define-key map " " 'scroll-up-command)
558 (define-key map "l" 'eww-back-url)
559 (define-key map "r" 'eww-forward-url)
560 (define-key map "n" 'eww-next-url)
561 (define-key map "p" 'eww-previous-url)
562 (define-key map "u" 'eww-up-url)
563 (define-key map "t" 'eww-top-url)
564 (define-key map "&" 'eww-browse-with-external-browser)
565 (define-key map "d" 'eww-download)
566 (define-key map "w" 'eww-copy-page-url)
567 (define-key map "C" 'url-cookie-list)
568 (define-key map "v" 'eww-view-source)
569 (define-key map "R" 'eww-readable)
570 (define-key map "H" 'eww-list-histories)
572 (define-key map "b" 'eww-add-bookmark)
573 (define-key map "B" 'eww-list-bookmarks)
574 (define-key map [(meta n)] 'eww-next-bookmark)
575 (define-key map [(meta p)] 'eww-previous-bookmark)
577 (easy-menu-define nil map ""
579 ["Exit" quit-window t]
580 ["Close browser" quit-window t]
581 ["Reload" eww-reload t]
582 ["Back to previous page" eww-back-url
583 :active (not (zerop (length eww-history)))]
584 ["Forward to next page" eww-forward-url
585 :active (not (zerop eww-history-position))]
586 ["Browse with external browser" eww-browse-with-external-browser t]
587 ["Download" eww-download t]
588 ["View page source" eww-view-source]
589 ["Copy page URL" eww-copy-page-url t]
590 ["List histories" eww-list-histories t]
591 ["Add bookmark" eww-add-bookmark t]
592 ["List bookmarks" eww-list-bookmarks t]
593 ["List cookies" url-cookie-list t]))
596 (defvar eww-tool-bar-map
597 (let ((map (make-sparse-keymap)))
598 (dolist (tool-bar-item
599 '((quit-window . "close")
600 (eww-reload . "refresh")
601 (eww-back-url . "left-arrow")
602 (eww-forward-url . "right-arrow")
603 (eww-view-source . "show")
604 (eww-copy-page-url . "copy")
605 (eww-add-bookmark . "bookmark_add"))) ;; ...
606 (tool-bar-local-item-from-menu
607 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
609 "Tool bar for `eww-mode'.")
611 (define-derived-mode eww-mode nil "eww"
612 "Mode for browsing the web.
615 (setq-local eww-data (list :title ""))
616 (setq-local browse-url-browser-function 'eww-browse-url)
617 (setq-local after-change-functions 'eww-process-text-input)
618 (setq-local eww-history nil)
619 (setq-local eww-history-position 0)
620 (when (boundp 'tool-bar-map)
621 (setq-local tool-bar-map eww-tool-bar-map))
623 (setq-local desktop-save-buffer 'eww-desktop-misc-data)
624 (buffer-disable-undo)
625 (setq buffer-read-only t))
628 (defun eww-browse-url (url &optional _new-window)
629 (when (and (equal major-mode 'eww-mode)
630 (plist-get eww-data :url))
634 (defun eww-back-url ()
635 "Go to the previously displayed page."
637 (when (>= eww-history-position (length eww-history))
638 (user-error "No previous page"))
640 (setq eww-history-position (+ eww-history-position 2))
641 (eww-restore-history (elt eww-history (1- eww-history-position))))
643 (defun eww-forward-url ()
644 "Go to the next displayed page."
646 (when (zerop eww-history-position)
647 (user-error "No next page"))
649 (eww-restore-history (elt eww-history (1- eww-history-position))))
651 (defun eww-restore-history (elem)
652 (let ((inhibit-read-only t)
653 (text (plist-get elem :text)))
656 (eww-reload) ; FIXME: restore :point?
659 (goto-char (plist-get elem :point))
660 (eww-update-header-line-format))))
662 (defun eww-next-url ()
663 "Go to the page marked `next'.
664 A page is marked `next' if rel=\"next\" appears in a <link>
667 (if (plist-get eww-data :next)
668 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
669 (plist-get eww-data :url)))
670 (user-error "No `next' on this page")))
672 (defun eww-previous-url ()
673 "Go to the page marked `previous'.
674 A page is marked `previous' if rel=\"previous\" appears in a <link>
677 (if (plist-get eww-data :previous)
678 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
679 (plist-get eww-data :url)))
680 (user-error "No `previous' on this page")))
683 "Go to the page marked `up'.
684 A page is marked `up' if rel=\"up\" appears in a <link>
687 (if (plist-get eww-data :up)
688 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
689 (plist-get eww-data :url)))
690 (user-error "No `up' on this page")))
692 (defun eww-top-url ()
693 "Go to the page marked `top'.
694 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
695 appears in a <link> or <a> tag."
697 (let ((best-url (or (plist-get eww-data :start)
698 (plist-get eww-data :contents)
699 (plist-get eww-data :home))))
701 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
702 (user-error "No `top' for this page"))))
705 "Reload the current page."
707 (let ((url (plist-get eww-data :url)))
708 (url-retrieve url 'eww-render
709 (list url (point) (current-buffer)))))
713 (defvar eww-form nil)
715 (defvar eww-submit-map
716 (let ((map (make-sparse-keymap)))
717 (define-key map "\r" 'eww-submit)
718 (define-key map [(control c) (control c)] 'eww-submit)
721 (defvar eww-submit-file
722 (let ((map (make-sparse-keymap)))
723 (define-key map "\r" 'eww-select-file)
724 (define-key map [(control c) (control c)] 'eww-submit)
727 (defvar eww-checkbox-map
728 (let ((map (make-sparse-keymap)))
729 (define-key map " " 'eww-toggle-checkbox)
730 (define-key map "\r" 'eww-toggle-checkbox)
731 (define-key map [(control c) (control c)] 'eww-submit)
735 (let ((map (make-keymap)))
736 (set-keymap-parent map text-mode-map)
737 (define-key map "\r" 'eww-submit)
738 (define-key map [(control a)] 'eww-beginning-of-text)
739 (define-key map [(control c) (control c)] 'eww-submit)
740 (define-key map [(control e)] 'eww-end-of-text)
741 (define-key map [?\t] 'shr-next-link)
742 (define-key map [?\M-\t] 'shr-previous-link)
745 (defvar eww-textarea-map
746 (let ((map (make-keymap)))
747 (set-keymap-parent map text-mode-map)
748 (define-key map "\r" 'forward-line)
749 (define-key map [(control c) (control c)] 'eww-submit)
750 (define-key map [?\t] 'shr-next-link)
751 (define-key map [?\M-\t] 'shr-previous-link)
754 (defvar eww-select-map
755 (let ((map (make-sparse-keymap)))
756 (define-key map "\r" 'eww-change-select)
757 (define-key map [(control c) (control c)] 'eww-submit)
760 (defun eww-beginning-of-text ()
761 "Move to the start of the input field."
763 (goto-char (eww-beginning-of-field)))
765 (defun eww-end-of-text ()
766 "Move to the end of the text in the input field."
768 (goto-char (eww-end-of-field))
769 (let ((start (eww-beginning-of-field)))
770 (while (and (equal (following-char) ? )
773 (when (> (point) start)
776 (defun eww-beginning-of-field ()
780 ((not (eq (get-text-property (point) 'eww-form)
781 (get-text-property (1- (point)) 'eww-form)))
784 (previous-single-property-change
785 (point) 'eww-form nil (point-min)))))
787 (defun eww-end-of-field ()
788 (1- (next-single-property-change
789 (point) 'eww-form nil (point-max))))
791 (defun eww-tag-form (cont)
793 (list (assq :method cont)
794 (assq :action cont)))
796 (shr-ensure-paragraph)
801 (when (> (point) start)
802 (put-text-property start (1+ start)
803 'eww-form eww-form))))
805 (defun eww-form-submit (cont)
806 (let ((start (point))
807 (value (cdr (assq :value cont))))
809 (if (zerop (length value))
813 (add-face-text-property start (point) 'eww-form-submit)
814 (put-text-property start (point) 'eww-form
815 (list :eww-form eww-form
818 :name (cdr (assq :name cont))))
819 (put-text-property start (point) 'keymap eww-submit-map)
822 (defun eww-form-checkbox (cont)
823 (let ((start (point)))
824 (if (cdr (assq :checked cont))
825 (insert eww-form-checkbox-selected-symbol)
826 (insert eww-form-checkbox-symbol))
827 (add-face-text-property start (point) 'eww-form-checkbox)
828 (put-text-property start (point) 'eww-form
829 (list :eww-form eww-form
830 :value (cdr (assq :value cont))
831 :type (downcase (cdr (assq :type cont)))
832 :checked (cdr (assq :checked cont))
833 :name (cdr (assq :name cont))))
834 (put-text-property start (point) 'keymap eww-checkbox-map)
837 (defun eww-form-file (cont)
838 (let ((start (point))
839 (value (cdr (assq :value cont))))
841 (if (zerop (length value))
845 (add-face-text-property start (point) 'eww-form-file)
847 (put-text-property start (point) 'eww-form
848 (list :eww-form eww-form
849 :value (cdr (assq :value cont))
850 :type (downcase (cdr (assq :type cont)))
851 :name (cdr (assq :name cont))))
852 (put-text-property start (point) 'keymap eww-submit-file)
855 (defun eww-select-file ()
856 "Change the value of the upload file menu under point."
858 (let* ((input (get-text-property (point) 'eww-form)))
860 (let ((insert-default-directory t))
861 (read-file-name "filename: "))))
862 (eww-update-field filename (length "Browse"))
863 (plist-put input :filename filename))))
865 (defun eww-form-text (cont)
866 (let ((start (point))
867 (type (downcase (or (cdr (assq :type cont))
869 (value (or (cdr (assq :value cont)) ""))
870 (width (string-to-number
871 (or (cdr (assq :size cont))
873 (readonly-property (if (or (cdr (assq :disabled cont))
874 (cdr (assq :readonly cont)))
876 'inhibit-read-only)))
878 (when (< (length value) width)
879 (insert (make-string (- width (length value)) ? )))
880 (put-text-property start (point) 'face 'eww-form-text)
881 (put-text-property start (point) 'inhibit-read-only t)
882 (put-text-property start (point) 'local-map eww-text-map)
883 (put-text-property start (point) readonly-property t)
884 (put-text-property start (point) 'eww-form
885 (list :eww-form eww-form
888 :name (cdr (assq :name cont))))
891 (defconst eww-text-input-types '("text" "password" "textarea"
892 "color" "date" "datetime" "datetime-local"
893 "email" "month" "number" "search" "tel"
895 "List of input types which represent a text input.
896 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
898 (defun eww-process-text-input (beg end length)
899 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
900 (properties (text-properties-at end))
901 (type (plist-get form :type)))
903 (member type eww-text-input-types))
906 ;; Delete some space at the end.
909 (if (equal type "textarea")
910 (1- (line-end-position))
912 (let ((new (- end beg)))
913 (while (and (> new 0)
914 (eql (following-char) ? ))
915 (delete-region (point) (1+ (point)))
916 (setq new (1- new))))
917 (set-text-properties beg end properties)))
922 (if (equal type "textarea")
923 (1- (line-end-position))
925 (let ((start (point)))
926 (insert (make-string length ? ))
927 (set-text-properties start (point) properties)))))
928 (let ((value (buffer-substring-no-properties
929 (eww-beginning-of-field)
930 (eww-end-of-field))))
931 (when (string-match " +\\'" value)
932 (setq value (substring value 0 (match-beginning 0))))
933 (plist-put form :value value)
934 (when (equal type "password")
935 ;; Display passwords as asterisks.
936 (let ((start (eww-beginning-of-field)))
937 (put-text-property start (+ start (length value))
938 'display (make-string (length value) ?*))))))))
940 (defun eww-tag-textarea (cont)
941 (let ((start (point))
942 (value (or (cdr (assq :value cont)) ""))
943 (lines (string-to-number
944 (or (cdr (assq :rows cont))
946 (width (string-to-number
947 (or (cdr (assq :cols cont))
953 (when (< (count-lines start (point)) lines)
954 (dotimes (i (- lines (count-lines start (point))))
956 (setq end (point-marker))
958 (while (< (point) end)
960 (let ((pad (- width (- (point) (line-beginning-position)))))
962 (insert (make-string pad ? ))))
963 (add-face-text-property (line-beginning-position)
964 (point) 'eww-form-textarea)
965 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
966 (put-text-property (line-beginning-position) (point)
967 'local-map eww-textarea-map)
969 (put-text-property start (point) 'eww-form
970 (list :eww-form eww-form
973 :name (cdr (assq :name cont))))))
975 (defun eww-tag-input (cont)
976 (let ((type (downcase (or (cdr (assq :type cont))
980 ((or (equal type "checkbox")
981 (equal type "radio"))
982 (eww-form-checkbox cont))
984 (eww-form-file cont))
985 ((equal type "submit")
986 (eww-form-submit cont))
987 ((equal type "hidden")
988 (let ((form eww-form)
989 (name (cdr (assq :name cont))))
990 ;; Don't add <input type=hidden> elements repeatedly.
992 (or (not (consp (car form)))
993 (not (eq (caar form) 'hidden))
994 (not (equal (plist-get (cdr (car form)) :name)
996 (setq form (cdr form)))
998 (nconc eww-form (list
1001 :value (cdr (assq :value cont))))))))
1003 (eww-form-text cont)))
1004 (unless (= start (point))
1005 (put-text-property start (1+ start) 'help-echo "Input field"))))
1007 (defun eww-tag-select (cont)
1008 (shr-ensure-paragraph)
1009 (let ((menu (list :name (cdr (assq :name cont))
1010 :eww-form eww-form))
1015 (if (eq (car (car cont)) 'optgroup)
1016 (dolist (groupelem cont)
1017 (unless (cdr (assq :disabled (cdr groupelem)))
1018 (setq opelem (append opelem (cdr (cdr groupelem))))))
1020 (dolist (elem opelem)
1021 (when (eq (car elem) 'option)
1022 (when (cdr (assq :selected (cdr elem)))
1023 (nconc menu (list :value
1024 (cdr (assq :value (cdr elem))))))
1025 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
1026 (setq max (max max (length display)))
1028 :value (cdr (assq :value (cdr elem)))
1032 (setq options (nreverse options))
1033 ;; If we have no selected values, default to the first value.
1034 (unless (plist-get menu :value)
1035 (nconc menu (list :value (nth 2 (car options)))))
1036 (nconc menu options)
1037 (let ((selected (eww-select-display menu)))
1039 (make-string (- max (length selected)) ? )))
1040 (put-text-property start (point) 'eww-form menu)
1041 (add-face-text-property start (point) 'eww-form-select)
1042 (put-text-property start (point) 'keymap eww-select-map)
1043 (unless (= start (point))
1044 (put-text-property start (1+ start) 'help-echo "select field"))
1045 (shr-ensure-paragraph))))
1047 (defun eww-select-display (select)
1048 (let ((value (plist-get select :value))
1050 (dolist (elem select)
1051 (when (and (consp elem)
1052 (eq (car elem) 'item)
1053 (equal value (plist-get (cdr elem) :value)))
1054 (setq display (plist-get (cdr elem) :display))))
1057 (defun eww-change-select ()
1058 "Change the value of the select drop-down menu under point."
1060 (let* ((input (get-text-property (point) 'eww-form))
1061 (completion-ignore-case t)
1064 (mapcar (lambda (elem)
1066 (eq (car elem) 'item)
1067 (cons (plist-get (cdr elem) :display)
1068 (plist-get (cdr elem) :value))))
1071 (completing-read "Change value: " options nil 'require-match))
1072 (inhibit-read-only t))
1073 (plist-put input :value (cdr (assoc-string display options t)))
1075 (eww-update-field display))))
1077 (defun eww-update-field (string &optional offset)
1078 (if (not offset) (setq offset 0))
1079 (let ((properties (text-properties-at (point)))
1080 (start (+ (eww-beginning-of-field) offset))
1081 (current-end (1+ (eww-end-of-field)))
1082 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1083 (delete-region start current-end)
1084 (forward-char offset)
1086 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1087 (if (= 0 offset) (set-text-properties start new-end properties))
1090 (defun eww-toggle-checkbox ()
1091 "Toggle the value of the checkbox under point."
1093 (let* ((input (get-text-property (point) 'eww-form))
1094 (type (plist-get input :type)))
1095 (if (equal type "checkbox")
1098 (if (plist-get input :checked)
1100 (plist-put input :checked nil)
1101 (eww-update-field eww-form-checkbox-symbol))
1102 (plist-put input :checked t)
1103 (eww-update-field eww-form-checkbox-selected-symbol))))
1104 ;; Radio button. Switch all other buttons off.
1105 (let ((name (plist-get input :name)))
1107 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1108 (when (equal (plist-get (cdr elem) :name) name)
1109 (goto-char (car elem))
1110 (if (not (eq (cdr elem) input))
1112 (plist-put input :checked nil)
1113 (eww-update-field eww-form-checkbox-symbol))
1114 (plist-put input :checked t)
1115 (eww-update-field eww-form-checkbox-selected-symbol)))))
1116 (forward-char 1)))))
1118 (defun eww-inputs (form)
1119 (let ((start (point-min))
1122 (< start (point-max)))
1123 (when (or (get-text-property start 'eww-form)
1124 (setq start (next-single-property-change start 'eww-form)))
1125 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1127 (push (cons start (get-text-property start 'eww-form))
1129 (setq start (next-single-property-change start 'eww-form))))
1132 (defun eww-input-value (input)
1133 (let ((type (plist-get input :type))
1134 (value (plist-get input :value)))
1136 ((equal type "textarea")
1139 (goto-char (point-min))
1140 (while (re-search-forward "^ +\n\\| +$" nil t)
1141 (replace-match "" t t))
1144 (if (string-match " +\\'" value)
1145 (substring value 0 (match-beginning 0))
1148 (defun eww-submit ()
1149 "Submit the current form."
1151 (let* ((this-input (get-text-property (point) 'eww-form))
1152 (form (plist-get this-input :eww-form))
1154 (dolist (elem (sort (eww-inputs form)
1156 (< (car o1) (car o2)))))
1157 (let* ((input (cdr elem))
1158 (input-start (car elem))
1159 (name (plist-get input :name)))
1162 ((member (plist-get input :type) '("checkbox" "radio"))
1163 (when (plist-get input :checked)
1164 (push (cons name (plist-get input :value))
1166 ((equal (plist-get input :type) "file")
1168 (list (cons "filedata"
1170 (insert-file-contents
1171 (plist-get input :filename))
1173 (cons "name" (plist-get input :name))
1174 (cons "filename" (plist-get input :filename))))
1176 ((equal (plist-get input :type) "submit")
1177 ;; We want the values from buttons if we hit a button if
1178 ;; we hit enter on it, or if it's the first button after
1179 ;; the field we did hit return on.
1180 (when (or (eq input this-input)
1181 (and (not (eq input this-input))
1183 (> input-start (point))))
1184 (setq next-submit t)
1185 (push (cons name (plist-get input :value))
1188 (push (cons name (eww-input-value input))
1191 (when (and (consp elem)
1192 (eq (car elem) 'hidden))
1193 (push (cons (plist-get (cdr elem) :name)
1194 (or (plist-get (cdr elem) :value) ""))
1196 (if (and (stringp (cdr (assq :method form)))
1197 (equal (downcase (cdr (assq :method form))) "post"))
1199 (dolist (x values mtype)
1200 (if (equal (car x) "file")
1202 (setq mtype "multipart/form-data"))))
1203 (cond ((equal mtype "multipart/form-data")
1204 (let ((boundary (mml-compute-boundary '())))
1205 (let ((url-request-method "POST")
1206 (url-request-extra-headers
1207 (list (cons "Content-Type"
1208 (concat "multipart/form-data; boundary="
1211 (mm-url-encode-multipart-form-data values boundary)))
1212 (eww-browse-url (shr-expand-url
1213 (cdr (assq :action form))
1214 (plist-get eww-data :url))))))
1216 (let ((url-request-method "POST")
1217 (url-request-extra-headers
1219 "application/x-www-form-urlencoded")))
1221 (mm-url-encode-www-form-urlencoded values)))
1222 (eww-browse-url (shr-expand-url
1223 (cdr (assq :action form))
1224 (plist-get eww-data :url)))))))
1227 (if (cdr (assq :action form))
1228 (shr-expand-url (cdr (assq :action form))
1229 (plist-get eww-data :url))
1230 (plist-get eww-data :url))
1232 (mm-url-encode-www-form-urlencoded values))))))
1234 (defun eww-browse-with-external-browser (&optional url)
1235 "Browse the current URL with an external browser.
1236 The browser to used is specified by the `shr-external-browser' variable."
1238 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1240 (defun eww-follow-link (&optional external mouse-event)
1241 "Browse the URL under point.
1242 If EXTERNAL, browse the URL using `shr-external-browser'."
1243 (interactive (list current-prefix-arg last-nonmenu-event))
1244 (mouse-set-point mouse-event)
1245 (let ((url (get-text-property (point) 'shr-url)))
1248 (message "No link under point"))
1249 ((string-match "^mailto:" url)
1250 (browse-url-mail url))
1252 (funcall shr-external-browser url))
1253 ;; This is a #target url in the same page as the current one.
1254 ((and (url-target (url-generic-parse-url url))
1255 (eww-same-page-p url (plist-get eww-data :url)))
1257 (eww-display-html 'utf-8 url (plist-get eww-data :url)
1258 nil (current-buffer)))
1260 (eww-browse-url url)))))
1262 (defun eww-same-page-p (url1 url2)
1263 "Return non-nil if both URLs represent the same page.
1264 Differences in #targets are ignored."
1265 (let ((obj1 (url-generic-parse-url url1))
1266 (obj2 (url-generic-parse-url url2)))
1267 (setf (url-target obj1) nil)
1268 (setf (url-target obj2) nil)
1269 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1271 (defun eww-copy-page-url ()
1273 (message "%s" (plist-get eww-data :url))
1274 (kill-new (plist-get eww-data :url)))
1276 (defun eww-download ()
1277 "Download URL under point to `eww-download-directory'."
1279 (let ((url (get-text-property (point) 'shr-url)))
1281 (message "No URL under point")
1282 (url-retrieve url 'eww-download-callback (list url)))))
1284 (defun eww-download-callback (status url)
1285 (unless (plist-get status :error)
1286 (let* ((obj (url-generic-parse-url url))
1287 (path (car (url-path-and-query obj)))
1288 (file (eww-make-unique-file-name (file-name-nondirectory path)
1289 eww-download-directory)))
1290 (goto-char (point-min))
1291 (re-search-forward "\r?\n\r?\n")
1292 (write-region (point) (point-max) file)
1293 (message "Saved %s" file))))
1295 (defun eww-make-unique-file-name (file directory)
1297 ((zerop (length file))
1299 ((string-match "\\`[.]" file)
1300 (setq file (concat "!" file))))
1302 (while (file-exists-p (expand-file-name file directory))
1304 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1305 (format "%s(%d)%s" (match-string 1 file)
1306 count (match-string 2 file))
1307 (format "%s(%d)" file count)))
1308 (setq count (1+ count)))
1309 (expand-file-name file directory)))
1313 (defvar eww-bookmarks nil)
1315 (defun eww-add-bookmark ()
1316 "Add the current page to the bookmarks."
1318 (eww-read-bookmarks)
1319 (dolist (bookmark eww-bookmarks)
1320 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1321 (user-error "Already bookmarked")))
1322 (if (y-or-n-p "bookmark this page? ")
1324 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1325 (plist-get eww-data :url))))
1326 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1327 (push (list :url (plist-get eww-data :url)
1329 :time (current-time-string))
1331 (eww-write-bookmarks)
1332 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1333 (plist-get eww-data :title)))))
1335 (defun eww-write-bookmarks ()
1336 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1337 (insert ";; Auto-generated file; don't edit\n")
1338 (pp eww-bookmarks (current-buffer))))
1340 (defun eww-read-bookmarks ()
1341 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1343 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1345 (insert-file-contents file)
1346 (read (current-buffer)))))))
1349 (defun eww-list-bookmarks ()
1350 "Display the bookmarks."
1352 (eww-bookmark-prepare)
1353 (pop-to-buffer "*eww bookmarks*"))
1355 (defun eww-bookmark-prepare ()
1356 (eww-read-bookmarks)
1357 (unless eww-bookmarks
1358 (user-error "No bookmarks are defined"))
1359 (set-buffer (get-buffer-create "*eww bookmarks*"))
1361 (let ((format "%-40s %s")
1362 (inhibit-read-only t)
1365 (setq header-line-format (concat " " (format format "URL" "Title")))
1366 (dolist (bookmark eww-bookmarks)
1367 (setq start (point))
1368 (setq url (plist-get bookmark :url))
1369 (when (> (length url) 40)
1370 (setq url (substring url 0 40)))
1371 (insert (format format url
1372 (plist-get bookmark :title))
1374 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1375 (goto-char (point-min))))
1377 (defvar eww-bookmark-kill-ring nil)
1379 (defun eww-bookmark-kill ()
1380 "Kill the current bookmark."
1382 (let* ((start (line-beginning-position))
1383 (bookmark (get-text-property start 'eww-bookmark))
1384 (inhibit-read-only t))
1386 (user-error "No bookmark on the current line"))
1388 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1389 (delete-region start (point))
1390 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1391 (eww-write-bookmarks)))
1393 (defun eww-bookmark-yank ()
1394 "Yank a previously killed bookmark to the current line."
1396 (unless eww-bookmark-kill-ring
1397 (user-error "No previously killed bookmark"))
1399 (let ((inhibit-read-only t)
1402 (insert (pop eww-bookmark-kill-ring))
1403 (setq bookmark (get-text-property start 'eww-bookmark))
1404 (if (= start (point-min))
1405 (push bookmark eww-bookmarks)
1406 (let ((line (count-lines start (point))))
1407 (setcdr (nthcdr (1- line) eww-bookmarks)
1408 (cons bookmark (nthcdr line eww-bookmarks)))))
1409 (eww-write-bookmarks)))
1411 (defun eww-bookmark-browse ()
1412 "Browse the bookmark under point in eww."
1414 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1416 (user-error "No bookmark on the current line"))
1418 (eww-browse-url (plist-get bookmark :url))))
1420 (defun eww-next-bookmark ()
1421 "Go to the next bookmark in the list."
1425 (unless (get-buffer "*eww bookmarks*")
1427 (eww-bookmark-prepare))
1428 (with-current-buffer (get-buffer "*eww bookmarks*")
1429 (when (and (not first)
1432 (setq bookmark (get-text-property (line-beginning-position)
1435 (user-error "No next bookmark")))
1436 (eww-browse-url (plist-get bookmark :url))))
1438 (defun eww-previous-bookmark ()
1439 "Go to the previous bookmark in the list."
1443 (unless (get-buffer "*eww bookmarks*")
1445 (eww-bookmark-prepare))
1446 (with-current-buffer (get-buffer "*eww bookmarks*")
1448 (goto-char (point-max))
1449 (beginning-of-line))
1450 ;; On the final line.
1454 (user-error "No previous bookmark")
1456 (setq bookmark (get-text-property (line-beginning-position)
1458 (eww-browse-url (plist-get bookmark :url))))
1460 (defvar eww-bookmark-mode-map
1461 (let ((map (make-sparse-keymap)))
1462 (suppress-keymap map)
1463 (define-key map "q" 'quit-window)
1464 (define-key map [(control k)] 'eww-bookmark-kill)
1465 (define-key map [(control y)] 'eww-bookmark-yank)
1466 (define-key map "\r" 'eww-bookmark-browse)
1468 (easy-menu-define nil map
1469 "Menu for `eww-bookmark-mode-map'."
1471 ["Exit" quit-window t]
1472 ["Browse" eww-bookmark-browse
1473 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1474 ["Kill" eww-bookmark-kill
1475 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1476 ["Yank" eww-bookmark-yank
1477 :active eww-bookmark-kill-ring]))
1480 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1481 "Mode for listing bookmarks.
1483 \\{eww-bookmark-mode-map}"
1484 (buffer-disable-undo)
1485 (setq buffer-read-only t
1490 (defun eww-save-history ()
1491 (plist-put eww-data :point (point))
1492 (plist-put eww-data :text (buffer-string))
1493 (push eww-data eww-history)
1494 (setq eww-data (list :title ""))
1495 ;; Don't let the history grow infinitely. We store quite a lot of
1497 (when-let (tail (and eww-history-limit
1498 (nthcdr eww-history-limit eww-history)))
1501 (defun eww-list-histories ()
1502 "List the eww-histories."
1504 (when (null eww-history)
1505 (error "No eww-histories are defined"))
1506 (let ((eww-history-trans eww-history))
1507 (set-buffer (get-buffer-create "*eww history*"))
1509 (let ((inhibit-read-only t)
1512 url title format start)
1514 (dolist (history eww-history-trans)
1515 (setq start (point))
1516 (setq domain-length (max domain-length (length (plist-get history :url))))
1517 (setq title-length (max title-length (length (plist-get history :title)))))
1518 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1520 (concat " " (format format "Title" "URL")))
1521 (dolist (history eww-history-trans)
1522 (setq start (point))
1523 (setq url (plist-get history :url))
1524 (setq title (plist-get history :title))
1525 (insert (format format title url))
1527 (put-text-property start (1+ start) 'eww-history history))
1528 (goto-char (point-min)))
1529 (pop-to-buffer "*eww history*")))
1531 (defun eww-history-browse ()
1532 "Browse the history under point in eww."
1534 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1536 (error "No history on the current line"))
1538 (eww-restore-history history)))
1540 (defvar eww-history-mode-map
1541 (let ((map (make-sparse-keymap)))
1542 (suppress-keymap map)
1543 (define-key map "q" 'quit-window)
1544 (define-key map "\r" 'eww-history-browse)
1545 ;; (define-key map "n" 'next-error-no-select)
1546 ;; (define-key map "p" 'previous-error-no-select)
1548 (easy-menu-define nil map
1549 "Menu for `eww-history-mode-map'."
1551 ["Exit" quit-window t]
1552 ["Browse" eww-history-browse
1553 :active (get-text-property (line-beginning-position) 'eww-history)]))
1556 (define-derived-mode eww-history-mode nil "eww history"
1557 "Mode for listing eww-histories.
1559 \\{eww-history-mode-map}"
1560 (buffer-disable-undo)
1561 (setq buffer-read-only t
1566 (defvar eww-desktop-data-save
1567 '(:url :title :point)
1568 "List of `eww-data' properties to preserve in the desktop file.
1569 Also used when saving `eww-history'.")
1571 (defun eww-desktop-data-1 (alist)
1575 (let ((k (car tail))
1577 (when (memq k eww-desktop-data-save)
1578 (setq acc (cons k (cons v acc)))))
1579 (setq tail (cddr tail)))
1582 (defun eww-desktop-history-duplicate (a b)
1583 (let ((tail a) (r t))
1585 (if (or (memq (car tail) '(:point)) ; ignore :point
1587 (plist-get b (car tail))))
1588 (setq tail (cddr tail))
1594 (defun eww-desktop-misc-data (directory)
1595 "Return a property list with data used to restore eww buffers.
1596 This list will contain, as :history, the list, whose first element is
1597 the value of `eww-data', and the tail is `eww-history'.
1599 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1600 entries (if any) will be removed from the list.
1602 Only the properties listed in `eww-desktop-data-save' are included.
1603 Generally, the list should not include the (usually overly large)
1604 :dom, :source and :text properties."
1605 (let ((history (mapcar 'eww-desktop-data-1
1606 (cons eww-data eww-history))))
1607 (list :history (if eww-desktop-remove-duplicates
1608 (cl-remove-duplicates
1609 history :test 'eww-desktop-history-duplicate)
1612 (defun eww-restore-desktop (file-name buffer-name misc-data)
1613 "Restore an eww buffer from its desktop file record.
1614 If `eww-restore-desktop' is t or 'auto, this function will also
1615 initiate the retrieval of the respective URI in the background.
1616 Otherwise, the restored buffer will contain a prompt to do so by using
1618 (with-current-buffer (get-buffer-create buffer-name)
1620 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1621 (setq eww-history (cdr (plist-get misc-data :history))
1622 eww-data (or (car (plist-get misc-data :history))
1623 ;; backwards compatibility
1624 (list :url (plist-get misc-data :uri))))
1626 (when (plist-get eww-data :url)
1627 (case eww-restore-desktop
1628 ((t auto) (eww (plist-get eww-data :url)))
1629 ((zerop (buffer-size))
1630 (insert (substitute-command-keys
1631 eww-restore-reload-prompt))))))
1635 (add-to-list 'desktop-locals-to-save
1636 'eww-history-position)
1637 (add-to-list 'desktop-buffer-mode-handlers
1638 '(eww-mode . eww-restore-desktop))
1642 ;;; eww.el ends here