Permalink
Browse files

implemented codepad-fetch-code

  • Loading branch information...
1 parent 4ee62f9 commit dff74d74e4a86cd4c07f6f138be4db9942b35df1 @ruediger committed Nov 30, 2009
Showing with 37 additions and 2 deletions.
  1. +1 −0 README
  2. +36 −2 codepad.el
View
1 README
@@ -18,6 +18,7 @@ Add the following lines to your ~/.emacs
(add-to-list 'load-path "PATH") ;; replace PATH with the path to codepad.el
(autoload 'codepad-paste-region "codepad" "paste region to codepad.org" t)
(autoload 'codepad-paste-buffer "codepad" "paste buffer to codepad.org" t)
+ (autoload 'codepad-fetch-code "codepad" "Fetch code from codepad.org." t)
Restart Emacs (or evaluate those lines) and you can use codepad.el by calling M-x codepad-paste-region to paste the current selected region or M-x codepad-paste-buffer to paste the current selected buffer.
View
@@ -36,7 +36,6 @@
;; TODO:
-;; * fetch code from codepad.org
;; * fetch Output from codepad.org (if run is True)
;; * support projects (http://project.codepad.org)
;; * support user accounts
@@ -196,8 +195,43 @@ should both be strings."
(interactive)
(codepad-paste-region (point-min) (point-max) private synchronously))
+(defconst +codepad-mime-to-mode+ '(("c++src" . c++-mode)
+ ("csrc" . c-mode)
+ ("python" . python-mode))
+ "MIME text/x-... to emacs mode.")
+
;;;###autoload
-;(defun codepad-fetch (id))
+(defun codepad-fetch-code (id &optional buffer-name)
+ "Fetch code from codepad.org.
+Argument ID is the codepad id and
+optional argument is the BUFFER-NAME where to write."
+ (interactive "sCodepad ID: ")
+ (let* ((just-id (replace-regexp-in-string "^.*/" "" id)) ; strip http://...
+ (buffer-name (or buffer-name (format "*codepad %s*" just-id)))
+ (url (concat +codepad-url+ "/" just-id "/raw"))
+ (buffer (get-buffer buffer-name)))
+ (unless (bufferp buffer)
+ (message "Fetching %s from Codepad" just-id)
+ (setq buffer (url-retrieve-synchronously url))
+ (with-current-buffer buffer
+ (rename-buffer buffer-name t)
+ (goto-char (point-min))
+ (re-search-forward "\n\n")
+ (let ((header-end (point)))
+ (goto-char (point-min))
+ ;; Determine and set mode
+ (when (re-search-forward "^[cC]ontent-[tT]ype: \\(.*\\)$"
+ header-end t)
+ (let ((content-type (match-string 1)))
+ (when (string-match "text/x-\\([^;[:space:]]*\\)" content-type)
+ (let ((mode (cdr (assoc (match-string 1 content-type)
+ +codepad-mime-to-mode+))))
+ (when mode
+ (funcall mode))))))
+ ;; Delete Headers
+ (delete-region (point-min) header-end)
+ (set-buffer-modified-p nil))))
+ (switch-to-buffer-other-window buffer)))
(provide 'codepad)
;;; codepad.el ends here

0 comments on commit dff74d7

Please sign in to comment.