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