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