aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Mastro <john.b.mastro@gmail.com>2013-07-20 12:44:06 -0700
committerJohn Mastro <john.b.mastro@gmail.com>2013-07-20 12:44:06 -0700
commitf5ef4e1756a9855de30f4d8d702b201a7672edd1 (patch)
tree0715c39ac5ff24292b71fca5a7fea8458f2f53f6
parentdbf4e2768a5aca45f36c804ae16d0717a5717991 (diff)
downloadtrident-mode.el-f5ef4e1756a9855de30f4d8d702b201a7672edd1.tar.gz
trident-mode.el-f5ef4e1756a9855de30f4d8d702b201a7672edd1.tar.bz2
trident-mode.el-f5ef4e1756a9855de30f4d8d702b201a7672edd1.zip
Some more commands
Add a couple expansion buffer commands (for copying to the kill ring and saving the expansion) and sps-compile-buffer-to-file which does pretty much as the name says.
-rw-r--r--readme.org9
-rw-r--r--sps-mode.el60
2 files changed, 44 insertions, 25 deletions
diff --git a/readme.org b/readme.org
index f4eca2e..03d7fb8 100644
--- a/readme.org
+++ b/readme.org
@@ -73,8 +73,13 @@ don't send it to the browser for evaluation:
- =sps-expand-buffer=
- =sps-expand-dwim=
-From within an expansion buffer you can press "x" to send the JavaScript to the
-browser.
+From within an expansion buffer you can press =e= to send the JavaScript to the
+browser, =w= to copy it to the kill ring, =s= to save it to a file (you'll be
+prompted for the destination) or =q= to dismiss the buffer. The copy command,
+=w=, acts on the region if it's active or the entire buffer otherwise.
+
+Additionally, you can use =M-x sps-compile-buffer-to-file= to expand the
+current buffer and save the generated code directly to a file.
*** Code evaluation commands
diff --git a/sps-mode.el b/sps-mode.el
index a924956..c6fe472 100644
--- a/sps-mode.el
+++ b/sps-mode.el
@@ -35,8 +35,10 @@ two-key sequences behind a prefix of your choice.")
(defvar sps-expansion-mode-map
(let ((map (make-sparse-keymap)))
+ (define-key map (kbd "e") 'sps-send-expanded-code)
+ (define-key map (kbd "w") 'sps-kill-ring-save-dwim)
+ (define-key map (kbd "s") 'write-file)
(define-key map (kbd "q") 'quit-window)
- (define-key map (kbd "x") 'sps-send-expanded-code)
map)
"Keymap for `sps-expansion-mode' buffers.")
@@ -58,7 +60,8 @@ what you're doing."
;; across it and gives it a try.
)
-;;;; A few utilities
+
+;;;; Code expansion
(defun sps-wrap-in-ps-form (string)
"Return Parenscript STRING wrapped in a PS:PS form."
@@ -68,12 +71,6 @@ what you're doing."
"Return a form appropriate for evaluating STRING via Swank."
`(swank:eval-and-grab-output ,(sps-wrap-in-ps-form string)))
-(defun sps-file-name-base (filename)
- "Return FILENAME's basename without it's extension."
- (file-name-sans-extension (file-name-nondirectory filename)))
-
-;;;; Code expansion
-
(defun sps-expand (string)
"Display the JavaScript generated from Parenscript STRING.
@@ -96,21 +93,27 @@ buffer's major mode is determined by the variable
(pop-to-buffer (current-buffer)))))
(slime-current-package)))
-;; (defun sps-compile-buffer-to-file (&optional append)
-;; (interactive "p")
-;; (when (and (buffer-modified-p)
-;; (y-or-n-p "Save buffer? "))
-;; (save-buffer))
-;; (let* ((this buffer-file-name)
-;; (dir (and this (file-name-directory this)))
-;; (initial (and this (concat (sps-file-name-base this) ".js")))
-;; (destination (read-file-name "Destination: " dir nil nil initial nil))
-;; (string (buffer-substring-no-properties (point-min) (point-max))))
-;; (slime-eval-async (sps-swank-eval-expr string)
-;; #'(lambda (result)
-;; (let ((code (read (cadr result))))
-;; (write-region code nil destination append)))
-;; (slime-current-package))))
+(defun sps-compile-buffer-to-file ()
+ "Compile the current buffer and write the result.
+Prompts for the destination. If a file already exists at the
+destination it's overwritten."
+ (interactive)
+ (when (and (buffer-modified-p)
+ (y-or-n-p "Save buffer? "))
+ (save-buffer))
+ (let* ((this buffer-file-name)
+ (dir (and this (file-name-directory this)))
+ (initial (and this (concat (file-name-base this) ".js")))
+ (destination (read-file-name "Destination: " dir nil nil initial nil))
+ (string (buffer-substring-no-properties (point-min) (point-max))))
+ (slime-eval-async (sps-swank-eval-expr string)
+ #'(lambda (result)
+ (let ((code (read (cadr result))))
+ (with-temp-buffer
+ (erase-buffer)
+ (insert code)
+ (write-region 1 (point-max) destination))))
+ (slime-current-package))))
(defun sps-expand-sexp ()
"Display the expansion of the form at point."
@@ -194,6 +197,8 @@ If the region is active this is equivalent to invoking
(sps-eval-region (region-beginning) (region-end))
(sps-eval-defun)))
+;;;; Expansion buffer commands
+
(defun sps-send-expanded-code ()
"Send the expanded code to the browser.
For use from `sps-expansion-mode' buffers."
@@ -202,6 +207,15 @@ For use from `sps-expansion-mode' buffers."
(buffer-substring-no-properties (point-min) (point-max))
#'skewer-post-minibuffer))
+(defun sps-kill-ring-save-dwim ()
+ "Save the current buffer's content to the kill ring.
+If the region is active, save it; otherwise save the entire
+buffer."
+ (interactive)
+ (if (region-active-p)
+ (kill-ring-save (region-beginning) (region-end))
+ (kill-ring-save (point-min) (point-max))))
+
;;;; Keybindings
(defun sps-prefix-keys (prefix keys)