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