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