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