eww.el 16.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
;;; eww.el --- Emacs Web Wowser

;; Copyright (C) 2013 Free Software Foundation, Inc.

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: html

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(eval-when-compile (require 'cl))
28
(require 'format-spec)
29 30
(require 'shr)
(require 'url)
31
(require 'mm-url)
32

33 34 35 36 37 38 39 40 41 42 43 44 45
(defgroup eww nil
  "Emacs Web Wowser"
  :version "24.4"
  :group 'hypermedia
  :prefix "eww-")

(defcustom eww-header-line-format "%t: %u"
  "Header line format.
- %t is replaced by the title.
- %u is replaced by the URL."
  :group 'eww
  :type 'string)

46 47 48 49 50 51 52 53
(defface eww-button
  '((((type x w32 ns) (class color))	; Like default mode line
     :box (:line-width 2 :style released-button)
     :background "lightgrey" :foreground "black"))
  "Face for eww buffer buttons."
  :version "24.4"
  :group 'eww)

54
(defvar eww-current-url nil)
55 56
(defvar eww-current-title ""
  "Title of current page.")
57 58
(defvar eww-history nil)

59 60 61 62 63
(defvar eww-next-url nil)
(defvar eww-previous-url nil)
(defvar eww-up-url nil)
(defvar eww-top-url nil)

64
;;;###autoload
65 66 67
(defun eww (url)
  "Fetch URL and render the page."
  (interactive "sUrl: ")
68 69
  (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
    (setq url (concat "http://" url)))
70 71
  (url-retrieve url 'eww-render (list url)))

72 73 74 75 76 77
;;;###autoload
(defun eww-open-file (file)
  "Render a file using EWW."
  (interactive "fFile: ")
  (eww (concat "file://" (expand-file-name file))))

78
(defun eww-render (status url &optional point)
79 80 81
  (let ((redirect (plist-get status :redirect)))
    (when redirect
      (setq url redirect)))
82 83 84 85
  (set (make-local-variable 'eww-next-url) nil)
  (set (make-local-variable 'eww-previous-url) nil)
  (set (make-local-variable 'eww-up-url) nil)
  (set (make-local-variable 'eww-top-url) nil)
86
  (let* ((headers (eww-parse-headers))
87 88 89
	 (shr-target-id
	  (and (string-match "#\\(.*\\)" url)
	       (match-string 1 url)))
90 91 92 93 94 95 96
	 (content-type
	  (mail-header-parse-content-type
	   (or (cdr (assoc "content-type" headers))
	       "text/plain")))
	 (charset (intern
		   (downcase
		    (or (cdr (assq 'charset (cdr content-type)))
97 98
			(eww-detect-charset (equal (car content-type)
						   "text/html"))
99 100 101 102 103 104 105 106 107 108 109
			"utf8"))))
	 (data-buffer (current-buffer)))
    (unwind-protect
	(progn
	  (cond
	   ((equal (car content-type) "text/html")
	    (eww-display-html charset url))
	   ((string-match "^image/" (car content-type))
	    (eww-display-image))
	   (t
	    (eww-display-raw charset)))
110 111 112 113 114 115 116 117
	  (cond
	   (point
	    (goto-char point))
	   (shr-target-id
	    (let ((point (next-single-property-change
			  (point-min) 'shr-target-id)))
	      (when point
		(goto-char (1+ point)))))))
118 119 120 121
      (kill-buffer data-buffer))))

(defun eww-parse-headers ()
  (let ((headers nil))
122
    (goto-char (point-min))
123 124 125 126 127 128 129 130 131 132 133
    (while (and (not (eobp))
		(not (eolp)))
      (when (looking-at "\\([^:]+\\): *\\(.*\\)")
	(push (cons (downcase (match-string 1))
		    (match-string 2))
	      headers))
      (forward-line 1))
    (unless (eobp)
      (forward-line 1))
    headers))

134 135 136 137 138 139 140 141 142 143 144 145
(defun eww-detect-charset (html-p)
  (let ((case-fold-search t)
	(pt (point)))
    (or (and html-p
	     (re-search-forward
	      "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)" nil t)
	     (goto-char pt)
	     (match-string 1))
	(and (looking-at
	      "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
	     (match-string 1)))))

146 147 148 149 150 151 152 153 154
(defun eww-display-html (charset url)
  (unless (eq charset 'utf8)
    (decode-coding-region (point) (point-max) charset))
  (let ((document
	 (list
	  'base (list (cons 'href url))
	  (libxml-parse-html-region (point) (point-max)))))
    (eww-setup-buffer)
    (setq eww-current-url url)
155
    (eww-update-header-line-format)
156
    (let ((inhibit-read-only t)
157
	  (shr-width nil)
158
	  (shr-external-rendering-functions
159 160
	   '((title . eww-tag-title)
	     (form . eww-tag-form)
161
	     (input . eww-tag-input)
162 163
	     (textarea . eww-tag-textarea)
	     (body . eww-tag-body)
164 165 166
	     (select . eww-tag-select)
	     (link . eww-tag-link)
	     (a . eww-tag-a))))
167 168
      (shr-insert-document document)
      (eww-convert-widgets))
169 170
    (goto-char (point-min))))

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
(defun eww-handle-link (cont)
  (let* ((rel (assq :rel cont))
  	(href (assq :href cont))
	(where (assoc (cdr rel)
		      '(("next" . eww-next-url)
			("previous" . eww-previous-url)
			("start" . eww-top-url)
			("up" . eww-up-url)))))
    (and href
	 where
	 (set (cdr where) (cdr href)))))

(defun eww-tag-link (cont)
  (eww-handle-link cont)
  (shr-generic cont))

(defun eww-tag-a (cont)
  (eww-handle-link cont)
  (shr-tag-a cont))

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
(defun eww-update-header-line-format ()
  (if eww-header-line-format
      (setq header-line-format (format-spec eww-header-line-format
                                            `((?u . ,eww-current-url)
                                              (?t . ,eww-current-title))))
    (setq header-line-format nil)))

(defun eww-tag-title (cont)
  (setq eww-current-title "")
  (dolist (sub cont)
    (when (eq (car sub) 'text)
      (setq eww-current-title (concat eww-current-title (cdr sub)))))
  (eww-update-header-line-format))

(defun eww-tag-body (cont)
  (let* ((start (point))
	 (fgcolor (cdr (or (assq :fgcolor cont)
                           (assq :text cont))))
	 (bgcolor (cdr (assq :bgcolor cont)))
	 (shr-stylesheet (list (cons 'color fgcolor)
			       (cons 'background-color bgcolor))))
    (shr-generic cont)
    (eww-colorize-region start (point) fgcolor bgcolor)))

(defun eww-colorize-region (start end fg &optional bg)
  (when (or fg bg)
    (let ((new-colors (shr-color-check fg bg)))
      (when new-colors
	(when fg
220 221
	  (add-face-text-property start end
				  (list :foreground (cadr new-colors))))
222
	(when bg
223 224
	  (add-face-text-property start end
				  (list :background (car new-colors))))))))
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
(defun eww-display-raw (charset)
  (let ((data (buffer-substring (point) (point-max))))
    (eww-setup-buffer)
    (let ((inhibit-read-only t))
      (insert data))
    (goto-char (point-min))))

(defun eww-display-image ()
  (let ((data (buffer-substring (point) (point-max))))
    (eww-setup-buffer)
    (let ((inhibit-read-only t))
      (shr-put-image data nil))
    (goto-char (point-min))))

(defun eww-setup-buffer ()
  (pop-to-buffer (get-buffer-create "*eww*"))
242 243
  (remove-overlays)
  (setq widget-field-list nil)
244 245 246 247 248 249 250 251
  (let ((inhibit-read-only t))
    (erase-buffer))
  (eww-mode))

(defvar eww-mode-map
  (let ((map (make-sparse-keymap)))
    (suppress-keymap map)
    (define-key map "q" 'eww-quit)
252
    (define-key map "g" 'eww-reload)
253 254
    (define-key map [tab] 'shr-next-link)
    (define-key map [backtab] 'shr-previous-link)
255 256 257
    (define-key map [delete] 'scroll-down-command)
    (define-key map "\177" 'scroll-down-command)
    (define-key map " " 'scroll-up-command)
258 259
    (define-key map "l" 'eww-back-url)
    (define-key map "n" 'eww-next-url)
260
    (define-key map "p" 'eww-previous-url)
261 262
    (define-key map "u" 'eww-up-url)
    (define-key map "t" 'eww-top-url)
263 264
    map))

265
(define-derived-mode eww-mode nil "eww"
266 267 268 269
  "Mode for browsing the web.

\\{eww-mode-map}"
  (set (make-local-variable 'eww-current-url) 'author)
270
  (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url))
271 272

(defun eww-browse-url (url &optional new-window)
273 274 275
  (push (list eww-current-url (point))
	eww-history)
  (eww url))
276 277 278 279 280 281 282

(defun eww-quit ()
  "Exit the Emacs Web Wowser."
  (interactive)
  (setq eww-history nil)
  (kill-buffer (current-buffer)))

283
(defun eww-back-url ()
284 285 286 287 288 289 290
  "Go to the previously displayed page."
  (interactive)
  (when (zerop (length eww-history))
    (error "No previous page"))
  (let ((prev (pop eww-history)))
    (url-retrieve (car prev) 'eww-render (list (car prev) (cadr prev)))))

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
(defun eww-next-url ()
  "Go to the page marked `next'.
A page is marked `next' if rel=\"next\" appears in a <link>
or <a> tag."
  (interactive)
  (if eww-next-url
      (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
    (error "No `next' on this page")))

(defun eww-previous-url ()
  "Go to the page marked `previous'.
A page is marked `previous' if rel=\"previous\" appears in a <link>
or <a> tag."
  (interactive)
  (if eww-previous-url
      (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
    (error "No `previous' on this page")))

(defun eww-up-url ()
  "Go to the page marked `up'.
A page is marked `up' if rel=\"up\" appears in a <link>
or <a> tag."
  (interactive)
  (if eww-up-url
      (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
    (error "No `up' on this page")))

(defun eww-top-url ()
  "Go to the page marked `top'.
A page is marked `top' if rel=\"start\" appears in a <link>
or <a> tag."
  (interactive)
  (if eww-top-url
      (eww-browse-url (shr-expand-url eww-top-url eww-current-url))
    (error "No `top' on this page")))

327 328 329 330 331 332
(defun eww-reload ()
  "Reload the current page."
  (interactive)
  (url-retrieve eww-current-url 'eww-render
		(list eww-current-url (point))))

333 334 335 336 337 338 339 340 341 342 343
;; Form support.

(defvar eww-form nil)

(defun eww-tag-form (cont)
  (let ((eww-form
	 (list (assq :method cont)
	       (assq :action cont)))
	(start (point)))
    (shr-ensure-paragraph)
    (shr-generic cont)
344 345 346
    (unless (bolp)
      (insert "\n"))
    (insert "\n")
347 348 349
    (when (> (point) start)
      (put-text-property start (1+ start)
			 'eww-form eww-form))))
350 351

(defun eww-tag-input (cont)
352 353 354
  (let* ((start (point))
	 (type (downcase (or (cdr (assq :type cont))
			     "text")))
355
	 (value (cdr (assq :value cont)))
356 357
	 (widget
	  (cond
358 359
	   ((or (equal type "submit")
		(equal type "image"))
360 361 362
	    (list 'push-button
		  :notify 'eww-submit
		  :name (cdr (assq :name cont))
363 364 365
		  :value (if (zerop (length value))
			     "Submit"
			   value)
366
		  :eww-form eww-form
367 368 369
		  (or (if (zerop (length value))
			  "Submit"
			value))))
370 371 372 373 374
	   ((or (equal type "radio")
		(equal type "checkbox"))
	    (list 'checkbox
		  :notify 'eww-click-radio
		  :name (cdr (assq :name cont))
375
		  :checkbox-value value
376
		  :checkbox-type type
377 378 379 380 381
		  :eww-form eww-form
		  (cdr (assq :checked cont))))
	   ((equal type "hidden")
	    (list 'hidden
		  :name (cdr (assq :name cont))
382
		  :value value))
383
	   (t
384 385 386 387
	    (list 'editable-field
		  :size (string-to-number
			 (or (cdr (assq :size cont))
			     "40"))
388
		  :value (or value "")
389 390 391 392 393 394
		  :secret (and (equal type "password") ?*)
		  :action 'eww-submit
		  :name (cdr (assq :name cont))
		  :eww-form eww-form)))))
    (nconc eww-form (list widget))
    (unless (eq (car widget) 'hidden)
395
      (apply 'widget-create widget)
396 397
      (put-text-property start (point) 'eww-widget widget)
      (insert " "))))
398

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
(defun eww-tag-textarea (cont)
  (let* ((start (point))
	 (widget
	  (list 'text
		:size (string-to-number
		       (or (cdr (assq :cols cont))
			   "40"))
		:value (or (cdr (assq 'text cont)) "")
		:action 'eww-submit
		:name (cdr (assq :name cont))
		:eww-form eww-form)))
    (nconc eww-form (list widget))
    (apply 'widget-create widget)
    (put-text-property start (point) 'eww-widget widget)))

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
(defun eww-tag-select (cont)
  (shr-ensure-paragraph)
  (let ((menu (list 'menu-choice
		    :name (cdr (assq :name cont))
		    :eww-form eww-form))
	(options nil)
	(start (point)))
    (dolist (elem cont)
      (when (eq (car elem) 'option)
	(when (cdr (assq :selected (cdr elem)))
	  (nconc menu (list :value
			    (cdr (assq :value (cdr elem))))))
	(push (list 'item
		    :value (cdr (assq :value (cdr elem)))
		    :tag (cdr (assq 'text (cdr elem))))
	      options)))
430 431 432 433 434 435 436 437
    (when options
      ;; If we have no selected values, default to the first value.
      (unless (plist-get (cdr menu) :value)
	(nconc menu (list :value (nth 2 (car options)))))
      (nconc menu options)
      (apply 'widget-create menu)
      (put-text-property start (point) 'eww-widget menu)
      (shr-ensure-paragraph))))
438

439 440 441
(defun eww-click-radio (widget &rest ignore)
  (let ((form (plist-get (cdr widget) :eww-form))
	(name (plist-get (cdr widget) :name)))
442 443 444 445 446 447 448 449 450 451
    (when (equal (plist-get (cdr widget) :type) "radio")
      (if (widget-value widget)
	  ;; Switch all the other radio buttons off.
	  (dolist (overlay (overlays-in (point-min) (point-max)))
	    (let ((field (plist-get (overlay-properties overlay) 'button)))
	      (when (and (eq (plist-get (cdr field) :eww-form) form)
			 (equal name (plist-get (cdr field) :name)))
		(unless (eq field widget)
		  (widget-value-set field nil)))))
	(widget-value-set widget t)))
452 453 454 455
    (eww-fix-widget-keymap)))

(defun eww-submit (widget &rest ignore)
  (let ((form (plist-get (cdr widget) :eww-form))
456
	values)
457 458 459 460
    (dolist (overlay (sort (overlays-in (point-min) (point-max))
			   (lambda (o1 o2)
			     (< (overlay-start o1) (overlay-start o2)))))
      (let ((field (or (plist-get (overlay-properties overlay) 'field)
461
		       (plist-get (overlay-properties overlay) 'button))))
462 463
	(when (eq (plist-get (cdr field) :eww-form) form)
	  (let ((name (plist-get (cdr field) :name)))
464
	    (when name
465 466 467 468 469 470 471
	      (cond
	       ((eq (car field) 'checkbox)
		(when (widget-value field)
		  (push (cons name (plist-get (cdr field) :checkbox-value))
			values)))
	       ((eq (car field) 'push-button)
		;; We want the values from buttons if we hit a button,
472 473 474 475
		;; if it's the first button in the DOM after the field
		;; hit ENTER on.
		(when (and (eq (car widget) 'push-button)
			   (eq widget field))
476 477 478 479 480 481 482 483 484 485 486
		  (push (cons name (widget-value field))
			values)))
	       (t
		(push (cons name (widget-value field))
		      values))))))))
    (dolist (elem form)
      (when (and (consp elem)
		 (eq (car elem) 'hidden))
	(push (cons (plist-get (cdr elem) :name)
		    (plist-get (cdr elem) :value))
	      values)))
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    ;; If we hit ENTER in a non-button field, include the value of the
    ;; first submit button after it.
    (unless (eq (car widget) 'push-button)
      (let ((rest form)
	    (name (plist-get (cdr widget) :name)))
	(when rest
	  (while (and rest
		      (or (not (consp (car rest)))
			  (not (equal name (plist-get (cdar rest) :name)))))
	    (pop rest)))
	(while rest
	  (let ((elem (pop rest)))
	    (when (and (consp (car rest))
		       (eq (car elem) 'push-button))
	      (push (cons (plist-get (cdr elem) :name)
			  (plist-get (cdr elem) :value))
		    values)
	      (setq rest nil))))))
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    (if (and (stringp (cdr (assq :method form)))
	     (equal (downcase (cdr (assq :method form))) "post"))
	(let ((url-request-method "POST")
	      (url-request-extra-headers
	       '(("Content-Type" . "application/x-www-form-urlencoded")))
	      (url-request-data (mm-url-encode-www-form-urlencoded values)))
	  (eww-browse-url (shr-expand-url (cdr (assq :action form))
					  eww-current-url)))
      (eww-browse-url
       (concat
	(if (cdr (assq :action form))
	    (shr-expand-url (cdr (assq :action form))
			    eww-current-url)
	  eww-current-url)
	"?"
	(mm-url-encode-www-form-urlencoded values))))))
521 522 523 524

(defun eww-convert-widgets ()
  (let ((start (point-min))
	widget)
525 526 527 528
    ;; Some widgets come from different buffers (rendered for tables),
    ;; so we need to nix out the list of widgets and recreate them.
    (setq widget-field-list nil
	  widget-field-new nil)
529 530 531
    (while (setq start (if (get-text-property start 'eww-widget)
			   start
			 (next-single-property-change start 'eww-widget)))
532 533
      (setq widget (get-text-property start 'eww-widget))
      (goto-char start)
534 535
      (let ((end (next-single-property-change start 'eww-widget)))
	(dolist (overlay (overlays-in start end))
536 537
	  (when (or (plist-get (overlay-properties overlay) 'button)
		    (plist-get (overlay-properties overlay) 'field))
538 539
	    (delete-overlay overlay)))
	(delete-region start end))
540 541
      (when (and widget
		 (not (eq (car widget) 'hidden)))
542 543 544 545 546 547 548
	(apply 'widget-create widget)
	(put-text-property start (point) 'help-echo
			   (if (memq (car widget) '(text editable-field))
			       "Input field"
			     "Button"))
	(when (eq (car widget) 'push-button)
	  (add-face-text-property start (point) 'eww-button t))))
549 550 551 552 553 554 555
    (widget-setup)
    (eww-fix-widget-keymap)))

(defun eww-fix-widget-keymap ()
  (dolist (overlay (overlays-in (point-min) (point-max)))
    (when (plist-get (overlay-properties overlay) 'button)
      (overlay-put overlay 'local-map widget-keymap))))
556

557 558 559
(provide 'eww)

;;; eww.el ends here