Created
April 4, 2017 22:17
-
-
Save micheller/7e004459dcca814cda83259b2e2b20bd to your computer and use it in GitHub Desktop.
emacs lisp code to post snippets on repl.it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun get-buffer-data () | |
(let (bounds) | |
(setq bounds | |
(if (and transient-mark-mode mark-active) | |
(cons (region-beginning) (region-end)) | |
(cons (point) (point-max)))) | |
(buffer-substring-no-properties (car bounds) | |
(cdr bounds)))) | |
(defun buffer-mode (&optional buffer-or-name) | |
"Returns the major mode associated with a buffer. | |
If buffer-or-name is nil return current buffer's mode." | |
(buffer-local-value 'major-mode | |
(if buffer-or-name | |
(get-buffer buffer-or-name) | |
(current-buffer)))) | |
(defun get-lexer () | |
(first (split-string (symbol-name (buffer-mode)) "-"))) | |
(defun repl-it () | |
(interactive) | |
(let ((url-request-method "POST") | |
(url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded"))) | |
(url-request-data (concat "editor_text=" | |
(url-hexify-string (get-buffer-data)) | |
"&language=" | |
(url-hexify-string (get-lexer))))) | |
(when (and transient-mark-mode mark-active) | |
(deactivate-mark)) | |
(with-current-buffer | |
(url-retrieve-synchronously "https://repl.it/save") | |
(goto-char (point-min)) | |
(re-search-forward "^$") | |
(delete-region (point) (point-min)) | |
(let ((url (concat "https://repl.it/" (cdr (car (json-read)))))) | |
(message "posted to: %s" url) | |
(kill-new url))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment