* filelock.c: Fix unlikely file descriptor leaks.
[emacs.git] / lisp / net / eww.el
blobd832aa7ef3e0f8ea3e0c76e2bf4b1b6d3b5d70ba
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 (define-key map "b" 'eww-add-bookmark)
341 (define-key map "B" 'eww-list-bookmarks)
342 (define-key map [(meta n)] 'eww-next-bookmark)
343 (define-key map [(meta p)] 'eww-previous-bookmark)
345 (easy-menu-define nil map ""
346 '("eww"
347 ["Quit" eww-quit t]
348 ["Reload" eww-reload t]
349 ["Back to previous page" eww-back-url
350 :active (not (zerop (length eww-history)))]
351 ["Forward to next page" eww-forward-url
352 :active (not (zerop eww-history-position))]
353 ["Browse with external browser" eww-browse-with-external-browser t]
354 ["Download" eww-download t]
355 ["Copy page URL" eww-copy-page-url t]
356 ["Add bookmark" eww-add-bookmark t]
357 ["List bookmarks" eww-copy-page-url t]
358 ["List cookies" url-cookie-list t]))
359 map))
361 (define-derived-mode eww-mode nil "eww"
362 "Mode for browsing the web.
364 \\{eww-mode-map}"
365 (set (make-local-variable 'eww-current-url) 'author)
366 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
367 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
368 (set (make-local-variable 'eww-history) nil)
369 (set (make-local-variable 'eww-history-position) 0)
370 (buffer-disable-undo)
371 ;;(setq buffer-read-only t)
374 (defun eww-save-history ()
375 (push (list :url eww-current-url
376 :title eww-current-title
377 :point (point)
378 :text (buffer-string))
379 eww-history))
381 ;;;###autoload
382 (defun eww-browse-url (url &optional new-window)
383 (when (and (equal major-mode 'eww-mode)
384 eww-current-url)
385 (eww-save-history))
386 (eww url))
388 (defun eww-quit ()
389 "Exit the Emacs Web Wowser."
390 (interactive)
391 (setq eww-history nil)
392 (kill-buffer (current-buffer)))
394 (defun eww-back-url ()
395 "Go to the previously displayed page."
396 (interactive)
397 (when (>= eww-history-position (length eww-history))
398 (error "No previous page"))
399 (eww-save-history)
400 (setq eww-history-position (+ eww-history-position 2))
401 (eww-restore-history (elt eww-history (1- eww-history-position))))
403 (defun eww-forward-url ()
404 "Go to the next displayed page."
405 (interactive)
406 (when (zerop eww-history-position)
407 (error "No next page"))
408 (eww-save-history)
409 (eww-restore-history (elt eww-history (1- eww-history-position))))
411 (defun eww-restore-history (elem)
412 (let ((inhibit-read-only t))
413 (erase-buffer)
414 (insert (plist-get elem :text))
415 (goto-char (plist-get elem :point))
416 (setq eww-current-url (plist-get elem :url)
417 eww-current-title (plist-get elem :title))))
419 (defun eww-next-url ()
420 "Go to the page marked `next'.
421 A page is marked `next' if rel=\"next\" appears in a <link>
422 or <a> tag."
423 (interactive)
424 (if eww-next-url
425 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
426 (error "No `next' on this page")))
428 (defun eww-previous-url ()
429 "Go to the page marked `previous'.
430 A page is marked `previous' if rel=\"previous\" appears in a <link>
431 or <a> tag."
432 (interactive)
433 (if eww-previous-url
434 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
435 (error "No `previous' on this page")))
437 (defun eww-up-url ()
438 "Go to the page marked `up'.
439 A page is marked `up' if rel=\"up\" appears in a <link>
440 or <a> tag."
441 (interactive)
442 (if eww-up-url
443 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
444 (error "No `up' on this page")))
446 (defun eww-top-url ()
447 "Go to the page marked `top'.
448 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
449 appears in a <link> or <a> tag."
450 (interactive)
451 (let ((best-url (or eww-start-url
452 eww-contents-url
453 eww-home-url)))
454 (if best-url
455 (eww-browse-url (shr-expand-url best-url eww-current-url))
456 (error "No `top' for this page"))))
458 (defun eww-reload ()
459 "Reload the current page."
460 (interactive)
461 (url-retrieve eww-current-url 'eww-render
462 (list eww-current-url (point))))
464 ;; Form support.
466 (defvar eww-form nil)
468 (defvar eww-submit-map
469 (let ((map (make-sparse-keymap)))
470 (define-key map "\r" 'eww-submit)
471 (define-key map [(control c) (control c)] 'eww-submit)
472 map))
474 (defvar eww-checkbox-map
475 (let ((map (make-sparse-keymap)))
476 (define-key map [space] 'eww-toggle-checkbox)
477 (define-key map "\r" 'eww-toggle-checkbox)
478 (define-key map [(control c) (control c)] 'eww-submit)
479 map))
481 (defvar eww-text-map
482 (let ((map (make-keymap)))
483 (set-keymap-parent map text-mode-map)
484 (define-key map "\r" 'eww-submit)
485 (define-key map [(control a)] 'eww-beginning-of-text)
486 (define-key map [(control c) (control c)] 'eww-submit)
487 (define-key map [(control e)] 'eww-end-of-text)
488 (define-key map [tab] 'shr-next-link)
489 (define-key map [backtab] 'shr-previous-link)
490 map))
492 (defvar eww-textarea-map
493 (let ((map (make-keymap)))
494 (set-keymap-parent map text-mode-map)
495 (define-key map "\r" 'forward-line)
496 (define-key map [(control c) (control c)] 'eww-submit)
497 (define-key map [tab] 'shr-next-link)
498 (define-key map [backtab] 'shr-previous-link)
499 map))
501 (defvar eww-select-map
502 (let ((map (make-sparse-keymap)))
503 (define-key map "\r" 'eww-change-select)
504 (define-key map [(control c) (control c)] 'eww-submit)
505 map))
507 (defun eww-beginning-of-text ()
508 "Move to the start of the input field."
509 (interactive)
510 (goto-char (eww-beginning-of-field)))
512 (defun eww-end-of-text ()
513 "Move to the end of the text in the input field."
514 (interactive)
515 (goto-char (eww-end-of-field))
516 (let ((start (eww-beginning-of-field)))
517 (while (and (equal (following-char) ? )
518 (> (point) start))
519 (forward-char -1))
520 (when (> (point) start)
521 (forward-char 1))))
523 (defun eww-beginning-of-field ()
524 (cond
525 ((bobp)
526 (point))
527 ((not (eq (get-text-property (point) 'eww-form)
528 (get-text-property (1- (point)) 'eww-form)))
529 (point))
531 (previous-single-property-change
532 (point) 'eww-form nil (point-min)))))
534 (defun eww-end-of-field ()
535 (1- (next-single-property-change
536 (point) 'eww-form nil (point-max))))
538 (defun eww-tag-form (cont)
539 (let ((eww-form
540 (list (assq :method cont)
541 (assq :action cont)))
542 (start (point)))
543 (shr-ensure-paragraph)
544 (shr-generic cont)
545 (unless (bolp)
546 (insert "\n"))
547 (insert "\n")
548 (when (> (point) start)
549 (put-text-property start (1+ start)
550 'eww-form eww-form))))
552 (defun eww-form-submit (cont)
553 (let ((start (point))
554 (value (cdr (assq :value cont))))
555 (setq value
556 (if (zerop (length value))
557 "Submit"
558 value))
559 (insert value)
560 (add-face-text-property start (point) 'eww-form-submit)
561 (put-text-property start (point) 'eww-form
562 (list :eww-form eww-form
563 :value value
564 :type "submit"
565 :name (cdr (assq :name cont))))
566 (put-text-property start (point) 'keymap eww-submit-map)
567 (insert " ")))
569 (defun eww-form-checkbox (cont)
570 (let ((start (point)))
571 (if (cdr (assq :checked cont))
572 (insert "[X]")
573 (insert "[ ]"))
574 (add-face-text-property start (point) 'eww-form-checkbox)
575 (put-text-property start (point) 'eww-form
576 (list :eww-form eww-form
577 :value (cdr (assq :value cont))
578 :type (downcase (cdr (assq :type cont)))
579 :checked (cdr (assq :checked cont))
580 :name (cdr (assq :name cont))))
581 (put-text-property start (point) 'keymap eww-checkbox-map)
582 (insert " ")))
584 (defun eww-form-text (cont)
585 (let ((start (point))
586 (type (downcase (or (cdr (assq :type cont))
587 "text")))
588 (value (or (cdr (assq :value cont)) ""))
589 (width (string-to-number
590 (or (cdr (assq :size cont))
591 "40"))))
592 (insert value)
593 (when (< (length value) width)
594 (insert (make-string (- width (length value)) ? )))
595 (put-text-property start (point) 'face 'eww-form-text)
596 (put-text-property start (point) 'local-map eww-text-map)
597 (put-text-property start (point) 'inhibit-read-only t)
598 (put-text-property start (point) 'eww-form
599 (list :eww-form eww-form
600 :value value
601 :type type
602 :name (cdr (assq :name cont))))
603 (insert " ")))
605 (defun eww-process-text-input (beg end length)
606 (let* ((form (get-text-property end 'eww-form))
607 (properties (text-properties-at end))
608 (type (plist-get form :type)))
609 (when (and form
610 (member type '("text" "password" "textarea")))
611 (cond
612 ((zerop length)
613 ;; Delete some space at the end.
614 (save-excursion
615 (goto-char
616 (if (equal type "textarea")
617 (1- (line-end-position))
618 (eww-end-of-field)))
619 (let ((new (- end beg)))
620 (while (and (> new 0)
621 (eql (following-char) ? ))
622 (delete-region (point) (1+ (point)))
623 (setq new (1- new))))
624 (set-text-properties beg end properties)))
625 ((> length 0)
626 ;; Add padding.
627 (save-excursion
628 (goto-char
629 (if (equal type "textarea")
630 (1- (line-end-position))
631 (eww-end-of-field)))
632 (let ((start (point)))
633 (insert (make-string length ? ))
634 (set-text-properties start (point) properties)))))
635 (let ((value (buffer-substring-no-properties
636 (eww-beginning-of-field)
637 (eww-end-of-field))))
638 (when (string-match " +\\'" value)
639 (setq value (substring value 0 (match-beginning 0))))
640 (plist-put form :value value)
641 (when (equal type "password")
642 ;; Display passwords as asterisks.
643 (let ((start (eww-beginning-of-field)))
644 (put-text-property start (+ start (length value))
645 'display (make-string (length value) ?*))))))))
647 (defun eww-tag-textarea (cont)
648 (let ((start (point))
649 (value (or (cdr (assq :value cont)) ""))
650 (lines (string-to-number
651 (or (cdr (assq :rows cont))
652 "10")))
653 (width (string-to-number
654 (or (cdr (assq :cols cont))
655 "10")))
656 end)
657 (shr-ensure-newline)
658 (insert value)
659 (shr-ensure-newline)
660 (when (< (count-lines start (point)) lines)
661 (dotimes (i (- lines (count-lines start (point))))
662 (insert "\n")))
663 (setq end (point-marker))
664 (goto-char start)
665 (while (< (point) end)
666 (end-of-line)
667 (let ((pad (- width (- (point) (line-beginning-position)))))
668 (when (> pad 0)
669 (insert (make-string pad ? ))))
670 (add-face-text-property (line-beginning-position)
671 (point) 'eww-form-text)
672 (put-text-property (line-beginning-position) (point)
673 'local-map eww-textarea-map)
674 (forward-line 1))
675 (put-text-property start (point) 'eww-form
676 (list :eww-form eww-form
677 :value value
678 :type "textarea"
679 :name (cdr (assq :name cont))))))
681 (defun eww-tag-input (cont)
682 (let ((type (downcase (or (cdr (assq :type cont))
683 "text")))
684 (start (point)))
685 (cond
686 ((or (equal type "checkbox")
687 (equal type "radio"))
688 (eww-form-checkbox cont))
689 ((equal type "submit")
690 (eww-form-submit cont))
691 ((equal type "hidden")
692 (let ((form eww-form)
693 (name (cdr (assq :name cont))))
694 ;; Don't add <input type=hidden> elements repeatedly.
695 (while (and form
696 (or (not (consp (car form)))
697 (not (eq (caar form) 'hidden))
698 (not (equal (plist-get (cdr (car form)) :name)
699 name))))
700 (setq form (cdr form)))
701 (unless form
702 (nconc eww-form (list
703 (list 'hidden
704 :name name
705 :value (cdr (assq :value cont))))))))
707 (eww-form-text cont)))
708 (unless (= start (point))
709 (put-text-property start (1+ start) 'help-echo "Input field"))))
711 (defun eww-tag-select (cont)
712 (shr-ensure-paragraph)
713 (let ((menu (list :name (cdr (assq :name cont))
714 :eww-form eww-form))
715 (options nil)
716 (start (point))
717 (max 0))
718 (dolist (elem cont)
719 (when (eq (car elem) 'option)
720 (when (cdr (assq :selected (cdr elem)))
721 (nconc menu (list :value
722 (cdr (assq :value (cdr elem))))))
723 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
724 (setq max (max max (length display)))
725 (push (list 'item
726 :value (cdr (assq :value (cdr elem)))
727 :display display)
728 options))))
729 (when options
730 (setq options (nreverse options))
731 ;; If we have no selected values, default to the first value.
732 (unless (plist-get menu :value)
733 (nconc menu (list :value (nth 2 (car options)))))
734 (nconc menu options)
735 (let ((selected (eww-select-display menu)))
736 (insert selected
737 (make-string (- max (length selected)) ? )))
738 (put-text-property start (point) 'eww-form menu)
739 (add-face-text-property start (point) 'eww-form-select)
740 (put-text-property start (point) 'keymap eww-select-map)
741 (shr-ensure-paragraph))))
743 (defun eww-select-display (select)
744 (let ((value (plist-get select :value))
745 display)
746 (dolist (elem select)
747 (when (and (consp elem)
748 (eq (car elem) 'item)
749 (equal value (plist-get (cdr elem) :value)))
750 (setq display (plist-get (cdr elem) :display))))
751 display))
753 (defun eww-change-select ()
754 "Change the value of the select drop-down menu under point."
755 (interactive)
756 (let* ((input (get-text-property (point) 'eww-form))
757 (properties (text-properties-at (point)))
758 (completion-ignore-case t)
759 (options
760 (delq nil
761 (mapcar (lambda (elem)
762 (and (consp elem)
763 (eq (car elem) 'item)
764 (cons (plist-get (cdr elem) :display)
765 (plist-get (cdr elem) :value))))
766 input)))
767 (display
768 (completing-read "Change value: " options nil 'require-match))
769 (inhibit-read-only t))
770 (plist-put input :value (cdr (assoc-string display options t)))
771 (goto-char
772 (eww-update-field display))))
774 (defun eww-update-field (string)
775 (let ((properties (text-properties-at (point)))
776 (start (eww-beginning-of-field))
777 (end (1+ (eww-end-of-field))))
778 (delete-region start end)
779 (insert string
780 (make-string (- (- end start) (length string)) ? ))
781 (set-text-properties start end properties)
782 start))
784 (defun eww-toggle-checkbox ()
785 "Toggle the value of the checkbox under point."
786 (interactive)
787 (let* ((input (get-text-property (point) 'eww-form))
788 (type (plist-get input :type)))
789 (if (equal type "checkbox")
790 (goto-char
792 (if (plist-get input :checked)
793 (progn
794 (plist-put input :checked nil)
795 (eww-update-field "[ ]"))
796 (plist-put input :checked t)
797 (eww-update-field "[X]"))))
798 ;; Radio button. Switch all other buttons off.
799 (let ((name (plist-get input :name)))
800 (save-excursion
801 (dolist (elem (eww-inputs (plist-get input :eww-form)))
802 (when (equal (plist-get (cdr elem) :name) name)
803 (goto-char (car elem))
804 (if (not (eq (cdr elem) input))
805 (progn
806 (plist-put input :checked nil)
807 (eww-update-field "[ ]"))
808 (plist-put input :checked t)
809 (eww-update-field "[X]")))))
810 (forward-char 1)))))
812 (defun eww-inputs (form)
813 (let ((start (point-min))
814 (inputs nil))
815 (while (and start
816 (< start (point-max)))
817 (when (or (get-text-property start 'eww-form)
818 (setq start (next-single-property-change start 'eww-form)))
819 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
820 form)
821 (push (cons start (get-text-property start 'eww-form))
822 inputs))
823 (setq start (next-single-property-change start 'eww-form))))
824 (nreverse inputs)))
826 (defun eww-input-value (input)
827 (let ((type (plist-get input :type))
828 (value (plist-get input :value)))
829 (cond
830 ((equal type "textarea")
831 (with-temp-buffer
832 (insert value)
833 (goto-char (point-min))
834 (while (re-search-forward "^ +\n\\| +$" nil t)
835 (replace-match "" t t))
836 (buffer-string)))
838 (if (string-match " +\\'" value)
839 (substring value 0 (match-beginning 0))
840 value)))))
842 (defun eww-submit ()
843 "Submit the current form."
844 (interactive)
845 (let* ((this-input (get-text-property (point) 'eww-form))
846 (form (plist-get this-input :eww-form))
847 values next-submit)
848 (dolist (elem (sort (eww-inputs form)
849 (lambda (o1 o2)
850 (< (car o1) (car o2)))))
851 (let* ((input (cdr elem))
852 (input-start (car elem))
853 (name (plist-get input :name)))
854 (when name
855 (cond
856 ((member (plist-get input :type) '("checkbox" "radio"))
857 (when (plist-get input :checked)
858 (push (cons name (plist-get input :value))
859 values)))
860 ((equal (plist-get input :type) "submit")
861 ;; We want the values from buttons if we hit a button if
862 ;; we hit enter on it, or if it's the first button after
863 ;; the field we did hit return on.
864 (when (or (eq input this-input)
865 (and (not (eq input this-input))
866 (null next-submit)
867 (> input-start (point))))
868 (setq next-submit t)
869 (push (cons name (plist-get input :value))
870 values)))
872 (push (cons name (eww-input-value input))
873 values))))))
874 (dolist (elem form)
875 (when (and (consp elem)
876 (eq (car elem) 'hidden))
877 (push (cons (plist-get (cdr elem) :name)
878 (plist-get (cdr elem) :value))
879 values)))
880 (if (and (stringp (cdr (assq :method form)))
881 (equal (downcase (cdr (assq :method form))) "post"))
882 (let ((url-request-method "POST")
883 (url-request-extra-headers
884 '(("Content-Type" . "application/x-www-form-urlencoded")))
885 (url-request-data (mm-url-encode-www-form-urlencoded values)))
886 (eww-browse-url (shr-expand-url (cdr (assq :action form))
887 eww-current-url)))
888 (eww-browse-url
889 (concat
890 (if (cdr (assq :action form))
891 (shr-expand-url (cdr (assq :action form))
892 eww-current-url)
893 eww-current-url)
895 (mm-url-encode-www-form-urlencoded values))))))
897 (defun eww-browse-with-external-browser ()
898 "Browse the current URL with an external browser.
899 The browser to used is specified by the `shr-external-browser' variable."
900 (interactive)
901 (funcall shr-external-browser eww-current-url))
903 (defun eww-copy-page-url ()
904 (interactive)
905 (message "%s" eww-current-url)
906 (kill-new eww-current-url))
908 (defun eww-download ()
909 "Download URL under point to `eww-download-directory'."
910 (interactive)
911 (let ((url (get-text-property (point) 'shr-url)))
912 (if (not url)
913 (message "No URL under point")
914 (url-retrieve url 'eww-download-callback (list url)))))
916 (defun eww-download-callback (status url)
917 (unless (plist-get status :error)
918 (let* ((obj (url-generic-parse-url url))
919 (path (car (url-path-and-query obj)))
920 (file (eww-make-unique-file-name (file-name-nondirectory path)
921 eww-download-path)))
922 (write-file file)
923 (message "Saved %s" file))))
925 (defun eww-make-unique-file-name (file directory)
926 (cond
927 ((zerop (length file))
928 (setq file "!"))
929 ((string-match "\\`[.]" file)
930 (setq file (concat "!" file))))
931 (let ((base file)
932 (count 1))
933 (while (file-exists-p (expand-file-name file directory))
934 (setq file
935 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
936 (format "%s(%d)%s" (match-string 1 file)
937 count (match-string 2 file))
938 (format "%s(%d)" file count)))
939 (setq count (1+ count)))
940 (expand-file-name file directory)))
942 ;;; Bookmarks code
944 (defvar eww-bookmarks nil)
946 (defun eww-add-bookmark ()
947 "Add the current page to the bookmarks."
948 (interactive)
949 (eww-read-bookmarks)
950 (dolist (bookmark eww-bookmarks)
951 (when (equal eww-current-url
952 (plist-get bookmark :url))
953 (error "Already bookmarked")))
954 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
955 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
956 (push (list :url eww-current-url
957 :title title
958 :time (current-time-string))
959 eww-bookmarks))
960 (eww-write-bookmarks)
961 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))
963 (defun eww-write-bookmarks ()
964 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory)
965 (insert ";; Auto-generated file; don't edit\n")
966 (pp eww-bookmarks (current-buffer))))
968 (defun eww-read-bookmarks ()
969 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory)))
970 (setq eww-bookmarks
971 (unless (zerop (or (nth 7 (file-attributes file)) 0))
972 (with-temp-buffer
973 (insert-file-contents file)
974 (read (current-buffer)))))))
976 (defun eww-list-bookmarks ()
977 "Display the bookmarks."
978 (interactive)
979 (eww-bookmark-prepare)
980 (pop-to-buffer "*eww bookmarks*"))
982 (defun eww-bookmark-prepare ()
983 (eww-read-bookmarks)
984 (when (null eww-bookmarks)
985 (error "No bookmarks are defined"))
986 (set-buffer (get-buffer-create "*eww bookmarks*"))
987 (eww-bookmark-mode)
988 (let ((format "%-40s %s")
989 (inhibit-read-only t)
990 start url)
991 (erase-buffer)
992 (setq header-line-format (concat " " (format format "URL" "Title")))
993 (dolist (bookmark eww-bookmarks)
994 (setq start (point))
995 (setq url (plist-get bookmark :url))
996 (when (> (length url) 40)
997 (setq url (substring url 0 40)))
998 (insert (format format url
999 (plist-get bookmark :title))
1000 "\n")
1001 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1002 (goto-char (point-min))))
1004 (defvar eww-bookmark-kill-ring nil)
1006 (defun eww-bookmark-kill ()
1007 "Kill the current bookmark."
1008 (interactive)
1009 (let* ((start (line-beginning-position))
1010 (bookmark (get-text-property start 'eww-bookmark))
1011 (inhibit-read-only t))
1012 (unless bookmark
1013 (error "No bookmark on the current line"))
1014 (forward-line 1)
1015 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1016 (delete-region start (point))
1017 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1018 (eww-write-bookmarks)))
1020 (defun eww-bookmark-yank ()
1021 "Yank a previously killed bookmark to the current line."
1022 (interactive)
1023 (unless eww-bookmark-kill-ring
1024 (error "No previously killed bookmark"))
1025 (beginning-of-line)
1026 (let ((inhibit-read-only t)
1027 (start (point))
1028 bookmark)
1029 (insert (pop eww-bookmark-kill-ring))
1030 (setq bookmark (get-text-property start 'eww-bookmark))
1031 (if (= start (point-min))
1032 (push bookmark eww-bookmarks)
1033 (let ((line (count-lines start (point))))
1034 (setcdr (nthcdr (1- line) eww-bookmarks)
1035 (cons bookmark (nthcdr line eww-bookmarks)))))
1036 (eww-write-bookmarks)))
1038 (defun eww-bookmark-quit ()
1039 "Kill the current buffer."
1040 (interactive)
1041 (kill-buffer (current-buffer)))
1043 (defun eww-bookmark-browse ()
1044 "Browse the bookmark under point in eww."
1045 (interactive)
1046 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1047 (unless bookmark
1048 (error "No bookmark on the current line"))
1049 ;; We wish to leave this window, but if it's the only window here,
1050 ;; just let it remain.
1051 (ignore-errors
1052 (delete-window))
1053 (eww (plist-get bookmark :url))))
1055 (defun eww-next-bookmark ()
1056 "Go to the next bookmark in the list."
1057 (interactive)
1058 (let ((first nil)
1059 bookmark)
1060 (unless (get-buffer "*eww bookmarks*")
1061 (setq first t)
1062 (eww-bookmark-prepare))
1063 (with-current-buffer (get-buffer "*eww bookmarks*")
1064 (when (and (not first)
1065 (not (eobp)))
1066 (forward-line 1))
1067 (setq bookmark (get-text-property (line-beginning-position)
1068 'eww-bookmark))
1069 (unless bookmark
1070 (error "No next bookmark")))
1071 (eww-browse-url (plist-get bookmark :url))))
1073 (defun eww-previous-bookmark ()
1074 "Go to the previous bookmark in the list."
1075 (interactive)
1076 (let ((first nil)
1077 bookmark)
1078 (unless (get-buffer "*eww bookmarks*")
1079 (setq first t)
1080 (eww-bookmark-prepare))
1081 (with-current-buffer (get-buffer "*eww bookmarks*")
1082 (if first
1083 (goto-char (point-max))
1084 (beginning-of-line))
1085 ;; On the final line.
1086 (when (eolp)
1087 (forward-line -1))
1088 (if (bobp)
1089 (error "No previous bookmark")
1090 (forward-line -1))
1091 (setq bookmark (get-text-property (line-beginning-position)
1092 'eww-bookmark)))
1093 (eww-browse-url (plist-get bookmark :url))))
1095 (defvar eww-bookmark-mode-map
1096 (let ((map (make-sparse-keymap)))
1097 (suppress-keymap map)
1098 (define-key map "q" 'eww-bookmark-quit)
1099 (define-key map [(control k)] 'eww-bookmark-kill)
1100 (define-key map [(control y)] 'eww-bookmark-yank)
1101 (define-key map "\r" 'eww-bookmark-browse)
1102 map))
1104 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1105 "Mode for listing bookmarks.
1107 \\{eww-bookmark-mode-map}"
1108 (buffer-disable-undo)
1109 (setq buffer-read-only t
1110 truncate-lines t))
1112 (provide 'eww)
1114 ;;; eww.el ends here