]> code.delx.au - gnu-emacs/blob - lisp/net/eww.el
Additional fixes for file notification
[gnu-emacs] / lisp / net / eww.el
1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'url-queue)
32 (require 'url-util) ; for url-get-url-at-point
33 (require 'mm-url)
34 (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 (pop-to-buffer-same-window
278 (if (eq major-mode 'eww-mode)
279 (current-buffer)
280 (get-buffer-create "*eww*")))
281 (eww-setup-buffer)
282 (plist-put eww-data :url url)
283 (plist-put eww-data :title "")
284 (eww-update-header-line-format)
285 (let ((inhibit-read-only t))
286 (insert (format "Loading %s..." url))
287 (goto-char (point-min)))
288 (url-retrieve url 'eww-render
289 (list url nil (current-buffer))))
290
291 ;;;###autoload (defalias 'browse-web 'eww)
292
293 ;;;###autoload
294 (defun eww-open-file (file)
295 "Render FILE using EWW."
296 (interactive "fFile: ")
297 (eww (concat "file://"
298 (and (memq system-type '(windows-nt ms-dos))
299 "/")
300 (expand-file-name file))))
301
302 ;;;###autoload
303 (defun eww-search-words (&optional beg end)
304 "Search the web for the text between BEG and END.
305 See the `eww-search-prefix' variable for the search engine used."
306 (interactive "r")
307 (eww (buffer-substring beg end)))
308
309 (defun eww-html-p (content-type)
310 "Return non-nil if CONTENT-TYPE designates an HTML content type.
311 Currently this means either text/html or application/xhtml+xml."
312 (member content-type '("text/html"
313 "application/xhtml+xml")))
314
315 (defun eww-render (status url &optional point buffer encode)
316 (let ((redirect (plist-get status :redirect)))
317 (when redirect
318 (setq url redirect)))
319 (let* ((headers (eww-parse-headers))
320 (content-type
321 (mail-header-parse-content-type
322 (if (zerop (length (cdr (assoc "content-type" headers))))
323 "text/plain"
324 (cdr (assoc "content-type" headers)))))
325 (charset (intern
326 (downcase
327 (or (cdr (assq 'charset (cdr content-type)))
328 (eww-detect-charset (eww-html-p (car content-type)))
329 "utf-8"))))
330 (data-buffer (current-buffer))
331 last-coding-system-used)
332 ;; Save the https peer status.
333 (with-current-buffer buffer
334 (plist-put eww-data :peer (plist-get status :peer)))
335 (unwind-protect
336 (progn
337 (cond
338 ((and eww-use-external-browser-for-content-type
339 (string-match-p eww-use-external-browser-for-content-type
340 (car content-type)))
341 (eww-browse-with-external-browser url))
342 ((eww-html-p (car content-type))
343 (eww-display-html charset url nil point buffer encode))
344 ((equal (car content-type) "application/pdf")
345 (eww-display-pdf))
346 ((string-match-p "\\`image/" (car content-type))
347 (eww-display-image buffer))
348 (t
349 (eww-display-raw buffer (or encode charset 'utf-8))))
350 (with-current-buffer buffer
351 (plist-put eww-data :url url)
352 (eww-update-header-line-format)
353 (setq eww-history-position 0)
354 (and last-coding-system-used
355 (set-buffer-file-coding-system last-coding-system-used))
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 (setq encode (or encode charset 'utf-8))
402 (condition-case nil
403 (decode-coding-region (point) (point-max) encode)
404 (coding-system-error nil))
405 (save-excursion
406 ;; Remove CRLF before parsing.
407 (while (re-search-forward "\r$" nil t)
408 (replace-match "" t t)))
409 (libxml-parse-html-region (point) (point-max))))))
410 (source (and (null document)
411 (buffer-substring (point) (point-max)))))
412 (with-current-buffer buffer
413 (setq bidi-paragraph-direction 'left-to-right)
414 (plist-put eww-data :source source)
415 (plist-put eww-data :dom document)
416 (let ((inhibit-read-only t)
417 (inhibit-modification-hooks t)
418 (shr-target-id (url-target (url-generic-parse-url url)))
419 (shr-external-rendering-functions
420 (append
421 '((title . eww-tag-title)
422 (form . eww-tag-form)
423 (input . eww-tag-input)
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 (condition-case nil
541 (decode-coding-region (point-min) (1+ (length data)) encode)
542 (coding-system-error nil)))
543 (goto-char (point-min)))))
544
545 (defun eww-display-image (buffer)
546 (let ((data (shr-parse-image-data)))
547 (unless (buffer-live-p buffer)
548 (error "Buffer %s doesn't exist" buffer))
549 (with-current-buffer buffer
550 (let ((inhibit-read-only t))
551 (erase-buffer)
552 (shr-put-image data nil))
553 (goto-char (point-min)))))
554
555 (declare-function mailcap-view-mime "mailcap" (type))
556 (defun eww-display-pdf ()
557 (let ((data (buffer-substring (point) (point-max))))
558 (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
559 (let ((coding-system-for-write 'raw-text)
560 (inhibit-read-only t))
561 (erase-buffer)
562 (insert data)
563 (mailcap-view-mime "application/pdf")))
564 (goto-char (point-min)))
565
566 (defun eww-setup-buffer ()
567 (when (or (plist-get eww-data :url)
568 (plist-get eww-data :dom))
569 (eww-save-history))
570 (let ((inhibit-read-only t))
571 (remove-overlays)
572 (erase-buffer))
573 (setq bidi-paragraph-direction 'left-to-right)
574 (unless (eq major-mode 'eww-mode)
575 (eww-mode)))
576
577 (defun eww-current-url nil
578 "Return URI of the Web page the current EWW buffer is visiting."
579 (plist-get eww-data :url))
580
581 (defun eww-links-at-point ()
582 "Return list of URIs, if any, linked at point."
583 (remq nil
584 (list (get-text-property (point) 'shr-url)
585 (get-text-property (point) 'image-url))))
586
587 (defun eww-view-source ()
588 "View the HTML source code of the current page."
589 (interactive)
590 (let ((buf (get-buffer-create "*eww-source*"))
591 (source (plist-get eww-data :source)))
592 (with-current-buffer buf
593 (let ((inhibit-read-only t))
594 (delete-region (point-min) (point-max))
595 (insert (or source "no source"))
596 (goto-char (point-min))
597 ;; Decode the source and set the buffer's encoding according
598 ;; to what the HTML source specifies in its 'charset' header,
599 ;; if any.
600 (let ((cs (find-auto-coding "" (point-max))))
601 (when (consp cs)
602 (setq cs (car cs))
603 (when (coding-system-p cs)
604 (decode-coding-region (point-min) (point-max) cs)
605 (setq buffer-file-coding-system last-coding-system-used))))
606 (when (fboundp 'html-mode)
607 (html-mode))))
608 (view-buffer buf)))
609
610 (defun eww-readable ()
611 "View the main \"readable\" parts of the current web page.
612 This command uses heuristics to find the parts of the web page that
613 contains the main textual portion, leaving out navigation menus and
614 the like."
615 (interactive)
616 (let* ((old-data eww-data)
617 (dom (with-temp-buffer
618 (insert (plist-get old-data :source))
619 (condition-case nil
620 (decode-coding-region (point-min) (point-max) 'utf-8)
621 (coding-system-error nil))
622 (libxml-parse-html-region (point-min) (point-max))))
623 (base (plist-get eww-data :url)))
624 (eww-score-readability dom)
625 (eww-save-history)
626 (eww-display-html nil nil
627 (list 'base (list (cons 'href base))
628 (eww-highest-readability dom))
629 nil (current-buffer))
630 (dolist (elem '(:source :url :title :next :previous :up))
631 (plist-put eww-data elem (plist-get old-data elem)))
632 (eww-update-header-line-format)))
633
634 (defun eww-score-readability (node)
635 (let ((score -1))
636 (cond
637 ((memq (dom-tag node) '(script head comment))
638 (setq score -2))
639 ((eq (dom-tag node) 'meta)
640 (setq score -1))
641 ((eq (dom-tag node) 'img)
642 (setq score 2))
643 ((eq (dom-tag node) 'a)
644 (setq score (- (length (split-string (dom-text node))))))
645 (t
646 (dolist (elem (dom-children node))
647 (if (stringp elem)
648 (setq score (+ score (length (split-string elem))))
649 (setq score (+ score
650 (or (cdr (assoc :eww-readability-score (cdr elem)))
651 (eww-score-readability elem))))))))
652 ;; Cache the score of the node to avoid recomputing all the time.
653 (dom-set-attribute node :eww-readability-score score)
654 score))
655
656 (defun eww-highest-readability (node)
657 (let ((result node)
658 highest)
659 (dolist (elem (dom-non-text-children node))
660 (when (> (or (dom-attr
661 (setq highest (eww-highest-readability elem))
662 :eww-readability-score)
663 most-negative-fixnum)
664 (or (dom-attr result :eww-readability-score)
665 most-negative-fixnum))
666 (setq result highest)))
667 result))
668
669 (defvar eww-mode-map
670 (let ((map (make-sparse-keymap)))
671 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
672 (define-key map "G" 'eww)
673 (define-key map [?\t] 'shr-next-link)
674 (define-key map [?\M-\t] 'shr-previous-link)
675 (define-key map [backtab] 'shr-previous-link)
676 (define-key map [delete] 'scroll-down-command)
677 (define-key map "l" 'eww-back-url)
678 (define-key map "r" 'eww-forward-url)
679 (define-key map "n" 'eww-next-url)
680 (define-key map "p" 'eww-previous-url)
681 (define-key map "u" 'eww-up-url)
682 (define-key map "t" 'eww-top-url)
683 (define-key map "&" 'eww-browse-with-external-browser)
684 (define-key map "d" 'eww-download)
685 (define-key map "w" 'eww-copy-page-url)
686 (define-key map "C" 'url-cookie-list)
687 (define-key map "v" 'eww-view-source)
688 (define-key map "R" 'eww-readable)
689 (define-key map "H" 'eww-list-histories)
690 (define-key map "E" 'eww-set-character-encoding)
691 (define-key map "S" 'eww-list-buffers)
692 (define-key map "F" 'eww-toggle-fonts)
693
694 (define-key map "b" 'eww-add-bookmark)
695 (define-key map "B" 'eww-list-bookmarks)
696 (define-key map [(meta n)] 'eww-next-bookmark)
697 (define-key map [(meta p)] 'eww-previous-bookmark)
698
699 (easy-menu-define nil map ""
700 '("Eww"
701 ["Exit" quit-window t]
702 ["Close browser" quit-window t]
703 ["Reload" eww-reload t]
704 ["Back to previous page" eww-back-url
705 :active (not (zerop (length eww-history)))]
706 ["Forward to next page" eww-forward-url
707 :active (not (zerop eww-history-position))]
708 ["Browse with external browser" eww-browse-with-external-browser t]
709 ["Download" eww-download t]
710 ["View page source" eww-view-source]
711 ["Copy page URL" eww-copy-page-url t]
712 ["List histories" eww-list-histories t]
713 ["List buffers" eww-list-buffers t]
714 ["Add bookmark" eww-add-bookmark t]
715 ["List bookmarks" eww-list-bookmarks t]
716 ["List cookies" url-cookie-list t]
717 ["Character Encoding" eww-set-character-encoding]))
718 map))
719
720 (defvar eww-tool-bar-map
721 (let ((map (make-sparse-keymap)))
722 (dolist (tool-bar-item
723 '((quit-window . "close")
724 (eww-reload . "refresh")
725 (eww-back-url . "left-arrow")
726 (eww-forward-url . "right-arrow")
727 (eww-view-source . "show")
728 (eww-copy-page-url . "copy")
729 (eww-add-bookmark . "bookmark_add"))) ;; ...
730 (tool-bar-local-item-from-menu
731 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
732 map)
733 "Tool bar for `eww-mode'.")
734
735 ;; Autoload cookie needed by desktop.el.
736 ;;;###autoload
737 (define-derived-mode eww-mode special-mode "eww"
738 "Mode for browsing the web."
739 (setq-local eww-data (list :title ""))
740 (setq-local browse-url-browser-function #'eww-browse-url)
741 (add-hook 'after-change-functions #'eww-process-text-input nil t)
742 (setq-local eww-history nil)
743 (setq-local eww-history-position 0)
744 (when (boundp 'tool-bar-map)
745 (setq-local tool-bar-map eww-tool-bar-map))
746 ;; desktop support
747 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
748 ;; multi-page isearch support
749 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
750 (setq truncate-lines t)
751 (buffer-disable-undo)
752 (setq buffer-read-only t))
753
754 ;;;###autoload
755 (defun eww-browse-url (url &optional new-window)
756 (when new-window
757 (pop-to-buffer-same-window (generate-new-buffer "*eww*"))
758 (eww-mode))
759 (eww url))
760
761 (defun eww-back-url ()
762 "Go to the previously displayed page."
763 (interactive)
764 (when (>= eww-history-position (length eww-history))
765 (user-error "No previous page"))
766 (eww-save-history)
767 (setq eww-history-position (+ eww-history-position 2))
768 (eww-restore-history (elt eww-history (1- eww-history-position))))
769
770 (defun eww-forward-url ()
771 "Go to the next displayed page."
772 (interactive)
773 (when (zerop eww-history-position)
774 (user-error "No next page"))
775 (eww-save-history)
776 (eww-restore-history (elt eww-history (1- eww-history-position))))
777
778 (defun eww-restore-history (elem)
779 (let ((inhibit-read-only t)
780 (inhibit-modification-hooks t)
781 (text (plist-get elem :text)))
782 (setq eww-data elem)
783 (if (null text)
784 (eww-reload) ; FIXME: restore :point?
785 (erase-buffer)
786 (insert text)
787 (goto-char (plist-get elem :point))
788 (eww-update-header-line-format))))
789
790 (defun eww-next-url ()
791 "Go to the page marked `next'.
792 A page is marked `next' if rel=\"next\" appears in a <link>
793 or <a> tag."
794 (interactive)
795 (if (plist-get eww-data :next)
796 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
797 (plist-get eww-data :url)))
798 (user-error "No `next' on this page")))
799
800 (defun eww-previous-url ()
801 "Go to the page marked `previous'.
802 A page is marked `previous' if rel=\"previous\" appears in a <link>
803 or <a> tag."
804 (interactive)
805 (if (plist-get eww-data :previous)
806 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
807 (plist-get eww-data :url)))
808 (user-error "No `previous' on this page")))
809
810 (defun eww-up-url ()
811 "Go to the page marked `up'.
812 A page is marked `up' if rel=\"up\" appears in a <link>
813 or <a> tag."
814 (interactive)
815 (if (plist-get eww-data :up)
816 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
817 (plist-get eww-data :url)))
818 (user-error "No `up' on this page")))
819
820 (defun eww-top-url ()
821 "Go to the page marked `top'.
822 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
823 appears in a <link> or <a> tag."
824 (interactive)
825 (let ((best-url (or (plist-get eww-data :start)
826 (plist-get eww-data :contents)
827 (plist-get eww-data :home))))
828 (if best-url
829 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
830 (user-error "No `top' for this page"))))
831
832 (defun eww-reload (&optional local encode)
833 "Reload the current page.
834 If LOCAL (the command prefix), don't reload the page from the
835 network, but just re-display the HTML already fetched."
836 (interactive "P")
837 (let ((url (plist-get eww-data :url)))
838 (if local
839 (if (null (plist-get eww-data :dom))
840 (error "No current HTML data")
841 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
842 (point) (current-buffer)))
843 (url-retrieve url 'eww-render
844 (list url (point) (current-buffer) encode)))))
845
846 ;; Form support.
847
848 (defvar eww-form nil)
849
850 (defvar eww-submit-map
851 (let ((map (make-sparse-keymap)))
852 (define-key map "\r" 'eww-submit)
853 (define-key map [(control c) (control c)] 'eww-submit)
854 map))
855
856 (defvar eww-submit-file
857 (let ((map (make-sparse-keymap)))
858 (define-key map "\r" 'eww-select-file)
859 (define-key map [(control c) (control c)] 'eww-submit)
860 map))
861
862 (defvar eww-checkbox-map
863 (let ((map (make-sparse-keymap)))
864 (define-key map " " 'eww-toggle-checkbox)
865 (define-key map "\r" 'eww-toggle-checkbox)
866 (define-key map [(control c) (control c)] 'eww-submit)
867 map))
868
869 (defvar eww-text-map
870 (let ((map (make-keymap)))
871 (set-keymap-parent map text-mode-map)
872 (define-key map "\r" 'eww-submit)
873 (define-key map [(control a)] 'eww-beginning-of-text)
874 (define-key map [(control c) (control c)] 'eww-submit)
875 (define-key map [(control e)] 'eww-end-of-text)
876 (define-key map [?\t] 'shr-next-link)
877 (define-key map [?\M-\t] 'shr-previous-link)
878 map))
879
880 (defvar eww-textarea-map
881 (let ((map (make-keymap)))
882 (set-keymap-parent map text-mode-map)
883 (define-key map "\r" 'forward-line)
884 (define-key map [(control c) (control c)] 'eww-submit)
885 (define-key map [?\t] 'shr-next-link)
886 (define-key map [?\M-\t] 'shr-previous-link)
887 map))
888
889 (defvar eww-select-map
890 (let ((map (make-sparse-keymap)))
891 (define-key map "\r" 'eww-change-select)
892 (define-key map [(control c) (control c)] 'eww-submit)
893 map))
894
895 (defun eww-beginning-of-text ()
896 "Move to the start of the input field."
897 (interactive)
898 (goto-char (eww-beginning-of-field)))
899
900 (defun eww-end-of-text ()
901 "Move to the end of the text in the input field."
902 (interactive)
903 (goto-char (eww-end-of-field))
904 (let ((start (eww-beginning-of-field)))
905 (while (and (equal (following-char) ? )
906 (> (point) start))
907 (forward-char -1))
908 (when (> (point) start)
909 (forward-char 1))))
910
911 (defun eww-beginning-of-field ()
912 (cond
913 ((bobp)
914 (point))
915 ((not (eq (get-text-property (point) 'eww-form)
916 (get-text-property (1- (point)) 'eww-form)))
917 (point))
918 (t
919 (previous-single-property-change
920 (point) 'eww-form nil (point-min)))))
921
922 (defun eww-end-of-field ()
923 (1- (next-single-property-change
924 (point) 'eww-form nil (point-max))))
925
926 (defun eww-tag-form (dom)
927 (let ((eww-form (list (cons :method (dom-attr dom 'method))
928 (cons :action (dom-attr dom 'action))))
929 (start (point)))
930 (insert "\n")
931 (shr-ensure-paragraph)
932 (shr-generic dom)
933 (unless (bolp)
934 (insert "\n"))
935 (insert "\n")
936 (when (> (point) start)
937 (put-text-property start (1+ start)
938 'eww-form eww-form))))
939
940 (defun eww-form-submit (dom)
941 (let ((start (point))
942 (value (dom-attr dom 'value)))
943 (setq value
944 (if (zerop (length value))
945 "Submit"
946 value))
947 (insert value)
948 (add-face-text-property start (point) 'eww-form-submit)
949 (put-text-property start (point) 'eww-form
950 (list :eww-form eww-form
951 :value value
952 :type "submit"
953 :name (dom-attr dom 'name)))
954 (put-text-property start (point) 'keymap eww-submit-map)
955 (insert " ")))
956
957 (defun eww-form-checkbox (dom)
958 (let ((start (point)))
959 (if (dom-attr dom 'checked)
960 (insert eww-form-checkbox-selected-symbol)
961 (insert eww-form-checkbox-symbol))
962 (add-face-text-property start (point) 'eww-form-checkbox)
963 (put-text-property start (point) 'eww-form
964 (list :eww-form eww-form
965 :value (dom-attr dom 'value)
966 :type (downcase (dom-attr dom 'type))
967 :checked (dom-attr dom 'checked)
968 :name (dom-attr dom 'name)))
969 (put-text-property start (point) 'keymap eww-checkbox-map)
970 (insert " ")))
971
972 (defun eww-form-file (dom)
973 (let ((start (point))
974 (value (dom-attr dom 'value)))
975 (setq value
976 (if (zerop (length value))
977 " No file selected"
978 value))
979 (insert "Browse")
980 (add-face-text-property start (point) 'eww-form-file)
981 (insert value)
982 (put-text-property start (point) 'eww-form
983 (list :eww-form eww-form
984 :value (dom-attr dom 'value)
985 :type (downcase (dom-attr dom 'type))
986 :name (dom-attr dom 'name)))
987 (put-text-property start (point) 'keymap eww-submit-file)
988 (insert " ")))
989
990 (defun eww-select-file ()
991 "Change the value of the upload file menu under point."
992 (interactive)
993 (let* ((input (get-text-property (point) 'eww-form)))
994 (let ((filename
995 (let ((insert-default-directory t))
996 (read-file-name "filename: "))))
997 (eww-update-field filename (length "Browse"))
998 (plist-put input :filename filename))))
999
1000 (defun eww-form-text (dom)
1001 (let ((start (point))
1002 (type (downcase (or (dom-attr dom 'type) "text")))
1003 (value (or (dom-attr dom 'value) ""))
1004 (width (string-to-number (or (dom-attr dom 'size) "40")))
1005 (readonly-property (if (or (dom-attr dom 'disabled)
1006 (dom-attr dom 'readonly))
1007 'read-only
1008 'inhibit-read-only)))
1009 (insert value)
1010 (when (< (length value) width)
1011 (insert (make-string (- width (length value)) ? )))
1012 (put-text-property start (point) 'face 'eww-form-text)
1013 (put-text-property start (point) 'inhibit-read-only t)
1014 (put-text-property start (point) 'local-map eww-text-map)
1015 (put-text-property start (point) readonly-property t)
1016 (put-text-property start (point) 'eww-form
1017 (list :eww-form eww-form
1018 :value value
1019 :type type
1020 :name (dom-attr dom 'name)))
1021 (insert " ")))
1022
1023 (defconst eww-text-input-types '("text" "password" "textarea"
1024 "color" "date" "datetime" "datetime-local"
1025 "email" "month" "number" "search" "tel"
1026 "time" "url" "week")
1027 "List of input types which represent a text input.
1028 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
1029
1030 (defun eww-process-text-input (beg end replace-length)
1031 (when-let (pos (and (< (1+ end) (point-max))
1032 (> (1- end) (point-min))
1033 (cond
1034 ((get-text-property (1+ end) 'eww-form)
1035 (1+ end))
1036 ((get-text-property (1- end) 'eww-form)
1037 (1- end)))))
1038 (let* ((form (get-text-property pos 'eww-form))
1039 (properties (text-properties-at pos))
1040 (buffer-undo-list t)
1041 (inhibit-read-only t)
1042 (length (- end beg replace-length))
1043 (type (plist-get form :type)))
1044 (when (and form
1045 (member type eww-text-input-types))
1046 (cond
1047 ((> length 0)
1048 ;; Delete some space at the end.
1049 (save-excursion
1050 (goto-char
1051 (if (equal type "textarea")
1052 (1- (line-end-position))
1053 (eww-end-of-field)))
1054 (while (and (> length 0)
1055 (eql (char-after (1- (point))) ? ))
1056 (delete-region (1- (point)) (point))
1057 (cl-decf length))))
1058 ((< length 0)
1059 ;; Add padding.
1060 (save-excursion
1061 (goto-char end)
1062 (goto-char
1063 (if (equal type "textarea")
1064 (1- (line-end-position))
1065 (1+ (eww-end-of-field))))
1066 (let ((start (point)))
1067 (insert (make-string (abs length) ? ))
1068 (set-text-properties start (point) properties))
1069 (goto-char (1- end)))))
1070 (set-text-properties (plist-get form :start) (plist-get form :end)
1071 properties)
1072 (let ((value (buffer-substring-no-properties
1073 (eww-beginning-of-field)
1074 (eww-end-of-field))))
1075 (when (string-match " +\\'" value)
1076 (setq value (substring value 0 (match-beginning 0))))
1077 (plist-put form :value value)
1078 (when (equal type "password")
1079 ;; Display passwords as asterisks.
1080 (let ((start (eww-beginning-of-field)))
1081 (put-text-property
1082 start (+ start (length value))
1083 'display (make-string (length value) ?*)))))))))
1084
1085 (defun eww-tag-textarea (dom)
1086 (let ((start (point))
1087 (value (or (dom-attr dom 'value) ""))
1088 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1089 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1090 end)
1091 (shr-ensure-newline)
1092 (insert value)
1093 (shr-ensure-newline)
1094 (when (< (count-lines start (point)) lines)
1095 (dotimes (_ (- lines (count-lines start (point))))
1096 (insert "\n")))
1097 (setq end (point-marker))
1098 (goto-char start)
1099 (while (< (point) end)
1100 (end-of-line)
1101 (let ((pad (- width (- (point) (line-beginning-position)))))
1102 (when (> pad 0)
1103 (insert (make-string pad ? ))))
1104 (add-face-text-property (line-beginning-position)
1105 (point) 'eww-form-textarea)
1106 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1107 (put-text-property (line-beginning-position) (point)
1108 'local-map eww-textarea-map)
1109 (forward-line 1))
1110 (put-text-property start (point) 'eww-form
1111 (list :eww-form eww-form
1112 :value value
1113 :type "textarea"
1114 :name (dom-attr dom 'name)))))
1115
1116 (defun eww-tag-input (dom)
1117 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1118 (start (point)))
1119 (cond
1120 ((or (equal type "checkbox")
1121 (equal type "radio"))
1122 (eww-form-checkbox dom))
1123 ((equal type "file")
1124 (eww-form-file dom))
1125 ((equal type "submit")
1126 (eww-form-submit dom))
1127 ((equal type "hidden")
1128 (let ((form eww-form)
1129 (name (dom-attr dom 'name)))
1130 ;; Don't add <input type=hidden> elements repeatedly.
1131 (while (and form
1132 (or (not (consp (car form)))
1133 (not (eq (caar form) 'hidden))
1134 (not (equal (plist-get (cdr (car form)) :name)
1135 name))))
1136 (setq form (cdr form)))
1137 (unless form
1138 (nconc eww-form (list
1139 (list 'hidden
1140 :name name
1141 :value (or (dom-attr dom 'value) "")))))))
1142 (t
1143 (eww-form-text dom)))
1144 (unless (= start (point))
1145 (put-text-property start (1+ start) 'help-echo "Input field")
1146 ;; Mark this as an element we can TAB to.
1147 (put-text-property start (1+ start) 'shr-url dom))))
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 file)
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 (setq shr-use-fonts (not shr-use-fonts))
1506 (eww-reload)
1507 (message "Proportional fonts are now %s"
1508 (if shr-use-fonts "on" "off")))
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 (pop-to-buffer "*eww bookmarks*")
1551 (eww-bookmark-prepare))
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 (pop-to-buffer-same-window 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 (pop-to-buffer-same-window 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 (pop-to-buffer-same-window 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