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