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