From 34aa3a11fa5b52f79cc61ef18fb44fa6ea6f431c Mon Sep 17 00:00:00 2001 From: "Alexander I.Grafov" Date: Tue, 25 Jun 2019 21:42:39 +0300 Subject: [PATCH] Add new command `go-playground-cmd` It allows apply custom arguments to the compile mode. --- README.md | 2 ++ go-playground.el | 64 ++++++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d48f211..b6d61f8 100644 --- a/README.md +++ b/README.md @@ -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. | --> @@ -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. | diff --git a/go-playground.el b/go-playground.el index 23ee935..d1bd395 100644 --- a/go-playground.el +++ b/go-playground.el @@ -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) +;; Author: Alexander I.Grafov ;; 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 @@ -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 () @@ -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 @@ -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 -*- @@ -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 @@ -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)) @@ -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." @@ -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."