Merge branch 'master' into comment-cache
[emacs.git] / lisp / net / eww.el
blobf7e06341443c98f6e85c6571fc09a3e4f6388102
1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'cl-lib)
28 (require 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'url-queue)
32 (require 'url-util) ; for url-get-url-at-point
33 (require 'mm-url)
34 (require 'puny)
35 (eval-when-compile (require 'subr-x)) ;; for string-trim
37 (defgroup eww nil
38 "Emacs Web Wowser"
39 :version "25.1"
40 :link '(custom-manual "(eww) Top")
41 :group 'web
42 :prefix "eww-")
44 (defcustom eww-header-line-format "%t: %u"
45 "Header line format.
46 - %t is replaced by the title.
47 - %u is replaced by the URL."
48 :version "24.4"
49 :group 'eww
50 :type 'string)
52 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
53 "Prefix URL to search engine."
54 :version "24.4"
55 :group 'eww
56 :type 'string)
58 (defcustom eww-download-directory "~/Downloads/"
59 "Directory where files will downloaded."
60 :version "24.4"
61 :group 'eww
62 :type 'directory)
64 ;;;###autoload
65 (defcustom eww-suggest-uris
66 '(eww-links-at-point
67 url-get-url-at-point
68 eww-current-url)
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."
73 :version "25.1"
74 :group 'eww
75 :type 'hook
76 :options '(eww-links-at-point
77 url-get-url-at-point
78 eww-current-url))
80 (defcustom eww-bookmarks-directory user-emacs-directory
81 "Directory where bookmark files will be stored."
82 :version "25.1"
83 :group 'eww
84 :type 'directory)
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."
91 :version "25.1"
92 :group 'eww
93 :type 'boolean)
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
100 page contents."
101 :version "25.1"
102 :group 'eww
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'."
112 :version "25.1"
113 :group 'eww
114 :type 'string)
116 (defcustom eww-history-limit 50
117 "Maximum number of entries to retain in the history."
118 :version "25.1"
119 :group 'eww
120 :type '(choice (const :tag "Unlimited" nil)
121 integer))
123 (defcustom eww-use-external-browser-for-content-type
124 "\\`\\(video/\\|audio/\\|application/ogg\\)"
125 "Always use external browser for specified content-type."
126 :version "24.4"
127 :group 'eww
128 :type '(choice (const :tag "Never" nil)
129 regexp))
131 (defcustom eww-after-render-hook nil
132 "A hook called after eww has finished rendering the buffer."
133 :version "25.1"
134 :group 'eww
135 :type 'hook)
137 (defcustom eww-form-checkbox-selected-symbol "[X]"
138 "Symbol used to represent a selected checkbox.
139 See also `eww-form-checkbox-symbol'."
140 :version "24.4"
141 :group 'eww
142 :type '(choice (const "[X]")
143 (const "☒") ; Unicode BALLOT BOX WITH X
144 (const "☑") ; Unicode BALLOT BOX WITH CHECK
145 string))
147 (defcustom eww-form-checkbox-symbol "[ ]"
148 "Symbol used to represent a checkbox.
149 See also `eww-form-checkbox-selected-symbol'."
150 :version "24.4"
151 :group 'eww
152 :type '(choice (const "[ ]")
153 (const "☐") ; Unicode BALLOT BOX
154 string))
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."
161 :version "24.4"
162 :group 'eww)
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."
169 :version "25.1"
170 :group 'eww)
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."
177 :version "24.4"
178 :group 'eww)
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."
185 :version "24.4"
186 :group 'eww)
188 (defface eww-form-text
189 '((t (:background "#505050"
190 :foreground "white"
191 :box (:line-width 1))))
192 "Face for eww text inputs."
193 :version "24.4"
194 :group 'eww)
196 (defface eww-form-textarea
197 '((t (:background "#C0C0C0"
198 :foreground "black"
199 :box (:line-width 1))))
200 "Face for eww textarea inputs."
201 :version "24.4"
202 :group 'eww)
204 (defface eww-invalid-certificate
205 '((default :weight bold)
206 (((class color)) :foreground "red"))
207 "Face for web pages with invalid certificates."
208 :version "25.1"
209 :group 'eww)
211 (defface eww-valid-certificate
212 '((default :weight bold)
213 (((class color)) :foreground "ForestGreen"))
214 "Face for web pages with valid certificates."
215 :version "25.1"
216 :group 'eww)
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)
228 map))
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))
234 (uris nil))
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)))
239 (intern uri obseen)
240 (push uri uris)))))
241 (nreverse uris)))
243 ;;;###autoload
244 (defun eww (url)
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'."
248 (interactive
249 (let* ((uris (eww-suggested-uris))
250 (prompt (concat "Enter URL or keywords"
251 (if uris (format " (default %s)" (car uris)) "")
252 ": ")))
253 (list (read-string prompt nil nil uris))))
254 (setq url (eww--dwim-expand-url url))
255 (pop-to-buffer-same-window
256 (if (eq major-mode 'eww-mode)
257 (current-buffer)
258 (get-buffer-create "*eww*")))
259 (eww-setup-buffer)
260 ;; Check whether the domain only uses "Highly Restricted" Unicode
261 ;; IDNA characters. If not, transform to punycode to indicate that
262 ;; there may be funny business going on.
263 (let ((parsed (url-generic-parse-url url)))
264 (unless (puny-highly-restrictive-domain-p (url-host parsed))
265 (setf (url-host parsed) (puny-encode-domain (url-host parsed)))
266 (setq url (url-recreate-url parsed))))
267 (plist-put eww-data :url url)
268 (plist-put eww-data :title "")
269 (eww-update-header-line-format)
270 (let ((inhibit-read-only t))
271 (insert (format "Loading %s..." url))
272 (goto-char (point-min)))
273 (url-retrieve url 'eww-render
274 (list url nil (current-buffer))))
276 (defun eww--dwim-expand-url (url)
277 (setq url (string-trim url))
278 (cond ((string-match-p "\\`file:/" url))
279 ;; Don't mangle file: URLs at all.
280 ((string-match-p "\\`ftp://" url)
281 (user-error "FTP is not supported"))
283 ;; Anything that starts with something that vaguely looks
284 ;; like a protocol designator is interpreted as a full URL.
285 (if (or (string-match "\\`[A-Za-z]+:" url)
286 ;; Also try to match "naked" URLs like
287 ;; en.wikipedia.org/wiki/Free software
288 (string-match "\\`[A-Za-z_]+\\.[A-Za-z._]+/" url)
289 (and (= (length (split-string url)) 1)
290 (or (and (not (string-match-p "\\`[\"'].*[\"']\\'" url))
291 (> (length (split-string url "[.:]")) 1))
292 (string-match eww-local-regex url))))
293 (progn
294 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
295 (setq url (concat "http://" url)))
296 ;; Some sites do not redirect final /
297 (when (string= (url-filename (url-generic-parse-url url)) "")
298 (setq url (concat url "/"))))
299 (setq url (concat eww-search-prefix
300 (replace-regexp-in-string " " "+" url))))))
301 url)
303 ;;;###autoload (defalias 'browse-web 'eww)
305 ;;;###autoload
306 (defun eww-open-file (file)
307 "Render FILE using EWW."
308 (interactive "fFile: ")
309 (eww (concat "file://"
310 (and (memq system-type '(windows-nt ms-dos))
311 "/")
312 (expand-file-name file))))
314 ;;;###autoload
315 (defun eww-search-words (&optional beg end)
316 "Search the web for the text between BEG and END.
317 See the `eww-search-prefix' variable for the search engine used."
318 (interactive "r")
319 (eww (buffer-substring beg end)))
321 (defun eww-open-in-new-buffer ()
322 "Fetch link at point in a new EWW buffer."
323 (interactive)
324 (let ((url (eww-suggested-uris)))
325 (if (null url) (user-error "No link at point")
326 ;; clone useful to keep history, but
327 ;; should not clone from non-eww buffer
328 (with-current-buffer
329 (if (eq major-mode 'eww-mode) (clone-buffer)
330 (generate-new-buffer "*eww*"))
331 (unless (equal url (eww-current-url))
332 (eww-mode)
333 (eww (if (consp url) (car url) url)))))))
335 (defun eww-html-p (content-type)
336 "Return non-nil if CONTENT-TYPE designates an HTML content type.
337 Currently this means either text/html or application/xhtml+xml."
338 (member content-type '("text/html"
339 "application/xhtml+xml")))
341 (defun eww-render (status url &optional point buffer encode)
342 (let ((redirect (plist-get status :redirect)))
343 (when redirect
344 (setq url redirect)))
345 (let* ((headers (eww-parse-headers))
346 (content-type
347 (mail-header-parse-content-type
348 (if (zerop (length (cdr (assoc "content-type" headers))))
349 "text/plain"
350 (cdr (assoc "content-type" headers)))))
351 (charset (intern
352 (downcase
353 (or (cdr (assq 'charset (cdr content-type)))
354 (eww-detect-charset (eww-html-p (car content-type)))
355 "utf-8"))))
356 (data-buffer (current-buffer))
357 last-coding-system-used)
358 (with-current-buffer buffer
359 ;; Save the https peer status.
360 (plist-put eww-data :peer (plist-get status :peer))
361 ;; Make buffer listings more informative.
362 (setq list-buffers-directory url))
363 (unwind-protect
364 (progn
365 (cond
366 ((and eww-use-external-browser-for-content-type
367 (string-match-p eww-use-external-browser-for-content-type
368 (car content-type)))
369 (erase-buffer)
370 (insert "<title>Unsupported content type</title>")
371 (insert (format "<h1>Content-type %s is unsupported</h1>"
372 (car content-type)))
373 (insert (format "<a href=%S>Direct link to the document</a>"
374 url))
375 (goto-char (point-min))
376 (eww-display-html charset url nil point buffer encode))
377 ((eww-html-p (car content-type))
378 (eww-display-html charset url nil point buffer encode))
379 ((equal (car content-type) "application/pdf")
380 (eww-display-pdf))
381 ((string-match-p "\\`image/" (car content-type))
382 (eww-display-image buffer))
384 (eww-display-raw buffer (or encode charset 'utf-8))))
385 (with-current-buffer buffer
386 (plist-put eww-data :url url)
387 (eww-update-header-line-format)
388 (setq eww-history-position 0)
389 (and last-coding-system-used
390 (set-buffer-file-coding-system last-coding-system-used))
391 (run-hooks 'eww-after-render-hook)))
392 (kill-buffer data-buffer))))
394 (defun eww-parse-headers ()
395 (let ((headers nil))
396 (goto-char (point-min))
397 (while (and (not (eobp))
398 (not (eolp)))
399 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
400 (push (cons (downcase (match-string 1))
401 (match-string 2))
402 headers))
403 (forward-line 1))
404 (unless (eobp)
405 (forward-line 1))
406 headers))
408 (defun eww-detect-charset (html-p)
409 (let ((case-fold-search t)
410 (pt (point)))
411 (or (and html-p
412 (re-search-forward
413 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
414 (goto-char pt)
415 (match-string 1))
416 (and (looking-at
417 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
418 (match-string 1)))))
420 (declare-function libxml-parse-html-region "xml.c"
421 (start end &optional base-url discard-comments))
423 (defun eww-display-html (charset url &optional document point buffer encode)
424 (unless (fboundp 'libxml-parse-html-region)
425 (error "This function requires Emacs to be compiled with libxml2"))
426 (unless (buffer-live-p buffer)
427 (error "Buffer %s doesn't exist" buffer))
428 ;; There should be a better way to abort loading images
429 ;; asynchronously.
430 (setq url-queue nil)
431 (let ((document
432 (or document
433 (list
434 'base (list (cons 'href url))
435 (progn
436 (setq encode (or encode charset 'utf-8))
437 (condition-case nil
438 (decode-coding-region (point) (point-max) encode)
439 (coding-system-error nil))
440 (save-excursion
441 ;; Remove CRLF before parsing.
442 (while (re-search-forward "\r$" nil t)
443 (replace-match "" t t)))
444 (libxml-parse-html-region (point) (point-max))))))
445 (source (and (null document)
446 (buffer-substring (point) (point-max)))))
447 (with-current-buffer buffer
448 (setq bidi-paragraph-direction nil)
449 (plist-put eww-data :source source)
450 (plist-put eww-data :dom document)
451 (let ((inhibit-read-only t)
452 (inhibit-modification-hooks t)
453 (shr-target-id (url-target (url-generic-parse-url url)))
454 (shr-external-rendering-functions
455 (append
456 shr-external-rendering-functions
457 '((title . eww-tag-title)
458 (form . eww-tag-form)
459 (input . eww-tag-input)
460 (button . eww-form-submit)
461 (textarea . eww-tag-textarea)
462 (select . eww-tag-select)
463 (link . eww-tag-link)
464 (meta . eww-tag-meta)
465 (a . eww-tag-a)))))
466 (erase-buffer)
467 (shr-insert-document document)
468 (cond
469 (point
470 (goto-char point))
471 (shr-target-id
472 (goto-char (point-min))
473 (let ((point (next-single-property-change
474 (point-min) 'shr-target-id)))
475 (when point
476 (goto-char point))))
478 (goto-char (point-min))
479 ;; Don't leave point inside forms, because the normal eww
480 ;; commands aren't available there.
481 (while (and (not (eobp))
482 (get-text-property (point) 'eww-form))
483 (forward-line 1)))))
484 (eww-size-text-inputs))))
486 (defun eww-handle-link (dom)
487 (let* ((rel (dom-attr dom 'rel))
488 (href (dom-attr dom 'href))
489 (where (assoc
490 ;; The text associated with :rel is case-insensitive.
491 (if rel (downcase rel))
492 '(("next" . :next)
493 ;; Texinfo uses "previous", but HTML specifies
494 ;; "prev", so recognize both.
495 ("previous" . :previous)
496 ("prev" . :previous)
497 ;; HTML specifies "start" but also "contents",
498 ;; and Gtk seems to use "home". Recognize
499 ;; them all; but store them in different
500 ;; variables so that we can readily choose the
501 ;; "best" one.
502 ("start" . :start)
503 ("home" . :home)
504 ("contents" . :contents)
505 ("up" . :up)))))
506 (and href
507 where
508 (plist-put eww-data (cdr where) href))))
510 (defvar eww-redirect-level 1)
512 (defun eww-tag-meta (dom)
513 (when (and (cl-equalp (dom-attr dom 'http-equiv) "refresh")
514 (< eww-redirect-level 5))
515 (when-let (refresh (dom-attr dom 'content))
516 (when (or (string-match "^\\([0-9]+\\) *;.*url=\"\\([^\"]+\\)\"" refresh)
517 (string-match "^\\([0-9]+\\) *;.*url=\\([^ ]+\\)" refresh))
518 (let ((timeout (match-string 1 refresh))
519 (url (match-string 2 refresh))
520 (eww-redirect-level (1+ eww-redirect-level)))
521 (if (equal timeout "0")
522 (eww (shr-expand-url url))
523 (eww-tag-a
524 (dom-node 'a `((href . ,(shr-expand-url url)))
525 (format "Auto refresh in %s second%s disabled"
526 timeout
527 (if (equal timeout "1")
529 "s"))))))))))
531 (defun eww-tag-link (dom)
532 (eww-handle-link dom)
533 (shr-generic dom))
535 (defun eww-tag-a (dom)
536 (eww-handle-link dom)
537 (let ((start (point)))
538 (shr-tag-a dom)
539 (put-text-property start (point) 'keymap eww-link-keymap)))
541 (defun eww-update-header-line-format ()
542 (setq header-line-format
543 (and eww-header-line-format
544 (let ((title (plist-get eww-data :title))
545 (peer (plist-get eww-data :peer)))
546 (when (zerop (length title))
547 (setq title "[untitled]"))
548 ;; This connection has is https.
549 (when peer
550 (setq title
551 (propertize title 'face
552 (if (plist-get peer :warnings)
553 'eww-invalid-certificate
554 'eww-valid-certificate))))
555 (replace-regexp-in-string
556 "%" "%%"
557 (format-spec
558 eww-header-line-format
559 `((?u . ,(or (plist-get eww-data :url) ""))
560 (?t . ,title))))))))
562 (defun eww-tag-title (dom)
563 (plist-put eww-data :title
564 (replace-regexp-in-string
565 "^ \\| $" ""
566 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
567 (eww-update-header-line-format))
569 (defun eww-display-raw (buffer &optional encode)
570 (let ((data (buffer-substring (point) (point-max))))
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))
575 (erase-buffer)
576 (insert data)
577 (condition-case nil
578 (decode-coding-region (point-min) (1+ (length data)) encode)
579 (coding-system-error nil)))
580 (goto-char (point-min)))))
582 (defun eww-display-image (buffer)
583 (let ((data (shr-parse-image-data)))
584 (unless (buffer-live-p buffer)
585 (error "Buffer %s doesn't exist" buffer))
586 (with-current-buffer buffer
587 (let ((inhibit-read-only t))
588 (erase-buffer)
589 (shr-put-image data nil))
590 (goto-char (point-min)))))
592 (declare-function mailcap-view-mime "mailcap" (type))
593 (defun eww-display-pdf ()
594 (let ((data (buffer-substring (point) (point-max))))
595 (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
596 (let ((coding-system-for-write 'raw-text)
597 (inhibit-read-only t))
598 (erase-buffer)
599 (insert data)
600 (mailcap-view-mime "application/pdf")))
601 (goto-char (point-min)))
603 (defun eww-setup-buffer ()
604 (when (or (plist-get eww-data :url)
605 (plist-get eww-data :dom))
606 (eww-save-history))
607 (let ((inhibit-read-only t))
608 (remove-overlays)
609 (erase-buffer))
610 (setq bidi-paragraph-direction nil)
611 (unless (eq major-mode 'eww-mode)
612 (eww-mode)))
614 (defun eww-current-url nil
615 "Return URI of the Web page the current EWW buffer is visiting."
616 (plist-get eww-data :url))
618 (defun eww-links-at-point ()
619 "Return list of URIs, if any, linked at point."
620 (remq nil
621 (list (get-text-property (point) 'shr-url)
622 (get-text-property (point) 'image-url))))
624 (defun eww-view-source ()
625 "View the HTML source code of the current page."
626 (interactive)
627 (let ((buf (get-buffer-create "*eww-source*"))
628 (source (plist-get eww-data :source)))
629 (with-current-buffer buf
630 (let ((inhibit-read-only t))
631 (delete-region (point-min) (point-max))
632 (insert (or source "no source"))
633 (goto-char (point-min))
634 ;; Decode the source and set the buffer's encoding according
635 ;; to what the HTML source specifies in its 'charset' header,
636 ;; if any.
637 (let ((cs (find-auto-coding "" (point-max))))
638 (when (consp cs)
639 (setq cs (car cs))
640 (when (coding-system-p cs)
641 (decode-coding-region (point-min) (point-max) cs)
642 (setq buffer-file-coding-system last-coding-system-used))))
643 (when (fboundp 'html-mode)
644 (html-mode))))
645 (view-buffer buf)))
647 (defun eww-toggle-paragraph-direction ()
648 "Cycle the paragraph direction between left-to-right, right-to-left and auto."
649 (interactive)
650 (setq bidi-paragraph-direction
651 (cond ((eq bidi-paragraph-direction 'left-to-right)
652 nil)
653 ((eq bidi-paragraph-direction 'right-to-left)
654 'left-to-right)
656 'right-to-left)))
657 (message "The paragraph direction is now %s"
658 (if (null bidi-paragraph-direction)
659 "automatic"
660 bidi-paragraph-direction)))
662 (defun eww-readable ()
663 "View the main \"readable\" parts of the current web page.
664 This command uses heuristics to find the parts of the web page that
665 contains the main textual portion, leaving out navigation menus and
666 the like."
667 (interactive)
668 (let* ((old-data eww-data)
669 (dom (with-temp-buffer
670 (insert (plist-get old-data :source))
671 (condition-case nil
672 (decode-coding-region (point-min) (point-max) 'utf-8)
673 (coding-system-error nil))
674 (libxml-parse-html-region (point-min) (point-max))))
675 (base (plist-get eww-data :url)))
676 (eww-score-readability dom)
677 (eww-save-history)
678 (eww-display-html nil nil
679 (list 'base (list (cons 'href base))
680 (eww-highest-readability dom))
681 nil (current-buffer))
682 (dolist (elem '(:source :url :title :next :previous :up))
683 (plist-put eww-data elem (plist-get old-data elem)))
684 (eww-update-header-line-format)))
686 (defun eww-score-readability (node)
687 (let ((score -1))
688 (cond
689 ((memq (dom-tag node) '(script head comment))
690 (setq score -2))
691 ((eq (dom-tag node) 'meta)
692 (setq score -1))
693 ((eq (dom-tag node) 'img)
694 (setq score 2))
695 ((eq (dom-tag node) 'a)
696 (setq score (- (length (split-string (dom-text node))))))
698 (dolist (elem (dom-children node))
699 (cond
700 ((stringp elem)
701 (setq score (+ score (length (split-string elem)))))
702 ((consp elem)
703 (setq score (+ score
704 (or (cdr (assoc :eww-readability-score (cdr elem)))
705 (eww-score-readability elem)))))))))
706 ;; Cache the score of the node to avoid recomputing all the time.
707 (dom-set-attribute node :eww-readability-score score)
708 score))
710 (defun eww-highest-readability (node)
711 (let ((result node)
712 highest)
713 (dolist (elem (dom-non-text-children node))
714 (when (> (or (dom-attr
715 (setq highest (eww-highest-readability elem))
716 :eww-readability-score)
717 most-negative-fixnum)
718 (or (dom-attr result :eww-readability-score)
719 most-negative-fixnum))
720 (setq result highest)))
721 result))
723 (defvar eww-mode-map
724 (let ((map (make-sparse-keymap)))
725 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
726 (define-key map "G" 'eww)
727 (define-key map [?\M-\r] 'eww-open-in-new-buffer)
728 (define-key map [?\t] 'shr-next-link)
729 (define-key map [?\M-\t] 'shr-previous-link)
730 (define-key map [backtab] 'shr-previous-link)
731 (define-key map [delete] 'scroll-down-command)
732 (define-key map "l" 'eww-back-url)
733 (define-key map "r" 'eww-forward-url)
734 (define-key map "n" 'eww-next-url)
735 (define-key map "p" 'eww-previous-url)
736 (define-key map "u" 'eww-up-url)
737 (define-key map "t" 'eww-top-url)
738 (define-key map "&" 'eww-browse-with-external-browser)
739 (define-key map "d" 'eww-download)
740 (define-key map "w" 'eww-copy-page-url)
741 (define-key map "C" 'url-cookie-list)
742 (define-key map "v" 'eww-view-source)
743 (define-key map "R" 'eww-readable)
744 (define-key map "H" 'eww-list-histories)
745 (define-key map "E" 'eww-set-character-encoding)
746 (define-key map "s" 'eww-switch-to-buffer)
747 (define-key map "S" 'eww-list-buffers)
748 (define-key map "F" 'eww-toggle-fonts)
749 (define-key map "D" 'eww-toggle-paragraph-direction)
750 (define-key map [(meta C)] 'eww-toggle-colors)
752 (define-key map "b" 'eww-add-bookmark)
753 (define-key map "B" 'eww-list-bookmarks)
754 (define-key map [(meta n)] 'eww-next-bookmark)
755 (define-key map [(meta p)] 'eww-previous-bookmark)
757 (easy-menu-define nil map ""
758 '("Eww"
759 ["Exit" quit-window t]
760 ["Close browser" quit-window t]
761 ["Reload" eww-reload t]
762 ["Follow URL in new buffer" eww-open-in-new-buffer]
763 ["Back to previous page" eww-back-url
764 :active (not (zerop (length eww-history)))]
765 ["Forward to next page" eww-forward-url
766 :active (not (zerop eww-history-position))]
767 ["Browse with external browser" eww-browse-with-external-browser t]
768 ["Download" eww-download t]
769 ["View page source" eww-view-source]
770 ["Copy page URL" eww-copy-page-url t]
771 ["List histories" eww-list-histories t]
772 ["Switch to buffer" eww-switch-to-buffer t]
773 ["List buffers" eww-list-buffers t]
774 ["Add bookmark" eww-add-bookmark t]
775 ["List bookmarks" eww-list-bookmarks t]
776 ["List cookies" url-cookie-list t]
777 ["Toggle fonts" eww-toggle-fonts t]
778 ["Toggle colors" eww-toggle-colors t]
779 ["Character Encoding" eww-set-character-encoding]
780 ["Toggle Paragraph Direction" eww-toggle-paragraph-direction]))
781 map))
783 (defvar eww-tool-bar-map
784 (let ((map (make-sparse-keymap)))
785 (dolist (tool-bar-item
786 '((quit-window . "close")
787 (eww-reload . "refresh")
788 (eww-back-url . "left-arrow")
789 (eww-forward-url . "right-arrow")
790 (eww-view-source . "show")
791 (eww-copy-page-url . "copy")
792 (eww-add-bookmark . "bookmark_add"))) ;; ...
793 (tool-bar-local-item-from-menu
794 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
795 map)
796 "Tool bar for `eww-mode'.")
798 ;; Autoload cookie needed by desktop.el.
799 ;;;###autoload
800 (define-derived-mode eww-mode special-mode "eww"
801 "Mode for browsing the web."
802 (setq-local eww-data (list :title ""))
803 (setq-local browse-url-browser-function #'eww-browse-url)
804 (add-hook 'after-change-functions #'eww-process-text-input nil t)
805 (setq-local eww-history nil)
806 (setq-local eww-history-position 0)
807 (when (boundp 'tool-bar-map)
808 (setq-local tool-bar-map eww-tool-bar-map))
809 ;; desktop support
810 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
811 ;; multi-page isearch support
812 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
813 (setq truncate-lines t)
814 (buffer-disable-undo)
815 (setq buffer-read-only t))
817 ;;;###autoload
818 (defun eww-browse-url (url &optional new-window)
819 (when new-window
820 (pop-to-buffer-same-window
821 (generate-new-buffer
822 (format "*eww-%s*" (url-host (url-generic-parse-url
823 (eww--dwim-expand-url url))))))
824 (eww-mode))
825 (eww url))
827 (defun eww-back-url ()
828 "Go to the previously displayed page."
829 (interactive)
830 (when (>= eww-history-position (length eww-history))
831 (user-error "No previous page"))
832 (eww-save-history)
833 (setq eww-history-position (+ eww-history-position 2))
834 (eww-restore-history (elt eww-history (1- eww-history-position))))
836 (defun eww-forward-url ()
837 "Go to the next displayed page."
838 (interactive)
839 (when (zerop eww-history-position)
840 (user-error "No next page"))
841 (eww-save-history)
842 (eww-restore-history (elt eww-history (1- eww-history-position))))
844 (defun eww-restore-history (elem)
845 (let ((inhibit-read-only t)
846 (inhibit-modification-hooks t)
847 (text (plist-get elem :text)))
848 (setq eww-data elem)
849 (if (null text)
850 (eww-reload) ; FIXME: restore :point?
851 (erase-buffer)
852 (insert text)
853 (goto-char (plist-get elem :point))
854 ;; Make buffer listings more informative.
855 (setq list-buffers-directory (plist-get elem :url))
856 (eww-update-header-line-format))))
858 (defun eww-next-url ()
859 "Go to the page marked `next'.
860 A page is marked `next' if rel=\"next\" appears in a <link>
861 or <a> tag."
862 (interactive)
863 (if (plist-get eww-data :next)
864 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
865 (plist-get eww-data :url)))
866 (user-error "No `next' on this page")))
868 (defun eww-previous-url ()
869 "Go to the page marked `previous'.
870 A page is marked `previous' if rel=\"previous\" appears in a <link>
871 or <a> tag."
872 (interactive)
873 (if (plist-get eww-data :previous)
874 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
875 (plist-get eww-data :url)))
876 (user-error "No `previous' on this page")))
878 (defun eww-up-url ()
879 "Go to the page marked `up'.
880 A page is marked `up' if rel=\"up\" appears in a <link>
881 or <a> tag."
882 (interactive)
883 (if (plist-get eww-data :up)
884 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
885 (plist-get eww-data :url)))
886 (user-error "No `up' on this page")))
888 (defun eww-top-url ()
889 "Go to the page marked `top'.
890 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
891 appears in a <link> or <a> tag."
892 (interactive)
893 (let ((best-url (or (plist-get eww-data :start)
894 (plist-get eww-data :contents)
895 (plist-get eww-data :home))))
896 (if best-url
897 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
898 (user-error "No `top' for this page"))))
900 (defun eww-reload (&optional local encode)
901 "Reload the current page.
902 If LOCAL (the command prefix), don't reload the page from the
903 network, but just re-display the HTML already fetched."
904 (interactive "P")
905 (let ((url (plist-get eww-data :url)))
906 (if local
907 (if (null (plist-get eww-data :dom))
908 (error "No current HTML data")
909 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
910 (point) (current-buffer)))
911 (url-retrieve url 'eww-render
912 (list url (point) (current-buffer) encode)))))
914 ;; Form support.
916 (defvar eww-form nil)
918 (defvar eww-submit-map
919 (let ((map (make-sparse-keymap)))
920 (define-key map "\r" 'eww-submit)
921 (define-key map [(control c) (control c)] 'eww-submit)
922 map))
924 (defvar eww-submit-file
925 (let ((map (make-sparse-keymap)))
926 (define-key map "\r" 'eww-select-file)
927 (define-key map [(control c) (control c)] 'eww-submit)
928 map))
930 (defvar eww-checkbox-map
931 (let ((map (make-sparse-keymap)))
932 (define-key map " " 'eww-toggle-checkbox)
933 (define-key map "\r" 'eww-toggle-checkbox)
934 (define-key map [(control c) (control c)] 'eww-submit)
935 map))
937 (defvar eww-text-map
938 (let ((map (make-keymap)))
939 (set-keymap-parent map text-mode-map)
940 (define-key map "\r" 'eww-submit)
941 (define-key map [(control a)] 'eww-beginning-of-text)
942 (define-key map [(control c) (control c)] 'eww-submit)
943 (define-key map [(control e)] 'eww-end-of-text)
944 (define-key map [?\t] 'shr-next-link)
945 (define-key map [?\M-\t] 'shr-previous-link)
946 map))
948 (defvar eww-textarea-map
949 (let ((map (make-keymap)))
950 (set-keymap-parent map text-mode-map)
951 (define-key map "\r" 'forward-line)
952 (define-key map [(control c) (control c)] 'eww-submit)
953 (define-key map [?\t] 'shr-next-link)
954 (define-key map [?\M-\t] 'shr-previous-link)
955 map))
957 (defvar eww-select-map
958 (let ((map (make-sparse-keymap)))
959 (define-key map "\r" 'eww-change-select)
960 (define-key map [(control c) (control c)] 'eww-submit)
961 map))
963 (defun eww-beginning-of-text ()
964 "Move to the start of the input field."
965 (interactive)
966 (goto-char (eww-beginning-of-field)))
968 (defun eww-end-of-text ()
969 "Move to the end of the text in the input field."
970 (interactive)
971 (goto-char (eww-end-of-field))
972 (let ((start (eww-beginning-of-field)))
973 (while (and (equal (following-char) ? )
974 (> (point) start))
975 (forward-char -1))
976 (when (> (point) start)
977 (forward-char 1))))
979 (defun eww-beginning-of-field ()
980 (cond
981 ((bobp)
982 (point))
983 ((not (eq (get-text-property (point) 'eww-form)
984 (get-text-property (1- (point)) 'eww-form)))
985 (point))
987 (previous-single-property-change
988 (point) 'eww-form nil (point-min)))))
990 (defun eww-end-of-field ()
991 (1- (next-single-property-change
992 (point) 'eww-form nil (point-max))))
994 (defun eww-tag-form (dom)
995 (let ((eww-form (list (cons :method (dom-attr dom 'method))
996 (cons :action (dom-attr dom 'action))))
997 (start (point)))
998 (insert "\n")
999 (shr-ensure-paragraph)
1000 (shr-generic dom)
1001 (unless (bolp)
1002 (insert "\n"))
1003 (insert "\n")
1004 (when (> (point) start)
1005 (put-text-property start (1+ start)
1006 'eww-form eww-form))))
1008 (defun eww-form-submit (dom)
1009 (let ((start (point))
1010 (value (dom-attr dom 'value)))
1011 (setq value
1012 (if (zerop (length value))
1013 "Submit"
1014 value))
1015 (insert value)
1016 (add-face-text-property start (point) 'eww-form-submit)
1017 (put-text-property start (point) 'eww-form
1018 (list :eww-form eww-form
1019 :value value
1020 :type "submit"
1021 :name (dom-attr dom 'name)))
1022 (put-text-property start (point) 'keymap eww-submit-map)
1023 (insert " ")))
1025 (defun eww-form-checkbox (dom)
1026 (let ((start (point)))
1027 (if (dom-attr dom 'checked)
1028 (insert eww-form-checkbox-selected-symbol)
1029 (insert eww-form-checkbox-symbol))
1030 (add-face-text-property start (point) 'eww-form-checkbox)
1031 (put-text-property start (point) 'eww-form
1032 (list :eww-form eww-form
1033 :value (dom-attr dom 'value)
1034 :type (downcase (dom-attr dom 'type))
1035 :checked (dom-attr dom 'checked)
1036 :name (dom-attr dom 'name)))
1037 (put-text-property start (point) 'keymap eww-checkbox-map)
1038 (insert " ")))
1040 (defun eww-form-file (dom)
1041 (let ((start (point))
1042 (value (dom-attr dom 'value)))
1043 (setq value
1044 (if (zerop (length value))
1045 " No file selected"
1046 value))
1047 (insert "Browse")
1048 (add-face-text-property start (point) 'eww-form-file)
1049 (insert value)
1050 (put-text-property start (point) 'eww-form
1051 (list :eww-form eww-form
1052 :value (dom-attr dom 'value)
1053 :type (downcase (dom-attr dom 'type))
1054 :name (dom-attr dom 'name)))
1055 (put-text-property start (point) 'keymap eww-submit-file)
1056 (insert " ")))
1058 (defun eww-select-file ()
1059 "Change the value of the upload file menu under point."
1060 (interactive)
1061 (let* ((input (get-text-property (point) 'eww-form)))
1062 (let ((filename
1063 (let ((insert-default-directory t))
1064 (read-file-name "filename: "))))
1065 (eww-update-field filename (length "Browse"))
1066 (plist-put input :filename filename))))
1068 (defun eww-form-text (dom)
1069 (let ((start (point))
1070 (type (downcase (or (dom-attr dom 'type) "text")))
1071 (value (or (dom-attr dom 'value) ""))
1072 (width (string-to-number (or (dom-attr dom 'size) "40")))
1073 (readonly-property (if (or (dom-attr dom 'disabled)
1074 (dom-attr dom 'readonly))
1075 'read-only
1076 'inhibit-read-only)))
1077 (insert value)
1078 (when (< (length value) width)
1079 (insert (make-string (- width (length value)) ? )))
1080 (put-text-property start (point) 'face 'eww-form-text)
1081 (put-text-property start (point) 'inhibit-read-only t)
1082 (put-text-property start (point) 'local-map eww-text-map)
1083 (put-text-property start (point) readonly-property t)
1084 (put-text-property start (point) 'eww-form
1085 (list :eww-form eww-form
1086 :value value
1087 :type type
1088 :name (dom-attr dom 'name)))
1089 (insert " ")))
1091 (defconst eww-text-input-types '("text" "password" "textarea"
1092 "color" "date" "datetime" "datetime-local"
1093 "email" "month" "number" "search" "tel"
1094 "time" "url" "week")
1095 "List of input types which represent a text input.
1096 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1098 (defun eww-process-text-input (beg end replace-length)
1099 (when-let (pos (and (< (1+ end) (point-max))
1100 (> (1- end) (point-min))
1101 (cond
1102 ((get-text-property (1+ end) 'eww-form)
1103 (1+ end))
1104 ((get-text-property (1- end) 'eww-form)
1105 (1- end)))))
1106 (let* ((form (get-text-property pos 'eww-form))
1107 (properties (text-properties-at pos))
1108 (buffer-undo-list t)
1109 (inhibit-read-only t)
1110 (length (- end beg replace-length))
1111 (type (plist-get form :type)))
1112 (when (and form
1113 (member type eww-text-input-types))
1114 (cond
1115 ((> length 0)
1116 ;; Delete some space at the end.
1117 (save-excursion
1118 (goto-char
1119 (if (equal type "textarea")
1120 (1- (line-end-position))
1121 (eww-end-of-field)))
1122 (while (and (> length 0)
1123 (eql (char-after (1- (point))) ? ))
1124 (delete-region (1- (point)) (point))
1125 (cl-decf length))))
1126 ((< length 0)
1127 ;; Add padding.
1128 (save-excursion
1129 (goto-char end)
1130 (goto-char
1131 (if (equal type "textarea")
1132 (1- (line-end-position))
1133 (1+ (eww-end-of-field))))
1134 (let ((start (point)))
1135 (insert (make-string (abs length) ? ))
1136 (set-text-properties start (point) properties))
1137 (goto-char (1- end)))))
1138 (set-text-properties (plist-get form :start) (plist-get form :end)
1139 properties)
1140 (let ((value (buffer-substring-no-properties
1141 (eww-beginning-of-field)
1142 (eww-end-of-field))))
1143 (when (string-match " +\\'" value)
1144 (setq value (substring value 0 (match-beginning 0))))
1145 (plist-put form :value value)
1146 (when (equal type "password")
1147 ;; Display passwords as asterisks.
1148 (let ((start (eww-beginning-of-field)))
1149 (put-text-property
1150 start (+ start (length value))
1151 'display (make-string (length value) ?*)))))))))
1153 (defun eww-tag-textarea (dom)
1154 (let ((start (point))
1155 (value (or (dom-attr dom 'value) ""))
1156 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1157 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1158 end)
1159 (shr-ensure-newline)
1160 (insert value)
1161 (shr-ensure-newline)
1162 (when (< (count-lines start (point)) lines)
1163 (dotimes (_ (- lines (count-lines start (point))))
1164 (insert "\n")))
1165 (setq end (point-marker))
1166 (goto-char start)
1167 (while (< (point) end)
1168 (end-of-line)
1169 (let ((pad (- width (- (point) (line-beginning-position)))))
1170 (when (> pad 0)
1171 (insert (make-string pad ? ))))
1172 (add-face-text-property (line-beginning-position)
1173 (point) 'eww-form-textarea)
1174 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1175 (put-text-property (line-beginning-position) (point)
1176 'local-map eww-textarea-map)
1177 (forward-line 1))
1178 (put-text-property start (point) 'eww-form
1179 (list :eww-form eww-form
1180 :value value
1181 :type "textarea"
1182 :name (dom-attr dom 'name)))))
1184 (defun eww-tag-input (dom)
1185 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1186 (start (point)))
1187 (cond
1188 ((or (equal type "checkbox")
1189 (equal type "radio"))
1190 (eww-form-checkbox dom))
1191 ((equal type "file")
1192 (eww-form-file dom))
1193 ((equal type "submit")
1194 (eww-form-submit dom))
1195 ((equal type "hidden")
1196 (let ((form eww-form)
1197 (name (dom-attr dom 'name)))
1198 ;; Don't add <input type=hidden> elements repeatedly.
1199 (while (and form
1200 (or (not (consp (car form)))
1201 (not (eq (caar form) 'hidden))
1202 (not (equal (plist-get (cdr (car form)) :name)
1203 name))))
1204 (setq form (cdr form)))
1205 (unless form
1206 (nconc eww-form (list
1207 (list 'hidden
1208 :name name
1209 :value (or (dom-attr dom 'value) "")))))))
1211 (eww-form-text dom)))
1212 (unless (= start (point))
1213 (put-text-property start (1+ start) 'help-echo "Input field")
1214 ;; Mark this as an element we can TAB to.
1215 (put-text-property start (1+ start) 'shr-url dom))))
1217 (defun eww-tag-select (dom)
1218 (shr-ensure-paragraph)
1219 (let ((menu (list :name (dom-attr dom 'name)
1220 :eww-form eww-form))
1221 (options nil)
1222 (start (point))
1223 (max 0)
1224 opelem)
1225 (if (eq (dom-tag dom) 'optgroup)
1226 (dolist (groupelem (dom-children dom))
1227 (unless (dom-attr groupelem 'disabled)
1228 (setq opelem (append opelem (list groupelem)))))
1229 (setq opelem (list dom)))
1230 (dolist (elem opelem)
1231 (when (eq (dom-tag elem) 'option)
1232 (when (dom-attr elem 'selected)
1233 (nconc menu (list :value (dom-attr elem 'value))))
1234 (let ((display (dom-text elem)))
1235 (setq max (max max (length display)))
1236 (push (list 'item
1237 :value (dom-attr elem 'value)
1238 :display display)
1239 options))))
1240 (when options
1241 (setq options (nreverse options))
1242 ;; If we have no selected values, default to the first value.
1243 (unless (plist-get menu :value)
1244 (nconc menu (list :value (nth 2 (car options)))))
1245 (nconc menu options)
1246 (let ((selected (eww-select-display menu)))
1247 (insert selected
1248 (make-string (- max (length selected)) ? )))
1249 (put-text-property start (point) 'eww-form menu)
1250 (add-face-text-property start (point) 'eww-form-select)
1251 (put-text-property start (point) 'keymap eww-select-map)
1252 (unless (= start (point))
1253 (put-text-property start (1+ start) 'help-echo "select field"))
1254 (shr-ensure-paragraph))))
1256 (defun eww-select-display (select)
1257 (let ((value (plist-get select :value))
1258 display)
1259 (dolist (elem select)
1260 (when (and (consp elem)
1261 (eq (car elem) 'item)
1262 (equal value (plist-get (cdr elem) :value)))
1263 (setq display (plist-get (cdr elem) :display))))
1264 display))
1266 (defun eww-change-select ()
1267 "Change the value of the select drop-down menu under point."
1268 (interactive)
1269 (let* ((input (get-text-property (point) 'eww-form))
1270 (completion-ignore-case t)
1271 (options
1272 (delq nil
1273 (mapcar (lambda (elem)
1274 (and (consp elem)
1275 (eq (car elem) 'item)
1276 (cons (plist-get (cdr elem) :display)
1277 (plist-get (cdr elem) :value))))
1278 input)))
1279 (display
1280 (completing-read "Change value: " options nil 'require-match))
1281 (inhibit-read-only t))
1282 (plist-put input :value (cdr (assoc-string display options t)))
1283 (goto-char
1284 (eww-update-field display))))
1286 (defun eww-update-field (string &optional offset)
1287 (unless offset
1288 (setq offset 0))
1289 (let ((properties (text-properties-at (point)))
1290 (start (+ (eww-beginning-of-field) offset))
1291 (current-end (1+ (eww-end-of-field)))
1292 (new-end (+ (eww-beginning-of-field) (length string)))
1293 (inhibit-read-only t))
1294 (delete-region start current-end)
1295 (forward-char offset)
1296 (insert string
1297 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1298 (when (= 0 offset)
1299 (set-text-properties start new-end properties))
1300 start))
1302 (defun eww-toggle-checkbox ()
1303 "Toggle the value of the checkbox under point."
1304 (interactive)
1305 (let* ((input (get-text-property (point) 'eww-form))
1306 (type (plist-get input :type)))
1307 (if (equal type "checkbox")
1308 (goto-char
1310 (if (plist-get input :checked)
1311 (progn
1312 (plist-put input :checked nil)
1313 (eww-update-field eww-form-checkbox-symbol))
1314 (plist-put input :checked t)
1315 (eww-update-field eww-form-checkbox-selected-symbol))))
1316 ;; Radio button. Switch all other buttons off.
1317 (let ((name (plist-get input :name)))
1318 (save-excursion
1319 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1320 (when (equal (plist-get (cdr elem) :name) name)
1321 (goto-char (car elem))
1322 (if (not (eq (cdr elem) input))
1323 (progn
1324 (plist-put input :checked nil)
1325 (eww-update-field eww-form-checkbox-symbol))
1326 (plist-put input :checked t)
1327 (eww-update-field eww-form-checkbox-selected-symbol)))))
1328 (forward-char 1)))))
1330 (defun eww-inputs (form)
1331 (let ((start (point-min))
1332 (inputs nil))
1333 (while (and start
1334 (< start (point-max)))
1335 (when (or (get-text-property start 'eww-form)
1336 (setq start (next-single-property-change start 'eww-form)))
1337 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1338 form)
1339 (push (cons start (get-text-property start 'eww-form))
1340 inputs))
1341 (setq start (next-single-property-change start 'eww-form))))
1342 (nreverse inputs)))
1344 (defun eww-size-text-inputs ()
1345 (let ((start (point-min)))
1346 (while (and start
1347 (< start (point-max)))
1348 (when (or (get-text-property start 'eww-form)
1349 (setq start (next-single-property-change start 'eww-form)))
1350 (let ((props (get-text-property start 'eww-form)))
1351 (plist-put props :start start)
1352 (setq start (next-single-property-change
1353 start 'eww-form nil (point-max)))
1354 (plist-put props :end start))))))
1356 (defun eww-input-value (input)
1357 (let ((type (plist-get input :type))
1358 (value (plist-get input :value)))
1359 (cond
1360 ((equal type "textarea")
1361 (with-temp-buffer
1362 (insert value)
1363 (goto-char (point-min))
1364 (while (re-search-forward "^ +\n\\| +$" nil t)
1365 (replace-match "" t t))
1366 (buffer-string)))
1368 (if (string-match " +\\'" value)
1369 (substring value 0 (match-beginning 0))
1370 value)))))
1372 (defun eww-submit ()
1373 "Submit the current form."
1374 (interactive)
1375 (let* ((this-input (get-text-property (point) 'eww-form))
1376 (form (plist-get this-input :eww-form))
1377 values next-submit)
1378 (dolist (elem (sort (eww-inputs form)
1379 (lambda (o1 o2)
1380 (< (car o1) (car o2)))))
1381 (let* ((input (cdr elem))
1382 (input-start (car elem))
1383 (name (plist-get input :name)))
1384 (when name
1385 (cond
1386 ((member (plist-get input :type) '("checkbox" "radio"))
1387 (when (plist-get input :checked)
1388 (push (cons name (plist-get input :value))
1389 values)))
1390 ((equal (plist-get input :type) "file")
1391 (push (cons "file"
1392 (list (cons "filedata"
1393 (with-temp-buffer
1394 (insert-file-contents
1395 (plist-get input :filename))
1396 (buffer-string)))
1397 (cons "name" (plist-get input :name))
1398 (cons "filename" (plist-get input :filename))))
1399 values))
1400 ((equal (plist-get input :type) "submit")
1401 ;; We want the values from buttons if we hit a button if
1402 ;; we hit enter on it, or if it's the first button after
1403 ;; the field we did hit return on.
1404 (when (or (eq input this-input)
1405 (and (not (eq input this-input))
1406 (null next-submit)
1407 (> input-start (point))))
1408 (setq next-submit t)
1409 (push (cons name (plist-get input :value))
1410 values)))
1412 (push (cons name (eww-input-value input))
1413 values))))))
1414 (dolist (elem form)
1415 (when (and (consp elem)
1416 (eq (car elem) 'hidden))
1417 (push (cons (plist-get (cdr elem) :name)
1418 (or (plist-get (cdr elem) :value) ""))
1419 values)))
1420 (if (and (stringp (cdr (assq :method form)))
1421 (equal (downcase (cdr (assq :method form))) "post"))
1422 (let ((mtype))
1423 (dolist (x values mtype)
1424 (if (equal (car x) "file")
1425 (progn
1426 (setq mtype "multipart/form-data"))))
1427 (cond ((equal mtype "multipart/form-data")
1428 (let ((boundary (mml-compute-boundary '())))
1429 (let ((url-request-method "POST")
1430 (url-request-extra-headers
1431 (list (cons "Content-Type"
1432 (concat "multipart/form-data; boundary="
1433 boundary))))
1434 (url-request-data
1435 (mm-url-encode-multipart-form-data values boundary)))
1436 (eww-browse-url (shr-expand-url
1437 (cdr (assq :action form))
1438 (plist-get eww-data :url))))))
1440 (let ((url-request-method "POST")
1441 (url-request-extra-headers
1442 '(("Content-Type" .
1443 "application/x-www-form-urlencoded")))
1444 (url-request-data
1445 (mm-url-encode-www-form-urlencoded values)))
1446 (eww-browse-url (shr-expand-url
1447 (cdr (assq :action form))
1448 (plist-get eww-data :url)))))))
1449 (eww-browse-url
1450 (concat
1451 (if (cdr (assq :action form))
1452 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1453 (plist-get eww-data :url))
1455 (mm-url-encode-www-form-urlencoded values))))))
1457 (defun eww-browse-with-external-browser (&optional url)
1458 "Browse the current URL with an external browser.
1459 The browser to used is specified by the `shr-external-browser' variable."
1460 (interactive)
1461 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1463 (defun eww-follow-link (&optional external mouse-event)
1464 "Browse the URL under point.
1465 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1466 If EXTERNAL is double prefix, browse in new buffer."
1467 (interactive (list current-prefix-arg last-nonmenu-event))
1468 (mouse-set-point mouse-event)
1469 (let ((url (get-text-property (point) 'shr-url)))
1470 (cond
1471 ((not url)
1472 (message "No link under point"))
1473 ((string-match "^mailto:" url)
1474 (browse-url-mail url))
1475 ((and (consp external) (<= (car external) 4))
1476 (funcall shr-external-browser url))
1477 ;; This is a #target url in the same page as the current one.
1478 ((and (url-target (url-generic-parse-url url))
1479 (eww-same-page-p url (plist-get eww-data :url)))
1480 (let ((dom (plist-get eww-data :dom)))
1481 (eww-save-history)
1482 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1484 (eww-browse-url url external)))))
1486 (defun eww-same-page-p (url1 url2)
1487 "Return non-nil if URL1 and URL2 represent the same page.
1488 Differences in #targets are ignored."
1489 (let ((obj1 (url-generic-parse-url url1))
1490 (obj2 (url-generic-parse-url url2)))
1491 (setf (url-target obj1) nil)
1492 (setf (url-target obj2) nil)
1493 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1495 (defun eww-copy-page-url ()
1496 "Copy the URL of the current page into the kill ring."
1497 (interactive)
1498 (message "%s" (plist-get eww-data :url))
1499 (kill-new (plist-get eww-data :url)))
1501 (defun eww-download ()
1502 "Download URL under point to `eww-download-directory'."
1503 (interactive)
1504 (access-file eww-download-directory "Download failed")
1505 (let ((url (get-text-property (point) 'shr-url)))
1506 (if (not url)
1507 (message "No URL under point")
1508 (url-retrieve url 'eww-download-callback (list url)))))
1510 (defun eww-download-callback (status url)
1511 (unless (plist-get status :error)
1512 (let* ((obj (url-generic-parse-url url))
1513 (path (car (url-path-and-query obj)))
1514 (file (eww-make-unique-file-name
1515 (eww-decode-url-file-name (file-name-nondirectory path))
1516 eww-download-directory)))
1517 (goto-char (point-min))
1518 (re-search-forward "\r?\n\r?\n")
1519 (write-region (point) (point-max) file)
1520 (message "Saved %s" file))))
1522 (defun eww-decode-url-file-name (string)
1523 (let* ((binary (url-unhex-string string))
1524 (decoded
1525 (decode-coding-string
1526 binary
1527 ;; Possibly set by `universal-coding-system-argument'.
1528 (or coding-system-for-read
1529 ;; RFC 3986 says that %AB stuff is utf-8.
1530 (if (equal (decode-coding-string binary 'utf-8)
1531 '(unicode))
1532 'utf-8
1533 ;; But perhaps not.
1534 (car (detect-coding-string binary))))))
1535 (encodes (find-coding-systems-string decoded)))
1536 (if (or (equal encodes '(undecided))
1537 (memq (coding-system-base (or file-name-coding-system
1538 default-file-name-coding-system))
1539 encodes))
1540 decoded
1541 ;; If we can't encode the decoded file name (due to language
1542 ;; environment settings), then we return the original, hexified
1543 ;; string.
1544 string)))
1546 (defun eww-make-unique-file-name (file directory)
1547 (cond
1548 ((zerop (length file))
1549 (setq file "!"))
1550 ((string-match "\\`[.]" file)
1551 (setq file (concat "!" file))))
1552 (let ((count 1)
1553 (stem file)
1554 (suffix ""))
1555 (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1556 (setq stem (match-string 1 file)
1557 suffix (match-string 2)))
1558 (while (file-exists-p (expand-file-name file directory))
1559 (setq file (format "%s(%d)%s" stem count suffix))
1560 (setq count (1+ count)))
1561 (expand-file-name file directory)))
1563 (defun eww-set-character-encoding (charset)
1564 "Set character encoding to CHARSET.
1565 If CHARSET is nil then use UTF-8."
1566 (interactive "zUse character set (default utf-8): ")
1567 (if (null charset)
1568 (eww-reload nil 'utf-8)
1569 (eww-reload nil charset)))
1571 (defun eww-switch-to-buffer ()
1572 "Prompt for an EWW buffer to display in the selected window."
1573 (interactive)
1574 (let ((completion-extra-properties
1575 '(:annotation-function (lambda (buf)
1576 (with-current-buffer buf
1577 (format " %s" (eww-current-url)))))))
1578 (pop-to-buffer-same-window
1579 (read-buffer "Switch to EWW buffer: "
1580 (cl-loop for buf in (nreverse (buffer-list))
1581 if (with-current-buffer buf (derived-mode-p 'eww-mode))
1582 return buf)
1584 (lambda (bufn)
1585 (with-current-buffer
1586 (if (consp bufn) (cdr bufn) (get-buffer bufn))
1587 (derived-mode-p 'eww-mode)))))))
1589 (defun eww-toggle-fonts ()
1590 "Toggle whether to use monospaced or font-enabled layouts."
1591 (interactive)
1592 (setq shr-use-fonts (not shr-use-fonts))
1593 (eww-reload)
1594 (message "Proportional fonts are now %s"
1595 (if shr-use-fonts "on" "off")))
1597 (defun eww-toggle-colors ()
1598 "Toggle whether to use HTML-specified colors or not."
1599 (interactive)
1600 (message "Colors are now %s"
1601 (if (setq shr-use-colors (not shr-use-colors))
1602 "on"
1603 "off"))
1604 (eww-reload))
1606 ;;; Bookmarks code
1608 (defvar eww-bookmarks nil)
1610 (defun eww-add-bookmark ()
1611 "Bookmark the current page."
1612 (interactive)
1613 (eww-read-bookmarks)
1614 (dolist (bookmark eww-bookmarks)
1615 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1616 (user-error "Already bookmarked")))
1617 (when (y-or-n-p "Bookmark this page?")
1618 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1619 (plist-get eww-data :title))))
1620 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1621 (push (list :url (plist-get eww-data :url)
1622 :title title
1623 :time (current-time-string))
1624 eww-bookmarks))
1625 (eww-write-bookmarks)
1626 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1627 (plist-get eww-data :title))))
1629 (defun eww-write-bookmarks ()
1630 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1631 (insert ";; Auto-generated file; don't edit\n")
1632 (pp eww-bookmarks (current-buffer))))
1634 (defun eww-read-bookmarks ()
1635 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1636 (setq eww-bookmarks
1637 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1638 (with-temp-buffer
1639 (insert-file-contents file)
1640 (read (current-buffer)))))))
1642 ;;;###autoload
1643 (defun eww-list-bookmarks ()
1644 "Display the bookmarks."
1645 (interactive)
1646 (pop-to-buffer "*eww bookmarks*")
1647 (eww-bookmark-prepare))
1649 (defun eww-bookmark-prepare ()
1650 (eww-read-bookmarks)
1651 (unless eww-bookmarks
1652 (user-error "No bookmarks are defined"))
1653 (set-buffer (get-buffer-create "*eww bookmarks*"))
1654 (eww-bookmark-mode)
1655 (let* ((width (/ (window-width) 2))
1656 (format (format "%%-%ds %%s" width))
1657 (inhibit-read-only t)
1658 start title)
1659 (erase-buffer)
1660 (setq header-line-format (concat " " (format format "Title" "URL")))
1661 (dolist (bookmark eww-bookmarks)
1662 (setq start (point)
1663 title (plist-get bookmark :title))
1664 (when (> (length title) width)
1665 (setq title (truncate-string-to-width title width)))
1666 (insert (format format title (plist-get bookmark :url)) "\n")
1667 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1668 (goto-char (point-min))))
1670 (defvar eww-bookmark-kill-ring nil)
1672 (defun eww-bookmark-kill ()
1673 "Kill the current bookmark."
1674 (interactive)
1675 (let* ((start (line-beginning-position))
1676 (bookmark (get-text-property start 'eww-bookmark))
1677 (inhibit-read-only t))
1678 (unless bookmark
1679 (user-error "No bookmark on the current line"))
1680 (forward-line 1)
1681 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1682 (delete-region start (point))
1683 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1684 (eww-write-bookmarks)))
1686 (defun eww-bookmark-yank ()
1687 "Yank a previously killed bookmark to the current line."
1688 (interactive)
1689 (unless eww-bookmark-kill-ring
1690 (user-error "No previously killed bookmark"))
1691 (beginning-of-line)
1692 (let ((inhibit-read-only t)
1693 (start (point))
1694 bookmark)
1695 (insert (pop eww-bookmark-kill-ring))
1696 (setq bookmark (get-text-property start 'eww-bookmark))
1697 (if (= start (point-min))
1698 (push bookmark eww-bookmarks)
1699 (let ((line (count-lines start (point))))
1700 (setcdr (nthcdr (1- line) eww-bookmarks)
1701 (cons bookmark (nthcdr line eww-bookmarks)))))
1702 (eww-write-bookmarks)))
1704 (defun eww-bookmark-browse ()
1705 "Browse the bookmark under point in eww."
1706 (interactive)
1707 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1708 (unless bookmark
1709 (user-error "No bookmark on the current line"))
1710 (quit-window)
1711 (eww-browse-url (plist-get bookmark :url))))
1713 (defun eww-next-bookmark ()
1714 "Go to the next bookmark in the list."
1715 (interactive)
1716 (let ((first nil)
1717 bookmark)
1718 (unless (get-buffer "*eww bookmarks*")
1719 (setq first t)
1720 (eww-bookmark-prepare))
1721 (with-current-buffer (get-buffer "*eww bookmarks*")
1722 (when (and (not first)
1723 (not (eobp)))
1724 (forward-line 1))
1725 (setq bookmark (get-text-property (line-beginning-position)
1726 'eww-bookmark))
1727 (unless bookmark
1728 (user-error "No next bookmark")))
1729 (eww-browse-url (plist-get bookmark :url))))
1731 (defun eww-previous-bookmark ()
1732 "Go to the previous bookmark in the list."
1733 (interactive)
1734 (let ((first nil)
1735 bookmark)
1736 (unless (get-buffer "*eww bookmarks*")
1737 (setq first t)
1738 (eww-bookmark-prepare))
1739 (with-current-buffer (get-buffer "*eww bookmarks*")
1740 (if first
1741 (goto-char (point-max))
1742 (beginning-of-line))
1743 ;; On the final line.
1744 (when (eolp)
1745 (forward-line -1))
1746 (if (bobp)
1747 (user-error "No previous bookmark")
1748 (forward-line -1))
1749 (setq bookmark (get-text-property (line-beginning-position)
1750 'eww-bookmark)))
1751 (eww-browse-url (plist-get bookmark :url))))
1753 (defvar eww-bookmark-mode-map
1754 (let ((map (make-sparse-keymap)))
1755 (define-key map [(control k)] 'eww-bookmark-kill)
1756 (define-key map [(control y)] 'eww-bookmark-yank)
1757 (define-key map "\r" 'eww-bookmark-browse)
1759 (easy-menu-define nil map
1760 "Menu for `eww-bookmark-mode-map'."
1761 '("Eww Bookmark"
1762 ["Exit" quit-window t]
1763 ["Browse" eww-bookmark-browse
1764 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1765 ["Kill" eww-bookmark-kill
1766 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1767 ["Yank" eww-bookmark-yank
1768 :active eww-bookmark-kill-ring]))
1769 map))
1771 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1772 "Mode for listing bookmarks.
1774 \\{eww-bookmark-mode-map}"
1775 (buffer-disable-undo)
1776 (setq truncate-lines t))
1778 ;;; History code
1780 (defun eww-save-history ()
1781 (plist-put eww-data :point (point))
1782 (plist-put eww-data :text (buffer-string))
1783 (push eww-data eww-history)
1784 (setq eww-data (list :title ""))
1785 ;; Don't let the history grow infinitely. We store quite a lot of
1786 ;; data per page.
1787 (when-let (tail (and eww-history-limit
1788 (nthcdr eww-history-limit eww-history)))
1789 (setcdr tail nil)))
1791 (defvar eww-current-buffer)
1793 (defun eww-list-histories ()
1794 "List the eww-histories."
1795 (interactive)
1796 (when (null eww-history)
1797 (error "No eww-histories are defined"))
1798 (let ((eww-history-trans eww-history)
1799 (buffer (current-buffer)))
1800 (set-buffer (get-buffer-create "*eww history*"))
1801 (eww-history-mode)
1802 (setq-local eww-current-buffer buffer)
1803 (let ((inhibit-read-only t)
1804 (domain-length 0)
1805 (title-length 0)
1806 url title format start)
1807 (erase-buffer)
1808 (dolist (history eww-history-trans)
1809 (setq start (point))
1810 (setq domain-length (max domain-length (length (plist-get history :url))))
1811 (setq title-length (max title-length (length (plist-get history :title)))))
1812 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1813 header-line-format
1814 (concat " " (format format "Title" "URL")))
1815 (dolist (history eww-history-trans)
1816 (setq start (point))
1817 (setq url (plist-get history :url))
1818 (setq title (plist-get history :title))
1819 (insert (format format title url))
1820 (insert "\n")
1821 (put-text-property start (1+ start) 'eww-history history))
1822 (goto-char (point-min)))
1823 (pop-to-buffer "*eww history*")))
1825 (defun eww-history-browse ()
1826 "Browse the history under point in eww."
1827 (interactive)
1828 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1829 (unless history
1830 (error "No history on the current line"))
1831 (let ((buffer eww-current-buffer))
1832 (quit-window)
1833 (when buffer
1834 (pop-to-buffer-same-window buffer)))
1835 (eww-restore-history history)))
1837 (defvar eww-history-mode-map
1838 (let ((map (make-sparse-keymap)))
1839 (define-key map "\r" 'eww-history-browse)
1840 ;; (define-key map "n" 'next-error-no-select)
1841 ;; (define-key map "p" 'previous-error-no-select)
1843 (easy-menu-define nil map
1844 "Menu for `eww-history-mode-map'."
1845 '("Eww History"
1846 ["Exit" quit-window t]
1847 ["Browse" eww-history-browse
1848 :active (get-text-property (line-beginning-position) 'eww-history)]))
1849 map))
1851 (define-derived-mode eww-history-mode special-mode "eww history"
1852 "Mode for listing eww-histories.
1854 \\{eww-history-mode-map}"
1855 (buffer-disable-undo)
1856 (setq truncate-lines t))
1858 ;;; eww buffers list
1860 (defun eww-list-buffers ()
1861 "Enlist eww buffers."
1862 (interactive)
1863 (let (buffers-info
1864 (current (current-buffer)))
1865 (dolist (buffer (buffer-list))
1866 (with-current-buffer buffer
1867 (when (derived-mode-p 'eww-mode)
1868 (push (vector buffer (plist-get eww-data :title)
1869 (plist-get eww-data :url))
1870 buffers-info))))
1871 (unless buffers-info
1872 (error "No eww buffers"))
1873 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1874 (set-buffer (get-buffer-create "*eww buffers*"))
1875 (eww-buffers-mode)
1876 (let ((inhibit-read-only t)
1877 (domain-length 0)
1878 (title-length 0)
1879 url title format start)
1880 (erase-buffer)
1881 (dolist (buffer-info buffers-info)
1882 (setq title-length (max title-length
1883 (length (elt buffer-info 1)))
1884 domain-length (max domain-length
1885 (length (elt buffer-info 2)))))
1886 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1887 header-line-format
1888 (concat " " (format format "Title" "URL")))
1889 (let ((line 0)
1890 (current-buffer-line 1))
1891 (dolist (buffer-info buffers-info)
1892 (setq start (point)
1893 title (elt buffer-info 1)
1894 url (elt buffer-info 2)
1895 line (1+ line))
1896 (insert (format format title url))
1897 (insert "\n")
1898 (let ((buffer (elt buffer-info 0)))
1899 (put-text-property start (1+ start) 'eww-buffer
1900 buffer)
1901 (when (eq current buffer)
1902 (setq current-buffer-line line))))
1903 (goto-char (point-min))
1904 (forward-line (1- current-buffer-line)))))
1905 (pop-to-buffer "*eww buffers*"))
1907 (defun eww-buffer-select ()
1908 "Switch to eww buffer."
1909 (interactive)
1910 (let ((buffer (get-text-property (line-beginning-position)
1911 'eww-buffer)))
1912 (unless buffer
1913 (error "No buffer on current line"))
1914 (quit-window)
1915 (pop-to-buffer-same-window buffer)))
1917 (defun eww-buffer-show ()
1918 "Display buffer under point in eww buffer list."
1919 (let ((buffer (get-text-property (line-beginning-position)
1920 'eww-buffer)))
1921 (unless buffer
1922 (error "No buffer on current line"))
1923 (other-window -1)
1924 (pop-to-buffer-same-window buffer)
1925 (other-window 1)))
1927 (defun eww-buffer-show-next ()
1928 "Move to next eww buffer in the list and display it."
1929 (interactive)
1930 (forward-line)
1931 (when (eobp)
1932 (goto-char (point-min)))
1933 (eww-buffer-show))
1935 (defun eww-buffer-show-previous ()
1936 "Move to previous eww buffer in the list and display it."
1937 (interactive)
1938 (beginning-of-line)
1939 (when (bobp)
1940 (goto-char (point-max)))
1941 (forward-line -1)
1942 (eww-buffer-show))
1944 (defun eww-buffer-kill ()
1945 "Kill buffer from eww list."
1946 (interactive)
1947 (let* ((start (line-beginning-position))
1948 (buffer (get-text-property start 'eww-buffer))
1949 (inhibit-read-only t))
1950 (unless buffer
1951 (user-error "No buffer on the current line"))
1952 (kill-buffer buffer)
1953 (forward-line 1)
1954 (delete-region start (point)))
1955 (when (eobp)
1956 (forward-line -1))
1957 (eww-buffer-show))
1959 (defvar eww-buffers-mode-map
1960 (let ((map (make-sparse-keymap)))
1961 (define-key map [(control k)] 'eww-buffer-kill)
1962 (define-key map "\r" 'eww-buffer-select)
1963 (define-key map "n" 'eww-buffer-show-next)
1964 (define-key map "p" 'eww-buffer-show-previous)
1966 (easy-menu-define nil map
1967 "Menu for `eww-buffers-mode-map'."
1968 '("Eww Buffers"
1969 ["Exit" quit-window t]
1970 ["Select" eww-buffer-select
1971 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1972 ["Kill" eww-buffer-kill
1973 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1974 map))
1976 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1977 "Mode for listing buffers.
1979 \\{eww-buffers-mode-map}"
1980 (buffer-disable-undo)
1981 (setq truncate-lines t))
1983 ;;; Desktop support
1985 (defvar eww-desktop-data-save
1986 '(:url :title :point)
1987 "List of `eww-data' properties to preserve in the desktop file.
1988 Also used when saving `eww-history'.")
1990 (defun eww-desktop-data-1 (alist)
1991 (let ((acc nil)
1992 (tail alist))
1993 (while tail
1994 (let ((k (car tail))
1995 (v (cadr tail)))
1996 (when (memq k eww-desktop-data-save)
1997 (setq acc (cons k (cons v acc)))))
1998 (setq tail (cddr tail)))
1999 acc))
2001 (defun eww-desktop-history-duplicate (a b)
2002 (let ((tail a) (r t))
2003 (while tail
2004 (if (or (memq (car tail) '(:point)) ; ignore :point
2005 (equal (cadr tail)
2006 (plist-get b (car tail))))
2007 (setq tail (cddr tail))
2008 (setq tail nil
2009 r nil)))
2010 ;; .
2013 (defun eww-desktop-misc-data (_directory)
2014 "Return a property list with data used to restore eww buffers.
2015 This list will contain, as :history, the list, whose first element is
2016 the value of `eww-data', and the tail is `eww-history'.
2018 If `eww-desktop-remove-duplicates' is non-nil, duplicate
2019 entries (if any) will be removed from the list.
2021 Only the properties listed in `eww-desktop-data-save' are included.
2022 Generally, the list should not include the (usually overly large)
2023 :dom, :source and :text properties."
2024 (let ((history (mapcar 'eww-desktop-data-1
2025 (cons eww-data eww-history))))
2026 (list :history (if eww-desktop-remove-duplicates
2027 (cl-remove-duplicates
2028 history :test 'eww-desktop-history-duplicate)
2029 history))))
2031 (defun eww-restore-desktop (file-name buffer-name misc-data)
2032 "Restore an eww buffer from its desktop file record.
2033 If `eww-restore-desktop' is t or `auto', this function will also
2034 initiate the retrieval of the respective URI in the background.
2035 Otherwise, the restored buffer will contain a prompt to do so by using
2036 \\[eww-reload]."
2037 (with-current-buffer (get-buffer-create buffer-name)
2038 (eww-mode)
2039 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
2040 (setq eww-history (cdr (plist-get misc-data :history))
2041 eww-data (or (car (plist-get misc-data :history))
2042 ;; backwards compatibility
2043 (list :url (plist-get misc-data :uri))))
2044 (unless file-name
2045 (when (plist-get eww-data :url)
2046 (cl-case eww-restore-desktop
2047 ((t auto) (eww (plist-get eww-data :url)))
2048 ((zerop (buffer-size))
2049 (let ((inhibit-read-only t))
2050 (insert (substitute-command-keys
2051 eww-restore-reload-prompt)))))))
2052 ;; .
2053 (current-buffer)))
2055 (add-to-list 'desktop-locals-to-save
2056 'eww-history-position)
2057 (add-to-list 'desktop-buffer-mode-handlers
2058 '(eww-mode . eww-restore-desktop))
2060 ;;; Isearch support
2062 (defun eww-isearch-next-buffer (&optional _buffer wrap)
2063 "Go to the next page to search using `rel' attribute for navigation."
2064 (if wrap
2065 (condition-case nil
2066 (eww-top-url)
2067 (error nil))
2068 (if isearch-forward
2069 (eww-next-url)
2070 (eww-previous-url)))
2071 (current-buffer))
2073 (provide 'eww)
2075 ;;; eww.el ends here