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