home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ruby164.zip / rbemx164.zip / ruby / share / misc / ruby-1.6.4 / inf-ruby.el next >
Lisp/Scheme  |  2001-06-18  |  11KB  |  326 lines

  1. ;;; -*-Emacs-Lisp-*-
  2. ;;;
  3. ;;;  $Id: inf-ruby.el,v 1.3 1999/12/01 09:24:18 matz Exp $
  4. ;;;  $Author: matz $
  5. ;;;  $Date: 1999/12/01 09:24:18 $
  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")
  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 road 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: inf-ruby.el,v $
  38. ;;;     Revision 1.3  1999/12/01 09:24:18  matz
  39. ;;;     19991201
  40. ;;;    
  41. ;;;     Revision 1.2  1999/08/13 05:45:18  matz
  42. ;;;     1.4.0
  43. ;;;    
  44. ;;;     Revision 1.1.1.1.2.1  1999/07/15 07:59:59  matz
  45. ;;;     990715
  46. ;;;    
  47. ;;;     Revision 1.1.1.1  1999/01/20 04:59:36  matz
  48. ;;;     ruby 1.3 cycle
  49. ;;;    
  50. ;;;     Revision 1.1.2.1  1998/12/16 07:30:36  matz
  51. ;;;     first public release of 1.1d (pre1.2) series
  52. ;;;    
  53. ;;;     Revision 1.4  1998/05/20 02:45:58  senda
  54. ;;;     default program to irb
  55. ;;;
  56. ;;;     Revision 1.3  1998/04/10 04:11:30  senda
  57. ;;;     modification by Matsumoto san (1.1b9_09)
  58. ;;;     remove-in-string defined
  59. ;;;     global variable :
  60. ;;;          inferior-ruby-first-prompt-pattern
  61. ;;;           inferior-ruby-prompt-pattern
  62. ;;;     defined
  63. ;;;
  64. ;;;     Revision 1.2  1998/04/09 07:53:42  senda
  65. ;;;     remove M-C-x in inferior-ruby-mode
  66. ;;;
  67. ;;;     Revision 1.1  1998/04/09 07:28:36  senda
  68. ;;;     Initial revision
  69. ;;;
  70. ;;;
  71.  
  72. (require 'comint)
  73. (require 'ruby-mode)
  74.  
  75. ;;
  76. ;; you may change these variables
  77. ;;
  78. ;(defvar ruby-program-name "rbc --noreadline"
  79. ;  "*Program invoked by the run-ruby command")
  80. ;
  81. ;(defvar inferior-ruby-first-prompt-pattern "^rbc0> *"
  82. ;  "first prompt regex pattern of ruby interpreter.")
  83. ;
  84. ;(defvar inferior-ruby-prompt-pattern "^\\(rbc.[>*\"'] *\\)+"
  85. ;  "prompt regex pattern of ruby interpreter.")
  86.  
  87. ;;;; for irb
  88. (defvar ruby-program-name "irb --inf-ruby-mode"
  89.   "*Program invoked by the run-ruby command")
  90.  
  91. (defvar inferior-ruby-first-prompt-pattern "^irb(.*)[0-9:]+0> *"
  92.   "first prompt regex pattern of ruby interpreter.")
  93.  
  94. (defvar inferior-ruby-prompt-pattern "^\\(irb(.*)[0-9:]+[>*\"'] *\\)+"
  95.   "prompt regex pattern of ruby interpreter.")
  96.  
  97. ;;
  98. ;; mode variables
  99. ;;
  100. (defvar inferior-ruby-mode-hook nil
  101.   "*Hook for customising inferior-ruby mode.")
  102. (defvar inferior-ruby-mode-map nil
  103.   "*Mode map for inferior-ruby-mode")
  104.  
  105. (cond ((not inferior-ruby-mode-map)
  106.        (setq inferior-ruby-mode-map
  107.          (copy-keymap comint-mode-map))
  108. ;       (define-key inferior-ruby-mode-map "\M-\C-x" ;gnu convention
  109. ;               'ruby-send-definition)
  110. ;       (define-key inferior-ruby-mode-map "\C-x\C-e" 'ruby-send-last-sexp)
  111.        (define-key inferior-ruby-mode-map "\C-c\C-l" 'ruby-load-file)
  112. ))
  113.  
  114. (defun inf-ruby-keys ()
  115.   "Set local key defs for inf-ruby in ruby-mode"
  116.   (define-key ruby-mode-map "\M-\C-x" 'ruby-send-definition)
  117. ;  (define-key ruby-mode-map "\C-x\C-e" 'ruby-send-last-sexp)
  118.   (define-key ruby-mode-map "\C-c\C-e" 'ruby-send-definition)
  119.   (define-key ruby-mode-map "\C-c\M-e" 'ruby-send-definition-and-go)
  120.   (define-key ruby-mode-map "\C-c\C-r" 'ruby-send-region)
  121.   (define-key ruby-mode-map "\C-c\M-r" 'ruby-send-region-and-go)
  122.   (define-key ruby-mode-map "\C-c\C-z" 'switch-to-ruby)
  123.   (define-key ruby-mode-map "\C-c\C-l" 'ruby-load-file)
  124.   (define-key ruby-mode-map "\C-c\C-s" 'run-ruby)
  125. )
  126.  
  127. (defvar ruby-buffer nil "current ruby (actually irb) process buffer.")
  128.  
  129. (defun inferior-ruby-mode ()
  130.   "Major mode for interacting with an inferior ruby (irb) process.
  131.  
  132. The following commands are available:
  133. \\{inferior-ruby-mode-map}
  134.  
  135. A ruby process can be fired up with M-x run-ruby.
  136.  
  137. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  138. inferior-ruby-mode-hook (in that order).
  139.  
  140. You can send text to the inferior ruby process from other buffers containing
  141. Ruby source.
  142.     switch-to-ruby switches the current buffer to the ruby process buffer.
  143.     ruby-send-definition sends the current definition to the ruby process.
  144.     ruby-send-region sends the current region to the ruby process.
  145.  
  146.     ruby-send-definition-and-go, ruby-send-region-and-go,
  147.         switch to the ruby process buffer after sending their text.
  148. For information on running multiple processes in multiple buffers, see
  149. documentation for variable ruby-buffer.
  150.  
  151. Commands:
  152. Return after the end of the process' output sends the text from the 
  153.     end of process to point.
  154. Return before the end of the process' output copies the sexp ending at point
  155.     to the end of the process' output, and sends it.
  156. Delete converts tabs to spaces as it moves back.
  157. Tab indents for ruby; with argument, shifts rest
  158.     of expression rigidly with the current line.
  159. C-M-q does Tab on each line starting within following expression.
  160. Paragraphs are separated only by blank lines.  # start comments.
  161. If you accidentally suspend your process, use \\[comint-continue-subjob]
  162. to continue it."
  163.   (interactive)
  164.   (comint-mode)
  165.   ;; Customise in inferior-ruby-mode-hook
  166.   ;(setq comint-prompt-regexp "^[^>\n]*>+ *")
  167.   (setq comint-prompt-regexp inferior-ruby-prompt-pattern)
  168.   ;;(scheme-mode-variables)
  169.   (ruby-mode-variables)
  170.   (setq major-mode 'inferior-ruby-mode)
  171.   (setq mode-name "Inferior Ruby")
  172.   (setq mode-line-process '(":%s"))
  173.   (use-local-map inferior-ruby-mode-map)
  174.   (setq comint-input-filter (function ruby-input-filter))
  175.   (setq comint-get-old-input (function ruby-get-old-input))
  176.   (run-hooks 'inferior-ruby-mode-hook))
  177.  
  178. (defvar inferior-ruby-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  179.   "*Input matching this regexp are not saved on the history list.
  180. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  181.  
  182. (defun ruby-input-filter (str)
  183.   "Don't save anything matching inferior-ruby-filter-regexp"
  184.   (not (string-match inferior-ruby-filter-regexp str)))
  185.  
  186. ;; adapted from replace-in-string in XEmacs (subr.el)
  187. (defun remove-in-string (str regexp)
  188.   "Remove all matches in STR for REGEXP and returns the new string."
  189.   (let ((rtn-str "") (start 0) match prev-start)
  190.     (while (setq match (string-match regexp str start))
  191.       (setq prev-start start
  192.         start (match-end 0)
  193.         rtn-str (concat rtn-str (substring str prev-start match))))
  194.     (concat rtn-str (substring str start))))
  195.  
  196. (defun ruby-get-old-input ()
  197.   "Snarf the sexp ending at point"
  198.   (save-excursion
  199.     (let ((end (point)))
  200.       (re-search-backward inferior-ruby-first-prompt-pattern)
  201.       (remove-in-string (buffer-substring (point) end)
  202.             inferior-ruby-prompt-pattern)
  203.       )))
  204.  
  205. (defun ruby-args-to-list (string)
  206.   (let ((where (string-match "[ \t]" string)))
  207.     (cond ((null where) (list string))
  208.       ((not (= where 0))
  209.        (cons (substring string 0 where)
  210.          (ruby-args-to-list (substring string (+ 1 where)
  211.                          (length string)))))
  212.       (t (let ((pos (string-match "[^ \t]" string)))
  213.            (if (null pos)
  214.            nil
  215.          (ruby-args-to-list (substring string pos
  216.                          (length string)))))))))
  217.  
  218. (defun run-ruby (cmd)
  219.   "Run an inferior Ruby process, input and output via buffer *ruby*.
  220. If there is a process already running in `*ruby*', switch to that buffer.
  221. With argument, allows you to edit the command line (default is value
  222. of `ruby-program-name').  Runs the hooks `inferior-ruby-mode-hook'
  223. \(after the `comint-mode-hook' is run).
  224. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  225.  
  226.   (interactive (list (if current-prefix-arg
  227.              (read-string "Run Ruby: " ruby-program-name)
  228.              ruby-program-name)))
  229.   (if (not (comint-check-proc "*ruby*"))
  230.       (let ((cmdlist (ruby-args-to-list cmd)))
  231.     (set-buffer (apply 'make-comint "ruby" (car cmdlist)
  232.                nil (cdr cmdlist)))
  233.     (inferior-ruby-mode)))
  234.   (setq ruby-program-name cmd)
  235.   (setq ruby-buffer "*ruby*")
  236.   (pop-to-buffer "*ruby*"))
  237.  
  238. (defun ruby-send-region (start end)
  239.   "Send the current region to the inferior Ruby process."
  240.   (interactive "r")
  241.   (comint-send-region (ruby-proc) start end)
  242.   (comint-send-string (ruby-proc) "\n"))
  243.  
  244. (defun ruby-send-definition ()
  245.   "Send the current definition to the inferior Ruby process."
  246.   (interactive)
  247.   (save-excursion
  248.     (ruby-end-of-defun)
  249.     (let ((end (point)))
  250.       (ruby-beginning-of-defun)
  251.       (ruby-send-region (point) end))))
  252.  
  253. ;(defun ruby-send-last-sexp ()
  254. ;  "Send the previous sexp to the inferior Ruby process."
  255. ;  (interactive)
  256. ;  (ruby-send-region (save-excursion (backward-sexp) (point)) (point)))
  257.  
  258. (defun switch-to-ruby (eob-p)
  259.   "Switch to the ruby process buffer.
  260. With argument, positions cursor at end of buffer."
  261.   (interactive "P")
  262.   (if (get-buffer ruby-buffer)
  263.       (pop-to-buffer ruby-buffer)
  264.       (error "No current process buffer. See variable ruby-buffer."))
  265.   (cond (eob-p
  266.      (push-mark)
  267.      (goto-char (point-max)))))
  268.  
  269. (defun ruby-send-region-and-go (start end)
  270.   "Send the current region to the inferior Ruby process.
  271. Then switch to the process buffer."
  272.   (interactive "r")
  273.   (ruby-send-region start end)
  274.   (switch-to-ruby t))
  275.  
  276. (defun ruby-send-definition-and-go ()
  277.   "Send the current definition to the inferior Ruby. 
  278. Then switch to the process buffer."
  279.   (interactive)
  280.   (ruby-send-definition)
  281.   (switch-to-ruby t))
  282.  
  283. (defvar ruby-source-modes '(ruby-mode)
  284.   "*Used to determine if a buffer contains Ruby source code.
  285. If it's loaded into a buffer that is in one of these major modes, it's
  286. considered a ruby source file by ruby-load-file.
  287. Used by these commands to determine defaults.")
  288.  
  289. (defvar ruby-prev-l/c-dir/file nil
  290.   "Caches the last (directory . file) pair.
  291. Caches the last pair used in the last ruby-load-file command.
  292. Used for determining the default in the 
  293. next one.")
  294.  
  295. (defun ruby-load-file (file-name)
  296.   "Load a Ruby file into the inferior Ruby process."
  297.   (interactive (comint-get-source "Load Ruby file: " ruby-prev-l/c-dir/file
  298.                   ruby-source-modes t)) ; T because LOAD 
  299.                                                           ; needs an exact name
  300.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  301.   (setq ruby-prev-l/c-dir/file (cons (file-name-directory    file-name)
  302.                        (file-name-nondirectory file-name)))
  303.   (comint-send-string (ruby-proc) (concat "(load \""
  304.                         file-name
  305.                         "\"\)\n")))
  306.  
  307. (defun ruby-proc ()
  308.   "Returns the current ruby process. See variable ruby-buffer."
  309.   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-ruby-mode)
  310.                       (current-buffer)
  311.                     ruby-buffer))))
  312.     (or proc
  313.     (error "No current process. See variable ruby-buffer"))))
  314.  
  315. ;;; Do the user's customisation...
  316.  
  317. (defvar inf-ruby-load-hook nil
  318.   "This hook is run when inf-ruby is loaded in.
  319. This is a good place to put keybindings.")
  320.     
  321. (run-hooks 'inf-ruby-load-hook)
  322.  
  323. (provide 'inf-ruby)
  324.  
  325. ;;; inf-ruby.el ends here
  326.