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