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/>.
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-open-in-new-buffer ()
318 "Fetch link at point in a new EWW buffer."
320 (let ((url (eww-suggested-uris)))
321 (if (null url
) (user-error "No link at point")
322 ;; clone useful to keep history, but
323 ;; should not clone from non-eww buffer
325 (if (eq major-mode
'eww-mode
) (clone-buffer)
326 (generate-new-buffer "*eww*"))
327 (unless (equal url
(eww-current-url))
329 (eww (if (consp url
) (car url
) url
)))))))
331 (defun eww-html-p (content-type)
332 "Return non-nil if CONTENT-TYPE designates an HTML content type.
333 Currently this means either text/html or application/xhtml+xml."
334 (member content-type
'("text/html"
335 "application/xhtml+xml")))
337 (defun eww-render (status url
&optional point buffer encode
)
338 (let ((redirect (plist-get status
:redirect
)))
340 (setq url redirect
)))
341 (let* ((headers (eww-parse-headers))
343 (mail-header-parse-content-type
344 (if (zerop (length (cdr (assoc "content-type" headers
))))
346 (cdr (assoc "content-type" headers
)))))
349 (or (cdr (assq 'charset
(cdr content-type
)))
350 (eww-detect-charset (eww-html-p (car content-type
)))
352 (data-buffer (current-buffer))
353 last-coding-system-used
)
354 ;; Save the https peer status.
355 (with-current-buffer buffer
356 (plist-put eww-data
:peer
(plist-get status
:peer
)))
360 ((and eww-use-external-browser-for-content-type
361 (string-match-p eww-use-external-browser-for-content-type
363 (eww-browse-with-external-browser url
))
364 ((eww-html-p (car content-type
))
365 (eww-display-html charset url nil point buffer encode
))
366 ((equal (car content-type
) "application/pdf")
368 ((string-match-p "\\`image/" (car content-type
))
369 (eww-display-image buffer
))
371 (eww-display-raw buffer
(or encode charset
'utf-8
))))
372 (with-current-buffer buffer
373 (plist-put eww-data
:url url
)
374 (eww-update-header-line-format)
375 (setq eww-history-position
0)
376 (and last-coding-system-used
377 (set-buffer-file-coding-system last-coding-system-used
))
378 (run-hooks 'eww-after-render-hook
)))
379 (kill-buffer data-buffer
))))
381 (defun eww-parse-headers ()
383 (goto-char (point-min))
384 (while (and (not (eobp))
386 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
387 (push (cons (downcase (match-string 1))
395 (defun eww-detect-charset (html-p)
396 (let ((case-fold-search t
)
400 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
404 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
407 (declare-function libxml-parse-html-region
"xml.c"
408 (start end
&optional base-url discard-comments
))
410 (defun eww-display-html (charset url
&optional document point buffer encode
)
411 (unless (fboundp 'libxml-parse-html-region
)
412 (error "This function requires Emacs to be compiled with libxml2"))
413 (unless (buffer-live-p buffer
)
414 (error "Buffer %s doesn't exist" buffer
))
415 ;; There should be a better way to abort loading images
421 'base
(list (cons 'href url
))
423 (setq encode
(or encode charset
'utf-8
))
425 (decode-coding-region (point) (point-max) encode
)
426 (coding-system-error nil
))
428 ;; Remove CRLF before parsing.
429 (while (re-search-forward "\r$" nil t
)
430 (replace-match "" t t
)))
431 (libxml-parse-html-region (point) (point-max))))))
432 (source (and (null document
)
433 (buffer-substring (point) (point-max)))))
434 (with-current-buffer buffer
435 (setq bidi-paragraph-direction nil
)
436 (plist-put eww-data
:source source
)
437 (plist-put eww-data
:dom document
)
438 (let ((inhibit-read-only t
)
439 (inhibit-modification-hooks t
)
440 (shr-target-id (url-target (url-generic-parse-url url
)))
441 (shr-external-rendering-functions
443 shr-external-rendering-functions
444 '((title . eww-tag-title
)
445 (form . eww-tag-form
)
446 (input . eww-tag-input
)
447 (button . eww-form-submit
)
448 (textarea . eww-tag-textarea
)
449 (select . eww-tag-select
)
450 (link . eww-tag-link
)
451 (meta . eww-tag-meta
)
454 (shr-insert-document document
)
459 (goto-char (point-min))
460 (let ((point (next-single-property-change
461 (point-min) 'shr-target-id
)))
465 (goto-char (point-min))
466 ;; Don't leave point inside forms, because the normal eww
467 ;; commands aren't available there.
468 (while (and (not (eobp))
469 (get-text-property (point) 'eww-form
))
471 (eww-size-text-inputs))))
473 (defun eww-handle-link (dom)
474 (let* ((rel (dom-attr dom
'rel
))
475 (href (dom-attr dom
'href
))
477 ;; The text associated with :rel is case-insensitive.
478 (if rel
(downcase rel
))
480 ;; Texinfo uses "previous", but HTML specifies
481 ;; "prev", so recognize both.
482 ("previous" .
:previous
)
484 ;; HTML specifies "start" but also "contents",
485 ;; and Gtk seems to use "home". Recognize
486 ;; them all; but store them in different
487 ;; variables so that we can readily choose the
491 ("contents" .
:contents
)
495 (plist-put eww-data
(cdr where
) href
))))
497 (defvar eww-redirect-level
1)
499 (defun eww-tag-meta (dom)
500 (when (and (cl-equalp (dom-attr dom
'http-equiv
) "refresh")
501 (< eww-redirect-level
5))
502 (when-let (refresh (dom-attr dom
'content
))
503 (when (or (string-match "^\\([0-9]+\\) *;.*url=\"\\([^\"]+\\)\"" refresh
)
504 (string-match "^\\([0-9]+\\) *;.*url=\\([^ ]+\\)" refresh
))
505 (let ((timeout (match-string 1 refresh
))
506 (url (match-string 2 refresh
))
507 (eww-redirect-level (1+ eww-redirect-level
)))
508 (if (equal timeout
"0")
509 (eww (shr-expand-url url
))
511 (dom-node 'a
`((href .
,(shr-expand-url url
)))
512 (format "Auto refresh in %s second%s disabled"
514 (if (equal timeout
"1")
518 (defun eww-tag-link (dom)
519 (eww-handle-link dom
)
522 (defun eww-tag-a (dom)
523 (eww-handle-link dom
)
524 (let ((start (point)))
526 (put-text-property start
(point) 'keymap eww-link-keymap
)))
528 (defun eww-update-header-line-format ()
529 (setq header-line-format
530 (and eww-header-line-format
531 (let ((title (plist-get eww-data
:title
))
532 (peer (plist-get eww-data
:peer
)))
533 (when (zerop (length title
))
534 (setq title
"[untitled]"))
535 ;; This connection has is https.
538 (propertize title
'face
539 (if (plist-get peer
:warnings
)
540 'eww-invalid-certificate
541 'eww-valid-certificate
))))
542 (replace-regexp-in-string
545 eww-header-line-format
546 `((?u .
,(or (plist-get eww-data
:url
) ""))
549 (defun eww-tag-title (dom)
550 (plist-put eww-data
:title
551 (replace-regexp-in-string
553 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom
))))
554 (eww-update-header-line-format))
556 (defun eww-display-raw (buffer &optional encode
)
557 (let ((data (buffer-substring (point) (point-max))))
558 (unless (buffer-live-p buffer
)
559 (error "Buffer %s doesn't exist" buffer
))
560 (with-current-buffer buffer
561 (let ((inhibit-read-only t
))
565 (decode-coding-region (point-min) (1+ (length data
)) encode
)
566 (coding-system-error nil
)))
567 (goto-char (point-min)))))
569 (defun eww-display-image (buffer)
570 (let ((data (shr-parse-image-data)))
571 (unless (buffer-live-p buffer
)
572 (error "Buffer %s doesn't exist" buffer
))
573 (with-current-buffer buffer
574 (let ((inhibit-read-only t
))
576 (shr-put-image data nil
))
577 (goto-char (point-min)))))
579 (declare-function mailcap-view-mime
"mailcap" (type))
580 (defun eww-display-pdf ()
581 (let ((data (buffer-substring (point) (point-max))))
582 (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
583 (let ((coding-system-for-write 'raw-text
)
584 (inhibit-read-only t
))
587 (mailcap-view-mime "application/pdf")))
588 (goto-char (point-min)))
590 (defun eww-setup-buffer ()
591 (when (or (plist-get eww-data
:url
)
592 (plist-get eww-data
:dom
))
594 (let ((inhibit-read-only t
))
597 (setq bidi-paragraph-direction nil
)
598 (unless (eq major-mode
'eww-mode
)
601 (defun eww-current-url nil
602 "Return URI of the Web page the current EWW buffer is visiting."
603 (plist-get eww-data
:url
))
605 (defun eww-links-at-point ()
606 "Return list of URIs, if any, linked at point."
608 (list (get-text-property (point) 'shr-url
)
609 (get-text-property (point) 'image-url
))))
611 (defun eww-view-source ()
612 "View the HTML source code of the current page."
614 (let ((buf (get-buffer-create "*eww-source*"))
615 (source (plist-get eww-data
:source
)))
616 (with-current-buffer buf
617 (let ((inhibit-read-only t
))
618 (delete-region (point-min) (point-max))
619 (insert (or source
"no source"))
620 (goto-char (point-min))
621 ;; Decode the source and set the buffer's encoding according
622 ;; to what the HTML source specifies in its 'charset' header,
624 (let ((cs (find-auto-coding "" (point-max))))
627 (when (coding-system-p cs
)
628 (decode-coding-region (point-min) (point-max) cs
)
629 (setq buffer-file-coding-system last-coding-system-used
))))
630 (when (fboundp 'html-mode
)
634 (defun eww-toggle-paragraph-direction ()
635 "Cycle the paragraph direction between left-to-right, right-to-left and auto."
637 (setq bidi-paragraph-direction
638 (cond ((eq bidi-paragraph-direction
'left-to-right
)
640 ((eq bidi-paragraph-direction
'right-to-left
)
644 (message "The paragraph direction is now %s"
645 (if (null bidi-paragraph-direction
)
647 bidi-paragraph-direction
)))
649 (defun eww-readable ()
650 "View the main \"readable\" parts of the current web page.
651 This command uses heuristics to find the parts of the web page that
652 contains the main textual portion, leaving out navigation menus and
655 (let* ((old-data eww-data
)
656 (dom (with-temp-buffer
657 (insert (plist-get old-data
:source
))
659 (decode-coding-region (point-min) (point-max) 'utf-8
)
660 (coding-system-error nil
))
661 (libxml-parse-html-region (point-min) (point-max))))
662 (base (plist-get eww-data
:url
)))
663 (eww-score-readability dom
)
665 (eww-display-html nil nil
666 (list 'base
(list (cons 'href base
))
667 (eww-highest-readability dom
))
668 nil
(current-buffer))
669 (dolist (elem '(:source
:url
:title
:next
:previous
:up
))
670 (plist-put eww-data elem
(plist-get old-data elem
)))
671 (eww-update-header-line-format)))
673 (defun eww-score-readability (node)
676 ((memq (dom-tag node
) '(script head comment
))
678 ((eq (dom-tag node
) 'meta
)
680 ((eq (dom-tag node
) 'img
)
682 ((eq (dom-tag node
) 'a
)
683 (setq score
(- (length (split-string (dom-text node
))))))
685 (dolist (elem (dom-children node
))
688 (setq score
(+ score
(length (split-string elem
)))))
691 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
692 (eww-score-readability elem
)))))))))
693 ;; Cache the score of the node to avoid recomputing all the time.
694 (dom-set-attribute node
:eww-readability-score score
)
697 (defun eww-highest-readability (node)
700 (dolist (elem (dom-non-text-children node
))
701 (when (> (or (dom-attr
702 (setq highest
(eww-highest-readability elem
))
703 :eww-readability-score
)
704 most-negative-fixnum
)
705 (or (dom-attr result
:eww-readability-score
)
706 most-negative-fixnum
))
707 (setq result highest
)))
711 (let ((map (make-sparse-keymap)))
712 (define-key map
"g" 'eww-reload
) ;FIXME: revert-buffer-function instead!
713 (define-key map
"G" 'eww
)
714 (define-key map
[?\M-
\r] 'eww-open-in-new-buffer
)
715 (define-key map
[?
\t] 'shr-next-link
)
716 (define-key map
[?\M-
\t] 'shr-previous-link
)
717 (define-key map
[backtab] 'shr-previous-link)
718 (define-key map [delete] 'scroll-down-command)
719 (define-key map "l" 'eww-back-url)
720 (define-key map "r" 'eww-forward-url)
721 (define-key map "n" 'eww-next-url)
722 (define-key map "p" 'eww-previous-url)
723 (define-key map "u" 'eww-up-url)
724 (define-key map "t" 'eww-top-url)
725 (define-key map "&" 'eww-browse-with-external-browser)
726 (define-key map "d" 'eww-download)
727 (define-key map "w" 'eww-copy-page-url)
728 (define-key map "C" 'url-cookie-list)
729 (define-key map "v" 'eww-view-source)
730 (define-key map "R" 'eww-readable)
731 (define-key map "H" 'eww-list-histories)
732 (define-key map "E" 'eww-set-character-encoding)
733 (define-key map "s" 'eww-switch-to-buffer)
734 (define-key map "S" 'eww-list-buffers)
735 (define-key map "F" 'eww-toggle-fonts)
736 (define-key map "D" 'eww-toggle-paragraph-direction)
737 (define-key map [(meta C)] 'eww-toggle-colors)
739 (define-key map "b" 'eww-add-bookmark)
740 (define-key map "B" 'eww-list-bookmarks)
741 (define-key map [(meta n)] 'eww-next-bookmark)
742 (define-key map [(meta p)] 'eww-previous-bookmark)
744 (easy-menu-define nil map ""
746 ["Exit" quit-window t]
747 ["Close browser" quit-window t]
748 ["Reload" eww-reload t]
749 ["Follow URL in new buffer" eww-open-in-new-buffer]
750 ["Back to previous page" eww-back-url
751 :active (not (zerop (length eww-history)))]
752 ["Forward to next page" eww-forward-url
753 :active (not (zerop eww-history-position))]
754 ["Browse with external browser" eww-browse-with-external-browser t]
755 ["Download" eww-download t]
756 ["View page source" eww-view-source]
757 ["Copy page URL" eww-copy-page-url t]
758 ["List histories" eww-list-histories t]
759 ["Switch to buffer" eww-switch-to-buffer t]
760 ["List buffers" eww-list-buffers t]
761 ["Add bookmark" eww-add-bookmark t]
762 ["List bookmarks" eww-list-bookmarks t]
763 ["List cookies" url-cookie-list t]
764 ["Toggle fonts" eww-toggle-fonts t]
765 ["Toggle colors" eww-toggle-colors t]
766 ["Character Encoding" eww-set-character-encoding]
767 ["Toggle Paragraph Direction" eww-toggle-paragraph-direction]))
770 (defvar eww-tool-bar-map
771 (let ((map (make-sparse-keymap)))
772 (dolist (tool-bar-item
773 '((quit-window . "close")
774 (eww-reload . "refresh")
775 (eww-back-url . "left-arrow")
776 (eww-forward-url . "right-arrow")
777 (eww-view-source . "show")
778 (eww-copy-page-url . "copy")
779 (eww-add-bookmark . "bookmark_add"))) ;; ...
780 (tool-bar-local-item-from-menu
781 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
783 "Tool bar for `eww-mode'.")
785 ;; Autoload cookie needed by desktop.el.
787 (define-derived-mode eww-mode special-mode "eww"
788 "Mode for browsing the web."
789 (setq-local eww-data (list :title ""))
790 (setq-local browse-url-browser-function #'eww-browse-url)
791 (add-hook 'after-change-functions #'eww-process-text-input nil t)
792 (setq-local eww-history nil)
793 (setq-local eww-history-position 0)
794 (when (boundp 'tool-bar-map)
795 (setq-local tool-bar-map eww-tool-bar-map))
797 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
798 ;; multi-page isearch support
799 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
800 (setq truncate-lines t)
801 (buffer-disable-undo)
802 (setq buffer-read-only t))
805 (defun eww-browse-url (url &optional new-window)
807 (pop-to-buffer-same-window (generate-new-buffer "*eww*"))
811 (defun eww-back-url ()
812 "Go to the previously displayed page."
814 (when (>= eww-history-position (length eww-history))
815 (user-error "No previous page"))
817 (setq eww-history-position (+ eww-history-position 2))
818 (eww-restore-history (elt eww-history (1- eww-history-position))))
820 (defun eww-forward-url ()
821 "Go to the next displayed page."
823 (when (zerop eww-history-position)
824 (user-error "No next page"))
826 (eww-restore-history (elt eww-history (1- eww-history-position))))
828 (defun eww-restore-history (elem)
829 (let ((inhibit-read-only t)
830 (inhibit-modification-hooks t)
831 (text (plist-get elem :text)))
834 (eww-reload) ; FIXME: restore :point?
837 (goto-char (plist-get elem :point))
838 (eww-update-header-line-format))))
840 (defun eww-next-url ()
841 "Go to the page marked `next'.
842 A page is marked `next' if rel=\"next\" appears in a <link>
845 (if (plist-get eww-data :next)
846 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
847 (plist-get eww-data :url)))
848 (user-error "No `next' on this page")))
850 (defun eww-previous-url ()
851 "Go to the page marked `previous'.
852 A page is marked `previous' if rel=\"previous\" appears in a <link>
855 (if (plist-get eww-data :previous)
856 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
857 (plist-get eww-data :url)))
858 (user-error "No `previous' on this page")))
861 "Go to the page marked `up'.
862 A page is marked `up' if rel=\"up\" appears in a <link>
865 (if (plist-get eww-data :up)
866 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
867 (plist-get eww-data :url)))
868 (user-error "No `up' on this page")))
870 (defun eww-top-url ()
871 "Go to the page marked `top'.
872 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
873 appears in a <link> or <a> tag."
875 (let ((best-url (or (plist-get eww-data :start)
876 (plist-get eww-data :contents)
877 (plist-get eww-data :home))))
879 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
880 (user-error "No `top' for this page"))))
882 (defun eww-reload (&optional local encode)
883 "Reload the current page.
884 If LOCAL (the command prefix), don't reload the page from the
885 network, but just re-display the HTML already fetched."
887 (let ((url (plist-get eww-data :url)))
889 (if (null (plist-get eww-data :dom))
890 (error "No current HTML data")
891 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
892 (point) (current-buffer)))
893 (url-retrieve url 'eww-render
894 (list url (point) (current-buffer) encode)))))
898 (defvar eww-form nil)
900 (defvar eww-submit-map
901 (let ((map (make-sparse-keymap)))
902 (define-key map "\r" 'eww-submit)
903 (define-key map [(control c) (control c)] 'eww-submit)
906 (defvar eww-submit-file
907 (let ((map (make-sparse-keymap)))
908 (define-key map "\r" 'eww-select-file)
909 (define-key map [(control c) (control c)] 'eww-submit)
912 (defvar eww-checkbox-map
913 (let ((map (make-sparse-keymap)))
914 (define-key map " " 'eww-toggle-checkbox)
915 (define-key map "\r" 'eww-toggle-checkbox)
916 (define-key map [(control c) (control c)] 'eww-submit)
920 (let ((map (make-keymap)))
921 (set-keymap-parent map text-mode-map)
922 (define-key map "\r" 'eww-submit)
923 (define-key map [(control a)] 'eww-beginning-of-text)
924 (define-key map [(control c) (control c)] 'eww-submit)
925 (define-key map [(control e)] 'eww-end-of-text)
926 (define-key map [?\t] 'shr-next-link)
927 (define-key map [?\M-\t] 'shr-previous-link)
930 (defvar eww-textarea-map
931 (let ((map (make-keymap)))
932 (set-keymap-parent map text-mode-map)
933 (define-key map "\r" 'forward-line)
934 (define-key map [(control c) (control c)] 'eww-submit)
935 (define-key map [?\t] 'shr-next-link)
936 (define-key map [?\M-\t] 'shr-previous-link)
939 (defvar eww-select-map
940 (let ((map (make-sparse-keymap)))
941 (define-key map "\r" 'eww-change-select)
942 (define-key map [(control c) (control c)] 'eww-submit)
945 (defun eww-beginning-of-text ()
946 "Move to the start of the input field."
948 (goto-char (eww-beginning-of-field)))
950 (defun eww-end-of-text ()
951 "Move to the end of the text in the input field."
953 (goto-char (eww-end-of-field))
954 (let ((start (eww-beginning-of-field)))
955 (while (and (equal (following-char) ? )
958 (when (> (point) start)
961 (defun eww-beginning-of-field ()
965 ((not (eq (get-text-property (point) 'eww-form)
966 (get-text-property (1- (point)) 'eww-form)))
969 (previous-single-property-change
970 (point) 'eww-form nil (point-min)))))
972 (defun eww-end-of-field ()
973 (1- (next-single-property-change
974 (point) 'eww-form nil (point-max))))
976 (defun eww-tag-form (dom)
977 (let ((eww-form (list (cons :method (dom-attr dom 'method))
978 (cons :action (dom-attr dom 'action))))
981 (shr-ensure-paragraph)
986 (when (> (point) start)
987 (put-text-property start (1+ start)
988 'eww-form eww-form))))
990 (defun eww-form-submit (dom)
991 (let ((start (point))
992 (value (dom-attr dom 'value)))
994 (if (zerop (length value))
998 (add-face-text-property start (point) 'eww-form-submit)
999 (put-text-property start (point) 'eww-form
1000 (list :eww-form eww-form
1003 :name (dom-attr dom 'name)))
1004 (put-text-property start (point) 'keymap eww-submit-map)
1007 (defun eww-form-checkbox (dom)
1008 (let ((start (point)))
1009 (if (dom-attr dom 'checked)
1010 (insert eww-form-checkbox-selected-symbol)
1011 (insert eww-form-checkbox-symbol))
1012 (add-face-text-property start (point) 'eww-form-checkbox)
1013 (put-text-property start (point) 'eww-form
1014 (list :eww-form eww-form
1015 :value (dom-attr dom 'value)
1016 :type (downcase (dom-attr dom 'type))
1017 :checked (dom-attr dom 'checked)
1018 :name (dom-attr dom 'name)))
1019 (put-text-property start (point) 'keymap eww-checkbox-map)
1022 (defun eww-form-file (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-file)
1032 (put-text-property start (point) 'eww-form
1033 (list :eww-form eww-form
1034 :value (dom-attr dom 'value)
1035 :type (downcase (dom-attr dom 'type))
1036 :name (dom-attr dom 'name)))
1037 (put-text-property start (point) 'keymap eww-submit-file)
1040 (defun eww-select-file ()
1041 "Change the value of the upload file menu under point."
1043 (let* ((input (get-text-property (point) 'eww-form)))
1045 (let ((insert-default-directory t))
1046 (read-file-name "filename: "))))
1047 (eww-update-field filename (length "Browse"))
1048 (plist-put input :filename filename))))
1050 (defun eww-form-text (dom)
1051 (let ((start (point))
1052 (type (downcase (or (dom-attr dom 'type) "text")))
1053 (value (or (dom-attr dom 'value) ""))
1054 (width (string-to-number (or (dom-attr dom 'size) "40")))
1055 (readonly-property (if (or (dom-attr dom 'disabled)
1056 (dom-attr dom 'readonly))
1058 'inhibit-read-only)))
1060 (when (< (length value) width)
1061 (insert (make-string (- width (length value)) ? )))
1062 (put-text-property start (point) 'face 'eww-form-text)
1063 (put-text-property start (point) 'inhibit-read-only t)
1064 (put-text-property start (point) 'local-map eww-text-map)
1065 (put-text-property start (point) readonly-property t)
1066 (put-text-property start (point) 'eww-form
1067 (list :eww-form eww-form
1070 :name (dom-attr dom 'name)))
1073 (defconst eww-text-input-types '("text" "password" "textarea"
1074 "color" "date" "datetime" "datetime-local"
1075 "email" "month" "number" "search" "tel"
1076 "time" "url" "week")
1077 "List of input types which represent a text input.
1078 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1080 (defun eww-process-text-input (beg end replace-length)
1081 (when-let (pos (and (< (1+ end) (point-max))
1082 (> (1- end) (point-min))
1084 ((get-text-property (1+ end) 'eww-form)
1086 ((get-text-property (1- end) 'eww-form)
1088 (let* ((form (get-text-property pos 'eww-form))
1089 (properties (text-properties-at pos))
1090 (buffer-undo-list t)
1091 (inhibit-read-only t)
1092 (length (- end beg replace-length))
1093 (type (plist-get form :type)))
1095 (member type eww-text-input-types))
1098 ;; Delete some space at the end.
1101 (if (equal type "textarea")
1102 (1- (line-end-position))
1103 (eww-end-of-field)))
1104 (while (and (> length 0)
1105 (eql (char-after (1- (point))) ? ))
1106 (delete-region (1- (point)) (point))
1113 (if (equal type "textarea")
1114 (1- (line-end-position))
1115 (1+ (eww-end-of-field))))
1116 (let ((start (point)))
1117 (insert (make-string (abs length) ? ))
1118 (set-text-properties start (point) properties))
1119 (goto-char (1- end)))))
1120 (set-text-properties (plist-get form :start) (plist-get form :end)
1122 (let ((value (buffer-substring-no-properties
1123 (eww-beginning-of-field)
1124 (eww-end-of-field))))
1125 (when (string-match " +\\'" value)
1126 (setq value (substring value 0 (match-beginning 0))))
1127 (plist-put form :value value)
1128 (when (equal type "password")
1129 ;; Display passwords as asterisks.
1130 (let ((start (eww-beginning-of-field)))
1132 start (+ start (length value))
1133 'display (make-string (length value) ?*)))))))))
1135 (defun eww-tag-textarea (dom)
1136 (let ((start (point))
1137 (value (or (dom-attr dom 'value) ""))
1138 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1139 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1141 (shr-ensure-newline)
1143 (shr-ensure-newline)
1144 (when (< (count-lines start (point)) lines)
1145 (dotimes (_ (- lines (count-lines start (point))))
1147 (setq end (point-marker))
1149 (while (< (point) end)
1151 (let ((pad (- width (- (point) (line-beginning-position)))))
1153 (insert (make-string pad ? ))))
1154 (add-face-text-property (line-beginning-position)
1155 (point) 'eww-form-textarea)
1156 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1157 (put-text-property (line-beginning-position) (point)
1158 'local-map eww-textarea-map)
1160 (put-text-property start (point) 'eww-form
1161 (list :eww-form eww-form
1164 :name (dom-attr dom 'name)))))
1166 (defun eww-tag-input (dom)
1167 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1170 ((or (equal type "checkbox")
1171 (equal type "radio"))
1172 (eww-form-checkbox dom))
1173 ((equal type "file")
1174 (eww-form-file dom))
1175 ((equal type "submit")
1176 (eww-form-submit dom))
1177 ((equal type "hidden")
1178 (let ((form eww-form)
1179 (name (dom-attr dom 'name)))
1180 ;; Don't add <input type=hidden> elements repeatedly.
1182 (or (not (consp (car form)))
1183 (not (eq (caar form) 'hidden))
1184 (not (equal (plist-get (cdr (car form)) :name)
1186 (setq form (cdr form)))
1188 (nconc eww-form (list
1191 :value (or (dom-attr dom 'value) "")))))))
1193 (eww-form-text dom)))
1194 (unless (= start (point))
1195 (put-text-property start (1+ start) 'help-echo "Input field")
1196 ;; Mark this as an element we can TAB to.
1197 (put-text-property start (1+ start) 'shr-url dom))))
1199 (defun eww-tag-select (dom)
1200 (shr-ensure-paragraph)
1201 (let ((menu (list :name (dom-attr dom 'name)
1202 :eww-form eww-form))
1207 (if (eq (dom-tag dom) 'optgroup)
1208 (dolist (groupelem (dom-children dom))
1209 (unless (dom-attr groupelem 'disabled)
1210 (setq opelem (append opelem (list groupelem)))))
1211 (setq opelem (list dom)))
1212 (dolist (elem opelem)
1213 (when (eq (dom-tag elem) 'option)
1214 (when (dom-attr elem 'selected)
1215 (nconc menu (list :value (dom-attr elem 'value))))
1216 (let ((display (dom-text elem)))
1217 (setq max (max max (length display)))
1219 :value (dom-attr elem 'value)
1223 (setq options (nreverse options))
1224 ;; If we have no selected values, default to the first value.
1225 (unless (plist-get menu :value)
1226 (nconc menu (list :value (nth 2 (car options)))))
1227 (nconc menu options)
1228 (let ((selected (eww-select-display menu)))
1230 (make-string (- max (length selected)) ? )))
1231 (put-text-property start (point) 'eww-form menu)
1232 (add-face-text-property start (point) 'eww-form-select)
1233 (put-text-property start (point) 'keymap eww-select-map)
1234 (unless (= start (point))
1235 (put-text-property start (1+ start) 'help-echo "select field"))
1236 (shr-ensure-paragraph))))
1238 (defun eww-select-display (select)
1239 (let ((value (plist-get select :value))
1241 (dolist (elem select)
1242 (when (and (consp elem)
1243 (eq (car elem) 'item)
1244 (equal value (plist-get (cdr elem) :value)))
1245 (setq display (plist-get (cdr elem) :display))))
1248 (defun eww-change-select ()
1249 "Change the value of the select drop-down menu under point."
1251 (let* ((input (get-text-property (point) 'eww-form))
1252 (completion-ignore-case t)
1255 (mapcar (lambda (elem)
1257 (eq (car elem) 'item)
1258 (cons (plist-get (cdr elem) :display)
1259 (plist-get (cdr elem) :value))))
1262 (completing-read "Change value: " options nil 'require-match))
1263 (inhibit-read-only t))
1264 (plist-put input :value (cdr (assoc-string display options t)))
1266 (eww-update-field display))))
1268 (defun eww-update-field (string &optional offset)
1271 (let ((properties (text-properties-at (point)))
1272 (start (+ (eww-beginning-of-field) offset))
1273 (current-end (1+ (eww-end-of-field)))
1274 (new-end (+ (eww-beginning-of-field) (length string)))
1275 (inhibit-read-only t))
1276 (delete-region start current-end)
1277 (forward-char offset)
1279 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1281 (set-text-properties start new-end properties))
1284 (defun eww-toggle-checkbox ()
1285 "Toggle the value of the checkbox under point."
1287 (let* ((input (get-text-property (point) 'eww-form))
1288 (type (plist-get input :type)))
1289 (if (equal type "checkbox")
1292 (if (plist-get input :checked)
1294 (plist-put input :checked nil)
1295 (eww-update-field eww-form-checkbox-symbol))
1296 (plist-put input :checked t)
1297 (eww-update-field eww-form-checkbox-selected-symbol))))
1298 ;; Radio button. Switch all other buttons off.
1299 (let ((name (plist-get input :name)))
1301 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1302 (when (equal (plist-get (cdr elem) :name) name)
1303 (goto-char (car elem))
1304 (if (not (eq (cdr elem) input))
1306 (plist-put input :checked nil)
1307 (eww-update-field eww-form-checkbox-symbol))
1308 (plist-put input :checked t)
1309 (eww-update-field eww-form-checkbox-selected-symbol)))))
1310 (forward-char 1)))))
1312 (defun eww-inputs (form)
1313 (let ((start (point-min))
1316 (< start (point-max)))
1317 (when (or (get-text-property start 'eww-form)
1318 (setq start (next-single-property-change start 'eww-form)))
1319 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1321 (push (cons start (get-text-property start 'eww-form))
1323 (setq start (next-single-property-change start 'eww-form))))
1326 (defun eww-size-text-inputs ()
1327 (let ((start (point-min)))
1329 (< start (point-max)))
1330 (when (or (get-text-property start 'eww-form)
1331 (setq start (next-single-property-change start 'eww-form)))
1332 (let ((props (get-text-property start 'eww-form)))
1333 (plist-put props :start start)
1334 (setq start (next-single-property-change
1335 start 'eww-form nil (point-max)))
1336 (plist-put props :end start))))))
1338 (defun eww-input-value (input)
1339 (let ((type (plist-get input :type))
1340 (value (plist-get input :value)))
1342 ((equal type "textarea")
1345 (goto-char (point-min))
1346 (while (re-search-forward "^ +\n\\| +$" nil t)
1347 (replace-match "" t t))
1350 (if (string-match " +\\'" value)
1351 (substring value 0 (match-beginning 0))
1354 (defun eww-submit ()
1355 "Submit the current form."
1357 (let* ((this-input (get-text-property (point) 'eww-form))
1358 (form (plist-get this-input :eww-form))
1360 (dolist (elem (sort (eww-inputs form)
1362 (< (car o1) (car o2)))))
1363 (let* ((input (cdr elem))
1364 (input-start (car elem))
1365 (name (plist-get input :name)))
1368 ((member (plist-get input :type) '("checkbox" "radio"))
1369 (when (plist-get input :checked)
1370 (push (cons name (plist-get input :value))
1372 ((equal (plist-get input :type) "file")
1374 (list (cons "filedata"
1376 (insert-file-contents
1377 (plist-get input :filename))
1379 (cons "name" (plist-get input :name))
1380 (cons "filename" (plist-get input :filename))))
1382 ((equal (plist-get input :type) "submit")
1383 ;; We want the values from buttons if we hit a button if
1384 ;; we hit enter on it, or if it's the first button after
1385 ;; the field we did hit return on.
1386 (when (or (eq input this-input)
1387 (and (not (eq input this-input))
1389 (> input-start (point))))
1390 (setq next-submit t)
1391 (push (cons name (plist-get input :value))
1394 (push (cons name (eww-input-value input))
1397 (when (and (consp elem)
1398 (eq (car elem) 'hidden))
1399 (push (cons (plist-get (cdr elem) :name)
1400 (or (plist-get (cdr elem) :value) ""))
1402 (if (and (stringp (cdr (assq :method form)))
1403 (equal (downcase (cdr (assq :method form))) "post"))
1405 (dolist (x values mtype)
1406 (if (equal (car x) "file")
1408 (setq mtype "multipart/form-data"))))
1409 (cond ((equal mtype "multipart/form-data")
1410 (let ((boundary (mml-compute-boundary '())))
1411 (let ((url-request-method "POST")
1412 (url-request-extra-headers
1413 (list (cons "Content-Type"
1414 (concat "multipart/form-data; boundary="
1417 (mm-url-encode-multipart-form-data values boundary)))
1418 (eww-browse-url (shr-expand-url
1419 (cdr (assq :action form))
1420 (plist-get eww-data :url))))))
1422 (let ((url-request-method "POST")
1423 (url-request-extra-headers
1425 "application/x-www-form-urlencoded")))
1427 (mm-url-encode-www-form-urlencoded values)))
1428 (eww-browse-url (shr-expand-url
1429 (cdr (assq :action form))
1430 (plist-get eww-data :url)))))))
1433 (if (cdr (assq :action form))
1434 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1435 (plist-get eww-data :url))
1437 (mm-url-encode-www-form-urlencoded values))))))
1439 (defun eww-browse-with-external-browser (&optional url)
1440 "Browse the current URL with an external browser.
1441 The browser to used is specified by the `shr-external-browser' variable."
1443 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1445 (defun eww-follow-link (&optional external mouse-event)
1446 "Browse the URL under point.
1447 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1448 If EXTERNAL is double prefix, browse in new buffer."
1449 (interactive (list current-prefix-arg last-nonmenu-event))
1450 (mouse-set-point mouse-event)
1451 (let ((url (get-text-property (point) 'shr-url)))
1454 (message "No link under point"))
1455 ((string-match "^mailto:" url)
1456 (browse-url-mail url))
1457 ((and (consp external) (<= (car external) 4))
1458 (funcall shr-external-browser url))
1459 ;; This is a #target url in the same page as the current one.
1460 ((and (url-target (url-generic-parse-url url))
1461 (eww-same-page-p url (plist-get eww-data :url)))
1462 (let ((dom (plist-get eww-data :dom)))
1464 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1466 (eww-browse-url url external)))))
1468 (defun eww-same-page-p (url1 url2)
1469 "Return non-nil if URL1 and URL2 represent the same page.
1470 Differences in #targets are ignored."
1471 (let ((obj1 (url-generic-parse-url url1))
1472 (obj2 (url-generic-parse-url url2)))
1473 (setf (url-target obj1) nil)
1474 (setf (url-target obj2) nil)
1475 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1477 (defun eww-copy-page-url ()
1478 "Copy the URL of the current page into the kill ring."
1480 (message "%s" (plist-get eww-data :url))
1481 (kill-new (plist-get eww-data :url)))
1483 (defun eww-download ()
1484 "Download URL under point to `eww-download-directory'."
1486 (let ((url (get-text-property (point) 'shr-url)))
1488 (message "No URL under point")
1489 (url-retrieve url 'eww-download-callback (list url)))))
1491 (defun eww-download-callback (status url)
1492 (unless (plist-get status :error)
1493 (let* ((obj (url-generic-parse-url url))
1494 (path (car (url-path-and-query obj)))
1495 (file (eww-make-unique-file-name
1496 (eww-decode-url-file-name (file-name-nondirectory path))
1497 eww-download-directory)))
1498 (goto-char (point-min))
1499 (re-search-forward "\r?\n\r?\n")
1500 (write-region (point) (point-max) file)
1501 (message "Saved %s" file))))
1503 (defun eww-decode-url-file-name (string)
1504 (let* ((binary (url-unhex-string string))
1506 (decode-coding-string
1508 ;; Possibly set by `universal-coding-system-argument'.
1509 (or coding-system-for-read
1510 ;; RFC 3986 says that %AB stuff is utf-8.
1511 (if (equal (decode-coding-string binary 'utf-8)
1515 (car (detect-coding-string binary))))))
1516 (encodes (find-coding-systems-string decoded)))
1517 (if (or (equal encodes '(undecided))
1518 (memq (coding-system-base (or file-name-coding-system
1519 default-file-name-coding-system))
1522 ;; If we can't encode the decoded file name (due to language
1523 ;; environment settings), then we return the original, hexified
1527 (defun eww-make-unique-file-name (file directory)
1529 ((zerop (length file))
1531 ((string-match "\\`[.]" file)
1532 (setq file (concat "!" file))))
1536 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1537 (setq stem (match-string 1 file)
1538 suffix (match-string 2)))
1539 (while (file-exists-p (expand-file-name file directory))
1540 (setq file (format "%s(%d)%s" stem count suffix))
1541 (setq count (1+ count)))
1542 (expand-file-name file directory)))
1544 (defun eww-set-character-encoding (charset)
1545 "Set character encoding to CHARSET.
1546 If CHARSET is nil then use UTF-8."
1547 (interactive "zUse character set (default utf-8): ")
1549 (eww-reload nil 'utf-8)
1550 (eww-reload nil charset)))
1552 (defun eww-switch-to-buffer ()
1553 "Prompt for an EWW buffer to display in the selected window."
1555 (let ((completion-extra-properties
1556 '(:annotation-function (lambda (buf)
1557 (with-current-buffer buf
1558 (format " %s" (eww-current-url)))))))
1559 (pop-to-buffer-same-window
1560 (read-buffer "Switch to EWW buffer: "
1561 (cl-loop for buf in (nreverse (buffer-list))
1562 if (with-current-buffer buf (derived-mode-p 'eww-mode))
1566 (with-current-buffer
1567 (if (consp bufn) (cdr bufn) (get-buffer bufn))
1568 (derived-mode-p 'eww-mode)))))))
1570 (defun eww-toggle-fonts ()
1571 "Toggle whether to use monospaced or font-enabled layouts."
1573 (setq shr-use-fonts (not shr-use-fonts))
1575 (message "Proportional fonts are now %s"
1576 (if shr-use-fonts "on" "off")))
1578 (defun eww-toggle-colors ()
1579 "Toggle whether to use HTML-specified colors or not."
1581 (message "Colors are now %s"
1582 (if (setq shr-use-colors (not shr-use-colors))
1589 (defvar eww-bookmarks nil)
1591 (defun eww-add-bookmark ()
1592 "Bookmark the current page."
1594 (eww-read-bookmarks)
1595 (dolist (bookmark eww-bookmarks)
1596 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1597 (user-error "Already bookmarked")))
1598 (when (y-or-n-p "Bookmark this page?")
1599 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1600 (plist-get eww-data :title))))
1601 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1602 (push (list :url (plist-get eww-data :url)
1604 :time (current-time-string))
1606 (eww-write-bookmarks)
1607 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1608 (plist-get eww-data :title))))
1610 (defun eww-write-bookmarks ()
1611 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1612 (insert ";; Auto-generated file; don't edit\n")
1613 (pp eww-bookmarks (current-buffer))))
1615 (defun eww-read-bookmarks ()
1616 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1618 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1620 (insert-file-contents file)
1621 (read (current-buffer)))))))
1624 (defun eww-list-bookmarks ()
1625 "Display the bookmarks."
1627 (pop-to-buffer "*eww bookmarks*")
1628 (eww-bookmark-prepare))
1630 (defun eww-bookmark-prepare ()
1631 (eww-read-bookmarks)
1632 (unless eww-bookmarks
1633 (user-error "No bookmarks are defined"))
1634 (set-buffer (get-buffer-create "*eww bookmarks*"))
1636 (let* ((width (/ (window-width) 2))
1637 (format (format "%%-%ds %%s" width))
1638 (inhibit-read-only t)
1641 (setq header-line-format (concat " " (format format "Title" "URL")))
1642 (dolist (bookmark eww-bookmarks)
1644 title (plist-get bookmark :title))
1645 (when (> (length title) width)
1646 (setq title (truncate-string-to-width title width)))
1647 (insert (format format title (plist-get bookmark :url)) "\n")
1648 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1649 (goto-char (point-min))))
1651 (defvar eww-bookmark-kill-ring nil)
1653 (defun eww-bookmark-kill ()
1654 "Kill the current bookmark."
1656 (let* ((start (line-beginning-position))
1657 (bookmark (get-text-property start 'eww-bookmark))
1658 (inhibit-read-only t))
1660 (user-error "No bookmark on the current line"))
1662 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1663 (delete-region start (point))
1664 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1665 (eww-write-bookmarks)))
1667 (defun eww-bookmark-yank ()
1668 "Yank a previously killed bookmark to the current line."
1670 (unless eww-bookmark-kill-ring
1671 (user-error "No previously killed bookmark"))
1673 (let ((inhibit-read-only t)
1676 (insert (pop eww-bookmark-kill-ring))
1677 (setq bookmark (get-text-property start 'eww-bookmark))
1678 (if (= start (point-min))
1679 (push bookmark eww-bookmarks)
1680 (let ((line (count-lines start (point))))
1681 (setcdr (nthcdr (1- line) eww-bookmarks)
1682 (cons bookmark (nthcdr line eww-bookmarks)))))
1683 (eww-write-bookmarks)))
1685 (defun eww-bookmark-browse ()
1686 "Browse the bookmark under point in eww."
1688 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1690 (user-error "No bookmark on the current line"))
1692 (eww-browse-url (plist-get bookmark :url))))
1694 (defun eww-next-bookmark ()
1695 "Go to the next bookmark in the list."
1699 (unless (get-buffer "*eww bookmarks*")
1701 (eww-bookmark-prepare))
1702 (with-current-buffer (get-buffer "*eww bookmarks*")
1703 (when (and (not first)
1706 (setq bookmark (get-text-property (line-beginning-position)
1709 (user-error "No next bookmark")))
1710 (eww-browse-url (plist-get bookmark :url))))
1712 (defun eww-previous-bookmark ()
1713 "Go to the previous bookmark in the list."
1717 (unless (get-buffer "*eww bookmarks*")
1719 (eww-bookmark-prepare))
1720 (with-current-buffer (get-buffer "*eww bookmarks*")
1722 (goto-char (point-max))
1723 (beginning-of-line))
1724 ;; On the final line.
1728 (user-error "No previous bookmark")
1730 (setq bookmark (get-text-property (line-beginning-position)
1732 (eww-browse-url (plist-get bookmark :url))))
1734 (defvar eww-bookmark-mode-map
1735 (let ((map (make-sparse-keymap)))
1736 (define-key map [(control k)] 'eww-bookmark-kill)
1737 (define-key map [(control y)] 'eww-bookmark-yank)
1738 (define-key map "\r" 'eww-bookmark-browse)
1740 (easy-menu-define nil map
1741 "Menu for `eww-bookmark-mode-map'."
1743 ["Exit" quit-window t]
1744 ["Browse" eww-bookmark-browse
1745 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1746 ["Kill" eww-bookmark-kill
1747 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1748 ["Yank" eww-bookmark-yank
1749 :active eww-bookmark-kill-ring]))
1752 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1753 "Mode for listing bookmarks.
1755 \\{eww-bookmark-mode-map}"
1756 (buffer-disable-undo)
1757 (setq truncate-lines t))
1761 (defun eww-save-history ()
1762 (plist-put eww-data :point (point))
1763 (plist-put eww-data :text (buffer-string))
1764 (push eww-data eww-history)
1765 (setq eww-data (list :title ""))
1766 ;; Don't let the history grow infinitely. We store quite a lot of
1768 (when-let (tail (and eww-history-limit
1769 (nthcdr eww-history-limit eww-history)))
1772 (defvar eww-current-buffer)
1774 (defun eww-list-histories ()
1775 "List the eww-histories."
1777 (when (null eww-history)
1778 (error "No eww-histories are defined"))
1779 (let ((eww-history-trans eww-history)
1780 (buffer (current-buffer)))
1781 (set-buffer (get-buffer-create "*eww history*"))
1783 (setq-local eww-current-buffer buffer)
1784 (let ((inhibit-read-only t)
1787 url title format start)
1789 (dolist (history eww-history-trans)
1790 (setq start (point))
1791 (setq domain-length (max domain-length (length (plist-get history :url))))
1792 (setq title-length (max title-length (length (plist-get history :title)))))
1793 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1795 (concat " " (format format "Title" "URL")))
1796 (dolist (history eww-history-trans)
1797 (setq start (point))
1798 (setq url (plist-get history :url))
1799 (setq title (plist-get history :title))
1800 (insert (format format title url))
1802 (put-text-property start (1+ start) 'eww-history history))
1803 (goto-char (point-min)))
1804 (pop-to-buffer "*eww history*")))
1806 (defun eww-history-browse ()
1807 "Browse the history under point in eww."
1809 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1811 (error "No history on the current line"))
1812 (let ((buffer eww-current-buffer))
1815 (pop-to-buffer-same-window buffer)))
1816 (eww-restore-history history)))
1818 (defvar eww-history-mode-map
1819 (let ((map (make-sparse-keymap)))
1820 (define-key map "\r" 'eww-history-browse)
1821 ;; (define-key map "n" 'next-error-no-select)
1822 ;; (define-key map "p" 'previous-error-no-select)
1824 (easy-menu-define nil map
1825 "Menu for `eww-history-mode-map'."
1827 ["Exit" quit-window t]
1828 ["Browse" eww-history-browse
1829 :active (get-text-property (line-beginning-position) 'eww-history)]))
1832 (define-derived-mode eww-history-mode special-mode "eww history"
1833 "Mode for listing eww-histories.
1835 \\{eww-history-mode-map}"
1836 (buffer-disable-undo)
1837 (setq truncate-lines t))
1839 ;;; eww buffers list
1841 (defun eww-list-buffers ()
1842 "Enlist eww buffers."
1845 (current (current-buffer)))
1846 (dolist (buffer (buffer-list))
1847 (with-current-buffer buffer
1848 (when (derived-mode-p 'eww-mode)
1849 (push (vector buffer (plist-get eww-data :title)
1850 (plist-get eww-data :url))
1852 (unless buffers-info
1853 (error "No eww buffers"))
1854 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1855 (set-buffer (get-buffer-create "*eww buffers*"))
1857 (let ((inhibit-read-only t)
1860 url title format start)
1862 (dolist (buffer-info buffers-info)
1863 (setq title-length (max title-length
1864 (length (elt buffer-info 1)))
1865 domain-length (max domain-length
1866 (length (elt buffer-info 2)))))
1867 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1869 (concat " " (format format "Title" "URL")))
1871 (current-buffer-line 1))
1872 (dolist (buffer-info buffers-info)
1874 title (elt buffer-info 1)
1875 url (elt buffer-info 2)
1877 (insert (format format title url))
1879 (let ((buffer (elt buffer-info 0)))
1880 (put-text-property start (1+ start) 'eww-buffer
1882 (when (eq current buffer)
1883 (setq current-buffer-line line))))
1884 (goto-char (point-min))
1885 (forward-line (1- current-buffer-line)))))
1886 (pop-to-buffer "*eww buffers*"))
1888 (defun eww-buffer-select ()
1889 "Switch to eww buffer."
1891 (let ((buffer (get-text-property (line-beginning-position)
1894 (error "No buffer on current line"))
1896 (pop-to-buffer-same-window buffer)))
1898 (defun eww-buffer-show ()
1899 "Display buffer under point in eww buffer list."
1900 (let ((buffer (get-text-property (line-beginning-position)
1903 (error "No buffer on current line"))
1905 (pop-to-buffer-same-window buffer)
1908 (defun eww-buffer-show-next ()
1909 "Move to next eww buffer in the list and display it."
1913 (goto-char (point-min)))
1916 (defun eww-buffer-show-previous ()
1917 "Move to previous eww buffer in the list and display it."
1921 (goto-char (point-max)))
1925 (defun eww-buffer-kill ()
1926 "Kill buffer from eww list."
1928 (let* ((start (line-beginning-position))
1929 (buffer (get-text-property start 'eww-buffer))
1930 (inhibit-read-only t))
1932 (user-error "No buffer on the current line"))
1933 (kill-buffer buffer)
1935 (delete-region start (point)))
1940 (defvar eww-buffers-mode-map
1941 (let ((map (make-sparse-keymap)))
1942 (define-key map [(control k)] 'eww-buffer-kill)
1943 (define-key map "\r" 'eww-buffer-select)
1944 (define-key map "n" 'eww-buffer-show-next)
1945 (define-key map "p" 'eww-buffer-show-previous)
1947 (easy-menu-define nil map
1948 "Menu for `eww-buffers-mode-map'."
1950 ["Exit" quit-window t]
1951 ["Select" eww-buffer-select
1952 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1953 ["Kill" eww-buffer-kill
1954 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1957 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1958 "Mode for listing buffers.
1960 \\{eww-buffers-mode-map}"
1961 (buffer-disable-undo)
1962 (setq truncate-lines t))
1966 (defvar eww-desktop-data-save
1967 '(:url :title :point)
1968 "List of `eww-data' properties to preserve in the desktop file.
1969 Also used when saving `eww-history'.")
1971 (defun eww-desktop-data-1 (alist)
1975 (let ((k (car tail))
1977 (when (memq k eww-desktop-data-save)
1978 (setq acc (cons k (cons v acc)))))
1979 (setq tail (cddr tail)))
1982 (defun eww-desktop-history-duplicate (a b)
1983 (let ((tail a) (r t))
1985 (if (or (memq (car tail) '(:point)) ; ignore :point
1987 (plist-get b (car tail))))
1988 (setq tail (cddr tail))
1994 (defun eww-desktop-misc-data (_directory)
1995 "Return a property list with data used to restore eww buffers.
1996 This list will contain, as :history, the list, whose first element is
1997 the value of `eww-data', and the tail is `eww-history'.
1999 If `eww-desktop-remove-duplicates' is non-nil, duplicate
2000 entries (if any) will be removed from the list.
2002 Only the properties listed in `eww-desktop-data-save' are included.
2003 Generally, the list should not include the (usually overly large)
2004 :dom, :source and :text properties."
2005 (let ((history (mapcar 'eww-desktop-data-1
2006 (cons eww-data eww-history))))
2007 (list :history (if eww-desktop-remove-duplicates
2008 (cl-remove-duplicates
2009 history :test 'eww-desktop-history-duplicate)
2012 (defun eww-restore-desktop (file-name buffer-name misc-data)
2013 "Restore an eww buffer from its desktop file record.
2014 If `eww-restore-desktop' is t or `auto', this function will also
2015 initiate the retrieval of the respective URI in the background.
2016 Otherwise, the restored buffer will contain a prompt to do so by using
2018 (with-current-buffer (get-buffer-create buffer-name)
2020 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
2021 (setq eww-history (cdr (plist-get misc-data :history))
2022 eww-data (or (car (plist-get misc-data :history))
2023 ;; backwards compatibility
2024 (list :url (plist-get misc-data :uri))))
2026 (when (plist-get eww-data :url)
2027 (case eww-restore-desktop
2028 ((t auto) (eww (plist-get eww-data :url)))
2029 ((zerop (buffer-size))
2030 (let ((inhibit-read-only t))
2031 (insert (substitute-command-keys
2032 eww-restore-reload-prompt)))))))
2036 (add-to-list 'desktop-locals-to-save
2037 'eww-history-position)
2038 (add-to-list 'desktop-buffer-mode-handlers
2039 '(eww-mode . eww-restore-desktop))
2043 (defun eww-isearch-next-buffer (&optional _buffer wrap)
2044 "Go to the next page to search using `rel' attribute for navigation."
2051 (eww-previous-url)))
2056 ;;; eww.el ends here