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