1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
32 (require 'url-util) ; for url-get-url-at-point
35 (eval-when-compile (require 'subr-x)) ;; for string-trim
40 :link '(custom-manual "(eww) Top")
44 (defcustom eww-header-line-format "%t: %u"
46 - %t is replaced by the title.
47 - %u is replaced by the URL."
52 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
53 "Prefix URL to search engine."
58 (defcustom eww-download-directory "~/Downloads/"
59 "Directory where files will downloaded."
65 (defcustom eww-suggest-uris
69 "List of functions called to form the list of default URIs for `eww'.
70 Each of the elements is a function returning either a string or a list
71 of strings. The results will be joined into a single list with
72 duplicate entries (if any) removed."
76 :options '(eww-links-at-point
80 (defcustom eww-bookmarks-directory user-emacs-directory
81 "Directory where bookmark files will be stored."
86 (defcustom eww-desktop-remove-duplicates t
87 "Whether to remove duplicates from the history when saving desktop data.
88 If non-nil, repetitive EWW history entries (comprising of the URI, the
89 title, and the point position) will not be saved as part of the Emacs
90 desktop. Otherwise, such entries will be retained."
95 (defcustom eww-restore-desktop nil
96 "How to restore EWW buffers on `desktop-restore'.
97 If t or `auto', the buffers will be reloaded automatically.
98 If nil, buffers will require manual reload, and will contain the text
99 specified in `eww-restore-reload-prompt' instead of the actual Web
103 :type '(choice (const :tag "Restore all automatically" t)
104 (const :tag "Require manual reload" nil)))
106 (defcustom eww-restore-reload-prompt
107 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
108 "The string to put in the buffers not reloaded on `desktop-restore'.
109 This prompt will be used if `eww-restore-desktop' is nil.
111 The string will be passed through `substitute-command-keys'."
116 (defcustom eww-history-limit 50
117 "Maximum number of entries to retain in the history."
120 :type '(choice (const :tag "Unlimited" nil)
123 (defcustom eww-use-external-browser-for-content-type
124 "\\`\\(video/\\|audio/\\|application/ogg\\)"
125 "Always use external browser for specified content-type."
128 :type '(choice (const :tag "Never" nil)
131 (defcustom eww-after-render-hook nil
132 "A hook called after eww has finished rendering the buffer."
137 (defcustom eww-form-checkbox-selected-symbol "[X]"
138 "Symbol used to represent a selected checkbox.
139 See also `eww-form-checkbox-symbol'."
142 :type '(choice (const "[X]")
143 (const "☒") ; Unicode BALLOT BOX WITH X
144 (const "☑") ; Unicode BALLOT BOX WITH CHECK
147 (defcustom eww-form-checkbox-symbol "[ ]"
148 "Symbol used to represent a checkbox.
149 See also `eww-form-checkbox-selected-symbol'."
152 :type '(choice (const "[ ]")
153 (const "☐") ; Unicode BALLOT BOX
156 (defface eww-form-submit
157 '((((type x w32 ns) (class color)) ; Like default mode line
158 :box (:line-width 2 :style released-button)
159 :background "#808080" :foreground "black"))
160 "Face for eww buffer buttons."
164 (defface eww-form-file
165 '((((type x w32 ns) (class color)) ; Like default mode line
166 :box (:line-width 2 :style released-button)
167 :background "#808080" :foreground "black"))
168 "Face for eww buffer buttons."
172 (defface eww-form-checkbox
173 '((((type x w32 ns) (class color)) ; Like default mode line
174 :box (:line-width 2 :style released-button)
175 :background "lightgrey" :foreground "black"))
176 "Face for eww buffer buttons."
180 (defface eww-form-select
181 '((((type x w32 ns) (class color)) ; Like default mode line
182 :box (:line-width 2 :style released-button)
183 :background "lightgrey" :foreground "black"))
184 "Face for eww buffer buttons."
188 (defface eww-form-text
189 '((t (:background "#505050"
191 :box (:line-width 1))))
192 "Face for eww text inputs."
196 (defface eww-form-textarea
197 '((t (:background "#C0C0C0"
199 :box (:line-width 1))))
200 "Face for eww textarea inputs."
204 (defface eww-invalid-certificate
205 '((default :weight bold)
206 (((class color)) :foreground "red"))
207 "Face for web pages with invalid certificates."
211 (defface eww-valid-certificate
212 '((default :weight bold)
213 (((class color)) :foreground "ForestGreen"))
214 "Face for web pages with valid certificates."
218 (defvar eww-data nil)
219 (defvar eww-history nil)
220 (defvar eww-history-position 0)
222 (defvar eww-local-regex "localhost"
223 "When this regex is found in the URL, it's not a keyword but an address.")
225 (defvar eww-link-keymap
226 (let ((map (copy-keymap shr-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))))))
279 ;; Check whether the domain only uses "Highly Restricted" Unicode
280 ;; IDNA characters. If not, transform to punycode to indicate that
281 ;; there may be funny business going on.
282 (let ((parsed (url-generic-parse-url url)))
283 (unless (puny-highly-restrictive-domain-p (url-host parsed))
284 (setf (url-host parsed) (puny-encode-domain (url-host parsed)))
285 (setq url (url-recreate-url parsed))))
286 (plist-put eww-data :url url)
287 (plist-put eww-data :title "")
288 (eww-update-header-line-format)
289 (let ((inhibit-read-only t))
290 (insert (format "Loading %s..." url))
291 (goto-char (point-min)))
292 (url-retrieve url 'eww-render
293 (list url nil (current-buffer))))
295 ;;;###autoload (defalias 'browse-web 'eww)
298 (defun eww-open-file (file)
299 "Render FILE using EWW."
300 (interactive "fFile: ")
301 (eww (concat "file://"
302 (and (memq system-type '(windows-nt ms-dos))
304 (expand-file-name file))))
307 (defun eww-search-words (&optional beg end)
308 "Search the web for the text between BEG and END.
309 See the `eww-search-prefix' variable for the search engine used."
311 (eww (buffer-substring beg end)))
313 (defun eww-html-p (content-type)
314 "Return non-nil if CONTENT-TYPE designates an HTML content type.
315 Currently this means either text/html or application/xhtml+xml."
316 (member content-type '("text/html"
317 "application/xhtml+xml")))
319 (defun eww-render (status url &optional point buffer encode)
320 (let ((redirect (plist-get status :redirect)))
322 (setq url redirect)))
323 (let* ((headers (eww-parse-headers))
325 (mail-header-parse-content-type
326 (or (cdr (assoc "content-type" headers))
330 (or (cdr (assq 'charset (cdr content-type)))
331 (eww-detect-charset (eww-html-p (car content-type)))
333 (data-buffer (current-buffer))
334 last-coding-system-used)
335 ;; Save the https peer status.
336 (with-current-buffer buffer
337 (plist-put eww-data :peer (plist-get status :peer)))
341 ((and eww-use-external-browser-for-content-type
342 (string-match-p eww-use-external-browser-for-content-type
344 (eww-browse-with-external-browser url))
345 ((eww-html-p (car content-type))
346 (eww-display-html charset url nil point buffer encode))
347 ((equal (car content-type) "application/pdf")
349 ((string-match-p "\\`image/" (car content-type))
350 (eww-display-image buffer))
352 (eww-display-raw buffer (or encode charset 'utf-8))))
353 (with-current-buffer buffer
354 (plist-put eww-data :url url)
355 (eww-update-header-line-format)
356 (setq eww-history-position 0)
357 (and last-coding-system-used
358 (set-buffer-file-coding-system last-coding-system-used))
359 (run-hooks 'eww-after-render-hook)))
360 (kill-buffer data-buffer))))
362 (defun eww-parse-headers ()
364 (goto-char (point-min))
365 (while (and (not (eobp))
367 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
368 (push (cons (downcase (match-string 1))
376 (defun eww-detect-charset (html-p)
377 (let ((case-fold-search t)
381 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
385 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
388 (declare-function libxml-parse-html-region "xml.c"
389 (start end &optional base-url discard-comments))
391 (defun eww-display-html (charset url &optional document point buffer encode)
392 (unless (fboundp 'libxml-parse-html-region)
393 (error "This function requires Emacs to be compiled with libxml2"))
394 (unless (buffer-live-p buffer)
395 (error "Buffer %s doesn't exist" buffer))
396 ;; There should be a better way to abort loading images
402 'base (list (cons 'href url))
404 (setq encode (or encode charset 'utf-8))
406 (decode-coding-region (point) (point-max) encode)
407 (coding-system-error nil))
408 (libxml-parse-html-region (point) (point-max))))))
409 (source (and (null document)
410 (buffer-substring (point) (point-max)))))
411 (with-current-buffer buffer
412 (setq bidi-paragraph-direction 'left-to-right)
413 (plist-put eww-data :source source)
414 (plist-put eww-data :dom document)
415 (let ((inhibit-read-only t)
416 (inhibit-modification-hooks t)
417 (shr-target-id (url-target (url-generic-parse-url url)))
418 (shr-external-rendering-functions
420 shr-external-rendering-functions
421 '((title . eww-tag-title)
422 (form . eww-tag-form)
423 (input . eww-tag-input)
424 (button . eww-form-submit)
425 (textarea . eww-tag-textarea)
426 (select . eww-tag-select)
427 (link . eww-tag-link)
428 (meta . eww-tag-meta)
431 (shr-insert-document document)
436 (goto-char (point-min))
437 (let ((point (next-single-property-change
438 (point-min) 'shr-target-id)))
442 (goto-char (point-min))
443 ;; Don't leave point inside forms, because the normal eww
444 ;; commands aren't available there.
445 (while (and (not (eobp))
446 (get-text-property (point) 'eww-form))
448 (eww-size-text-inputs))))
450 (defun eww-handle-link (dom)
451 (let* ((rel (dom-attr dom 'rel))
452 (href (dom-attr dom 'href))
454 ;; The text associated with :rel is case-insensitive.
455 (if rel (downcase rel))
457 ;; Texinfo uses "previous", but HTML specifies
458 ;; "prev", so recognize both.
459 ("previous" . :previous)
461 ;; HTML specifies "start" but also "contents",
462 ;; and Gtk seems to use "home". Recognize
463 ;; them all; but store them in different
464 ;; variables so that we can readily choose the
468 ("contents" . :contents)
472 (plist-put eww-data (cdr where) href))))
474 (defvar eww-redirect-level 1)
476 (defun eww-tag-meta (dom)
477 (when (and (cl-equalp (dom-attr dom 'http-equiv) "refresh")
478 (< eww-redirect-level 5))
479 (when-let (refresh (dom-attr dom 'content))
480 (when (or (string-match "^\\([0-9]+\\) *;.*url=\"\\([^\"]+\\)\"" refresh)
481 (string-match "^\\([0-9]+\\) *;.*url=\\([^ ]+\\)" refresh))
482 (let ((timeout (match-string 1 refresh))
483 (url (match-string 2 refresh))
484 (eww-redirect-level (1+ eww-redirect-level)))
485 (if (equal timeout "0")
486 (eww (shr-expand-url url))
488 (dom-node 'a `((href . ,(shr-expand-url url)))
489 (format "Auto refresh in %s second%s disabled"
491 (if (equal timeout "1")
495 (defun eww-tag-link (dom)
496 (eww-handle-link dom)
499 (defun eww-tag-a (dom)
500 (eww-handle-link dom)
501 (let ((start (point)))
503 (put-text-property start (point) 'keymap eww-link-keymap)))
505 (defun eww-update-header-line-format ()
506 (setq header-line-format
507 (and eww-header-line-format
508 (let ((title (plist-get eww-data :title))
509 (peer (plist-get eww-data :peer)))
510 (when (zerop (length title))
511 (setq title "[untitled]"))
512 ;; This connection has is https.
515 (propertize title 'face
516 (if (plist-get peer :warnings)
517 'eww-invalid-certificate
518 'eww-valid-certificate))))
519 (replace-regexp-in-string
522 eww-header-line-format
523 `((?u . ,(or (plist-get eww-data :url) ""))
526 (defun eww-tag-title (dom)
527 (plist-put eww-data :title
528 (replace-regexp-in-string
530 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
531 (eww-update-header-line-format))
533 (defun eww-display-raw (buffer &optional encode)
534 (let ((data (buffer-substring (point) (point-max))))
535 (unless (buffer-live-p buffer)
536 (error "Buffer %s doesn't exist" buffer))
537 (with-current-buffer buffer
538 (let ((inhibit-read-only t))
542 (decode-coding-region (point-min) (1+ (length data)) encode)
543 (coding-system-error nil)))
544 (goto-char (point-min)))))
546 (defun eww-display-image (buffer)
547 (let ((data (shr-parse-image-data)))
548 (unless (buffer-live-p buffer)
549 (error "Buffer %s doesn't exist" buffer))
550 (with-current-buffer buffer
551 (let ((inhibit-read-only t))
553 (shr-put-image data nil))
554 (goto-char (point-min)))))
556 (declare-function mailcap-view-mime "mailcap" (type))
557 (defun eww-display-pdf ()
558 (let ((data (buffer-substring (point) (point-max))))
559 (switch-to-buffer (get-buffer-create "*eww pdf*"))
560 (let ((coding-system-for-write 'raw-text)
561 (inhibit-read-only t))
564 (mailcap-view-mime "application/pdf")))
565 (goto-char (point-min)))
567 (defun eww-setup-buffer ()
568 (switch-to-buffer (get-buffer-create "*eww*"))
569 (when (or (plist-get eww-data :url)
570 (plist-get eww-data :dom))
572 (let ((inhibit-read-only t))
575 (setq bidi-paragraph-direction 'left-to-right)
576 (unless (eq major-mode 'eww-mode)
579 (defun eww-current-url nil
580 "Return URI of the Web page the current EWW buffer is visiting."
581 (plist-get eww-data :url))
583 (defun eww-links-at-point ()
584 "Return list of URIs, if any, linked at point."
586 (list (get-text-property (point) 'shr-url)
587 (get-text-property (point) 'image-url))))
589 (defun eww-view-source ()
590 "View the HTML source code of the current page."
592 (let ((buf (get-buffer-create "*eww-source*"))
593 (source (plist-get eww-data :source)))
594 (with-current-buffer buf
595 (let ((inhibit-read-only t))
596 (delete-region (point-min) (point-max))
597 (insert (or source "no source"))
598 (goto-char (point-min))
599 ;; Decode the source and set the buffer's encoding according
600 ;; to what the HTML source specifies in its 'charset' header,
602 (let ((cs (find-auto-coding "" (point-max))))
605 (when (coding-system-p cs)
606 (decode-coding-region (point-min) (point-max) cs)
607 (setq buffer-file-coding-system last-coding-system-used))))
608 (when (fboundp 'html-mode)
612 (defun eww-readable ()
613 "View the main \"readable\" parts of the current web page.
614 This command uses heuristics to find the parts of the web page that
615 contains the main textual portion, leaving out navigation menus and
618 (let* ((old-data eww-data)
619 (dom (with-temp-buffer
620 (insert (plist-get old-data :source))
622 (decode-coding-region (point-min) (point-max) 'utf-8)
623 (coding-system-error nil))
624 (libxml-parse-html-region (point-min) (point-max)))))
625 (eww-score-readability dom)
627 (eww-display-html nil nil
628 (eww-highest-readability dom)
629 nil (current-buffer))
630 (dolist (elem '(:source :url :title :next :previous :up))
631 (plist-put eww-data elem (plist-get old-data elem)))
632 (eww-update-header-line-format)))
634 (defun eww-score-readability (node)
637 ((memq (dom-tag node) '(script head comment))
639 ((eq (dom-tag node) 'meta)
641 ((eq (dom-tag node) 'img)
643 ((eq (dom-tag node) 'a)
644 (setq score (- (length (split-string (dom-text node))))))
646 (dolist (elem (dom-children node))
648 (setq score (+ score (length (split-string elem))))
650 (or (cdr (assoc :eww-readability-score (cdr elem)))
651 (eww-score-readability elem))))))))
652 ;; Cache the score of the node to avoid recomputing all the time.
653 (dom-set-attribute node :eww-readability-score score)
656 (defun eww-highest-readability (node)
659 (dolist (elem (dom-non-text-children node))
660 (when (> (or (dom-attr
661 (setq highest (eww-highest-readability elem))
662 :eww-readability-score)
663 most-negative-fixnum)
664 (or (dom-attr result :eww-readability-score)
665 most-negative-fixnum))
666 (setq result highest)))
670 (let ((map (make-sparse-keymap)))
671 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
672 (define-key map "G" 'eww)
673 (define-key map [?\t] 'shr-next-link)
674 (define-key map [?\M-\t] 'shr-previous-link)
675 (define-key map [backtab] 'shr-previous-link)
676 (define-key map [delete] 'scroll-down-command)
677 (define-key map "l" 'eww-back-url)
678 (define-key map "r" 'eww-forward-url)
679 (define-key map "n" 'eww-next-url)
680 (define-key map "p" 'eww-previous-url)
681 (define-key map "u" 'eww-up-url)
682 (define-key map "t" 'eww-top-url)
683 (define-key map "&" 'eww-browse-with-external-browser)
684 (define-key map "d" 'eww-download)
685 (define-key map "w" 'eww-copy-page-url)
686 (define-key map "C" 'url-cookie-list)
687 (define-key map "v" 'eww-view-source)
688 (define-key map "R" 'eww-readable)
689 (define-key map "H" 'eww-list-histories)
690 (define-key map "E" 'eww-set-character-encoding)
691 (define-key map "S" 'eww-list-buffers)
692 (define-key map "F" 'eww-toggle-fonts)
693 (define-key map [(meta C)] 'eww-toggle-colors)
695 (define-key map "b" 'eww-add-bookmark)
696 (define-key map "B" 'eww-list-bookmarks)
697 (define-key map [(meta n)] 'eww-next-bookmark)
698 (define-key map [(meta p)] 'eww-previous-bookmark)
700 (easy-menu-define nil map ""
702 ["Exit" quit-window t]
703 ["Close browser" quit-window t]
704 ["Reload" eww-reload t]
705 ["Back to previous page" eww-back-url
706 :active (not (zerop (length eww-history)))]
707 ["Forward to next page" eww-forward-url
708 :active (not (zerop eww-history-position))]
709 ["Browse with external browser" eww-browse-with-external-browser t]
710 ["Download" eww-download t]
711 ["View page source" eww-view-source]
712 ["Copy page URL" eww-copy-page-url t]
713 ["List histories" eww-list-histories t]
714 ["List buffers" eww-list-buffers t]
715 ["Add bookmark" eww-add-bookmark t]
716 ["List bookmarks" eww-list-bookmarks t]
717 ["List cookies" url-cookie-list t]
718 ["Toggle fonts" eww-toggle-fonts t]
719 ["Toggle colors" eww-toggle-colors t]
720 ["Character Encoding" eww-set-character-encoding]))
723 (defvar eww-tool-bar-map
724 (let ((map (make-sparse-keymap)))
725 (dolist (tool-bar-item
726 '((quit-window . "close")
727 (eww-reload . "refresh")
728 (eww-back-url . "left-arrow")
729 (eww-forward-url . "right-arrow")
730 (eww-view-source . "show")
731 (eww-copy-page-url . "copy")
732 (eww-add-bookmark . "bookmark_add"))) ;; ...
733 (tool-bar-local-item-from-menu
734 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
736 "Tool bar for `eww-mode'.")
738 ;; Autoload cookie needed by desktop.el.
740 (define-derived-mode eww-mode special-mode "eww"
741 "Mode for browsing the web."
742 (setq-local eww-data (list :title ""))
743 (setq-local browse-url-browser-function #'eww-browse-url)
744 (add-hook 'after-change-functions #'eww-process-text-input nil t)
745 (setq-local eww-history nil)
746 (setq-local eww-history-position 0)
747 (when (boundp 'tool-bar-map)
748 (setq-local tool-bar-map eww-tool-bar-map))
750 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
751 ;; multi-page isearch support
752 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
753 (setq truncate-lines t)
754 (buffer-disable-undo)
755 (setq buffer-read-only t))
758 (defun eww-browse-url (url &optional new-window)
760 (switch-to-buffer (generate-new-buffer "*eww*"))
764 (defun eww-back-url ()
765 "Go to the previously displayed page."
767 (when (>= eww-history-position (length eww-history))
768 (user-error "No previous page"))
770 (setq eww-history-position (+ eww-history-position 2))
771 (eww-restore-history (elt eww-history (1- eww-history-position))))
773 (defun eww-forward-url ()
774 "Go to the next displayed page."
776 (when (zerop eww-history-position)
777 (user-error "No next page"))
779 (eww-restore-history (elt eww-history (1- eww-history-position))))
781 (defun eww-restore-history (elem)
782 (let ((inhibit-read-only t)
783 (inhibit-modification-hooks t)
784 (text (plist-get elem :text)))
787 (eww-reload) ; FIXME: restore :point?
790 (goto-char (plist-get elem :point))
791 (eww-update-header-line-format))))
793 (defun eww-next-url ()
794 "Go to the page marked `next'.
795 A page is marked `next' if rel=\"next\" appears in a <link>
798 (if (plist-get eww-data :next)
799 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
800 (plist-get eww-data :url)))
801 (user-error "No `next' on this page")))
803 (defun eww-previous-url ()
804 "Go to the page marked `previous'.
805 A page is marked `previous' if rel=\"previous\" appears in a <link>
808 (if (plist-get eww-data :previous)
809 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
810 (plist-get eww-data :url)))
811 (user-error "No `previous' on this page")))
814 "Go to the page marked `up'.
815 A page is marked `up' if rel=\"up\" appears in a <link>
818 (if (plist-get eww-data :up)
819 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
820 (plist-get eww-data :url)))
821 (user-error "No `up' on this page")))
823 (defun eww-top-url ()
824 "Go to the page marked `top'.
825 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
826 appears in a <link> or <a> tag."
828 (let ((best-url (or (plist-get eww-data :start)
829 (plist-get eww-data :contents)
830 (plist-get eww-data :home))))
832 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
833 (user-error "No `top' for this page"))))
835 (defun eww-reload (&optional local encode)
836 "Reload the current page.
837 If LOCAL (the command prefix), don't reload the page from the
838 network, but just re-display the HTML already fetched."
840 (let ((url (plist-get eww-data :url)))
842 (if (null (plist-get eww-data :dom))
843 (error "No current HTML data")
844 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
845 (point) (current-buffer)))
846 (url-retrieve url 'eww-render
847 (list url (point) (current-buffer) encode)))))
851 (defvar eww-form nil)
853 (defvar eww-submit-map
854 (let ((map (make-sparse-keymap)))
855 (define-key map "\r" 'eww-submit)
856 (define-key map [(control c) (control c)] 'eww-submit)
859 (defvar eww-submit-file
860 (let ((map (make-sparse-keymap)))
861 (define-key map "\r" 'eww-select-file)
862 (define-key map [(control c) (control c)] 'eww-submit)
865 (defvar eww-checkbox-map
866 (let ((map (make-sparse-keymap)))
867 (define-key map " " 'eww-toggle-checkbox)
868 (define-key map "\r" 'eww-toggle-checkbox)
869 (define-key map [(control c) (control c)] 'eww-submit)
873 (let ((map (make-keymap)))
874 (set-keymap-parent map text-mode-map)
875 (define-key map "\r" 'eww-submit)
876 (define-key map [(control a)] 'eww-beginning-of-text)
877 (define-key map [(control c) (control c)] 'eww-submit)
878 (define-key map [(control e)] 'eww-end-of-text)
879 (define-key map [?\t] 'shr-next-link)
880 (define-key map [?\M-\t] 'shr-previous-link)
883 (defvar eww-textarea-map
884 (let ((map (make-keymap)))
885 (set-keymap-parent map text-mode-map)
886 (define-key map "\r" 'forward-line)
887 (define-key map [(control c) (control c)] 'eww-submit)
888 (define-key map [?\t] 'shr-next-link)
889 (define-key map [?\M-\t] 'shr-previous-link)
892 (defvar eww-select-map
893 (let ((map (make-sparse-keymap)))
894 (define-key map "\r" 'eww-change-select)
895 (define-key map [(control c) (control c)] 'eww-submit)
898 (defun eww-beginning-of-text ()
899 "Move to the start of the input field."
901 (goto-char (eww-beginning-of-field)))
903 (defun eww-end-of-text ()
904 "Move to the end of the text in the input field."
906 (goto-char (eww-end-of-field))
907 (let ((start (eww-beginning-of-field)))
908 (while (and (equal (following-char) ? )
911 (when (> (point) start)
914 (defun eww-beginning-of-field ()
918 ((not (eq (get-text-property (point) 'eww-form)
919 (get-text-property (1- (point)) 'eww-form)))
922 (previous-single-property-change
923 (point) 'eww-form nil (point-min)))))
925 (defun eww-end-of-field ()
926 (1- (next-single-property-change
927 (point) 'eww-form nil (point-max))))
929 (defun eww-tag-form (dom)
930 (let ((eww-form (list (cons :method (dom-attr dom 'method))
931 (cons :action (dom-attr dom 'action))))
933 (shr-ensure-paragraph)
938 (when (> (point) start)
939 (put-text-property start (1+ start)
940 'eww-form eww-form))))
942 (defun eww-form-submit (dom)
943 (let ((start (point))
944 (value (dom-attr dom 'value)))
946 (if (zerop (length value))
950 (add-face-text-property start (point) 'eww-form-submit)
951 (put-text-property start (point) 'eww-form
952 (list :eww-form eww-form
955 :name (dom-attr dom 'name)))
956 (put-text-property start (point) 'keymap eww-submit-map)
959 (defun eww-form-checkbox (dom)
960 (let ((start (point)))
961 (if (dom-attr dom 'checked)
962 (insert eww-form-checkbox-selected-symbol)
963 (insert eww-form-checkbox-symbol))
964 (add-face-text-property start (point) 'eww-form-checkbox)
965 (put-text-property start (point) 'eww-form
966 (list :eww-form eww-form
967 :value (dom-attr dom 'value)
968 :type (downcase (dom-attr dom 'type))
969 :checked (dom-attr dom 'checked)
970 :name (dom-attr dom 'name)))
971 (put-text-property start (point) 'keymap eww-checkbox-map)
974 (defun eww-form-file (dom)
975 (let ((start (point))
976 (value (dom-attr dom 'value)))
978 (if (zerop (length value))
982 (add-face-text-property start (point) 'eww-form-file)
984 (put-text-property start (point) 'eww-form
985 (list :eww-form eww-form
986 :value (dom-attr dom 'value)
987 :type (downcase (dom-attr dom 'type))
988 :name (dom-attr dom 'name)))
989 (put-text-property start (point) 'keymap eww-submit-file)
992 (defun eww-select-file ()
993 "Change the value of the upload file menu under point."
995 (let* ((input (get-text-property (point) 'eww-form)))
997 (let ((insert-default-directory t))
998 (read-file-name "filename: "))))
999 (eww-update-field filename (length "Browse"))
1000 (plist-put input :filename filename))))
1002 (defun eww-form-text (dom)
1003 (let ((start (point))
1004 (type (downcase (or (dom-attr dom 'type) "text")))
1005 (value (or (dom-attr dom 'value) ""))
1006 (width (string-to-number (or (dom-attr dom 'size) "40")))
1007 (readonly-property (if (or (dom-attr dom 'disabled)
1008 (dom-attr dom 'readonly))
1010 'inhibit-read-only)))
1012 (when (< (length value) width)
1013 (insert (make-string (- width (length value)) ? )))
1014 (put-text-property start (point) 'face 'eww-form-text)
1015 (put-text-property start (point) 'inhibit-read-only t)
1016 (put-text-property start (point) 'local-map eww-text-map)
1017 (put-text-property start (point) readonly-property t)
1018 (put-text-property start (point) 'eww-form
1019 (list :eww-form eww-form
1022 :name (dom-attr dom 'name)))
1025 (defconst eww-text-input-types '("text" "password" "textarea"
1026 "color" "date" "datetime" "datetime-local"
1027 "email" "month" "number" "search" "tel"
1028 "time" "url" "week")
1029 "List of input types which represent a text input.
1030 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1032 (defun eww-process-text-input (beg end replace-length)
1033 (when-let (pos (and (< (1+ end) (point-max))
1034 (> (1- end) (point-min))
1036 ((get-text-property (1+ end) 'eww-form)
1038 ((get-text-property (1- end) 'eww-form)
1040 (let* ((form (get-text-property pos 'eww-form))
1041 (properties (text-properties-at pos))
1042 (inhibit-read-only t)
1043 (length (- end beg replace-length))
1044 (type (plist-get form :type)))
1046 (member type eww-text-input-types))
1049 ;; Delete some space at the end.
1052 (if (equal type "textarea")
1053 (1- (line-end-position))
1054 (eww-end-of-field)))
1055 (while (and (> length 0)
1056 (eql (following-char) ? ))
1057 (delete-region (1- (point)) (point))
1062 (goto-char (1- end))
1064 (if (equal type "textarea")
1065 (1- (line-end-position))
1066 (1+ (eww-end-of-field))))
1067 (let ((start (point)))
1068 (insert (make-string (abs length) ? ))
1069 (set-text-properties start (point) properties))
1070 (goto-char (1- end)))))
1071 (set-text-properties (plist-get form :start) (plist-get form :end)
1073 (let ((value (buffer-substring-no-properties
1074 (eww-beginning-of-field)
1075 (eww-end-of-field))))
1076 (when (string-match " +\\'" value)
1077 (setq value (substring value 0 (match-beginning 0))))
1078 (plist-put form :value value)
1079 (when (equal type "password")
1080 ;; Display passwords as asterisks.
1081 (let ((start (eww-beginning-of-field)))
1082 (put-text-property start (+ start (length value))
1083 'display (make-string (length value) ?*)))))))))
1085 (defun eww-tag-textarea (dom)
1086 (let ((start (point))
1087 (value (or (dom-attr dom 'value) ""))
1088 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1089 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1091 (shr-ensure-newline)
1093 (shr-ensure-newline)
1094 (when (< (count-lines start (point)) lines)
1095 (dotimes (_ (- lines (count-lines start (point))))
1097 (setq end (point-marker))
1099 (while (< (point) end)
1101 (let ((pad (- width (- (point) (line-beginning-position)))))
1103 (insert (make-string pad ? ))))
1104 (add-face-text-property (line-beginning-position)
1105 (point) 'eww-form-textarea)
1106 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1107 (put-text-property (line-beginning-position) (point)
1108 'local-map eww-textarea-map)
1110 (put-text-property start (point) 'eww-form
1111 (list :eww-form eww-form
1114 :name (dom-attr dom 'name)))))
1116 (defun eww-tag-input (dom)
1117 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1120 ((or (equal type "checkbox")
1121 (equal type "radio"))
1122 (eww-form-checkbox dom))
1123 ((equal type "file")
1124 (eww-form-file dom))
1125 ((equal type "submit")
1126 (eww-form-submit dom))
1127 ((equal type "hidden")
1128 (let ((form eww-form)
1129 (name (dom-attr dom 'name)))
1130 ;; Don't add <input type=hidden> elements repeatedly.
1132 (or (not (consp (car form)))
1133 (not (eq (caar form) 'hidden))
1134 (not (equal (plist-get (cdr (car form)) :name)
1136 (setq form (cdr form)))
1138 (nconc eww-form (list
1141 :value (dom-attr dom 'value)))))))
1143 (eww-form-text dom)))
1144 (unless (= start (point))
1145 (put-text-property start (1+ start) 'help-echo "Input field"))))
1147 (defun eww-tag-select (dom)
1148 (shr-ensure-paragraph)
1149 (let ((menu (list :name (dom-attr dom 'name)
1150 :eww-form eww-form))
1155 (if (eq (dom-tag dom) 'optgroup)
1156 (dolist (groupelem (dom-children dom))
1157 (unless (dom-attr groupelem 'disabled)
1158 (setq opelem (append opelem (list groupelem)))))
1159 (setq opelem (list dom)))
1160 (dolist (elem opelem)
1161 (when (eq (dom-tag elem) 'option)
1162 (when (dom-attr elem 'selected)
1163 (nconc menu (list :value (dom-attr elem 'value))))
1164 (let ((display (dom-text elem)))
1165 (setq max (max max (length display)))
1167 :value (dom-attr elem 'value)
1171 (setq options (nreverse options))
1172 ;; If we have no selected values, default to the first value.
1173 (unless (plist-get menu :value)
1174 (nconc menu (list :value (nth 2 (car options)))))
1175 (nconc menu options)
1176 (let ((selected (eww-select-display menu)))
1178 (make-string (- max (length selected)) ? )))
1179 (put-text-property start (point) 'eww-form menu)
1180 (add-face-text-property start (point) 'eww-form-select)
1181 (put-text-property start (point) 'keymap eww-select-map)
1182 (unless (= start (point))
1183 (put-text-property start (1+ start) 'help-echo "select field"))
1184 (shr-ensure-paragraph))))
1186 (defun eww-select-display (select)
1187 (let ((value (plist-get select :value))
1189 (dolist (elem select)
1190 (when (and (consp elem)
1191 (eq (car elem) 'item)
1192 (equal value (plist-get (cdr elem) :value)))
1193 (setq display (plist-get (cdr elem) :display))))
1196 (defun eww-change-select ()
1197 "Change the value of the select drop-down menu under point."
1199 (let* ((input (get-text-property (point) 'eww-form))
1200 (completion-ignore-case t)
1203 (mapcar (lambda (elem)
1205 (eq (car elem) 'item)
1206 (cons (plist-get (cdr elem) :display)
1207 (plist-get (cdr elem) :value))))
1210 (completing-read "Change value: " options nil 'require-match))
1211 (inhibit-read-only t))
1212 (plist-put input :value (cdr (assoc-string display options t)))
1214 (eww-update-field display))))
1216 (defun eww-update-field (string &optional offset)
1219 (let ((properties (text-properties-at (point)))
1220 (start (+ (eww-beginning-of-field) offset))
1221 (current-end (1+ (eww-end-of-field)))
1222 (new-end (+ (eww-beginning-of-field) (length string)))
1223 (inhibit-read-only t))
1224 (delete-region start current-end)
1225 (forward-char offset)
1227 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1229 (set-text-properties start new-end properties))
1232 (defun eww-toggle-checkbox ()
1233 "Toggle the value of the checkbox under point."
1235 (let* ((input (get-text-property (point) 'eww-form))
1236 (type (plist-get input :type)))
1237 (if (equal type "checkbox")
1240 (if (plist-get input :checked)
1242 (plist-put input :checked nil)
1243 (eww-update-field eww-form-checkbox-symbol))
1244 (plist-put input :checked t)
1245 (eww-update-field eww-form-checkbox-selected-symbol))))
1246 ;; Radio button. Switch all other buttons off.
1247 (let ((name (plist-get input :name)))
1249 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1250 (when (equal (plist-get (cdr elem) :name) name)
1251 (goto-char (car elem))
1252 (if (not (eq (cdr elem) input))
1254 (plist-put input :checked nil)
1255 (eww-update-field eww-form-checkbox-symbol))
1256 (plist-put input :checked t)
1257 (eww-update-field eww-form-checkbox-selected-symbol)))))
1258 (forward-char 1)))))
1260 (defun eww-inputs (form)
1261 (let ((start (point-min))
1264 (< start (point-max)))
1265 (when (or (get-text-property start 'eww-form)
1266 (setq start (next-single-property-change start 'eww-form)))
1267 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1269 (push (cons start (get-text-property start 'eww-form))
1271 (setq start (next-single-property-change start 'eww-form))))
1274 (defun eww-size-text-inputs ()
1275 (let ((start (point-min)))
1277 (< start (point-max)))
1278 (when (or (get-text-property start 'eww-form)
1279 (setq start (next-single-property-change start 'eww-form)))
1280 (let ((props (get-text-property start 'eww-form)))
1281 (plist-put props :start start)
1282 (setq start (next-single-property-change
1283 start 'eww-form nil (point-max)))
1284 (plist-put props :end start))))))
1286 (defun eww-input-value (input)
1287 (let ((type (plist-get input :type))
1288 (value (plist-get input :value)))
1290 ((equal type "textarea")
1293 (goto-char (point-min))
1294 (while (re-search-forward "^ +\n\\| +$" nil t)
1295 (replace-match "" t t))
1298 (if (string-match " +\\'" value)
1299 (substring value 0 (match-beginning 0))
1302 (defun eww-submit ()
1303 "Submit the current form."
1305 (let* ((this-input (get-text-property (point) 'eww-form))
1306 (form (plist-get this-input :eww-form))
1308 (dolist (elem (sort (eww-inputs form)
1310 (< (car o1) (car o2)))))
1311 (let* ((input (cdr elem))
1312 (input-start (car elem))
1313 (name (plist-get input :name)))
1316 ((member (plist-get input :type) '("checkbox" "radio"))
1317 (when (plist-get input :checked)
1318 (push (cons name (plist-get input :value))
1320 ((equal (plist-get input :type) "file")
1322 (list (cons "filedata"
1324 (insert-file-contents
1325 (plist-get input :filename))
1327 (cons "name" (plist-get input :name))
1328 (cons "filename" (plist-get input :filename))))
1330 ((equal (plist-get input :type) "submit")
1331 ;; We want the values from buttons if we hit a button if
1332 ;; we hit enter on it, or if it's the first button after
1333 ;; the field we did hit return on.
1334 (when (or (eq input this-input)
1335 (and (not (eq input this-input))
1337 (> input-start (point))))
1338 (setq next-submit t)
1339 (push (cons name (plist-get input :value))
1342 (push (cons name (eww-input-value input))
1345 (when (and (consp elem)
1346 (eq (car elem) 'hidden))
1347 (push (cons (plist-get (cdr elem) :name)
1348 (or (plist-get (cdr elem) :value) ""))
1350 (if (and (stringp (cdr (assq :method form)))
1351 (equal (downcase (cdr (assq :method form))) "post"))
1353 (dolist (x values mtype)
1354 (if (equal (car x) "file")
1356 (setq mtype "multipart/form-data"))))
1357 (cond ((equal mtype "multipart/form-data")
1358 (let ((boundary (mml-compute-boundary '())))
1359 (let ((url-request-method "POST")
1360 (url-request-extra-headers
1361 (list (cons "Content-Type"
1362 (concat "multipart/form-data; boundary="
1365 (mm-url-encode-multipart-form-data values boundary)))
1366 (eww-browse-url (shr-expand-url
1367 (cdr (assq :action form))
1368 (plist-get eww-data :url))))))
1370 (let ((url-request-method "POST")
1371 (url-request-extra-headers
1373 "application/x-www-form-urlencoded")))
1375 (mm-url-encode-www-form-urlencoded values)))
1376 (eww-browse-url (shr-expand-url
1377 (cdr (assq :action form))
1378 (plist-get eww-data :url)))))))
1381 (if (cdr (assq :action form))
1382 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1383 (plist-get eww-data :url))
1385 (mm-url-encode-www-form-urlencoded values))))))
1387 (defun eww-browse-with-external-browser (&optional url)
1388 "Browse the current URL with an external browser.
1389 The browser to used is specified by the `shr-external-browser' variable."
1391 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1393 (defun eww-follow-link (&optional external mouse-event)
1394 "Browse the URL under point.
1395 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1396 If EXTERNAL is double prefix, browse in new buffer."
1397 (interactive (list current-prefix-arg last-nonmenu-event))
1398 (mouse-set-point mouse-event)
1399 (let ((url (get-text-property (point) 'shr-url)))
1402 (message "No link under point"))
1403 ((string-match "^mailto:" url)
1404 (browse-url-mail url))
1405 ((and (consp external) (<= (car external) 4))
1406 (funcall shr-external-browser url))
1407 ;; This is a #target url in the same page as the current one.
1408 ((and (url-target (url-generic-parse-url url))
1409 (eww-same-page-p url (plist-get eww-data :url)))
1410 (let ((dom (plist-get eww-data :dom)))
1412 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1414 (eww-browse-url url external)))))
1416 (defun eww-same-page-p (url1 url2)
1417 "Return non-nil if URL1 and URL2 represent the same page.
1418 Differences in #targets are ignored."
1419 (let ((obj1 (url-generic-parse-url url1))
1420 (obj2 (url-generic-parse-url url2)))
1421 (setf (url-target obj1) nil)
1422 (setf (url-target obj2) nil)
1423 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1425 (defun eww-copy-page-url ()
1426 "Copy the URL of the current page into the kill ring."
1428 (message "%s" (plist-get eww-data :url))
1429 (kill-new (plist-get eww-data :url)))
1431 (defun eww-download ()
1432 "Download URL under point to `eww-download-directory'."
1434 (let ((url (get-text-property (point) 'shr-url)))
1436 (message "No URL under point")
1437 (url-retrieve url 'eww-download-callback (list url)))))
1439 (defun eww-download-callback (status url)
1440 (unless (plist-get status :error)
1441 (let* ((obj (url-generic-parse-url url))
1442 (path (car (url-path-and-query obj)))
1443 (file (eww-make-unique-file-name
1444 (eww-decode-url-file-name (file-name-nondirectory path))
1445 eww-download-directory)))
1446 (goto-char (point-min))
1447 (re-search-forward "\r?\n\r?\n")
1448 (write-region (point) (point-max) file)
1449 (message "Saved %s" file))))
1451 (defun eww-decode-url-file-name (string)
1452 (let* ((binary (url-unhex-string string))
1454 (decode-coding-string
1456 ;; Possibly set by `universal-coding-system-argument'.
1457 (or coding-system-for-read
1458 ;; RFC 3986 says that %AB stuff is utf-8.
1459 (if (equal (decode-coding-string binary 'utf-8)
1463 (car (detect-coding-string binary))))))
1464 (encodes (find-coding-systems-string decoded)))
1465 (if (or (equal encodes '(undecided))
1466 (memq (coding-system-base (or file-name-coding-system
1467 default-file-name-coding-system))
1470 ;; If we can't encode the decoded file name (due to language
1471 ;; environment settings), then we return the original, hexified
1475 (defun eww-make-unique-file-name (file directory)
1477 ((zerop (length file))
1479 ((string-match "\\`[.]" file)
1480 (setq file (concat "!" file))))
1484 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1485 (setq stem (match-string 1)
1486 suffix (match-string 2)))
1487 (while (file-exists-p (expand-file-name file directory))
1488 (setq file (format "%s(%d)%s" stem count suffix))
1489 (setq count (1+ count)))
1490 (expand-file-name file directory)))
1492 (defun eww-set-character-encoding (charset)
1493 "Set character encoding to CHARSET.
1494 If CHARSET is nil then use UTF-8."
1495 (interactive "zUse character set (default utf-8): ")
1497 (eww-reload nil 'utf-8)
1498 (eww-reload nil charset)))
1500 (defun eww-toggle-fonts ()
1501 "Toggle whether to use monospaced or font-enabled layouts."
1503 (message "Fonts are now %s"
1504 (if (setq shr-use-fonts (not shr-use-fonts))
1509 (defun eww-toggle-colors ()
1510 "Toggle whether to use HTML-specified colors or not."
1512 (message "Colors are now %s"
1513 (if (setq shr-use-colors (not shr-use-colors))
1520 (defvar eww-bookmarks nil)
1522 (defun eww-add-bookmark ()
1523 "Bookmark the current page."
1525 (eww-read-bookmarks)
1526 (dolist (bookmark eww-bookmarks)
1527 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1528 (user-error "Already bookmarked")))
1529 (when (y-or-n-p "Bookmark this page?")
1530 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1531 (plist-get eww-data :title))))
1532 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1533 (push (list :url (plist-get eww-data :url)
1535 :time (current-time-string))
1537 (eww-write-bookmarks)
1538 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1539 (plist-get eww-data :title))))
1541 (defun eww-write-bookmarks ()
1542 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1543 (insert ";; Auto-generated file; don't edit\n")
1544 (pp eww-bookmarks (current-buffer))))
1546 (defun eww-read-bookmarks ()
1547 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1549 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1551 (insert-file-contents file)
1552 (read (current-buffer)))))))
1555 (defun eww-list-bookmarks ()
1556 "Display the bookmarks."
1558 (eww-bookmark-prepare)
1559 (pop-to-buffer "*eww bookmarks*"))
1561 (defun eww-bookmark-prepare ()
1562 (eww-read-bookmarks)
1563 (unless eww-bookmarks
1564 (user-error "No bookmarks are defined"))
1565 (set-buffer (get-buffer-create "*eww bookmarks*"))
1567 (let* ((width (/ (window-width) 2))
1568 (format (format "%%-%ds %%s" width))
1569 (inhibit-read-only t)
1572 (setq header-line-format (concat " " (format format "Title" "URL")))
1573 (dolist (bookmark eww-bookmarks)
1575 title (plist-get bookmark :title))
1576 (when (> (length title) width)
1577 (setq title (truncate-string-to-width title width)))
1578 (insert (format format title (plist-get bookmark :url)) "\n")
1579 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1580 (goto-char (point-min))))
1582 (defvar eww-bookmark-kill-ring nil)
1584 (defun eww-bookmark-kill ()
1585 "Kill the current bookmark."
1587 (let* ((start (line-beginning-position))
1588 (bookmark (get-text-property start 'eww-bookmark))
1589 (inhibit-read-only t))
1591 (user-error "No bookmark on the current line"))
1593 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1594 (delete-region start (point))
1595 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1596 (eww-write-bookmarks)))
1598 (defun eww-bookmark-yank ()
1599 "Yank a previously killed bookmark to the current line."
1601 (unless eww-bookmark-kill-ring
1602 (user-error "No previously killed bookmark"))
1604 (let ((inhibit-read-only t)
1607 (insert (pop eww-bookmark-kill-ring))
1608 (setq bookmark (get-text-property start 'eww-bookmark))
1609 (if (= start (point-min))
1610 (push bookmark eww-bookmarks)
1611 (let ((line (count-lines start (point))))
1612 (setcdr (nthcdr (1- line) eww-bookmarks)
1613 (cons bookmark (nthcdr line eww-bookmarks)))))
1614 (eww-write-bookmarks)))
1616 (defun eww-bookmark-browse ()
1617 "Browse the bookmark under point in eww."
1619 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1621 (user-error "No bookmark on the current line"))
1623 (eww-browse-url (plist-get bookmark :url))))
1625 (defun eww-next-bookmark ()
1626 "Go to the next bookmark in the list."
1630 (unless (get-buffer "*eww bookmarks*")
1632 (eww-bookmark-prepare))
1633 (with-current-buffer (get-buffer "*eww bookmarks*")
1634 (when (and (not first)
1637 (setq bookmark (get-text-property (line-beginning-position)
1640 (user-error "No next bookmark")))
1641 (eww-browse-url (plist-get bookmark :url))))
1643 (defun eww-previous-bookmark ()
1644 "Go to the previous bookmark in the list."
1648 (unless (get-buffer "*eww bookmarks*")
1650 (eww-bookmark-prepare))
1651 (with-current-buffer (get-buffer "*eww bookmarks*")
1653 (goto-char (point-max))
1654 (beginning-of-line))
1655 ;; On the final line.
1659 (user-error "No previous bookmark")
1661 (setq bookmark (get-text-property (line-beginning-position)
1663 (eww-browse-url (plist-get bookmark :url))))
1665 (defvar eww-bookmark-mode-map
1666 (let ((map (make-sparse-keymap)))
1667 (define-key map [(control k)] 'eww-bookmark-kill)
1668 (define-key map [(control y)] 'eww-bookmark-yank)
1669 (define-key map "\r" 'eww-bookmark-browse)
1671 (easy-menu-define nil map
1672 "Menu for `eww-bookmark-mode-map'."
1674 ["Exit" quit-window t]
1675 ["Browse" eww-bookmark-browse
1676 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1677 ["Kill" eww-bookmark-kill
1678 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1679 ["Yank" eww-bookmark-yank
1680 :active eww-bookmark-kill-ring]))
1683 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1684 "Mode for listing bookmarks.
1686 \\{eww-bookmark-mode-map}"
1687 (buffer-disable-undo)
1688 (setq truncate-lines t))
1692 (defun eww-save-history ()
1693 (plist-put eww-data :point (point))
1694 (plist-put eww-data :text (buffer-string))
1695 (push eww-data eww-history)
1696 (setq eww-data (list :title ""))
1697 ;; Don't let the history grow infinitely. We store quite a lot of
1699 (when-let (tail (and eww-history-limit
1700 (nthcdr eww-history-limit eww-history)))
1703 (defvar eww-current-buffer)
1705 (defun eww-list-histories ()
1706 "List the eww-histories."
1708 (when (null eww-history)
1709 (error "No eww-histories are defined"))
1710 (let ((eww-history-trans eww-history)
1711 (buffer (current-buffer)))
1712 (set-buffer (get-buffer-create "*eww history*"))
1714 (setq-local eww-current-buffer buffer)
1715 (let ((inhibit-read-only t)
1718 url title format start)
1720 (dolist (history eww-history-trans)
1721 (setq start (point))
1722 (setq domain-length (max domain-length (length (plist-get history :url))))
1723 (setq title-length (max title-length (length (plist-get history :title)))))
1724 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1726 (concat " " (format format "Title" "URL")))
1727 (dolist (history eww-history-trans)
1728 (setq start (point))
1729 (setq url (plist-get history :url))
1730 (setq title (plist-get history :title))
1731 (insert (format format title url))
1733 (put-text-property start (1+ start) 'eww-history history))
1734 (goto-char (point-min)))
1735 (pop-to-buffer "*eww history*")))
1737 (defun eww-history-browse ()
1738 "Browse the history under point in eww."
1740 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1742 (error "No history on the current line"))
1743 (let ((buffer eww-current-buffer))
1746 (switch-to-buffer buffer)))
1747 (eww-restore-history history)))
1749 (defvar eww-history-mode-map
1750 (let ((map (make-sparse-keymap)))
1751 (define-key map "\r" 'eww-history-browse)
1752 ;; (define-key map "n" 'next-error-no-select)
1753 ;; (define-key map "p" 'previous-error-no-select)
1755 (easy-menu-define nil map
1756 "Menu for `eww-history-mode-map'."
1758 ["Exit" quit-window t]
1759 ["Browse" eww-history-browse
1760 :active (get-text-property (line-beginning-position) 'eww-history)]))
1763 (define-derived-mode eww-history-mode special-mode "eww history"
1764 "Mode for listing eww-histories.
1766 \\{eww-history-mode-map}"
1767 (buffer-disable-undo)
1768 (setq truncate-lines t))
1770 ;;; eww buffers list
1772 (defun eww-list-buffers ()
1773 "Enlist eww buffers."
1776 (current (current-buffer)))
1777 (dolist (buffer (buffer-list))
1778 (with-current-buffer buffer
1779 (when (derived-mode-p 'eww-mode)
1780 (push (vector buffer (plist-get eww-data :title)
1781 (plist-get eww-data :url))
1783 (unless buffers-info
1784 (error "No eww buffers"))
1785 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1786 (set-buffer (get-buffer-create "*eww buffers*"))
1788 (let ((inhibit-read-only t)
1791 url title format start)
1793 (dolist (buffer-info buffers-info)
1794 (setq title-length (max title-length
1795 (length (elt buffer-info 1)))
1796 domain-length (max domain-length
1797 (length (elt buffer-info 2)))))
1798 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1800 (concat " " (format format "Title" "URL")))
1802 (current-buffer-line 1))
1803 (dolist (buffer-info buffers-info)
1805 title (elt buffer-info 1)
1806 url (elt buffer-info 2)
1808 (insert (format format title url))
1810 (let ((buffer (elt buffer-info 0)))
1811 (put-text-property start (1+ start) 'eww-buffer
1813 (when (eq current buffer)
1814 (setq current-buffer-line line))))
1815 (goto-char (point-min))
1816 (forward-line (1- current-buffer-line)))))
1817 (pop-to-buffer "*eww buffers*"))
1819 (defun eww-buffer-select ()
1820 "Switch to eww buffer."
1822 (let ((buffer (get-text-property (line-beginning-position)
1825 (error "No buffer on current line"))
1827 (switch-to-buffer buffer)))
1829 (defun eww-buffer-show ()
1830 "Display buffer under point in eww buffer list."
1831 (let ((buffer (get-text-property (line-beginning-position)
1834 (error "No buffer on current line"))
1836 (switch-to-buffer buffer)
1839 (defun eww-buffer-show-next ()
1840 "Move to next eww buffer in the list and display it."
1844 (goto-char (point-min)))
1847 (defun eww-buffer-show-previous ()
1848 "Move to previous eww buffer in the list and display it."
1852 (goto-char (point-max)))
1856 (defun eww-buffer-kill ()
1857 "Kill buffer from eww list."
1859 (let* ((start (line-beginning-position))
1860 (buffer (get-text-property start 'eww-buffer))
1861 (inhibit-read-only t))
1863 (user-error "No buffer on the current line"))
1864 (kill-buffer buffer)
1866 (delete-region start (point)))
1871 (defvar eww-buffers-mode-map
1872 (let ((map (make-sparse-keymap)))
1873 (define-key map [(control k)] 'eww-buffer-kill)
1874 (define-key map "\r" 'eww-buffer-select)
1875 (define-key map "n" 'eww-buffer-show-next)
1876 (define-key map "p" 'eww-buffer-show-previous)
1878 (easy-menu-define nil map
1879 "Menu for `eww-buffers-mode-map'."
1881 ["Exit" quit-window t]
1882 ["Select" eww-buffer-select
1883 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1884 ["Kill" eww-buffer-kill
1885 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1888 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1889 "Mode for listing buffers.
1891 \\{eww-buffers-mode-map}"
1892 (buffer-disable-undo)
1893 (setq truncate-lines t))
1897 (defvar eww-desktop-data-save
1898 '(:url :title :point)
1899 "List of `eww-data' properties to preserve in the desktop file.
1900 Also used when saving `eww-history'.")
1902 (defun eww-desktop-data-1 (alist)
1906 (let ((k (car tail))
1908 (when (memq k eww-desktop-data-save)
1909 (setq acc (cons k (cons v acc)))))
1910 (setq tail (cddr tail)))
1913 (defun eww-desktop-history-duplicate (a b)
1914 (let ((tail a) (r t))
1916 (if (or (memq (car tail) '(:point)) ; ignore :point
1918 (plist-get b (car tail))))
1919 (setq tail (cddr tail))
1925 (defun eww-desktop-misc-data (_directory)
1926 "Return a property list with data used to restore eww buffers.
1927 This list will contain, as :history, the list, whose first element is
1928 the value of `eww-data', and the tail is `eww-history'.
1930 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1931 entries (if any) will be removed from the list.
1933 Only the properties listed in `eww-desktop-data-save' are included.
1934 Generally, the list should not include the (usually overly large)
1935 :dom, :source and :text properties."
1936 (let ((history (mapcar 'eww-desktop-data-1
1937 (cons eww-data eww-history))))
1938 (list :history (if eww-desktop-remove-duplicates
1939 (cl-remove-duplicates
1940 history :test 'eww-desktop-history-duplicate)
1943 (defun eww-restore-desktop (file-name buffer-name misc-data)
1944 "Restore an eww buffer from its desktop file record.
1945 If `eww-restore-desktop' is t or `auto', this function will also
1946 initiate the retrieval of the respective URI in the background.
1947 Otherwise, the restored buffer will contain a prompt to do so by using
1949 (with-current-buffer (get-buffer-create buffer-name)
1951 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1952 (setq eww-history (cdr (plist-get misc-data :history))
1953 eww-data (or (car (plist-get misc-data :history))
1954 ;; backwards compatibility
1955 (list :url (plist-get misc-data :uri))))
1957 (when (plist-get eww-data :url)
1958 (case eww-restore-desktop
1959 ((t auto) (eww (plist-get eww-data :url)))
1960 ((zerop (buffer-size))
1961 (let ((inhibit-read-only t))
1962 (insert (substitute-command-keys
1963 eww-restore-reload-prompt)))))))
1967 (add-to-list 'desktop-locals-to-save
1968 'eww-history-position)
1969 (add-to-list 'desktop-buffer-mode-handlers
1970 '(eww-mode . eww-restore-desktop))
1974 (defun eww-isearch-next-buffer (&optional _buffer wrap)
1975 "Go to the next page to search using `rel' attribute for navigation."
1982 (eww-previous-url)))
1987 ;;; eww.el ends here