3 ;;; $Id: inf-ruby.el 12031 2007-03-11 10:01:15Z knu $
5 ;;; $Date: 2007-03-11 19:01:15 +0900 (Sun, 11 Mar 2007) $
7 ;;; Inferior Ruby Mode - ruby process in a buffer.
8 ;;; adapted from cmuscheme.el
12 ;;; (0) check ruby-program-name variable that can run your environment.
14 ;;; (1) modify .emacs to use ruby-mode
17 ;;; (autoload 'ruby-mode "ruby-mode"
18 ;;; "Mode for editing ruby source files" t)
19 ;;; (setq auto-mode-alist
20 ;;; (append '(("\\.rb$" . ruby-mode)) auto-mode-alist))
21 ;;; (setq interpreter-mode-alist (append '(("ruby" . ruby-mode))
22 ;;; interpreter-mode-alist))
24 ;;; (2) set to load inf-ruby and set inf-ruby key definition in ruby-mode.
26 ;;; (autoload 'run-ruby "inf-ruby"
27 ;;; "Run an inferior Ruby process")
28 ;;; (autoload 'inf-ruby-keys "inf-ruby"
29 ;;; "Set local key defs for inf-ruby in ruby-mode")
30 ;;; (add-hook 'ruby-mode-hook
36 ;;; senda - 8 Apr 1998: Created.
38 ;;; Revision 1.7 2004/07/27 08:11:36 matz
39 ;;; * eval.c (rb_eval): copy on write for argument local variable
42 ;;; * eval.c (assign): ditto.
44 ;;; * eval.c (rb_call0): update ruby_frame->argv with the default
45 ;;; value used for the optional arguments.
47 ;;; * object.c (Init_Object): "===" calls rb_obj_equal() directly.
50 ;;; Revision 1.6 2002/09/07 14:35:46 nobu
51 ;;; * misc/inf-ruby.el (inferior-ruby-error-regexp-alist): regexp
52 ;;; alist for error message from ruby.
54 ;;; * misc/inf-ruby.el (inferior-ruby-mode): fixed for Emacs.
56 ;;; * misc/inf-ruby.el (ruby-send-region): compilation-parse-errors
57 ;;; doesn't parse first line, so insert separators before each
60 ;;; Revision 1.5 2002/08/19 10:05:47 nobu
61 ;;; * misc/inf-ruby.el (inf-ruby-keys): ruby-send-definition
62 ;;; conflicted with ruby-insert-end.
64 ;;; * misc/inf-ruby.el (inferior-ruby-mode): compilation-minor-mode.
66 ;;; * misc/inf-ruby.el (ruby-send-region): send as here document to
67 ;;; adjust source file/line. [ruby-talk:47113], [ruby-dev:17965]
69 ;;; * misc/inf-ruby.el (ruby-send-terminator): added to make unique
72 ;;; Revision 1.4 2002/01/29 07:16:09 matz
73 ;;; * file.c (rb_stat_rdev_major): added. [new]
75 ;;; * file.c (rb_stat_rdev_minor): added. [new]
77 ;;; * file.c (rb_stat_inspect): print mode in octal.
79 ;;; Revision 1.3 1999/12/01 09:24:18 matz
82 ;;; Revision 1.2 1999/08/13 05:45:18 matz
85 ;;; Revision 1.1.1.1.2.1 1999/07/15 07:59:59 matz
88 ;;; Revision 1.1.1.1 1999/01/20 04:59:36 matz
91 ;;; Revision 1.1.2.1 1998/12/16 07:30:36 matz
92 ;;; first public release of 1.1d (pre1.2) series
94 ;;; Revision 1.4 1998/05/20 02:45:58 senda
95 ;;; default program to irb
97 ;;; Revision 1.3 1998/04/10 04:11:30 senda
98 ;;; modification by Matsumoto san (1.1b9_09)
99 ;;; remove-in-string defined
100 ;;; global variable :
101 ;;; inferior-ruby-first-prompt-pattern
102 ;;; inferior-ruby-prompt-pattern
105 ;;; Revision 1.2 1998/04/09 07:53:42 senda
106 ;;; remove M-C-x in inferior-ruby-mode
108 ;;; Revision 1.1 1998/04/09 07:28:36 senda
117 (defgroup ruby-inferior-mode nil
118 "Major mode for interacting with an inferior ruby (irb) process."
120 :prefix "inferior-ruby-")
123 (defcustom ruby-program-name "irb --inf-ruby-mode"
124 "*Program invoked by the `run-ruby' command."
126 :group 'ruby-inferior-mode)
128 (defcustom inferior-ruby-first-prompt-pattern "^irb(.*)[0-9:]+0> *"
129 "*First prompt regex pattern of ruby interpreter."
131 :group 'ruby-inferior-mode)
133 (defcustom inferior-ruby-prompt-pattern "^\\(irb(.*)[0-9:]+[>*\"'] *\\)+"
134 "*Prompt regex pattern of ruby interpreter."
136 :group 'ruby-inferior-mode)
141 (defcustom inferior-ruby-mode-hook nil
142 "*Hook for customising `inferior-ruby-mode'."
144 :group 'ruby-inferior-mode)
146 (defvar inferior-ruby-mode-map nil
147 "*Mode map for `inferior-ruby-mode'.")
149 (defconst inferior-ruby-error-regexp-alist
150 '(("SyntaxError: compile error\n^\\([^\(].*\\):\\([1-9][0-9]*\\):" 1 2)
151 ("^\tfrom \\([^\(].*\\):\\([1-9][0-9]*\\)\\(:in `.*'\\)?$" 1 2)))
153 (cond ((not inferior-ruby-mode-map)
154 (setq inferior-ruby-mode-map
155 (copy-keymap comint-mode-map))
156 ; (define-key inferior-ruby-mode-map "\M-\C-x" ;gnu convention
157 ; 'ruby-send-definition)
158 ; (define-key inferior-ruby-mode-map "\C-x\C-e" 'ruby-send-last-sexp)
159 (define-key inferior-ruby-mode-map "\C-c\C-l" 'ruby-load-file)
162 (defcustom inferior-ruby-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
163 "*Input matching this regexp are not saved on the history list.
164 Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters."
166 :group 'ruby-inferior-mode)
169 (defun inf-ruby-keys ()
170 "Set local key defs for inf-ruby in `ruby-mode'."
171 (define-key ruby-mode-map "\M-\C-x" 'ruby-send-definition)
172 ; (define-key ruby-mode-map "\C-x\C-e" 'ruby-send-last-sexp)
173 (define-key ruby-mode-map "\C-c\C-b" 'ruby-send-block)
174 (define-key ruby-mode-map "\C-c\M-b" 'ruby-send-block-and-go)
175 (define-key ruby-mode-map "\C-c\C-x" 'ruby-send-definition)
176 (define-key ruby-mode-map "\C-c\M-x" 'ruby-send-definition-and-go)
177 (define-key ruby-mode-map "\C-c\C-r" 'ruby-send-region)
178 (define-key ruby-mode-map "\C-c\M-r" 'ruby-send-region-and-go)
179 (define-key ruby-mode-map "\C-c\C-z" 'switch-to-ruby)
180 (define-key ruby-mode-map "\C-c\C-l" 'ruby-load-file)
181 (define-key ruby-mode-map "\C-c\C-s" 'run-ruby)
184 (defvar ruby-buffer nil
185 "Current ruby (actually irb) process buffer.")
187 (defun inferior-ruby-mode ()
188 "Major mode for interacting with an inferior ruby (irb) process.
190 The following commands are available:
191 \\{inferior-ruby-mode-map}
193 A ruby process can be fired up with \\[run-ruby].
197 Entry to this mode runs the hooks on `comint-mode-hook' and
198 `inferior-ruby-mode-hook' (in that order).
200 You can send text to the inferior ruby process from other buffers containing
202 `switch-to-ruby' switches the current buffer to the ruby process buffer.
203 `ruby-send-definition' sends the current definition to the ruby process.
204 `ruby-send-region' sends the current region to the ruby process.
206 `ruby-send-definition-and-go', `ruby-send-region-and-go',
207 switch to the ruby process buffer after sending their text.
209 For information on running multiple processes in multiple buffers, see
210 documentation for variable `ruby-buffer'.
214 Return after the end of the process' output sends the text from the end of
217 Return before the end of the process' output copies the sexp ending at
218 point to the end of the process' output, and sends it.
220 Delete converts tabs to spaces as it moves back.
222 Tab indents for ruby; with argument, shifts rest of expression rigidly with
225 C-M-q does Tab on each line starting within following expression.
227 Paragraphs are separated only by blank lines.
231 If you accidentally suspend your process, use \\[comint-continue-subjob]
235 ;; Customise in inferior-ruby-mode-hook
236 ;(setq comint-prompt-regexp "^[^>\n]*>+ *")
237 (setq comint-prompt-regexp inferior-ruby-prompt-pattern)
238 ;;(scheme-mode-variables)
239 (ruby-mode-variables)
240 (setq major-mode 'inferior-ruby-mode)
241 (setq mode-name "Inferior Ruby")
242 (setq mode-line-process '(":%s"))
243 (use-local-map inferior-ruby-mode-map)
244 (setq comint-input-filter (function ruby-input-filter))
245 (setq comint-get-old-input (function ruby-get-old-input))
246 (compilation-shell-minor-mode t)
247 (make-local-variable 'compilation-error-regexp-alist)
248 (setq compilation-error-regexp-alist inferior-ruby-error-regexp-alist)
249 (run-hooks 'inferior-ruby-mode-hook))
251 (defun ruby-input-filter (str)
252 "Don't save anything matching `inferior-ruby-filter-regexp'."
253 (not (string-match inferior-ruby-filter-regexp str)))
255 ;; adapted from replace-in-string in XEmacs (subr.el)
256 (defun remove-in-string (str regexp)
257 "Remove all matches in STR for REGEXP and returns the new string."
258 (let ((rtn-str "") (start 0) match prev-start)
259 (while (setq match (string-match regexp str start))
260 (setq prev-start start
262 rtn-str (concat rtn-str (substring str prev-start match))))
263 (concat rtn-str (substring str start))))
265 (defun ruby-get-old-input ()
266 "Snarf the sexp ending at point."
269 (re-search-backward inferior-ruby-first-prompt-pattern)
270 (remove-in-string (buffer-substring (point) end)
271 inferior-ruby-prompt-pattern)
274 (defun ruby-args-to-list (string)
275 (let ((where (string-match "[ \t]" string)))
276 (cond ((null where) (list string))
278 (cons (substring string 0 where)
279 (ruby-args-to-list (substring string (+ 1 where)
281 (t (let ((pos (string-match "[^ \t]" string)))
284 (ruby-args-to-list (substring string pos
285 (length string)))))))))
288 (defun run-ruby (cmd)
289 "Run an inferior Ruby process, input and output via buffer *ruby*.
290 If there is a process already running in `*ruby*', switch to that buffer.
291 With argument, allows you to edit the command line (default is value
292 of `ruby-program-name'). Runs the hooks `inferior-ruby-mode-hook'
293 \(after the `comint-mode-hook' is run).
294 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
296 (interactive (list (if current-prefix-arg
297 (read-string "Run Ruby: " ruby-program-name)
299 (if (not (comint-check-proc "*ruby*"))
300 (let ((cmdlist (ruby-args-to-list cmd)))
301 (set-buffer (apply 'make-comint "ruby" (car cmdlist)
303 (inferior-ruby-mode)))
304 (setq ruby-program-name cmd)
305 (setq ruby-buffer "*ruby*")
306 (pop-to-buffer "*ruby*"))
308 (defconst ruby-send-terminator "--inf-ruby-%x-%d-%d-%d--"
309 "Template for irb here document terminator.
310 Must not contain ruby meta characters.")
312 (defconst ruby-eval-separator "")
314 (defun ruby-send-region (start end)
315 "Send the current region to the inferior Ruby process."
317 (let (term (file (buffer-file-name)) line)
322 (setq line (+ start (forward-line (- start)) 1))
325 (setq term (apply 'format ruby-send-terminator (random) (current-time)))
326 (re-search-forward (concat "^" (regexp-quote term) "$") end t)))))
327 ;; compilation-parse-errors parses from second line.
329 (let ((m (process-mark (ruby-proc))))
330 (set-buffer (marker-buffer m))
332 (insert ruby-eval-separator "\n")
333 (set-marker m (point))))
334 (comint-send-string (ruby-proc) (format "eval <<'%s', nil, %S, %d\n" term file line))
335 (comint-send-region (ruby-proc) start end)
336 (comint-send-string (ruby-proc) (concat "\n" term "\n"))))
338 (defun ruby-send-definition ()
339 "Send the current definition to the inferior Ruby process."
344 (ruby-beginning-of-defun)
345 (ruby-send-region (point) end))))
347 ;(defun ruby-send-last-sexp ()
348 ; "Send the previous sexp to the inferior Ruby process."
350 ; (ruby-send-region (save-excursion (backward-sexp) (point)) (point)))
352 (defun ruby-send-block ()
353 "Send the current block to the inferior Ruby process."
359 (ruby-beginning-of-block)
360 (ruby-send-region (point) end))))
362 (defun switch-to-ruby (eob-p)
363 "Switch to the ruby process buffer.
364 With argument, positions cursor at end of buffer."
366 (if (get-buffer ruby-buffer)
367 (pop-to-buffer ruby-buffer)
368 (error "No current process buffer. See variable `ruby-buffer'."))
371 (goto-char (point-max)))))
373 (defun ruby-send-region-and-go (start end)
374 "Send the current region to the inferior Ruby process.
375 Then switch to the process buffer."
377 (ruby-send-region start end)
380 (defun ruby-send-definition-and-go ()
381 "Send the current definition to the inferior Ruby.
382 Then switch to the process buffer."
384 (ruby-send-definition)
387 (defun ruby-send-block-and-go ()
388 "Send the current block to the inferior Ruby.
389 Then switch to the process buffer."
394 (defvar ruby-source-modes '(ruby-mode)
395 "*Used to determine if a buffer contains Ruby source code.
396 If it's loaded into a buffer that is in one of these major modes, it's
397 considered a ruby source file by `ruby-load-file'.
398 Used by these commands to determine defaults.")
400 (defvar ruby-prev-l/c-dir/file nil
401 "Caches the last (directory . file) pair.
402 Caches the last pair used in the last `ruby-load-file' command.
403 Used for determining the default in the next one.")
405 (defun ruby-load-file (file-name)
406 "Load a Ruby file into the inferior Ruby process."
407 (interactive (comint-get-source "Load Ruby file: " ruby-prev-l/c-dir/file
408 ruby-source-modes t)) ; T because LOAD
409 ; needs an exact name
410 (comint-check-source file-name) ; Check to see if buffer needs saved.
411 (setq ruby-prev-l/c-dir/file (cons (file-name-directory file-name)
412 (file-name-nondirectory file-name)))
413 (comint-send-string (ruby-proc) (concat "(load \""
418 "Returns the current ruby process. See variable `ruby-buffer'."
419 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-ruby-mode)
423 (error "No current process. See variable `ruby-buffer'."))))
425 ;;; Do the user's customisation...
427 (defvar inf-ruby-load-hook nil
428 "This hook is run when inf-ruby is loaded in.
429 This is a good place to put keybindings.")
431 (run-hooks 'inf-ruby-load-hook)
434 ;;;###autoload(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
435 ;;;###autoload(add-hook 'ruby-mode-hook '(lambda () (inf-ruby-keys)))
439 ;;; inf-ruby.el ends here