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