1
;;; rails-view-minor-mode.el --- minor mode for RubyOnRails views
2
3
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
4
5
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>
6
7
;; Keywords: ruby rails languages oop
8
;; $URL$
9
;; $Id$
10
11
;;; License
12
13
;; This program is free software; you can redistribute it and/or
14
;; modify it under the terms of the GNU General Public License
15
;; as published by the Free Software Foundation; either version 2
16
;; of the License, or (at your option) any later version.
17
18
;; This program is distributed in the hope that it will be useful,
19
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
;; GNU General Public License for more details.
22
23
;; You should have received a copy of the GNU General Public License
24
;; along with this program; if not, write to the Free Software
25
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
27
;;; Code:
28
29
(defun rails-view-minor-mode:create-partial-from-selection ()
30
  "Create a partial from current buffer selection."
31
  (interactive)
32
  (if mark-active
33
      (save-excursion
34
        (let ((name (read-string "Partial name (without _ and extension)? "))
35
              (content (buffer-substring-no-properties (region-beginning) (region-end)))
36
              (modified (buffer-modified-p)))
37
          (unless (string-not-empty name)
38
            (progn
39
              (message "Empty partial name") (return)))
40
          (kill-region (region-beginning) (region-end))
41
          (insert (concat "<%= render :partial => \"" name "\" %>"))
42
          (insert "\n")
43
          (split-window-vertically)
44
          (other-window 1)
45
          (find-file (concat "_" name ".html.erb"))
46
          (goto-char (point-min))
47
          (erase-buffer)
48
          (insert content)
49
          (save-buffer)
50
          (fit-window-to-buffer)
51
          (other-window -1)
52
          (unless modified (save-buffer))
53
          (message "type `C-x +` to balance windows")))))
54
55
(defun rails-view-minor-mode:create-helper-from-block (&optional helper-name)
56
  "Create a helper function from current ERb block (<% .. %>)."
57
  (interactive)
58
  (let ((current-pos (point))
59
        (file buffer-file-name)
60
        begin-pos
61
        end-pos)
62
    (save-excursion
63
      (setq begin-pos (search-backward "<%" nil t))
64
      (setq end-pos (search-forward "%>" nil t)))
65
    (if (and begin-pos
66
             end-pos
67
             (> current-pos begin-pos)
68
             (< current-pos end-pos))
69
        (let* ((helper-file (concat (rails-project:root) (rails-core:helper-file (rails-core:current-controller))))
70
               (content (replace-regexp-in-string "\\(<%=?\\|-?%>\\)" ""
71
                                                  (buffer-substring-no-properties begin-pos end-pos)))
72
               (helper-defination (if helper-name helper-name
73
                                     (read-string "Type helper function defination (without `def` keyword): "))))
74
           (if (file-exists-p helper-file)
75
               (let ((modified (buffer-modified-p))
76
                     (helper-func-def (concat "def " helper-defination)))
77
                 (kill-region begin-pos end-pos)
78
                 (insert (concat "<%= " helper-defination " -%>" ))
79
                 (funcall indent-line-function)
80
                 (insert "\n")
81
                 (split-window-vertically)
82
                 (other-window 1)
83
                 (find-file helper-file)
84
                 (goto-char (point-min))
85
                 (search-forward-regexp "module +[a-zA-Z0-9:]+")
86
                 (end-of-line)
87
                 (newline)
88
                 (ruby-indent-command)
89
                 (save-excursion
90
                   (insert (concat helper-func-def "\n" content "\nend\n")))
91
                 (ruby-indent-exp)
92
                 (fit-window-to-buffer)
93
                 (save-buffer)
94
                 (other-window -1)
95
                 (unless modified (save-buffer))
96
                 (message "Type `C-x +` to balance windows"))
97
             (message "helper not found")))
98
       (message "block not found"))))
99
100
(define-minor-mode rails-view-minor-mode
101
  "Minor mode for RubyOnRails views."
102
  :lighter " View"
103
  :keymap (rails-controller-layout:keymap :view)
104
  (setq rails-primary-switch-func 'rails-controller-layout:toggle-action-view)
105
  (setq rails-secondary-switch-func 'rails-controller-layout:menu)
106
  (if (boundp 'mmm-mode-map)
107
      (progn
108
        (define-key mmm-mode-map (rails-key "p") 'rails-view-minor-mode:create-partial-from-selection)
109
        (define-key mmm-mode-map (rails-key "b") 'rails-view-minor-mode:create-helper-from-block))
110
    (progn
111
      (local-set-key (rails-key "p") 'rails-view-minor-mode:create-partial-from-selection)
112
      (local-set-key (rails-key "b") 'rails-view-minor-mode:create-helper-from-block))))
113
114
(provide 'rails-view-minor-mode)