-
Notifications
You must be signed in to change notification settings - Fork 101
/
rustic-playground.el
76 lines (65 loc) · 2.85 KB
/
rustic-playground.el
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;; rustic-playground.el --- Support for playground -*- lexical-binding:t -*-
;;; Commentary:
;;; Code:
(require 'org-element)
(require 'url)
;;; Options
(defcustom rustic-playground-url-format "https://play.rust-lang.org/?code=%s"
"Format string to use when submitting code to the playground."
:type 'string
:group 'rustic)
(defcustom rustic-shortener-url-format "https://is.gd/create.php?format=simple&url=%s"
"Format string to use for creating the shortened link of a playground submission."
:type 'string
:group 'rustic)
(defcustom rustic-playground-enable-shortener t
"Enable shortend URL for playground links."
:type 'boolean
:safe #'booleanp
:group 'rustic)
;;; Commands
;;;###autoload
(defun rustic-playground (begin end)
"Create a shareable URL for the contents of the current region,
src-block or buffer on the Rust playground."
(interactive "r")
(let (data)
(cond
((region-active-p)
(setq data (buffer-substring begin end)))
((org-in-src-block-p)
(setq data (org-element-property :value (org-element-at-point))))
(t
(setq data (buffer-substring (point-min) (point-max)))))
(let* ((escaped-data (url-hexify-string data))
(playground-url (format rustic-playground-url-format escaped-data))
(escaped-playground-url (url-hexify-string playground-url)))
(if (> (length escaped-playground-url) 5000)
(error "Encoded playground data exceeds 5000 character limit (length %s)"
(length escaped-playground-url))
(let ((shortener-url (format rustic-shortener-url-format escaped-playground-url))
(url-request-method "POST"))
(if rustic-playground-enable-shortener
(url-retrieve shortener-url
(lambda (state)
;; filter out the headers etc. included at the
;; start of the buffer: the relevant text
;; (shortened url or error message) is exactly
;; the last line.
(goto-char (point-max))
(let ((last-line (thing-at-point 'line t))
(err (plist-get state :error)))
(kill-buffer)
(if err
(error "Failed to shorten playground url: %s" last-line)
(let ((URL (read-from-minibuffer "Playground URL: " last-line)))
(browse-url URL))))))
(browse-url playground-url)))))))
(defun rustic-playground-buffer ()
"Create a shareable URL for the contents of the buffer on the Rust playground."
(interactive)
(rustic-playground (point-min) (point-max)))
(defalias 'rustic-playpen #'rustic-playground)
;;; _
(provide 'rustic-playground)
;;; rustic-playground.el ends here