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
)))
167 (let ((point (next-single-property-change
168 (point-min) 'shr-target-id
)))
170 (goto-char (1+ point
)))))))
171 (kill-buffer data-buffer
))))
173 (defun eww-parse-headers ()
175 (goto-char (point-min))
176 (while (and (not (eobp))
178 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
179 (push (cons (downcase (match-string 1))
187 (defun eww-detect-charset (html-p)
188 (let ((case-fold-search t
)
192 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
196 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
199 (defun eww-display-html (charset url
)
200 (unless (eq charset
'utf8
)
201 (decode-coding-region (point) (point-max) charset
))
204 'base
(list (cons 'href url
))
205 (libxml-parse-html-region (point) (point-max)))))
207 (setq eww-current-url url
)
208 (eww-update-header-line-format)
209 (let ((inhibit-read-only t
)
210 (after-change-functions nil
)
212 (shr-external-rendering-functions
213 '((title . eww-tag-title
)
214 (form . eww-tag-form
)
215 (input . eww-tag-input
)
216 (textarea . eww-tag-textarea
)
217 (body . eww-tag-body
)
218 (select . eww-tag-select
)
219 (link . eww-tag-link
)
221 (shr-insert-document document
))
222 (goto-char (point-min))))
224 (defun eww-handle-link (cont)
225 (let* ((rel (assq :rel cont
))
226 (href (assq :href cont
))
228 ;; The text associated with :rel is case-insensitive.
229 (if rel
(downcase (cdr rel
)))
230 '(("next" . eww-next-url
)
231 ;; Texinfo uses "previous", but HTML specifies
232 ;; "prev", so recognize both.
233 ("previous" . eww-previous-url
)
234 ("prev" . eww-previous-url
)
235 ;; HTML specifies "start" but also "contents",
236 ;; and Gtk seems to use "home". Recognize
237 ;; them all; but store them in different
238 ;; variables so that we can readily choose the
240 ("start" . eww-start-url
)
241 ("home" . eww-home-url
)
242 ("contents" . eww-contents-url
)
243 ("up" . eww-up-url
)))))
246 (set (cdr where
) (cdr href
)))))
248 (defun eww-tag-link (cont)
249 (eww-handle-link cont
)
252 (defun eww-tag-a (cont)
253 (eww-handle-link cont
)
256 (defun eww-update-header-line-format ()
257 (if eww-header-line-format
258 (setq header-line-format
259 (replace-regexp-in-string
261 (format-spec eww-header-line-format
262 `((?u .
,eww-current-url
)
263 (?t .
,eww-current-title
)))))
264 (setq header-line-format nil
)))
266 (defun eww-tag-title (cont)
267 (setq eww-current-title
"")
269 (when (eq (car sub
) 'text
)
270 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
271 (eww-update-header-line-format))
273 (defun eww-tag-body (cont)
274 (let* ((start (point))
275 (fgcolor (cdr (or (assq :fgcolor cont
)
277 (bgcolor (cdr (assq :bgcolor cont
)))
278 (shr-stylesheet (list (cons 'color fgcolor
)
279 (cons 'background-color bgcolor
))))
281 (eww-colorize-region start
(point) fgcolor bgcolor
)))
283 (defun eww-colorize-region (start end fg
&optional bg
)
285 (let ((new-colors (shr-color-check fg bg
)))
288 (add-face-text-property start end
289 (list :foreground
(cadr new-colors
))
292 (add-face-text-property start end
293 (list :background
(car new-colors
))
296 (defun eww-display-raw (charset)
297 (let ((data (buffer-substring (point) (point-max))))
299 (let ((inhibit-read-only t
))
301 (goto-char (point-min))))
303 (defun eww-display-image ()
304 (let ((data (buffer-substring (point) (point-max))))
306 (let ((inhibit-read-only t
))
307 (shr-put-image data nil
))
308 (goto-char (point-min))))
310 (defun eww-setup-buffer ()
311 (pop-to-buffer (get-buffer-create "*eww*"))
313 (let ((inhibit-read-only t
))
318 (let ((map (make-sparse-keymap)))
319 (suppress-keymap map
)
320 (define-key map
"q" 'eww-quit
)
321 (define-key map
"g" 'eww-reload
)
322 (define-key map
[tab] 'shr-next-link)
323 (define-key map [backtab] 'shr-previous-link)
324 (define-key map [delete] 'scroll-down-command)
325 (define-key map "\177" 'scroll-down-command)
326 (define-key map " " 'scroll-up-command)
327 (define-key map "l" 'eww-back-url)
328 (define-key map "f" 'eww-forward-url)
329 (define-key map "n" 'eww-next-url)
330 (define-key map "p" 'eww-previous-url)
331 (define-key map "u" 'eww-up-url)
332 (define-key map "t" 'eww-top-url)
333 (define-key map "&" 'eww-browse-with-external-browser)
334 (define-key map "d" 'eww-download)
335 (define-key map "w" 'eww-copy-page-url)
338 (define-derived-mode eww-mode nil "eww"
339 "Mode for browsing the web.
342 (set (make-local-variable 'eww-current-url) 'author)
343 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
344 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
345 ;;(setq buffer-read-only t)
348 (defun eww-save-history ()
349 (let ((elem (list :url eww-current-url
351 :text (buffer-string))))
352 (if (or (zerop eww-history-position)
353 (= eww-history-position (length eww-history)))
354 (push elem eww-history)
355 (setcdr (nthcdr eww-history-position eww-history)
356 (cons elem (nthcdr eww-history-position eww-history))))))
358 (defun eww-browse-url (url &optional new-window)
359 (when (and (equal major-mode 'eww-mode)
365 "Exit the Emacs Web Wowser."
367 (setq eww-history nil)
368 (kill-buffer (current-buffer)))
370 (defun eww-back-url ()
371 "Go to the previously displayed page."
373 (when (>= eww-history-position (length eww-history))
374 (error "No previous page"))
376 (if (not (zerop eww-history-position))
377 (elt eww-history eww-history-position)
379 (elt eww-history (1+ eww-history-position))))
380 (setq eww-history-position (1+ eww-history-position)))
382 (defun eww-forward-url ()
383 "Go to the next displayed page."
385 (when (zerop eww-history-position)
386 (error "No next page"))
387 (eww-restore-history (elt eww-history (1- eww-history-position)))
388 (setq eww-history-position (1- eww-history-position)))
390 (defun eww-restore-history (elem)
391 (let ((inhibit-read-only t))
393 (insert (plist-get elem :text))
394 (goto-char (plist-get elem :point))
395 (setq eww-current-url (plist-get elem :url))))
397 (defun eww-next-url ()
398 "Go to the page marked `next'.
399 A page is marked `next' if rel=\"next\" appears in a <link>
403 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
404 (error "No `next' on this page")))
406 (defun eww-previous-url ()
407 "Go to the page marked `previous'.
408 A page is marked `previous' if rel=\"previous\" appears in a <link>
412 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
413 (error "No `previous' on this page")))
416 "Go to the page marked `up'.
417 A page is marked `up' if rel=\"up\" appears in a <link>
421 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
422 (error "No `up' on this page")))
424 (defun eww-top-url ()
425 "Go to the page marked `top'.
426 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
427 appears in a <link> or <a> tag."
429 (let ((best-url (or eww-start-url
433 (eww-browse-url (shr-expand-url best-url eww-current-url))
434 (error "No `top' for this page"))))
437 "Reload the current page."
439 (url-retrieve eww-current-url 'eww-render
440 (list eww-current-url (point))))
444 (defvar eww-form nil)
446 (defvar eww-submit-map
447 (let ((map (make-sparse-keymap)))
448 (define-key map "\r" 'eww-submit)
449 (define-key map [(control c) (control c)] 'eww-submit)
452 (defvar eww-checkbox-map
453 (let ((map (make-sparse-keymap)))
454 (define-key map [space] 'eww-toggle-checkbox)
455 (define-key map "\r" 'eww-toggle-checkbox)
456 (define-key map [(control c) (control c)] 'eww-submit)
460 (let ((map (make-keymap)))
461 (set-keymap-parent map text-mode-map)
462 (define-key map "\r" 'eww-submit)
463 (define-key map [(control a)] 'eww-beginning-of-text)
464 (define-key map [(control c) (control c)] 'eww-submit)
465 (define-key map [(control e)] 'eww-end-of-text)
466 (define-key map [tab] 'shr-next-link
)
467 (define-key map
[backtab] 'shr-previous-link)
470 (defvar eww-textarea-map
471 (let ((map (make-keymap)))
472 (set-keymap-parent map text-mode-map)
473 (define-key map "\r" 'forward-line)
474 (define-key map [(control c) (control c)] 'eww-submit)
475 (define-key map [tab] 'shr-next-link)
476 (define-key map [backtab] 'shr-previous-link
)
479 (defvar eww-select-map
480 (let ((map (make-sparse-keymap)))
481 (define-key map
"\r" 'eww-change-select
)
482 (define-key map
[(control c
) (control c
)] 'eww-submit
)
485 (defun eww-beginning-of-text ()
486 "Move to the start of the input field."
488 (goto-char (eww-beginning-of-field)))
490 (defun eww-end-of-text ()
491 "Move to the end of the text in the input field."
493 (goto-char (eww-end-of-field))
494 (let ((start (eww-beginning-of-field)))
495 (while (and (equal (following-char) ?
)
498 (when (> (point) start
)
501 (defun eww-beginning-of-field ()
505 ((not (eq (get-text-property (point) 'eww-form
)
506 (get-text-property (1- (point)) 'eww-form
)))
509 (previous-single-property-change
510 (point) 'eww-form nil
(point-min)))))
512 (defun eww-end-of-field ()
513 (1- (next-single-property-change
514 (point) 'eww-form nil
(point-max))))
516 (defun eww-tag-form (cont)
518 (list (assq :method cont
)
519 (assq :action cont
)))
521 (shr-ensure-paragraph)
526 (when (> (point) start
)
527 (put-text-property start
(1+ start
)
528 'eww-form eww-form
))))
530 (defun eww-form-submit (cont)
531 (let ((start (point))
532 (value (cdr (assq :value cont
))))
534 (if (zerop (length value
))
538 (add-face-text-property start
(point) 'eww-form-submit
)
539 (put-text-property start
(point) 'eww-form
540 (list :eww-form eww-form
543 :name
(cdr (assq :name cont
))))
544 (put-text-property start
(point) 'keymap eww-submit-map
)
547 (defun eww-form-checkbox (cont)
548 (let ((start (point)))
549 (if (cdr (assq :checked cont
))
552 (add-face-text-property start
(point) 'eww-form-checkbox
)
553 (put-text-property start
(point) 'eww-form
554 (list :eww-form eww-form
555 :value
(cdr (assq :value cont
))
556 :type
(downcase (cdr (assq :type cont
)))
557 :checked
(cdr (assq :checked cont
))
558 :name
(cdr (assq :name cont
))))
559 (put-text-property start
(point) 'keymap eww-checkbox-map
)
562 (defun eww-form-text (cont)
563 (let ((start (point))
564 (type (downcase (or (cdr (assq :type cont
))
566 (value (or (cdr (assq :value cont
)) ""))
567 (width (string-to-number
568 (or (cdr (assq :size cont
))
571 (when (< (length value
) width
)
572 (insert (make-string (- width
(length value
)) ?
)))
573 (put-text-property start
(point) 'face
'eww-form-text
)
574 (put-text-property start
(point) 'local-map eww-text-map
)
575 (put-text-property start
(point) 'inhibit-read-only t
)
576 (put-text-property start
(point) 'eww-form
577 (list :eww-form eww-form
580 :name
(cdr (assq :name cont
))))
583 (defun eww-process-text-input (beg end length
)
584 (let* ((form (get-text-property end
'eww-form
))
585 (properties (text-properties-at end
))
586 (type (plist-get form
:type
)))
588 (member type
'("text" "password" "textarea")))
591 ;; Delete some space at the end.
594 (if (equal type
"textarea")
595 (1- (line-end-position))
597 (let ((new (- end beg
)))
598 (while (and (> new
0)
599 (eql (following-char) ?
))
600 (delete-region (point) (1+ (point)))
601 (setq new
(1- new
))))
602 (set-text-properties beg end properties
)))
607 (if (equal type
"textarea")
608 (1- (line-end-position))
610 (let ((start (point)))
611 (insert (make-string length ?
))
612 (set-text-properties start
(point) properties
)))))
613 (let ((value (buffer-substring-no-properties
614 (eww-beginning-of-field)
615 (eww-end-of-field))))
616 (when (string-match " +\\'" value
)
617 (setq value
(substring value
0 (match-beginning 0))))
618 (plist-put form
:value value
)
619 (when (equal type
"password")
620 ;; Display passwords as asterisks.
621 (let ((start (eww-beginning-of-field)))
622 (put-text-property start
(+ start
(length value
))
623 'display
(make-string (length value
) ?
*))))))))
625 (defun eww-tag-textarea (cont)
626 (let ((start (point))
627 (value (or (cdr (assq :value cont
)) ""))
628 (lines (string-to-number
629 (or (cdr (assq :rows cont
))
631 (width (string-to-number
632 (or (cdr (assq :cols cont
))
638 (when (< (count-lines start
(point)) lines
)
639 (dotimes (i (- lines
(count-lines start
(point))))
641 (setq end
(point-marker))
643 (while (< (point) end
)
645 (let ((pad (- width
(- (point) (line-beginning-position)))))
647 (insert (make-string pad ?
))))
648 (add-face-text-property (line-beginning-position)
649 (point) 'eww-form-text
)
650 (put-text-property (line-beginning-position) (point)
651 'local-map eww-textarea-map
)
653 (put-text-property start
(point) 'eww-form
654 (list :eww-form eww-form
657 :name
(cdr (assq :name cont
))))))
659 (defun eww-tag-input (cont)
660 (let ((type (downcase (or (cdr (assq :type cont
))
664 ((or (equal type
"checkbox")
665 (equal type
"radio"))
666 (eww-form-checkbox cont
))
667 ((equal type
"submit")
668 (eww-form-submit cont
))
669 ((equal type
"hidden")
670 (let ((form eww-form
)
671 (name (cdr (assq :name cont
))))
672 ;; Don't add <input type=hidden> elements repeatedly.
674 (or (not (consp (car form
)))
675 (not (eq (caar form
) 'hidden
))
676 (not (equal (plist-get (cdr (car form
)) :name
)
678 (setq form
(cdr form
)))
680 (nconc eww-form
(list
683 :value
(cdr (assq :value cont
))))))))
685 (eww-form-text cont
)))
686 (unless (= start
(point))
687 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
689 (defun eww-tag-select (cont)
690 (shr-ensure-paragraph)
691 (let ((menu (list :name
(cdr (assq :name cont
))
697 (when (eq (car elem
) 'option
)
698 (when (cdr (assq :selected
(cdr elem
)))
699 (nconc menu
(list :value
700 (cdr (assq :value
(cdr elem
))))))
701 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
702 (setq max
(max max
(length display
)))
704 :value
(cdr (assq :value
(cdr elem
)))
708 (setq options
(nreverse options
))
709 ;; If we have no selected values, default to the first value.
710 (unless (plist-get menu
:value
)
711 (nconc menu
(list :value
(nth 2 (car options
)))))
713 (let ((selected (eww-select-display menu
)))
715 (make-string (- max
(length selected
)) ?
)))
716 (put-text-property start
(point) 'eww-form menu
)
717 (add-face-text-property start
(point) 'eww-form-select
)
718 (put-text-property start
(point) 'keymap eww-select-map
)
719 (shr-ensure-paragraph))))
721 (defun eww-select-display (select)
722 (let ((value (plist-get select
:value
))
724 (dolist (elem select
)
725 (when (and (consp elem
)
726 (eq (car elem
) 'item
)
727 (equal value
(plist-get (cdr elem
) :value
)))
728 (setq display
(plist-get (cdr elem
) :display
))))
731 (defun eww-change-select ()
732 "Change the value of the select drop-down menu under point."
734 (let* ((input (get-text-property (point) 'eww-form
))
735 (properties (text-properties-at (point)))
736 (completion-ignore-case t
)
739 (mapcar (lambda (elem)
741 (eq (car elem
) 'item
)
742 (cons (plist-get (cdr elem
) :display
)
743 (plist-get (cdr elem
) :value
))))
746 (completing-read "Change value: " options nil
'require-match
))
747 (inhibit-read-only t
))
748 (plist-put input
:value
(cdr (assoc-string display options t
)))
750 (eww-update-field display
))))
752 (defun eww-update-field (string)
753 (let ((properties (text-properties-at (point)))
754 (start (eww-beginning-of-field))
755 (end (1+ (eww-end-of-field))))
756 (delete-region start end
)
758 (make-string (- (- end start
) (length string
)) ?
))
759 (set-text-properties start end properties
)
762 (defun eww-toggle-checkbox ()
763 "Toggle the value of the checkbox under point."
765 (let* ((input (get-text-property (point) 'eww-form
))
766 (type (plist-get input
:type
)))
767 (if (equal type
"checkbox")
770 (if (plist-get input
:checked
)
772 (plist-put input
:checked nil
)
773 (eww-update-field "[ ]"))
774 (plist-put input
:checked t
)
775 (eww-update-field "[X]"))))
776 ;; Radio button. Switch all other buttons off.
777 (let ((name (plist-get input
:name
)))
779 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
780 (when (equal (plist-get (cdr elem
) :name
) name
)
781 (goto-char (car elem
))
782 (if (not (eq (cdr elem
) input
))
784 (plist-put input
:checked nil
)
785 (eww-update-field "[ ]"))
786 (plist-put input
:checked t
)
787 (eww-update-field "[X]")))))
790 (defun eww-inputs (form)
791 (let ((start (point-min))
794 (< start
(point-max)))
795 (when (or (get-text-property start
'eww-form
)
796 (setq start
(next-single-property-change start
'eww-form
)))
797 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
799 (push (cons start
(get-text-property start
'eww-form
))
801 (setq start
(next-single-property-change start
'eww-form
))))
804 (defun eww-input-value (input)
805 (let ((type (plist-get input
:type
))
806 (value (plist-get input
:value
)))
808 ((equal type
"textarea")
811 (goto-char (point-min))
812 (while (re-search-forward "^ +\n\\| +$" nil t
)
813 (replace-match "" t t
))
816 (if (string-match " +\\'" value
)
817 (substring value
0 (match-beginning 0))
821 "Submit the current form."
823 (let* ((this-input (get-text-property (point) 'eww-form
))
824 (form (plist-get this-input
:eww-form
))
826 (dolist (elem (sort (eww-inputs form
)
828 (< (car o1
) (car o2
)))))
829 (let* ((input (cdr elem
))
830 (input-start (car elem
))
831 (name (plist-get input
:name
)))
834 ((member (plist-get input
:type
) '("checkbox" "radio"))
835 (when (plist-get input
:checked
)
836 (push (cons name
(plist-get input
:value
))
838 ((equal (plist-get input
:type
) "submit")
839 ;; We want the values from buttons if we hit a button if
840 ;; we hit enter on it, or if it's the first button after
841 ;; the field we did hit return on.
842 (when (or (eq input this-input
)
843 (and (not (eq input this-input
))
845 (> input-start
(point))))
847 (push (cons name
(plist-get input
:value
))
850 (push (cons name
(eww-input-value input
))
853 (when (and (consp elem
)
854 (eq (car elem
) 'hidden
))
855 (push (cons (plist-get (cdr elem
) :name
)
856 (plist-get (cdr elem
) :value
))
858 (if (and (stringp (cdr (assq :method form
)))
859 (equal (downcase (cdr (assq :method form
))) "post"))
860 (let ((url-request-method "POST")
861 (url-request-extra-headers
862 '(("Content-Type" .
"application/x-www-form-urlencoded")))
863 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
864 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
868 (if (cdr (assq :action form
))
869 (shr-expand-url (cdr (assq :action form
))
873 (mm-url-encode-www-form-urlencoded values
))))))
875 (defun eww-browse-with-external-browser ()
876 "Browse the current URL with an external browser.
877 The browser to used is specified by the `shr-external-browser' variable."
879 (funcall shr-external-browser eww-current-url
))
881 (defun eww-copy-page-url ()
883 (message "%s" eww-current-url
)
884 (kill-new eww-current-url
))
886 (defun eww-download ()
887 "Download URL under point to `eww-download-directory'."
889 (let ((url (get-text-property (point) 'shr-url
)))
891 (message "No URL under point")
892 (url-retrieve url
'eww-download-callback
(list url
)))))
894 (defun eww-download-callback (status url
)
895 (unless (plist-get status
:error
)
896 (let* ((obj (url-generic-parse-url url
))
897 (path (car (url-path-and-query obj
)))
898 (file (eww-make-unique-file-name (file-name-nondirectory path
)
901 (message "Saved %s" file
))))
903 (defun eww-make-unique-file-name (file directory
)
905 ((zerop (length file
))
907 ((string-match "\\`[.]" file
)
908 (setq file
(concat "!" file
))))
911 (while (file-exists-p (expand-file-name file directory
))
913 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
914 (format "%s(%d)%s" (match-string 1 file
)
915 count
(match-string 2 file
))
916 (format "%s(%d)" file count
)))
917 (setq count
(1+ count
)))
918 (expand-file-name file directory
)))