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
)))
163 (defun eww-open-file (file)
164 "Render a file using EWW."
165 (interactive "fFile: ")
166 (eww (concat "file://" (expand-file-name file
))))
168 (defun eww-render (status url
&optional point
)
169 (let ((redirect (plist-get status
:redirect
)))
171 (setq url redirect
)))
172 (set (make-local-variable 'eww-next-url
) nil
)
173 (set (make-local-variable 'eww-previous-url
) nil
)
174 (set (make-local-variable 'eww-up-url
) nil
)
175 (set (make-local-variable 'eww-home-url
) nil
)
176 (set (make-local-variable 'eww-start-url
) nil
)
177 (set (make-local-variable 'eww-contents-url
) nil
)
178 (let* ((headers (eww-parse-headers))
180 (mail-header-parse-content-type
181 (or (cdr (assoc "content-type" headers
))
185 (or (cdr (assq 'charset
(cdr content-type
)))
186 (eww-detect-charset (equal (car content-type
)
189 (data-buffer (current-buffer)))
192 (setq eww-current-title
"")
194 ((and eww-use-external-browser-for-content-type
195 (string-match-p eww-use-external-browser-for-content-type
197 (eww-browse-with-external-browser url
))
198 ((equal (car content-type
) "text/html")
199 (eww-display-html charset url nil point
))
200 ((string-match-p "\\`image/" (car content-type
))
202 (eww-update-header-line-format))
205 (eww-update-header-line-format)))
206 (setq eww-current-url url
207 eww-history-position
0))
208 (kill-buffer data-buffer
))))
210 (defun eww-parse-headers ()
212 (goto-char (point-min))
213 (while (and (not (eobp))
215 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
216 (push (cons (downcase (match-string 1))
224 (defun eww-detect-charset (html-p)
225 (let ((case-fold-search t
)
229 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
233 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
236 (declare-function libxml-parse-html-region
"xml.c"
237 (start end
&optional base-url
))
239 (defun eww-display-html (charset url
&optional document point
)
240 (or (fboundp 'libxml-parse-html-region
)
241 (error "This function requires Emacs to be compiled with libxml2"))
242 (unless (eq charset
'utf8
)
244 (decode-coding-region (point) (point-max) charset
)
245 (coding-system-error nil
)))
249 'base
(list (cons 'href url
))
250 (libxml-parse-html-region (point) (point-max))))))
251 (setq eww-current-source
(buffer-substring (point) (point-max)))
253 (setq eww-current-dom document
)
254 (let ((inhibit-read-only t
)
255 (after-change-functions nil
)
257 (shr-target-id (url-target (url-generic-parse-url url
)))
258 (shr-external-rendering-functions
259 '((title . eww-tag-title
)
260 (form . eww-tag-form
)
261 (input . eww-tag-input
)
262 (textarea . eww-tag-textarea
)
263 (body . eww-tag-body
)
264 (select . eww-tag-select
)
265 (link . eww-tag-link
)
267 (shr-insert-document document
)
272 (let ((point (next-single-property-change
273 (point-min) 'shr-target-id
)))
275 (goto-char (1+ point
)))))
277 (goto-char (point-min)))))
278 (setq eww-current-url url
279 eww-history-position
0)
280 (eww-update-header-line-format)))
282 (defun eww-handle-link (cont)
283 (let* ((rel (assq :rel cont
))
284 (href (assq :href cont
))
286 ;; The text associated with :rel is case-insensitive.
287 (if rel
(downcase (cdr rel
)))
288 '(("next" . eww-next-url
)
289 ;; Texinfo uses "previous", but HTML specifies
290 ;; "prev", so recognize both.
291 ("previous" . eww-previous-url
)
292 ("prev" . eww-previous-url
)
293 ;; HTML specifies "start" but also "contents",
294 ;; and Gtk seems to use "home". Recognize
295 ;; them all; but store them in different
296 ;; variables so that we can readily choose the
298 ("start" . eww-start-url
)
299 ("home" . eww-home-url
)
300 ("contents" . eww-contents-url
)
301 ("up" . eww-up-url
)))))
304 (set (cdr where
) (cdr href
)))))
306 (defun eww-tag-link (cont)
307 (eww-handle-link cont
)
310 (defun eww-tag-a (cont)
311 (eww-handle-link cont
)
312 (let ((start (point)))
314 (put-text-property start
(point) 'keymap eww-link-keymap
)))
316 (defun eww-update-header-line-format ()
317 (if eww-header-line-format
318 (setq header-line-format
319 (replace-regexp-in-string
321 ;; FIXME? Title can be blank. Default to, eg, last component
323 (format-spec eww-header-line-format
324 `((?u .
,eww-current-url
)
325 (?t .
,eww-current-title
)))))
326 (setq header-line-format nil
)))
328 (defun eww-tag-title (cont)
329 (setq eww-current-title
"")
331 (when (eq (car sub
) 'text
)
332 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
333 (eww-update-header-line-format))
335 (defun eww-tag-body (cont)
336 (let* ((start (point))
337 (fgcolor (cdr (or (assq :fgcolor cont
)
339 (bgcolor (cdr (assq :bgcolor cont
)))
340 (shr-stylesheet (list (cons 'color fgcolor
)
341 (cons 'background-color bgcolor
))))
343 (eww-colorize-region start
(point) fgcolor bgcolor
)))
345 (defun eww-colorize-region (start end fg
&optional bg
)
347 (let ((new-colors (shr-color-check fg bg
)))
350 (add-face-text-property start end
351 (list :foreground
(cadr new-colors
))
354 (add-face-text-property start end
355 (list :background
(car new-colors
))
358 (defun eww-display-raw ()
359 (let ((data (buffer-substring (point) (point-max))))
361 (let ((inhibit-read-only t
))
363 (goto-char (point-min))))
365 (defun eww-display-image ()
366 (let ((data (shr-parse-image-data)))
368 (let ((inhibit-read-only t
))
369 (shr-put-image data nil
))
370 (goto-char (point-min))))
372 (defun eww-setup-buffer ()
373 (switch-to-buffer (get-buffer-create "*eww*"))
374 (let ((inhibit-read-only t
))
377 (unless (eq major-mode
'eww-mode
)
380 (defun eww-view-source ()
382 (let ((buf (get-buffer-create "*eww-source*"))
383 (source eww-current-source
))
384 (with-current-buffer buf
385 (delete-region (point-min) (point-max))
386 (insert (or eww-current-source
"no source"))
387 (goto-char (point-min))
388 (when (featurep 'html-mode
)
393 (let ((map (make-sparse-keymap)))
394 (suppress-keymap map
)
395 (define-key map
"q" 'quit-window
)
396 (define-key map
"g" 'eww-reload
)
397 (define-key map
[tab] 'shr-next-link)
398 (define-key map [backtab] 'shr-previous-link)
399 (define-key map [delete] 'scroll-down-command)
400 (define-key map "\177" 'scroll-down-command)
401 (define-key map " " 'scroll-up-command)
402 (define-key map "l" 'eww-back-url)
403 (define-key map "f" 'eww-forward-url)
404 (define-key map "n" 'eww-next-url)
405 (define-key map "p" 'eww-previous-url)
406 (define-key map "u" 'eww-up-url)
407 (define-key map "t" 'eww-top-url)
408 (define-key map "&" 'eww-browse-with-external-browser)
409 (define-key map "d" 'eww-download)
410 (define-key map "w" 'eww-copy-page-url)
411 (define-key map "C" 'url-cookie-list)
412 (define-key map "v" 'eww-view-source)
414 (define-key map "b" 'eww-add-bookmark)
415 (define-key map "B" 'eww-list-bookmarks)
416 (define-key map [(meta n)] 'eww-next-bookmark)
417 (define-key map [(meta p)] 'eww-previous-bookmark)
419 (easy-menu-define nil map ""
422 ["Close browser" quit-window t]
423 ["Reload" eww-reload t]
424 ["Back to previous page" eww-back-url
425 :active (not (zerop (length eww-history)))]
426 ["Forward to next page" eww-forward-url
427 :active (not (zerop eww-history-position))]
428 ["Browse with external browser" eww-browse-with-external-browser t]
429 ["Download" eww-download t]
430 ["View page source" eww-view-source]
431 ["Copy page URL" eww-copy-page-url t]
432 ["Add bookmark" eww-add-bookmark t]
433 ["List bookmarks" eww-list-bookmarks t]
434 ["List cookies" url-cookie-list t]))
437 (define-derived-mode eww-mode nil "eww"
438 "Mode for browsing the web.
441 ;; FIXME? This seems a strange default.
442 (set (make-local-variable 'eww-current-url) 'author)
443 (set (make-local-variable 'eww-current-dom) nil)
444 (set (make-local-variable 'eww-current-source) nil)
445 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
446 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
447 (set (make-local-variable 'eww-history) nil)
448 (set (make-local-variable 'eww-history-position) 0)
449 (buffer-disable-undo)
450 ;;(setq buffer-read-only t)
453 (defun eww-save-history ()
454 (push (list :url eww-current-url
455 :title eww-current-title
458 :source eww-current-source
459 :text (buffer-string))
463 (defun eww-browse-url (url &optional _new-window)
464 (when (and (equal major-mode 'eww-mode)
469 (defun eww-back-url ()
470 "Go to the previously displayed page."
472 (when (>= eww-history-position (length eww-history))
473 (error "No previous page"))
475 (setq eww-history-position (+ eww-history-position 2))
476 (eww-restore-history (elt eww-history (1- eww-history-position))))
478 (defun eww-forward-url ()
479 "Go to the next displayed page."
481 (when (zerop eww-history-position)
482 (error "No next page"))
484 (eww-restore-history (elt eww-history (1- eww-history-position))))
486 (defun eww-restore-history (elem)
487 (let ((inhibit-read-only t))
489 (insert (plist-get elem :text))
490 (setq eww-current-source (plist-get elem :source))
491 (setq eww-current-dom (plist-get elem :dom))
492 (goto-char (plist-get elem :point))
493 (setq eww-current-url (plist-get elem :url)
494 eww-current-title (plist-get elem :title))
495 (eww-update-header-line-format)))
497 (defun eww-next-url ()
498 "Go to the page marked `next'.
499 A page is marked `next' if rel=\"next\" appears in a <link>
503 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
504 (error "No `next' on this page")))
506 (defun eww-previous-url ()
507 "Go to the page marked `previous'.
508 A page is marked `previous' if rel=\"previous\" appears in a <link>
512 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
513 (error "No `previous' on this page")))
516 "Go to the page marked `up'.
517 A page is marked `up' if rel=\"up\" appears in a <link>
521 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
522 (error "No `up' on this page")))
524 (defun eww-top-url ()
525 "Go to the page marked `top'.
526 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
527 appears in a <link> or <a> tag."
529 (let ((best-url (or eww-start-url
533 (eww-browse-url (shr-expand-url best-url eww-current-url))
534 (error "No `top' for this page"))))
537 "Reload the current page."
539 (url-retrieve eww-current-url 'eww-render
540 (list eww-current-url (point))))
544 (defvar eww-form nil)
546 (defvar eww-submit-map
547 (let ((map (make-sparse-keymap)))
548 (define-key map "\r" 'eww-submit)
549 (define-key map [(control c) (control c)] 'eww-submit)
552 (defvar eww-checkbox-map
553 (let ((map (make-sparse-keymap)))
554 (define-key map [space] 'eww-toggle-checkbox)
555 (define-key map "\r" 'eww-toggle-checkbox)
556 (define-key map [(control c) (control c)] 'eww-submit)
560 (let ((map (make-keymap)))
561 (set-keymap-parent map text-mode-map)
562 (define-key map "\r" 'eww-submit)
563 (define-key map [(control a)] 'eww-beginning-of-text)
564 (define-key map [(control c) (control c)] 'eww-submit)
565 (define-key map [(control e)] 'eww-end-of-text)
566 (define-key map [tab] 'shr-next-link
)
567 (define-key map
[backtab] 'shr-previous-link)
570 (defvar eww-textarea-map
571 (let ((map (make-keymap)))
572 (set-keymap-parent map text-mode-map)
573 (define-key map "\r" 'forward-line)
574 (define-key map [(control c) (control c)] 'eww-submit)
575 (define-key map [tab] 'shr-next-link)
576 (define-key map [backtab] 'shr-previous-link
)
579 (defvar eww-select-map
580 (let ((map (make-sparse-keymap)))
581 (define-key map
"\r" 'eww-change-select
)
582 (define-key map
[(control c
) (control c
)] 'eww-submit
)
585 (defun eww-beginning-of-text ()
586 "Move to the start of the input field."
588 (goto-char (eww-beginning-of-field)))
590 (defun eww-end-of-text ()
591 "Move to the end of the text in the input field."
593 (goto-char (eww-end-of-field))
594 (let ((start (eww-beginning-of-field)))
595 (while (and (equal (following-char) ?
)
598 (when (> (point) start
)
601 (defun eww-beginning-of-field ()
605 ((not (eq (get-text-property (point) 'eww-form
)
606 (get-text-property (1- (point)) 'eww-form
)))
609 (previous-single-property-change
610 (point) 'eww-form nil
(point-min)))))
612 (defun eww-end-of-field ()
613 (1- (next-single-property-change
614 (point) 'eww-form nil
(point-max))))
616 (defun eww-tag-form (cont)
618 (list (assq :method cont
)
619 (assq :action cont
)))
621 (shr-ensure-paragraph)
626 (when (> (point) start
)
627 (put-text-property start
(1+ start
)
628 'eww-form eww-form
))))
630 (defun eww-form-submit (cont)
631 (let ((start (point))
632 (value (cdr (assq :value cont
))))
634 (if (zerop (length value
))
638 (add-face-text-property start
(point) 'eww-form-submit
)
639 (put-text-property start
(point) 'eww-form
640 (list :eww-form eww-form
643 :name
(cdr (assq :name cont
))))
644 (put-text-property start
(point) 'keymap eww-submit-map
)
647 (defun eww-form-checkbox (cont)
648 (let ((start (point)))
649 (if (cdr (assq :checked cont
))
650 (insert eww-form-checkbox-selected-symbol
)
651 (insert eww-form-checkbox-symbol
))
652 (add-face-text-property start
(point) 'eww-form-checkbox
)
653 (put-text-property start
(point) 'eww-form
654 (list :eww-form eww-form
655 :value
(cdr (assq :value cont
))
656 :type
(downcase (cdr (assq :type cont
)))
657 :checked
(cdr (assq :checked cont
))
658 :name
(cdr (assq :name cont
))))
659 (put-text-property start
(point) 'keymap eww-checkbox-map
)
662 (defun eww-form-text (cont)
663 (let ((start (point))
664 (type (downcase (or (cdr (assq :type cont
))
666 (value (or (cdr (assq :value cont
)) ""))
667 (width (string-to-number
668 (or (cdr (assq :size cont
))
671 (when (< (length value
) width
)
672 (insert (make-string (- width
(length value
)) ?
)))
673 (put-text-property start
(point) 'face
'eww-form-text
)
674 (put-text-property start
(point) 'local-map eww-text-map
)
675 (put-text-property start
(point) 'inhibit-read-only t
)
676 (put-text-property start
(point) 'eww-form
677 (list :eww-form eww-form
680 :name
(cdr (assq :name cont
))))
683 (defconst eww-text-input-types
'("text" "password" "textarea"
684 "color" "date" "datetime" "datetime-local"
685 "email" "month" "number" "search" "tel"
687 "List of input types which represent a text input.
688 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
690 (defun eww-process-text-input (beg end length
)
691 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
692 (properties (text-properties-at end
))
693 (type (plist-get form
:type
)))
695 (member type eww-text-input-types
))
698 ;; Delete some space at the end.
701 (if (equal type
"textarea")
702 (1- (line-end-position))
704 (let ((new (- end beg
)))
705 (while (and (> new
0)
706 (eql (following-char) ?
))
707 (delete-region (point) (1+ (point)))
708 (setq new
(1- new
))))
709 (set-text-properties beg end properties
)))
714 (if (equal type
"textarea")
715 (1- (line-end-position))
717 (let ((start (point)))
718 (insert (make-string length ?
))
719 (set-text-properties start
(point) properties
)))))
720 (let ((value (buffer-substring-no-properties
721 (eww-beginning-of-field)
722 (eww-end-of-field))))
723 (when (string-match " +\\'" value
)
724 (setq value
(substring value
0 (match-beginning 0))))
725 (plist-put form
:value value
)
726 (when (equal type
"password")
727 ;; Display passwords as asterisks.
728 (let ((start (eww-beginning-of-field)))
729 (put-text-property start
(+ start
(length value
))
730 'display
(make-string (length value
) ?
*))))))))
732 (defun eww-tag-textarea (cont)
733 (let ((start (point))
734 (value (or (cdr (assq :value cont
)) ""))
735 (lines (string-to-number
736 (or (cdr (assq :rows cont
))
738 (width (string-to-number
739 (or (cdr (assq :cols cont
))
745 (when (< (count-lines start
(point)) lines
)
746 (dotimes (i (- lines
(count-lines start
(point))))
748 (setq end
(point-marker))
750 (while (< (point) end
)
752 (let ((pad (- width
(- (point) (line-beginning-position)))))
754 (insert (make-string pad ?
))))
755 (add-face-text-property (line-beginning-position)
756 (point) 'eww-form-text
)
757 (put-text-property (line-beginning-position) (point)
758 'local-map eww-textarea-map
)
760 (put-text-property start
(point) 'eww-form
761 (list :eww-form eww-form
764 :name
(cdr (assq :name cont
))))))
766 (defun eww-tag-input (cont)
767 (let ((type (downcase (or (cdr (assq :type cont
))
771 ((or (equal type
"checkbox")
772 (equal type
"radio"))
773 (eww-form-checkbox cont
))
774 ((equal type
"submit")
775 (eww-form-submit cont
))
776 ((equal type
"hidden")
777 (let ((form eww-form
)
778 (name (cdr (assq :name cont
))))
779 ;; Don't add <input type=hidden> elements repeatedly.
781 (or (not (consp (car form
)))
782 (not (eq (caar form
) 'hidden
))
783 (not (equal (plist-get (cdr (car form
)) :name
)
785 (setq form
(cdr form
)))
787 (nconc eww-form
(list
790 :value
(cdr (assq :value cont
))))))))
792 (eww-form-text cont
)))
793 (unless (= start
(point))
794 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
796 (defun eww-tag-select (cont)
797 (shr-ensure-paragraph)
798 (let ((menu (list :name
(cdr (assq :name cont
))
804 (if (eq (car (car cont
)) 'optgroup
)
805 (dolist (groupelem cont
)
806 (unless (cdr (assq :disabled
(cdr groupelem
)))
807 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
809 (dolist (elem opelem
)
810 (when (eq (car elem
) 'option
)
811 (when (cdr (assq :selected
(cdr elem
)))
812 (nconc menu
(list :value
813 (cdr (assq :value
(cdr elem
))))))
814 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
815 (setq max
(max max
(length display
)))
817 :value
(cdr (assq :value
(cdr elem
)))
821 (setq options
(nreverse options
))
822 ;; If we have no selected values, default to the first value.
823 (unless (plist-get menu
:value
)
824 (nconc menu
(list :value
(nth 2 (car options
)))))
826 (let ((selected (eww-select-display menu
)))
828 (make-string (- max
(length selected
)) ?
)))
829 (put-text-property start
(point) 'eww-form menu
)
830 (add-face-text-property start
(point) 'eww-form-select
)
831 (put-text-property start
(point) 'keymap eww-select-map
)
832 (shr-ensure-paragraph))))
834 (defun eww-select-display (select)
835 (let ((value (plist-get select
:value
))
837 (dolist (elem select
)
838 (when (and (consp elem
)
839 (eq (car elem
) 'item
)
840 (equal value
(plist-get (cdr elem
) :value
)))
841 (setq display
(plist-get (cdr elem
) :display
))))
844 (defun eww-change-select ()
845 "Change the value of the select drop-down menu under point."
847 (let* ((input (get-text-property (point) 'eww-form
))
848 (completion-ignore-case t
)
851 (mapcar (lambda (elem)
853 (eq (car elem
) 'item
)
854 (cons (plist-get (cdr elem
) :display
)
855 (plist-get (cdr elem
) :value
))))
858 (completing-read "Change value: " options nil
'require-match
))
859 (inhibit-read-only t
))
860 (plist-put input
:value
(cdr (assoc-string display options t
)))
862 (eww-update-field display
))))
864 (defun eww-update-field (string)
865 (let ((properties (text-properties-at (point)))
866 (start (eww-beginning-of-field))
867 (end (1+ (eww-end-of-field))))
868 (delete-region start end
)
870 (make-string (- (- end start
) (length string
)) ?
))
871 (set-text-properties start end properties
)
874 (defun eww-toggle-checkbox ()
875 "Toggle the value of the checkbox under point."
877 (let* ((input (get-text-property (point) 'eww-form
))
878 (type (plist-get input
:type
)))
879 (if (equal type
"checkbox")
882 (if (plist-get input
:checked
)
884 (plist-put input
:checked nil
)
885 (eww-update-field eww-form-checkbox-symbol
))
886 (plist-put input
:checked t
)
887 (eww-update-field eww-form-checkbox-selected-symbol
))))
888 ;; Radio button. Switch all other buttons off.
889 (let ((name (plist-get input
:name
)))
891 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
892 (when (equal (plist-get (cdr elem
) :name
) name
)
893 (goto-char (car elem
))
894 (if (not (eq (cdr elem
) input
))
896 (plist-put input
:checked nil
)
897 (eww-update-field eww-form-checkbox-symbol
))
898 (plist-put input
:checked t
)
899 (eww-update-field eww-form-checkbox-selected-symbol
)))))
902 (defun eww-inputs (form)
903 (let ((start (point-min))
906 (< start
(point-max)))
907 (when (or (get-text-property start
'eww-form
)
908 (setq start
(next-single-property-change start
'eww-form
)))
909 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
911 (push (cons start
(get-text-property start
'eww-form
))
913 (setq start
(next-single-property-change start
'eww-form
))))
916 (defun eww-input-value (input)
917 (let ((type (plist-get input
:type
))
918 (value (plist-get input
:value
)))
920 ((equal type
"textarea")
923 (goto-char (point-min))
924 (while (re-search-forward "^ +\n\\| +$" nil t
)
925 (replace-match "" t t
))
928 (if (string-match " +\\'" value
)
929 (substring value
0 (match-beginning 0))
933 "Submit the current form."
935 (let* ((this-input (get-text-property (point) 'eww-form
))
936 (form (plist-get this-input
:eww-form
))
938 (dolist (elem (sort (eww-inputs form
)
940 (< (car o1
) (car o2
)))))
941 (let* ((input (cdr elem
))
942 (input-start (car elem
))
943 (name (plist-get input
:name
)))
946 ((member (plist-get input
:type
) '("checkbox" "radio"))
947 (when (plist-get input
:checked
)
948 (push (cons name
(plist-get input
:value
))
950 ((equal (plist-get input
:type
) "submit")
951 ;; We want the values from buttons if we hit a button if
952 ;; we hit enter on it, or if it's the first button after
953 ;; the field we did hit return on.
954 (when (or (eq input this-input
)
955 (and (not (eq input this-input
))
957 (> input-start
(point))))
959 (push (cons name
(plist-get input
:value
))
962 (push (cons name
(eww-input-value input
))
965 (when (and (consp elem
)
966 (eq (car elem
) 'hidden
))
967 (push (cons (plist-get (cdr elem
) :name
)
968 (plist-get (cdr elem
) :value
))
970 (if (and (stringp (cdr (assq :method form
)))
971 (equal (downcase (cdr (assq :method form
))) "post"))
972 (let ((url-request-method "POST")
973 (url-request-extra-headers
974 '(("Content-Type" .
"application/x-www-form-urlencoded")))
975 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
976 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
980 (if (cdr (assq :action form
))
981 (shr-expand-url (cdr (assq :action form
))
985 (mm-url-encode-www-form-urlencoded values
))))))
987 (defun eww-browse-with-external-browser (&optional url
)
988 "Browse the current URL with an external browser.
989 The browser to used is specified by the `shr-external-browser' variable."
991 (funcall shr-external-browser
(or url eww-current-url
)))
993 (defun eww-follow-link (&optional external mouse-event
)
994 "Browse the URL under point.
995 If EXTERNAL, browse the URL using `shr-external-browser'."
996 (interactive (list current-prefix-arg last-nonmenu-event
))
997 (mouse-set-point mouse-event
)
998 (let ((url (get-text-property (point) 'shr-url
)))
1001 (message "No link under point"))
1002 ((string-match "^mailto:" url
)
1003 (browse-url-mail url
))
1005 (funcall shr-external-browser url
))
1006 ;; This is a #target url in the same page as the current one.
1007 ((and (url-target (url-generic-parse-url url
))
1008 (eww-same-page-p url eww-current-url
))
1010 (eww-display-html 'utf8 url eww-current-dom
))
1012 (eww-browse-url url
)))))
1014 (defun eww-same-page-p (url1 url2
)
1015 "Return non-nil if both URLs represent the same page.
1016 Differences in #targets are ignored."
1017 (let ((obj1 (url-generic-parse-url url1
))
1018 (obj2 (url-generic-parse-url url2
)))
1019 (setf (url-target obj1
) nil
)
1020 (setf (url-target obj2
) nil
)
1021 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1023 (defun eww-copy-page-url ()
1025 (message "%s" eww-current-url
)
1026 (kill-new eww-current-url
))
1028 (defun eww-download ()
1029 "Download URL under point to `eww-download-directory'."
1031 (let ((url (get-text-property (point) 'shr-url
)))
1033 (message "No URL under point")
1034 (url-retrieve url
'eww-download-callback
(list url
)))))
1036 (defun eww-download-callback (status url
)
1037 (unless (plist-get status
:error
)
1038 (let* ((obj (url-generic-parse-url url
))
1039 (path (car (url-path-and-query obj
)))
1040 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1041 eww-download-path
)))
1043 (message "Saved %s" file
))))
1045 (defun eww-make-unique-file-name (file directory
)
1047 ((zerop (length file
))
1049 ((string-match "\\`[.]" file
)
1050 (setq file
(concat "!" file
))))
1052 (while (file-exists-p (expand-file-name file directory
))
1054 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1055 (format "%s(%d)%s" (match-string 1 file
)
1056 count
(match-string 2 file
))
1057 (format "%s(%d)" file count
)))
1058 (setq count
(1+ count
)))
1059 (expand-file-name file directory
)))
1063 (defvar eww-bookmarks nil
)
1065 (defun eww-add-bookmark ()
1066 "Add the current page to the bookmarks."
1068 (eww-read-bookmarks)
1069 (dolist (bookmark eww-bookmarks
)
1070 (when (equal eww-current-url
1071 (plist-get bookmark
:url
))
1072 (error "Already bookmarked")))
1073 (if (y-or-n-p "bookmark this page? ")
1075 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1076 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1077 (push (list :url eww-current-url
1079 :time
(current-time-string))
1081 (eww-write-bookmarks)
1082 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1084 (defun eww-write-bookmarks ()
1085 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1086 (insert ";; Auto-generated file; don't edit\n")
1087 (pp eww-bookmarks
(current-buffer))))
1089 (defun eww-read-bookmarks ()
1090 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1092 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1094 (insert-file-contents file
)
1095 (read (current-buffer)))))))
1097 (defun eww-list-bookmarks ()
1098 "Display the bookmarks."
1100 (eww-bookmark-prepare)
1101 (pop-to-buffer "*eww bookmarks*"))
1103 (defun eww-bookmark-prepare ()
1104 (eww-read-bookmarks)
1105 (when (null eww-bookmarks
)
1106 (error "No bookmarks are defined"))
1107 (set-buffer (get-buffer-create "*eww bookmarks*"))
1109 (let ((format "%-40s %s")
1110 (inhibit-read-only t
)
1113 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1114 (dolist (bookmark eww-bookmarks
)
1115 (setq start
(point))
1116 (setq url
(plist-get bookmark
:url
))
1117 (when (> (length url
) 40)
1118 (setq url
(substring url
0 40)))
1119 (insert (format format url
1120 (plist-get bookmark
:title
))
1122 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1123 (goto-char (point-min))))
1125 (defvar eww-bookmark-kill-ring nil
)
1127 (defun eww-bookmark-kill ()
1128 "Kill the current bookmark."
1130 (let* ((start (line-beginning-position))
1131 (bookmark (get-text-property start
'eww-bookmark
))
1132 (inhibit-read-only t
))
1134 (error "No bookmark on the current line"))
1136 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1137 (delete-region start
(point))
1138 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1139 (eww-write-bookmarks)))
1141 (defun eww-bookmark-yank ()
1142 "Yank a previously killed bookmark to the current line."
1144 (unless eww-bookmark-kill-ring
1145 (error "No previously killed bookmark"))
1147 (let ((inhibit-read-only t
)
1150 (insert (pop eww-bookmark-kill-ring
))
1151 (setq bookmark
(get-text-property start
'eww-bookmark
))
1152 (if (= start
(point-min))
1153 (push bookmark eww-bookmarks
)
1154 (let ((line (count-lines start
(point))))
1155 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1156 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1157 (eww-write-bookmarks)))
1159 (defun eww-bookmark-quit ()
1160 "Kill the current buffer."
1162 (kill-buffer (current-buffer)))
1164 (defun eww-bookmark-browse ()
1165 "Browse the bookmark under point in eww."
1167 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1169 (error "No bookmark on the current line"))
1170 ;; We wish to leave this window, but if it's the only window here,
1171 ;; just let it remain.
1174 (eww-browse-url (plist-get bookmark
:url
))))
1176 (defun eww-next-bookmark ()
1177 "Go to the next bookmark in the list."
1181 (unless (get-buffer "*eww bookmarks*")
1183 (eww-bookmark-prepare))
1184 (with-current-buffer (get-buffer "*eww bookmarks*")
1185 (when (and (not first
)
1188 (setq bookmark
(get-text-property (line-beginning-position)
1191 (error "No next bookmark")))
1192 (eww-browse-url (plist-get bookmark
:url
))))
1194 (defun eww-previous-bookmark ()
1195 "Go to the previous bookmark in the list."
1199 (unless (get-buffer "*eww bookmarks*")
1201 (eww-bookmark-prepare))
1202 (with-current-buffer (get-buffer "*eww bookmarks*")
1204 (goto-char (point-max))
1205 (beginning-of-line))
1206 ;; On the final line.
1210 (error "No previous bookmark")
1212 (setq bookmark
(get-text-property (line-beginning-position)
1214 (eww-browse-url (plist-get bookmark
:url
))))
1216 (defvar eww-bookmark-mode-map
1217 (let ((map (make-sparse-keymap)))
1218 (suppress-keymap map
)
1219 (define-key map
"q" 'eww-bookmark-quit
)
1220 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1221 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1222 (define-key map
"\r" 'eww-bookmark-browse
)
1225 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1226 "Mode for listing bookmarks.
1228 \\{eww-bookmark-mode-map}"
1229 (buffer-disable-undo)
1230 (setq buffer-read-only t
1235 ;;; eww.el ends here