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
))
149 (if (and (= (length (split-string url
)) 1)
150 (or (> (length (split-string url
"\\.")) 1)
151 (string-match eww-local-regex url
)))
153 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
154 (setq url
(concat "http://" url
)))
155 ;; some site don't redirect final /
156 (when (string= (url-filename (url-generic-parse-url url
)) "")
157 (setq url
(concat url
"/"))))
158 (setq url
(concat eww-search-prefix
159 (replace-regexp-in-string " " "+" url
))))))
160 (url-retrieve url
'eww-render
(list url
)))
163 (defun eww-open-file (file)
164 "Render a file using EWW."
165 (interactive "fFile: ")
166 (eww (concat "file://" (expand-file-name file
))))
168 (defun eww-render (status url
&optional point
)
169 (let ((redirect (plist-get status
:redirect
)))
171 (setq url redirect
)))
172 (set (make-local-variable 'eww-next-url
) nil
)
173 (set (make-local-variable 'eww-previous-url
) nil
)
174 (set (make-local-variable 'eww-up-url
) nil
)
175 (set (make-local-variable 'eww-home-url
) nil
)
176 (set (make-local-variable 'eww-start-url
) nil
)
177 (set (make-local-variable 'eww-contents-url
) nil
)
178 (let* ((headers (eww-parse-headers))
180 (mail-header-parse-content-type
181 (or (cdr (assoc "content-type" headers
))
185 (or (cdr (assq 'charset
(cdr content-type
)))
186 (eww-detect-charset (equal (car content-type
)
189 (data-buffer (current-buffer)))
192 (setq eww-current-title
"")
194 ((and eww-use-external-browser-for-content-type
195 (string-match-p eww-use-external-browser-for-content-type
197 (eww-browse-with-external-browser url
))
198 ((equal (car content-type
) "text/html")
199 (eww-display-html charset url nil point
))
200 ((string-match-p "\\`image/" (car content-type
))
202 (eww-update-header-line-format))
205 (eww-update-header-line-format)))
206 (setq eww-current-url url
207 eww-history-position
0))
208 (kill-buffer data-buffer
))))
210 (defun eww-parse-headers ()
212 (goto-char (point-min))
213 (while (and (not (eobp))
215 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
216 (push (cons (downcase (match-string 1))
224 (defun eww-detect-charset (html-p)
225 (let ((case-fold-search t
)
229 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
233 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
236 (declare-function libxml-parse-html-region
"xml.c"
237 (start end
&optional base-url
))
239 (defun eww-display-html (charset url
&optional document point
)
240 (or (fboundp 'libxml-parse-html-region
)
241 (error "This function requires Emacs to be compiled with libxml2"))
242 (unless (eq charset
'utf8
)
244 (decode-coding-region (point) (point-max) charset
)
245 (coding-system-error nil
)))
249 'base
(list (cons 'href url
))
250 (libxml-parse-html-region (point) (point-max))))))
251 (setq eww-current-source
(buffer-substring (point) (point-max)))
253 (setq eww-current-dom document
)
254 (let ((inhibit-read-only t
)
255 (after-change-functions nil
)
257 (shr-target-id (url-target (url-generic-parse-url url
)))
258 (shr-external-rendering-functions
259 '((title . eww-tag-title
)
260 (form . eww-tag-form
)
261 (input . eww-tag-input
)
262 (textarea . eww-tag-textarea
)
263 (body . eww-tag-body
)
264 (select . eww-tag-select
)
265 (link . eww-tag-link
)
267 (shr-insert-document document
)
272 (let ((point (next-single-property-change
273 (point-min) 'shr-target-id
)))
275 (goto-char (1+ point
)))))
277 (goto-char (point-min)))))
278 (setq eww-current-url url
279 eww-history-position
0)
280 (eww-update-header-line-format)))
282 (defun eww-handle-link (cont)
283 (let* ((rel (assq :rel cont
))
284 (href (assq :href cont
))
286 ;; The text associated with :rel is case-insensitive.
287 (if rel
(downcase (cdr rel
)))
288 '(("next" . eww-next-url
)
289 ;; Texinfo uses "previous", but HTML specifies
290 ;; "prev", so recognize both.
291 ("previous" . eww-previous-url
)
292 ("prev" . eww-previous-url
)
293 ;; HTML specifies "start" but also "contents",
294 ;; and Gtk seems to use "home". Recognize
295 ;; them all; but store them in different
296 ;; variables so that we can readily choose the
298 ("start" . eww-start-url
)
299 ("home" . eww-home-url
)
300 ("contents" . eww-contents-url
)
301 ("up" . eww-up-url
)))))
304 (set (cdr where
) (cdr href
)))))
306 (defun eww-tag-link (cont)
307 (eww-handle-link cont
)
310 (defun eww-tag-a (cont)
311 (eww-handle-link cont
)
312 (let ((start (point)))
314 (put-text-property start
(point) 'keymap eww-link-keymap
)))
316 (defun eww-update-header-line-format ()
317 (if eww-header-line-format
318 (setq header-line-format
319 (replace-regexp-in-string
321 ;; FIXME? Title can be blank. Default to, eg, last component
323 (format-spec eww-header-line-format
324 `((?u .
,eww-current-url
)
325 (?t .
,eww-current-title
)))))
326 (setq header-line-format nil
)))
328 (defun eww-tag-title (cont)
329 (setq eww-current-title
"")
331 (when (eq (car sub
) 'text
)
332 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
333 (eww-update-header-line-format))
335 (defun eww-tag-body (cont)
336 (let* ((start (point))
337 (fgcolor (cdr (or (assq :fgcolor cont
)
339 (bgcolor (cdr (assq :bgcolor cont
)))
340 (shr-stylesheet (list (cons 'color fgcolor
)
341 (cons 'background-color bgcolor
))))
343 (eww-colorize-region start
(point) fgcolor bgcolor
)))
345 (defun eww-colorize-region (start end fg
&optional bg
)
347 (let ((new-colors (shr-color-check fg bg
)))
350 (add-face-text-property start end
351 (list :foreground
(cadr new-colors
))
354 (add-face-text-property start end
355 (list :background
(car new-colors
))
358 (defun eww-display-raw ()
359 (let ((data (buffer-substring (point) (point-max))))
361 (let ((inhibit-read-only t
))
363 (goto-char (point-min))))
365 (defun eww-display-image ()
366 (let ((data (shr-parse-image-data)))
368 (let ((inhibit-read-only t
))
369 (shr-put-image data nil
))
370 (goto-char (point-min))))
372 (defun eww-setup-buffer ()
373 (switch-to-buffer (get-buffer-create "*eww*"))
374 (let ((inhibit-read-only t
))
377 (unless (eq major-mode
'eww-mode
)
380 (defun eww-view-source ()
382 (let ((buf (get-buffer-create "*eww-source*"))
383 (source eww-current-source
))
384 (with-current-buffer buf
385 (delete-region (point-min) (point-max))
386 (insert (or eww-current-source
"no source"))
387 (goto-char (point-min))
388 (when (featurep 'html-mode
)
393 (let ((map (make-sparse-keymap)))
394 (suppress-keymap map
)
395 (define-key map
"q" 'eww-close
)
396 (define-key map
"Q" 'eww-exit
)
397 (define-key map
"g" 'eww-reload
)
398 (define-key map
[tab] 'shr-next-link)
399 (define-key map [backtab] 'shr-previous-link)
400 (define-key map [delete] 'scroll-down-command)
401 (define-key map "\177" 'scroll-down-command)
402 (define-key map " " 'scroll-up-command)
403 (define-key map "l" 'eww-back-url)
404 (define-key map "f" 'eww-forward-url)
405 (define-key map "n" 'eww-next-url)
406 (define-key map "p" 'eww-previous-url)
407 (define-key map "u" 'eww-up-url)
408 (define-key map "t" 'eww-top-url)
409 (define-key map "&" 'eww-browse-with-external-browser)
410 (define-key map "d" 'eww-download)
411 (define-key map "w" 'eww-copy-page-url)
412 (define-key map "C" 'url-cookie-list)
413 (define-key map "v" 'eww-view-source)
415 (define-key map "b" 'eww-add-bookmark)
416 (define-key map "B" 'eww-list-bookmarks)
417 (define-key map [(meta n)] 'eww-next-bookmark)
418 (define-key map [(meta p)] 'eww-previous-bookmark)
420 (easy-menu-define nil map ""
423 ["Close browser" eww-close t]
424 ["Reload" eww-reload t]
425 ["Back to previous page" eww-back-url
426 :active (not (zerop (length eww-history)))]
427 ["Forward to next page" eww-forward-url
428 :active (not (zerop eww-history-position))]
429 ["Browse with external browser" eww-browse-with-external-browser t]
430 ["Download" eww-download t]
431 ["View page source" eww-view-source]
432 ["Copy page URL" eww-copy-page-url t]
433 ["Add bookmark" eww-add-bookmark t]
434 ["List bookmarks" eww-list-bookmarks t]
435 ["List cookies" url-cookie-list t]))
438 (define-derived-mode eww-mode nil "eww"
439 "Mode for browsing the web.
442 ;; FIXME? This seems a strange default.
443 (set (make-local-variable 'eww-current-url) 'author)
444 (set (make-local-variable 'eww-current-dom) nil)
445 (set (make-local-variable 'eww-current-source) nil)
446 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
447 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
448 (set (make-local-variable 'eww-history) nil)
449 (set (make-local-variable 'eww-history-position) 0)
450 (buffer-disable-undo)
451 ;;(setq buffer-read-only t)
455 "Exit the Emacs Web Wowser."
457 (setq eww-history nil)
458 (kill-buffer (current-buffer)))
461 "Close the Emacs Web Wowser browser, leaving history intact."
465 (defun eww-save-history ()
466 (push (list :url eww-current-url
467 :title eww-current-title
470 :source eww-current-source
471 :text (buffer-string))
475 (defun eww-browse-url (url &optional _new-window)
476 (when (and (equal major-mode 'eww-mode)
481 (defun eww-back-url ()
482 "Go to the previously displayed page."
484 (when (>= eww-history-position (length eww-history))
485 (error "No previous page"))
487 (setq eww-history-position (+ eww-history-position 2))
488 (eww-restore-history (elt eww-history (1- eww-history-position))))
490 (defun eww-forward-url ()
491 "Go to the next displayed page."
493 (when (zerop eww-history-position)
494 (error "No next page"))
496 (eww-restore-history (elt eww-history (1- eww-history-position))))
498 (defun eww-restore-history (elem)
499 (let ((inhibit-read-only t))
501 (insert (plist-get elem :text))
502 (setq eww-current-source (plist-get elem :source))
503 (setq eww-current-dom (plist-get elem :dom))
504 (goto-char (plist-get elem :point))
505 (setq eww-current-url (plist-get elem :url)
506 eww-current-title (plist-get elem :title))
507 (eww-update-header-line-format)))
509 (defun eww-next-url ()
510 "Go to the page marked `next'.
511 A page is marked `next' if rel=\"next\" appears in a <link>
515 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
516 (error "No `next' on this page")))
518 (defun eww-previous-url ()
519 "Go to the page marked `previous'.
520 A page is marked `previous' if rel=\"previous\" appears in a <link>
524 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
525 (error "No `previous' on this page")))
528 "Go to the page marked `up'.
529 A page is marked `up' if rel=\"up\" appears in a <link>
533 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
534 (error "No `up' on this page")))
536 (defun eww-top-url ()
537 "Go to the page marked `top'.
538 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
539 appears in a <link> or <a> tag."
541 (let ((best-url (or eww-start-url
545 (eww-browse-url (shr-expand-url best-url eww-current-url))
546 (error "No `top' for this page"))))
549 "Reload the current page."
551 (url-retrieve eww-current-url 'eww-render
552 (list eww-current-url (point))))
556 (defvar eww-form nil)
558 (defvar eww-submit-map
559 (let ((map (make-sparse-keymap)))
560 (define-key map "\r" 'eww-submit)
561 (define-key map [(control c) (control c)] 'eww-submit)
564 (defvar eww-checkbox-map
565 (let ((map (make-sparse-keymap)))
566 (define-key map [space] 'eww-toggle-checkbox)
567 (define-key map "\r" 'eww-toggle-checkbox)
568 (define-key map [(control c) (control c)] 'eww-submit)
572 (let ((map (make-keymap)))
573 (set-keymap-parent map text-mode-map)
574 (define-key map "\r" 'eww-submit)
575 (define-key map [(control a)] 'eww-beginning-of-text)
576 (define-key map [(control c) (control c)] 'eww-submit)
577 (define-key map [(control e)] 'eww-end-of-text)
578 (define-key map [tab] 'shr-next-link
)
579 (define-key map
[backtab] 'shr-previous-link)
582 (defvar eww-textarea-map
583 (let ((map (make-keymap)))
584 (set-keymap-parent map text-mode-map)
585 (define-key map "\r" 'forward-line)
586 (define-key map [(control c) (control c)] 'eww-submit)
587 (define-key map [tab] 'shr-next-link)
588 (define-key map [backtab] 'shr-previous-link
)
591 (defvar eww-select-map
592 (let ((map (make-sparse-keymap)))
593 (define-key map
"\r" 'eww-change-select
)
594 (define-key map
[(control c
) (control c
)] 'eww-submit
)
597 (defun eww-beginning-of-text ()
598 "Move to the start of the input field."
600 (goto-char (eww-beginning-of-field)))
602 (defun eww-end-of-text ()
603 "Move to the end of the text in the input field."
605 (goto-char (eww-end-of-field))
606 (let ((start (eww-beginning-of-field)))
607 (while (and (equal (following-char) ?
)
610 (when (> (point) start
)
613 (defun eww-beginning-of-field ()
617 ((not (eq (get-text-property (point) 'eww-form
)
618 (get-text-property (1- (point)) 'eww-form
)))
621 (previous-single-property-change
622 (point) 'eww-form nil
(point-min)))))
624 (defun eww-end-of-field ()
625 (1- (next-single-property-change
626 (point) 'eww-form nil
(point-max))))
628 (defun eww-tag-form (cont)
630 (list (assq :method cont
)
631 (assq :action cont
)))
633 (shr-ensure-paragraph)
638 (when (> (point) start
)
639 (put-text-property start
(1+ start
)
640 'eww-form eww-form
))))
642 (defun eww-form-submit (cont)
643 (let ((start (point))
644 (value (cdr (assq :value cont
))))
646 (if (zerop (length value
))
650 (add-face-text-property start
(point) 'eww-form-submit
)
651 (put-text-property start
(point) 'eww-form
652 (list :eww-form eww-form
655 :name
(cdr (assq :name cont
))))
656 (put-text-property start
(point) 'keymap eww-submit-map
)
659 (defun eww-form-checkbox (cont)
660 (let ((start (point)))
661 (if (cdr (assq :checked cont
))
662 (insert eww-form-checkbox-selected-symbol
)
663 (insert eww-form-checkbox-symbol
))
664 (add-face-text-property start
(point) 'eww-form-checkbox
)
665 (put-text-property start
(point) 'eww-form
666 (list :eww-form eww-form
667 :value
(cdr (assq :value cont
))
668 :type
(downcase (cdr (assq :type cont
)))
669 :checked
(cdr (assq :checked cont
))
670 :name
(cdr (assq :name cont
))))
671 (put-text-property start
(point) 'keymap eww-checkbox-map
)
674 (defun eww-form-text (cont)
675 (let ((start (point))
676 (type (downcase (or (cdr (assq :type cont
))
678 (value (or (cdr (assq :value cont
)) ""))
679 (width (string-to-number
680 (or (cdr (assq :size cont
))
683 (when (< (length value
) width
)
684 (insert (make-string (- width
(length value
)) ?
)))
685 (put-text-property start
(point) 'face
'eww-form-text
)
686 (put-text-property start
(point) 'local-map eww-text-map
)
687 (put-text-property start
(point) 'inhibit-read-only t
)
688 (put-text-property start
(point) 'eww-form
689 (list :eww-form eww-form
692 :name
(cdr (assq :name cont
))))
695 (defconst eww-text-input-types
'("text" "password" "textarea"
696 "color" "date" "datetime" "datetime-local"
697 "email" "month" "number" "search" "tel"
699 "List of input types which represent a text input.
700 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
702 (defun eww-process-text-input (beg end length
)
703 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
704 (properties (text-properties-at end
))
705 (type (plist-get form
:type
)))
707 (member type eww-text-input-types
))
710 ;; Delete some space at the end.
713 (if (equal type
"textarea")
714 (1- (line-end-position))
716 (let ((new (- end beg
)))
717 (while (and (> new
0)
718 (eql (following-char) ?
))
719 (delete-region (point) (1+ (point)))
720 (setq new
(1- new
))))
721 (set-text-properties beg end properties
)))
726 (if (equal type
"textarea")
727 (1- (line-end-position))
729 (let ((start (point)))
730 (insert (make-string length ?
))
731 (set-text-properties start
(point) properties
)))))
732 (let ((value (buffer-substring-no-properties
733 (eww-beginning-of-field)
734 (eww-end-of-field))))
735 (when (string-match " +\\'" value
)
736 (setq value
(substring value
0 (match-beginning 0))))
737 (plist-put form
:value value
)
738 (when (equal type
"password")
739 ;; Display passwords as asterisks.
740 (let ((start (eww-beginning-of-field)))
741 (put-text-property start
(+ start
(length value
))
742 'display
(make-string (length value
) ?
*))))))))
744 (defun eww-tag-textarea (cont)
745 (let ((start (point))
746 (value (or (cdr (assq :value cont
)) ""))
747 (lines (string-to-number
748 (or (cdr (assq :rows cont
))
750 (width (string-to-number
751 (or (cdr (assq :cols cont
))
757 (when (< (count-lines start
(point)) lines
)
758 (dotimes (i (- lines
(count-lines start
(point))))
760 (setq end
(point-marker))
762 (while (< (point) end
)
764 (let ((pad (- width
(- (point) (line-beginning-position)))))
766 (insert (make-string pad ?
))))
767 (add-face-text-property (line-beginning-position)
768 (point) 'eww-form-text
)
769 (put-text-property (line-beginning-position) (point)
770 'local-map eww-textarea-map
)
772 (put-text-property start
(point) 'eww-form
773 (list :eww-form eww-form
776 :name
(cdr (assq :name cont
))))))
778 (defun eww-tag-input (cont)
779 (let ((type (downcase (or (cdr (assq :type cont
))
783 ((or (equal type
"checkbox")
784 (equal type
"radio"))
785 (eww-form-checkbox cont
))
786 ((equal type
"submit")
787 (eww-form-submit cont
))
788 ((equal type
"hidden")
789 (let ((form eww-form
)
790 (name (cdr (assq :name cont
))))
791 ;; Don't add <input type=hidden> elements repeatedly.
793 (or (not (consp (car form
)))
794 (not (eq (caar form
) 'hidden
))
795 (not (equal (plist-get (cdr (car form
)) :name
)
797 (setq form
(cdr form
)))
799 (nconc eww-form
(list
802 :value
(cdr (assq :value cont
))))))))
804 (eww-form-text cont
)))
805 (unless (= start
(point))
806 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
808 (defun eww-tag-select (cont)
809 (shr-ensure-paragraph)
810 (let ((menu (list :name
(cdr (assq :name cont
))
816 (if (eq (car (car cont
)) 'optgroup
)
817 (dolist (groupelem cont
)
818 (unless (cdr (assq :disabled
(cdr groupelem
)))
819 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
821 (dolist (elem opelem
)
822 (when (eq (car elem
) 'option
)
823 (when (cdr (assq :selected
(cdr elem
)))
824 (nconc menu
(list :value
825 (cdr (assq :value
(cdr elem
))))))
826 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
827 (setq max
(max max
(length display
)))
829 :value
(cdr (assq :value
(cdr elem
)))
833 (setq options
(nreverse options
))
834 ;; If we have no selected values, default to the first value.
835 (unless (plist-get menu
:value
)
836 (nconc menu
(list :value
(nth 2 (car options
)))))
838 (let ((selected (eww-select-display menu
)))
840 (make-string (- max
(length selected
)) ?
)))
841 (put-text-property start
(point) 'eww-form menu
)
842 (add-face-text-property start
(point) 'eww-form-select
)
843 (put-text-property start
(point) 'keymap eww-select-map
)
844 (shr-ensure-paragraph))))
846 (defun eww-select-display (select)
847 (let ((value (plist-get select
:value
))
849 (dolist (elem select
)
850 (when (and (consp elem
)
851 (eq (car elem
) 'item
)
852 (equal value
(plist-get (cdr elem
) :value
)))
853 (setq display
(plist-get (cdr elem
) :display
))))
856 (defun eww-change-select ()
857 "Change the value of the select drop-down menu under point."
859 (let* ((input (get-text-property (point) 'eww-form
))
860 (completion-ignore-case t
)
863 (mapcar (lambda (elem)
865 (eq (car elem
) 'item
)
866 (cons (plist-get (cdr elem
) :display
)
867 (plist-get (cdr elem
) :value
))))
870 (completing-read "Change value: " options nil
'require-match
))
871 (inhibit-read-only t
))
872 (plist-put input
:value
(cdr (assoc-string display options t
)))
874 (eww-update-field display
))))
876 (defun eww-update-field (string)
877 (let ((properties (text-properties-at (point)))
878 (start (eww-beginning-of-field))
879 (end (1+ (eww-end-of-field))))
880 (delete-region start end
)
882 (make-string (- (- end start
) (length string
)) ?
))
883 (set-text-properties start end properties
)
886 (defun eww-toggle-checkbox ()
887 "Toggle the value of the checkbox under point."
889 (let* ((input (get-text-property (point) 'eww-form
))
890 (type (plist-get input
:type
)))
891 (if (equal type
"checkbox")
894 (if (plist-get input
:checked
)
896 (plist-put input
:checked nil
)
897 (eww-update-field eww-form-checkbox-symbol
))
898 (plist-put input
:checked t
)
899 (eww-update-field eww-form-checkbox-selected-symbol
))))
900 ;; Radio button. Switch all other buttons off.
901 (let ((name (plist-get input
:name
)))
903 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
904 (when (equal (plist-get (cdr elem
) :name
) name
)
905 (goto-char (car elem
))
906 (if (not (eq (cdr elem
) input
))
908 (plist-put input
:checked nil
)
909 (eww-update-field eww-form-checkbox-symbol
))
910 (plist-put input
:checked t
)
911 (eww-update-field eww-form-checkbox-selected-symbol
)))))
914 (defun eww-inputs (form)
915 (let ((start (point-min))
918 (< start
(point-max)))
919 (when (or (get-text-property start
'eww-form
)
920 (setq start
(next-single-property-change start
'eww-form
)))
921 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
923 (push (cons start
(get-text-property start
'eww-form
))
925 (setq start
(next-single-property-change start
'eww-form
))))
928 (defun eww-input-value (input)
929 (let ((type (plist-get input
:type
))
930 (value (plist-get input
:value
)))
932 ((equal type
"textarea")
935 (goto-char (point-min))
936 (while (re-search-forward "^ +\n\\| +$" nil t
)
937 (replace-match "" t t
))
940 (if (string-match " +\\'" value
)
941 (substring value
0 (match-beginning 0))
945 "Submit the current form."
947 (let* ((this-input (get-text-property (point) 'eww-form
))
948 (form (plist-get this-input
:eww-form
))
950 (dolist (elem (sort (eww-inputs form
)
952 (< (car o1
) (car o2
)))))
953 (let* ((input (cdr elem
))
954 (input-start (car elem
))
955 (name (plist-get input
:name
)))
958 ((member (plist-get input
:type
) '("checkbox" "radio"))
959 (when (plist-get input
:checked
)
960 (push (cons name
(plist-get input
:value
))
962 ((equal (plist-get input
:type
) "submit")
963 ;; We want the values from buttons if we hit a button if
964 ;; we hit enter on it, or if it's the first button after
965 ;; the field we did hit return on.
966 (when (or (eq input this-input
)
967 (and (not (eq input this-input
))
969 (> input-start
(point))))
971 (push (cons name
(plist-get input
:value
))
974 (push (cons name
(eww-input-value input
))
977 (when (and (consp elem
)
978 (eq (car elem
) 'hidden
))
979 (push (cons (plist-get (cdr elem
) :name
)
980 (plist-get (cdr elem
) :value
))
982 (if (and (stringp (cdr (assq :method form
)))
983 (equal (downcase (cdr (assq :method form
))) "post"))
984 (let ((url-request-method "POST")
985 (url-request-extra-headers
986 '(("Content-Type" .
"application/x-www-form-urlencoded")))
987 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
988 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
992 (if (cdr (assq :action form
))
993 (shr-expand-url (cdr (assq :action form
))
997 (mm-url-encode-www-form-urlencoded values
))))))
999 (defun eww-browse-with-external-browser (&optional url
)
1000 "Browse the current URL with an external browser.
1001 The browser to used is specified by the `shr-external-browser' variable."
1003 (funcall shr-external-browser
(or url eww-current-url
)))
1005 (defun eww-follow-link (&optional external mouse-event
)
1006 "Browse the URL under point.
1007 If EXTERNAL, browse the URL using `shr-external-browser'."
1008 (interactive (list current-prefix-arg last-nonmenu-event
))
1009 (mouse-set-point mouse-event
)
1010 (let ((url (get-text-property (point) 'shr-url
)))
1013 (message "No link under point"))
1014 ((string-match "^mailto:" url
)
1015 (browse-url-mail url
))
1017 (funcall shr-external-browser url
))
1018 ;; This is a #target url in the same page as the current one.
1019 ((and (url-target (url-generic-parse-url url
))
1020 (eww-same-page-p url eww-current-url
))
1022 (eww-display-html 'utf8 url eww-current-dom
))
1024 (eww-browse-url url
)))))
1026 (defun eww-same-page-p (url1 url2
)
1027 "Return non-nil if both URLs represent the same page.
1028 Differences in #targets are ignored."
1029 (let ((obj1 (url-generic-parse-url url1
))
1030 (obj2 (url-generic-parse-url url2
)))
1031 (setf (url-target obj1
) nil
)
1032 (setf (url-target obj2
) nil
)
1033 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1035 (defun eww-copy-page-url ()
1037 (message "%s" eww-current-url
)
1038 (kill-new eww-current-url
))
1040 (defun eww-download ()
1041 "Download URL under point to `eww-download-directory'."
1043 (let ((url (get-text-property (point) 'shr-url
)))
1045 (message "No URL under point")
1046 (url-retrieve url
'eww-download-callback
(list url
)))))
1048 (defun eww-download-callback (status url
)
1049 (unless (plist-get status
:error
)
1050 (let* ((obj (url-generic-parse-url url
))
1051 (path (car (url-path-and-query obj
)))
1052 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1053 eww-download-path
)))
1055 (message "Saved %s" file
))))
1057 (defun eww-make-unique-file-name (file directory
)
1059 ((zerop (length file
))
1061 ((string-match "\\`[.]" file
)
1062 (setq file
(concat "!" file
))))
1064 (while (file-exists-p (expand-file-name file directory
))
1066 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1067 (format "%s(%d)%s" (match-string 1 file
)
1068 count
(match-string 2 file
))
1069 (format "%s(%d)" file count
)))
1070 (setq count
(1+ count
)))
1071 (expand-file-name file directory
)))
1075 (defvar eww-bookmarks nil
)
1077 (defun eww-add-bookmark ()
1078 "Add the current page to the bookmarks."
1080 (eww-read-bookmarks)
1081 (dolist (bookmark eww-bookmarks
)
1082 (when (equal eww-current-url
1083 (plist-get bookmark
:url
))
1084 (error "Already bookmarked")))
1085 (if (y-or-n-p "bookmark this page? ")
1087 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1088 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1089 (push (list :url eww-current-url
1091 :time
(current-time-string))
1093 (eww-write-bookmarks)
1094 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1096 (defun eww-write-bookmarks ()
1097 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1098 (insert ";; Auto-generated file; don't edit\n")
1099 (pp eww-bookmarks
(current-buffer))))
1101 (defun eww-read-bookmarks ()
1102 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1104 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1106 (insert-file-contents file
)
1107 (read (current-buffer)))))))
1109 (defun eww-list-bookmarks ()
1110 "Display the bookmarks."
1112 (eww-bookmark-prepare)
1113 (pop-to-buffer "*eww bookmarks*"))
1115 (defun eww-bookmark-prepare ()
1116 (eww-read-bookmarks)
1117 (when (null eww-bookmarks
)
1118 (error "No bookmarks are defined"))
1119 (set-buffer (get-buffer-create "*eww bookmarks*"))
1121 (let ((format "%-40s %s")
1122 (inhibit-read-only t
)
1125 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1126 (dolist (bookmark eww-bookmarks
)
1127 (setq start
(point))
1128 (setq url
(plist-get bookmark
:url
))
1129 (when (> (length url
) 40)
1130 (setq url
(substring url
0 40)))
1131 (insert (format format url
1132 (plist-get bookmark
:title
))
1134 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1135 (goto-char (point-min))))
1137 (defvar eww-bookmark-kill-ring nil
)
1139 (defun eww-bookmark-kill ()
1140 "Kill the current bookmark."
1142 (let* ((start (line-beginning-position))
1143 (bookmark (get-text-property start
'eww-bookmark
))
1144 (inhibit-read-only t
))
1146 (error "No bookmark on the current line"))
1148 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1149 (delete-region start
(point))
1150 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1151 (eww-write-bookmarks)))
1153 (defun eww-bookmark-yank ()
1154 "Yank a previously killed bookmark to the current line."
1156 (unless eww-bookmark-kill-ring
1157 (error "No previously killed bookmark"))
1159 (let ((inhibit-read-only t
)
1162 (insert (pop eww-bookmark-kill-ring
))
1163 (setq bookmark
(get-text-property start
'eww-bookmark
))
1164 (if (= start
(point-min))
1165 (push bookmark eww-bookmarks
)
1166 (let ((line (count-lines start
(point))))
1167 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1168 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1169 (eww-write-bookmarks)))
1171 (defun eww-bookmark-quit ()
1172 "Kill the current buffer."
1174 (kill-buffer (current-buffer)))
1176 (defun eww-bookmark-browse ()
1177 "Browse the bookmark under point in eww."
1179 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1181 (error "No bookmark on the current line"))
1182 ;; We wish to leave this window, but if it's the only window here,
1183 ;; just let it remain.
1186 (eww-browse-url (plist-get bookmark
:url
))))
1188 (defun eww-next-bookmark ()
1189 "Go to the next bookmark in the list."
1193 (unless (get-buffer "*eww bookmarks*")
1195 (eww-bookmark-prepare))
1196 (with-current-buffer (get-buffer "*eww bookmarks*")
1197 (when (and (not first
)
1200 (setq bookmark
(get-text-property (line-beginning-position)
1203 (error "No next bookmark")))
1204 (eww-browse-url (plist-get bookmark
:url
))))
1206 (defun eww-previous-bookmark ()
1207 "Go to the previous bookmark in the list."
1211 (unless (get-buffer "*eww bookmarks*")
1213 (eww-bookmark-prepare))
1214 (with-current-buffer (get-buffer "*eww bookmarks*")
1216 (goto-char (point-max))
1217 (beginning-of-line))
1218 ;; On the final line.
1222 (error "No previous bookmark")
1224 (setq bookmark
(get-text-property (line-beginning-position)
1226 (eww-browse-url (plist-get bookmark
:url
))))
1228 (defvar eww-bookmark-mode-map
1229 (let ((map (make-sparse-keymap)))
1230 (suppress-keymap map
)
1231 (define-key map
"q" 'eww-bookmark-quit
)
1232 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1233 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1234 (define-key map
"\r" 'eww-bookmark-browse
)
1237 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1238 "Mode for listing bookmarks.
1240 \\{eww-bookmark-mode-map}"
1241 (buffer-disable-undo)
1242 (setq buffer-read-only t
1247 ;;; eww.el ends here