public
Description: Tomtt's version of the minor mode for editing RubyOnRails code in Emacs
Homepage: http://rubyforge.org/projects/emacs-rails/
Clone URL: git://github.com/tomtt/emacs-rails.git
rmm5t (author)
Thu May 15 09:47:48 -0700 2008
commit  dd4a565085b992c6da4de442e7b9f471afc4e7d7
tree    e79f0e78b40df2b287d18257eb9b5a9a5166d2e5
parent  872bb5cc0fa3eabbcbfbbb70ddafb03d68e7133b
emacs-rails / rails-project.el
100644 82 lines (67 sloc) 2.817 kb
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
;;; rails-project.el ---
 
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
 
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
 
;; Keywords: ruby rails languages oop
;; $URL: svn+ssh://rubyforge/var/svn/emacs-rails/trunk/rails.el $
;; $Id: rails.el 149 2007-03-29 15:07:49Z dimaexe $
 
;;; 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 2
;; of the License, 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 this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
;;; Code:
 
(defun rails-project:root ()
  "Return RAILS_ROOT if this file is a part of a Rails application,
else return nil"
  (let ((curdir default-directory)
        (max 10)
        (found nil))
    (while (and (not found) (> max 0))
      (progn
        (if (file-exists-p (concat curdir "config/environment.rb"))
            (progn
              (setq found t))
          (progn
            (setq curdir (concat curdir "../"))
            (setq max (- max 1))))))
    (if found (expand-file-name curdir))))
 
(defmacro* rails-project:with-root ((root) &body body)
  "If you use `rails-project:root' or functions related on it
several times in a block of code, you can optimize your code by
using this macro. Also, blocks of code will be executed only if
rails-root exist.
(rails-project:with-root (root)
(foo root)
(bar (rails-core:file \"some/path\")))
"
 `(let ((,root (rails-project:root)))
    (when ,root
      (flet ((rails-project:root () ,root))
        ,@body))))
 
(defmacro rails-project:in-root (&rest body)
  "Set the default directory to the Rails root directory while
BODY is executed."
  (let ((root (gensym)))
    `(rails-project:with-root
      (,root)
      (let ((default-dir ,root))
        ,@body))))
 
(defmacro* rails-project:in-root-with-cd (&rest body)
  (let ((root (gensym)))
  `(rails-project:with-root (,root)
    (in-directory (,root) ,@body))))
 
(defun rails-project:compile-in-root (command)
  (rails-project:in-root-with-cd
   (compile command)))
 
(defun rails-project:name ()
  "Return the name of current Rails project."
  (replace-regexp-in-string "^.*/\\(.*\\)/$" "\\1"
          (directory-name (rails-project:root))))
 
(provide 'rails-project)