branch: elpa/swift-mode commit 286c4b844d5c92fe31519276f61f7ee242f8ceb6 Merge: 98b627a d136303 Author: Chris Barrett <chris.d.barr...@me.com> Commit: Chris Barrett <chris.d.barr...@me.com>
Merge pull request #21 from ap4y/feature_repl Feature REPL --- swift-mode.el | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/swift-mode.el b/swift-mode.el index a226a9b..eff7214 100644 --- a/swift-mode.el +++ b/swift-mode.el @@ -28,6 +28,7 @@ ;;; Code: (require 'rx) +(require 'comint) (eval-and-compile ;; Added in Emacs 24.3 @@ -318,6 +319,71 @@ Returns the column number as an integer." ;;; Mode definition +;;; REPL +(defcustom swift-mode-executable + "xcrun swift" + "Path to the Swift CLI.") + +(defgroup swift-repl nil + "Run a Swift REPL process in a buffer." + :group 'swift-repl) + +(defcustom swift-repl-mode-hook nil + "*Hook for customizing swift-repl mode." + :type 'hook + :group 'swift-repl) + +(defun swift-mode-run-repl (cmd &optional dont-switch-p) + "Run a REPL process, input and output via buffer `*swift-repl*'. +If there is a process already running in `*swift-repl*', switch to that buffer. +With argument CMD allows you to edit the command line (default is value +of `swift-mode-executable'). +With DONT-SWITCH-P cursor will stay in current buffer. +Runs the hook `swift-repl-mode-hook' \(after the `comint-mode-hook' +is run). +\(Type \\[describe-mode] in the process buffer for a list of commands.)" + + (interactive (list (if current-prefix-arg + (read-string "Run swift-repl: " swift-mode-executable) + swift-mode-executable))) + (if (not (comint-check-proc "*swift-repl*")) + (save-excursion (let ((cmdlist (split-string cmd))) + (set-buffer (apply 'make-comint "swift-repl" (car cmdlist) + nil (cdr cmdlist))) + (swift-repl-mode)))) + (setq swift-mode-executable cmd) + (setq swift-mode-repl-buffer "*swift-repl*") + (if (not dont-switch-p) + (pop-to-buffer "*swift-repl*"))) + +(defvar swift-mode-repl-buffer) + +(defun swift-mode-send-region (start end) + "Send the current region to the inferior Javascript process. +START and END define region within current buffer" + (interactive "r") + (swift-mode-run-repl swift-mode-executable t) + (comint-send-region swift-mode-repl-buffer start end) + (comint-send-string swift-mode-repl-buffer "\n")) + +(defun swift-mode-send-buffer () + "Send the buffer to the Swift REPL process." + (interactive) + (swift-mode-send-region (point-min) (point-max))) + +(define-derived-mode swift-repl-mode comint-mode "Swift REPL" + "Major mode for interacting with Swift REPL. + +A REPL can be fired up with M-x swift-mode-run-repl. + +Customization: Entry to this mode runs the hooks on comint-mode-hook and +swift-repl-mode-hook (in that order). + +You can send text to the REPL process from other buffers containing source. + swift-mode-send-region sends the current region to the REPL process, + swift-mode-send-buffer sends the current buffer to the REPL process. +") + ;; HACK: This syntax table is lifted directly from `rust-mode'. There may be ;; corner cases in the Swift syntax that are not accounted for. (defvar swift-mode-syntax-table