forked from eschulte/rinari
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrinari.el
314 lines (275 loc) · 11.7 KB
/
rinari.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
;;; rinari.el --- Rinari Is Not A Rails IDE
;; Copyright (C) 2008 Phil Hagelberg, Eric Schulte
;; Authors: Phil Hagelberg, Eric Schulte
;; URL: http://rinari.rubyforge.org
;; Version: 2.0
;; Created: 2006-11-10
;; Keywords: ruby, rails
;; EmacsWiki: Rinari
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Rinari Is Not A Ruby IDE.
;; Well, ok it kind of is. Rinari is a set of Emacs Lisp modes that is
;; aimed towards making Emacs into a top-notch Ruby and Rails
;; development environment.
;; Copy the directory containing this file into your Emacs lisp
;; directory, assumed here to be ~/.emacs.d. Add these lines of code
;; to your .emacs file:
;; (add-to-list 'load-path "~/.emacs.d/rinari")
;; (require 'rinari)
;; See TODO file in this directory.
;;; Code:
(require 'ruby-mode)
(require 'inf-ruby)
(require 'ruby-compilation)
(require 'cl)
(require 'toggle)
(require 'find-file-in-project)
(require 'rinari-movement)
(defcustom rinari-browse-url-func
'browse-url
"`browse-url' function used by `rinari-browse-view'.")
(defcustom rinari-tags-file-name
"TAGS"
"Path to your TAGS file inside of your rails project. See `tags-file-name'.")
(defadvice find-file-in-project (around find-file-in-rinari-project activate)
"Wrap `find-file-in-project' to use `rinari-root' as the base of
the project."
(let ((ffip-project-root (rinari-root)))
ad-do-it))
(defadvice ruby-run-w/compilation (around rinari-run-w/compilation activate)
"Set default directory to the root of the rails application
before running ruby processes."
(let ((default-directory (or (rinari-root) default-directory)))
ad-do-it
(rinari-launch)))
(defadvice ruby-rake-w/compilation (around rinari-rake-w/compilation activate)
"Set default directory to the root of the rails application
before running rake processes."
(let ((default-directory (or (rinari-root) default-directory)))
ad-do-it
(rinari-launch)))
(defun rinari-parse-yaml ()
(let ((start (point))
(end (save-excursion (re-search-forward "^$" nil t) (point)))
alist)
(while (and (< (point) end)
(re-search-forward "^ *\\(.*\\): \\(.*\\)$" nil t))
(setf alist (cons (cons (match-string 1) (match-string 2)) alist)))
alist))
(defun rinari-root (&optional dir)
(or dir (setq dir default-directory))
(if (file-exists-p (concat dir "config/environment.rb"))
dir
(unless (equal dir "/")
(rinari-root (expand-file-name (concat dir "../"))))))
;;--------------------------------------------------------------------------------
;; user functions
(defun rinari-rake (&optional task edit-cmd-args)
"Tab completion selection of a rake task to execute with the
output dumped to a compilation buffer allowing jumping between
errors and source code. With optional prefix argument allows
editing of the rake command arguments."
(interactive "P")
(ruby-rake-w/compilation task edit-cmd-args))
(defun rinari-script (&optional script)
"Tab completing selection of a script from the script/
directory of the rails application."
(interactive)
(let* ((root (rinari-root))
(script (or script
(completing-read "Script: " (directory-files (concat root "script") nil "^[^.]"))))
(ruby-compilation-error-regexp-alist ;; for jumping to newly created files
(if (equal script "generate")
'(("^ +\\(exists\\|create\\) +\\([^[:space:]]+\\.rb\\)" 2 3))
ruby-compilation-error-regexp-alist))
(script (concat "script/" script " ")))
(ruby-run-w/compilation (concat root script (read-from-minibuffer script)))))
(defun rinari-test (&optional edit-cmd-args)
"Test the current ruby function. If current function is not a
test, then try to jump to the related test using `toggle-buffer'.
Dump output to a compilation buffer allowing jumping between
errors and source code. With optional prefix argument allows
editing of the test command arguments."
(interactive "P")
(or (string-match "test" (or (ruby-add-log-current-method)
(file-name-nondirectory (buffer-file-name))))
(toggle-buffer))
(let* ((funname (ruby-add-log-current-method))
(fn (and funname
(string-match "#\\(.*\\)" funname)
(match-string 1 funname)))
(path (buffer-file-name))
(default-command (if fn
(concat path " --name /" fn "/")
path))
(command (if edit-cmd-args
(read-string "Run w/Compilation: " default-command)
default-command)))
(if path (ruby-run-w/compilation command)
(message "no test available"))))
(defun rinari-console (&optional edit-cmd-args)
"Run script/console in a compilation buffer, with command
history and links between errors and source code. With optional
prefix argument allows editing of the console command arguments."
(interactive "P")
(let* ((script (concat (rinari-root) "script/console"))
(command (if edit-cmd-args
(read-string "Run Ruby: " (concat script " "))
script)))
(run-ruby command)
(save-excursion (pop-to-buffer "*ruby*")
(set (make-local-variable 'inferior-ruby-first-prompt-pattern) "^>> ")
(set (make-local-variable 'inferior-ruby-prompt-pattern) "^>> ")
(rinari-launch))))
(defun rinari-sql ()
"Browse the application's database. Looks up login information
from your conf/database.sql file."
(interactive)
(flet ((sql-name (env) (format "*%s-sql*" env)))
(let* ((environment (or (getenv "RAILS_ENV") "development"))
(sql-buffer (get-buffer (sql-name environment))))
(if sql-buffer
(pop-to-buffer sql-buffer)
(let* ((database-alist (save-excursion
(with-temp-buffer
(insert-file (concat (rinari-root)
"/config/database.yml"))
(goto-char (point-min))
(re-search-forward (concat "^" environment ":"))
(rinari-parse-yaml))))
(adapter (or (cdr (assoc "adapter" database-alist)) "sqlite"))
(sql-user (or (cdr (assoc "username" database-alist)) "root"))
(sql-password (or (cdr (assoc "password" database-alist)) ""))
(sql-database (or (cdr (assoc "database" database-alist))
(concat (file-name-nondirectory (rinari-root))
"_" environment)))
(server (or (cdr (assoc "host" database-alist)) "localhost"))
(port (cdr (assoc "port" database-alist)))
(sql-server (if port (concat server ":" port) server)))
(if (string-match "sqlite" adapter) (setf adapter "sqlite"))
(eval (list (intern (concat "sql-" adapter))))
(rename-buffer (sql-name environment)) (rinari-launch))))))
(defun rinari-web-server (&optional edit-cmd-args)
"Run script/server. Dump output to a compilation buffer
allowing jumping between errors and source code. With optional
prefix argument allows editing of the server command arguments."
(interactive "P")
(let* ((default-directory (rinari-root))
(script (concat (rinari-root) "script/server"))
(command (if edit-cmd-args
(read-string "Run Ruby: " (concat script " "))
script)))
(ruby-run-w/compilation command)))
(defun rinari-browse-url ()
"Browse the url of the current view, controller, test, or model
with `rinari-browse-url-func' which defaults to `browse-url'."
(interactive)
(unless (equal :view (rinari-whats-my-type))
(rinari-find-view))
(let* ((path (buffer-file-name))
(route (and (string-match "app/views/\\(.+\\)\.r[ebhtml]+" path)
(match-string 1 path)))
(port (or () ;; guess port (or not)
"3000"))
(server (or () ;; guess server (or not)
"localhost"))
(base (concat server ":" port "/" route))
(url (read-from-minibuffer "url: " (concat base "/"))))
(eval (list rinari-browse-url-func url))))
(defun rinari-insert-erb-skeleton (no-equals)
"Insert an erb skeleton at point, with optional prefix argument
don't include an '='."
(interactive "P")
(insert "<%") (unless no-equals (insert "=")) (insert " %>")
(backward-char 3))
(defun rinari-rgrep (&optional arg)
"Search through the rails project for a string or `regexp'.
With optional prefix argument just run `rgrep'."
(interactive "P")
(grep-compute-defaults)
(if arg (call-interactively 'rgrep)
(funcall 'rgrep (read-from-minibuffer "search for: ")
"*.rb *.rhtml *.yml *.erb" (rinari-root))))
;;--------------------------------------------------------------------
;; minor mode and keymaps
(defvar rinari-minor-mode-map
(let ((map (make-sparse-keymap)))
map)
"Key map for Rinari minor mode.")
(defvar rinari-minor-mode-keybindings
'(("o" . 'toggle-buffer) ("s" . 'rinari-script)
("e" . 'rinari-insert-erb-skeleton) ("t" . 'rinari-test)
("r" . 'rinari-rake) ("c" . 'rinari-console)
("w" . 'rinari-web-server) ("g" . 'rinari-rgrep)
("b" . 'rinari-browse-url) ("q" . 'rinari-sql)
("fc" . 'rinari-find-controller) ("ft" . 'rinari-find-test)
("fv" . 'rinari-find-view) ("fm" . 'rinari-find-model)
("fi" . 'rinari-find-migration) ("fe" . 'rinari-find-environment)
("fj" . 'rinari-find-javascript) ("fs" . 'rinari-find-stylesheet))
"alist mapping of keys to functions in `rinari-minor-mode'")
(mapcar (lambda (el)
(eval `(define-key rinari-minor-mode-map
,(format "\C-c;%s" (car el)) ,(cdr el)))
(eval `(define-key rinari-minor-mode-map
,(format "\C-c'%s" (car el)) ,(cdr el))))
rinari-minor-mode-keybindings)
(defun rinari-launch ()
"Run `rinari-minor-mode' if inside of a rails projcect,
otherwise turn `rinari-minor-mode' off if it is on."
(interactive)
(let* ((root (rinari-root)) (r-tags-path (concat root rinari-tags-file-name)))
(if root
(progn
;; customize toggle.el for rinari
(add-to-list
'toggle-mapping-styles
'(rinari . (("app/controllers/\\1.rb#\\2" . "test/functional/\\1_test.rb#test_\\2")
("app/controllers/\\1.rb" . "test/functional/\\1_test.rb")
("app/models/\\1.rb#\\2" . "test/unit/\\1_test.rb#test_\\2")
("app/models/\\1.rb" . "test/unit/\\1_test.rb")
("lib/\\1.rb#\\2" . "test/unit/test_\\1.rb#test_\\2")
("lib/\\1.rb" . "test/unit/test_\\1.rb"))))
(setq toggle-mapping-style 'rinari)
(setq toggle-mappings (toggle-style toggle-mapping-style))
(setq toggle-which-function-command 'ruby-add-log-current-method)
(setq toggle-method-format "def %s")
(if (file-exists-p r-tags-path) (setq tags-file-name r-tags-path))
(unless rinari-minor-mode (rinari-minor-mode t)))
(if rinari-minor-mode (rinari-minor-mode)))))
(defvar rinari-major-modes
'('ruby-mode-hook 'yaml-mode-hook 'mumamo-after-change-major-mode-hook 'css-mode-hook
'javascript-mode-hook 'dired-mode-hook)
"Major Modes from which to launch Rinari.")
(mapcar (lambda (hook)
(eval `(add-hook ,hook
(lambda () (rinari-launch)))))
rinari-major-modes)
(defadvice cd (after rinari-on-cd activate)
"Active/Deactive rinari-minor-node when changing into and out
of raills project directories."
(rinari-launch))
;;;###autoload
(define-minor-mode rinari-minor-mode
"Enable Rinari minor mode providing Emacs support for working
with the Ruby on Rails framework."
nil
" Rinari"
rinari-minor-mode-map)
(provide 'rinari)
;;; rinari.el ends here