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))
31 (defvar eww-current-url nil)
32 (defvar eww-history nil)
35 "Fetch URL and render the page."
36 (interactive "sUrl: ")
37 (url-retrieve url 'eww-render (list url)))
39 (defun eww-render (status url &optional point)
40 (let* ((headers (eww-parse-headers))
42 (mail-header-parse-content-type
43 (or (cdr (assoc "content-type" headers))
47 (or (cdr (assq 'charset (cdr content-type)))
49 (data-buffer (current-buffer)))
53 ((equal (car content-type) "text/html")
54 (eww-display-html charset url))
55 ((string-match "^image/" (car content-type))
58 (eww-display-raw charset)))
61 (kill-buffer data-buffer))))
63 (defun eww-parse-headers ()
65 (while (and (not (eobp))
67 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
68 (push (cons (downcase (match-string 1))
76 (defun eww-display-html (charset url)
77 (unless (eq charset 'utf8)
78 (decode-coding-region (point) (point-max) charset))
81 'base (list (cons 'href url))
82 (libxml-parse-html-region (point) (point-max)))))
84 (setq eww-current-url url)
85 (let ((inhibit-read-only t)
86 (shr-external-rendering-functions
87 '((form . eww-tag-form)
88 (input . eww-tag-input)
89 (submit . eww-tag-submit))))
90 (shr-insert-document document)
91 (eww-convert-widgets))
92 (goto-char (point-min))))
94 (defun eww-display-raw (charset)
95 (let ((data (buffer-substring (point) (point-max))))
97 (let ((inhibit-read-only t))
99 (goto-char (point-min))))
101 (defun eww-display-image ()
102 (let ((data (buffer-substring (point) (point-max))))
104 (let ((inhibit-read-only t))
105 (shr-put-image data nil))
106 (goto-char (point-min))))
108 (defun eww-setup-buffer ()
109 (pop-to-buffer (get-buffer-create "*eww*"))
111 (setq widget-field-list nil)
112 (let ((inhibit-read-only t))
117 (let ((map (make-sparse-keymap)))
118 (suppress-keymap map)
119 (define-key map "q" 'eww-quit)
120 (define-key map [tab] 'widget-forward)
121 (define-key map [backtab] 'widget-backward)
122 (define-key map [delete] 'scroll-down-command)
123 (define-key map "\177" 'scroll-down-command)
124 (define-key map " " 'scroll-up-command)
125 (define-key map "p" 'eww-previous-url)
126 ;;(define-key map "n" 'eww-next-url)
130 "Mode for browsing the web.
134 (setq major-mode 'eww-mode
136 (set (make-local-variable 'eww-current-url) 'author)
137 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
138 (setq buffer-read-only t)
139 (use-local-map eww-mode-map))
141 (defun eww-browse-url (url &optional new-window)
142 (push (list eww-current-url (point))
147 "Exit the Emacs Web Wowser."
149 (setq eww-history nil)
150 (kill-buffer (current-buffer)))
152 (defun eww-previous-url ()
153 "Go to the previously displayed page."
155 (when (zerop (length eww-history))
156 (error "No previous page"))
157 (let ((prev (pop eww-history)))
158 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
162 (defvar eww-form nil)
164 (defun eww-tag-form (cont)
166 (list (assq :method cont)
167 (assq :action cont)))
169 (shr-ensure-paragraph)
171 (shr-ensure-paragraph)
172 (put-text-property start (1+ start)
173 'eww-form eww-form)))
175 (defun eww-tag-input (cont)
176 (push (cons (cdr (assq :name cont))
177 (cdr (assq :value cont)))
179 (let ((start (point))
182 :size (string-to-number
183 (or (cdr (assq :size cont))
185 :value (or "____" (cdr (assq :value cont)) "")
186 :action 'eww-submit)))
187 (apply 'widget-create widget)
189 (put-text-property start (point) 'eww-widget widget)))
191 (defun eww-convert-widgets ()
192 (let ((start (point-min))
194 (while (setq start (next-single-property-change start 'eww-widget))
195 (setq widget (get-text-property start 'eww-widget))
197 (delete-region start (next-single-property-change start 'eww-widget))
198 (apply 'widget-create widget)
199 (put-text-property start (point) 'not-read-only t))