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-view-minor-mode.el
100644 115 lines (103 sloc) 4.992 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
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
;;; rails-view-minor-mode.el --- minor mode for RubyOnRails views
 
;; 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$
;; $Id$
 
;;; 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-view-minor-mode:create-partial-from-selection ()
  "Create a partial from current buffer selection."
  (interactive)
  (if mark-active
      (save-excursion
        (let ((name (read-string "Partial name (without _ and extension)? "))
              (content (buffer-substring-no-properties (region-beginning) (region-end)))
              (modified (buffer-modified-p)))
          (unless (string-not-empty name)
            (progn
              (message "Empty partial name") (return)))
          (kill-region (region-beginning) (region-end))
          (insert (concat "<%= render :partial => \"" name "\" %>"))
          (mmm-parse-region (line-beginning-position) (line-end-position))
          (insert "\n")
          (split-window-vertically)
          (other-window 1)
          (find-file (concat "_" name ".rhtml"))
          (goto-char (point-min))
          (erase-buffer)
          (insert content)
          (save-buffer)
          (fit-window-to-buffer)
          (other-window -1)
          (unless modified (save-buffer))
          (message "type `C-x +` to balance windows")))))
 
(defun rails-view-minor-mode:create-helper-from-block (&optional helper-name)
  "Create a helper function from current ERb block (<% .. %>)."
  (interactive)
  (let ((current-pos (point))
        (file buffer-file-name)
        begin-pos
        end-pos)
    (save-excursion
      (setq begin-pos (search-backward "<%" nil t))
      (setq end-pos (search-forward "%>" nil t)))
    (if (and begin-pos
             end-pos
             (> current-pos begin-pos)
             (< current-pos end-pos))
        (let* ((helper-file (concat (rails-project:root) (rails-core:helper-file (rails-core:current-controller))))
               (content (replace-regexp-in-string "\\(<%=?\\|-?%>\\)" ""
                                                  (buffer-substring-no-properties begin-pos end-pos)))
               (helper-defination (if helper-name helper-name
                                     (read-string "Type helper function defination (without `def` keyword): "))))
           (if (file-exists-p helper-file)
               (let ((modified (buffer-modified-p))
                     (helper-func-def (concat "def " helper-defination)))
                 (kill-region begin-pos end-pos)
                 (insert (concat "<%= " helper-defination " -%>" ))
                 (mmm-parse-region (line-beginning-position) (line-end-position))
                 (insert "\n")
                 (split-window-vertically)
                 (other-window 1)
                 (find-file helper-file)
                 (goto-char (point-min))
                 (search-forward-regexp "module +[a-zA-Z0-9:]+")
                 (end-of-line)
                 (newline)
                 (ruby-indent-command)
                 (save-excursion
                   (insert (concat helper-func-def "\n" content "\nend\n")))
                 (ruby-indent-exp)
                 (fit-window-to-buffer)
                 (save-buffer)
                 (other-window -1)
                 (unless modified (save-buffer))
                 (message "Type `C-x +` to balance windows"))
             (message "helper not found")))
       (message "block not found"))))
 
(define-minor-mode rails-view-minor-mode
  "Minor mode for RubyOnRails views."
  :lighter " View"
  :keymap (rails-controller-layout:keymap :view)
  (setq rails-primary-switch-func 'rails-controller-layout:toggle-action-view)
  (setq rails-secondary-switch-func 'rails-controller-layout:menu)
  (if (boundp 'mmm-mode-map)
      (progn
        (define-key mmm-mode-map (rails-key "p") 'rails-view-minor-mode:create-partial-from-selection)
        (define-key mmm-mode-map (rails-key "b") 'rails-view-minor-mode:create-helper-from-block))
    (progn
      (local-set-key (rails-key "p") 'rails-view-minor-mode:create-partial-from-selection)
      (local-set-key (rails-key "b") 'rails-view-minor-mode:create-helper-from-block))))
 
(provide 'rails-view-minor-mode)