1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013 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-path
"~/Downloads/"
54 "Path 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 (defvar eww-current-url nil
)
119 (defvar eww-current-dom nil
)
120 (defvar eww-current-source nil
)
121 (defvar eww-current-title
""
122 "Title of current page.")
123 (defvar eww-history nil
)
124 (defvar eww-history-position
0)
126 (defvar eww-next-url nil
)
127 (defvar eww-previous-url nil
)
128 (defvar eww-up-url nil
)
129 (defvar eww-home-url nil
)
130 (defvar eww-start-url nil
)
131 (defvar eww-contents-url nil
)
133 (defvar eww-local-regex
"localhost"
134 "When this regex is found in the URL, it's not a keyword but an address.")
136 (defvar eww-link-keymap
137 (let ((map (copy-keymap shr-map
)))
138 (define-key map
"\r" 'eww-follow-link
)
143 "Fetch URL and render the page.
144 If the input doesn't look like an URL or a domain name, the
145 word(s) will be searched for via `eww-search-prefix'."
146 (interactive "sEnter URL or keywords: ")
147 (cond ((string-match-p "\\`file://" url
))
148 ((string-match-p "\\`ftp://" url
)
149 (user-error "FTP is not supported."))
151 (if (and (= (length (split-string url
)) 1)
152 (or (> (length (split-string url
"\\.")) 1)
153 (string-match eww-local-regex url
)))
155 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
156 (setq url
(concat "http://" url
)))
157 ;; some site don't redirect final /
158 (when (string= (url-filename (url-generic-parse-url url
)) "")
159 (setq url
(concat url
"/"))))
160 (setq url
(concat eww-search-prefix
161 (replace-regexp-in-string " " "+" url
))))))
162 (url-retrieve url
'eww-render
(list url
)))
164 ;;;###autoload (defalias 'browse-web 'eww)
167 (defun eww-open-file (file)
168 "Render a file using EWW."
169 (interactive "fFile: ")
170 (eww (concat "file://"
171 (and (memq system-type
'(windows-nt ms-dos
))
173 (expand-file-name file
))))
175 (defun eww-render (status url
&optional point
)
176 (let ((redirect (plist-get status
:redirect
)))
178 (setq url redirect
)))
179 (setq-local eww-next-url nil
)
180 (setq-local eww-previous-url nil
)
181 (setq-local eww-up-url nil
)
182 (setq-local eww-home-url nil
)
183 (setq-local eww-start-url nil
)
184 (setq-local eww-contents-url nil
)
185 (let* ((headers (eww-parse-headers))
187 (mail-header-parse-content-type
188 (or (cdr (assoc "content-type" headers
))
192 (or (cdr (assq 'charset
(cdr content-type
)))
193 (eww-detect-charset (equal (car content-type
)
196 (data-buffer (current-buffer)))
199 (setq eww-current-title
"")
201 ((and eww-use-external-browser-for-content-type
202 (string-match-p eww-use-external-browser-for-content-type
204 (eww-browse-with-external-browser url
))
205 ((equal (car content-type
) "text/html")
206 (eww-display-html charset url nil point
))
207 ((string-match-p "\\`image/" (car content-type
))
209 (eww-update-header-line-format))
212 (eww-update-header-line-format)))
213 (setq eww-current-url url
214 eww-history-position
0))
215 (kill-buffer data-buffer
))))
217 (defun eww-parse-headers ()
219 (goto-char (point-min))
220 (while (and (not (eobp))
222 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
223 (push (cons (downcase (match-string 1))
231 (defun eww-detect-charset (html-p)
232 (let ((case-fold-search t
)
236 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
240 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
243 (declare-function libxml-parse-html-region
"xml.c"
244 (start end
&optional base-url
))
246 (defun eww-display-html (charset url
&optional document point
)
247 (or (fboundp 'libxml-parse-html-region
)
248 (error "This function requires Emacs to be compiled with libxml2"))
249 (unless (eq charset
'utf8
)
251 (decode-coding-region (point) (point-max) charset
)
252 (coding-system-error nil
)))
256 'base
(list (cons 'href url
))
257 (libxml-parse-html-region (point) (point-max))))))
258 (setq eww-current-source
(buffer-substring (point) (point-max)))
260 (setq eww-current-dom document
)
261 (let ((inhibit-read-only t
)
262 (after-change-functions nil
)
264 (shr-target-id (url-target (url-generic-parse-url url
)))
265 (shr-external-rendering-functions
266 '((title . eww-tag-title
)
267 (form . eww-tag-form
)
268 (input . eww-tag-input
)
269 (textarea . eww-tag-textarea
)
270 (body . eww-tag-body
)
271 (select . eww-tag-select
)
272 (link . eww-tag-link
)
274 (shr-insert-document document
)
279 (goto-char (point-min))
280 (let ((point (next-single-property-change
281 (point-min) 'shr-target-id
)))
285 (goto-char (point-min)))))
286 (setq eww-current-url url
287 eww-history-position
0)
288 (eww-update-header-line-format)))
290 (defun eww-handle-link (cont)
291 (let* ((rel (assq :rel cont
))
292 (href (assq :href cont
))
294 ;; The text associated with :rel is case-insensitive.
295 (if rel
(downcase (cdr rel
)))
296 '(("next" . eww-next-url
)
297 ;; Texinfo uses "previous", but HTML specifies
298 ;; "prev", so recognize both.
299 ("previous" . eww-previous-url
)
300 ("prev" . eww-previous-url
)
301 ;; HTML specifies "start" but also "contents",
302 ;; and Gtk seems to use "home". Recognize
303 ;; them all; but store them in different
304 ;; variables so that we can readily choose the
306 ("start" . eww-start-url
)
307 ("home" . eww-home-url
)
308 ("contents" . eww-contents-url
)
309 ("up" . eww-up-url
)))))
312 (set (cdr where
) (cdr href
)))))
314 (defun eww-tag-link (cont)
315 (eww-handle-link cont
)
318 (defun eww-tag-a (cont)
319 (eww-handle-link cont
)
320 (let ((start (point)))
322 (put-text-property start
(point) 'keymap eww-link-keymap
)))
324 (defun eww-update-header-line-format ()
325 (if eww-header-line-format
326 (setq header-line-format
327 (replace-regexp-in-string
329 ;; FIXME? Title can be blank. Default to, eg, last component
331 (format-spec eww-header-line-format
332 `((?u .
,eww-current-url
)
333 (?t .
,eww-current-title
)))))
334 (setq header-line-format nil
)))
336 (defun eww-tag-title (cont)
337 (setq eww-current-title
"")
339 (when (eq (car sub
) 'text
)
340 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
341 (eww-update-header-line-format))
343 (defun eww-tag-body (cont)
344 (let* ((start (point))
345 (fgcolor (cdr (or (assq :fgcolor cont
)
347 (bgcolor (cdr (assq :bgcolor cont
)))
348 (shr-stylesheet (list (cons 'color fgcolor
)
349 (cons 'background-color bgcolor
))))
351 (eww-colorize-region start
(point) fgcolor bgcolor
)))
353 (defun eww-colorize-region (start end fg
&optional bg
)
355 (let ((new-colors (shr-color-check fg bg
)))
358 (add-face-text-property start end
359 (list :foreground
(cadr new-colors
))
362 (add-face-text-property start end
363 (list :background
(car new-colors
))
366 (defun eww-display-raw ()
367 (let ((data (buffer-substring (point) (point-max))))
369 (let ((inhibit-read-only t
))
371 (goto-char (point-min))))
373 (defun eww-display-image ()
374 (let ((data (shr-parse-image-data)))
376 (let ((inhibit-read-only t
))
377 (shr-put-image data nil
))
378 (goto-char (point-min))))
380 (defun eww-setup-buffer ()
381 (switch-to-buffer (get-buffer-create "*eww*"))
382 (let ((inhibit-read-only t
))
385 (unless (eq major-mode
'eww-mode
)
388 (defun eww-view-source ()
390 (let ((buf (get-buffer-create "*eww-source*"))
391 (source eww-current-source
))
392 (with-current-buffer buf
393 (delete-region (point-min) (point-max))
394 (insert (or eww-current-source
"no source"))
395 (goto-char (point-min))
396 (when (fboundp 'html-mode
)
401 (let ((map (make-sparse-keymap)))
402 (suppress-keymap map
)
403 (define-key map
"q" 'quit-window
)
404 (define-key map
"g" 'eww-reload
)
405 (define-key map
[tab] 'shr-next-link)
406 (define-key map [backtab] 'shr-previous-link)
407 (define-key map [delete] 'scroll-down-command)
408 (define-key map [?\S-\ ] 'scroll-down-command)
409 (define-key map "\177" 'scroll-down-command)
410 (define-key map " " 'scroll-up-command)
411 (define-key map "l" 'eww-back-url)
412 (define-key map "r" 'eww-forward-url)
413 (define-key map "n" 'eww-next-url)
414 (define-key map "p" 'eww-previous-url)
415 (define-key map "u" 'eww-up-url)
416 (define-key map "t" 'eww-top-url)
417 (define-key map "&" 'eww-browse-with-external-browser)
418 (define-key map "d" 'eww-download)
419 (define-key map "w" 'eww-copy-page-url)
420 (define-key map "C" 'url-cookie-list)
421 (define-key map "v" 'eww-view-source)
422 (define-key map "H" 'eww-list-histories)
424 (define-key map "b" 'eww-add-bookmark)
425 (define-key map "B" 'eww-list-bookmarks)
426 (define-key map [(meta n)] 'eww-next-bookmark)
427 (define-key map [(meta p)] 'eww-previous-bookmark)
429 (easy-menu-define nil map ""
432 ["Close browser" quit-window t]
433 ["Reload" eww-reload t]
434 ["Back to previous page" eww-back-url
435 :active (not (zerop (length eww-history)))]
436 ["Forward to next page" eww-forward-url
437 :active (not (zerop eww-history-position))]
438 ["Browse with external browser" eww-browse-with-external-browser t]
439 ["Download" eww-download t]
440 ["View page source" eww-view-source]
441 ["Copy page URL" eww-copy-page-url t]
442 ["List histories" eww-list-histories t]
443 ["Add bookmark" eww-add-bookmark t]
444 ["List bookmarks" eww-list-bookmarks t]
445 ["List cookies" url-cookie-list t]))
448 (defvar eww-tool-bar-map
449 (let ((map (make-sparse-keymap)))
450 (dolist (tool-bar-item
451 '((eww-quit . "close")
452 (eww-reload . "refresh")
453 (eww-back-url . "left-arrow")
454 (eww-forward-url . "right-arrow")
455 (eww-view-source . "show")
456 (eww-copy-page-url . "copy")
457 (eww-add-bookmark . "bookmark_add"))) ;; ...
458 (tool-bar-local-item-from-menu
459 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
461 "Tool bar for `eww-mode'.")
463 (define-derived-mode eww-mode nil "eww"
464 "Mode for browsing the web.
467 ;; FIXME? This seems a strange default.
468 (setq-local eww-current-url 'author)
469 (setq-local eww-current-dom nil)
470 (setq-local eww-current-source nil)
471 (setq-local browse-url-browser-function 'eww-browse-url)
472 (setq-local after-change-functions 'eww-process-text-input)
473 (setq-local eww-history nil)
474 (setq-local eww-history-position 0)
475 (when (boundp 'tool-bar-map)
476 (setq-local tool-bar-map eww-tool-bar-map))
477 (buffer-disable-undo)
478 ;;(setq buffer-read-only t)
482 (defun eww-browse-url (url &optional _new-window)
483 (when (and (equal major-mode 'eww-mode)
488 (defun eww-back-url ()
489 "Go to the previously displayed page."
491 (when (>= eww-history-position (length eww-history))
492 (user-error "No previous page"))
494 (setq eww-history-position (+ eww-history-position 2))
495 (eww-restore-history (elt eww-history (1- eww-history-position))))
497 (defun eww-forward-url ()
498 "Go to the next displayed page."
500 (when (zerop eww-history-position)
501 (user-error "No next page"))
503 (eww-restore-history (elt eww-history (1- eww-history-position))))
505 (defun eww-restore-history (elem)
506 (let ((inhibit-read-only t))
508 (insert (plist-get elem :text))
509 (setq eww-current-source (plist-get elem :source))
510 (setq eww-current-dom (plist-get elem :dom))
511 (goto-char (plist-get elem :point))
512 (setq eww-current-url (plist-get elem :url)
513 eww-current-title (plist-get elem :title))
514 (eww-update-header-line-format)))
516 (defun eww-next-url ()
517 "Go to the page marked `next'.
518 A page is marked `next' if rel=\"next\" appears in a <link>
522 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
523 (user-error "No `next' on this page")))
525 (defun eww-previous-url ()
526 "Go to the page marked `previous'.
527 A page is marked `previous' if rel=\"previous\" appears in a <link>
531 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
532 (user-error "No `previous' on this page")))
535 "Go to the page marked `up'.
536 A page is marked `up' if rel=\"up\" appears in a <link>
540 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
541 (user-error "No `up' on this page")))
543 (defun eww-top-url ()
544 "Go to the page marked `top'.
545 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
546 appears in a <link> or <a> tag."
548 (let ((best-url (or eww-start-url
552 (eww-browse-url (shr-expand-url best-url eww-current-url))
553 (user-error "No `top' for this page"))))
556 "Reload the current page."
558 (url-retrieve eww-current-url 'eww-render
559 (list eww-current-url (point))))
563 (defvar eww-form nil)
565 (defvar eww-submit-map
566 (let ((map (make-sparse-keymap)))
567 (define-key map "\r" 'eww-submit)
568 (define-key map [(control c) (control c)] 'eww-submit)
571 (defvar eww-checkbox-map
572 (let ((map (make-sparse-keymap)))
573 (define-key map " " 'eww-toggle-checkbox)
574 (define-key map "\r" 'eww-toggle-checkbox)
575 (define-key map [(control c) (control c)] 'eww-submit)
579 (let ((map (make-keymap)))
580 (set-keymap-parent map text-mode-map)
581 (define-key map "\r" 'eww-submit)
582 (define-key map [(control a)] 'eww-beginning-of-text)
583 (define-key map [(control c) (control c)] 'eww-submit)
584 (define-key map [(control e)] 'eww-end-of-text)
585 (define-key map [tab] 'shr-next-link
)
586 (define-key map
[backtab] 'shr-previous-link)
589 (defvar eww-textarea-map
590 (let ((map (make-keymap)))
591 (set-keymap-parent map text-mode-map)
592 (define-key map "\r" 'forward-line)
593 (define-key map [(control c) (control c)] 'eww-submit)
594 (define-key map [tab] 'shr-next-link)
595 (define-key map [backtab] 'shr-previous-link
)
598 (defvar eww-select-map
599 (let ((map (make-sparse-keymap)))
600 (define-key map
"\r" 'eww-change-select
)
601 (define-key map
[(control c
) (control c
)] 'eww-submit
)
604 (defun eww-beginning-of-text ()
605 "Move to the start of the input field."
607 (goto-char (eww-beginning-of-field)))
609 (defun eww-end-of-text ()
610 "Move to the end of the text in the input field."
612 (goto-char (eww-end-of-field))
613 (let ((start (eww-beginning-of-field)))
614 (while (and (equal (following-char) ?
)
617 (when (> (point) start
)
620 (defun eww-beginning-of-field ()
624 ((not (eq (get-text-property (point) 'eww-form
)
625 (get-text-property (1- (point)) 'eww-form
)))
628 (previous-single-property-change
629 (point) 'eww-form nil
(point-min)))))
631 (defun eww-end-of-field ()
632 (1- (next-single-property-change
633 (point) 'eww-form nil
(point-max))))
635 (defun eww-tag-form (cont)
637 (list (assq :method cont
)
638 (assq :action cont
)))
640 (shr-ensure-paragraph)
645 (when (> (point) start
)
646 (put-text-property start
(1+ start
)
647 'eww-form eww-form
))))
649 (defun eww-form-submit (cont)
650 (let ((start (point))
651 (value (cdr (assq :value cont
))))
653 (if (zerop (length value
))
657 (add-face-text-property start
(point) 'eww-form-submit
)
658 (put-text-property start
(point) 'eww-form
659 (list :eww-form eww-form
662 :name
(cdr (assq :name cont
))))
663 (put-text-property start
(point) 'keymap eww-submit-map
)
666 (defun eww-form-checkbox (cont)
667 (let ((start (point)))
668 (if (cdr (assq :checked cont
))
669 (insert eww-form-checkbox-selected-symbol
)
670 (insert eww-form-checkbox-symbol
))
671 (add-face-text-property start
(point) 'eww-form-checkbox
)
672 (put-text-property start
(point) 'eww-form
673 (list :eww-form eww-form
674 :value
(cdr (assq :value cont
))
675 :type
(downcase (cdr (assq :type cont
)))
676 :checked
(cdr (assq :checked cont
))
677 :name
(cdr (assq :name cont
))))
678 (put-text-property start
(point) 'keymap eww-checkbox-map
)
681 (defun eww-form-text (cont)
682 (let ((start (point))
683 (type (downcase (or (cdr (assq :type cont
))
685 (value (or (cdr (assq :value cont
)) ""))
686 (width (string-to-number
687 (or (cdr (assq :size cont
))
689 (readonly-property (if (or (cdr (assq :disabled cont
))
690 (cdr (assq :readonly cont
)))
692 'inhibit-read-only
)))
694 (when (< (length value
) width
)
695 (insert (make-string (- width
(length value
)) ?
)))
696 (put-text-property start
(point) 'face
'eww-form-text
)
697 (put-text-property start
(point) 'local-map eww-text-map
)
698 (put-text-property start
(point) readonly-property t
)
699 (put-text-property start
(point) 'eww-form
700 (list :eww-form eww-form
703 :name
(cdr (assq :name cont
))))
706 (defconst eww-text-input-types
'("text" "password" "textarea"
707 "color" "date" "datetime" "datetime-local"
708 "email" "month" "number" "search" "tel"
710 "List of input types which represent a text input.
711 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
713 (defun eww-process-text-input (beg end length
)
714 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
715 (properties (text-properties-at end
))
716 (type (plist-get form
:type
)))
718 (member type eww-text-input-types
))
721 ;; Delete some space at the end.
724 (if (equal type
"textarea")
725 (1- (line-end-position))
727 (let ((new (- end beg
)))
728 (while (and (> new
0)
729 (eql (following-char) ?
))
730 (delete-region (point) (1+ (point)))
731 (setq new
(1- new
))))
732 (set-text-properties beg end properties
)))
737 (if (equal type
"textarea")
738 (1- (line-end-position))
740 (let ((start (point)))
741 (insert (make-string length ?
))
742 (set-text-properties start
(point) properties
)))))
743 (let ((value (buffer-substring-no-properties
744 (eww-beginning-of-field)
745 (eww-end-of-field))))
746 (when (string-match " +\\'" value
)
747 (setq value
(substring value
0 (match-beginning 0))))
748 (plist-put form
:value value
)
749 (when (equal type
"password")
750 ;; Display passwords as asterisks.
751 (let ((start (eww-beginning-of-field)))
752 (put-text-property start
(+ start
(length value
))
753 'display
(make-string (length value
) ?
*))))))))
755 (defun eww-tag-textarea (cont)
756 (let ((start (point))
757 (value (or (cdr (assq :value cont
)) ""))
758 (lines (string-to-number
759 (or (cdr (assq :rows cont
))
761 (width (string-to-number
762 (or (cdr (assq :cols cont
))
768 (when (< (count-lines start
(point)) lines
)
769 (dotimes (i (- lines
(count-lines start
(point))))
771 (setq end
(point-marker))
773 (while (< (point) end
)
775 (let ((pad (- width
(- (point) (line-beginning-position)))))
777 (insert (make-string pad ?
))))
778 (add-face-text-property (line-beginning-position)
779 (point) 'eww-form-text
)
780 (put-text-property (line-beginning-position) (point)
781 'local-map eww-textarea-map
)
783 (put-text-property start
(point) 'eww-form
784 (list :eww-form eww-form
787 :name
(cdr (assq :name cont
))))))
789 (defun eww-tag-input (cont)
790 (let ((type (downcase (or (cdr (assq :type cont
))
794 ((or (equal type
"checkbox")
795 (equal type
"radio"))
796 (eww-form-checkbox cont
))
797 ((equal type
"submit")
798 (eww-form-submit cont
))
799 ((equal type
"hidden")
800 (let ((form eww-form
)
801 (name (cdr (assq :name cont
))))
802 ;; Don't add <input type=hidden> elements repeatedly.
804 (or (not (consp (car form
)))
805 (not (eq (caar form
) 'hidden
))
806 (not (equal (plist-get (cdr (car form
)) :name
)
808 (setq form
(cdr form
)))
810 (nconc eww-form
(list
813 :value
(cdr (assq :value cont
))))))))
815 (eww-form-text cont
)))
816 (unless (= start
(point))
817 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
819 (defun eww-tag-select (cont)
820 (shr-ensure-paragraph)
821 (let ((menu (list :name
(cdr (assq :name cont
))
827 (if (eq (car (car cont
)) 'optgroup
)
828 (dolist (groupelem cont
)
829 (unless (cdr (assq :disabled
(cdr groupelem
)))
830 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
832 (dolist (elem opelem
)
833 (when (eq (car elem
) 'option
)
834 (when (cdr (assq :selected
(cdr elem
)))
835 (nconc menu
(list :value
836 (cdr (assq :value
(cdr elem
))))))
837 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
838 (setq max
(max max
(length display
)))
840 :value
(cdr (assq :value
(cdr elem
)))
844 (setq options
(nreverse options
))
845 ;; If we have no selected values, default to the first value.
846 (unless (plist-get menu
:value
)
847 (nconc menu
(list :value
(nth 2 (car options
)))))
849 (let ((selected (eww-select-display menu
)))
851 (make-string (- max
(length selected
)) ?
)))
852 (put-text-property start
(point) 'eww-form menu
)
853 (add-face-text-property start
(point) 'eww-form-select
)
854 (put-text-property start
(point) 'keymap eww-select-map
)
855 (unless (= start
(point))
856 (put-text-property start
(1+ start
) 'help-echo
"select field"))
857 (shr-ensure-paragraph))))
859 (defun eww-select-display (select)
860 (let ((value (plist-get select
:value
))
862 (dolist (elem select
)
863 (when (and (consp elem
)
864 (eq (car elem
) 'item
)
865 (equal value
(plist-get (cdr elem
) :value
)))
866 (setq display
(plist-get (cdr elem
) :display
))))
869 (defun eww-change-select ()
870 "Change the value of the select drop-down menu under point."
872 (let* ((input (get-text-property (point) 'eww-form
))
873 (completion-ignore-case t
)
876 (mapcar (lambda (elem)
878 (eq (car elem
) 'item
)
879 (cons (plist-get (cdr elem
) :display
)
880 (plist-get (cdr elem
) :value
))))
883 (completing-read "Change value: " options nil
'require-match
))
884 (inhibit-read-only t
))
885 (plist-put input
:value
(cdr (assoc-string display options t
)))
887 (eww-update-field display
))))
889 (defun eww-update-field (string)
890 (let ((properties (text-properties-at (point)))
891 (start (eww-beginning-of-field))
892 (end (1+ (eww-end-of-field))))
893 (delete-region start end
)
895 (make-string (- (- end start
) (length string
)) ?
))
896 (set-text-properties start end properties
)
899 (defun eww-toggle-checkbox ()
900 "Toggle the value of the checkbox under point."
902 (let* ((input (get-text-property (point) 'eww-form
))
903 (type (plist-get input
:type
)))
904 (if (equal type
"checkbox")
907 (if (plist-get input
:checked
)
909 (plist-put input
:checked nil
)
910 (eww-update-field eww-form-checkbox-symbol
))
911 (plist-put input
:checked t
)
912 (eww-update-field eww-form-checkbox-selected-symbol
))))
913 ;; Radio button. Switch all other buttons off.
914 (let ((name (plist-get input
:name
)))
916 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
917 (when (equal (plist-get (cdr elem
) :name
) name
)
918 (goto-char (car elem
))
919 (if (not (eq (cdr elem
) input
))
921 (plist-put input
:checked nil
)
922 (eww-update-field eww-form-checkbox-symbol
))
923 (plist-put input
:checked t
)
924 (eww-update-field eww-form-checkbox-selected-symbol
)))))
927 (defun eww-inputs (form)
928 (let ((start (point-min))
931 (< start
(point-max)))
932 (when (or (get-text-property start
'eww-form
)
933 (setq start
(next-single-property-change start
'eww-form
)))
934 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
936 (push (cons start
(get-text-property start
'eww-form
))
938 (setq start
(next-single-property-change start
'eww-form
))))
941 (defun eww-input-value (input)
942 (let ((type (plist-get input
:type
))
943 (value (plist-get input
:value
)))
945 ((equal type
"textarea")
948 (goto-char (point-min))
949 (while (re-search-forward "^ +\n\\| +$" nil t
)
950 (replace-match "" t t
))
953 (if (string-match " +\\'" value
)
954 (substring value
0 (match-beginning 0))
958 "Submit the current form."
960 (let* ((this-input (get-text-property (point) 'eww-form
))
961 (form (plist-get this-input
:eww-form
))
963 (dolist (elem (sort (eww-inputs form
)
965 (< (car o1
) (car o2
)))))
966 (let* ((input (cdr elem
))
967 (input-start (car elem
))
968 (name (plist-get input
:name
)))
971 ((member (plist-get input
:type
) '("checkbox" "radio"))
972 (when (plist-get input
:checked
)
973 (push (cons name
(plist-get input
:value
))
975 ((equal (plist-get input
:type
) "submit")
976 ;; We want the values from buttons if we hit a button if
977 ;; we hit enter on it, or if it's the first button after
978 ;; the field we did hit return on.
979 (when (or (eq input this-input
)
980 (and (not (eq input this-input
))
982 (> input-start
(point))))
984 (push (cons name
(plist-get input
:value
))
987 (push (cons name
(eww-input-value input
))
990 (when (and (consp elem
)
991 (eq (car elem
) 'hidden
))
992 (push (cons (plist-get (cdr elem
) :name
)
993 (plist-get (cdr elem
) :value
))
995 (if (and (stringp (cdr (assq :method form
)))
996 (equal (downcase (cdr (assq :method form
))) "post"))
997 (let ((url-request-method "POST")
998 (url-request-extra-headers
999 '(("Content-Type" .
"application/x-www-form-urlencoded")))
1000 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
1001 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
1005 (if (cdr (assq :action form
))
1006 (shr-expand-url (cdr (assq :action form
))
1010 (mm-url-encode-www-form-urlencoded values
))))))
1012 (defun eww-browse-with-external-browser (&optional url
)
1013 "Browse the current URL with an external browser.
1014 The browser to used is specified by the `shr-external-browser' variable."
1016 (funcall shr-external-browser
(or url eww-current-url
)))
1018 (defun eww-follow-link (&optional external mouse-event
)
1019 "Browse the URL under point.
1020 If EXTERNAL, browse the URL using `shr-external-browser'."
1021 (interactive (list current-prefix-arg last-nonmenu-event
))
1022 (mouse-set-point mouse-event
)
1023 (let ((url (get-text-property (point) 'shr-url
)))
1026 (message "No link under point"))
1027 ((string-match "^mailto:" url
)
1028 (browse-url-mail url
))
1030 (funcall shr-external-browser url
))
1031 ;; This is a #target url in the same page as the current one.
1032 ((and (url-target (url-generic-parse-url url
))
1033 (eww-same-page-p url eww-current-url
))
1035 (eww-display-html 'utf8 url eww-current-dom
))
1037 (eww-browse-url url
)))))
1039 (defun eww-same-page-p (url1 url2
)
1040 "Return non-nil if both URLs represent the same page.
1041 Differences in #targets are ignored."
1042 (let ((obj1 (url-generic-parse-url url1
))
1043 (obj2 (url-generic-parse-url url2
)))
1044 (setf (url-target obj1
) nil
)
1045 (setf (url-target obj2
) nil
)
1046 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1048 (defun eww-copy-page-url ()
1050 (message "%s" eww-current-url
)
1051 (kill-new eww-current-url
))
1053 (defun eww-download ()
1054 "Download URL under point to `eww-download-directory'."
1056 (let ((url (get-text-property (point) 'shr-url
)))
1058 (message "No URL under point")
1059 (url-retrieve url
'eww-download-callback
(list url
)))))
1061 (defun eww-download-callback (status url
)
1062 (unless (plist-get status
:error
)
1063 (let* ((obj (url-generic-parse-url url
))
1064 (path (car (url-path-and-query obj
)))
1065 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1066 eww-download-path
)))
1068 (message "Saved %s" file
))))
1070 (defun eww-make-unique-file-name (file directory
)
1072 ((zerop (length file
))
1074 ((string-match "\\`[.]" file
)
1075 (setq file
(concat "!" file
))))
1077 (while (file-exists-p (expand-file-name file directory
))
1079 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1080 (format "%s(%d)%s" (match-string 1 file
)
1081 count
(match-string 2 file
))
1082 (format "%s(%d)" file count
)))
1083 (setq count
(1+ count
)))
1084 (expand-file-name file directory
)))
1088 (defvar eww-bookmarks nil
)
1090 (defun eww-add-bookmark ()
1091 "Add the current page to the bookmarks."
1093 (eww-read-bookmarks)
1094 (dolist (bookmark eww-bookmarks
)
1095 (when (equal eww-current-url
1096 (plist-get bookmark
:url
))
1097 (user-error "Already bookmarked")))
1098 (if (y-or-n-p "bookmark this page? ")
1100 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1101 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1102 (push (list :url eww-current-url
1104 :time
(current-time-string))
1106 (eww-write-bookmarks)
1107 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1109 (defun eww-write-bookmarks ()
1110 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1111 (insert ";; Auto-generated file; don't edit\n")
1112 (pp eww-bookmarks
(current-buffer))))
1114 (defun eww-read-bookmarks ()
1115 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1117 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1119 (insert-file-contents file
)
1120 (read (current-buffer)))))))
1122 (defun eww-list-bookmarks ()
1123 "Display the bookmarks."
1125 (eww-bookmark-prepare)
1126 (pop-to-buffer "*eww bookmarks*"))
1128 (defun eww-bookmark-prepare ()
1129 (eww-read-bookmarks)
1130 (unless eww-bookmarks
1131 (user-error "No bookmarks are defined"))
1132 (set-buffer (get-buffer-create "*eww bookmarks*"))
1134 (let ((format "%-40s %s")
1135 (inhibit-read-only t
)
1138 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1139 (dolist (bookmark eww-bookmarks
)
1140 (setq start
(point))
1141 (setq url
(plist-get bookmark
:url
))
1142 (when (> (length url
) 40)
1143 (setq url
(substring url
0 40)))
1144 (insert (format format url
1145 (plist-get bookmark
:title
))
1147 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1148 (goto-char (point-min))))
1150 (defvar eww-bookmark-kill-ring nil
)
1152 (defun eww-bookmark-kill ()
1153 "Kill the current bookmark."
1155 (let* ((start (line-beginning-position))
1156 (bookmark (get-text-property start
'eww-bookmark
))
1157 (inhibit-read-only t
))
1159 (user-error "No bookmark on the current line"))
1161 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1162 (delete-region start
(point))
1163 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1164 (eww-write-bookmarks)))
1166 (defun eww-bookmark-yank ()
1167 "Yank a previously killed bookmark to the current line."
1169 (unless eww-bookmark-kill-ring
1170 (user-error "No previously killed bookmark"))
1172 (let ((inhibit-read-only t
)
1175 (insert (pop eww-bookmark-kill-ring
))
1176 (setq bookmark
(get-text-property start
'eww-bookmark
))
1177 (if (= start
(point-min))
1178 (push bookmark eww-bookmarks
)
1179 (let ((line (count-lines start
(point))))
1180 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1181 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1182 (eww-write-bookmarks)))
1184 (defun eww-bookmark-browse ()
1185 "Browse the bookmark under point in eww."
1187 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1189 (user-error "No bookmark on the current line"))
1191 (eww-browse-url (plist-get bookmark
:url
))))
1193 (defun eww-next-bookmark ()
1194 "Go to the next bookmark in the list."
1198 (unless (get-buffer "*eww bookmarks*")
1200 (eww-bookmark-prepare))
1201 (with-current-buffer (get-buffer "*eww bookmarks*")
1202 (when (and (not first
)
1205 (setq bookmark
(get-text-property (line-beginning-position)
1208 (user-error "No next bookmark")))
1209 (eww-browse-url (plist-get bookmark
:url
))))
1211 (defun eww-previous-bookmark ()
1212 "Go to the previous bookmark in the list."
1216 (unless (get-buffer "*eww bookmarks*")
1218 (eww-bookmark-prepare))
1219 (with-current-buffer (get-buffer "*eww bookmarks*")
1221 (goto-char (point-max))
1222 (beginning-of-line))
1223 ;; On the final line.
1227 (user-error "No previous bookmark")
1229 (setq bookmark
(get-text-property (line-beginning-position)
1231 (eww-browse-url (plist-get bookmark
:url
))))
1233 (defvar eww-bookmark-mode-map
1234 (let ((map (make-sparse-keymap)))
1235 (suppress-keymap map
)
1236 (define-key map
"q" 'quit-window
)
1237 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1238 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1239 (define-key map
"\r" 'eww-bookmark-browse
)
1241 (easy-menu-define nil map
1242 "Menu for `eww-bookmark-mode-map'."
1244 ["Exit" quit-window t
]
1245 ["Browse" eww-bookmark-browse
1246 :active
(get-text-property (line-beginning-position) 'eww-bookmark
)]
1247 ["Kill" eww-bookmark-kill
1248 :active
(get-text-property (line-beginning-position) 'eww-bookmark
)]
1249 ["Yank" eww-bookmark-yank
1250 :active eww-bookmark-kill-ring
]))
1253 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1254 "Mode for listing bookmarks.
1256 \\{eww-bookmark-mode-map}"
1257 (buffer-disable-undo)
1258 (setq buffer-read-only t
1263 (defun eww-save-history ()
1264 (push (list :url eww-current-url
1265 :title eww-current-title
1267 :dom eww-current-dom
1268 :source eww-current-source
1269 :text
(buffer-string))
1272 (defun eww-list-histories ()
1273 "List the eww-histories."
1275 (when (null eww-history
)
1276 (error "No eww-histories are defined"))
1277 (set-buffer (get-buffer-create "*eww history*"))
1279 (let ((inhibit-read-only t
)
1282 url title format start
)
1284 (dolist (history eww-history
)
1285 (setq start
(point))
1286 (setq domain-length
(max domain-length
(length (plist-get history
:url
))))
1287 (setq title-length
(max title-length
(length (plist-get history
:title
))))
1289 (setq format
(format "%%-%ds %%-%ds" title-length domain-length
)
1291 (concat " " (format format
"Title" "URL")))
1293 (dolist (history eww-history
)
1294 (setq url
(plist-get history
:url
))
1295 (setq title
(plist-get history
:title
))
1296 (insert (format format title url
))
1298 (put-text-property start
(point) 'eww-history history
)
1300 (goto-char (point-min)))
1301 (pop-to-buffer "*eww history*")
1304 (defun eww-history-browse ()
1305 "Browse the history under point in eww."
1307 (let ((history (get-text-property (line-beginning-position) 'eww-history
)))
1309 (error "No history on the current line"))
1311 (pop-to-buffer "*eww*")
1312 (eww-browse-url (plist-get history
:url
))))
1314 (defun eww-history-quit ()
1315 "Kill the current buffer."
1317 (kill-buffer (current-buffer)))
1319 (defvar eww-history-kill-ring nil
)
1321 (defun eww-history-kill ()
1322 "Kill the current history."
1324 (let* ((start (line-beginning-position))
1325 (history (get-text-property start
'eww-history
))
1326 (inhibit-read-only t
))
1328 (error "No history on the current line"))
1330 (push (buffer-substring start
(point)) eww-history-kill-ring
)
1331 (delete-region start
(point))
1332 (setq eww-history
(delq history eww-history
))
1335 (defvar eww-history-mode-map
1336 (let ((map (make-sparse-keymap)))
1337 (suppress-keymap map
)
1338 (define-key map
"q" 'eww-history-quit
)
1339 (define-key map
[(control k
)] 'eww-history-kill
)
1340 (define-key map
"\r" 'eww-history-browse
)
1341 (define-key map
"n" 'next-error-no-select
)
1342 (define-key map
"p" 'previous-error-no-select
)
1345 (define-derived-mode eww-history-mode nil
"eww history"
1346 "Mode for listing eww-histories.
1348 \\{eww-history-mode-map}"
1349 (buffer-disable-undo)
1350 (setq buffer-read-only t
1355 ;;; eww.el ends here