Fix MS-Windows build.
[emacs.git] / lisp / net / eww.el
blob34934a03549248bd7b8d02ae1ac4cb4a404ce1ad
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 (condition-case nil
203 (decode-coding-region (point) (point-max) charset)
204 (coding-system-error nil)))
205 (let ((document
206 (list
207 'base (list (cons 'href url))
208 (libxml-parse-html-region (point) (point-max)))))
209 (eww-setup-buffer)
210 (setq eww-current-url url)
211 (eww-update-header-line-format)
212 (let ((inhibit-read-only t)
213 (after-change-functions nil)
214 (shr-width nil)
215 (shr-external-rendering-functions
216 '((title . eww-tag-title)
217 (form . eww-tag-form)
218 (input . eww-tag-input)
219 (textarea . eww-tag-textarea)
220 (body . eww-tag-body)
221 (select . eww-tag-select)
222 (link . eww-tag-link)
223 (a . eww-tag-a))))
224 (shr-insert-document document))
225 (goto-char (point-min))))
227 (defun eww-handle-link (cont)
228 (let* ((rel (assq :rel cont))
229 (href (assq :href cont))
230 (where (assoc
231 ;; The text associated with :rel is case-insensitive.
232 (if rel (downcase (cdr rel)))
233 '(("next" . eww-next-url)
234 ;; Texinfo uses "previous", but HTML specifies
235 ;; "prev", so recognize both.
236 ("previous" . eww-previous-url)
237 ("prev" . eww-previous-url)
238 ;; HTML specifies "start" but also "contents",
239 ;; and Gtk seems to use "home". Recognize
240 ;; them all; but store them in different
241 ;; variables so that we can readily choose the
242 ;; "best" one.
243 ("start" . eww-start-url)
244 ("home" . eww-home-url)
245 ("contents" . eww-contents-url)
246 ("up" . eww-up-url)))))
247 (and href
248 where
249 (set (cdr where) (cdr href)))))
251 (defun eww-tag-link (cont)
252 (eww-handle-link cont)
253 (shr-generic cont))
255 (defun eww-tag-a (cont)
256 (eww-handle-link cont)
257 (shr-tag-a cont))
259 (defun eww-update-header-line-format ()
260 (if eww-header-line-format
261 (setq header-line-format
262 (replace-regexp-in-string
263 "%" "%%"
264 (format-spec eww-header-line-format
265 `((?u . ,eww-current-url)
266 (?t . ,eww-current-title)))))
267 (setq header-line-format nil)))
269 (defun eww-tag-title (cont)
270 (setq eww-current-title "")
271 (dolist (sub cont)
272 (when (eq (car sub) 'text)
273 (setq eww-current-title (concat eww-current-title (cdr sub)))))
274 (eww-update-header-line-format))
276 (defun eww-tag-body (cont)
277 (let* ((start (point))
278 (fgcolor (cdr (or (assq :fgcolor cont)
279 (assq :text cont))))
280 (bgcolor (cdr (assq :bgcolor cont)))
281 (shr-stylesheet (list (cons 'color fgcolor)
282 (cons 'background-color bgcolor))))
283 (shr-generic cont)
284 (eww-colorize-region start (point) fgcolor bgcolor)))
286 (defun eww-colorize-region (start end fg &optional bg)
287 (when (or fg bg)
288 (let ((new-colors (shr-color-check fg bg)))
289 (when new-colors
290 (when fg
291 (add-face-text-property start end
292 (list :foreground (cadr new-colors))
294 (when bg
295 (add-face-text-property start end
296 (list :background (car new-colors))
297 t))))))
299 (defun eww-display-raw (charset)
300 (let ((data (buffer-substring (point) (point-max))))
301 (eww-setup-buffer)
302 (let ((inhibit-read-only t))
303 (insert data))
304 (goto-char (point-min))))
306 (defun eww-display-image ()
307 (let ((data (shr-parse-image-data)))
308 (eww-setup-buffer)
309 (let ((inhibit-read-only t))
310 (shr-put-image data nil))
311 (goto-char (point-min))))
313 (defun eww-setup-buffer ()
314 (switch-to-buffer (get-buffer-create "*eww*"))
315 (let ((inhibit-read-only t))
316 (remove-overlays)
317 (erase-buffer))
318 (unless (eq major-mode 'eww-mode)
319 (eww-mode)))
321 (defvar eww-mode-map
322 (let ((map (make-sparse-keymap)))
323 (suppress-keymap map)
324 (define-key map "q" 'eww-quit)
325 (define-key map "g" 'eww-reload)
326 (define-key map [tab] 'shr-next-link)
327 (define-key map [backtab] 'shr-previous-link)
328 (define-key map [delete] 'scroll-down-command)
329 (define-key map "\177" 'scroll-down-command)
330 (define-key map " " 'scroll-up-command)
331 (define-key map "l" 'eww-back-url)
332 (define-key map "f" 'eww-forward-url)
333 (define-key map "n" 'eww-next-url)
334 (define-key map "p" 'eww-previous-url)
335 (define-key map "u" 'eww-up-url)
336 (define-key map "t" 'eww-top-url)
337 (define-key map "&" 'eww-browse-with-external-browser)
338 (define-key map "d" 'eww-download)
339 (define-key map "w" 'eww-copy-page-url)
340 (define-key map "C" 'url-cookie-list)
342 (define-key map "b" 'eww-add-bookmark)
343 (define-key map "B" 'eww-list-bookmarks)
344 (define-key map [(meta n)] 'eww-next-bookmark)
345 (define-key map [(meta p)] 'eww-previous-bookmark)
347 (easy-menu-define nil map ""
348 '("eww"
349 ["Quit" eww-quit t]
350 ["Reload" eww-reload t]
351 ["Back to previous page" eww-back-url
352 :active (not (zerop (length eww-history)))]
353 ["Forward to next page" eww-forward-url
354 :active (not (zerop eww-history-position))]
355 ["Browse with external browser" eww-browse-with-external-browser t]
356 ["Download" eww-download t]
357 ["Copy page URL" eww-copy-page-url t]
358 ["Add bookmark" eww-add-bookmark t]
359 ["List bookmarks" eww-copy-page-url t]
360 ["List cookies" url-cookie-list t]))
361 map))
363 (define-derived-mode eww-mode nil "eww"
364 "Mode for browsing the web.
366 \\{eww-mode-map}"
367 (set (make-local-variable 'eww-current-url) 'author)
368 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
369 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
370 (set (make-local-variable 'eww-history) nil)
371 (set (make-local-variable 'eww-history-position) 0)
372 (buffer-disable-undo)
373 ;;(setq buffer-read-only t)
376 (defun eww-save-history ()
377 (push (list :url eww-current-url
378 :title eww-current-title
379 :point (point)
380 :text (buffer-string))
381 eww-history))
383 ;;;###autoload
384 (defun eww-browse-url (url &optional new-window)
385 (when (and (equal major-mode 'eww-mode)
386 eww-current-url)
387 (eww-save-history))
388 (eww url))
390 (defun eww-quit ()
391 "Exit the Emacs Web Wowser."
392 (interactive)
393 (setq eww-history nil)
394 (kill-buffer (current-buffer)))
396 (defun eww-back-url ()
397 "Go to the previously displayed page."
398 (interactive)
399 (when (>= eww-history-position (length eww-history))
400 (error "No previous page"))
401 (eww-save-history)
402 (setq eww-history-position (+ eww-history-position 2))
403 (eww-restore-history (elt eww-history (1- eww-history-position))))
405 (defun eww-forward-url ()
406 "Go to the next displayed page."
407 (interactive)
408 (when (zerop eww-history-position)
409 (error "No next page"))
410 (eww-save-history)
411 (eww-restore-history (elt eww-history (1- eww-history-position))))
413 (defun eww-restore-history (elem)
414 (let ((inhibit-read-only t))
415 (erase-buffer)
416 (insert (plist-get elem :text))
417 (goto-char (plist-get elem :point))
418 (setq eww-current-url (plist-get elem :url)
419 eww-current-title (plist-get elem :title))))
421 (defun eww-next-url ()
422 "Go to the page marked `next'.
423 A page is marked `next' if rel=\"next\" appears in a <link>
424 or <a> tag."
425 (interactive)
426 (if eww-next-url
427 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
428 (error "No `next' on this page")))
430 (defun eww-previous-url ()
431 "Go to the page marked `previous'.
432 A page is marked `previous' if rel=\"previous\" appears in a <link>
433 or <a> tag."
434 (interactive)
435 (if eww-previous-url
436 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
437 (error "No `previous' on this page")))
439 (defun eww-up-url ()
440 "Go to the page marked `up'.
441 A page is marked `up' if rel=\"up\" appears in a <link>
442 or <a> tag."
443 (interactive)
444 (if eww-up-url
445 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
446 (error "No `up' on this page")))
448 (defun eww-top-url ()
449 "Go to the page marked `top'.
450 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
451 appears in a <link> or <a> tag."
452 (interactive)
453 (let ((best-url (or eww-start-url
454 eww-contents-url
455 eww-home-url)))
456 (if best-url
457 (eww-browse-url (shr-expand-url best-url eww-current-url))
458 (error "No `top' for this page"))))
460 (defun eww-reload ()
461 "Reload the current page."
462 (interactive)
463 (url-retrieve eww-current-url 'eww-render
464 (list eww-current-url (point))))
466 ;; Form support.
468 (defvar eww-form nil)
470 (defvar eww-submit-map
471 (let ((map (make-sparse-keymap)))
472 (define-key map "\r" 'eww-submit)
473 (define-key map [(control c) (control c)] 'eww-submit)
474 map))
476 (defvar eww-checkbox-map
477 (let ((map (make-sparse-keymap)))
478 (define-key map [space] 'eww-toggle-checkbox)
479 (define-key map "\r" 'eww-toggle-checkbox)
480 (define-key map [(control c) (control c)] 'eww-submit)
481 map))
483 (defvar eww-text-map
484 (let ((map (make-keymap)))
485 (set-keymap-parent map text-mode-map)
486 (define-key map "\r" 'eww-submit)
487 (define-key map [(control a)] 'eww-beginning-of-text)
488 (define-key map [(control c) (control c)] 'eww-submit)
489 (define-key map [(control e)] 'eww-end-of-text)
490 (define-key map [tab] 'shr-next-link)
491 (define-key map [backtab] 'shr-previous-link)
492 map))
494 (defvar eww-textarea-map
495 (let ((map (make-keymap)))
496 (set-keymap-parent map text-mode-map)
497 (define-key map "\r" 'forward-line)
498 (define-key map [(control c) (control c)] 'eww-submit)
499 (define-key map [tab] 'shr-next-link)
500 (define-key map [backtab] 'shr-previous-link)
501 map))
503 (defvar eww-select-map
504 (let ((map (make-sparse-keymap)))
505 (define-key map "\r" 'eww-change-select)
506 (define-key map [(control c) (control c)] 'eww-submit)
507 map))
509 (defun eww-beginning-of-text ()
510 "Move to the start of the input field."
511 (interactive)
512 (goto-char (eww-beginning-of-field)))
514 (defun eww-end-of-text ()
515 "Move to the end of the text in the input field."
516 (interactive)
517 (goto-char (eww-end-of-field))
518 (let ((start (eww-beginning-of-field)))
519 (while (and (equal (following-char) ? )
520 (> (point) start))
521 (forward-char -1))
522 (when (> (point) start)
523 (forward-char 1))))
525 (defun eww-beginning-of-field ()
526 (cond
527 ((bobp)
528 (point))
529 ((not (eq (get-text-property (point) 'eww-form)
530 (get-text-property (1- (point)) 'eww-form)))
531 (point))
533 (previous-single-property-change
534 (point) 'eww-form nil (point-min)))))
536 (defun eww-end-of-field ()
537 (1- (next-single-property-change
538 (point) 'eww-form nil (point-max))))
540 (defun eww-tag-form (cont)
541 (let ((eww-form
542 (list (assq :method cont)
543 (assq :action cont)))
544 (start (point)))
545 (shr-ensure-paragraph)
546 (shr-generic cont)
547 (unless (bolp)
548 (insert "\n"))
549 (insert "\n")
550 (when (> (point) start)
551 (put-text-property start (1+ start)
552 'eww-form eww-form))))
554 (defun eww-form-submit (cont)
555 (let ((start (point))
556 (value (cdr (assq :value cont))))
557 (setq value
558 (if (zerop (length value))
559 "Submit"
560 value))
561 (insert value)
562 (add-face-text-property start (point) 'eww-form-submit)
563 (put-text-property start (point) 'eww-form
564 (list :eww-form eww-form
565 :value value
566 :type "submit"
567 :name (cdr (assq :name cont))))
568 (put-text-property start (point) 'keymap eww-submit-map)
569 (insert " ")))
571 (defun eww-form-checkbox (cont)
572 (let ((start (point)))
573 (if (cdr (assq :checked cont))
574 (insert "[X]")
575 (insert "[ ]"))
576 (add-face-text-property start (point) 'eww-form-checkbox)
577 (put-text-property start (point) 'eww-form
578 (list :eww-form eww-form
579 :value (cdr (assq :value cont))
580 :type (downcase (cdr (assq :type cont)))
581 :checked (cdr (assq :checked cont))
582 :name (cdr (assq :name cont))))
583 (put-text-property start (point) 'keymap eww-checkbox-map)
584 (insert " ")))
586 (defun eww-form-text (cont)
587 (let ((start (point))
588 (type (downcase (or (cdr (assq :type cont))
589 "text")))
590 (value (or (cdr (assq :value cont)) ""))
591 (width (string-to-number
592 (or (cdr (assq :size cont))
593 "40"))))
594 (insert value)
595 (when (< (length value) width)
596 (insert (make-string (- width (length value)) ? )))
597 (put-text-property start (point) 'face 'eww-form-text)
598 (put-text-property start (point) 'local-map eww-text-map)
599 (put-text-property start (point) 'inhibit-read-only t)
600 (put-text-property start (point) 'eww-form
601 (list :eww-form eww-form
602 :value value
603 :type type
604 :name (cdr (assq :name cont))))
605 (insert " ")))
607 (defun eww-process-text-input (beg end length)
608 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
609 (properties (text-properties-at end))
610 (type (plist-get form :type)))
611 (when (and form
612 (member type '("text" "password" "textarea")))
613 (cond
614 ((zerop length)
615 ;; Delete some space at the end.
616 (save-excursion
617 (goto-char
618 (if (equal type "textarea")
619 (1- (line-end-position))
620 (eww-end-of-field)))
621 (let ((new (- end beg)))
622 (while (and (> new 0)
623 (eql (following-char) ? ))
624 (delete-region (point) (1+ (point)))
625 (setq new (1- new))))
626 (set-text-properties beg end properties)))
627 ((> length 0)
628 ;; Add padding.
629 (save-excursion
630 (goto-char
631 (if (equal type "textarea")
632 (1- (line-end-position))
633 (eww-end-of-field)))
634 (let ((start (point)))
635 (insert (make-string length ? ))
636 (set-text-properties start (point) properties)))))
637 (let ((value (buffer-substring-no-properties
638 (eww-beginning-of-field)
639 (eww-end-of-field))))
640 (when (string-match " +\\'" value)
641 (setq value (substring value 0 (match-beginning 0))))
642 (plist-put form :value value)
643 (when (equal type "password")
644 ;; Display passwords as asterisks.
645 (let ((start (eww-beginning-of-field)))
646 (put-text-property start (+ start (length value))
647 'display (make-string (length value) ?*))))))))
649 (defun eww-tag-textarea (cont)
650 (let ((start (point))
651 (value (or (cdr (assq :value cont)) ""))
652 (lines (string-to-number
653 (or (cdr (assq :rows cont))
654 "10")))
655 (width (string-to-number
656 (or (cdr (assq :cols cont))
657 "10")))
658 end)
659 (shr-ensure-newline)
660 (insert value)
661 (shr-ensure-newline)
662 (when (< (count-lines start (point)) lines)
663 (dotimes (i (- lines (count-lines start (point))))
664 (insert "\n")))
665 (setq end (point-marker))
666 (goto-char start)
667 (while (< (point) end)
668 (end-of-line)
669 (let ((pad (- width (- (point) (line-beginning-position)))))
670 (when (> pad 0)
671 (insert (make-string pad ? ))))
672 (add-face-text-property (line-beginning-position)
673 (point) 'eww-form-text)
674 (put-text-property (line-beginning-position) (point)
675 'local-map eww-textarea-map)
676 (forward-line 1))
677 (put-text-property start (point) 'eww-form
678 (list :eww-form eww-form
679 :value value
680 :type "textarea"
681 :name (cdr (assq :name cont))))))
683 (defun eww-tag-input (cont)
684 (let ((type (downcase (or (cdr (assq :type cont))
685 "text")))
686 (start (point)))
687 (cond
688 ((or (equal type "checkbox")
689 (equal type "radio"))
690 (eww-form-checkbox cont))
691 ((equal type "submit")
692 (eww-form-submit cont))
693 ((equal type "hidden")
694 (let ((form eww-form)
695 (name (cdr (assq :name cont))))
696 ;; Don't add <input type=hidden> elements repeatedly.
697 (while (and form
698 (or (not (consp (car form)))
699 (not (eq (caar form) 'hidden))
700 (not (equal (plist-get (cdr (car form)) :name)
701 name))))
702 (setq form (cdr form)))
703 (unless form
704 (nconc eww-form (list
705 (list 'hidden
706 :name name
707 :value (cdr (assq :value cont))))))))
709 (eww-form-text cont)))
710 (unless (= start (point))
711 (put-text-property start (1+ start) 'help-echo "Input field"))))
713 (defun eww-tag-select (cont)
714 (shr-ensure-paragraph)
715 (let ((menu (list :name (cdr (assq :name cont))
716 :eww-form eww-form))
717 (options nil)
718 (start (point))
719 (max 0))
720 (dolist (elem cont)
721 (when (eq (car elem) 'option)
722 (when (cdr (assq :selected (cdr elem)))
723 (nconc menu (list :value
724 (cdr (assq :value (cdr elem))))))
725 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
726 (setq max (max max (length display)))
727 (push (list 'item
728 :value (cdr (assq :value (cdr elem)))
729 :display display)
730 options))))
731 (when options
732 (setq options (nreverse options))
733 ;; If we have no selected values, default to the first value.
734 (unless (plist-get menu :value)
735 (nconc menu (list :value (nth 2 (car options)))))
736 (nconc menu options)
737 (let ((selected (eww-select-display menu)))
738 (insert selected
739 (make-string (- max (length selected)) ? )))
740 (put-text-property start (point) 'eww-form menu)
741 (add-face-text-property start (point) 'eww-form-select)
742 (put-text-property start (point) 'keymap eww-select-map)
743 (shr-ensure-paragraph))))
745 (defun eww-select-display (select)
746 (let ((value (plist-get select :value))
747 display)
748 (dolist (elem select)
749 (when (and (consp elem)
750 (eq (car elem) 'item)
751 (equal value (plist-get (cdr elem) :value)))
752 (setq display (plist-get (cdr elem) :display))))
753 display))
755 (defun eww-change-select ()
756 "Change the value of the select drop-down menu under point."
757 (interactive)
758 (let* ((input (get-text-property (point) 'eww-form))
759 (properties (text-properties-at (point)))
760 (completion-ignore-case t)
761 (options
762 (delq nil
763 (mapcar (lambda (elem)
764 (and (consp elem)
765 (eq (car elem) 'item)
766 (cons (plist-get (cdr elem) :display)
767 (plist-get (cdr elem) :value))))
768 input)))
769 (display
770 (completing-read "Change value: " options nil 'require-match))
771 (inhibit-read-only t))
772 (plist-put input :value (cdr (assoc-string display options t)))
773 (goto-char
774 (eww-update-field display))))
776 (defun eww-update-field (string)
777 (let ((properties (text-properties-at (point)))
778 (start (eww-beginning-of-field))
779 (end (1+ (eww-end-of-field))))
780 (delete-region start end)
781 (insert string
782 (make-string (- (- end start) (length string)) ? ))
783 (set-text-properties start end properties)
784 start))
786 (defun eww-toggle-checkbox ()
787 "Toggle the value of the checkbox under point."
788 (interactive)
789 (let* ((input (get-text-property (point) 'eww-form))
790 (type (plist-get input :type)))
791 (if (equal type "checkbox")
792 (goto-char
794 (if (plist-get input :checked)
795 (progn
796 (plist-put input :checked nil)
797 (eww-update-field "[ ]"))
798 (plist-put input :checked t)
799 (eww-update-field "[X]"))))
800 ;; Radio button. Switch all other buttons off.
801 (let ((name (plist-get input :name)))
802 (save-excursion
803 (dolist (elem (eww-inputs (plist-get input :eww-form)))
804 (when (equal (plist-get (cdr elem) :name) name)
805 (goto-char (car elem))
806 (if (not (eq (cdr elem) input))
807 (progn
808 (plist-put input :checked nil)
809 (eww-update-field "[ ]"))
810 (plist-put input :checked t)
811 (eww-update-field "[X]")))))
812 (forward-char 1)))))
814 (defun eww-inputs (form)
815 (let ((start (point-min))
816 (inputs nil))
817 (while (and start
818 (< start (point-max)))
819 (when (or (get-text-property start 'eww-form)
820 (setq start (next-single-property-change start 'eww-form)))
821 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
822 form)
823 (push (cons start (get-text-property start 'eww-form))
824 inputs))
825 (setq start (next-single-property-change start 'eww-form))))
826 (nreverse inputs)))
828 (defun eww-input-value (input)
829 (let ((type (plist-get input :type))
830 (value (plist-get input :value)))
831 (cond
832 ((equal type "textarea")
833 (with-temp-buffer
834 (insert value)
835 (goto-char (point-min))
836 (while (re-search-forward "^ +\n\\| +$" nil t)
837 (replace-match "" t t))
838 (buffer-string)))
840 (if (string-match " +\\'" value)
841 (substring value 0 (match-beginning 0))
842 value)))))
844 (defun eww-submit ()
845 "Submit the current form."
846 (interactive)
847 (let* ((this-input (get-text-property (point) 'eww-form))
848 (form (plist-get this-input :eww-form))
849 values next-submit)
850 (dolist (elem (sort (eww-inputs form)
851 (lambda (o1 o2)
852 (< (car o1) (car o2)))))
853 (let* ((input (cdr elem))
854 (input-start (car elem))
855 (name (plist-get input :name)))
856 (when name
857 (cond
858 ((member (plist-get input :type) '("checkbox" "radio"))
859 (when (plist-get input :checked)
860 (push (cons name (plist-get input :value))
861 values)))
862 ((equal (plist-get input :type) "submit")
863 ;; We want the values from buttons if we hit a button if
864 ;; we hit enter on it, or if it's the first button after
865 ;; the field we did hit return on.
866 (when (or (eq input this-input)
867 (and (not (eq input this-input))
868 (null next-submit)
869 (> input-start (point))))
870 (setq next-submit t)
871 (push (cons name (plist-get input :value))
872 values)))
874 (push (cons name (eww-input-value input))
875 values))))))
876 (dolist (elem form)
877 (when (and (consp elem)
878 (eq (car elem) 'hidden))
879 (push (cons (plist-get (cdr elem) :name)
880 (plist-get (cdr elem) :value))
881 values)))
882 (if (and (stringp (cdr (assq :method form)))
883 (equal (downcase (cdr (assq :method form))) "post"))
884 (let ((url-request-method "POST")
885 (url-request-extra-headers
886 '(("Content-Type" . "application/x-www-form-urlencoded")))
887 (url-request-data (mm-url-encode-www-form-urlencoded values)))
888 (eww-browse-url (shr-expand-url (cdr (assq :action form))
889 eww-current-url)))
890 (eww-browse-url
891 (concat
892 (if (cdr (assq :action form))
893 (shr-expand-url (cdr (assq :action form))
894 eww-current-url)
895 eww-current-url)
897 (mm-url-encode-www-form-urlencoded values))))))
899 (defun eww-browse-with-external-browser ()
900 "Browse the current URL with an external browser.
901 The browser to used is specified by the `shr-external-browser' variable."
902 (interactive)
903 (funcall shr-external-browser eww-current-url))
905 (defun eww-copy-page-url ()
906 (interactive)
907 (message "%s" eww-current-url)
908 (kill-new eww-current-url))
910 (defun eww-download ()
911 "Download URL under point to `eww-download-directory'."
912 (interactive)
913 (let ((url (get-text-property (point) 'shr-url)))
914 (if (not url)
915 (message "No URL under point")
916 (url-retrieve url 'eww-download-callback (list url)))))
918 (defun eww-download-callback (status url)
919 (unless (plist-get status :error)
920 (let* ((obj (url-generic-parse-url url))
921 (path (car (url-path-and-query obj)))
922 (file (eww-make-unique-file-name (file-name-nondirectory path)
923 eww-download-path)))
924 (write-file file)
925 (message "Saved %s" file))))
927 (defun eww-make-unique-file-name (file directory)
928 (cond
929 ((zerop (length file))
930 (setq file "!"))
931 ((string-match "\\`[.]" file)
932 (setq file (concat "!" file))))
933 (let ((base file)
934 (count 1))
935 (while (file-exists-p (expand-file-name file directory))
936 (setq file
937 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
938 (format "%s(%d)%s" (match-string 1 file)
939 count (match-string 2 file))
940 (format "%s(%d)" file count)))
941 (setq count (1+ count)))
942 (expand-file-name file directory)))
944 ;;; Bookmarks code
946 (defvar eww-bookmarks nil)
948 (defun eww-add-bookmark ()
949 "Add the current page to the bookmarks."
950 (interactive)
951 (eww-read-bookmarks)
952 (dolist (bookmark eww-bookmarks)
953 (when (equal eww-current-url
954 (plist-get bookmark :url))
955 (error "Already bookmarked")))
956 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
957 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
958 (push (list :url eww-current-url
959 :title title
960 :time (current-time-string))
961 eww-bookmarks))
962 (eww-write-bookmarks)
963 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))
965 (defun eww-write-bookmarks ()
966 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory)
967 (insert ";; Auto-generated file; don't edit\n")
968 (pp eww-bookmarks (current-buffer))))
970 (defun eww-read-bookmarks ()
971 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory)))
972 (setq eww-bookmarks
973 (unless (zerop (or (nth 7 (file-attributes file)) 0))
974 (with-temp-buffer
975 (insert-file-contents file)
976 (read (current-buffer)))))))
978 (defun eww-list-bookmarks ()
979 "Display the bookmarks."
980 (interactive)
981 (eww-bookmark-prepare)
982 (pop-to-buffer "*eww bookmarks*"))
984 (defun eww-bookmark-prepare ()
985 (eww-read-bookmarks)
986 (when (null eww-bookmarks)
987 (error "No bookmarks are defined"))
988 (set-buffer (get-buffer-create "*eww bookmarks*"))
989 (eww-bookmark-mode)
990 (let ((format "%-40s %s")
991 (inhibit-read-only t)
992 start url)
993 (erase-buffer)
994 (setq header-line-format (concat " " (format format "URL" "Title")))
995 (dolist (bookmark eww-bookmarks)
996 (setq start (point))
997 (setq url (plist-get bookmark :url))
998 (when (> (length url) 40)
999 (setq url (substring url 0 40)))
1000 (insert (format format url
1001 (plist-get bookmark :title))
1002 "\n")
1003 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1004 (goto-char (point-min))))
1006 (defvar eww-bookmark-kill-ring nil)
1008 (defun eww-bookmark-kill ()
1009 "Kill the current bookmark."
1010 (interactive)
1011 (let* ((start (line-beginning-position))
1012 (bookmark (get-text-property start 'eww-bookmark))
1013 (inhibit-read-only t))
1014 (unless bookmark
1015 (error "No bookmark on the current line"))
1016 (forward-line 1)
1017 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1018 (delete-region start (point))
1019 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1020 (eww-write-bookmarks)))
1022 (defun eww-bookmark-yank ()
1023 "Yank a previously killed bookmark to the current line."
1024 (interactive)
1025 (unless eww-bookmark-kill-ring
1026 (error "No previously killed bookmark"))
1027 (beginning-of-line)
1028 (let ((inhibit-read-only t)
1029 (start (point))
1030 bookmark)
1031 (insert (pop eww-bookmark-kill-ring))
1032 (setq bookmark (get-text-property start 'eww-bookmark))
1033 (if (= start (point-min))
1034 (push bookmark eww-bookmarks)
1035 (let ((line (count-lines start (point))))
1036 (setcdr (nthcdr (1- line) eww-bookmarks)
1037 (cons bookmark (nthcdr line eww-bookmarks)))))
1038 (eww-write-bookmarks)))
1040 (defun eww-bookmark-quit ()
1041 "Kill the current buffer."
1042 (interactive)
1043 (kill-buffer (current-buffer)))
1045 (defun eww-bookmark-browse ()
1046 "Browse the bookmark under point in eww."
1047 (interactive)
1048 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1049 (unless bookmark
1050 (error "No bookmark on the current line"))
1051 ;; We wish to leave this window, but if it's the only window here,
1052 ;; just let it remain.
1053 (ignore-errors
1054 (delete-window))
1055 (eww (plist-get bookmark :url))))
1057 (defun eww-next-bookmark ()
1058 "Go to the next bookmark in the list."
1059 (interactive)
1060 (let ((first nil)
1061 bookmark)
1062 (unless (get-buffer "*eww bookmarks*")
1063 (setq first t)
1064 (eww-bookmark-prepare))
1065 (with-current-buffer (get-buffer "*eww bookmarks*")
1066 (when (and (not first)
1067 (not (eobp)))
1068 (forward-line 1))
1069 (setq bookmark (get-text-property (line-beginning-position)
1070 'eww-bookmark))
1071 (unless bookmark
1072 (error "No next bookmark")))
1073 (eww-browse-url (plist-get bookmark :url))))
1075 (defun eww-previous-bookmark ()
1076 "Go to the previous bookmark in the list."
1077 (interactive)
1078 (let ((first nil)
1079 bookmark)
1080 (unless (get-buffer "*eww bookmarks*")
1081 (setq first t)
1082 (eww-bookmark-prepare))
1083 (with-current-buffer (get-buffer "*eww bookmarks*")
1084 (if first
1085 (goto-char (point-max))
1086 (beginning-of-line))
1087 ;; On the final line.
1088 (when (eolp)
1089 (forward-line -1))
1090 (if (bobp)
1091 (error "No previous bookmark")
1092 (forward-line -1))
1093 (setq bookmark (get-text-property (line-beginning-position)
1094 'eww-bookmark)))
1095 (eww-browse-url (plist-get bookmark :url))))
1097 (defvar eww-bookmark-mode-map
1098 (let ((map (make-sparse-keymap)))
1099 (suppress-keymap map)
1100 (define-key map "q" 'eww-bookmark-quit)
1101 (define-key map [(control k)] 'eww-bookmark-kill)
1102 (define-key map [(control y)] 'eww-bookmark-yank)
1103 (define-key map "\r" 'eww-bookmark-browse)
1104 map))
1106 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1107 "Mode for listing bookmarks.
1109 \\{eww-bookmark-mode-map}"
1110 (buffer-disable-undo)
1111 (setq buffer-read-only t
1112 truncate-lines t))
1114 (provide 'eww)
1116 ;;; eww.el ends here