Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 238 lines (209 sloc) 8.773 kB
3d7a18f @qoobaa improved description of the package
authored
1 ;;; ruby-electric.el --- Minor mode with electric editing commands for Ruby files
7b9bc96 add ruby-electric.el
zsombor authored
2 ;;
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
3 ;; Copyright (C) 2005 by Dee Zsombor
7b9bc96 add ruby-electric.el
zsombor authored
4 ;;
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
5 ;; Authors: Dee Zsombor <dee dot zsombor at gmail dot com>
6 ;; Maintainer: Jakub Kuźma <qoobaa@gmail.com>
7 ;; URL: http://github.com/qoobaa/ruby-electric/raw/master/ruby-electric.el
8 ;; Keywords: languages ruby
9 ;; Version: 1.1
7b9bc96 add ruby-electric.el
zsombor authored
10
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
11 ;;; Code:
7b9bc96 add ruby-electric.el
zsombor authored
12
13 (require 'ruby-mode)
14
15 (defgroup ruby-electric nil
16 "Minor mode providing electric editing commands for ruby files"
20ad878 removed trailing whitespaces
Jakub Kuźma authored
17 :group 'ruby)
7b9bc96 add ruby-electric.el
zsombor authored
18
19 (defconst ruby-electric-expandable-do-re
20 "do\\s-$")
21
22 (defconst ruby-electric-expandable-bar
0be1710 @qoobaa allow electric bar expansion in curly braces without spaces
authored
23 "\\(\\s-do\\s-+\\)\\|\\({\\s-*\\)")
7b9bc96 add ruby-electric.el
zsombor authored
24
25 (defvar ruby-electric-matching-delimeter-alist
26 '((?\[ . ?\])
27 (?\( . ?\))
28 (?\' . ?\')
29 (?\` . ?\`)
30 (?\" . ?\")))
31
d26c115 * misc/ruby-mode.el, misc/ruby-electric.el: use regexp-opt where
matz authored
32 (defcustom ruby-electric-simple-keywords-re
33 (regexp-opt '("def" "if" "class" "module" "unless" "case" "while" "do" "until" "for" "begin") t)
7b9bc96 add ruby-electric.el
zsombor authored
34 "*Regular expresion matching keywords for which closing 'end'
35 is to be inserted."
36 :type 'regexp :group 'ruby-electric)
37
38 (defcustom ruby-electric-expand-delimiters-list '(all)
39 "*List of contexts where matching delimiter should be
40 inserted. The word 'all' will do all insertions."
41 :type '(set :extra-offset 8
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
42 (const :tag "Everything" all )
43 (const :tag "Curly brace" ?\{ )
44 (const :tag "Square brace" ?\[ )
45 (const :tag "Round brace" ?\( )
46 (const :tag "Quote" ?\' )
47 (const :tag "Double quote" ?\" )
48 (const :tag "Back quote" ?\` )
49 (const :tag "Vertical bar" ?\| ))
20ad878 removed trailing whitespaces
Jakub Kuźma authored
50 :group 'ruby-electric)
7b9bc96 add ruby-electric.el
zsombor authored
51
52 (defcustom ruby-electric-newline-before-closing-bracket nil
53 "*Controls whether a newline should be inserted before the
54 closing bracket or not."
55 :type 'boolean :group 'ruby-electric)
56
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
57 ;;;###autoload
7b9bc96 add ruby-electric.el
zsombor authored
58 (define-minor-mode ruby-electric-mode
59 "Toggle Ruby Electric minor mode.
60 With no argument, this command toggles the mode. Non-null prefix
61 argument turns on the mode. Null prefix argument turns off the
62 mode.
63
64 When Ruby Electric mode is enabled, an indented 'end' is
65 heuristicaly inserted whenever typing a word like 'module',
66 'class', 'def', 'if', 'unless', 'case', 'until', 'for', 'begin',
67 'do'. Simple, double and back quotes as well as braces are paired
68 auto-magically. Expansion does not occur inside comments and
69 strings. Note that you must have Font Lock enabled."
70 ;; initial value.
71 nil
72 ;;indicator for the mode line.
73 " REl"
74 ;;keymap
75 ruby-mode-map
76 (ruby-electric-setup-keymap))
77
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
78 (defun ruby-electric-setup-keymap ()
7b9bc96 add ruby-electric.el
zsombor authored
79 (define-key ruby-mode-map " " 'ruby-electric-space)
80 (define-key ruby-mode-map "{" 'ruby-electric-curlies)
81 (define-key ruby-mode-map "(" 'ruby-electric-matching-char)
82 (define-key ruby-mode-map "[" 'ruby-electric-matching-char)
25d3e10 @pedz fixed quotes inside comments, fixed closing quotes and block arg lists
pedz authored
83 (define-key ruby-mode-map "\"" 'ruby-electric-quote)
84 (define-key ruby-mode-map "\'" 'ruby-electric-quote)
0decfc4 added ruby-electric-return
Jakub Kuźma authored
85 (define-key ruby-mode-map "|" 'ruby-electric-bar)
86 (define-key ruby-mode-map (kbd "RET") 'ruby-electric-return)
87 (define-key ruby-mode-map (kbd "C-j") 'ruby-electric-return)
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
88 (define-key ruby-mode-map (kbd "C-m") 'ruby-electric-return)
35a2ffe fixed ', ", | broken by rodimius patch
Jakub Kuźma authored
89 (define-key ruby-mode-map "}" 'ruby-electric-close-matching-char)
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
90 (define-key ruby-mode-map ")" 'ruby-electric-close-matching-char)
35a2ffe fixed ', ", | broken by rodimius patch
Jakub Kuźma authored
91 (define-key ruby-mode-map "]" 'ruby-electric-close-matching-char))
7b9bc96 add ruby-electric.el
zsombor authored
92
93 (defun ruby-electric-space (arg)
94 (interactive "P")
95 (self-insert-command (prefix-numeric-value arg))
96 (if (ruby-electric-space-can-be-expanded-p)
97 (save-excursion
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
98 (ruby-indent-line t)
99 (newline)
ed229f8 @qoobaa added own ruby-electric-insert-end
authored
100 (ruby-electric-insert-end))))
7b9bc96 add ruby-electric.el
zsombor authored
101
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
102 (defun ruby-electric-code-at-point-p ()
7b9bc96 add ruby-electric.el
zsombor authored
103 (and ruby-electric-mode
104 (let* ((properties (text-properties-at (point))))
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
105 (and (null (memq 'font-lock-string-face properties))
106 (null (memq 'font-lock-comment-face properties))))))
7b9bc96 add ruby-electric.el
zsombor authored
107
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
108 (defun ruby-electric-string-at-point-p ()
7b9bc96 add ruby-electric.el
zsombor authored
109 (and ruby-electric-mode
110 (consp (memq 'font-lock-string-face (text-properties-at (point))))))
111
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
112 (defun ruby-electric-is-last-command-char-expandable-punct-p ()
7b9bc96 add ruby-electric.el
zsombor authored
113 (or (memq 'all ruby-electric-expand-delimiters-list)
b3d3294 @qoobaa changed deprecated last-command-char to last-command-event
authored
114 (memq last-command-event ruby-electric-expand-delimiters-list)))
7b9bc96 add ruby-electric.el
zsombor authored
115
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
116 (defun ruby-electric-space-can-be-expanded-p ()
7b9bc96 add ruby-electric.el
zsombor authored
117 (if (ruby-electric-code-at-point-p)
20ad878 removed trailing whitespaces
Jakub Kuźma authored
118 (let* ((ruby-electric-keywords-re
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
119 (concat ruby-electric-simple-keywords-re "\\s-$"))
20ad878 removed trailing whitespaces
Jakub Kuźma authored
120 (ruby-electric-single-keyword-in-line-re
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
121 (concat "\\s-*" ruby-electric-keywords-re)))
122 (save-excursion
629ed22 @rranelli Change ruby-backward-sexp to backward-word on ruby-electric-space
rranelli authored
123 (backward-word 1)
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
124 (or (looking-at ruby-electric-expandable-do-re)
125 (and (looking-at ruby-electric-keywords-re)
126 (not (string= "do" (match-string 1)))
127 (progn
128 (beginning-of-line)
129 (looking-at ruby-electric-single-keyword-in-line-re))))))))
7b9bc96 add ruby-electric.el
zsombor authored
130
c2f7f5d @qoobaa fixed problem with inserting closing quotes and bars
authored
131 (defun ruby-electric-curlies(arg)
7b9bc96 add ruby-electric.el
zsombor authored
132 (interactive "P")
133 (self-insert-command (prefix-numeric-value arg))
134 (if (ruby-electric-is-last-command-char-expandable-punct-p)
135 (cond ((ruby-electric-code-at-point-p)
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
136 (save-excursion
137 (if ruby-electric-newline-before-closing-bracket
112234b @qoobaa applied patch from ruby source
authored
138 (progn
139 (newline)
140 (insert "}")
141 (ruby-indent-line t))
142 (insert "}"))))
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
143 ((ruby-electric-string-at-point-p)
c2f7f5d @qoobaa fixed problem with inserting closing quotes and bars
authored
144 (if (eq last-command-event ?{)
145 (save-excursion
146 (backward-char 1)
147 (or (char-equal ?\# (preceding-char))
148 (insert "#"))
149 (forward-char 1)
150 (insert "}")))))))
7b9bc96 add ruby-electric.el
zsombor authored
151
25d3e10 @pedz fixed quotes inside comments, fixed closing quotes and block arg lists
pedz authored
152 (defun ruby-electric-quote (arg)
153 (interactive "P")
154 (if (ruby-electric-is-last-command-char-expandable-punct-p)
155 ;; if outside quotes, do the self-insert as before
156 (if (ruby-electric-code-at-point-p)
157 (progn
158 (self-insert-command (prefix-numeric-value arg))
159 (save-excursion
160 (insert (cdr (assoc last-command-event
161 ruby-electric-matching-delimeter-alist)))))
06404ec @qoobaa allow escaping quotes inside of a string
authored
162 ;; else, inside quote so see if we need to just hop over the
163 ;; closing quote
164 (if (and
165 (looking-at (string last-command-event))
166 ;; allow escaping - don't hop over the quote if the
167 ;; previous char is a backslash
168 (not (char-equal ?\\ (preceding-char))))
25d3e10 @pedz fixed quotes inside comments, fixed closing quotes and block arg lists
pedz authored
169 (forward-char 1)
170 ;; else inside quote but not at the end.
171 (self-insert-command (prefix-numeric-value arg))))
172 ;; else electric mode is off, just do self-insert
173 (self-insert-command (prefix-numeric-value arg))))
174
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
175 (defun ruby-electric-matching-char (arg)
7b9bc96 add ruby-electric.el
zsombor authored
176 (interactive "P")
0804f3c @qoobaa fixed electric-bar
authored
177 (if (looking-at (regexp-quote (string last-command-event)))
c2f7f5d @qoobaa fixed problem with inserting closing quotes and bars
authored
178 (forward-char 1)
179 (progn
180 (self-insert-command (prefix-numeric-value arg))
181 (and (ruby-electric-is-last-command-char-expandable-punct-p)
182 (ruby-electric-code-at-point-p)
183 (save-excursion
184 (insert (cdr (assoc last-command-event
185 ruby-electric-matching-delimeter-alist))))))))
7b9bc96 add ruby-electric.el
zsombor authored
186
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
187 (defun ruby-electric-close-matching-char (arg)
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
188 (interactive "P")
0804f3c @qoobaa fixed electric-bar
authored
189 (if (looking-at (regexp-quote (string last-command-event)))
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
190 (forward-char 1)
191 (self-insert-command (prefix-numeric-value arg))))
192
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
193 (defun ruby-electric-bar (arg)
7b9bc96 add ruby-electric.el
zsombor authored
194 (interactive "P")
0804f3c @qoobaa fixed electric-bar
authored
195 (if (and (ruby-electric-is-last-command-char-expandable-punct-p)
196 (ruby-electric-code-at-point-p))
197 (if (and (save-excursion (re-search-backward ruby-electric-expandable-bar nil t))
198 (= (point) (match-end 0)))
199 ;; expand bar after ' do ' or ' { '
200 (progn
201 (self-insert-command (prefix-numeric-value arg))
202 (save-excursion
203 (insert "|")))
204 ;; behave like on closing ')'
205 (ruby-electric-close-matching-char arg))
25d3e10 @pedz fixed quotes inside comments, fixed closing quotes and block arg lists
pedz authored
206 (self-insert-command (prefix-numeric-value arg))))
7b9bc96 add ruby-electric.el
zsombor authored
207
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
208 (defun ruby-electric-return-can-be-expanded-p ()
0decfc4 added ruby-electric-return
Jakub Kuźma authored
209 (if (ruby-electric-code-at-point-p)
210 (let* ((ruby-electric-keywords-re
211 (concat ruby-electric-simple-keywords-re "$")))
212 (save-excursion
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
213 (skip-chars-backward "A-Za-z0-9_:")
0decfc4 added ruby-electric-return
Jakub Kuźma authored
214 (looking-at ruby-electric-keywords-re)))))
215
216 (defun ruby-electric-return ()
217 (interactive "*")
218 (if (ruby-electric-return-can-be-expanded-p)
219 (save-excursion
220 (newline)
ed229f8 @qoobaa added own ruby-electric-insert-end
authored
221 (ruby-electric-insert-end)))
0decfc4 added ruby-electric-return
Jakub Kuźma authored
222 (reindent-then-newline-and-indent))
7b9bc96 add ruby-electric.el
zsombor authored
223
ed229f8 @qoobaa added own ruby-electric-insert-end
authored
224 (defun ruby-electric-insert-end ()
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
225 (interactive)
226 (insert "end")
227 (ruby-indent-line t)
228 (end-of-line))
229
b3b1a45 @achamian Makes sure that ruby buffers are given the ruby-electric minor mode b…
achamian authored
230 ;; Makes sure that ruby buffers are given the ruby-electric minor mode by default
231 ;;;###autoload
232 (eval-after-load 'ruby-mode
fbbab97 @qoobaa simplified hook
authored
233 '(add-hook 'ruby-mode-hook 'ruby-electric-mode))
b3b1a45 @achamian Makes sure that ruby buffers are given the ruby-electric minor mode b…
achamian authored
234
7b9bc96 add ruby-electric.el
zsombor authored
235 (provide 'ruby-electric)
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
236
237 ;;; ruby-electric.el ends here
Something went wrong with that request. Please try again.