1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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/>.
27 (eval-when-compile (require 'cl
))
28 (require 'format-spec
)
39 (defcustom eww-header-line-format
"%t: %u"
41 - %t is replaced by the title.
42 - %u is replaced by the URL."
47 (defcustom eww-search-prefix
"https://duckduckgo.com/html/?q="
48 "Prefix URL to search engine"
53 (defcustom eww-download-path
"~/Downloads/"
54 "Path where files will downloaded."
59 (defcustom eww-use-external-browser-for-content-type
60 "\\`\\(video/\\|audio/\\|application/ogg\\)"
61 "Always use external browser for specified content-type."
64 :type
'(choice (const :tag
"Never" nil
)
67 (defcustom eww-form-checkbox-selected-symbol
"[X]"
68 "Symbol used to represent a selected checkbox.
69 See also `eww-form-checkbox-symbol'."
72 :type
'(choice (const "[X]")
73 (const "☒") ; Unicode BALLOT BOX WITH X
74 (const "☑") ; Unicode BALLOT BOX WITH CHECK
77 (defcustom eww-form-checkbox-symbol
"[ ]"
78 "Symbol used to represent a checkbox.
79 See also `eww-form-checkbox-selected-symbol'."
82 :type
'(choice (const "[ ]")
83 (const "☐") ; Unicode BALLOT BOX
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."
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."
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."
110 (defface eww-form-text
111 '((t (:background
"#505050"
113 :box
(:line-width
1))))
114 "Face for eww text inputs."
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
)
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
)))
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
)))
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
)))
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))
179 (mail-header-parse-content-type
180 (or (cdr (assoc "content-type" headers
))
184 (or (cdr (assq 'charset
(cdr content-type
)))
185 (eww-detect-charset (equal (car content-type
)
188 (data-buffer (current-buffer)))
191 (setq eww-current-title
"")
193 ((and eww-use-external-browser-for-content-type
194 (string-match-p eww-use-external-browser-for-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
))
201 (eww-update-header-line-format))
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 ()
211 (goto-char (point-min))
212 (while (and (not (eobp))
214 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
215 (push (cons (downcase (match-string 1))
223 (defun eww-detect-charset (html-p)
224 (let ((case-fold-search t
)
228 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
232 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
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
)
243 (decode-coding-region (point) (point-max) charset
)
244 (coding-system-error nil
)))
248 'base
(list (cons 'href url
))
249 (libxml-parse-html-region (point) (point-max))))))
251 (setq eww-current-dom document
)
252 (let ((inhibit-read-only t
)
253 (after-change-functions 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
)
265 (shr-insert-document document
)
270 (let ((point (next-single-property-change
271 (point-min) 'shr-target-id
)))
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
))
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
296 ("start" . eww-start-url
)
297 ("home" . eww-home-url
)
298 ("contents" . eww-contents-url
)
299 ("up" . eww-up-url
)))))
302 (set (cdr where
) (cdr href
)))))
304 (defun eww-tag-link (cont)
305 (eww-handle-link cont
)
308 (defun eww-tag-a (cont)
309 (eww-handle-link cont
)
310 (let ((start (point)))
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
319 ;; FIXME? Title can be blank. Default to, eg, last component
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
"")
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
)
337 (bgcolor (cdr (assq :bgcolor cont
)))
338 (shr-stylesheet (list (cons 'color fgcolor
)
339 (cons 'background-color bgcolor
))))
341 (eww-colorize-region start
(point) fgcolor bgcolor
)))
343 (defun eww-colorize-region (start end fg
&optional bg
)
345 (let ((new-colors (shr-color-check fg bg
)))
348 (add-face-text-property start end
349 (list :foreground
(cadr new-colors
))
352 (add-face-text-property start end
353 (list :background
(car new-colors
))
356 (defun eww-display-raw ()
357 (let ((data (buffer-substring (point) (point-max))))
359 (let ((inhibit-read-only t
))
361 (goto-char (point-min))))
363 (defun eww-display-image ()
364 (let ((data (shr-parse-image-data)))
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
))
375 (unless (eq major-mode
'eww-mode
)
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 ""
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]))
420 (define-derived-mode eww-mode nil "eww"
421 "Mode for browsing the web.
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
440 :text (buffer-string))
444 (defun eww-browse-url (url &optional _new-window)
445 (when (and (equal major-mode 'eww-mode)
450 (defun eww-back-url ()
451 "Go to the previously displayed page."
453 (when (>= eww-history-position (length eww-history))
454 (error "No previous page"))
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."
462 (when (zerop eww-history-position)
463 (error "No next page"))
465 (eww-restore-history (elt eww-history (1- eww-history-position))))
467 (defun eww-restore-history (elem)
468 (let ((inhibit-read-only t))
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>
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>
492 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
493 (error "No `previous' on this page")))
496 "Go to the page marked `up'.
497 A page is marked `up' if rel=\"up\" appears in a <link>
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."
509 (let ((best-url (or eww-start-url
513 (eww-browse-url (shr-expand-url best-url eww-current-url))
514 (error "No `top' for this page"))))
517 "Reload the current page."
519 (url-retrieve eww-current-url 'eww-render
520 (list eww-current-url (point))))
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)
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)
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)
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
)
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
)
565 (defun eww-beginning-of-text ()
566 "Move to the start of the input field."
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."
573 (goto-char (eww-end-of-field))
574 (let ((start (eww-beginning-of-field)))
575 (while (and (equal (following-char) ?
)
578 (when (> (point) start
)
581 (defun eww-beginning-of-field ()
585 ((not (eq (get-text-property (point) 'eww-form
)
586 (get-text-property (1- (point)) 'eww-form
)))
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)
598 (list (assq :method cont
)
599 (assq :action cont
)))
601 (shr-ensure-paragraph)
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
))))
614 (if (zerop (length 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
623 :name
(cdr (assq :name cont
))))
624 (put-text-property start
(point) 'keymap eww-submit-map
)
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
)
642 (defun eww-form-text (cont)
643 (let ((start (point))
644 (type (downcase (or (cdr (assq :type cont
))
646 (value (or (cdr (assq :value cont
)) ""))
647 (width (string-to-number
648 (or (cdr (assq :size cont
))
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
660 :name
(cdr (assq :name cont
))))
663 (defconst eww-text-input-types
'("text" "password" "textarea"
664 "color" "date" "datetime" "datetime-local"
665 "email" "month" "number" "search" "tel"
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
)))
675 (member type eww-text-input-types
))
678 ;; Delete some space at the end.
681 (if (equal type
"textarea")
682 (1- (line-end-position))
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
)))
694 (if (equal type
"textarea")
695 (1- (line-end-position))
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
))
718 (width (string-to-number
719 (or (cdr (assq :cols cont
))
725 (when (< (count-lines start
(point)) lines
)
726 (dotimes (i (- lines
(count-lines start
(point))))
728 (setq end
(point-marker))
730 (while (< (point) end
)
732 (let ((pad (- width
(- (point) (line-beginning-position)))))
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
)
740 (put-text-property start
(point) 'eww-form
741 (list :eww-form eww-form
744 :name
(cdr (assq :name cont
))))))
746 (defun eww-tag-input (cont)
747 (let ((type (downcase (or (cdr (assq :type cont
))
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.
761 (or (not (consp (car form
)))
762 (not (eq (caar form
) 'hidden
))
763 (not (equal (plist-get (cdr (car form
)) :name
)
765 (setq form
(cdr form
)))
767 (nconc eww-form
(list
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
))
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
))))))
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
)))
797 :value
(cdr (assq :value
(cdr elem
)))
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
)))))
806 (let ((selected (eww-select-display menu
)))
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
))
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
))))
824 (defun eww-change-select ()
825 "Change the value of the select drop-down menu under point."
827 (let* ((input (get-text-property (point) 'eww-form
))
828 (completion-ignore-case t
)
831 (mapcar (lambda (elem)
833 (eq (car elem
) 'item
)
834 (cons (plist-get (cdr elem
) :display
)
835 (plist-get (cdr elem
) :value
))))
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
)))
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
)
850 (make-string (- (- end start
) (length string
)) ?
))
851 (set-text-properties start end properties
)
854 (defun eww-toggle-checkbox ()
855 "Toggle the value of the checkbox under point."
857 (let* ((input (get-text-property (point) 'eww-form
))
858 (type (plist-get input
:type
)))
859 (if (equal type
"checkbox")
862 (if (plist-get input
:checked
)
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
)))
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
))
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
)))))
882 (defun eww-inputs (form)
883 (let ((start (point-min))
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
)
891 (push (cons start
(get-text-property start
'eww-form
))
893 (setq start
(next-single-property-change start
'eww-form
))))
896 (defun eww-input-value (input)
897 (let ((type (plist-get input
:type
))
898 (value (plist-get input
:value
)))
900 ((equal type
"textarea")
903 (goto-char (point-min))
904 (while (re-search-forward "^ +\n\\| +$" nil t
)
905 (replace-match "" t t
))
908 (if (string-match " +\\'" value
)
909 (substring value
0 (match-beginning 0))
913 "Submit the current form."
915 (let* ((this-input (get-text-property (point) 'eww-form
))
916 (form (plist-get this-input
:eww-form
))
918 (dolist (elem (sort (eww-inputs form
)
920 (< (car o1
) (car o2
)))))
921 (let* ((input (cdr elem
))
922 (input-start (car elem
))
923 (name (plist-get input
:name
)))
926 ((member (plist-get input
:type
) '("checkbox" "radio"))
927 (when (plist-get input
:checked
)
928 (push (cons name
(plist-get input
:value
))
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
))
937 (> input-start
(point))))
939 (push (cons name
(plist-get input
:value
))
942 (push (cons name
(eww-input-value input
))
945 (when (and (consp elem
)
946 (eq (car elem
) 'hidden
))
947 (push (cons (plist-get (cdr elem
) :name
)
948 (plist-get (cdr elem
) :value
))
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
))
960 (if (cdr (assq :action form
))
961 (shr-expand-url (cdr (assq :action form
))
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."
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
)))
981 (message "No link under point"))
982 ((string-match "^mailto:" url
)
983 (browse-url-mail url
))
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
))
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 ()
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'."
1011 (let ((url (get-text-property (point) 'shr-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
)))
1023 (message "Saved %s" file
))))
1025 (defun eww-make-unique-file-name (file directory
)
1027 ((zerop (length file
))
1029 ((string-match "\\`[.]" file
)
1030 (setq file
(concat "!" file
))))
1032 (while (file-exists-p (expand-file-name file directory
))
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
)))
1043 (defvar eww-bookmarks nil
)
1045 (defun eww-add-bookmark ()
1046 "Add the current page to the bookmarks."
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? ")
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
1059 :time
(current-time-string))
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
)))
1072 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1074 (insert-file-contents file
)
1075 (read (current-buffer)))))))
1077 (defun eww-list-bookmarks ()
1078 "Display the bookmarks."
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*"))
1089 (let ((format "%-40s %s")
1090 (inhibit-read-only t
)
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
))
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."
1110 (let* ((start (line-beginning-position))
1111 (bookmark (get-text-property start
'eww-bookmark
))
1112 (inhibit-read-only t
))
1114 (error "No bookmark on the current line"))
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."
1124 (unless eww-bookmark-kill-ring
1125 (error "No previously killed bookmark"))
1127 (let ((inhibit-read-only t
)
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."
1142 (kill-buffer (current-buffer)))
1144 (defun eww-bookmark-browse ()
1145 "Browse the bookmark under point in eww."
1147 (let ((bookmark (get-text-property (line-beginning-position) 'eww-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.
1154 (eww-browse-url (plist-get bookmark
:url
))))
1156 (defun eww-next-bookmark ()
1157 "Go to the next bookmark in the list."
1161 (unless (get-buffer "*eww bookmarks*")
1163 (eww-bookmark-prepare))
1164 (with-current-buffer (get-buffer "*eww bookmarks*")
1165 (when (and (not first
)
1168 (setq bookmark
(get-text-property (line-beginning-position)
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."
1179 (unless (get-buffer "*eww bookmarks*")
1181 (eww-bookmark-prepare))
1182 (with-current-buffer (get-buffer "*eww bookmarks*")
1184 (goto-char (point-max))
1185 (beginning-of-line))
1186 ;; On the final line.
1190 (error "No previous bookmark")
1192 (setq bookmark
(get-text-property (line-beginning-position)
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
)
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
1215 ;;; eww.el ends here