Create a gist now

Instantly share code, notes, and snippets.

Tim's refactoring of ruby-send-region, which overcomes issue where new local variables would not persist past an evaluation.
;;; inf-ruby.el --- Run a ruby process in a buffer
;; Copyright (C) 1999-2008 Yukihiro Matsumoto, Nobuyoshi Nakada
;; Authors: Yukihiro Matsumoto, Nobuyoshi Nakada, Tim Harper
;; URL:
;; Created: 8 April 1998
;; Keywords: languages ruby
;; Version: 2.1
;; Package-Requires: ((ruby-mode "1.1"))
;;; Commentary:
;; inf-ruby.el provides a REPL buffer connected to an IRB subprocess.
;; If you're installing manually, you'll need to:
;; * drop the file somewhere on your load path (perhaps ~/.emacs.d)
;; * Add the following lines to your .emacs file:
;; (autoload 'inf-ruby "inf-ruby" "Run an inferior Ruby process" t)
;; (autoload 'inf-ruby-keys "inf-ruby" "" t)
;; (eval-after-load 'ruby-mode
;; '(add-hook 'ruby-mode-hook 'inf-ruby-keys))
;;; TODO:
;; inferior-ruby-error-regexp-alist doesn't match this example
;; SyntaxError: /home/eschulte/united/org/work/arf/arf/lib/cluster.rb:35: syntax error, unexpected '~', expecting kEND
;; similarity = comparison_cache[m][n] ||= clusters[m] ~ clusters[n]
;; M-p skips the first entry in the input ring.
(require 'comint)
(require 'compile)
(require 'ruby-mode)
(defvar inf-ruby-default-implementation "ruby"
"Which ruby implementation to use if none is specified.")
(defvar inf-ruby-first-prompt-pattern "^irb(.*)[0-9:]+0> *"
"First prompt regex pattern of ruby interpreter.")
(defvar inf-ruby-prompt-pattern "^\\(irb(.*)[0-9:]+[>*\"'] *\\)+"
"Prompt regex pattern of ruby interpreter.")
(defvar inf-ruby-mode-hook nil
"*Hook for customising inf-ruby mode.")
(defvar inf-ruby-mode-map
(let ((map (copy-keymap comint-mode-map)))
(define-key map (kbd "C-c C-l") 'inf-ruby-load-file)
(define-key map (kbd "C-x C-e") 'ruby-send-last-sexp)
(define-key map (kbd "TAB") 'inf-ruby-complete-or-tab)
"*Mode map for inf-ruby-mode")
(defvar inf-ruby-implementations
'(("ruby" . "irb --inf-ruby-mode -r irb/completion")
("jruby" . "jruby -S irb -r irb/completion")
("rubinius" . "rbx -r irb/completion")
("yarv" . "irb1.9 --inf-ruby-mode -r irb/completion")) ;; TODO: ironruby?
"An alist of ruby implementations to irb executable names.")
;; TODO: do we need these two defvars?
(defvar ruby-source-modes '(ruby-mode)
"*Used to determine if a buffer contains Ruby source code.
If it's loaded into a buffer that is in one of these major modes, it's
considered a ruby source file by ruby-load-file.
Used by these commands to determine defaults.")
(defvar ruby-prev-l/c-dir/file nil
"Caches the last (directory . file) pair.
Caches the last pair used in the last ruby-load-file command.
Used for determining the default in the
next one.")
(defvar inf-ruby-last-prompt ""
"Stores the last prompt captured by ruby-send-and-read-response")
(defconst inf-ruby-error-regexp-alist
'(("SyntaxError: compile error\n^\\([^\(].*\\):\\([1-9][0-9]*\\):" 1 2)
("^\tfrom \\([^\(].*\\):\\([1-9][0-9]*\\)\\(:in `.*'\\)?$" 1 2)))
(defun inf-ruby-keys ()
"Set local key defs to invoke inf-ruby from ruby-mode."
(define-key ruby-mode-map "\M-\C-x" 'ruby-send-definition)
(define-key ruby-mode-map "\C-x\C-e" 'ruby-send-last-sexp)
(define-key ruby-mode-map "\C-c\C-b" 'ruby-send-block)
(define-key ruby-mode-map "\C-c\M-b" 'ruby-send-block-and-go)
(define-key ruby-mode-map "\C-c\C-x" 'ruby-send-definition)
(define-key ruby-mode-map "\C-c\M-x" 'ruby-send-definition-and-go)
(define-key ruby-mode-map "\C-c\C-r" 'ruby-send-region)
(define-key ruby-mode-map "\C-c\M-r" 'ruby-send-region-and-go)
(define-key ruby-mode-map "\C-c\C-z" 'ruby-switch-to-inf)
(define-key ruby-mode-map "\C-c\C-l" 'ruby-load-file)
(define-key ruby-mode-map "\C-c\C-s" 'inf-ruby))
(defvar inf-ruby-buffer nil "Current irb process buffer.")
(defun inf-ruby-mode ()
"Major mode for interacting with an inferior ruby (irb) process.
The following commands are available:
A ruby process can be fired up with M-x inf-ruby.
Customisation: Entry to this mode runs the hooks on comint-mode-hook and
inf-ruby-mode-hook (in that order).
You can send text to the inferior ruby process from other buffers containing
Ruby source.
ruby-switch-to-inf switches the current buffer to the ruby process buffer.
ruby-send-definition sends the current definition to the ruby process.
ruby-send-region sends the current region to the ruby process.
ruby-send-definition-and-go, ruby-send-region-and-go,
switch to the ruby process buffer after sending their text.
Return after the end of the process' output sends the text from the
end of process to point.
Return before the end of the process' output copies the sexp ending at point
to the end of the process' output, and sends it.
Delete converts tabs to spaces as it moves back.
Tab indents for ruby; with arugment, shifts rest
of expression rigidly with the current line.
C-M-q does Tab on each line starting within following expression.
Paragraphs are separated only by blank lines. # start comments.
If you accidentally suspend your process, use \\[comint-continue-subjob]
to continue it."
(setq comint-prompt-regexp inf-ruby-prompt-pattern)
(setq major-mode 'inf-ruby-mode)
(setq mode-name "Inf-Ruby")
(setq mode-line-process '(":%s"))
(use-local-map inf-ruby-mode-map)
(setq comint-input-filter (function inf-ruby-input-filter))
(setq comint-get-old-input (function inf-ruby-get-old-input))
(make-local-variable 'compilation-error-regexp-alist)
(setq compilation-error-regexp-alist inf-ruby-error-regexp-alist)
(compilation-shell-minor-mode t)
(run-hooks 'inf-ruby-mode-hook))
(defvar inf-ruby-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
"*Input matching this regexp are not saved on the history list.
Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
(defun inf-ruby-input-filter (str)
"Don't save anything matching inf-ruby-filter-regexp"
(not (string-match inf-ruby-filter-regexp str)))
;; adapted from replace-in-string in XEmacs (subr.el)
(defun inf-ruby-remove-in-string (str regexp)
"Remove all matches in STR for REGEXP and returns the new string."
(let ((rtn-str "") (start 0) match prev-start)
(while (setq match (string-match regexp str start))
(setq prev-start start
start (match-end 0)
rtn-str (concat rtn-str (substring str prev-start match))))
(concat rtn-str (substring str start))))
(defun inf-ruby-get-old-input ()
"Snarf the sexp ending at point"
(let ((end (point)))
(re-search-backward inf-ruby-first-prompt-pattern)
(inf-ruby-remove-in-string (buffer-substring (point) end)
(defun inf-ruby (&optional impl)
"Run an inferior Ruby process in a buffer.
With prefix argument, prompts for which Ruby implementation
\(from the list `inf-ruby-implementations') to use. Runs the
hooks `inf-ruby-mode-hook' \(after the `comint-mode-hook' is
(interactive (list (if current-prefix-arg
(completing-read "Ruby Implementation: "
(mapc #'car inf-ruby-implementations))
(setq impl (or impl "ruby"))
(let ((command (cdr (assoc impl inf-ruby-implementations))))
(run-ruby command impl)))
(defun run-ruby (&optional command name)
"Run an inferior Ruby process, input and output via buffer *ruby*.
If there is a process already running in `*ruby*', switch to that buffer.
With argument, allows you to edit the command line (default is value
of `ruby-program-name'). Runs the hooks `inferior-ruby-mode-hook'
\(after the `comint-mode-hook' is run).
\(Type \\[describe-mode] in the process buffer for a list of commands.)"
(setq command (or command (cdr (assoc inf-ruby-default-implementation
(setq name (or name "ruby"))
(if (not (comint-check-proc inf-ruby-buffer))
(let ((commandlist (split-string command)))
(set-buffer (apply 'make-comint name (car commandlist)
nil (cdr commandlist)))
(pop-to-buffer (setq inf-ruby-buffer (format "*%s*" name))))
(defun inf-ruby-proc ()
"Returns the current IRB process. See variable inf-ruby-buffer."
(or (get-buffer-process (if (eq major-mode 'inf-ruby-mode)
(error "No current process. See variable inf-ruby-buffer")))
;; These commands are added to the ruby-mode keymap:
(defconst ruby-send-terminator "--inf-ruby-%x-%d-%d-%d--"
"Template for irb here document terminator.
Must not contain ruby meta characters.")
(defconst ruby-eval-separator "")
(defun ruby-send-process (text)
"Sends text directly to the ruby process for processing"
(comint-send-string (inf-ruby-proc) text))
(defun ruby-join-strings (pieces separator)
(mapconcat 'identity pieces separator)) ;; Why is there no interleave abstraction? mapconcat is the WRONG level of abstraction.
(defun ruby-strip-string (str)
"strips a string from trailing and preceeding whitespace"
(let* ((lines (split-string str "\n"))
(lines (append (list (replace-regexp-in-string "^[\n ]*" "" (first lines)))
(rest lines)))
(lines (append (butlast lines 1)
(list (replace-regexp-in-string "[\n ]*$" "" (car (last lines)))))))
(cond ((= 0 (length (first lines)))
(ruby-strip-string (ruby-join-strings (cdr lines) "\n")))
((= 0 (length (car (last lines))))
(ruby-strip-string (ruby-join-strings (butlast lines 1) "\n")))
(t (ruby-join-strings lines "\n")))))
(defun ruby-send-and-read-response (str)
"Sends a string to the ruby inferior process, and reads it's response"
(let* ((read-buffer "")
(proc (get-buffer-process inf-ruby-buffer))
(comint-filt (process-filter proc)))
(set-process-filter proc (lambda (proc string) (setf read-buffer (concat read-buffer string))))
(ruby-send-process str)
(while (not (string-match inf-ruby-prompt-pattern read-buffer)) (accept-process-output proc))
(set-process-filter proc comint-filt)
(let* ((read-buffer (ruby-strip-string read-buffer))
(lines (split-string read-buffer "\n")))
(setq inf-ruby-last-prompt (car (last lines)))
(ruby-join-strings (butlast lines 1) "\n"))))
(defun ruby-comint-output (what)
"Sends output to the comint buffer as if it were outputted by the ruby process"
(let* ((proc (get-buffer-process inf-ruby-buffer))
(comint-filt (process-filter proc)))
(funcall comint-filt (inf-ruby-proc) what)))
;; TODO - extract capture comint output code to a macro
(defun ruby-send-region (start end)
"Send the current region to the inferior Ruby process."
(interactive "r")
(let (term (file (or (buffer-file-name) (buffer-name))) line)
(goto-char start)
(setq line (+ start (forward-line (- start)) 1))
(goto-char start)
(while (progn
(setq term (apply 'format ruby-send-terminator (random) (current-time)))
(re-search-forward (concat "^" (regexp-quote term) "$") end t)))))
;; compilation-parse-errors parses from second line.
(let* ((m (process-mark (inf-ruby-proc)))
(code (buffer-substring-no-properties start end)))
(set-buffer (marker-buffer m))
(goto-char m)
(insert ruby-eval-separator "\n")
(set-marker m (point))
(let* ((result (ruby-send-and-read-response (format "eval <<-'%s', irb_context.workspace.binding, %S, %d\n%s\n%s\n" term file line code term))))
(ruby-comint-output (concat (ruby-strip-string result) "\n" inf-ruby-last-prompt " "))
(message "%.80s" (first (split-string result "\n")))))))
(defun ruby-send-definition ()
"Send the current definition to the inferior Ruby process."
(let ((end (point)))
(ruby-send-region (point) end))))
(defun ruby-send-last-sexp ()
"Send the previous sexp to the inferior Ruby process."
(ruby-send-region (save-excursion (backward-sexp) (point)) (point)))
(defun ruby-send-block ()
"Send the current block to the inferior Ruby process."
(let ((end (point)))
(ruby-send-region (point) end))))
(defun ruby-switch-to-inf (eob-p)
"Switch to the ruby process buffer.
With argument, positions cursor at end of buffer."
(interactive "P")
(if (get-buffer inf-ruby-buffer)
(pop-to-buffer inf-ruby-buffer)
(error "No current process buffer. See variable inf-ruby-buffer."))
(cond (eob-p
(goto-char (point-max)))))
(defun ruby-send-region-and-go (start end)
"Send the current region to the inferior Ruby process.
Then switch to the process buffer."
(interactive "r")
(ruby-send-region start end)
(ruby-switch-to-inf t))
(defun ruby-send-definition-and-go ()
"Send the current definition to the inferior Ruby.
Then switch to the process buffer."
(ruby-switch-to-inf t))
(defun ruby-send-block-and-go ()
"Send the current block to the inferior Ruby.
Then switch to the process buffer."
(ruby-switch-to-inf t))
(defun ruby-load-file (file-name)
"Load a Ruby file into the inferior Ruby process."
(interactive (comint-get-source "Load Ruby file: " ruby-prev-l/c-dir/file
ruby-source-modes t)) ;; T because LOAD needs an exact name
(comint-check-source file-name) ; Check to see if buffer needs saved.
(setq ruby-prev-l/c-dir/file (cons (file-name-directory file-name)
(file-name-nondirectory file-name)))
(comint-send-string (inf-ruby-proc) (concat "(load \""
(defun inf-ruby-completions (seed)
"Return a list of completions for the line of ruby code starting with SEED."
(let* ((seed (ruby-strip-string (replace-regexp-in-string inf-ruby-prompt-pattern "" seed)))
(output (ruby-send-and-read-response (format "puts'%s').compact\n" seed)))
(if (string-match "^[[:alpha:]]+?Error: " output) (error output))
(setf completions (butlast (split-string output "[\r\n]") 1))
(defun inf-ruby-complete-or-tab (&optional command seed)
"Either complete the ruby code at point or call
`indent-for-tab-command' if no completion is available. Relies
on the irb/completion Module used by readline when running irb
through a terminal."
(interactive (let* ((curr (save-excursion
(let ((p1 (point))
(p2 (progn (comint-bol) (point))))
(replace-regexp-in-string "^.* " "" (buffer-substring-no-properties p1 p2) ""))))
(completions (inf-ruby-completions curr)))
(list (case (length completions)
(0 nil)
(1 (car completions))
(t (completing-read "possible completions: " completions nil 'confirm-only curr)))
(if (not command)
(message "%s" "No completions")
(insert (replace-regexp-in-string (format "^%s" (regexp-quote seed)) "" command))))
(eval-after-load 'ruby-mode
'(add-hook 'ruby-mode-hook 'inf-ruby-keys))
(provide 'inf-ruby)
;;; inf-ruby.el ends here
rdp commented Jul 5, 2010

that is odd that they don't persist.
Does core know about this? Is it a bug?


It's a change in behavior with ruby 1.9. Local variables are bound at compile time, so eval("a=1"); eval("a") no longer works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment