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