1- ; ;; rs -playground.el --- Local Rust playground for short code snippets.
1+ ; ;; rust -playground.el --- Local Rust playground for short code snippets.
22
33; ; Copyright (C) 2016 Alexander I.Grafov (axel)
44
55; ; Author: Alexander I.Grafov <[email protected] >6- ; ; URL: https://github.com/grafov/rs -playground
6+ ; ; URL: https://github.com/grafov/rust -playground
77; ; Keywords: tools, rust
8- ; ; Package-Requires: ((emacs "24") (rust-mode "1.0 .0"))
8+ ; ; Package-Requires: ((emacs "24") (rust-mode "0.3 .0"))
99
1010; ; This program is free software; you can redistribute it and/or modify
1111; ; it under the terms of the GNU General Public License as published by
2323; ;; Commentary:
2424
2525; ; Local playground for the Rust programs similar to play.rust-lang.org.
26- ; ; `M-x rs -playground` and type you rust code then make&run it with `C-Return`.
26+ ; ; `M-x rust -playground` and type you rust code then make&run it with `C-Return`.
2727
2828; ; Playground works in conjunction with `rust-mode` and requires
2929; ; preconfigured environment for Rust language.
4646 :type 'string
4747 :group 'rust-mode )
4848
49- (defgroup rs -playground nil
49+ (defgroup rust -playground nil
5050 " Options specific to Rust Playground."
5151 :group 'rust-mode )
5252
53- (defcustom rs -playground-ask-file-name nil
53+ (defcustom rust -playground-ask-file-name nil
5454 " Non-nil means we ask for a name for the snippet.
5555
5656By default it will be created as snippet.go"
5757 :type 'boolean
58- :group 'rs -playground )
58+ :group 'rust -playground )
5959
60- (defcustom rs -playground-confirm-deletion t
61- " Non-nil means you will be asked for confirmation on the snippet deletion with `rs -playground-rm' .
60+ (defcustom rust -playground-confirm-deletion t
61+ " Non-nil means you will be asked for confirmation on the snippet deletion with `rust -playground-rm' .
6262
6363By default confirmation required."
6464 :type 'boolean
65- :group 'rs -playground )
65+ :group 'rust -playground )
6666
67- (defcustom rs -playground-basedir " ~/rs -playground"
67+ (defcustom rust -playground-basedir ( locate-user-emacs-file " rust -playground" )
6868 " Base directory for playground snippets."
69- :group 'rs -playground )
69+ :group 'rust -playground )
7070
71- (defvar-local rs -playground-current-snippet-file " snippet.rs"
71+ (defvar-local rust -playground-current-snippet-file " snippet.rs"
7272 " The current snippet file." )
7373
74- (define-minor-mode rs -playground-mode
74+ (define-minor-mode rust -playground-mode
7575 " A place for playing with golang code and export it in short snippets."
7676 :init-value nil
77- :lighter " "
78- :keymap '(([C-return] . rs-playground-exec))
79- (setq mode-name " Play(Rust)" ))
80-
81- (defun rs-playground-snippet-file-name (&optional snippet-name )
82- (setq-local rs-playground-current-snippet-file (let ((file-name (cond (snippet-name)
83- (rs-playground-ask-file-name
84- (read-string " Rust Playground filename: " ))
85- (" snippet" ))))
86- (concat (rs-playground-snippet-unique-dir file-name) " /" file-name " .rs" ))))
87-
88- (defun rs-playground-exec ()
77+ :lighter " Play(Rust)"
78+ :keymap '(([C-return] . rust-playground-exec)))
79+
80+ (defun rust-playground-snippet-file-name (&optional snippet-name )
81+ (setq-local rust-playground-current-snippet-file (let ((file-name (cond (snippet-name)
82+ (rust-playground-ask-file-name
83+ (read-string " Rust Playground filename: " )) (" snippet" ))))
84+ (concat (rust-playground-snippet-unique-dir file-name) " /" file-name " .rs" ))))
85+
86+ (defun rust-playground-exec ()
8987 " Save the buffer then runs Rust compiler for executing the code."
9088 (interactive )
9189 (make-local-variable 'compile-command )
@@ -94,83 +92,89 @@ By default confirmation required."
9492 (compile (concat rust-bin " " snippet-file " -o snippet && " (file-name-directory snippet-file) " snippet" ))))
9593
9694;;;### autoload
97- (defun rs -playground ()
95+ (defun rust -playground ()
9896 " Run playground for Rust language in a new buffer."
9997 (interactive )
100- (let ((snippet-file-name (rs -playground-snippet-file-name)))
98+ (let ((snippet-file-name (rust -playground-snippet-file-name)))
10199 (switch-to-buffer (create-file-buffer snippet-file-name))
102- (rs-playground-insert-template-head " snippet of code" )
100+ (add-hook 'kill-buffer-hook 'rust-playground-on-buffer-kill nil t )
101+ (rust-playground-insert-template-head " snippet of code" )
103102(insert " fn main() {
104103
105104 println!(\" Results:\" )
106105
107- }
108- " )
109- (backward-char 3 )
110- (rust-mode )
111- (rs-playground-mode)
112- (set-visited-file-name snippet-file-name t )))
106+ }" )
107+ (backward-char 3 )
108+ (rust-mode )
109+ (rust-playground-mode)
110+ (set-visited-file-name snippet-file-name t )))
111+
112+ ; remove compiled binary from snippet dir but not touch source files
113+ (defun rust-playground-on-buffer-kill ()
114+ (if (string-match-p (file-truename rust-playground-basedir) (file-truename (buffer-file-name )))
115+ (delete-file (concat (file-name-directory (buffer-file-name )) " snippet" ))))
113116
114- (defun rs -playground-insert-template-head (description )
115- (insert " // -*- mode:rust;mode:rs -playground -*-
117+ (defun rust -playground-insert-template-head (description )
118+ (insert " // -*- mode:rust;mode:rust -playground -*-
116119// " description " @ " (time-stamp-string " %:y-%02m-%02d %02H:%02M:%02S" ) "
117120
118121// === Rust Playground ===
119122// Execute the snippet with Ctl-Return
120- // Remove the snippet completely with its dir and all files M-x `rs -playground-rm`
123+ // Remove the snippet completely with its dir and all files M-x `rust -playground-rm`
121124
122125" ))
123126
124127;;;### autoload
125- (defun rs -playground-rm ()
128+ (defun rust -playground-rm ()
126129 " Remove files of the current snippet together with directory of this snippet."
127130 (interactive )
128- (if (string-match-p (file-truename rs -playground-basedir) (file-truename (buffer-file-name )))
129- (if (or (not rs -playground-confirm-deletion)
131+ (if (string-match-p (file-truename rust -playground-basedir) (file-truename (buffer-file-name )))
132+ (if (or (not rust -playground-confirm-deletion)
130133 (y-or-n-p (format " Do you want delete whole snippet dir %s ? "
131134 (file-name-directory (buffer-file-name )))))
132135 (progn
133136 (save-buffer )
134137 (delete-directory (file-name-directory (buffer-file-name )) t t )
138+ (remove-hook 'kill-buffer-hook 'rust-playground-on-buffer-kill t )
135139 (kill-buffer )))
136140 (message " Won't delete this! Because %s is not under the path %s . Remove the snippet manually! "
137- (buffer-file-name ) rs -playground-basedir)))
141+ (buffer-file-name ) rust -playground-basedir)))
138142
139143; ; ;;;###autoload
140- ; ; (defun rs -playground-download (url)
144+ ; ; (defun rust -playground-download (url)
141145; ; "Download a paste from the play.golang.org and insert it in a new local playground buffer.
142146; ; Tries to look for a URL at point."
143147; ; (interactive (list (read-from-minibuffer "Playground URL: " (ffap-url-p (ffap-string-at-point 'url)))))
144148; ; (with-current-buffer
145149; ; (let ((url-request-method "GET") url-request-data url-request-extra-headers)
146150; ; (url-retrieve-synchronously (concat url ".go")))
147- ; ; (let* ((snippet-file-name (rs -playground-snippet-file-name)) (buffer (create-file-buffer snippet-file-name)))
151+ ; ; (let* ((snippet-file-name (rust -playground-snippet-file-name)) (buffer (create-file-buffer snippet-file-name)))
148152; ; (goto-char (point-min))
149153; ; (re-search-forward "\n\n")
150154; ; (copy-to-buffer buffer (point) (point-max))
151155; ; (kill-buffer)
152156; ; (with-current-buffer buffer
153157; ; (goto-char (point-min))
154- ; ; (rs -playground-insert-template-head (concat url " imported"))
158+ ; ; (rust -playground-insert-template-head (concat url " imported"))
155159; ; (rust-mode)
156- ; ; (rs -playground-mode)
160+ ; ; (rust -playground-mode)
157161; ; (set-visited-file-name snippet-file-name t)
158162; ; (switch-to-buffer buffer)))))
159163
160- ; ; (defun rs -playground-upload ()
164+ ; ; (defun rust -playground-upload ()
161165; ; "Upload the current buffer to play.golang.org and return the short URL of the playground."
162166; ; (interactive)
163167; ; (goto-char (point-min))
164168; ; (forward-line)
165169; ; (insert (rust-playpen-buffer)))
166170
167- (defun rs -playground-snippet-unique-dir (prefix )
168- " Get unique directory under `rs -playground-basedir`."
169- (let ((dir-name (concat rs -playground-basedir " /"
170- (if (and prefix rs -playground-ask-file-name) (concat prefix " -" ))
171+ (defun rust -playground-snippet-unique-dir (prefix )
172+ " Get unique directory under `rust -playground-basedir`."
173+ (let ((dir-name (concat rust -playground-basedir " /"
174+ (if (and prefix rust -playground-ask-file-name) (concat prefix " -" ))
171175 (time-stamp-string " at-%:y-%02m-%02d-%02H%02M%02S" ))))
172176 (make-directory dir-name t )
173177 dir-name))
174178
175- (provide 'rs -playground )
176- ; ;; rs -playground.el ends here
179+ (provide 'rust -playground )
180+ ; ;; rust -playground.el ends here
0 commit comments