Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
14 KiB

  1. ;;; inf-ruby.el --- Run a ruby process in a buffer
  2. ;; Copyright (C) 1999-2008 Yukihiro Matsumoto, Nobuyoshi Nakada
  3. ;; Author: Yukihiro Matsumoto, Nobuyoshi Nakada
  4. ;; URL: http://github.com/nonsequitur/inf-ruby
  5. ;; Created: 8 April 1998
  6. ;; Keywords: languages ruby
  7. ;; Version: 2.2.2
  8. ;;; Commentary:
  9. ;;
  10. ;; inf-ruby.el provides a REPL buffer connected to an IRB subprocess.
  11. ;;
  12. ;; If you're installing manually, you'll need to:
  13. ;; * drop the file somewhere on your load path (perhaps ~/.emacs.d)
  14. ;; * Add the following lines to your .emacs file:
  15. ;; (autoload 'inf-ruby "inf-ruby" "Run an inferior Ruby process" t)
  16. ;; (autoload 'inf-ruby-keys "inf-ruby" "" t)
  17. ;; (eval-after-load 'ruby-mode
  18. ;; '(add-hook 'ruby-mode-hook 'inf-ruby-keys))
  19. ;;; TODO:
  20. ;;
  21. ;; inferior-ruby-error-regexp-alist doesn't match this example
  22. ;; SyntaxError: /home/eschulte/united/org/work/arf/arf/lib/cluster.rb:35: syntax error, unexpected '~', expecting kEND
  23. ;; similarity = comparison_cache[m][n] ||= clusters[m] ~ clusters[n]
  24. ;;
  25. ;; M-p skips the first entry in the input ring.
  26. ;;
  27. (require 'comint)
  28. (require 'compile)
  29. (require 'ruby-mode)
  30. (defvar inf-ruby-default-implementation "ruby"
  31. "Which ruby implementation to use if none is specified.")
  32. (defvar inf-ruby-first-prompt-pattern "^irb(.*)[0-9:]+0> *"
  33. "First prompt regex pattern of ruby interpreter.")
  34. (defvar inf-ruby-prompt-pattern "^\\(irb(.*)[0-9:]+[>*\"'] *\\)+"
  35. "Prompt regex pattern of ruby interpreter.")
  36. (defvar inf-ruby-mode-hook nil
  37. "*Hook for customising inf-ruby mode.")
  38. (defvar inf-ruby-mode-map
  39. (let ((map (copy-keymap comint-mode-map)))
  40. (define-key map (kbd "C-c C-l") 'inf-ruby-load-file)
  41. (define-key map (kbd "C-x C-e") 'ruby-send-last-sexp)
  42. (define-key map (kbd "TAB") 'inf-ruby-complete)
  43. map)
  44. "*Mode map for inf-ruby-mode")
  45. (defvar inf-ruby-implementations
  46. '(("ruby" . "irb --inf-ruby-mode -r irb/completion")
  47. ("jruby" . "jruby -S irb -r irb/completion")
  48. ("rubinius" . "rbx -r irb/completion")
  49. ("yarv" . "irb1.9 --inf-ruby-mode -r irb/completion")) ;; TODO: ironruby?
  50. "An alist of ruby implementations to irb executable names.")
  51. ;; TODO: do we need these two defvars?
  52. (defvar ruby-source-modes '(ruby-mode)
  53. "*Used to determine if a buffer contains Ruby source code.
  54. If it's loaded into a buffer that is in one of these major modes, it's
  55. considered a ruby source file by ruby-load-file.
  56. Used by these commands to determine defaults.")
  57. (defvar ruby-prev-l/c-dir/file nil
  58. "Caches the last (directory . file) pair.
  59. Caches the last pair used in the last ruby-load-file command.
  60. Used for determining the default in the
  61. next one.")
  62. (defvar inf-ruby-at-top-level-prompt-p t)
  63. (defconst inf-ruby-error-regexp-alist
  64. '(("SyntaxError: compile error\n^\\([^\(].*\\):\\([1-9][0-9]*\\):" 1 2)
  65. ("^\tfrom \\([^\(].*\\):\\([1-9][0-9]*\\)\\(:in `.*'\\)?$" 1 2)))
  66. ;;;###autoload
  67. (defun inf-ruby-keys ()
  68. "Set local key defs to invoke inf-ruby from ruby-mode."
  69. (define-key ruby-mode-map "\M-\C-x" 'ruby-send-definition)
  70. (define-key ruby-mode-map "\C-x\C-e" 'ruby-send-last-sexp)
  71. (define-key ruby-mode-map "\C-c\C-b" 'ruby-send-block)
  72. (define-key ruby-mode-map "\C-c\M-b" 'ruby-send-block-and-go)
  73. (define-key ruby-mode-map "\C-c\C-x" 'ruby-send-definition)
  74. (define-key ruby-mode-map "\C-c\M-x" 'ruby-send-definition-and-go)
  75. (define-key ruby-mode-map "\C-c\C-r" 'ruby-send-region)
  76. (define-key ruby-mode-map "\C-c\M-r" 'ruby-send-region-and-go)
  77. (define-key ruby-mode-map "\C-c\C-z" 'ruby-switch-to-inf)
  78. (define-key ruby-mode-map "\C-c\C-l" 'ruby-load-file)
  79. (define-key ruby-mode-map "\C-c\C-s" 'inf-ruby))
  80. (defvar inf-ruby-buffer nil "Current irb process buffer.")
  81. (defun inf-ruby-mode ()
  82. "Major mode for interacting with an inferior ruby (irb) process.
  83. The following commands are available:
  84. \\{inf-ruby-mode-map}
  85. A ruby process can be fired up with M-x inf-ruby.
  86. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  87. inf-ruby-mode-hook (in that order).
  88. You can send text to the inferior ruby process from other buffers containing
  89. Ruby source.
  90. ruby-switch-to-inf switches the current buffer to the ruby process buffer.
  91. ruby-send-definition sends the current definition to the ruby process.
  92. ruby-send-region sends the current region to the ruby process.
  93. ruby-send-definition-and-go, ruby-send-region-and-go,
  94. switch to the ruby process buffer after sending their text.
  95. Commands:
  96. Return after the end of the process' output sends the text from the
  97. end of process to point.
  98. Return before the end of the process' output copies the sexp ending at point
  99. to the end of the process' output, and sends it.
  100. Delete converts tabs to spaces as it moves back.
  101. Tab indents for ruby; with arugment, shifts rest
  102. of expression rigidly with the current line.
  103. C-M-q does Tab on each line starting within following expression.
  104. Paragraphs are separated only by blank lines. # start comments.
  105. If you accidentally suspend your process, use \\[comint-continue-subjob]
  106. to continue it."
  107. (interactive)
  108. (comint-mode)
  109. (setq comint-prompt-regexp inf-ruby-prompt-pattern)
  110. (ruby-mode-variables)
  111. (setq major-mode 'inf-ruby-mode)
  112. (setq mode-name "Inf-Ruby")
  113. (setq mode-line-process '(":%s"))
  114. (use-local-map inf-ruby-mode-map)
  115. (setq comint-input-filter (function inf-ruby-input-filter))
  116. (add-to-list 'comint-output-filter-functions 'inf-ruby-output-filter)
  117. (setq comint-get-old-input (function inf-ruby-get-old-input))
  118. (make-local-variable 'compilation-error-regexp-alist)
  119. (setq compilation-error-regexp-alist inf-ruby-error-regexp-alist)
  120. (compilation-shell-minor-mode t)
  121. (run-hooks 'inf-ruby-mode-hook))
  122. (defvar inf-ruby-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  123. "*Input matching this regexp are not saved on the history list.
  124. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  125. (defun inf-ruby-input-filter (str)
  126. "Don't save anything matching inf-ruby-filter-regexp"
  127. (not (string-match inf-ruby-filter-regexp str)))
  128. (defun inf-ruby-output-filter (output)
  129. "Check if the current prompt is a top-level prompt"
  130. (setq inf-ruby-at-top-level-prompt-p
  131. (string-match inf-ruby-prompt-pattern
  132. (car (last (split-string output "\n"))))))
  133. ;; adapted from replace-in-string in XEmacs (subr.el)
  134. (defun inf-ruby-remove-in-string (str regexp)
  135. "Remove all matches in STR for REGEXP and returns the new string."
  136. (let ((rtn-str "") (start 0) match prev-start)
  137. (while (setq match (string-match regexp str start))
  138. (setq prev-start start
  139. start (match-end 0)
  140. rtn-str (concat rtn-str (substring str prev-start match))))
  141. (concat rtn-str (substring str start))))
  142. (defun inf-ruby-get-old-input ()
  143. "Snarf the sexp ending at point"
  144. (save-excursion
  145. (let ((end (point)))
  146. (re-search-backward inf-ruby-first-prompt-pattern)
  147. (inf-ruby-remove-in-string (buffer-substring (point) end)
  148. inf-ruby-prompt-pattern))))
  149. ;;;###autoload
  150. (defun inf-ruby (&optional impl)
  151. "Run an inferior Ruby process in a buffer.
  152. With prefix argument, prompts for which Ruby implementation
  153. \(from the list `inf-ruby-implementations') to use. Runs the
  154. hooks `inf-ruby-mode-hook' \(after the `comint-mode-hook' is
  155. run)."
  156. (interactive (list (if current-prefix-arg
  157. (completing-read "Ruby Implementation: "
  158. (mapc #'car inf-ruby-implementations))
  159. inf-ruby-default-implementation)))
  160. (setq impl (or impl "ruby"))
  161. (let ((command (cdr (assoc impl inf-ruby-implementations))))
  162. (run-ruby command impl)))
  163. ;;;###autoload
  164. (defun run-ruby (&optional command name)
  165. "Run an inferior Ruby process, input and output via buffer *ruby*.
  166. If there is a process already running in `*ruby*', switch to that buffer.
  167. With argument, allows you to edit the command line (default is value
  168. of `ruby-program-name'). Runs the hooks `inferior-ruby-mode-hook'
  169. \(after the `comint-mode-hook' is run).
  170. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  171. (interactive)
  172. (setq command (or command (cdr (assoc inf-ruby-default-implementation
  173. inf-ruby-implementations))))
  174. (setq name (or name "ruby"))
  175. (if (not (comint-check-proc inf-ruby-buffer))
  176. (let ((commandlist (split-string command)))
  177. (set-buffer (apply 'make-comint name (car commandlist)
  178. nil (cdr commandlist)))
  179. (inf-ruby-mode)))
  180. (pop-to-buffer (setq inf-ruby-buffer (format "*%s*" name))))
  181. (defun inf-ruby-proc ()
  182. "Returns the current IRB process. See variable inf-ruby-buffer."
  183. (or (get-buffer-process (if (eq major-mode 'inf-ruby-mode)
  184. (current-buffer)
  185. inf-ruby-buffer))
  186. (error "No current process. See variable inf-ruby-buffer")))
  187. ;; These commands are added to the ruby-mode keymap:
  188. (defconst ruby-send-terminator "--inf-ruby-%x-%d-%d-%d--"
  189. "Template for irb here document terminator.
  190. Must not contain ruby meta characters.")
  191. (defconst inf-ruby-eval-binding "IRB.conf[:MAIN_CONTEXT].workspace.binding")
  192. (defconst ruby-eval-separator "")
  193. (defun ruby-send-region (start end)
  194. "Send the current region to the inferior Ruby process."
  195. (interactive "r")
  196. (let (term (file (or buffer-file-name (buffer-name))) line)
  197. (save-excursion
  198. (save-restriction
  199. (widen)
  200. (goto-char start)
  201. (setq line (+ start (forward-line (- start)) 1))
  202. (goto-char start)
  203. (while (progn
  204. (setq term (apply 'format ruby-send-terminator (random) (current-time)))
  205. (re-search-forward (concat "^" (regexp-quote term) "$") end t)))))
  206. ;; compilation-parse-errors parses from second line.
  207. (save-excursion
  208. (let ((m (process-mark (inf-ruby-proc))))
  209. (set-buffer (marker-buffer m))
  210. (goto-char m)
  211. (insert ruby-eval-separator "\n")
  212. (set-marker m (point))))
  213. (comint-send-string (inf-ruby-proc) (format "eval <<'%s', %s, %S, %d\n"
  214. term inf-ruby-eval-binding
  215. file line))
  216. (comint-send-region (inf-ruby-proc) start end)
  217. (comint-send-string (inf-ruby-proc) (concat "\n" term "\n"))))
  218. (defun ruby-send-definition ()
  219. "Send the current definition to the inferior Ruby process."
  220. (interactive)
  221. (save-excursion
  222. (ruby-end-of-defun)
  223. (let ((end (point)))
  224. (ruby-beginning-of-defun)
  225. (ruby-send-region (point) end))))
  226. (defun ruby-send-last-sexp ()
  227. "Send the previous sexp to the inferior Ruby process."
  228. (interactive)
  229. (ruby-send-region (save-excursion (backward-sexp) (point)) (point)))
  230. (defun ruby-send-block ()
  231. "Send the current block to the inferior Ruby process."
  232. (interactive)
  233. (save-excursion
  234. (ruby-end-of-block)
  235. (end-of-line)
  236. (let ((end (point)))
  237. (ruby-beginning-of-block)
  238. (ruby-send-region (point) end))))
  239. (defun ruby-switch-to-inf (eob-p)
  240. "Switch to the ruby process buffer.
  241. With argument, positions cursor at end of buffer."
  242. (interactive "P")
  243. (if (get-buffer inf-ruby-buffer)
  244. (pop-to-buffer inf-ruby-buffer)
  245. (error "No current process buffer. See variable inf-ruby-buffer."))
  246. (cond (eob-p
  247. (push-mark)
  248. (goto-char (point-max)))))
  249. (defun ruby-send-region-and-go (start end)
  250. "Send the current region to the inferior Ruby process.
  251. Then switch to the process buffer."
  252. (interactive "r")
  253. (ruby-send-region start end)
  254. (ruby-switch-to-inf t))
  255. (defun ruby-send-definition-and-go ()
  256. "Send the current definition to the inferior Ruby.
  257. Then switch to the process buffer."
  258. (interactive)
  259. (ruby-send-definition)
  260. (ruby-switch-to-inf t))
  261. (defun ruby-send-block-and-go ()
  262. "Send the current block to the inferior Ruby.
  263. Then switch to the process buffer."
  264. (interactive)
  265. (ruby-send-block)
  266. (ruby-switch-to-inf t))
  267. (defun ruby-load-file (file-name)
  268. "Load a Ruby file into the inferior Ruby process."
  269. (interactive (comint-get-source "Load Ruby file: " ruby-prev-l/c-dir/file
  270. ruby-source-modes t)) ;; T because LOAD needs an exact name
  271. (comint-check-source file-name) ; Check to see if buffer needs saved.
  272. (setq ruby-prev-l/c-dir/file (cons (file-name-directory file-name)
  273. (file-name-nondirectory file-name)))
  274. (comint-send-string (inf-ruby-proc) (concat "(load \""
  275. file-name
  276. "\"\)\n")))
  277. (defun ruby-escape-single-quoted (str)
  278. (replace-regexp-in-string "'" "\\\\'"
  279. (replace-regexp-in-string "\n" "\\\\n"
  280. (replace-regexp-in-string "\\\\" "\\\\\\\\" str))))
  281. (defun inf-ruby-completions (seed)
  282. "Return a list of completions for the line of ruby code starting with SEED."
  283. (let* ((proc (get-buffer-process inf-ruby-buffer))
  284. (comint-filt (process-filter proc))
  285. (kept "") completions)
  286. (set-process-filter proc (lambda (proc string) (setq kept (concat kept string))))
  287. (process-send-string proc (format "puts IRB::InputCompletor::CompletionProc.call('%s').compact\n"
  288. (ruby-escape-single-quoted seed)))
  289. (while (and (not (string-match inf-ruby-prompt-pattern kept))
  290. (accept-process-output proc 2)))
  291. (setq completions (cdr (butlast (split-string kept "\r?\n") 2)))
  292. (set-process-filter proc comint-filt)
  293. completions))
  294. (defun inf-ruby-completion-at-point ()
  295. (if inf-ruby-at-top-level-prompt-p
  296. (let* ((curr (replace-regexp-in-string "\n$" "" (thing-at-point 'line)))
  297. (completions (inf-ruby-completions curr)))
  298. (if completions
  299. (if (= (length completions) 1)
  300. (car completions)
  301. (completing-read "possible completions: "
  302. completions nil t curr))))
  303. (message "Completion aborted: Not at a top-level prompt")
  304. nil))
  305. (defun inf-ruby-complete (command)
  306. "Complete the ruby code at point. Relies on the irb/completion
  307. Module used by readline when running irb through a terminal"
  308. (interactive (list (inf-ruby-completion-at-point)))
  309. (when command
  310. (kill-whole-line 0)
  311. (insert command)))
  312. (defun inf-ruby-complete-or-tab (&optional command)
  313. "Either complete the ruby code at point or call
  314. `indent-for-tab-command' if no completion is available."
  315. (interactive (list (inf-ruby-completion-at-point)))
  316. (if (not command)
  317. (call-interactively 'indent-for-tab-command)
  318. (inf-ruby-complete command)))
  319. ;;;###autoload
  320. (eval-after-load 'ruby-mode
  321. '(add-hook 'ruby-mode-hook 'inf-ruby-keys))
  322. (provide 'inf-ruby)
  323. ;;; inf-ruby.el ends here