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
)
39 (defcustom eww-header-line-format
"%t: %u"
41 - %t is replaced by the title.
42 - %u is replaced by the URL."
47 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
48 "Prefix URL to search engine"
53 (defcustom eww-download-directory
"~/Downloads/"
54 "Directory where files will downloaded."
59 (defcustom eww-use-external-browser-for-content-type
60 "\\`\\(video/\\|audio/\\|application/ogg\\)"
61 "Always use external browser for specified content-type."
64 :type
'(choice (const :tag
"Never" nil
)
67 (defcustom eww-form-checkbox-selected-symbol
"[X]"
68 "Symbol used to represent a selected checkbox.
69 See also `eww-form-checkbox-symbol'."
72 :type
'(choice (const "[X]")
73 (const "☒") ; Unicode BALLOT BOX WITH X
74 (const "☑") ; Unicode BALLOT BOX WITH CHECK
77 (defcustom eww-form-checkbox-symbol
"[ ]"
78 "Symbol used to represent a checkbox.
79 See also `eww-form-checkbox-selected-symbol'."
82 :type
'(choice (const "[ ]")
83 (const "☐") ; Unicode BALLOT BOX
86 (defface eww-form-submit
87 '((((type x w32 ns
) (class color
)) ; Like default mode line
88 :box
(:line-width
2 :style released-button
)
89 :background
"#808080" :foreground
"black"))
90 "Face for eww buffer buttons."
94 (defface eww-form-checkbox
95 '((((type x w32 ns
) (class color
)) ; Like default mode line
96 :box
(:line-width
2 :style released-button
)
97 :background
"lightgrey" :foreground
"black"))
98 "Face for eww buffer buttons."
102 (defface eww-form-select
103 '((((type x w32 ns
) (class color
)) ; Like default mode line
104 :box
(:line-width
2 :style released-button
)
105 :background
"lightgrey" :foreground
"black"))
106 "Face for eww buffer buttons."
110 (defface eww-form-text
111 '((t (:background
"#505050"
113 :box
(:line-width
1))))
114 "Face for eww text inputs."
118 (defface eww-form-textarea
119 '((t (:background
"#C0C0C0"
121 :box
(:line-width
1))))
122 "Face for eww textarea inputs."
126 (defvar eww-current-url nil
)
127 (defvar eww-current-dom nil
)
128 (defvar eww-current-source nil
)
129 (defvar eww-current-title
""
130 "Title of current page.")
131 (defvar eww-history nil
)
132 (defvar eww-history-position
0)
134 (defvar eww-next-url nil
)
135 (defvar eww-previous-url nil
)
136 (defvar eww-up-url nil
)
137 (defvar eww-home-url nil
)
138 (defvar eww-start-url nil
)
139 (defvar eww-contents-url nil
)
141 (defvar eww-local-regex
"localhost"
142 "When this regex is found in the URL, it's not a keyword but an address.")
144 (defvar eww-link-keymap
145 (let ((map (copy-keymap shr-map
)))
146 (define-key map
"\r" 'eww-follow-link
)
151 "Fetch URL and render the page.
152 If the input doesn't look like an URL or a domain name, the
153 word(s) will be searched for via `eww-search-prefix'."
154 (interactive "sEnter URL or keywords: ")
155 (cond ((string-match-p "\\`file://" url
))
156 ((string-match-p "\\`ftp://" url
)
157 (user-error "FTP is not supported."))
159 (if (and (= (length (split-string url
)) 1)
160 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url
))
161 (> (length (split-string url
"\\.")) 1))
162 (string-match eww-local-regex url
)))
164 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
165 (setq url
(concat "http://" url
)))
166 ;; some site don't redirect final /
167 (when (string= (url-filename (url-generic-parse-url url
)) "")
168 (setq url
(concat url
"/"))))
169 (setq url
(concat eww-search-prefix
170 (replace-regexp-in-string " " "+" url
))))))
171 (url-retrieve url
'eww-render
(list url
)))
173 ;;;###autoload (defalias 'browse-web 'eww)
176 (defun eww-open-file (file)
177 "Render a file using EWW."
178 (interactive "fFile: ")
179 (eww (concat "file://"
180 (and (memq system-type
'(windows-nt ms-dos
))
182 (expand-file-name file
))))
184 (defun eww-render (status url
&optional point
)
185 (let ((redirect (plist-get status
:redirect
)))
187 (setq url redirect
)))
188 (setq-local eww-next-url nil
)
189 (setq-local eww-previous-url nil
)
190 (setq-local eww-up-url nil
)
191 (setq-local eww-home-url nil
)
192 (setq-local eww-start-url nil
)
193 (setq-local eww-contents-url nil
)
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 (setq eww-current-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 ((string-match-p "\\`image/" (car content-type
))
218 (eww-update-header-line-format))
221 (eww-update-header-line-format)))
222 (setq eww-current-url url
223 eww-history-position
0))
224 (kill-buffer data-buffer
))))
226 (defun eww-parse-headers ()
228 (goto-char (point-min))
229 (while (and (not (eobp))
231 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
232 (push (cons (downcase (match-string 1))
240 (defun eww-detect-charset (html-p)
241 (let ((case-fold-search t
)
245 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
249 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
252 (declare-function libxml-parse-html-region
"xml.c"
253 (start end
&optional base-url
))
255 (defun eww-display-html (charset url
&optional document point
)
256 (or (fboundp 'libxml-parse-html-region
)
257 (error "This function requires Emacs to be compiled with libxml2"))
258 (unless (eq charset
'utf8
)
260 (decode-coding-region (point) (point-max) charset
)
261 (coding-system-error nil
)))
265 'base
(list (cons 'href url
))
266 (libxml-parse-html-region (point) (point-max))))))
267 (setq eww-current-source
(buffer-substring (point) (point-max)))
269 (setq eww-current-dom document
)
270 (let ((inhibit-read-only t
)
271 (after-change-functions nil
)
273 (shr-target-id (url-target (url-generic-parse-url url
)))
274 (shr-external-rendering-functions
275 '((title . eww-tag-title
)
276 (form . eww-tag-form
)
277 (input . eww-tag-input
)
278 (textarea . eww-tag-textarea
)
279 (body . eww-tag-body
)
280 (select . eww-tag-select
)
281 (link . eww-tag-link
)
283 (shr-insert-document document
)
288 (goto-char (point-min))
289 (let ((point (next-single-property-change
290 (point-min) 'shr-target-id
)))
294 (goto-char (point-min)))))
295 (setq eww-current-url url
296 eww-history-position
0)
297 (eww-update-header-line-format)))
299 (defun eww-handle-link (cont)
300 (let* ((rel (assq :rel cont
))
301 (href (assq :href cont
))
303 ;; The text associated with :rel is case-insensitive.
304 (if rel
(downcase (cdr rel
)))
305 '(("next" . eww-next-url
)
306 ;; Texinfo uses "previous", but HTML specifies
307 ;; "prev", so recognize both.
308 ("previous" . eww-previous-url
)
309 ("prev" . eww-previous-url
)
310 ;; HTML specifies "start" but also "contents",
311 ;; and Gtk seems to use "home". Recognize
312 ;; them all; but store them in different
313 ;; variables so that we can readily choose the
315 ("start" . eww-start-url
)
316 ("home" . eww-home-url
)
317 ("contents" . eww-contents-url
)
318 ("up" . eww-up-url
)))))
321 (set (cdr where
) (cdr href
)))))
323 (defun eww-tag-link (cont)
324 (eww-handle-link cont
)
327 (defun eww-tag-a (cont)
328 (eww-handle-link cont
)
329 (let ((start (point)))
331 (put-text-property start
(point) 'keymap eww-link-keymap
)))
333 (defun eww-update-header-line-format ()
334 (if eww-header-line-format
335 (setq header-line-format
336 (replace-regexp-in-string
338 ;; FIXME? Title can be blank. Default to, eg, last component
340 (format-spec eww-header-line-format
341 `((?u .
,eww-current-url
)
342 (?t .
,eww-current-title
)))))
343 (setq header-line-format nil
)))
345 (defun eww-tag-title (cont)
346 (setq eww-current-title
"")
348 (when (eq (car sub
) 'text
)
349 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
350 (eww-update-header-line-format))
352 (defun eww-tag-body (cont)
353 (let* ((start (point))
354 (fgcolor (cdr (or (assq :fgcolor cont
)
356 (bgcolor (cdr (assq :bgcolor cont
)))
357 (shr-stylesheet (list (cons 'color fgcolor
)
358 (cons 'background-color bgcolor
))))
360 (eww-colorize-region start
(point) fgcolor bgcolor
)))
362 (defun eww-colorize-region (start end fg
&optional bg
)
364 (let ((new-colors (shr-color-check fg bg
)))
367 (add-face-text-property start end
368 (list :foreground
(cadr new-colors
))
371 (add-face-text-property start end
372 (list :background
(car new-colors
))
375 (defun eww-display-raw ()
376 (let ((data (buffer-substring (point) (point-max))))
378 (let ((inhibit-read-only t
))
380 (goto-char (point-min))))
382 (defun eww-display-image ()
383 (let ((data (shr-parse-image-data)))
385 (let ((inhibit-read-only t
))
386 (shr-put-image data nil
))
387 (goto-char (point-min))))
389 (defun eww-setup-buffer ()
390 (switch-to-buffer (get-buffer-create "*eww*"))
391 (let ((inhibit-read-only t
))
394 (unless (eq major-mode
'eww-mode
)
397 (defun eww-view-source ()
399 (let ((buf (get-buffer-create "*eww-source*"))
400 (source eww-current-source
))
401 (with-current-buffer buf
402 (delete-region (point-min) (point-max))
403 (insert (or eww-current-source
"no source"))
404 (goto-char (point-min))
405 (when (fboundp 'html-mode
)
410 (let ((map (make-sparse-keymap)))
411 (suppress-keymap map
)
412 (define-key map
"q" 'quit-window
)
413 (define-key map
"g" 'eww-reload
)
414 (define-key map
[tab] 'shr-next-link)
415 (define-key map [backtab] 'shr-previous-link)
416 (define-key map [delete] 'scroll-down-command)
417 (define-key map [?\S-\ ] 'scroll-down-command)
418 (define-key map "\177" 'scroll-down-command)
419 (define-key map " " 'scroll-up-command)
420 (define-key map "l" 'eww-back-url)
421 (define-key map "r" 'eww-forward-url)
422 (define-key map "n" 'eww-next-url)
423 (define-key map "p" 'eww-previous-url)
424 (define-key map "u" 'eww-up-url)
425 (define-key map "t" 'eww-top-url)
426 (define-key map "&" 'eww-browse-with-external-browser)
427 (define-key map "d" 'eww-download)
428 (define-key map "w" 'eww-copy-page-url)
429 (define-key map "C" 'url-cookie-list)
430 (define-key map "v" 'eww-view-source)
431 (define-key map "H" 'eww-list-histories)
433 (define-key map "b" 'eww-add-bookmark)
434 (define-key map "B" 'eww-list-bookmarks)
435 (define-key map [(meta n)] 'eww-next-bookmark)
436 (define-key map [(meta p)] 'eww-previous-bookmark)
438 (easy-menu-define nil map ""
441 ["Close browser" quit-window t]
442 ["Reload" eww-reload t]
443 ["Back to previous page" eww-back-url
444 :active (not (zerop (length eww-history)))]
445 ["Forward to next page" eww-forward-url
446 :active (not (zerop eww-history-position))]
447 ["Browse with external browser" eww-browse-with-external-browser t]
448 ["Download" eww-download t]
449 ["View page source" eww-view-source]
450 ["Copy page URL" eww-copy-page-url t]
451 ["List histories" eww-list-histories t]
452 ["Add bookmark" eww-add-bookmark t]
453 ["List bookmarks" eww-list-bookmarks t]
454 ["List cookies" url-cookie-list t]))
457 (defvar eww-tool-bar-map
458 (let ((map (make-sparse-keymap)))
459 (dolist (tool-bar-item
460 '((eww-quit . "close")
461 (eww-reload . "refresh")
462 (eww-back-url . "left-arrow")
463 (eww-forward-url . "right-arrow")
464 (eww-view-source . "show")
465 (eww-copy-page-url . "copy")
466 (eww-add-bookmark . "bookmark_add"))) ;; ...
467 (tool-bar-local-item-from-menu
468 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
470 "Tool bar for `eww-mode'.")
472 (define-derived-mode eww-mode nil "eww"
473 "Mode for browsing the web.
476 ;; FIXME? This seems a strange default.
477 (setq-local eww-current-url 'author)
478 (setq-local eww-current-dom nil)
479 (setq-local eww-current-source nil)
480 (setq-local browse-url-browser-function 'eww-browse-url)
481 (setq-local after-change-functions 'eww-process-text-input)
482 (setq-local eww-history nil)
483 (setq-local eww-history-position 0)
484 (when (boundp 'tool-bar-map)
485 (setq-local tool-bar-map eww-tool-bar-map))
486 (buffer-disable-undo)
487 ;;(setq buffer-read-only t)
491 (defun eww-browse-url (url &optional _new-window)
492 (when (and (equal major-mode 'eww-mode)
497 (defun eww-back-url ()
498 "Go to the previously displayed page."
500 (when (>= eww-history-position (length eww-history))
501 (user-error "No previous page"))
503 (setq eww-history-position (+ eww-history-position 2))
504 (eww-restore-history (elt eww-history (1- eww-history-position))))
506 (defun eww-forward-url ()
507 "Go to the next displayed page."
509 (when (zerop eww-history-position)
510 (user-error "No next page"))
512 (eww-restore-history (elt eww-history (1- eww-history-position))))
514 (defun eww-restore-history (elem)
515 (let ((inhibit-read-only t))
517 (insert (plist-get elem :text))
518 (setq eww-current-source (plist-get elem :source))
519 (setq eww-current-dom (plist-get elem :dom))
520 (goto-char (plist-get elem :point))
521 (setq eww-current-url (plist-get elem :url)
522 eww-current-title (plist-get elem :title))
523 (eww-update-header-line-format)))
525 (defun eww-next-url ()
526 "Go to the page marked `next'.
527 A page is marked `next' if rel=\"next\" appears in a <link>
531 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
532 (user-error "No `next' on this page")))
534 (defun eww-previous-url ()
535 "Go to the page marked `previous'.
536 A page is marked `previous' if rel=\"previous\" appears in a <link>
540 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
541 (user-error "No `previous' on this page")))
544 "Go to the page marked `up'.
545 A page is marked `up' if rel=\"up\" appears in a <link>
549 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
550 (user-error "No `up' on this page")))
552 (defun eww-top-url ()
553 "Go to the page marked `top'.
554 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
555 appears in a <link> or <a> tag."
557 (let ((best-url (or eww-start-url
561 (eww-browse-url (shr-expand-url best-url eww-current-url))
562 (user-error "No `top' for this page"))))
565 "Reload the current page."
567 (url-retrieve eww-current-url 'eww-render
568 (list eww-current-url (point))))
572 (defvar eww-form nil)
574 (defvar eww-submit-map
575 (let ((map (make-sparse-keymap)))
576 (define-key map "\r" 'eww-submit)
577 (define-key map [(control c) (control c)] 'eww-submit)
580 (defvar eww-checkbox-map
581 (let ((map (make-sparse-keymap)))
582 (define-key map " " 'eww-toggle-checkbox)
583 (define-key map "\r" 'eww-toggle-checkbox)
584 (define-key map [(control c) (control c)] 'eww-submit)
588 (let ((map (make-keymap)))
589 (set-keymap-parent map text-mode-map)
590 (define-key map "\r" 'eww-submit)
591 (define-key map [(control a)] 'eww-beginning-of-text)
592 (define-key map [(control c) (control c)] 'eww-submit)
593 (define-key map [(control e)] 'eww-end-of-text)
594 (define-key map [tab] 'shr-next-link
)
595 (define-key map
[backtab] 'shr-previous-link)
598 (defvar eww-textarea-map
599 (let ((map (make-keymap)))
600 (set-keymap-parent map text-mode-map)
601 (define-key map "\r" 'forward-line)
602 (define-key map [(control c) (control c)] 'eww-submit)
603 (define-key map [tab] 'shr-next-link)
604 (define-key map [backtab] 'shr-previous-link
)
607 (defvar eww-select-map
608 (let ((map (make-sparse-keymap)))
609 (define-key map
"\r" 'eww-change-select
)
610 (define-key map
[(control c
) (control c
)] 'eww-submit
)
613 (defun eww-beginning-of-text ()
614 "Move to the start of the input field."
616 (goto-char (eww-beginning-of-field)))
618 (defun eww-end-of-text ()
619 "Move to the end of the text in the input field."
621 (goto-char (eww-end-of-field))
622 (let ((start (eww-beginning-of-field)))
623 (while (and (equal (following-char) ?
)
626 (when (> (point) start
)
629 (defun eww-beginning-of-field ()
633 ((not (eq (get-text-property (point) 'eww-form
)
634 (get-text-property (1- (point)) 'eww-form
)))
637 (previous-single-property-change
638 (point) 'eww-form nil
(point-min)))))
640 (defun eww-end-of-field ()
641 (1- (next-single-property-change
642 (point) 'eww-form nil
(point-max))))
644 (defun eww-tag-form (cont)
646 (list (assq :method cont
)
647 (assq :action cont
)))
649 (shr-ensure-paragraph)
654 (when (> (point) start
)
655 (put-text-property start
(1+ start
)
656 'eww-form eww-form
))))
658 (defun eww-form-submit (cont)
659 (let ((start (point))
660 (value (cdr (assq :value cont
))))
662 (if (zerop (length value
))
666 (add-face-text-property start
(point) 'eww-form-submit
)
667 (put-text-property start
(point) 'eww-form
668 (list :eww-form eww-form
671 :name
(cdr (assq :name cont
))))
672 (put-text-property start
(point) 'keymap eww-submit-map
)
675 (defun eww-form-checkbox (cont)
676 (let ((start (point)))
677 (if (cdr (assq :checked cont
))
678 (insert eww-form-checkbox-selected-symbol
)
679 (insert eww-form-checkbox-symbol
))
680 (add-face-text-property start
(point) 'eww-form-checkbox
)
681 (put-text-property start
(point) 'eww-form
682 (list :eww-form eww-form
683 :value
(cdr (assq :value cont
))
684 :type
(downcase (cdr (assq :type cont
)))
685 :checked
(cdr (assq :checked cont
))
686 :name
(cdr (assq :name cont
))))
687 (put-text-property start
(point) 'keymap eww-checkbox-map
)
690 (defun eww-form-text (cont)
691 (let ((start (point))
692 (type (downcase (or (cdr (assq :type cont
))
694 (value (or (cdr (assq :value cont
)) ""))
695 (width (string-to-number
696 (or (cdr (assq :size cont
))
698 (readonly-property (if (or (cdr (assq :disabled cont
))
699 (cdr (assq :readonly cont
)))
701 'inhibit-read-only
)))
703 (when (< (length value
) width
)
704 (insert (make-string (- width
(length value
)) ?
)))
705 (put-text-property start
(point) 'face
'eww-form-text
)
706 (put-text-property start
(point) 'local-map eww-text-map
)
707 (put-text-property start
(point) readonly-property t
)
708 (put-text-property start
(point) 'eww-form
709 (list :eww-form eww-form
712 :name
(cdr (assq :name cont
))))
715 (defconst eww-text-input-types
'("text" "password" "textarea"
716 "color" "date" "datetime" "datetime-local"
717 "email" "month" "number" "search" "tel"
719 "List of input types which represent a text input.
720 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
722 (defun eww-process-text-input (beg end length
)
723 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
724 (properties (text-properties-at end
))
725 (type (plist-get form
:type
)))
727 (member type eww-text-input-types
))
730 ;; Delete some space at the end.
733 (if (equal type
"textarea")
734 (1- (line-end-position))
736 (let ((new (- end beg
)))
737 (while (and (> new
0)
738 (eql (following-char) ?
))
739 (delete-region (point) (1+ (point)))
740 (setq new
(1- new
))))
741 (set-text-properties beg end properties
)))
746 (if (equal type
"textarea")
747 (1- (line-end-position))
749 (let ((start (point)))
750 (insert (make-string length ?
))
751 (set-text-properties start
(point) properties
)))))
752 (let ((value (buffer-substring-no-properties
753 (eww-beginning-of-field)
754 (eww-end-of-field))))
755 (when (string-match " +\\'" value
)
756 (setq value
(substring value
0 (match-beginning 0))))
757 (plist-put form
:value value
)
758 (when (equal type
"password")
759 ;; Display passwords as asterisks.
760 (let ((start (eww-beginning-of-field)))
761 (put-text-property start
(+ start
(length value
))
762 'display
(make-string (length value
) ?
*))))))))
764 (defun eww-tag-textarea (cont)
765 (let ((start (point))
766 (value (or (cdr (assq :value cont
)) ""))
767 (lines (string-to-number
768 (or (cdr (assq :rows cont
))
770 (width (string-to-number
771 (or (cdr (assq :cols cont
))
777 (when (< (count-lines start
(point)) lines
)
778 (dotimes (i (- lines
(count-lines start
(point))))
780 (setq end
(point-marker))
782 (while (< (point) end
)
784 (let ((pad (- width
(- (point) (line-beginning-position)))))
786 (insert (make-string pad ?
))))
787 (add-face-text-property (line-beginning-position)
788 (point) 'eww-form-textarea
)
789 (put-text-property (line-beginning-position) (point)
790 'local-map eww-textarea-map
)
792 (put-text-property start
(point) 'eww-form
793 (list :eww-form eww-form
796 :name
(cdr (assq :name cont
))))))
798 (defun eww-tag-input (cont)
799 (let ((type (downcase (or (cdr (assq :type cont
))
803 ((or (equal type
"checkbox")
804 (equal type
"radio"))
805 (eww-form-checkbox cont
))
806 ((equal type
"submit")
807 (eww-form-submit cont
))
808 ((equal type
"hidden")
809 (let ((form eww-form
)
810 (name (cdr (assq :name cont
))))
811 ;; Don't add <input type=hidden> elements repeatedly.
813 (or (not (consp (car form
)))
814 (not (eq (caar form
) 'hidden
))
815 (not (equal (plist-get (cdr (car form
)) :name
)
817 (setq form
(cdr form
)))
819 (nconc eww-form
(list
822 :value
(cdr (assq :value cont
))))))))
824 (eww-form-text cont
)))
825 (unless (= start
(point))
826 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
828 (defun eww-tag-select (cont)
829 (shr-ensure-paragraph)
830 (let ((menu (list :name
(cdr (assq :name cont
))
836 (if (eq (car (car cont
)) 'optgroup
)
837 (dolist (groupelem cont
)
838 (unless (cdr (assq :disabled
(cdr groupelem
)))
839 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
841 (dolist (elem opelem
)
842 (when (eq (car elem
) 'option
)
843 (when (cdr (assq :selected
(cdr elem
)))
844 (nconc menu
(list :value
845 (cdr (assq :value
(cdr elem
))))))
846 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
847 (setq max
(max max
(length display
)))
849 :value
(cdr (assq :value
(cdr elem
)))
853 (setq options
(nreverse options
))
854 ;; If we have no selected values, default to the first value.
855 (unless (plist-get menu
:value
)
856 (nconc menu
(list :value
(nth 2 (car options
)))))
858 (let ((selected (eww-select-display menu
)))
860 (make-string (- max
(length selected
)) ?
)))
861 (put-text-property start
(point) 'eww-form menu
)
862 (add-face-text-property start
(point) 'eww-form-select
)
863 (put-text-property start
(point) 'keymap eww-select-map
)
864 (unless (= start
(point))
865 (put-text-property start
(1+ start
) 'help-echo
"select field"))
866 (shr-ensure-paragraph))))
868 (defun eww-select-display (select)
869 (let ((value (plist-get select
:value
))
871 (dolist (elem select
)
872 (when (and (consp elem
)
873 (eq (car elem
) 'item
)
874 (equal value
(plist-get (cdr elem
) :value
)))
875 (setq display
(plist-get (cdr elem
) :display
))))
878 (defun eww-change-select ()
879 "Change the value of the select drop-down menu under point."
881 (let* ((input (get-text-property (point) 'eww-form
))
882 (completion-ignore-case t
)
885 (mapcar (lambda (elem)
887 (eq (car elem
) 'item
)
888 (cons (plist-get (cdr elem
) :display
)
889 (plist-get (cdr elem
) :value
))))
892 (completing-read "Change value: " options nil
'require-match
))
893 (inhibit-read-only t
))
894 (plist-put input
:value
(cdr (assoc-string display options t
)))
896 (eww-update-field display
))))
898 (defun eww-update-field (string)
899 (let ((properties (text-properties-at (point)))
900 (start (eww-beginning-of-field))
901 (end (1+ (eww-end-of-field))))
902 (delete-region start end
)
904 (make-string (- (- end start
) (length string
)) ?
))
905 (set-text-properties start end properties
)
908 (defun eww-toggle-checkbox ()
909 "Toggle the value of the checkbox under point."
911 (let* ((input (get-text-property (point) 'eww-form
))
912 (type (plist-get input
:type
)))
913 (if (equal type
"checkbox")
916 (if (plist-get input
:checked
)
918 (plist-put input
:checked nil
)
919 (eww-update-field eww-form-checkbox-symbol
))
920 (plist-put input
:checked t
)
921 (eww-update-field eww-form-checkbox-selected-symbol
))))
922 ;; Radio button. Switch all other buttons off.
923 (let ((name (plist-get input
:name
)))
925 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
926 (when (equal (plist-get (cdr elem
) :name
) name
)
927 (goto-char (car elem
))
928 (if (not (eq (cdr elem
) input
))
930 (plist-put input
:checked nil
)
931 (eww-update-field eww-form-checkbox-symbol
))
932 (plist-put input
:checked t
)
933 (eww-update-field eww-form-checkbox-selected-symbol
)))))
936 (defun eww-inputs (form)
937 (let ((start (point-min))
940 (< start
(point-max)))
941 (when (or (get-text-property start
'eww-form
)
942 (setq start
(next-single-property-change start
'eww-form
)))
943 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
945 (push (cons start
(get-text-property start
'eww-form
))
947 (setq start
(next-single-property-change start
'eww-form
))))
950 (defun eww-input-value (input)
951 (let ((type (plist-get input
:type
))
952 (value (plist-get input
:value
)))
954 ((equal type
"textarea")
957 (goto-char (point-min))
958 (while (re-search-forward "^ +\n\\| +$" nil t
)
959 (replace-match "" t t
))
962 (if (string-match " +\\'" value
)
963 (substring value
0 (match-beginning 0))
967 "Submit the current form."
969 (let* ((this-input (get-text-property (point) 'eww-form
))
970 (form (plist-get this-input
:eww-form
))
972 (dolist (elem (sort (eww-inputs form
)
974 (< (car o1
) (car o2
)))))
975 (let* ((input (cdr elem
))
976 (input-start (car elem
))
977 (name (plist-get input
:name
)))
980 ((member (plist-get input
:type
) '("checkbox" "radio"))
981 (when (plist-get input
:checked
)
982 (push (cons name
(plist-get input
:value
))
984 ((equal (plist-get input
:type
) "submit")
985 ;; We want the values from buttons if we hit a button if
986 ;; we hit enter on it, or if it's the first button after
987 ;; the field we did hit return on.
988 (when (or (eq input this-input
)
989 (and (not (eq input this-input
))
991 (> input-start
(point))))
993 (push (cons name
(plist-get input
:value
))
996 (push (cons name
(eww-input-value input
))
999 (when (and (consp elem
)
1000 (eq (car elem
) 'hidden
))
1001 (push (cons (plist-get (cdr elem
) :name
)
1002 (plist-get (cdr elem
) :value
))
1004 (if (and (stringp (cdr (assq :method form
)))
1005 (equal (downcase (cdr (assq :method form
))) "post"))
1006 (let ((url-request-method "POST")
1007 (url-request-extra-headers
1008 '(("Content-Type" .
"application/x-www-form-urlencoded")))
1009 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
1010 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
1014 (if (cdr (assq :action form
))
1015 (shr-expand-url (cdr (assq :action form
))
1019 (mm-url-encode-www-form-urlencoded values
))))))
1021 (defun eww-browse-with-external-browser (&optional url
)
1022 "Browse the current URL with an external browser.
1023 The browser to used is specified by the `shr-external-browser' variable."
1025 (funcall shr-external-browser
(or url eww-current-url
)))
1027 (defun eww-follow-link (&optional external mouse-event
)
1028 "Browse the URL under point.
1029 If EXTERNAL, browse the URL using `shr-external-browser'."
1030 (interactive (list current-prefix-arg last-nonmenu-event
))
1031 (mouse-set-point mouse-event
)
1032 (let ((url (get-text-property (point) 'shr-url
)))
1035 (message "No link under point"))
1036 ((string-match "^mailto:" url
)
1037 (browse-url-mail url
))
1039 (funcall shr-external-browser url
))
1040 ;; This is a #target url in the same page as the current one.
1041 ((and (url-target (url-generic-parse-url url
))
1042 (eww-same-page-p url eww-current-url
))
1044 (eww-display-html 'utf8 url eww-current-dom
))
1046 (eww-browse-url url
)))))
1048 (defun eww-same-page-p (url1 url2
)
1049 "Return non-nil if both URLs represent the same page.
1050 Differences in #targets are ignored."
1051 (let ((obj1 (url-generic-parse-url url1
))
1052 (obj2 (url-generic-parse-url url2
)))
1053 (setf (url-target obj1
) nil
)
1054 (setf (url-target obj2
) nil
)
1055 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1057 (defun eww-copy-page-url ()
1059 (message "%s" eww-current-url
)
1060 (kill-new eww-current-url
))
1062 (defun eww-download ()
1063 "Download URL under point to `eww-download-directory'."
1065 (let ((url (get-text-property (point) 'shr-url
)))
1067 (message "No URL under point")
1068 (url-retrieve url
'eww-download-callback
(list url
)))))
1070 (defun eww-download-callback (status url
)
1071 (unless (plist-get status
:error
)
1072 (let* ((obj (url-generic-parse-url url
))
1073 (path (car (url-path-and-query obj
)))
1074 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1075 eww-download-directory
)))
1077 (message "Saved %s" file
))))
1079 (defun eww-make-unique-file-name (file directory
)
1081 ((zerop (length file
))
1083 ((string-match "\\`[.]" file
)
1084 (setq file
(concat "!" file
))))
1086 (while (file-exists-p (expand-file-name file directory
))
1088 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1089 (format "%s(%d)%s" (match-string 1 file
)
1090 count
(match-string 2 file
))
1091 (format "%s(%d)" file count
)))
1092 (setq count
(1+ count
)))
1093 (expand-file-name file directory
)))
1097 (defvar eww-bookmarks nil
)
1099 (defun eww-add-bookmark ()
1100 "Add the current page to the bookmarks."
1102 (eww-read-bookmarks)
1103 (dolist (bookmark eww-bookmarks
)
1104 (when (equal eww-current-url
1105 (plist-get bookmark
:url
))
1106 (user-error "Already bookmarked")))
1107 (if (y-or-n-p "bookmark this page? ")
1109 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1110 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1111 (push (list :url eww-current-url
1113 :time
(current-time-string))
1115 (eww-write-bookmarks)
1116 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1118 (defun eww-write-bookmarks ()
1119 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1120 (insert ";; Auto-generated file; don't edit\n")
1121 (pp eww-bookmarks
(current-buffer))))
1123 (defun eww-read-bookmarks ()
1124 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1126 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1128 (insert-file-contents file
)
1129 (read (current-buffer)))))))
1131 (defun eww-list-bookmarks ()
1132 "Display the bookmarks."
1134 (eww-bookmark-prepare)
1135 (pop-to-buffer "*eww bookmarks*"))
1137 (defun eww-bookmark-prepare ()
1138 (eww-read-bookmarks)
1139 (unless eww-bookmarks
1140 (user-error "No bookmarks are defined"))
1141 (set-buffer (get-buffer-create "*eww bookmarks*"))
1143 (let ((format "%-40s %s")
1144 (inhibit-read-only t
)
1147 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1148 (dolist (bookmark eww-bookmarks
)
1149 (setq start
(point))
1150 (setq url
(plist-get bookmark
:url
))
1151 (when (> (length url
) 40)
1152 (setq url
(substring url
0 40)))
1153 (insert (format format url
1154 (plist-get bookmark
:title
))
1156 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1157 (goto-char (point-min))))
1159 (defvar eww-bookmark-kill-ring nil
)
1161 (defun eww-bookmark-kill ()
1162 "Kill the current bookmark."
1164 (let* ((start (line-beginning-position))
1165 (bookmark (get-text-property start
'eww-bookmark
))
1166 (inhibit-read-only t
))
1168 (user-error "No bookmark on the current line"))
1170 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1171 (delete-region start
(point))
1172 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1173 (eww-write-bookmarks)))
1175 (defun eww-bookmark-yank ()
1176 "Yank a previously killed bookmark to the current line."
1178 (unless eww-bookmark-kill-ring
1179 (user-error "No previously killed bookmark"))
1181 (let ((inhibit-read-only t
)
1184 (insert (pop eww-bookmark-kill-ring
))
1185 (setq bookmark
(get-text-property start
'eww-bookmark
))
1186 (if (= start
(point-min))
1187 (push bookmark eww-bookmarks
)
1188 (let ((line (count-lines start
(point))))
1189 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1190 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1191 (eww-write-bookmarks)))
1193 (defun eww-bookmark-browse ()
1194 "Browse the bookmark under point in eww."
1196 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1198 (user-error "No bookmark on the current line"))
1200 (eww-browse-url (plist-get bookmark
:url
))))
1202 (defun eww-next-bookmark ()
1203 "Go to the next bookmark in the list."
1207 (unless (get-buffer "*eww bookmarks*")
1209 (eww-bookmark-prepare))
1210 (with-current-buffer (get-buffer "*eww bookmarks*")
1211 (when (and (not first
)
1214 (setq bookmark
(get-text-property (line-beginning-position)
1217 (user-error "No next bookmark")))
1218 (eww-browse-url (plist-get bookmark
:url
))))
1220 (defun eww-previous-bookmark ()
1221 "Go to the previous bookmark in the list."
1225 (unless (get-buffer "*eww bookmarks*")
1227 (eww-bookmark-prepare))
1228 (with-current-buffer (get-buffer "*eww bookmarks*")
1230 (goto-char (point-max))
1231 (beginning-of-line))
1232 ;; On the final line.
1236 (user-error "No previous bookmark")
1238 (setq bookmark
(get-text-property (line-beginning-position)
1240 (eww-browse-url (plist-get bookmark
:url
))))
1242 (defvar eww-bookmark-mode-map
1243 (let ((map (make-sparse-keymap)))
1244 (suppress-keymap map
)
1245 (define-key map
"q" 'quit-window
)
1246 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1247 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1248 (define-key map
"\r" 'eww-bookmark-browse
)
1250 (easy-menu-define nil map
1251 "Menu for `eww-bookmark-mode-map'."
1253 ["Exit" quit-window t
]
1254 ["Browse" eww-bookmark-browse
1255 :active
(get-text-property (line-beginning-position) 'eww-bookmark
)]
1256 ["Kill" eww-bookmark-kill
1257 :active
(get-text-property (line-beginning-position) 'eww-bookmark
)]
1258 ["Yank" eww-bookmark-yank
1259 :active eww-bookmark-kill-ring
]))
1262 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1263 "Mode for listing bookmarks.
1265 \\{eww-bookmark-mode-map}"
1266 (buffer-disable-undo)
1267 (setq buffer-read-only t
1272 (defun eww-save-history ()
1273 (push (list :url eww-current-url
1274 :title eww-current-title
1276 :dom eww-current-dom
1277 :source eww-current-source
1278 :text
(buffer-string))
1281 (defun eww-list-histories ()
1282 "List the eww-histories."
1284 (when (null eww-history
)
1285 (error "No eww-histories are defined"))
1286 (let ((eww-history-trans eww-history
))
1287 (set-buffer (get-buffer-create "*eww history*"))
1289 (let ((inhibit-read-only t
)
1292 url title format start
)
1294 (dolist (history eww-history-trans
)
1295 (setq start
(point))
1296 (setq domain-length
(max domain-length
(length (plist-get history
:url
))))
1297 (setq title-length
(max title-length
(length (plist-get history
:title
)))))
1298 (setq format
(format "%%-%ds %%-%ds" title-length domain-length
)
1300 (concat " " (format format
"Title" "URL")))
1301 (dolist (history eww-history-trans
)
1302 (setq start
(point))
1303 (setq url
(plist-get history
:url
))
1304 (setq title
(plist-get history
:title
))
1305 (insert (format format title url
))
1307 (put-text-property start
(1+ start
) 'eww-history history
))
1308 (goto-char (point-min)))
1309 (pop-to-buffer "*eww history*")))
1311 (defun eww-history-browse ()
1312 "Browse the history under point in eww."
1314 (let ((history (get-text-property (line-beginning-position) 'eww-history
)))
1316 (error "No history on the current line"))
1318 (eww-restore-history history
)))
1320 (defvar eww-history-mode-map
1321 (let ((map (make-sparse-keymap)))
1322 (suppress-keymap map
)
1323 (define-key map
"q" 'quit-window
)
1324 (define-key map
"\r" 'eww-history-browse
)
1325 ;; (define-key map "n" 'next-error-no-select)
1326 ;; (define-key map "p" 'previous-error-no-select)
1328 (easy-menu-define nil map
1329 "Menu for `eww-history-mode-map'."
1331 ["Exit" quit-window t
]
1332 ["Browse" eww-history-browse
1333 :active
(get-text-property (line-beginning-position) 'eww-history
)]))
1336 (define-derived-mode eww-history-mode nil
"eww history"
1337 "Mode for listing eww-histories.
1339 \\{eww-history-mode-map}"
1340 (buffer-disable-undo)
1341 (setq buffer-read-only t
1346 ;;; eww.el ends here