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