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."
64 (defcustom eww-suggest-uris
68 "List of functions called to form the list of default URIs for `eww'.
69 Each of the elements is a function returning either a string or a list
70 of strings. The results will be joined into a single list with
71 duplicate entries (if any) removed."
75 :options '(eww-links-at-point
79 (defcustom eww-bookmarks-directory user-emacs-directory
80 "Directory where bookmark files will be stored."
85 (defcustom eww-desktop-remove-duplicates t
86 "Whether to remove duplicates from the history when saving desktop data.
87 If non-nil, repetitive EWW history entries (comprising of the URI, the
88 title, and the point position) will not be saved as part of the Emacs
89 desktop. Otherwise, such entries will be retained."
94 (defcustom eww-restore-desktop nil
95 "How to restore EWW buffers on `desktop-restore'.
96 If t or `auto', the buffers will be reloaded automatically.
97 If nil, buffers will require manual reload, and will contain the text
98 specified in `eww-restore-reload-prompt' instead of the actual Web
102 :type '(choice (const :tag "Restore all automatically" t)
103 (const :tag "Require manual reload" nil)))
105 (defcustom eww-restore-reload-prompt
106 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
107 "The string to put in the buffers not reloaded on `desktop-restore'.
108 This prompt will be used if `eww-restore-desktop' is nil.
110 The string will be passed through `substitute-command-keys'."
115 (defcustom eww-history-limit 50
116 "Maximum number of entries to retain in the history."
119 :type '(choice (const :tag "Unlimited" nil)
122 (defcustom eww-use-external-browser-for-content-type
123 "\\`\\(video/\\|audio/\\|application/ogg\\)"
124 "Always use external browser for specified content-type."
127 :type '(choice (const :tag "Never" nil)
130 (defcustom eww-after-render-hook nil
131 "A hook called after eww has finished rendering the buffer."
136 (defcustom eww-form-checkbox-selected-symbol "[X]"
137 "Symbol used to represent a selected checkbox.
138 See also `eww-form-checkbox-symbol'."
141 :type '(choice (const "[X]")
142 (const "☒") ; Unicode BALLOT BOX WITH X
143 (const "☑") ; Unicode BALLOT BOX WITH CHECK
146 (defcustom eww-form-checkbox-symbol "[ ]"
147 "Symbol used to represent a checkbox.
148 See also `eww-form-checkbox-selected-symbol'."
151 :type '(choice (const "[ ]")
152 (const "☐") ; Unicode BALLOT BOX
155 (defface eww-form-submit
156 '((((type x w32 ns) (class color)) ; Like default mode line
157 :box (:line-width 2 :style released-button)
158 :background "#808080" :foreground "black"))
159 "Face for eww buffer buttons."
163 (defface eww-form-file
164 '((((type x w32 ns) (class color)) ; Like default mode line
165 :box (:line-width 2 :style released-button)
166 :background "#808080" :foreground "black"))
167 "Face for eww buffer buttons."
171 (defface eww-form-checkbox
172 '((((type x w32 ns) (class color)) ; Like default mode line
173 :box (:line-width 2 :style released-button)
174 :background "lightgrey" :foreground "black"))
175 "Face for eww buffer buttons."
179 (defface eww-form-select
180 '((((type x w32 ns) (class color)) ; Like default mode line
181 :box (:line-width 2 :style released-button)
182 :background "lightgrey" :foreground "black"))
183 "Face for eww buffer buttons."
187 (defface eww-form-text
188 '((t (:background "#505050"
190 :box (:line-width 1))))
191 "Face for eww text inputs."
195 (defface eww-form-textarea
196 '((t (:background "#C0C0C0"
198 :box (:line-width 1))))
199 "Face for eww textarea inputs."
203 (defface eww-invalid-certificate
204 '((default :weight bold)
205 (((class color)) :foreground "red"))
206 "Face for web pages with invalid certificates."
210 (defface eww-valid-certificate
211 '((default :weight bold)
212 (((class color)) :foreground "ForestGreen"))
213 "Face for web pages with valid certificates."
217 (defvar eww-data nil)
218 (defvar eww-history nil)
219 (defvar eww-history-position 0)
221 (defvar eww-local-regex "localhost"
222 "When this regex is found in the URL, it's not a keyword but an address.")
224 (defvar eww-link-keymap
225 (let ((map (copy-keymap shr-map)))
226 (define-key map "\r" 'eww-follow-link)
229 (defun eww-suggested-uris nil
230 "Return the list of URIs to suggest at the `eww' prompt.
231 This list can be customized via `eww-suggest-uris'."
232 (let ((obseen (make-vector 42 0))
234 (dolist (fun eww-suggest-uris)
235 (let ((ret (funcall fun)))
236 (dolist (uri (if (stringp ret) (list ret) ret))
237 (when (and uri (not (intern-soft uri obseen)))
244 "Fetch URL and render the page.
245 If the input doesn't look like an URL or a domain name, the
246 word(s) will be searched for via `eww-search-prefix'."
248 (let* ((uris (eww-suggested-uris))
249 (prompt (concat "Enter URL or keywords"
250 (if uris (format " (default %s)" (car uris)) "")
252 (list (read-string prompt nil nil uris))))
253 (setq url (string-trim url))
254 (cond ((string-match-p "\\`file:/" url))
255 ;; Don't mangle file: URLs at all.
256 ((string-match-p "\\`ftp://" url)
257 (user-error "FTP is not supported"))
259 ;; Anything that starts with something that vaguely looks
260 ;; like a protocol designator is interpreted as a full URL.
261 (if (or (string-match "\\`[A-Za-z]+:" url)
262 ;; Also try to match "naked" URLs like
263 ;; en.wikipedia.org/wiki/Free software
264 (string-match "\\`[A-Za-z_]+\\.[A-Za-z._]+/" url)
265 (and (= (length (split-string url)) 1)
266 (or (and (not (string-match-p "\\`[\"'].*[\"']\\'" url))
267 (> (length (split-string url "[.:]")) 1))
268 (string-match eww-local-regex url))))
270 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
271 (setq url (concat "http://" url)))
272 ;; Some sites do not redirect final /
273 (when (string= (url-filename (url-generic-parse-url url)) "")
274 (setq url (concat url "/"))))
275 (setq url (concat eww-search-prefix
276 (replace-regexp-in-string " " "+" url))))))
278 (plist-put eww-data :url url)
279 (plist-put eww-data :title "")
280 (eww-update-header-line-format)
281 (let ((inhibit-read-only t))
282 (insert (format "Loading %s..." url))
283 (goto-char (point-min)))
284 (url-retrieve url 'eww-render
285 (list url nil (current-buffer))))
287 ;;;###autoload (defalias 'browse-web 'eww)
290 (defun eww-open-file (file)
291 "Render FILE using EWW."
292 (interactive "fFile: ")
293 (eww (concat "file://"
294 (and (memq system-type '(windows-nt ms-dos))
296 (expand-file-name file))))
299 (defun eww-search-words (&optional beg end)
300 "Search the web for the text between BEG and END.
301 See the `eww-search-prefix' variable for the search engine used."
303 (eww (buffer-substring beg end)))
305 (defun eww-html-p (content-type)
306 "Return non-nil if CONTENT-TYPE designates an HTML content type.
307 Currently this means either text/html or application/xhtml+xml."
308 (member content-type '("text/html"
309 "application/xhtml+xml")))
311 (defun eww-render (status url &optional point buffer encode)
312 (let ((redirect (plist-get status :redirect)))
314 (setq url redirect)))
315 (let* ((headers (eww-parse-headers))
317 (mail-header-parse-content-type
318 (or (cdr (assoc "content-type" headers))
322 (or (cdr (assq 'charset (cdr content-type)))
323 (eww-detect-charset (eww-html-p (car content-type)))
325 (data-buffer (current-buffer))
326 last-coding-system-used)
327 ;; Save the https peer status.
328 (with-current-buffer buffer
329 (plist-put eww-data :peer (plist-get status :peer)))
333 ((and eww-use-external-browser-for-content-type
334 (string-match-p eww-use-external-browser-for-content-type
336 (eww-browse-with-external-browser url))
337 ((eww-html-p (car content-type))
338 (eww-display-html charset url nil point buffer encode))
339 ((equal (car content-type) "application/pdf")
341 ((string-match-p "\\`image/" (car content-type))
342 (eww-display-image buffer))
344 (eww-display-raw buffer (or encode charset 'utf-8))))
345 (with-current-buffer buffer
346 (plist-put eww-data :url url)
347 (eww-update-header-line-format)
348 (setq eww-history-position 0)
349 (and last-coding-system-used
350 (set-buffer-file-coding-system last-coding-system-used))
351 (run-hooks 'eww-after-render-hook)))
352 (kill-buffer data-buffer))))
354 (defun eww-parse-headers ()
356 (goto-char (point-min))
357 (while (and (not (eobp))
359 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
360 (push (cons (downcase (match-string 1))
368 (defun eww-detect-charset (html-p)
369 (let ((case-fold-search t)
373 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
377 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
380 (declare-function libxml-parse-html-region "xml.c"
381 (start end &optional base-url discard-comments))
383 (defun eww-display-html (charset url &optional document point buffer encode)
384 (unless (fboundp 'libxml-parse-html-region)
385 (error "This function requires Emacs to be compiled with libxml2"))
386 (unless (buffer-live-p buffer)
387 (error "Buffer %s doesn't exist" buffer))
388 ;; There should be a better way to abort loading images
394 'base (list (cons 'href url))
396 (setq encode (or encode charset 'utf-8))
398 (decode-coding-region (point) (point-max) encode)
399 (coding-system-error nil))
400 (libxml-parse-html-region (point) (point-max))))))
401 (source (and (null document)
402 (buffer-substring (point) (point-max)))))
403 (with-current-buffer buffer
404 (plist-put eww-data :source source)
405 (plist-put eww-data :dom document)
406 (let ((inhibit-read-only t)
407 (inhibit-modification-hooks t)
408 (shr-target-id (url-target (url-generic-parse-url url)))
409 (shr-external-rendering-functions
410 '((title . eww-tag-title)
411 (form . eww-tag-form)
412 (input . eww-tag-input)
413 (textarea . eww-tag-textarea)
414 (select . eww-tag-select)
415 (link . eww-tag-link)
418 (shr-insert-document document)
423 (goto-char (point-min))
424 (let ((point (next-single-property-change
425 (point-min) 'shr-target-id)))
429 (goto-char (point-min))
430 ;; Don't leave point inside forms, because the normal eww
431 ;; commands aren't available there.
432 (while (and (not (eobp))
433 (get-text-property (point) 'eww-form))
435 (eww-size-text-inputs))))
437 (defun eww-handle-link (dom)
438 (let* ((rel (dom-attr dom 'rel))
439 (href (dom-attr dom 'href))
441 ;; The text associated with :rel is case-insensitive.
442 (if rel (downcase rel))
444 ;; Texinfo uses "previous", but HTML specifies
445 ;; "prev", so recognize both.
446 ("previous" . :previous)
448 ;; HTML specifies "start" but also "contents",
449 ;; and Gtk seems to use "home". Recognize
450 ;; them all; but store them in different
451 ;; variables so that we can readily choose the
455 ("contents" . :contents)
459 (plist-put eww-data (cdr where) href))))
461 (defun eww-tag-link (dom)
462 (eww-handle-link dom)
465 (defun eww-tag-a (dom)
466 (eww-handle-link dom)
467 (let ((start (point)))
469 (put-text-property start (point) 'keymap eww-link-keymap)))
471 (defun eww-update-header-line-format ()
472 (setq header-line-format
473 (and eww-header-line-format
474 (let ((title (plist-get eww-data :title))
475 (peer (plist-get eww-data :peer)))
476 (when (zerop (length title))
477 (setq title "[untitled]"))
478 ;; This connection has is https.
481 (propertize title 'face
482 (if (plist-get peer :warnings)
483 'eww-invalid-certificate
484 'eww-valid-certificate))))
485 (replace-regexp-in-string
488 eww-header-line-format
489 `((?u . ,(or (plist-get eww-data :url) ""))
492 (defun eww-tag-title (dom)
493 (plist-put eww-data :title
494 (replace-regexp-in-string
496 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
497 (eww-update-header-line-format))
499 (defun eww-display-raw (buffer &optional encode)
500 (let ((data (buffer-substring (point) (point-max))))
501 (unless (buffer-live-p buffer)
502 (error "Buffer %s doesn't exist" buffer))
503 (with-current-buffer buffer
504 (let ((inhibit-read-only t))
508 (decode-coding-region (point-min) (1+ (length data)) encode)
509 (coding-system-error nil)))
510 (goto-char (point-min)))))
512 (defun eww-display-image (buffer)
513 (let ((data (shr-parse-image-data)))
514 (unless (buffer-live-p buffer)
515 (error "Buffer %s doesn't exist" buffer))
516 (with-current-buffer buffer
517 (let ((inhibit-read-only t))
519 (shr-put-image data nil))
520 (goto-char (point-min)))))
522 (declare-function mailcap-view-mime "mailcap" (type))
523 (defun eww-display-pdf ()
524 (let ((data (buffer-substring (point) (point-max))))
525 (switch-to-buffer (get-buffer-create "*eww pdf*"))
526 (let ((coding-system-for-write 'raw-text)
527 (inhibit-read-only t))
530 (mailcap-view-mime "application/pdf")))
531 (goto-char (point-min)))
533 (defun eww-setup-buffer ()
534 (switch-to-buffer (get-buffer-create "*eww*"))
535 (when (or (plist-get eww-data :url)
536 (plist-get eww-data :dom))
538 (let ((inhibit-read-only t))
541 (unless (eq major-mode 'eww-mode)
544 (defun eww-current-url nil
545 "Return URI of the Web page the current EWW buffer is visiting."
546 (plist-get eww-data :url))
548 (defun eww-links-at-point ()
549 "Return list of URIs, if any, linked at point."
551 (list (get-text-property (point) 'shr-url)
552 (get-text-property (point) 'image-url))))
554 (defun eww-view-source ()
555 "View the HTML source code of the current page."
557 (let ((buf (get-buffer-create "*eww-source*"))
558 (source (plist-get eww-data :source)))
559 (with-current-buffer buf
560 (let ((inhibit-read-only t))
561 (delete-region (point-min) (point-max))
562 (insert (or source "no source"))
563 (goto-char (point-min))
564 ;; Decode the source and set the buffer's encoding according
565 ;; to what the HTML source specifies in its 'charset' header,
567 (let ((cs (find-auto-coding "" (point-max))))
570 (when (coding-system-p cs)
571 (decode-coding-region (point-min) (point-max) cs)
572 (setq buffer-file-coding-system last-coding-system-used))))
573 (when (fboundp 'html-mode)
577 (defun eww-readable ()
578 "View the main \"readable\" parts of the current web page.
579 This command uses heuristics to find the parts of the web page that
580 contains the main textual portion, leaving out navigation menus and
583 (let* ((old-data eww-data)
584 (dom (with-temp-buffer
585 (insert (plist-get old-data :source))
587 (decode-coding-region (point-min) (point-max) 'utf-8)
588 (coding-system-error nil))
589 (libxml-parse-html-region (point-min) (point-max)))))
590 (eww-score-readability dom)
592 (eww-display-html nil nil
593 (eww-highest-readability dom)
594 nil (current-buffer))
595 (dolist (elem '(:source :url :title :next :previous :up))
596 (plist-put eww-data elem (plist-get old-data elem)))
597 (eww-update-header-line-format)))
599 (defun eww-score-readability (node)
602 ((memq (dom-tag node) '(script head comment))
604 ((eq (dom-tag node) 'meta)
606 ((eq (dom-tag node) 'img)
608 ((eq (dom-tag node) 'a)
609 (setq score (- (length (split-string (dom-text node))))))
611 (dolist (elem (dom-children node))
613 (setq score (+ score (length (split-string elem))))
615 (or (cdr (assoc :eww-readability-score (cdr elem)))
616 (eww-score-readability elem))))))))
617 ;; Cache the score of the node to avoid recomputing all the time.
618 (dom-set-attribute node :eww-readability-score score)
621 (defun eww-highest-readability (node)
624 (dolist (elem (dom-non-text-children node))
625 (when (> (or (dom-attr
626 (setq highest (eww-highest-readability elem))
627 :eww-readability-score)
628 most-negative-fixnum)
629 (or (dom-attr result :eww-readability-score)
630 most-negative-fixnum))
631 (setq result highest)))
635 (let ((map (make-sparse-keymap)))
636 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
637 (define-key map "G" 'eww)
638 (define-key map [?\t] 'shr-next-link)
639 (define-key map [?\M-\t] 'shr-previous-link)
640 (define-key map [backtab] 'shr-previous-link)
641 (define-key map [delete] 'scroll-down-command)
642 (define-key map "l" 'eww-back-url)
643 (define-key map "r" 'eww-forward-url)
644 (define-key map "n" 'eww-next-url)
645 (define-key map "p" 'eww-previous-url)
646 (define-key map "u" 'eww-up-url)
647 (define-key map "t" 'eww-top-url)
648 (define-key map "&" 'eww-browse-with-external-browser)
649 (define-key map "d" 'eww-download)
650 (define-key map "w" 'eww-copy-page-url)
651 (define-key map "C" 'url-cookie-list)
652 (define-key map "v" 'eww-view-source)
653 (define-key map "R" 'eww-readable)
654 (define-key map "H" 'eww-list-histories)
655 (define-key map "E" 'eww-set-character-encoding)
656 (define-key map "S" 'eww-list-buffers)
657 (define-key map "F" 'eww-toggle-fonts)
659 (define-key map "b" 'eww-add-bookmark)
660 (define-key map "B" 'eww-list-bookmarks)
661 (define-key map [(meta n)] 'eww-next-bookmark)
662 (define-key map [(meta p)] 'eww-previous-bookmark)
664 (easy-menu-define nil map ""
666 ["Exit" quit-window t]
667 ["Close browser" quit-window t]
668 ["Reload" eww-reload t]
669 ["Back to previous page" eww-back-url
670 :active (not (zerop (length eww-history)))]
671 ["Forward to next page" eww-forward-url
672 :active (not (zerop eww-history-position))]
673 ["Browse with external browser" eww-browse-with-external-browser t]
674 ["Download" eww-download t]
675 ["View page source" eww-view-source]
676 ["Copy page URL" eww-copy-page-url t]
677 ["List histories" eww-list-histories t]
678 ["List buffers" eww-list-buffers t]
679 ["Add bookmark" eww-add-bookmark t]
680 ["List bookmarks" eww-list-bookmarks t]
681 ["List cookies" url-cookie-list t]
682 ["Character Encoding" eww-set-character-encoding]))
685 (defvar eww-tool-bar-map
686 (let ((map (make-sparse-keymap)))
687 (dolist (tool-bar-item
688 '((quit-window . "close")
689 (eww-reload . "refresh")
690 (eww-back-url . "left-arrow")
691 (eww-forward-url . "right-arrow")
692 (eww-view-source . "show")
693 (eww-copy-page-url . "copy")
694 (eww-add-bookmark . "bookmark_add"))) ;; ...
695 (tool-bar-local-item-from-menu
696 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
698 "Tool bar for `eww-mode'.")
700 ;; Autoload cookie needed by desktop.el.
702 (define-derived-mode eww-mode special-mode "eww"
703 "Mode for browsing the web."
704 (setq-local eww-data (list :title ""))
705 (setq-local browse-url-browser-function #'eww-browse-url)
706 (add-hook 'after-change-functions #'eww-process-text-input nil t)
707 (setq-local eww-history nil)
708 (setq-local eww-history-position 0)
709 (when (boundp 'tool-bar-map)
710 (setq-local tool-bar-map eww-tool-bar-map))
712 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
713 ;; multi-page isearch support
714 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
715 (setq truncate-lines t
716 bidi-paragraph-direction 'left-to-right)
717 (buffer-disable-undo)
718 (setq buffer-read-only t))
721 (defun eww-browse-url (url &optional new-window)
723 (switch-to-buffer (generate-new-buffer "*eww*"))
727 (defun eww-back-url ()
728 "Go to the previously displayed page."
730 (when (>= eww-history-position (length eww-history))
731 (user-error "No previous page"))
733 (setq eww-history-position (+ eww-history-position 2))
734 (eww-restore-history (elt eww-history (1- eww-history-position))))
736 (defun eww-forward-url ()
737 "Go to the next displayed page."
739 (when (zerop eww-history-position)
740 (user-error "No next page"))
742 (eww-restore-history (elt eww-history (1- eww-history-position))))
744 (defun eww-restore-history (elem)
745 (let ((inhibit-read-only t)
746 (inhibit-modification-hooks t)
747 (text (plist-get elem :text)))
750 (eww-reload) ; FIXME: restore :point?
753 (goto-char (plist-get elem :point))
754 (eww-update-header-line-format))))
756 (defun eww-next-url ()
757 "Go to the page marked `next'.
758 A page is marked `next' if rel=\"next\" appears in a <link>
761 (if (plist-get eww-data :next)
762 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
763 (plist-get eww-data :url)))
764 (user-error "No `next' on this page")))
766 (defun eww-previous-url ()
767 "Go to the page marked `previous'.
768 A page is marked `previous' if rel=\"previous\" appears in a <link>
771 (if (plist-get eww-data :previous)
772 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
773 (plist-get eww-data :url)))
774 (user-error "No `previous' on this page")))
777 "Go to the page marked `up'.
778 A page is marked `up' if rel=\"up\" appears in a <link>
781 (if (plist-get eww-data :up)
782 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
783 (plist-get eww-data :url)))
784 (user-error "No `up' on this page")))
786 (defun eww-top-url ()
787 "Go to the page marked `top'.
788 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
789 appears in a <link> or <a> tag."
791 (let ((best-url (or (plist-get eww-data :start)
792 (plist-get eww-data :contents)
793 (plist-get eww-data :home))))
795 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
796 (user-error "No `top' for this page"))))
798 (defun eww-reload (&optional local encode)
799 "Reload the current page.
800 If LOCAL (the command prefix), don't reload the page from the
801 network, but just re-display the HTML already fetched."
803 (let ((url (plist-get eww-data :url)))
805 (if (null (plist-get eww-data :dom))
806 (error "No current HTML data")
807 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
808 (point) (current-buffer)))
809 (url-retrieve url 'eww-render
810 (list url (point) (current-buffer) encode)))))
814 (defvar eww-form nil)
816 (defvar eww-submit-map
817 (let ((map (make-sparse-keymap)))
818 (define-key map "\r" 'eww-submit)
819 (define-key map [(control c) (control c)] 'eww-submit)
822 (defvar eww-submit-file
823 (let ((map (make-sparse-keymap)))
824 (define-key map "\r" 'eww-select-file)
825 (define-key map [(control c) (control c)] 'eww-submit)
828 (defvar eww-checkbox-map
829 (let ((map (make-sparse-keymap)))
830 (define-key map " " 'eww-toggle-checkbox)
831 (define-key map "\r" 'eww-toggle-checkbox)
832 (define-key map [(control c) (control c)] 'eww-submit)
836 (let ((map (make-keymap)))
837 (set-keymap-parent map text-mode-map)
838 (define-key map "\r" 'eww-submit)
839 (define-key map [(control a)] 'eww-beginning-of-text)
840 (define-key map [(control c) (control c)] 'eww-submit)
841 (define-key map [(control e)] 'eww-end-of-text)
842 (define-key map [?\t] 'shr-next-link)
843 (define-key map [?\M-\t] 'shr-previous-link)
846 (defvar eww-textarea-map
847 (let ((map (make-keymap)))
848 (set-keymap-parent map text-mode-map)
849 (define-key map "\r" 'forward-line)
850 (define-key map [(control c) (control c)] 'eww-submit)
851 (define-key map [?\t] 'shr-next-link)
852 (define-key map [?\M-\t] 'shr-previous-link)
855 (defvar eww-select-map
856 (let ((map (make-sparse-keymap)))
857 (define-key map "\r" 'eww-change-select)
858 (define-key map [(control c) (control c)] 'eww-submit)
861 (defun eww-beginning-of-text ()
862 "Move to the start of the input field."
864 (goto-char (eww-beginning-of-field)))
866 (defun eww-end-of-text ()
867 "Move to the end of the text in the input field."
869 (goto-char (eww-end-of-field))
870 (let ((start (eww-beginning-of-field)))
871 (while (and (equal (following-char) ? )
874 (when (> (point) start)
877 (defun eww-beginning-of-field ()
881 ((not (eq (get-text-property (point) 'eww-form)
882 (get-text-property (1- (point)) 'eww-form)))
885 (previous-single-property-change
886 (point) 'eww-form nil (point-min)))))
888 (defun eww-end-of-field ()
889 (1- (next-single-property-change
890 (point) 'eww-form nil (point-max))))
892 (defun eww-tag-form (dom)
893 (let ((eww-form (list (cons :method (dom-attr dom 'method))
894 (cons :action (dom-attr dom 'action))))
896 (shr-ensure-paragraph)
901 (when (> (point) start)
902 (put-text-property start (1+ start)
903 'eww-form eww-form))))
905 (defun eww-form-submit (dom)
906 (let ((start (point))
907 (value (dom-attr dom 'value)))
909 (if (zerop (length value))
913 (add-face-text-property start (point) 'eww-form-submit)
914 (put-text-property start (point) 'eww-form
915 (list :eww-form eww-form
918 :name (dom-attr dom 'name)))
919 (put-text-property start (point) 'keymap eww-submit-map)
922 (defun eww-form-checkbox (dom)
923 (let ((start (point)))
924 (if (dom-attr dom 'checked)
925 (insert eww-form-checkbox-selected-symbol)
926 (insert eww-form-checkbox-symbol))
927 (add-face-text-property start (point) 'eww-form-checkbox)
928 (put-text-property start (point) 'eww-form
929 (list :eww-form eww-form
930 :value (dom-attr dom 'value)
931 :type (downcase (dom-attr dom 'type))
932 :checked (dom-attr dom 'checked)
933 :name (dom-attr dom 'name)))
934 (put-text-property start (point) 'keymap eww-checkbox-map)
937 (defun eww-form-file (dom)
938 (let ((start (point))
939 (value (dom-attr dom 'value)))
941 (if (zerop (length value))
945 (add-face-text-property start (point) 'eww-form-file)
947 (put-text-property start (point) 'eww-form
948 (list :eww-form eww-form
949 :value (dom-attr dom 'value)
950 :type (downcase (dom-attr dom 'type))
951 :name (dom-attr dom 'name)))
952 (put-text-property start (point) 'keymap eww-submit-file)
955 (defun eww-select-file ()
956 "Change the value of the upload file menu under point."
958 (let* ((input (get-text-property (point) 'eww-form)))
960 (let ((insert-default-directory t))
961 (read-file-name "filename: "))))
962 (eww-update-field filename (length "Browse"))
963 (plist-put input :filename filename))))
965 (defun eww-form-text (dom)
966 (let ((start (point))
967 (type (downcase (or (dom-attr dom 'type) "text")))
968 (value (or (dom-attr dom 'value) ""))
969 (width (string-to-number (or (dom-attr dom 'size) "40")))
970 (readonly-property (if (or (dom-attr dom 'disabled)
971 (dom-attr dom 'readonly))
973 'inhibit-read-only)))
975 (when (< (length value) width)
976 (insert (make-string (- width (length value)) ? )))
977 (put-text-property start (point) 'face 'eww-form-text)
978 (put-text-property start (point) 'inhibit-read-only t)
979 (put-text-property start (point) 'local-map eww-text-map)
980 (put-text-property start (point) readonly-property t)
981 (put-text-property start (point) 'eww-form
982 (list :eww-form eww-form
985 :name (dom-attr dom 'name)))
988 (defconst eww-text-input-types '("text" "password" "textarea"
989 "color" "date" "datetime" "datetime-local"
990 "email" "month" "number" "search" "tel"
992 "List of input types which represent a text input.
993 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
995 (defun eww-process-text-input (beg end replace-length)
996 (when-let (pos (and (< (1+ end) (point-max))
997 (> (1- end) (point-min))
999 ((get-text-property (1+ end) 'eww-form)
1001 ((get-text-property (1- end) 'eww-form)
1003 (let* ((form (get-text-property pos 'eww-form))
1004 (properties (text-properties-at pos))
1005 (inhibit-read-only t)
1006 (length (- end beg replace-length))
1007 (type (plist-get form :type)))
1009 (member type eww-text-input-types))
1012 ;; Delete some space at the end.
1015 (if (equal type "textarea")
1016 (1- (line-end-position))
1017 (eww-end-of-field)))
1018 (while (and (> length 0)
1019 (eql (following-char) ? ))
1020 (delete-region (1- (point)) (point))
1025 (goto-char (1- end))
1027 (if (equal type "textarea")
1028 (1- (line-end-position))
1029 (1+ (eww-end-of-field))))
1030 (let ((start (point)))
1031 (insert (make-string (abs length) ? ))
1032 (set-text-properties start (point) properties))
1033 (goto-char (1- end)))))
1034 (set-text-properties (plist-get form :start) (plist-get form :end)
1036 (let ((value (buffer-substring-no-properties
1037 (eww-beginning-of-field)
1038 (eww-end-of-field))))
1039 (when (string-match " +\\'" value)
1040 (setq value (substring value 0 (match-beginning 0))))
1041 (plist-put form :value value)
1042 (when (equal type "password")
1043 ;; Display passwords as asterisks.
1044 (let ((start (eww-beginning-of-field)))
1045 (put-text-property start (+ start (length value))
1046 'display (make-string (length value) ?*)))))))))
1048 (defun eww-tag-textarea (dom)
1049 (let ((start (point))
1050 (value (or (dom-attr dom 'value) ""))
1051 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1052 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1054 (shr-ensure-newline)
1056 (shr-ensure-newline)
1057 (when (< (count-lines start (point)) lines)
1058 (dotimes (_ (- lines (count-lines start (point))))
1060 (setq end (point-marker))
1062 (while (< (point) end)
1064 (let ((pad (- width (- (point) (line-beginning-position)))))
1066 (insert (make-string pad ? ))))
1067 (add-face-text-property (line-beginning-position)
1068 (point) 'eww-form-textarea)
1069 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1070 (put-text-property (line-beginning-position) (point)
1071 'local-map eww-textarea-map)
1073 (put-text-property start (point) 'eww-form
1074 (list :eww-form eww-form
1077 :name (dom-attr dom 'name)))))
1079 (defun eww-tag-input (dom)
1080 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1083 ((or (equal type "checkbox")
1084 (equal type "radio"))
1085 (eww-form-checkbox dom))
1086 ((equal type "file")
1087 (eww-form-file dom))
1088 ((equal type "submit")
1089 (eww-form-submit dom))
1090 ((equal type "hidden")
1091 (let ((form eww-form)
1092 (name (dom-attr dom 'name)))
1093 ;; Don't add <input type=hidden> elements repeatedly.
1095 (or (not (consp (car form)))
1096 (not (eq (caar form) 'hidden))
1097 (not (equal (plist-get (cdr (car form)) :name)
1099 (setq form (cdr form)))
1101 (nconc eww-form (list
1104 :value (dom-attr dom 'value)))))))
1106 (eww-form-text dom)))
1107 (unless (= start (point))
1108 (put-text-property start (1+ start) 'help-echo "Input field"))))
1110 (defun eww-tag-select (dom)
1111 (shr-ensure-paragraph)
1112 (let ((menu (list :name (dom-attr dom 'name)
1113 :eww-form eww-form))
1118 (if (eq (dom-tag dom) 'optgroup)
1119 (dolist (groupelem (dom-children dom))
1120 (unless (dom-attr groupelem 'disabled)
1121 (setq opelem (append opelem (list groupelem)))))
1122 (setq opelem (list dom)))
1123 (dolist (elem opelem)
1124 (when (eq (dom-tag elem) 'option)
1125 (when (dom-attr elem 'selected)
1126 (nconc menu (list :value (dom-attr elem 'value))))
1127 (let ((display (dom-text elem)))
1128 (setq max (max max (length display)))
1130 :value (dom-attr elem 'value)
1134 (setq options (nreverse options))
1135 ;; If we have no selected values, default to the first value.
1136 (unless (plist-get menu :value)
1137 (nconc menu (list :value (nth 2 (car options)))))
1138 (nconc menu options)
1139 (let ((selected (eww-select-display menu)))
1141 (make-string (- max (length selected)) ? )))
1142 (put-text-property start (point) 'eww-form menu)
1143 (add-face-text-property start (point) 'eww-form-select)
1144 (put-text-property start (point) 'keymap eww-select-map)
1145 (unless (= start (point))
1146 (put-text-property start (1+ start) 'help-echo "select field"))
1147 (shr-ensure-paragraph))))
1149 (defun eww-select-display (select)
1150 (let ((value (plist-get select :value))
1152 (dolist (elem select)
1153 (when (and (consp elem)
1154 (eq (car elem) 'item)
1155 (equal value (plist-get (cdr elem) :value)))
1156 (setq display (plist-get (cdr elem) :display))))
1159 (defun eww-change-select ()
1160 "Change the value of the select drop-down menu under point."
1162 (let* ((input (get-text-property (point) 'eww-form))
1163 (completion-ignore-case t)
1166 (mapcar (lambda (elem)
1168 (eq (car elem) 'item)
1169 (cons (plist-get (cdr elem) :display)
1170 (plist-get (cdr elem) :value))))
1173 (completing-read "Change value: " options nil 'require-match))
1174 (inhibit-read-only t))
1175 (plist-put input :value (cdr (assoc-string display options t)))
1177 (eww-update-field display))))
1179 (defun eww-update-field (string &optional offset)
1182 (let ((properties (text-properties-at (point)))
1183 (start (+ (eww-beginning-of-field) offset))
1184 (current-end (1+ (eww-end-of-field)))
1185 (new-end (+ (eww-beginning-of-field) (length string)))
1186 (inhibit-read-only t))
1187 (delete-region start current-end)
1188 (forward-char offset)
1190 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1192 (set-text-properties start new-end properties))
1195 (defun eww-toggle-checkbox ()
1196 "Toggle the value of the checkbox under point."
1198 (let* ((input (get-text-property (point) 'eww-form))
1199 (type (plist-get input :type)))
1200 (if (equal type "checkbox")
1203 (if (plist-get input :checked)
1205 (plist-put input :checked nil)
1206 (eww-update-field eww-form-checkbox-symbol))
1207 (plist-put input :checked t)
1208 (eww-update-field eww-form-checkbox-selected-symbol))))
1209 ;; Radio button. Switch all other buttons off.
1210 (let ((name (plist-get input :name)))
1212 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1213 (when (equal (plist-get (cdr elem) :name) name)
1214 (goto-char (car elem))
1215 (if (not (eq (cdr elem) input))
1217 (plist-put input :checked nil)
1218 (eww-update-field eww-form-checkbox-symbol))
1219 (plist-put input :checked t)
1220 (eww-update-field eww-form-checkbox-selected-symbol)))))
1221 (forward-char 1)))))
1223 (defun eww-inputs (form)
1224 (let ((start (point-min))
1227 (< start (point-max)))
1228 (when (or (get-text-property start 'eww-form)
1229 (setq start (next-single-property-change start 'eww-form)))
1230 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1232 (push (cons start (get-text-property start 'eww-form))
1234 (setq start (next-single-property-change start 'eww-form))))
1237 (defun eww-size-text-inputs ()
1238 (let ((start (point-min)))
1240 (< start (point-max)))
1241 (when (or (get-text-property start 'eww-form)
1242 (setq start (next-single-property-change start 'eww-form)))
1243 (let ((props (get-text-property start 'eww-form)))
1244 (plist-put props :start start)
1245 (setq start (next-single-property-change
1246 start 'eww-form nil (point-max)))
1247 (plist-put props :end start))))))
1249 (defun eww-input-value (input)
1250 (let ((type (plist-get input :type))
1251 (value (plist-get input :value)))
1253 ((equal type "textarea")
1256 (goto-char (point-min))
1257 (while (re-search-forward "^ +\n\\| +$" nil t)
1258 (replace-match "" t t))
1261 (if (string-match " +\\'" value)
1262 (substring value 0 (match-beginning 0))
1265 (defun eww-submit ()
1266 "Submit the current form."
1268 (let* ((this-input (get-text-property (point) 'eww-form))
1269 (form (plist-get this-input :eww-form))
1271 (dolist (elem (sort (eww-inputs form)
1273 (< (car o1) (car o2)))))
1274 (let* ((input (cdr elem))
1275 (input-start (car elem))
1276 (name (plist-get input :name)))
1279 ((member (plist-get input :type) '("checkbox" "radio"))
1280 (when (plist-get input :checked)
1281 (push (cons name (plist-get input :value))
1283 ((equal (plist-get input :type) "file")
1285 (list (cons "filedata"
1287 (insert-file-contents
1288 (plist-get input :filename))
1290 (cons "name" (plist-get input :name))
1291 (cons "filename" (plist-get input :filename))))
1293 ((equal (plist-get input :type) "submit")
1294 ;; We want the values from buttons if we hit a button if
1295 ;; we hit enter on it, or if it's the first button after
1296 ;; the field we did hit return on.
1297 (when (or (eq input this-input)
1298 (and (not (eq input this-input))
1300 (> input-start (point))))
1301 (setq next-submit t)
1302 (push (cons name (plist-get input :value))
1305 (push (cons name (eww-input-value input))
1308 (when (and (consp elem)
1309 (eq (car elem) 'hidden))
1310 (push (cons (plist-get (cdr elem) :name)
1311 (or (plist-get (cdr elem) :value) ""))
1313 (if (and (stringp (cdr (assq :method form)))
1314 (equal (downcase (cdr (assq :method form))) "post"))
1316 (dolist (x values mtype)
1317 (if (equal (car x) "file")
1319 (setq mtype "multipart/form-data"))))
1320 (cond ((equal mtype "multipart/form-data")
1321 (let ((boundary (mml-compute-boundary '())))
1322 (let ((url-request-method "POST")
1323 (url-request-extra-headers
1324 (list (cons "Content-Type"
1325 (concat "multipart/form-data; boundary="
1328 (mm-url-encode-multipart-form-data values boundary)))
1329 (eww-browse-url (shr-expand-url
1330 (cdr (assq :action form))
1331 (plist-get eww-data :url))))))
1333 (let ((url-request-method "POST")
1334 (url-request-extra-headers
1336 "application/x-www-form-urlencoded")))
1338 (mm-url-encode-www-form-urlencoded values)))
1339 (eww-browse-url (shr-expand-url
1340 (cdr (assq :action form))
1341 (plist-get eww-data :url)))))))
1344 (if (cdr (assq :action form))
1345 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1346 (plist-get eww-data :url))
1348 (mm-url-encode-www-form-urlencoded values))))))
1350 (defun eww-browse-with-external-browser (&optional url)
1351 "Browse the current URL with an external browser.
1352 The browser to used is specified by the `shr-external-browser' variable."
1354 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1356 (defun eww-follow-link (&optional external mouse-event)
1357 "Browse the URL under point.
1358 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1359 If EXTERNAL is double prefix, browse in new buffer."
1360 (interactive (list current-prefix-arg last-nonmenu-event))
1361 (mouse-set-point mouse-event)
1362 (let ((url (get-text-property (point) 'shr-url)))
1365 (message "No link under point"))
1366 ((string-match "^mailto:" url)
1367 (browse-url-mail url))
1368 ((and (consp external) (<= (car external) 4))
1369 (funcall shr-external-browser url))
1370 ;; This is a #target url in the same page as the current one.
1371 ((and (url-target (url-generic-parse-url url))
1372 (eww-same-page-p url (plist-get eww-data :url)))
1373 (let ((dom (plist-get eww-data :dom)))
1375 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1377 (eww-browse-url url external)))))
1379 (defun eww-same-page-p (url1 url2)
1380 "Return non-nil if URL1 and URL2 represent the same page.
1381 Differences in #targets are ignored."
1382 (let ((obj1 (url-generic-parse-url url1))
1383 (obj2 (url-generic-parse-url url2)))
1384 (setf (url-target obj1) nil)
1385 (setf (url-target obj2) nil)
1386 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1388 (defun eww-copy-page-url ()
1389 "Copy the URL of the current page into the kill ring."
1391 (message "%s" (plist-get eww-data :url))
1392 (kill-new (plist-get eww-data :url)))
1394 (defun eww-download ()
1395 "Download URL under point to `eww-download-directory'."
1397 (let ((url (get-text-property (point) 'shr-url)))
1399 (message "No URL under point")
1400 (url-retrieve url 'eww-download-callback (list url)))))
1402 (defun eww-download-callback (status url)
1403 (unless (plist-get status :error)
1404 (let* ((obj (url-generic-parse-url url))
1405 (path (car (url-path-and-query obj)))
1406 (file (eww-make-unique-file-name
1407 (eww-decode-url-file-name (file-name-nondirectory path))
1408 eww-download-directory)))
1409 (goto-char (point-min))
1410 (re-search-forward "\r?\n\r?\n")
1411 (write-region (point) (point-max) file)
1412 (message "Saved %s" file))))
1414 (defun eww-decode-url-file-name (string)
1415 (let* ((binary (url-unhex-string string))
1417 (decode-coding-string
1419 ;; Possibly set by `universal-coding-system-argument'.
1420 (or coding-system-for-read
1421 ;; RFC 3986 says that %AB stuff is utf-8.
1422 (if (equal (decode-coding-string binary 'utf-8)
1426 (car (detect-coding-string binary))))))
1427 (encodes (find-coding-systems-string decoded)))
1428 (if (or (equal encodes '(undecided))
1429 (memq (or file-name-coding-system
1430 default-file-name-coding-system)
1433 ;; If we can't encode the decoded file name (due to language
1434 ;; environment settings), then we return the original, hexified
1438 (defun eww-make-unique-file-name (file directory)
1440 ((zerop (length file))
1442 ((string-match "\\`[.]" file)
1443 (setq file (concat "!" file))))
1447 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1448 (setq stem (match-string 1)
1449 suffix (match-string 2)))
1450 (while (file-exists-p (expand-file-name file directory))
1451 (setq file (format "%s(%d)%s" stem count suffix))
1452 (setq count (1+ count)))
1453 (expand-file-name file directory)))
1455 (defun eww-set-character-encoding (charset)
1456 "Set character encoding to CHARSET.
1457 If CHARSET is nil then use UTF-8."
1458 (interactive "zUse character set (default utf-8): ")
1460 (eww-reload nil 'utf-8)
1461 (eww-reload nil charset)))
1463 (defun eww-toggle-fonts ()
1464 "Toggle whether to use monospaced or font-enabled layouts."
1466 (message "Fonts are now %s"
1467 (if (setq shr-use-fonts (not shr-use-fonts))
1474 (defvar eww-bookmarks nil)
1476 (defun eww-add-bookmark ()
1477 "Bookmark the current page."
1479 (eww-read-bookmarks)
1480 (dolist (bookmark eww-bookmarks)
1481 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1482 (user-error "Already bookmarked")))
1483 (when (y-or-n-p "Bookmark this page?")
1484 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1485 (plist-get eww-data :title))))
1486 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1487 (push (list :url (plist-get eww-data :url)
1489 :time (current-time-string))
1491 (eww-write-bookmarks)
1492 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1493 (plist-get eww-data :title))))
1495 (defun eww-write-bookmarks ()
1496 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1497 (insert ";; Auto-generated file; don't edit\n")
1498 (pp eww-bookmarks (current-buffer))))
1500 (defun eww-read-bookmarks ()
1501 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1503 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1505 (insert-file-contents file)
1506 (read (current-buffer)))))))
1509 (defun eww-list-bookmarks ()
1510 "Display the bookmarks."
1512 (eww-bookmark-prepare)
1513 (pop-to-buffer "*eww bookmarks*"))
1515 (defun eww-bookmark-prepare ()
1516 (eww-read-bookmarks)
1517 (unless eww-bookmarks
1518 (user-error "No bookmarks are defined"))
1519 (set-buffer (get-buffer-create "*eww bookmarks*"))
1521 (let* ((width (/ (window-width) 2))
1522 (format (format "%%-%ds %%s" width))
1523 (inhibit-read-only t)
1526 (setq header-line-format (concat " " (format format "Title" "URL")))
1527 (dolist (bookmark eww-bookmarks)
1529 title (plist-get bookmark :title))
1530 (when (> (length title) width)
1531 (setq title (truncate-string-to-width title width)))
1532 (insert (format format title (plist-get bookmark :url)) "\n")
1533 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1534 (goto-char (point-min))))
1536 (defvar eww-bookmark-kill-ring nil)
1538 (defun eww-bookmark-kill ()
1539 "Kill the current bookmark."
1541 (let* ((start (line-beginning-position))
1542 (bookmark (get-text-property start 'eww-bookmark))
1543 (inhibit-read-only t))
1545 (user-error "No bookmark on the current line"))
1547 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1548 (delete-region start (point))
1549 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1550 (eww-write-bookmarks)))
1552 (defun eww-bookmark-yank ()
1553 "Yank a previously killed bookmark to the current line."
1555 (unless eww-bookmark-kill-ring
1556 (user-error "No previously killed bookmark"))
1558 (let ((inhibit-read-only t)
1561 (insert (pop eww-bookmark-kill-ring))
1562 (setq bookmark (get-text-property start 'eww-bookmark))
1563 (if (= start (point-min))
1564 (push bookmark eww-bookmarks)
1565 (let ((line (count-lines start (point))))
1566 (setcdr (nthcdr (1- line) eww-bookmarks)
1567 (cons bookmark (nthcdr line eww-bookmarks)))))
1568 (eww-write-bookmarks)))
1570 (defun eww-bookmark-browse ()
1571 "Browse the bookmark under point in eww."
1573 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1575 (user-error "No bookmark on the current line"))
1577 (eww-browse-url (plist-get bookmark :url))))
1579 (defun eww-next-bookmark ()
1580 "Go to the next bookmark in the list."
1584 (unless (get-buffer "*eww bookmarks*")
1586 (eww-bookmark-prepare))
1587 (with-current-buffer (get-buffer "*eww bookmarks*")
1588 (when (and (not first)
1591 (setq bookmark (get-text-property (line-beginning-position)
1594 (user-error "No next bookmark")))
1595 (eww-browse-url (plist-get bookmark :url))))
1597 (defun eww-previous-bookmark ()
1598 "Go to the previous bookmark in the list."
1602 (unless (get-buffer "*eww bookmarks*")
1604 (eww-bookmark-prepare))
1605 (with-current-buffer (get-buffer "*eww bookmarks*")
1607 (goto-char (point-max))
1608 (beginning-of-line))
1609 ;; On the final line.
1613 (user-error "No previous bookmark")
1615 (setq bookmark (get-text-property (line-beginning-position)
1617 (eww-browse-url (plist-get bookmark :url))))
1619 (defvar eww-bookmark-mode-map
1620 (let ((map (make-sparse-keymap)))
1621 (define-key map [(control k)] 'eww-bookmark-kill)
1622 (define-key map [(control y)] 'eww-bookmark-yank)
1623 (define-key map "\r" 'eww-bookmark-browse)
1625 (easy-menu-define nil map
1626 "Menu for `eww-bookmark-mode-map'."
1628 ["Exit" quit-window t]
1629 ["Browse" eww-bookmark-browse
1630 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1631 ["Kill" eww-bookmark-kill
1632 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1633 ["Yank" eww-bookmark-yank
1634 :active eww-bookmark-kill-ring]))
1637 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1638 "Mode for listing bookmarks.
1640 \\{eww-bookmark-mode-map}"
1641 (buffer-disable-undo)
1642 (setq truncate-lines t))
1646 (defun eww-save-history ()
1647 (plist-put eww-data :point (point))
1648 (plist-put eww-data :text (buffer-string))
1649 (push eww-data eww-history)
1650 (setq eww-data (list :title ""))
1651 ;; Don't let the history grow infinitely. We store quite a lot of
1653 (when-let (tail (and eww-history-limit
1654 (nthcdr eww-history-limit eww-history)))
1657 (defvar eww-current-buffer)
1659 (defun eww-list-histories ()
1660 "List the eww-histories."
1662 (when (null eww-history)
1663 (error "No eww-histories are defined"))
1664 (let ((eww-history-trans eww-history)
1665 (buffer (current-buffer)))
1666 (set-buffer (get-buffer-create "*eww history*"))
1668 (setq-local eww-current-buffer buffer)
1669 (let ((inhibit-read-only t)
1672 url title format start)
1674 (dolist (history eww-history-trans)
1675 (setq start (point))
1676 (setq domain-length (max domain-length (length (plist-get history :url))))
1677 (setq title-length (max title-length (length (plist-get history :title)))))
1678 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1680 (concat " " (format format "Title" "URL")))
1681 (dolist (history eww-history-trans)
1682 (setq start (point))
1683 (setq url (plist-get history :url))
1684 (setq title (plist-get history :title))
1685 (insert (format format title url))
1687 (put-text-property start (1+ start) 'eww-history history))
1688 (goto-char (point-min)))
1689 (pop-to-buffer "*eww history*")))
1691 (defun eww-history-browse ()
1692 "Browse the history under point in eww."
1694 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1696 (error "No history on the current line"))
1697 (let ((buffer eww-current-buffer))
1700 (switch-to-buffer buffer)))
1701 (eww-restore-history history)))
1703 (defvar eww-history-mode-map
1704 (let ((map (make-sparse-keymap)))
1705 (define-key map "\r" 'eww-history-browse)
1706 ;; (define-key map "n" 'next-error-no-select)
1707 ;; (define-key map "p" 'previous-error-no-select)
1709 (easy-menu-define nil map
1710 "Menu for `eww-history-mode-map'."
1712 ["Exit" quit-window t]
1713 ["Browse" eww-history-browse
1714 :active (get-text-property (line-beginning-position) 'eww-history)]))
1717 (define-derived-mode eww-history-mode special-mode "eww history"
1718 "Mode for listing eww-histories.
1720 \\{eww-history-mode-map}"
1721 (buffer-disable-undo)
1722 (setq truncate-lines t))
1724 ;;; eww buffers list
1726 (defun eww-list-buffers ()
1727 "Enlist eww buffers."
1730 (current (current-buffer)))
1731 (dolist (buffer (buffer-list))
1732 (with-current-buffer buffer
1733 (when (derived-mode-p 'eww-mode)
1734 (push (vector buffer (plist-get eww-data :title)
1735 (plist-get eww-data :url))
1737 (unless buffers-info
1738 (error "No eww buffers"))
1739 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1740 (set-buffer (get-buffer-create "*eww buffers*"))
1742 (let ((inhibit-read-only t)
1745 url title format start)
1747 (dolist (buffer-info buffers-info)
1748 (setq title-length (max title-length
1749 (length (elt buffer-info 1)))
1750 domain-length (max domain-length
1751 (length (elt buffer-info 2)))))
1752 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1754 (concat " " (format format "Title" "URL")))
1756 (current-buffer-line 1))
1757 (dolist (buffer-info buffers-info)
1759 title (elt buffer-info 1)
1760 url (elt buffer-info 2)
1762 (insert (format format title url))
1764 (let ((buffer (elt buffer-info 0)))
1765 (put-text-property start (1+ start) 'eww-buffer
1767 (when (eq current buffer)
1768 (setq current-buffer-line line))))
1769 (goto-char (point-min))
1770 (forward-line (1- current-buffer-line)))))
1771 (pop-to-buffer "*eww buffers*"))
1773 (defun eww-buffer-select ()
1774 "Switch to eww buffer."
1776 (let ((buffer (get-text-property (line-beginning-position)
1779 (error "No buffer on current line"))
1781 (switch-to-buffer buffer)))
1783 (defun eww-buffer-show ()
1784 "Display buffer under point in eww buffer list."
1785 (let ((buffer (get-text-property (line-beginning-position)
1788 (error "No buffer on current line"))
1790 (switch-to-buffer buffer)
1793 (defun eww-buffer-show-next ()
1794 "Move to next eww buffer in the list and display it."
1798 (goto-char (point-min)))
1801 (defun eww-buffer-show-previous ()
1802 "Move to previous eww buffer in the list and display it."
1806 (goto-char (point-max)))
1810 (defun eww-buffer-kill ()
1811 "Kill buffer from eww list."
1813 (let* ((start (line-beginning-position))
1814 (buffer (get-text-property start 'eww-buffer))
1815 (inhibit-read-only t))
1817 (user-error "No buffer on the current line"))
1818 (kill-buffer buffer)
1820 (delete-region start (point)))
1825 (defvar eww-buffers-mode-map
1826 (let ((map (make-sparse-keymap)))
1827 (define-key map [(control k)] 'eww-buffer-kill)
1828 (define-key map "\r" 'eww-buffer-select)
1829 (define-key map "n" 'eww-buffer-show-next)
1830 (define-key map "p" 'eww-buffer-show-previous)
1832 (easy-menu-define nil map
1833 "Menu for `eww-buffers-mode-map'."
1835 ["Exit" quit-window t]
1836 ["Select" eww-buffer-select
1837 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1838 ["Kill" eww-buffer-kill
1839 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1842 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1843 "Mode for listing buffers.
1845 \\{eww-buffers-mode-map}"
1846 (buffer-disable-undo)
1847 (setq truncate-lines t))
1851 (defvar eww-desktop-data-save
1852 '(:url :title :point)
1853 "List of `eww-data' properties to preserve in the desktop file.
1854 Also used when saving `eww-history'.")
1856 (defun eww-desktop-data-1 (alist)
1860 (let ((k (car tail))
1862 (when (memq k eww-desktop-data-save)
1863 (setq acc (cons k (cons v acc)))))
1864 (setq tail (cddr tail)))
1867 (defun eww-desktop-history-duplicate (a b)
1868 (let ((tail a) (r t))
1870 (if (or (memq (car tail) '(:point)) ; ignore :point
1872 (plist-get b (car tail))))
1873 (setq tail (cddr tail))
1879 (defun eww-desktop-misc-data (_directory)
1880 "Return a property list with data used to restore eww buffers.
1881 This list will contain, as :history, the list, whose first element is
1882 the value of `eww-data', and the tail is `eww-history'.
1884 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1885 entries (if any) will be removed from the list.
1887 Only the properties listed in `eww-desktop-data-save' are included.
1888 Generally, the list should not include the (usually overly large)
1889 :dom, :source and :text properties."
1890 (let ((history (mapcar 'eww-desktop-data-1
1891 (cons eww-data eww-history))))
1892 (list :history (if eww-desktop-remove-duplicates
1893 (cl-remove-duplicates
1894 history :test 'eww-desktop-history-duplicate)
1897 (defun eww-restore-desktop (file-name buffer-name misc-data)
1898 "Restore an eww buffer from its desktop file record.
1899 If `eww-restore-desktop' is t or `auto', this function will also
1900 initiate the retrieval of the respective URI in the background.
1901 Otherwise, the restored buffer will contain a prompt to do so by using
1903 (with-current-buffer (get-buffer-create buffer-name)
1905 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1906 (setq eww-history (cdr (plist-get misc-data :history))
1907 eww-data (or (car (plist-get misc-data :history))
1908 ;; backwards compatibility
1909 (list :url (plist-get misc-data :uri))))
1911 (when (plist-get eww-data :url)
1912 (case eww-restore-desktop
1913 ((t auto) (eww (plist-get eww-data :url)))
1914 ((zerop (buffer-size))
1915 (let ((inhibit-read-only t))
1916 (insert (substitute-command-keys
1917 eww-restore-reload-prompt)))))))
1921 (add-to-list 'desktop-locals-to-save
1922 'eww-history-position)
1923 (add-to-list 'desktop-buffer-mode-handlers
1924 '(eww-mode . eww-restore-desktop))
1928 (defun eww-isearch-next-buffer (&optional _buffer wrap)
1929 "Go to the next page to search using `rel' attribute for navigation."
1936 (eww-previous-url)))
1941 ;;; eww.el ends here