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