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 (defface eww-form-submit
60 '((((type x w32 ns
) (class color
)) ; Like default mode line
61 :box
(:line-width
2 :style released-button
)
62 :background
"#808080" :foreground
"black"))
63 "Face for eww buffer buttons."
67 (defface eww-form-checkbox
68 '((((type x w32 ns
) (class color
)) ; Like default mode line
69 :box
(:line-width
2 :style released-button
)
70 :background
"lightgrey" :foreground
"black"))
71 "Face for eww buffer buttons."
75 (defface eww-form-select
76 '((((type x w32 ns
) (class color
)) ; Like default mode line
77 :box
(:line-width
2 :style released-button
)
78 :background
"lightgrey" :foreground
"black"))
79 "Face for eww buffer buttons."
83 (defface eww-form-text
84 '((t (:background
"#505050"
86 :box
(:line-width
1))))
87 "Face for eww text inputs."
91 (defvar eww-current-url nil
)
92 (defvar eww-current-title
""
93 "Title of current page.")
94 (defvar eww-history nil
)
95 (defvar eww-history-position
0)
97 (defvar eww-next-url nil
)
98 (defvar eww-previous-url nil
)
99 (defvar eww-up-url nil
)
100 (defvar eww-home-url nil
)
101 (defvar eww-start-url nil
)
102 (defvar eww-contents-url nil
)
106 "Fetch URL and render the page.
107 If the input doesn't look like an URL or a domain name, the
108 word(s) will be searched for via `eww-search-prefix'."
109 (interactive "sEnter URL or keywords: ")
110 (if (and (= (length (split-string url
)) 1)
111 (> (length (split-string url
"\\.")) 1))
113 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
114 (setq url
(concat "http://" url
)))
115 ;; some site don't redirect final /
116 (when (string= (url-filename (url-generic-parse-url url
)) "")
117 (setq url
(concat url
"/"))))
118 (unless (string-match-p "\\'file:" url
)
119 (setq url
(concat eww-search-prefix
120 (replace-regexp-in-string " " "+" url
)))))
121 (url-retrieve url
'eww-render
(list url
)))
124 (defun eww-open-file (file)
125 "Render a file using EWW."
126 (interactive "fFile: ")
127 (eww (concat "file://" (expand-file-name file
))))
129 (defun eww-render (status url
&optional point
)
130 (let ((redirect (plist-get status
:redirect
)))
132 (setq url redirect
)))
133 (set (make-local-variable 'eww-next-url
) nil
)
134 (set (make-local-variable 'eww-previous-url
) nil
)
135 (set (make-local-variable 'eww-up-url
) nil
)
136 (set (make-local-variable 'eww-home-url
) nil
)
137 (set (make-local-variable 'eww-start-url
) nil
)
138 (set (make-local-variable 'eww-contents-url
) nil
)
139 (let* ((headers (eww-parse-headers))
141 (and (string-match "#\\(.*\\)" url
)
142 (match-string 1 url
)))
144 (mail-header-parse-content-type
145 (or (cdr (assoc "content-type" headers
))
149 (or (cdr (assq 'charset
(cdr content-type
)))
150 (eww-detect-charset (equal (car content-type
)
153 (data-buffer (current-buffer)))
157 ((equal (car content-type
) "text/html")
158 (eww-display-html charset url
))
159 ((string-match "^image/" (car content-type
))
163 (setq eww-history-position
0)
168 (let ((point (next-single-property-change
169 (point-min) 'shr-target-id
)))
171 (goto-char (1+ point
)))))))
172 (kill-buffer data-buffer
))))
174 (defun eww-parse-headers ()
176 (goto-char (point-min))
177 (while (and (not (eobp))
179 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
180 (push (cons (downcase (match-string 1))
188 (defun eww-detect-charset (html-p)
189 (let ((case-fold-search t
)
193 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t
)
197 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
200 (declare-function libxml-parse-html-region
"xml.c"
201 (start end
&optional base-url
))
203 (defun eww-display-html (charset url
)
204 (or (fboundp 'libxml-parse-html-region
)
205 (error "This function requires Emacs to be compiled with libxml2"))
206 (unless (eq charset
'utf8
)
208 (decode-coding-region (point) (point-max) charset
)
209 (coding-system-error nil
)))
212 'base
(list (cons 'href url
))
213 (libxml-parse-html-region (point) (point-max)))))
215 (setq eww-current-url url
)
216 (eww-update-header-line-format)
217 (let ((inhibit-read-only t
)
218 (after-change-functions nil
)
220 (shr-external-rendering-functions
221 '((title . eww-tag-title
)
222 (form . eww-tag-form
)
223 (input . eww-tag-input
)
224 (textarea . eww-tag-textarea
)
225 (body . eww-tag-body
)
226 (select . eww-tag-select
)
227 (link . eww-tag-link
)
229 (shr-insert-document document
))
230 (goto-char (point-min))))
232 (defun eww-handle-link (cont)
233 (let* ((rel (assq :rel cont
))
234 (href (assq :href cont
))
236 ;; The text associated with :rel is case-insensitive.
237 (if rel
(downcase (cdr rel
)))
238 '(("next" . eww-next-url
)
239 ;; Texinfo uses "previous", but HTML specifies
240 ;; "prev", so recognize both.
241 ("previous" . eww-previous-url
)
242 ("prev" . eww-previous-url
)
243 ;; HTML specifies "start" but also "contents",
244 ;; and Gtk seems to use "home". Recognize
245 ;; them all; but store them in different
246 ;; variables so that we can readily choose the
248 ("start" . eww-start-url
)
249 ("home" . eww-home-url
)
250 ("contents" . eww-contents-url
)
251 ("up" . eww-up-url
)))))
254 (set (cdr where
) (cdr href
)))))
256 (defun eww-tag-link (cont)
257 (eww-handle-link cont
)
260 (defun eww-tag-a (cont)
261 (eww-handle-link cont
)
264 (defun eww-update-header-line-format ()
265 (if eww-header-line-format
266 (setq header-line-format
267 (replace-regexp-in-string
269 (format-spec eww-header-line-format
270 `((?u .
,eww-current-url
)
271 (?t .
,eww-current-title
)))))
272 (setq header-line-format nil
)))
274 (defun eww-tag-title (cont)
275 (setq eww-current-title
"")
277 (when (eq (car sub
) 'text
)
278 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
279 (eww-update-header-line-format))
281 (defun eww-tag-body (cont)
282 (let* ((start (point))
283 (fgcolor (cdr (or (assq :fgcolor cont
)
285 (bgcolor (cdr (assq :bgcolor cont
)))
286 (shr-stylesheet (list (cons 'color fgcolor
)
287 (cons 'background-color bgcolor
))))
289 (eww-colorize-region start
(point) fgcolor bgcolor
)))
291 (defun eww-colorize-region (start end fg
&optional bg
)
293 (let ((new-colors (shr-color-check fg bg
)))
296 (add-face-text-property start end
297 (list :foreground
(cadr new-colors
))
300 (add-face-text-property start end
301 (list :background
(car new-colors
))
304 (defun eww-display-raw ()
305 (let ((data (buffer-substring (point) (point-max))))
307 (let ((inhibit-read-only t
))
309 (goto-char (point-min))))
311 (defun eww-display-image ()
312 (let ((data (shr-parse-image-data)))
314 (let ((inhibit-read-only t
))
315 (shr-put-image data nil
))
316 (goto-char (point-min))))
318 (defun eww-setup-buffer ()
319 (switch-to-buffer (get-buffer-create "*eww*"))
320 (let ((inhibit-read-only t
))
323 (unless (eq major-mode
'eww-mode
)
327 (let ((map (make-sparse-keymap)))
328 (suppress-keymap map
)
329 (define-key map
"q" 'eww-quit
)
330 (define-key map
"g" 'eww-reload
)
331 (define-key map
[tab] 'shr-next-link)
332 (define-key map [backtab] 'shr-previous-link)
333 (define-key map [delete] 'scroll-down-command)
334 (define-key map "\177" 'scroll-down-command)
335 (define-key map " " 'scroll-up-command)
336 (define-key map "l" 'eww-back-url)
337 (define-key map "f" 'eww-forward-url)
338 (define-key map "n" 'eww-next-url)
339 (define-key map "p" 'eww-previous-url)
340 (define-key map "u" 'eww-up-url)
341 (define-key map "t" 'eww-top-url)
342 (define-key map "&" 'eww-browse-with-external-browser)
343 (define-key map "d" 'eww-download)
344 (define-key map "w" 'eww-copy-page-url)
345 (define-key map "C" 'url-cookie-list)
347 (define-key map "b" 'eww-add-bookmark)
348 (define-key map "B" 'eww-list-bookmarks)
349 (define-key map [(meta n)] 'eww-next-bookmark)
350 (define-key map [(meta p)] 'eww-previous-bookmark)
352 (easy-menu-define nil map ""
355 ["Reload" eww-reload t]
356 ["Back to previous page" eww-back-url
357 :active (not (zerop (length eww-history)))]
358 ["Forward to next page" eww-forward-url
359 :active (not (zerop eww-history-position))]
360 ["Browse with external browser" eww-browse-with-external-browser t]
361 ["Download" eww-download t]
362 ["Copy page URL" eww-copy-page-url t]
363 ["Add bookmark" eww-add-bookmark t]
364 ["List bookmarks" eww-copy-page-url t]
365 ["List cookies" url-cookie-list t]))
368 (define-derived-mode eww-mode nil "eww"
369 "Mode for browsing the web.
372 (set (make-local-variable 'eww-current-url) 'author)
373 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
374 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
375 (set (make-local-variable 'eww-history) nil)
376 (set (make-local-variable 'eww-history-position) 0)
377 (buffer-disable-undo)
378 ;;(setq buffer-read-only t)
381 (defun eww-save-history ()
382 (push (list :url eww-current-url
383 :title eww-current-title
385 :text (buffer-string))
389 (defun eww-browse-url (url &optional _new-window)
390 (when (and (equal major-mode 'eww-mode)
396 "Exit the Emacs Web Wowser."
398 (setq eww-history nil)
399 (kill-buffer (current-buffer)))
401 (defun eww-back-url ()
402 "Go to the previously displayed page."
404 (when (>= eww-history-position (length eww-history))
405 (error "No previous page"))
407 (setq eww-history-position (+ eww-history-position 2))
408 (eww-restore-history (elt eww-history (1- eww-history-position))))
410 (defun eww-forward-url ()
411 "Go to the next displayed page."
413 (when (zerop eww-history-position)
414 (error "No next page"))
416 (eww-restore-history (elt eww-history (1- eww-history-position))))
418 (defun eww-restore-history (elem)
419 (let ((inhibit-read-only t))
421 (insert (plist-get elem :text))
422 (goto-char (plist-get elem :point))
423 (setq eww-current-url (plist-get elem :url)
424 eww-current-title (plist-get elem :title))))
426 (defun eww-next-url ()
427 "Go to the page marked `next'.
428 A page is marked `next' if rel=\"next\" appears in a <link>
432 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
433 (error "No `next' on this page")))
435 (defun eww-previous-url ()
436 "Go to the page marked `previous'.
437 A page is marked `previous' if rel=\"previous\" appears in a <link>
441 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
442 (error "No `previous' on this page")))
445 "Go to the page marked `up'.
446 A page is marked `up' if rel=\"up\" appears in a <link>
450 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
451 (error "No `up' on this page")))
453 (defun eww-top-url ()
454 "Go to the page marked `top'.
455 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
456 appears in a <link> or <a> tag."
458 (let ((best-url (or eww-start-url
462 (eww-browse-url (shr-expand-url best-url eww-current-url))
463 (error "No `top' for this page"))))
466 "Reload the current page."
468 (url-retrieve eww-current-url 'eww-render
469 (list eww-current-url (point))))
473 (defvar eww-form nil)
475 (defvar eww-submit-map
476 (let ((map (make-sparse-keymap)))
477 (define-key map "\r" 'eww-submit)
478 (define-key map [(control c) (control c)] 'eww-submit)
481 (defvar eww-checkbox-map
482 (let ((map (make-sparse-keymap)))
483 (define-key map [space] 'eww-toggle-checkbox)
484 (define-key map "\r" 'eww-toggle-checkbox)
485 (define-key map [(control c) (control c)] 'eww-submit)
489 (let ((map (make-keymap)))
490 (set-keymap-parent map text-mode-map)
491 (define-key map "\r" 'eww-submit)
492 (define-key map [(control a)] 'eww-beginning-of-text)
493 (define-key map [(control c) (control c)] 'eww-submit)
494 (define-key map [(control e)] 'eww-end-of-text)
495 (define-key map [tab] 'shr-next-link
)
496 (define-key map
[backtab] 'shr-previous-link)
499 (defvar eww-textarea-map
500 (let ((map (make-keymap)))
501 (set-keymap-parent map text-mode-map)
502 (define-key map "\r" 'forward-line)
503 (define-key map [(control c) (control c)] 'eww-submit)
504 (define-key map [tab] 'shr-next-link)
505 (define-key map [backtab] 'shr-previous-link
)
508 (defvar eww-select-map
509 (let ((map (make-sparse-keymap)))
510 (define-key map
"\r" 'eww-change-select
)
511 (define-key map
[(control c
) (control c
)] 'eww-submit
)
514 (defun eww-beginning-of-text ()
515 "Move to the start of the input field."
517 (goto-char (eww-beginning-of-field)))
519 (defun eww-end-of-text ()
520 "Move to the end of the text in the input field."
522 (goto-char (eww-end-of-field))
523 (let ((start (eww-beginning-of-field)))
524 (while (and (equal (following-char) ?
)
527 (when (> (point) start
)
530 (defun eww-beginning-of-field ()
534 ((not (eq (get-text-property (point) 'eww-form
)
535 (get-text-property (1- (point)) 'eww-form
)))
538 (previous-single-property-change
539 (point) 'eww-form nil
(point-min)))))
541 (defun eww-end-of-field ()
542 (1- (next-single-property-change
543 (point) 'eww-form nil
(point-max))))
545 (defun eww-tag-form (cont)
547 (list (assq :method cont
)
548 (assq :action cont
)))
550 (shr-ensure-paragraph)
555 (when (> (point) start
)
556 (put-text-property start
(1+ start
)
557 'eww-form eww-form
))))
559 (defun eww-form-submit (cont)
560 (let ((start (point))
561 (value (cdr (assq :value cont
))))
563 (if (zerop (length value
))
567 (add-face-text-property start
(point) 'eww-form-submit
)
568 (put-text-property start
(point) 'eww-form
569 (list :eww-form eww-form
572 :name
(cdr (assq :name cont
))))
573 (put-text-property start
(point) 'keymap eww-submit-map
)
576 (defun eww-form-checkbox (cont)
577 (let ((start (point)))
578 (if (cdr (assq :checked cont
))
581 (add-face-text-property start
(point) 'eww-form-checkbox
)
582 (put-text-property start
(point) 'eww-form
583 (list :eww-form eww-form
584 :value
(cdr (assq :value cont
))
585 :type
(downcase (cdr (assq :type cont
)))
586 :checked
(cdr (assq :checked cont
))
587 :name
(cdr (assq :name cont
))))
588 (put-text-property start
(point) 'keymap eww-checkbox-map
)
591 (defun eww-form-text (cont)
592 (let ((start (point))
593 (type (downcase (or (cdr (assq :type cont
))
595 (value (or (cdr (assq :value cont
)) ""))
596 (width (string-to-number
597 (or (cdr (assq :size cont
))
600 (when (< (length value
) width
)
601 (insert (make-string (- width
(length value
)) ?
)))
602 (put-text-property start
(point) 'face
'eww-form-text
)
603 (put-text-property start
(point) 'local-map eww-text-map
)
604 (put-text-property start
(point) 'inhibit-read-only t
)
605 (put-text-property start
(point) 'eww-form
606 (list :eww-form eww-form
609 :name
(cdr (assq :name cont
))))
612 (defun eww-process-text-input (beg end length
)
613 (let* ((form (get-text-property (min (1+ end
) (point-max)) 'eww-form
))
614 (properties (text-properties-at end
))
615 (type (plist-get form
:type
)))
617 (member type
'("text" "password" "textarea")))
620 ;; Delete some space at the end.
623 (if (equal type
"textarea")
624 (1- (line-end-position))
626 (let ((new (- end beg
)))
627 (while (and (> new
0)
628 (eql (following-char) ?
))
629 (delete-region (point) (1+ (point)))
630 (setq new
(1- new
))))
631 (set-text-properties beg end properties
)))
636 (if (equal type
"textarea")
637 (1- (line-end-position))
639 (let ((start (point)))
640 (insert (make-string length ?
))
641 (set-text-properties start
(point) properties
)))))
642 (let ((value (buffer-substring-no-properties
643 (eww-beginning-of-field)
644 (eww-end-of-field))))
645 (when (string-match " +\\'" value
)
646 (setq value
(substring value
0 (match-beginning 0))))
647 (plist-put form
:value value
)
648 (when (equal type
"password")
649 ;; Display passwords as asterisks.
650 (let ((start (eww-beginning-of-field)))
651 (put-text-property start
(+ start
(length value
))
652 'display
(make-string (length value
) ?
*))))))))
654 (defun eww-tag-textarea (cont)
655 (let ((start (point))
656 (value (or (cdr (assq :value cont
)) ""))
657 (lines (string-to-number
658 (or (cdr (assq :rows cont
))
660 (width (string-to-number
661 (or (cdr (assq :cols cont
))
667 (when (< (count-lines start
(point)) lines
)
668 (dotimes (i (- lines
(count-lines start
(point))))
670 (setq end
(point-marker))
672 (while (< (point) end
)
674 (let ((pad (- width
(- (point) (line-beginning-position)))))
676 (insert (make-string pad ?
))))
677 (add-face-text-property (line-beginning-position)
678 (point) 'eww-form-text
)
679 (put-text-property (line-beginning-position) (point)
680 'local-map eww-textarea-map
)
682 (put-text-property start
(point) 'eww-form
683 (list :eww-form eww-form
686 :name
(cdr (assq :name cont
))))))
688 (defun eww-tag-input (cont)
689 (let ((type (downcase (or (cdr (assq :type cont
))
693 ((or (equal type
"checkbox")
694 (equal type
"radio"))
695 (eww-form-checkbox cont
))
696 ((equal type
"submit")
697 (eww-form-submit cont
))
698 ((equal type
"hidden")
699 (let ((form eww-form
)
700 (name (cdr (assq :name cont
))))
701 ;; Don't add <input type=hidden> elements repeatedly.
703 (or (not (consp (car form
)))
704 (not (eq (caar form
) 'hidden
))
705 (not (equal (plist-get (cdr (car form
)) :name
)
707 (setq form
(cdr form
)))
709 (nconc eww-form
(list
712 :value
(cdr (assq :value cont
))))))))
714 (eww-form-text cont
)))
715 (unless (= start
(point))
716 (put-text-property start
(1+ start
) 'help-echo
"Input field"))))
718 (defun eww-tag-select (cont)
719 (shr-ensure-paragraph)
720 (let ((menu (list :name
(cdr (assq :name cont
))
726 (when (eq (car elem
) 'option
)
727 (when (cdr (assq :selected
(cdr elem
)))
728 (nconc menu
(list :value
729 (cdr (assq :value
(cdr elem
))))))
730 (let ((display (or (cdr (assq 'text
(cdr elem
))) "")))
731 (setq max
(max max
(length display
)))
733 :value
(cdr (assq :value
(cdr elem
)))
737 (setq options
(nreverse options
))
738 ;; If we have no selected values, default to the first value.
739 (unless (plist-get menu
:value
)
740 (nconc menu
(list :value
(nth 2 (car options
)))))
742 (let ((selected (eww-select-display menu
)))
744 (make-string (- max
(length selected
)) ?
)))
745 (put-text-property start
(point) 'eww-form menu
)
746 (add-face-text-property start
(point) 'eww-form-select
)
747 (put-text-property start
(point) 'keymap eww-select-map
)
748 (shr-ensure-paragraph))))
750 (defun eww-select-display (select)
751 (let ((value (plist-get select
:value
))
753 (dolist (elem select
)
754 (when (and (consp elem
)
755 (eq (car elem
) 'item
)
756 (equal value
(plist-get (cdr elem
) :value
)))
757 (setq display
(plist-get (cdr elem
) :display
))))
760 (defun eww-change-select ()
761 "Change the value of the select drop-down menu under point."
763 (let* ((input (get-text-property (point) 'eww-form
))
764 (completion-ignore-case t
)
767 (mapcar (lambda (elem)
769 (eq (car elem
) 'item
)
770 (cons (plist-get (cdr elem
) :display
)
771 (plist-get (cdr elem
) :value
))))
774 (completing-read "Change value: " options nil
'require-match
))
775 (inhibit-read-only t
))
776 (plist-put input
:value
(cdr (assoc-string display options t
)))
778 (eww-update-field display
))))
780 (defun eww-update-field (string)
781 (let ((properties (text-properties-at (point)))
782 (start (eww-beginning-of-field))
783 (end (1+ (eww-end-of-field))))
784 (delete-region start end
)
786 (make-string (- (- end start
) (length string
)) ?
))
787 (set-text-properties start end properties
)
790 (defun eww-toggle-checkbox ()
791 "Toggle the value of the checkbox under point."
793 (let* ((input (get-text-property (point) 'eww-form
))
794 (type (plist-get input
:type
)))
795 (if (equal type
"checkbox")
798 (if (plist-get input
:checked
)
800 (plist-put input
:checked nil
)
801 (eww-update-field "[ ]"))
802 (plist-put input
:checked t
)
803 (eww-update-field "[X]"))))
804 ;; Radio button. Switch all other buttons off.
805 (let ((name (plist-get input
:name
)))
807 (dolist (elem (eww-inputs (plist-get input
:eww-form
)))
808 (when (equal (plist-get (cdr elem
) :name
) name
)
809 (goto-char (car elem
))
810 (if (not (eq (cdr elem
) input
))
812 (plist-put input
:checked nil
)
813 (eww-update-field "[ ]"))
814 (plist-put input
:checked t
)
815 (eww-update-field "[X]")))))
818 (defun eww-inputs (form)
819 (let ((start (point-min))
822 (< start
(point-max)))
823 (when (or (get-text-property start
'eww-form
)
824 (setq start
(next-single-property-change start
'eww-form
)))
825 (when (eq (plist-get (get-text-property start
'eww-form
) :eww-form
)
827 (push (cons start
(get-text-property start
'eww-form
))
829 (setq start
(next-single-property-change start
'eww-form
))))
832 (defun eww-input-value (input)
833 (let ((type (plist-get input
:type
))
834 (value (plist-get input
:value
)))
836 ((equal type
"textarea")
839 (goto-char (point-min))
840 (while (re-search-forward "^ +\n\\| +$" nil t
)
841 (replace-match "" t t
))
844 (if (string-match " +\\'" value
)
845 (substring value
0 (match-beginning 0))
849 "Submit the current form."
851 (let* ((this-input (get-text-property (point) 'eww-form
))
852 (form (plist-get this-input
:eww-form
))
854 (dolist (elem (sort (eww-inputs form
)
856 (< (car o1
) (car o2
)))))
857 (let* ((input (cdr elem
))
858 (input-start (car elem
))
859 (name (plist-get input
:name
)))
862 ((member (plist-get input
:type
) '("checkbox" "radio"))
863 (when (plist-get input
:checked
)
864 (push (cons name
(plist-get input
:value
))
866 ((equal (plist-get input
:type
) "submit")
867 ;; We want the values from buttons if we hit a button if
868 ;; we hit enter on it, or if it's the first button after
869 ;; the field we did hit return on.
870 (when (or (eq input this-input
)
871 (and (not (eq input this-input
))
873 (> input-start
(point))))
875 (push (cons name
(plist-get input
:value
))
878 (push (cons name
(eww-input-value input
))
881 (when (and (consp elem
)
882 (eq (car elem
) 'hidden
))
883 (push (cons (plist-get (cdr elem
) :name
)
884 (plist-get (cdr elem
) :value
))
886 (if (and (stringp (cdr (assq :method form
)))
887 (equal (downcase (cdr (assq :method form
))) "post"))
888 (let ((url-request-method "POST")
889 (url-request-extra-headers
890 '(("Content-Type" .
"application/x-www-form-urlencoded")))
891 (url-request-data (mm-url-encode-www-form-urlencoded values
)))
892 (eww-browse-url (shr-expand-url (cdr (assq :action form
))
896 (if (cdr (assq :action form
))
897 (shr-expand-url (cdr (assq :action form
))
901 (mm-url-encode-www-form-urlencoded values
))))))
903 (defun eww-browse-with-external-browser ()
904 "Browse the current URL with an external browser.
905 The browser to used is specified by the `shr-external-browser' variable."
907 (funcall shr-external-browser eww-current-url
))
909 (defun eww-copy-page-url ()
911 (message "%s" eww-current-url
)
912 (kill-new eww-current-url
))
914 (defun eww-download ()
915 "Download URL under point to `eww-download-directory'."
917 (let ((url (get-text-property (point) 'shr-url
)))
919 (message "No URL under point")
920 (url-retrieve url
'eww-download-callback
(list url
)))))
922 (defun eww-download-callback (status url
)
923 (unless (plist-get status
:error
)
924 (let* ((obj (url-generic-parse-url url
))
925 (path (car (url-path-and-query obj
)))
926 (file (eww-make-unique-file-name (file-name-nondirectory path
)
929 (message "Saved %s" file
))))
931 (defun eww-make-unique-file-name (file directory
)
933 ((zerop (length file
))
935 ((string-match "\\`[.]" file
)
936 (setq file
(concat "!" file
))))
938 (while (file-exists-p (expand-file-name file directory
))
940 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file
)
941 (format "%s(%d)%s" (match-string 1 file
)
942 count
(match-string 2 file
))
943 (format "%s(%d)" file count
)))
944 (setq count
(1+ count
)))
945 (expand-file-name file directory
)))
949 (defvar eww-bookmarks nil
)
951 (defun eww-add-bookmark ()
952 "Add the current page to the bookmarks."
955 (dolist (bookmark eww-bookmarks
)
956 (when (equal eww-current-url
957 (plist-get bookmark
:url
))
958 (error "Already bookmarked")))
959 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title
)))
960 (setq title
(replace-regexp-in-string "\\` +\\| +\\'" "" title
))
961 (push (list :url eww-current-url
963 :time
(current-time-string))
965 (eww-write-bookmarks)
966 (message "Bookmarked %s (%s)" eww-current-url eww-current-title
))
968 (defun eww-write-bookmarks ()
969 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory
)
970 (insert ";; Auto-generated file; don't edit\n")
971 (pp eww-bookmarks
(current-buffer))))
973 (defun eww-read-bookmarks ()
974 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory
)))
976 (unless (zerop (or (nth 7 (file-attributes file
)) 0))
978 (insert-file-contents file
)
979 (read (current-buffer)))))))
981 (defun eww-list-bookmarks ()
982 "Display the bookmarks."
984 (eww-bookmark-prepare)
985 (pop-to-buffer "*eww bookmarks*"))
987 (defun eww-bookmark-prepare ()
989 (when (null eww-bookmarks
)
990 (error "No bookmarks are defined"))
991 (set-buffer (get-buffer-create "*eww bookmarks*"))
993 (let ((format "%-40s %s")
994 (inhibit-read-only t
)
997 (setq header-line-format
(concat " " (format format
"URL" "Title")))
998 (dolist (bookmark eww-bookmarks
)
1000 (setq url
(plist-get bookmark
:url
))
1001 (when (> (length url
) 40)
1002 (setq url
(substring url
0 40)))
1003 (insert (format format url
1004 (plist-get bookmark
:title
))
1006 (put-text-property start
(1+ start
) 'eww-bookmark bookmark
))
1007 (goto-char (point-min))))
1009 (defvar eww-bookmark-kill-ring nil
)
1011 (defun eww-bookmark-kill ()
1012 "Kill the current bookmark."
1014 (let* ((start (line-beginning-position))
1015 (bookmark (get-text-property start
'eww-bookmark
))
1016 (inhibit-read-only t
))
1018 (error "No bookmark on the current line"))
1020 (push (buffer-substring start
(point)) eww-bookmark-kill-ring
)
1021 (delete-region start
(point))
1022 (setq eww-bookmarks
(delq bookmark eww-bookmarks
))
1023 (eww-write-bookmarks)))
1025 (defun eww-bookmark-yank ()
1026 "Yank a previously killed bookmark to the current line."
1028 (unless eww-bookmark-kill-ring
1029 (error "No previously killed bookmark"))
1031 (let ((inhibit-read-only t
)
1034 (insert (pop eww-bookmark-kill-ring
))
1035 (setq bookmark
(get-text-property start
'eww-bookmark
))
1036 (if (= start
(point-min))
1037 (push bookmark eww-bookmarks
)
1038 (let ((line (count-lines start
(point))))
1039 (setcdr (nthcdr (1- line
) eww-bookmarks
)
1040 (cons bookmark
(nthcdr line eww-bookmarks
)))))
1041 (eww-write-bookmarks)))
1043 (defun eww-bookmark-quit ()
1044 "Kill the current buffer."
1046 (kill-buffer (current-buffer)))
1048 (defun eww-bookmark-browse ()
1049 "Browse the bookmark under point in eww."
1051 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark
)))
1053 (error "No bookmark on the current line"))
1054 ;; We wish to leave this window, but if it's the only window here,
1055 ;; just let it remain.
1058 (eww (plist-get bookmark
:url
))))
1060 (defun eww-next-bookmark ()
1061 "Go to the next bookmark in the list."
1065 (unless (get-buffer "*eww bookmarks*")
1067 (eww-bookmark-prepare))
1068 (with-current-buffer (get-buffer "*eww bookmarks*")
1069 (when (and (not first
)
1072 (setq bookmark
(get-text-property (line-beginning-position)
1075 (error "No next bookmark")))
1076 (eww-browse-url (plist-get bookmark
:url
))))
1078 (defun eww-previous-bookmark ()
1079 "Go to the previous bookmark in the list."
1083 (unless (get-buffer "*eww bookmarks*")
1085 (eww-bookmark-prepare))
1086 (with-current-buffer (get-buffer "*eww bookmarks*")
1088 (goto-char (point-max))
1089 (beginning-of-line))
1090 ;; On the final line.
1094 (error "No previous bookmark")
1096 (setq bookmark
(get-text-property (line-beginning-position)
1098 (eww-browse-url (plist-get bookmark
:url
))))
1100 (defvar eww-bookmark-mode-map
1101 (let ((map (make-sparse-keymap)))
1102 (suppress-keymap map
)
1103 (define-key map
"q" 'eww-bookmark-quit
)
1104 (define-key map
[(control k
)] 'eww-bookmark-kill
)
1105 (define-key map
[(control y
)] 'eww-bookmark-yank
)
1106 (define-key map
"\r" 'eww-bookmark-browse
)
1109 (define-derived-mode eww-bookmark-mode nil
"eww bookmarks"
1110 "Mode for listing bookmarks.
1112 \\{eww-bookmark-mode-map}"
1113 (buffer-disable-undo)
1114 (setq buffer-read-only t
1119 ;;; eww.el ends here