Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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