Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 211 lines (181 sloc) 7.502 kB
7b9bc96 add ruby-electric.el
zsombor authored
1 ;; ruby-electric.el --- electric editing commands for ruby files
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
dcdc455 do not add space between curly braces, fixed expandable-bar regexp (d…
Jakub Kuźma 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)
83 (define-key ruby-mode-map "\"" 'ruby-electric-matching-char)
84 (define-key ruby-mode-map "\'" 'ruby-electric-matching-char)
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
c5d4c28 replaced backward-word by ruby-backward-sexp to avoid symbol expandin…
Jakub Kuźma authored
123 (ruby-backward-sexp 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
131
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
132 (defun ruby-electric-curlies (arg)
7b9bc96 add ruby-electric.el
zsombor authored
133 (interactive "P")
134 (self-insert-command (prefix-numeric-value arg))
135 (if (ruby-electric-is-last-command-char-expandable-punct-p)
136 (cond ((ruby-electric-code-at-point-p)
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
137 (save-excursion
138 (if ruby-electric-newline-before-closing-bracket
139 (newline))
140 (insert "}")))
141 ((ruby-electric-string-at-point-p)
142 (save-excursion
143 (backward-char 1)
144 (when (char-equal ?\# (preceding-char))
145 (forward-char 1)
146 (insert "}")))))))
7b9bc96 add ruby-electric.el
zsombor authored
147
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
148 (defun ruby-electric-close-curlies (arg)
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
149 (interactive "P")
150 (if (looking-at "}")
151 (forward-char 1)
152 (self-insert-command (prefix-numeric-value arg))))
153
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
154 (defun ruby-electric-matching-char (arg)
7b9bc96 add ruby-electric.el
zsombor authored
155 (interactive "P")
156 (self-insert-command (prefix-numeric-value arg))
157 (and (ruby-electric-is-last-command-char-expandable-punct-p)
158 (ruby-electric-code-at-point-p)
159 (save-excursion
b3d3294 @qoobaa changed deprecated last-command-char to last-command-event
authored
160 (insert (cdr (assoc last-command-event
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
161 ruby-electric-matching-delimeter-alist))))))
7b9bc96 add ruby-electric.el
zsombor authored
162
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
163 (defun ruby-electric-close-matching-char (arg)
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
164 (interactive "P")
b3d3294 @qoobaa changed deprecated last-command-char to last-command-event
authored
165 (if (looking-at (string last-command-event))
9d4a7a1 @rodimius Added support for my clumsy typing. So typing '(' then something else…
rodimius authored
166 (forward-char 1)
167 (self-insert-command (prefix-numeric-value arg))))
168
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
169 (defun ruby-electric-bar (arg)
7b9bc96 add ruby-electric.el
zsombor authored
170 (interactive "P")
171 (self-insert-command (prefix-numeric-value arg))
172 (and (ruby-electric-is-last-command-char-expandable-punct-p)
173 (ruby-electric-code-at-point-p)
174 (and (save-excursion (re-search-backward ruby-electric-expandable-bar nil t))
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
175 (= (point) (match-end 0))) ;looking-back is missing on XEmacs
20ad878 removed trailing whitespaces
Jakub Kuźma authored
176 (save-excursion
aa2e9da * misc/*.el: untabify, for interal consistency and consistency
matz authored
177 (insert "|"))))
7b9bc96 add ruby-electric.el
zsombor authored
178
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
179 (defun ruby-electric-return-can-be-expanded-p ()
0decfc4 added ruby-electric-return
Jakub Kuźma authored
180 (if (ruby-electric-code-at-point-p)
181 (let* ((ruby-electric-keywords-re
182 (concat ruby-electric-simple-keywords-re "$")))
183 (save-excursion
4e02deb @qoobaa fixed ruby-electric-return-can-be-expanded-p
authored
184 (skip-chars-backward "A-Za-z0-9_:")
0decfc4 added ruby-electric-return
Jakub Kuźma authored
185 (looking-at ruby-electric-keywords-re)))))
186
187 (defun ruby-electric-return ()
188 (interactive "*")
189 (if (ruby-electric-return-can-be-expanded-p)
190 (save-excursion
191 (newline)
ed229f8 @qoobaa added own ruby-electric-insert-end
authored
192 (ruby-electric-insert-end)))
0decfc4 added ruby-electric-return
Jakub Kuźma authored
193 (reindent-then-newline-and-indent))
7b9bc96 add ruby-electric.el
zsombor authored
194
ed229f8 @qoobaa added own ruby-electric-insert-end
authored
195 (defun ruby-electric-insert-end ()
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
196 (interactive)
197 (insert "end")
198 (ruby-indent-line t)
199 (end-of-line))
200
b3b1a45 @achamian Makes sure that ruby buffers are given the ruby-electric minor mode b…
achamian authored
201 ;; Makes sure that ruby buffers are given the ruby-electric minor mode by default
202 ;;;###autoload
203 (eval-after-load 'ruby-mode
204 '(add-hook 'ruby-mode-hook
205 (lambda ()
206 (ruby-electric-mode))))
207
7b9bc96 add ruby-electric.el
zsombor authored
208 (provide 'ruby-electric)
da8ceb7 ELPA integration (standarized comments, added autoloads, etc.)
Jakub Kuźma authored
209
210 ;;; ruby-electric.el ends here
Something went wrong with that request. Please try again.