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