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."
46 (defvar eww-current-url nil)
47 (defvar eww-current-title ""
48 "Title of current page.")
49 (defvar eww-history nil)
53 "Fetch URL and render the page."
54 (interactive "sUrl: ")
55 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
56 (setq url (concat "http://" url)))
57 (url-retrieve url 'eww-render (list url)))
59 (defun eww-detect-charset (html-p)
60 (let ((case-fold-search t)
64 "<meta[\t\n\r ]+[^>]*charset=\\([^\t\n\r \"/>]+\\)" nil t)
68 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
71 (defun eww-render (status url &optional point)
72 (let ((redirect (plist-get status :redirect)))
75 (let* ((headers (eww-parse-headers))
77 (and (string-match "#\\(.*\\)" url)
78 (match-string 1 url)))
80 (mail-header-parse-content-type
81 (or (cdr (assoc "content-type" headers))
85 (or (cdr (assq 'charset (cdr content-type)))
86 (eww-detect-charset (equal (car content-type)
89 (data-buffer (current-buffer)))
93 ((equal (car content-type) "text/html")
94 (eww-display-html charset url))
95 ((string-match "^image/" (car content-type))
98 (eww-display-raw charset)))
103 (let ((point (next-single-property-change
104 (point-min) 'shr-target-id)))
106 (goto-char (1+ point)))))))
107 (kill-buffer data-buffer))))
109 (defun eww-parse-headers ()
111 (goto-char (point-min))
112 (while (and (not (eobp))
114 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
115 (push (cons (downcase (match-string 1))
123 (defun eww-display-html (charset url)
124 (unless (eq charset 'utf8)
125 (decode-coding-region (point) (point-max) charset))
128 'base (list (cons 'href url))
129 (libxml-parse-html-region (point) (point-max)))))
131 (setq eww-current-url url)
132 (eww-update-header-line-format)
133 (let ((inhibit-read-only t)
135 (shr-external-rendering-functions
136 '((title . eww-tag-title)
137 (form . eww-tag-form)
138 (input . eww-tag-input)
139 (textarea . eww-tag-textarea)
140 (body . eww-tag-body)
141 (select . eww-tag-select))))
142 (shr-insert-document document)
143 (eww-convert-widgets))
144 (goto-char (point-min))))
146 (defun eww-update-header-line-format ()
147 (if eww-header-line-format
148 (setq header-line-format (format-spec eww-header-line-format
149 `((?u . ,eww-current-url)
150 (?t . ,eww-current-title))))
151 (setq header-line-format nil)))
153 (defun eww-tag-title (cont)
154 (setq eww-current-title "")
156 (when (eq (car sub) 'text)
157 (setq eww-current-title (concat eww-current-title (cdr sub)))))
158 (eww-update-header-line-format))
160 (defun eww-tag-body (cont)
161 (let* ((start (point))
162 (fgcolor (cdr (or (assq :fgcolor cont)
164 (bgcolor (cdr (assq :bgcolor cont)))
165 (shr-stylesheet (list (cons 'color fgcolor)
166 (cons 'background-color bgcolor))))
168 (eww-colorize-region start (point) fgcolor bgcolor)))
170 (defun eww-colorize-region (start end fg &optional bg)
172 (let ((new-colors (shr-color-check fg bg)))
175 (eww-put-color start end :foreground (cadr new-colors)))
177 (eww-put-color start end :background (car new-colors)))))))
179 (defun eww-put-color (start end type color)
180 (shr-put-color-1 start end type color))
182 (defun eww-display-raw (charset)
183 (let ((data (buffer-substring (point) (point-max))))
185 (let ((inhibit-read-only t))
187 (goto-char (point-min))))
189 (defun eww-display-image ()
190 (let ((data (buffer-substring (point) (point-max))))
192 (let ((inhibit-read-only t))
193 (shr-put-image data nil))
194 (goto-char (point-min))))
196 (defun eww-setup-buffer ()
197 (pop-to-buffer (get-buffer-create "*eww*"))
199 (setq widget-field-list nil)
200 (let ((inhibit-read-only t))
205 (let ((map (make-sparse-keymap)))
206 (suppress-keymap map)
207 (define-key map "q" 'eww-quit)
208 (define-key map "g" 'eww-reload)
209 (define-key map [tab] 'shr-next-link)
210 (define-key map [backtab] 'shr-previous-link)
211 (define-key map [delete] 'scroll-down-command)
212 (define-key map "\177" 'scroll-down-command)
213 (define-key map " " 'scroll-up-command)
214 (define-key map "p" 'eww-previous-url)
215 ;;(define-key map "n" 'eww-next-url)
218 (define-derived-mode eww-mode nil "eww"
219 "Mode for browsing the web.
222 (set (make-local-variable 'eww-current-url) 'author)
223 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
225 (defun eww-browse-url (url &optional new-window)
226 (push (list eww-current-url (point))
231 "Exit the Emacs Web Wowser."
233 (setq eww-history nil)
234 (kill-buffer (current-buffer)))
236 (defun eww-previous-url ()
237 "Go to the previously displayed page."
239 (when (zerop (length eww-history))
240 (error "No previous page"))
241 (let ((prev (pop eww-history)))
242 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
245 "Reload the current page."
247 (url-retrieve eww-current-url 'eww-render
248 (list eww-current-url (point))))
252 (defvar eww-form nil)
254 (defun eww-tag-form (cont)
256 (list (assq :method cont)
257 (assq :action cont)))
259 (shr-ensure-paragraph)
264 (when (> (point) start)
265 (put-text-property start (1+ start)
266 'eww-form eww-form))))
268 (defun eww-tag-input (cont)
269 (let* ((start (point))
270 (type (downcase (or (cdr (assq :type cont))
274 ((equal type "submit")
277 :name (cdr (assq :name cont))
278 :value (cdr (assq :value cont))
280 (or (cdr (assq :value cont)) "Submit")))
281 ((or (equal type "radio")
282 (equal type "checkbox"))
284 :notify 'eww-click-radio
285 :name (cdr (assq :name cont))
286 :checkbox-value (cdr (assq :value cont))
289 (cdr (assq :checked cont))))
290 ((equal type "hidden")
292 :name (cdr (assq :name cont))
293 :value (cdr (assq :value cont))))
295 (list 'editable-field
296 :size (string-to-number
297 (or (cdr (assq :size cont))
299 :value (or (cdr (assq :value cont)) "")
300 :secret (and (equal type "password") ?*)
302 :name (cdr (assq :name cont))
303 :eww-form eww-form)))))
304 (nconc eww-form (list widget))
305 (unless (eq (car widget) 'hidden)
306 (apply 'widget-create widget)
307 (put-text-property start (point) 'eww-widget widget))))
309 (defun eww-tag-textarea (cont)
310 (let* ((start (point))
313 :size (string-to-number
314 (or (cdr (assq :cols cont))
316 :value (or (cdr (assq 'text cont)) "")
318 :name (cdr (assq :name cont))
319 :eww-form eww-form)))
320 (nconc eww-form (list widget))
321 (apply 'widget-create widget)
322 (put-text-property start (point) 'eww-widget widget)))
324 (defun eww-tag-select (cont)
325 (shr-ensure-paragraph)
326 (let ((menu (list 'menu-choice
327 :name (cdr (assq :name cont))
332 (when (eq (car elem) 'option)
333 (when (cdr (assq :selected (cdr elem)))
334 (nconc menu (list :value
335 (cdr (assq :value (cdr elem))))))
337 :value (cdr (assq :value (cdr elem)))
338 :tag (cdr (assq 'text (cdr elem))))
340 ;; If we have no selected values, default to the first value.
341 (unless (plist-get (cdr menu) :value)
342 (nconc menu (list :value (nth 2 (car options)))))
344 (apply 'widget-create menu)
345 (put-text-property start (point) 'eww-widget menu)
346 (shr-ensure-paragraph)))
348 (defun eww-click-radio (widget &rest ignore)
349 (let ((form (plist-get (cdr widget) :eww-form))
350 (name (plist-get (cdr widget) :name)))
351 (when (equal (plist-get (cdr widget) :type) "radio")
352 (if (widget-value widget)
353 ;; Switch all the other radio buttons off.
354 (dolist (overlay (overlays-in (point-min) (point-max)))
355 (let ((field (plist-get (overlay-properties overlay) 'button)))
356 (when (and (eq (plist-get (cdr field) :eww-form) form)
357 (equal name (plist-get (cdr field) :name)))
358 (unless (eq field widget)
359 (widget-value-set field nil)))))
360 (widget-value-set widget t)))
361 (eww-fix-widget-keymap)))
363 (defun eww-submit (widget &rest ignore)
364 (let ((form (plist-get (cdr widget) :eww-form))
366 (dolist (overlay (sort (overlays-in (point-min) (point-max))
368 (< (overlay-start o1) (overlay-start o2)))))
369 (let ((field (or (plist-get (overlay-properties overlay) 'field)
370 (plist-get (overlay-properties overlay) 'button))))
371 (when (eq (plist-get (cdr field) :eww-form) form)
372 (let ((name (plist-get (cdr field) :name)))
375 ((eq (car field) 'checkbox)
376 (when (widget-value field)
377 (push (cons name (plist-get (cdr field) :checkbox-value))
379 ((eq (car field) 'push-button)
380 ;; We want the values from buttons if we hit a button,
381 ;; if it's the first button in the DOM after the field
383 (when (and (eq (car widget) 'push-button)
385 (push (cons name (widget-value field))
388 (push (cons name (widget-value field))
391 (when (and (consp elem)
392 (eq (car elem) 'hidden))
393 (push (cons (plist-get (cdr elem) :name)
394 (plist-get (cdr elem) :value))
396 ;; If we hit ENTER in a non-button field, include the value of the
397 ;; first submit button after it.
398 (unless (eq (car widget) 'push-button)
400 (name (plist-get (cdr widget) :name)))
403 (or (not (consp (car rest)))
404 (not (equal name (plist-get (cdar rest) :name)))))
407 (let ((elem (pop rest)))
408 (when (and (consp (car rest))
409 (eq (car elem) 'push-button))
410 (push (cons (plist-get (cdr elem) :name)
411 (plist-get (cdr elem) :value))
414 (if (and (stringp (cdr (assq :method form)))
415 (equal (downcase (cdr (assq :method form))) "post"))
416 (let ((url-request-method "POST")
417 (url-request-extra-headers
418 '(("Content-Type" . "application/x-www-form-urlencoded")))
419 (url-request-data (mm-url-encode-www-form-urlencoded values)))
420 (eww-browse-url (shr-expand-url (cdr (assq :action form))
424 (if (cdr (assq :action form))
425 (shr-expand-url (cdr (assq :action form))
429 (mm-url-encode-www-form-urlencoded values))))))
431 (defun eww-convert-widgets ()
432 (let ((start (point-min))
434 ;; Some widgets come from different buffers (rendered for tables),
435 ;; so we need to nix out the list of widgets and recreate them.
436 (setq widget-field-list nil
437 widget-field-new nil)
438 (while (setq start (next-single-property-change start 'eww-widget))
439 (setq widget (get-text-property start 'eww-widget))
441 (let ((end (next-single-property-change start 'eww-widget)))
442 (dolist (overlay (overlays-in start end))
443 (when (or (plist-get (overlay-properties overlay) 'button)
444 (plist-get (overlay-properties overlay) 'field))
445 (delete-overlay overlay)))
446 (delete-region start end))
448 (not (eq (car widget) 'hidden)))
449 (apply 'widget-create widget)))
451 (eww-fix-widget-keymap)))
453 (defun eww-fix-widget-keymap ()
454 (dolist (overlay (overlays-in (point-min) (point-max)))
455 (when (plist-get (overlay-properties overlay) 'button)
456 (overlay-put overlay 'local-map widget-keymap))))