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))
38 (defcustom eww-header-line-format "%t: %u"
40 - %t is replaced by the title.
41 - %u is replaced by the URL."
45 (defvar eww-current-url nil)
46 (defvar eww-current-title ""
47 "Title of current page.")
48 (defvar eww-history nil)
52 "Fetch URL and render the page."
53 (interactive "sUrl: ")
54 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
55 (setq url (concat "http://" url)))
56 (url-retrieve url 'eww-render (list url)))
58 (defun eww-detect-charset (html-p)
59 (let ((case-fold-search t)
63 "<meta[\t\n\r ]+[^>]*charset=\\([^\t\n\r \"/>]+\\)" nil t)
67 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
70 (defun eww-render (status url &optional point)
71 (let ((redirect (plist-get status :redirect)))
74 (let* ((headers (eww-parse-headers))
76 (and (string-match "#\\(.*\\)" url)
77 (match-string 1 url)))
79 (mail-header-parse-content-type
80 (or (cdr (assoc "content-type" headers))
84 (or (cdr (assq 'charset (cdr content-type)))
85 (eww-detect-charset (equal (car content-type)
88 (data-buffer (current-buffer)))
92 ((equal (car content-type) "text/html")
93 (eww-display-html charset url))
94 ((string-match "^image/" (car content-type))
97 (eww-display-raw charset)))
102 (let ((point (next-single-property-change
103 (point-min) 'shr-target-id)))
105 (goto-char (1+ point)))))))
106 (kill-buffer data-buffer))))
108 (defun eww-parse-headers ()
110 (goto-char (point-min))
111 (while (and (not (eobp))
113 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
114 (push (cons (downcase (match-string 1))
122 (defun eww-display-html (charset url)
123 (unless (eq charset 'utf8)
124 (decode-coding-region (point) (point-max) charset))
127 'base (list (cons 'href url))
128 (libxml-parse-html-region (point) (point-max)))))
130 (setq eww-current-url url)
131 (eww-update-header-line-format)
132 (let ((inhibit-read-only t)
133 (shr-external-rendering-functions
134 '((title . eww-tag-title)
135 (form . eww-tag-form)
136 (input . eww-tag-input)
137 (textarea . eww-tag-textarea)
138 (body . eww-tag-body)
139 (select . eww-tag-select))))
140 (shr-insert-document document)
141 (eww-convert-widgets))
142 (goto-char (point-min))))
144 (defun eww-update-header-line-format ()
145 (if eww-header-line-format
146 (setq header-line-format (format-spec eww-header-line-format
147 `((?u . ,eww-current-url)
148 (?t . ,eww-current-title))))
149 (setq header-line-format nil)))
151 (defun eww-tag-title (cont)
152 (setq eww-current-title "")
154 (when (eq (car sub) 'text)
155 (setq eww-current-title (concat eww-current-title (cdr sub)))))
156 (eww-update-header-line-format))
158 (defun eww-tag-body (cont)
159 (let* ((start (point))
160 (fgcolor (cdr (or (assq :fgcolor cont)
162 (bgcolor (cdr (assq :bgcolor cont)))
163 (shr-stylesheet (list (cons 'color fgcolor)
164 (cons 'background-color bgcolor))))
166 (eww-colorize-region start (point) fgcolor bgcolor)))
168 (defun eww-colorize-region (start end fg &optional bg)
170 (let ((new-colors (shr-color-check fg bg)))
173 (eww-put-color start end :foreground (cadr new-colors)))
175 (eww-put-color start end :background (car new-colors)))))))
177 (defun eww-put-color (start end type color)
178 (shr-put-color-1 start end type color))
180 (defun eww-display-raw (charset)
181 (let ((data (buffer-substring (point) (point-max))))
183 (let ((inhibit-read-only t))
185 (goto-char (point-min))))
187 (defun eww-display-image ()
188 (let ((data (buffer-substring (point) (point-max))))
190 (let ((inhibit-read-only t))
191 (shr-put-image data nil))
192 (goto-char (point-min))))
194 (defun eww-setup-buffer ()
195 (pop-to-buffer (get-buffer-create "*eww*"))
197 (setq widget-field-list nil)
198 (let ((inhibit-read-only t))
203 (let ((map (make-sparse-keymap)))
204 (suppress-keymap map)
205 (define-key map "q" 'eww-quit)
206 (define-key map "g" 'eww-reload)
207 (define-key map [tab] 'widget-forward)
208 (define-key map [backtab] 'widget-backward)
209 (define-key map [delete] 'scroll-down-command)
210 (define-key map "\177" 'scroll-down-command)
211 (define-key map " " 'scroll-up-command)
212 (define-key map "p" 'eww-previous-url)
213 ;;(define-key map "n" 'eww-next-url)
216 (define-derived-mode eww-mode nil "eww"
217 "Mode for browsing the web.
220 (set (make-local-variable 'eww-current-url) 'author)
221 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
223 (defun eww-browse-url (url &optional new-window)
224 (let ((url-request-extra-headers
225 (append '(("User-Agent" . "eww/1.0"))
226 url-request-extra-headers)))
227 (push (list eww-current-url (point))
232 "Exit the Emacs Web Wowser."
234 (setq eww-history nil)
235 (kill-buffer (current-buffer)))
237 (defun eww-previous-url ()
238 "Go to the previously displayed page."
240 (when (zerop (length eww-history))
241 (error "No previous page"))
242 (let ((prev (pop eww-history)))
243 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
246 "Reload the current page."
248 (url-retrieve eww-current-url 'eww-render
249 (list eww-current-url (point))))
253 (defvar eww-form nil)
255 (defun eww-tag-form (cont)
257 (list (assq :method cont)
258 (assq :action cont)))
260 (shr-ensure-paragraph)
265 (when (> (point) start)
266 (put-text-property start (1+ start)
267 'eww-form eww-form))))
269 (defun eww-tag-input (cont)
270 (let* ((start (point))
271 (type (downcase (or (cdr (assq :type cont))
275 ((equal type "submit")
278 :name (cdr (assq :name cont))
279 :value (cdr (assq :value cont))
281 (or (cdr (assq :value cont)) "Submit")))
282 ((or (equal type "radio")
283 (equal type "checkbox"))
285 :notify 'eww-click-radio
286 :name (cdr (assq :name cont))
287 :checkbox-value (cdr (assq :value cont))
290 (cdr (assq :checked cont))))
291 ((equal type "hidden")
293 :name (cdr (assq :name cont))
294 :value (cdr (assq :value cont))))
296 (list 'editable-field
297 :size (string-to-number
298 (or (cdr (assq :size cont))
300 :value (or (cdr (assq :value cont)) "")
301 :secret (and (equal type "password") ?*)
303 :name (cdr (assq :name cont))
304 :eww-form eww-form)))))
305 (nconc eww-form (list widget))
306 (unless (eq (car widget) 'hidden)
307 (apply 'widget-create widget)
308 (put-text-property start (point) 'eww-widget widget))))
310 (defun eww-tag-textarea (cont)
311 (let* ((start (point))
314 :size (string-to-number
315 (or (cdr (assq :cols cont))
317 :value (or (cdr (assq 'text cont)) "")
319 :name (cdr (assq :name cont))
320 :eww-form eww-form)))
321 (nconc eww-form (list widget))
322 (apply 'widget-create widget)
323 (put-text-property start (point) 'eww-widget widget)))
325 (defun eww-tag-select (cont)
326 (shr-ensure-paragraph)
327 (let ((menu (list 'menu-choice
328 :name (cdr (assq :name cont))
333 (when (eq (car elem) 'option)
334 (when (cdr (assq :selected (cdr elem)))
335 (nconc menu (list :value
336 (cdr (assq :value (cdr elem))))))
338 :value (cdr (assq :value (cdr elem)))
339 :tag (cdr (assq 'text (cdr elem))))
341 ;; If we have no selected values, default to the first value.
342 (unless (plist-get (cdr menu) :value)
343 (nconc menu (list :value (nth 2 (car options)))))
345 (apply 'widget-create menu)
346 (put-text-property start (point) 'eww-widget menu)
347 (shr-ensure-paragraph)))
349 (defun eww-click-radio (widget &rest ignore)
350 (let ((form (plist-get (cdr widget) :eww-form))
351 (name (plist-get (cdr widget) :name)))
352 (when (equal (plist-get (cdr widget) :type) "radio")
353 (if (widget-value widget)
354 ;; Switch all the other radio buttons off.
355 (dolist (overlay (overlays-in (point-min) (point-max)))
356 (let ((field (plist-get (overlay-properties overlay) 'button)))
357 (when (and (eq (plist-get (cdr field) :eww-form) form)
358 (equal name (plist-get (cdr field) :name)))
359 (unless (eq field widget)
360 (widget-value-set field nil)))))
361 (widget-value-set widget t)))
362 (eww-fix-widget-keymap)))
364 (defun eww-submit (widget &rest ignore)
365 (let ((form (plist-get (cdr widget) :eww-form))
367 (dolist (overlay (sort (overlays-in (point-min) (point-max))
369 (< (overlay-start o1) (overlay-start o2)))))
370 (let ((field (or (plist-get (overlay-properties overlay) 'field)
371 (plist-get (overlay-properties overlay) 'button))))
372 (when (eq (plist-get (cdr field) :eww-form) form)
373 (let ((name (plist-get (cdr field) :name)))
376 ((eq (car field) 'checkbox)
377 (when (widget-value field)
378 (push (cons name (plist-get (cdr field) :checkbox-value))
380 ((eq (car field) 'push-button)
381 ;; We want the values from buttons if we hit a button,
382 ;; if it's the first button in the DOM after the field
384 (when (and (eq (car widget) 'push-button)
386 (push (cons name (widget-value field))
389 (push (cons name (widget-value field))
392 (when (and (consp elem)
393 (eq (car elem) 'hidden))
394 (push (cons (plist-get (cdr elem) :name)
395 (plist-get (cdr elem) :value))
397 ;; If we hit ENTER in a non-button field, include the value of the
398 ;; first submit button after it.
399 (unless (eq (car widget) 'push-button)
401 (name (plist-get (cdr widget) :name)))
404 (or (not (consp (car rest)))
405 (not (equal name (plist-get (cdar rest) :name)))))
408 (let ((elem (pop rest)))
409 (when (and (consp (car rest))
410 (eq (car elem) 'push-button))
411 (push (cons (plist-get (cdr elem) :name)
412 (plist-get (cdr elem) :value))
415 (if (and (stringp (cdr (assq :method form)))
416 (equal (downcase (cdr (assq :method form))) "post"))
417 (let ((url-request-method "POST")
418 (url-request-extra-headers
419 '(("Content-Type" . "application/x-www-form-urlencoded")))
420 (url-request-data (mm-url-encode-www-form-urlencoded values)))
421 (eww-browse-url (shr-expand-url (cdr (assq :action form))
425 (if (cdr (assq :action form))
426 (shr-expand-url (cdr (assq :action form))
430 (mm-url-encode-www-form-urlencoded values))))))
432 (defun eww-convert-widgets ()
433 (let ((start (point-min))
435 ;; Some widgets come from different buffers (rendered for tables),
436 ;; so we need to nix out the list of widgets and recreate them.
437 (setq widget-field-list nil
438 widget-field-new nil)
439 (while (setq start (next-single-property-change start 'eww-widget))
440 (setq widget (get-text-property start 'eww-widget))
442 (let ((end (next-single-property-change start 'eww-widget)))
443 (dolist (overlay (overlays-in start end))
444 (when (or (plist-get (overlay-properties overlay) 'button)
445 (plist-get (overlay-properties overlay) 'field))
446 (delete-overlay overlay)))
447 (delete-region start end))
449 (not (eq (car widget) 'hidden)))
450 (apply 'widget-create widget)))
452 (eww-fix-widget-keymap)))
454 (defun eww-fix-widget-keymap ()
455 (dolist (overlay (overlays-in (point-min) (point-max)))
456 (when (plist-get (overlay-properties overlay) 'button)
457 (overlay-put overlay 'local-map widget-keymap))))