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 (defface eww-form-submit
60 '((((type x w32 ns
) (class color
)) ; Like default mode line
61 :box
(:line-width
2 :style released-button
)
62 :background
"#808080" :foreground
"black"))
63 "Face for eww buffer buttons."
67 (defface eww-form-checkbox
68 '((((type x w32 ns
) (class color
)) ; Like default mode line
69 :box
(:line-width
2 :style released-button
)
70 :background
"lightgrey" :foreground
"black"))
71 "Face for eww buffer buttons."
75 (defface eww-form-select
76 '((((type x w32 ns
) (class color
)) ; Like default mode line
77 :box
(:line-width
2 :style released-button
)
78 :background
"lightgrey" :foreground
"black"))
79 "Face for eww buffer buttons."
83 (defface eww-form-text
84 '((t (:background
"#505050"
86 :box
(:line-width
1))))
87 "Face for eww text inputs."
91 (defvar eww-current-url nil
)
92 (defvar eww-current-title
""
93 "Title of current page.")
94 (defvar eww-history nil
)
95 (defvar eww-history-position
0)
97 (defvar eww-next-url nil
)
98 (defvar eww-previous-url nil
)
99 (defvar eww-up-url nil
)
100 (defvar eww-home-url nil
)
101 (defvar eww-start-url nil
)
102 (defvar eww-contents-url nil
)
106 "Fetch URL and render the page.
107 If the input doesn't look like an URL or a domain name, the
108 word(s) will be searched for via `eww-search-prefix'."
109 (interactive "sEnter URL or keywords: ")
110 (if (and (= (length (split-string url
)) 1)
111 (> (length (split-string url
"\\.")) 1))
113 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
114 (setq url
(concat "http://" url
)))
115 ;; some site don't redirect final /
116 (when (string= (url-filename (url-generic-parse-url url
)) "")
117 (setq url
(concat url
"/"))))
118 (unless (string-match-p "\\'file:" url
)
119 (setq url
(concat eww-search-prefix
120 (replace-regexp-in-string " " "+" url
)))))
121 (url-retrieve url
'eww-render
(list url
)))
124 (defun eww-open-file (file)
125 "Render a file using EWW."
126 (interactive "fFile: ")
127 (eww (concat "file://" (expand-file-name file
))))
129 (defun eww-render (status url
&optional point
)
130 (let ((redirect (plist-get status
:redirect
)))
132 (setq url redirect
)))
133 (set (make-local-variable 'eww-next-url
) nil
)
134 (set (make-local-variable 'eww-previous-url
) nil
)
135 (set (make-local-variable 'eww-up-url
) nil
)
136 (set (make-local-variable 'eww-home-url
) nil
)
137 (set (make-local-variable 'eww-start-url
) nil
)
138 (set (make-local-variable 'eww-contents-url
) nil
)
139 (let* ((headers (eww-parse-headers))
141 (and (string-match "#\\(.*\\)" url
)
142 (match-string 1 url
)))
144 (mail-header-parse-content-type
145 (or (cdr (assoc "content-type" headers
))
149 (or (cdr (assq 'charset
(cdr content-type
)))
150 (eww-detect-charset (equal (car content-type
)
153 (data-buffer (current-buffer)))
157 ((equal (car content-type
) "text/html")
158 (eww-display-html charset url
))
159 ((string-match "^image/" (car content-type
))
162 (eww-display-raw charset
)))
163 (setq eww-history-position
0)
168 (let ((point (next-single-property-change
169 (point-min) 'shr-target-id
)))
171 (goto-char (1+ point
)))))))
172 (kill-buffer data-buffer
))))
174 (defun eww-parse-headers ()
176 (goto-char (point-min))
177 (while (and (not (eobp))
179 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
180 (push (cons (downcase (match-string 1))
188 (defun eww-detect-charset (html-p)
189 (let ((case-fold-search t
)
193 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
197 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
200 (defun eww-display-html (charset url
)
201 (unless (eq charset
'utf8
)
202 (decode-coding-region (point) (point-max) charset
))
205 'base
(list (cons 'href url
))
206 (libxml-parse-html-region (point) (point-max)))))
208 (setq eww-current-url url
)
209 (eww-update-header-line-format)
210 (let ((inhibit-read-only t
)
211 (after-change-functions nil
)
213 (shr-external-rendering-functions
214 '((title . eww-tag-title
)
215 (form . eww-tag-form
)
216 (input . eww-tag-input
)
217 (textarea . eww-tag-textarea
)
218 (body . eww-tag-body
)
219 (select . eww-tag-select
)
220 (link . eww-tag-link
)
222 (shr-insert-document document
))
223 (goto-char (point-min))))
225 (defun eww-handle-link (cont)
226 (let* ((rel (assq :rel cont
))
227 (href (assq :href cont
))
229 ;; The text associated with :rel is case-insensitive.
230 (if rel
(downcase (cdr rel
)))
231 '(("next" . eww-next-url
)
232 ;; Texinfo uses "previous", but HTML specifies
233 ;; "prev", so recognize both.
234 ("previous" . eww-previous-url
)
235 ("prev" . eww-previous-url
)
236 ;; HTML specifies "start" but also "contents",
237 ;; and Gtk seems to use "home". Recognize
238 ;; them all; but store them in different
239 ;; variables so that we can readily choose the
241 ("start" . eww-start-url
)
242 ("home" . eww-home-url
)
243 ("contents" . eww-contents-url
)
244 ("up" . eww-up-url
)))))
247 (set (cdr where
) (cdr href
)))))
249 (defun eww-tag-link (cont)
250 (eww-handle-link cont
)
253 (defun eww-tag-a (cont)
254 (eww-handle-link cont
)
257 (defun eww-update-header-line-format ()
258 (if eww-header-line-format
259 (setq header-line-format
260 (replace-regexp-in-string
262 (format-spec eww-header-line-format
263 `((?u .
,eww-current-url
)
264 (?t .
,eww-current-title
)))))
265 (setq header-line-format nil
)))
267 (defun eww-tag-title (cont)
268 (setq eww-current-title
"")
270 (when (eq (car sub
) 'text
)
271 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
272 (eww-update-header-line-format))
274 (defun eww-tag-body (cont)
275 (let* ((start (point))
276 (fgcolor (cdr (or (assq :fgcolor cont
)
278 (bgcolor (cdr (assq :bgcolor cont
)))
279 (shr-stylesheet (list (cons 'color fgcolor
)
280 (cons 'background-color bgcolor
))))
282 (eww-colorize-region start
(point) fgcolor bgcolor
)))
284 (defun eww-colorize-region (start end fg
&optional bg
)
286 (let ((new-colors (shr-color-check fg bg
)))
289 (add-face-text-property start end
290 (list :foreground
(cadr new-colors
))
293 (add-face-text-property start end
294 (list :background
(car new-colors
))
297 (defun eww-display-raw (charset)
298 (let ((data (buffer-substring (point) (point-max))))
300 (let ((inhibit-read-only t
))
302 (goto-char (point-min))))
304 (defun eww-display-image ()
305 (let ((data (buffer-substring (point) (point-max))))
307 (let ((inhibit-read-only t
))
308 (shr-put-image data nil
))
309 (goto-char (point-min))))
311 (defun eww-setup-buffer ()
312 (pop-to-buffer (get-buffer-create "*eww*"))
313 (let ((inhibit-read-only t
))
316 (unless (eq major-mode
'eww-mode
)
320 (let ((map (make-sparse-keymap)))
321 (suppress-keymap map
)
322 (define-key map
"q" 'eww-quit
)
323 (define-key map
"g" 'eww-reload
)
324 (define-key map
[tab] 'shr-next-link)
325 (define-key map [backtab] 'shr-previous-link)
326 (define-key map [delete] 'scroll-down-command)
327 (define-key map "\177" 'scroll-down-command)
328 (define-key map " " 'scroll-up-command)
329 (define-key map "l" 'eww-back-url)
330 (define-key map "f" 'eww-forward-url)
331 (define-key map "n" 'eww-next-url)
332 (define-key map "p" 'eww-previous-url)
333 (define-key map "u" 'eww-up-url)
334 (define-key map "t" 'eww-top-url)
335 (define-key map "&" 'eww-browse-with-external-browser)
336 (define-key map "d" 'eww-download)
337 (define-key map "w" 'eww-copy-page-url)
338 (define-key map "C" 'url-cookie-list)
340 (easy-menu-define nil map ""
343 ["Reload" eww-reload t]
344 ["Back to previous page" eww-back-url
345 :active (not (zerop (length eww-history)))]
346 ["Forward to next page" eww-forward-url
347 :active (not (zerop eww-history-position))]
348 ["Browse with external browser" eww-browse-with-external-browser t]
349 ["Download" eww-download t]
350 ["Copy page URL" eww-copy-page-url t]
351 ["List cookies" url-cookie-list t]))
354 (define-derived-mode eww-mode nil "eww"
355 "Mode for browsing the web.
358 (set (make-local-variable 'eww-current-url) 'author)
359 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
360 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
361 (set (make-local-variable 'eww-history) nil)
362 (set (make-local-variable 'eww-history-position) 0)
363 (buffer-disable-undo)
364 ;;(setq buffer-read-only t)
367 (defun eww-save-history ()
368 (push (list :url eww-current-url
370 :text (buffer-string))
373 (defun eww-browse-url (url &optional new-window)
374 (when (and (equal major-mode 'eww-mode)
380 "Exit the Emacs Web Wowser."
382 (setq eww-history nil)
383 (kill-buffer (current-buffer)))
385 (defun eww-back-url ()
386 "Go to the previously displayed page."
388 (when (>= eww-history-position (length eww-history))
389 (error "No previous page"))
391 (setq eww-history-position (+ eww-history-position 2))
392 (eww-restore-history (elt eww-history (1- eww-history-position))))
394 (defun eww-forward-url ()
395 "Go to the next displayed page."
397 (when (zerop eww-history-position)
398 (error "No next page"))
400 (eww-restore-history (elt eww-history (1- eww-history-position))))
402 (defun eww-restore-history (elem)
403 (let ((inhibit-read-only t))
405 (insert (plist-get elem :text))
406 (goto-char (plist-get elem :point))
407 (setq eww-current-url (plist-get elem :url))))
409 (defun eww-next-url ()
410 "Go to the page marked `next'.
411 A page is marked `next' if rel=\"next\" appears in a <link>
415 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
416 (error "No `next' on this page")))
418 (defun eww-previous-url ()
419 "Go to the page marked `previous'.
420 A page is marked `previous' if rel=\"previous\" appears in a <link>
424 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
425 (error "No `previous' on this page")))
428 "Go to the page marked `up'.
429 A page is marked `up' if rel=\"up\" appears in a <link>
433 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
434 (error "No `up' on this page")))
436 (defun eww-top-url ()
437 "Go to the page marked `top'.
438 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
439 appears in a <link> or <a> tag."
441 (let ((best-url (or eww-start-url
445 (eww-browse-url (shr-expand-url best-url eww-current-url))
446 (error "No `top' for this page"))))
449 "Reload the current page."
451 (url-retrieve eww-current-url 'eww-render
452 (list eww-current-url (point))))
456 (defvar eww-form nil)
458 (defvar eww-submit-map
459 (let ((map (make-sparse-keymap)))
460 (define-key map "\r" 'eww-submit)
461 (define-key map [(control c) (control c)] 'eww-submit)
464 (defvar eww-checkbox-map
465 (let ((map (make-sparse-keymap)))
466 (define-key map [space] 'eww-toggle-checkbox)
467 (define-key map "\r" 'eww-toggle-checkbox)
468 (define-key map [(control c) (control c)] 'eww-submit)
472 (let ((map (make-keymap)))
473 (set-keymap-parent map text-mode-map)
474 (define-key map "\r" 'eww-submit)
475 (define-key map [(control a)] 'eww-beginning-of-text)
476 (define-key map [(control c) (control c)] 'eww-submit)
477 (define-key map [(control e)] 'eww-end-of-text)
478 (define-key map [tab] 'shr-next-link
)
479 (define-key map
[backtab] 'shr-previous-link)
482 (defvar eww-textarea-map
483 (let ((map (make-keymap)))
484 (set-keymap-parent map text-mode-map)
485 (define-key map "\r" 'forward-line)
486 (define-key map [(control c) (control c)] 'eww-submit)
487 (define-key map [tab] 'shr-next-link)
488 (define-key map [backtab] 'shr-previous-link
)
491 (defvar eww-select-map
492 (let ((map (make-sparse-keymap)))
493 (define-key map
"\r" 'eww-change-select
)
494 (define-key map
[(control c
) (control c
)] 'eww-submit
)
497 (defun eww-beginning-of-text ()
498 "Move to the start of the input field."
500 (goto-char (eww-beginning-of-field)))
502 (defun eww-end-of-text ()
503 "Move to the end of the text in the input field."
505 (goto-char (eww-end-of-field))
506 (let ((start (eww-beginning-of-field)))
507 (while (and (equal (following-char) ?
)
510 (when (> (point) start
)
513 (defun eww-beginning-of-field ()
517 ((not (eq (get-text-property (point) 'eww-form
)
518 (get-text-property (1- (point)) 'eww-form
)))
521 (previous-single-property-change
522 (point) 'eww-form nil
(point-min)))))
524 (defun eww-end-of-field ()
525 (1- (next-single-property-change
526 (point) 'eww-form nil
(point-max))))
528 (defun eww-tag-form (cont)
530 (list (assq :method cont
)
531 (assq :action cont
)))
533 (shr-ensure-paragraph)
538 (when (> (point) start
)
539 (put-text-property start
(1+ start
)
540 'eww-form eww-form
))))
542 (defun eww-form-submit (cont)
543 (let ((start (point))
544 (value (cdr (assq :value cont
))))
546 (if (zerop (length value
))
550 (add-face-text-property start
(point) 'eww-form-submit
)
551 (put-text-property start
(point) 'eww-form
552 (list :eww-form eww-form
555 :name
(cdr (assq :name cont
))))
556 (put-text-property start
(point) 'keymap eww-submit-map
)
559 (defun eww-form-checkbox (cont)
560 (let ((start (point)))
561 (if (cdr (assq :checked cont
))
564 (add-face-text-property start
(point) 'eww-form-checkbox
)
565 (put-text-property start
(point) 'eww-form
566 (list :eww-form eww-form
567 :value
(cdr (assq :value cont
))
568 :type
(downcase (cdr (assq :type cont
)))
569 :checked
(cdr (assq :checked cont
))
570 :name
(cdr (assq :name cont
))))
571 (put-text-property start
(point) 'keymap eww-checkbox-map
)
574 (defun eww-form-text (cont)
575 (let ((start (point))
576 (type (downcase (or (cdr (assq :type cont
))
578 (value (or (cdr (assq :value cont
)) ""))
579 (width (string-to-number
580 (or (cdr (assq :size cont
))
583 (when (< (length value
) width
)
584 (insert (make-string (- width
(length value
)) ?
)))
585 (put-text-property start
(point) 'face
'eww-form-text
)
586 (put-text-property start
(point) 'local-map eww-text-map
)
587 (put-text-property start
(point) 'inhibit-read-only t
)
588 (put-text-property start
(point) 'eww-form
589 (list :eww-form eww-form
592 :name
(cdr (assq :name cont
))))
595 (defun eww-process-text-input (beg end length
)
596 (let* ((form (get-text-property end
'eww-form
))
597 (properties (text-properties-at end
))
598 (type (plist-get form
:type
)))
600 (member type
'("text" "password" "textarea")))
603 ;; Delete some space at the end.
606 (if (equal type
"textarea")
607 (1- (line-end-position))
609 (let ((new (- end beg
)))
610 (while (and (> new
0)
611 (eql (following-char) ?
))
612 (delete-region (point) (1+ (point)))
613 (setq new
(1- new
))))
614 (set-text-properties beg end properties
)))
619 (if (equal type
"textarea")
620 (1- (line-end-position))
622 (let ((start (point)))
623 (insert (make-string length ?
))
624 (set-text-properties start
(point) properties
)))))
625 (let ((value (buffer-substring-no-properties
626 (eww-beginning-of-field)
627 (eww-end-of-field))))
628 (when (string-match " +\\'" value
)
629 (setq value
(substring value
0 (match-beginning 0))))
630 (plist-put form
:value value
)
631 (when (equal type
"password")
632 ;; Display passwords as asterisks.
633 (let ((start (eww-beginning-of-field)))
634 (put-text-property start
(+ start
(length value
))
635 'display
(make-string (length value
) ?
*))))))))
637 (defun eww-tag-textarea (cont)
638 (let ((start (point))
639 (value (or (cdr (assq :value cont
)) ""))
640 (lines (string-to-number
641 (or (cdr (assq :rows cont
))
643 (width (string-to-number
644 (or (cdr (assq :cols cont
))
650 (when (< (count-lines start
(point)) lines
)
651 (dotimes (i (- lines
(count-lines start
(point))))
653 (setq end
(point-marker))
655 (while (< (point) end
)
657 (let ((pad (- width
(- (point) (line-beginning-position)))))
659 (insert (make-string pad ?
))))
660 (add-face-text-property (line-beginning-position)
661 (point) 'eww-form-text
)
662 (put-text-property (line-beginning-position) (point)
663 'local-map eww-textarea-map
)
665 (put-text-property start
(point) 'eww-form
666 (list :eww-form eww-form
669 :name
(cdr (assq :name cont
))))))
671 (defun eww-tag-input (cont)
672 (let ((type (downcase (or (cdr (assq :type cont
))
676 ((or (equal type
"checkbox")
677 (equal type
"radio"))
678 (eww-form-checkbox cont
))
679 ((equal type
"submit")
680 (eww-form-submit cont
))
681 ((equal type
"hidden")
682 (let ((form eww-form
)
683 (name (cdr (assq :name cont
))))
684 ;; Don't add <input type=hidden> elements repeatedly.
686 (or (not (consp (car form
)))
687 (not (eq (caar form
) 'hidden
))
688 (not (equal (plist-get (cdr (car form
)) :name
)
690 (setq form
(cdr form
)))
692 (nconc eww-form
(list
695 :value
(cdr (assq :value cont
))))))))
697 (eww-form-text cont
)))
698 (unless (= start
(point))
699 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
701 (defun eww-tag-select (cont)
702 (shr-ensure-paragraph)
703 (let ((menu (list :name
(cdr (assq :name cont
))
709 (when (eq (car elem
) 'option
)
710 (when (cdr (assq :selected
(cdr elem
)))
711 (nconc menu
(list :value
712 (cdr (assq :value
(cdr elem
))))))
713 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
714 (setq max
(max max
(length display
)))
716 :value
(cdr (assq :value
(cdr elem
)))
720 (setq options
(nreverse options
))
721 ;; If we have no selected values, default to the first value.
722 (unless (plist-get menu
:value
)
723 (nconc menu
(list :value
(nth 2 (car options
)))))
725 (let ((selected (eww-select-display menu
)))
727 (make-string (- max
(length selected
)) ?
)))
728 (put-text-property start
(point) 'eww-form menu
)
729 (add-face-text-property start
(point) 'eww-form-select
)
730 (put-text-property start
(point) 'keymap eww-select-map
)
731 (shr-ensure-paragraph))))
733 (defun eww-select-display (select)
734 (let ((value (plist-get select
:value
))
736 (dolist (elem select
)
737 (when (and (consp elem
)
738 (eq (car elem
) 'item
)
739 (equal value
(plist-get (cdr elem
) :value
)))
740 (setq display
(plist-get (cdr elem
) :display
))))
743 (defun eww-change-select ()
744 "Change the value of the select drop-down menu under point."
746 (let* ((input (get-text-property (point) 'eww-form
))
747 (properties (text-properties-at (point)))
748 (completion-ignore-case t
)
751 (mapcar (lambda (elem)
753 (eq (car elem
) 'item
)
754 (cons (plist-get (cdr elem
) :display
)
755 (plist-get (cdr elem
) :value
))))
758 (completing-read "Change value: " options nil
'require-match
))
759 (inhibit-read-only t
))
760 (plist-put input
:value
(cdr (assoc-string display options t
)))
762 (eww-update-field display
))))
764 (defun eww-update-field (string)
765 (let ((properties (text-properties-at (point)))
766 (start (eww-beginning-of-field))
767 (end (1+ (eww-end-of-field))))
768 (delete-region start end
)
770 (make-string (- (- end start
) (length string
)) ?
))
771 (set-text-properties start end properties
)
774 (defun eww-toggle-checkbox ()
775 "Toggle the value of the checkbox under point."
777 (let* ((input (get-text-property (point) 'eww-form
))
778 (type (plist-get input
:type
)))
779 (if (equal type
"checkbox")
782 (if (plist-get input
:checked
)
784 (plist-put input
:checked nil
)
785 (eww-update-field "[ ]"))
786 (plist-put input
:checked t
)
787 (eww-update-field "[X]"))))
788 ;; Radio button. Switch all other buttons off.
789 (let ((name (plist-get input
:name
)))
791 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
792 (when (equal (plist-get (cdr elem
) :name
) name
)
793 (goto-char (car elem
))
794 (if (not (eq (cdr elem
) input
))
796 (plist-put input
:checked nil
)
797 (eww-update-field "[ ]"))
798 (plist-put input
:checked t
)
799 (eww-update-field "[X]")))))
802 (defun eww-inputs (form)
803 (let ((start (point-min))
806 (< start
(point-max)))
807 (when (or (get-text-property start
'eww-form
)
808 (setq start
(next-single-property-change start
'eww-form
)))
809 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
811 (push (cons start
(get-text-property start
'eww-form
))
813 (setq start
(next-single-property-change start
'eww-form
))))
816 (defun eww-input-value (input)
817 (let ((type (plist-get input
:type
))
818 (value (plist-get input
:value
)))
820 ((equal type
"textarea")
823 (goto-char (point-min))
824 (while (re-search-forward "^ +\n\\| +$" nil t
)
825 (replace-match "" t t
))
828 (if (string-match " +\\'" value
)
829 (substring value
0 (match-beginning 0))
833 "Submit the current form."
835 (let* ((this-input (get-text-property (point) 'eww-form
))
836 (form (plist-get this-input
:eww-form
))
838 (dolist (elem (sort (eww-inputs form
)
840 (< (car o1
) (car o2
)))))
841 (let* ((input (cdr elem
))
842 (input-start (car elem
))
843 (name (plist-get input
:name
)))
846 ((member (plist-get input
:type
) '("checkbox" "radio"))
847 (when (plist-get input
:checked
)
848 (push (cons name
(plist-get input
:value
))
850 ((equal (plist-get input
:type
) "submit")
851 ;; We want the values from buttons if we hit a button if
852 ;; we hit enter on it, or if it's the first button after
853 ;; the field we did hit return on.
854 (when (or (eq input this-input
)
855 (and (not (eq input this-input
))
857 (> input-start
(point))))
859 (push (cons name
(plist-get input
:value
))
862 (push (cons name
(eww-input-value input
))
865 (when (and (consp elem
)
866 (eq (car elem
) 'hidden
))
867 (push (cons (plist-get (cdr elem
) :name
)
868 (plist-get (cdr elem
) :value
))
870 (if (and (stringp (cdr (assq :method form
)))
871 (equal (downcase (cdr (assq :method form
))) "post"))
872 (let ((url-request-method "POST")
873 (url-request-extra-headers
874 '(("Content-Type" .
"application/x-www-form-urlencoded")))
875 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
876 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
880 (if (cdr (assq :action form
))
881 (shr-expand-url (cdr (assq :action form
))
885 (mm-url-encode-www-form-urlencoded values
))))))
887 (defun eww-browse-with-external-browser ()
888 "Browse the current URL with an external browser.
889 The browser to used is specified by the `shr-external-browser' variable."
891 (funcall shr-external-browser eww-current-url
))
893 (defun eww-copy-page-url ()
895 (message "%s" eww-current-url
)
896 (kill-new eww-current-url
))
898 (defun eww-download ()
899 "Download URL under point to `eww-download-directory'."
901 (let ((url (get-text-property (point) 'shr-url
)))
903 (message "No URL under point")
904 (url-retrieve url
'eww-download-callback
(list url
)))))
906 (defun eww-download-callback (status url
)
907 (unless (plist-get status
:error
)
908 (let* ((obj (url-generic-parse-url url
))
909 (path (car (url-path-and-query obj
)))
910 (file (eww-make-unique-file-name (file-name-nondirectory path
)
913 (message "Saved %s" file
))))
915 (defun eww-make-unique-file-name (file directory
)
917 ((zerop (length file
))
919 ((string-match "\\`[.]" file
)
920 (setq file
(concat "!" file
))))
923 (while (file-exists-p (expand-file-name file directory
))
925 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
926 (format "%s(%d)%s" (match-string 1 file
)
927 count
(match-string 2 file
))
928 (format "%s(%d)" file count
)))
929 (setq count
(1+ count
)))
930 (expand-file-name file directory
)))