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