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
)))
162 ;;;###autoload (defalias 'browse-web 'eww)
165 (defun eww-open-file (file)
166 "Render a file using EWW."
167 (interactive "fFile: ")
168 (eww (concat "file://" (expand-file-name file
))))
170 (defun eww-render (status url
&optional point
)
171 (let ((redirect (plist-get status
:redirect
)))
173 (setq url redirect
)))
174 (set (make-local-variable 'eww-next-url
) nil
)
175 (set (make-local-variable 'eww-previous-url
) nil
)
176 (set (make-local-variable 'eww-up-url
) nil
)
177 (set (make-local-variable 'eww-home-url
) nil
)
178 (set (make-local-variable 'eww-start-url
) nil
)
179 (set (make-local-variable 'eww-contents-url
) nil
)
180 (let* ((headers (eww-parse-headers))
182 (mail-header-parse-content-type
183 (or (cdr (assoc "content-type" headers
))
187 (or (cdr (assq 'charset
(cdr content-type
)))
188 (eww-detect-charset (equal (car content-type
)
191 (data-buffer (current-buffer)))
194 (setq eww-current-title
"")
196 ((and eww-use-external-browser-for-content-type
197 (string-match-p eww-use-external-browser-for-content-type
199 (eww-browse-with-external-browser url
))
200 ((equal (car content-type
) "text/html")
201 (eww-display-html charset url nil point
))
202 ((string-match-p "\\`image/" (car content-type
))
204 (eww-update-header-line-format))
207 (eww-update-header-line-format)))
208 (setq eww-current-url url
209 eww-history-position
0))
210 (kill-buffer data-buffer
))))
212 (defun eww-parse-headers ()
214 (goto-char (point-min))
215 (while (and (not (eobp))
217 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
218 (push (cons (downcase (match-string 1))
226 (defun eww-detect-charset (html-p)
227 (let ((case-fold-search t
)
231 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
235 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
238 (declare-function libxml-parse-html-region
"xml.c"
239 (start end
&optional base-url
))
241 (defun eww-display-html (charset url
&optional document point
)
242 (or (fboundp 'libxml-parse-html-region
)
243 (error "This function requires Emacs to be compiled with libxml2"))
244 (unless (eq charset
'utf8
)
246 (decode-coding-region (point) (point-max) charset
)
247 (coding-system-error nil
)))
251 'base
(list (cons 'href url
))
252 (libxml-parse-html-region (point) (point-max))))))
253 (setq eww-current-source
(buffer-substring (point) (point-max)))
255 (setq eww-current-dom document
)
256 (let ((inhibit-read-only t
)
257 (after-change-functions nil
)
259 (shr-target-id (url-target (url-generic-parse-url url
)))
260 (shr-external-rendering-functions
261 '((title . eww-tag-title
)
262 (form . eww-tag-form
)
263 (input . eww-tag-input
)
264 (textarea . eww-tag-textarea
)
265 (body . eww-tag-body
)
266 (select . eww-tag-select
)
267 (link . eww-tag-link
)
269 (shr-insert-document document
)
274 (goto-char (point-min))
275 (let ((point (next-single-property-change
276 (point-min) 'shr-target-id
)))
280 (goto-char (point-min)))))
281 (setq eww-current-url url
282 eww-history-position
0)
283 (eww-update-header-line-format)))
285 (defun eww-handle-link (cont)
286 (let* ((rel (assq :rel cont
))
287 (href (assq :href cont
))
289 ;; The text associated with :rel is case-insensitive.
290 (if rel
(downcase (cdr rel
)))
291 '(("next" . eww-next-url
)
292 ;; Texinfo uses "previous", but HTML specifies
293 ;; "prev", so recognize both.
294 ("previous" . eww-previous-url
)
295 ("prev" . eww-previous-url
)
296 ;; HTML specifies "start" but also "contents",
297 ;; and Gtk seems to use "home". Recognize
298 ;; them all; but store them in different
299 ;; variables so that we can readily choose the
301 ("start" . eww-start-url
)
302 ("home" . eww-home-url
)
303 ("contents" . eww-contents-url
)
304 ("up" . eww-up-url
)))))
307 (set (cdr where
) (cdr href
)))))
309 (defun eww-tag-link (cont)
310 (eww-handle-link cont
)
313 (defun eww-tag-a (cont)
314 (eww-handle-link cont
)
315 (let ((start (point)))
317 (put-text-property start
(point) 'keymap eww-link-keymap
)))
319 (defun eww-update-header-line-format ()
320 (if eww-header-line-format
321 (setq header-line-format
322 (replace-regexp-in-string
324 ;; FIXME? Title can be blank. Default to, eg, last component
326 (format-spec eww-header-line-format
327 `((?u .
,eww-current-url
)
328 (?t .
,eww-current-title
)))))
329 (setq header-line-format nil
)))
331 (defun eww-tag-title (cont)
332 (setq eww-current-title
"")
334 (when (eq (car sub
) 'text
)
335 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
336 (eww-update-header-line-format))
338 (defun eww-tag-body (cont)
339 (let* ((start (point))
340 (fgcolor (cdr (or (assq :fgcolor cont
)
342 (bgcolor (cdr (assq :bgcolor cont
)))
343 (shr-stylesheet (list (cons 'color fgcolor
)
344 (cons 'background-color bgcolor
))))
346 (eww-colorize-region start
(point) fgcolor bgcolor
)))
348 (defun eww-colorize-region (start end fg
&optional bg
)
350 (let ((new-colors (shr-color-check fg bg
)))
353 (add-face-text-property start end
354 (list :foreground
(cadr new-colors
))
357 (add-face-text-property start end
358 (list :background
(car new-colors
))
361 (defun eww-display-raw ()
362 (let ((data (buffer-substring (point) (point-max))))
364 (let ((inhibit-read-only t
))
366 (goto-char (point-min))))
368 (defun eww-display-image ()
369 (let ((data (shr-parse-image-data)))
371 (let ((inhibit-read-only t
))
372 (shr-put-image data nil
))
373 (goto-char (point-min))))
375 (defun eww-setup-buffer ()
376 (switch-to-buffer (get-buffer-create "*eww*"))
377 (let ((inhibit-read-only t
))
380 (unless (eq major-mode
'eww-mode
)
383 (defun eww-view-source ()
385 (let ((buf (get-buffer-create "*eww-source*"))
386 (source eww-current-source
))
387 (with-current-buffer buf
388 (delete-region (point-min) (point-max))
389 (insert (or eww-current-source
"no source"))
390 (goto-char (point-min))
391 (when (featurep 'html-mode
)
396 (let ((map (make-sparse-keymap)))
397 (suppress-keymap map
)
398 (define-key map
"q" 'quit-window
)
399 (define-key map
"g" 'eww-reload
)
400 (define-key map
[tab] 'shr-next-link)
401 (define-key map [backtab] 'shr-previous-link)
402 (define-key map [delete] 'scroll-down-command)
403 (define-key map [?\S-\ ] 'scroll-down-command)
404 (define-key map "\177" 'scroll-down-command)
405 (define-key map " " 'scroll-up-command)
406 (define-key map "l" 'eww-back-url)
407 (define-key map "r" 'eww-forward-url)
408 (define-key map "n" 'eww-next-url)
409 (define-key map "p" 'eww-previous-url)
410 (define-key map "u" 'eww-up-url)
411 (define-key map "t" 'eww-top-url)
412 (define-key map "&" 'eww-browse-with-external-browser)
413 (define-key map "d" 'eww-download)
414 (define-key map "w" 'eww-copy-page-url)
415 (define-key map "C" 'url-cookie-list)
416 (define-key map "v" 'eww-view-source)
418 (define-key map "b" 'eww-add-bookmark)
419 (define-key map "B" 'eww-list-bookmarks)
420 (define-key map [(meta n)] 'eww-next-bookmark)
421 (define-key map [(meta p)] 'eww-previous-bookmark)
423 (easy-menu-define nil map ""
426 ["Close browser" quit-window t]
427 ["Reload" eww-reload t]
428 ["Back to previous page" eww-back-url
429 :active (not (zerop (length eww-history)))]
430 ["Forward to next page" eww-forward-url
431 :active (not (zerop eww-history-position))]
432 ["Browse with external browser" eww-browse-with-external-browser t]
433 ["Download" eww-download t]
434 ["View page source" eww-view-source]
435 ["Copy page URL" eww-copy-page-url t]
436 ["Add bookmark" eww-add-bookmark t]
437 ["List bookmarks" eww-list-bookmarks t]
438 ["List cookies" url-cookie-list t]))
441 (define-derived-mode eww-mode nil "eww"
442 "Mode for browsing the web.
445 ;; FIXME? This seems a strange default.
446 (set (make-local-variable 'eww-current-url) 'author)
447 (set (make-local-variable 'eww-current-dom) nil)
448 (set (make-local-variable 'eww-current-source) nil)
449 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
450 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
451 (set (make-local-variable 'eww-history) nil)
452 (set (make-local-variable 'eww-history-position) 0)
453 (buffer-disable-undo)
454 ;;(setq buffer-read-only t)
457 (defun eww-save-history ()
458 (push (list :url eww-current-url
459 :title eww-current-title
462 :source eww-current-source
463 :text (buffer-string))
467 (defun eww-browse-url (url &optional _new-window)
468 (when (and (equal major-mode 'eww-mode)
473 (defun eww-back-url ()
474 "Go to the previously displayed page."
476 (when (>= eww-history-position (length eww-history))
477 (error "No previous page"))
479 (setq eww-history-position (+ eww-history-position 2))
480 (eww-restore-history (elt eww-history (1- eww-history-position))))
482 (defun eww-forward-url ()
483 "Go to the next displayed page."
485 (when (zerop eww-history-position)
486 (error "No next page"))
488 (eww-restore-history (elt eww-history (1- eww-history-position))))
490 (defun eww-restore-history (elem)
491 (let ((inhibit-read-only t))
493 (insert (plist-get elem :text))
494 (setq eww-current-source (plist-get elem :source))
495 (setq eww-current-dom (plist-get elem :dom))
496 (goto-char (plist-get elem :point))
497 (setq eww-current-url (plist-get elem :url)
498 eww-current-title (plist-get elem :title))
499 (eww-update-header-line-format)))
501 (defun eww-next-url ()
502 "Go to the page marked `next'.
503 A page is marked `next' if rel=\"next\" appears in a <link>
507 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
508 (error "No `next' on this page")))
510 (defun eww-previous-url ()
511 "Go to the page marked `previous'.
512 A page is marked `previous' if rel=\"previous\" appears in a <link>
516 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
517 (error "No `previous' on this page")))
520 "Go to the page marked `up'.
521 A page is marked `up' if rel=\"up\" appears in a <link>
525 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
526 (error "No `up' on this page")))
528 (defun eww-top-url ()
529 "Go to the page marked `top'.
530 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
531 appears in a <link> or <a> tag."
533 (let ((best-url (or eww-start-url
537 (eww-browse-url (shr-expand-url best-url eww-current-url))
538 (error "No `top' for this page"))))
541 "Reload the current page."
543 (url-retrieve eww-current-url 'eww-render
544 (list eww-current-url (point))))
548 (defvar eww-form nil)
550 (defvar eww-submit-map
551 (let ((map (make-sparse-keymap)))
552 (define-key map "\r" 'eww-submit)
553 (define-key map [(control c) (control c)] 'eww-submit)
556 (defvar eww-checkbox-map
557 (let ((map (make-sparse-keymap)))
558 (define-key map [space] 'eww-toggle-checkbox)
559 (define-key map "\r" 'eww-toggle-checkbox)
560 (define-key map [(control c) (control c)] 'eww-submit)
564 (let ((map (make-keymap)))
565 (set-keymap-parent map text-mode-map)
566 (define-key map "\r" 'eww-submit)
567 (define-key map [(control a)] 'eww-beginning-of-text)
568 (define-key map [(control c) (control c)] 'eww-submit)
569 (define-key map [(control e)] 'eww-end-of-text)
570 (define-key map [tab] 'shr-next-link
)
571 (define-key map
[backtab] 'shr-previous-link)
574 (defvar eww-textarea-map
575 (let ((map (make-keymap)))
576 (set-keymap-parent map text-mode-map)
577 (define-key map "\r" 'forward-line)
578 (define-key map [(control c) (control c)] 'eww-submit)
579 (define-key map [tab] 'shr-next-link)
580 (define-key map [backtab] 'shr-previous-link
)
583 (defvar eww-select-map
584 (let ((map (make-sparse-keymap)))
585 (define-key map
"\r" 'eww-change-select
)
586 (define-key map
[(control c
) (control c
)] 'eww-submit
)
589 (defun eww-beginning-of-text ()
590 "Move to the start of the input field."
592 (goto-char (eww-beginning-of-field)))
594 (defun eww-end-of-text ()
595 "Move to the end of the text in the input field."
597 (goto-char (eww-end-of-field))
598 (let ((start (eww-beginning-of-field)))
599 (while (and (equal (following-char) ?
)
602 (when (> (point) start
)
605 (defun eww-beginning-of-field ()
609 ((not (eq (get-text-property (point) 'eww-form
)
610 (get-text-property (1- (point)) 'eww-form
)))
613 (previous-single-property-change
614 (point) 'eww-form nil
(point-min)))))
616 (defun eww-end-of-field ()
617 (1- (next-single-property-change
618 (point) 'eww-form nil
(point-max))))
620 (defun eww-tag-form (cont)
622 (list (assq :method cont
)
623 (assq :action cont
)))
625 (shr-ensure-paragraph)
630 (when (> (point) start
)
631 (put-text-property start
(1+ start
)
632 'eww-form eww-form
))))
634 (defun eww-form-submit (cont)
635 (let ((start (point))
636 (value (cdr (assq :value cont
))))
638 (if (zerop (length value
))
642 (add-face-text-property start
(point) 'eww-form-submit
)
643 (put-text-property start
(point) 'eww-form
644 (list :eww-form eww-form
647 :name
(cdr (assq :name cont
))))
648 (put-text-property start
(point) 'keymap eww-submit-map
)
651 (defun eww-form-checkbox (cont)
652 (let ((start (point)))
653 (if (cdr (assq :checked cont
))
654 (insert eww-form-checkbox-selected-symbol
)
655 (insert eww-form-checkbox-symbol
))
656 (add-face-text-property start
(point) 'eww-form-checkbox
)
657 (put-text-property start
(point) 'eww-form
658 (list :eww-form eww-form
659 :value
(cdr (assq :value cont
))
660 :type
(downcase (cdr (assq :type cont
)))
661 :checked
(cdr (assq :checked cont
))
662 :name
(cdr (assq :name cont
))))
663 (put-text-property start
(point) 'keymap eww-checkbox-map
)
666 (defun eww-form-text (cont)
667 (let ((start (point))
668 (type (downcase (or (cdr (assq :type cont
))
670 (value (or (cdr (assq :value cont
)) ""))
671 (width (string-to-number
672 (or (cdr (assq :size cont
))
675 (when (< (length value
) width
)
676 (insert (make-string (- width
(length value
)) ?
)))
677 (put-text-property start
(point) 'face
'eww-form-text
)
678 (put-text-property start
(point) 'local-map eww-text-map
)
679 (put-text-property start
(point) 'inhibit-read-only t
)
680 (put-text-property start
(point) 'eww-form
681 (list :eww-form eww-form
684 :name
(cdr (assq :name cont
))))
687 (defconst eww-text-input-types
'("text" "password" "textarea"
688 "color" "date" "datetime" "datetime-local"
689 "email" "month" "number" "search" "tel"
691 "List of input types which represent a text input.
692 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
694 (defun eww-process-text-input (beg end length
)
695 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
696 (properties (text-properties-at end
))
697 (type (plist-get form
:type
)))
699 (member type eww-text-input-types
))
702 ;; Delete some space at the end.
705 (if (equal type
"textarea")
706 (1- (line-end-position))
708 (let ((new (- end beg
)))
709 (while (and (> new
0)
710 (eql (following-char) ?
))
711 (delete-region (point) (1+ (point)))
712 (setq new
(1- new
))))
713 (set-text-properties beg end properties
)))
718 (if (equal type
"textarea")
719 (1- (line-end-position))
721 (let ((start (point)))
722 (insert (make-string length ?
))
723 (set-text-properties start
(point) properties
)))))
724 (let ((value (buffer-substring-no-properties
725 (eww-beginning-of-field)
726 (eww-end-of-field))))
727 (when (string-match " +\\'" value
)
728 (setq value
(substring value
0 (match-beginning 0))))
729 (plist-put form
:value value
)
730 (when (equal type
"password")
731 ;; Display passwords as asterisks.
732 (let ((start (eww-beginning-of-field)))
733 (put-text-property start
(+ start
(length value
))
734 'display
(make-string (length value
) ?
*))))))))
736 (defun eww-tag-textarea (cont)
737 (let ((start (point))
738 (value (or (cdr (assq :value cont
)) ""))
739 (lines (string-to-number
740 (or (cdr (assq :rows cont
))
742 (width (string-to-number
743 (or (cdr (assq :cols cont
))
749 (when (< (count-lines start
(point)) lines
)
750 (dotimes (i (- lines
(count-lines start
(point))))
752 (setq end
(point-marker))
754 (while (< (point) end
)
756 (let ((pad (- width
(- (point) (line-beginning-position)))))
758 (insert (make-string pad ?
))))
759 (add-face-text-property (line-beginning-position)
760 (point) 'eww-form-text
)
761 (put-text-property (line-beginning-position) (point)
762 'local-map eww-textarea-map
)
764 (put-text-property start
(point) 'eww-form
765 (list :eww-form eww-form
768 :name
(cdr (assq :name cont
))))))
770 (defun eww-tag-input (cont)
771 (let ((type (downcase (or (cdr (assq :type cont
))
775 ((or (equal type
"checkbox")
776 (equal type
"radio"))
777 (eww-form-checkbox cont
))
778 ((equal type
"submit")
779 (eww-form-submit cont
))
780 ((equal type
"hidden")
781 (let ((form eww-form
)
782 (name (cdr (assq :name cont
))))
783 ;; Don't add <input type=hidden> elements repeatedly.
785 (or (not (consp (car form
)))
786 (not (eq (caar form
) 'hidden
))
787 (not (equal (plist-get (cdr (car form
)) :name
)
789 (setq form
(cdr form
)))
791 (nconc eww-form
(list
794 :value
(cdr (assq :value cont
))))))))
796 (eww-form-text cont
)))
797 (unless (= start
(point))
798 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
800 (defun eww-tag-select (cont)
801 (shr-ensure-paragraph)
802 (let ((menu (list :name
(cdr (assq :name cont
))
808 (if (eq (car (car cont
)) 'optgroup
)
809 (dolist (groupelem cont
)
810 (unless (cdr (assq :disabled
(cdr groupelem
)))
811 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
813 (dolist (elem opelem
)
814 (when (eq (car elem
) 'option
)
815 (when (cdr (assq :selected
(cdr elem
)))
816 (nconc menu
(list :value
817 (cdr (assq :value
(cdr elem
))))))
818 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
819 (setq max
(max max
(length display
)))
821 :value
(cdr (assq :value
(cdr elem
)))
825 (setq options
(nreverse options
))
826 ;; If we have no selected values, default to the first value.
827 (unless (plist-get menu
:value
)
828 (nconc menu
(list :value
(nth 2 (car options
)))))
830 (let ((selected (eww-select-display menu
)))
832 (make-string (- max
(length selected
)) ?
)))
833 (put-text-property start
(point) 'eww-form menu
)
834 (add-face-text-property start
(point) 'eww-form-select
)
835 (put-text-property start
(point) 'keymap eww-select-map
)
836 (shr-ensure-paragraph))))
838 (defun eww-select-display (select)
839 (let ((value (plist-get select
:value
))
841 (dolist (elem select
)
842 (when (and (consp elem
)
843 (eq (car elem
) 'item
)
844 (equal value
(plist-get (cdr elem
) :value
)))
845 (setq display
(plist-get (cdr elem
) :display
))))
848 (defun eww-change-select ()
849 "Change the value of the select drop-down menu under point."
851 (let* ((input (get-text-property (point) 'eww-form
))
852 (completion-ignore-case t
)
855 (mapcar (lambda (elem)
857 (eq (car elem
) 'item
)
858 (cons (plist-get (cdr elem
) :display
)
859 (plist-get (cdr elem
) :value
))))
862 (completing-read "Change value: " options nil
'require-match
))
863 (inhibit-read-only t
))
864 (plist-put input
:value
(cdr (assoc-string display options t
)))
866 (eww-update-field display
))))
868 (defun eww-update-field (string)
869 (let ((properties (text-properties-at (point)))
870 (start (eww-beginning-of-field))
871 (end (1+ (eww-end-of-field))))
872 (delete-region start end
)
874 (make-string (- (- end start
) (length string
)) ?
))
875 (set-text-properties start end properties
)
878 (defun eww-toggle-checkbox ()
879 "Toggle the value of the checkbox under point."
881 (let* ((input (get-text-property (point) 'eww-form
))
882 (type (plist-get input
:type
)))
883 (if (equal type
"checkbox")
886 (if (plist-get input
:checked
)
888 (plist-put input
:checked nil
)
889 (eww-update-field eww-form-checkbox-symbol
))
890 (plist-put input
:checked t
)
891 (eww-update-field eww-form-checkbox-selected-symbol
))))
892 ;; Radio button. Switch all other buttons off.
893 (let ((name (plist-get input
:name
)))
895 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
896 (when (equal (plist-get (cdr elem
) :name
) name
)
897 (goto-char (car elem
))
898 (if (not (eq (cdr elem
) input
))
900 (plist-put input
:checked nil
)
901 (eww-update-field eww-form-checkbox-symbol
))
902 (plist-put input
:checked t
)
903 (eww-update-field eww-form-checkbox-selected-symbol
)))))
906 (defun eww-inputs (form)
907 (let ((start (point-min))
910 (< start
(point-max)))
911 (when (or (get-text-property start
'eww-form
)
912 (setq start
(next-single-property-change start
'eww-form
)))
913 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
915 (push (cons start
(get-text-property start
'eww-form
))
917 (setq start
(next-single-property-change start
'eww-form
))))
920 (defun eww-input-value (input)
921 (let ((type (plist-get input
:type
))
922 (value (plist-get input
:value
)))
924 ((equal type
"textarea")
927 (goto-char (point-min))
928 (while (re-search-forward "^ +\n\\| +$" nil t
)
929 (replace-match "" t t
))
932 (if (string-match " +\\'" value
)
933 (substring value
0 (match-beginning 0))
937 "Submit the current form."
939 (let* ((this-input (get-text-property (point) 'eww-form
))
940 (form (plist-get this-input
:eww-form
))
942 (dolist (elem (sort (eww-inputs form
)
944 (< (car o1
) (car o2
)))))
945 (let* ((input (cdr elem
))
946 (input-start (car elem
))
947 (name (plist-get input
:name
)))
950 ((member (plist-get input
:type
) '("checkbox" "radio"))
951 (when (plist-get input
:checked
)
952 (push (cons name
(plist-get input
:value
))
954 ((equal (plist-get input
:type
) "submit")
955 ;; We want the values from buttons if we hit a button if
956 ;; we hit enter on it, or if it's the first button after
957 ;; the field we did hit return on.
958 (when (or (eq input this-input
)
959 (and (not (eq input this-input
))
961 (> input-start
(point))))
963 (push (cons name
(plist-get input
:value
))
966 (push (cons name
(eww-input-value input
))
969 (when (and (consp elem
)
970 (eq (car elem
) 'hidden
))
971 (push (cons (plist-get (cdr elem
) :name
)
972 (plist-get (cdr elem
) :value
))
974 (if (and (stringp (cdr (assq :method form
)))
975 (equal (downcase (cdr (assq :method form
))) "post"))
976 (let ((url-request-method "POST")
977 (url-request-extra-headers
978 '(("Content-Type" .
"application/x-www-form-urlencoded")))
979 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
980 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
984 (if (cdr (assq :action form
))
985 (shr-expand-url (cdr (assq :action form
))
989 (mm-url-encode-www-form-urlencoded values
))))))
991 (defun eww-browse-with-external-browser (&optional url
)
992 "Browse the current URL with an external browser.
993 The browser to used is specified by the `shr-external-browser' variable."
995 (funcall shr-external-browser
(or url eww-current-url
)))
997 (defun eww-follow-link (&optional external mouse-event
)
998 "Browse the URL under point.
999 If EXTERNAL, browse the URL using `shr-external-browser'."
1000 (interactive (list current-prefix-arg last-nonmenu-event
))
1001 (mouse-set-point mouse-event
)
1002 (let ((url (get-text-property (point) 'shr-url
)))
1005 (message "No link under point"))
1006 ((string-match "^mailto:" url
)
1007 (browse-url-mail url
))
1009 (funcall shr-external-browser url
))
1010 ;; This is a #target url in the same page as the current one.
1011 ((and (url-target (url-generic-parse-url url
))
1012 (eww-same-page-p url eww-current-url
))
1014 (eww-display-html 'utf8 url eww-current-dom
))
1016 (eww-browse-url url
)))))
1018 (defun eww-same-page-p (url1 url2
)
1019 "Return non-nil if both URLs represent the same page.
1020 Differences in #targets are ignored."
1021 (let ((obj1 (url-generic-parse-url url1
))
1022 (obj2 (url-generic-parse-url url2
)))
1023 (setf (url-target obj1
) nil
)
1024 (setf (url-target obj2
) nil
)
1025 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1027 (defun eww-copy-page-url ()
1029 (message "%s" eww-current-url
)
1030 (kill-new eww-current-url
))
1032 (defun eww-download ()
1033 "Download URL under point to `eww-download-directory'."
1035 (let ((url (get-text-property (point) 'shr-url
)))
1037 (message "No URL under point")
1038 (url-retrieve url
'eww-download-callback
(list url
)))))
1040 (defun eww-download-callback (status url
)
1041 (unless (plist-get status
:error
)
1042 (let* ((obj (url-generic-parse-url url
))
1043 (path (car (url-path-and-query obj
)))
1044 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1045 eww-download-path
)))
1047 (message "Saved %s" file
))))
1049 (defun eww-make-unique-file-name (file directory
)
1051 ((zerop (length file
))
1053 ((string-match "\\`[.]" file
)
1054 (setq file
(concat "!" file
))))
1056 (while (file-exists-p (expand-file-name file directory
))
1058 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1059 (format "%s(%d)%s" (match-string 1 file
)
1060 count
(match-string 2 file
))
1061 (format "%s(%d)" file count
)))
1062 (setq count
(1+ count
)))
1063 (expand-file-name file directory
)))
1067 (defvar eww-bookmarks nil
)
1069 (defun eww-add-bookmark ()
1070 "Add the current page to the bookmarks."
1072 (eww-read-bookmarks)
1073 (dolist (bookmark eww-bookmarks
)
1074 (when (equal eww-current-url
1075 (plist-get bookmark
:url
))
1076 (error "Already bookmarked")))
1077 (if (y-or-n-p "bookmark this page? ")
1079 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1080 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1081 (push (list :url eww-current-url
1083 :time
(current-time-string))
1085 (eww-write-bookmarks)
1086 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1088 (defun eww-write-bookmarks ()
1089 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1090 (insert ";; Auto-generated file; don't edit\n")
1091 (pp eww-bookmarks
(current-buffer))))
1093 (defun eww-read-bookmarks ()
1094 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1096 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1098 (insert-file-contents file
)
1099 (read (current-buffer)))))))
1101 (defun eww-list-bookmarks ()
1102 "Display the bookmarks."
1104 (eww-bookmark-prepare)
1105 (pop-to-buffer "*eww bookmarks*"))
1107 (defun eww-bookmark-prepare ()
1108 (eww-read-bookmarks)
1109 (when (null eww-bookmarks
)
1110 (error "No bookmarks are defined"))
1111 (set-buffer (get-buffer-create "*eww bookmarks*"))
1113 (let ((format "%-40s %s")
1114 (inhibit-read-only t
)
1117 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1118 (dolist (bookmark eww-bookmarks
)
1119 (setq start
(point))
1120 (setq url
(plist-get bookmark
:url
))
1121 (when (> (length url
) 40)
1122 (setq url
(substring url
0 40)))
1123 (insert (format format url
1124 (plist-get bookmark
:title
))
1126 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1127 (goto-char (point-min))))
1129 (defvar eww-bookmark-kill-ring nil
)
1131 (defun eww-bookmark-kill ()
1132 "Kill the current bookmark."
1134 (let* ((start (line-beginning-position))
1135 (bookmark (get-text-property start
'eww-bookmark
))
1136 (inhibit-read-only t
))
1138 (error "No bookmark on the current line"))
1140 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1141 (delete-region start
(point))
1142 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1143 (eww-write-bookmarks)))
1145 (defun eww-bookmark-yank ()
1146 "Yank a previously killed bookmark to the current line."
1148 (unless eww-bookmark-kill-ring
1149 (error "No previously killed bookmark"))
1151 (let ((inhibit-read-only t
)
1154 (insert (pop eww-bookmark-kill-ring
))
1155 (setq bookmark
(get-text-property start
'eww-bookmark
))
1156 (if (= start
(point-min))
1157 (push bookmark eww-bookmarks
)
1158 (let ((line (count-lines start
(point))))
1159 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1160 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1161 (eww-write-bookmarks)))
1163 (defun eww-bookmark-quit ()
1164 "Kill the current buffer."
1166 (kill-buffer (current-buffer)))
1168 (defun eww-bookmark-browse ()
1169 "Browse the bookmark under point in eww."
1171 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1173 (error "No bookmark on the current line"))
1174 ;; We wish to leave this window, but if it's the only window here,
1175 ;; just let it remain.
1178 (eww-browse-url (plist-get bookmark
:url
))))
1180 (defun eww-next-bookmark ()
1181 "Go to the next bookmark in the list."
1185 (unless (get-buffer "*eww bookmarks*")
1187 (eww-bookmark-prepare))
1188 (with-current-buffer (get-buffer "*eww bookmarks*")
1189 (when (and (not first
)
1192 (setq bookmark
(get-text-property (line-beginning-position)
1195 (error "No next bookmark")))
1196 (eww-browse-url (plist-get bookmark
:url
))))
1198 (defun eww-previous-bookmark ()
1199 "Go to the previous bookmark in the list."
1203 (unless (get-buffer "*eww bookmarks*")
1205 (eww-bookmark-prepare))
1206 (with-current-buffer (get-buffer "*eww bookmarks*")
1208 (goto-char (point-max))
1209 (beginning-of-line))
1210 ;; On the final line.
1214 (error "No previous bookmark")
1216 (setq bookmark
(get-text-property (line-beginning-position)
1218 (eww-browse-url (plist-get bookmark
:url
))))
1220 (defvar eww-bookmark-mode-map
1221 (let ((map (make-sparse-keymap)))
1222 (suppress-keymap map
)
1223 (define-key map
"q" 'eww-bookmark-quit
)
1224 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1225 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1226 (define-key map
"\r" 'eww-bookmark-browse
)
1229 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1230 "Mode for listing bookmarks.
1232 \\{eww-bookmark-mode-map}"
1233 (buffer-disable-undo)
1234 (setq buffer-read-only t
1239 ;;; eww.el ends here