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