eww.el: If given a non-domain text, search for the term
[bpt/emacs.git] / lisp / net / eww.el
1 ;;; eww.el --- Emacs Web Wowser
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'mm-url)
32
33 (defgroup eww nil
34 "Emacs Web Wowser"
35 :version "24.4"
36 :group 'hypermedia
37 :prefix "eww-")
38
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)
46
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)
52
53 (defface eww-form-submit
54 '((((type x w32 ns) (class color)) ; Like default mode line
55 :box (:line-width 2 :style released-button)
56 :background "#808080" :foreground "black"))
57 "Face for eww buffer buttons."
58 :version "24.4"
59 :group 'eww)
60
61 (defface eww-form-checkbox
62 '((((type x w32 ns) (class color)) ; Like default mode line
63 :box (:line-width 2 :style released-button)
64 :background "lightgrey" :foreground "black"))
65 "Face for eww buffer buttons."
66 :version "24.4"
67 :group 'eww)
68
69 (defface eww-form-select
70 '((((type x w32 ns) (class color)) ; Like default mode line
71 :box (:line-width 2 :style released-button)
72 :background "lightgrey" :foreground "black"))
73 "Face for eww buffer buttons."
74 :version "24.4"
75 :group 'eww)
76
77 (defface eww-form-text
78 '((t (:background "#505050"
79 :foreground "white"
80 :box (:line-width 1))))
81 "Face for eww text inputs."
82 :version "24.4"
83 :group 'eww)
84
85 (defvar eww-current-url nil)
86 (defvar eww-current-title ""
87 "Title of current page.")
88 (defvar eww-history nil)
89
90 (defvar eww-next-url nil)
91 (defvar eww-previous-url nil)
92 (defvar eww-up-url nil)
93 (defvar eww-home-url nil)
94 (defvar eww-start-url nil)
95 (defvar eww-contents-url nil)
96
97 ;;;###autoload
98 (defun eww (url)
99 "Fetch URL and render the page.
100 If the input doesn't look like an URL or a domain name, the
101 word(s) will be searched for via `eww-search-prefix'."
102 (interactive "sEnter URL or keywords: ")
103 (if (and (= (length (split-string url)) 1)
104 (> (length (split-string url "\\.")) 1))
105 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
106 (setq url (concat "http://" url)))
107 (setq url (concat eww-search-prefix
108 (replace-regexp-in-string " " "+" url))))
109 (url-retrieve url 'eww-render (list url)))
110
111 ;;;###autoload
112 (defun eww-open-file (file)
113 "Render a file using EWW."
114 (interactive "fFile: ")
115 (eww (concat "file://" (expand-file-name file))))
116
117 (defun eww-render (status url &optional point)
118 (let ((redirect (plist-get status :redirect)))
119 (when redirect
120 (setq url redirect)))
121 (set (make-local-variable 'eww-next-url) nil)
122 (set (make-local-variable 'eww-previous-url) nil)
123 (set (make-local-variable 'eww-up-url) nil)
124 (set (make-local-variable 'eww-home-url) nil)
125 (set (make-local-variable 'eww-start-url) nil)
126 (set (make-local-variable 'eww-contents-url) nil)
127 (let* ((headers (eww-parse-headers))
128 (shr-target-id
129 (and (string-match "#\\(.*\\)" url)
130 (match-string 1 url)))
131 (content-type
132 (mail-header-parse-content-type
133 (or (cdr (assoc "content-type" headers))
134 "text/plain")))
135 (charset (intern
136 (downcase
137 (or (cdr (assq 'charset (cdr content-type)))
138 (eww-detect-charset (equal (car content-type)
139 "text/html"))
140 "utf8"))))
141 (data-buffer (current-buffer)))
142 (unwind-protect
143 (progn
144 (cond
145 ((equal (car content-type) "text/html")
146 (eww-display-html charset url))
147 ((string-match "^image/" (car content-type))
148 (eww-display-image))
149 (t
150 (eww-display-raw charset)))
151 (cond
152 (point
153 (goto-char point))
154 (shr-target-id
155 (let ((point (next-single-property-change
156 (point-min) 'shr-target-id)))
157 (when point
158 (goto-char (1+ point)))))))
159 (kill-buffer data-buffer))))
160
161 (defun eww-parse-headers ()
162 (let ((headers nil))
163 (goto-char (point-min))
164 (while (and (not (eobp))
165 (not (eolp)))
166 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
167 (push (cons (downcase (match-string 1))
168 (match-string 2))
169 headers))
170 (forward-line 1))
171 (unless (eobp)
172 (forward-line 1))
173 headers))
174
175 (defun eww-detect-charset (html-p)
176 (let ((case-fold-search t)
177 (pt (point)))
178 (or (and html-p
179 (re-search-forward
180 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t)
181 (goto-char pt)
182 (match-string 1))
183 (and (looking-at
184 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
185 (match-string 1)))))
186
187 (defun eww-display-html (charset url)
188 (unless (eq charset 'utf8)
189 (decode-coding-region (point) (point-max) charset))
190 (let ((document
191 (list
192 'base (list (cons 'href url))
193 (libxml-parse-html-region (point) (point-max)))))
194 (eww-setup-buffer)
195 (setq eww-current-url url)
196 (eww-update-header-line-format)
197 (let ((inhibit-read-only t)
198 (after-change-functions nil)
199 (shr-width nil)
200 (shr-external-rendering-functions
201 '((title . eww-tag-title)
202 (form . eww-tag-form)
203 (input . eww-tag-input)
204 (textarea . eww-tag-textarea)
205 (body . eww-tag-body)
206 (select . eww-tag-select)
207 (link . eww-tag-link)
208 (a . eww-tag-a))))
209 (shr-insert-document document))
210 (goto-char (point-min))))
211
212 (defun eww-handle-link (cont)
213 (let* ((rel (assq :rel cont))
214 (href (assq :href cont))
215 (where (assoc
216 ;; The text associated with :rel is case-insensitive.
217 (if rel (downcase (cdr rel)))
218 '(("next" . eww-next-url)
219 ;; Texinfo uses "previous", but HTML specifies
220 ;; "prev", so recognize both.
221 ("previous" . eww-previous-url)
222 ("prev" . eww-previous-url)
223 ;; HTML specifies "start" but also "contents",
224 ;; and Gtk seems to use "home". Recognize
225 ;; them all; but store them in different
226 ;; variables so that we can readily choose the
227 ;; "best" one.
228 ("start" . eww-start-url)
229 ("home" . eww-home-url)
230 ("contents" . eww-contents-url)
231 ("up" . eww-up-url)))))
232 (and href
233 where
234 (set (cdr where) (cdr href)))))
235
236 (defun eww-tag-link (cont)
237 (eww-handle-link cont)
238 (shr-generic cont))
239
240 (defun eww-tag-a (cont)
241 (eww-handle-link cont)
242 (shr-tag-a cont))
243
244 (defun eww-update-header-line-format ()
245 (if eww-header-line-format
246 (setq header-line-format
247 (replace-regexp-in-string
248 "%" "%%"
249 (format-spec eww-header-line-format
250 `((?u . ,eww-current-url)
251 (?t . ,eww-current-title)))))
252 (setq header-line-format nil)))
253
254 (defun eww-tag-title (cont)
255 (setq eww-current-title "")
256 (dolist (sub cont)
257 (when (eq (car sub) 'text)
258 (setq eww-current-title (concat eww-current-title (cdr sub)))))
259 (eww-update-header-line-format))
260
261 (defun eww-tag-body (cont)
262 (let* ((start (point))
263 (fgcolor (cdr (or (assq :fgcolor cont)
264 (assq :text cont))))
265 (bgcolor (cdr (assq :bgcolor cont)))
266 (shr-stylesheet (list (cons 'color fgcolor)
267 (cons 'background-color bgcolor))))
268 (shr-generic cont)
269 (eww-colorize-region start (point) fgcolor bgcolor)))
270
271 (defun eww-colorize-region (start end fg &optional bg)
272 (when (or fg bg)
273 (let ((new-colors (shr-color-check fg bg)))
274 (when new-colors
275 (when fg
276 (add-face-text-property start end
277 (list :foreground (cadr new-colors))
278 t))
279 (when bg
280 (add-face-text-property start end
281 (list :background (car new-colors))
282 t))))))
283
284 (defun eww-display-raw (charset)
285 (let ((data (buffer-substring (point) (point-max))))
286 (eww-setup-buffer)
287 (let ((inhibit-read-only t))
288 (insert data))
289 (goto-char (point-min))))
290
291 (defun eww-display-image ()
292 (let ((data (buffer-substring (point) (point-max))))
293 (eww-setup-buffer)
294 (let ((inhibit-read-only t))
295 (shr-put-image data nil))
296 (goto-char (point-min))))
297
298 (defun eww-setup-buffer ()
299 (pop-to-buffer (get-buffer-create "*eww*"))
300 (remove-overlays)
301 (let ((inhibit-read-only t))
302 (erase-buffer))
303 (eww-mode))
304
305 (defvar eww-mode-map
306 (let ((map (make-sparse-keymap)))
307 (suppress-keymap map)
308 (define-key map "q" 'eww-quit)
309 (define-key map "g" 'eww-reload)
310 (define-key map [tab] 'shr-next-link)
311 (define-key map [backtab] 'shr-previous-link)
312 (define-key map [delete] 'scroll-down-command)
313 (define-key map "\177" 'scroll-down-command)
314 (define-key map " " 'scroll-up-command)
315 (define-key map "l" 'eww-back-url)
316 (define-key map "n" 'eww-next-url)
317 (define-key map "p" 'eww-previous-url)
318 (define-key map "u" 'eww-up-url)
319 (define-key map "t" 'eww-top-url)
320 map))
321
322 (define-derived-mode eww-mode nil "eww"
323 "Mode for browsing the web.
324
325 \\{eww-mode-map}"
326 (set (make-local-variable 'eww-current-url) 'author)
327 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
328 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
329 ;;(setq buffer-read-only t)
330 )
331
332 (defun eww-browse-url (url &optional new-window)
333 (when (and (equal major-mode 'eww-mode)
334 eww-current-url)
335 (push (list eww-current-url (point))
336 eww-history))
337 (eww url))
338
339 (defun eww-quit ()
340 "Exit the Emacs Web Wowser."
341 (interactive)
342 (setq eww-history nil)
343 (kill-buffer (current-buffer)))
344
345 (defun eww-back-url ()
346 "Go to the previously displayed page."
347 (interactive)
348 (when (zerop (length eww-history))
349 (error "No previous page"))
350 (let ((prev (pop eww-history)))
351 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
352
353 (defun eww-next-url ()
354 "Go to the page marked `next'.
355 A page is marked `next' if rel=\"next\" appears in a <link>
356 or <a> tag."
357 (interactive)
358 (if eww-next-url
359 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
360 (error "No `next' on this page")))
361
362 (defun eww-previous-url ()
363 "Go to the page marked `previous'.
364 A page is marked `previous' if rel=\"previous\" appears in a <link>
365 or <a> tag."
366 (interactive)
367 (if eww-previous-url
368 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
369 (error "No `previous' on this page")))
370
371 (defun eww-up-url ()
372 "Go to the page marked `up'.
373 A page is marked `up' if rel=\"up\" appears in a <link>
374 or <a> tag."
375 (interactive)
376 (if eww-up-url
377 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
378 (error "No `up' on this page")))
379
380 (defun eww-top-url ()
381 "Go to the page marked `top'.
382 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
383 appears in a <link> or <a> tag."
384 (interactive)
385 (let ((best-url (or eww-start-url
386 eww-contents-url
387 eww-home-url)))
388 (if best-url
389 (eww-browse-url (shr-expand-url best-url eww-current-url))
390 (error "No `top' for this page"))))
391
392 (defun eww-reload ()
393 "Reload the current page."
394 (interactive)
395 (url-retrieve eww-current-url 'eww-render
396 (list eww-current-url (point))))
397
398 ;; Form support.
399
400 (defvar eww-form nil)
401
402 (defvar eww-submit-map
403 (let ((map (make-sparse-keymap)))
404 (define-key map "\r" 'eww-submit)
405 map))
406
407 (defvar eww-checkbox-map
408 (let ((map (make-sparse-keymap)))
409 (define-key map [space] 'eww-toggle-checkbox)
410 (define-key map "\r" 'eww-toggle-checkbox)
411 map))
412
413 (defvar eww-text-map
414 (let ((map (make-keymap)))
415 (set-keymap-parent map text-mode-map)
416 (define-key map "\r" 'eww-submit)
417 (define-key map [(control a)] 'eww-beginning-of-text)
418 (define-key map [(control e)] 'eww-end-of-text)
419 (define-key map [tab] 'shr-next-link)
420 (define-key map [backtab] 'shr-previous-link)
421 map))
422
423 (defvar eww-textarea-map
424 (let ((map (make-keymap)))
425 (set-keymap-parent map text-mode-map)
426 (define-key map "\r" 'forward-line)
427 (define-key map [tab] 'shr-next-link)
428 (define-key map [backtab] 'shr-previous-link)
429 map))
430
431 (defvar eww-select-map
432 (let ((map (make-sparse-keymap)))
433 (define-key map "\r" 'eww-change-select)
434 map))
435
436 (defun eww-beginning-of-text ()
437 "Move to the start of the input field."
438 (interactive)
439 (goto-char (eww-beginning-of-field)))
440
441 (defun eww-end-of-text ()
442 "Move to the end of the text in the input field."
443 (interactive)
444 (goto-char (eww-end-of-field))
445 (let ((start (eww-beginning-of-field)))
446 (while (and (equal (following-char) ? )
447 (> (point) start))
448 (forward-char -1))
449 (when (> (point) start)
450 (forward-char 1))))
451
452 (defun eww-beginning-of-field ()
453 (cond
454 ((bobp)
455 (point))
456 ((not (eq (get-text-property (point) 'eww-form)
457 (get-text-property (1- (point)) 'eww-form)))
458 (point))
459 (t
460 (previous-single-property-change
461 (point) 'eww-form nil (point-min)))))
462
463 (defun eww-end-of-field ()
464 (1- (next-single-property-change
465 (point) 'eww-form nil (point-max))))
466
467 (defun eww-tag-form (cont)
468 (let ((eww-form
469 (list (assq :method cont)
470 (assq :action cont)))
471 (start (point)))
472 (shr-ensure-paragraph)
473 (shr-generic cont)
474 (unless (bolp)
475 (insert "\n"))
476 (insert "\n")
477 (when (> (point) start)
478 (put-text-property start (1+ start)
479 'eww-form eww-form))))
480
481 (defun eww-form-submit (cont)
482 (let ((start (point))
483 (value (cdr (assq :value cont))))
484 (setq value
485 (if (zerop (length value))
486 "Submit"
487 value))
488 (insert value)
489 (add-face-text-property start (point) 'eww-form-submit)
490 (put-text-property start (point) 'eww-form
491 (list :eww-form eww-form
492 :value value
493 :type "submit"
494 :name (cdr (assq :name cont))))
495 (put-text-property start (point) 'keymap eww-submit-map)
496 (insert " ")))
497
498 (defun eww-form-checkbox (cont)
499 (let ((start (point)))
500 (if (cdr (assq :checked cont))
501 (insert "[X]")
502 (insert "[ ]"))
503 (add-face-text-property start (point) 'eww-form-checkbox)
504 (put-text-property start (point) 'eww-form
505 (list :eww-form eww-form
506 :value (cdr (assq :value cont))
507 :type (downcase (cdr (assq :type cont)))
508 :checked (cdr (assq :checked cont))
509 :name (cdr (assq :name cont))))
510 (put-text-property start (point) 'keymap eww-checkbox-map)
511 (insert " ")))
512
513 (defun eww-form-text (cont)
514 (let ((start (point))
515 (type (downcase (or (cdr (assq :type cont))
516 "text")))
517 (value (or (cdr (assq :value cont)) ""))
518 (width (string-to-number
519 (or (cdr (assq :size cont))
520 "40"))))
521 (insert value)
522 (when (< (length value) width)
523 (insert (make-string (- width (length value)) ? )))
524 (put-text-property start (point) 'face 'eww-form-text)
525 (put-text-property start (point) 'local-map eww-text-map)
526 (put-text-property start (point) 'inhibit-read-only t)
527 (put-text-property start (point) 'eww-form
528 (list :eww-form eww-form
529 :value value
530 :type type
531 :name (cdr (assq :name cont))))
532 (insert " ")))
533
534 (defun eww-process-text-input (beg end length)
535 (let* ((form (get-text-property end 'eww-form))
536 (properties (text-properties-at end))
537 (type (plist-get form :type)))
538 (when (and form
539 (member type '("text" "password" "textarea")))
540 (cond
541 ((zerop length)
542 ;; Delete some space at the end.
543 (save-excursion
544 (goto-char
545 (if (equal type "textarea")
546 (1- (line-end-position))
547 (eww-end-of-field)))
548 (let ((new (- end beg)))
549 (while (and (> new 0)
550 (eql (following-char) ? ))
551 (delete-region (point) (1+ (point)))
552 (setq new (1- new))))
553 (set-text-properties beg end properties)))
554 ((> length 0)
555 ;; Add padding.
556 (save-excursion
557 (goto-char
558 (if (equal type "textarea")
559 (1- (line-end-position))
560 (eww-end-of-field)))
561 (let ((start (point)))
562 (insert (make-string length ? ))
563 (set-text-properties start (point) properties)))))
564 (let ((value (buffer-substring-no-properties
565 (eww-beginning-of-field)
566 (eww-end-of-field))))
567 (when (string-match " +\\'" value)
568 (setq value (substring value 0 (match-beginning 0))))
569 (plist-put form :value value)
570 (when (equal type "password")
571 ;; Display passwords as asterisks.
572 (let ((start (eww-beginning-of-field)))
573 (put-text-property start (+ start (length value))
574 'display (make-string (length value) ?*))))))))
575
576 (defun eww-tag-textarea (cont)
577 (let ((start (point))
578 (value (or (cdr (assq :value cont)) ""))
579 (lines (string-to-number
580 (or (cdr (assq :rows cont))
581 "10")))
582 (width (string-to-number
583 (or (cdr (assq :cols cont))
584 "10")))
585 end)
586 (shr-ensure-newline)
587 (insert value)
588 (shr-ensure-newline)
589 (when (< (count-lines start (point)) lines)
590 (dotimes (i (- lines (count-lines start (point))))
591 (insert "\n")))
592 (setq end (point-marker))
593 (goto-char start)
594 (while (< (point) end)
595 (end-of-line)
596 (let ((pad (- width (- (point) (line-beginning-position)))))
597 (when (> pad 0)
598 (insert (make-string pad ? ))))
599 (add-face-text-property (line-beginning-position)
600 (point) 'eww-form-text)
601 (put-text-property (line-beginning-position) (point)
602 'local-map eww-textarea-map)
603 (forward-line 1))
604 (put-text-property start (point) 'eww-form
605 (list :eww-form eww-form
606 :value value
607 :type "textarea"
608 :name (cdr (assq :name cont))))))
609
610 (defun eww-tag-input (cont)
611 (let ((type (downcase (or (cdr (assq :type cont))
612 "text")))
613 (start (point)))
614 (cond
615 ((or (equal type "checkbox")
616 (equal type "radio"))
617 (eww-form-checkbox cont))
618 ((equal type "submit")
619 (eww-form-submit cont))
620 ((equal type "hidden")
621 (let ((form eww-form)
622 (name (cdr (assq :name cont))))
623 ;; Don't add <input type=hidden> elements repeatedly.
624 (while (and form
625 (or (not (consp (car form)))
626 (not (eq (caar form) 'hidden))
627 (not (equal (plist-get (cdr (car form)) :name)
628 name))))
629 (setq form (cdr form)))
630 (unless form
631 (nconc eww-form (list
632 (list 'hidden
633 :name name
634 :value (cdr (assq :value cont))))))))
635 (t
636 (eww-form-text cont)))
637 (unless (= start (point))
638 (put-text-property start (1+ start) 'help-echo "Input field"))))
639
640 (defun eww-tag-select (cont)
641 (shr-ensure-paragraph)
642 (let ((menu (list :name (cdr (assq :name cont))
643 :eww-form eww-form))
644 (options nil)
645 (start (point))
646 (max 0))
647 (dolist (elem cont)
648 (when (eq (car elem) 'option)
649 (when (cdr (assq :selected (cdr elem)))
650 (nconc menu (list :value
651 (cdr (assq :value (cdr elem))))))
652 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
653 (setq max (max max (length display)))
654 (push (list 'item
655 :value (cdr (assq :value (cdr elem)))
656 :display display)
657 options))))
658 (when options
659 (setq options (nreverse options))
660 ;; If we have no selected values, default to the first value.
661 (unless (plist-get menu :value)
662 (nconc menu (list :value (nth 2 (car options)))))
663 (nconc menu options)
664 (let ((selected (eww-select-display menu)))
665 (insert selected
666 (make-string (- max (length selected)) ? )))
667 (put-text-property start (point) 'eww-form menu)
668 (add-face-text-property start (point) 'eww-form-select)
669 (put-text-property start (point) 'keymap eww-select-map)
670 (shr-ensure-paragraph))))
671
672 (defun eww-select-display (select)
673 (let ((value (plist-get select :value))
674 display)
675 (dolist (elem select)
676 (when (and (consp elem)
677 (eq (car elem) 'item)
678 (equal value (plist-get (cdr elem) :value)))
679 (setq display (plist-get (cdr elem) :display))))
680 display))
681
682 (defun eww-change-select ()
683 "Change the value of the select drop-down menu under point."
684 (interactive)
685 (let* ((input (get-text-property (point) 'eww-form))
686 (properties (text-properties-at (point)))
687 (completion-ignore-case t)
688 (options
689 (delq nil
690 (mapcar (lambda (elem)
691 (and (consp elem)
692 (eq (car elem) 'item)
693 (cons (plist-get (cdr elem) :display)
694 (plist-get (cdr elem) :value))))
695 input)))
696 (display
697 (completing-read "Change value: " options nil 'require-match))
698 (inhibit-read-only t))
699 (plist-put input :value (cdr (assoc-string display options t)))
700 (goto-char
701 (eww-update-field display))))
702
703 (defun eww-update-field (string)
704 (let ((properties (text-properties-at (point)))
705 (start (eww-beginning-of-field))
706 (end (1+ (eww-end-of-field))))
707 (delete-region start end)
708 (insert string
709 (make-string (- (- end start) (length string)) ? ))
710 (set-text-properties start end properties)
711 start))
712
713 (defun eww-toggle-checkbox ()
714 "Toggle the value of the checkbox under point."
715 (interactive)
716 (let* ((input (get-text-property (point) 'eww-form))
717 (type (plist-get input :type)))
718 (if (equal type "checkbox")
719 (goto-char
720 (1+
721 (if (plist-get input :checked)
722 (progn
723 (plist-put input :checked nil)
724 (eww-update-field "[ ]"))
725 (plist-put input :checked t)
726 (eww-update-field "[X]"))))
727 ;; Radio button. Switch all other buttons off.
728 (let ((name (plist-get input :name)))
729 (save-excursion
730 (dolist (elem (eww-inputs (plist-get input :eww-form)))
731 (when (equal (plist-get (cdr elem) :name) name)
732 (goto-char (car elem))
733 (if (not (eq (cdr elem) input))
734 (progn
735 (plist-put input :checked nil)
736 (eww-update-field "[ ]"))
737 (plist-put input :checked t)
738 (eww-update-field "[X]")))))
739 (forward-char 1)))))
740
741 (defun eww-inputs (form)
742 (let ((start (point-min))
743 (inputs nil))
744 (while (and start
745 (< start (point-max)))
746 (when (or (get-text-property start 'eww-form)
747 (setq start (next-single-property-change start 'eww-form)))
748 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
749 form)
750 (push (cons start (get-text-property start 'eww-form))
751 inputs))
752 (setq start (next-single-property-change start 'eww-form))))
753 (nreverse inputs)))
754
755 (defun eww-input-value (input)
756 (let ((type (plist-get input :type))
757 (value (plist-get input :value)))
758 (cond
759 ((equal type "textarea")
760 (with-temp-buffer
761 (insert value)
762 (goto-char (point-min))
763 (while (re-search-forward "^ +\n\\| +$" nil t)
764 (replace-match "" t t))
765 (buffer-string)))
766 (t
767 (if (string-match " +\\'" value)
768 (substring value 0 (match-beginning 0))
769 value)))))
770
771 (defun eww-submit ()
772 "Submit the current form."
773 (interactive)
774 (let* ((this-input (get-text-property (point) 'eww-form))
775 (form (plist-get this-input :eww-form))
776 values next-submit)
777 (dolist (elem (sort (eww-inputs form)
778 (lambda (o1 o2)
779 (< (car o1) (car o2)))))
780 (let* ((input (cdr elem))
781 (input-start (car elem))
782 (name (plist-get input :name)))
783 (when name
784 (cond
785 ((member (plist-get input :type) '("checkbox" "radio"))
786 (when (plist-get input :checked)
787 (push (cons name (plist-get input :value))
788 values)))
789 ((equal (plist-get input :type) "submit")
790 ;; We want the values from buttons if we hit a button if
791 ;; we hit enter on it, or if it's the first button after
792 ;; the field we did hit return on.
793 (when (or (eq input this-input)
794 (and (not (eq input this-input))
795 (null next-submit)
796 (> input-start (point))))
797 (setq next-submit t)
798 (push (cons name (plist-get input :value))
799 values)))
800 (t
801 (push (cons name (eww-input-value input))
802 values))))))
803 (dolist (elem form)
804 (when (and (consp elem)
805 (eq (car elem) 'hidden))
806 (push (cons (plist-get (cdr elem) :name)
807 (plist-get (cdr elem) :value))
808 values)))
809 (if (and (stringp (cdr (assq :method form)))
810 (equal (downcase (cdr (assq :method form))) "post"))
811 (let ((url-request-method "POST")
812 (url-request-extra-headers
813 '(("Content-Type" . "application/x-www-form-urlencoded")))
814 (url-request-data (mm-url-encode-www-form-urlencoded values)))
815 (eww-browse-url (shr-expand-url (cdr (assq :action form))
816 eww-current-url)))
817 (eww-browse-url
818 (concat
819 (if (cdr (assq :action form))
820 (shr-expand-url (cdr (assq :action form))
821 eww-current-url)
822 eww-current-url)
823 "?"
824 (mm-url-encode-www-form-urlencoded values))))))
825
826 (provide 'eww)
827
828 ;;; eww.el ends here