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