eww: Revert 2013-12-11T19:01:44Z!tzz@lifelogs.com UI wrappers (eww-exit, eww-close)
[emacs.git] / lisp / net / eww.el
blob2263f81ea7d4fd6072601aeb37c137450d0ba900
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 (defcustom eww-use-external-browser-for-content-type
60 "\\`\\(video/\\|audio/\\|application/ogg\\)"
61 "Always use external browser for specified content-type."
62 :version "24.4"
63 :group 'eww
64 :type '(choice (const :tag "Never" nil)
65 regexp))
67 (defcustom eww-form-checkbox-selected-symbol "[X]"
68 "Symbol used to represent a selected checkbox.
69 See also `eww-form-checkbox-symbol'."
70 :version "24.4"
71 :group 'eww
72 :type '(choice (const "[X]")
73 (const "☒") ; Unicode BALLOT BOX WITH X
74 (const "☑") ; Unicode BALLOT BOX WITH CHECK
75 string))
77 (defcustom eww-form-checkbox-symbol "[ ]"
78 "Symbol used to represent a checkbox.
79 See also `eww-form-checkbox-selected-symbol'."
80 :version "24.4"
81 :group 'eww
82 :type '(choice (const "[ ]")
83 (const "☐") ; Unicode BALLOT BOX
84 string))
86 (defface eww-form-submit
87 '((((type x w32 ns) (class color)) ; Like default mode line
88 :box (:line-width 2 :style released-button)
89 :background "#808080" :foreground "black"))
90 "Face for eww buffer buttons."
91 :version "24.4"
92 :group 'eww)
94 (defface eww-form-checkbox
95 '((((type x w32 ns) (class color)) ; Like default mode line
96 :box (:line-width 2 :style released-button)
97 :background "lightgrey" :foreground "black"))
98 "Face for eww buffer buttons."
99 :version "24.4"
100 :group 'eww)
102 (defface eww-form-select
103 '((((type x w32 ns) (class color)) ; Like default mode line
104 :box (:line-width 2 :style released-button)
105 :background "lightgrey" :foreground "black"))
106 "Face for eww buffer buttons."
107 :version "24.4"
108 :group 'eww)
110 (defface eww-form-text
111 '((t (:background "#505050"
112 :foreground "white"
113 :box (:line-width 1))))
114 "Face for eww text inputs."
115 :version "24.4"
116 :group 'eww)
118 (defvar eww-current-url nil)
119 (defvar eww-current-dom nil)
120 (defvar eww-current-source nil)
121 (defvar eww-current-title ""
122 "Title of current page.")
123 (defvar eww-history nil)
124 (defvar eww-history-position 0)
126 (defvar eww-next-url nil)
127 (defvar eww-previous-url nil)
128 (defvar eww-up-url nil)
129 (defvar eww-home-url nil)
130 (defvar eww-start-url nil)
131 (defvar eww-contents-url nil)
133 (defvar eww-local-regex "localhost"
134 "When this regex is found in the URL, it's not a keyword but an address.")
136 (defvar eww-link-keymap
137 (let ((map (copy-keymap shr-map)))
138 (define-key map "\r" 'eww-follow-link)
139 map))
141 ;;;###autoload
142 (defun eww (url)
143 "Fetch URL and render the page.
144 If the input doesn't look like an URL or a domain name, the
145 word(s) will be searched for via `eww-search-prefix'."
146 (interactive "sEnter URL or keywords: ")
147 (cond ((string-match-p "\\`file:" url))
149 (if (and (= (length (split-string url)) 1)
150 (or (> (length (split-string url "\\.")) 1)
151 (string-match eww-local-regex url)))
152 (progn
153 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
154 (setq url (concat "http://" url)))
155 ;; some site don't redirect final /
156 (when (string= (url-filename (url-generic-parse-url url)) "")
157 (setq url (concat url "/"))))
158 (setq url (concat eww-search-prefix
159 (replace-regexp-in-string " " "+" url))))))
160 (url-retrieve url 'eww-render (list url)))
162 ;;;###autoload (defalias 'browse-web 'eww)
164 ;;;###autoload
165 (defun eww-open-file (file)
166 "Render a file using EWW."
167 (interactive "fFile: ")
168 (eww (concat "file://" (expand-file-name file))))
170 (defun eww-render (status url &optional point)
171 (let ((redirect (plist-get status :redirect)))
172 (when redirect
173 (setq url redirect)))
174 (set (make-local-variable 'eww-next-url) nil)
175 (set (make-local-variable 'eww-previous-url) nil)
176 (set (make-local-variable 'eww-up-url) nil)
177 (set (make-local-variable 'eww-home-url) nil)
178 (set (make-local-variable 'eww-start-url) nil)
179 (set (make-local-variable 'eww-contents-url) nil)
180 (let* ((headers (eww-parse-headers))
181 (content-type
182 (mail-header-parse-content-type
183 (or (cdr (assoc "content-type" headers))
184 "text/plain")))
185 (charset (intern
186 (downcase
187 (or (cdr (assq 'charset (cdr content-type)))
188 (eww-detect-charset (equal (car content-type)
189 "text/html"))
190 "utf8"))))
191 (data-buffer (current-buffer)))
192 (unwind-protect
193 (progn
194 (setq eww-current-title "")
195 (cond
196 ((and eww-use-external-browser-for-content-type
197 (string-match-p eww-use-external-browser-for-content-type
198 (car content-type)))
199 (eww-browse-with-external-browser url))
200 ((equal (car content-type) "text/html")
201 (eww-display-html charset url nil point))
202 ((string-match-p "\\`image/" (car content-type))
203 (eww-display-image)
204 (eww-update-header-line-format))
206 (eww-display-raw)
207 (eww-update-header-line-format)))
208 (setq eww-current-url url
209 eww-history-position 0))
210 (kill-buffer data-buffer))))
212 (defun eww-parse-headers ()
213 (let ((headers nil))
214 (goto-char (point-min))
215 (while (and (not (eobp))
216 (not (eolp)))
217 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
218 (push (cons (downcase (match-string 1))
219 (match-string 2))
220 headers))
221 (forward-line 1))
222 (unless (eobp)
223 (forward-line 1))
224 headers))
226 (defun eww-detect-charset (html-p)
227 (let ((case-fold-search t)
228 (pt (point)))
229 (or (and html-p
230 (re-search-forward
231 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
232 (goto-char pt)
233 (match-string 1))
234 (and (looking-at
235 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
236 (match-string 1)))))
238 (declare-function libxml-parse-html-region "xml.c"
239 (start end &optional base-url))
241 (defun eww-display-html (charset url &optional document point)
242 (or (fboundp 'libxml-parse-html-region)
243 (error "This function requires Emacs to be compiled with libxml2"))
244 (unless (eq charset 'utf8)
245 (condition-case nil
246 (decode-coding-region (point) (point-max) charset)
247 (coding-system-error nil)))
248 (let ((document
249 (or document
250 (list
251 'base (list (cons 'href url))
252 (libxml-parse-html-region (point) (point-max))))))
253 (setq eww-current-source (buffer-substring (point) (point-max)))
254 (eww-setup-buffer)
255 (setq eww-current-dom document)
256 (let ((inhibit-read-only t)
257 (after-change-functions nil)
258 (shr-width nil)
259 (shr-target-id (url-target (url-generic-parse-url url)))
260 (shr-external-rendering-functions
261 '((title . eww-tag-title)
262 (form . eww-tag-form)
263 (input . eww-tag-input)
264 (textarea . eww-tag-textarea)
265 (body . eww-tag-body)
266 (select . eww-tag-select)
267 (link . eww-tag-link)
268 (a . eww-tag-a))))
269 (shr-insert-document document)
270 (cond
271 (point
272 (goto-char point))
273 (shr-target-id
274 (goto-char (point-min))
275 (let ((point (next-single-property-change
276 (point-min) 'shr-target-id)))
277 (when point
278 (goto-char point))))
280 (goto-char (point-min)))))
281 (setq eww-current-url url
282 eww-history-position 0)
283 (eww-update-header-line-format)))
285 (defun eww-handle-link (cont)
286 (let* ((rel (assq :rel cont))
287 (href (assq :href cont))
288 (where (assoc
289 ;; The text associated with :rel is case-insensitive.
290 (if rel (downcase (cdr rel)))
291 '(("next" . eww-next-url)
292 ;; Texinfo uses "previous", but HTML specifies
293 ;; "prev", so recognize both.
294 ("previous" . eww-previous-url)
295 ("prev" . eww-previous-url)
296 ;; HTML specifies "start" but also "contents",
297 ;; and Gtk seems to use "home". Recognize
298 ;; them all; but store them in different
299 ;; variables so that we can readily choose the
300 ;; "best" one.
301 ("start" . eww-start-url)
302 ("home" . eww-home-url)
303 ("contents" . eww-contents-url)
304 ("up" . eww-up-url)))))
305 (and href
306 where
307 (set (cdr where) (cdr href)))))
309 (defun eww-tag-link (cont)
310 (eww-handle-link cont)
311 (shr-generic cont))
313 (defun eww-tag-a (cont)
314 (eww-handle-link cont)
315 (let ((start (point)))
316 (shr-tag-a cont)
317 (put-text-property start (point) 'keymap eww-link-keymap)))
319 (defun eww-update-header-line-format ()
320 (if eww-header-line-format
321 (setq header-line-format
322 (replace-regexp-in-string
323 "%" "%%"
324 ;; FIXME? Title can be blank. Default to, eg, last component
325 ;; of url?
326 (format-spec eww-header-line-format
327 `((?u . ,eww-current-url)
328 (?t . ,eww-current-title)))))
329 (setq header-line-format nil)))
331 (defun eww-tag-title (cont)
332 (setq eww-current-title "")
333 (dolist (sub cont)
334 (when (eq (car sub) 'text)
335 (setq eww-current-title (concat eww-current-title (cdr sub)))))
336 (eww-update-header-line-format))
338 (defun eww-tag-body (cont)
339 (let* ((start (point))
340 (fgcolor (cdr (or (assq :fgcolor cont)
341 (assq :text cont))))
342 (bgcolor (cdr (assq :bgcolor cont)))
343 (shr-stylesheet (list (cons 'color fgcolor)
344 (cons 'background-color bgcolor))))
345 (shr-generic cont)
346 (eww-colorize-region start (point) fgcolor bgcolor)))
348 (defun eww-colorize-region (start end fg &optional bg)
349 (when (or fg bg)
350 (let ((new-colors (shr-color-check fg bg)))
351 (when new-colors
352 (when fg
353 (add-face-text-property start end
354 (list :foreground (cadr new-colors))
356 (when bg
357 (add-face-text-property start end
358 (list :background (car new-colors))
359 t))))))
361 (defun eww-display-raw ()
362 (let ((data (buffer-substring (point) (point-max))))
363 (eww-setup-buffer)
364 (let ((inhibit-read-only t))
365 (insert data))
366 (goto-char (point-min))))
368 (defun eww-display-image ()
369 (let ((data (shr-parse-image-data)))
370 (eww-setup-buffer)
371 (let ((inhibit-read-only t))
372 (shr-put-image data nil))
373 (goto-char (point-min))))
375 (defun eww-setup-buffer ()
376 (switch-to-buffer (get-buffer-create "*eww*"))
377 (let ((inhibit-read-only t))
378 (remove-overlays)
379 (erase-buffer))
380 (unless (eq major-mode 'eww-mode)
381 (eww-mode)))
383 (defun eww-view-source ()
384 (interactive)
385 (let ((buf (get-buffer-create "*eww-source*"))
386 (source eww-current-source))
387 (with-current-buffer buf
388 (delete-region (point-min) (point-max))
389 (insert (or eww-current-source "no source"))
390 (goto-char (point-min))
391 (when (featurep 'html-mode)
392 (html-mode)))
393 (view-buffer buf)))
395 (defvar eww-mode-map
396 (let ((map (make-sparse-keymap)))
397 (suppress-keymap map)
398 (define-key map "q" 'quit-window)
399 (define-key map "g" 'eww-reload)
400 (define-key map [tab] 'shr-next-link)
401 (define-key map [backtab] 'shr-previous-link)
402 (define-key map [delete] 'scroll-down-command)
403 (define-key map [?\S-\ ] 'scroll-down-command)
404 (define-key map "\177" 'scroll-down-command)
405 (define-key map " " 'scroll-up-command)
406 (define-key map "l" 'eww-back-url)
407 (define-key map "r" 'eww-forward-url)
408 (define-key map "n" 'eww-next-url)
409 (define-key map "p" 'eww-previous-url)
410 (define-key map "u" 'eww-up-url)
411 (define-key map "t" 'eww-top-url)
412 (define-key map "&" 'eww-browse-with-external-browser)
413 (define-key map "d" 'eww-download)
414 (define-key map "w" 'eww-copy-page-url)
415 (define-key map "C" 'url-cookie-list)
416 (define-key map "v" 'eww-view-source)
418 (define-key map "b" 'eww-add-bookmark)
419 (define-key map "B" 'eww-list-bookmarks)
420 (define-key map [(meta n)] 'eww-next-bookmark)
421 (define-key map [(meta p)] 'eww-previous-bookmark)
423 (easy-menu-define nil map ""
424 '("Eww"
425 ["Exit" eww-quit t]
426 ["Close browser" quit-window t]
427 ["Reload" eww-reload t]
428 ["Back to previous page" eww-back-url
429 :active (not (zerop (length eww-history)))]
430 ["Forward to next page" eww-forward-url
431 :active (not (zerop eww-history-position))]
432 ["Browse with external browser" eww-browse-with-external-browser t]
433 ["Download" eww-download t]
434 ["View page source" eww-view-source]
435 ["Copy page URL" eww-copy-page-url t]
436 ["Add bookmark" eww-add-bookmark t]
437 ["List bookmarks" eww-list-bookmarks t]
438 ["List cookies" url-cookie-list t]))
439 map))
441 (define-derived-mode eww-mode nil "eww"
442 "Mode for browsing the web.
444 \\{eww-mode-map}"
445 ;; FIXME? This seems a strange default.
446 (set (make-local-variable 'eww-current-url) 'author)
447 (set (make-local-variable 'eww-current-dom) nil)
448 (set (make-local-variable 'eww-current-source) nil)
449 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
450 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
451 (set (make-local-variable 'eww-history) nil)
452 (set (make-local-variable 'eww-history-position) 0)
453 (buffer-disable-undo)
454 ;;(setq buffer-read-only t)
457 (defun eww-save-history ()
458 (push (list :url eww-current-url
459 :title eww-current-title
460 :point (point)
461 :dom eww-current-dom
462 :source eww-current-source
463 :text (buffer-string))
464 eww-history))
466 ;;;###autoload
467 (defun eww-browse-url (url &optional _new-window)
468 (when (and (equal major-mode 'eww-mode)
469 eww-current-url)
470 (eww-save-history))
471 (eww url))
473 (defun eww-back-url ()
474 "Go to the previously displayed page."
475 (interactive)
476 (when (>= eww-history-position (length eww-history))
477 (error "No previous page"))
478 (eww-save-history)
479 (setq eww-history-position (+ eww-history-position 2))
480 (eww-restore-history (elt eww-history (1- eww-history-position))))
482 (defun eww-forward-url ()
483 "Go to the next displayed page."
484 (interactive)
485 (when (zerop eww-history-position)
486 (error "No next page"))
487 (eww-save-history)
488 (eww-restore-history (elt eww-history (1- eww-history-position))))
490 (defun eww-restore-history (elem)
491 (let ((inhibit-read-only t))
492 (erase-buffer)
493 (insert (plist-get elem :text))
494 (setq eww-current-source (plist-get elem :source))
495 (setq eww-current-dom (plist-get elem :dom))
496 (goto-char (plist-get elem :point))
497 (setq eww-current-url (plist-get elem :url)
498 eww-current-title (plist-get elem :title))
499 (eww-update-header-line-format)))
501 (defun eww-next-url ()
502 "Go to the page marked `next'.
503 A page is marked `next' if rel=\"next\" appears in a <link>
504 or <a> tag."
505 (interactive)
506 (if eww-next-url
507 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
508 (error "No `next' on this page")))
510 (defun eww-previous-url ()
511 "Go to the page marked `previous'.
512 A page is marked `previous' if rel=\"previous\" appears in a <link>
513 or <a> tag."
514 (interactive)
515 (if eww-previous-url
516 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
517 (error "No `previous' on this page")))
519 (defun eww-up-url ()
520 "Go to the page marked `up'.
521 A page is marked `up' if rel=\"up\" appears in a <link>
522 or <a> tag."
523 (interactive)
524 (if eww-up-url
525 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
526 (error "No `up' on this page")))
528 (defun eww-top-url ()
529 "Go to the page marked `top'.
530 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
531 appears in a <link> or <a> tag."
532 (interactive)
533 (let ((best-url (or eww-start-url
534 eww-contents-url
535 eww-home-url)))
536 (if best-url
537 (eww-browse-url (shr-expand-url best-url eww-current-url))
538 (error "No `top' for this page"))))
540 (defun eww-reload ()
541 "Reload the current page."
542 (interactive)
543 (url-retrieve eww-current-url 'eww-render
544 (list eww-current-url (point))))
546 ;; Form support.
548 (defvar eww-form nil)
550 (defvar eww-submit-map
551 (let ((map (make-sparse-keymap)))
552 (define-key map "\r" 'eww-submit)
553 (define-key map [(control c) (control c)] 'eww-submit)
554 map))
556 (defvar eww-checkbox-map
557 (let ((map (make-sparse-keymap)))
558 (define-key map [space] 'eww-toggle-checkbox)
559 (define-key map "\r" 'eww-toggle-checkbox)
560 (define-key map [(control c) (control c)] 'eww-submit)
561 map))
563 (defvar eww-text-map
564 (let ((map (make-keymap)))
565 (set-keymap-parent map text-mode-map)
566 (define-key map "\r" 'eww-submit)
567 (define-key map [(control a)] 'eww-beginning-of-text)
568 (define-key map [(control c) (control c)] 'eww-submit)
569 (define-key map [(control e)] 'eww-end-of-text)
570 (define-key map [tab] 'shr-next-link)
571 (define-key map [backtab] 'shr-previous-link)
572 map))
574 (defvar eww-textarea-map
575 (let ((map (make-keymap)))
576 (set-keymap-parent map text-mode-map)
577 (define-key map "\r" 'forward-line)
578 (define-key map [(control c) (control c)] 'eww-submit)
579 (define-key map [tab] 'shr-next-link)
580 (define-key map [backtab] 'shr-previous-link)
581 map))
583 (defvar eww-select-map
584 (let ((map (make-sparse-keymap)))
585 (define-key map "\r" 'eww-change-select)
586 (define-key map [(control c) (control c)] 'eww-submit)
587 map))
589 (defun eww-beginning-of-text ()
590 "Move to the start of the input field."
591 (interactive)
592 (goto-char (eww-beginning-of-field)))
594 (defun eww-end-of-text ()
595 "Move to the end of the text in the input field."
596 (interactive)
597 (goto-char (eww-end-of-field))
598 (let ((start (eww-beginning-of-field)))
599 (while (and (equal (following-char) ? )
600 (> (point) start))
601 (forward-char -1))
602 (when (> (point) start)
603 (forward-char 1))))
605 (defun eww-beginning-of-field ()
606 (cond
607 ((bobp)
608 (point))
609 ((not (eq (get-text-property (point) 'eww-form)
610 (get-text-property (1- (point)) 'eww-form)))
611 (point))
613 (previous-single-property-change
614 (point) 'eww-form nil (point-min)))))
616 (defun eww-end-of-field ()
617 (1- (next-single-property-change
618 (point) 'eww-form nil (point-max))))
620 (defun eww-tag-form (cont)
621 (let ((eww-form
622 (list (assq :method cont)
623 (assq :action cont)))
624 (start (point)))
625 (shr-ensure-paragraph)
626 (shr-generic cont)
627 (unless (bolp)
628 (insert "\n"))
629 (insert "\n")
630 (when (> (point) start)
631 (put-text-property start (1+ start)
632 'eww-form eww-form))))
634 (defun eww-form-submit (cont)
635 (let ((start (point))
636 (value (cdr (assq :value cont))))
637 (setq value
638 (if (zerop (length value))
639 "Submit"
640 value))
641 (insert value)
642 (add-face-text-property start (point) 'eww-form-submit)
643 (put-text-property start (point) 'eww-form
644 (list :eww-form eww-form
645 :value value
646 :type "submit"
647 :name (cdr (assq :name cont))))
648 (put-text-property start (point) 'keymap eww-submit-map)
649 (insert " ")))
651 (defun eww-form-checkbox (cont)
652 (let ((start (point)))
653 (if (cdr (assq :checked cont))
654 (insert eww-form-checkbox-selected-symbol)
655 (insert eww-form-checkbox-symbol))
656 (add-face-text-property start (point) 'eww-form-checkbox)
657 (put-text-property start (point) 'eww-form
658 (list :eww-form eww-form
659 :value (cdr (assq :value cont))
660 :type (downcase (cdr (assq :type cont)))
661 :checked (cdr (assq :checked cont))
662 :name (cdr (assq :name cont))))
663 (put-text-property start (point) 'keymap eww-checkbox-map)
664 (insert " ")))
666 (defun eww-form-text (cont)
667 (let ((start (point))
668 (type (downcase (or (cdr (assq :type cont))
669 "text")))
670 (value (or (cdr (assq :value cont)) ""))
671 (width (string-to-number
672 (or (cdr (assq :size cont))
673 "40"))))
674 (insert value)
675 (when (< (length value) width)
676 (insert (make-string (- width (length value)) ? )))
677 (put-text-property start (point) 'face 'eww-form-text)
678 (put-text-property start (point) 'local-map eww-text-map)
679 (put-text-property start (point) 'inhibit-read-only t)
680 (put-text-property start (point) 'eww-form
681 (list :eww-form eww-form
682 :value value
683 :type type
684 :name (cdr (assq :name cont))))
685 (insert " ")))
687 (defconst eww-text-input-types '("text" "password" "textarea"
688 "color" "date" "datetime" "datetime-local"
689 "email" "month" "number" "search" "tel"
690 "time" "url" "week")
691 "List of input types which represent a text input.
692 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
694 (defun eww-process-text-input (beg end length)
695 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
696 (properties (text-properties-at end))
697 (type (plist-get form :type)))
698 (when (and form
699 (member type eww-text-input-types))
700 (cond
701 ((zerop length)
702 ;; Delete some space at the end.
703 (save-excursion
704 (goto-char
705 (if (equal type "textarea")
706 (1- (line-end-position))
707 (eww-end-of-field)))
708 (let ((new (- end beg)))
709 (while (and (> new 0)
710 (eql (following-char) ? ))
711 (delete-region (point) (1+ (point)))
712 (setq new (1- new))))
713 (set-text-properties beg end properties)))
714 ((> length 0)
715 ;; Add padding.
716 (save-excursion
717 (goto-char
718 (if (equal type "textarea")
719 (1- (line-end-position))
720 (eww-end-of-field)))
721 (let ((start (point)))
722 (insert (make-string length ? ))
723 (set-text-properties start (point) properties)))))
724 (let ((value (buffer-substring-no-properties
725 (eww-beginning-of-field)
726 (eww-end-of-field))))
727 (when (string-match " +\\'" value)
728 (setq value (substring value 0 (match-beginning 0))))
729 (plist-put form :value value)
730 (when (equal type "password")
731 ;; Display passwords as asterisks.
732 (let ((start (eww-beginning-of-field)))
733 (put-text-property start (+ start (length value))
734 'display (make-string (length value) ?*))))))))
736 (defun eww-tag-textarea (cont)
737 (let ((start (point))
738 (value (or (cdr (assq :value cont)) ""))
739 (lines (string-to-number
740 (or (cdr (assq :rows cont))
741 "10")))
742 (width (string-to-number
743 (or (cdr (assq :cols cont))
744 "10")))
745 end)
746 (shr-ensure-newline)
747 (insert value)
748 (shr-ensure-newline)
749 (when (< (count-lines start (point)) lines)
750 (dotimes (i (- lines (count-lines start (point))))
751 (insert "\n")))
752 (setq end (point-marker))
753 (goto-char start)
754 (while (< (point) end)
755 (end-of-line)
756 (let ((pad (- width (- (point) (line-beginning-position)))))
757 (when (> pad 0)
758 (insert (make-string pad ? ))))
759 (add-face-text-property (line-beginning-position)
760 (point) 'eww-form-text)
761 (put-text-property (line-beginning-position) (point)
762 'local-map eww-textarea-map)
763 (forward-line 1))
764 (put-text-property start (point) 'eww-form
765 (list :eww-form eww-form
766 :value value
767 :type "textarea"
768 :name (cdr (assq :name cont))))))
770 (defun eww-tag-input (cont)
771 (let ((type (downcase (or (cdr (assq :type cont))
772 "text")))
773 (start (point)))
774 (cond
775 ((or (equal type "checkbox")
776 (equal type "radio"))
777 (eww-form-checkbox cont))
778 ((equal type "submit")
779 (eww-form-submit cont))
780 ((equal type "hidden")
781 (let ((form eww-form)
782 (name (cdr (assq :name cont))))
783 ;; Don't add <input type=hidden> elements repeatedly.
784 (while (and form
785 (or (not (consp (car form)))
786 (not (eq (caar form) 'hidden))
787 (not (equal (plist-get (cdr (car form)) :name)
788 name))))
789 (setq form (cdr form)))
790 (unless form
791 (nconc eww-form (list
792 (list 'hidden
793 :name name
794 :value (cdr (assq :value cont))))))))
796 (eww-form-text cont)))
797 (unless (= start (point))
798 (put-text-property start (1+ start) 'help-echo "Input field"))))
800 (defun eww-tag-select (cont)
801 (shr-ensure-paragraph)
802 (let ((menu (list :name (cdr (assq :name cont))
803 :eww-form eww-form))
804 (options nil)
805 (start (point))
806 (max 0)
807 opelem)
808 (if (eq (car (car cont)) 'optgroup)
809 (dolist (groupelem cont)
810 (unless (cdr (assq :disabled (cdr groupelem)))
811 (setq opelem (append opelem (cdr (cdr groupelem))))))
812 (setq opelem cont))
813 (dolist (elem opelem)
814 (when (eq (car elem) 'option)
815 (when (cdr (assq :selected (cdr elem)))
816 (nconc menu (list :value
817 (cdr (assq :value (cdr elem))))))
818 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
819 (setq max (max max (length display)))
820 (push (list 'item
821 :value (cdr (assq :value (cdr elem)))
822 :display display)
823 options))))
824 (when options
825 (setq options (nreverse options))
826 ;; If we have no selected values, default to the first value.
827 (unless (plist-get menu :value)
828 (nconc menu (list :value (nth 2 (car options)))))
829 (nconc menu options)
830 (let ((selected (eww-select-display menu)))
831 (insert selected
832 (make-string (- max (length selected)) ? )))
833 (put-text-property start (point) 'eww-form menu)
834 (add-face-text-property start (point) 'eww-form-select)
835 (put-text-property start (point) 'keymap eww-select-map)
836 (shr-ensure-paragraph))))
838 (defun eww-select-display (select)
839 (let ((value (plist-get select :value))
840 display)
841 (dolist (elem select)
842 (when (and (consp elem)
843 (eq (car elem) 'item)
844 (equal value (plist-get (cdr elem) :value)))
845 (setq display (plist-get (cdr elem) :display))))
846 display))
848 (defun eww-change-select ()
849 "Change the value of the select drop-down menu under point."
850 (interactive)
851 (let* ((input (get-text-property (point) 'eww-form))
852 (completion-ignore-case t)
853 (options
854 (delq nil
855 (mapcar (lambda (elem)
856 (and (consp elem)
857 (eq (car elem) 'item)
858 (cons (plist-get (cdr elem) :display)
859 (plist-get (cdr elem) :value))))
860 input)))
861 (display
862 (completing-read "Change value: " options nil 'require-match))
863 (inhibit-read-only t))
864 (plist-put input :value (cdr (assoc-string display options t)))
865 (goto-char
866 (eww-update-field display))))
868 (defun eww-update-field (string)
869 (let ((properties (text-properties-at (point)))
870 (start (eww-beginning-of-field))
871 (end (1+ (eww-end-of-field))))
872 (delete-region start end)
873 (insert string
874 (make-string (- (- end start) (length string)) ? ))
875 (set-text-properties start end properties)
876 start))
878 (defun eww-toggle-checkbox ()
879 "Toggle the value of the checkbox under point."
880 (interactive)
881 (let* ((input (get-text-property (point) 'eww-form))
882 (type (plist-get input :type)))
883 (if (equal type "checkbox")
884 (goto-char
886 (if (plist-get input :checked)
887 (progn
888 (plist-put input :checked nil)
889 (eww-update-field eww-form-checkbox-symbol))
890 (plist-put input :checked t)
891 (eww-update-field eww-form-checkbox-selected-symbol))))
892 ;; Radio button. Switch all other buttons off.
893 (let ((name (plist-get input :name)))
894 (save-excursion
895 (dolist (elem (eww-inputs (plist-get input :eww-form)))
896 (when (equal (plist-get (cdr elem) :name) name)
897 (goto-char (car elem))
898 (if (not (eq (cdr elem) input))
899 (progn
900 (plist-put input :checked nil)
901 (eww-update-field eww-form-checkbox-symbol))
902 (plist-put input :checked t)
903 (eww-update-field eww-form-checkbox-selected-symbol)))))
904 (forward-char 1)))))
906 (defun eww-inputs (form)
907 (let ((start (point-min))
908 (inputs nil))
909 (while (and start
910 (< start (point-max)))
911 (when (or (get-text-property start 'eww-form)
912 (setq start (next-single-property-change start 'eww-form)))
913 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
914 form)
915 (push (cons start (get-text-property start 'eww-form))
916 inputs))
917 (setq start (next-single-property-change start 'eww-form))))
918 (nreverse inputs)))
920 (defun eww-input-value (input)
921 (let ((type (plist-get input :type))
922 (value (plist-get input :value)))
923 (cond
924 ((equal type "textarea")
925 (with-temp-buffer
926 (insert value)
927 (goto-char (point-min))
928 (while (re-search-forward "^ +\n\\| +$" nil t)
929 (replace-match "" t t))
930 (buffer-string)))
932 (if (string-match " +\\'" value)
933 (substring value 0 (match-beginning 0))
934 value)))))
936 (defun eww-submit ()
937 "Submit the current form."
938 (interactive)
939 (let* ((this-input (get-text-property (point) 'eww-form))
940 (form (plist-get this-input :eww-form))
941 values next-submit)
942 (dolist (elem (sort (eww-inputs form)
943 (lambda (o1 o2)
944 (< (car o1) (car o2)))))
945 (let* ((input (cdr elem))
946 (input-start (car elem))
947 (name (plist-get input :name)))
948 (when name
949 (cond
950 ((member (plist-get input :type) '("checkbox" "radio"))
951 (when (plist-get input :checked)
952 (push (cons name (plist-get input :value))
953 values)))
954 ((equal (plist-get input :type) "submit")
955 ;; We want the values from buttons if we hit a button if
956 ;; we hit enter on it, or if it's the first button after
957 ;; the field we did hit return on.
958 (when (or (eq input this-input)
959 (and (not (eq input this-input))
960 (null next-submit)
961 (> input-start (point))))
962 (setq next-submit t)
963 (push (cons name (plist-get input :value))
964 values)))
966 (push (cons name (eww-input-value input))
967 values))))))
968 (dolist (elem form)
969 (when (and (consp elem)
970 (eq (car elem) 'hidden))
971 (push (cons (plist-get (cdr elem) :name)
972 (plist-get (cdr elem) :value))
973 values)))
974 (if (and (stringp (cdr (assq :method form)))
975 (equal (downcase (cdr (assq :method form))) "post"))
976 (let ((url-request-method "POST")
977 (url-request-extra-headers
978 '(("Content-Type" . "application/x-www-form-urlencoded")))
979 (url-request-data (mm-url-encode-www-form-urlencoded values)))
980 (eww-browse-url (shr-expand-url (cdr (assq :action form))
981 eww-current-url)))
982 (eww-browse-url
983 (concat
984 (if (cdr (assq :action form))
985 (shr-expand-url (cdr (assq :action form))
986 eww-current-url)
987 eww-current-url)
989 (mm-url-encode-www-form-urlencoded values))))))
991 (defun eww-browse-with-external-browser (&optional url)
992 "Browse the current URL with an external browser.
993 The browser to used is specified by the `shr-external-browser' variable."
994 (interactive)
995 (funcall shr-external-browser (or url eww-current-url)))
997 (defun eww-follow-link (&optional external mouse-event)
998 "Browse the URL under point.
999 If EXTERNAL, browse the URL using `shr-external-browser'."
1000 (interactive (list current-prefix-arg last-nonmenu-event))
1001 (mouse-set-point mouse-event)
1002 (let ((url (get-text-property (point) 'shr-url)))
1003 (cond
1004 ((not url)
1005 (message "No link under point"))
1006 ((string-match "^mailto:" url)
1007 (browse-url-mail url))
1008 (external
1009 (funcall shr-external-browser url))
1010 ;; This is a #target url in the same page as the current one.
1011 ((and (url-target (url-generic-parse-url url))
1012 (eww-same-page-p url eww-current-url))
1013 (eww-save-history)
1014 (eww-display-html 'utf8 url eww-current-dom))
1016 (eww-browse-url url)))))
1018 (defun eww-same-page-p (url1 url2)
1019 "Return non-nil if both URLs represent the same page.
1020 Differences in #targets are ignored."
1021 (let ((obj1 (url-generic-parse-url url1))
1022 (obj2 (url-generic-parse-url url2)))
1023 (setf (url-target obj1) nil)
1024 (setf (url-target obj2) nil)
1025 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1027 (defun eww-copy-page-url ()
1028 (interactive)
1029 (message "%s" eww-current-url)
1030 (kill-new eww-current-url))
1032 (defun eww-download ()
1033 "Download URL under point to `eww-download-directory'."
1034 (interactive)
1035 (let ((url (get-text-property (point) 'shr-url)))
1036 (if (not url)
1037 (message "No URL under point")
1038 (url-retrieve url 'eww-download-callback (list url)))))
1040 (defun eww-download-callback (status url)
1041 (unless (plist-get status :error)
1042 (let* ((obj (url-generic-parse-url url))
1043 (path (car (url-path-and-query obj)))
1044 (file (eww-make-unique-file-name (file-name-nondirectory path)
1045 eww-download-path)))
1046 (write-file file)
1047 (message "Saved %s" file))))
1049 (defun eww-make-unique-file-name (file directory)
1050 (cond
1051 ((zerop (length file))
1052 (setq file "!"))
1053 ((string-match "\\`[.]" file)
1054 (setq file (concat "!" file))))
1055 (let ((count 1))
1056 (while (file-exists-p (expand-file-name file directory))
1057 (setq file
1058 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1059 (format "%s(%d)%s" (match-string 1 file)
1060 count (match-string 2 file))
1061 (format "%s(%d)" file count)))
1062 (setq count (1+ count)))
1063 (expand-file-name file directory)))
1065 ;;; Bookmarks code
1067 (defvar eww-bookmarks nil)
1069 (defun eww-add-bookmark ()
1070 "Add the current page to the bookmarks."
1071 (interactive)
1072 (eww-read-bookmarks)
1073 (dolist (bookmark eww-bookmarks)
1074 (when (equal eww-current-url
1075 (plist-get bookmark :url))
1076 (error "Already bookmarked")))
1077 (if (y-or-n-p "bookmark this page? ")
1078 (progn
1079 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1080 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1081 (push (list :url eww-current-url
1082 :title title
1083 :time (current-time-string))
1084 eww-bookmarks))
1085 (eww-write-bookmarks)
1086 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1088 (defun eww-write-bookmarks ()
1089 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory)
1090 (insert ";; Auto-generated file; don't edit\n")
1091 (pp eww-bookmarks (current-buffer))))
1093 (defun eww-read-bookmarks ()
1094 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory)))
1095 (setq eww-bookmarks
1096 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1097 (with-temp-buffer
1098 (insert-file-contents file)
1099 (read (current-buffer)))))))
1101 (defun eww-list-bookmarks ()
1102 "Display the bookmarks."
1103 (interactive)
1104 (eww-bookmark-prepare)
1105 (pop-to-buffer "*eww bookmarks*"))
1107 (defun eww-bookmark-prepare ()
1108 (eww-read-bookmarks)
1109 (when (null eww-bookmarks)
1110 (error "No bookmarks are defined"))
1111 (set-buffer (get-buffer-create "*eww bookmarks*"))
1112 (eww-bookmark-mode)
1113 (let ((format "%-40s %s")
1114 (inhibit-read-only t)
1115 start url)
1116 (erase-buffer)
1117 (setq header-line-format (concat " " (format format "URL" "Title")))
1118 (dolist (bookmark eww-bookmarks)
1119 (setq start (point))
1120 (setq url (plist-get bookmark :url))
1121 (when (> (length url) 40)
1122 (setq url (substring url 0 40)))
1123 (insert (format format url
1124 (plist-get bookmark :title))
1125 "\n")
1126 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1127 (goto-char (point-min))))
1129 (defvar eww-bookmark-kill-ring nil)
1131 (defun eww-bookmark-kill ()
1132 "Kill the current bookmark."
1133 (interactive)
1134 (let* ((start (line-beginning-position))
1135 (bookmark (get-text-property start 'eww-bookmark))
1136 (inhibit-read-only t))
1137 (unless bookmark
1138 (error "No bookmark on the current line"))
1139 (forward-line 1)
1140 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1141 (delete-region start (point))
1142 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1143 (eww-write-bookmarks)))
1145 (defun eww-bookmark-yank ()
1146 "Yank a previously killed bookmark to the current line."
1147 (interactive)
1148 (unless eww-bookmark-kill-ring
1149 (error "No previously killed bookmark"))
1150 (beginning-of-line)
1151 (let ((inhibit-read-only t)
1152 (start (point))
1153 bookmark)
1154 (insert (pop eww-bookmark-kill-ring))
1155 (setq bookmark (get-text-property start 'eww-bookmark))
1156 (if (= start (point-min))
1157 (push bookmark eww-bookmarks)
1158 (let ((line (count-lines start (point))))
1159 (setcdr (nthcdr (1- line) eww-bookmarks)
1160 (cons bookmark (nthcdr line eww-bookmarks)))))
1161 (eww-write-bookmarks)))
1163 (defun eww-bookmark-quit ()
1164 "Kill the current buffer."
1165 (interactive)
1166 (kill-buffer (current-buffer)))
1168 (defun eww-bookmark-browse ()
1169 "Browse the bookmark under point in eww."
1170 (interactive)
1171 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1172 (unless bookmark
1173 (error "No bookmark on the current line"))
1174 ;; We wish to leave this window, but if it's the only window here,
1175 ;; just let it remain.
1176 (ignore-errors
1177 (delete-window))
1178 (eww-browse-url (plist-get bookmark :url))))
1180 (defun eww-next-bookmark ()
1181 "Go to the next bookmark in the list."
1182 (interactive)
1183 (let ((first nil)
1184 bookmark)
1185 (unless (get-buffer "*eww bookmarks*")
1186 (setq first t)
1187 (eww-bookmark-prepare))
1188 (with-current-buffer (get-buffer "*eww bookmarks*")
1189 (when (and (not first)
1190 (not (eobp)))
1191 (forward-line 1))
1192 (setq bookmark (get-text-property (line-beginning-position)
1193 'eww-bookmark))
1194 (unless bookmark
1195 (error "No next bookmark")))
1196 (eww-browse-url (plist-get bookmark :url))))
1198 (defun eww-previous-bookmark ()
1199 "Go to the previous bookmark in the list."
1200 (interactive)
1201 (let ((first nil)
1202 bookmark)
1203 (unless (get-buffer "*eww bookmarks*")
1204 (setq first t)
1205 (eww-bookmark-prepare))
1206 (with-current-buffer (get-buffer "*eww bookmarks*")
1207 (if first
1208 (goto-char (point-max))
1209 (beginning-of-line))
1210 ;; On the final line.
1211 (when (eolp)
1212 (forward-line -1))
1213 (if (bobp)
1214 (error "No previous bookmark")
1215 (forward-line -1))
1216 (setq bookmark (get-text-property (line-beginning-position)
1217 'eww-bookmark)))
1218 (eww-browse-url (plist-get bookmark :url))))
1220 (defvar eww-bookmark-mode-map
1221 (let ((map (make-sparse-keymap)))
1222 (suppress-keymap map)
1223 (define-key map "q" 'eww-bookmark-quit)
1224 (define-key map [(control k)] 'eww-bookmark-kill)
1225 (define-key map [(control y)] 'eww-bookmark-yank)
1226 (define-key map "\r" 'eww-bookmark-browse)
1227 map))
1229 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1230 "Mode for listing bookmarks.
1232 \\{eww-bookmark-mode-map}"
1233 (buffer-disable-undo)
1234 (setq buffer-read-only t
1235 truncate-lines t))
1237 (provide 'eww)
1239 ;;; eww.el ends here