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 ["Reload" eww-reload t]
423 ["Back to previous page" eww-back-url
424 :active (not (zerop (length eww-history)))]
425 ["Forward to next page" eww-forward-url
426 :active (not (zerop eww-history-position))]
427 ["Browse with external browser" eww-browse-with-external-browser t]
428 ["Download" eww-download t]
429 ["View page source" eww-view-source]
430 ["Copy page URL" eww-copy-page-url t]
431 ["Add bookmark" eww-add-bookmark t]
432 ["List bookmarks" eww-copy-page-url t]
433 ["List cookies" url-cookie-list t]))
436 (define-derived-mode eww-mode nil "eww"
437 "Mode for browsing the web.
440 ;; FIXME? This seems a strange default.
441 (set (make-local-variable 'eww-current-url) 'author)
442 (set (make-local-variable 'eww-current-dom) nil)
443 (set (make-local-variable 'eww-current-source) nil)
444 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
445 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
446 (set (make-local-variable 'eww-history) nil)
447 (set (make-local-variable 'eww-history-position) 0)
448 (buffer-disable-undo)
449 ;;(setq buffer-read-only t)
452 (defun eww-save-history ()
453 (push (list :url eww-current-url
454 :title eww-current-title
457 :source eww-current-source
458 :text (buffer-string))
462 (defun eww-browse-url (url &optional _new-window)
463 (when (and (equal major-mode 'eww-mode)
468 (defun eww-back-url ()
469 "Go to the previously displayed page."
471 (when (>= eww-history-position (length eww-history))
472 (error "No previous page"))
474 (setq eww-history-position (+ eww-history-position 2))
475 (eww-restore-history (elt eww-history (1- eww-history-position))))
477 (defun eww-forward-url ()
478 "Go to the next displayed page."
480 (when (zerop eww-history-position)
481 (error "No next page"))
483 (eww-restore-history (elt eww-history (1- eww-history-position))))
485 (defun eww-restore-history (elem)
486 (let ((inhibit-read-only t))
488 (insert (plist-get elem :text))
489 (setq eww-current-source (plist-get elem :source))
490 (setq eww-current-dom (plist-get elem :dom))
491 (goto-char (plist-get elem :point))
492 (setq eww-current-url (plist-get elem :url)
493 eww-current-title (plist-get elem :title))
494 (eww-update-header-line-format)))
496 (defun eww-next-url ()
497 "Go to the page marked `next'.
498 A page is marked `next' if rel=\"next\" appears in a <link>
502 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
503 (error "No `next' on this page")))
505 (defun eww-previous-url ()
506 "Go to the page marked `previous'.
507 A page is marked `previous' if rel=\"previous\" appears in a <link>
511 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
512 (error "No `previous' on this page")))
515 "Go to the page marked `up'.
516 A page is marked `up' if rel=\"up\" appears in a <link>
520 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
521 (error "No `up' on this page")))
523 (defun eww-top-url ()
524 "Go to the page marked `top'.
525 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
526 appears in a <link> or <a> tag."
528 (let ((best-url (or eww-start-url
532 (eww-browse-url (shr-expand-url best-url eww-current-url))
533 (error "No `top' for this page"))))
536 "Reload the current page."
538 (url-retrieve eww-current-url 'eww-render
539 (list eww-current-url (point))))
543 (defvar eww-form nil)
545 (defvar eww-submit-map
546 (let ((map (make-sparse-keymap)))
547 (define-key map "\r" 'eww-submit)
548 (define-key map [(control c) (control c)] 'eww-submit)
551 (defvar eww-checkbox-map
552 (let ((map (make-sparse-keymap)))
553 (define-key map [space] 'eww-toggle-checkbox)
554 (define-key map "\r" 'eww-toggle-checkbox)
555 (define-key map [(control c) (control c)] 'eww-submit)
559 (let ((map (make-keymap)))
560 (set-keymap-parent map text-mode-map)
561 (define-key map "\r" 'eww-submit)
562 (define-key map [(control a)] 'eww-beginning-of-text)
563 (define-key map [(control c) (control c)] 'eww-submit)
564 (define-key map [(control e)] 'eww-end-of-text)
565 (define-key map [tab] 'shr-next-link
)
566 (define-key map
[backtab] 'shr-previous-link)
569 (defvar eww-textarea-map
570 (let ((map (make-keymap)))
571 (set-keymap-parent map text-mode-map)
572 (define-key map "\r" 'forward-line)
573 (define-key map [(control c) (control c)] 'eww-submit)
574 (define-key map [tab] 'shr-next-link)
575 (define-key map [backtab] 'shr-previous-link
)
578 (defvar eww-select-map
579 (let ((map (make-sparse-keymap)))
580 (define-key map
"\r" 'eww-change-select
)
581 (define-key map
[(control c
) (control c
)] 'eww-submit
)
584 (defun eww-beginning-of-text ()
585 "Move to the start of the input field."
587 (goto-char (eww-beginning-of-field)))
589 (defun eww-end-of-text ()
590 "Move to the end of the text in the input field."
592 (goto-char (eww-end-of-field))
593 (let ((start (eww-beginning-of-field)))
594 (while (and (equal (following-char) ?
)
597 (when (> (point) start
)
600 (defun eww-beginning-of-field ()
604 ((not (eq (get-text-property (point) 'eww-form
)
605 (get-text-property (1- (point)) 'eww-form
)))
608 (previous-single-property-change
609 (point) 'eww-form nil
(point-min)))))
611 (defun eww-end-of-field ()
612 (1- (next-single-property-change
613 (point) 'eww-form nil
(point-max))))
615 (defun eww-tag-form (cont)
617 (list (assq :method cont
)
618 (assq :action cont
)))
620 (shr-ensure-paragraph)
625 (when (> (point) start
)
626 (put-text-property start
(1+ start
)
627 'eww-form eww-form
))))
629 (defun eww-form-submit (cont)
630 (let ((start (point))
631 (value (cdr (assq :value cont
))))
633 (if (zerop (length value
))
637 (add-face-text-property start
(point) 'eww-form-submit
)
638 (put-text-property start
(point) 'eww-form
639 (list :eww-form eww-form
642 :name
(cdr (assq :name cont
))))
643 (put-text-property start
(point) 'keymap eww-submit-map
)
646 (defun eww-form-checkbox (cont)
647 (let ((start (point)))
648 (if (cdr (assq :checked cont
))
649 (insert eww-form-checkbox-selected-symbol
)
650 (insert eww-form-checkbox-symbol
))
651 (add-face-text-property start
(point) 'eww-form-checkbox
)
652 (put-text-property start
(point) 'eww-form
653 (list :eww-form eww-form
654 :value
(cdr (assq :value cont
))
655 :type
(downcase (cdr (assq :type cont
)))
656 :checked
(cdr (assq :checked cont
))
657 :name
(cdr (assq :name cont
))))
658 (put-text-property start
(point) 'keymap eww-checkbox-map
)
661 (defun eww-form-text (cont)
662 (let ((start (point))
663 (type (downcase (or (cdr (assq :type cont
))
665 (value (or (cdr (assq :value cont
)) ""))
666 (width (string-to-number
667 (or (cdr (assq :size cont
))
670 (when (< (length value
) width
)
671 (insert (make-string (- width
(length value
)) ?
)))
672 (put-text-property start
(point) 'face
'eww-form-text
)
673 (put-text-property start
(point) 'local-map eww-text-map
)
674 (put-text-property start
(point) 'inhibit-read-only t
)
675 (put-text-property start
(point) 'eww-form
676 (list :eww-form eww-form
679 :name
(cdr (assq :name cont
))))
682 (defconst eww-text-input-types
'("text" "password" "textarea"
683 "color" "date" "datetime" "datetime-local"
684 "email" "month" "number" "search" "tel"
686 "List of input types which represent a text input.
687 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
689 (defun eww-process-text-input (beg end length
)
690 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
691 (properties (text-properties-at end
))
692 (type (plist-get form
:type
)))
694 (member type eww-text-input-types
))
697 ;; Delete some space at the end.
700 (if (equal type
"textarea")
701 (1- (line-end-position))
703 (let ((new (- end beg
)))
704 (while (and (> new
0)
705 (eql (following-char) ?
))
706 (delete-region (point) (1+ (point)))
707 (setq new
(1- new
))))
708 (set-text-properties beg end properties
)))
713 (if (equal type
"textarea")
714 (1- (line-end-position))
716 (let ((start (point)))
717 (insert (make-string length ?
))
718 (set-text-properties start
(point) properties
)))))
719 (let ((value (buffer-substring-no-properties
720 (eww-beginning-of-field)
721 (eww-end-of-field))))
722 (when (string-match " +\\'" value
)
723 (setq value
(substring value
0 (match-beginning 0))))
724 (plist-put form
:value value
)
725 (when (equal type
"password")
726 ;; Display passwords as asterisks.
727 (let ((start (eww-beginning-of-field)))
728 (put-text-property start
(+ start
(length value
))
729 'display
(make-string (length value
) ?
*))))))))
731 (defun eww-tag-textarea (cont)
732 (let ((start (point))
733 (value (or (cdr (assq :value cont
)) ""))
734 (lines (string-to-number
735 (or (cdr (assq :rows cont
))
737 (width (string-to-number
738 (or (cdr (assq :cols cont
))
744 (when (< (count-lines start
(point)) lines
)
745 (dotimes (i (- lines
(count-lines start
(point))))
747 (setq end
(point-marker))
749 (while (< (point) end
)
751 (let ((pad (- width
(- (point) (line-beginning-position)))))
753 (insert (make-string pad ?
))))
754 (add-face-text-property (line-beginning-position)
755 (point) 'eww-form-text
)
756 (put-text-property (line-beginning-position) (point)
757 'local-map eww-textarea-map
)
759 (put-text-property start
(point) 'eww-form
760 (list :eww-form eww-form
763 :name
(cdr (assq :name cont
))))))
765 (defun eww-tag-input (cont)
766 (let ((type (downcase (or (cdr (assq :type cont
))
770 ((or (equal type
"checkbox")
771 (equal type
"radio"))
772 (eww-form-checkbox cont
))
773 ((equal type
"submit")
774 (eww-form-submit cont
))
775 ((equal type
"hidden")
776 (let ((form eww-form
)
777 (name (cdr (assq :name cont
))))
778 ;; Don't add <input type=hidden> elements repeatedly.
780 (or (not (consp (car form
)))
781 (not (eq (caar form
) 'hidden
))
782 (not (equal (plist-get (cdr (car form
)) :name
)
784 (setq form
(cdr form
)))
786 (nconc eww-form
(list
789 :value
(cdr (assq :value cont
))))))))
791 (eww-form-text cont
)))
792 (unless (= start
(point))
793 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
795 (defun eww-tag-select (cont)
796 (shr-ensure-paragraph)
797 (let ((menu (list :name
(cdr (assq :name cont
))
803 (if (eq (car (car cont
)) 'optgroup
)
804 (dolist (groupelem cont
)
805 (unless (cdr (assq :disabled
(cdr groupelem
)))
806 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
808 (dolist (elem opelem
)
809 (when (eq (car elem
) 'option
)
810 (when (cdr (assq :selected
(cdr elem
)))
811 (nconc menu
(list :value
812 (cdr (assq :value
(cdr elem
))))))
813 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
814 (setq max
(max max
(length display
)))
816 :value
(cdr (assq :value
(cdr elem
)))
820 (setq options
(nreverse options
))
821 ;; If we have no selected values, default to the first value.
822 (unless (plist-get menu
:value
)
823 (nconc menu
(list :value
(nth 2 (car options
)))))
825 (let ((selected (eww-select-display menu
)))
827 (make-string (- max
(length selected
)) ?
)))
828 (put-text-property start
(point) 'eww-form menu
)
829 (add-face-text-property start
(point) 'eww-form-select
)
830 (put-text-property start
(point) 'keymap eww-select-map
)
831 (shr-ensure-paragraph))))
833 (defun eww-select-display (select)
834 (let ((value (plist-get select
:value
))
836 (dolist (elem select
)
837 (when (and (consp elem
)
838 (eq (car elem
) 'item
)
839 (equal value
(plist-get (cdr elem
) :value
)))
840 (setq display
(plist-get (cdr elem
) :display
))))
843 (defun eww-change-select ()
844 "Change the value of the select drop-down menu under point."
846 (let* ((input (get-text-property (point) 'eww-form
))
847 (completion-ignore-case t
)
850 (mapcar (lambda (elem)
852 (eq (car elem
) 'item
)
853 (cons (plist-get (cdr elem
) :display
)
854 (plist-get (cdr elem
) :value
))))
857 (completing-read "Change value: " options nil
'require-match
))
858 (inhibit-read-only t
))
859 (plist-put input
:value
(cdr (assoc-string display options t
)))
861 (eww-update-field display
))))
863 (defun eww-update-field (string)
864 (let ((properties (text-properties-at (point)))
865 (start (eww-beginning-of-field))
866 (end (1+ (eww-end-of-field))))
867 (delete-region start end
)
869 (make-string (- (- end start
) (length string
)) ?
))
870 (set-text-properties start end properties
)
873 (defun eww-toggle-checkbox ()
874 "Toggle the value of the checkbox under point."
876 (let* ((input (get-text-property (point) 'eww-form
))
877 (type (plist-get input
:type
)))
878 (if (equal type
"checkbox")
881 (if (plist-get input
:checked
)
883 (plist-put input
:checked nil
)
884 (eww-update-field eww-form-checkbox-symbol
))
885 (plist-put input
:checked t
)
886 (eww-update-field eww-form-checkbox-selected-symbol
))))
887 ;; Radio button. Switch all other buttons off.
888 (let ((name (plist-get input
:name
)))
890 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
891 (when (equal (plist-get (cdr elem
) :name
) name
)
892 (goto-char (car elem
))
893 (if (not (eq (cdr elem
) input
))
895 (plist-put input
:checked nil
)
896 (eww-update-field eww-form-checkbox-symbol
))
897 (plist-put input
:checked t
)
898 (eww-update-field eww-form-checkbox-selected-symbol
)))))
901 (defun eww-inputs (form)
902 (let ((start (point-min))
905 (< start
(point-max)))
906 (when (or (get-text-property start
'eww-form
)
907 (setq start
(next-single-property-change start
'eww-form
)))
908 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
910 (push (cons start
(get-text-property start
'eww-form
))
912 (setq start
(next-single-property-change start
'eww-form
))))
915 (defun eww-input-value (input)
916 (let ((type (plist-get input
:type
))
917 (value (plist-get input
:value
)))
919 ((equal type
"textarea")
922 (goto-char (point-min))
923 (while (re-search-forward "^ +\n\\| +$" nil t
)
924 (replace-match "" t t
))
927 (if (string-match " +\\'" value
)
928 (substring value
0 (match-beginning 0))
932 "Submit the current form."
934 (let* ((this-input (get-text-property (point) 'eww-form
))
935 (form (plist-get this-input
:eww-form
))
937 (dolist (elem (sort (eww-inputs form
)
939 (< (car o1
) (car o2
)))))
940 (let* ((input (cdr elem
))
941 (input-start (car elem
))
942 (name (plist-get input
:name
)))
945 ((member (plist-get input
:type
) '("checkbox" "radio"))
946 (when (plist-get input
:checked
)
947 (push (cons name
(plist-get input
:value
))
949 ((equal (plist-get input
:type
) "submit")
950 ;; We want the values from buttons if we hit a button if
951 ;; we hit enter on it, or if it's the first button after
952 ;; the field we did hit return on.
953 (when (or (eq input this-input
)
954 (and (not (eq input this-input
))
956 (> input-start
(point))))
958 (push (cons name
(plist-get input
:value
))
961 (push (cons name
(eww-input-value input
))
964 (when (and (consp elem
)
965 (eq (car elem
) 'hidden
))
966 (push (cons (plist-get (cdr elem
) :name
)
967 (plist-get (cdr elem
) :value
))
969 (if (and (stringp (cdr (assq :method form
)))
970 (equal (downcase (cdr (assq :method form
))) "post"))
971 (let ((url-request-method "POST")
972 (url-request-extra-headers
973 '(("Content-Type" .
"application/x-www-form-urlencoded")))
974 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
975 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
979 (if (cdr (assq :action form
))
980 (shr-expand-url (cdr (assq :action form
))
984 (mm-url-encode-www-form-urlencoded values
))))))
986 (defun eww-browse-with-external-browser (&optional url
)
987 "Browse the current URL with an external browser.
988 The browser to used is specified by the `shr-external-browser' variable."
990 (funcall shr-external-browser
(or url eww-current-url
)))
992 (defun eww-follow-link (&optional external mouse-event
)
993 "Browse the URL under point.
994 If EXTERNAL, browse the URL using `shr-external-browser'."
995 (interactive (list current-prefix-arg last-nonmenu-event
))
996 (mouse-set-point mouse-event
)
997 (let ((url (get-text-property (point) 'shr-url
)))
1000 (message "No link under point"))
1001 ((string-match "^mailto:" url
)
1002 (browse-url-mail url
))
1004 (funcall shr-external-browser url
))
1005 ;; This is a #target url in the same page as the current one.
1006 ((and (url-target (url-generic-parse-url url
))
1007 (eww-same-page-p url eww-current-url
))
1009 (eww-display-html 'utf8 url eww-current-dom
))
1011 (eww-browse-url url
)))))
1013 (defun eww-same-page-p (url1 url2
)
1014 "Return non-nil if both URLs represent the same page.
1015 Differences in #targets are ignored."
1016 (let ((obj1 (url-generic-parse-url url1
))
1017 (obj2 (url-generic-parse-url url2
)))
1018 (setf (url-target obj1
) nil
)
1019 (setf (url-target obj2
) nil
)
1020 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1022 (defun eww-copy-page-url ()
1024 (message "%s" eww-current-url
)
1025 (kill-new eww-current-url
))
1027 (defun eww-download ()
1028 "Download URL under point to `eww-download-directory'."
1030 (let ((url (get-text-property (point) 'shr-url
)))
1032 (message "No URL under point")
1033 (url-retrieve url
'eww-download-callback
(list url
)))))
1035 (defun eww-download-callback (status url
)
1036 (unless (plist-get status
:error
)
1037 (let* ((obj (url-generic-parse-url url
))
1038 (path (car (url-path-and-query obj
)))
1039 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1040 eww-download-path
)))
1042 (message "Saved %s" file
))))
1044 (defun eww-make-unique-file-name (file directory
)
1046 ((zerop (length file
))
1048 ((string-match "\\`[.]" file
)
1049 (setq file
(concat "!" file
))))
1051 (while (file-exists-p (expand-file-name file directory
))
1053 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1054 (format "%s(%d)%s" (match-string 1 file
)
1055 count
(match-string 2 file
))
1056 (format "%s(%d)" file count
)))
1057 (setq count
(1+ count
)))
1058 (expand-file-name file directory
)))
1062 (defvar eww-bookmarks nil
)
1064 (defun eww-add-bookmark ()
1065 "Add the current page to the bookmarks."
1067 (eww-read-bookmarks)
1068 (dolist (bookmark eww-bookmarks
)
1069 (when (equal eww-current-url
1070 (plist-get bookmark
:url
))
1071 (error "Already bookmarked")))
1072 (if (y-or-n-p "bookmark this page? ")
1074 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1075 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1076 (push (list :url eww-current-url
1078 :time
(current-time-string))
1080 (eww-write-bookmarks)
1081 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1083 (defun eww-write-bookmarks ()
1084 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1085 (insert ";; Auto-generated file; don't edit\n")
1086 (pp eww-bookmarks
(current-buffer))))
1088 (defun eww-read-bookmarks ()
1089 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1091 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1093 (insert-file-contents file
)
1094 (read (current-buffer)))))))
1096 (defun eww-list-bookmarks ()
1097 "Display the bookmarks."
1099 (eww-bookmark-prepare)
1100 (pop-to-buffer "*eww bookmarks*"))
1102 (defun eww-bookmark-prepare ()
1103 (eww-read-bookmarks)
1104 (when (null eww-bookmarks
)
1105 (error "No bookmarks are defined"))
1106 (set-buffer (get-buffer-create "*eww bookmarks*"))
1108 (let ((format "%-40s %s")
1109 (inhibit-read-only t
)
1112 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1113 (dolist (bookmark eww-bookmarks
)
1114 (setq start
(point))
1115 (setq url
(plist-get bookmark
:url
))
1116 (when (> (length url
) 40)
1117 (setq url
(substring url
0 40)))
1118 (insert (format format url
1119 (plist-get bookmark
:title
))
1121 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1122 (goto-char (point-min))))
1124 (defvar eww-bookmark-kill-ring nil
)
1126 (defun eww-bookmark-kill ()
1127 "Kill the current bookmark."
1129 (let* ((start (line-beginning-position))
1130 (bookmark (get-text-property start
'eww-bookmark
))
1131 (inhibit-read-only t
))
1133 (error "No bookmark on the current line"))
1135 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1136 (delete-region start
(point))
1137 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1138 (eww-write-bookmarks)))
1140 (defun eww-bookmark-yank ()
1141 "Yank a previously killed bookmark to the current line."
1143 (unless eww-bookmark-kill-ring
1144 (error "No previously killed bookmark"))
1146 (let ((inhibit-read-only t
)
1149 (insert (pop eww-bookmark-kill-ring
))
1150 (setq bookmark
(get-text-property start
'eww-bookmark
))
1151 (if (= start
(point-min))
1152 (push bookmark eww-bookmarks
)
1153 (let ((line (count-lines start
(point))))
1154 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1155 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1156 (eww-write-bookmarks)))
1158 (defun eww-bookmark-quit ()
1159 "Kill the current buffer."
1161 (kill-buffer (current-buffer)))
1163 (defun eww-bookmark-browse ()
1164 "Browse the bookmark under point in eww."
1166 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1168 (error "No bookmark on the current line"))
1169 ;; We wish to leave this window, but if it's the only window here,
1170 ;; just let it remain.
1173 (eww-browse-url (plist-get bookmark
:url
))))
1175 (defun eww-next-bookmark ()
1176 "Go to the next bookmark in the list."
1180 (unless (get-buffer "*eww bookmarks*")
1182 (eww-bookmark-prepare))
1183 (with-current-buffer (get-buffer "*eww bookmarks*")
1184 (when (and (not first
)
1187 (setq bookmark
(get-text-property (line-beginning-position)
1190 (error "No next bookmark")))
1191 (eww-browse-url (plist-get bookmark
:url
))))
1193 (defun eww-previous-bookmark ()
1194 "Go to the previous bookmark in the list."
1198 (unless (get-buffer "*eww bookmarks*")
1200 (eww-bookmark-prepare))
1201 (with-current-buffer (get-buffer "*eww bookmarks*")
1203 (goto-char (point-max))
1204 (beginning-of-line))
1205 ;; On the final line.
1209 (error "No previous bookmark")
1211 (setq bookmark
(get-text-property (line-beginning-position)
1213 (eww-browse-url (plist-get bookmark
:url
))))
1215 (defvar eww-bookmark-mode-map
1216 (let ((map (make-sparse-keymap)))
1217 (suppress-keymap map
)
1218 (define-key map
"q" 'eww-bookmark-quit
)
1219 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1220 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1221 (define-key map
"\r" 'eww-bookmark-browse
)
1224 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1225 "Mode for listing bookmarks.
1227 \\{eww-bookmark-mode-map}"
1228 (buffer-disable-undo)
1229 (setq buffer-read-only t
1234 ;;; eww.el ends here