viper -- Update and prettify package-info.in provides.
[packages] / xemacs-packages / ruby-modes / inf-ruby.el
1 ;;; -*-Emacs-Lisp-*-
2 ;;;
3 ;;;  $Id: inf-ruby.el 12031 2007-03-11 10:01:15Z knu $
4 ;;;  $Author: knu $
5 ;;;  $Date: 2007-03-11 19:01:15 +0900 (Sun, 11 Mar 2007) $
6 ;;;
7 ;;; Inferior Ruby Mode - ruby process in a buffer.
8 ;;;                      adapted from cmuscheme.el
9 ;;;
10 ;;; Usage:
11 ;;;
12 ;;; (0) check ruby-program-name variable that can run your environment.
13 ;;;
14 ;;; (1) modify .emacs to use ruby-mode 
15 ;;;     for example :
16 ;;;
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))
23 ;;;    
24 ;;; (2) set to load inf-ruby and set inf-ruby key definition in ruby-mode.
25 ;;;
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
31 ;;;          '(lambda ()
32 ;;;             (inf-ruby-keys)
33 ;;;    ))
34 ;;;
35 ;;; HISTORY
36 ;;; senda -  8 Apr 1998: Created.
37 ;;;      $Log$
38 ;;;      Revision 1.7  2004/07/27 08:11:36  matz
39 ;;;      * eval.c (rb_eval): copy on write for argument local variable
40 ;;;        assignment.
41 ;;;
42 ;;;      * eval.c (assign): ditto.
43 ;;;
44 ;;;      * eval.c (rb_call0): update ruby_frame->argv with the default
45 ;;;        value used for the optional arguments.
46 ;;;
47 ;;;      * object.c (Init_Object): "===" calls rb_obj_equal() directly.
48 ;;;        [ruby-list:39937]
49 ;;;
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.
53 ;;;     
54 ;;;      * misc/inf-ruby.el (inferior-ruby-mode): fixed for Emacs.
55 ;;;     
56 ;;;      * misc/inf-ruby.el (ruby-send-region): compilation-parse-errors
57 ;;;        doesn't parse first line, so insert separators before each
58 ;;;        evaluations.
59 ;;;     
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.
63 ;;;     
64 ;;;      * misc/inf-ruby.el (inferior-ruby-mode): compilation-minor-mode.
65 ;;;     
66 ;;;      * misc/inf-ruby.el (ruby-send-region): send as here document to
67 ;;;        adjust source file/line.  [ruby-talk:47113], [ruby-dev:17965]
68 ;;;     
69 ;;;      * misc/inf-ruby.el (ruby-send-terminator): added to make unique
70 ;;;        terminator.
71 ;;;     
72 ;;;      Revision 1.4  2002/01/29 07:16:09  matz
73 ;;;      * file.c (rb_stat_rdev_major): added. [new]
74 ;;;     
75 ;;;      * file.c (rb_stat_rdev_minor): added. [new]
76 ;;;     
77 ;;;      * file.c (rb_stat_inspect): print mode in octal.
78 ;;;     
79 ;;;      Revision 1.3  1999/12/01 09:24:18  matz
80 ;;;      19991201
81 ;;;     
82 ;;;      Revision 1.2  1999/08/13 05:45:18  matz
83 ;;;      1.4.0
84 ;;;     
85 ;;;      Revision 1.1.1.1.2.1  1999/07/15 07:59:59  matz
86 ;;;      990715
87 ;;;     
88 ;;;      Revision 1.1.1.1  1999/01/20 04:59:36  matz
89 ;;;      ruby 1.3 cycle
90 ;;;     
91 ;;;      Revision 1.1.2.1  1998/12/16 07:30:36  matz
92 ;;;      first public release of 1.1d (pre1.2) series
93 ;;;     
94 ;;;      Revision 1.4  1998/05/20 02:45:58  senda
95 ;;;      default program to irb
96 ;;;
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
103 ;;;      defined
104 ;;;
105 ;;;      Revision 1.2  1998/04/09 07:53:42  senda
106 ;;;      remove M-C-x in inferior-ruby-mode
107 ;;;
108 ;;;      Revision 1.1  1998/04/09 07:28:36  senda
109 ;;;      Initial revision
110 ;;;
111 ;;;
112
113 (require 'comint)
114 (require 'compile)
115 (require 'ruby-mode)
116
117 (defgroup ruby-inferior-mode nil
118   "Major mode for interacting with an inferior ruby (irb) process."
119   :group 'ruby
120   :prefix "inferior-ruby-")
121
122 ;;;; for irb
123 (defcustom ruby-program-name "irb --inf-ruby-mode"
124   "*Program invoked by the `run-ruby' command."
125   :type 'string
126   :group 'ruby-inferior-mode)
127
128 (defcustom inferior-ruby-first-prompt-pattern "^irb(.*)[0-9:]+0> *"
129   "*First prompt regex pattern of ruby interpreter."
130   :type 'regexp
131   :group 'ruby-inferior-mode)
132
133 (defcustom inferior-ruby-prompt-pattern "^\\(irb(.*)[0-9:]+[>*\"'] *\\)+"
134   "*Prompt regex pattern of ruby interpreter."
135   :type 'regexp
136   :group 'ruby-inferior-mode)
137
138 ;;
139 ;; mode variables
140 ;;
141 (defcustom inferior-ruby-mode-hook nil
142   "*Hook for customising `inferior-ruby-mode'."
143   :type 'hook
144   :group 'ruby-inferior-mode)
145
146 (defvar inferior-ruby-mode-map nil
147   "*Mode map for `inferior-ruby-mode'.")
148
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)))
152
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)
160 ))
161
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."
165   :type 'regexp
166   :group 'ruby-inferior-mode)
167
168 ;;;###autoload
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)
182 )
183
184 (defvar ruby-buffer nil
185   "Current ruby (actually irb) process buffer.")
186
187 (defun inferior-ruby-mode ()
188   "Major mode for interacting with an inferior ruby (irb) process.
189
190 The following commands are available:
191 \\{inferior-ruby-mode-map}
192
193 A ruby process can be fired up with \\[run-ruby].
194
195 Customisation:
196
197 Entry to this mode runs the hooks on `comint-mode-hook' and
198 `inferior-ruby-mode-hook' (in that order).
199
200 You can send text to the inferior ruby process from other buffers containing
201 Ruby source.
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.
205
206     `ruby-send-definition-and-go', `ruby-send-region-and-go',
207         switch to the ruby process buffer after sending their text.
208
209 For information on running multiple processes in multiple buffers, see
210 documentation for variable `ruby-buffer'.
211
212 Commands:
213
214 Return after the end of the process' output sends the text from the end of
215 process to point.
216
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.
219
220 Delete converts tabs to spaces as it moves back.
221
222 Tab indents for ruby; with argument, shifts rest of expression rigidly with
223 the current line.
224
225 C-M-q does Tab on each line starting within following expression.
226
227 Paragraphs are separated only by blank lines.
228
229 # start comments.
230
231 If you accidentally suspend your process, use \\[comint-continue-subjob]
232 to continue it."
233   (interactive)
234   (comint-mode)
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))
250
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)))
254
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
261             start (match-end 0)
262             rtn-str (concat rtn-str (substring str prev-start match))))
263     (concat rtn-str (substring str start))))
264
265 (defun ruby-get-old-input ()
266   "Snarf the sexp ending at point."
267   (save-excursion
268     (let ((end (point)))
269       (re-search-backward inferior-ruby-first-prompt-pattern)
270       (remove-in-string (buffer-substring (point) end)
271                         inferior-ruby-prompt-pattern)
272       )))
273
274 (defun ruby-args-to-list (string)
275   (let ((where (string-match "[ \t]" string)))
276     (cond ((null where) (list string))
277           ((not (= where 0))
278            (cons (substring string 0 where)
279                  (ruby-args-to-list (substring string (+ 1 where)
280                                                  (length string)))))
281           (t (let ((pos (string-match "[^ \t]" string)))
282                (if (null pos)
283                    nil
284                  (ruby-args-to-list (substring string pos
285                                                  (length string)))))))))
286
287 ;;;###autoload
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.)"
295
296   (interactive (list (if current-prefix-arg
297                          (read-string "Run Ruby: " ruby-program-name)
298                          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)
302                            nil (cdr cmdlist)))
303         (inferior-ruby-mode)))
304   (setq ruby-program-name cmd)
305   (setq ruby-buffer "*ruby*")
306   (pop-to-buffer "*ruby*"))
307
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.")
311
312 (defconst ruby-eval-separator "")
313
314 (defun ruby-send-region (start end)
315   "Send the current region to the inferior Ruby process."
316   (interactive "r")
317   (let (term (file (buffer-file-name)) line)
318     (save-excursion
319       (save-restriction
320         (widen)
321         (goto-char start)
322         (setq line (+ start (forward-line (- start)) 1))
323         (goto-char start)
324         (while (progn
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.
328     (save-excursion
329       (let ((m (process-mark (ruby-proc))))
330         (set-buffer (marker-buffer m))
331         (goto-char 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"))))
337
338 (defun ruby-send-definition ()
339   "Send the current definition to the inferior Ruby process."
340   (interactive)
341   (save-excursion
342     (ruby-end-of-defun)
343     (let ((end (point)))
344       (ruby-beginning-of-defun)
345       (ruby-send-region (point) end))))
346
347 ;(defun ruby-send-last-sexp ()
348 ;  "Send the previous sexp to the inferior Ruby process."
349 ;  (interactive)
350 ;  (ruby-send-region (save-excursion (backward-sexp) (point)) (point)))
351
352 (defun ruby-send-block ()
353   "Send the current block to the inferior Ruby process."
354   (interactive)
355   (save-excursion
356     (ruby-end-of-block)
357     (end-of-line)
358     (let ((end (point)))
359       (ruby-beginning-of-block)
360       (ruby-send-region (point) end))))
361
362 (defun switch-to-ruby (eob-p)
363   "Switch to the ruby process buffer.
364 With argument, positions cursor at end of buffer."
365   (interactive "P")
366   (if (get-buffer ruby-buffer)
367       (pop-to-buffer ruby-buffer)
368       (error "No current process buffer. See variable `ruby-buffer'."))
369   (cond (eob-p
370          (push-mark)
371          (goto-char (point-max)))))
372
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."
376   (interactive "r")
377   (ruby-send-region start end)
378   (switch-to-ruby t))
379
380 (defun ruby-send-definition-and-go ()
381   "Send the current definition to the inferior Ruby. 
382 Then switch to the process buffer."
383   (interactive)
384   (ruby-send-definition)
385   (switch-to-ruby t))
386
387 (defun ruby-send-block-and-go ()
388   "Send the current block to the inferior Ruby. 
389 Then switch to the process buffer."
390   (interactive)
391   (ruby-send-block)
392   (switch-to-ruby t))
393
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.")
399
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.")
404
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 \""
414                                             file-name
415                                             "\"\)\n")))
416
417 (defun ruby-proc ()
418   "Returns the current ruby process. See variable `ruby-buffer'."
419   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-ruby-mode)
420                                       (current-buffer)
421                                     ruby-buffer))))
422     (or proc
423         (error "No current process. See variable `ruby-buffer'."))))
424
425 ;;; Do the user's customisation...
426
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.")
430         
431 (run-hooks 'inf-ruby-load-hook)
432
433 ;; XEmacs additions
434 ;;;###autoload(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
435 ;;;###autoload(add-hook 'ruby-mode-hook '(lambda () (inf-ruby-keys)))
436
437 (provide 'inf-ruby)
438
439 ;;; inf-ruby.el ends here