1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2016 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
35 (eval-when-compile (require 'subr-x)) ;; for string-trim
40 :link '(custom-manual "(eww) Top")
44 (defcustom eww-header-line-format "%t: %u"
46 - %t is replaced by the title.
47 - %u is replaced by the URL."
52 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
53 "Prefix URL to search engine."
58 (defcustom eww-download-directory "~/Downloads/"
59 "Directory where files will downloaded."
65 (defcustom eww-suggest-uris
69 "List of functions called to form the list of default URIs for `eww'.
70 Each of the elements is a function returning either a string or a list
71 of strings. The results will be joined into a single list with
72 duplicate entries (if any) removed."
76 :options '(eww-links-at-point
80 (defcustom eww-bookmarks-directory user-emacs-directory
81 "Directory where bookmark files will be stored."
86 (defcustom eww-desktop-remove-duplicates t
87 "Whether to remove duplicates from the history when saving desktop data.
88 If non-nil, repetitive EWW history entries (comprising of the URI, the
89 title, and the point position) will not be saved as part of the Emacs
90 desktop. Otherwise, such entries will be retained."
95 (defcustom eww-restore-desktop nil
96 "How to restore EWW buffers on `desktop-restore'.
97 If t or `auto', the buffers will be reloaded automatically.
98 If nil, buffers will require manual reload, and will contain the text
99 specified in `eww-restore-reload-prompt' instead of the actual Web
103 :type '(choice (const :tag "Restore all automatically" t)
104 (const :tag "Require manual reload" nil)))
106 (defcustom eww-restore-reload-prompt
107 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
108 "The string to put in the buffers not reloaded on `desktop-restore'.
109 This prompt will be used if `eww-restore-desktop' is nil.
111 The string will be passed through `substitute-command-keys'."
116 (defcustom eww-history-limit 50
117 "Maximum number of entries to retain in the history."
120 :type '(choice (const :tag "Unlimited" nil)
123 (defcustom eww-use-external-browser-for-content-type
124 "\\`\\(video/\\|audio/\\|application/ogg\\)"
125 "Always use external browser for specified content-type."
128 :type '(choice (const :tag "Never" nil)
131 (defcustom eww-after-render-hook nil
132 "A hook called after eww has finished rendering the buffer."
137 (defcustom eww-form-checkbox-selected-symbol "[X]"
138 "Symbol used to represent a selected checkbox.
139 See also `eww-form-checkbox-symbol'."
142 :type '(choice (const "[X]")
143 (const "☒") ; Unicode BALLOT BOX WITH X
144 (const "☑") ; Unicode BALLOT BOX WITH CHECK
147 (defcustom eww-form-checkbox-symbol "[ ]"
148 "Symbol used to represent a checkbox.
149 See also `eww-form-checkbox-selected-symbol'."
152 :type '(choice (const "[ ]")
153 (const "☐") ; Unicode BALLOT BOX
156 (defface eww-form-submit
157 '((((type x w32 ns) (class color)) ; Like default mode line
158 :box (:line-width 2 :style released-button)
159 :background "#808080" :foreground "black"))
160 "Face for eww buffer buttons."
164 (defface eww-form-file
165 '((((type x w32 ns) (class color)) ; Like default mode line
166 :box (:line-width 2 :style released-button)
167 :background "#808080" :foreground "black"))
168 "Face for eww buffer buttons."
172 (defface eww-form-checkbox
173 '((((type x w32 ns) (class color)) ; Like default mode line
174 :box (:line-width 2 :style released-button)
175 :background "lightgrey" :foreground "black"))
176 "Face for eww buffer buttons."
180 (defface eww-form-select
181 '((((type x w32 ns) (class color)) ; Like default mode line
182 :box (:line-width 2 :style released-button)
183 :background "lightgrey" :foreground "black"))
184 "Face for eww buffer buttons."
188 (defface eww-form-text
189 '((t (:background "#505050"
191 :box (:line-width 1))))
192 "Face for eww text inputs."
196 (defface eww-form-textarea
197 '((t (:background "#C0C0C0"
199 :box (:line-width 1))))
200 "Face for eww textarea inputs."
204 (defface eww-invalid-certificate
205 '((default :weight bold)
206 (((class color)) :foreground "red"))
207 "Face for web pages with invalid certificates."
211 (defface eww-valid-certificate
212 '((default :weight bold)
213 (((class color)) :foreground "ForestGreen"))
214 "Face for web pages with valid certificates."
218 (defvar eww-data nil)
219 (defvar eww-history nil)
220 (defvar eww-history-position 0)
222 (defvar eww-local-regex "localhost"
223 "When this regex is found in the URL, it's not a keyword but an address.")
225 (defvar eww-link-keymap
226 (let ((map (copy-keymap shr-image-map)))
227 (define-key map "\r" 'eww-follow-link)
230 (defun eww-suggested-uris nil
231 "Return the list of URIs to suggest at the `eww' prompt.
232 This list can be customized via `eww-suggest-uris'."
233 (let ((obseen (make-vector 42 0))
235 (dolist (fun eww-suggest-uris)
236 (let ((ret (funcall fun)))
237 (dolist (uri (if (stringp ret) (list ret) ret))
238 (when (and uri (not (intern-soft uri obseen)))
245 "Fetch URL and render the page.
246 If the input doesn't look like an URL or a domain name, the
247 word(s) will be searched for via `eww-search-prefix'."
249 (let* ((uris (eww-suggested-uris))
250 (prompt (concat "Enter URL or keywords"
251 (if uris (format " (default %s)" (car uris)) "")
253 (list (read-string prompt nil nil uris))))
254 (setq url (string-trim url))
255 (cond ((string-match-p "\\`file:/" url))
256 ;; Don't mangle file: URLs at all.
257 ((string-match-p "\\`ftp://" url)
258 (user-error "FTP is not supported"))
260 ;; Anything that starts with something that vaguely looks
261 ;; like a protocol designator is interpreted as a full URL.
262 (if (or (string-match "\\`[A-Za-z]+:" url)
263 ;; Also try to match "naked" URLs like
264 ;; en.wikipedia.org/wiki/Free software
265 (string-match "\\`[A-Za-z_]+\\.[A-Za-z._]+/" url)
266 (and (= (length (split-string url)) 1)
267 (or (and (not (string-match-p "\\`[\"'].*[\"']\\'" url))
268 (> (length (split-string url "[.:]")) 1))
269 (string-match eww-local-regex url))))
271 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
272 (setq url (concat "http://" url)))
273 ;; Some sites do not redirect final /
274 (when (string= (url-filename (url-generic-parse-url url)) "")
275 (setq url (concat url "/"))))
276 (setq url (concat eww-search-prefix
277 (replace-regexp-in-string " " "+" url))))))
278 (pop-to-buffer-same-window
279 (if (eq major-mode 'eww-mode)
281 (get-buffer-create "*eww*")))
283 ;; Check whether the domain only uses "Highly Restricted" Unicode
284 ;; IDNA characters. If not, transform to punycode to indicate that
285 ;; there may be funny business going on.
286 (let ((parsed (url-generic-parse-url url)))
287 (unless (puny-highly-restrictive-domain-p (url-host parsed))
288 (setf (url-host parsed) (puny-encode-domain (url-host parsed)))
289 (setq url (url-recreate-url parsed))))
290 (plist-put eww-data :url url)
291 (plist-put eww-data :title "")
292 (eww-update-header-line-format)
293 (let ((inhibit-read-only t))
294 (insert (format "Loading %s..." url))
295 (goto-char (point-min)))
296 (url-retrieve url 'eww-render
297 (list url nil (current-buffer))))
299 ;;;###autoload (defalias 'browse-web 'eww)
302 (defun eww-open-file (file)
303 "Render FILE using EWW."
304 (interactive "fFile: ")
305 (eww (concat "file://"
306 (and (memq system-type '(windows-nt ms-dos))
308 (expand-file-name file))))
311 (defun eww-search-words (&optional beg end)
312 "Search the web for the text between BEG and END.
313 See the `eww-search-prefix' variable for the search engine used."
315 (eww (buffer-substring beg end)))
317 (defun eww-html-p (content-type)
318 "Return non-nil if CONTENT-TYPE designates an HTML content type.
319 Currently this means either text/html or application/xhtml+xml."
320 (member content-type '("text/html"
321 "application/xhtml+xml")))
323 (defun eww-render (status url &optional point buffer encode)
324 (let ((redirect (plist-get status :redirect)))
326 (setq url redirect)))
327 (let* ((headers (eww-parse-headers))
329 (mail-header-parse-content-type
330 (if (zerop (length (cdr (assoc "content-type" headers))))
332 (cdr (assoc "content-type" headers)))))
335 (or (cdr (assq 'charset (cdr content-type)))
336 (eww-detect-charset (eww-html-p (car content-type)))
338 (data-buffer (current-buffer))
339 last-coding-system-used)
340 ;; Save the https peer status.
341 (with-current-buffer buffer
342 (plist-put eww-data :peer (plist-get status :peer)))
346 ((and eww-use-external-browser-for-content-type
347 (string-match-p eww-use-external-browser-for-content-type
349 (eww-browse-with-external-browser url))
350 ((eww-html-p (car content-type))
351 (eww-display-html charset url nil point buffer encode))
352 ((equal (car content-type) "application/pdf")
354 ((string-match-p "\\`image/" (car content-type))
355 (eww-display-image buffer))
357 (eww-display-raw buffer (or encode charset 'utf-8))))
358 (with-current-buffer buffer
359 (plist-put eww-data :url url)
360 (eww-update-header-line-format)
361 (setq eww-history-position 0)
362 (and last-coding-system-used
363 (set-buffer-file-coding-system last-coding-system-used))
364 (run-hooks 'eww-after-render-hook)))
365 (kill-buffer data-buffer))))
367 (defun eww-parse-headers ()
369 (goto-char (point-min))
370 (while (and (not (eobp))
372 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
373 (push (cons (downcase (match-string 1))
381 (defun eww-detect-charset (html-p)
382 (let ((case-fold-search t)
386 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
390 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
393 (declare-function libxml-parse-html-region "xml.c"
394 (start end &optional base-url discard-comments))
396 (defun eww-display-html (charset url &optional document point buffer encode)
397 (unless (fboundp 'libxml-parse-html-region)
398 (error "This function requires Emacs to be compiled with libxml2"))
399 (unless (buffer-live-p buffer)
400 (error "Buffer %s doesn't exist" buffer))
401 ;; There should be a better way to abort loading images
407 'base (list (cons 'href url))
409 (setq encode (or encode charset 'utf-8))
411 (decode-coding-region (point) (point-max) encode)
412 (coding-system-error nil))
413 (libxml-parse-html-region (point) (point-max))))))
414 (source (and (null document)
415 (buffer-substring (point) (point-max)))))
416 (with-current-buffer buffer
417 (setq bidi-paragraph-direction 'left-to-right)
418 (plist-put eww-data :source source)
419 (plist-put eww-data :dom document)
420 (let ((inhibit-read-only t)
421 (inhibit-modification-hooks t)
422 (shr-target-id (url-target (url-generic-parse-url url)))
423 (shr-external-rendering-functions
425 shr-external-rendering-functions
426 '((title . eww-tag-title)
427 (form . eww-tag-form)
428 (input . eww-tag-input)
429 (button . eww-form-submit)
430 (textarea . eww-tag-textarea)
431 (select . eww-tag-select)
432 (link . eww-tag-link)
433 (meta . eww-tag-meta)
436 (shr-insert-document document)
441 (goto-char (point-min))
442 (let ((point (next-single-property-change
443 (point-min) 'shr-target-id)))
447 (goto-char (point-min))
448 ;; Don't leave point inside forms, because the normal eww
449 ;; commands aren't available there.
450 (while (and (not (eobp))
451 (get-text-property (point) 'eww-form))
453 (eww-size-text-inputs))))
455 (defun eww-handle-link (dom)
456 (let* ((rel (dom-attr dom 'rel))
457 (href (dom-attr dom 'href))
459 ;; The text associated with :rel is case-insensitive.
460 (if rel (downcase rel))
462 ;; Texinfo uses "previous", but HTML specifies
463 ;; "prev", so recognize both.
464 ("previous" . :previous)
466 ;; HTML specifies "start" but also "contents",
467 ;; and Gtk seems to use "home". Recognize
468 ;; them all; but store them in different
469 ;; variables so that we can readily choose the
473 ("contents" . :contents)
477 (plist-put eww-data (cdr where) href))))
479 (defvar eww-redirect-level 1)
481 (defun eww-tag-meta (dom)
482 (when (and (cl-equalp (dom-attr dom 'http-equiv) "refresh")
483 (< eww-redirect-level 5))
484 (when-let (refresh (dom-attr dom 'content))
485 (when (or (string-match "^\\([0-9]+\\) *;.*url=\"\\([^\"]+\\)\"" refresh)
486 (string-match "^\\([0-9]+\\) *;.*url=\\([^ ]+\\)" refresh))
487 (let ((timeout (match-string 1 refresh))
488 (url (match-string 2 refresh))
489 (eww-redirect-level (1+ eww-redirect-level)))
490 (if (equal timeout "0")
491 (eww (shr-expand-url url))
493 (dom-node 'a `((href . ,(shr-expand-url url)))
494 (format "Auto refresh in %s second%s disabled"
496 (if (equal timeout "1")
500 (defun eww-tag-link (dom)
501 (eww-handle-link dom)
504 (defun eww-tag-a (dom)
505 (eww-handle-link dom)
506 (let ((start (point)))
508 (put-text-property start (point) 'keymap eww-link-keymap)))
510 (defun eww-update-header-line-format ()
511 (setq header-line-format
512 (and eww-header-line-format
513 (let ((title (plist-get eww-data :title))
514 (peer (plist-get eww-data :peer)))
515 (when (zerop (length title))
516 (setq title "[untitled]"))
517 ;; This connection has is https.
520 (propertize title 'face
521 (if (plist-get peer :warnings)
522 'eww-invalid-certificate
523 'eww-valid-certificate))))
524 (replace-regexp-in-string
527 eww-header-line-format
528 `((?u . ,(or (plist-get eww-data :url) ""))
531 (defun eww-tag-title (dom)
532 (plist-put eww-data :title
533 (replace-regexp-in-string
535 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
536 (eww-update-header-line-format))
538 (defun eww-display-raw (buffer &optional encode)
539 (let ((data (buffer-substring (point) (point-max))))
540 (unless (buffer-live-p buffer)
541 (error "Buffer %s doesn't exist" buffer))
542 (with-current-buffer buffer
543 (let ((inhibit-read-only t))
547 (decode-coding-region (point-min) (1+ (length data)) encode)
548 (coding-system-error nil)))
549 (goto-char (point-min)))))
551 (defun eww-display-image (buffer)
552 (let ((data (shr-parse-image-data)))
553 (unless (buffer-live-p buffer)
554 (error "Buffer %s doesn't exist" buffer))
555 (with-current-buffer buffer
556 (let ((inhibit-read-only t))
558 (shr-put-image data nil))
559 (goto-char (point-min)))))
561 (declare-function mailcap-view-mime "mailcap" (type))
562 (defun eww-display-pdf ()
563 (let ((data (buffer-substring (point) (point-max))))
564 (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
565 (let ((coding-system-for-write 'raw-text)
566 (inhibit-read-only t))
569 (mailcap-view-mime "application/pdf")))
570 (goto-char (point-min)))
572 (defun eww-setup-buffer ()
573 (when (or (plist-get eww-data :url)
574 (plist-get eww-data :dom))
576 (let ((inhibit-read-only t))
579 (setq bidi-paragraph-direction 'left-to-right)
580 (unless (eq major-mode 'eww-mode)
583 (defun eww-current-url nil
584 "Return URI of the Web page the current EWW buffer is visiting."
585 (plist-get eww-data :url))
587 (defun eww-links-at-point ()
588 "Return list of URIs, if any, linked at point."
590 (list (get-text-property (point) 'shr-url)
591 (get-text-property (point) 'image-url))))
593 (defun eww-view-source ()
594 "View the HTML source code of the current page."
596 (let ((buf (get-buffer-create "*eww-source*"))
597 (source (plist-get eww-data :source)))
598 (with-current-buffer buf
599 (let ((inhibit-read-only t))
600 (delete-region (point-min) (point-max))
601 (insert (or source "no source"))
602 (goto-char (point-min))
603 ;; Decode the source and set the buffer's encoding according
604 ;; to what the HTML source specifies in its 'charset' header,
606 (let ((cs (find-auto-coding "" (point-max))))
609 (when (coding-system-p cs)
610 (decode-coding-region (point-min) (point-max) cs)
611 (setq buffer-file-coding-system last-coding-system-used))))
612 (when (fboundp 'html-mode)
616 (defun eww-readable ()
617 "View the main \"readable\" parts of the current web page.
618 This command uses heuristics to find the parts of the web page that
619 contains the main textual portion, leaving out navigation menus and
622 (let* ((old-data eww-data)
623 (dom (with-temp-buffer
624 (insert (plist-get old-data :source))
626 (decode-coding-region (point-min) (point-max) 'utf-8)
627 (coding-system-error nil))
628 (libxml-parse-html-region (point-min) (point-max))))
629 (base (plist-get eww-data :url)))
630 (eww-score-readability dom)
632 (eww-display-html nil nil
633 (list 'base (list (cons 'href base))
634 (eww-highest-readability dom))
635 nil (current-buffer))
636 (dolist (elem '(:source :url :title :next :previous :up))
637 (plist-put eww-data elem (plist-get old-data elem)))
638 (eww-update-header-line-format)))
640 (defun eww-score-readability (node)
643 ((memq (dom-tag node) '(script head comment))
645 ((eq (dom-tag node) 'meta)
647 ((eq (dom-tag node) 'img)
649 ((eq (dom-tag node) 'a)
650 (setq score (- (length (split-string (dom-text node))))))
652 (dolist (elem (dom-children node))
654 (setq score (+ score (length (split-string elem))))
656 (or (cdr (assoc :eww-readability-score (cdr elem)))
657 (eww-score-readability elem))))))))
658 ;; Cache the score of the node to avoid recomputing all the time.
659 (dom-set-attribute node :eww-readability-score score)
662 (defun eww-highest-readability (node)
665 (dolist (elem (dom-non-text-children node))
666 (when (> (or (dom-attr
667 (setq highest (eww-highest-readability elem))
668 :eww-readability-score)
669 most-negative-fixnum)
670 (or (dom-attr result :eww-readability-score)
671 most-negative-fixnum))
672 (setq result highest)))
676 (let ((map (make-sparse-keymap)))
677 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
678 (define-key map "G" 'eww)
679 (define-key map [?\t] 'shr-next-link)
680 (define-key map [?\M-\t] 'shr-previous-link)
681 (define-key map [backtab] 'shr-previous-link)
682 (define-key map [delete] 'scroll-down-command)
683 (define-key map "l" 'eww-back-url)
684 (define-key map "r" 'eww-forward-url)
685 (define-key map "n" 'eww-next-url)
686 (define-key map "p" 'eww-previous-url)
687 (define-key map "u" 'eww-up-url)
688 (define-key map "t" 'eww-top-url)
689 (define-key map "&" 'eww-browse-with-external-browser)
690 (define-key map "d" 'eww-download)
691 (define-key map "w" 'eww-copy-page-url)
692 (define-key map "C" 'url-cookie-list)
693 (define-key map "v" 'eww-view-source)
694 (define-key map "R" 'eww-readable)
695 (define-key map "H" 'eww-list-histories)
696 (define-key map "E" 'eww-set-character-encoding)
697 (define-key map "s" 'eww-switch-to-buffer)
698 (define-key map "S" 'eww-list-buffers)
699 (define-key map "F" 'eww-toggle-fonts)
700 (define-key map [(meta C)] 'eww-toggle-colors)
702 (define-key map "b" 'eww-add-bookmark)
703 (define-key map "B" 'eww-list-bookmarks)
704 (define-key map [(meta n)] 'eww-next-bookmark)
705 (define-key map [(meta p)] 'eww-previous-bookmark)
707 (easy-menu-define nil map ""
709 ["Exit" quit-window t]
710 ["Close browser" quit-window t]
711 ["Reload" eww-reload t]
712 ["Back to previous page" eww-back-url
713 :active (not (zerop (length eww-history)))]
714 ["Forward to next page" eww-forward-url
715 :active (not (zerop eww-history-position))]
716 ["Browse with external browser" eww-browse-with-external-browser t]
717 ["Download" eww-download t]
718 ["View page source" eww-view-source]
719 ["Copy page URL" eww-copy-page-url t]
720 ["List histories" eww-list-histories t]
721 ["Switch to buffer" eww-switch-to-buffer t]
722 ["List buffers" eww-list-buffers t]
723 ["Add bookmark" eww-add-bookmark t]
724 ["List bookmarks" eww-list-bookmarks t]
725 ["List cookies" url-cookie-list t]
726 ["Toggle fonts" eww-toggle-fonts t]
727 ["Toggle colors" eww-toggle-colors t]
728 ["Character Encoding" eww-set-character-encoding]))
731 (defvar eww-tool-bar-map
732 (let ((map (make-sparse-keymap)))
733 (dolist (tool-bar-item
734 '((quit-window . "close")
735 (eww-reload . "refresh")
736 (eww-back-url . "left-arrow")
737 (eww-forward-url . "right-arrow")
738 (eww-view-source . "show")
739 (eww-copy-page-url . "copy")
740 (eww-add-bookmark . "bookmark_add"))) ;; ...
741 (tool-bar-local-item-from-menu
742 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
744 "Tool bar for `eww-mode'.")
746 ;; Autoload cookie needed by desktop.el.
748 (define-derived-mode eww-mode special-mode "eww"
749 "Mode for browsing the web."
750 (setq-local eww-data (list :title ""))
751 (setq-local browse-url-browser-function #'eww-browse-url)
752 (add-hook 'after-change-functions #'eww-process-text-input nil t)
753 (setq-local eww-history nil)
754 (setq-local eww-history-position 0)
755 (when (boundp 'tool-bar-map)
756 (setq-local tool-bar-map eww-tool-bar-map))
758 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
759 ;; multi-page isearch support
760 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
761 (setq truncate-lines t)
762 (buffer-disable-undo)
763 (setq buffer-read-only t))
766 (defun eww-browse-url (url &optional new-window)
768 (pop-to-buffer-same-window (generate-new-buffer "*eww*"))
772 (defun eww-back-url ()
773 "Go to the previously displayed page."
775 (when (>= eww-history-position (length eww-history))
776 (user-error "No previous page"))
778 (setq eww-history-position (+ eww-history-position 2))
779 (eww-restore-history (elt eww-history (1- eww-history-position))))
781 (defun eww-forward-url ()
782 "Go to the next displayed page."
784 (when (zerop eww-history-position)
785 (user-error "No next page"))
787 (eww-restore-history (elt eww-history (1- eww-history-position))))
789 (defun eww-restore-history (elem)
790 (let ((inhibit-read-only t)
791 (inhibit-modification-hooks t)
792 (text (plist-get elem :text)))
795 (eww-reload) ; FIXME: restore :point?
798 (goto-char (plist-get elem :point))
799 (eww-update-header-line-format))))
801 (defun eww-next-url ()
802 "Go to the page marked `next'.
803 A page is marked `next' if rel=\"next\" appears in a <link>
806 (if (plist-get eww-data :next)
807 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
808 (plist-get eww-data :url)))
809 (user-error "No `next' on this page")))
811 (defun eww-previous-url ()
812 "Go to the page marked `previous'.
813 A page is marked `previous' if rel=\"previous\" appears in a <link>
816 (if (plist-get eww-data :previous)
817 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
818 (plist-get eww-data :url)))
819 (user-error "No `previous' on this page")))
822 "Go to the page marked `up'.
823 A page is marked `up' if rel=\"up\" appears in a <link>
826 (if (plist-get eww-data :up)
827 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
828 (plist-get eww-data :url)))
829 (user-error "No `up' on this page")))
831 (defun eww-top-url ()
832 "Go to the page marked `top'.
833 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
834 appears in a <link> or <a> tag."
836 (let ((best-url (or (plist-get eww-data :start)
837 (plist-get eww-data :contents)
838 (plist-get eww-data :home))))
840 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
841 (user-error "No `top' for this page"))))
843 (defun eww-reload (&optional local encode)
844 "Reload the current page.
845 If LOCAL (the command prefix), don't reload the page from the
846 network, but just re-display the HTML already fetched."
848 (let ((url (plist-get eww-data :url)))
850 (if (null (plist-get eww-data :dom))
851 (error "No current HTML data")
852 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
853 (point) (current-buffer)))
854 (url-retrieve url 'eww-render
855 (list url (point) (current-buffer) encode)))))
859 (defvar eww-form nil)
861 (defvar eww-submit-map
862 (let ((map (make-sparse-keymap)))
863 (define-key map "\r" 'eww-submit)
864 (define-key map [(control c) (control c)] 'eww-submit)
867 (defvar eww-submit-file
868 (let ((map (make-sparse-keymap)))
869 (define-key map "\r" 'eww-select-file)
870 (define-key map [(control c) (control c)] 'eww-submit)
873 (defvar eww-checkbox-map
874 (let ((map (make-sparse-keymap)))
875 (define-key map " " 'eww-toggle-checkbox)
876 (define-key map "\r" 'eww-toggle-checkbox)
877 (define-key map [(control c) (control c)] 'eww-submit)
881 (let ((map (make-keymap)))
882 (set-keymap-parent map text-mode-map)
883 (define-key map "\r" 'eww-submit)
884 (define-key map [(control a)] 'eww-beginning-of-text)
885 (define-key map [(control c) (control c)] 'eww-submit)
886 (define-key map [(control e)] 'eww-end-of-text)
887 (define-key map [?\t] 'shr-next-link)
888 (define-key map [?\M-\t] 'shr-previous-link)
891 (defvar eww-textarea-map
892 (let ((map (make-keymap)))
893 (set-keymap-parent map text-mode-map)
894 (define-key map "\r" 'forward-line)
895 (define-key map [(control c) (control c)] 'eww-submit)
896 (define-key map [?\t] 'shr-next-link)
897 (define-key map [?\M-\t] 'shr-previous-link)
900 (defvar eww-select-map
901 (let ((map (make-sparse-keymap)))
902 (define-key map "\r" 'eww-change-select)
903 (define-key map [(control c) (control c)] 'eww-submit)
906 (defun eww-beginning-of-text ()
907 "Move to the start of the input field."
909 (goto-char (eww-beginning-of-field)))
911 (defun eww-end-of-text ()
912 "Move to the end of the text in the input field."
914 (goto-char (eww-end-of-field))
915 (let ((start (eww-beginning-of-field)))
916 (while (and (equal (following-char) ? )
919 (when (> (point) start)
922 (defun eww-beginning-of-field ()
926 ((not (eq (get-text-property (point) 'eww-form)
927 (get-text-property (1- (point)) 'eww-form)))
930 (previous-single-property-change
931 (point) 'eww-form nil (point-min)))))
933 (defun eww-end-of-field ()
934 (1- (next-single-property-change
935 (point) 'eww-form nil (point-max))))
937 (defun eww-tag-form (dom)
938 (let ((eww-form (list (cons :method (dom-attr dom 'method))
939 (cons :action (dom-attr dom 'action))))
942 (shr-ensure-paragraph)
947 (when (> (point) start)
948 (put-text-property start (1+ start)
949 'eww-form eww-form))))
951 (defun eww-form-submit (dom)
952 (let ((start (point))
953 (value (dom-attr dom 'value)))
955 (if (zerop (length value))
959 (add-face-text-property start (point) 'eww-form-submit)
960 (put-text-property start (point) 'eww-form
961 (list :eww-form eww-form
964 :name (dom-attr dom 'name)))
965 (put-text-property start (point) 'keymap eww-submit-map)
968 (defun eww-form-checkbox (dom)
969 (let ((start (point)))
970 (if (dom-attr dom 'checked)
971 (insert eww-form-checkbox-selected-symbol)
972 (insert eww-form-checkbox-symbol))
973 (add-face-text-property start (point) 'eww-form-checkbox)
974 (put-text-property start (point) 'eww-form
975 (list :eww-form eww-form
976 :value (dom-attr dom 'value)
977 :type (downcase (dom-attr dom 'type))
978 :checked (dom-attr dom 'checked)
979 :name (dom-attr dom 'name)))
980 (put-text-property start (point) 'keymap eww-checkbox-map)
983 (defun eww-form-file (dom)
984 (let ((start (point))
985 (value (dom-attr dom 'value)))
987 (if (zerop (length value))
991 (add-face-text-property start (point) 'eww-form-file)
993 (put-text-property start (point) 'eww-form
994 (list :eww-form eww-form
995 :value (dom-attr dom 'value)
996 :type (downcase (dom-attr dom 'type))
997 :name (dom-attr dom 'name)))
998 (put-text-property start (point) 'keymap eww-submit-file)
1001 (defun eww-select-file ()
1002 "Change the value of the upload file menu under point."
1004 (let* ((input (get-text-property (point) 'eww-form)))
1006 (let ((insert-default-directory t))
1007 (read-file-name "filename: "))))
1008 (eww-update-field filename (length "Browse"))
1009 (plist-put input :filename filename))))
1011 (defun eww-form-text (dom)
1012 (let ((start (point))
1013 (type (downcase (or (dom-attr dom 'type) "text")))
1014 (value (or (dom-attr dom 'value) ""))
1015 (width (string-to-number (or (dom-attr dom 'size) "40")))
1016 (readonly-property (if (or (dom-attr dom 'disabled)
1017 (dom-attr dom 'readonly))
1019 'inhibit-read-only)))
1021 (when (< (length value) width)
1022 (insert (make-string (- width (length value)) ? )))
1023 (put-text-property start (point) 'face 'eww-form-text)
1024 (put-text-property start (point) 'inhibit-read-only t)
1025 (put-text-property start (point) 'local-map eww-text-map)
1026 (put-text-property start (point) readonly-property t)
1027 (put-text-property start (point) 'eww-form
1028 (list :eww-form eww-form
1031 :name (dom-attr dom 'name)))
1034 (defconst eww-text-input-types '("text" "password" "textarea"
1035 "color" "date" "datetime" "datetime-local"
1036 "email" "month" "number" "search" "tel"
1037 "time" "url" "week")
1038 "List of input types which represent a text input.
1039 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1041 (defun eww-process-text-input (beg end replace-length)
1042 (when-let (pos (and (< (1+ end) (point-max))
1043 (> (1- end) (point-min))
1045 ((get-text-property (1+ end) 'eww-form)
1047 ((get-text-property (1- end) 'eww-form)
1049 (let* ((form (get-text-property pos 'eww-form))
1050 (properties (text-properties-at pos))
1051 (buffer-undo-list t)
1052 (inhibit-read-only t)
1053 (length (- end beg replace-length))
1054 (type (plist-get form :type)))
1056 (member type eww-text-input-types))
1059 ;; Delete some space at the end.
1062 (if (equal type "textarea")
1063 (1- (line-end-position))
1064 (eww-end-of-field)))
1065 (while (and (> length 0)
1066 (eql (char-after (1- (point))) ? ))
1067 (delete-region (1- (point)) (point))
1074 (if (equal type "textarea")
1075 (1- (line-end-position))
1076 (1+ (eww-end-of-field))))
1077 (let ((start (point)))
1078 (insert (make-string (abs length) ? ))
1079 (set-text-properties start (point) properties))
1080 (goto-char (1- end)))))
1081 (set-text-properties (plist-get form :start) (plist-get form :end)
1083 (let ((value (buffer-substring-no-properties
1084 (eww-beginning-of-field)
1085 (eww-end-of-field))))
1086 (when (string-match " +\\'" value)
1087 (setq value (substring value 0 (match-beginning 0))))
1088 (plist-put form :value value)
1089 (when (equal type "password")
1090 ;; Display passwords as asterisks.
1091 (let ((start (eww-beginning-of-field)))
1093 start (+ start (length value))
1094 'display (make-string (length value) ?*)))))))))
1096 (defun eww-tag-textarea (dom)
1097 (let ((start (point))
1098 (value (or (dom-attr dom 'value) ""))
1099 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1100 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1102 (shr-ensure-newline)
1104 (shr-ensure-newline)
1105 (when (< (count-lines start (point)) lines)
1106 (dotimes (_ (- lines (count-lines start (point))))
1108 (setq end (point-marker))
1110 (while (< (point) end)
1112 (let ((pad (- width (- (point) (line-beginning-position)))))
1114 (insert (make-string pad ? ))))
1115 (add-face-text-property (line-beginning-position)
1116 (point) 'eww-form-textarea)
1117 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1118 (put-text-property (line-beginning-position) (point)
1119 'local-map eww-textarea-map)
1121 (put-text-property start (point) 'eww-form
1122 (list :eww-form eww-form
1125 :name (dom-attr dom 'name)))))
1127 (defun eww-tag-input (dom)
1128 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1131 ((or (equal type "checkbox")
1132 (equal type "radio"))
1133 (eww-form-checkbox dom))
1134 ((equal type "file")
1135 (eww-form-file dom))
1136 ((equal type "submit")
1137 (eww-form-submit dom))
1138 ((equal type "hidden")
1139 (let ((form eww-form)
1140 (name (dom-attr dom 'name)))
1141 ;; Don't add <input type=hidden> elements repeatedly.
1143 (or (not (consp (car form)))
1144 (not (eq (caar form) 'hidden))
1145 (not (equal (plist-get (cdr (car form)) :name)
1147 (setq form (cdr form)))
1149 (nconc eww-form (list
1152 :value (or (dom-attr dom 'value) "")))))))
1154 (eww-form-text dom)))
1155 (unless (= start (point))
1156 (put-text-property start (1+ start) 'help-echo "Input field")
1157 ;; Mark this as an element we can TAB to.
1158 (put-text-property start (1+ start) 'shr-url dom))))
1160 (defun eww-tag-select (dom)
1161 (shr-ensure-paragraph)
1162 (let ((menu (list :name (dom-attr dom 'name)
1163 :eww-form eww-form))
1168 (if (eq (dom-tag dom) 'optgroup)
1169 (dolist (groupelem (dom-children dom))
1170 (unless (dom-attr groupelem 'disabled)
1171 (setq opelem (append opelem (list groupelem)))))
1172 (setq opelem (list dom)))
1173 (dolist (elem opelem)
1174 (when (eq (dom-tag elem) 'option)
1175 (when (dom-attr elem 'selected)
1176 (nconc menu (list :value (dom-attr elem 'value))))
1177 (let ((display (dom-text elem)))
1178 (setq max (max max (length display)))
1180 :value (dom-attr elem 'value)
1184 (setq options (nreverse options))
1185 ;; If we have no selected values, default to the first value.
1186 (unless (plist-get menu :value)
1187 (nconc menu (list :value (nth 2 (car options)))))
1188 (nconc menu options)
1189 (let ((selected (eww-select-display menu)))
1191 (make-string (- max (length selected)) ? )))
1192 (put-text-property start (point) 'eww-form menu)
1193 (add-face-text-property start (point) 'eww-form-select)
1194 (put-text-property start (point) 'keymap eww-select-map)
1195 (unless (= start (point))
1196 (put-text-property start (1+ start) 'help-echo "select field"))
1197 (shr-ensure-paragraph))))
1199 (defun eww-select-display (select)
1200 (let ((value (plist-get select :value))
1202 (dolist (elem select)
1203 (when (and (consp elem)
1204 (eq (car elem) 'item)
1205 (equal value (plist-get (cdr elem) :value)))
1206 (setq display (plist-get (cdr elem) :display))))
1209 (defun eww-change-select ()
1210 "Change the value of the select drop-down menu under point."
1212 (let* ((input (get-text-property (point) 'eww-form))
1213 (completion-ignore-case t)
1216 (mapcar (lambda (elem)
1218 (eq (car elem) 'item)
1219 (cons (plist-get (cdr elem) :display)
1220 (plist-get (cdr elem) :value))))
1223 (completing-read "Change value: " options nil 'require-match))
1224 (inhibit-read-only t))
1225 (plist-put input :value (cdr (assoc-string display options t)))
1227 (eww-update-field display))))
1229 (defun eww-update-field (string &optional offset)
1232 (let ((properties (text-properties-at (point)))
1233 (start (+ (eww-beginning-of-field) offset))
1234 (current-end (1+ (eww-end-of-field)))
1235 (new-end (+ (eww-beginning-of-field) (length string)))
1236 (inhibit-read-only t))
1237 (delete-region start current-end)
1238 (forward-char offset)
1240 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1242 (set-text-properties start new-end properties))
1245 (defun eww-toggle-checkbox ()
1246 "Toggle the value of the checkbox under point."
1248 (let* ((input (get-text-property (point) 'eww-form))
1249 (type (plist-get input :type)))
1250 (if (equal type "checkbox")
1253 (if (plist-get input :checked)
1255 (plist-put input :checked nil)
1256 (eww-update-field eww-form-checkbox-symbol))
1257 (plist-put input :checked t)
1258 (eww-update-field eww-form-checkbox-selected-symbol))))
1259 ;; Radio button. Switch all other buttons off.
1260 (let ((name (plist-get input :name)))
1262 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1263 (when (equal (plist-get (cdr elem) :name) name)
1264 (goto-char (car elem))
1265 (if (not (eq (cdr elem) input))
1267 (plist-put input :checked nil)
1268 (eww-update-field eww-form-checkbox-symbol))
1269 (plist-put input :checked t)
1270 (eww-update-field eww-form-checkbox-selected-symbol)))))
1271 (forward-char 1)))))
1273 (defun eww-inputs (form)
1274 (let ((start (point-min))
1277 (< start (point-max)))
1278 (when (or (get-text-property start 'eww-form)
1279 (setq start (next-single-property-change start 'eww-form)))
1280 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1282 (push (cons start (get-text-property start 'eww-form))
1284 (setq start (next-single-property-change start 'eww-form))))
1287 (defun eww-size-text-inputs ()
1288 (let ((start (point-min)))
1290 (< start (point-max)))
1291 (when (or (get-text-property start 'eww-form)
1292 (setq start (next-single-property-change start 'eww-form)))
1293 (let ((props (get-text-property start 'eww-form)))
1294 (plist-put props :start start)
1295 (setq start (next-single-property-change
1296 start 'eww-form nil (point-max)))
1297 (plist-put props :end start))))))
1299 (defun eww-input-value (input)
1300 (let ((type (plist-get input :type))
1301 (value (plist-get input :value)))
1303 ((equal type "textarea")
1306 (goto-char (point-min))
1307 (while (re-search-forward "^ +\n\\| +$" nil t)
1308 (replace-match "" t t))
1311 (if (string-match " +\\'" value)
1312 (substring value 0 (match-beginning 0))
1315 (defun eww-submit ()
1316 "Submit the current form."
1318 (let* ((this-input (get-text-property (point) 'eww-form))
1319 (form (plist-get this-input :eww-form))
1321 (dolist (elem (sort (eww-inputs form)
1323 (< (car o1) (car o2)))))
1324 (let* ((input (cdr elem))
1325 (input-start (car elem))
1326 (name (plist-get input :name)))
1329 ((member (plist-get input :type) '("checkbox" "radio"))
1330 (when (plist-get input :checked)
1331 (push (cons name (plist-get input :value))
1333 ((equal (plist-get input :type) "file")
1335 (list (cons "filedata"
1337 (insert-file-contents
1338 (plist-get input :filename))
1340 (cons "name" (plist-get input :name))
1341 (cons "filename" (plist-get input :filename))))
1343 ((equal (plist-get input :type) "submit")
1344 ;; We want the values from buttons if we hit a button if
1345 ;; we hit enter on it, or if it's the first button after
1346 ;; the field we did hit return on.
1347 (when (or (eq input this-input)
1348 (and (not (eq input this-input))
1350 (> input-start (point))))
1351 (setq next-submit t)
1352 (push (cons name (plist-get input :value))
1355 (push (cons name (eww-input-value input))
1358 (when (and (consp elem)
1359 (eq (car elem) 'hidden))
1360 (push (cons (plist-get (cdr elem) :name)
1361 (or (plist-get (cdr elem) :value) ""))
1363 (if (and (stringp (cdr (assq :method form)))
1364 (equal (downcase (cdr (assq :method form))) "post"))
1366 (dolist (x values mtype)
1367 (if (equal (car x) "file")
1369 (setq mtype "multipart/form-data"))))
1370 (cond ((equal mtype "multipart/form-data")
1371 (let ((boundary (mml-compute-boundary '())))
1372 (let ((url-request-method "POST")
1373 (url-request-extra-headers
1374 (list (cons "Content-Type"
1375 (concat "multipart/form-data; boundary="
1378 (mm-url-encode-multipart-form-data values boundary)))
1379 (eww-browse-url (shr-expand-url
1380 (cdr (assq :action form))
1381 (plist-get eww-data :url))))))
1383 (let ((url-request-method "POST")
1384 (url-request-extra-headers
1386 "application/x-www-form-urlencoded")))
1388 (mm-url-encode-www-form-urlencoded values)))
1389 (eww-browse-url (shr-expand-url
1390 (cdr (assq :action form))
1391 (plist-get eww-data :url)))))))
1394 (if (cdr (assq :action form))
1395 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1396 (plist-get eww-data :url))
1398 (mm-url-encode-www-form-urlencoded values))))))
1400 (defun eww-browse-with-external-browser (&optional url)
1401 "Browse the current URL with an external browser.
1402 The browser to used is specified by the `shr-external-browser' variable."
1404 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1406 (defun eww-follow-link (&optional external mouse-event)
1407 "Browse the URL under point.
1408 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1409 If EXTERNAL is double prefix, browse in new buffer."
1410 (interactive (list current-prefix-arg last-nonmenu-event))
1411 (mouse-set-point mouse-event)
1412 (let ((url (get-text-property (point) 'shr-url)))
1415 (message "No link under point"))
1416 ((string-match "^mailto:" url)
1417 (browse-url-mail url))
1418 ((and (consp external) (<= (car external) 4))
1419 (funcall shr-external-browser url))
1420 ;; This is a #target url in the same page as the current one.
1421 ((and (url-target (url-generic-parse-url url))
1422 (eww-same-page-p url (plist-get eww-data :url)))
1423 (let ((dom (plist-get eww-data :dom)))
1425 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1427 (eww-browse-url url external)))))
1429 (defun eww-same-page-p (url1 url2)
1430 "Return non-nil if URL1 and URL2 represent the same page.
1431 Differences in #targets are ignored."
1432 (let ((obj1 (url-generic-parse-url url1))
1433 (obj2 (url-generic-parse-url url2)))
1434 (setf (url-target obj1) nil)
1435 (setf (url-target obj2) nil)
1436 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1438 (defun eww-copy-page-url ()
1439 "Copy the URL of the current page into the kill ring."
1441 (message "%s" (plist-get eww-data :url))
1442 (kill-new (plist-get eww-data :url)))
1444 (defun eww-download ()
1445 "Download URL under point to `eww-download-directory'."
1447 (let ((url (get-text-property (point) 'shr-url)))
1449 (message "No URL under point")
1450 (url-retrieve url 'eww-download-callback (list url)))))
1452 (defun eww-download-callback (status url)
1453 (unless (plist-get status :error)
1454 (let* ((obj (url-generic-parse-url url))
1455 (path (car (url-path-and-query obj)))
1456 (file (eww-make-unique-file-name
1457 (eww-decode-url-file-name (file-name-nondirectory path))
1458 eww-download-directory)))
1459 (goto-char (point-min))
1460 (re-search-forward "\r?\n\r?\n")
1461 (write-region (point) (point-max) file)
1462 (message "Saved %s" file))))
1464 (defun eww-decode-url-file-name (string)
1465 (let* ((binary (url-unhex-string string))
1467 (decode-coding-string
1469 ;; Possibly set by `universal-coding-system-argument'.
1470 (or coding-system-for-read
1471 ;; RFC 3986 says that %AB stuff is utf-8.
1472 (if (equal (decode-coding-string binary 'utf-8)
1476 (car (detect-coding-string binary))))))
1477 (encodes (find-coding-systems-string decoded)))
1478 (if (or (equal encodes '(undecided))
1479 (memq (coding-system-base (or file-name-coding-system
1480 default-file-name-coding-system))
1483 ;; If we can't encode the decoded file name (due to language
1484 ;; environment settings), then we return the original, hexified
1488 (defun eww-make-unique-file-name (file directory)
1490 ((zerop (length file))
1492 ((string-match "\\`[.]" file)
1493 (setq file (concat "!" file))))
1497 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1498 (setq stem (match-string 1 file)
1499 suffix (match-string 2)))
1500 (while (file-exists-p (expand-file-name file directory))
1501 (setq file (format "%s(%d)%s" stem count suffix))
1502 (setq count (1+ count)))
1503 (expand-file-name file directory)))
1505 (defun eww-set-character-encoding (charset)
1506 "Set character encoding to CHARSET.
1507 If CHARSET is nil then use UTF-8."
1508 (interactive "zUse character set (default utf-8): ")
1510 (eww-reload nil 'utf-8)
1511 (eww-reload nil charset)))
1513 (defun eww-switch-to-buffer ()
1514 "Prompt for an EWW buffer to display in the selected window."
1516 (let ((completion-extra-properties
1517 '(:annotation-function (lambda (buf)
1518 (with-current-buffer buf
1519 (format " %s" (eww-current-url)))))))
1520 (pop-to-buffer-same-window
1521 (read-buffer "Switch to EWW buffer: "
1522 (cl-loop for buf in (nreverse (buffer-list))
1523 if (with-current-buffer buf (derived-mode-p 'eww-mode))
1527 (with-current-buffer
1528 (if (consp bufn) (cdr bufn) (get-buffer bufn))
1529 (derived-mode-p 'eww-mode)))))))
1531 (defun eww-toggle-fonts ()
1532 "Toggle whether to use monospaced or font-enabled layouts."
1534 (message "Fonts are now %s"
1535 (if (setq shr-use-fonts (not shr-use-fonts))
1540 (defun eww-toggle-colors ()
1541 "Toggle whether to use HTML-specified colors or not."
1543 (message "Colors are now %s"
1544 (if (setq shr-use-colors (not shr-use-colors))
1551 (defvar eww-bookmarks nil)
1553 (defun eww-add-bookmark ()
1554 "Bookmark the current page."
1556 (eww-read-bookmarks)
1557 (dolist (bookmark eww-bookmarks)
1558 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1559 (user-error "Already bookmarked")))
1560 (when (y-or-n-p "Bookmark this page?")
1561 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1562 (plist-get eww-data :title))))
1563 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1564 (push (list :url (plist-get eww-data :url)
1566 :time (current-time-string))
1568 (eww-write-bookmarks)
1569 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1570 (plist-get eww-data :title))))
1572 (defun eww-write-bookmarks ()
1573 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1574 (insert ";; Auto-generated file; don't edit\n")
1575 (pp eww-bookmarks (current-buffer))))
1577 (defun eww-read-bookmarks ()
1578 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1580 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1582 (insert-file-contents file)
1583 (read (current-buffer)))))))
1586 (defun eww-list-bookmarks ()
1587 "Display the bookmarks."
1589 (pop-to-buffer "*eww bookmarks*")
1590 (eww-bookmark-prepare))
1592 (defun eww-bookmark-prepare ()
1593 (eww-read-bookmarks)
1594 (unless eww-bookmarks
1595 (user-error "No bookmarks are defined"))
1596 (set-buffer (get-buffer-create "*eww bookmarks*"))
1598 (let* ((width (/ (window-width) 2))
1599 (format (format "%%-%ds %%s" width))
1600 (inhibit-read-only t)
1603 (setq header-line-format (concat " " (format format "Title" "URL")))
1604 (dolist (bookmark eww-bookmarks)
1606 title (plist-get bookmark :title))
1607 (when (> (length title) width)
1608 (setq title (truncate-string-to-width title width)))
1609 (insert (format format title (plist-get bookmark :url)) "\n")
1610 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1611 (goto-char (point-min))))
1613 (defvar eww-bookmark-kill-ring nil)
1615 (defun eww-bookmark-kill ()
1616 "Kill the current bookmark."
1618 (let* ((start (line-beginning-position))
1619 (bookmark (get-text-property start 'eww-bookmark))
1620 (inhibit-read-only t))
1622 (user-error "No bookmark on the current line"))
1624 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1625 (delete-region start (point))
1626 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1627 (eww-write-bookmarks)))
1629 (defun eww-bookmark-yank ()
1630 "Yank a previously killed bookmark to the current line."
1632 (unless eww-bookmark-kill-ring
1633 (user-error "No previously killed bookmark"))
1635 (let ((inhibit-read-only t)
1638 (insert (pop eww-bookmark-kill-ring))
1639 (setq bookmark (get-text-property start 'eww-bookmark))
1640 (if (= start (point-min))
1641 (push bookmark eww-bookmarks)
1642 (let ((line (count-lines start (point))))
1643 (setcdr (nthcdr (1- line) eww-bookmarks)
1644 (cons bookmark (nthcdr line eww-bookmarks)))))
1645 (eww-write-bookmarks)))
1647 (defun eww-bookmark-browse ()
1648 "Browse the bookmark under point in eww."
1650 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1652 (user-error "No bookmark on the current line"))
1654 (eww-browse-url (plist-get bookmark :url))))
1656 (defun eww-next-bookmark ()
1657 "Go to the next bookmark in the list."
1661 (unless (get-buffer "*eww bookmarks*")
1663 (eww-bookmark-prepare))
1664 (with-current-buffer (get-buffer "*eww bookmarks*")
1665 (when (and (not first)
1668 (setq bookmark (get-text-property (line-beginning-position)
1671 (user-error "No next bookmark")))
1672 (eww-browse-url (plist-get bookmark :url))))
1674 (defun eww-previous-bookmark ()
1675 "Go to the previous bookmark in the list."
1679 (unless (get-buffer "*eww bookmarks*")
1681 (eww-bookmark-prepare))
1682 (with-current-buffer (get-buffer "*eww bookmarks*")
1684 (goto-char (point-max))
1685 (beginning-of-line))
1686 ;; On the final line.
1690 (user-error "No previous bookmark")
1692 (setq bookmark (get-text-property (line-beginning-position)
1694 (eww-browse-url (plist-get bookmark :url))))
1696 (defvar eww-bookmark-mode-map
1697 (let ((map (make-sparse-keymap)))
1698 (define-key map [(control k)] 'eww-bookmark-kill)
1699 (define-key map [(control y)] 'eww-bookmark-yank)
1700 (define-key map "\r" 'eww-bookmark-browse)
1702 (easy-menu-define nil map
1703 "Menu for `eww-bookmark-mode-map'."
1705 ["Exit" quit-window t]
1706 ["Browse" eww-bookmark-browse
1707 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1708 ["Kill" eww-bookmark-kill
1709 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1710 ["Yank" eww-bookmark-yank
1711 :active eww-bookmark-kill-ring]))
1714 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1715 "Mode for listing bookmarks.
1717 \\{eww-bookmark-mode-map}"
1718 (buffer-disable-undo)
1719 (setq truncate-lines t))
1723 (defun eww-save-history ()
1724 (plist-put eww-data :point (point))
1725 (plist-put eww-data :text (buffer-string))
1726 (push eww-data eww-history)
1727 (setq eww-data (list :title ""))
1728 ;; Don't let the history grow infinitely. We store quite a lot of
1730 (when-let (tail (and eww-history-limit
1731 (nthcdr eww-history-limit eww-history)))
1734 (defvar eww-current-buffer)
1736 (defun eww-list-histories ()
1737 "List the eww-histories."
1739 (when (null eww-history)
1740 (error "No eww-histories are defined"))
1741 (let ((eww-history-trans eww-history)
1742 (buffer (current-buffer)))
1743 (set-buffer (get-buffer-create "*eww history*"))
1745 (setq-local eww-current-buffer buffer)
1746 (let ((inhibit-read-only t)
1749 url title format start)
1751 (dolist (history eww-history-trans)
1752 (setq start (point))
1753 (setq domain-length (max domain-length (length (plist-get history :url))))
1754 (setq title-length (max title-length (length (plist-get history :title)))))
1755 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1757 (concat " " (format format "Title" "URL")))
1758 (dolist (history eww-history-trans)
1759 (setq start (point))
1760 (setq url (plist-get history :url))
1761 (setq title (plist-get history :title))
1762 (insert (format format title url))
1764 (put-text-property start (1+ start) 'eww-history history))
1765 (goto-char (point-min)))
1766 (pop-to-buffer "*eww history*")))
1768 (defun eww-history-browse ()
1769 "Browse the history under point in eww."
1771 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1773 (error "No history on the current line"))
1774 (let ((buffer eww-current-buffer))
1777 (pop-to-buffer-same-window buffer)))
1778 (eww-restore-history history)))
1780 (defvar eww-history-mode-map
1781 (let ((map (make-sparse-keymap)))
1782 (define-key map "\r" 'eww-history-browse)
1783 ;; (define-key map "n" 'next-error-no-select)
1784 ;; (define-key map "p" 'previous-error-no-select)
1786 (easy-menu-define nil map
1787 "Menu for `eww-history-mode-map'."
1789 ["Exit" quit-window t]
1790 ["Browse" eww-history-browse
1791 :active (get-text-property (line-beginning-position) 'eww-history)]))
1794 (define-derived-mode eww-history-mode special-mode "eww history"
1795 "Mode for listing eww-histories.
1797 \\{eww-history-mode-map}"
1798 (buffer-disable-undo)
1799 (setq truncate-lines t))
1801 ;;; eww buffers list
1803 (defun eww-list-buffers ()
1804 "Enlist eww buffers."
1807 (current (current-buffer)))
1808 (dolist (buffer (buffer-list))
1809 (with-current-buffer buffer
1810 (when (derived-mode-p 'eww-mode)
1811 (push (vector buffer (plist-get eww-data :title)
1812 (plist-get eww-data :url))
1814 (unless buffers-info
1815 (error "No eww buffers"))
1816 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1817 (set-buffer (get-buffer-create "*eww buffers*"))
1819 (let ((inhibit-read-only t)
1822 url title format start)
1824 (dolist (buffer-info buffers-info)
1825 (setq title-length (max title-length
1826 (length (elt buffer-info 1)))
1827 domain-length (max domain-length
1828 (length (elt buffer-info 2)))))
1829 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1831 (concat " " (format format "Title" "URL")))
1833 (current-buffer-line 1))
1834 (dolist (buffer-info buffers-info)
1836 title (elt buffer-info 1)
1837 url (elt buffer-info 2)
1839 (insert (format format title url))
1841 (let ((buffer (elt buffer-info 0)))
1842 (put-text-property start (1+ start) 'eww-buffer
1844 (when (eq current buffer)
1845 (setq current-buffer-line line))))
1846 (goto-char (point-min))
1847 (forward-line (1- current-buffer-line)))))
1848 (pop-to-buffer "*eww buffers*"))
1850 (defun eww-buffer-select ()
1851 "Switch to eww buffer."
1853 (let ((buffer (get-text-property (line-beginning-position)
1856 (error "No buffer on current line"))
1858 (pop-to-buffer-same-window buffer)))
1860 (defun eww-buffer-show ()
1861 "Display buffer under point in eww buffer list."
1862 (let ((buffer (get-text-property (line-beginning-position)
1865 (error "No buffer on current line"))
1867 (pop-to-buffer-same-window buffer)
1870 (defun eww-buffer-show-next ()
1871 "Move to next eww buffer in the list and display it."
1875 (goto-char (point-min)))
1878 (defun eww-buffer-show-previous ()
1879 "Move to previous eww buffer in the list and display it."
1883 (goto-char (point-max)))
1887 (defun eww-buffer-kill ()
1888 "Kill buffer from eww list."
1890 (let* ((start (line-beginning-position))
1891 (buffer (get-text-property start 'eww-buffer))
1892 (inhibit-read-only t))
1894 (user-error "No buffer on the current line"))
1895 (kill-buffer buffer)
1897 (delete-region start (point)))
1902 (defvar eww-buffers-mode-map
1903 (let ((map (make-sparse-keymap)))
1904 (define-key map [(control k)] 'eww-buffer-kill)
1905 (define-key map "\r" 'eww-buffer-select)
1906 (define-key map "n" 'eww-buffer-show-next)
1907 (define-key map "p" 'eww-buffer-show-previous)
1909 (easy-menu-define nil map
1910 "Menu for `eww-buffers-mode-map'."
1912 ["Exit" quit-window t]
1913 ["Select" eww-buffer-select
1914 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1915 ["Kill" eww-buffer-kill
1916 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1919 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1920 "Mode for listing buffers.
1922 \\{eww-buffers-mode-map}"
1923 (buffer-disable-undo)
1924 (setq truncate-lines t))
1928 (defvar eww-desktop-data-save
1929 '(:url :title :point)
1930 "List of `eww-data' properties to preserve in the desktop file.
1931 Also used when saving `eww-history'.")
1933 (defun eww-desktop-data-1 (alist)
1937 (let ((k (car tail))
1939 (when (memq k eww-desktop-data-save)
1940 (setq acc (cons k (cons v acc)))))
1941 (setq tail (cddr tail)))
1944 (defun eww-desktop-history-duplicate (a b)
1945 (let ((tail a) (r t))
1947 (if (or (memq (car tail) '(:point)) ; ignore :point
1949 (plist-get b (car tail))))
1950 (setq tail (cddr tail))
1956 (defun eww-desktop-misc-data (_directory)
1957 "Return a property list with data used to restore eww buffers.
1958 This list will contain, as :history, the list, whose first element is
1959 the value of `eww-data', and the tail is `eww-history'.
1961 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1962 entries (if any) will be removed from the list.
1964 Only the properties listed in `eww-desktop-data-save' are included.
1965 Generally, the list should not include the (usually overly large)
1966 :dom, :source and :text properties."
1967 (let ((history (mapcar 'eww-desktop-data-1
1968 (cons eww-data eww-history))))
1969 (list :history (if eww-desktop-remove-duplicates
1970 (cl-remove-duplicates
1971 history :test 'eww-desktop-history-duplicate)
1974 (defun eww-restore-desktop (file-name buffer-name misc-data)
1975 "Restore an eww buffer from its desktop file record.
1976 If `eww-restore-desktop' is t or `auto', this function will also
1977 initiate the retrieval of the respective URI in the background.
1978 Otherwise, the restored buffer will contain a prompt to do so by using
1980 (with-current-buffer (get-buffer-create buffer-name)
1982 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1983 (setq eww-history (cdr (plist-get misc-data :history))
1984 eww-data (or (car (plist-get misc-data :history))
1985 ;; backwards compatibility
1986 (list :url (plist-get misc-data :uri))))
1988 (when (plist-get eww-data :url)
1989 (case eww-restore-desktop
1990 ((t auto) (eww (plist-get eww-data :url)))
1991 ((zerop (buffer-size))
1992 (let ((inhibit-read-only t))
1993 (insert (substitute-command-keys
1994 eww-restore-reload-prompt)))))))
1998 (add-to-list 'desktop-locals-to-save
1999 'eww-history-position)
2000 (add-to-list 'desktop-buffer-mode-handlers
2001 '(eww-mode . eww-restore-desktop))
2005 (defun eww-isearch-next-buffer (&optional _buffer wrap)
2006 "Go to the next page to search using `rel' attribute for navigation."
2013 (eww-previous-url)))
2018 ;;; eww.el ends here