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))
414 ;; Remove CRLF before parsing.
415 (while (re-search-forward "\r$" nil t)
416 (replace-match "" t t)))
417 (libxml-parse-html-region (point) (point-max))))))
418 (source (and (null document)
419 (buffer-substring (point) (point-max)))))
420 (with-current-buffer buffer
421 (setq bidi-paragraph-direction nil)
422 (plist-put eww-data :source source)
423 (plist-put eww-data :dom document)
424 (let ((inhibit-read-only t)
425 (inhibit-modification-hooks t)
426 (shr-target-id (url-target (url-generic-parse-url url)))
427 (shr-external-rendering-functions
429 shr-external-rendering-functions
430 '((title . eww-tag-title)
431 (form . eww-tag-form)
432 (input . eww-tag-input)
433 (button . eww-form-submit)
434 (textarea . eww-tag-textarea)
435 (select . eww-tag-select)
436 (link . eww-tag-link)
437 (meta . eww-tag-meta)
440 (shr-insert-document document)
445 (goto-char (point-min))
446 (let ((point (next-single-property-change
447 (point-min) 'shr-target-id)))
451 (goto-char (point-min))
452 ;; Don't leave point inside forms, because the normal eww
453 ;; commands aren't available there.
454 (while (and (not (eobp))
455 (get-text-property (point) 'eww-form))
457 (eww-size-text-inputs))))
459 (defun eww-handle-link (dom)
460 (let* ((rel (dom-attr dom 'rel))
461 (href (dom-attr dom 'href))
463 ;; The text associated with :rel is case-insensitive.
464 (if rel (downcase rel))
466 ;; Texinfo uses "previous", but HTML specifies
467 ;; "prev", so recognize both.
468 ("previous" . :previous)
470 ;; HTML specifies "start" but also "contents",
471 ;; and Gtk seems to use "home". Recognize
472 ;; them all; but store them in different
473 ;; variables so that we can readily choose the
477 ("contents" . :contents)
481 (plist-put eww-data (cdr where) href))))
483 (defvar eww-redirect-level 1)
485 (defun eww-tag-meta (dom)
486 (when (and (cl-equalp (dom-attr dom 'http-equiv) "refresh")
487 (< eww-redirect-level 5))
488 (when-let (refresh (dom-attr dom 'content))
489 (when (or (string-match "^\\([0-9]+\\) *;.*url=\"\\([^\"]+\\)\"" refresh)
490 (string-match "^\\([0-9]+\\) *;.*url=\\([^ ]+\\)" refresh))
491 (let ((timeout (match-string 1 refresh))
492 (url (match-string 2 refresh))
493 (eww-redirect-level (1+ eww-redirect-level)))
494 (if (equal timeout "0")
495 (eww (shr-expand-url url))
497 (dom-node 'a `((href . ,(shr-expand-url url)))
498 (format "Auto refresh in %s second%s disabled"
500 (if (equal timeout "1")
504 (defun eww-tag-link (dom)
505 (eww-handle-link dom)
508 (defun eww-tag-a (dom)
509 (eww-handle-link dom)
510 (let ((start (point)))
512 (put-text-property start (point) 'keymap eww-link-keymap)))
514 (defun eww-update-header-line-format ()
515 (setq header-line-format
516 (and eww-header-line-format
517 (let ((title (plist-get eww-data :title))
518 (peer (plist-get eww-data :peer)))
519 (when (zerop (length title))
520 (setq title "[untitled]"))
521 ;; This connection has is https.
524 (propertize title 'face
525 (if (plist-get peer :warnings)
526 'eww-invalid-certificate
527 'eww-valid-certificate))))
528 (replace-regexp-in-string
531 eww-header-line-format
532 `((?u . ,(or (plist-get eww-data :url) ""))
535 (defun eww-tag-title (dom)
536 (plist-put eww-data :title
537 (replace-regexp-in-string
539 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
540 (eww-update-header-line-format))
542 (defun eww-display-raw (buffer &optional encode)
543 (let ((data (buffer-substring (point) (point-max))))
544 (unless (buffer-live-p buffer)
545 (error "Buffer %s doesn't exist" buffer))
546 (with-current-buffer buffer
547 (let ((inhibit-read-only t))
551 (decode-coding-region (point-min) (1+ (length data)) encode)
552 (coding-system-error nil)))
553 (goto-char (point-min)))))
555 (defun eww-display-image (buffer)
556 (let ((data (shr-parse-image-data)))
557 (unless (buffer-live-p buffer)
558 (error "Buffer %s doesn't exist" buffer))
559 (with-current-buffer buffer
560 (let ((inhibit-read-only t))
562 (shr-put-image data nil))
563 (goto-char (point-min)))))
565 (declare-function mailcap-view-mime "mailcap" (type))
566 (defun eww-display-pdf ()
567 (let ((data (buffer-substring (point) (point-max))))
568 (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
569 (let ((coding-system-for-write 'raw-text)
570 (inhibit-read-only t))
573 (mailcap-view-mime "application/pdf")))
574 (goto-char (point-min)))
576 (defun eww-setup-buffer ()
577 (when (or (plist-get eww-data :url)
578 (plist-get eww-data :dom))
580 (let ((inhibit-read-only t))
583 (setq bidi-paragraph-direction nil)
584 (unless (eq major-mode 'eww-mode)
587 (defun eww-current-url nil
588 "Return URI of the Web page the current EWW buffer is visiting."
589 (plist-get eww-data :url))
591 (defun eww-links-at-point ()
592 "Return list of URIs, if any, linked at point."
594 (list (get-text-property (point) 'shr-url)
595 (get-text-property (point) 'image-url))))
597 (defun eww-view-source ()
598 "View the HTML source code of the current page."
600 (let ((buf (get-buffer-create "*eww-source*"))
601 (source (plist-get eww-data :source)))
602 (with-current-buffer buf
603 (let ((inhibit-read-only t))
604 (delete-region (point-min) (point-max))
605 (insert (or source "no source"))
606 (goto-char (point-min))
607 ;; Decode the source and set the buffer's encoding according
608 ;; to what the HTML source specifies in its 'charset' header,
610 (let ((cs (find-auto-coding "" (point-max))))
613 (when (coding-system-p cs)
614 (decode-coding-region (point-min) (point-max) cs)
615 (setq buffer-file-coding-system last-coding-system-used))))
616 (when (fboundp 'html-mode)
620 (defun eww-toggle-paragraph-direction ()
621 "Cycle the paragraph direction between left-to-right, right-to-left and auto."
623 (setq bidi-paragraph-direction
624 (cond ((eq bidi-paragraph-direction 'left-to-right)
626 ((eq bidi-paragraph-direction 'right-to-left)
630 (message "The paragraph direction is now %s"
631 (if (null bidi-paragraph-direction)
633 bidi-paragraph-direction)))
635 (defun eww-readable ()
636 "View the main \"readable\" parts of the current web page.
637 This command uses heuristics to find the parts of the web page that
638 contains the main textual portion, leaving out navigation menus and
641 (let* ((old-data eww-data)
642 (dom (with-temp-buffer
643 (insert (plist-get old-data :source))
645 (decode-coding-region (point-min) (point-max) 'utf-8)
646 (coding-system-error nil))
647 (libxml-parse-html-region (point-min) (point-max))))
648 (base (plist-get eww-data :url)))
649 (eww-score-readability dom)
651 (eww-display-html nil nil
652 (list 'base (list (cons 'href base))
653 (eww-highest-readability dom))
654 nil (current-buffer))
655 (dolist (elem '(:source :url :title :next :previous :up))
656 (plist-put eww-data elem (plist-get old-data elem)))
657 (eww-update-header-line-format)))
659 (defun eww-score-readability (node)
662 ((memq (dom-tag node) '(script head comment))
664 ((eq (dom-tag node) 'meta)
666 ((eq (dom-tag node) 'img)
668 ((eq (dom-tag node) 'a)
669 (setq score (- (length (split-string (dom-text node))))))
671 (dolist (elem (dom-children node))
673 (setq score (+ score (length (split-string elem))))
675 (or (cdr (assoc :eww-readability-score (cdr elem)))
676 (eww-score-readability elem))))))))
677 ;; Cache the score of the node to avoid recomputing all the time.
678 (dom-set-attribute node :eww-readability-score score)
681 (defun eww-highest-readability (node)
684 (dolist (elem (dom-non-text-children node))
685 (when (> (or (dom-attr
686 (setq highest (eww-highest-readability elem))
687 :eww-readability-score)
688 most-negative-fixnum)
689 (or (dom-attr result :eww-readability-score)
690 most-negative-fixnum))
691 (setq result highest)))
695 (let ((map (make-sparse-keymap)))
696 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
697 (define-key map "G" 'eww)
698 (define-key map [?\t] 'shr-next-link)
699 (define-key map [?\M-\t] 'shr-previous-link)
700 (define-key map [backtab] 'shr-previous-link)
701 (define-key map [delete] 'scroll-down-command)
702 (define-key map "l" 'eww-back-url)
703 (define-key map "r" 'eww-forward-url)
704 (define-key map "n" 'eww-next-url)
705 (define-key map "p" 'eww-previous-url)
706 (define-key map "u" 'eww-up-url)
707 (define-key map "t" 'eww-top-url)
708 (define-key map "&" 'eww-browse-with-external-browser)
709 (define-key map "d" 'eww-download)
710 (define-key map "w" 'eww-copy-page-url)
711 (define-key map "C" 'url-cookie-list)
712 (define-key map "v" 'eww-view-source)
713 (define-key map "R" 'eww-readable)
714 (define-key map "H" 'eww-list-histories)
715 (define-key map "E" 'eww-set-character-encoding)
716 (define-key map "s" 'eww-switch-to-buffer)
717 (define-key map "S" 'eww-list-buffers)
718 (define-key map "F" 'eww-toggle-fonts)
719 (define-key map "D" 'eww-toggle-paragraph-direction)
720 (define-key map [(meta C)] 'eww-toggle-colors)
722 (define-key map "b" 'eww-add-bookmark)
723 (define-key map "B" 'eww-list-bookmarks)
724 (define-key map [(meta n)] 'eww-next-bookmark)
725 (define-key map [(meta p)] 'eww-previous-bookmark)
727 (easy-menu-define nil map ""
729 ["Exit" quit-window t]
730 ["Close browser" quit-window t]
731 ["Reload" eww-reload t]
732 ["Back to previous page" eww-back-url
733 :active (not (zerop (length eww-history)))]
734 ["Forward to next page" eww-forward-url
735 :active (not (zerop eww-history-position))]
736 ["Browse with external browser" eww-browse-with-external-browser t]
737 ["Download" eww-download t]
738 ["View page source" eww-view-source]
739 ["Copy page URL" eww-copy-page-url t]
740 ["List histories" eww-list-histories t]
741 ["Switch to buffer" eww-switch-to-buffer t]
742 ["List buffers" eww-list-buffers t]
743 ["Add bookmark" eww-add-bookmark t]
744 ["List bookmarks" eww-list-bookmarks t]
745 ["List cookies" url-cookie-list t]
746 ["Toggle fonts" eww-toggle-fonts t]
747 ["Toggle colors" eww-toggle-colors t]
748 ["Character Encoding" eww-set-character-encoding]
749 ["Toggle Paragraph Direction" eww-toggle-paragraph-direction]))
752 (defvar eww-tool-bar-map
753 (let ((map (make-sparse-keymap)))
754 (dolist (tool-bar-item
755 '((quit-window . "close")
756 (eww-reload . "refresh")
757 (eww-back-url . "left-arrow")
758 (eww-forward-url . "right-arrow")
759 (eww-view-source . "show")
760 (eww-copy-page-url . "copy")
761 (eww-add-bookmark . "bookmark_add"))) ;; ...
762 (tool-bar-local-item-from-menu
763 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
765 "Tool bar for `eww-mode'.")
767 ;; Autoload cookie needed by desktop.el.
769 (define-derived-mode eww-mode special-mode "eww"
770 "Mode for browsing the web."
771 (setq-local eww-data (list :title ""))
772 (setq-local browse-url-browser-function #'eww-browse-url)
773 (add-hook 'after-change-functions #'eww-process-text-input nil t)
774 (setq-local eww-history nil)
775 (setq-local eww-history-position 0)
776 (when (boundp 'tool-bar-map)
777 (setq-local tool-bar-map eww-tool-bar-map))
779 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
780 ;; multi-page isearch support
781 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
782 (setq truncate-lines t)
783 (buffer-disable-undo)
784 (setq buffer-read-only t))
787 (defun eww-browse-url (url &optional new-window)
789 (pop-to-buffer-same-window (generate-new-buffer "*eww*"))
793 (defun eww-back-url ()
794 "Go to the previously displayed page."
796 (when (>= eww-history-position (length eww-history))
797 (user-error "No previous page"))
799 (setq eww-history-position (+ eww-history-position 2))
800 (eww-restore-history (elt eww-history (1- eww-history-position))))
802 (defun eww-forward-url ()
803 "Go to the next displayed page."
805 (when (zerop eww-history-position)
806 (user-error "No next page"))
808 (eww-restore-history (elt eww-history (1- eww-history-position))))
810 (defun eww-restore-history (elem)
811 (let ((inhibit-read-only t)
812 (inhibit-modification-hooks t)
813 (text (plist-get elem :text)))
816 (eww-reload) ; FIXME: restore :point?
819 (goto-char (plist-get elem :point))
820 (eww-update-header-line-format))))
822 (defun eww-next-url ()
823 "Go to the page marked `next'.
824 A page is marked `next' if rel=\"next\" appears in a <link>
827 (if (plist-get eww-data :next)
828 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
829 (plist-get eww-data :url)))
830 (user-error "No `next' on this page")))
832 (defun eww-previous-url ()
833 "Go to the page marked `previous'.
834 A page is marked `previous' if rel=\"previous\" appears in a <link>
837 (if (plist-get eww-data :previous)
838 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
839 (plist-get eww-data :url)))
840 (user-error "No `previous' on this page")))
843 "Go to the page marked `up'.
844 A page is marked `up' if rel=\"up\" appears in a <link>
847 (if (plist-get eww-data :up)
848 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
849 (plist-get eww-data :url)))
850 (user-error "No `up' on this page")))
852 (defun eww-top-url ()
853 "Go to the page marked `top'.
854 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
855 appears in a <link> or <a> tag."
857 (let ((best-url (or (plist-get eww-data :start)
858 (plist-get eww-data :contents)
859 (plist-get eww-data :home))))
861 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
862 (user-error "No `top' for this page"))))
864 (defun eww-reload (&optional local encode)
865 "Reload the current page.
866 If LOCAL (the command prefix), don't reload the page from the
867 network, but just re-display the HTML already fetched."
869 (let ((url (plist-get eww-data :url)))
871 (if (null (plist-get eww-data :dom))
872 (error "No current HTML data")
873 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
874 (point) (current-buffer)))
875 (url-retrieve url 'eww-render
876 (list url (point) (current-buffer) encode)))))
880 (defvar eww-form nil)
882 (defvar eww-submit-map
883 (let ((map (make-sparse-keymap)))
884 (define-key map "\r" 'eww-submit)
885 (define-key map [(control c) (control c)] 'eww-submit)
888 (defvar eww-submit-file
889 (let ((map (make-sparse-keymap)))
890 (define-key map "\r" 'eww-select-file)
891 (define-key map [(control c) (control c)] 'eww-submit)
894 (defvar eww-checkbox-map
895 (let ((map (make-sparse-keymap)))
896 (define-key map " " 'eww-toggle-checkbox)
897 (define-key map "\r" 'eww-toggle-checkbox)
898 (define-key map [(control c) (control c)] 'eww-submit)
902 (let ((map (make-keymap)))
903 (set-keymap-parent map text-mode-map)
904 (define-key map "\r" 'eww-submit)
905 (define-key map [(control a)] 'eww-beginning-of-text)
906 (define-key map [(control c) (control c)] 'eww-submit)
907 (define-key map [(control e)] 'eww-end-of-text)
908 (define-key map [?\t] 'shr-next-link)
909 (define-key map [?\M-\t] 'shr-previous-link)
912 (defvar eww-textarea-map
913 (let ((map (make-keymap)))
914 (set-keymap-parent map text-mode-map)
915 (define-key map "\r" 'forward-line)
916 (define-key map [(control c) (control c)] 'eww-submit)
917 (define-key map [?\t] 'shr-next-link)
918 (define-key map [?\M-\t] 'shr-previous-link)
921 (defvar eww-select-map
922 (let ((map (make-sparse-keymap)))
923 (define-key map "\r" 'eww-change-select)
924 (define-key map [(control c) (control c)] 'eww-submit)
927 (defun eww-beginning-of-text ()
928 "Move to the start of the input field."
930 (goto-char (eww-beginning-of-field)))
932 (defun eww-end-of-text ()
933 "Move to the end of the text in the input field."
935 (goto-char (eww-end-of-field))
936 (let ((start (eww-beginning-of-field)))
937 (while (and (equal (following-char) ? )
940 (when (> (point) start)
943 (defun eww-beginning-of-field ()
947 ((not (eq (get-text-property (point) 'eww-form)
948 (get-text-property (1- (point)) 'eww-form)))
951 (previous-single-property-change
952 (point) 'eww-form nil (point-min)))))
954 (defun eww-end-of-field ()
955 (1- (next-single-property-change
956 (point) 'eww-form nil (point-max))))
958 (defun eww-tag-form (dom)
959 (let ((eww-form (list (cons :method (dom-attr dom 'method))
960 (cons :action (dom-attr dom 'action))))
963 (shr-ensure-paragraph)
968 (when (> (point) start)
969 (put-text-property start (1+ start)
970 'eww-form eww-form))))
972 (defun eww-form-submit (dom)
973 (let ((start (point))
974 (value (dom-attr dom 'value)))
976 (if (zerop (length value))
980 (add-face-text-property start (point) 'eww-form-submit)
981 (put-text-property start (point) 'eww-form
982 (list :eww-form eww-form
985 :name (dom-attr dom 'name)))
986 (put-text-property start (point) 'keymap eww-submit-map)
989 (defun eww-form-checkbox (dom)
990 (let ((start (point)))
991 (if (dom-attr dom 'checked)
992 (insert eww-form-checkbox-selected-symbol)
993 (insert eww-form-checkbox-symbol))
994 (add-face-text-property start (point) 'eww-form-checkbox)
995 (put-text-property start (point) 'eww-form
996 (list :eww-form eww-form
997 :value (dom-attr dom 'value)
998 :type (downcase (dom-attr dom 'type))
999 :checked (dom-attr dom 'checked)
1000 :name (dom-attr dom 'name)))
1001 (put-text-property start (point) 'keymap eww-checkbox-map)
1004 (defun eww-form-file (dom)
1005 (let ((start (point))
1006 (value (dom-attr dom 'value)))
1008 (if (zerop (length value))
1012 (add-face-text-property start (point) 'eww-form-file)
1014 (put-text-property start (point) 'eww-form
1015 (list :eww-form eww-form
1016 :value (dom-attr dom 'value)
1017 :type (downcase (dom-attr dom 'type))
1018 :name (dom-attr dom 'name)))
1019 (put-text-property start (point) 'keymap eww-submit-file)
1022 (defun eww-select-file ()
1023 "Change the value of the upload file menu under point."
1025 (let* ((input (get-text-property (point) 'eww-form)))
1027 (let ((insert-default-directory t))
1028 (read-file-name "filename: "))))
1029 (eww-update-field filename (length "Browse"))
1030 (plist-put input :filename filename))))
1032 (defun eww-form-text (dom)
1033 (let ((start (point))
1034 (type (downcase (or (dom-attr dom 'type) "text")))
1035 (value (or (dom-attr dom 'value) ""))
1036 (width (string-to-number (or (dom-attr dom 'size) "40")))
1037 (readonly-property (if (or (dom-attr dom 'disabled)
1038 (dom-attr dom 'readonly))
1040 'inhibit-read-only)))
1042 (when (< (length value) width)
1043 (insert (make-string (- width (length value)) ? )))
1044 (put-text-property start (point) 'face 'eww-form-text)
1045 (put-text-property start (point) 'inhibit-read-only t)
1046 (put-text-property start (point) 'local-map eww-text-map)
1047 (put-text-property start (point) readonly-property t)
1048 (put-text-property start (point) 'eww-form
1049 (list :eww-form eww-form
1052 :name (dom-attr dom 'name)))
1055 (defconst eww-text-input-types '("text" "password" "textarea"
1056 "color" "date" "datetime" "datetime-local"
1057 "email" "month" "number" "search" "tel"
1058 "time" "url" "week")
1059 "List of input types which represent a text input.
1060 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1062 (defun eww-process-text-input (beg end replace-length)
1063 (when-let (pos (and (< (1+ end) (point-max))
1064 (> (1- end) (point-min))
1066 ((get-text-property (1+ end) 'eww-form)
1068 ((get-text-property (1- end) 'eww-form)
1070 (let* ((form (get-text-property pos 'eww-form))
1071 (properties (text-properties-at pos))
1072 (buffer-undo-list t)
1073 (inhibit-read-only t)
1074 (length (- end beg replace-length))
1075 (type (plist-get form :type)))
1077 (member type eww-text-input-types))
1080 ;; Delete some space at the end.
1083 (if (equal type "textarea")
1084 (1- (line-end-position))
1085 (eww-end-of-field)))
1086 (while (and (> length 0)
1087 (eql (char-after (1- (point))) ? ))
1088 (delete-region (1- (point)) (point))
1095 (if (equal type "textarea")
1096 (1- (line-end-position))
1097 (1+ (eww-end-of-field))))
1098 (let ((start (point)))
1099 (insert (make-string (abs length) ? ))
1100 (set-text-properties start (point) properties))
1101 (goto-char (1- end)))))
1102 (set-text-properties (plist-get form :start) (plist-get form :end)
1104 (let ((value (buffer-substring-no-properties
1105 (eww-beginning-of-field)
1106 (eww-end-of-field))))
1107 (when (string-match " +\\'" value)
1108 (setq value (substring value 0 (match-beginning 0))))
1109 (plist-put form :value value)
1110 (when (equal type "password")
1111 ;; Display passwords as asterisks.
1112 (let ((start (eww-beginning-of-field)))
1114 start (+ start (length value))
1115 'display (make-string (length value) ?*)))))))))
1117 (defun eww-tag-textarea (dom)
1118 (let ((start (point))
1119 (value (or (dom-attr dom 'value) ""))
1120 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1121 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1123 (shr-ensure-newline)
1125 (shr-ensure-newline)
1126 (when (< (count-lines start (point)) lines)
1127 (dotimes (_ (- lines (count-lines start (point))))
1129 (setq end (point-marker))
1131 (while (< (point) end)
1133 (let ((pad (- width (- (point) (line-beginning-position)))))
1135 (insert (make-string pad ? ))))
1136 (add-face-text-property (line-beginning-position)
1137 (point) 'eww-form-textarea)
1138 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1139 (put-text-property (line-beginning-position) (point)
1140 'local-map eww-textarea-map)
1142 (put-text-property start (point) 'eww-form
1143 (list :eww-form eww-form
1146 :name (dom-attr dom 'name)))))
1148 (defun eww-tag-input (dom)
1149 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1152 ((or (equal type "checkbox")
1153 (equal type "radio"))
1154 (eww-form-checkbox dom))
1155 ((equal type "file")
1156 (eww-form-file dom))
1157 ((equal type "submit")
1158 (eww-form-submit dom))
1159 ((equal type "hidden")
1160 (let ((form eww-form)
1161 (name (dom-attr dom 'name)))
1162 ;; Don't add <input type=hidden> elements repeatedly.
1164 (or (not (consp (car form)))
1165 (not (eq (caar form) 'hidden))
1166 (not (equal (plist-get (cdr (car form)) :name)
1168 (setq form (cdr form)))
1170 (nconc eww-form (list
1173 :value (or (dom-attr dom 'value) "")))))))
1175 (eww-form-text dom)))
1176 (unless (= start (point))
1177 (put-text-property start (1+ start) 'help-echo "Input field")
1178 ;; Mark this as an element we can TAB to.
1179 (put-text-property start (1+ start) 'shr-url dom))))
1181 (defun eww-tag-select (dom)
1182 (shr-ensure-paragraph)
1183 (let ((menu (list :name (dom-attr dom 'name)
1184 :eww-form eww-form))
1189 (if (eq (dom-tag dom) 'optgroup)
1190 (dolist (groupelem (dom-children dom))
1191 (unless (dom-attr groupelem 'disabled)
1192 (setq opelem (append opelem (list groupelem)))))
1193 (setq opelem (list dom)))
1194 (dolist (elem opelem)
1195 (when (eq (dom-tag elem) 'option)
1196 (when (dom-attr elem 'selected)
1197 (nconc menu (list :value (dom-attr elem 'value))))
1198 (let ((display (dom-text elem)))
1199 (setq max (max max (length display)))
1201 :value (dom-attr elem 'value)
1205 (setq options (nreverse options))
1206 ;; If we have no selected values, default to the first value.
1207 (unless (plist-get menu :value)
1208 (nconc menu (list :value (nth 2 (car options)))))
1209 (nconc menu options)
1210 (let ((selected (eww-select-display menu)))
1212 (make-string (- max (length selected)) ? )))
1213 (put-text-property start (point) 'eww-form menu)
1214 (add-face-text-property start (point) 'eww-form-select)
1215 (put-text-property start (point) 'keymap eww-select-map)
1216 (unless (= start (point))
1217 (put-text-property start (1+ start) 'help-echo "select field"))
1218 (shr-ensure-paragraph))))
1220 (defun eww-select-display (select)
1221 (let ((value (plist-get select :value))
1223 (dolist (elem select)
1224 (when (and (consp elem)
1225 (eq (car elem) 'item)
1226 (equal value (plist-get (cdr elem) :value)))
1227 (setq display (plist-get (cdr elem) :display))))
1230 (defun eww-change-select ()
1231 "Change the value of the select drop-down menu under point."
1233 (let* ((input (get-text-property (point) 'eww-form))
1234 (completion-ignore-case t)
1237 (mapcar (lambda (elem)
1239 (eq (car elem) 'item)
1240 (cons (plist-get (cdr elem) :display)
1241 (plist-get (cdr elem) :value))))
1244 (completing-read "Change value: " options nil 'require-match))
1245 (inhibit-read-only t))
1246 (plist-put input :value (cdr (assoc-string display options t)))
1248 (eww-update-field display))))
1250 (defun eww-update-field (string &optional offset)
1253 (let ((properties (text-properties-at (point)))
1254 (start (+ (eww-beginning-of-field) offset))
1255 (current-end (1+ (eww-end-of-field)))
1256 (new-end (+ (eww-beginning-of-field) (length string)))
1257 (inhibit-read-only t))
1258 (delete-region start current-end)
1259 (forward-char offset)
1261 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1263 (set-text-properties start new-end properties))
1266 (defun eww-toggle-checkbox ()
1267 "Toggle the value of the checkbox under point."
1269 (let* ((input (get-text-property (point) 'eww-form))
1270 (type (plist-get input :type)))
1271 (if (equal type "checkbox")
1274 (if (plist-get input :checked)
1276 (plist-put input :checked nil)
1277 (eww-update-field eww-form-checkbox-symbol))
1278 (plist-put input :checked t)
1279 (eww-update-field eww-form-checkbox-selected-symbol))))
1280 ;; Radio button. Switch all other buttons off.
1281 (let ((name (plist-get input :name)))
1283 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1284 (when (equal (plist-get (cdr elem) :name) name)
1285 (goto-char (car elem))
1286 (if (not (eq (cdr elem) input))
1288 (plist-put input :checked nil)
1289 (eww-update-field eww-form-checkbox-symbol))
1290 (plist-put input :checked t)
1291 (eww-update-field eww-form-checkbox-selected-symbol)))))
1292 (forward-char 1)))))
1294 (defun eww-inputs (form)
1295 (let ((start (point-min))
1298 (< start (point-max)))
1299 (when (or (get-text-property start 'eww-form)
1300 (setq start (next-single-property-change start 'eww-form)))
1301 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1303 (push (cons start (get-text-property start 'eww-form))
1305 (setq start (next-single-property-change start 'eww-form))))
1308 (defun eww-size-text-inputs ()
1309 (let ((start (point-min)))
1311 (< start (point-max)))
1312 (when (or (get-text-property start 'eww-form)
1313 (setq start (next-single-property-change start 'eww-form)))
1314 (let ((props (get-text-property start 'eww-form)))
1315 (plist-put props :start start)
1316 (setq start (next-single-property-change
1317 start 'eww-form nil (point-max)))
1318 (plist-put props :end start))))))
1320 (defun eww-input-value (input)
1321 (let ((type (plist-get input :type))
1322 (value (plist-get input :value)))
1324 ((equal type "textarea")
1327 (goto-char (point-min))
1328 (while (re-search-forward "^ +\n\\| +$" nil t)
1329 (replace-match "" t t))
1332 (if (string-match " +\\'" value)
1333 (substring value 0 (match-beginning 0))
1336 (defun eww-submit ()
1337 "Submit the current form."
1339 (let* ((this-input (get-text-property (point) 'eww-form))
1340 (form (plist-get this-input :eww-form))
1342 (dolist (elem (sort (eww-inputs form)
1344 (< (car o1) (car o2)))))
1345 (let* ((input (cdr elem))
1346 (input-start (car elem))
1347 (name (plist-get input :name)))
1350 ((member (plist-get input :type) '("checkbox" "radio"))
1351 (when (plist-get input :checked)
1352 (push (cons name (plist-get input :value))
1354 ((equal (plist-get input :type) "file")
1356 (list (cons "filedata"
1358 (insert-file-contents
1359 (plist-get input :filename))
1361 (cons "name" (plist-get input :name))
1362 (cons "filename" (plist-get input :filename))))
1364 ((equal (plist-get input :type) "submit")
1365 ;; We want the values from buttons if we hit a button if
1366 ;; we hit enter on it, or if it's the first button after
1367 ;; the field we did hit return on.
1368 (when (or (eq input this-input)
1369 (and (not (eq input this-input))
1371 (> input-start (point))))
1372 (setq next-submit t)
1373 (push (cons name (plist-get input :value))
1376 (push (cons name (eww-input-value input))
1379 (when (and (consp elem)
1380 (eq (car elem) 'hidden))
1381 (push (cons (plist-get (cdr elem) :name)
1382 (or (plist-get (cdr elem) :value) ""))
1384 (if (and (stringp (cdr (assq :method form)))
1385 (equal (downcase (cdr (assq :method form))) "post"))
1387 (dolist (x values mtype)
1388 (if (equal (car x) "file")
1390 (setq mtype "multipart/form-data"))))
1391 (cond ((equal mtype "multipart/form-data")
1392 (let ((boundary (mml-compute-boundary '())))
1393 (let ((url-request-method "POST")
1394 (url-request-extra-headers
1395 (list (cons "Content-Type"
1396 (concat "multipart/form-data; boundary="
1399 (mm-url-encode-multipart-form-data values boundary)))
1400 (eww-browse-url (shr-expand-url
1401 (cdr (assq :action form))
1402 (plist-get eww-data :url))))))
1404 (let ((url-request-method "POST")
1405 (url-request-extra-headers
1407 "application/x-www-form-urlencoded")))
1409 (mm-url-encode-www-form-urlencoded values)))
1410 (eww-browse-url (shr-expand-url
1411 (cdr (assq :action form))
1412 (plist-get eww-data :url)))))))
1415 (if (cdr (assq :action form))
1416 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1417 (plist-get eww-data :url))
1419 (mm-url-encode-www-form-urlencoded values))))))
1421 (defun eww-browse-with-external-browser (&optional url)
1422 "Browse the current URL with an external browser.
1423 The browser to used is specified by the `shr-external-browser' variable."
1425 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1427 (defun eww-follow-link (&optional external mouse-event)
1428 "Browse the URL under point.
1429 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1430 If EXTERNAL is double prefix, browse in new buffer."
1431 (interactive (list current-prefix-arg last-nonmenu-event))
1432 (mouse-set-point mouse-event)
1433 (let ((url (get-text-property (point) 'shr-url)))
1436 (message "No link under point"))
1437 ((string-match "^mailto:" url)
1438 (browse-url-mail url))
1439 ((and (consp external) (<= (car external) 4))
1440 (funcall shr-external-browser url))
1441 ;; This is a #target url in the same page as the current one.
1442 ((and (url-target (url-generic-parse-url url))
1443 (eww-same-page-p url (plist-get eww-data :url)))
1444 (let ((dom (plist-get eww-data :dom)))
1446 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1448 (eww-browse-url url external)))))
1450 (defun eww-same-page-p (url1 url2)
1451 "Return non-nil if URL1 and URL2 represent the same page.
1452 Differences in #targets are ignored."
1453 (let ((obj1 (url-generic-parse-url url1))
1454 (obj2 (url-generic-parse-url url2)))
1455 (setf (url-target obj1) nil)
1456 (setf (url-target obj2) nil)
1457 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1459 (defun eww-copy-page-url ()
1460 "Copy the URL of the current page into the kill ring."
1462 (message "%s" (plist-get eww-data :url))
1463 (kill-new (plist-get eww-data :url)))
1465 (defun eww-download ()
1466 "Download URL under point to `eww-download-directory'."
1468 (let ((url (get-text-property (point) 'shr-url)))
1470 (message "No URL under point")
1471 (url-retrieve url 'eww-download-callback (list url)))))
1473 (defun eww-download-callback (status url)
1474 (unless (plist-get status :error)
1475 (let* ((obj (url-generic-parse-url url))
1476 (path (car (url-path-and-query obj)))
1477 (file (eww-make-unique-file-name
1478 (eww-decode-url-file-name (file-name-nondirectory path))
1479 eww-download-directory)))
1480 (goto-char (point-min))
1481 (re-search-forward "\r?\n\r?\n")
1482 (write-region (point) (point-max) file)
1483 (message "Saved %s" file))))
1485 (defun eww-decode-url-file-name (string)
1486 (let* ((binary (url-unhex-string string))
1488 (decode-coding-string
1490 ;; Possibly set by `universal-coding-system-argument'.
1491 (or coding-system-for-read
1492 ;; RFC 3986 says that %AB stuff is utf-8.
1493 (if (equal (decode-coding-string binary 'utf-8)
1497 (car (detect-coding-string binary))))))
1498 (encodes (find-coding-systems-string decoded)))
1499 (if (or (equal encodes '(undecided))
1500 (memq (coding-system-base (or file-name-coding-system
1501 default-file-name-coding-system))
1504 ;; If we can't encode the decoded file name (due to language
1505 ;; environment settings), then we return the original, hexified
1509 (defun eww-make-unique-file-name (file directory)
1511 ((zerop (length file))
1513 ((string-match "\\`[.]" file)
1514 (setq file (concat "!" file))))
1518 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1519 (setq stem (match-string 1 file)
1520 suffix (match-string 2)))
1521 (while (file-exists-p (expand-file-name file directory))
1522 (setq file (format "%s(%d)%s" stem count suffix))
1523 (setq count (1+ count)))
1524 (expand-file-name file directory)))
1526 (defun eww-set-character-encoding (charset)
1527 "Set character encoding to CHARSET.
1528 If CHARSET is nil then use UTF-8."
1529 (interactive "zUse character set (default utf-8): ")
1531 (eww-reload nil 'utf-8)
1532 (eww-reload nil charset)))
1534 (defun eww-switch-to-buffer ()
1535 "Prompt for an EWW buffer to display in the selected window."
1537 (let ((completion-extra-properties
1538 '(:annotation-function (lambda (buf)
1539 (with-current-buffer buf
1540 (format " %s" (eww-current-url)))))))
1541 (pop-to-buffer-same-window
1542 (read-buffer "Switch to EWW buffer: "
1543 (cl-loop for buf in (nreverse (buffer-list))
1544 if (with-current-buffer buf (derived-mode-p 'eww-mode))
1548 (with-current-buffer
1549 (if (consp bufn) (cdr bufn) (get-buffer bufn))
1550 (derived-mode-p 'eww-mode)))))))
1552 (defun eww-toggle-fonts ()
1553 "Toggle whether to use monospaced or font-enabled layouts."
1555 (setq shr-use-fonts (not shr-use-fonts))
1557 (message "Proportional fonts are now %s"
1558 (if shr-use-fonts "on" "off")))
1560 (defun eww-toggle-colors ()
1561 "Toggle whether to use HTML-specified colors or not."
1563 (message "Colors are now %s"
1564 (if (setq shr-use-colors (not shr-use-colors))
1571 (defvar eww-bookmarks nil)
1573 (defun eww-add-bookmark ()
1574 "Bookmark the current page."
1576 (eww-read-bookmarks)
1577 (dolist (bookmark eww-bookmarks)
1578 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1579 (user-error "Already bookmarked")))
1580 (when (y-or-n-p "Bookmark this page?")
1581 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1582 (plist-get eww-data :title))))
1583 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1584 (push (list :url (plist-get eww-data :url)
1586 :time (current-time-string))
1588 (eww-write-bookmarks)
1589 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1590 (plist-get eww-data :title))))
1592 (defun eww-write-bookmarks ()
1593 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1594 (insert ";; Auto-generated file; don't edit\n")
1595 (pp eww-bookmarks (current-buffer))))
1597 (defun eww-read-bookmarks ()
1598 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1600 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1602 (insert-file-contents file)
1603 (read (current-buffer)))))))
1606 (defun eww-list-bookmarks ()
1607 "Display the bookmarks."
1609 (pop-to-buffer "*eww bookmarks*")
1610 (eww-bookmark-prepare))
1612 (defun eww-bookmark-prepare ()
1613 (eww-read-bookmarks)
1614 (unless eww-bookmarks
1615 (user-error "No bookmarks are defined"))
1616 (set-buffer (get-buffer-create "*eww bookmarks*"))
1618 (let* ((width (/ (window-width) 2))
1619 (format (format "%%-%ds %%s" width))
1620 (inhibit-read-only t)
1623 (setq header-line-format (concat " " (format format "Title" "URL")))
1624 (dolist (bookmark eww-bookmarks)
1626 title (plist-get bookmark :title))
1627 (when (> (length title) width)
1628 (setq title (truncate-string-to-width title width)))
1629 (insert (format format title (plist-get bookmark :url)) "\n")
1630 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1631 (goto-char (point-min))))
1633 (defvar eww-bookmark-kill-ring nil)
1635 (defun eww-bookmark-kill ()
1636 "Kill the current bookmark."
1638 (let* ((start (line-beginning-position))
1639 (bookmark (get-text-property start 'eww-bookmark))
1640 (inhibit-read-only t))
1642 (user-error "No bookmark on the current line"))
1644 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1645 (delete-region start (point))
1646 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1647 (eww-write-bookmarks)))
1649 (defun eww-bookmark-yank ()
1650 "Yank a previously killed bookmark to the current line."
1652 (unless eww-bookmark-kill-ring
1653 (user-error "No previously killed bookmark"))
1655 (let ((inhibit-read-only t)
1658 (insert (pop eww-bookmark-kill-ring))
1659 (setq bookmark (get-text-property start 'eww-bookmark))
1660 (if (= start (point-min))
1661 (push bookmark eww-bookmarks)
1662 (let ((line (count-lines start (point))))
1663 (setcdr (nthcdr (1- line) eww-bookmarks)
1664 (cons bookmark (nthcdr line eww-bookmarks)))))
1665 (eww-write-bookmarks)))
1667 (defun eww-bookmark-browse ()
1668 "Browse the bookmark under point in eww."
1670 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1672 (user-error "No bookmark on the current line"))
1674 (eww-browse-url (plist-get bookmark :url))))
1676 (defun eww-next-bookmark ()
1677 "Go to the next bookmark in the list."
1681 (unless (get-buffer "*eww bookmarks*")
1683 (eww-bookmark-prepare))
1684 (with-current-buffer (get-buffer "*eww bookmarks*")
1685 (when (and (not first)
1688 (setq bookmark (get-text-property (line-beginning-position)
1691 (user-error "No next bookmark")))
1692 (eww-browse-url (plist-get bookmark :url))))
1694 (defun eww-previous-bookmark ()
1695 "Go to the previous bookmark in the list."
1699 (unless (get-buffer "*eww bookmarks*")
1701 (eww-bookmark-prepare))
1702 (with-current-buffer (get-buffer "*eww bookmarks*")
1704 (goto-char (point-max))
1705 (beginning-of-line))
1706 ;; On the final line.
1710 (user-error "No previous bookmark")
1712 (setq bookmark (get-text-property (line-beginning-position)
1714 (eww-browse-url (plist-get bookmark :url))))
1716 (defvar eww-bookmark-mode-map
1717 (let ((map (make-sparse-keymap)))
1718 (define-key map [(control k)] 'eww-bookmark-kill)
1719 (define-key map [(control y)] 'eww-bookmark-yank)
1720 (define-key map "\r" 'eww-bookmark-browse)
1722 (easy-menu-define nil map
1723 "Menu for `eww-bookmark-mode-map'."
1725 ["Exit" quit-window t]
1726 ["Browse" eww-bookmark-browse
1727 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1728 ["Kill" eww-bookmark-kill
1729 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1730 ["Yank" eww-bookmark-yank
1731 :active eww-bookmark-kill-ring]))
1734 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1735 "Mode for listing bookmarks.
1737 \\{eww-bookmark-mode-map}"
1738 (buffer-disable-undo)
1739 (setq truncate-lines t))
1743 (defun eww-save-history ()
1744 (plist-put eww-data :point (point))
1745 (plist-put eww-data :text (buffer-string))
1746 (push eww-data eww-history)
1747 (setq eww-data (list :title ""))
1748 ;; Don't let the history grow infinitely. We store quite a lot of
1750 (when-let (tail (and eww-history-limit
1751 (nthcdr eww-history-limit eww-history)))
1754 (defvar eww-current-buffer)
1756 (defun eww-list-histories ()
1757 "List the eww-histories."
1759 (when (null eww-history)
1760 (error "No eww-histories are defined"))
1761 (let ((eww-history-trans eww-history)
1762 (buffer (current-buffer)))
1763 (set-buffer (get-buffer-create "*eww history*"))
1765 (setq-local eww-current-buffer buffer)
1766 (let ((inhibit-read-only t)
1769 url title format start)
1771 (dolist (history eww-history-trans)
1772 (setq start (point))
1773 (setq domain-length (max domain-length (length (plist-get history :url))))
1774 (setq title-length (max title-length (length (plist-get history :title)))))
1775 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1777 (concat " " (format format "Title" "URL")))
1778 (dolist (history eww-history-trans)
1779 (setq start (point))
1780 (setq url (plist-get history :url))
1781 (setq title (plist-get history :title))
1782 (insert (format format title url))
1784 (put-text-property start (1+ start) 'eww-history history))
1785 (goto-char (point-min)))
1786 (pop-to-buffer "*eww history*")))
1788 (defun eww-history-browse ()
1789 "Browse the history under point in eww."
1791 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1793 (error "No history on the current line"))
1794 (let ((buffer eww-current-buffer))
1797 (pop-to-buffer-same-window buffer)))
1798 (eww-restore-history history)))
1800 (defvar eww-history-mode-map
1801 (let ((map (make-sparse-keymap)))
1802 (define-key map "\r" 'eww-history-browse)
1803 ;; (define-key map "n" 'next-error-no-select)
1804 ;; (define-key map "p" 'previous-error-no-select)
1806 (easy-menu-define nil map
1807 "Menu for `eww-history-mode-map'."
1809 ["Exit" quit-window t]
1810 ["Browse" eww-history-browse
1811 :active (get-text-property (line-beginning-position) 'eww-history)]))
1814 (define-derived-mode eww-history-mode special-mode "eww history"
1815 "Mode for listing eww-histories.
1817 \\{eww-history-mode-map}"
1818 (buffer-disable-undo)
1819 (setq truncate-lines t))
1821 ;;; eww buffers list
1823 (defun eww-list-buffers ()
1824 "Enlist eww buffers."
1827 (current (current-buffer)))
1828 (dolist (buffer (buffer-list))
1829 (with-current-buffer buffer
1830 (when (derived-mode-p 'eww-mode)
1831 (push (vector buffer (plist-get eww-data :title)
1832 (plist-get eww-data :url))
1834 (unless buffers-info
1835 (error "No eww buffers"))
1836 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1837 (set-buffer (get-buffer-create "*eww buffers*"))
1839 (let ((inhibit-read-only t)
1842 url title format start)
1844 (dolist (buffer-info buffers-info)
1845 (setq title-length (max title-length
1846 (length (elt buffer-info 1)))
1847 domain-length (max domain-length
1848 (length (elt buffer-info 2)))))
1849 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1851 (concat " " (format format "Title" "URL")))
1853 (current-buffer-line 1))
1854 (dolist (buffer-info buffers-info)
1856 title (elt buffer-info 1)
1857 url (elt buffer-info 2)
1859 (insert (format format title url))
1861 (let ((buffer (elt buffer-info 0)))
1862 (put-text-property start (1+ start) 'eww-buffer
1864 (when (eq current buffer)
1865 (setq current-buffer-line line))))
1866 (goto-char (point-min))
1867 (forward-line (1- current-buffer-line)))))
1868 (pop-to-buffer "*eww buffers*"))
1870 (defun eww-buffer-select ()
1871 "Switch to eww buffer."
1873 (let ((buffer (get-text-property (line-beginning-position)
1876 (error "No buffer on current line"))
1878 (pop-to-buffer-same-window buffer)))
1880 (defun eww-buffer-show ()
1881 "Display buffer under point in eww buffer list."
1882 (let ((buffer (get-text-property (line-beginning-position)
1885 (error "No buffer on current line"))
1887 (pop-to-buffer-same-window buffer)
1890 (defun eww-buffer-show-next ()
1891 "Move to next eww buffer in the list and display it."
1895 (goto-char (point-min)))
1898 (defun eww-buffer-show-previous ()
1899 "Move to previous eww buffer in the list and display it."
1903 (goto-char (point-max)))
1907 (defun eww-buffer-kill ()
1908 "Kill buffer from eww list."
1910 (let* ((start (line-beginning-position))
1911 (buffer (get-text-property start 'eww-buffer))
1912 (inhibit-read-only t))
1914 (user-error "No buffer on the current line"))
1915 (kill-buffer buffer)
1917 (delete-region start (point)))
1922 (defvar eww-buffers-mode-map
1923 (let ((map (make-sparse-keymap)))
1924 (define-key map [(control k)] 'eww-buffer-kill)
1925 (define-key map "\r" 'eww-buffer-select)
1926 (define-key map "n" 'eww-buffer-show-next)
1927 (define-key map "p" 'eww-buffer-show-previous)
1929 (easy-menu-define nil map
1930 "Menu for `eww-buffers-mode-map'."
1932 ["Exit" quit-window t]
1933 ["Select" eww-buffer-select
1934 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1935 ["Kill" eww-buffer-kill
1936 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1939 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1940 "Mode for listing buffers.
1942 \\{eww-buffers-mode-map}"
1943 (buffer-disable-undo)
1944 (setq truncate-lines t))
1948 (defvar eww-desktop-data-save
1949 '(:url :title :point)
1950 "List of `eww-data' properties to preserve in the desktop file.
1951 Also used when saving `eww-history'.")
1953 (defun eww-desktop-data-1 (alist)
1957 (let ((k (car tail))
1959 (when (memq k eww-desktop-data-save)
1960 (setq acc (cons k (cons v acc)))))
1961 (setq tail (cddr tail)))
1964 (defun eww-desktop-history-duplicate (a b)
1965 (let ((tail a) (r t))
1967 (if (or (memq (car tail) '(:point)) ; ignore :point
1969 (plist-get b (car tail))))
1970 (setq tail (cddr tail))
1976 (defun eww-desktop-misc-data (_directory)
1977 "Return a property list with data used to restore eww buffers.
1978 This list will contain, as :history, the list, whose first element is
1979 the value of `eww-data', and the tail is `eww-history'.
1981 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1982 entries (if any) will be removed from the list.
1984 Only the properties listed in `eww-desktop-data-save' are included.
1985 Generally, the list should not include the (usually overly large)
1986 :dom, :source and :text properties."
1987 (let ((history (mapcar 'eww-desktop-data-1
1988 (cons eww-data eww-history))))
1989 (list :history (if eww-desktop-remove-duplicates
1990 (cl-remove-duplicates
1991 history :test 'eww-desktop-history-duplicate)
1994 (defun eww-restore-desktop (file-name buffer-name misc-data)
1995 "Restore an eww buffer from its desktop file record.
1996 If `eww-restore-desktop' is t or `auto', this function will also
1997 initiate the retrieval of the respective URI in the background.
1998 Otherwise, the restored buffer will contain a prompt to do so by using
2000 (with-current-buffer (get-buffer-create buffer-name)
2002 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
2003 (setq eww-history (cdr (plist-get misc-data :history))
2004 eww-data (or (car (plist-get misc-data :history))
2005 ;; backwards compatibility
2006 (list :url (plist-get misc-data :uri))))
2008 (when (plist-get eww-data :url)
2009 (case eww-restore-desktop
2010 ((t auto) (eww (plist-get eww-data :url)))
2011 ((zerop (buffer-size))
2012 (let ((inhibit-read-only t))
2013 (insert (substitute-command-keys
2014 eww-restore-reload-prompt)))))))
2018 (add-to-list 'desktop-locals-to-save
2019 'eww-history-position)
2020 (add-to-list 'desktop-buffer-mode-handlers
2021 '(eww-mode . eww-restore-desktop))
2025 (defun eww-isearch-next-buffer (&optional _buffer wrap)
2026 "Go to the next page to search using `rel' attribute for navigation."
2033 (eww-previous-url)))
2038 ;;; eww.el ends here