1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2017 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 <https://www.gnu.org/licenses/>.
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
(eww--dwim-expand-url url
))
255 (pop-to-buffer-same-window
256 (if (eq major-mode
'eww-mode
)
258 (get-buffer-create "*eww*")))
260 ;; Check whether the domain only uses "Highly Restricted" Unicode
261 ;; IDNA characters. If not, transform to punycode to indicate that
262 ;; there may be funny business going on.
263 (let ((parsed (url-generic-parse-url url
)))
264 (unless (puny-highly-restrictive-domain-p (url-host parsed
))
265 (setf (url-host parsed
) (puny-encode-domain (url-host parsed
)))
266 (setq url
(url-recreate-url parsed
))))
267 (plist-put eww-data
:url url
)
268 (plist-put eww-data
:title
"")
269 (eww-update-header-line-format)
270 (let ((inhibit-read-only t
))
271 (insert (format "Loading %s..." url
))
272 (goto-char (point-min)))
273 (url-retrieve url
'eww-render
274 (list url nil
(current-buffer))))
276 (defun eww--dwim-expand-url (url)
277 (setq url
(string-trim url
))
278 (cond ((string-match-p "\\`file:/" url
))
279 ;; Don't mangle file: URLs at all.
280 ((string-match-p "\\`ftp://" url
)
281 (user-error "FTP is not supported"))
283 ;; Anything that starts with something that vaguely looks
284 ;; like a protocol designator is interpreted as a full URL.
285 (if (or (string-match "\\`[A-Za-z]+:" url
)
286 ;; Also try to match "naked" URLs like
287 ;; en.wikipedia.org/wiki/Free software
288 (string-match "\\`[A-Za-z_]+\\.[A-Za-z._]+/" url
)
289 (and (= (length (split-string url
)) 1)
290 (or (and (not (string-match-p "\\`[\"'].*[\"']\\'" url
))
291 (> (length (split-string url
"[.:]")) 1))
292 (string-match eww-local-regex url
))))
294 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
295 (setq url
(concat "http://" url
)))
296 ;; Some sites do not redirect final /
297 (when (string= (url-filename (url-generic-parse-url url
)) "")
298 (setq url
(concat url
"/"))))
299 (setq url
(concat eww-search-prefix
301 #'url-hexify-string
(split-string url
) "+"))))))
304 ;;;###autoload (defalias 'browse-web 'eww)
307 (defun eww-open-file (file)
308 "Render FILE using EWW."
309 (interactive "fFile: ")
310 (eww (concat "file://"
311 (and (memq system-type
'(windows-nt ms-dos
))
313 (expand-file-name file
))))
316 (defun eww-search-words ()
317 "Search the web for the text between BEG and END.
318 If region is active (and not whitespace), search the web for
319 the text between BEG and END. Else, prompt the user for a search
320 string. See the `eww-search-prefix' variable for the search
324 (let ((region-string (buffer-substring (region-beginning) (region-end))))
325 (if (not (string-match-p "\\`[ \n\t\r\v\f]*\\'" region-string
))
327 (call-interactively 'eww
)))
328 (call-interactively 'eww
)))
330 (defun eww-open-in-new-buffer ()
331 "Fetch link at point in a new EWW buffer."
333 (let ((url (eww-suggested-uris)))
334 (if (null url
) (user-error "No link at point")
335 ;; clone useful to keep history, but
336 ;; should not clone from non-eww buffer
338 (if (eq major-mode
'eww-mode
) (clone-buffer)
339 (generate-new-buffer "*eww*"))
340 (unless (equal url
(eww-current-url))
342 (eww (if (consp url
) (car url
) url
)))))))
344 (defun eww-html-p (content-type)
345 "Return non-nil if CONTENT-TYPE designates an HTML content type.
346 Currently this means either text/html or application/xhtml+xml."
347 (member content-type
'("text/html"
348 "application/xhtml+xml")))
350 (defun eww-render (status url
&optional point buffer encode
)
351 (let ((redirect (plist-get status
:redirect
)))
353 (setq url redirect
)))
354 (let* ((headers (eww-parse-headers))
356 (mail-header-parse-content-type
357 (if (zerop (length (cdr (assoc "content-type" headers
))))
359 (cdr (assoc "content-type" headers
)))))
362 (or (cdr (assq 'charset
(cdr content-type
)))
363 (eww-detect-charset (eww-html-p (car content-type
)))
365 (data-buffer (current-buffer))
366 last-coding-system-used
)
367 (with-current-buffer buffer
368 ;; Save the https peer status.
369 (plist-put eww-data
:peer
(plist-get status
:peer
))
370 ;; Make buffer listings more informative.
371 (setq list-buffers-directory url
))
375 ((and eww-use-external-browser-for-content-type
376 (string-match-p eww-use-external-browser-for-content-type
379 (insert "<title>Unsupported content type</title>")
380 (insert (format "<h1>Content-type %s is unsupported</h1>"
382 (insert (format "<a href=%S>Direct link to the document</a>"
384 (goto-char (point-min))
385 (eww-display-html charset url nil point buffer encode
))
386 ((eww-html-p (car content-type
))
387 (eww-display-html charset url nil point buffer encode
))
388 ((equal (car content-type
) "application/pdf")
390 ((string-match-p "\\`image/" (car content-type
))
391 (eww-display-image buffer
))
393 (eww-display-raw buffer
(or encode charset
'utf-8
))))
394 (with-current-buffer buffer
395 (plist-put eww-data
:url url
)
396 (eww-update-header-line-format)
397 (setq eww-history-position
0)
398 (and last-coding-system-used
399 (set-buffer-file-coding-system last-coding-system-used
))
400 (run-hooks 'eww-after-render-hook
)))
401 (kill-buffer data-buffer
))))
403 (defun eww-parse-headers ()
405 (goto-char (point-min))
406 (while (and (not (eobp))
408 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
409 (push (cons (downcase (match-string 1))
417 (defun eww-detect-charset (html-p)
418 (let ((case-fold-search t
)
422 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
426 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
429 (declare-function libxml-parse-html-region
"xml.c"
430 (start end
&optional base-url discard-comments
))
432 (defun eww-display-html (charset url
&optional document point buffer encode
)
433 (unless (fboundp 'libxml-parse-html-region
)
434 (error "This function requires Emacs to be compiled with libxml2"))
435 (unless (buffer-live-p buffer
)
436 (error "Buffer %s doesn't exist" buffer
))
437 ;; There should be a better way to abort loading images
443 'base
(list (cons 'href url
))
445 (setq encode
(or encode charset
'utf-8
))
447 (decode-coding-region (point) (point-max) encode
)
448 (coding-system-error nil
))
450 ;; Remove CRLF before parsing.
451 (while (re-search-forward "\r$" nil t
)
452 (replace-match "" t t
)))
453 (libxml-parse-html-region (point) (point-max))))))
454 (source (and (null document
)
455 (buffer-substring (point) (point-max)))))
456 (with-current-buffer buffer
457 (setq bidi-paragraph-direction nil
)
458 (plist-put eww-data
:source source
)
459 (plist-put eww-data
:dom document
)
460 (let ((inhibit-read-only t
)
461 (inhibit-modification-hooks t
)
462 (shr-target-id (url-target (url-generic-parse-url url
)))
463 (shr-external-rendering-functions
465 shr-external-rendering-functions
466 '((title . eww-tag-title
)
467 (form . eww-tag-form
)
468 (input . eww-tag-input
)
469 (button . eww-form-submit
)
470 (textarea . eww-tag-textarea
)
471 (select . eww-tag-select
)
472 (link . eww-tag-link
)
473 (meta . eww-tag-meta
)
476 (shr-insert-document document
)
481 (goto-char (point-min))
482 (let ((point (next-single-property-change
483 (point-min) 'shr-target-id
)))
487 (goto-char (point-min))
488 ;; Don't leave point inside forms, because the normal eww
489 ;; commands aren't available there.
490 (while (and (not (eobp))
491 (get-text-property (point) 'eww-form
))
493 (eww-size-text-inputs))))
495 (defun eww-handle-link (dom)
496 (let* ((rel (dom-attr dom
'rel
))
497 (href (dom-attr dom
'href
))
499 ;; The text associated with :rel is case-insensitive.
500 (if rel
(downcase rel
))
502 ;; Texinfo uses "previous", but HTML specifies
503 ;; "prev", so recognize both.
504 ("previous" .
:previous
)
506 ;; HTML specifies "start" but also "contents",
507 ;; and Gtk seems to use "home". Recognize
508 ;; them all; but store them in different
509 ;; variables so that we can readily choose the
513 ("contents" .
:contents
)
517 (plist-put eww-data
(cdr where
) href
))))
519 (defvar eww-redirect-level
1)
521 (defun eww-tag-meta (dom)
522 (when (and (cl-equalp (dom-attr dom
'http-equiv
) "refresh")
523 (< eww-redirect-level
5))
524 (when-let* ((refresh (dom-attr dom
'content
)))
525 (when (or (string-match "^\\([0-9]+\\) *;.*url=\"\\([^\"]+\\)\"" refresh
)
526 (string-match "^\\([0-9]+\\) *;.*url='\\([^']+\\)'" refresh
)
527 (string-match "^\\([0-9]+\\) *;.*url=\\([^ ]+\\)" refresh
))
528 (let ((timeout (match-string 1 refresh
))
529 (url (match-string 2 refresh
))
530 (eww-redirect-level (1+ eww-redirect-level
)))
531 (if (equal timeout
"0")
532 (eww (shr-expand-url url
))
534 (dom-node 'a
`((href .
,(shr-expand-url url
)))
535 (format "Auto refresh in %s second%s disabled"
537 (if (equal timeout
"1")
541 (defun eww-tag-link (dom)
542 (eww-handle-link dom
)
545 (defun eww-tag-a (dom)
546 (eww-handle-link dom
)
547 (let ((start (point)))
549 (put-text-property start
(point) 'keymap eww-link-keymap
)))
551 (defun eww-update-header-line-format ()
552 (setq header-line-format
553 (and eww-header-line-format
554 (let ((title (plist-get eww-data
:title
))
555 (peer (plist-get eww-data
:peer
)))
556 (when (zerop (length title
))
557 (setq title
"[untitled]"))
558 ;; This connection has is https.
561 (propertize title
'face
562 (if (plist-get peer
:warnings
)
563 'eww-invalid-certificate
564 'eww-valid-certificate
))))
565 (replace-regexp-in-string
568 eww-header-line-format
569 `((?u .
,(or (plist-get eww-data
:url
) ""))
572 (defun eww-tag-title (dom)
573 (plist-put eww-data
:title
574 (replace-regexp-in-string
576 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom
))))
577 (eww-update-header-line-format))
579 (defun eww-display-raw (buffer &optional encode
)
580 (let ((data (buffer-substring (point) (point-max))))
581 (unless (buffer-live-p buffer
)
582 (error "Buffer %s doesn't exist" buffer
))
583 (with-current-buffer buffer
584 (let ((inhibit-read-only t
))
588 (decode-coding-region (point-min) (1+ (length data
)) encode
)
589 (coding-system-error nil
)))
590 (goto-char (point-min)))))
592 (defun eww-display-image (buffer)
593 (let ((data (shr-parse-image-data)))
594 (unless (buffer-live-p buffer
)
595 (error "Buffer %s doesn't exist" buffer
))
596 (with-current-buffer buffer
597 (let ((inhibit-read-only t
))
599 (shr-put-image data nil
))
600 (goto-char (point-min)))))
602 (declare-function mailcap-view-mime
"mailcap" (type))
603 (defun eww-display-pdf ()
604 (let ((data (buffer-substring (point) (point-max))))
605 (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
606 (let ((coding-system-for-write 'raw-text
)
607 (inhibit-read-only t
))
610 (mailcap-view-mime "application/pdf")))
611 (goto-char (point-min)))
613 (defun eww-setup-buffer ()
614 (when (or (plist-get eww-data
:url
)
615 (plist-get eww-data
:dom
))
617 (let ((inhibit-read-only t
))
620 (setq bidi-paragraph-direction nil
)
621 (unless (eq major-mode
'eww-mode
)
624 (defun eww-current-url nil
625 "Return URI of the Web page the current EWW buffer is visiting."
626 (plist-get eww-data
:url
))
628 (defun eww-links-at-point ()
629 "Return list of URIs, if any, linked at point."
631 (list (get-text-property (point) 'shr-url
)
632 (get-text-property (point) 'image-url
))))
634 (defun eww-view-source ()
635 "View the HTML source code of the current page."
637 (let ((buf (get-buffer-create "*eww-source*"))
638 (source (plist-get eww-data
:source
)))
639 (with-current-buffer buf
640 (let ((inhibit-read-only t
))
641 (delete-region (point-min) (point-max))
642 (insert (or source
"no source"))
643 (goto-char (point-min))
644 ;; Decode the source and set the buffer's encoding according
645 ;; to what the HTML source specifies in its 'charset' header,
647 (let ((cs (find-auto-coding "" (point-max))))
650 (when (coding-system-p cs
)
651 (decode-coding-region (point-min) (point-max) cs
)
652 (setq buffer-file-coding-system last-coding-system-used
))))
654 ((fboundp 'mhtml-mode
)
656 ((fboundp 'html-mode
)
660 (defun eww-toggle-paragraph-direction ()
661 "Cycle the paragraph direction between left-to-right, right-to-left and auto."
663 (setq bidi-paragraph-direction
664 (cond ((eq bidi-paragraph-direction
'left-to-right
)
666 ((eq bidi-paragraph-direction
'right-to-left
)
670 (message "The paragraph direction is now %s"
671 (if (null bidi-paragraph-direction
)
673 bidi-paragraph-direction
)))
675 (defun eww-readable ()
676 "View the main \"readable\" parts of the current web page.
677 This command uses heuristics to find the parts of the web page that
678 contains the main textual portion, leaving out navigation menus and
681 (let* ((old-data eww-data
)
682 (dom (with-temp-buffer
683 (insert (plist-get old-data
:source
))
685 (decode-coding-region (point-min) (point-max) 'utf-8
)
686 (coding-system-error nil
))
687 (libxml-parse-html-region (point-min) (point-max))))
688 (base (plist-get eww-data
:url
)))
689 (eww-score-readability dom
)
691 (eww-display-html nil nil
692 (list 'base
(list (cons 'href base
))
693 (eww-highest-readability dom
))
694 nil
(current-buffer))
695 (dolist (elem '(:source
:url
:title
:next
:previous
:up
))
696 (plist-put eww-data elem
(plist-get old-data elem
)))
697 (eww-update-header-line-format)))
699 (defun eww-score-readability (node)
702 ((memq (dom-tag node
) '(script head comment
))
704 ((eq (dom-tag node
) 'meta
)
706 ((eq (dom-tag node
) 'img
)
708 ((eq (dom-tag node
) 'a
)
709 (setq score
(- (length (split-string (dom-text node
))))))
711 (dolist (elem (dom-children node
))
714 (setq score
(+ score
(length (split-string elem
)))))
717 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
718 (eww-score-readability elem
)))))))))
719 ;; Cache the score of the node to avoid recomputing all the time.
720 (dom-set-attribute node
:eww-readability-score score
)
723 (defun eww-highest-readability (node)
726 (dolist (elem (dom-non-text-children node
))
727 (when (> (or (dom-attr
728 (setq highest
(eww-highest-readability elem
))
729 :eww-readability-score
)
730 most-negative-fixnum
)
731 (or (dom-attr result
:eww-readability-score
)
732 most-negative-fixnum
))
733 (setq result highest
)))
737 (let ((map (make-sparse-keymap)))
738 (define-key map
"g" 'eww-reload
) ;FIXME: revert-buffer-function instead!
739 (define-key map
"G" 'eww
)
740 (define-key map
[?\M-
\r] 'eww-open-in-new-buffer
)
741 (define-key map
[?
\t] 'shr-next-link
)
742 (define-key map
[?\M-
\t] 'shr-previous-link
)
743 (define-key map
[backtab] 'shr-previous-link)
744 (define-key map [delete] 'scroll-down-command)
745 (define-key map "l" 'eww-back-url)
746 (define-key map "r" 'eww-forward-url)
747 (define-key map "n" 'eww-next-url)
748 (define-key map "p" 'eww-previous-url)
749 (define-key map "u" 'eww-up-url)
750 (define-key map "t" 'eww-top-url)
751 (define-key map "&" 'eww-browse-with-external-browser)
752 (define-key map "d" 'eww-download)
753 (define-key map "w" 'eww-copy-page-url)
754 (define-key map "C" 'url-cookie-list)
755 (define-key map "v" 'eww-view-source)
756 (define-key map "R" 'eww-readable)
757 (define-key map "H" 'eww-list-histories)
758 (define-key map "E" 'eww-set-character-encoding)
759 (define-key map "s" 'eww-switch-to-buffer)
760 (define-key map "S" 'eww-list-buffers)
761 (define-key map "F" 'eww-toggle-fonts)
762 (define-key map "D" 'eww-toggle-paragraph-direction)
763 (define-key map [(meta C)] 'eww-toggle-colors)
765 (define-key map "b" 'eww-add-bookmark)
766 (define-key map "B" 'eww-list-bookmarks)
767 (define-key map [(meta n)] 'eww-next-bookmark)
768 (define-key map [(meta p)] 'eww-previous-bookmark)
770 (easy-menu-define nil map ""
772 ["Exit" quit-window t]
773 ["Close browser" quit-window t]
774 ["Reload" eww-reload t]
775 ["Follow URL in new buffer" eww-open-in-new-buffer]
776 ["Back to previous page" eww-back-url
777 :active (not (zerop (length eww-history)))]
778 ["Forward to next page" eww-forward-url
779 :active (not (zerop eww-history-position))]
780 ["Browse with external browser" eww-browse-with-external-browser t]
781 ["Download" eww-download t]
782 ["View page source" eww-view-source]
783 ["Copy page URL" eww-copy-page-url t]
784 ["List histories" eww-list-histories t]
785 ["Switch to buffer" eww-switch-to-buffer t]
786 ["List buffers" eww-list-buffers t]
787 ["Add bookmark" eww-add-bookmark t]
788 ["List bookmarks" eww-list-bookmarks t]
789 ["List cookies" url-cookie-list t]
790 ["Toggle fonts" eww-toggle-fonts t]
791 ["Toggle colors" eww-toggle-colors t]
792 ["Character Encoding" eww-set-character-encoding]
793 ["Toggle Paragraph Direction" eww-toggle-paragraph-direction]))
796 (defvar eww-tool-bar-map
797 (let ((map (make-sparse-keymap)))
798 (dolist (tool-bar-item
799 '((quit-window . "close")
800 (eww-reload . "refresh")
801 (eww-back-url . "left-arrow")
802 (eww-forward-url . "right-arrow")
803 (eww-view-source . "show")
804 (eww-copy-page-url . "copy")
805 (eww-add-bookmark . "bookmark_add"))) ;; ...
806 (tool-bar-local-item-from-menu
807 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
809 "Tool bar for `eww-mode'.")
811 ;; Autoload cookie needed by desktop.el.
813 (define-derived-mode eww-mode special-mode "eww"
814 "Mode for browsing the web."
815 (setq-local eww-data (list :title ""))
816 (setq-local browse-url-browser-function #'eww-browse-url)
817 (add-hook 'after-change-functions #'eww-process-text-input nil t)
818 (setq-local eww-history nil)
819 (setq-local eww-history-position 0)
820 (when (boundp 'tool-bar-map)
821 (setq-local tool-bar-map eww-tool-bar-map))
823 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
824 ;; multi-page isearch support
825 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
826 (setq truncate-lines t)
827 (buffer-disable-undo)
828 (setq buffer-read-only t))
831 (defun eww-browse-url (url &optional new-window)
833 (pop-to-buffer-same-window
835 (format "*eww-%s*" (url-host (url-generic-parse-url
836 (eww--dwim-expand-url url))))))
840 (defun eww-back-url ()
841 "Go to the previously displayed page."
843 (when (>= eww-history-position (length eww-history))
844 (user-error "No previous page"))
846 (setq eww-history-position (+ eww-history-position 2))
847 (eww-restore-history (elt eww-history (1- eww-history-position))))
849 (defun eww-forward-url ()
850 "Go to the next displayed page."
852 (when (zerop eww-history-position)
853 (user-error "No next page"))
855 (eww-restore-history (elt eww-history (1- eww-history-position))))
857 (defun eww-restore-history (elem)
858 (let ((inhibit-read-only t)
859 (inhibit-modification-hooks t)
860 (text (plist-get elem :text)))
863 (eww-reload) ; FIXME: restore :point?
866 (goto-char (plist-get elem :point))
867 ;; Make buffer listings more informative.
868 (setq list-buffers-directory (plist-get elem :url))
869 (eww-update-header-line-format))))
871 (defun eww-next-url ()
872 "Go to the page marked `next'.
873 A page is marked `next' if rel=\"next\" appears in a <link>
876 (if (plist-get eww-data :next)
877 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
878 (plist-get eww-data :url)))
879 (user-error "No `next' on this page")))
881 (defun eww-previous-url ()
882 "Go to the page marked `previous'.
883 A page is marked `previous' if rel=\"previous\" appears in a <link>
886 (if (plist-get eww-data :previous)
887 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
888 (plist-get eww-data :url)))
889 (user-error "No `previous' on this page")))
892 "Go to the page marked `up'.
893 A page is marked `up' if rel=\"up\" appears in a <link>
896 (if (plist-get eww-data :up)
897 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
898 (plist-get eww-data :url)))
899 (user-error "No `up' on this page")))
901 (defun eww-top-url ()
902 "Go to the page marked `top'.
903 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
904 appears in a <link> or <a> tag."
906 (let ((best-url (or (plist-get eww-data :start)
907 (plist-get eww-data :contents)
908 (plist-get eww-data :home))))
910 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
911 (user-error "No `top' for this page"))))
913 (defun eww-reload (&optional local encode)
914 "Reload the current page.
915 If LOCAL is non-nil (interactively, the command was invoked with
916 a prefix argument), don't reload the page from the network, but
917 just re-display the HTML already fetched."
919 (let ((url (plist-get eww-data :url)))
921 (if (null (plist-get eww-data :dom))
922 (error "No current HTML data")
923 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
924 (point) (current-buffer)))
925 (url-retrieve url 'eww-render
926 (list url (point) (current-buffer) encode)))))
930 (defvar eww-form nil)
932 (defvar eww-submit-map
933 (let ((map (make-sparse-keymap)))
934 (define-key map "\r" 'eww-submit)
935 (define-key map [(control c) (control c)] 'eww-submit)
938 (defvar eww-submit-file
939 (let ((map (make-sparse-keymap)))
940 (define-key map "\r" 'eww-select-file)
941 (define-key map [(control c) (control c)] 'eww-submit)
944 (defvar eww-checkbox-map
945 (let ((map (make-sparse-keymap)))
946 (define-key map " " 'eww-toggle-checkbox)
947 (define-key map "\r" 'eww-toggle-checkbox)
948 (define-key map [(control c) (control c)] 'eww-submit)
952 (let ((map (make-keymap)))
953 (set-keymap-parent map text-mode-map)
954 (define-key map "\r" 'eww-submit)
955 (define-key map [(control a)] 'eww-beginning-of-text)
956 (define-key map [(control c) (control c)] 'eww-submit)
957 (define-key map [(control e)] 'eww-end-of-text)
958 (define-key map [?\t] 'shr-next-link)
959 (define-key map [?\M-\t] 'shr-previous-link)
962 (defvar eww-textarea-map
963 (let ((map (make-keymap)))
964 (set-keymap-parent map text-mode-map)
965 (define-key map "\r" 'forward-line)
966 (define-key map [(control c) (control c)] 'eww-submit)
967 (define-key map [?\t] 'shr-next-link)
968 (define-key map [?\M-\t] 'shr-previous-link)
971 (defvar eww-select-map
972 (let ((map (make-sparse-keymap)))
973 (define-key map "\r" 'eww-change-select)
974 (define-key map [(control c) (control c)] 'eww-submit)
977 (defun eww-beginning-of-text ()
978 "Move to the start of the input field."
980 (goto-char (eww-beginning-of-field)))
982 (defun eww-end-of-text ()
983 "Move to the end of the text in the input field."
985 (goto-char (eww-end-of-field))
986 (let ((start (eww-beginning-of-field)))
987 (while (and (equal (following-char) ? )
990 (when (> (point) start)
993 (defun eww-beginning-of-field ()
997 ((not (eq (get-text-property (point) 'eww-form)
998 (get-text-property (1- (point)) 'eww-form)))
1001 (previous-single-property-change
1002 (point) 'eww-form nil (point-min)))))
1004 (defun eww-end-of-field ()
1005 (1- (next-single-property-change
1006 (point) 'eww-form nil (point-max))))
1008 (defun eww-tag-form (dom)
1009 (let ((eww-form (list (cons :method (dom-attr dom 'method))
1010 (cons :action (dom-attr dom 'action))))
1013 (shr-ensure-paragraph)
1018 (when (> (point) start)
1019 (put-text-property start (1+ start)
1020 'eww-form eww-form))))
1022 (defun eww-form-submit (dom)
1023 (let ((start (point))
1024 (value (dom-attr dom 'value)))
1026 (if (zerop (length value))
1030 (add-face-text-property start (point) 'eww-form-submit)
1031 (put-text-property start (point) 'eww-form
1032 (list :eww-form eww-form
1035 :name (dom-attr dom 'name)))
1036 (put-text-property start (point) 'keymap eww-submit-map)
1039 (defun eww-form-checkbox (dom)
1040 (let ((start (point)))
1041 (if (dom-attr dom 'checked)
1042 (insert eww-form-checkbox-selected-symbol)
1043 (insert eww-form-checkbox-symbol))
1044 (add-face-text-property start (point) 'eww-form-checkbox)
1045 (put-text-property start (point) 'eww-form
1046 (list :eww-form eww-form
1047 :value (dom-attr dom 'value)
1048 :type (downcase (dom-attr dom 'type))
1049 :checked (dom-attr dom 'checked)
1050 :name (dom-attr dom 'name)))
1051 (put-text-property start (point) 'keymap eww-checkbox-map)
1054 (defun eww-form-file (dom)
1055 (let ((start (point))
1056 (value (dom-attr dom 'value)))
1058 (if (zerop (length value))
1062 (add-face-text-property start (point) 'eww-form-file)
1064 (put-text-property start (point) 'eww-form
1065 (list :eww-form eww-form
1066 :value (dom-attr dom 'value)
1067 :type (downcase (dom-attr dom 'type))
1068 :name (dom-attr dom 'name)))
1069 (put-text-property start (point) 'keymap eww-submit-file)
1072 (defun eww-select-file ()
1073 "Change the value of the upload file menu under point."
1075 (let* ((input (get-text-property (point) 'eww-form)))
1077 (let ((insert-default-directory t))
1078 (read-file-name "filename: "))))
1079 (eww-update-field filename (length "Browse"))
1080 (plist-put input :filename filename))))
1082 (defun eww-form-text (dom)
1083 (let ((start (point))
1084 (type (downcase (or (dom-attr dom 'type) "text")))
1085 (value (or (dom-attr dom 'value) ""))
1086 (width (string-to-number (or (dom-attr dom 'size) "40")))
1087 (readonly-property (if (or (dom-attr dom 'disabled)
1088 (dom-attr dom 'readonly))
1090 'inhibit-read-only)))
1092 (when (< (length value) width)
1093 (insert (make-string (- width (length value)) ? )))
1094 (put-text-property start (point) 'face 'eww-form-text)
1095 (put-text-property start (point) 'inhibit-read-only t)
1096 (put-text-property start (point) 'local-map eww-text-map)
1097 (put-text-property start (point) readonly-property t)
1098 (put-text-property start (point) 'eww-form
1099 (list :eww-form eww-form
1102 :name (dom-attr dom 'name)))
1105 (defconst eww-text-input-types '("text" "password" "textarea"
1106 "color" "date" "datetime" "datetime-local"
1107 "email" "month" "number" "search" "tel"
1108 "time" "url" "week")
1109 "List of input types which represent a text input.
1110 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1112 (defun eww-process-text-input (beg end replace-length)
1113 (when-let* ((pos (and (< (1+ end) (point-max))
1114 (> (1- end) (point-min))
1116 ((get-text-property (1+ end) 'eww-form)
1118 ((get-text-property (1- end) 'eww-form)
1120 (let* ((form (get-text-property pos 'eww-form))
1121 (properties (text-properties-at pos))
1122 (buffer-undo-list t)
1123 (inhibit-read-only t)
1124 (length (- end beg replace-length))
1125 (type (plist-get form :type)))
1127 (member type eww-text-input-types))
1130 ;; Delete some space at the end.
1133 (if (equal type "textarea")
1134 (1- (line-end-position))
1135 (eww-end-of-field)))
1136 (while (and (> length 0)
1137 (eql (char-after (1- (point))) ? ))
1138 (delete-region (1- (point)) (point))
1145 (if (equal type "textarea")
1146 (1- (line-end-position))
1147 (1+ (eww-end-of-field))))
1148 (let ((start (point)))
1149 (insert (make-string (abs length) ? ))
1150 (set-text-properties start (point) properties))
1151 (goto-char (1- end)))))
1152 (set-text-properties (cdr (assq :start form))
1153 (cdr (assq :end form))
1155 (let ((value (buffer-substring-no-properties
1156 (eww-beginning-of-field)
1157 (eww-end-of-field))))
1158 (when (string-match " +\\'" value)
1159 (setq value (substring value 0 (match-beginning 0))))
1160 (plist-put form :value value)
1161 (when (equal type "password")
1162 ;; Display passwords as asterisks.
1163 (let ((start (eww-beginning-of-field)))
1165 start (+ start (length value))
1166 'display (make-string (length value) ?*)))))))))
1168 (defun eww-tag-textarea (dom)
1169 (let ((start (point))
1170 (value (or (dom-attr dom 'value) ""))
1171 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1172 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1174 (shr-ensure-newline)
1176 (shr-ensure-newline)
1177 (when (< (count-lines start (point)) lines)
1178 (dotimes (_ (- lines (count-lines start (point))))
1180 (setq end (point-marker))
1182 (while (< (point) end)
1184 (let ((pad (- width (- (point) (line-beginning-position)))))
1186 (insert (make-string pad ? ))))
1187 (add-face-text-property (line-beginning-position)
1188 (point) 'eww-form-textarea)
1189 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1190 (put-text-property (line-beginning-position) (point)
1191 'local-map eww-textarea-map)
1193 (put-text-property start (point) 'eww-form
1194 (list :eww-form eww-form
1197 :name (dom-attr dom 'name)))))
1199 (defun eww-tag-input (dom)
1200 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1203 ((or (equal type "checkbox")
1204 (equal type "radio"))
1205 (eww-form-checkbox dom))
1206 ((equal type "file")
1207 (eww-form-file dom))
1208 ((equal type "submit")
1209 (eww-form-submit dom))
1210 ((equal type "hidden")
1211 (let ((form eww-form)
1212 (name (dom-attr dom 'name)))
1213 ;; Don't add <input type=hidden> elements repeatedly.
1215 (or (not (consp (car form)))
1216 (not (eq (caar form) 'hidden))
1217 (not (equal (plist-get (cdr (car form)) :name)
1219 (setq form (cdr form)))
1221 (nconc eww-form (list
1224 :value (or (dom-attr dom 'value) "")))))))
1226 (eww-form-text dom)))
1227 (unless (= start (point))
1228 (put-text-property start (1+ start) 'help-echo "Input field")
1229 ;; Mark this as an element we can TAB to.
1230 (put-text-property start (1+ start) 'shr-url dom))))
1232 (defun eww-tag-select (dom)
1233 (shr-ensure-paragraph)
1234 (let ((menu (list :name (dom-attr dom 'name)
1235 :eww-form eww-form))
1240 (if (eq (dom-tag dom) 'optgroup)
1241 (dolist (groupelem (dom-children dom))
1242 (unless (dom-attr groupelem 'disabled)
1243 (setq opelem (append opelem (list groupelem)))))
1244 (setq opelem (list dom)))
1245 (dolist (elem opelem)
1246 (when (eq (dom-tag elem) 'option)
1247 (when (dom-attr elem 'selected)
1248 (nconc menu (list :value (dom-attr elem 'value))))
1249 (let ((display (dom-text elem)))
1250 (setq max (max max (length display)))
1252 :value (dom-attr elem 'value)
1256 (setq options (nreverse options))
1257 ;; If we have no selected values, default to the first value.
1258 (unless (plist-get menu :value)
1259 (nconc menu (list :value (nth 2 (car options)))))
1260 (nconc menu options)
1261 (let ((selected (eww-select-display menu)))
1263 (make-string (- max (length selected)) ? )))
1264 (put-text-property start (point) 'eww-form menu)
1265 (add-face-text-property start (point) 'eww-form-select)
1266 (put-text-property start (point) 'keymap eww-select-map)
1267 (unless (= start (point))
1268 (put-text-property start (1+ start) 'help-echo "select field"))
1269 (shr-ensure-paragraph))))
1271 (defun eww-select-display (select)
1272 (let ((value (plist-get select :value))
1274 (dolist (elem select)
1275 (when (and (consp elem)
1276 (eq (car elem) 'item)
1277 (equal value (plist-get (cdr elem) :value)))
1278 (setq display (plist-get (cdr elem) :display))))
1281 (defun eww-change-select ()
1282 "Change the value of the select drop-down menu under point."
1284 (let* ((input (get-text-property (point) 'eww-form))
1285 (completion-ignore-case t)
1288 (mapcar (lambda (elem)
1290 (eq (car elem) 'item)
1291 (cons (plist-get (cdr elem) :display)
1292 (plist-get (cdr elem) :value))))
1295 (completing-read "Change value: " options nil 'require-match))
1296 (inhibit-read-only t))
1297 (plist-put input :value (cdr (assoc-string display options t)))
1299 (eww-update-field display))))
1301 (defun eww-update-field (string &optional offset)
1304 (let ((properties (text-properties-at (point)))
1305 (start (+ (eww-beginning-of-field) offset))
1306 (current-end (1+ (eww-end-of-field)))
1307 (new-end (+ (eww-beginning-of-field) (length string)))
1308 (inhibit-read-only t))
1309 (delete-region start current-end)
1310 (forward-char offset)
1312 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1314 (set-text-properties start new-end properties))
1317 (defun eww-toggle-checkbox ()
1318 "Toggle the value of the checkbox under point."
1320 (let* ((input (get-text-property (point) 'eww-form))
1321 (type (plist-get input :type)))
1322 (if (equal type "checkbox")
1325 (if (plist-get input :checked)
1327 (plist-put input :checked nil)
1328 (eww-update-field eww-form-checkbox-symbol))
1329 (plist-put input :checked t)
1330 (eww-update-field eww-form-checkbox-selected-symbol))))
1331 ;; Radio button. Switch all other buttons off.
1332 (let ((name (plist-get input :name)))
1334 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1335 (when (equal (plist-get (cdr elem) :name) name)
1336 (goto-char (car elem))
1337 (if (not (eq (cdr elem) input))
1339 (plist-put input :checked nil)
1340 (eww-update-field eww-form-checkbox-symbol))
1341 (plist-put input :checked t)
1342 (eww-update-field eww-form-checkbox-selected-symbol)))))
1343 (forward-char 1)))))
1345 (defun eww-inputs (form)
1346 (let ((start (point-min))
1349 (< start (point-max)))
1350 (when (or (get-text-property start 'eww-form)
1351 (setq start (next-single-property-change start 'eww-form)))
1352 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1354 (push (cons start (get-text-property start 'eww-form))
1356 (setq start (next-single-property-change start 'eww-form))))
1359 (defun eww-size-text-inputs ()
1360 (let ((start (point-min)))
1362 (< start (point-max)))
1363 (when (or (get-text-property start 'eww-form)
1364 (setq start (next-single-property-change start 'eww-form)))
1365 (let ((props (get-text-property start 'eww-form)))
1366 (nconc props (list (cons :start start)))
1367 (setq start (next-single-property-change
1368 start 'eww-form nil (point-max)))
1369 (nconc props (list (cons :end start))))))))
1371 (defun eww-input-value (input)
1372 (let ((type (plist-get input :type))
1373 (value (plist-get input :value)))
1375 ((equal type "textarea")
1378 (goto-char (point-min))
1379 (while (re-search-forward "^ +\n\\| +$" nil t)
1380 (replace-match "" t t))
1383 (if (string-match " +\\'" value)
1384 (substring value 0 (match-beginning 0))
1387 (defun eww-submit ()
1388 "Submit the current form."
1390 (let* ((this-input (get-text-property (point) 'eww-form))
1391 (form (plist-get this-input :eww-form))
1393 (dolist (elem (sort (eww-inputs form)
1395 (< (car o1) (car o2)))))
1396 (let* ((input (cdr elem))
1397 (input-start (car elem))
1398 (name (plist-get input :name)))
1401 ((member (plist-get input :type) '("checkbox" "radio"))
1402 (when (plist-get input :checked)
1403 (push (cons name (plist-get input :value))
1405 ((equal (plist-get input :type) "file")
1407 (list (cons "filedata"
1409 (insert-file-contents
1410 (plist-get input :filename))
1412 (cons "name" (plist-get input :name))
1413 (cons "filename" (plist-get input :filename))))
1415 ((equal (plist-get input :type) "submit")
1416 ;; We want the values from buttons if we hit a button if
1417 ;; we hit enter on it, or if it's the first button after
1418 ;; the field we did hit return on.
1419 (when (or (eq input this-input)
1420 (and (not (eq input this-input))
1422 (> input-start (point))))
1423 (setq next-submit t)
1424 (push (cons name (plist-get input :value))
1427 (push (cons name (eww-input-value input))
1430 (when (and (consp elem)
1431 (eq (car elem) 'hidden))
1432 (push (cons (plist-get (cdr elem) :name)
1433 (or (plist-get (cdr elem) :value) ""))
1435 (if (and (stringp (cdr (assq :method form)))
1436 (equal (downcase (cdr (assq :method form))) "post"))
1438 (dolist (x values mtype)
1439 (if (equal (car x) "file")
1441 (setq mtype "multipart/form-data"))))
1442 (cond ((equal mtype "multipart/form-data")
1443 (let ((boundary (mml-compute-boundary '())))
1444 (let ((url-request-method "POST")
1445 (url-request-extra-headers
1446 (list (cons "Content-Type"
1447 (concat "multipart/form-data; boundary="
1450 (mm-url-encode-multipart-form-data values boundary)))
1451 (eww-browse-url (shr-expand-url
1452 (cdr (assq :action form))
1453 (plist-get eww-data :url))))))
1455 (let ((url-request-method "POST")
1456 (url-request-extra-headers
1458 "application/x-www-form-urlencoded")))
1460 (mm-url-encode-www-form-urlencoded values)))
1461 (eww-browse-url (shr-expand-url
1462 (cdr (assq :action form))
1463 (plist-get eww-data :url)))))))
1466 (if (cdr (assq :action form))
1467 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1468 (plist-get eww-data :url))
1470 (mm-url-encode-www-form-urlencoded values))))))
1472 (defun eww-browse-with-external-browser (&optional url)
1473 "Browse the current URL with an external browser.
1474 The browser to used is specified by the `shr-external-browser' variable."
1476 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1478 (defun eww-follow-link (&optional external mouse-event)
1479 "Browse the URL under point.
1480 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1481 If EXTERNAL is double prefix, browse in new buffer."
1482 (interactive (list current-prefix-arg last-nonmenu-event))
1483 (mouse-set-point mouse-event)
1484 (let ((url (get-text-property (point) 'shr-url)))
1487 (message "No link under point"))
1488 ((string-match "^mailto:" url)
1489 (browse-url-mail url))
1490 ((and (consp external) (<= (car external) 4))
1491 (funcall shr-external-browser url))
1492 ;; This is a #target url in the same page as the current one.
1493 ((and (url-target (url-generic-parse-url url))
1494 (eww-same-page-p url (plist-get eww-data :url)))
1495 (let ((dom (plist-get eww-data :dom)))
1497 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1499 (eww-browse-url url external)))))
1501 (defun eww-same-page-p (url1 url2)
1502 "Return non-nil if URL1 and URL2 represent the same page.
1503 Differences in #targets are ignored."
1504 (let ((obj1 (url-generic-parse-url url1))
1505 (obj2 (url-generic-parse-url url2)))
1506 (setf (url-target obj1) nil)
1507 (setf (url-target obj2) nil)
1508 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1510 (defun eww-copy-page-url ()
1511 "Copy the URL of the current page into the kill ring."
1513 (message "%s" (plist-get eww-data :url))
1514 (kill-new (plist-get eww-data :url)))
1516 (defun eww-download ()
1517 "Download URL under point to `eww-download-directory'."
1519 (access-file eww-download-directory "Download failed")
1520 (let ((url (get-text-property (point) 'shr-url)))
1522 (message "No URL under point")
1523 (url-retrieve url 'eww-download-callback (list url)))))
1525 (defun eww-download-callback (status url)
1526 (unless (plist-get status :error)
1527 (let* ((obj (url-generic-parse-url url))
1528 (path (car (url-path-and-query obj)))
1529 (file (eww-make-unique-file-name
1530 (eww-decode-url-file-name (file-name-nondirectory path))
1531 eww-download-directory)))
1532 (goto-char (point-min))
1533 (re-search-forward "\r?\n\r?\n")
1534 (write-region (point) (point-max) file)
1535 (message "Saved %s" file))))
1537 (defun eww-decode-url-file-name (string)
1538 (let* ((binary (url-unhex-string string))
1540 (decode-coding-string
1542 ;; Possibly set by `universal-coding-system-argument'.
1543 (or coding-system-for-read
1544 ;; RFC 3986 says that %AB stuff is utf-8.
1545 (if (equal (decode-coding-string binary 'utf-8)
1549 (car (detect-coding-string binary))))))
1550 (encodes (find-coding-systems-string decoded)))
1551 (if (or (equal encodes '(undecided))
1552 (memq (coding-system-base (or file-name-coding-system
1553 default-file-name-coding-system))
1556 ;; If we can't encode the decoded file name (due to language
1557 ;; environment settings), then we return the original, hexified
1561 (defun eww-make-unique-file-name (file directory)
1563 ((zerop (length file))
1565 ((string-match "\\`[.]" file)
1566 (setq file (concat "!" file))))
1570 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1571 (setq stem (match-string 1 file)
1572 suffix (match-string 2)))
1573 (while (file-exists-p (expand-file-name file directory))
1574 (setq file (format "%s(%d)%s" stem count suffix))
1575 (setq count (1+ count)))
1576 (expand-file-name file directory)))
1578 (defun eww-set-character-encoding (charset)
1579 "Set character encoding to CHARSET.
1580 If CHARSET is nil then use UTF-8."
1581 (interactive "zUse character set (default utf-8): ")
1583 (eww-reload nil 'utf-8)
1584 (eww-reload nil charset)))
1586 (defun eww-switch-to-buffer ()
1587 "Prompt for an EWW buffer to display in the selected window."
1589 (let ((completion-extra-properties
1590 '(:annotation-function (lambda (buf)
1591 (with-current-buffer buf
1592 (format " %s" (eww-current-url)))))))
1593 (pop-to-buffer-same-window
1594 (read-buffer "Switch to EWW buffer: "
1595 (cl-loop for buf in (nreverse (buffer-list))
1596 if (with-current-buffer buf (derived-mode-p 'eww-mode))
1600 (with-current-buffer
1601 (if (consp bufn) (cdr bufn) (get-buffer bufn))
1602 (derived-mode-p 'eww-mode)))))))
1604 (defun eww-toggle-fonts ()
1605 "Toggle whether to use monospaced or font-enabled layouts."
1607 (setq shr-use-fonts (not shr-use-fonts))
1609 (message "Proportional fonts are now %s"
1610 (if shr-use-fonts "on" "off")))
1612 (defun eww-toggle-colors ()
1613 "Toggle whether to use HTML-specified colors or not."
1615 (message "Colors are now %s"
1616 (if (setq shr-use-colors (not shr-use-colors))
1623 (defvar eww-bookmarks nil)
1625 (defun eww-add-bookmark ()
1626 "Bookmark the current page."
1628 (eww-read-bookmarks)
1629 (dolist (bookmark eww-bookmarks)
1630 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1631 (user-error "Already bookmarked")))
1632 (when (y-or-n-p "Bookmark this page?")
1633 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1634 (plist-get eww-data :title))))
1635 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1636 (push (list :url (plist-get eww-data :url)
1638 :time (current-time-string))
1640 (eww-write-bookmarks)
1641 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1642 (plist-get eww-data :title))))
1644 (defun eww-write-bookmarks ()
1645 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1646 (insert ";; Auto-generated file; don't edit\n")
1647 (pp eww-bookmarks (current-buffer))))
1649 (defun eww-read-bookmarks ()
1650 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1652 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1654 (insert-file-contents file)
1655 (read (current-buffer)))))))
1658 (defun eww-list-bookmarks ()
1659 "Display the bookmarks."
1661 (pop-to-buffer "*eww bookmarks*")
1662 (eww-bookmark-prepare))
1664 (defun eww-bookmark-prepare ()
1665 (eww-read-bookmarks)
1666 (unless eww-bookmarks
1667 (user-error "No bookmarks are defined"))
1668 (set-buffer (get-buffer-create "*eww bookmarks*"))
1670 (let* ((width (/ (window-width) 2))
1671 (format (format "%%-%ds %%s" width))
1672 (inhibit-read-only t)
1675 (setq header-line-format (concat " " (format format "Title" "URL")))
1676 (dolist (bookmark eww-bookmarks)
1678 title (plist-get bookmark :title))
1679 (when (> (length title) width)
1680 (setq title (truncate-string-to-width title width)))
1681 (insert (format format title (plist-get bookmark :url)) "\n")
1682 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1683 (goto-char (point-min))))
1685 (defvar eww-bookmark-kill-ring nil)
1687 (defun eww-bookmark-kill ()
1688 "Kill the current bookmark."
1690 (let* ((start (line-beginning-position))
1691 (bookmark (get-text-property start 'eww-bookmark))
1692 (inhibit-read-only t))
1694 (user-error "No bookmark on the current line"))
1696 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1697 (delete-region start (point))
1698 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1699 (eww-write-bookmarks)))
1701 (defun eww-bookmark-yank ()
1702 "Yank a previously killed bookmark to the current line."
1704 (unless eww-bookmark-kill-ring
1705 (user-error "No previously killed bookmark"))
1707 (let ((inhibit-read-only t)
1710 (insert (pop eww-bookmark-kill-ring))
1711 (setq bookmark (get-text-property start 'eww-bookmark))
1712 (if (= start (point-min))
1713 (push bookmark eww-bookmarks)
1714 (let ((line (count-lines start (point))))
1715 (setcdr (nthcdr (1- line) eww-bookmarks)
1716 (cons bookmark (nthcdr line eww-bookmarks)))))
1717 (eww-write-bookmarks)))
1719 (defun eww-bookmark-browse ()
1720 "Browse the bookmark under point in eww."
1722 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1724 (user-error "No bookmark on the current line"))
1726 (eww-browse-url (plist-get bookmark :url))))
1728 (defun eww-next-bookmark ()
1729 "Go to the next bookmark in the list."
1733 (unless (get-buffer "*eww bookmarks*")
1735 (eww-bookmark-prepare))
1736 (with-current-buffer (get-buffer "*eww bookmarks*")
1737 (when (and (not first)
1740 (setq bookmark (get-text-property (line-beginning-position)
1743 (user-error "No next bookmark")))
1744 (eww-browse-url (plist-get bookmark :url))))
1746 (defun eww-previous-bookmark ()
1747 "Go to the previous bookmark in the list."
1751 (unless (get-buffer "*eww bookmarks*")
1753 (eww-bookmark-prepare))
1754 (with-current-buffer (get-buffer "*eww bookmarks*")
1756 (goto-char (point-max))
1757 (beginning-of-line))
1758 ;; On the final line.
1762 (user-error "No previous bookmark")
1764 (setq bookmark (get-text-property (line-beginning-position)
1766 (eww-browse-url (plist-get bookmark :url))))
1768 (defvar eww-bookmark-mode-map
1769 (let ((map (make-sparse-keymap)))
1770 (define-key map [(control k)] 'eww-bookmark-kill)
1771 (define-key map [(control y)] 'eww-bookmark-yank)
1772 (define-key map "\r" 'eww-bookmark-browse)
1774 (easy-menu-define nil map
1775 "Menu for `eww-bookmark-mode-map'."
1777 ["Exit" quit-window t]
1778 ["Browse" eww-bookmark-browse
1779 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1780 ["Kill" eww-bookmark-kill
1781 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1782 ["Yank" eww-bookmark-yank
1783 :active eww-bookmark-kill-ring]))
1786 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1787 "Mode for listing bookmarks.
1789 \\{eww-bookmark-mode-map}"
1790 (buffer-disable-undo)
1791 (setq truncate-lines t))
1795 (defun eww-save-history ()
1796 (plist-put eww-data :point (point))
1797 (plist-put eww-data :text (buffer-string))
1798 (push eww-data eww-history)
1799 (setq eww-data (list :title ""))
1800 ;; Don't let the history grow infinitely. We store quite a lot of
1802 (when-let* ((tail (and eww-history-limit
1803 (nthcdr eww-history-limit eww-history))))
1806 (defvar eww-current-buffer)
1808 (defun eww-list-histories ()
1809 "List the eww-histories."
1811 (when (null eww-history)
1812 (error "No eww-histories are defined"))
1813 (let ((eww-history-trans eww-history)
1814 (buffer (current-buffer)))
1815 (set-buffer (get-buffer-create "*eww history*"))
1817 (setq-local eww-current-buffer buffer)
1818 (let ((inhibit-read-only t)
1821 url title format start)
1823 (dolist (history eww-history-trans)
1824 (setq start (point))
1825 (setq domain-length (max domain-length (length (plist-get history :url))))
1826 (setq title-length (max title-length (length (plist-get history :title)))))
1827 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1829 (concat " " (format format "Title" "URL")))
1830 (dolist (history eww-history-trans)
1831 (setq start (point))
1832 (setq url (plist-get history :url))
1833 (setq title (plist-get history :title))
1834 (insert (format format title url))
1836 (put-text-property start (1+ start) 'eww-history history))
1837 (goto-char (point-min)))
1838 (pop-to-buffer "*eww history*")))
1840 (defun eww-history-browse ()
1841 "Browse the history under point in eww."
1843 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1845 (error "No history on the current line"))
1846 (let ((buffer eww-current-buffer))
1849 (pop-to-buffer-same-window buffer)))
1850 (eww-restore-history history)))
1852 (defvar eww-history-mode-map
1853 (let ((map (make-sparse-keymap)))
1854 (define-key map "\r" 'eww-history-browse)
1855 ;; (define-key map "n" 'next-error-no-select)
1856 ;; (define-key map "p" 'previous-error-no-select)
1858 (easy-menu-define nil map
1859 "Menu for `eww-history-mode-map'."
1861 ["Exit" quit-window t]
1862 ["Browse" eww-history-browse
1863 :active (get-text-property (line-beginning-position) 'eww-history)]))
1866 (define-derived-mode eww-history-mode special-mode "eww history"
1867 "Mode for listing eww-histories.
1869 \\{eww-history-mode-map}"
1870 (buffer-disable-undo)
1871 (setq truncate-lines t))
1873 ;;; eww buffers list
1875 (defun eww-list-buffers ()
1876 "Enlist eww buffers."
1879 (current (current-buffer)))
1880 (dolist (buffer (buffer-list))
1881 (with-current-buffer buffer
1882 (when (derived-mode-p 'eww-mode)
1883 (push (vector buffer (plist-get eww-data :title)
1884 (plist-get eww-data :url))
1886 (unless buffers-info
1887 (error "No eww buffers"))
1888 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1889 (set-buffer (get-buffer-create "*eww buffers*"))
1891 (let ((inhibit-read-only t)
1894 url title format start)
1896 (dolist (buffer-info buffers-info)
1897 (setq title-length (max title-length
1898 (length (elt buffer-info 1)))
1899 domain-length (max domain-length
1900 (length (elt buffer-info 2)))))
1901 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1903 (concat " " (format format "Title" "URL")))
1905 (current-buffer-line 1))
1906 (dolist (buffer-info buffers-info)
1908 title (elt buffer-info 1)
1909 url (elt buffer-info 2)
1911 (insert (format format title url))
1913 (let ((buffer (elt buffer-info 0)))
1914 (put-text-property start (1+ start) 'eww-buffer
1916 (when (eq current buffer)
1917 (setq current-buffer-line line))))
1918 (goto-char (point-min))
1919 (forward-line (1- current-buffer-line)))))
1920 (pop-to-buffer "*eww buffers*"))
1922 (defun eww-buffer-select ()
1923 "Switch to eww buffer."
1925 (let ((buffer (get-text-property (line-beginning-position)
1928 (error "No buffer on current line"))
1930 (pop-to-buffer-same-window buffer)))
1932 (defun eww-buffer-show ()
1933 "Display buffer under point in eww buffer list."
1934 (let ((buffer (get-text-property (line-beginning-position)
1937 (error "No buffer on current line"))
1939 (pop-to-buffer-same-window buffer)
1942 (defun eww-buffer-show-next ()
1943 "Move to next eww buffer in the list and display it."
1947 (goto-char (point-min)))
1950 (defun eww-buffer-show-previous ()
1951 "Move to previous eww buffer in the list and display it."
1955 (goto-char (point-max)))
1959 (defun eww-buffer-kill ()
1960 "Kill buffer from eww list."
1962 (let* ((start (line-beginning-position))
1963 (buffer (get-text-property start 'eww-buffer))
1964 (inhibit-read-only t))
1966 (user-error "No buffer on the current line"))
1967 (kill-buffer buffer)
1969 (delete-region start (point)))
1974 (defvar eww-buffers-mode-map
1975 (let ((map (make-sparse-keymap)))
1976 (define-key map [(control k)] 'eww-buffer-kill)
1977 (define-key map "\r" 'eww-buffer-select)
1978 (define-key map "n" 'eww-buffer-show-next)
1979 (define-key map "p" 'eww-buffer-show-previous)
1981 (easy-menu-define nil map
1982 "Menu for `eww-buffers-mode-map'."
1984 ["Exit" quit-window t]
1985 ["Select" eww-buffer-select
1986 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1987 ["Kill" eww-buffer-kill
1988 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1991 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1992 "Mode for listing buffers.
1994 \\{eww-buffers-mode-map}"
1995 (buffer-disable-undo)
1996 (setq truncate-lines t))
2000 (defvar eww-desktop-data-save
2001 '(:url :title :point)
2002 "List of `eww-data' properties to preserve in the desktop file.
2003 Also used when saving `eww-history'.")
2005 (defun eww-desktop-data-1 (alist)
2009 (let ((k (car tail))
2011 (when (memq k eww-desktop-data-save)
2012 (setq acc (cons k (cons v acc)))))
2013 (setq tail (cddr tail)))
2016 (defun eww-desktop-history-duplicate (a b)
2017 (let ((tail a) (r t))
2019 (if (or (memq (car tail) '(:point)) ; ignore :point
2021 (plist-get b (car tail))))
2022 (setq tail (cddr tail))
2028 (defun eww-desktop-misc-data (_directory)
2029 "Return a property list with data used to restore eww buffers.
2030 This list will contain, as :history, the list, whose first element is
2031 the value of `eww-data', and the tail is `eww-history'.
2033 If `eww-desktop-remove-duplicates' is non-nil, duplicate
2034 entries (if any) will be removed from the list.
2036 Only the properties listed in `eww-desktop-data-save' are included.
2037 Generally, the list should not include the (usually overly large)
2038 :dom, :source and :text properties."
2039 (let ((history (mapcar 'eww-desktop-data-1
2040 (cons eww-data eww-history))))
2041 (list :history (if eww-desktop-remove-duplicates
2042 (cl-remove-duplicates
2043 history :test 'eww-desktop-history-duplicate)
2046 (defun eww-restore-desktop (file-name buffer-name misc-data)
2047 "Restore an eww buffer from its desktop file record.
2048 If `eww-restore-desktop' is t or `auto', this function will also
2049 initiate the retrieval of the respective URI in the background.
2050 Otherwise, the restored buffer will contain a prompt to do so by using
2052 (with-current-buffer (get-buffer-create buffer-name)
2054 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
2055 (setq eww-history (cdr (plist-get misc-data :history))
2056 eww-data (or (car (plist-get misc-data :history))
2057 ;; backwards compatibility
2058 (list :url (plist-get misc-data :uri))))
2060 (when (plist-get eww-data :url)
2061 (cl-case eww-restore-desktop
2062 ((t auto) (eww (plist-get eww-data :url)))
2063 ((zerop (buffer-size))
2064 (let ((inhibit-read-only t))
2065 (insert (substitute-command-keys
2066 eww-restore-reload-prompt)))))))
2070 (add-to-list 'desktop-locals-to-save
2071 'eww-history-position)
2072 (add-to-list 'desktop-buffer-mode-handlers
2073 '(eww-mode . eww-restore-desktop))
2077 (defun eww-isearch-next-buffer (&optional _buffer wrap)
2078 "Go to the next page to search using `rel' attribute for navigation."
2085 (eww-previous-url)))
2090 ;;; eww.el ends here