Permalink
Browse files

Added a minor mode for interacting with Gosu programs running in *she…

…ll* buffers. This mode just adds some convenient key bindings for things like running programs or running tests.
  • Loading branch information...
1 parent 7ba9560 commit 94c958ee461edcedfadf68bdb71d748751e34104 @TikhonJelvis committed Jul 18, 2011
Showing with 116 additions and 0 deletions.
  1. +116 −0 gosu-program-mode.el
View
@@ -0,0 +1,116 @@
+;; A mode for interacting with gosu programs.
+;;
+;; This is not a mode for interacting with _inferior gosu processes_; rather, it
+;; is a minor mode that enhances the shell with some additional commands I find
+;; convenient. It allows you to easily restart the running program, clear the
+;; shell or run tests; all this is abstracted into a system involving "profiles"
+;; which just let you to control the commands used for both running the program
+;; and running tests.
+;;
+;; The supplied example is for a gosu server that uses Aardvark to both run the
+;; actual program ("vark server") and to run the tests ("vark test"); however,
+;; there is nothing binding this to Aardvark, or indeed to Gosu at all. However,
+;; since I personally have only used this for Gosu so far,
+(defvar gosu-program-mode-map (make-keymap))
+
+(define-key gosu-program-mode-map (kbd "C-c C-j") 'gosu-run-program)
+(define-key gosu-program-mode-map (kbd "C-c C-l") 'gosu-reset-and-clear)
+(define-key gosu-program-mode-map (kbd "C-c C-t") 'gosu-run-tests)
+(define-key gosu-program-mode-map (kbd "C-c C-;") 'gosu-program-profile)
+
+(defvar gosu-program-profiles '())
+
+(defun gosu-add-profile (name profile)
+ (unless (assoc name profile)
+ (setq gosu-program-profiles (cons (cons name profile) gosu-program-profiles))))
+
+;; An example profile for a Ronin server:
+(gosu-add-profile "server" '((test-command . "vark test")
+ (run-command . "vark server")))
+
+(defun gosu-program-load-profile (profile)
+ (if profile
+ (progn (setq gosu-program-command(cdr (assoc 'run-command profile)))
+ (setq gosu-test-command (cdr (assoc 'test-command profile))))
+ (message "Error! Specified profile does not exist.")))
+(defun gosu-program-profile ()
+ (interactive)
+ (let ((profile-name (read-from-minibuffer "Profile: ")))
+ (message "Switching to profile %s." profile-name)
+ (sleep-for 0 750)
+ (gosu-program-load-profile (assoc profile-name gosu-program-profiles))))
+
+(defun gosu-interrupt-and-clear ()
+ (interactive)
+ (comint-interrupt-subjob)
+ (sleep-for 0 100)
+ (delete-region (point-min) (point-max)))
+
+(defun gosu-reset-and-clear ()
+ (interactive)
+ (gosu-interrupt-and-clear)
+ (sleep-for 0 100)
+ (comint-send-input)
+ (save-excursion
+ (beginning-of-buffer)
+ (delete-blank-lines)
+ (delete-blank-lines)))
+
+(defun gosu-run-cmd (cmd msg)
+ (interactive)
+ (gosu-interrupt-and-clear)
+ (comint-simple-send gosu-program-mode-process
+ (concat "echo '" msg " Command: " cmd "'"))
+ (sleep-for 0 50)
+ (save-excursion
+ (backward-char)
+ (beginning-of-line)
+ (kill-line))
+ (end-of-buffer)
+ (message "Sending %s to %s." cmd gosu-program-mode-process)
+ (comint-simple-send gosu-program-mode-process cmd))
+
+(defun gosu-run-tests ()
+ (interactive)
+ (gosu-run-cmd gosu-test-command "Running tests..."))
+
+(defun gosu-run-program ()
+ (interactive)
+ (gosu-run-cmd gosu-program-command "Running..."))
+
+(defun gosu-set-test-command ()
+ (interactive)
+ (setq gosu-test-command (read-from-minibuffer "New gosu test command: ")))
+
+(defun gosu-set-program ()
+ (interactive)
+ (setq gosu-program-command (read-from-minibuffer "New gosu program: ")))
+
+;; Sets up the buffer-specific variables to a default value (Ronin commands, in this case).
+(defun gosu-program-mode-start ()
+ (set (make-local-variable 'gosu-test-command) "vark test")
+ (set (make-local-variable 'gosu-program-command) "vark server")
+ (set (make-local-variable 'gosu-program-mode-process)
+ (get-buffer-process (current-buffer))))
+
+(define-minor-mode gosu-program-mode "Simpler interaction with gosu programs."
+ :init-value nil
+ :lighter " gsp"
+ :keymap gosu-program-mode-map
+ 'gosu-program-mode-start)
+
+(defun switch-to-program (name location profile)
+ (pop-to-buffer (concat "*" name "*"))
+ (unless (eq major-mode 'shell-mode)
+ (shell (current-buffer))
+ (sleep-for 0 100)
+ (delete-region (point-min) (point-max))
+ (comint-simple-send (get-buffer-process (current-buffer))
+ (concat "cd " location)))
+ (gosu-program-mode 1)
+ (gosu-program-load-profile (assoc profile gosu-program-profiles)))
+
+(defvar example-server-location "~/server")
+(defun tehamon-server ()
+ (interactive)
+ (switch-to-program "server" tehamon-server-location "server"))

0 comments on commit 94c958e

Please sign in to comment.