(eww-mode-map): Add a menu bar.
[emacs.git] / lisp / net / eww.el
blobd56031a6f34ba4d13d482a7d6410fdd440ab8f25
1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'mm-url)
33 (defgroup eww nil
34 "Emacs Web Wowser"
35 :version "24.4"
36 :group 'hypermedia
37 :prefix "eww-")
39 (defcustom eww-header-line-format "%t: %u"
40 "Header line format.
41 - %t is replaced by the title.
42 - %u is replaced by the URL."
43 :version "24.4"
44 :group 'eww
45 :type 'string)
47 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
48 "Prefix URL to search engine"
49 :version "24.4"
50 :group 'eww
51 :type 'string)
53 (defcustom eww-download-path "~/Downloads/"
54 "Path where files will downloaded."
55 :version "24.4"
56 :group 'eww
57 :type 'string)
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."
64 :version "24.4"
65 :group 'eww)
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."
72 :version "24.4"
73 :group 'eww)
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."
80 :version "24.4"
81 :group 'eww)
83 (defface eww-form-text
84 '((t (:background "#505050"
85 :foreground "white"
86 :box (:line-width 1))))
87 "Face for eww text inputs."
88 :version "24.4"
89 :group 'eww)
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)
104 ;;;###autoload
105 (defun eww (url)
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))
112 (progn
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)))
123 ;;;###autoload
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)))
131 (when 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))
140 (shr-target-id
141 (and (string-match "#\\(.*\\)" url)
142 (match-string 1 url)))
143 (content-type
144 (mail-header-parse-content-type
145 (or (cdr (assoc "content-type" headers))
146 "text/plain")))
147 (charset (intern
148 (downcase
149 (or (cdr (assq 'charset (cdr content-type)))
150 (eww-detect-charset (equal (car content-type)
151 "text/html"))
152 "utf8"))))
153 (data-buffer (current-buffer)))
154 (unwind-protect
155 (progn
156 (cond
157 ((equal (car content-type) "text/html")
158 (eww-display-html charset url))
159 ((string-match "^image/" (car content-type))
160 (eww-display-image))
162 (eww-display-raw charset)))
163 (setq eww-history-position 0)
164 (cond
165 (point
166 (goto-char point))
167 (shr-target-id
168 (let ((point (next-single-property-change
169 (point-min) 'shr-target-id)))
170 (when point
171 (goto-char (1+ point)))))))
172 (kill-buffer data-buffer))))
174 (defun eww-parse-headers ()
175 (let ((headers nil))
176 (goto-char (point-min))
177 (while (and (not (eobp))
178 (not (eolp)))
179 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
180 (push (cons (downcase (match-string 1))
181 (match-string 2))
182 headers))
183 (forward-line 1))
184 (unless (eobp)
185 (forward-line 1))
186 headers))
188 (defun eww-detect-charset (html-p)
189 (let ((case-fold-search t)
190 (pt (point)))
191 (or (and html-p
192 (re-search-forward
193 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
194 (goto-char pt)
195 (match-string 1))
196 (and (looking-at
197 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
198 (match-string 1)))))
200 (defun eww-display-html (charset url)
201 (unless (eq charset 'utf8)
202 (decode-coding-region (point) (point-max) charset))
203 (let ((document
204 (list
205 'base (list (cons 'href url))
206 (libxml-parse-html-region (point) (point-max)))))
207 (eww-setup-buffer)
208 (setq eww-current-url url)
209 (eww-update-header-line-format)
210 (let ((inhibit-read-only t)
211 (after-change-functions nil)
212 (shr-width 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)
221 (a . eww-tag-a))))
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))
228 (where (assoc
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
240 ;; "best" one.
241 ("start" . eww-start-url)
242 ("home" . eww-home-url)
243 ("contents" . eww-contents-url)
244 ("up" . eww-up-url)))))
245 (and href
246 where
247 (set (cdr where) (cdr href)))))
249 (defun eww-tag-link (cont)
250 (eww-handle-link cont)
251 (shr-generic cont))
253 (defun eww-tag-a (cont)
254 (eww-handle-link cont)
255 (shr-tag-a cont))
257 (defun eww-update-header-line-format ()
258 (if eww-header-line-format
259 (setq header-line-format
260 (replace-regexp-in-string
261 "%" "%%"
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 "")
269 (dolist (sub cont)
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)
277 (assq :text cont))))
278 (bgcolor (cdr (assq :bgcolor cont)))
279 (shr-stylesheet (list (cons 'color fgcolor)
280 (cons 'background-color bgcolor))))
281 (shr-generic cont)
282 (eww-colorize-region start (point) fgcolor bgcolor)))
284 (defun eww-colorize-region (start end fg &optional bg)
285 (when (or fg bg)
286 (let ((new-colors (shr-color-check fg bg)))
287 (when new-colors
288 (when fg
289 (add-face-text-property start end
290 (list :foreground (cadr new-colors))
292 (when bg
293 (add-face-text-property start end
294 (list :background (car new-colors))
295 t))))))
297 (defun eww-display-raw (charset)
298 (let ((data (buffer-substring (point) (point-max))))
299 (eww-setup-buffer)
300 (let ((inhibit-read-only t))
301 (insert data))
302 (goto-char (point-min))))
304 (defun eww-display-image ()
305 (let ((data (buffer-substring (point) (point-max))))
306 (eww-setup-buffer)
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))
314 (remove-overlays)
315 (erase-buffer))
316 (unless (eq major-mode 'eww-mode)
317 (eww-mode)))
319 (defvar eww-mode-map
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 ""
341 '("eww"
342 ["Quit" eww-quit t]
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]))
352 map))
354 (define-derived-mode eww-mode nil "eww"
355 "Mode for browsing the web.
357 \\{eww-mode-map}"
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
369 :point (point)
370 :text (buffer-string))
371 eww-history))
373 (defun eww-browse-url (url &optional new-window)
374 (when (and (equal major-mode 'eww-mode)
375 eww-current-url)
376 (eww-save-history))
377 (eww url))
379 (defun eww-quit ()
380 "Exit the Emacs Web Wowser."
381 (interactive)
382 (setq eww-history nil)
383 (kill-buffer (current-buffer)))
385 (defun eww-back-url ()
386 "Go to the previously displayed page."
387 (interactive)
388 (when (>= eww-history-position (length eww-history))
389 (error "No previous page"))
390 (eww-save-history)
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."
396 (interactive)
397 (when (zerop eww-history-position)
398 (error "No next page"))
399 (eww-save-history)
400 (eww-restore-history (elt eww-history (1- eww-history-position))))
402 (defun eww-restore-history (elem)
403 (let ((inhibit-read-only t))
404 (erase-buffer)
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>
412 or <a> tag."
413 (interactive)
414 (if eww-next-url
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>
421 or <a> tag."
422 (interactive)
423 (if eww-previous-url
424 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
425 (error "No `previous' on this page")))
427 (defun eww-up-url ()
428 "Go to the page marked `up'.
429 A page is marked `up' if rel=\"up\" appears in a <link>
430 or <a> tag."
431 (interactive)
432 (if eww-up-url
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."
440 (interactive)
441 (let ((best-url (or eww-start-url
442 eww-contents-url
443 eww-home-url)))
444 (if best-url
445 (eww-browse-url (shr-expand-url best-url eww-current-url))
446 (error "No `top' for this page"))))
448 (defun eww-reload ()
449 "Reload the current page."
450 (interactive)
451 (url-retrieve eww-current-url 'eww-render
452 (list eww-current-url (point))))
454 ;; Form support.
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)
462 map))
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)
469 map))
471 (defvar eww-text-map
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)
480 map))
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)
489 map))
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)
495 map))
497 (defun eww-beginning-of-text ()
498 "Move to the start of the input field."
499 (interactive)
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."
504 (interactive)
505 (goto-char (eww-end-of-field))
506 (let ((start (eww-beginning-of-field)))
507 (while (and (equal (following-char) ? )
508 (> (point) start))
509 (forward-char -1))
510 (when (> (point) start)
511 (forward-char 1))))
513 (defun eww-beginning-of-field ()
514 (cond
515 ((bobp)
516 (point))
517 ((not (eq (get-text-property (point) 'eww-form)
518 (get-text-property (1- (point)) 'eww-form)))
519 (point))
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)
529 (let ((eww-form
530 (list (assq :method cont)
531 (assq :action cont)))
532 (start (point)))
533 (shr-ensure-paragraph)
534 (shr-generic cont)
535 (unless (bolp)
536 (insert "\n"))
537 (insert "\n")
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))))
545 (setq value
546 (if (zerop (length value))
547 "Submit"
548 value))
549 (insert 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
553 :value value
554 :type "submit"
555 :name (cdr (assq :name cont))))
556 (put-text-property start (point) 'keymap eww-submit-map)
557 (insert " ")))
559 (defun eww-form-checkbox (cont)
560 (let ((start (point)))
561 (if (cdr (assq :checked cont))
562 (insert "[X]")
563 (insert "[ ]"))
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)
572 (insert " ")))
574 (defun eww-form-text (cont)
575 (let ((start (point))
576 (type (downcase (or (cdr (assq :type cont))
577 "text")))
578 (value (or (cdr (assq :value cont)) ""))
579 (width (string-to-number
580 (or (cdr (assq :size cont))
581 "40"))))
582 (insert value)
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
590 :value value
591 :type type
592 :name (cdr (assq :name cont))))
593 (insert " ")))
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)))
599 (when (and form
600 (member type '("text" "password" "textarea")))
601 (cond
602 ((zerop length)
603 ;; Delete some space at the end.
604 (save-excursion
605 (goto-char
606 (if (equal type "textarea")
607 (1- (line-end-position))
608 (eww-end-of-field)))
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)))
615 ((> length 0)
616 ;; Add padding.
617 (save-excursion
618 (goto-char
619 (if (equal type "textarea")
620 (1- (line-end-position))
621 (eww-end-of-field)))
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))
642 "10")))
643 (width (string-to-number
644 (or (cdr (assq :cols cont))
645 "10")))
646 end)
647 (shr-ensure-newline)
648 (insert value)
649 (shr-ensure-newline)
650 (when (< (count-lines start (point)) lines)
651 (dotimes (i (- lines (count-lines start (point))))
652 (insert "\n")))
653 (setq end (point-marker))
654 (goto-char start)
655 (while (< (point) end)
656 (end-of-line)
657 (let ((pad (- width (- (point) (line-beginning-position)))))
658 (when (> pad 0)
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)
664 (forward-line 1))
665 (put-text-property start (point) 'eww-form
666 (list :eww-form eww-form
667 :value value
668 :type "textarea"
669 :name (cdr (assq :name cont))))))
671 (defun eww-tag-input (cont)
672 (let ((type (downcase (or (cdr (assq :type cont))
673 "text")))
674 (start (point)))
675 (cond
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.
685 (while (and form
686 (or (not (consp (car form)))
687 (not (eq (caar form) 'hidden))
688 (not (equal (plist-get (cdr (car form)) :name)
689 name))))
690 (setq form (cdr form)))
691 (unless form
692 (nconc eww-form (list
693 (list 'hidden
694 :name name
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))
704 :eww-form eww-form))
705 (options nil)
706 (start (point))
707 (max 0))
708 (dolist (elem 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)))
715 (push (list 'item
716 :value (cdr (assq :value (cdr elem)))
717 :display display)
718 options))))
719 (when options
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)))))
724 (nconc menu options)
725 (let ((selected (eww-select-display menu)))
726 (insert selected
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))
735 display)
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))))
741 display))
743 (defun eww-change-select ()
744 "Change the value of the select drop-down menu under point."
745 (interactive)
746 (let* ((input (get-text-property (point) 'eww-form))
747 (properties (text-properties-at (point)))
748 (completion-ignore-case t)
749 (options
750 (delq nil
751 (mapcar (lambda (elem)
752 (and (consp elem)
753 (eq (car elem) 'item)
754 (cons (plist-get (cdr elem) :display)
755 (plist-get (cdr elem) :value))))
756 input)))
757 (display
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)))
761 (goto-char
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)
769 (insert string
770 (make-string (- (- end start) (length string)) ? ))
771 (set-text-properties start end properties)
772 start))
774 (defun eww-toggle-checkbox ()
775 "Toggle the value of the checkbox under point."
776 (interactive)
777 (let* ((input (get-text-property (point) 'eww-form))
778 (type (plist-get input :type)))
779 (if (equal type "checkbox")
780 (goto-char
782 (if (plist-get input :checked)
783 (progn
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)))
790 (save-excursion
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))
795 (progn
796 (plist-put input :checked nil)
797 (eww-update-field "[ ]"))
798 (plist-put input :checked t)
799 (eww-update-field "[X]")))))
800 (forward-char 1)))))
802 (defun eww-inputs (form)
803 (let ((start (point-min))
804 (inputs nil))
805 (while (and start
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)
810 form)
811 (push (cons start (get-text-property start 'eww-form))
812 inputs))
813 (setq start (next-single-property-change start 'eww-form))))
814 (nreverse inputs)))
816 (defun eww-input-value (input)
817 (let ((type (plist-get input :type))
818 (value (plist-get input :value)))
819 (cond
820 ((equal type "textarea")
821 (with-temp-buffer
822 (insert value)
823 (goto-char (point-min))
824 (while (re-search-forward "^ +\n\\| +$" nil t)
825 (replace-match "" t t))
826 (buffer-string)))
828 (if (string-match " +\\'" value)
829 (substring value 0 (match-beginning 0))
830 value)))))
832 (defun eww-submit ()
833 "Submit the current form."
834 (interactive)
835 (let* ((this-input (get-text-property (point) 'eww-form))
836 (form (plist-get this-input :eww-form))
837 values next-submit)
838 (dolist (elem (sort (eww-inputs form)
839 (lambda (o1 o2)
840 (< (car o1) (car o2)))))
841 (let* ((input (cdr elem))
842 (input-start (car elem))
843 (name (plist-get input :name)))
844 (when name
845 (cond
846 ((member (plist-get input :type) '("checkbox" "radio"))
847 (when (plist-get input :checked)
848 (push (cons name (plist-get input :value))
849 values)))
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))
856 (null next-submit)
857 (> input-start (point))))
858 (setq next-submit t)
859 (push (cons name (plist-get input :value))
860 values)))
862 (push (cons name (eww-input-value input))
863 values))))))
864 (dolist (elem form)
865 (when (and (consp elem)
866 (eq (car elem) 'hidden))
867 (push (cons (plist-get (cdr elem) :name)
868 (plist-get (cdr elem) :value))
869 values)))
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))
877 eww-current-url)))
878 (eww-browse-url
879 (concat
880 (if (cdr (assq :action form))
881 (shr-expand-url (cdr (assq :action form))
882 eww-current-url)
883 eww-current-url)
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."
890 (interactive)
891 (funcall shr-external-browser eww-current-url))
893 (defun eww-copy-page-url ()
894 (interactive)
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'."
900 (interactive)
901 (let ((url (get-text-property (point) 'shr-url)))
902 (if (not 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)
911 eww-download-path)))
912 (write-file file)
913 (message "Saved %s" file))))
915 (defun eww-make-unique-file-name (file directory)
916 (cond
917 ((zerop (length file))
918 (setq file "!"))
919 ((string-match "\\`[.]" file)
920 (setq file (concat "!" file))))
921 (let ((base file)
922 (count 1))
923 (while (file-exists-p (expand-file-name file directory))
924 (setq file
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)))
932 (provide 'eww)
934 ;;; eww.el ends here