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