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-detect-charset (html-p)
68 (let ((case-fold-search t)
72 "<meta[\t\n\r ]+[^>]*charset=\\([^\t\n\r \"/>]+\\)" nil t)
76 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
79 (defun eww-render (status url &optional point)
80 (let ((redirect (plist-get status :redirect)))
83 (let* ((headers (eww-parse-headers))
85 (and (string-match "#\\(.*\\)" url)
86 (match-string 1 url)))
88 (mail-header-parse-content-type
89 (or (cdr (assoc "content-type" headers))
93 (or (cdr (assq 'charset (cdr content-type)))
94 (eww-detect-charset (equal (car content-type)
97 (data-buffer (current-buffer)))
101 ((equal (car content-type) "text/html")
102 (eww-display-html charset url))
103 ((string-match "^image/" (car content-type))
106 (eww-display-raw charset)))
111 (let ((point (next-single-property-change
112 (point-min) 'shr-target-id)))
114 (goto-char (1+ point)))))))
115 (kill-buffer data-buffer))))
117 (defun eww-parse-headers ()
119 (goto-char (point-min))
120 (while (and (not (eobp))
122 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
123 (push (cons (downcase (match-string 1))
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 ((equal type "submit")
285 :name (cdr (assq :name cont))
286 :value (if (zerop (length value))
290 (or (if (zerop (length value))
293 ((or (equal type "radio")
294 (equal type "checkbox"))
296 :notify 'eww-click-radio
297 :name (cdr (assq :name cont))
298 :checkbox-value value
301 (cdr (assq :checked cont))))
302 ((equal type "hidden")
304 :name (cdr (assq :name cont))
307 (list 'editable-field
308 :size (string-to-number
309 (or (cdr (assq :size cont))
312 :secret (and (equal type "password") ?*)
314 :name (cdr (assq :name cont))
315 :eww-form eww-form)))))
316 (nconc eww-form (list widget))
317 (unless (eq (car widget) 'hidden)
318 (apply 'widget-create widget)
319 (put-text-property start (point) 'eww-widget widget)
322 (defun eww-tag-textarea (cont)
323 (let* ((start (point))
326 :size (string-to-number
327 (or (cdr (assq :cols cont))
329 :value (or (cdr (assq 'text cont)) "")
331 :name (cdr (assq :name cont))
332 :eww-form eww-form)))
333 (nconc eww-form (list widget))
334 (apply 'widget-create widget)
335 (put-text-property start (point) 'eww-widget widget)))
337 (defun eww-tag-select (cont)
338 (shr-ensure-paragraph)
339 (let ((menu (list 'menu-choice
340 :name (cdr (assq :name cont))
345 (when (eq (car elem) 'option)
346 (when (cdr (assq :selected (cdr elem)))
347 (nconc menu (list :value
348 (cdr (assq :value (cdr elem))))))
350 :value (cdr (assq :value (cdr elem)))
351 :tag (cdr (assq 'text (cdr elem))))
354 ;; If we have no selected values, default to the first value.
355 (unless (plist-get (cdr menu) :value)
356 (nconc menu (list :value (nth 2 (car options)))))
358 (apply 'widget-create menu)
359 (put-text-property start (point) 'eww-widget menu)
360 (shr-ensure-paragraph))))
362 (defun eww-click-radio (widget &rest ignore)
363 (let ((form (plist-get (cdr widget) :eww-form))
364 (name (plist-get (cdr widget) :name)))
365 (when (equal (plist-get (cdr widget) :type) "radio")
366 (if (widget-value widget)
367 ;; Switch all the other radio buttons off.
368 (dolist (overlay (overlays-in (point-min) (point-max)))
369 (let ((field (plist-get (overlay-properties overlay) 'button)))
370 (when (and (eq (plist-get (cdr field) :eww-form) form)
371 (equal name (plist-get (cdr field) :name)))
372 (unless (eq field widget)
373 (widget-value-set field nil)))))
374 (widget-value-set widget t)))
375 (eww-fix-widget-keymap)))
377 (defun eww-submit (widget &rest ignore)
378 (let ((form (plist-get (cdr widget) :eww-form))
380 (dolist (overlay (sort (overlays-in (point-min) (point-max))
382 (< (overlay-start o1) (overlay-start o2)))))
383 (let ((field (or (plist-get (overlay-properties overlay) 'field)
384 (plist-get (overlay-properties overlay) 'button))))
385 (when (eq (plist-get (cdr field) :eww-form) form)
386 (let ((name (plist-get (cdr field) :name)))
389 ((eq (car field) 'checkbox)
390 (when (widget-value field)
391 (push (cons name (plist-get (cdr field) :checkbox-value))
393 ((eq (car field) 'push-button)
394 ;; We want the values from buttons if we hit a button,
395 ;; if it's the first button in the DOM after the field
397 (when (and (eq (car widget) 'push-button)
399 (push (cons name (widget-value field))
402 (push (cons name (widget-value field))
405 (when (and (consp elem)
406 (eq (car elem) 'hidden))
407 (push (cons (plist-get (cdr elem) :name)
408 (plist-get (cdr elem) :value))
410 ;; If we hit ENTER in a non-button field, include the value of the
411 ;; first submit button after it.
412 (unless (eq (car widget) 'push-button)
414 (name (plist-get (cdr widget) :name)))
417 (or (not (consp (car rest)))
418 (not (equal name (plist-get (cdar rest) :name)))))
421 (let ((elem (pop rest)))
422 (when (and (consp (car rest))
423 (eq (car elem) 'push-button))
424 (push (cons (plist-get (cdr elem) :name)
425 (plist-get (cdr elem) :value))
428 (if (and (stringp (cdr (assq :method form)))
429 (equal (downcase (cdr (assq :method form))) "post"))
430 (let ((url-request-method "POST")
431 (url-request-extra-headers
432 '(("Content-Type" . "application/x-www-form-urlencoded")))
433 (url-request-data (mm-url-encode-www-form-urlencoded values)))
434 (eww-browse-url (shr-expand-url (cdr (assq :action form))
438 (if (cdr (assq :action form))
439 (shr-expand-url (cdr (assq :action form))
443 (mm-url-encode-www-form-urlencoded values))))))
445 (defun eww-convert-widgets ()
446 (let ((start (point-min))
448 ;; Some widgets come from different buffers (rendered for tables),
449 ;; so we need to nix out the list of widgets and recreate them.
450 (setq widget-field-list nil
451 widget-field-new nil)
452 (while (setq start (next-single-property-change start 'eww-widget))
453 (setq widget (get-text-property start 'eww-widget))
455 (let ((end (next-single-property-change start 'eww-widget)))
456 (dolist (overlay (overlays-in start end))
457 (when (or (plist-get (overlay-properties overlay) 'button)
458 (plist-get (overlay-properties overlay) 'field))
459 (delete-overlay overlay)))
460 (delete-region start end))
462 (not (eq (car widget) 'hidden)))
463 (apply 'widget-create widget)
464 (put-text-property start (point) 'help-echo
465 (if (memq (car widget) '(text editable-field))
468 (when (eq (car widget) 'push-button)
469 (add-face-text-property start (point) 'eww-button t))))
471 (eww-fix-widget-keymap)))
473 (defun eww-fix-widget-keymap ()
474 (dolist (overlay (overlays-in (point-min) (point-max)))
475 (when (plist-get (overlay-properties overlay) 'button)
476 (overlay-put overlay 'local-map widget-keymap))))