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