Skip to content

Commit eaa332e

Browse files
committed
Add rust-playpen-buffer and rust-playpen-region for sharing code.
This will create (and shorten) a URL with the code on play.rust-lang.org.
1 parent cb5781d commit eaa332e

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

rust-mode.el

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
:type 'boolean
6868
:group 'rust-mode)
6969

70+
(defcustom rust-playpen-url-format "https://play.rust-lang.org/?code=%s"
71+
"Format string to use when submitting code to the playpen"
72+
:type 'string
73+
:group 'rust-mode)
74+
(defcustom rust-shortener-url-format "http://is.gd/create.php?format=simple&url=%s"
75+
"Format string to use for creating the shortened link of a playpen submission"
76+
:type 'string
77+
:group 'rust-mode)
78+
7079
(defun rust-paren-level () (nth 0 (syntax-ppss)))
7180
(defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
7281
(defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
@@ -115,7 +124,7 @@
115124
;; annoying to have to press tab again to align to a method chain
116125
;; than to have an over-eager indent in all other cases which must
117126
;; be undone via tab.
118-
127+
119128
(when (looking-at (concat "\s*\." rust-re-ident))
120129
(forward-line -1)
121130
(end-of-line)
@@ -128,7 +137,7 @@
128137
;; ^ ^
129138
;; | |
130139
;; | position of point
131-
;; returned offset
140+
;; returned offset
132141
;;
133142
((skip-dot-identifier
134143
(lambda ()
@@ -215,7 +224,7 @@
215224
(goto-char end-of-prev-line-pos)
216225
(back-to-indentation)
217226
(current-column))))))
218-
227+
219228
;; A function return type is indented to the corresponding function arguments
220229
((looking-at "->")
221230
(save-excursion
@@ -225,7 +234,7 @@
225234

226235
;; A closing brace is 1 level unindended
227236
((looking-at "}") (- baseline rust-indent-offset))
228-
237+
229238
;; Doc comments in /** style with leading * indent to line up the *s
230239
((and (nth 4 (syntax-ppss)) (looking-at "*"))
231240
(+ 1 baseline))
@@ -364,7 +373,7 @@
364373
(defun rust-look-for-raw-string (bound)
365374
;; Find a raw string, but only if it's not in the middle of another string or
366375
;; a comment
367-
376+
368377
(let* ((raw-str-regexp
369378
(rx
370379
(seq
@@ -745,6 +754,40 @@ See `compilation-error-regexp-alist for help on their format.")
745754
(cons 'rustc rustc-compilation-regexps))
746755
(add-to-list 'compilation-error-regexp-alist 'rustc)))
747756

757+
;;; Functions to submit (parts of) buffers to the rust playpen, for
758+
;;; sharing.
759+
(defun rust-playpen-region (begin end)
760+
"Create a sharable URL for the contents of the current region
761+
on the Rust playpen."
762+
(interactive "r")
763+
(let* ((data (buffer-substring begin end))
764+
(escaped-data (url-hexify-string data))
765+
(escaped-playpen-url (url-hexify-string (format rust-playpen-url-format escaped-data))))
766+
(if (> (length escaped-playpen-url) 5000)
767+
(error "encoded playpen data exceeds 5000 character limit (length %s)"
768+
(length escaped-playpen-url))
769+
(let ((shortener-url (format rust-shortener-url-format escaped-playpen-url))
770+
(url-request-method "POST"))
771+
(url-retrieve shortener-url
772+
(lambda (state)
773+
; filter out the headers etc. included at the
774+
; start of the buffer: the relevant text
775+
; (shortened url or error message) is exactly
776+
; the last line.
777+
(end-of-buffer)
778+
(let ((last-line (thing-at-point 'line t))
779+
(err (plist-get state :error)))
780+
(kill-buffer)
781+
(if err
782+
(error "failed to shorten playpen url: %s" last-line)
783+
(message "%s" last-line)))))))))
784+
785+
(defun rust-playpen-buffer ()
786+
"Create a sharable URL for the contents of the current buffer
787+
on the Rust playpen."
788+
(interactive)
789+
(rust-playpen-region (point-min) (point-max)))
790+
748791
(provide 'rust-mode)
749792

750793
;;; rust-mode.el ends here

0 commit comments

Comments
 (0)