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 (set (make-local-variable 'eww-next-url
) nil
)
175 (set (make-local-variable 'eww-previous-url
) nil
)
176 (set (make-local-variable 'eww-up-url
) nil
)
177 (set (make-local-variable 'eww-home-url
) nil
)
178 (set (make-local-variable 'eww-start-url
) nil
)
179 (set (make-local-variable 'eww-contents-url
) nil
)
180 (let* ((headers (eww-parse-headers))
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 (featurep 'html-mode
)
396 (let ((map (make-sparse-keymap)))
397 (suppress-keymap map
)
398 (define-key map
"q" 'eww-close
)
399 (define-key map
"Q" 'eww-exit
)
400 (define-key map
"g" 'eww-reload
)
401 (define-key map
[tab] 'shr-next-link)
402 (define-key map [backtab] 'shr-previous-link)
403 (define-key map [delete] 'scroll-down-command)
404 (define-key map [?\S-\ ] 'scroll-down-command)
405 (define-key map "\177" 'scroll-down-command)
406 (define-key map " " 'scroll-up-command)
407 (define-key map "l" 'eww-back-url)
408 (define-key map "r" 'eww-forward-url)
409 (define-key map "n" 'eww-next-url)
410 (define-key map "p" 'eww-previous-url)
411 (define-key map "u" 'eww-up-url)
412 (define-key map "t" 'eww-top-url)
413 (define-key map "&" 'eww-browse-with-external-browser)
414 (define-key map "d" 'eww-download)
415 (define-key map "w" 'eww-copy-page-url)
416 (define-key map "C" 'url-cookie-list)
417 (define-key map "v" 'eww-view-source)
419 (define-key map "b" 'eww-add-bookmark)
420 (define-key map "B" 'eww-list-bookmarks)
421 (define-key map [(meta n)] 'eww-next-bookmark)
422 (define-key map [(meta p)] 'eww-previous-bookmark)
424 (easy-menu-define nil map ""
427 ["Close browser" eww-close t]
428 ["Reload" eww-reload t]
429 ["Back to previous page" eww-back-url
430 :active (not (zerop (length eww-history)))]
431 ["Forward to next page" eww-forward-url
432 :active (not (zerop eww-history-position))]
433 ["Browse with external browser" eww-browse-with-external-browser t]
434 ["Download" eww-download t]
435 ["View page source" eww-view-source]
436 ["Copy page URL" eww-copy-page-url t]
437 ["Add bookmark" eww-add-bookmark t]
438 ["List bookmarks" eww-list-bookmarks t]
439 ["List cookies" url-cookie-list t]))
442 (define-derived-mode eww-mode nil "eww"
443 "Mode for browsing the web.
446 ;; FIXME? This seems a strange default.
447 (set (make-local-variable 'eww-current-url) 'author)
448 (set (make-local-variable 'eww-current-dom) nil)
449 (set (make-local-variable 'eww-current-source) nil)
450 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
451 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
452 (set (make-local-variable 'eww-history) nil)
453 (set (make-local-variable 'eww-history-position) 0)
454 (buffer-disable-undo)
455 ;;(setq buffer-read-only t)
459 "Exit the Emacs Web Wowser."
461 (setq eww-history nil)
462 (kill-buffer (current-buffer)))
465 "Close the Emacs Web Wowser browser, leaving history intact."
469 (defun eww-save-history ()
470 (push (list :url eww-current-url
471 :title eww-current-title
474 :source eww-current-source
475 :text (buffer-string))
479 (defun eww-browse-url (url &optional _new-window)
480 (when (and (equal major-mode 'eww-mode)
485 (defun eww-back-url ()
486 "Go to the previously displayed page."
488 (when (>= eww-history-position (length eww-history))
489 (error "No previous page"))
491 (setq eww-history-position (+ eww-history-position 2))
492 (eww-restore-history (elt eww-history (1- eww-history-position))))
494 (defun eww-forward-url ()
495 "Go to the next displayed page."
497 (when (zerop eww-history-position)
498 (error "No next page"))
500 (eww-restore-history (elt eww-history (1- eww-history-position))))
502 (defun eww-restore-history (elem)
503 (let ((inhibit-read-only t))
505 (insert (plist-get elem :text))
506 (setq eww-current-source (plist-get elem :source))
507 (setq eww-current-dom (plist-get elem :dom))
508 (goto-char (plist-get elem :point))
509 (setq eww-current-url (plist-get elem :url)
510 eww-current-title (plist-get elem :title))
511 (eww-update-header-line-format)))
513 (defun eww-next-url ()
514 "Go to the page marked `next'.
515 A page is marked `next' if rel=\"next\" appears in a <link>
519 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
520 (error "No `next' on this page")))
522 (defun eww-previous-url ()
523 "Go to the page marked `previous'.
524 A page is marked `previous' if rel=\"previous\" appears in a <link>
528 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
529 (error "No `previous' on this page")))
532 "Go to the page marked `up'.
533 A page is marked `up' if rel=\"up\" appears in a <link>
537 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
538 (error "No `up' on this page")))
540 (defun eww-top-url ()
541 "Go to the page marked `top'.
542 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
543 appears in a <link> or <a> tag."
545 (let ((best-url (or eww-start-url
549 (eww-browse-url (shr-expand-url best-url eww-current-url))
550 (error "No `top' for this page"))))
553 "Reload the current page."
555 (url-retrieve eww-current-url 'eww-render
556 (list eww-current-url (point))))
560 (defvar eww-form nil)
562 (defvar eww-submit-map
563 (let ((map (make-sparse-keymap)))
564 (define-key map "\r" 'eww-submit)
565 (define-key map [(control c) (control c)] 'eww-submit)
568 (defvar eww-checkbox-map
569 (let ((map (make-sparse-keymap)))
570 (define-key map [space] 'eww-toggle-checkbox)
571 (define-key map "\r" 'eww-toggle-checkbox)
572 (define-key map [(control c) (control c)] 'eww-submit)
576 (let ((map (make-keymap)))
577 (set-keymap-parent map text-mode-map)
578 (define-key map "\r" 'eww-submit)
579 (define-key map [(control a)] 'eww-beginning-of-text)
580 (define-key map [(control c) (control c)] 'eww-submit)
581 (define-key map [(control e)] 'eww-end-of-text)
582 (define-key map [tab] 'shr-next-link
)
583 (define-key map
[backtab] 'shr-previous-link)
586 (defvar eww-textarea-map
587 (let ((map (make-keymap)))
588 (set-keymap-parent map text-mode-map)
589 (define-key map "\r" 'forward-line)
590 (define-key map [(control c) (control c)] 'eww-submit)
591 (define-key map [tab] 'shr-next-link)
592 (define-key map [backtab] 'shr-previous-link
)
595 (defvar eww-select-map
596 (let ((map (make-sparse-keymap)))
597 (define-key map
"\r" 'eww-change-select
)
598 (define-key map
[(control c
) (control c
)] 'eww-submit
)
601 (defun eww-beginning-of-text ()
602 "Move to the start of the input field."
604 (goto-char (eww-beginning-of-field)))
606 (defun eww-end-of-text ()
607 "Move to the end of the text in the input field."
609 (goto-char (eww-end-of-field))
610 (let ((start (eww-beginning-of-field)))
611 (while (and (equal (following-char) ?
)
614 (when (> (point) start
)
617 (defun eww-beginning-of-field ()
621 ((not (eq (get-text-property (point) 'eww-form
)
622 (get-text-property (1- (point)) 'eww-form
)))
625 (previous-single-property-change
626 (point) 'eww-form nil
(point-min)))))
628 (defun eww-end-of-field ()
629 (1- (next-single-property-change
630 (point) 'eww-form nil
(point-max))))
632 (defun eww-tag-form (cont)
634 (list (assq :method cont
)
635 (assq :action cont
)))
637 (shr-ensure-paragraph)
642 (when (> (point) start
)
643 (put-text-property start
(1+ start
)
644 'eww-form eww-form
))))
646 (defun eww-form-submit (cont)
647 (let ((start (point))
648 (value (cdr (assq :value cont
))))
650 (if (zerop (length value
))
654 (add-face-text-property start
(point) 'eww-form-submit
)
655 (put-text-property start
(point) 'eww-form
656 (list :eww-form eww-form
659 :name
(cdr (assq :name cont
))))
660 (put-text-property start
(point) 'keymap eww-submit-map
)
663 (defun eww-form-checkbox (cont)
664 (let ((start (point)))
665 (if (cdr (assq :checked cont
))
666 (insert eww-form-checkbox-selected-symbol
)
667 (insert eww-form-checkbox-symbol
))
668 (add-face-text-property start
(point) 'eww-form-checkbox
)
669 (put-text-property start
(point) 'eww-form
670 (list :eww-form eww-form
671 :value
(cdr (assq :value cont
))
672 :type
(downcase (cdr (assq :type cont
)))
673 :checked
(cdr (assq :checked cont
))
674 :name
(cdr (assq :name cont
))))
675 (put-text-property start
(point) 'keymap eww-checkbox-map
)
678 (defun eww-form-text (cont)
679 (let ((start (point))
680 (type (downcase (or (cdr (assq :type cont
))
682 (value (or (cdr (assq :value cont
)) ""))
683 (width (string-to-number
684 (or (cdr (assq :size cont
))
687 (when (< (length value
) width
)
688 (insert (make-string (- width
(length value
)) ?
)))
689 (put-text-property start
(point) 'face
'eww-form-text
)
690 (put-text-property start
(point) 'local-map eww-text-map
)
691 (put-text-property start
(point) 'inhibit-read-only t
)
692 (put-text-property start
(point) 'eww-form
693 (list :eww-form eww-form
696 :name
(cdr (assq :name cont
))))
699 (defconst eww-text-input-types
'("text" "password" "textarea"
700 "color" "date" "datetime" "datetime-local"
701 "email" "month" "number" "search" "tel"
703 "List of input types which represent a text input.
704 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
706 (defun eww-process-text-input (beg end length
)
707 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
708 (properties (text-properties-at end
))
709 (type (plist-get form
:type
)))
711 (member type eww-text-input-types
))
714 ;; Delete some space at the end.
717 (if (equal type
"textarea")
718 (1- (line-end-position))
720 (let ((new (- end beg
)))
721 (while (and (> new
0)
722 (eql (following-char) ?
))
723 (delete-region (point) (1+ (point)))
724 (setq new
(1- new
))))
725 (set-text-properties beg end properties
)))
730 (if (equal type
"textarea")
731 (1- (line-end-position))
733 (let ((start (point)))
734 (insert (make-string length ?
))
735 (set-text-properties start
(point) properties
)))))
736 (let ((value (buffer-substring-no-properties
737 (eww-beginning-of-field)
738 (eww-end-of-field))))
739 (when (string-match " +\\'" value
)
740 (setq value
(substring value
0 (match-beginning 0))))
741 (plist-put form
:value value
)
742 (when (equal type
"password")
743 ;; Display passwords as asterisks.
744 (let ((start (eww-beginning-of-field)))
745 (put-text-property start
(+ start
(length value
))
746 'display
(make-string (length value
) ?
*))))))))
748 (defun eww-tag-textarea (cont)
749 (let ((start (point))
750 (value (or (cdr (assq :value cont
)) ""))
751 (lines (string-to-number
752 (or (cdr (assq :rows cont
))
754 (width (string-to-number
755 (or (cdr (assq :cols cont
))
761 (when (< (count-lines start
(point)) lines
)
762 (dotimes (i (- lines
(count-lines start
(point))))
764 (setq end
(point-marker))
766 (while (< (point) end
)
768 (let ((pad (- width
(- (point) (line-beginning-position)))))
770 (insert (make-string pad ?
))))
771 (add-face-text-property (line-beginning-position)
772 (point) 'eww-form-text
)
773 (put-text-property (line-beginning-position) (point)
774 'local-map eww-textarea-map
)
776 (put-text-property start
(point) 'eww-form
777 (list :eww-form eww-form
780 :name
(cdr (assq :name cont
))))))
782 (defun eww-tag-input (cont)
783 (let ((type (downcase (or (cdr (assq :type cont
))
787 ((or (equal type
"checkbox")
788 (equal type
"radio"))
789 (eww-form-checkbox cont
))
790 ((equal type
"submit")
791 (eww-form-submit cont
))
792 ((equal type
"hidden")
793 (let ((form eww-form
)
794 (name (cdr (assq :name cont
))))
795 ;; Don't add <input type=hidden> elements repeatedly.
797 (or (not (consp (car form
)))
798 (not (eq (caar form
) 'hidden
))
799 (not (equal (plist-get (cdr (car form
)) :name
)
801 (setq form
(cdr form
)))
803 (nconc eww-form
(list
806 :value
(cdr (assq :value cont
))))))))
808 (eww-form-text cont
)))
809 (unless (= start
(point))
810 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
812 (defun eww-tag-select (cont)
813 (shr-ensure-paragraph)
814 (let ((menu (list :name
(cdr (assq :name cont
))
820 (if (eq (car (car cont
)) 'optgroup
)
821 (dolist (groupelem cont
)
822 (unless (cdr (assq :disabled
(cdr groupelem
)))
823 (setq opelem
(append opelem
(cdr (cdr groupelem
))))))
825 (dolist (elem opelem
)
826 (when (eq (car elem
) 'option
)
827 (when (cdr (assq :selected
(cdr elem
)))
828 (nconc menu
(list :value
829 (cdr (assq :value
(cdr elem
))))))
830 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
831 (setq max
(max max
(length display
)))
833 :value
(cdr (assq :value
(cdr elem
)))
837 (setq options
(nreverse options
))
838 ;; If we have no selected values, default to the first value.
839 (unless (plist-get menu
:value
)
840 (nconc menu
(list :value
(nth 2 (car options
)))))
842 (let ((selected (eww-select-display menu
)))
844 (make-string (- max
(length selected
)) ?
)))
845 (put-text-property start
(point) 'eww-form menu
)
846 (add-face-text-property start
(point) 'eww-form-select
)
847 (put-text-property start
(point) 'keymap eww-select-map
)
848 (shr-ensure-paragraph))))
850 (defun eww-select-display (select)
851 (let ((value (plist-get select
:value
))
853 (dolist (elem select
)
854 (when (and (consp elem
)
855 (eq (car elem
) 'item
)
856 (equal value
(plist-get (cdr elem
) :value
)))
857 (setq display
(plist-get (cdr elem
) :display
))))
860 (defun eww-change-select ()
861 "Change the value of the select drop-down menu under point."
863 (let* ((input (get-text-property (point) 'eww-form
))
864 (completion-ignore-case t
)
867 (mapcar (lambda (elem)
869 (eq (car elem
) 'item
)
870 (cons (plist-get (cdr elem
) :display
)
871 (plist-get (cdr elem
) :value
))))
874 (completing-read "Change value: " options nil
'require-match
))
875 (inhibit-read-only t
))
876 (plist-put input
:value
(cdr (assoc-string display options t
)))
878 (eww-update-field display
))))
880 (defun eww-update-field (string)
881 (let ((properties (text-properties-at (point)))
882 (start (eww-beginning-of-field))
883 (end (1+ (eww-end-of-field))))
884 (delete-region start end
)
886 (make-string (- (- end start
) (length string
)) ?
))
887 (set-text-properties start end properties
)
890 (defun eww-toggle-checkbox ()
891 "Toggle the value of the checkbox under point."
893 (let* ((input (get-text-property (point) 'eww-form
))
894 (type (plist-get input
:type
)))
895 (if (equal type
"checkbox")
898 (if (plist-get input
:checked
)
900 (plist-put input
:checked nil
)
901 (eww-update-field eww-form-checkbox-symbol
))
902 (plist-put input
:checked t
)
903 (eww-update-field eww-form-checkbox-selected-symbol
))))
904 ;; Radio button. Switch all other buttons off.
905 (let ((name (plist-get input
:name
)))
907 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
908 (when (equal (plist-get (cdr elem
) :name
) name
)
909 (goto-char (car elem
))
910 (if (not (eq (cdr elem
) input
))
912 (plist-put input
:checked nil
)
913 (eww-update-field eww-form-checkbox-symbol
))
914 (plist-put input
:checked t
)
915 (eww-update-field eww-form-checkbox-selected-symbol
)))))
918 (defun eww-inputs (form)
919 (let ((start (point-min))
922 (< start
(point-max)))
923 (when (or (get-text-property start
'eww-form
)
924 (setq start
(next-single-property-change start
'eww-form
)))
925 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
927 (push (cons start
(get-text-property start
'eww-form
))
929 (setq start
(next-single-property-change start
'eww-form
))))
932 (defun eww-input-value (input)
933 (let ((type (plist-get input
:type
))
934 (value (plist-get input
:value
)))
936 ((equal type
"textarea")
939 (goto-char (point-min))
940 (while (re-search-forward "^ +\n\\| +$" nil t
)
941 (replace-match "" t t
))
944 (if (string-match " +\\'" value
)
945 (substring value
0 (match-beginning 0))
949 "Submit the current form."
951 (let* ((this-input (get-text-property (point) 'eww-form
))
952 (form (plist-get this-input
:eww-form
))
954 (dolist (elem (sort (eww-inputs form
)
956 (< (car o1
) (car o2
)))))
957 (let* ((input (cdr elem
))
958 (input-start (car elem
))
959 (name (plist-get input
:name
)))
962 ((member (plist-get input
:type
) '("checkbox" "radio"))
963 (when (plist-get input
:checked
)
964 (push (cons name
(plist-get input
:value
))
966 ((equal (plist-get input
:type
) "submit")
967 ;; We want the values from buttons if we hit a button if
968 ;; we hit enter on it, or if it's the first button after
969 ;; the field we did hit return on.
970 (when (or (eq input this-input
)
971 (and (not (eq input this-input
))
973 (> input-start
(point))))
975 (push (cons name
(plist-get input
:value
))
978 (push (cons name
(eww-input-value input
))
981 (when (and (consp elem
)
982 (eq (car elem
) 'hidden
))
983 (push (cons (plist-get (cdr elem
) :name
)
984 (plist-get (cdr elem
) :value
))
986 (if (and (stringp (cdr (assq :method form
)))
987 (equal (downcase (cdr (assq :method form
))) "post"))
988 (let ((url-request-method "POST")
989 (url-request-extra-headers
990 '(("Content-Type" .
"application/x-www-form-urlencoded")))
991 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
992 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
996 (if (cdr (assq :action form
))
997 (shr-expand-url (cdr (assq :action form
))
1001 (mm-url-encode-www-form-urlencoded values
))))))
1003 (defun eww-browse-with-external-browser (&optional url
)
1004 "Browse the current URL with an external browser.
1005 The browser to used is specified by the `shr-external-browser' variable."
1007 (funcall shr-external-browser
(or url eww-current-url
)))
1009 (defun eww-follow-link (&optional external mouse-event
)
1010 "Browse the URL under point.
1011 If EXTERNAL, browse the URL using `shr-external-browser'."
1012 (interactive (list current-prefix-arg last-nonmenu-event
))
1013 (mouse-set-point mouse-event
)
1014 (let ((url (get-text-property (point) 'shr-url
)))
1017 (message "No link under point"))
1018 ((string-match "^mailto:" url
)
1019 (browse-url-mail url
))
1021 (funcall shr-external-browser url
))
1022 ;; This is a #target url in the same page as the current one.
1023 ((and (url-target (url-generic-parse-url url
))
1024 (eww-same-page-p url eww-current-url
))
1026 (eww-display-html 'utf8 url eww-current-dom
))
1028 (eww-browse-url url
)))))
1030 (defun eww-same-page-p (url1 url2
)
1031 "Return non-nil if both URLs represent the same page.
1032 Differences in #targets are ignored."
1033 (let ((obj1 (url-generic-parse-url url1
))
1034 (obj2 (url-generic-parse-url url2
)))
1035 (setf (url-target obj1
) nil
)
1036 (setf (url-target obj2
) nil
)
1037 (equal (url-recreate-url obj1
) (url-recreate-url obj2
))))
1039 (defun eww-copy-page-url ()
1041 (message "%s" eww-current-url
)
1042 (kill-new eww-current-url
))
1044 (defun eww-download ()
1045 "Download URL under point to `eww-download-directory'."
1047 (let ((url (get-text-property (point) 'shr-url
)))
1049 (message "No URL under point")
1050 (url-retrieve url
'eww-download-callback
(list url
)))))
1052 (defun eww-download-callback (status url
)
1053 (unless (plist-get status
:error
)
1054 (let* ((obj (url-generic-parse-url url
))
1055 (path (car (url-path-and-query obj
)))
1056 (file (eww-make-unique-file-name (file-name-nondirectory path
)
1057 eww-download-path
)))
1059 (message "Saved %s" file
))))
1061 (defun eww-make-unique-file-name (file directory
)
1063 ((zerop (length file
))
1065 ((string-match "\\`[.]" file
)
1066 (setq file
(concat "!" file
))))
1068 (while (file-exists-p (expand-file-name file directory
))
1070 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
1071 (format "%s(%d)%s" (match-string 1 file
)
1072 count
(match-string 2 file
))
1073 (format "%s(%d)" file count
)))
1074 (setq count
(1+ count
)))
1075 (expand-file-name file directory
)))
1079 (defvar eww-bookmarks nil
)
1081 (defun eww-add-bookmark ()
1082 "Add the current page to the bookmarks."
1084 (eww-read-bookmarks)
1085 (dolist (bookmark eww-bookmarks
)
1086 (when (equal eww-current-url
1087 (plist-get bookmark
:url
))
1088 (error "Already bookmarked")))
1089 (if (y-or-n-p "bookmark this page? ")
1091 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
1092 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
1093 (push (list :url eww-current-url
1095 :time
(current-time-string))
1097 (eww-write-bookmarks)
1098 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))))
1100 (defun eww-write-bookmarks ()
1101 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
1102 (insert ";; Auto-generated file; don't edit\n")
1103 (pp eww-bookmarks
(current-buffer))))
1105 (defun eww-read-bookmarks ()
1106 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
1108 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
1110 (insert-file-contents file
)
1111 (read (current-buffer)))))))
1113 (defun eww-list-bookmarks ()
1114 "Display the bookmarks."
1116 (eww-bookmark-prepare)
1117 (pop-to-buffer "*eww bookmarks*"))
1119 (defun eww-bookmark-prepare ()
1120 (eww-read-bookmarks)
1121 (when (null eww-bookmarks
)
1122 (error "No bookmarks are defined"))
1123 (set-buffer (get-buffer-create "*eww bookmarks*"))
1125 (let ((format "%-40s %s")
1126 (inhibit-read-only t
)
1129 (setq header-line-format
(concat " " (format format
"URL" "Title")))
1130 (dolist (bookmark eww-bookmarks
)
1131 (setq start
(point))
1132 (setq url
(plist-get bookmark
:url
))
1133 (when (> (length url
) 40)
1134 (setq url
(substring url
0 40)))
1135 (insert (format format url
1136 (plist-get bookmark
:title
))
1138 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1139 (goto-char (point-min))))
1141 (defvar eww-bookmark-kill-ring nil
)
1143 (defun eww-bookmark-kill ()
1144 "Kill the current bookmark."
1146 (let* ((start (line-beginning-position))
1147 (bookmark (get-text-property start
'eww-bookmark
))
1148 (inhibit-read-only t
))
1150 (error "No bookmark on the current line"))
1152 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1153 (delete-region start
(point))
1154 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1155 (eww-write-bookmarks)))
1157 (defun eww-bookmark-yank ()
1158 "Yank a previously killed bookmark to the current line."
1160 (unless eww-bookmark-kill-ring
1161 (error "No previously killed bookmark"))
1163 (let ((inhibit-read-only t
)
1166 (insert (pop eww-bookmark-kill-ring
))
1167 (setq bookmark
(get-text-property start
'eww-bookmark
))
1168 (if (= start
(point-min))
1169 (push bookmark eww-bookmarks
)
1170 (let ((line (count-lines start
(point))))
1171 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1172 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1173 (eww-write-bookmarks)))
1175 (defun eww-bookmark-quit ()
1176 "Kill the current buffer."
1178 (kill-buffer (current-buffer)))
1180 (defun eww-bookmark-browse ()
1181 "Browse the bookmark under point in eww."
1183 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1185 (error "No bookmark on the current line"))
1186 ;; We wish to leave this window, but if it's the only window here,
1187 ;; just let it remain.
1190 (eww-browse-url (plist-get bookmark
:url
))))
1192 (defun eww-next-bookmark ()
1193 "Go to the next bookmark in the list."
1197 (unless (get-buffer "*eww bookmarks*")
1199 (eww-bookmark-prepare))
1200 (with-current-buffer (get-buffer "*eww bookmarks*")
1201 (when (and (not first
)
1204 (setq bookmark
(get-text-property (line-beginning-position)
1207 (error "No next bookmark")))
1208 (eww-browse-url (plist-get bookmark
:url
))))
1210 (defun eww-previous-bookmark ()
1211 "Go to the previous bookmark in the list."
1215 (unless (get-buffer "*eww bookmarks*")
1217 (eww-bookmark-prepare))
1218 (with-current-buffer (get-buffer "*eww bookmarks*")
1220 (goto-char (point-max))
1221 (beginning-of-line))
1222 ;; On the final line.
1226 (error "No previous bookmark")
1228 (setq bookmark
(get-text-property (line-beginning-position)
1230 (eww-browse-url (plist-get bookmark
:url
))))
1232 (defvar eww-bookmark-mode-map
1233 (let ((map (make-sparse-keymap)))
1234 (suppress-keymap map
)
1235 (define-key map
"q" 'eww-bookmark-quit
)
1236 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1237 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1238 (define-key map
"\r" 'eww-bookmark-browse
)
1241 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1242 "Mode for listing bookmarks.
1244 \\{eww-bookmark-mode-map}"
1245 (buffer-disable-undo)
1246 (setq buffer-read-only t
1251 ;;; eww.el ends here