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-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
)
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
)))
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)
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
)))
173 (setq url redirect
)))
174 (setq-local eww-next-url nil
)
175 (setq-local eww-previous-url nil
)
176 (setq-local eww-up-url nil
)
177 (setq-local eww-home-url nil
)
178 (setq-local eww-start-url nil
)
179 (setq-local eww-contents-url nil
)
180 (let* ((headers (eww-parse-headers))
182 (mail-header-parse-content-type
183 (or (cdr (assoc "content-type" headers
))
187 (or (cdr (assq 'charset
(cdr content-type
)))
188 (eww-detect-charset (equal (car content-type
)
191 (data-buffer (current-buffer)))
194 (setq eww-current-title
"")
196 ((and eww-use-external-browser-for-content-type
197 (string-match-p eww-use-external-browser-for-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
))
204 (eww-update-header-line-format))
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 ()
214 (goto-char (point-min))
215 (while (and (not (eobp))
217 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
218 (push (cons (downcase (match-string 1))
226 (defun eww-detect-charset (html-p)
227 (let ((case-fold-search t
)
231 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
235 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
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
)
246 (decode-coding-region (point) (point-max) charset
)
247 (coding-system-error nil
)))
251 'base
(list (cons 'href url
))
252 (libxml-parse-html-region (point) (point-max))))))
253 (setq eww-current-source
(buffer-substring (point) (point-max)))
255 (setq eww-current-dom document
)
256 (let ((inhibit-read-only t
)
257 (after-change-functions 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
)
269 (shr-insert-document document
)
274 (goto-char (point-min))
275 (let ((point (next-single-property-change
276 (point-min) 'shr-target-id
)))
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
))
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
301 ("start" . eww-start-url
)
302 ("home" . eww-home-url
)
303 ("contents" . eww-contents-url
)
304 ("up" . eww-up-url
)))))
307 (set (cdr where
) (cdr href
)))))
309 (defun eww-tag-link (cont)
310 (eww-handle-link cont
)
313 (defun eww-tag-a (cont)
314 (eww-handle-link cont
)
315 (let ((start (point)))
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
324 ;; FIXME? Title can be blank. Default to, eg, last component
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
"")
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
)
342 (bgcolor (cdr (assq :bgcolor cont
)))
343 (shr-stylesheet (list (cons 'color fgcolor
)
344 (cons 'background-color bgcolor
))))
346 (eww-colorize-region start
(point) fgcolor bgcolor
)))
348 (defun eww-colorize-region (start end fg
&optional bg
)
350 (let ((new-colors (shr-color-check fg bg
)))
353 (add-face-text-property start end
354 (list :foreground
(cadr new-colors
))
357 (add-face-text-property start end
358 (list :background
(car new-colors
))
361 (defun eww-display-raw ()
362 (let ((data (buffer-substring (point) (point-max))))
364 (let ((inhibit-read-only t
))
366 (goto-char (point-min))))
368 (defun eww-display-image ()
369 (let ((data (shr-parse-image-data)))
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
))
380 (unless (eq major-mode
'eww-mode
)
383 (defun eww-view-source ()
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 (fboundp 'html-mode
)
396 (let ((map (make-sparse-keymap)))
397 (suppress-keymap map
)
398 (define-key map
"q" 'quit-window
)
399 (define-key map
"g" 'eww-reload
)
400 (define-key map
[tab] 'shr-next-link)
401 (define-key map [backtab] 'shr-previous-link)
402 (define-key map [delete] 'scroll-down-command)
403 (define-key map [?\S-\ ] 'scroll-down-command)
404 (define-key map "\177" 'scroll-down-command)
405 (define-key map " " 'scroll-up-command)
406 (define-key map "l" 'eww-back-url)
407 (define-key map "r" 'eww-forward-url)
408 (define-key map "n" 'eww-next-url)
409 (define-key map "p" 'eww-previous-url)
410 (define-key map "u" 'eww-up-url)
411 (define-key map "t" 'eww-top-url)
412 (define-key map "&" 'eww-browse-with-external-browser)
413 (define-key map "d" 'eww-download)
414 (define-key map "w" 'eww-copy-page-url)
415 (define-key map "C" 'url-cookie-list)
416 (define-key map "v" 'eww-view-source)
417 (define-key map "H" 'eww-list-histories)
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 ""
427 ["Close browser" quit-window 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 ["List histories" eww-list-histories t]
438 ["Add bookmark" eww-add-bookmark t]
439 ["List bookmarks" eww-list-bookmarks t]
440 ["List cookies" url-cookie-list t]))
443 (defvar eww-tool-bar-map
444 (let ((map (make-sparse-keymap)))
445 (dolist (tool-bar-item
446 '((eww-quit . "close")
447 (eww-reload . "refresh")
448 (eww-back-url . "left-arrow")
449 (eww-forward-url . "right-arrow")
450 (eww-view-source . "show")
451 (eww-copy-page-url . "copy")
452 (eww-add-bookmark . "bookmark_add"))) ;; ...
453 (tool-bar-local-item-from-menu
454 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
456 "Tool bar for `eww-mode'.")
458 (define-derived-mode eww-mode nil "eww"
459 "Mode for browsing the web.
462 ;; FIXME? This seems a strange default.
463 (setq-local eww-current-url 'author)
464 (setq-local eww-current-dom nil)
465 (setq-local eww-current-source nil)
466 (setq-local browse-url-browser-function 'eww-browse-url)
467 (setq-local after-change-functions 'eww-process-text-input)
468 (setq-local eww-history nil)
469 (setq-local eww-history-position 0)
470 (when (boundp 'tool-bar-map)
471 (setq-local tool-bar-map eww-tool-bar-map))
472 (buffer-disable-undo)
473 ;;(setq buffer-read-only t)
477 (defun eww-browse-url (url &optional _new-window)
478 (when (and (equal major-mode 'eww-mode)
483 (defun eww-back-url ()
484 "Go to the previously displayed page."
486 (when (>= eww-history-position (length eww-history))
487 (user-error "No previous page"))
489 (setq eww-history-position (+ eww-history-position 2))
490 (eww-restore-history (elt eww-history (1- eww-history-position))))
492 (defun eww-forward-url ()
493 "Go to the next displayed page."
495 (when (zerop eww-history-position)
496 (user-error "No next page"))
498 (eww-restore-history (elt eww-history (1- eww-history-position))))
500 (defun eww-restore-history (elem)
501 (let ((inhibit-read-only t))
503 (insert (plist-get elem :text))
504 (setq eww-current-source (plist-get elem :source))
505 (setq eww-current-dom (plist-get elem :dom))
506 (goto-char (plist-get elem :point))
507 (setq eww-current-url (plist-get elem :url)
508 eww-current-title (plist-get elem :title))
509 (eww-update-header-line-format)))
511 (defun eww-next-url ()
512 "Go to the page marked `next'.
513 A page is marked `next' if rel=\"next\" appears in a <link>
517 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
518 (user-error "No `next' on this page")))
520 (defun eww-previous-url ()
521 "Go to the page marked `previous'.
522 A page is marked `previous' if rel=\"previous\" appears in a <link>
526 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
527 (user-error "No `previous' on this page")))
530 "Go to the page marked `up'.
531 A page is marked `up' if rel=\"up\" appears in a <link>
535 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
536 (user-error "No `up' on this page")))
538 (defun eww-top-url ()
539 "Go to the page marked `top'.
540 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
541 appears in a <link> or <a> tag."
543 (let ((best-url (or eww-start-url
547 (eww-browse-url (shr-expand-url best-url eww-current-url))
548 (user-error "No `top' for this page"))))
551 "Reload the current page."
553 (url-retrieve eww-current-url 'eww-render
554 (list eww-current-url (point))))
558 (defvar eww-form nil)
560 (defvar eww-submit-map
561 (let ((map (make-sparse-keymap)))
562 (define-key map "\r" 'eww-submit)
563 (define-key map [(control c) (control c)] 'eww-submit)
566 (defvar eww-checkbox-map
567 (let ((map (make-sparse-keymap)))
568 (define-key map [space] 'eww-toggle-checkbox)
569 (define-key map "\r" 'eww-toggle-checkbox)
570 (define-key map [(control c) (control c)] 'eww-submit)
574 (let ((map (make-keymap)))
575 (set-keymap-parent map text-mode-map)
576 (define-key map "\r" 'eww-submit)
577 (define-key map [(control a)] 'eww-beginning-of-text)
578 (define-key map [(control c) (control c)] 'eww-submit)
579 (define-key map [(control e)] 'eww-end-of-text)
580 (define-key map [tab] 'shr-next-link
)
581 (define-key map
[backtab] 'shr-previous-link)
584 (defvar eww-textarea-map
585 (let ((map (make-keymap)))
586 (set-keymap-parent map text-mode-map)
587 (define-key map "\r" 'forward-line)
588 (define-key map [(control c) (control c)] 'eww-submit)
589 (define-key map [tab] 'shr-next-link)
590 (define-key map [backtab] 'shr-previous-link
)
593 (defvar eww-select-map
594 (let ((map (make-sparse-keymap)))
595 (define-key map
"\r" 'eww-change-select
)
596 (define-key map
[(control c
) (control c
)] 'eww-submit
)
599 (defun eww-beginning-of-text ()
600 "Move to the start of the input field."
602 (goto-char (eww-beginning-of-field)))
604 (defun eww-end-of-text ()
605 "Move to the end of the text in the input field."
607 (goto-char (eww-end-of-field))
608 (let ((start (eww-beginning-of-field)))
609 (while (and (equal (following-char) ?
)
612 (when (> (point) start
)
615 (defun eww-beginning-of-field ()
619 ((not (eq (get-text-property (point) 'eww-form
)
620 (get-text-property (1- (point)) 'eww-form
)))
623 (previous-single-property-change
624 (point) 'eww-form nil
(point-min)))))
626 (defun eww-end-of-field ()
627 (1- (next-single-property-change
628 (point) 'eww-form nil
(point-max))))
630 (defun eww-tag-form (cont)
632 (list (assq :method cont
)
633 (assq :action cont
)))
635 (shr-ensure-paragraph)
640 (when (> (point) start
)
641 (put-text-property start
(1+ start
)
642 'eww-form eww-form
))))
644 (defun eww-form-submit (cont)
645 (let ((start (point))
646 (value (cdr (assq :value cont
))))
648 (if (zerop (length value
))
652 (add-face-text-property start
(point) 'eww-form-submit
)
653 (put-text-property start
(point) 'eww-form
654 (list :eww-form eww-form
657 :name
(cdr (assq :name cont
))))
658 (put-text-property start
(point) 'keymap eww-submit-map
)
661 (defun eww-form-checkbox (cont)
662 (let ((start (point)))
663 (if (cdr (assq :checked cont
))
664 (insert eww-form-checkbox-selected-symbol
)
665 (insert eww-form-checkbox-symbol
))
666 (add-face-text-property start
(point) 'eww-form-checkbox
)
667 (put-text-property start
(point) 'eww-form
668 (list :eww-form eww-form
669 :value
(cdr (assq :value cont
))
670 :type
(downcase (cdr (assq :type cont
)))
671 :checked
(cdr (assq :checked cont
))
672 :name
(cdr (assq :name cont
))))
673 (put-text-property start
(point) 'keymap eww-checkbox-map
)
676 (defun eww-form-text (cont)
677 (let ((start (point))
678 (type (downcase (or (cdr (assq :type cont
))
680 (value (or (cdr (assq :value cont
)) ""))
681 (width (string-to-number
682 (or (cdr (assq :size cont
))
685 (when (< (length value
) width
)
686 (insert (make-string (- width
(length value
)) ?
)))
687 (put-text-property start
(point) 'face
'eww-form-text
)
688 (put-text-property start
(point) 'local-map eww-text-map
)
689 (put-text-property start
(point) 'inhibit-read-only t
)
690 (put-text-property start
(point) 'eww-form
691 (list :eww-form eww-form
694 :name
(cdr (assq :name cont
))))
697 (defconst eww-text-input-types
'("text" "password" "textarea"
698 "color" "date" "datetime" "datetime-local"
699 "email" "month" "number" "search" "tel"
701 "List of input types which represent a text input.
702 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
704 (defun eww-process-text-input (beg end length
)
705 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
706 (properties (text-properties-at end
))
707 (type (plist-get form
:type
)))
709 (member type eww-text-input-types
))
712 ;; Delete some space at the end.
715 (if (equal type
"textarea")
716 (1- (line-end-position))
718 (let ((new (- end beg
)))
719 (while (and (> new
0)
720 (eql (following-char) ?
))
721 (delete-region (point) (1+ (point)))
722 (setq new
(1- new
))))
723 (set-text-properties beg end properties
)))
728 (if (equal type
"textarea")
729 (1- (line-end-position))
731 (let ((start (point)))
732 (insert (make-string length ?
))
733 (set-text-properties start
(point) properties
)))))
734 (let ((value (buffer-substring-no-properties
735 (eww-beginning-of-field)
736 (eww-end-of-field))))
737 (when (string-match " +\\'" value
)
738 (setq value
(substring value
0 (match-beginning 0))))
739 (plist-put form
:value value
)
740 (when (equal type
"password")
741 ;; Display passwords as asterisks.
742 (let ((start (eww-beginning-of-field)))
743 (put-text-property start
(+ start
(length value
))
744 'display
(make-string (length value
) ?
*))))))))
746 (defun eww-tag-textarea (cont)
747 (let ((start (point))
748 (value (or (cdr (assq :value cont
)) ""))
749 (lines (string-to-number
750 (or (cdr (assq :rows cont
))
752 (width (string-to-number
753 (or (cdr (assq :cols cont
))
759 (when (< (count-lines start
(point)) lines
)
760 (dotimes (i (- lines
(count-lines start
(point))))
762 (setq end
(point-marker))
764 (while (< (point) end
)
766 (let ((pad (- width
(- (point) (line-beginning-position)))))
768 (insert (make-string pad ?
))))
769 (add-face-text-property (line-beginning-position)
770 (point) 'eww-form-text
)
771 (put-text-property (line-beginning-position) (point)
772 'local-map eww-textarea-map
)
774 (put-text-property start
(point) 'eww-form
775 (list :eww-form eww-form
778 :name
(cdr (assq :name cont
))))))
780 (defun eww-tag-input (cont)
781 (let ((type (downcase (or (cdr (assq :type cont
))
785 ((or (equal type
"checkbox")
786 (equal type
"radio"))
787 (eww-form-checkbox cont
))
788 ((equal type
"submit")
789 (eww-form-submit cont
))
790 ((equal type
"hidden")
791 (let ((form eww-form
)
792 (name (cdr (assq :name cont
))))
793 ;; Don't add <input type=hidden> elements repeatedly.
795 (or (not (consp (car form
)))
796 (not (eq (caar form
) 'hidden
))
797 (not (equal (plist-get (cdr (car form
)) :name
)
799 (setq form
(cdr form
)))
801 (nconc eww-form
(list
804 :value
(cdr (assq :value cont
))))))))
806 (eww-form-text cont
)))
807 (unless (= start
(point))
808 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
810 (defun eww-tag-select (cont)
811 (shr-ensure-paragraph)
812 (let ((menu (list :name
(cdr (assq :name cont
))
818 (if (eq (car (car cont
)) 'optgroup
)
819 (dolist (groupelem cont
)
820 (unless (cdr (assq :disabled
(cdr groupelem
)))
821 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
823 (dolist (elem opelem
)
824 (when (eq (car elem
) 'option
)
825 (when (cdr (assq :selected
(cdr elem
)))
826 (nconc menu
(list :value
827 (cdr (assq :value
(cdr elem
))))))
828 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
829 (setq max
(max max
(length display
)))
831 :value
(cdr (assq :value
(cdr elem
)))
835 (setq options
(nreverse options
))
836 ;; If we have no selected values, default to the first value.
837 (unless (plist-get menu
:value
)
838 (nconc menu
(list :value
(nth 2 (car options
)))))
840 (let ((selected (eww-select-display menu
)))
842 (make-string (- max
(length selected
)) ?
)))
843 (put-text-property start
(point) 'eww-form menu
)
844 (add-face-text-property start
(point) 'eww-form-select
)
845 (put-text-property start
(point) 'keymap eww-select-map
)
846 (shr-ensure-paragraph))))
848 (defun eww-select-display (select)
849 (let ((value (plist-get select
:value
))
851 (dolist (elem select
)
852 (when (and (consp elem
)
853 (eq (car elem
) 'item
)
854 (equal value
(plist-get (cdr elem
) :value
)))
855 (setq display
(plist-get (cdr elem
) :display
))))
858 (defun eww-change-select ()
859 "Change the value of the select drop-down menu under point."
861 (let* ((input (get-text-property (point) 'eww-form
))
862 (completion-ignore-case t
)
865 (mapcar (lambda (elem)
867 (eq (car elem
) 'item
)
868 (cons (plist-get (cdr elem
) :display
)
869 (plist-get (cdr elem
) :value
))))
872 (completing-read "Change value: " options nil
'require-match
))
873 (inhibit-read-only t
))
874 (plist-put input
:value
(cdr (assoc-string display options t
)))
876 (eww-update-field display
))))
878 (defun eww-update-field (string)
879 (let ((properties (text-properties-at (point)))
880 (start (eww-beginning-of-field))
881 (end (1+ (eww-end-of-field))))
882 (delete-region start end
)
884 (make-string (- (- end start
) (length string
)) ?
))
885 (set-text-properties start end properties
)
888 (defun eww-toggle-checkbox ()
889 "Toggle the value of the checkbox under point."
891 (let* ((input (get-text-property (point) 'eww-form
))
892 (type (plist-get input
:type
)))
893 (if (equal type
"checkbox")
896 (if (plist-get input
:checked
)
898 (plist-put input
:checked nil
)
899 (eww-update-field eww-form-checkbox-symbol
))
900 (plist-put input
:checked t
)
901 (eww-update-field eww-form-checkbox-selected-symbol
))))
902 ;; Radio button. Switch all other buttons off.
903 (let ((name (plist-get input
:name
)))
905 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
906 (when (equal (plist-get (cdr elem
) :name
) name
)
907 (goto-char (car elem
))
908 (if (not (eq (cdr elem
) input
))
910 (plist-put input
:checked nil
)
911 (eww-update-field eww-form-checkbox-symbol
))
912 (plist-put input
:checked t
)
913 (eww-update-field eww-form-checkbox-selected-symbol
)))))
916 (defun eww-inputs (form)
917 (let ((start (point-min))
920 (< start
(point-max)))
921 (when (or (get-text-property start
'eww-form
)
922 (setq start
(next-single-property-change start
'eww-form
)))
923 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
925 (push (cons start
(get-text-property start
'eww-form
))
927 (setq start
(next-single-property-change start
'eww-form
))))
930 (defun eww-input-value (input)
931 (let ((type (plist-get input
:type
))
932 (value (plist-get input
:value
)))
934 ((equal type
"textarea")
937 (goto-char (point-min))
938 (while (re-search-forward "^ +\n\\| +$" nil t
)
939 (replace-match "" t t
))
942 (if (string-match " +\\'" value
)
943 (substring value
0 (match-beginning 0))
947 "Submit the current form."
949 (let* ((this-input (get-text-property (point) 'eww-form
))
950 (form (plist-get this-input
:eww-form
))
952 (dolist (elem (sort (eww-inputs form
)
954 (< (car o1
) (car o2
)))))
955 (let* ((input (cdr elem
))
956 (input-start (car elem
))
957 (name (plist-get input
:name
)))
960 ((member (plist-get input
:type
) '("checkbox" "radio"))
961 (when (plist-get input
:checked
)
962 (push (cons name
(plist-get input
:value
))
964 ((equal (plist-get input
:type
) "submit")
965 ;; We want the values from buttons if we hit a button if
966 ;; we hit enter on it, or if it's the first button after
967 ;; the field we did hit return on.
968 (when (or (eq input this-input
)
969 (and (not (eq input this-input
))
971 (> input-start
(point))))
973 (push (cons name
(plist-get input
:value
))
976 (push (cons name
(eww-input-value input
))
979 (when (and (consp elem
)
980 (eq (car elem
) 'hidden
))
981 (push (cons (plist-get (cdr elem
) :name
)
982 (plist-get (cdr elem
) :value
))
984 (if (and (stringp (cdr (assq :method form
)))
985 (equal (downcase (cdr (assq :method form
))) "post"))
986 (let ((url-request-method "POST")
987 (url-request-extra-headers
988 '(("Content-Type" .
"application/x-www-form-urlencoded")))
989 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
990 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
994 (if (cdr (assq :action form
))
995 (shr-expand-url (cdr (assq :action form
))
999 (mm-url-encode-www-form-urlencoded values
))))))
1001 (defun eww-browse-with-external-browser (&optional url
)
1002 "Browse the current URL with an external browser.
1003 The browser to used is specified by the `shr-external-browser' variable."
1005 (funcall shr-external-browser
(or url eww-current-url
)))
1007 (defun eww-follow-link (&optional external mouse-event
)
1008 "Browse the URL under point.
1009 If EXTERNAL, browse the URL using `shr-external-browser'."
1010 (interactive (list current-prefix-arg last-nonmenu-event
))
1011 (mouse-set-point mouse-event
)
1012 (let ((url (get-text-property (point) 'shr-url
)))
1015 (message "No link under point"))
1016 ((string-match "^mailto:" url
)
1017 (browse-url-mail url
))
1019 (funcall shr-external-browser url
))
1020 ;; This is a #target url in the same page as the current one.
1021 ((and (url-target (url-generic-parse-url url
))
1022 (eww-same-page-p url eww-current-url
))
1024 (eww-display-html 'utf8 url eww-current-dom
))
1026 (eww-browse-url url
)))))
1028 (defun eww-same-page-p (url1 url2
)
1029 "Return non-nil if both URLs represent the same page.
1030 Differences in #targets are ignored."
1031 (let ((obj1 (url-generic-parse-url url1
))
1032 (obj2 (url-generic-parse-url url2
)))
1033 (setf (url-target obj1
) nil
)
1034 (setf (url-target obj2
) nil
)
1035 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1037 (defun eww-copy-page-url ()
1039 (message "%s" eww-current-url
)
1040 (kill-new eww-current-url
))
1042 (defun eww-download ()
1043 "Download URL under point to `eww-download-directory'."
1045 (let ((url (get-text-property (point) 'shr-url
)))
1047 (message "No URL under point")
1048 (url-retrieve url
'eww-download-callback
(list url
)))))
1050 (defun eww-download-callback (status url
)
1051 (unless (plist-get status
:error
)
1052 (let* ((obj (url-generic-parse-url url
))
1053 (path (car (url-path-and-query obj
)))
1054 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1055 eww-download-path
)))
1057 (message "Saved %s" file
))))
1059 (defun eww-make-unique-file-name (file directory
)
1061 ((zerop (length file
))
1063 ((string-match "\\`[.]" file
)
1064 (setq file
(concat "!" file
))))
1066 (while (file-exists-p (expand-file-name file directory
))
1068 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1069 (format "%s(%d)%s" (match-string 1 file
)
1070 count
(match-string 2 file
))
1071 (format "%s(%d)" file count
)))
1072 (setq count
(1+ count
)))
1073 (expand-file-name file directory
)))
1077 (defvar eww-bookmarks nil
)
1079 (defun eww-add-bookmark ()
1080 "Add the current page to the bookmarks."
1082 (eww-read-bookmarks)
1083 (dolist (bookmark eww-bookmarks
)
1084 (when (equal eww-current-url
1085 (plist-get bookmark
:url
))
1086 (user-error "Already bookmarked")))
1087 (if (y-or-n-p "bookmark this page? ")
1089 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1090 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1091 (push (list :url eww-current-url
1093 :time
(current-time-string))
1095 (eww-write-bookmarks)
1096 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1098 (defun eww-write-bookmarks ()
1099 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1100 (insert ";; Auto-generated file; don't edit\n")
1101 (pp eww-bookmarks
(current-buffer))))
1103 (defun eww-read-bookmarks ()
1104 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1106 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1108 (insert-file-contents file
)
1109 (read (current-buffer)))))))
1111 (defun eww-list-bookmarks ()
1112 "Display the bookmarks."
1114 (eww-bookmark-prepare)
1115 (pop-to-buffer "*eww bookmarks*"))
1117 (defun eww-bookmark-prepare ()
1118 (eww-read-bookmarks)
1119 (unless eww-bookmarks
1120 (user-error "No bookmarks are defined"))
1121 (set-buffer (get-buffer-create "*eww bookmarks*"))
1123 (let ((format "%-40s %s")
1124 (inhibit-read-only t
)
1127 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1128 (dolist (bookmark eww-bookmarks
)
1129 (setq start
(point))
1130 (setq url
(plist-get bookmark
:url
))
1131 (when (> (length url
) 40)
1132 (setq url
(substring url
0 40)))
1133 (insert (format format url
1134 (plist-get bookmark
:title
))
1136 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1137 (goto-char (point-min))))
1139 (defvar eww-bookmark-kill-ring nil
)
1141 (defun eww-bookmark-kill ()
1142 "Kill the current bookmark."
1144 (let* ((start (line-beginning-position))
1145 (bookmark (get-text-property start
'eww-bookmark
))
1146 (inhibit-read-only t
))
1148 (user-error "No bookmark on the current line"))
1150 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1151 (delete-region start
(point))
1152 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1153 (eww-write-bookmarks)))
1155 (defun eww-bookmark-yank ()
1156 "Yank a previously killed bookmark to the current line."
1158 (unless eww-bookmark-kill-ring
1159 (user-error "No previously killed bookmark"))
1161 (let ((inhibit-read-only t
)
1164 (insert (pop eww-bookmark-kill-ring
))
1165 (setq bookmark
(get-text-property start
'eww-bookmark
))
1166 (if (= start
(point-min))
1167 (push bookmark eww-bookmarks
)
1168 (let ((line (count-lines start
(point))))
1169 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1170 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1171 (eww-write-bookmarks)))
1173 (defun eww-bookmark-quit ()
1174 "Kill the current buffer."
1176 (kill-buffer (current-buffer)))
1178 (defun eww-bookmark-browse ()
1179 "Browse the bookmark under point in eww."
1181 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1183 (user-error "No bookmark on the current line"))
1184 ;; We wish to leave this window, but if it's the only window here,
1185 ;; just let it remain.
1188 (eww-browse-url (plist-get bookmark
:url
))))
1190 (defun eww-next-bookmark ()
1191 "Go to the next bookmark in the list."
1195 (unless (get-buffer "*eww bookmarks*")
1197 (eww-bookmark-prepare))
1198 (with-current-buffer (get-buffer "*eww bookmarks*")
1199 (when (and (not first
)
1202 (setq bookmark
(get-text-property (line-beginning-position)
1205 (user-error "No next bookmark")))
1206 (eww-browse-url (plist-get bookmark
:url
))))
1208 (defun eww-previous-bookmark ()
1209 "Go to the previous bookmark in the list."
1213 (unless (get-buffer "*eww bookmarks*")
1215 (eww-bookmark-prepare))
1216 (with-current-buffer (get-buffer "*eww bookmarks*")
1218 (goto-char (point-max))
1219 (beginning-of-line))
1220 ;; On the final line.
1224 (user-error "No previous bookmark")
1226 (setq bookmark
(get-text-property (line-beginning-position)
1228 (eww-browse-url (plist-get bookmark
:url
))))
1230 (defvar eww-bookmark-mode-map
1231 (let ((map (make-sparse-keymap)))
1232 (suppress-keymap map
)
1233 (define-key map
"q" 'eww-bookmark-quit
)
1234 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1235 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1236 (define-key map
"\r" 'eww-bookmark-browse
)
1238 (easy-menu-define nil map
1239 "Menu for `eww-bookmark-mode-map'."
1241 ["Exit" eww-bookmark-quit t
]
1242 ["Browse" eww-bookmark-browse
1243 :active
(get-text-property (line-beginning-position) 'eww-bookmark
)]
1244 ["Kill" eww-bookmark-kill
1245 :active
(get-text-property (line-beginning-position) 'eww-bookmark
)]
1246 ["Yank" eww-bookmark-yank
1247 :active eww-bookmark-kill-ring
]))
1250 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1251 "Mode for listing bookmarks.
1253 \\{eww-bookmark-mode-map}"
1254 (buffer-disable-undo)
1255 (setq buffer-read-only t
1260 (defun eww-save-history ()
1261 (push (list :url eww-current-url
1262 :title eww-current-title
1264 :dom eww-current-dom
1265 :source eww-current-source
1266 :text
(buffer-string))
1269 (defun eww-list-histories ()
1270 "List the eww-histories."
1272 (when (null eww-history
)
1273 (error "No eww-histories are defined"))
1274 (set-buffer (get-buffer-create "*eww history*"))
1276 (let ((inhibit-read-only t
)
1279 url title format start
)
1281 (dolist (history eww-history
)
1282 (setq start
(point))
1283 (setq domain-length
(max domain-length
(length (plist-get history
:url
))))
1284 (setq title-length
(max title-length
(length (plist-get history
:title
))))
1286 (setq format
(format "%%-%ds %%-%ds" title-length domain-length
)
1288 (concat " " (format format
"Title" "URL")))
1290 (dolist (history eww-history
)
1291 (setq url
(plist-get history
:url
))
1292 (setq title
(plist-get history
:title
))
1293 (insert (format format title url
))
1295 (put-text-property start
(point) 'eww-history history
)
1297 (goto-char (point-min)))
1298 (pop-to-buffer "*eww history*")
1301 (defun eww-history-browse ()
1302 "Browse the history under point in eww."
1304 (let ((history (get-text-property (line-beginning-position) 'eww-history
)))
1306 (error "No history on the current line"))
1308 (pop-to-buffer "*eww*")
1309 (eww-browse-url (plist-get history
:url
))))
1311 (defun eww-history-quit ()
1312 "Kill the current buffer."
1314 (kill-buffer (current-buffer)))
1316 (defvar eww-history-kill-ring nil
)
1318 (defun eww-history-kill ()
1319 "Kill the current history."
1321 (let* ((start (line-beginning-position))
1322 (history (get-text-property start
'eww-history
))
1323 (inhibit-read-only t
))
1325 (error "No history on the current line"))
1327 (push (buffer-substring start
(point)) eww-history-kill-ring
)
1328 (delete-region start
(point))
1329 (setq eww-history
(delq history eww-history
))
1332 (defvar eww-history-mode-map
1333 (let ((map (make-sparse-keymap)))
1334 (suppress-keymap map
)
1335 (define-key map
"q" 'eww-history-quit
)
1336 (define-key map
[(control k
)] 'eww-history-kill
)
1337 (define-key map
"\r" 'eww-history-browse
)
1338 (define-key map
"n" 'next-error-no-select
)
1339 (define-key map
"p" 'previous-error-no-select
)
1342 (define-derived-mode eww-history-mode nil
"eww history"
1343 "Mode for listing eww-histories.
1345 \\{eww-history-mode-map}"
1346 (buffer-disable-undo)
1347 (setq buffer-read-only t
1352 ;;; eww.el ends here