Skip to content

Commit

Permalink
Add new command go-playground-cmd
Browse files Browse the repository at this point in the history
It allows apply custom arguments to the compile mode.
  • Loading branch information
grafov committed Jun 25, 2019
1 parent b107196 commit 34aa3a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ compiler as `go run *.go` so any sources from the snippet directory will be incl
| `go-playground` | Create a new playground buffer with basic template for the `main` package. |
| `go-playground-download` | Download the snippet from the URL at play.golang.org. |
| `go-playground-exec` | Save, compile and run the code of the snippet. |
| `go-playground-cmd` | Save the code then prompts for the command (compile-mode used). |
| `go-playground-upload` | Upload the buffer to play.golang.org and return the short URL. |
| `go-playground-rm` | Remove the snippet with its directory with all files. |
-->
Expand All @@ -77,6 +78,7 @@ compiler as `go run *.go` so any sources from the snippet directory will be incl
| `go-playground` | Create a new playground buffer with basic template for the `main` package. |
| `go-playground-download` | Download the snippet from the URL at play.golang.org. |
| `go-playground-exec` | Save, compile and run the code of the snippet. |
| `go-playground-cmd` | Save the code then prompts for the command (compile-mode used). |
| `go-playground-upload` | Upload the buffer to play.golang.org and return the short URL. |
| `go-playground-rm` | Remove the snippet with its directory with all files. |
<!--- END RECEIVE ORGTBL keys -->
Expand Down
64 changes: 37 additions & 27 deletions go-playground.el
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
;;; go-playground.el --- Local Golang playground for short snippets.

;; Copyright (C) 2015-2018 Alexander I.Grafov (axel)
;; Copyright (C) 2015-2019 Alexander I.Grafov

;; Author: Alexander I.Grafov (axel) <grafov@gmail.com>
;; Author: Alexander I.Grafov <grafov@gmail.com>
;; URL: https://github.com/grafov/go-playground
;; Keywords: tools, golang
;; Version: 1.4
;; Version: 1.5
;; Package-Requires: ((emacs "24") (go-mode "1.4.0") (gotest "0.13.0"))

;; This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -80,10 +80,10 @@ By default confirmation required."

(defun go-playground-snippet-file-name(&optional snippet-name)
(let ((file-name (cond (snippet-name)
(go-playground-ask-file-name
(read-string "Go Playground filename: "))
("snippet"))))
(concat (go-playground-snippet-unique-dir file-name) "/" file-name ".go")))
(go-playground-ask-file-name
(read-string "Go Playground filename: "))
("snippet"))))
(concat (go-playground-snippet-unique-dir file-name) "/" file-name ".go")))

;
(defun go-playground-save-and-run ()
Expand All @@ -100,12 +100,22 @@ By default confirmation required."
(make-local-variable 'compile-command)
(compile (concat go-command " " go-playground-go-command-args)))))

(defun go-playground-cmd (cmd)
"Save the buffer then apply custom compile command from
minibuffer to the files or buffer."
(interactive "scompile command: ")
(if (go-playground-inside)
(progn
(save-buffer t)
(make-local-variable 'compile-command)
(compile cmd))))

;;;###autoload
(defun go-playground ()
"Run playground for Go language in a new buffer."
(interactive)
(let ((snippet-file-name (go-playground-snippet-file-name)))
(switch-to-buffer (create-file-buffer snippet-file-name))
(switch-to-buffer (create-file-buffer snippet-file-name))
(go-playground-insert-template-head "snippet of code")
(insert "package main
Expand All @@ -117,10 +127,10 @@ func main() {
fmt.Println(\"Results:\")
}
")
(backward-char 3)
(go-mode)
(go-playground-mode)
(set-visited-file-name snippet-file-name t)))
(backward-char 3)
(go-mode)
(go-playground-mode)
(set-visited-file-name snippet-file-name t)))

(defun go-playground-insert-template-head (description)
(insert "// -*- mode:go;mode:go-playground -*-
Expand All @@ -136,7 +146,7 @@ func main() {
"Remove files of the current snippet together with directory of this snippet."
(interactive)
(if (go-playground-inside)
(if (or (not go-playground-confirm-deletion)
(if (or (not go-playground-confirm-deletion)
(y-or-n-p (format "Do you want delete whole snippet dir %s? "
(file-name-directory (buffer-file-name)))))
(progn
Expand All @@ -148,7 +158,7 @@ func main() {

;;;###autoload
(defun go-playground-remove-current-snippet ()
"Obsoleted by `go-playground-rm'."
"Obsoleted by `go-playground-rm'."
(interactive)
(go-playground-rm))

Expand All @@ -158,20 +168,20 @@ func main() {
Tries to look for a URL at point."
(interactive (list (read-from-minibuffer "Playground URL: " (ffap-url-p (ffap-string-at-point 'url)))))
(with-current-buffer
(let ((url-request-method "GET") url-request-data url-request-extra-headers)
(url-retrieve-synchronously (concat url ".go")))
(let* ((snippet-file-name (go-playground-snippet-file-name)) (buffer (create-file-buffer snippet-file-name)))
(goto-char (point-min))
(re-search-forward "\n\n")
(copy-to-buffer buffer (point) (point-max))
(kill-buffer)
(with-current-buffer buffer
(let ((url-request-method "GET") url-request-data url-request-extra-headers)
(url-retrieve-synchronously (concat url ".go")))
(let* ((snippet-file-name (go-playground-snippet-file-name)) (buffer (create-file-buffer snippet-file-name)))
(goto-char (point-min))
(re-search-forward "\n\n")
(copy-to-buffer buffer (point) (point-max))
(kill-buffer)
(with-current-buffer buffer
(goto-char (point-min))
(go-playground-insert-template-head (concat url " imported"))
(go-mode)
(go-playground-mode)
(set-visited-file-name snippet-file-name t)
(switch-to-buffer buffer)))))
(switch-to-buffer buffer)))))

(defun go-playground-upload ()
"Upload the current buffer to play.golang.org and return the short URL of the playground."
Expand All @@ -184,10 +194,10 @@ Tries to look for a URL at point."
(defun go-playground-snippet-unique-dir (prefix)
"Get unique directory under GOPATH/`go-playground-basedir`."
(let ((dir-name (concat go-playground-basedir "/"
(if (and prefix go-playground-ask-file-name) (concat prefix "-"))
(time-stamp-string "at-%:y-%02m-%02d-%02H%02M%02S"))))
(make-directory dir-name t)
dir-name))
(if (and prefix go-playground-ask-file-name) (concat prefix "-"))
(time-stamp-string "at-%:y-%02m-%02d-%02H%02M%02S"))))
(make-directory dir-name t)
dir-name))

(defun go-playground-inside ()
"Is the current buffer is valid go-playground buffer."
Expand Down

0 comments on commit 34aa3a1

Please sign in to comment.