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-checkbox
104 '((((type x w32 ns
) (class color
)) ; Like default mode line
105 :box
(:line-width
2 :style released-button
)
106 :background
"lightgrey" :foreground
"black"))
107 "Face for eww buffer buttons."
111 (defface eww-form-select
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-text
120 '((t (:background
"#505050"
122 :box
(:line-width
1))))
123 "Face for eww text inputs."
127 (defface eww-form-textarea
128 '((t (:background
"#C0C0C0"
130 :box
(:line-width
1))))
131 "Face for eww textarea inputs."
135 (defvar eww-data nil
)
136 (defvar eww-history nil
)
137 (defvar eww-history-position
0)
139 (defvar eww-next-url nil
)
140 (defvar eww-previous-url nil
)
141 (defvar eww-up-url nil
)
142 (defvar eww-home-url nil
)
143 (defvar eww-start-url nil
)
144 (defvar eww-contents-url nil
)
146 (defvar eww-local-regex
"localhost"
147 "When this regex is found in the URL, it's not a keyword but an address.")
149 (defvar eww-link-keymap
150 (let ((map (copy-keymap shr-map
)))
151 (define-key map
"\r" 'eww-follow-link
)
156 "Fetch URL and render the page.
157 If the input doesn't look like an URL or a domain name, the
158 word(s) will be searched for via `eww-search-prefix'."
159 (interactive "sEnter URL or keywords: ")
160 (setq url
(string-trim url
))
161 (cond ((string-match-p "\\`file:/" url
))
162 ((string-match-p "\\`ftp://" url
)
163 (user-error "FTP is not supported."))
165 (if (and (= (length (split-string url
)) 1)
166 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
167 (> (length (split-string url
"[.:]")) 1))
168 (string-match eww-local-regex url
)))
170 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
171 (setq url
(concat "http://" url
)))
172 ;; some site don't redirect final /
173 (when (string= (url-filename (url-generic-parse-url url
)) "")
174 (setq url
(concat url
"/"))))
175 (setq url
(concat eww-search-prefix
176 (replace-regexp-in-string " " "+" url
))))))
177 (url-retrieve url
'eww-render
(list url
)))
179 ;;;###autoload (defalias 'browse-web 'eww)
182 (defun eww-open-file (file)
183 "Render a file using EWW."
184 (interactive "fFile: ")
185 (eww (concat "file://"
186 (and (memq system-type
'(windows-nt ms-dos
))
188 (expand-file-name file
))))
190 (defun eww-render (status url
&optional point
)
191 (let ((redirect (plist-get status
:redirect
)))
193 (setq url redirect
)))
194 (let* ((headers (eww-parse-headers))
196 (mail-header-parse-content-type
197 (or (cdr (assoc "content-type" headers
))
201 (or (cdr (assq 'charset
(cdr content-type
)))
202 (eww-detect-charset (equal (car content-type
)
205 (data-buffer (current-buffer)))
208 (plist-put eww-data
:title
"")
210 ((and eww-use-external-browser-for-content-type
211 (string-match-p eww-use-external-browser-for-content-type
213 (eww-browse-with-external-browser url
))
214 ((equal (car content-type
) "text/html")
215 (eww-display-html charset url nil point
))
216 ((equal (car content-type
) "application/pdf")
218 ((string-match-p "\\`image/" (car content-type
))
220 (eww-update-header-line-format))
223 (eww-update-header-line-format)))
224 (plist-put eww-data
:title url
)
225 (setq eww-history-position
0))
226 (kill-buffer data-buffer
))))
228 (defun eww-parse-headers ()
230 (goto-char (point-min))
231 (while (and (not (eobp))
233 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
234 (push (cons (downcase (match-string 1))
242 (defun eww-detect-charset (html-p)
243 (let ((case-fold-search t
)
247 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
251 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
254 (declare-function libxml-parse-html-region
"xml.c"
255 (start end
&optional base-url
))
257 (defun eww-display-html (charset url
&optional document point
)
258 (or (fboundp 'libxml-parse-html-region
)
259 (error "This function requires Emacs to be compiled with libxml2"))
260 ;; There should be a better way to abort loading images
266 'base
(list (cons 'href url
))
268 (unless (eq charset
'utf-8
)
270 (decode-coding-region (point) (point-max) charset
)
271 (coding-system-error nil
)))
272 (libxml-parse-html-region (point) (point-max))))))
273 (source (and (null document
)
274 (buffer-substring (point) (point-max)))))
276 (plist-put eww-data
:source source
)
277 (plist-put eww-data
:dom document
)
278 (let ((inhibit-read-only t
)
279 (after-change-functions nil
)
280 (shr-target-id (url-target (url-generic-parse-url url
)))
281 (shr-external-rendering-functions
282 '((title . eww-tag-title
)
283 (form . eww-tag-form
)
284 (input . eww-tag-input
)
285 (textarea . eww-tag-textarea
)
286 (body . eww-tag-body
)
287 (select . eww-tag-select
)
288 (link . eww-tag-link
)
290 (shr-insert-document document
)
295 (goto-char (point-min))
296 (let ((point (next-single-property-change
297 (point-min) 'shr-target-id
)))
301 (goto-char (point-min))
302 ;; Don't leave point inside forms, because the normal eww
303 ;; commands aren't available there.
304 (while (and (not (eobp))
305 (get-text-property (point) 'eww-form
))
307 (plist-put eww-data
:url url
)
308 (setq eww-history-position
0)
309 (eww-update-header-line-format)))
311 (defun eww-handle-link (cont)
312 (let* ((rel (assq :rel cont
))
313 (href (assq :href cont
))
315 ;; The text associated with :rel is case-insensitive.
316 (if rel
(downcase (cdr rel
)))
317 '(("next" . eww-next-url
)
318 ;; Texinfo uses "previous", but HTML specifies
319 ;; "prev", so recognize both.
320 ("previous" . eww-previous-url
)
321 ("prev" . eww-previous-url
)
322 ;; HTML specifies "start" but also "contents",
323 ;; and Gtk seems to use "home". Recognize
324 ;; them all; but store them in different
325 ;; variables so that we can readily choose the
327 ("start" . eww-start-url
)
328 ("home" . eww-home-url
)
329 ("contents" . eww-contents-url
)
330 ("up" . eww-up-url
)))))
333 (set (cdr where
) (cdr href
)))))
335 (defun eww-tag-link (cont)
336 (eww-handle-link cont
)
339 (defun eww-tag-a (cont)
340 (eww-handle-link cont
)
341 (let ((start (point)))
343 (put-text-property start
(point) 'keymap eww-link-keymap
)))
345 (defun eww-update-header-line-format ()
346 (if eww-header-line-format
347 (setq header-line-format
348 (replace-regexp-in-string
350 ;; FIXME? Title can be blank. Default to, eg, last component
352 (format-spec eww-header-line-format
353 `((?u .
,(plist-get eww-data
:url
))
354 (?t .
,(or (plist-get eww-data
:title
) ""))))))
355 (setq header-line-format nil
)))
357 (defun eww-tag-title (cont)
360 (when (eq (car sub
) 'text
)
361 (setq title
(concat title
(cdr sub
)))))
362 (plist-put eww-data
:title
363 (replace-regexp-in-string
365 (replace-regexp-in-string "[ \t\r\n]+" " " title
))))
366 (eww-update-header-line-format))
368 (defun eww-tag-body (cont)
369 (let* ((start (point))
370 (fgcolor (cdr (or (assq :fgcolor cont
)
372 (bgcolor (cdr (assq :bgcolor cont
)))
373 (shr-stylesheet (list (cons 'color fgcolor
)
374 (cons 'background-color bgcolor
))))
376 (shr-colorize-region start
(point) fgcolor bgcolor
)))
378 (defun eww-display-raw ()
379 (let ((data (buffer-substring (point) (point-max))))
381 (let ((inhibit-read-only t
))
383 (goto-char (point-min))))
385 (defun eww-display-image ()
386 (let ((data (shr-parse-image-data)))
388 (let ((inhibit-read-only t
))
389 (shr-put-image data nil
))
390 (goto-char (point-min))))
392 (defun eww-display-pdf ()
393 (let ((data (buffer-substring (point) (point-max))))
394 (switch-to-buffer (get-buffer-create "*eww pdf*"))
395 (let ((coding-system-for-write 'raw-text
)
396 (inhibit-read-only t
))
400 (goto-char (point-min)))
402 (defun eww-setup-buffer ()
403 (switch-to-buffer (get-buffer-create "*eww*"))
404 (let ((inhibit-read-only t
))
407 (unless (eq major-mode
'eww-mode
)
409 (setq-local eww-next-url nil
)
410 (setq-local eww-previous-url nil
)
411 (setq-local eww-up-url nil
)
412 (setq-local eww-home-url nil
)
413 (setq-local eww-start-url nil
)
414 (setq-local eww-contents-url nil
))
416 (defun eww-view-source ()
417 "View the HTML source code of the current page."
419 (let ((buf (get-buffer-create "*eww-source*"))
420 (source (plist-get eww-data
:source
)))
421 (with-current-buffer buf
422 (delete-region (point-min) (point-max))
423 (insert (or source
"no source"))
424 (goto-char (point-min))
425 (when (fboundp 'html-mode
)
429 (defun eww-readable ()
430 "View the main \"readable\" parts of the current web page.
431 This command uses heuristics to find the parts of the web page that
432 contains the main textual portion, leaving out navigation menus and
435 (let* ((source (plist-get eww-data
:source
))
436 (dom (shr-transform-dom
440 (decode-coding-region (point-min) (point-max) 'utf-8
)
441 (coding-system-error nil
))
442 (libxml-parse-html-region (point-min) (point-max))))))
443 (eww-score-readability dom
)
445 (eww-display-html nil nil
447 (eww-highest-readability dom
)))
448 (plist-put eww-data
:source source
)))
450 (defun eww-score-readability (node)
453 ((memq (car node
) '(script head comment
))
455 ((eq (car node
) 'meta
)
457 ((eq (car node
) 'img
)
460 (setq score
(- (length (split-string
461 (or (cdr (assoc 'text
(cdr node
))) ""))))))
463 (dolist (elem (cdr node
))
465 ((and (stringp (cdr elem
))
466 (eq (car elem
) 'text
))
467 (setq score
(+ score
(length (split-string (cdr elem
))))))
470 (or (cdr (assoc :eww-readability-score
(cdr elem
)))
471 (eww-score-readability elem
)))))))))
472 ;; Cache the score of the node to avoid recomputing all the time.
473 (setcdr node
(cons (cons :eww-readability-score score
) (cdr node
)))
476 (defun eww-highest-readability (node)
479 (dolist (elem (cdr node
))
480 (when (and (consp (cdr elem
))
482 :eww-readability-score
484 (eww-highest-readability elem
))))
485 most-negative-fixnum
)
486 (or (cdr (assoc :eww-readability-score
(cdr result
)))
487 most-negative-fixnum
)))
488 (setq result highest
)))
492 (let ((map (make-sparse-keymap)))
493 (suppress-keymap map
)
494 (define-key map
"q" 'quit-window
)
495 (define-key map
"g" 'eww-reload
)
496 (define-key map
[?
\t] 'shr-next-link
)
497 (define-key map
[?\M-
\t] 'shr-previous-link
)
498 (define-key map
[delete] 'scroll-down-command)
499 (define-key map [?\S-\ ] 'scroll-down-command)
500 (define-key map "\177" 'scroll-down-command)
501 (define-key map " " 'scroll-up-command)
502 (define-key map "l" 'eww-back-url)
503 (define-key map "r" 'eww-forward-url)
504 (define-key map "n" 'eww-next-url)
505 (define-key map "p" 'eww-previous-url)
506 (define-key map "u" 'eww-up-url)
507 (define-key map "t" 'eww-top-url)
508 (define-key map "&" 'eww-browse-with-external-browser)
509 (define-key map "d" 'eww-download)
510 (define-key map "w" 'eww-copy-page-url)
511 (define-key map "C" 'url-cookie-list)
512 (define-key map "v" 'eww-view-source)
513 (define-key map "R" 'eww-readable)
514 (define-key map "H" 'eww-list-histories)
516 (define-key map "b" 'eww-add-bookmark)
517 (define-key map "B" 'eww-list-bookmarks)
518 (define-key map [(meta n)] 'eww-next-bookmark)
519 (define-key map [(meta p)] 'eww-previous-bookmark)
521 (easy-menu-define nil map ""
523 ["Exit" quit-window t]
524 ["Close browser" quit-window t]
525 ["Reload" eww-reload t]
526 ["Back to previous page" eww-back-url
527 :active (not (zerop (length eww-history)))]
528 ["Forward to next page" eww-forward-url
529 :active (not (zerop eww-history-position))]
530 ["Browse with external browser" eww-browse-with-external-browser t]
531 ["Download" eww-download t]
532 ["View page source" eww-view-source]
533 ["Copy page URL" eww-copy-page-url t]
534 ["List histories" eww-list-histories t]
535 ["Add bookmark" eww-add-bookmark t]
536 ["List bookmarks" eww-list-bookmarks t]
537 ["List cookies" url-cookie-list t]))
540 (defvar eww-tool-bar-map
541 (let ((map (make-sparse-keymap)))
542 (dolist (tool-bar-item
543 '((quit-window . "close")
544 (eww-reload . "refresh")
545 (eww-back-url . "left-arrow")
546 (eww-forward-url . "right-arrow")
547 (eww-view-source . "show")
548 (eww-copy-page-url . "copy")
549 (eww-add-bookmark . "bookmark_add"))) ;; ...
550 (tool-bar-local-item-from-menu
551 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
553 "Tool bar for `eww-mode'.")
555 (define-derived-mode eww-mode nil "eww"
556 "Mode for browsing the web.
559 (setq-local eww-data (list :title ""))
560 (setq-local browse-url-browser-function 'eww-browse-url)
561 (setq-local after-change-functions 'eww-process-text-input)
562 (setq-local eww-history nil)
563 (setq-local eww-history-position 0)
564 (when (boundp 'tool-bar-map)
565 (setq-local tool-bar-map eww-tool-bar-map))
566 (buffer-disable-undo)
567 ;;(setq buffer-read-only t)
571 (defun eww-browse-url (url &optional _new-window)
572 (when (and (equal major-mode 'eww-mode)
573 (plist-get eww-data :url))
577 (defun eww-back-url ()
578 "Go to the previously displayed page."
580 (when (>= eww-history-position (length eww-history))
581 (user-error "No previous page"))
583 (setq eww-history-position (+ eww-history-position 2))
584 (eww-restore-history (elt eww-history (1- eww-history-position))))
586 (defun eww-forward-url ()
587 "Go to the next displayed page."
589 (when (zerop eww-history-position)
590 (user-error "No next page"))
592 (eww-restore-history (elt eww-history (1- eww-history-position))))
594 (defun eww-restore-history (elem)
595 (let ((inhibit-read-only t))
597 (insert (plist-get elem :text))
598 (goto-char (plist-get elem :point))
600 (eww-update-header-line-format)))
602 (defun eww-next-url ()
603 "Go to the page marked `next'.
604 A page is marked `next' if rel=\"next\" appears in a <link>
608 (eww-browse-url (shr-expand-url eww-next-url (plist-get eww-data :url)))
609 (user-error "No `next' on this page")))
611 (defun eww-previous-url ()
612 "Go to the page marked `previous'.
613 A page is marked `previous' if rel=\"previous\" appears in a <link>
617 (eww-browse-url (shr-expand-url eww-previous-url
618 (plist-get eww-data :url)))
619 (user-error "No `previous' on this page")))
622 "Go to the page marked `up'.
623 A page is marked `up' if rel=\"up\" appears in a <link>
627 (eww-browse-url (shr-expand-url eww-up-url (plist-get eww-data :url)))
628 (user-error "No `up' on this page")))
630 (defun eww-top-url ()
631 "Go to the page marked `top'.
632 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
633 appears in a <link> or <a> tag."
635 (let ((best-url (or eww-start-url
639 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
640 (user-error "No `top' for this page"))))
643 "Reload the current page."
645 (let ((url (plist-get eww-data :url)))
646 (url-retrieve url 'eww-render (list url (point)))))
650 (defvar eww-form nil)
652 (defvar eww-submit-map
653 (let ((map (make-sparse-keymap)))
654 (define-key map "\r" 'eww-submit)
655 (define-key map [(control c) (control c)] 'eww-submit)
658 (defvar eww-checkbox-map
659 (let ((map (make-sparse-keymap)))
660 (define-key map " " 'eww-toggle-checkbox)
661 (define-key map "\r" 'eww-toggle-checkbox)
662 (define-key map [(control c) (control c)] 'eww-submit)
666 (let ((map (make-keymap)))
667 (set-keymap-parent map text-mode-map)
668 (define-key map "\r" 'eww-submit)
669 (define-key map [(control a)] 'eww-beginning-of-text)
670 (define-key map [(control c) (control c)] 'eww-submit)
671 (define-key map [(control e)] 'eww-end-of-text)
672 (define-key map [?\t] 'shr-next-link)
673 (define-key map [?\M-\t] 'shr-previous-link)
676 (defvar eww-textarea-map
677 (let ((map (make-keymap)))
678 (set-keymap-parent map text-mode-map)
679 (define-key map "\r" 'forward-line)
680 (define-key map [(control c) (control c)] 'eww-submit)
681 (define-key map [?\t] 'shr-next-link)
682 (define-key map [?\M-\t] 'shr-previous-link)
685 (defvar eww-select-map
686 (let ((map (make-sparse-keymap)))
687 (define-key map "\r" 'eww-change-select)
688 (define-key map [(control c) (control c)] 'eww-submit)
691 (defun eww-beginning-of-text ()
692 "Move to the start of the input field."
694 (goto-char (eww-beginning-of-field)))
696 (defun eww-end-of-text ()
697 "Move to the end of the text in the input field."
699 (goto-char (eww-end-of-field))
700 (let ((start (eww-beginning-of-field)))
701 (while (and (equal (following-char) ? )
704 (when (> (point) start)
707 (defun eww-beginning-of-field ()
711 ((not (eq (get-text-property (point) 'eww-form)
712 (get-text-property (1- (point)) 'eww-form)))
715 (previous-single-property-change
716 (point) 'eww-form nil (point-min)))))
718 (defun eww-end-of-field ()
719 (1- (next-single-property-change
720 (point) 'eww-form nil (point-max))))
722 (defun eww-tag-form (cont)
724 (list (assq :method cont)
725 (assq :action cont)))
727 (shr-ensure-paragraph)
732 (when (> (point) start)
733 (put-text-property start (1+ start)
734 'eww-form eww-form))))
736 (defun eww-form-submit (cont)
737 (let ((start (point))
738 (value (cdr (assq :value cont))))
740 (if (zerop (length value))
744 (add-face-text-property start (point) 'eww-form-submit)
745 (put-text-property start (point) 'eww-form
746 (list :eww-form eww-form
749 :name (cdr (assq :name cont))))
750 (put-text-property start (point) 'keymap eww-submit-map)
753 (defun eww-form-checkbox (cont)
754 (let ((start (point)))
755 (if (cdr (assq :checked cont))
756 (insert eww-form-checkbox-selected-symbol)
757 (insert eww-form-checkbox-symbol))
758 (add-face-text-property start (point) 'eww-form-checkbox)
759 (put-text-property start (point) 'eww-form
760 (list :eww-form eww-form
761 :value (cdr (assq :value cont))
762 :type (downcase (cdr (assq :type cont)))
763 :checked (cdr (assq :checked cont))
764 :name (cdr (assq :name cont))))
765 (put-text-property start (point) 'keymap eww-checkbox-map)
768 (defun eww-form-text (cont)
769 (let ((start (point))
770 (type (downcase (or (cdr (assq :type cont))
772 (value (or (cdr (assq :value cont)) ""))
773 (width (string-to-number
774 (or (cdr (assq :size cont))
776 (readonly-property (if (or (cdr (assq :disabled cont))
777 (cdr (assq :readonly cont)))
779 'inhibit-read-only)))
781 (when (< (length value) width)
782 (insert (make-string (- width (length value)) ? )))
783 (put-text-property start (point) 'face 'eww-form-text)
784 (put-text-property start (point) 'local-map eww-text-map)
785 (put-text-property start (point) readonly-property t)
786 (put-text-property start (point) 'eww-form
787 (list :eww-form eww-form
790 :name (cdr (assq :name cont))))
793 (defconst eww-text-input-types '("text" "password" "textarea"
794 "color" "date" "datetime" "datetime-local"
795 "email" "month" "number" "search" "tel"
797 "List of input types which represent a text input.
798 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
800 (defun eww-process-text-input (beg end length)
801 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
802 (properties (text-properties-at end))
803 (type (plist-get form :type)))
805 (member type eww-text-input-types))
808 ;; Delete some space at the end.
811 (if (equal type "textarea")
812 (1- (line-end-position))
814 (let ((new (- end beg)))
815 (while (and (> new 0)
816 (eql (following-char) ? ))
817 (delete-region (point) (1+ (point)))
818 (setq new (1- new))))
819 (set-text-properties beg end properties)))
824 (if (equal type "textarea")
825 (1- (line-end-position))
827 (let ((start (point)))
828 (insert (make-string length ? ))
829 (set-text-properties start (point) properties)))))
830 (let ((value (buffer-substring-no-properties
831 (eww-beginning-of-field)
832 (eww-end-of-field))))
833 (when (string-match " +\\'" value)
834 (setq value (substring value 0 (match-beginning 0))))
835 (plist-put form :value value)
836 (when (equal type "password")
837 ;; Display passwords as asterisks.
838 (let ((start (eww-beginning-of-field)))
839 (put-text-property start (+ start (length value))
840 'display (make-string (length value) ?*))))))))
842 (defun eww-tag-textarea (cont)
843 (let ((start (point))
844 (value (or (cdr (assq :value cont)) ""))
845 (lines (string-to-number
846 (or (cdr (assq :rows cont))
848 (width (string-to-number
849 (or (cdr (assq :cols cont))
855 (when (< (count-lines start (point)) lines)
856 (dotimes (i (- lines (count-lines start (point))))
858 (setq end (point-marker))
860 (while (< (point) end)
862 (let ((pad (- width (- (point) (line-beginning-position)))))
864 (insert (make-string pad ? ))))
865 (add-face-text-property (line-beginning-position)
866 (point) 'eww-form-textarea)
867 (put-text-property (line-beginning-position) (point)
868 'local-map eww-textarea-map)
870 (put-text-property start (point) 'eww-form
871 (list :eww-form eww-form
874 :name (cdr (assq :name cont))))))
876 (defun eww-tag-input (cont)
877 (let ((type (downcase (or (cdr (assq :type cont))
881 ((or (equal type "checkbox")
882 (equal type "radio"))
883 (eww-form-checkbox cont))
884 ((equal type "submit")
885 (eww-form-submit cont))
886 ((equal type "hidden")
887 (let ((form eww-form)
888 (name (cdr (assq :name cont))))
889 ;; Don't add <input type=hidden> elements repeatedly.
891 (or (not (consp (car form)))
892 (not (eq (caar form) 'hidden))
893 (not (equal (plist-get (cdr (car form)) :name)
895 (setq form (cdr form)))
897 (nconc eww-form (list
900 :value (cdr (assq :value cont))))))))
902 (eww-form-text cont)))
903 (unless (= start (point))
904 (put-text-property start (1+ start) 'help-echo "Input field"))))
906 (defun eww-tag-select (cont)
907 (shr-ensure-paragraph)
908 (let ((menu (list :name (cdr (assq :name cont))
914 (if (eq (car (car cont)) 'optgroup)
915 (dolist (groupelem cont)
916 (unless (cdr (assq :disabled (cdr groupelem)))
917 (setq opelem (append opelem (cdr (cdr groupelem))))))
919 (dolist (elem opelem)
920 (when (eq (car elem) 'option)
921 (when (cdr (assq :selected (cdr elem)))
922 (nconc menu (list :value
923 (cdr (assq :value (cdr elem))))))
924 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
925 (setq max (max max (length display)))
927 :value (cdr (assq :value (cdr elem)))
931 (setq options (nreverse options))
932 ;; If we have no selected values, default to the first value.
933 (unless (plist-get menu :value)
934 (nconc menu (list :value (nth 2 (car options)))))
936 (let ((selected (eww-select-display menu)))
938 (make-string (- max (length selected)) ? )))
939 (put-text-property start (point) 'eww-form menu)
940 (add-face-text-property start (point) 'eww-form-select)
941 (put-text-property start (point) 'keymap eww-select-map)
942 (unless (= start (point))
943 (put-text-property start (1+ start) 'help-echo "select field"))
944 (shr-ensure-paragraph))))
946 (defun eww-select-display (select)
947 (let ((value (plist-get select :value))
949 (dolist (elem select)
950 (when (and (consp elem)
951 (eq (car elem) 'item)
952 (equal value (plist-get (cdr elem) :value)))
953 (setq display (plist-get (cdr elem) :display))))
956 (defun eww-change-select ()
957 "Change the value of the select drop-down menu under point."
959 (let* ((input (get-text-property (point) 'eww-form))
960 (completion-ignore-case t)
963 (mapcar (lambda (elem)
965 (eq (car elem) 'item)
966 (cons (plist-get (cdr elem) :display)
967 (plist-get (cdr elem) :value))))
970 (completing-read "Change value: " options nil 'require-match))
971 (inhibit-read-only t))
972 (plist-put input :value (cdr (assoc-string display options t)))
974 (eww-update-field display))))
976 (defun eww-update-field (string)
977 (let ((properties (text-properties-at (point)))
978 (start (eww-beginning-of-field))
979 (end (1+ (eww-end-of-field))))
980 (delete-region start end)
982 (make-string (- (- end start) (length string)) ? ))
983 (set-text-properties start end properties)
986 (defun eww-toggle-checkbox ()
987 "Toggle the value of the checkbox under point."
989 (let* ((input (get-text-property (point) 'eww-form))
990 (type (plist-get input :type)))
991 (if (equal type "checkbox")
994 (if (plist-get input :checked)
996 (plist-put input :checked nil)
997 (eww-update-field eww-form-checkbox-symbol))
998 (plist-put input :checked t)
999 (eww-update-field eww-form-checkbox-selected-symbol))))
1000 ;; Radio button. Switch all other buttons off.
1001 (let ((name (plist-get input :name)))
1003 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1004 (when (equal (plist-get (cdr elem) :name) name)
1005 (goto-char (car elem))
1006 (if (not (eq (cdr elem) input))
1008 (plist-put input :checked nil)
1009 (eww-update-field eww-form-checkbox-symbol))
1010 (plist-put input :checked t)
1011 (eww-update-field eww-form-checkbox-selected-symbol)))))
1012 (forward-char 1)))))
1014 (defun eww-inputs (form)
1015 (let ((start (point-min))
1018 (< start (point-max)))
1019 (when (or (get-text-property start 'eww-form)
1020 (setq start (next-single-property-change start 'eww-form)))
1021 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1023 (push (cons start (get-text-property start 'eww-form))
1025 (setq start (next-single-property-change start 'eww-form))))
1028 (defun eww-input-value (input)
1029 (let ((type (plist-get input :type))
1030 (value (plist-get input :value)))
1032 ((equal type "textarea")
1035 (goto-char (point-min))
1036 (while (re-search-forward "^ +\n\\| +$" nil t)
1037 (replace-match "" t t))
1040 (if (string-match " +\\'" value)
1041 (substring value 0 (match-beginning 0))
1044 (defun eww-submit ()
1045 "Submit the current form."
1047 (let* ((this-input (get-text-property (point) 'eww-form))
1048 (form (plist-get this-input :eww-form))
1050 (dolist (elem (sort (eww-inputs form)
1052 (< (car o1) (car o2)))))
1053 (let* ((input (cdr elem))
1054 (input-start (car elem))
1055 (name (plist-get input :name)))
1058 ((member (plist-get input :type) '("checkbox" "radio"))
1059 (when (plist-get input :checked)
1060 (push (cons name (plist-get input :value))
1062 ((equal (plist-get input :type) "submit")
1063 ;; We want the values from buttons if we hit a button if
1064 ;; we hit enter on it, or if it's the first button after
1065 ;; the field we did hit return on.
1066 (when (or (eq input this-input)
1067 (and (not (eq input this-input))
1069 (> input-start (point))))
1070 (setq next-submit t)
1071 (push (cons name (plist-get input :value))
1074 (push (cons name (eww-input-value input))
1077 (when (and (consp elem)
1078 (eq (car elem) 'hidden))
1079 (push (cons (plist-get (cdr elem) :name)
1080 (or (plist-get (cdr elem) :value) ""))
1082 (if (and (stringp (cdr (assq :method form)))
1083 (equal (downcase (cdr (assq :method form))) "post"))
1084 (let ((url-request-method "POST")
1085 (url-request-extra-headers
1086 '(("Content-Type" . "application/x-www-form-urlencoded")))
1087 (url-request-data (mm-url-encode-www-form-urlencoded values)))
1088 (eww-browse-url (shr-expand-url (cdr (assq :action form))
1089 (plist-get eww-data :url))))
1092 (if (cdr (assq :action form))
1093 (shr-expand-url (cdr (assq :action form))
1094 (plist-get eww-data :url))
1095 (plist-get eww-data :url))
1097 (mm-url-encode-www-form-urlencoded values))))))
1099 (defun eww-browse-with-external-browser (&optional url)
1100 "Browse the current URL with an external browser.
1101 The browser to used is specified by the `shr-external-browser' variable."
1103 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1105 (defun eww-follow-link (&optional external mouse-event)
1106 "Browse the URL under point.
1107 If EXTERNAL, browse the URL using `shr-external-browser'."
1108 (interactive (list current-prefix-arg last-nonmenu-event))
1109 (mouse-set-point mouse-event)
1110 (let ((url (get-text-property (point) 'shr-url)))
1113 (message "No link under point"))
1114 ((string-match "^mailto:" url)
1115 (browse-url-mail url))
1117 (funcall shr-external-browser url))
1118 ;; This is a #target url in the same page as the current one.
1119 ((and (url-target (url-generic-parse-url url))
1120 (eww-same-page-p url (plist-get eww-data :url)))
1122 (eww-display-html 'utf-8 url (plist-get eww-data :url)))
1124 (eww-browse-url url)))))
1126 (defun eww-same-page-p (url1 url2)
1127 "Return non-nil if both URLs represent the same page.
1128 Differences in #targets are ignored."
1129 (let ((obj1 (url-generic-parse-url url1))
1130 (obj2 (url-generic-parse-url url2)))
1131 (setf (url-target obj1) nil)
1132 (setf (url-target obj2) nil)
1133 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1135 (defun eww-copy-page-url ()
1137 (message "%s" (plist-get eww-data :url))
1138 (kill-new (plist-get eww-data :url)))
1140 (defun eww-download ()
1141 "Download URL under point to `eww-download-directory'."
1143 (let ((url (get-text-property (point) 'shr-url)))
1145 (message "No URL under point")
1146 (url-retrieve url 'eww-download-callback (list url)))))
1148 (defun eww-download-callback (status url)
1149 (unless (plist-get status :error)
1150 (let* ((obj (url-generic-parse-url url))
1151 (path (car (url-path-and-query obj)))
1152 (file (eww-make-unique-file-name (file-name-nondirectory path)
1153 eww-download-directory)))
1155 (message "Saved %s" file))))
1157 (defun eww-make-unique-file-name (file directory)
1159 ((zerop (length file))
1161 ((string-match "\\`[.]" file)
1162 (setq file (concat "!" file))))
1164 (while (file-exists-p (expand-file-name file directory))
1166 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1167 (format "%s(%d)%s" (match-string 1 file)
1168 count (match-string 2 file))
1169 (format "%s(%d)" file count)))
1170 (setq count (1+ count)))
1171 (expand-file-name file directory)))
1175 (defvar eww-bookmarks nil)
1177 (defun eww-add-bookmark ()
1178 "Add the current page to the bookmarks."
1180 (eww-read-bookmarks)
1181 (dolist (bookmark eww-bookmarks)
1182 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1183 (user-error "Already bookmarked")))
1184 (if (y-or-n-p "bookmark this page? ")
1186 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1187 (plist-get eww-data :url))))
1188 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1189 (push (list :url (plist-get eww-data :url)
1191 :time (current-time-string))
1193 (eww-write-bookmarks)
1194 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1195 (plist-get eww-data :title)))))
1197 (defun eww-write-bookmarks ()
1198 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1199 (insert ";; Auto-generated file; don't edit\n")
1200 (pp eww-bookmarks (current-buffer))))
1202 (defun eww-read-bookmarks ()
1203 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1205 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1207 (insert-file-contents file)
1208 (read (current-buffer)))))))
1211 (defun eww-list-bookmarks ()
1212 "Display the bookmarks."
1214 (eww-bookmark-prepare)
1215 (pop-to-buffer "*eww bookmarks*"))
1217 (defun eww-bookmark-prepare ()
1218 (eww-read-bookmarks)
1219 (unless eww-bookmarks
1220 (user-error "No bookmarks are defined"))
1221 (set-buffer (get-buffer-create "*eww bookmarks*"))
1223 (let ((format "%-40s %s")
1224 (inhibit-read-only t)
1227 (setq header-line-format (concat " " (format format "URL" "Title")))
1228 (dolist (bookmark eww-bookmarks)
1229 (setq start (point))
1230 (setq url (plist-get bookmark :url))
1231 (when (> (length url) 40)
1232 (setq url (substring url 0 40)))
1233 (insert (format format url
1234 (plist-get bookmark :title))
1236 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1237 (goto-char (point-min))))
1239 (defvar eww-bookmark-kill-ring nil)
1241 (defun eww-bookmark-kill ()
1242 "Kill the current bookmark."
1244 (let* ((start (line-beginning-position))
1245 (bookmark (get-text-property start 'eww-bookmark))
1246 (inhibit-read-only t))
1248 (user-error "No bookmark on the current line"))
1250 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1251 (delete-region start (point))
1252 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1253 (eww-write-bookmarks)))
1255 (defun eww-bookmark-yank ()
1256 "Yank a previously killed bookmark to the current line."
1258 (unless eww-bookmark-kill-ring
1259 (user-error "No previously killed bookmark"))
1261 (let ((inhibit-read-only t)
1264 (insert (pop eww-bookmark-kill-ring))
1265 (setq bookmark (get-text-property start 'eww-bookmark))
1266 (if (= start (point-min))
1267 (push bookmark eww-bookmarks)
1268 (let ((line (count-lines start (point))))
1269 (setcdr (nthcdr (1- line) eww-bookmarks)
1270 (cons bookmark (nthcdr line eww-bookmarks)))))
1271 (eww-write-bookmarks)))
1273 (defun eww-bookmark-browse ()
1274 "Browse the bookmark under point in eww."
1276 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1278 (user-error "No bookmark on the current line"))
1280 (eww-browse-url (plist-get bookmark :url))))
1282 (defun eww-next-bookmark ()
1283 "Go to the next bookmark in the list."
1287 (unless (get-buffer "*eww bookmarks*")
1289 (eww-bookmark-prepare))
1290 (with-current-buffer (get-buffer "*eww bookmarks*")
1291 (when (and (not first)
1294 (setq bookmark (get-text-property (line-beginning-position)
1297 (user-error "No next bookmark")))
1298 (eww-browse-url (plist-get bookmark :url))))
1300 (defun eww-previous-bookmark ()
1301 "Go to the previous bookmark in the list."
1305 (unless (get-buffer "*eww bookmarks*")
1307 (eww-bookmark-prepare))
1308 (with-current-buffer (get-buffer "*eww bookmarks*")
1310 (goto-char (point-max))
1311 (beginning-of-line))
1312 ;; On the final line.
1316 (user-error "No previous bookmark")
1318 (setq bookmark (get-text-property (line-beginning-position)
1320 (eww-browse-url (plist-get bookmark :url))))
1322 (defvar eww-bookmark-mode-map
1323 (let ((map (make-sparse-keymap)))
1324 (suppress-keymap map)
1325 (define-key map "q" 'quit-window)
1326 (define-key map [(control k)] 'eww-bookmark-kill)
1327 (define-key map [(control y)] 'eww-bookmark-yank)
1328 (define-key map "\r" 'eww-bookmark-browse)
1330 (easy-menu-define nil map
1331 "Menu for `eww-bookmark-mode-map'."
1333 ["Exit" quit-window t]
1334 ["Browse" eww-bookmark-browse
1335 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1336 ["Kill" eww-bookmark-kill
1337 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1338 ["Yank" eww-bookmark-yank
1339 :active eww-bookmark-kill-ring]))
1342 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1343 "Mode for listing bookmarks.
1345 \\{eww-bookmark-mode-map}"
1346 (buffer-disable-undo)
1347 (setq buffer-read-only t
1352 (defun eww-save-history ()
1353 (plist-put eww-data :point (point))
1354 (plist-put eww-data :text (buffer-string))
1355 (push eww-data eww-history)
1356 (setq eww-data (list :title "")))
1358 (defun eww-list-histories ()
1359 "List the eww-histories."
1361 (when (null eww-history)
1362 (error "No eww-histories are defined"))
1363 (let ((eww-history-trans eww-history))
1364 (set-buffer (get-buffer-create "*eww history*"))
1366 (let ((inhibit-read-only t)
1369 url title format start)
1371 (dolist (history eww-history-trans)
1372 (setq start (point))
1373 (setq domain-length (max domain-length (length (plist-get history :url))))
1374 (setq title-length (max title-length (length (plist-get history :title)))))
1375 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1377 (concat " " (format format "Title" "URL")))
1378 (dolist (history eww-history-trans)
1379 (setq start (point))
1380 (setq url (plist-get history :url))
1381 (setq title (plist-get history :title))
1382 (insert (format format title url))
1384 (put-text-property start (1+ start) 'eww-history history))
1385 (goto-char (point-min)))
1386 (pop-to-buffer "*eww history*")))
1388 (defun eww-history-browse ()
1389 "Browse the history under point in eww."
1391 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1393 (error "No history on the current line"))
1395 (eww-restore-history history)))
1397 (defvar eww-history-mode-map
1398 (let ((map (make-sparse-keymap)))
1399 (suppress-keymap map)
1400 (define-key map "q" 'quit-window)
1401 (define-key map "\r" 'eww-history-browse)
1402 ;; (define-key map "n" 'next-error-no-select)
1403 ;; (define-key map "p" 'previous-error-no-select)
1405 (easy-menu-define nil map
1406 "Menu for `eww-history-mode-map'."
1408 ["Exit" quit-window t]
1409 ["Browse" eww-history-browse
1410 :active (get-text-property (line-beginning-position) 'eww-history)]))
1413 (define-derived-mode eww-history-mode nil "eww history"
1414 "Mode for listing eww-histories.
1416 \\{eww-history-mode-map}"
1417 (buffer-disable-undo)
1418 (setq buffer-read-only t
1423 ;;; eww.el ends here