Add form support.
[gnus] / lisp / eww.el
1 ;;; eww.el --- Emacs Web Wowser
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'shr)
29 (require 'url)
30
31 (defvar eww-current-url nil)
32 (defvar eww-history nil)
33
34 (defun eww (url)
35   "Fetch URL and render the page."
36   (interactive "sUrl: ")
37   (url-retrieve url 'eww-render (list url)))
38
39 (defun eww-render (status url &optional point)
40   (let* ((headers (eww-parse-headers))
41          (content-type
42           (mail-header-parse-content-type
43            (or (cdr (assoc "content-type" headers))
44                "text/plain")))
45          (charset (intern
46                    (downcase
47                     (or (cdr (assq 'charset (cdr content-type)))
48                         "utf8"))))
49          (data-buffer (current-buffer)))
50     (unwind-protect
51         (progn
52           (cond
53            ((equal (car content-type) "text/html")
54             (eww-display-html charset url))
55            ((string-match "^image/" (car content-type))
56             (eww-display-image))
57            (t
58             (eww-display-raw charset)))
59           (when point
60             (goto-char point)))
61       (kill-buffer data-buffer))))
62
63 (defun eww-parse-headers ()
64   (let ((headers nil))
65     (while (and (not (eobp))
66                 (not (eolp)))
67       (when (looking-at "\\([^:]+\\): *\\(.*\\)")
68         (push (cons (downcase (match-string 1))
69                     (match-string 2))
70               headers))
71       (forward-line 1))
72     (unless (eobp)
73       (forward-line 1))
74     headers))
75
76 (defun eww-display-html (charset url)
77   (unless (eq charset 'utf8)
78     (decode-coding-region (point) (point-max) charset))
79   (let ((document
80          (list
81           'base (list (cons 'href url))
82           (libxml-parse-html-region (point) (point-max)))))
83     (eww-setup-buffer)
84     (setq eww-current-url url)
85     (let ((inhibit-read-only t)
86           (shr-external-rendering-functions
87            '((form . eww-tag-form)
88              (input . eww-tag-input)
89              (submit . eww-tag-submit))))
90       (shr-insert-document document)
91       (eww-convert-widgets))
92     (goto-char (point-min))))
93
94 (defun eww-display-raw (charset)
95   (let ((data (buffer-substring (point) (point-max))))
96     (eww-setup-buffer)
97     (let ((inhibit-read-only t))
98       (insert data))
99     (goto-char (point-min))))
100
101 (defun eww-display-image ()
102   (let ((data (buffer-substring (point) (point-max))))
103     (eww-setup-buffer)
104     (let ((inhibit-read-only t))
105       (shr-put-image data nil))
106     (goto-char (point-min))))
107
108 (defun eww-setup-buffer ()
109   (pop-to-buffer (get-buffer-create "*eww*"))
110   (remove-overlays)
111   (setq widget-field-list nil)
112   (let ((inhibit-read-only t))
113     (erase-buffer))
114   (eww-mode))
115
116 (defvar eww-mode-map
117   (let ((map (make-sparse-keymap)))
118     (suppress-keymap map)
119     (define-key map "q" 'eww-quit)
120     (define-key map [tab] 'widget-forward)
121     (define-key map [backtab] 'widget-backward)
122     (define-key map [delete] 'scroll-down-command)
123     (define-key map "\177" 'scroll-down-command)
124     (define-key map " " 'scroll-up-command)
125     (define-key map "p" 'eww-previous-url)
126     ;;(define-key map "n" 'eww-next-url)
127     map))
128
129 (defun eww-mode ()
130   "Mode for browsing the web.
131
132 \\{eww-mode-map}"
133   (interactive)
134   (setq major-mode 'eww-mode
135         mode-name "eww")
136   (set (make-local-variable 'eww-current-url) 'author)
137   (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
138   (setq buffer-read-only t)
139   (use-local-map eww-mode-map))
140
141 (defun eww-browse-url (url &optional new-window)
142   (push (list eww-current-url (point))
143         eww-history)
144   (eww url))
145
146 (defun eww-quit ()
147   "Exit the Emacs Web Wowser."
148   (interactive)
149   (setq eww-history nil)
150   (kill-buffer (current-buffer)))
151
152 (defun eww-previous-url ()
153   "Go to the previously displayed page."
154   (interactive)
155   (when (zerop (length eww-history))
156     (error "No previous page"))
157   (let ((prev (pop eww-history)))
158     (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
159
160 ;; Form support.
161
162 (defvar eww-form nil)
163
164 (defun eww-tag-form (cont)
165   (let ((eww-form
166          (list (assq :method cont)
167                (assq :action cont)))
168         (start (point)))
169     (shr-ensure-paragraph)
170     (shr-generic cont)
171     (shr-ensure-paragraph)
172     (put-text-property start (1+ start)
173                        'eww-form eww-form)))
174
175 (defun eww-tag-input (cont)
176   (push (cons (cdr (assq :name cont))
177               (cdr (assq :value cont)))
178         eww-form)
179   (let ((start (point))
180         (widget (list
181                  'editable-field
182                  :size (string-to-number
183                         (or (cdr (assq :size cont))
184                             "40"))
185                  :value (or "____" (cdr (assq :value cont)) "")
186                  :action 'eww-submit)))
187     (apply 'widget-create widget)
188     (shr-generic cont)
189     (put-text-property start (point) 'eww-widget widget)))
190
191 (defun eww-convert-widgets ()
192   (let ((start (point-min))
193         widget)
194     (while (setq start (next-single-property-change start 'eww-widget))
195       (setq widget (get-text-property start 'eww-widget))
196       (goto-char start)
197       (delete-region start (next-single-property-change start 'eww-widget))
198       (apply 'widget-create widget)
199       (put-text-property start (point) 'not-read-only t))
200     (widget-setup)))
201
202 (provide 'eww)
203
204 ;;; eww.el ends here