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)
134 (shr-external-rendering-functions
135 '((title . eww-tag-title)
136 (form . eww-tag-form)
137 (input . eww-tag-input)
138 (textarea . eww-tag-textarea)
139 (body . eww-tag-body)
140 (select . eww-tag-select))))
141 (shr-insert-document document)
142 (eww-convert-widgets))
143 (goto-char (point-min))))
145 (defun eww-update-header-line-format ()
146 (if eww-header-line-format
147 (setq header-line-format (format-spec eww-header-line-format
148 `((?u . ,eww-current-url)
149 (?t . ,eww-current-title))))
150 (setq header-line-format nil)))
152 (defun eww-tag-title (cont)
153 (setq eww-current-title "")
155 (when (eq (car sub) 'text)
156 (setq eww-current-title (concat eww-current-title (cdr sub)))))
157 (eww-update-header-line-format))
159 (defun eww-tag-body (cont)
160 (let* ((start (point))
161 (fgcolor (cdr (or (assq :fgcolor cont)
163 (bgcolor (cdr (assq :bgcolor cont)))
164 (shr-stylesheet (list (cons 'color fgcolor)
165 (cons 'background-color bgcolor))))
167 (eww-colorize-region start (point) fgcolor bgcolor)))
169 (defun eww-colorize-region (start end fg &optional bg)
171 (let ((new-colors (shr-color-check fg bg)))
174 (eww-put-color start end :foreground (cadr new-colors)))
176 (eww-put-color start end :background (car new-colors)))))))
178 (defun eww-put-color (start end type color)
179 (shr-put-color-1 start end type color))
181 (defun eww-display-raw (charset)
182 (let ((data (buffer-substring (point) (point-max))))
184 (let ((inhibit-read-only t))
186 (goto-char (point-min))))
188 (defun eww-display-image ()
189 (let ((data (buffer-substring (point) (point-max))))
191 (let ((inhibit-read-only t))
192 (shr-put-image data nil))
193 (goto-char (point-min))))
195 (defun eww-setup-buffer ()
196 (pop-to-buffer (get-buffer-create "*eww*"))
198 (setq widget-field-list nil)
199 (let ((inhibit-read-only t))
204 (let ((map (make-sparse-keymap)))
205 (suppress-keymap map)
206 (define-key map "q" 'eww-quit)
207 (define-key map "g" 'eww-reload)
208 (define-key map [tab] 'shr-next-link)
209 (define-key map [backtab] 'shr-previous-link)
210 (define-key map [delete] 'scroll-down-command)
211 (define-key map "\177" 'scroll-down-command)
212 (define-key map " " 'scroll-up-command)
213 (define-key map "p" 'eww-previous-url)
214 ;;(define-key map "n" 'eww-next-url)
217 (define-derived-mode eww-mode nil "eww"
218 "Mode for browsing the web.
221 (set (make-local-variable 'eww-current-url) 'author)
222 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
224 (defun eww-browse-url (url &optional new-window)
225 (push (list eww-current-url (point))
230 "Exit the Emacs Web Wowser."
232 (setq eww-history nil)
233 (kill-buffer (current-buffer)))
235 (defun eww-previous-url ()
236 "Go to the previously displayed page."
238 (when (zerop (length eww-history))
239 (error "No previous page"))
240 (let ((prev (pop eww-history)))
241 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
244 "Reload the current page."
246 (url-retrieve eww-current-url 'eww-render
247 (list eww-current-url (point))))
251 (defvar eww-form nil)
253 (defun eww-tag-form (cont)
255 (list (assq :method cont)
256 (assq :action cont)))
258 (shr-ensure-paragraph)
263 (when (> (point) start)
264 (put-text-property start (1+ start)
265 'eww-form eww-form))))
267 (defun eww-tag-input (cont)
268 (let* ((start (point))
269 (type (downcase (or (cdr (assq :type cont))
273 ((equal type "submit")
276 :name (cdr (assq :name cont))
277 :value (cdr (assq :value cont))
279 (or (cdr (assq :value cont)) "Submit")))
280 ((or (equal type "radio")
281 (equal type "checkbox"))
283 :notify 'eww-click-radio
284 :name (cdr (assq :name cont))
285 :checkbox-value (cdr (assq :value cont))
288 (cdr (assq :checked cont))))
289 ((equal type "hidden")
291 :name (cdr (assq :name cont))
292 :value (cdr (assq :value cont))))
294 (list 'editable-field
295 :size (string-to-number
296 (or (cdr (assq :size cont))
298 :value (or (cdr (assq :value cont)) "")
299 :secret (and (equal type "password") ?*)
301 :name (cdr (assq :name cont))
302 :eww-form eww-form)))))
303 (nconc eww-form (list widget))
304 (unless (eq (car widget) 'hidden)
305 (apply 'widget-create widget)
306 (put-text-property start (point) 'eww-widget widget))))
308 (defun eww-tag-textarea (cont)
309 (let* ((start (point))
312 :size (string-to-number
313 (or (cdr (assq :cols cont))
315 :value (or (cdr (assq 'text cont)) "")
317 :name (cdr (assq :name cont))
318 :eww-form eww-form)))
319 (nconc eww-form (list widget))
320 (apply 'widget-create widget)
321 (put-text-property start (point) 'eww-widget widget)))
323 (defun eww-tag-select (cont)
324 (shr-ensure-paragraph)
325 (let ((menu (list 'menu-choice
326 :name (cdr (assq :name cont))
331 (when (eq (car elem) 'option)
332 (when (cdr (assq :selected (cdr elem)))
333 (nconc menu (list :value
334 (cdr (assq :value (cdr elem))))))
336 :value (cdr (assq :value (cdr elem)))
337 :tag (cdr (assq 'text (cdr elem))))
339 ;; If we have no selected values, default to the first value.
340 (unless (plist-get (cdr menu) :value)
341 (nconc menu (list :value (nth 2 (car options)))))
343 (apply 'widget-create menu)
344 (put-text-property start (point) 'eww-widget menu)
345 (shr-ensure-paragraph)))
347 (defun eww-click-radio (widget &rest ignore)
348 (let ((form (plist-get (cdr widget) :eww-form))
349 (name (plist-get (cdr widget) :name)))
350 (when (equal (plist-get (cdr widget) :type) "radio")
351 (if (widget-value widget)
352 ;; Switch all the other radio buttons off.
353 (dolist (overlay (overlays-in (point-min) (point-max)))
354 (let ((field (plist-get (overlay-properties overlay) 'button)))
355 (when (and (eq (plist-get (cdr field) :eww-form) form)
356 (equal name (plist-get (cdr field) :name)))
357 (unless (eq field widget)
358 (widget-value-set field nil)))))
359 (widget-value-set widget t)))
360 (eww-fix-widget-keymap)))
362 (defun eww-submit (widget &rest ignore)
363 (let ((form (plist-get (cdr widget) :eww-form))
365 (dolist (overlay (sort (overlays-in (point-min) (point-max))
367 (< (overlay-start o1) (overlay-start o2)))))
368 (let ((field (or (plist-get (overlay-properties overlay) 'field)
369 (plist-get (overlay-properties overlay) 'button))))
370 (when (eq (plist-get (cdr field) :eww-form) form)
371 (let ((name (plist-get (cdr field) :name)))
374 ((eq (car field) 'checkbox)
375 (when (widget-value field)
376 (push (cons name (plist-get (cdr field) :checkbox-value))
378 ((eq (car field) 'push-button)
379 ;; We want the values from buttons if we hit a button,
380 ;; if it's the first button in the DOM after the field
382 (when (and (eq (car widget) 'push-button)
384 (push (cons name (widget-value field))
387 (push (cons name (widget-value field))
390 (when (and (consp elem)
391 (eq (car elem) 'hidden))
392 (push (cons (plist-get (cdr elem) :name)
393 (plist-get (cdr elem) :value))
395 ;; If we hit ENTER in a non-button field, include the value of the
396 ;; first submit button after it.
397 (unless (eq (car widget) 'push-button)
399 (name (plist-get (cdr widget) :name)))
402 (or (not (consp (car rest)))
403 (not (equal name (plist-get (cdar rest) :name)))))
406 (let ((elem (pop rest)))
407 (when (and (consp (car rest))
408 (eq (car elem) 'push-button))
409 (push (cons (plist-get (cdr elem) :name)
410 (plist-get (cdr elem) :value))
413 (if (and (stringp (cdr (assq :method form)))
414 (equal (downcase (cdr (assq :method form))) "post"))
415 (let ((url-request-method "POST")
416 (url-request-extra-headers
417 '(("Content-Type" . "application/x-www-form-urlencoded")))
418 (url-request-data (mm-url-encode-www-form-urlencoded values)))
419 (eww-browse-url (shr-expand-url (cdr (assq :action form))
423 (if (cdr (assq :action form))
424 (shr-expand-url (cdr (assq :action form))
428 (mm-url-encode-www-form-urlencoded values))))))
430 (defun eww-convert-widgets ()
431 (let ((start (point-min))
433 ;; Some widgets come from different buffers (rendered for tables),
434 ;; so we need to nix out the list of widgets and recreate them.
435 (setq widget-field-list nil
436 widget-field-new nil)
437 (while (setq start (next-single-property-change start 'eww-widget))
438 (setq widget (get-text-property start 'eww-widget))
440 (let ((end (next-single-property-change start 'eww-widget)))
441 (dolist (overlay (overlays-in start end))
442 (when (or (plist-get (overlay-properties overlay) 'button)
443 (plist-get (overlay-properties overlay) 'field))
444 (delete-overlay overlay)))
445 (delete-region start end))
447 (not (eq (car widget) 'hidden)))
448 (apply 'widget-create widget)))
450 (eww-fix-widget-keymap)))
452 (defun eww-fix-widget-keymap ()
453 (dolist (overlay (overlays-in (point-min) (point-max)))
454 (when (plist-get (overlay-properties overlay) 'button)
455 (overlay-put overlay 'local-map widget-keymap))))