Skip to content

Commit d548425

Browse files
tarsiusbrotzeit
authored andcommitted
Create rust-playpen.el from existing code
1 parent 139a658 commit d548425

File tree

3 files changed

+60
-42
lines changed

3 files changed

+60
-42
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ EMACS_ARGS ?=
1111

1212
ELS = rust-mode.el
1313
ELS += rust-compile.el
14+
ELS += rust-playpen.el
1415
ELS += rust-rustfmt.el
1516
ELCS = $(ELS:.el=.elc)
1617

rust-mode.el

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
;;; Code:
1717

1818
(eval-when-compile (require 'rx))
19-
(eval-when-compile (require 'url-vars))
2019

2120
(require 'json)
2221
(require 'thingatpt)
@@ -54,16 +53,6 @@ When nil, `where' will be aligned with `fn' or `trait'."
5453
:group 'rust-mode
5554
:safe #'booleanp)
5655

57-
(defcustom rust-playpen-url-format "https://play.rust-lang.org/?code=%s"
58-
"Format string to use when submitting code to the playpen."
59-
:type 'string
60-
:group 'rust-mode)
61-
62-
(defcustom rust-shortener-url-format "https://is.gd/create.php?format=simple&url=%s"
63-
"Format string to use for creating the shortened link of a playpen submission."
64-
:type 'string
65-
:group 'rust-mode)
66-
6756
(defcustom rust-match-angle-brackets t
6857
"Whether to enable angle bracket (`<' and `>') matching where appropriate."
6958
:type 'boolean
@@ -1621,37 +1610,6 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
16211610

16221611
;;; Secondary Commands
16231612

1624-
(defun rust-playpen-region (begin end)
1625-
"Create a shareable URL for the region from BEGIN to END on the Rust playpen."
1626-
(interactive "r")
1627-
(let* ((data (buffer-substring begin end))
1628-
(escaped-data (url-hexify-string data))
1629-
(escaped-playpen-url (url-hexify-string
1630-
(format rust-playpen-url-format escaped-data))))
1631-
(if (> (length escaped-playpen-url) 5000)
1632-
(error "encoded playpen data exceeds 5000 character limit (length %s)"
1633-
(length escaped-playpen-url))
1634-
(let ((shortener-url (format rust-shortener-url-format escaped-playpen-url))
1635-
(url-request-method "POST"))
1636-
(url-retrieve shortener-url
1637-
(lambda (state)
1638-
;; filter out the headers etc. included at the
1639-
;; start of the buffer: the relevant text
1640-
;; (shortened url or error message) is exactly
1641-
;; the last line.
1642-
(goto-char (point-max))
1643-
(let ((last-line (thing-at-point 'line t))
1644-
(err (plist-get state :error)))
1645-
(kill-buffer)
1646-
(if err
1647-
(error "failed to shorten playpen url: %s" last-line)
1648-
(message "%s" last-line)))))))))
1649-
1650-
(defun rust-playpen-buffer ()
1651-
"Create a shareable URL for the contents of the buffer on the Rust playpen."
1652-
(interactive)
1653-
(rust-playpen-region (point-min) (point-max)))
1654-
16551613
(defun rust-promote-module-into-dir ()
16561614
"Promote the module file visited by the current buffer into its own directory.
16571615

rust-playpen.el

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)