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
)
33 (eval-when-compile (require 'subr-x
)) ;; for string-trim
38 :link
'(custom-manual "(eww) Top")
42 (defcustom eww-header-line-format
"%t: %u"
44 - %t is replaced by the title.
45 - %u is replaced by the URL."
50 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
51 "Prefix URL to search engine"
56 (defcustom eww-download-directory
"~/Downloads/"
57 "Directory where files will downloaded."
62 (defcustom eww-bookmarks-directory user-emacs-directory
63 "Directory where bookmark files will be stored."
68 (defcustom eww-use-external-browser-for-content-type
69 "\\`\\(video/\\|audio/\\|application/ogg\\)"
70 "Always use external browser for specified content-type."
73 :type
'(choice (const :tag
"Never" nil
)
76 (defcustom eww-after-render-hook nil
77 "A hook called after eww has finished rendering the buffer."
82 (defcustom eww-form-checkbox-selected-symbol
"[X]"
83 "Symbol used to represent a selected checkbox.
84 See also `eww-form-checkbox-symbol'."
87 :type
'(choice (const "[X]")
88 (const "☒") ; Unicode BALLOT BOX WITH X
89 (const "☑") ; Unicode BALLOT BOX WITH CHECK
92 (defcustom eww-form-checkbox-symbol
"[ ]"
93 "Symbol used to represent a checkbox.
94 See also `eww-form-checkbox-selected-symbol'."
97 :type
'(choice (const "[ ]")
98 (const "☐") ; Unicode BALLOT BOX
101 (defface eww-form-submit
102 '((((type x w32 ns
) (class color
)) ; Like default mode line
103 :box
(:line-width
2 :style released-button
)
104 :background
"#808080" :foreground
"black"))
105 "Face for eww buffer buttons."
109 (defface eww-form-file
110 '((((type x w32 ns
) (class color
)) ; Like default mode line
111 :box
(:line-width
2 :style released-button
)
112 :background
"#808080" :foreground
"black"))
113 "Face for eww buffer buttons."
117 (defface eww-form-checkbox
118 '((((type x w32 ns
) (class color
)) ; Like default mode line
119 :box
(:line-width
2 :style released-button
)
120 :background
"lightgrey" :foreground
"black"))
121 "Face for eww buffer buttons."
125 (defface eww-form-select
126 '((((type x w32 ns
) (class color
)) ; Like default mode line
127 :box
(:line-width
2 :style released-button
)
128 :background
"lightgrey" :foreground
"black"))
129 "Face for eww buffer buttons."
133 (defface eww-form-text
134 '((t (:background
"#505050"
136 :box
(:line-width
1))))
137 "Face for eww text inputs."
141 (defface eww-form-textarea
142 '((t (:background
"#C0C0C0"
144 :box
(:line-width
1))))
145 "Face for eww textarea inputs."
149 (defvar eww-data nil
)
150 (defvar eww-history nil
)
151 (defvar eww-history-position
0)
153 (defvar eww-local-regex
"localhost"
154 "When this regex is found in the URL, it's not a keyword but an address.")
156 (defvar eww-link-keymap
157 (let ((map (copy-keymap shr-map
)))
158 (define-key map
"\r" 'eww-follow-link
)
163 "Fetch URL and render the page.
164 If the input doesn't look like an URL or a domain name, the
165 word(s) will be searched for via `eww-search-prefix'."
166 (interactive "sEnter URL or keywords: ")
167 (setq url
(string-trim url
))
168 (cond ((string-match-p "\\`file:/" url
))
169 ;; Don't mangle file: URLs at all.
170 ((string-match-p "\\`ftp://" url
)
171 (user-error "FTP is not supported."))
173 (if (and (= (length (split-string url
)) 1)
174 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
175 (> (length (split-string url
"[.:]")) 1))
176 (string-match eww-local-regex url
)))
178 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
179 (setq url
(concat "http://" url
)))
180 ;; some site don't redirect final /
181 (when (string= (url-filename (url-generic-parse-url url
)) "")
182 (setq url
(concat url
"/"))))
183 (setq url
(concat eww-search-prefix
184 (replace-regexp-in-string " " "+" url
))))))
185 (url-retrieve url
'eww-render
187 (and (eq major-mode
'eww-mode
)
190 ;;;###autoload (defalias 'browse-web 'eww)
193 (defun eww-open-file (file)
194 "Render a file using EWW."
195 (interactive "fFile: ")
196 (eww (concat "file://"
197 (and (memq system-type
'(windows-nt ms-dos
))
199 (expand-file-name file
))))
202 (defun eww-search-words (&optional beg end
)
203 "Search the web for the text between the point and marker.
204 See the `eww-search-prefix' variable for the search engine used."
206 (eww (buffer-substring beg end
)))
208 (defun eww-render (status url
&optional point buffer
)
209 (let ((redirect (plist-get status
:redirect
)))
211 (setq url redirect
)))
212 (let* ((headers (eww-parse-headers))
214 (mail-header-parse-content-type
215 (or (cdr (assoc "content-type" headers
))
219 (or (cdr (assq 'charset
(cdr content-type
)))
220 (eww-detect-charset (equal (car content-type
)
223 (data-buffer (current-buffer)))
226 (plist-put eww-data
:title
"")
228 ((and eww-use-external-browser-for-content-type
229 (string-match-p eww-use-external-browser-for-content-type
231 (eww-browse-with-external-browser url
))
232 ((equal (car content-type
) "text/html")
233 (eww-display-html charset url nil point buffer
))
234 ((equal (car content-type
) "application/pdf")
236 ((string-match-p "\\`image/" (car content-type
))
237 (eww-display-image buffer
)
238 (eww-update-header-line-format))
240 (eww-display-raw buffer
)
241 (eww-update-header-line-format)))
242 (plist-put eww-data
:url url
)
243 (setq eww-history-position
0)
244 (run-hooks 'eww-after-render-hook
))
245 (kill-buffer data-buffer
))))
247 (defun eww-parse-headers ()
249 (goto-char (point-min))
250 (while (and (not (eobp))
252 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
253 (push (cons (downcase (match-string 1))
261 (defun eww-detect-charset (html-p)
262 (let ((case-fold-search t
)
266 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
270 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
273 (declare-function libxml-parse-html-region
"xml.c"
274 (start end
&optional base-url
))
276 (defun eww-display-html (charset url
&optional document point buffer
)
277 (or (fboundp 'libxml-parse-html-region
)
278 (error "This function requires Emacs to be compiled with libxml2"))
279 ;; There should be a better way to abort loading images
285 'base
(list (cons 'href url
))
287 (unless (eq charset
'utf-8
)
289 (decode-coding-region (point) (point-max) charset
)
290 (coding-system-error nil
)))
291 (libxml-parse-html-region (point) (point-max))))))
292 (source (and (null document
)
293 (buffer-substring (point) (point-max)))))
294 (eww-setup-buffer buffer
)
295 (plist-put eww-data
:source source
)
296 (plist-put eww-data
:dom document
)
297 (let ((inhibit-read-only t
)
298 (after-change-functions nil
)
299 (shr-target-id (url-target (url-generic-parse-url url
)))
300 (shr-external-rendering-functions
301 '((title . eww-tag-title
)
302 (form . eww-tag-form
)
303 (input . eww-tag-input
)
304 (textarea . eww-tag-textarea
)
305 (body . eww-tag-body
)
306 (select . eww-tag-select
)
307 (link . eww-tag-link
)
309 (shr-insert-document document
)
314 (goto-char (point-min))
315 (let ((point (next-single-property-change
316 (point-min) 'shr-target-id
)))
320 (goto-char (point-min))
321 ;; Don't leave point inside forms, because the normal eww
322 ;; commands aren't available there.
323 (while (and (not (eobp))
324 (get-text-property (point) 'eww-form
))
326 (plist-put eww-data
:url url
)
327 (setq eww-history-position
0)
328 (eww-update-header-line-format)))
330 (defun eww-handle-link (cont)
331 (let* ((rel (assq :rel cont
))
332 (href (assq :href cont
))
334 ;; The text associated with :rel is case-insensitive.
335 (if rel
(downcase (cdr rel
)))
337 ;; Texinfo uses "previous", but HTML specifies
338 ;; "prev", so recognize both.
339 ("previous" .
:previous
)
341 ;; HTML specifies "start" but also "contents",
342 ;; and Gtk seems to use "home". Recognize
343 ;; them all; but store them in different
344 ;; variables so that we can readily choose the
348 ("contents" .
:contents
)
352 (plist-put eww-data
(cdr where
) (cdr href
)))))
354 (defun eww-tag-link (cont)
355 (eww-handle-link cont
)
358 (defun eww-tag-a (cont)
359 (eww-handle-link cont
)
360 (let ((start (point)))
362 (put-text-property start
(point) 'keymap eww-link-keymap
)))
364 (defun eww-update-header-line-format ()
365 (if eww-header-line-format
366 (setq header-line-format
367 (replace-regexp-in-string
369 ;; FIXME? Title can be blank. Default to, eg, last component
371 (format-spec eww-header-line-format
372 `((?u .
,(plist-get eww-data
:url
))
373 (?t .
,(or (plist-get eww-data
:title
) ""))))))
374 (setq header-line-format nil
)))
376 (defun eww-tag-title (cont)
379 (when (eq (car sub
) 'text
)
380 (setq title
(concat title
(cdr sub
)))))
381 (plist-put eww-data
:title
382 (replace-regexp-in-string
384 (replace-regexp-in-string "[ \t\r\n]+" " " title
))))
385 (eww-update-header-line-format))
387 (defun eww-tag-body (cont)
388 (let* ((start (point))
389 (fgcolor (cdr (or (assq :fgcolor cont
)
391 (bgcolor (cdr (assq :bgcolor cont
)))
392 (shr-stylesheet (list (cons 'color fgcolor
)
393 (cons 'background-color bgcolor
))))
395 (shr-colorize-region start
(point) fgcolor bgcolor
)))
397 (defun eww-display-raw (&optional buffer
)
398 (let ((data (buffer-substring (point) (point-max))))
399 (eww-setup-buffer buffer
)
400 (let ((inhibit-read-only t
))
402 (goto-char (point-min))))
404 (defun eww-display-image (&optional buffer
)
405 (let ((data (shr-parse-image-data)))
406 (eww-setup-buffer buffer
)
407 (let ((inhibit-read-only t
))
408 (shr-put-image data nil
))
409 (goto-char (point-min))))
411 (defun eww-display-pdf ()
412 (let ((data (buffer-substring (point) (point-max))))
413 (switch-to-buffer (get-buffer-create "*eww pdf*"))
414 (let ((coding-system-for-write 'raw-text
)
415 (inhibit-read-only t
))
419 (goto-char (point-min)))
421 (defun eww-setup-buffer (&optional buffer
)
423 (if (buffer-live-p buffer
)
425 (get-buffer-create "*eww*")))
426 (let ((inhibit-read-only t
))
429 (unless (eq major-mode
'eww-mode
)
432 (defun eww-view-source ()
433 "View the HTML source code of the current page."
435 (let ((buf (get-buffer-create "*eww-source*"))
436 (source (plist-get eww-data
:source
)))
437 (with-current-buffer buf
438 (let ((inhibit-read-only t
))
439 (delete-region (point-min) (point-max))
440 (insert (or source
"no source"))
441 (goto-char (point-min))
442 (when (fboundp 'html-mode
)
446 (defun eww-readable ()
447 "View the main \"readable\" parts of the current web page.
448 This command uses heuristics to find the parts of the web page that
449 contains the main textual portion, leaving out navigation menus and
452 (let* ((old-data eww-data
)
453 (dom (shr-transform-dom
455 (insert (plist-get old-data
:source
))
457 (decode-coding-region (point-min) (point-max) 'utf-8
)
458 (coding-system-error nil
))
459 (libxml-parse-html-region (point-min) (point-max))))))
460 (eww-score-readability dom
)
462 (eww-display-html nil nil
464 (eww-highest-readability dom
))
465 nil
(current-buffer))
466 (dolist (elem '(:source
:url
:title
:next
:previous
:up
))
467 (plist-put eww-data elem
(plist-get old-data elem
)))
468 (eww-update-header-line-format)))
470 (defun eww-score-readability (node)
473 ((memq (car node
) '(script head comment
))
475 ((eq (car node
) 'meta
)
477 ((eq (car node
) 'img
)
480 (setq score
(- (length (split-string
481 (or (cdr (assoc 'text
(cdr node
))) ""))))))
483 (dolist (elem (cdr node
))
485 ((and (stringp (cdr elem
))
486 (eq (car elem
) 'text
))
487 (setq score
(+ score
(length (split-string (cdr elem
))))))
490 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
491 (eww-score-readability elem
)))))))))
492 ;; Cache the score of the node to avoid recomputing all the time.
493 (setcdr node
(cons (cons :eww-readability-score score
) (cdr node
)))
496 (defun eww-highest-readability (node)
499 (dolist (elem (cdr node
))
500 (when (and (consp (cdr elem
))
502 :eww-readability-score
504 (eww-highest-readability elem
))))
505 most-negative-fixnum
)
506 (or (cdr (assoc :eww-readability-score
(cdr result
)))
507 most-negative-fixnum
)))
508 (setq result highest
)))
512 (let ((map (make-sparse-keymap)))
513 (suppress-keymap map
)
514 (define-key map
"q" 'quit-window
)
515 (define-key map
"g" 'eww-reload
)
516 (define-key map
[?
\t] 'shr-next-link
)
517 (define-key map
[?\M-
\t] 'shr-previous-link
)
518 (define-key map
[delete] 'scroll-down-command)
519 (define-key map [?\S-\ ] 'scroll-down-command)
520 (define-key map "\177" 'scroll-down-command)
521 (define-key map " " 'scroll-up-command)
522 (define-key map "l" 'eww-back-url)
523 (define-key map "r" 'eww-forward-url)
524 (define-key map "n" 'eww-next-url)
525 (define-key map "p" 'eww-previous-url)
526 (define-key map "u" 'eww-up-url)
527 (define-key map "t" 'eww-top-url)
528 (define-key map "&" 'eww-browse-with-external-browser)
529 (define-key map "d" 'eww-download)
530 (define-key map "w" 'eww-copy-page-url)
531 (define-key map "C" 'url-cookie-list)
532 (define-key map "v" 'eww-view-source)
533 (define-key map "R" 'eww-readable)
534 (define-key map "H" 'eww-list-histories)
536 (define-key map "b" 'eww-add-bookmark)
537 (define-key map "B" 'eww-list-bookmarks)
538 (define-key map [(meta n)] 'eww-next-bookmark)
539 (define-key map [(meta p)] 'eww-previous-bookmark)
541 (easy-menu-define nil map ""
543 ["Exit" quit-window t]
544 ["Close browser" quit-window t]
545 ["Reload" eww-reload t]
546 ["Back to previous page" eww-back-url
547 :active (not (zerop (length eww-history)))]
548 ["Forward to next page" eww-forward-url
549 :active (not (zerop eww-history-position))]
550 ["Browse with external browser" eww-browse-with-external-browser t]
551 ["Download" eww-download t]
552 ["View page source" eww-view-source]
553 ["Copy page URL" eww-copy-page-url t]
554 ["List histories" eww-list-histories t]
555 ["Add bookmark" eww-add-bookmark t]
556 ["List bookmarks" eww-list-bookmarks t]
557 ["List cookies" url-cookie-list t]))
560 (defvar eww-tool-bar-map
561 (let ((map (make-sparse-keymap)))
562 (dolist (tool-bar-item
563 '((quit-window . "close")
564 (eww-reload . "refresh")
565 (eww-back-url . "left-arrow")
566 (eww-forward-url . "right-arrow")
567 (eww-view-source . "show")
568 (eww-copy-page-url . "copy")
569 (eww-add-bookmark . "bookmark_add"))) ;; ...
570 (tool-bar-local-item-from-menu
571 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
573 "Tool bar for `eww-mode'.")
575 (define-derived-mode eww-mode nil "eww"
576 "Mode for browsing the web.
579 (setq-local eww-data (list :title ""))
580 (setq-local browse-url-browser-function 'eww-browse-url)
581 (setq-local after-change-functions 'eww-process-text-input)
582 (setq-local eww-history nil)
583 (setq-local eww-history-position 0)
584 (when (boundp 'tool-bar-map)
585 (setq-local tool-bar-map eww-tool-bar-map))
586 (buffer-disable-undo)
587 ;;(setq buffer-read-only t)
591 (defun eww-browse-url (url &optional _new-window)
592 (when (and (equal major-mode 'eww-mode)
593 (plist-get eww-data :url))
597 (defun eww-back-url ()
598 "Go to the previously displayed page."
600 (when (>= eww-history-position (length eww-history))
601 (user-error "No previous page"))
603 (setq eww-history-position (+ eww-history-position 2))
604 (eww-restore-history (elt eww-history (1- eww-history-position))))
606 (defun eww-forward-url ()
607 "Go to the next displayed page."
609 (when (zerop eww-history-position)
610 (user-error "No next page"))
612 (eww-restore-history (elt eww-history (1- eww-history-position))))
614 (defun eww-restore-history (elem)
615 (let ((inhibit-read-only t))
617 (insert (plist-get elem :text))
618 (goto-char (plist-get elem :point))
620 (eww-update-header-line-format)))
622 (defun eww-next-url ()
623 "Go to the page marked `next'.
624 A page is marked `next' if rel=\"next\" appears in a <link>
627 (if (plist-get eww-data :next)
628 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
629 (plist-get eww-data :url)))
630 (user-error "No `next' on this page")))
632 (defun eww-previous-url ()
633 "Go to the page marked `previous'.
634 A page is marked `previous' if rel=\"previous\" appears in a <link>
637 (if (plist-get eww-data :previous)
638 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
639 (plist-get eww-data :url)))
640 (user-error "No `previous' on this page")))
643 "Go to the page marked `up'.
644 A page is marked `up' if rel=\"up\" appears in a <link>
647 (if (plist-get eww-data :up)
648 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
649 (plist-get eww-data :url)))
650 (user-error "No `up' on this page")))
652 (defun eww-top-url ()
653 "Go to the page marked `top'.
654 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
655 appears in a <link> or <a> tag."
657 (let ((best-url (or (plist-get eww-data :start)
658 (plist-get eww-data :contents)
659 (plist-get eww-data :home))))
661 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
662 (user-error "No `top' for this page"))))
665 "Reload the current page."
667 (let ((url (plist-get eww-data :url)))
668 (url-retrieve url 'eww-render (list url (point)))))
672 (defvar eww-form nil)
674 (defvar eww-submit-map
675 (let ((map (make-sparse-keymap)))
676 (define-key map "\r" 'eww-submit)
677 (define-key map [(control c) (control c)] 'eww-submit)
680 (defvar eww-submit-file
681 (let ((map (make-sparse-keymap)))
682 (define-key map "\r" 'eww-select-file)
683 (define-key map [(control c) (control c)] 'eww-submit)
686 (defvar eww-checkbox-map
687 (let ((map (make-sparse-keymap)))
688 (define-key map " " 'eww-toggle-checkbox)
689 (define-key map "\r" 'eww-toggle-checkbox)
690 (define-key map [(control c) (control c)] 'eww-submit)
694 (let ((map (make-keymap)))
695 (set-keymap-parent map text-mode-map)
696 (define-key map "\r" 'eww-submit)
697 (define-key map [(control a)] 'eww-beginning-of-text)
698 (define-key map [(control c) (control c)] 'eww-submit)
699 (define-key map [(control e)] 'eww-end-of-text)
700 (define-key map [?\t] 'shr-next-link)
701 (define-key map [?\M-\t] 'shr-previous-link)
704 (defvar eww-textarea-map
705 (let ((map (make-keymap)))
706 (set-keymap-parent map text-mode-map)
707 (define-key map "\r" 'forward-line)
708 (define-key map [(control c) (control c)] 'eww-submit)
709 (define-key map [?\t] 'shr-next-link)
710 (define-key map [?\M-\t] 'shr-previous-link)
713 (defvar eww-select-map
714 (let ((map (make-sparse-keymap)))
715 (define-key map "\r" 'eww-change-select)
716 (define-key map [(control c) (control c)] 'eww-submit)
719 (defun eww-beginning-of-text ()
720 "Move to the start of the input field."
722 (goto-char (eww-beginning-of-field)))
724 (defun eww-end-of-text ()
725 "Move to the end of the text in the input field."
727 (goto-char (eww-end-of-field))
728 (let ((start (eww-beginning-of-field)))
729 (while (and (equal (following-char) ? )
732 (when (> (point) start)
735 (defun eww-beginning-of-field ()
739 ((not (eq (get-text-property (point) 'eww-form)
740 (get-text-property (1- (point)) 'eww-form)))
743 (previous-single-property-change
744 (point) 'eww-form nil (point-min)))))
746 (defun eww-end-of-field ()
747 (1- (next-single-property-change
748 (point) 'eww-form nil (point-max))))
750 (defun eww-tag-form (cont)
752 (list (assq :method cont)
753 (assq :action cont)))
755 (shr-ensure-paragraph)
760 (when (> (point) start)
761 (put-text-property start (1+ start)
762 'eww-form eww-form))))
764 (defun eww-form-submit (cont)
765 (let ((start (point))
766 (value (cdr (assq :value cont))))
768 (if (zerop (length value))
772 (add-face-text-property start (point) 'eww-form-submit)
773 (put-text-property start (point) 'eww-form
774 (list :eww-form eww-form
777 :name (cdr (assq :name cont))))
778 (put-text-property start (point) 'keymap eww-submit-map)
781 (defun eww-form-checkbox (cont)
782 (let ((start (point)))
783 (if (cdr (assq :checked cont))
784 (insert eww-form-checkbox-selected-symbol)
785 (insert eww-form-checkbox-symbol))
786 (add-face-text-property start (point) 'eww-form-checkbox)
787 (put-text-property start (point) 'eww-form
788 (list :eww-form eww-form
789 :value (cdr (assq :value cont))
790 :type (downcase (cdr (assq :type cont)))
791 :checked (cdr (assq :checked cont))
792 :name (cdr (assq :name cont))))
793 (put-text-property start (point) 'keymap eww-checkbox-map)
796 (defun eww-form-file (cont)
797 (let ((start (point))
798 (value (cdr (assq :value cont))))
800 (if (zerop (length value))
804 (add-face-text-property start (point) 'eww-form-file)
806 (put-text-property start (point) 'eww-form
807 (list :eww-form eww-form
808 :value (cdr (assq :value cont))
809 :type (downcase (cdr (assq :type cont)))
810 :name (cdr (assq :name cont))))
811 (put-text-property start (point) 'keymap eww-submit-file)
814 (defun eww-select-file ()
815 "Change the value of the upload file menu under point."
817 (let* ((input (get-text-property (point) 'eww-form)))
819 (let ((insert-default-directory t))
820 (read-file-name "filename: "))))
821 (eww-update-field filename (length "Browse"))
822 (plist-put input :filename filename))))
824 (defun eww-form-text (cont)
825 (let ((start (point))
826 (type (downcase (or (cdr (assq :type cont))
828 (value (or (cdr (assq :value cont)) ""))
829 (width (string-to-number
830 (or (cdr (assq :size cont))
832 (readonly-property (if (or (cdr (assq :disabled cont))
833 (cdr (assq :readonly cont)))
835 'inhibit-read-only)))
837 (when (< (length value) width)
838 (insert (make-string (- width (length value)) ? )))
839 (put-text-property start (point) 'face 'eww-form-text)
840 (put-text-property start (point) 'local-map eww-text-map)
841 (put-text-property start (point) readonly-property t)
842 (put-text-property start (point) 'eww-form
843 (list :eww-form eww-form
846 :name (cdr (assq :name cont))))
849 (defconst eww-text-input-types '("text" "password" "textarea"
850 "color" "date" "datetime" "datetime-local"
851 "email" "month" "number" "search" "tel"
853 "List of input types which represent a text input.
854 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
856 (defun eww-process-text-input (beg end length)
857 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
858 (properties (text-properties-at end))
859 (type (plist-get form :type)))
861 (member type eww-text-input-types))
864 ;; Delete some space at the end.
867 (if (equal type "textarea")
868 (1- (line-end-position))
870 (let ((new (- end beg)))
871 (while (and (> new 0)
872 (eql (following-char) ? ))
873 (delete-region (point) (1+ (point)))
874 (setq new (1- new))))
875 (set-text-properties beg end properties)))
880 (if (equal type "textarea")
881 (1- (line-end-position))
883 (let ((start (point)))
884 (insert (make-string length ? ))
885 (set-text-properties start (point) properties)))))
886 (let ((value (buffer-substring-no-properties
887 (eww-beginning-of-field)
888 (eww-end-of-field))))
889 (when (string-match " +\\'" value)
890 (setq value (substring value 0 (match-beginning 0))))
891 (plist-put form :value value)
892 (when (equal type "password")
893 ;; Display passwords as asterisks.
894 (let ((start (eww-beginning-of-field)))
895 (put-text-property start (+ start (length value))
896 'display (make-string (length value) ?*))))))))
898 (defun eww-tag-textarea (cont)
899 (let ((start (point))
900 (value (or (cdr (assq :value cont)) ""))
901 (lines (string-to-number
902 (or (cdr (assq :rows cont))
904 (width (string-to-number
905 (or (cdr (assq :cols cont))
911 (when (< (count-lines start (point)) lines)
912 (dotimes (i (- lines (count-lines start (point))))
914 (setq end (point-marker))
916 (while (< (point) end)
918 (let ((pad (- width (- (point) (line-beginning-position)))))
920 (insert (make-string pad ? ))))
921 (add-face-text-property (line-beginning-position)
922 (point) 'eww-form-textarea)
923 (put-text-property (line-beginning-position) (point)
924 'local-map eww-textarea-map)
926 (put-text-property start (point) 'eww-form
927 (list :eww-form eww-form
930 :name (cdr (assq :name cont))))))
932 (defun eww-tag-input (cont)
933 (let ((type (downcase (or (cdr (assq :type cont))
937 ((or (equal type "checkbox")
938 (equal type "radio"))
939 (eww-form-checkbox cont))
941 (eww-form-file cont))
942 ((equal type "submit")
943 (eww-form-submit cont))
944 ((equal type "hidden")
945 (let ((form eww-form)
946 (name (cdr (assq :name cont))))
947 ;; Don't add <input type=hidden> elements repeatedly.
949 (or (not (consp (car form)))
950 (not (eq (caar form) 'hidden))
951 (not (equal (plist-get (cdr (car form)) :name)
953 (setq form (cdr form)))
955 (nconc eww-form (list
958 :value (cdr (assq :value cont))))))))
960 (eww-form-text cont)))
961 (unless (= start (point))
962 (put-text-property start (1+ start) 'help-echo "Input field"))))
964 (defun eww-tag-select (cont)
965 (shr-ensure-paragraph)
966 (let ((menu (list :name (cdr (assq :name cont))
972 (if (eq (car (car cont)) 'optgroup)
973 (dolist (groupelem cont)
974 (unless (cdr (assq :disabled (cdr groupelem)))
975 (setq opelem (append opelem (cdr (cdr groupelem))))))
977 (dolist (elem opelem)
978 (when (eq (car elem) 'option)
979 (when (cdr (assq :selected (cdr elem)))
980 (nconc menu (list :value
981 (cdr (assq :value (cdr elem))))))
982 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
983 (setq max (max max (length display)))
985 :value (cdr (assq :value (cdr elem)))
989 (setq options (nreverse options))
990 ;; If we have no selected values, default to the first value.
991 (unless (plist-get menu :value)
992 (nconc menu (list :value (nth 2 (car options)))))
994 (let ((selected (eww-select-display menu)))
996 (make-string (- max (length selected)) ? )))
997 (put-text-property start (point) 'eww-form menu)
998 (add-face-text-property start (point) 'eww-form-select)
999 (put-text-property start (point) 'keymap eww-select-map)
1000 (unless (= start (point))
1001 (put-text-property start (1+ start) 'help-echo "select field"))
1002 (shr-ensure-paragraph))))
1004 (defun eww-select-display (select)
1005 (let ((value (plist-get select :value))
1007 (dolist (elem select)
1008 (when (and (consp elem)
1009 (eq (car elem) 'item)
1010 (equal value (plist-get (cdr elem) :value)))
1011 (setq display (plist-get (cdr elem) :display))))
1014 (defun eww-change-select ()
1015 "Change the value of the select drop-down menu under point."
1017 (let* ((input (get-text-property (point) 'eww-form))
1018 (completion-ignore-case t)
1021 (mapcar (lambda (elem)
1023 (eq (car elem) 'item)
1024 (cons (plist-get (cdr elem) :display)
1025 (plist-get (cdr elem) :value))))
1028 (completing-read "Change value: " options nil 'require-match))
1029 (inhibit-read-only t))
1030 (plist-put input :value (cdr (assoc-string display options t)))
1032 (eww-update-field display))))
1034 (defun eww-update-field (string &optional offset)
1035 (if (not offset) (setq offset 0))
1036 (let ((properties (text-properties-at (point)))
1037 (start (+ (eww-beginning-of-field) offset))
1038 (current-end (1+ (eww-end-of-field)))
1039 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1040 (delete-region start current-end)
1041 (forward-char offset)
1043 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1044 (if (= 0 offset) (set-text-properties start new-end properties))
1047 (defun eww-toggle-checkbox ()
1048 "Toggle the value of the checkbox under point."
1050 (let* ((input (get-text-property (point) 'eww-form))
1051 (type (plist-get input :type)))
1052 (if (equal type "checkbox")
1055 (if (plist-get input :checked)
1057 (plist-put input :checked nil)
1058 (eww-update-field eww-form-checkbox-symbol))
1059 (plist-put input :checked t)
1060 (eww-update-field eww-form-checkbox-selected-symbol))))
1061 ;; Radio button. Switch all other buttons off.
1062 (let ((name (plist-get input :name)))
1064 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1065 (when (equal (plist-get (cdr elem) :name) name)
1066 (goto-char (car elem))
1067 (if (not (eq (cdr elem) input))
1069 (plist-put input :checked nil)
1070 (eww-update-field eww-form-checkbox-symbol))
1071 (plist-put input :checked t)
1072 (eww-update-field eww-form-checkbox-selected-symbol)))))
1073 (forward-char 1)))))
1075 (defun eww-inputs (form)
1076 (let ((start (point-min))
1079 (< start (point-max)))
1080 (when (or (get-text-property start 'eww-form)
1081 (setq start (next-single-property-change start 'eww-form)))
1082 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1084 (push (cons start (get-text-property start 'eww-form))
1086 (setq start (next-single-property-change start 'eww-form))))
1089 (defun eww-input-value (input)
1090 (let ((type (plist-get input :type))
1091 (value (plist-get input :value)))
1093 ((equal type "textarea")
1096 (goto-char (point-min))
1097 (while (re-search-forward "^ +\n\\| +$" nil t)
1098 (replace-match "" t t))
1101 (if (string-match " +\\'" value)
1102 (substring value 0 (match-beginning 0))
1105 (defun eww-submit ()
1106 "Submit the current form."
1108 (let* ((this-input (get-text-property (point) 'eww-form))
1109 (form (plist-get this-input :eww-form))
1111 (dolist (elem (sort (eww-inputs form)
1113 (< (car o1) (car o2)))))
1114 (let* ((input (cdr elem))
1115 (input-start (car elem))
1116 (name (plist-get input :name)))
1119 ((member (plist-get input :type) '("checkbox" "radio"))
1120 (when (plist-get input :checked)
1121 (push (cons name (plist-get input :value))
1123 ((equal (plist-get input :type) "file")
1125 (list (cons "filedata"
1127 (insert-file-contents
1128 (plist-get input :filename))
1130 (cons "name" (plist-get input :name))
1131 (cons "filename" (plist-get input :filename))))
1133 ((equal (plist-get input :type) "submit")
1134 ;; We want the values from buttons if we hit a button if
1135 ;; we hit enter on it, or if it's the first button after
1136 ;; the field we did hit return on.
1137 (when (or (eq input this-input)
1138 (and (not (eq input this-input))
1140 (> input-start (point))))
1141 (setq next-submit t)
1142 (push (cons name (plist-get input :value))
1145 (push (cons name (eww-input-value input))
1148 (when (and (consp elem)
1149 (eq (car elem) 'hidden))
1150 (push (cons (plist-get (cdr elem) :name)
1151 (or (plist-get (cdr elem) :value) ""))
1153 (if (and (stringp (cdr (assq :method form)))
1154 (equal (downcase (cdr (assq :method form))) "post"))
1156 (dolist (x values mtype)
1157 (if (equal (car x) "file")
1159 (setq mtype "multipart/form-data"))))
1160 (cond ((equal mtype "multipart/form-data")
1161 (let ((boundary (mml-compute-boundary '())))
1162 (let ((url-request-method "POST")
1163 (url-request-extra-headers
1164 (list (cons "Content-Type"
1165 (concat "multipart/form-data; boundary="
1168 (mm-url-encode-multipart-form-data values boundary)))
1169 (eww-browse-url (shr-expand-url
1170 (cdr (assq :action form))
1171 (plist-get eww-data :url))))))
1173 (let ((url-request-method "POST")
1174 (url-request-extra-headers
1176 "application/x-www-form-urlencoded")))
1178 (mm-url-encode-www-form-urlencoded values)))
1179 (eww-browse-url (shr-expand-url
1180 (cdr (assq :action form))
1181 (plist-get eww-data :url)))))))
1184 (if (cdr (assq :action form))
1185 (shr-expand-url (cdr (assq :action form))
1186 (plist-get eww-data :url))
1187 (plist-get eww-data :url))
1189 (mm-url-encode-www-form-urlencoded values))))))
1191 (defun eww-browse-with-external-browser (&optional url)
1192 "Browse the current URL with an external browser.
1193 The browser to used is specified by the `shr-external-browser' variable."
1195 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1197 (defun eww-follow-link (&optional external mouse-event)
1198 "Browse the URL under point.
1199 If EXTERNAL, browse the URL using `shr-external-browser'."
1200 (interactive (list current-prefix-arg last-nonmenu-event))
1201 (mouse-set-point mouse-event)
1202 (let ((url (get-text-property (point) 'shr-url)))
1205 (message "No link under point"))
1206 ((string-match "^mailto:" url)
1207 (browse-url-mail url))
1209 (funcall shr-external-browser url))
1210 ;; This is a #target url in the same page as the current one.
1211 ((and (url-target (url-generic-parse-url url))
1212 (eww-same-page-p url (plist-get eww-data :url)))
1214 (eww-display-html 'utf-8 url (plist-get eww-data :url)
1215 nil (current-buffer)))
1217 (eww-browse-url url)))))
1219 (defun eww-same-page-p (url1 url2)
1220 "Return non-nil if both URLs represent the same page.
1221 Differences in #targets are ignored."
1222 (let ((obj1 (url-generic-parse-url url1))
1223 (obj2 (url-generic-parse-url url2)))
1224 (setf (url-target obj1) nil)
1225 (setf (url-target obj2) nil)
1226 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1228 (defun eww-copy-page-url ()
1230 (message "%s" (plist-get eww-data :url))
1231 (kill-new (plist-get eww-data :url)))
1233 (defun eww-download ()
1234 "Download URL under point to `eww-download-directory'."
1236 (let ((url (get-text-property (point) 'shr-url)))
1238 (message "No URL under point")
1239 (url-retrieve url 'eww-download-callback (list url)))))
1241 (defun eww-download-callback (status url)
1242 (unless (plist-get status :error)
1243 (let* ((obj (url-generic-parse-url url))
1244 (path (car (url-path-and-query obj)))
1245 (file (eww-make-unique-file-name (file-name-nondirectory path)
1246 eww-download-directory)))
1247 (goto-char (point-min))
1248 (re-search-forward "\r?\n\r?\n")
1249 (write-region (point) (point-max) file)
1250 (message "Saved %s" file))))
1252 (defun eww-make-unique-file-name (file directory)
1254 ((zerop (length file))
1256 ((string-match "\\`[.]" file)
1257 (setq file (concat "!" file))))
1259 (while (file-exists-p (expand-file-name file directory))
1261 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1262 (format "%s(%d)%s" (match-string 1 file)
1263 count (match-string 2 file))
1264 (format "%s(%d)" file count)))
1265 (setq count (1+ count)))
1266 (expand-file-name file directory)))
1270 (defvar eww-bookmarks nil)
1272 (defun eww-add-bookmark ()
1273 "Add the current page to the bookmarks."
1275 (eww-read-bookmarks)
1276 (dolist (bookmark eww-bookmarks)
1277 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1278 (user-error "Already bookmarked")))
1279 (if (y-or-n-p "bookmark this page? ")
1281 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1282 (plist-get eww-data :url))))
1283 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1284 (push (list :url (plist-get eww-data :url)
1286 :time (current-time-string))
1288 (eww-write-bookmarks)
1289 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1290 (plist-get eww-data :title)))))
1292 (defun eww-write-bookmarks ()
1293 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1294 (insert ";; Auto-generated file; don't edit\n")
1295 (pp eww-bookmarks (current-buffer))))
1297 (defun eww-read-bookmarks ()
1298 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1300 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1302 (insert-file-contents file)
1303 (read (current-buffer)))))))
1306 (defun eww-list-bookmarks ()
1307 "Display the bookmarks."
1309 (eww-bookmark-prepare)
1310 (pop-to-buffer "*eww bookmarks*"))
1312 (defun eww-bookmark-prepare ()
1313 (eww-read-bookmarks)
1314 (unless eww-bookmarks
1315 (user-error "No bookmarks are defined"))
1316 (set-buffer (get-buffer-create "*eww bookmarks*"))
1318 (let ((format "%-40s %s")
1319 (inhibit-read-only t)
1322 (setq header-line-format (concat " " (format format "URL" "Title")))
1323 (dolist (bookmark eww-bookmarks)
1324 (setq start (point))
1325 (setq url (plist-get bookmark :url))
1326 (when (> (length url) 40)
1327 (setq url (substring url 0 40)))
1328 (insert (format format url
1329 (plist-get bookmark :title))
1331 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1332 (goto-char (point-min))))
1334 (defvar eww-bookmark-kill-ring nil)
1336 (defun eww-bookmark-kill ()
1337 "Kill the current bookmark."
1339 (let* ((start (line-beginning-position))
1340 (bookmark (get-text-property start 'eww-bookmark))
1341 (inhibit-read-only t))
1343 (user-error "No bookmark on the current line"))
1345 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1346 (delete-region start (point))
1347 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1348 (eww-write-bookmarks)))
1350 (defun eww-bookmark-yank ()
1351 "Yank a previously killed bookmark to the current line."
1353 (unless eww-bookmark-kill-ring
1354 (user-error "No previously killed bookmark"))
1356 (let ((inhibit-read-only t)
1359 (insert (pop eww-bookmark-kill-ring))
1360 (setq bookmark (get-text-property start 'eww-bookmark))
1361 (if (= start (point-min))
1362 (push bookmark eww-bookmarks)
1363 (let ((line (count-lines start (point))))
1364 (setcdr (nthcdr (1- line) eww-bookmarks)
1365 (cons bookmark (nthcdr line eww-bookmarks)))))
1366 (eww-write-bookmarks)))
1368 (defun eww-bookmark-browse ()
1369 "Browse the bookmark under point in eww."
1371 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1373 (user-error "No bookmark on the current line"))
1375 (eww-browse-url (plist-get bookmark :url))))
1377 (defun eww-next-bookmark ()
1378 "Go to the next bookmark in the list."
1382 (unless (get-buffer "*eww bookmarks*")
1384 (eww-bookmark-prepare))
1385 (with-current-buffer (get-buffer "*eww bookmarks*")
1386 (when (and (not first)
1389 (setq bookmark (get-text-property (line-beginning-position)
1392 (user-error "No next bookmark")))
1393 (eww-browse-url (plist-get bookmark :url))))
1395 (defun eww-previous-bookmark ()
1396 "Go to the previous bookmark in the list."
1400 (unless (get-buffer "*eww bookmarks*")
1402 (eww-bookmark-prepare))
1403 (with-current-buffer (get-buffer "*eww bookmarks*")
1405 (goto-char (point-max))
1406 (beginning-of-line))
1407 ;; On the final line.
1411 (user-error "No previous bookmark")
1413 (setq bookmark (get-text-property (line-beginning-position)
1415 (eww-browse-url (plist-get bookmark :url))))
1417 (defvar eww-bookmark-mode-map
1418 (let ((map (make-sparse-keymap)))
1419 (suppress-keymap map)
1420 (define-key map "q" 'quit-window)
1421 (define-key map [(control k)] 'eww-bookmark-kill)
1422 (define-key map [(control y)] 'eww-bookmark-yank)
1423 (define-key map "\r" 'eww-bookmark-browse)
1425 (easy-menu-define nil map
1426 "Menu for `eww-bookmark-mode-map'."
1428 ["Exit" quit-window t]
1429 ["Browse" eww-bookmark-browse
1430 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1431 ["Kill" eww-bookmark-kill
1432 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1433 ["Yank" eww-bookmark-yank
1434 :active eww-bookmark-kill-ring]))
1437 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1438 "Mode for listing bookmarks.
1440 \\{eww-bookmark-mode-map}"
1441 (buffer-disable-undo)
1442 (setq buffer-read-only t
1447 (defun eww-save-history ()
1448 (plist-put eww-data :point (point))
1449 (plist-put eww-data :text (buffer-string))
1450 (push eww-data eww-history)
1451 (setq eww-data (list :title ""))
1452 ;; Don't let the history grow infinitely. We store quite a lot of
1454 (when-let (tail (nthcdr 50 eww-history))
1457 (defun eww-list-histories ()
1458 "List the eww-histories."
1460 (when (null eww-history)
1461 (error "No eww-histories are defined"))
1462 (let ((eww-history-trans eww-history))
1463 (set-buffer (get-buffer-create "*eww history*"))
1465 (let ((inhibit-read-only t)
1468 url title format start)
1470 (dolist (history eww-history-trans)
1471 (setq start (point))
1472 (setq domain-length (max domain-length (length (plist-get history :url))))
1473 (setq title-length (max title-length (length (plist-get history :title)))))
1474 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1476 (concat " " (format format "Title" "URL")))
1477 (dolist (history eww-history-trans)
1478 (setq start (point))
1479 (setq url (plist-get history :url))
1480 (setq title (plist-get history :title))
1481 (insert (format format title url))
1483 (put-text-property start (1+ start) 'eww-history history))
1484 (goto-char (point-min)))
1485 (pop-to-buffer "*eww history*")))
1487 (defun eww-history-browse ()
1488 "Browse the history under point in eww."
1490 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1492 (error "No history on the current line"))
1494 (eww-restore-history history)))
1496 (defvar eww-history-mode-map
1497 (let ((map (make-sparse-keymap)))
1498 (suppress-keymap map)
1499 (define-key map "q" 'quit-window)
1500 (define-key map "\r" 'eww-history-browse)
1501 ;; (define-key map "n" 'next-error-no-select)
1502 ;; (define-key map "p" 'previous-error-no-select)
1504 (easy-menu-define nil map
1505 "Menu for `eww-history-mode-map'."
1507 ["Exit" quit-window t]
1508 ["Browse" eww-history-browse
1509 :active (get-text-property (line-beginning-position) 'eww-history)]))
1512 (define-derived-mode eww-history-mode nil "eww history"
1513 "Mode for listing eww-histories.
1515 \\{eww-history-mode-map}"
1516 (buffer-disable-undo)
1517 (setq buffer-read-only t
1522 ;;; eww.el ends here