forked from remvee/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails-ruby.el
114 lines (96 loc) · 3.98 KB
/
rails-ruby.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
;;; rails-ruby.el --- provide features for ruby-mode
;; 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:
;; setup align for ruby-mode
(require 'align)
(defconst align-ruby-modes '(ruby-mode)
"align-perl-modes is a variable defined in `align.el'.")
(defconst ruby-align-rules-list
'((ruby-comma-delimiter
(regexp . ",\\(\\s-*\\)[^/ \t\n]")
(modes . align-ruby-modes)
(repeat . t))
(ruby-string-after-func
(regexp . "^\\s-*[a-zA-Z0-9.:?_]+\\(\\s-+\\)['\"]\\w+['\"]")
(modes . align-ruby-modes)
(repeat . t))
(ruby-symbol-after-func
(regexp . "^\\s-*[a-zA-Z0-9.:?_]+\\(\\s-+\\):\\w+")
(modes . align-ruby-modes)))
"Alignment rules specific to the ruby mode.
See the variable `align-rules-list' for more details.")
(add-to-list 'align-perl-modes 'ruby-mode)
(add-to-list 'align-dq-string-modes 'ruby-mode)
(add-to-list 'align-sq-string-modes 'ruby-mode)
(add-to-list 'align-open-comment-modes 'ruby-mode)
(dolist (it ruby-align-rules-list)
(add-to-list 'align-rules-list it))
;; other stuff
(defun ruby-newline-and-indent ()
(interactive)
(newline)
(ruby-indent-line))
(defun ruby-toggle-string<>simbol ()
"Easy to switch between strings and symbols."
(interactive)
(let ((initial-pos (point)))
(save-excursion
(when (looking-at "[\"']") ;; skip beggining quote
(goto-char (+ (point) 1))
(unless (looking-at "\\w")
(goto-char (- (point) 1))))
(let* ((point (point))
(start (skip-syntax-backward "w"))
(end (skip-syntax-forward "w"))
(end (+ point start end))
(start (+ point start))
(start-quote (- start 1))
(end-quote (+ end 1))
(quoted-str (buffer-substring-no-properties start-quote end-quote))
(symbol-str (buffer-substring-no-properties start end)))
(cond
((or (string-match "^\"\\w+\"$" quoted-str)
(string-match "^\'\\w+\'$" quoted-str))
(setq quoted-str (substring quoted-str 1 (- (length quoted-str) 1)))
(kill-region start-quote end-quote)
(goto-char start-quote)
(insert (concat ":" quoted-str)))
((string-match "^\:\\w+$" symbol-str)
(setq symbol-str (substring symbol-str 1))
(kill-region start end)
(goto-char start)
(insert (format "'%s'" symbol-str))))))
(goto-char initial-pos)))
(require 'inf-ruby)
(defun run-ruby-in-buffer (buf script &rest params)
"Run CMD as a ruby process in BUF if BUF does not exist."
(message "run-ruby-in-buffer %s" params)
(let ((abuf (concat "*" buf "*")))
(when (not (comint-check-proc abuf))
(set-buffer (apply #'make-comint buf rails-ruby-command nil script params)))
(pop-to-buffer abuf)
(when (fboundp 'inf-ruby-mode)
(inf-ruby-mode)
(when (< (rails-core:current-rails-major-version) 3)
(make-local-variable 'inf-ruby-first-prompt-pattern)
(make-local-variable 'inf-ruby-prompt-pattern)
(setq inf-ruby-first-prompt-pattern "^>> "
inf-ruby-prompt-pattern "^>> "
inf-ruby-buffer (current-buffer))))))
(provide 'rails-ruby)