               (delete-region (point-min) (point))
               (normal-mode)
               (set-buffer-modified-p nil)
-              (switch-to-buffer pastie-buffer))
+              (switch-to-buffer *pastie-buffer*))
           (message "Error occured: %s" status)
-          (kill-buffer pastie-buffer))))))
+          (kill-buffer *pastie-buffer*))))))
 
+(defun pastie-browse ()
+  (interactive)
+  (browse-url *pastie-last-url*))
 
-(provide 'pastie)+(provide 'pastie)

                               (diff-mode . "diff")
                               (html-mode . "html")
                               (java-mode . "java")
                               (javascript-mode . "javascript")
                               (jde-mode . "java")
                               (php-mode . "php")
                               (python-mode . "python")
                               (ruby-mode . "ruby")
                               (text-mode . "plain_text")
                               (sql-mode . "sql")
                               (sh-mode . "shell-unix-generic"))))
      "plain_text"))

(defun pastie-region (begin end)
  "Post the current region as a new paste at pastie.caboo.se.
Copies the URL into the kill ring."
  (interactive "r")

  (let* ((body-raw (buffer-substring begin end))
         (body (replace-regexp-in-string
                "[<>&]"
                (lambda (match)

                    (?> "&gt;")
                    (?& "&amp;")))
                body-raw))
         (mode (pastie-language))
         (url-request-method "POST")
         (url-mime-accept-string "application/xml")
         (url-request-extra-headers '(("Content-Type" . "application/xml")))
         (url (url-generic-parse-url "http://pastie.org/pastes"))
	 (restricted (if *pastie-restricted* "<restricted>1</restricted>"
		       ""))
         (url-request-data
          (concat "<paste>"
                  "<parser>" mode "</parser>"
                  "<authorization>burger</authorization>"
		  restricted
                  "<body>" body "</body>"
                  "</paste>")))
    (with-timeout (*pastie-timeout* (error "Pastie timed out."))
      (pastie-retrieve url))))

(defun pastie-retrieve (url)
  "Submits the request and validates the response."
  (let ((*pastie-buffer* (url-retrieve-synchronously url)))
    (with-current-buffer *pastie-buffer*
      (goto-char (point-min))
      (search-forward-regexp "^Status: \\([0-9]+.*\\)")
      (let ((status (match-string 1)))
	(if (string-match "^20[01]" status)
	    (progn
	      (goto-char (point-max))
	      (beginning-of-line)
	      (let ((id (buffer-substring (point) (point-max))))
		(let ((url (format (pastie-url-format) id)))
		  (message "Paste created: %s" url)
		  (setq *pastie-last-url* url)
		  (kill-new url))))
	  (message "Error occured: %s" status))))
    (kill-buffer *pastie-buffer*)))

(defun pastie-buffer ()
  "Post the current buffer as a new paste at pastie.org.
Copies the URL into the kill ring."
  (interactive)
  (pastie-region (point-min) (point-max)))

(defun pastie-get (id)
  "Fetch the contents of the paste from pastie.org into a new buffer."
  (interactive "nPastie #: ")

  (let ((url-request-method "GET")
        (url-request-extra-headers nil)
        (url-mime-accept-string "*/*")
        (url (url-generic-parse-url
              (format "http://pastie.org/pastes/%s/download" id))))
    (setq *pastie-buffer* (url-retrieve-synchronously url))

    (with-current-buffer *pastie-buffer*
      (goto-char (point-min))
      (search-forward-regexp "^Status: \\([0-9]+.*\\)")
      (let ((status (match-string 1)))
