summaryrefslogtreecommitdiff
path: root/.emacs.d/lisp/rust-playpen.el
diff options
context:
space:
mode:
Diffstat (limited to '.emacs.d/lisp/rust-playpen.el')
-rw-r--r--.emacs.d/lisp/rust-playpen.el59
1 files changed, 59 insertions, 0 deletions
diff --git a/.emacs.d/lisp/rust-playpen.el b/.emacs.d/lisp/rust-playpen.el
new file mode 100644
index 0000000..1a9e583
--- /dev/null
+++ b/.emacs.d/lisp/rust-playpen.el
@@ -0,0 +1,59 @@
1;;; rust-playpen.el --- Support for the Rust Playground -*- lexical-binding:t -*-
2;;; Commentary:
3
4;; This library implements support for the Rust Playground,
5;; which is hosted at https://play.rust-lang.org and developed
6;; at https://github.com/integer32llc/rust-playground.
7
8;;; Code:
9
10(eval-when-compile (require 'url))
11
12;;; Options
13
14(defcustom rust-playpen-url-format "https://play.rust-lang.org/?code=%s"
15 "Format string to use when submitting code to the playpen."
16 :type 'string
17 :group 'rust-mode)
18
19(defcustom rust-shortener-url-format "https://is.gd/create.php?format=simple&url=%s"
20 "Format string to use for creating the shortened link of a playpen submission."
21 :type 'string
22 :group 'rust-mode)
23
24;;; Commands
25
26(defun rust-playpen-region (begin end)
27 "Create a shareable URL for the region from BEGIN to END on the Rust playpen."
28 (interactive "r")
29 (let* ((data (buffer-substring begin end))
30 (escaped-data (url-hexify-string data))
31 (escaped-playpen-url (url-hexify-string
32 (format rust-playpen-url-format escaped-data))))
33 (if (> (length escaped-playpen-url) 5000)
34 (error "encoded playpen data exceeds 5000 character limit (length %s)"
35 (length escaped-playpen-url))
36 (let ((shortener-url (format rust-shortener-url-format escaped-playpen-url))
37 (url-request-method "POST"))
38 (url-retrieve shortener-url
39 (lambda (state)
40 ;; filter out the headers etc. included at the
41 ;; start of the buffer: the relevant text
42 ;; (shortened url or error message) is exactly
43 ;; the last line.
44 (goto-char (point-max))
45 (let ((last-line (thing-at-point 'line t))
46 (err (plist-get state :error)))
47 (kill-buffer)
48 (if err
49 (error "failed to shorten playpen url: %s" last-line)
50 (message "%s" last-line)))))))))
51
52(defun rust-playpen-buffer ()
53 "Create a shareable URL for the contents of the buffer on the Rust playpen."
54 (interactive)
55 (rust-playpen-region (point-min) (point-max)))
56
57;;; _
58(provide 'rust-playpen)
59;;; rust-playpen.el ends here