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