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