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