* lisp/menu-bar.el (menu-bar-tools-menu): Add `browse-web'.
[emacs.git] / lisp / net / eww.el
blobbff5cd8f2d3ee9be3e7e183338d5eec85614801f
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" 'eww-close)
399 (define-key map "Q" 'eww-exit)
400 (define-key map "g" 'eww-reload)
401 (define-key map [tab] 'shr-next-link)
402 (define-key map [backtab] 'shr-previous-link)
403 (define-key map [delete] 'scroll-down-command)
404 (define-key map [?\S-\ ] 'scroll-down-command)
405 (define-key map "\177" 'scroll-down-command)
406 (define-key map " " 'scroll-up-command)
407 (define-key map "l" 'eww-back-url)
408 (define-key map "r" 'eww-forward-url)
409 (define-key map "n" 'eww-next-url)
410 (define-key map "p" 'eww-previous-url)
411 (define-key map "u" 'eww-up-url)
412 (define-key map "t" 'eww-top-url)
413 (define-key map "&" 'eww-browse-with-external-browser)
414 (define-key map "d" 'eww-download)
415 (define-key map "w" 'eww-copy-page-url)
416 (define-key map "C" 'url-cookie-list)
417 (define-key map "v" 'eww-view-source)
419 (define-key map "b" 'eww-add-bookmark)
420 (define-key map "B" 'eww-list-bookmarks)
421 (define-key map [(meta n)] 'eww-next-bookmark)
422 (define-key map [(meta p)] 'eww-previous-bookmark)
424 (easy-menu-define nil map ""
425 '("Eww"
426 ["Exit" eww-exit t]
427 ["Close browser" eww-close t]
428 ["Reload" eww-reload t]
429 ["Back to previous page" eww-back-url
430 :active (not (zerop (length eww-history)))]
431 ["Forward to next page" eww-forward-url
432 :active (not (zerop eww-history-position))]
433 ["Browse with external browser" eww-browse-with-external-browser t]
434 ["Download" eww-download t]
435 ["View page source" eww-view-source]
436 ["Copy page URL" eww-copy-page-url t]
437 ["Add bookmark" eww-add-bookmark t]
438 ["List bookmarks" eww-list-bookmarks t]
439 ["List cookies" url-cookie-list t]))
440 map))
442 (define-derived-mode eww-mode nil "eww"
443 "Mode for browsing the web.
445 \\{eww-mode-map}"
446 ;; FIXME? This seems a strange default.
447 (set (make-local-variable 'eww-current-url) 'author)
448 (set (make-local-variable 'eww-current-dom) nil)
449 (set (make-local-variable 'eww-current-source) nil)
450 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
451 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
452 (set (make-local-variable 'eww-history) nil)
453 (set (make-local-variable 'eww-history-position) 0)
454 (buffer-disable-undo)
455 ;;(setq buffer-read-only t)
458 (defun eww-exit ()
459 "Exit the Emacs Web Wowser."
460 (interactive)
461 (setq eww-history nil)
462 (kill-buffer (current-buffer)))
464 (defun eww-close ()
465 "Close the Emacs Web Wowser browser, leaving history intact."
466 (interactive)
467 (quit-window))
469 (defun eww-save-history ()
470 (push (list :url eww-current-url
471 :title eww-current-title
472 :point (point)
473 :dom eww-current-dom
474 :source eww-current-source
475 :text (buffer-string))
476 eww-history))
478 ;;;###autoload
479 (defun eww-browse-url (url &optional _new-window)
480 (when (and (equal major-mode 'eww-mode)
481 eww-current-url)
482 (eww-save-history))
483 (eww url))
485 (defun eww-back-url ()
486 "Go to the previously displayed page."
487 (interactive)
488 (when (>= eww-history-position (length eww-history))
489 (error "No previous page"))
490 (eww-save-history)
491 (setq eww-history-position (+ eww-history-position 2))
492 (eww-restore-history (elt eww-history (1- eww-history-position))))
494 (defun eww-forward-url ()
495 "Go to the next displayed page."
496 (interactive)
497 (when (zerop eww-history-position)
498 (error "No next page"))
499 (eww-save-history)
500 (eww-restore-history (elt eww-history (1- eww-history-position))))
502 (defun eww-restore-history (elem)
503 (let ((inhibit-read-only t))
504 (erase-buffer)
505 (insert (plist-get elem :text))
506 (setq eww-current-source (plist-get elem :source))
507 (setq eww-current-dom (plist-get elem :dom))
508 (goto-char (plist-get elem :point))
509 (setq eww-current-url (plist-get elem :url)
510 eww-current-title (plist-get elem :title))
511 (eww-update-header-line-format)))
513 (defun eww-next-url ()
514 "Go to the page marked `next'.
515 A page is marked `next' if rel=\"next\" appears in a <link>
516 or <a> tag."
517 (interactive)
518 (if eww-next-url
519 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
520 (error "No `next' on this page")))
522 (defun eww-previous-url ()
523 "Go to the page marked `previous'.
524 A page is marked `previous' if rel=\"previous\" appears in a <link>
525 or <a> tag."
526 (interactive)
527 (if eww-previous-url
528 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
529 (error "No `previous' on this page")))
531 (defun eww-up-url ()
532 "Go to the page marked `up'.
533 A page is marked `up' if rel=\"up\" appears in a <link>
534 or <a> tag."
535 (interactive)
536 (if eww-up-url
537 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
538 (error "No `up' on this page")))
540 (defun eww-top-url ()
541 "Go to the page marked `top'.
542 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
543 appears in a <link> or <a> tag."
544 (interactive)
545 (let ((best-url (or eww-start-url
546 eww-contents-url
547 eww-home-url)))
548 (if best-url
549 (eww-browse-url (shr-expand-url best-url eww-current-url))
550 (error "No `top' for this page"))))
552 (defun eww-reload ()
553 "Reload the current page."
554 (interactive)
555 (url-retrieve eww-current-url 'eww-render
556 (list eww-current-url (point))))
558 ;; Form support.
560 (defvar eww-form nil)
562 (defvar eww-submit-map
563 (let ((map (make-sparse-keymap)))
564 (define-key map "\r" 'eww-submit)
565 (define-key map [(control c) (control c)] 'eww-submit)
566 map))
568 (defvar eww-checkbox-map
569 (let ((map (make-sparse-keymap)))
570 (define-key map [space] 'eww-toggle-checkbox)
571 (define-key map "\r" 'eww-toggle-checkbox)
572 (define-key map [(control c) (control c)] 'eww-submit)
573 map))
575 (defvar eww-text-map
576 (let ((map (make-keymap)))
577 (set-keymap-parent map text-mode-map)
578 (define-key map "\r" 'eww-submit)
579 (define-key map [(control a)] 'eww-beginning-of-text)
580 (define-key map [(control c) (control c)] 'eww-submit)
581 (define-key map [(control e)] 'eww-end-of-text)
582 (define-key map [tab] 'shr-next-link)
583 (define-key map [backtab] 'shr-previous-link)
584 map))
586 (defvar eww-textarea-map
587 (let ((map (make-keymap)))
588 (set-keymap-parent map text-mode-map)
589 (define-key map "\r" 'forward-line)
590 (define-key map [(control c) (control c)] 'eww-submit)
591 (define-key map [tab] 'shr-next-link)
592 (define-key map [backtab] 'shr-previous-link)
593 map))
595 (defvar eww-select-map
596 (let ((map (make-sparse-keymap)))
597 (define-key map "\r" 'eww-change-select)
598 (define-key map [(control c) (control c)] 'eww-submit)
599 map))
601 (defun eww-beginning-of-text ()
602 "Move to the start of the input field."
603 (interactive)
604 (goto-char (eww-beginning-of-field)))
606 (defun eww-end-of-text ()
607 "Move to the end of the text in the input field."
608 (interactive)
609 (goto-char (eww-end-of-field))
610 (let ((start (eww-beginning-of-field)))
611 (while (and (equal (following-char) ? )
612 (> (point) start))
613 (forward-char -1))
614 (when (> (point) start)
615 (forward-char 1))))
617 (defun eww-beginning-of-field ()
618 (cond
619 ((bobp)
620 (point))
621 ((not (eq (get-text-property (point) 'eww-form)
622 (get-text-property (1- (point)) 'eww-form)))
623 (point))
625 (previous-single-property-change
626 (point) 'eww-form nil (point-min)))))
628 (defun eww-end-of-field ()
629 (1- (next-single-property-change
630 (point) 'eww-form nil (point-max))))
632 (defun eww-tag-form (cont)
633 (let ((eww-form
634 (list (assq :method cont)
635 (assq :action cont)))
636 (start (point)))
637 (shr-ensure-paragraph)
638 (shr-generic cont)
639 (unless (bolp)
640 (insert "\n"))
641 (insert "\n")
642 (when (> (point) start)
643 (put-text-property start (1+ start)
644 'eww-form eww-form))))
646 (defun eww-form-submit (cont)
647 (let ((start (point))
648 (value (cdr (assq :value cont))))
649 (setq value
650 (if (zerop (length value))
651 "Submit"
652 value))
653 (insert value)
654 (add-face-text-property start (point) 'eww-form-submit)
655 (put-text-property start (point) 'eww-form
656 (list :eww-form eww-form
657 :value value
658 :type "submit"
659 :name (cdr (assq :name cont))))
660 (put-text-property start (point) 'keymap eww-submit-map)
661 (insert " ")))
663 (defun eww-form-checkbox (cont)
664 (let ((start (point)))
665 (if (cdr (assq :checked cont))
666 (insert eww-form-checkbox-selected-symbol)
667 (insert eww-form-checkbox-symbol))
668 (add-face-text-property start (point) 'eww-form-checkbox)
669 (put-text-property start (point) 'eww-form
670 (list :eww-form eww-form
671 :value (cdr (assq :value cont))
672 :type (downcase (cdr (assq :type cont)))
673 :checked (cdr (assq :checked cont))
674 :name (cdr (assq :name cont))))
675 (put-text-property start (point) 'keymap eww-checkbox-map)
676 (insert " ")))
678 (defun eww-form-text (cont)
679 (let ((start (point))
680 (type (downcase (or (cdr (assq :type cont))
681 "text")))
682 (value (or (cdr (assq :value cont)) ""))
683 (width (string-to-number
684 (or (cdr (assq :size cont))
685 "40"))))
686 (insert value)
687 (when (< (length value) width)
688 (insert (make-string (- width (length value)) ? )))
689 (put-text-property start (point) 'face 'eww-form-text)
690 (put-text-property start (point) 'local-map eww-text-map)
691 (put-text-property start (point) 'inhibit-read-only t)
692 (put-text-property start (point) 'eww-form
693 (list :eww-form eww-form
694 :value value
695 :type type
696 :name (cdr (assq :name cont))))
697 (insert " ")))
699 (defconst eww-text-input-types '("text" "password" "textarea"
700 "color" "date" "datetime" "datetime-local"
701 "email" "month" "number" "search" "tel"
702 "time" "url" "week")
703 "List of input types which represent a text input.
704 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
706 (defun eww-process-text-input (beg end length)
707 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
708 (properties (text-properties-at end))
709 (type (plist-get form :type)))
710 (when (and form
711 (member type eww-text-input-types))
712 (cond
713 ((zerop length)
714 ;; Delete some space at the end.
715 (save-excursion
716 (goto-char
717 (if (equal type "textarea")
718 (1- (line-end-position))
719 (eww-end-of-field)))
720 (let ((new (- end beg)))
721 (while (and (> new 0)
722 (eql (following-char) ? ))
723 (delete-region (point) (1+ (point)))
724 (setq new (1- new))))
725 (set-text-properties beg end properties)))
726 ((> length 0)
727 ;; Add padding.
728 (save-excursion
729 (goto-char
730 (if (equal type "textarea")
731 (1- (line-end-position))
732 (eww-end-of-field)))
733 (let ((start (point)))
734 (insert (make-string length ? ))
735 (set-text-properties start (point) properties)))))
736 (let ((value (buffer-substring-no-properties
737 (eww-beginning-of-field)
738 (eww-end-of-field))))
739 (when (string-match " +\\'" value)
740 (setq value (substring value 0 (match-beginning 0))))
741 (plist-put form :value value)
742 (when (equal type "password")
743 ;; Display passwords as asterisks.
744 (let ((start (eww-beginning-of-field)))
745 (put-text-property start (+ start (length value))
746 'display (make-string (length value) ?*))))))))
748 (defun eww-tag-textarea (cont)
749 (let ((start (point))
750 (value (or (cdr (assq :value cont)) ""))
751 (lines (string-to-number
752 (or (cdr (assq :rows cont))
753 "10")))
754 (width (string-to-number
755 (or (cdr (assq :cols cont))
756 "10")))
757 end)
758 (shr-ensure-newline)
759 (insert value)
760 (shr-ensure-newline)
761 (when (< (count-lines start (point)) lines)
762 (dotimes (i (- lines (count-lines start (point))))
763 (insert "\n")))
764 (setq end (point-marker))
765 (goto-char start)
766 (while (< (point) end)
767 (end-of-line)
768 (let ((pad (- width (- (point) (line-beginning-position)))))
769 (when (> pad 0)
770 (insert (make-string pad ? ))))
771 (add-face-text-property (line-beginning-position)
772 (point) 'eww-form-text)
773 (put-text-property (line-beginning-position) (point)
774 'local-map eww-textarea-map)
775 (forward-line 1))
776 (put-text-property start (point) 'eww-form
777 (list :eww-form eww-form
778 :value value
779 :type "textarea"
780 :name (cdr (assq :name cont))))))
782 (defun eww-tag-input (cont)
783 (let ((type (downcase (or (cdr (assq :type cont))
784 "text")))
785 (start (point)))
786 (cond
787 ((or (equal type "checkbox")
788 (equal type "radio"))
789 (eww-form-checkbox cont))
790 ((equal type "submit")
791 (eww-form-submit cont))
792 ((equal type "hidden")
793 (let ((form eww-form)
794 (name (cdr (assq :name cont))))
795 ;; Don't add <input type=hidden> elements repeatedly.
796 (while (and form
797 (or (not (consp (car form)))
798 (not (eq (caar form) 'hidden))
799 (not (equal (plist-get (cdr (car form)) :name)
800 name))))
801 (setq form (cdr form)))
802 (unless form
803 (nconc eww-form (list
804 (list 'hidden
805 :name name
806 :value (cdr (assq :value cont))))))))
808 (eww-form-text cont)))
809 (unless (= start (point))
810 (put-text-property start (1+ start) 'help-echo "Input field"))))
812 (defun eww-tag-select (cont)
813 (shr-ensure-paragraph)
814 (let ((menu (list :name (cdr (assq :name cont))
815 :eww-form eww-form))
816 (options nil)
817 (start (point))
818 (max 0)
819 opelem)
820 (if (eq (car (car cont)) 'optgroup)
821 (dolist (groupelem cont)
822 (unless (cdr (assq :disabled (cdr groupelem)))
823 (setq opelem (append opelem (cdr (cdr groupelem))))))
824 (setq opelem cont))
825 (dolist (elem opelem)
826 (when (eq (car elem) 'option)
827 (when (cdr (assq :selected (cdr elem)))
828 (nconc menu (list :value
829 (cdr (assq :value (cdr elem))))))
830 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
831 (setq max (max max (length display)))
832 (push (list 'item
833 :value (cdr (assq :value (cdr elem)))
834 :display display)
835 options))))
836 (when options
837 (setq options (nreverse options))
838 ;; If we have no selected values, default to the first value.
839 (unless (plist-get menu :value)
840 (nconc menu (list :value (nth 2 (car options)))))
841 (nconc menu options)
842 (let ((selected (eww-select-display menu)))
843 (insert selected
844 (make-string (- max (length selected)) ? )))
845 (put-text-property start (point) 'eww-form menu)
846 (add-face-text-property start (point) 'eww-form-select)
847 (put-text-property start (point) 'keymap eww-select-map)
848 (shr-ensure-paragraph))))
850 (defun eww-select-display (select)
851 (let ((value (plist-get select :value))
852 display)
853 (dolist (elem select)
854 (when (and (consp elem)
855 (eq (car elem) 'item)
856 (equal value (plist-get (cdr elem) :value)))
857 (setq display (plist-get (cdr elem) :display))))
858 display))
860 (defun eww-change-select ()
861 "Change the value of the select drop-down menu under point."
862 (interactive)
863 (let* ((input (get-text-property (point) 'eww-form))
864 (completion-ignore-case t)
865 (options
866 (delq nil
867 (mapcar (lambda (elem)
868 (and (consp elem)
869 (eq (car elem) 'item)
870 (cons (plist-get (cdr elem) :display)
871 (plist-get (cdr elem) :value))))
872 input)))
873 (display
874 (completing-read "Change value: " options nil 'require-match))
875 (inhibit-read-only t))
876 (plist-put input :value (cdr (assoc-string display options t)))
877 (goto-char
878 (eww-update-field display))))
880 (defun eww-update-field (string)
881 (let ((properties (text-properties-at (point)))
882 (start (eww-beginning-of-field))
883 (end (1+ (eww-end-of-field))))
884 (delete-region start end)
885 (insert string
886 (make-string (- (- end start) (length string)) ? ))
887 (set-text-properties start end properties)
888 start))
890 (defun eww-toggle-checkbox ()
891 "Toggle the value of the checkbox under point."
892 (interactive)
893 (let* ((input (get-text-property (point) 'eww-form))
894 (type (plist-get input :type)))
895 (if (equal type "checkbox")
896 (goto-char
898 (if (plist-get input :checked)
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 ;; Radio button. Switch all other buttons off.
905 (let ((name (plist-get input :name)))
906 (save-excursion
907 (dolist (elem (eww-inputs (plist-get input :eww-form)))
908 (when (equal (plist-get (cdr elem) :name) name)
909 (goto-char (car elem))
910 (if (not (eq (cdr elem) input))
911 (progn
912 (plist-put input :checked nil)
913 (eww-update-field eww-form-checkbox-symbol))
914 (plist-put input :checked t)
915 (eww-update-field eww-form-checkbox-selected-symbol)))))
916 (forward-char 1)))))
918 (defun eww-inputs (form)
919 (let ((start (point-min))
920 (inputs nil))
921 (while (and start
922 (< start (point-max)))
923 (when (or (get-text-property start 'eww-form)
924 (setq start (next-single-property-change start 'eww-form)))
925 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
926 form)
927 (push (cons start (get-text-property start 'eww-form))
928 inputs))
929 (setq start (next-single-property-change start 'eww-form))))
930 (nreverse inputs)))
932 (defun eww-input-value (input)
933 (let ((type (plist-get input :type))
934 (value (plist-get input :value)))
935 (cond
936 ((equal type "textarea")
937 (with-temp-buffer
938 (insert value)
939 (goto-char (point-min))
940 (while (re-search-forward "^ +\n\\| +$" nil t)
941 (replace-match "" t t))
942 (buffer-string)))
944 (if (string-match " +\\'" value)
945 (substring value 0 (match-beginning 0))
946 value)))))
948 (defun eww-submit ()
949 "Submit the current form."
950 (interactive)
951 (let* ((this-input (get-text-property (point) 'eww-form))
952 (form (plist-get this-input :eww-form))
953 values next-submit)
954 (dolist (elem (sort (eww-inputs form)
955 (lambda (o1 o2)
956 (< (car o1) (car o2)))))
957 (let* ((input (cdr elem))
958 (input-start (car elem))
959 (name (plist-get input :name)))
960 (when name
961 (cond
962 ((member (plist-get input :type) '("checkbox" "radio"))
963 (when (plist-get input :checked)
964 (push (cons name (plist-get input :value))
965 values)))
966 ((equal (plist-get input :type) "submit")
967 ;; We want the values from buttons if we hit a button if
968 ;; we hit enter on it, or if it's the first button after
969 ;; the field we did hit return on.
970 (when (or (eq input this-input)
971 (and (not (eq input this-input))
972 (null next-submit)
973 (> input-start (point))))
974 (setq next-submit t)
975 (push (cons name (plist-get input :value))
976 values)))
978 (push (cons name (eww-input-value input))
979 values))))))
980 (dolist (elem form)
981 (when (and (consp elem)
982 (eq (car elem) 'hidden))
983 (push (cons (plist-get (cdr elem) :name)
984 (plist-get (cdr elem) :value))
985 values)))
986 (if (and (stringp (cdr (assq :method form)))
987 (equal (downcase (cdr (assq :method form))) "post"))
988 (let ((url-request-method "POST")
989 (url-request-extra-headers
990 '(("Content-Type" . "application/x-www-form-urlencoded")))
991 (url-request-data (mm-url-encode-www-form-urlencoded values)))
992 (eww-browse-url (shr-expand-url (cdr (assq :action form))
993 eww-current-url)))
994 (eww-browse-url
995 (concat
996 (if (cdr (assq :action form))
997 (shr-expand-url (cdr (assq :action form))
998 eww-current-url)
999 eww-current-url)
1001 (mm-url-encode-www-form-urlencoded values))))))
1003 (defun eww-browse-with-external-browser (&optional url)
1004 "Browse the current URL with an external browser.
1005 The browser to used is specified by the `shr-external-browser' variable."
1006 (interactive)
1007 (funcall shr-external-browser (or url eww-current-url)))
1009 (defun eww-follow-link (&optional external mouse-event)
1010 "Browse the URL under point.
1011 If EXTERNAL, browse the URL using `shr-external-browser'."
1012 (interactive (list current-prefix-arg last-nonmenu-event))
1013 (mouse-set-point mouse-event)
1014 (let ((url (get-text-property (point) 'shr-url)))
1015 (cond
1016 ((not url)
1017 (message "No link under point"))
1018 ((string-match "^mailto:" url)
1019 (browse-url-mail url))
1020 (external
1021 (funcall shr-external-browser url))
1022 ;; This is a #target url in the same page as the current one.
1023 ((and (url-target (url-generic-parse-url url))
1024 (eww-same-page-p url eww-current-url))
1025 (eww-save-history)
1026 (eww-display-html 'utf8 url eww-current-dom))
1028 (eww-browse-url url)))))
1030 (defun eww-same-page-p (url1 url2)
1031 "Return non-nil if both URLs represent the same page.
1032 Differences in #targets are ignored."
1033 (let ((obj1 (url-generic-parse-url url1))
1034 (obj2 (url-generic-parse-url url2)))
1035 (setf (url-target obj1) nil)
1036 (setf (url-target obj2) nil)
1037 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1039 (defun eww-copy-page-url ()
1040 (interactive)
1041 (message "%s" eww-current-url)
1042 (kill-new eww-current-url))
1044 (defun eww-download ()
1045 "Download URL under point to `eww-download-directory'."
1046 (interactive)
1047 (let ((url (get-text-property (point) 'shr-url)))
1048 (if (not url)
1049 (message "No URL under point")
1050 (url-retrieve url 'eww-download-callback (list url)))))
1052 (defun eww-download-callback (status url)
1053 (unless (plist-get status :error)
1054 (let* ((obj (url-generic-parse-url url))
1055 (path (car (url-path-and-query obj)))
1056 (file (eww-make-unique-file-name (file-name-nondirectory path)
1057 eww-download-path)))
1058 (write-file file)
1059 (message "Saved %s" file))))
1061 (defun eww-make-unique-file-name (file directory)
1062 (cond
1063 ((zerop (length file))
1064 (setq file "!"))
1065 ((string-match "\\`[.]" file)
1066 (setq file (concat "!" file))))
1067 (let ((count 1))
1068 (while (file-exists-p (expand-file-name file directory))
1069 (setq file
1070 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1071 (format "%s(%d)%s" (match-string 1 file)
1072 count (match-string 2 file))
1073 (format "%s(%d)" file count)))
1074 (setq count (1+ count)))
1075 (expand-file-name file directory)))
1077 ;;; Bookmarks code
1079 (defvar eww-bookmarks nil)
1081 (defun eww-add-bookmark ()
1082 "Add the current page to the bookmarks."
1083 (interactive)
1084 (eww-read-bookmarks)
1085 (dolist (bookmark eww-bookmarks)
1086 (when (equal eww-current-url
1087 (plist-get bookmark :url))
1088 (error "Already bookmarked")))
1089 (if (y-or-n-p "bookmark this page? ")
1090 (progn
1091 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1092 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1093 (push (list :url eww-current-url
1094 :title title
1095 :time (current-time-string))
1096 eww-bookmarks))
1097 (eww-write-bookmarks)
1098 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1100 (defun eww-write-bookmarks ()
1101 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory)
1102 (insert ";; Auto-generated file; don't edit\n")
1103 (pp eww-bookmarks (current-buffer))))
1105 (defun eww-read-bookmarks ()
1106 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory)))
1107 (setq eww-bookmarks
1108 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1109 (with-temp-buffer
1110 (insert-file-contents file)
1111 (read (current-buffer)))))))
1113 (defun eww-list-bookmarks ()
1114 "Display the bookmarks."
1115 (interactive)
1116 (eww-bookmark-prepare)
1117 (pop-to-buffer "*eww bookmarks*"))
1119 (defun eww-bookmark-prepare ()
1120 (eww-read-bookmarks)
1121 (when (null eww-bookmarks)
1122 (error "No bookmarks are defined"))
1123 (set-buffer (get-buffer-create "*eww bookmarks*"))
1124 (eww-bookmark-mode)
1125 (let ((format "%-40s %s")
1126 (inhibit-read-only t)
1127 start url)
1128 (erase-buffer)
1129 (setq header-line-format (concat " " (format format "URL" "Title")))
1130 (dolist (bookmark eww-bookmarks)
1131 (setq start (point))
1132 (setq url (plist-get bookmark :url))
1133 (when (> (length url) 40)
1134 (setq url (substring url 0 40)))
1135 (insert (format format url
1136 (plist-get bookmark :title))
1137 "\n")
1138 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1139 (goto-char (point-min))))
1141 (defvar eww-bookmark-kill-ring nil)
1143 (defun eww-bookmark-kill ()
1144 "Kill the current bookmark."
1145 (interactive)
1146 (let* ((start (line-beginning-position))
1147 (bookmark (get-text-property start 'eww-bookmark))
1148 (inhibit-read-only t))
1149 (unless bookmark
1150 (error "No bookmark on the current line"))
1151 (forward-line 1)
1152 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1153 (delete-region start (point))
1154 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1155 (eww-write-bookmarks)))
1157 (defun eww-bookmark-yank ()
1158 "Yank a previously killed bookmark to the current line."
1159 (interactive)
1160 (unless eww-bookmark-kill-ring
1161 (error "No previously killed bookmark"))
1162 (beginning-of-line)
1163 (let ((inhibit-read-only t)
1164 (start (point))
1165 bookmark)
1166 (insert (pop eww-bookmark-kill-ring))
1167 (setq bookmark (get-text-property start 'eww-bookmark))
1168 (if (= start (point-min))
1169 (push bookmark eww-bookmarks)
1170 (let ((line (count-lines start (point))))
1171 (setcdr (nthcdr (1- line) eww-bookmarks)
1172 (cons bookmark (nthcdr line eww-bookmarks)))))
1173 (eww-write-bookmarks)))
1175 (defun eww-bookmark-quit ()
1176 "Kill the current buffer."
1177 (interactive)
1178 (kill-buffer (current-buffer)))
1180 (defun eww-bookmark-browse ()
1181 "Browse the bookmark under point in eww."
1182 (interactive)
1183 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1184 (unless bookmark
1185 (error "No bookmark on the current line"))
1186 ;; We wish to leave this window, but if it's the only window here,
1187 ;; just let it remain.
1188 (ignore-errors
1189 (delete-window))
1190 (eww-browse-url (plist-get bookmark :url))))
1192 (defun eww-next-bookmark ()
1193 "Go to the next bookmark in the list."
1194 (interactive)
1195 (let ((first nil)
1196 bookmark)
1197 (unless (get-buffer "*eww bookmarks*")
1198 (setq first t)
1199 (eww-bookmark-prepare))
1200 (with-current-buffer (get-buffer "*eww bookmarks*")
1201 (when (and (not first)
1202 (not (eobp)))
1203 (forward-line 1))
1204 (setq bookmark (get-text-property (line-beginning-position)
1205 'eww-bookmark))
1206 (unless bookmark
1207 (error "No next bookmark")))
1208 (eww-browse-url (plist-get bookmark :url))))
1210 (defun eww-previous-bookmark ()
1211 "Go to the previous bookmark in the list."
1212 (interactive)
1213 (let ((first nil)
1214 bookmark)
1215 (unless (get-buffer "*eww bookmarks*")
1216 (setq first t)
1217 (eww-bookmark-prepare))
1218 (with-current-buffer (get-buffer "*eww bookmarks*")
1219 (if first
1220 (goto-char (point-max))
1221 (beginning-of-line))
1222 ;; On the final line.
1223 (when (eolp)
1224 (forward-line -1))
1225 (if (bobp)
1226 (error "No previous bookmark")
1227 (forward-line -1))
1228 (setq bookmark (get-text-property (line-beginning-position)
1229 'eww-bookmark)))
1230 (eww-browse-url (plist-get bookmark :url))))
1232 (defvar eww-bookmark-mode-map
1233 (let ((map (make-sparse-keymap)))
1234 (suppress-keymap map)
1235 (define-key map "q" 'eww-bookmark-quit)
1236 (define-key map [(control k)] 'eww-bookmark-kill)
1237 (define-key map [(control y)] 'eww-bookmark-yank)
1238 (define-key map "\r" 'eww-bookmark-browse)
1239 map))
1241 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1242 "Mode for listing bookmarks.
1244 \\{eww-bookmark-mode-map}"
1245 (buffer-disable-undo)
1246 (setq buffer-read-only t
1247 truncate-lines t))
1249 (provide 'eww)
1251 ;;; eww.el ends here