* dgnushack.el (dgnushack-compile): Ignore eww on XEmacs.
[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 (require 'cl-lib)
31
32 (defvar eww-current-url nil)
33 (defvar eww-history nil)
34
35 (defun eww (url)
36   "Fetch URL and render the page."
37   (interactive "sUrl: ")
38   (url-retrieve url 'eww-render (list url)))
39
40 (defun eww-render (status url &optional point)
41   (let* ((headers (eww-parse-headers))
42          (content-type
43           (mail-header-parse-content-type
44            (or (cdr (assoc "content-type" headers))
45                "text/plain")))
46          (charset (intern
47                    (downcase
48                     (or (cdr (assq 'charset (cdr content-type)))
49                         "utf8"))))
50          (data-buffer (current-buffer)))
51     (unwind-protect
52         (progn
53           (cond
54            ((equal (car content-type) "text/html")
55             (eww-display-html charset url))
56            ((string-match "^image/" (car content-type))
57             (eww-display-image))
58            (t
59             (eww-display-raw charset)))
60           (when point
61             (goto-char point)))
62       (kill-buffer data-buffer))))
63
64 (defun eww-parse-headers ()
65   (let ((headers nil))
66     (while (and (not (eobp))
67                 (not (eolp)))
68       (when (looking-at "\\([^:]+\\): *\\(.*\\)")
69         (push (cons (downcase (match-string 1))
70                     (match-string 2))
71               headers))
72       (forward-line 1))
73     (unless (eobp)
74       (forward-line 1))
75     headers))
76
77 (defun eww-display-html (charset url)
78   (unless (eq charset 'utf8)
79     (decode-coding-region (point) (point-max) charset))
80   (let ((document
81          (list
82           'base (list (cons 'href url))
83           (libxml-parse-html-region (point) (point-max)))))
84     (eww-setup-buffer)
85     (setq eww-current-url url)
86     (let ((inhibit-read-only t))
87       (shr-insert-document document))
88     (goto-char (point-min))))
89
90 (defun eww-display-raw (charset)
91   (let ((data (buffer-substring (point) (point-max))))
92     (eww-setup-buffer)
93     (let ((inhibit-read-only t))
94       (insert data))
95     (goto-char (point-min))))
96
97 (defun eww-display-image ()
98   (let ((data (buffer-substring (point) (point-max))))
99     (eww-setup-buffer)
100     (let ((inhibit-read-only t))
101       (shr-put-image data nil))
102     (goto-char (point-min))))
103
104 (defun eww-setup-buffer ()
105   (pop-to-buffer (get-buffer-create "*eww*"))
106   (let ((inhibit-read-only t))
107     (erase-buffer))
108   (eww-mode))
109
110 (defvar eww-mode-map
111   (let ((map (make-sparse-keymap)))
112     (suppress-keymap map)
113     (define-key map "q" 'eww-quit)
114     (define-key map [tab] 'widget-forward)
115     (define-key map [backtab] 'widget-backward)
116     (define-key map [delete] 'scroll-down-command)
117     (define-key map "\177" 'scroll-down-command)
118     (define-key map " " 'scroll-up-command)
119     (define-key map "p" 'eww-previous-url)
120     ;;(define-key map "n" 'eww-next-url)
121     map))
122
123 (defun eww-mode ()
124   "Mode for browsing the web.
125
126 \\{eww-mode-map}"
127   (interactive)
128   (setq major-mode 'eww-mode
129         mode-name "eww")
130   (set (make-local-variable 'eww-current-url) 'author)
131   (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
132   (setq buffer-read-only t)
133   (use-local-map eww-mode-map))
134
135 (defun eww-browse-url (url &optional new-window)
136   (push (list eww-current-url (point))
137         eww-history)
138   (eww url))
139
140 (defun eww-quit ()
141   "Exit the Emacs Web Wowser."
142   (interactive)
143   (setq eww-history nil)
144   (kill-buffer (current-buffer)))
145
146 (defun eww-previous-url ()
147   "Go to the previously displayed page."
148   (interactive)
149   (when (zerop (length eww-history))
150     (error "No previous page"))
151   (let ((prev (pop eww-history)))
152     (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))
153
154 (provide 'eww)
155
156 ;;; eww.el ends here