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