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-form-checkbox-selected-symbol
"[X]"
77 "Symbol used to represent a selected checkbox.
78 See also `eww-form-checkbox-symbol'."
81 :type
'(choice (const "[X]")
82 (const "☒") ; Unicode BALLOT BOX WITH X
83 (const "☑") ; Unicode BALLOT BOX WITH CHECK
86 (defcustom eww-form-checkbox-symbol
"[ ]"
87 "Symbol used to represent a checkbox.
88 See also `eww-form-checkbox-selected-symbol'."
91 :type
'(choice (const "[ ]")
92 (const "☐") ; Unicode BALLOT BOX
95 (defface eww-form-submit
96 '((((type x w32 ns
) (class color
)) ; Like default mode line
97 :box
(:line-width
2 :style released-button
)
98 :background
"#808080" :foreground
"black"))
99 "Face for eww buffer buttons."
103 (defface eww-form-file
104 '((((type x w32 ns
) (class color
)) ; Like default mode line
105 :box
(:line-width
2 :style released-button
)
106 :background
"#808080" :foreground
"black"))
107 "Face for eww buffer buttons."
111 (defface eww-form-checkbox
112 '((((type x w32 ns
) (class color
)) ; Like default mode line
113 :box
(:line-width
2 :style released-button
)
114 :background
"lightgrey" :foreground
"black"))
115 "Face for eww buffer buttons."
119 (defface eww-form-select
120 '((((type x w32 ns
) (class color
)) ; Like default mode line
121 :box
(:line-width
2 :style released-button
)
122 :background
"lightgrey" :foreground
"black"))
123 "Face for eww buffer buttons."
127 (defface eww-form-text
128 '((t (:background
"#505050"
130 :box
(:line-width
1))))
131 "Face for eww text inputs."
135 (defface eww-form-textarea
136 '((t (:background
"#C0C0C0"
138 :box
(:line-width
1))))
139 "Face for eww textarea inputs."
143 (defvar eww-data nil
)
144 (defvar eww-history nil
)
145 (defvar eww-history-position
0)
147 (defvar eww-local-regex
"localhost"
148 "When this regex is found in the URL, it's not a keyword but an address.")
150 (defvar eww-link-keymap
151 (let ((map (copy-keymap shr-map
)))
152 (define-key map
"\r" 'eww-follow-link
)
157 "Fetch URL and render the page.
158 If the input doesn't look like an URL or a domain name, the
159 word(s) will be searched for via `eww-search-prefix'."
160 (interactive "sEnter URL or keywords: ")
161 (setq url
(string-trim url
))
162 (cond ((string-match-p "\\`file:/" url
))
163 ((string-match-p "\\`ftp://" url
)
164 (user-error "FTP is not supported."))
166 (if (and (= (length (split-string url
)) 1)
167 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
168 (> (length (split-string url
"[.:]")) 1))
169 (string-match eww-local-regex url
)))
171 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
172 (setq url
(concat "http://" url
)))
173 ;; some site don't redirect final /
174 (when (string= (url-filename (url-generic-parse-url url
)) "")
175 (setq url
(concat url
"/"))))
176 (setq url
(concat eww-search-prefix
177 (replace-regexp-in-string " " "+" url
))))))
178 (url-retrieve url
'eww-render
180 (and (eq major-mode
'eww-mode
)
183 ;;;###autoload (defalias 'browse-web 'eww)
186 (defun eww-open-file (file)
187 "Render a file using EWW."
188 (interactive "fFile: ")
189 (eww (concat "file://"
190 (and (memq system-type
'(windows-nt ms-dos
))
192 (expand-file-name file
))))
194 (defun eww-render (status url
&optional point buffer
)
195 (let ((redirect (plist-get status
:redirect
)))
197 (setq url redirect
)))
198 (let* ((headers (eww-parse-headers))
200 (mail-header-parse-content-type
201 (or (cdr (assoc "content-type" headers
))
205 (or (cdr (assq 'charset
(cdr content-type
)))
206 (eww-detect-charset (equal (car content-type
)
209 (data-buffer (current-buffer)))
212 (plist-put eww-data
:title
"")
214 ((and eww-use-external-browser-for-content-type
215 (string-match-p eww-use-external-browser-for-content-type
217 (eww-browse-with-external-browser url
))
218 ((equal (car content-type
) "text/html")
219 (eww-display-html charset url nil point buffer
))
220 ((equal (car content-type
) "application/pdf")
222 ((string-match-p "\\`image/" (car content-type
))
223 (eww-display-image buffer
)
224 (eww-update-header-line-format))
226 (eww-display-raw buffer
)
227 (eww-update-header-line-format)))
228 (plist-put eww-data
:title url
)
229 (setq eww-history-position
0))
230 (kill-buffer data-buffer
))))
232 (defun eww-parse-headers ()
234 (goto-char (point-min))
235 (while (and (not (eobp))
237 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
238 (push (cons (downcase (match-string 1))
246 (defun eww-detect-charset (html-p)
247 (let ((case-fold-search t
)
251 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
255 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
258 (declare-function libxml-parse-html-region
"xml.c"
259 (start end
&optional base-url
))
261 (defun eww-display-html (charset url
&optional document point buffer
)
262 (or (fboundp 'libxml-parse-html-region
)
263 (error "This function requires Emacs to be compiled with libxml2"))
264 ;; There should be a better way to abort loading images
270 'base
(list (cons 'href url
))
272 (unless (eq charset
'utf-8
)
274 (decode-coding-region (point) (point-max) charset
)
275 (coding-system-error nil
)))
276 (libxml-parse-html-region (point) (point-max))))))
277 (source (and (null document
)
278 (buffer-substring (point) (point-max)))))
279 (eww-setup-buffer buffer
)
280 (plist-put eww-data
:source source
)
281 (plist-put eww-data
:dom document
)
282 (let ((inhibit-read-only t
)
283 (after-change-functions nil
)
284 (shr-target-id (url-target (url-generic-parse-url url
)))
285 (shr-external-rendering-functions
286 '((title . eww-tag-title
)
287 (form . eww-tag-form
)
288 (input . eww-tag-input
)
289 (textarea . eww-tag-textarea
)
290 (body . eww-tag-body
)
291 (select . eww-tag-select
)
292 (link . eww-tag-link
)
294 (shr-insert-document document
)
299 (goto-char (point-min))
300 (let ((point (next-single-property-change
301 (point-min) 'shr-target-id
)))
305 (goto-char (point-min))
306 ;; Don't leave point inside forms, because the normal eww
307 ;; commands aren't available there.
308 (while (and (not (eobp))
309 (get-text-property (point) 'eww-form
))
311 (plist-put eww-data
:url url
)
312 (setq eww-history-position
0)
313 (eww-update-header-line-format)))
315 (defun eww-handle-link (cont)
316 (let* ((rel (assq :rel cont
))
317 (href (assq :href cont
))
319 ;; The text associated with :rel is case-insensitive.
320 (if rel
(downcase (cdr rel
)))
322 ;; Texinfo uses "previous", but HTML specifies
323 ;; "prev", so recognize both.
324 ("previous" .
:previous
)
326 ;; HTML specifies "start" but also "contents",
327 ;; and Gtk seems to use "home". Recognize
328 ;; them all; but store them in different
329 ;; variables so that we can readily choose the
333 ("contents" .
:contents
)
337 (plist-put eww-data
(cdr where
) (cdr href
)))))
339 (defun eww-tag-link (cont)
340 (eww-handle-link cont
)
343 (defun eww-tag-a (cont)
344 (eww-handle-link cont
)
345 (let ((start (point)))
347 (put-text-property start
(point) 'keymap eww-link-keymap
)))
349 (defun eww-update-header-line-format ()
350 (if eww-header-line-format
351 (setq header-line-format
352 (replace-regexp-in-string
354 ;; FIXME? Title can be blank. Default to, eg, last component
356 (format-spec eww-header-line-format
357 `((?u .
,(plist-get eww-data
:url
))
358 (?t .
,(or (plist-get eww-data
:title
) ""))))))
359 (setq header-line-format nil
)))
361 (defun eww-tag-title (cont)
364 (when (eq (car sub
) 'text
)
365 (setq title
(concat title
(cdr sub
)))))
366 (plist-put eww-data
:title
367 (replace-regexp-in-string
369 (replace-regexp-in-string "[ \t\r\n]+" " " title
))))
370 (eww-update-header-line-format))
372 (defun eww-tag-body (cont)
373 (let* ((start (point))
374 (fgcolor (cdr (or (assq :fgcolor cont
)
376 (bgcolor (cdr (assq :bgcolor cont
)))
377 (shr-stylesheet (list (cons 'color fgcolor
)
378 (cons 'background-color bgcolor
))))
380 (shr-colorize-region start
(point) fgcolor bgcolor
)))
382 (defun eww-display-raw (&optional buffer
)
383 (let ((data (buffer-substring (point) (point-max))))
384 (eww-setup-buffer buffer
)
385 (let ((inhibit-read-only t
))
387 (goto-char (point-min))))
389 (defun eww-display-image (&optional buffer
)
390 (let ((data (shr-parse-image-data)))
391 (eww-setup-buffer buffer
)
392 (let ((inhibit-read-only t
))
393 (shr-put-image data nil
))
394 (goto-char (point-min))))
396 (defun eww-display-pdf ()
397 (let ((data (buffer-substring (point) (point-max))))
398 (switch-to-buffer (get-buffer-create "*eww pdf*"))
399 (let ((coding-system-for-write 'raw-text
)
400 (inhibit-read-only t
))
404 (goto-char (point-min)))
406 (defun eww-setup-buffer (&optional buffer
)
408 (if (buffer-live-p buffer
)
410 (get-buffer-create "*eww*")))
411 (let ((inhibit-read-only t
))
414 (unless (eq major-mode
'eww-mode
)
417 (defun eww-view-source ()
418 "View the HTML source code of the current page."
420 (let ((buf (get-buffer-create "*eww-source*"))
421 (source (plist-get eww-data
:source
)))
422 (with-current-buffer buf
423 (delete-region (point-min) (point-max))
424 (insert (or source
"no source"))
425 (goto-char (point-min))
426 (when (fboundp 'html-mode
)
430 (defun eww-readable ()
431 "View the main \"readable\" parts of the current web page.
432 This command uses heuristics to find the parts of the web page that
433 contains the main textual portion, leaving out navigation menus and
436 (let* ((old-data eww-data
)
437 (dom (shr-transform-dom
439 (insert (plist-get old-data
:source
))
441 (decode-coding-region (point-min) (point-max) 'utf-8
)
442 (coding-system-error nil
))
443 (libxml-parse-html-region (point-min) (point-max))))))
444 (eww-score-readability dom
)
446 (eww-display-html nil nil
448 (eww-highest-readability dom
))
449 nil
(current-buffer))
450 (dolist (elem '(:source
:url
:title
:next
:previous
:up
))
451 (plist-put eww-data elem
(plist-get old-data elem
)))
452 (eww-update-header-line-format)))
454 (defun eww-score-readability (node)
457 ((memq (car node
) '(script head comment
))
459 ((eq (car node
) 'meta
)
461 ((eq (car node
) 'img
)
464 (setq score
(- (length (split-string
465 (or (cdr (assoc 'text
(cdr node
))) ""))))))
467 (dolist (elem (cdr node
))
469 ((and (stringp (cdr elem
))
470 (eq (car elem
) 'text
))
471 (setq score
(+ score
(length (split-string (cdr elem
))))))
474 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
475 (eww-score-readability elem
)))))))))
476 ;; Cache the score of the node to avoid recomputing all the time.
477 (setcdr node
(cons (cons :eww-readability-score score
) (cdr node
)))
480 (defun eww-highest-readability (node)
483 (dolist (elem (cdr node
))
484 (when (and (consp (cdr elem
))
486 :eww-readability-score
488 (eww-highest-readability elem
))))
489 most-negative-fixnum
)
490 (or (cdr (assoc :eww-readability-score
(cdr result
)))
491 most-negative-fixnum
)))
492 (setq result highest
)))
496 (let ((map (make-sparse-keymap)))
497 (suppress-keymap map
)
498 (define-key map
"q" 'quit-window
)
499 (define-key map
"g" 'eww-reload
)
500 (define-key map
[?
\t] 'shr-next-link
)
501 (define-key map
[?\M-
\t] 'shr-previous-link
)
502 (define-key map
[delete] 'scroll-down-command)
503 (define-key map [?\S-\ ] 'scroll-down-command)
504 (define-key map "\177" 'scroll-down-command)
505 (define-key map " " 'scroll-up-command)
506 (define-key map "l" 'eww-back-url)
507 (define-key map "r" 'eww-forward-url)
508 (define-key map "n" 'eww-next-url)
509 (define-key map "p" 'eww-previous-url)
510 (define-key map "u" 'eww-up-url)
511 (define-key map "t" 'eww-top-url)
512 (define-key map "&" 'eww-browse-with-external-browser)
513 (define-key map "d" 'eww-download)
514 (define-key map "w" 'eww-copy-page-url)
515 (define-key map "C" 'url-cookie-list)
516 (define-key map "v" 'eww-view-source)
517 (define-key map "R" 'eww-readable)
518 (define-key map "H" 'eww-list-histories)
520 (define-key map "b" 'eww-add-bookmark)
521 (define-key map "B" 'eww-list-bookmarks)
522 (define-key map [(meta n)] 'eww-next-bookmark)
523 (define-key map [(meta p)] 'eww-previous-bookmark)
525 (easy-menu-define nil map ""
527 ["Exit" quit-window t]
528 ["Close browser" quit-window t]
529 ["Reload" eww-reload t]
530 ["Back to previous page" eww-back-url
531 :active (not (zerop (length eww-history)))]
532 ["Forward to next page" eww-forward-url
533 :active (not (zerop eww-history-position))]
534 ["Browse with external browser" eww-browse-with-external-browser t]
535 ["Download" eww-download t]
536 ["View page source" eww-view-source]
537 ["Copy page URL" eww-copy-page-url t]
538 ["List histories" eww-list-histories t]
539 ["Add bookmark" eww-add-bookmark t]
540 ["List bookmarks" eww-list-bookmarks t]
541 ["List cookies" url-cookie-list t]))
544 (defvar eww-tool-bar-map
545 (let ((map (make-sparse-keymap)))
546 (dolist (tool-bar-item
547 '((quit-window . "close")
548 (eww-reload . "refresh")
549 (eww-back-url . "left-arrow")
550 (eww-forward-url . "right-arrow")
551 (eww-view-source . "show")
552 (eww-copy-page-url . "copy")
553 (eww-add-bookmark . "bookmark_add"))) ;; ...
554 (tool-bar-local-item-from-menu
555 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
557 "Tool bar for `eww-mode'.")
559 (define-derived-mode eww-mode nil "eww"
560 "Mode for browsing the web.
563 (setq-local eww-data (list :title ""))
564 (setq-local browse-url-browser-function 'eww-browse-url)
565 (setq-local after-change-functions 'eww-process-text-input)
566 (setq-local eww-history nil)
567 (setq-local eww-history-position 0)
568 (when (boundp 'tool-bar-map)
569 (setq-local tool-bar-map eww-tool-bar-map))
570 (buffer-disable-undo)
571 ;;(setq buffer-read-only t)
575 (defun eww-browse-url (url &optional _new-window)
576 (when (and (equal major-mode 'eww-mode)
577 (plist-get eww-data :url))
581 (defun eww-back-url ()
582 "Go to the previously displayed page."
584 (when (>= eww-history-position (length eww-history))
585 (user-error "No previous page"))
587 (setq eww-history-position (+ eww-history-position 2))
588 (eww-restore-history (elt eww-history (1- eww-history-position))))
590 (defun eww-forward-url ()
591 "Go to the next displayed page."
593 (when (zerop eww-history-position)
594 (user-error "No next page"))
596 (eww-restore-history (elt eww-history (1- eww-history-position))))
598 (defun eww-restore-history (elem)
599 (let ((inhibit-read-only t))
601 (insert (plist-get elem :text))
602 (goto-char (plist-get elem :point))
604 (eww-update-header-line-format)))
606 (defun eww-next-url ()
607 "Go to the page marked `next'.
608 A page is marked `next' if rel=\"next\" appears in a <link>
611 (if (plist-get eww-data :next)
612 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
613 (plist-get eww-data :url)))
614 (user-error "No `next' on this page")))
616 (defun eww-previous-url ()
617 "Go to the page marked `previous'.
618 A page is marked `previous' if rel=\"previous\" appears in a <link>
621 (if (plist-get eww-data :previous)
622 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
623 (plist-get eww-data :url)))
624 (user-error "No `previous' on this page")))
627 "Go to the page marked `up'.
628 A page is marked `up' if rel=\"up\" appears in a <link>
631 (if (plist-get eww-data :up)
632 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
633 (plist-get eww-data :url)))
634 (user-error "No `up' on this page")))
636 (defun eww-top-url ()
637 "Go to the page marked `top'.
638 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
639 appears in a <link> or <a> tag."
641 (let ((best-url (or (plist-get eww-data :start)
642 (plist-get eww-data :contents)
643 (plist-get eww-data :home))))
645 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
646 (user-error "No `top' for this page"))))
649 "Reload the current page."
651 (let ((url (plist-get eww-data :url)))
652 (url-retrieve url 'eww-render (list url (point)))))
656 (defvar eww-form nil)
658 (defvar eww-submit-map
659 (let ((map (make-sparse-keymap)))
660 (define-key map "\r" 'eww-submit)
661 (define-key map [(control c) (control c)] 'eww-submit)
664 (defvar eww-submit-file
665 (let ((map (make-sparse-keymap)))
666 (define-key map "\r" 'eww-select-file)
667 (define-key map [(control c) (control c)] 'eww-submit)
670 (defvar eww-checkbox-map
671 (let ((map (make-sparse-keymap)))
672 (define-key map " " 'eww-toggle-checkbox)
673 (define-key map "\r" 'eww-toggle-checkbox)
674 (define-key map [(control c) (control c)] 'eww-submit)
678 (let ((map (make-keymap)))
679 (set-keymap-parent map text-mode-map)
680 (define-key map "\r" 'eww-submit)
681 (define-key map [(control a)] 'eww-beginning-of-text)
682 (define-key map [(control c) (control c)] 'eww-submit)
683 (define-key map [(control e)] 'eww-end-of-text)
684 (define-key map [?\t] 'shr-next-link)
685 (define-key map [?\M-\t] 'shr-previous-link)
688 (defvar eww-textarea-map
689 (let ((map (make-keymap)))
690 (set-keymap-parent map text-mode-map)
691 (define-key map "\r" 'forward-line)
692 (define-key map [(control c) (control c)] 'eww-submit)
693 (define-key map [?\t] 'shr-next-link)
694 (define-key map [?\M-\t] 'shr-previous-link)
697 (defvar eww-select-map
698 (let ((map (make-sparse-keymap)))
699 (define-key map "\r" 'eww-change-select)
700 (define-key map [(control c) (control c)] 'eww-submit)
703 (defun eww-beginning-of-text ()
704 "Move to the start of the input field."
706 (goto-char (eww-beginning-of-field)))
708 (defun eww-end-of-text ()
709 "Move to the end of the text in the input field."
711 (goto-char (eww-end-of-field))
712 (let ((start (eww-beginning-of-field)))
713 (while (and (equal (following-char) ? )
716 (when (> (point) start)
719 (defun eww-beginning-of-field ()
723 ((not (eq (get-text-property (point) 'eww-form)
724 (get-text-property (1- (point)) 'eww-form)))
727 (previous-single-property-change
728 (point) 'eww-form nil (point-min)))))
730 (defun eww-end-of-field ()
731 (1- (next-single-property-change
732 (point) 'eww-form nil (point-max))))
734 (defun eww-tag-form (cont)
736 (list (assq :method cont)
737 (assq :action cont)))
739 (shr-ensure-paragraph)
744 (when (> (point) start)
745 (put-text-property start (1+ start)
746 'eww-form eww-form))))
748 (defun eww-form-submit (cont)
749 (let ((start (point))
750 (value (cdr (assq :value cont))))
752 (if (zerop (length value))
756 (add-face-text-property start (point) 'eww-form-submit)
757 (put-text-property start (point) 'eww-form
758 (list :eww-form eww-form
761 :name (cdr (assq :name cont))))
762 (put-text-property start (point) 'keymap eww-submit-map)
765 (defun eww-form-checkbox (cont)
766 (let ((start (point)))
767 (if (cdr (assq :checked cont))
768 (insert eww-form-checkbox-selected-symbol)
769 (insert eww-form-checkbox-symbol))
770 (add-face-text-property start (point) 'eww-form-checkbox)
771 (put-text-property start (point) 'eww-form
772 (list :eww-form eww-form
773 :value (cdr (assq :value cont))
774 :type (downcase (cdr (assq :type cont)))
775 :checked (cdr (assq :checked cont))
776 :name (cdr (assq :name cont))))
777 (put-text-property start (point) 'keymap eww-checkbox-map)
780 (defun eww-form-file (cont)
781 (let ((start (point))
782 (value (cdr (assq :value cont))))
784 (if (zerop (length value))
788 (add-face-text-property start (point) 'eww-form-file)
790 (put-text-property start (point) 'eww-form
791 (list :eww-form eww-form
792 :value (cdr (assq :value cont))
793 :type (downcase (cdr (assq :type cont)))
794 :name (cdr (assq :name cont))))
795 (put-text-property start (point) 'keymap eww-submit-file)
798 (defun eww-select-file ()
799 "Change the value of the upload file menu under point."
801 (let* ((input (get-text-property (point) 'eww-form)))
803 (let ((insert-default-directory t))
804 (read-file-name "filename: "))))
805 (eww-update-field filename (length "Browse"))
806 (plist-put input :filename filename))))
808 (defun eww-form-text (cont)
809 (let ((start (point))
810 (type (downcase (or (cdr (assq :type cont))
812 (value (or (cdr (assq :value cont)) ""))
813 (width (string-to-number
814 (or (cdr (assq :size cont))
816 (readonly-property (if (or (cdr (assq :disabled cont))
817 (cdr (assq :readonly cont)))
819 'inhibit-read-only)))
821 (when (< (length value) width)
822 (insert (make-string (- width (length value)) ? )))
823 (put-text-property start (point) 'face 'eww-form-text)
824 (put-text-property start (point) 'local-map eww-text-map)
825 (put-text-property start (point) readonly-property t)
826 (put-text-property start (point) 'eww-form
827 (list :eww-form eww-form
830 :name (cdr (assq :name cont))))
833 (defconst eww-text-input-types '("text" "password" "textarea"
834 "color" "date" "datetime" "datetime-local"
835 "email" "month" "number" "search" "tel"
837 "List of input types which represent a text input.
838 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
840 (defun eww-process-text-input (beg end length)
841 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
842 (properties (text-properties-at end))
843 (type (plist-get form :type)))
845 (member type eww-text-input-types))
848 ;; Delete some space at the end.
851 (if (equal type "textarea")
852 (1- (line-end-position))
854 (let ((new (- end beg)))
855 (while (and (> new 0)
856 (eql (following-char) ? ))
857 (delete-region (point) (1+ (point)))
858 (setq new (1- new))))
859 (set-text-properties beg end properties)))
864 (if (equal type "textarea")
865 (1- (line-end-position))
867 (let ((start (point)))
868 (insert (make-string length ? ))
869 (set-text-properties start (point) properties)))))
870 (let ((value (buffer-substring-no-properties
871 (eww-beginning-of-field)
872 (eww-end-of-field))))
873 (when (string-match " +\\'" value)
874 (setq value (substring value 0 (match-beginning 0))))
875 (plist-put form :value value)
876 (when (equal type "password")
877 ;; Display passwords as asterisks.
878 (let ((start (eww-beginning-of-field)))
879 (put-text-property start (+ start (length value))
880 'display (make-string (length value) ?*))))))))
882 (defun eww-tag-textarea (cont)
883 (let ((start (point))
884 (value (or (cdr (assq :value cont)) ""))
885 (lines (string-to-number
886 (or (cdr (assq :rows cont))
888 (width (string-to-number
889 (or (cdr (assq :cols cont))
895 (when (< (count-lines start (point)) lines)
896 (dotimes (i (- lines (count-lines start (point))))
898 (setq end (point-marker))
900 (while (< (point) end)
902 (let ((pad (- width (- (point) (line-beginning-position)))))
904 (insert (make-string pad ? ))))
905 (add-face-text-property (line-beginning-position)
906 (point) 'eww-form-textarea)
907 (put-text-property (line-beginning-position) (point)
908 'local-map eww-textarea-map)
910 (put-text-property start (point) 'eww-form
911 (list :eww-form eww-form
914 :name (cdr (assq :name cont))))))
916 (defun eww-tag-input (cont)
917 (let ((type (downcase (or (cdr (assq :type cont))
921 ((or (equal type "checkbox")
922 (equal type "radio"))
923 (eww-form-checkbox cont))
925 (eww-form-file cont))
926 ((equal type "submit")
927 (eww-form-submit cont))
928 ((equal type "hidden")
929 (let ((form eww-form)
930 (name (cdr (assq :name cont))))
931 ;; Don't add <input type=hidden> elements repeatedly.
933 (or (not (consp (car form)))
934 (not (eq (caar form) 'hidden))
935 (not (equal (plist-get (cdr (car form)) :name)
937 (setq form (cdr form)))
939 (nconc eww-form (list
942 :value (cdr (assq :value cont))))))))
944 (eww-form-text cont)))
945 (unless (= start (point))
946 (put-text-property start (1+ start) 'help-echo "Input field"))))
948 (defun eww-tag-select (cont)
949 (shr-ensure-paragraph)
950 (let ((menu (list :name (cdr (assq :name cont))
956 (if (eq (car (car cont)) 'optgroup)
957 (dolist (groupelem cont)
958 (unless (cdr (assq :disabled (cdr groupelem)))
959 (setq opelem (append opelem (cdr (cdr groupelem))))))
961 (dolist (elem opelem)
962 (when (eq (car elem) 'option)
963 (when (cdr (assq :selected (cdr elem)))
964 (nconc menu (list :value
965 (cdr (assq :value (cdr elem))))))
966 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
967 (setq max (max max (length display)))
969 :value (cdr (assq :value (cdr elem)))
973 (setq options (nreverse options))
974 ;; If we have no selected values, default to the first value.
975 (unless (plist-get menu :value)
976 (nconc menu (list :value (nth 2 (car options)))))
978 (let ((selected (eww-select-display menu)))
980 (make-string (- max (length selected)) ? )))
981 (put-text-property start (point) 'eww-form menu)
982 (add-face-text-property start (point) 'eww-form-select)
983 (put-text-property start (point) 'keymap eww-select-map)
984 (unless (= start (point))
985 (put-text-property start (1+ start) 'help-echo "select field"))
986 (shr-ensure-paragraph))))
988 (defun eww-select-display (select)
989 (let ((value (plist-get select :value))
991 (dolist (elem select)
992 (when (and (consp elem)
993 (eq (car elem) 'item)
994 (equal value (plist-get (cdr elem) :value)))
995 (setq display (plist-get (cdr elem) :display))))
998 (defun eww-change-select ()
999 "Change the value of the select drop-down menu under point."
1001 (let* ((input (get-text-property (point) 'eww-form))
1002 (completion-ignore-case t)
1005 (mapcar (lambda (elem)
1007 (eq (car elem) 'item)
1008 (cons (plist-get (cdr elem) :display)
1009 (plist-get (cdr elem) :value))))
1012 (completing-read "Change value: " options nil 'require-match))
1013 (inhibit-read-only t))
1014 (plist-put input :value (cdr (assoc-string display options t)))
1016 (eww-update-field display))))
1018 (defun eww-update-field (string &optional offset)
1019 (if (not offset) (setq offset 0))
1020 (let ((properties (text-properties-at (point)))
1021 (start (+ (eww-beginning-of-field) offset))
1022 (current-end (1+ (eww-end-of-field)))
1023 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1024 (delete-region start current-end)
1025 (forward-char offset)
1027 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1028 (if (= 0 offset) (set-text-properties start new-end properties))
1031 (defun eww-toggle-checkbox ()
1032 "Toggle the value of the checkbox under point."
1034 (let* ((input (get-text-property (point) 'eww-form))
1035 (type (plist-get input :type)))
1036 (if (equal type "checkbox")
1039 (if (plist-get input :checked)
1041 (plist-put input :checked nil)
1042 (eww-update-field eww-form-checkbox-symbol))
1043 (plist-put input :checked t)
1044 (eww-update-field eww-form-checkbox-selected-symbol))))
1045 ;; Radio button. Switch all other buttons off.
1046 (let ((name (plist-get input :name)))
1048 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1049 (when (equal (plist-get (cdr elem) :name) name)
1050 (goto-char (car elem))
1051 (if (not (eq (cdr elem) input))
1053 (plist-put input :checked nil)
1054 (eww-update-field eww-form-checkbox-symbol))
1055 (plist-put input :checked t)
1056 (eww-update-field eww-form-checkbox-selected-symbol)))))
1057 (forward-char 1)))))
1059 (defun eww-inputs (form)
1060 (let ((start (point-min))
1063 (< start (point-max)))
1064 (when (or (get-text-property start 'eww-form)
1065 (setq start (next-single-property-change start 'eww-form)))
1066 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1068 (push (cons start (get-text-property start 'eww-form))
1070 (setq start (next-single-property-change start 'eww-form))))
1073 (defun eww-input-value (input)
1074 (let ((type (plist-get input :type))
1075 (value (plist-get input :value)))
1077 ((equal type "textarea")
1080 (goto-char (point-min))
1081 (while (re-search-forward "^ +\n\\| +$" nil t)
1082 (replace-match "" t t))
1085 (if (string-match " +\\'" value)
1086 (substring value 0 (match-beginning 0))
1089 (defun eww-submit ()
1090 "Submit the current form."
1092 (let* ((this-input (get-text-property (point) 'eww-form))
1093 (form (plist-get this-input :eww-form))
1095 (dolist (elem (sort (eww-inputs form)
1097 (< (car o1) (car o2)))))
1098 (let* ((input (cdr elem))
1099 (input-start (car elem))
1100 (name (plist-get input :name)))
1103 ((member (plist-get input :type) '("checkbox" "radio"))
1104 (when (plist-get input :checked)
1105 (push (cons name (plist-get input :value))
1107 ((equal (plist-get input :type) "file")
1109 (list (cons "filedata"
1111 (insert-file-contents
1112 (plist-get input :filename))
1114 (cons "name" (plist-get input :name))
1115 (cons "filename" (plist-get input :filename))))
1117 ((equal (plist-get input :type) "submit")
1118 ;; We want the values from buttons if we hit a button if
1119 ;; we hit enter on it, or if it's the first button after
1120 ;; the field we did hit return on.
1121 (when (or (eq input this-input)
1122 (and (not (eq input this-input))
1124 (> input-start (point))))
1125 (setq next-submit t)
1126 (push (cons name (plist-get input :value))
1129 (push (cons name (eww-input-value input))
1132 (when (and (consp elem)
1133 (eq (car elem) 'hidden))
1134 (push (cons (plist-get (cdr elem) :name)
1135 (or (plist-get (cdr elem) :value) ""))
1137 (if (and (stringp (cdr (assq :method form)))
1138 (equal (downcase (cdr (assq :method form))) "post"))
1140 (dolist (x values mtype)
1141 (if (equal (car x) "file")
1143 (setq mtype "multipart/form-data"))))
1144 (cond ((equal mtype "multipart/form-data")
1145 (let ((boundary (mml-compute-boundary '())))
1146 (let ((url-request-method "POST")
1147 (url-request-extra-headers
1148 (list (cons "Content-Type"
1149 (concat "multipart/form-data; boundary="
1152 (mm-url-encode-multipart-form-data values boundary)))
1153 (eww-browse-url (shr-expand-url
1154 (cdr (assq :action form))
1155 (plist-get eww-data :url))))))
1157 (let ((url-request-method "POST")
1158 (url-request-extra-headers
1160 "application/x-www-form-urlencoded")))
1162 (mm-url-encode-www-form-urlencoded values)))
1163 (eww-browse-url (shr-expand-url
1164 (cdr (assq :action form))
1165 (plist-get eww-data :url)))))))
1168 (if (cdr (assq :action form))
1169 (shr-expand-url (cdr (assq :action form))
1170 (plist-get eww-data :url))
1171 (plist-get eww-data :url))
1173 (mm-url-encode-www-form-urlencoded values))))))
1175 (defun eww-browse-with-external-browser (&optional url)
1176 "Browse the current URL with an external browser.
1177 The browser to used is specified by the `shr-external-browser' variable."
1179 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1181 (defun eww-follow-link (&optional external mouse-event)
1182 "Browse the URL under point.
1183 If EXTERNAL, browse the URL using `shr-external-browser'."
1184 (interactive (list current-prefix-arg last-nonmenu-event))
1185 (mouse-set-point mouse-event)
1186 (let ((url (get-text-property (point) 'shr-url)))
1189 (message "No link under point"))
1190 ((string-match "^mailto:" url)
1191 (browse-url-mail url))
1193 (funcall shr-external-browser url))
1194 ;; This is a #target url in the same page as the current one.
1195 ((and (url-target (url-generic-parse-url url))
1196 (eww-same-page-p url (plist-get eww-data :url)))
1198 (eww-display-html 'utf-8 url (plist-get eww-data :url)
1199 nil (current-buffer)))
1201 (eww-browse-url url)))))
1203 (defun eww-same-page-p (url1 url2)
1204 "Return non-nil if both URLs represent the same page.
1205 Differences in #targets are ignored."
1206 (let ((obj1 (url-generic-parse-url url1))
1207 (obj2 (url-generic-parse-url url2)))
1208 (setf (url-target obj1) nil)
1209 (setf (url-target obj2) nil)
1210 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1212 (defun eww-copy-page-url ()
1214 (message "%s" (plist-get eww-data :url))
1215 (kill-new (plist-get eww-data :url)))
1217 (defun eww-download ()
1218 "Download URL under point to `eww-download-directory'."
1220 (let ((url (get-text-property (point) 'shr-url)))
1222 (message "No URL under point")
1223 (url-retrieve url 'eww-download-callback (list url)))))
1225 (defun eww-download-callback (status url)
1226 (unless (plist-get status :error)
1227 (let* ((obj (url-generic-parse-url url))
1228 (path (car (url-path-and-query obj)))
1229 (file (eww-make-unique-file-name (file-name-nondirectory path)
1230 eww-download-directory)))
1231 (goto-char (point-min))
1232 (re-search-forward "\r?\n\r?\n")
1233 (write-region (point) (point-max) file)
1234 (message "Saved %s" file))))
1236 (defun eww-make-unique-file-name (file directory)
1238 ((zerop (length file))
1240 ((string-match "\\`[.]" file)
1241 (setq file (concat "!" file))))
1243 (while (file-exists-p (expand-file-name file directory))
1245 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1246 (format "%s(%d)%s" (match-string 1 file)
1247 count (match-string 2 file))
1248 (format "%s(%d)" file count)))
1249 (setq count (1+ count)))
1250 (expand-file-name file directory)))
1254 (defvar eww-bookmarks nil)
1256 (defun eww-add-bookmark ()
1257 "Add the current page to the bookmarks."
1259 (eww-read-bookmarks)
1260 (dolist (bookmark eww-bookmarks)
1261 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1262 (user-error "Already bookmarked")))
1263 (if (y-or-n-p "bookmark this page? ")
1265 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1266 (plist-get eww-data :url))))
1267 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1268 (push (list :url (plist-get eww-data :url)
1270 :time (current-time-string))
1272 (eww-write-bookmarks)
1273 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1274 (plist-get eww-data :title)))))
1276 (defun eww-write-bookmarks ()
1277 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1278 (insert ";; Auto-generated file; don't edit\n")
1279 (pp eww-bookmarks (current-buffer))))
1281 (defun eww-read-bookmarks ()
1282 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1284 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1286 (insert-file-contents file)
1287 (read (current-buffer)))))))
1290 (defun eww-list-bookmarks ()
1291 "Display the bookmarks."
1293 (eww-bookmark-prepare)
1294 (pop-to-buffer "*eww bookmarks*"))
1296 (defun eww-bookmark-prepare ()
1297 (eww-read-bookmarks)
1298 (unless eww-bookmarks
1299 (user-error "No bookmarks are defined"))
1300 (set-buffer (get-buffer-create "*eww bookmarks*"))
1302 (let ((format "%-40s %s")
1303 (inhibit-read-only t)
1306 (setq header-line-format (concat " " (format format "URL" "Title")))
1307 (dolist (bookmark eww-bookmarks)
1308 (setq start (point))
1309 (setq url (plist-get bookmark :url))
1310 (when (> (length url) 40)
1311 (setq url (substring url 0 40)))
1312 (insert (format format url
1313 (plist-get bookmark :title))
1315 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1316 (goto-char (point-min))))
1318 (defvar eww-bookmark-kill-ring nil)
1320 (defun eww-bookmark-kill ()
1321 "Kill the current bookmark."
1323 (let* ((start (line-beginning-position))
1324 (bookmark (get-text-property start 'eww-bookmark))
1325 (inhibit-read-only t))
1327 (user-error "No bookmark on the current line"))
1329 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1330 (delete-region start (point))
1331 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1332 (eww-write-bookmarks)))
1334 (defun eww-bookmark-yank ()
1335 "Yank a previously killed bookmark to the current line."
1337 (unless eww-bookmark-kill-ring
1338 (user-error "No previously killed bookmark"))
1340 (let ((inhibit-read-only t)
1343 (insert (pop eww-bookmark-kill-ring))
1344 (setq bookmark (get-text-property start 'eww-bookmark))
1345 (if (= start (point-min))
1346 (push bookmark eww-bookmarks)
1347 (let ((line (count-lines start (point))))
1348 (setcdr (nthcdr (1- line) eww-bookmarks)
1349 (cons bookmark (nthcdr line eww-bookmarks)))))
1350 (eww-write-bookmarks)))
1352 (defun eww-bookmark-browse ()
1353 "Browse the bookmark under point in eww."
1355 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1357 (user-error "No bookmark on the current line"))
1359 (eww-browse-url (plist-get bookmark :url))))
1361 (defun eww-next-bookmark ()
1362 "Go to the next bookmark in the list."
1366 (unless (get-buffer "*eww bookmarks*")
1368 (eww-bookmark-prepare))
1369 (with-current-buffer (get-buffer "*eww bookmarks*")
1370 (when (and (not first)
1373 (setq bookmark (get-text-property (line-beginning-position)
1376 (user-error "No next bookmark")))
1377 (eww-browse-url (plist-get bookmark :url))))
1379 (defun eww-previous-bookmark ()
1380 "Go to the previous bookmark in the list."
1384 (unless (get-buffer "*eww bookmarks*")
1386 (eww-bookmark-prepare))
1387 (with-current-buffer (get-buffer "*eww bookmarks*")
1389 (goto-char (point-max))
1390 (beginning-of-line))
1391 ;; On the final line.
1395 (user-error "No previous bookmark")
1397 (setq bookmark (get-text-property (line-beginning-position)
1399 (eww-browse-url (plist-get bookmark :url))))
1401 (defvar eww-bookmark-mode-map
1402 (let ((map (make-sparse-keymap)))
1403 (suppress-keymap map)
1404 (define-key map "q" 'quit-window)
1405 (define-key map [(control k)] 'eww-bookmark-kill)
1406 (define-key map [(control y)] 'eww-bookmark-yank)
1407 (define-key map "\r" 'eww-bookmark-browse)
1409 (easy-menu-define nil map
1410 "Menu for `eww-bookmark-mode-map'."
1412 ["Exit" quit-window t]
1413 ["Browse" eww-bookmark-browse
1414 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1415 ["Kill" eww-bookmark-kill
1416 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1417 ["Yank" eww-bookmark-yank
1418 :active eww-bookmark-kill-ring]))
1421 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1422 "Mode for listing bookmarks.
1424 \\{eww-bookmark-mode-map}"
1425 (buffer-disable-undo)
1426 (setq buffer-read-only t
1431 (defun eww-save-history ()
1432 (plist-put eww-data :point (point))
1433 (plist-put eww-data :text (buffer-string))
1434 (push eww-data eww-history)
1435 (setq eww-data (list :title ""))
1436 ;; Don't let the history grow infinitely. We store quite a lot of
1438 (when-let (tail (nthcdr 50 eww-history))
1441 (defun eww-list-histories ()
1442 "List the eww-histories."
1444 (when (null eww-history)
1445 (error "No eww-histories are defined"))
1446 (let ((eww-history-trans eww-history))
1447 (set-buffer (get-buffer-create "*eww history*"))
1449 (let ((inhibit-read-only t)
1452 url title format start)
1454 (dolist (history eww-history-trans)
1455 (setq start (point))
1456 (setq domain-length (max domain-length (length (plist-get history :url))))
1457 (setq title-length (max title-length (length (plist-get history :title)))))
1458 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1460 (concat " " (format format "Title" "URL")))
1461 (dolist (history eww-history-trans)
1462 (setq start (point))
1463 (setq url (plist-get history :url))
1464 (setq title (plist-get history :title))
1465 (insert (format format title url))
1467 (put-text-property start (1+ start) 'eww-history history))
1468 (goto-char (point-min)))
1469 (pop-to-buffer "*eww history*")))
1471 (defun eww-history-browse ()
1472 "Browse the history under point in eww."
1474 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1476 (error "No history on the current line"))
1478 (eww-restore-history history)))
1480 (defvar eww-history-mode-map
1481 (let ((map (make-sparse-keymap)))
1482 (suppress-keymap map)
1483 (define-key map "q" 'quit-window)
1484 (define-key map "\r" 'eww-history-browse)
1485 ;; (define-key map "n" 'next-error-no-select)
1486 ;; (define-key map "p" 'previous-error-no-select)
1488 (easy-menu-define nil map
1489 "Menu for `eww-history-mode-map'."
1491 ["Exit" quit-window t]
1492 ["Browse" eww-history-browse
1493 :active (get-text-property (line-beginning-position) 'eww-history)]))
1496 (define-derived-mode eww-history-mode nil "eww history"
1497 "Mode for listing eww-histories.
1499 \\{eww-history-mode-map}"
1500 (buffer-disable-undo)
1501 (setq buffer-read-only t
1506 ;;; eww.el ends here