1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
28 (require 'format-spec
)
32 (require 'url-util
) ; for url-get-url-at-point
34 (eval-when-compile (require 'subr-x
)) ;; for string-trim
39 :link
'(custom-manual "(eww) Top")
43 (defcustom eww-header-line-format
"%t: %u"
45 - %t is replaced by the title.
46 - %u is replaced by the URL."
51 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
52 "Prefix URL to search engine"
57 (defcustom eww-download-directory
"~/Downloads/"
58 "Directory where files will downloaded."
63 (defcustom eww-suggest-uris
67 "List of functions called to form the list of default URIs for `eww'.
68 Each of the elements is a function returning either a string or a list
69 of strings. The results will be joined into a single list with
70 duplicate entries (if any) removed."
74 :options
'(eww-links-at-point
78 (defcustom eww-bookmarks-directory user-emacs-directory
79 "Directory where bookmark files will be stored."
84 (defcustom eww-desktop-remove-duplicates t
85 "Whether to remove duplicates from the history when saving desktop data.
86 If non-nil, repetitive EWW history entries (comprising of the URI, the
87 title, and the point position) will not be saved as part of the Emacs
88 desktop. Otherwise, such entries will be retained."
93 (defcustom eww-restore-desktop nil
94 "How to restore EWW buffers on `desktop-restore'.
95 If t or 'auto, the buffers will be reloaded automatically.
96 If nil, buffers will require manual reload, and will contain the text
97 specified in `eww-restore-reload-prompt' instead of the actual Web
101 :type
'(choice (const :tag
"Restore all automatically" t
)
102 (const :tag
"Require manual reload" nil
)))
104 (defcustom eww-restore-reload-prompt
105 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
106 "The string to put in the buffers not reloaded on `desktop-restore'.
107 This prompt will be used if `eww-restore-desktop' is nil.
109 The string will be passed through `substitute-command-keys'."
114 (defcustom eww-history-limit
50
115 "Maximum number of entries to retain in the history."
118 :type
'(choice (const :tag
"Unlimited" nil
)
121 (defcustom eww-use-external-browser-for-content-type
122 "\\`\\(video/\\|audio/\\|application/ogg\\)"
123 "Always use external browser for specified content-type."
126 :type
'(choice (const :tag
"Never" nil
)
129 (defcustom eww-after-render-hook nil
130 "A hook called after eww has finished rendering the buffer."
135 (defcustom eww-form-checkbox-selected-symbol
"[X]"
136 "Symbol used to represent a selected checkbox.
137 See also `eww-form-checkbox-symbol'."
140 :type
'(choice (const "[X]")
141 (const "☒") ; Unicode BALLOT BOX WITH X
142 (const "☑") ; Unicode BALLOT BOX WITH CHECK
145 (defcustom eww-form-checkbox-symbol
"[ ]"
146 "Symbol used to represent a checkbox.
147 See also `eww-form-checkbox-selected-symbol'."
150 :type
'(choice (const "[ ]")
151 (const "☐") ; Unicode BALLOT BOX
154 (defface eww-form-submit
155 '((((type x w32 ns
) (class color
)) ; Like default mode line
156 :box
(:line-width
2 :style released-button
)
157 :background
"#808080" :foreground
"black"))
158 "Face for eww buffer buttons."
162 (defface eww-form-file
163 '((((type x w32 ns
) (class color
)) ; Like default mode line
164 :box
(:line-width
2 :style released-button
)
165 :background
"#808080" :foreground
"black"))
166 "Face for eww buffer buttons."
170 (defface eww-form-checkbox
171 '((((type x w32 ns
) (class color
)) ; Like default mode line
172 :box
(:line-width
2 :style released-button
)
173 :background
"lightgrey" :foreground
"black"))
174 "Face for eww buffer buttons."
178 (defface eww-form-select
179 '((((type x w32 ns
) (class color
)) ; Like default mode line
180 :box
(:line-width
2 :style released-button
)
181 :background
"lightgrey" :foreground
"black"))
182 "Face for eww buffer buttons."
186 (defface eww-form-text
187 '((t (:background
"#505050"
189 :box
(:line-width
1))))
190 "Face for eww text inputs."
194 (defface eww-form-textarea
195 '((t (:background
"#C0C0C0"
197 :box
(:line-width
1))))
198 "Face for eww textarea inputs."
202 (defvar eww-data nil
)
203 (defvar eww-history nil
)
204 (defvar eww-history-position
0)
206 (defvar eww-local-regex
"localhost"
207 "When this regex is found in the URL, it's not a keyword but an address.")
209 (defvar eww-link-keymap
210 (let ((map (copy-keymap shr-map
)))
211 (define-key map
"\r" 'eww-follow-link
)
214 (defun eww-suggested-uris nil
215 "Return the list of URIs to suggest at the `eww' prompt.
216 This list can be customized via `eww-suggest-uris'."
217 (let ((obseen (make-vector 42 0))
219 (dolist (fun eww-suggest-uris
)
220 (let ((ret (funcall fun
)))
221 (dolist (uri (if (stringp ret
) (list ret
) ret
))
222 (when (and uri
(not (intern-soft uri obseen
)))
229 "Fetch URL and render the page.
230 If the input doesn't look like an URL or a domain name, the
231 word(s) will be searched for via `eww-search-prefix'."
233 (let* ((uris (eww-suggested-uris))
234 (prompt (concat "Enter URL or keywords"
235 (if uris
(format " (default %s)" (car uris
)) "")
237 (list (read-string prompt nil nil uris
))))
238 (setq url
(string-trim url
))
239 (cond ((string-match-p "\\`file:/" url
))
240 ;; Don't mangle file: URLs at all.
241 ((string-match-p "\\`ftp://" url
)
242 (user-error "FTP is not supported."))
244 (if (and (= (length (split-string url
)) 1)
245 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
246 (> (length (split-string url
"[.:]")) 1))
247 (string-match eww-local-regex url
)))
249 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
250 (setq url
(concat "http://" url
)))
251 ;; some site don't redirect final /
252 (when (string= (url-filename (url-generic-parse-url url
)) "")
253 (setq url
(concat url
"/"))))
254 (setq url
(concat eww-search-prefix
255 (replace-regexp-in-string " " "+" url
))))))
256 (if (eq major-mode
'eww-mode
)
257 (when (or (plist-get eww-data
:url
)
258 (plist-get eww-data
:dom
))
261 (plist-put eww-data
:url url
)
262 (eww-update-header-line-format)
263 (let ((inhibit-read-only t
))
264 (insert (format "Loading %s..." url
))
265 (goto-char (point-min))))
266 (url-retrieve url
'eww-render
267 (list url nil
(current-buffer))))
269 ;;;###autoload (defalias 'browse-web 'eww)
272 (defun eww-open-file (file)
273 "Render a file using EWW."
274 (interactive "fFile: ")
275 (eww (concat "file://"
276 (and (memq system-type
'(windows-nt ms-dos
))
278 (expand-file-name file
))))
281 (defun eww-search-words (&optional beg end
)
282 "Search the web for the text between the point and marker.
283 See the `eww-search-prefix' variable for the search engine used."
285 (eww (buffer-substring beg end
)))
287 (defun eww-render (status url
&optional point buffer encode
)
288 (let ((redirect (plist-get status
:redirect
)))
290 (setq url redirect
)))
291 (let* ((headers (eww-parse-headers))
293 (mail-header-parse-content-type
294 (or (cdr (assoc "content-type" headers
))
298 (or (cdr (assq 'charset
(cdr content-type
)))
299 (eww-detect-charset (equal (car content-type
)
302 (data-buffer (current-buffer)))
306 ((and eww-use-external-browser-for-content-type
307 (string-match-p eww-use-external-browser-for-content-type
309 (eww-browse-with-external-browser url
))
310 ((equal (car content-type
) "text/html")
311 (eww-display-html charset url nil point buffer encode
))
312 ((equal (car content-type
) "application/pdf")
314 ((string-match-p "\\`image/" (car content-type
))
315 (eww-display-image buffer
))
317 (eww-display-raw buffer encode
)))
318 (with-current-buffer buffer
319 (plist-put eww-data
:url url
)
320 (eww-update-header-line-format)
321 (setq eww-history-position
0)
322 (run-hooks 'eww-after-render-hook
)))
323 (kill-buffer data-buffer
))))
325 (defun eww-parse-headers ()
327 (goto-char (point-min))
328 (while (and (not (eobp))
330 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
331 (push (cons (downcase (match-string 1))
339 (defun eww-detect-charset (html-p)
340 (let ((case-fold-search t
)
344 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
348 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
351 (declare-function libxml-parse-html-region
"xml.c"
352 (start end
&optional base-url
))
354 (defun eww-display-html (charset url
&optional document point buffer encode
)
355 (unless (fboundp 'libxml-parse-html-region
)
356 (error "This function requires Emacs to be compiled with libxml2"))
357 (unless (buffer-live-p buffer
)
358 (error "Buffer %s doesn't exist" buffer
))
359 ;; There should be a better way to abort loading images
365 'base
(list (cons 'href url
))
367 (when (or (and encode
368 (not (eq charset encode
)))
369 (not (eq charset
'utf-8
)))
371 (decode-coding-region (point) (point-max)
373 (coding-system-error nil
)))
374 (libxml-parse-html-region (point) (point-max))))))
375 (source (and (null document
)
376 (buffer-substring (point) (point-max)))))
377 (with-current-buffer buffer
378 (plist-put eww-data
:source source
)
379 (plist-put eww-data
:dom document
)
380 (let ((inhibit-read-only t
)
381 (inhibit-modification-hooks t
)
382 (shr-target-id (url-target (url-generic-parse-url url
)))
383 (shr-external-rendering-functions
384 '((title . eww-tag-title
)
385 (form . eww-tag-form
)
386 (input . eww-tag-input
)
387 (textarea . eww-tag-textarea
)
388 (body . eww-tag-body
)
389 (select . eww-tag-select
)
390 (link . eww-tag-link
)
393 (shr-insert-document document
)
398 (goto-char (point-min))
399 (let ((point (next-single-property-change
400 (point-min) 'shr-target-id
)))
404 (goto-char (point-min))
405 ;; Don't leave point inside forms, because the normal eww
406 ;; commands aren't available there.
407 (while (and (not (eobp))
408 (get-text-property (point) 'eww-form
))
410 (eww-size-text-inputs))))
412 (defun eww-handle-link (dom)
413 (let* ((rel (dom-attr dom
'rel
))
414 (href (dom-attr dom
'href
))
416 ;; The text associated with :rel is case-insensitive.
417 (if rel
(downcase rel
))
419 ;; Texinfo uses "previous", but HTML specifies
420 ;; "prev", so recognize both.
421 ("previous" .
:previous
)
423 ;; HTML specifies "start" but also "contents",
424 ;; and Gtk seems to use "home". Recognize
425 ;; them all; but store them in different
426 ;; variables so that we can readily choose the
430 ("contents" .
:contents
)
434 (plist-put eww-data
(cdr where
) href
))))
436 (defun eww-tag-link (dom)
437 (eww-handle-link dom
)
440 (defun eww-tag-a (dom)
441 (eww-handle-link dom
)
442 (let ((start (point)))
444 (put-text-property start
(point) 'keymap eww-link-keymap
)))
446 (defun eww-update-header-line-format ()
447 (if eww-header-line-format
448 (setq header-line-format
449 (replace-regexp-in-string
451 ;; FIXME? Title can be blank. Default to, eg, last component
453 (format-spec eww-header-line-format
454 `((?u .
,(or (plist-get eww-data
:url
) ""))
455 (?t .
,(or (plist-get eww-data
:title
) ""))))))
456 (setq header-line-format nil
)))
458 (defun eww-tag-title (dom)
459 (plist-put eww-data
:title
460 (replace-regexp-in-string
462 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom
))))
463 (eww-update-header-line-format))
465 (defun eww-tag-body (dom)
466 (let* ((start (point))
467 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
468 (bgcolor (dom-attr dom
'bgcolor
))
469 (shr-stylesheet (list (cons 'color fgcolor
)
470 (cons 'background-color bgcolor
))))
472 (shr-colorize-region start
(point) fgcolor bgcolor
)))
474 (defun eww-display-raw (buffer &optional encode
)
475 (let ((data (buffer-substring (point) (point-max))))
476 (unless (buffer-live-p buffer
)
477 (error "Buffer %s doesn't exist" buffer
))
478 (with-current-buffer buffer
479 (let ((inhibit-read-only t
))
482 (unless (eq encode
'utf-8
)
483 (encode-coding-region (point-min) (1+ (length data
)) 'utf-8
)
485 (decode-coding-region (point-min) (1+ (length data
)) encode
)
486 (coding-system-error nil
))))
487 (goto-char (point-min)))))
489 (defun eww-display-image (buffer)
490 (let ((data (shr-parse-image-data)))
491 (unless (buffer-live-p buffer
)
492 (error "Buffer %s doesn't exist" buffer
))
493 (with-current-buffer buffer
494 (let ((inhibit-read-only t
))
496 (shr-put-image data nil
))
497 (goto-char (point-min)))))
499 (declare-function mailcap-view-mime
"mailcap" (type))
500 (defun eww-display-pdf ()
501 (let ((data (buffer-substring (point) (point-max))))
502 (switch-to-buffer (get-buffer-create "*eww pdf*"))
503 (let ((coding-system-for-write 'raw-text
)
504 (inhibit-read-only t
))
507 (mailcap-view-mime "application/pdf")))
508 (goto-char (point-min)))
510 (defun eww-setup-buffer ()
511 (switch-to-buffer (get-buffer-create "*eww*"))
512 (let ((inhibit-read-only t
))
515 (unless (eq major-mode
'eww-mode
)
518 (defun eww-current-url nil
519 "Return URI of the Web page the current EWW buffer is visiting."
520 (plist-get eww-data
:url
))
522 (defun eww-links-at-point (&optional pt
)
523 "Return list of URIs, if any, linked at point."
525 (list (get-text-property (point) 'shr-url
)
526 (get-text-property (point) 'image-url
))))
528 (defun eww-view-source ()
529 "View the HTML source code of the current page."
531 (let ((buf (get-buffer-create "*eww-source*"))
532 (source (plist-get eww-data
:source
)))
533 (with-current-buffer buf
534 (let ((inhibit-read-only t
))
535 (delete-region (point-min) (point-max))
536 (insert (or source
"no source"))
537 (goto-char (point-min))
538 (when (fboundp 'html-mode
)
542 (defun eww-readable ()
543 "View the main \"readable\" parts of the current web page.
544 This command uses heuristics to find the parts of the web page that
545 contains the main textual portion, leaving out navigation menus and
548 (let* ((old-data eww-data
)
549 (dom (with-temp-buffer
550 (insert (plist-get old-data
:source
))
552 (decode-coding-region (point-min) (point-max) 'utf-8
)
553 (coding-system-error nil
))
554 (libxml-parse-html-region (point-min) (point-max)))))
555 (eww-score-readability dom
)
557 (eww-display-html nil nil
558 (eww-highest-readability dom
)
559 nil
(current-buffer))
560 (dolist (elem '(:source
:url
:title
:next
:previous
:up
))
561 (plist-put eww-data elem
(plist-get old-data elem
)))
562 (eww-update-header-line-format)))
564 (defun eww-score-readability (node)
567 ((memq (dom-tag node
) '(script head comment
))
569 ((eq (dom-tag node
) 'meta
)
571 ((eq (dom-tag node
) 'img
)
573 ((eq (dom-tag node
) 'a
)
574 (setq score
(- (length (split-string (dom-text node
))))))
576 (dolist (elem (dom-children node
))
578 (setq score
(+ score
(length (split-string elem
))))
580 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
581 (eww-score-readability elem
))))))))
582 ;; Cache the score of the node to avoid recomputing all the time.
583 (dom-set-attribute node
:eww-readability-score score
)
586 (defun eww-highest-readability (node)
589 (dolist (elem (dom-non-text-children node
))
590 (when (> (or (dom-attr
591 (setq highest
(eww-highest-readability elem
))
592 :eww-readability-score
)
593 most-negative-fixnum
)
594 (or (dom-attr result
:eww-readability-score
)
595 most-negative-fixnum
))
596 (setq result highest
)))
600 (let ((map (make-sparse-keymap)))
601 (suppress-keymap map
)
602 (define-key map
"q" 'quit-window
)
603 (define-key map
"g" 'eww-reload
)
604 (define-key map
"G" 'eww
)
605 (define-key map
[?
\t] 'shr-next-link
)
606 (define-key map
[?\M-
\t] 'shr-previous-link
)
607 (define-key map
[backtab] 'shr-previous-link)
608 (define-key map [delete] 'scroll-down-command)
609 (define-key map [?\S-\ ] 'scroll-down-command)
610 (define-key map "\177" 'scroll-down-command)
611 (define-key map " " 'scroll-up-command)
612 (define-key map "l" 'eww-back-url)
613 (define-key map "r" 'eww-forward-url)
614 (define-key map "n" 'eww-next-url)
615 (define-key map "p" 'eww-previous-url)
616 (define-key map "u" 'eww-up-url)
617 (define-key map "t" 'eww-top-url)
618 (define-key map "&" 'eww-browse-with-external-browser)
619 (define-key map "d" 'eww-download)
620 (define-key map "w" 'eww-copy-page-url)
621 (define-key map "C" 'url-cookie-list)
622 (define-key map "v" 'eww-view-source)
623 (define-key map "R" 'eww-readable)
624 (define-key map "H" 'eww-list-histories)
625 (define-key map "E" 'eww-set-character-encoding)
627 (define-key map "b" 'eww-add-bookmark)
628 (define-key map "B" 'eww-list-bookmarks)
629 (define-key map [(meta n)] 'eww-next-bookmark)
630 (define-key map [(meta p)] 'eww-previous-bookmark)
632 (easy-menu-define nil map ""
634 ["Exit" quit-window t]
635 ["Close browser" quit-window t]
636 ["Reload" eww-reload t]
637 ["Back to previous page" eww-back-url
638 :active (not (zerop (length eww-history)))]
639 ["Forward to next page" eww-forward-url
640 :active (not (zerop eww-history-position))]
641 ["Browse with external browser" eww-browse-with-external-browser t]
642 ["Download" eww-download t]
643 ["View page source" eww-view-source]
644 ["Copy page URL" eww-copy-page-url t]
645 ["List histories" eww-list-histories t]
646 ["Add bookmark" eww-add-bookmark t]
647 ["List bookmarks" eww-list-bookmarks t]
648 ["List cookies" url-cookie-list t]
649 ["Character Encoding" eww-set-character-encoding]))
652 (defvar eww-tool-bar-map
653 (let ((map (make-sparse-keymap)))
654 (dolist (tool-bar-item
655 '((quit-window . "close")
656 (eww-reload . "refresh")
657 (eww-back-url . "left-arrow")
658 (eww-forward-url . "right-arrow")
659 (eww-view-source . "show")
660 (eww-copy-page-url . "copy")
661 (eww-add-bookmark . "bookmark_add"))) ;; ...
662 (tool-bar-local-item-from-menu
663 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
665 "Tool bar for `eww-mode'.")
667 (define-derived-mode eww-mode nil "eww"
668 "Mode for browsing the web.
671 (setq-local eww-data (list :title ""))
672 (setq-local browse-url-browser-function 'eww-browse-url)
673 (setq-local after-change-functions 'eww-process-text-input)
674 (setq-local eww-history nil)
675 (setq-local eww-history-position 0)
676 (when (boundp 'tool-bar-map)
677 (setq-local tool-bar-map eww-tool-bar-map))
679 (setq-local desktop-save-buffer 'eww-desktop-misc-data)
680 (buffer-disable-undo)
681 (setq buffer-read-only t))
684 (defun eww-browse-url (url &optional new-window)
686 (switch-to-buffer (generate-new-buffer "*eww*"))
690 (defun eww-back-url ()
691 "Go to the previously displayed page."
693 (when (>= eww-history-position (length eww-history))
694 (user-error "No previous page"))
696 (setq eww-history-position (+ eww-history-position 2))
697 (eww-restore-history (elt eww-history (1- eww-history-position))))
699 (defun eww-forward-url ()
700 "Go to the next displayed page."
702 (when (zerop eww-history-position)
703 (user-error "No next page"))
705 (eww-restore-history (elt eww-history (1- eww-history-position))))
707 (defun eww-restore-history (elem)
708 (let ((inhibit-read-only t)
709 (inhibit-modification-hooks t)
710 (text (plist-get elem :text)))
713 (eww-reload) ; FIXME: restore :point?
716 (goto-char (plist-get elem :point))
717 (eww-update-header-line-format))))
719 (defun eww-next-url ()
720 "Go to the page marked `next'.
721 A page is marked `next' if rel=\"next\" appears in a <link>
724 (if (plist-get eww-data :next)
725 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
726 (plist-get eww-data :url)))
727 (user-error "No `next' on this page")))
729 (defun eww-previous-url ()
730 "Go to the page marked `previous'.
731 A page is marked `previous' if rel=\"previous\" appears in a <link>
734 (if (plist-get eww-data :previous)
735 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
736 (plist-get eww-data :url)))
737 (user-error "No `previous' on this page")))
740 "Go to the page marked `up'.
741 A page is marked `up' if rel=\"up\" appears in a <link>
744 (if (plist-get eww-data :up)
745 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
746 (plist-get eww-data :url)))
747 (user-error "No `up' on this page")))
749 (defun eww-top-url ()
750 "Go to the page marked `top'.
751 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
752 appears in a <link> or <a> tag."
754 (let ((best-url (or (plist-get eww-data :start)
755 (plist-get eww-data :contents)
756 (plist-get eww-data :home))))
758 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
759 (user-error "No `top' for this page"))))
761 (defun eww-reload (&optional local encode)
762 "Reload the current page.
763 If LOCAL (the command prefix), don't reload the page from the
764 network, but just re-display the HTML already fetched."
766 (let ((url (plist-get eww-data :url)))
768 (if (null (plist-get eww-data :dom))
769 (error "No current HTML data")
770 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
771 (point) (current-buffer)))
772 (url-retrieve url 'eww-render
773 (list url (point) (current-buffer) encode)))))
777 (defvar eww-form nil)
779 (defvar eww-submit-map
780 (let ((map (make-sparse-keymap)))
781 (define-key map "\r" 'eww-submit)
782 (define-key map [(control c) (control c)] 'eww-submit)
785 (defvar eww-submit-file
786 (let ((map (make-sparse-keymap)))
787 (define-key map "\r" 'eww-select-file)
788 (define-key map [(control c) (control c)] 'eww-submit)
791 (defvar eww-checkbox-map
792 (let ((map (make-sparse-keymap)))
793 (define-key map " " 'eww-toggle-checkbox)
794 (define-key map "\r" 'eww-toggle-checkbox)
795 (define-key map [(control c) (control c)] 'eww-submit)
799 (let ((map (make-keymap)))
800 (set-keymap-parent map text-mode-map)
801 (define-key map "\r" 'eww-submit)
802 (define-key map [(control a)] 'eww-beginning-of-text)
803 (define-key map [(control c) (control c)] 'eww-submit)
804 (define-key map [(control e)] 'eww-end-of-text)
805 (define-key map [?\t] 'shr-next-link)
806 (define-key map [?\M-\t] 'shr-previous-link)
809 (defvar eww-textarea-map
810 (let ((map (make-keymap)))
811 (set-keymap-parent map text-mode-map)
812 (define-key map "\r" 'forward-line)
813 (define-key map [(control c) (control c)] 'eww-submit)
814 (define-key map [?\t] 'shr-next-link)
815 (define-key map [?\M-\t] 'shr-previous-link)
818 (defvar eww-select-map
819 (let ((map (make-sparse-keymap)))
820 (define-key map "\r" 'eww-change-select)
821 (define-key map [(control c) (control c)] 'eww-submit)
824 (defun eww-beginning-of-text ()
825 "Move to the start of the input field."
827 (goto-char (eww-beginning-of-field)))
829 (defun eww-end-of-text ()
830 "Move to the end of the text in the input field."
832 (goto-char (eww-end-of-field))
833 (let ((start (eww-beginning-of-field)))
834 (while (and (equal (following-char) ? )
837 (when (> (point) start)
840 (defun eww-beginning-of-field ()
844 ((not (eq (get-text-property (point) 'eww-form)
845 (get-text-property (1- (point)) 'eww-form)))
848 (previous-single-property-change
849 (point) 'eww-form nil (point-min)))))
851 (defun eww-end-of-field ()
852 (1- (next-single-property-change
853 (point) 'eww-form nil (point-max))))
855 (defun eww-tag-form (dom)
856 (let ((eww-form (list (cons :method (dom-attr dom 'method))
857 (cons :action (dom-attr dom 'action))))
859 (shr-ensure-paragraph)
864 (when (> (point) start)
865 (put-text-property start (1+ start)
866 'eww-form eww-form))))
868 (defun eww-form-submit (dom)
869 (let ((start (point))
870 (value (dom-attr dom 'value)))
872 (if (zerop (length value))
876 (add-face-text-property start (point) 'eww-form-submit)
877 (put-text-property start (point) 'eww-form
878 (list :eww-form eww-form
881 :name (dom-attr dom 'name)))
882 (put-text-property start (point) 'keymap eww-submit-map)
885 (defun eww-form-checkbox (dom)
886 (let ((start (point)))
887 (if (dom-attr dom 'checked)
888 (insert eww-form-checkbox-selected-symbol)
889 (insert eww-form-checkbox-symbol))
890 (add-face-text-property start (point) 'eww-form-checkbox)
891 (put-text-property start (point) 'eww-form
892 (list :eww-form eww-form
893 :value (dom-attr dom 'value)
894 :type (downcase (dom-attr dom 'type))
895 :checked (dom-attr dom 'checked)
896 :name (dom-attr dom 'name)))
897 (put-text-property start (point) 'keymap eww-checkbox-map)
900 (defun eww-form-file (dom)
901 (let ((start (point))
902 (value (dom-attr dom 'value)))
904 (if (zerop (length value))
908 (add-face-text-property start (point) 'eww-form-file)
910 (put-text-property start (point) 'eww-form
911 (list :eww-form eww-form
912 :value (dom-attr dom 'value)
913 :type (downcase (dom-attr dom 'type))
914 :name (dom-attr dom 'name)))
915 (put-text-property start (point) 'keymap eww-submit-file)
918 (defun eww-select-file ()
919 "Change the value of the upload file menu under point."
921 (let* ((input (get-text-property (point) 'eww-form)))
923 (let ((insert-default-directory t))
924 (read-file-name "filename: "))))
925 (eww-update-field filename (length "Browse"))
926 (plist-put input :filename filename))))
928 (defun eww-form-text (dom)
929 (let ((start (point))
930 (type (downcase (or (dom-attr dom 'type) "text")))
931 (value (or (dom-attr dom 'value) ""))
932 (width (string-to-number (or (dom-attr dom 'size) "40")))
933 (readonly-property (if (or (dom-attr dom 'disabled)
934 (dom-attr dom 'readonly))
936 'inhibit-read-only)))
938 (when (< (length value) width)
939 (insert (make-string (- width (length value)) ? )))
940 (put-text-property start (point) 'face 'eww-form-text)
941 (put-text-property start (point) 'inhibit-read-only t)
942 (put-text-property start (point) 'local-map eww-text-map)
943 (put-text-property start (point) readonly-property t)
944 (put-text-property start (point) 'eww-form
945 (list :eww-form eww-form
948 :name (dom-attr dom 'name)))
951 (defconst eww-text-input-types '("text" "password" "textarea"
952 "color" "date" "datetime" "datetime-local"
953 "email" "month" "number" "search" "tel"
955 "List of input types which represent a text input.
956 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
958 (defun eww-process-text-input (beg end replace-length)
959 (when-let (pos (and (< (1+ end) (point-max))
960 (> (1- end) (point-min))
962 ((get-text-property (1+ end) 'eww-form)
964 ((get-text-property (1- end) 'eww-form)
966 (let* ((form (get-text-property pos 'eww-form))
967 (properties (text-properties-at pos))
968 (inhibit-read-only t)
969 (length (- end beg replace-length))
970 (type (plist-get form :type)))
972 (member type eww-text-input-types))
975 ;; Delete some space at the end.
978 (if (equal type "textarea")
979 (1- (line-end-position))
981 (while (and (> length 0)
982 (eql (following-char) ? ))
983 (delete-region (1- (point)) (point))
990 (if (equal type "textarea")
991 (1- (line-end-position))
992 (1+ (eww-end-of-field))))
993 (let ((start (point)))
994 (insert (make-string (abs length) ? ))
995 (set-text-properties start (point) properties))
996 (goto-char (1- end)))))
997 (set-text-properties (plist-get form :start) (plist-get form :end)
999 (let ((value (buffer-substring-no-properties
1000 (eww-beginning-of-field)
1001 (eww-end-of-field))))
1002 (when (string-match " +\\'" value)
1003 (setq value (substring value 0 (match-beginning 0))))
1004 (plist-put form :value value)
1005 (when (equal type "password")
1006 ;; Display passwords as asterisks.
1007 (let ((start (eww-beginning-of-field)))
1008 (put-text-property start (+ start (length value))
1009 'display (make-string (length value) ?*)))))))))
1011 (defun eww-tag-textarea (dom)
1012 (let ((start (point))
1013 (value (or (dom-attr dom 'value) ""))
1014 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1015 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1017 (shr-ensure-newline)
1019 (shr-ensure-newline)
1020 (when (< (count-lines start (point)) lines)
1021 (dotimes (i (- lines (count-lines start (point))))
1023 (setq end (point-marker))
1025 (while (< (point) end)
1027 (let ((pad (- width (- (point) (line-beginning-position)))))
1029 (insert (make-string pad ? ))))
1030 (add-face-text-property (line-beginning-position)
1031 (point) 'eww-form-textarea)
1032 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1033 (put-text-property (line-beginning-position) (point)
1034 'local-map eww-textarea-map)
1036 (put-text-property start (point) 'eww-form
1037 (list :eww-form eww-form
1040 :name (dom-attr dom 'name)))))
1042 (defun eww-tag-input (dom)
1043 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1046 ((or (equal type "checkbox")
1047 (equal type "radio"))
1048 (eww-form-checkbox dom))
1049 ((equal type "file")
1050 (eww-form-file dom))
1051 ((equal type "submit")
1052 (eww-form-submit dom))
1053 ((equal type "hidden")
1054 (let ((form eww-form)
1055 (name (dom-attr dom 'name)))
1056 ;; Don't add <input type=hidden> elements repeatedly.
1058 (or (not (consp (car form)))
1059 (not (eq (caar form) 'hidden))
1060 (not (equal (plist-get (cdr (car form)) :name)
1062 (setq form (cdr form)))
1064 (nconc eww-form (list
1067 :value (dom-attr dom 'value)))))))
1069 (eww-form-text dom)))
1070 (unless (= start (point))
1071 (put-text-property start (1+ start) 'help-echo "Input field"))))
1073 (defun eww-tag-select (dom)
1074 (shr-ensure-paragraph)
1075 (let ((menu (list :name (dom-attr dom 'name)
1076 :eww-form eww-form))
1081 (if (eq (dom-tag dom) 'optgroup)
1082 (dolist (groupelem (dom-children dom))
1083 (unless (dom-attr groupelem 'disabled)
1084 (setq opelem (append opelem (list groupelem)))))
1085 (setq opelem (list dom)))
1086 (dolist (elem opelem)
1087 (when (eq (dom-tag elem) 'option)
1088 (when (dom-attr elem 'selected)
1089 (nconc menu (list :value (dom-attr elem 'value))))
1090 (let ((display (dom-text elem)))
1091 (setq max (max max (length display)))
1093 :value (dom-attr elem 'value)
1097 (setq options (nreverse options))
1098 ;; If we have no selected values, default to the first value.
1099 (unless (plist-get menu :value)
1100 (nconc menu (list :value (nth 2 (car options)))))
1101 (nconc menu options)
1102 (let ((selected (eww-select-display menu)))
1104 (make-string (- max (length selected)) ? )))
1105 (put-text-property start (point) 'eww-form menu)
1106 (add-face-text-property start (point) 'eww-form-select)
1107 (put-text-property start (point) 'keymap eww-select-map)
1108 (unless (= start (point))
1109 (put-text-property start (1+ start) 'help-echo "select field"))
1110 (shr-ensure-paragraph))))
1112 (defun eww-select-display (select)
1113 (let ((value (plist-get select :value))
1115 (dolist (elem select)
1116 (when (and (consp elem)
1117 (eq (car elem) 'item)
1118 (equal value (plist-get (cdr elem) :value)))
1119 (setq display (plist-get (cdr elem) :display))))
1122 (defun eww-change-select ()
1123 "Change the value of the select drop-down menu under point."
1125 (let* ((input (get-text-property (point) 'eww-form))
1126 (completion-ignore-case t)
1129 (mapcar (lambda (elem)
1131 (eq (car elem) 'item)
1132 (cons (plist-get (cdr elem) :display)
1133 (plist-get (cdr elem) :value))))
1136 (completing-read "Change value: " options nil 'require-match))
1137 (inhibit-read-only t))
1138 (plist-put input :value (cdr (assoc-string display options t)))
1140 (eww-update-field display))))
1142 (defun eww-update-field (string &optional offset)
1143 (if (not offset) (setq offset 0))
1144 (let ((properties (text-properties-at (point)))
1145 (start (+ (eww-beginning-of-field) offset))
1146 (current-end (1+ (eww-end-of-field)))
1147 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1148 (delete-region start current-end)
1149 (forward-char offset)
1151 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1152 (if (= 0 offset) (set-text-properties start new-end properties))
1155 (defun eww-toggle-checkbox ()
1156 "Toggle the value of the checkbox under point."
1158 (let* ((input (get-text-property (point) 'eww-form))
1159 (type (plist-get input :type)))
1160 (if (equal type "checkbox")
1163 (if (plist-get input :checked)
1165 (plist-put input :checked nil)
1166 (eww-update-field eww-form-checkbox-symbol))
1167 (plist-put input :checked t)
1168 (eww-update-field eww-form-checkbox-selected-symbol))))
1169 ;; Radio button. Switch all other buttons off.
1170 (let ((name (plist-get input :name)))
1172 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1173 (when (equal (plist-get (cdr elem) :name) name)
1174 (goto-char (car elem))
1175 (if (not (eq (cdr elem) input))
1177 (plist-put input :checked nil)
1178 (eww-update-field eww-form-checkbox-symbol))
1179 (plist-put input :checked t)
1180 (eww-update-field eww-form-checkbox-selected-symbol)))))
1181 (forward-char 1)))))
1183 (defun eww-inputs (form)
1184 (let ((start (point-min))
1187 (< start (point-max)))
1188 (when (or (get-text-property start 'eww-form)
1189 (setq start (next-single-property-change start 'eww-form)))
1190 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1192 (push (cons start (get-text-property start 'eww-form))
1194 (setq start (next-single-property-change start 'eww-form))))
1197 (defun eww-size-text-inputs ()
1198 (let ((start (point-min)))
1200 (< start (point-max)))
1201 (when (or (get-text-property start 'eww-form)
1202 (setq start (next-single-property-change start 'eww-form)))
1203 (let ((props (get-text-property start 'eww-form)))
1204 (plist-put props :start start)
1205 (setq start (next-single-property-change
1206 start 'eww-form nil (point-max)))
1207 (plist-put props :end start))))))
1209 (defun eww-input-value (input)
1210 (let ((type (plist-get input :type))
1211 (value (plist-get input :value)))
1213 ((equal type "textarea")
1216 (goto-char (point-min))
1217 (while (re-search-forward "^ +\n\\| +$" nil t)
1218 (replace-match "" t t))
1221 (if (string-match " +\\'" value)
1222 (substring value 0 (match-beginning 0))
1225 (defun eww-submit ()
1226 "Submit the current form."
1228 (let* ((this-input (get-text-property (point) 'eww-form))
1229 (form (plist-get this-input :eww-form))
1231 (dolist (elem (sort (eww-inputs form)
1233 (< (car o1) (car o2)))))
1234 (let* ((input (cdr elem))
1235 (input-start (car elem))
1236 (name (plist-get input :name)))
1239 ((member (plist-get input :type) '("checkbox" "radio"))
1240 (when (plist-get input :checked)
1241 (push (cons name (plist-get input :value))
1243 ((equal (plist-get input :type) "file")
1245 (list (cons "filedata"
1247 (insert-file-contents
1248 (plist-get input :filename))
1250 (cons "name" (plist-get input :name))
1251 (cons "filename" (plist-get input :filename))))
1253 ((equal (plist-get input :type) "submit")
1254 ;; We want the values from buttons if we hit a button if
1255 ;; we hit enter on it, or if it's the first button after
1256 ;; the field we did hit return on.
1257 (when (or (eq input this-input)
1258 (and (not (eq input this-input))
1260 (> input-start (point))))
1261 (setq next-submit t)
1262 (push (cons name (plist-get input :value))
1265 (push (cons name (eww-input-value input))
1268 (when (and (consp elem)
1269 (eq (car elem) 'hidden))
1270 (push (cons (plist-get (cdr elem) :name)
1271 (or (plist-get (cdr elem) :value) ""))
1273 (if (and (stringp (cdr (assq :method form)))
1274 (equal (downcase (cdr (assq :method form))) "post"))
1276 (dolist (x values mtype)
1277 (if (equal (car x) "file")
1279 (setq mtype "multipart/form-data"))))
1280 (cond ((equal mtype "multipart/form-data")
1281 (let ((boundary (mml-compute-boundary '())))
1282 (let ((url-request-method "POST")
1283 (url-request-extra-headers
1284 (list (cons "Content-Type"
1285 (concat "multipart/form-data; boundary="
1288 (mm-url-encode-multipart-form-data values boundary)))
1289 (eww-browse-url (shr-expand-url
1290 (cdr (assq :action form))
1291 (plist-get eww-data :url))))))
1293 (let ((url-request-method "POST")
1294 (url-request-extra-headers
1296 "application/x-www-form-urlencoded")))
1298 (mm-url-encode-www-form-urlencoded values)))
1299 (eww-browse-url (shr-expand-url
1300 (cdr (assq :action form))
1301 (plist-get eww-data :url)))))))
1304 (if (cdr (assq :action form))
1305 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1306 (plist-get eww-data :url))
1308 (mm-url-encode-www-form-urlencoded values))))))
1310 (defun eww-browse-with-external-browser (&optional url)
1311 "Browse the current URL with an external browser.
1312 The browser to used is specified by the `shr-external-browser' variable."
1314 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1316 (defun eww-follow-link (&optional external mouse-event)
1317 "Browse the URL under point.
1318 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1319 If EXTERNAL is double prefix, browse in new buffer."
1320 (interactive (list current-prefix-arg last-nonmenu-event))
1321 (mouse-set-point mouse-event)
1322 (let ((url (get-text-property (point) 'shr-url)))
1325 (message "No link under point"))
1326 ((string-match "^mailto:" url)
1327 (browse-url-mail url))
1328 ((and (consp external) (<= (car external) 4))
1329 (funcall shr-external-browser url))
1330 ;; This is a #target url in the same page as the current one.
1331 ((and (url-target (url-generic-parse-url url))
1332 (eww-same-page-p url (plist-get eww-data :url)))
1333 (let ((dom (plist-get eww-data :dom)))
1335 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1337 (eww-browse-url url external)))))
1339 (defun eww-same-page-p (url1 url2)
1340 "Return non-nil if both URLs represent the same page.
1341 Differences in #targets are ignored."
1342 (let ((obj1 (url-generic-parse-url url1))
1343 (obj2 (url-generic-parse-url url2)))
1344 (setf (url-target obj1) nil)
1345 (setf (url-target obj2) nil)
1346 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1348 (defun eww-copy-page-url ()
1350 (message "%s" (plist-get eww-data :url))
1351 (kill-new (plist-get eww-data :url)))
1353 (defun eww-download ()
1354 "Download URL under point to `eww-download-directory'."
1356 (let ((url (get-text-property (point) 'shr-url)))
1358 (message "No URL under point")
1359 (url-retrieve url 'eww-download-callback (list url)))))
1361 (defun eww-download-callback (status url)
1362 (unless (plist-get status :error)
1363 (let* ((obj (url-generic-parse-url url))
1364 (path (car (url-path-and-query obj)))
1365 (file (eww-make-unique-file-name (file-name-nondirectory path)
1366 eww-download-directory)))
1367 (goto-char (point-min))
1368 (re-search-forward "\r?\n\r?\n")
1369 (write-region (point) (point-max) file)
1370 (message "Saved %s" file))))
1372 (defun eww-make-unique-file-name (file directory)
1374 ((zerop (length file))
1376 ((string-match "\\`[.]" file)
1377 (setq file (concat "!" file))))
1379 (while (file-exists-p (expand-file-name file directory))
1381 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1382 (format "%s(%d)%s" (match-string 1 file)
1383 count (match-string 2 file))
1384 (format "%s(%d)" file count)))
1385 (setq count (1+ count)))
1386 (expand-file-name file directory)))
1388 (defun eww-set-character-encoding (charset)
1389 "Set character encoding."
1390 (interactive "zUse character set (default utf-8): ")
1392 (eww-reload nil 'utf-8)
1393 (eww-reload nil charset)))
1397 (defvar eww-bookmarks nil)
1399 (defun eww-add-bookmark ()
1400 "Add the current page to the bookmarks."
1402 (eww-read-bookmarks)
1403 (dolist (bookmark eww-bookmarks)
1404 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1405 (user-error "Already bookmarked")))
1406 (if (y-or-n-p "bookmark this page? ")
1408 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1409 (plist-get eww-data :title))))
1410 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1411 (push (list :url (plist-get eww-data :url)
1413 :time (current-time-string))
1415 (eww-write-bookmarks)
1416 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1417 (plist-get eww-data :title)))))
1419 (defun eww-write-bookmarks ()
1420 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1421 (insert ";; Auto-generated file; don't edit\n")
1422 (pp eww-bookmarks (current-buffer))))
1424 (defun eww-read-bookmarks ()
1425 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1427 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1429 (insert-file-contents file)
1430 (read (current-buffer)))))))
1433 (defun eww-list-bookmarks ()
1434 "Display the bookmarks."
1436 (eww-bookmark-prepare)
1437 (pop-to-buffer "*eww bookmarks*"))
1439 (defun eww-bookmark-prepare ()
1440 (eww-read-bookmarks)
1441 (unless eww-bookmarks
1442 (user-error "No bookmarks are defined"))
1443 (set-buffer (get-buffer-create "*eww bookmarks*"))
1445 (let* ((width (/ (window-width) 2))
1446 (format (format "%%-%ds %%s" width))
1447 (inhibit-read-only t)
1450 (setq header-line-format (concat " " (format format "Title" "URL")))
1451 (dolist (bookmark eww-bookmarks)
1453 title (plist-get bookmark :title))
1454 (when (> (length title) width)
1455 (setq title (substring title 0 width)))
1456 (insert (format format title (plist-get bookmark :url)) "\n")
1457 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1458 (goto-char (point-min))))
1460 (defvar eww-bookmark-kill-ring nil)
1462 (defun eww-bookmark-kill ()
1463 "Kill the current bookmark."
1465 (let* ((start (line-beginning-position))
1466 (bookmark (get-text-property start 'eww-bookmark))
1467 (inhibit-read-only t))
1469 (user-error "No bookmark on the current line"))
1471 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1472 (delete-region start (point))
1473 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1474 (eww-write-bookmarks)))
1476 (defun eww-bookmark-yank ()
1477 "Yank a previously killed bookmark to the current line."
1479 (unless eww-bookmark-kill-ring
1480 (user-error "No previously killed bookmark"))
1482 (let ((inhibit-read-only t)
1485 (insert (pop eww-bookmark-kill-ring))
1486 (setq bookmark (get-text-property start 'eww-bookmark))
1487 (if (= start (point-min))
1488 (push bookmark eww-bookmarks)
1489 (let ((line (count-lines start (point))))
1490 (setcdr (nthcdr (1- line) eww-bookmarks)
1491 (cons bookmark (nthcdr line eww-bookmarks)))))
1492 (eww-write-bookmarks)))
1494 (defun eww-bookmark-browse ()
1495 "Browse the bookmark under point in eww."
1497 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1499 (user-error "No bookmark on the current line"))
1501 (eww-browse-url (plist-get bookmark :url))))
1503 (defun eww-next-bookmark ()
1504 "Go to the next bookmark in the list."
1508 (unless (get-buffer "*eww bookmarks*")
1510 (eww-bookmark-prepare))
1511 (with-current-buffer (get-buffer "*eww bookmarks*")
1512 (when (and (not first)
1515 (setq bookmark (get-text-property (line-beginning-position)
1518 (user-error "No next bookmark")))
1519 (eww-browse-url (plist-get bookmark :url))))
1521 (defun eww-previous-bookmark ()
1522 "Go to the previous bookmark in the list."
1526 (unless (get-buffer "*eww bookmarks*")
1528 (eww-bookmark-prepare))
1529 (with-current-buffer (get-buffer "*eww bookmarks*")
1531 (goto-char (point-max))
1532 (beginning-of-line))
1533 ;; On the final line.
1537 (user-error "No previous bookmark")
1539 (setq bookmark (get-text-property (line-beginning-position)
1541 (eww-browse-url (plist-get bookmark :url))))
1543 (defvar eww-bookmark-mode-map
1544 (let ((map (make-sparse-keymap)))
1545 (suppress-keymap map)
1546 (define-key map "q" 'quit-window)
1547 (define-key map [(control k)] 'eww-bookmark-kill)
1548 (define-key map [(control y)] 'eww-bookmark-yank)
1549 (define-key map "\r" 'eww-bookmark-browse)
1551 (easy-menu-define nil map
1552 "Menu for `eww-bookmark-mode-map'."
1554 ["Exit" quit-window t]
1555 ["Browse" eww-bookmark-browse
1556 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1557 ["Kill" eww-bookmark-kill
1558 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1559 ["Yank" eww-bookmark-yank
1560 :active eww-bookmark-kill-ring]))
1563 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1564 "Mode for listing bookmarks.
1566 \\{eww-bookmark-mode-map}"
1567 (buffer-disable-undo)
1568 (setq buffer-read-only t
1573 (defun eww-save-history ()
1574 (plist-put eww-data :point (point))
1575 (plist-put eww-data :text (buffer-string))
1576 (push eww-data eww-history)
1577 (setq eww-data (list :title ""))
1578 ;; Don't let the history grow infinitely. We store quite a lot of
1580 (when-let (tail (and eww-history-limit
1581 (nthcdr eww-history-limit eww-history)))
1584 (defvar eww-current-buffer)
1586 (defun eww-list-histories ()
1587 "List the eww-histories."
1589 (when (null eww-history)
1590 (error "No eww-histories are defined"))
1591 (let ((eww-history-trans eww-history)
1592 (buffer (current-buffer)))
1593 (set-buffer (get-buffer-create "*eww history*"))
1595 (setq-local eww-current-buffer buffer)
1596 (let ((inhibit-read-only t)
1599 url title format start)
1601 (dolist (history eww-history-trans)
1602 (setq start (point))
1603 (setq domain-length (max domain-length (length (plist-get history :url))))
1604 (setq title-length (max title-length (length (plist-get history :title)))))
1605 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1607 (concat " " (format format "Title" "URL")))
1608 (dolist (history eww-history-trans)
1609 (setq start (point))
1610 (setq url (plist-get history :url))
1611 (setq title (plist-get history :title))
1612 (insert (format format title url))
1614 (put-text-property start (1+ start) 'eww-history history))
1615 (goto-char (point-min)))
1616 (pop-to-buffer "*eww history*")))
1618 (defun eww-history-browse ()
1619 "Browse the history under point in eww."
1621 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1623 (error "No history on the current line"))
1624 (let ((buffer eww-current-buffer))
1627 (switch-to-buffer buffer)))
1628 (eww-restore-history history)))
1630 (defvar eww-history-mode-map
1631 (let ((map (make-sparse-keymap)))
1632 (suppress-keymap map)
1633 (define-key map "q" 'quit-window)
1634 (define-key map "\r" 'eww-history-browse)
1635 ;; (define-key map "n" 'next-error-no-select)
1636 ;; (define-key map "p" 'previous-error-no-select)
1638 (easy-menu-define nil map
1639 "Menu for `eww-history-mode-map'."
1641 ["Exit" quit-window t]
1642 ["Browse" eww-history-browse
1643 :active (get-text-property (line-beginning-position) 'eww-history)]))
1646 (define-derived-mode eww-history-mode nil "eww history"
1647 "Mode for listing eww-histories.
1649 \\{eww-history-mode-map}"
1650 (buffer-disable-undo)
1651 (setq buffer-read-only t
1656 (defvar eww-desktop-data-save
1657 '(:url :title :point)
1658 "List of `eww-data' properties to preserve in the desktop file.
1659 Also used when saving `eww-history'.")
1661 (defun eww-desktop-data-1 (alist)
1665 (let ((k (car tail))
1667 (when (memq k eww-desktop-data-save)
1668 (setq acc (cons k (cons v acc)))))
1669 (setq tail (cddr tail)))
1672 (defun eww-desktop-history-duplicate (a b)
1673 (let ((tail a) (r t))
1675 (if (or (memq (car tail) '(:point)) ; ignore :point
1677 (plist-get b (car tail))))
1678 (setq tail (cddr tail))
1684 (defun eww-desktop-misc-data (directory)
1685 "Return a property list with data used to restore eww buffers.
1686 This list will contain, as :history, the list, whose first element is
1687 the value of `eww-data', and the tail is `eww-history'.
1689 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1690 entries (if any) will be removed from the list.
1692 Only the properties listed in `eww-desktop-data-save' are included.
1693 Generally, the list should not include the (usually overly large)
1694 :dom, :source and :text properties."
1695 (let ((history (mapcar 'eww-desktop-data-1
1696 (cons eww-data eww-history))))
1697 (list :history (if eww-desktop-remove-duplicates
1698 (cl-remove-duplicates
1699 history :test 'eww-desktop-history-duplicate)
1702 (defun eww-restore-desktop (file-name buffer-name misc-data)
1703 "Restore an eww buffer from its desktop file record.
1704 If `eww-restore-desktop' is t or 'auto, this function will also
1705 initiate the retrieval of the respective URI in the background.
1706 Otherwise, the restored buffer will contain a prompt to do so by using
1708 (with-current-buffer (get-buffer-create buffer-name)
1710 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1711 (setq eww-history (cdr (plist-get misc-data :history))
1712 eww-data (or (car (plist-get misc-data :history))
1713 ;; backwards compatibility
1714 (list :url (plist-get misc-data :uri))))
1716 (when (plist-get eww-data :url)
1717 (case eww-restore-desktop
1718 ((t auto) (eww (plist-get eww-data :url)))
1719 ((zerop (buffer-size))
1720 (insert (substitute-command-keys
1721 eww-restore-reload-prompt))))))
1725 (add-to-list 'desktop-locals-to-save
1726 'eww-history-position)
1727 (add-to-list 'desktop-buffer-mode-handlers
1728 '(eww-mode . eww-restore-desktop))
1732 ;;; eww.el ends here