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 '((((type x w32 ns
) (class color
)) ; Like default mode line
48 :box
(:line-width
2 :style released-button
)
49 :background
"lightgrey" :foreground
"black"))
50 "Face for eww buffer buttons."
54 (defvar eww-current-url nil
)
55 (defvar eww-current-title
""
56 "Title of current page.")
57 (defvar eww-history nil
)
61 "Fetch URL and render the page."
62 (interactive "sUrl: ")
63 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url
)
64 (setq url
(concat "http://" url
)))
65 (url-retrieve url
'eww-render
(list url
)))
67 (defun eww-render (status url
&optional point
)
68 (let ((redirect (plist-get status
:redirect
)))
71 (let* ((headers (eww-parse-headers))
73 (and (string-match "#\\(.*\\)" url
)
74 (match-string 1 url
)))
76 (mail-header-parse-content-type
77 (or (cdr (assoc "content-type" headers
))
81 (or (cdr (assq 'charset
(cdr content-type
)))
82 (eww-detect-charset (equal (car content-type
)
85 (data-buffer (current-buffer)))
89 ((equal (car content-type
) "text/html")
90 (eww-display-html charset url
))
91 ((string-match "^image/" (car content-type
))
94 (eww-display-raw charset
)))
99 (let ((point (next-single-property-change
100 (point-min) 'shr-target-id
)))
102 (goto-char (1+ point
)))))))
103 (kill-buffer data-buffer
))))
105 (defun eww-parse-headers ()
107 (goto-char (point-min))
108 (while (and (not (eobp))
110 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
111 (push (cons (downcase (match-string 1))
119 (defun eww-detect-charset (html-p)
120 (let ((case-fold-search t
)
124 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t
)
128 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
131 (defun eww-display-html (charset url
)
132 (unless (eq charset
'utf8
)
133 (decode-coding-region (point) (point-max) charset
))
136 'base
(list (cons 'href url
))
137 (libxml-parse-html-region (point) (point-max)))))
139 (setq eww-current-url url
)
140 (eww-update-header-line-format)
141 (let ((inhibit-read-only t
)
143 (shr-external-rendering-functions
144 '((title . eww-tag-title
)
145 (form . eww-tag-form
)
146 (input . eww-tag-input
)
147 (textarea . eww-tag-textarea
)
148 (body . eww-tag-body
)
149 (select . eww-tag-select
))))
150 (shr-insert-document document
)
151 (eww-convert-widgets))
152 (goto-char (point-min))))
154 (defun eww-update-header-line-format ()
155 (if eww-header-line-format
156 (setq header-line-format
(format-spec eww-header-line-format
157 `((?u .
,eww-current-url
)
158 (?t .
,eww-current-title
))))
159 (setq header-line-format nil
)))
161 (defun eww-tag-title (cont)
162 (setq eww-current-title
"")
164 (when (eq (car sub
) 'text
)
165 (setq eww-current-title
(concat eww-current-title
(cdr sub
)))))
166 (eww-update-header-line-format))
168 (defun eww-tag-body (cont)
169 (let* ((start (point))
170 (fgcolor (cdr (or (assq :fgcolor cont
)
172 (bgcolor (cdr (assq :bgcolor cont
)))
173 (shr-stylesheet (list (cons 'color fgcolor
)
174 (cons 'background-color bgcolor
))))
176 (eww-colorize-region start
(point) fgcolor bgcolor
)))
178 (defun eww-colorize-region (start end fg
&optional bg
)
180 (let ((new-colors (shr-color-check fg bg
)))
183 (add-face-text-property start end
184 (list :foreground
(cadr new-colors
))))
186 (add-face-text-property start end
187 (list :background
(car new-colors
))))))))
189 (defun eww-display-raw (charset)
190 (let ((data (buffer-substring (point) (point-max))))
192 (let ((inhibit-read-only t
))
194 (goto-char (point-min))))
196 (defun eww-display-image ()
197 (let ((data (buffer-substring (point) (point-max))))
199 (let ((inhibit-read-only t
))
200 (shr-put-image data nil
))
201 (goto-char (point-min))))
203 (defun eww-setup-buffer ()
204 (pop-to-buffer (get-buffer-create "*eww*"))
206 (setq widget-field-list nil
)
207 (let ((inhibit-read-only t
))
212 (let ((map (make-sparse-keymap)))
213 (suppress-keymap map
)
214 (define-key map
"q" 'eww-quit
)
215 (define-key map
"g" 'eww-reload
)
216 (define-key map
[tab] 'shr-next-link)
217 (define-key map [backtab] 'shr-previous-link)
218 (define-key map [delete] 'scroll-down-command)
219 (define-key map "\177" 'scroll-down-command)
220 (define-key map " " 'scroll-up-command)
221 (define-key map "p" 'eww-previous-url)
222 ;;(define-key map "n" 'eww-next-url)
225 (define-derived-mode eww-mode nil "eww"
226 "Mode for browsing the web.
229 (set (make-local-variable 'eww-current-url) 'author)
230 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
232 (defun eww-browse-url (url &optional new-window)
233 (push (list eww-current-url (point))
238 "Exit the Emacs Web Wowser."
240 (setq eww-history nil)
241 (kill-buffer (current-buffer)))
243 (defun eww-previous-url ()
244 "Go to the previously displayed page."
246 (when (zerop (length eww-history))
247 (error "No previous page"))
248 (let ((prev (pop eww-history)))
249 (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
252 "Reload the current page."
254 (url-retrieve eww-current-url 'eww-render
255 (list eww-current-url (point))))
259 (defvar eww-form nil)
261 (defun eww-tag-form (cont)
263 (list (assq :method cont)
264 (assq :action cont)))
266 (shr-ensure-paragraph)
271 (when (> (point) start)
272 (put-text-property start (1+ start)
273 'eww-form eww-form))))
275 (defun eww-tag-input (cont)
276 (let* ((start (point))
277 (type (downcase (or (cdr (assq :type cont))
279 (value (cdr (assq :value cont)))
282 ((or (equal type "submit")
283 (equal type "image"))
286 :name (cdr (assq :name cont))
287 :value (if (zerop (length value))
291 (or (if (zerop (length value))
294 ((or (equal type "radio")
295 (equal type "checkbox"))
297 :notify 'eww-click-radio
298 :name (cdr (assq :name cont))
299 :checkbox-value value
302 (cdr (assq :checked cont))))
303 ((equal type "hidden")
305 :name (cdr (assq :name cont))
308 (list 'editable-field
309 :size (string-to-number
310 (or (cdr (assq :size cont))
313 :secret (and (equal type "password") ?*)
315 :name (cdr (assq :name cont))
316 :eww-form eww-form)))))
317 (nconc eww-form (list widget))
318 (unless (eq (car widget) 'hidden)
319 (apply 'widget-create widget)
320 (put-text-property start (point) 'eww-widget widget)
323 (defun eww-tag-textarea (cont)
324 (let* ((start (point))
327 :size (string-to-number
328 (or (cdr (assq :cols cont))
330 :value (or (cdr (assq 'text cont)) "")
332 :name (cdr (assq :name cont))
333 :eww-form eww-form)))
334 (nconc eww-form (list widget))
335 (apply 'widget-create widget)
336 (put-text-property start (point) 'eww-widget widget)))
338 (defun eww-tag-select (cont)
339 (shr-ensure-paragraph)
340 (let ((menu (list 'menu-choice
341 :name (cdr (assq :name cont))
346 (when (eq (car elem) 'option)
347 (when (cdr (assq :selected (cdr elem)))
348 (nconc menu (list :value
349 (cdr (assq :value (cdr elem))))))
351 :value (cdr (assq :value (cdr elem)))
352 :tag (cdr (assq 'text (cdr elem))))
355 ;; If we have no selected values, default to the first value.
356 (unless (plist-get (cdr menu) :value)
357 (nconc menu (list :value (nth 2 (car options)))))
359 (apply 'widget-create menu)
360 (put-text-property start (point) 'eww-widget menu)
361 (shr-ensure-paragraph))))
363 (defun eww-click-radio (widget &rest ignore)
364 (let ((form (plist-get (cdr widget) :eww-form))
365 (name (plist-get (cdr widget) :name)))
366 (when (equal (plist-get (cdr widget) :type) "radio")
367 (if (widget-value widget)
368 ;; Switch all the other radio buttons off.
369 (dolist (overlay (overlays-in (point-min) (point-max)))
370 (let ((field (plist-get (overlay-properties overlay) 'button)))
371 (when (and (eq (plist-get (cdr field) :eww-form) form)
372 (equal name (plist-get (cdr field) :name)))
373 (unless (eq field widget)
374 (widget-value-set field nil)))))
375 (widget-value-set widget t)))
376 (eww-fix-widget-keymap)))
378 (defun eww-submit (widget &rest ignore)
379 (let ((form (plist-get (cdr widget) :eww-form))
381 (dolist (overlay (sort (overlays-in (point-min) (point-max))
383 (< (overlay-start o1) (overlay-start o2)))))
384 (let ((field (or (plist-get (overlay-properties overlay) 'field)
385 (plist-get (overlay-properties overlay) 'button))))
386 (when (eq (plist-get (cdr field) :eww-form) form)
387 (let ((name (plist-get (cdr field) :name)))
390 ((eq (car field) 'checkbox)
391 (when (widget-value field)
392 (push (cons name (plist-get (cdr field) :checkbox-value))
394 ((eq (car field) 'push-button)
395 ;; We want the values from buttons if we hit a button,
396 ;; if it's the first button in the DOM after the field
398 (when (and (eq (car widget) 'push-button)
400 (push (cons name (widget-value field))
403 (push (cons name (widget-value field))
406 (when (and (consp elem)
407 (eq (car elem) 'hidden))
408 (push (cons (plist-get (cdr elem) :name)
409 (plist-get (cdr elem) :value))
411 ;; If we hit ENTER in a non-button field, include the value of the
412 ;; first submit button after it.
413 (unless (eq (car widget) 'push-button)
415 (name (plist-get (cdr widget) :name)))
418 (or (not (consp (car rest)))
419 (not (equal name (plist-get (cdar rest) :name)))))
422 (let ((elem (pop rest)))
423 (when (and (consp (car rest))
424 (eq (car elem) 'push-button))
425 (push (cons (plist-get (cdr elem) :name)
426 (plist-get (cdr elem) :value))
429 (if (and (stringp (cdr (assq :method form)))
430 (equal (downcase (cdr (assq :method form))) "post"))
431 (let ((url-request-method "POST")
432 (url-request-extra-headers
433 '(("Content-Type" . "application/x-www-form-urlencoded")))
434 (url-request-data (mm-url-encode-www-form-urlencoded values)))
435 (eww-browse-url (shr-expand-url (cdr (assq :action form))
439 (if (cdr (assq :action form))
440 (shr-expand-url (cdr (assq :action form))
444 (mm-url-encode-www-form-urlencoded values))))))
446 (defun eww-convert-widgets ()
447 (let ((start (point-min))
449 ;; Some widgets come from different buffers (rendered for tables),
450 ;; so we need to nix out the list of widgets and recreate them.
451 (setq widget-field-list nil
452 widget-field-new nil)
453 (while (setq start (if (get-text-property start 'eww-widget)
455 (next-single-property-change start 'eww-widget)))
456 (setq widget (get-text-property start 'eww-widget))
458 (let ((end (next-single-property-change start 'eww-widget)))
459 (dolist (overlay (overlays-in start end))
460 (when (or (plist-get (overlay-properties overlay) 'button)
461 (plist-get (overlay-properties overlay) 'field))
462 (delete-overlay overlay)))
463 (delete-region start end))
465 (not (eq (car widget) 'hidden)))
466 (apply 'widget-create widget)
467 (put-text-property start (point) 'help-echo
468 (if (memq (car widget) '(text editable-field))
471 (when (eq (car widget) 'push-button)
472 (add-face-text-property start (point) 'eww-button t))))
474 (eww-fix-widget-keymap)))
476 (defun eww-fix-widget-keymap ()
477 (dolist (overlay (overlays-in (point-min) (point-max)))
478 (when (plist-get (overlay-properties overlay) 'button)
479 (overlay-put overlay 'local-map widget-keymap))))