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