Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 203 lines (179 sloc) 7.135 kB
5a9417e @technomancy Added ruby-electric-mode.
authored
1 ;; -*-Emacs-Lisp-*-
2 ;;
3 ;; ruby-electric.el --- electric editing commands for ruby files
4 ;;
5 ;; Copyright (C) 2005 by Dee Zsombor <dee dot zsombor at gmail dot com>.
6 ;; Released under same license terms as Ruby.
7 ;;
8 ;; Due credit: this work was inspired by a code snippet posted by
9 ;; Frederick Ros at http://rubygarden.org/ruby?EmacsExtensions.
10 ;;
11 ;; Following improvements where added:
12 ;;
13 ;; - handling of strings of type 'here document'
14 ;; - more keywords, with special handling for 'do'
15 ;; - packaged into a minor mode
16 ;;
17 ;; Usage:
18 ;;
19 ;; 0) copy ruby-electric.el into directory where emacs can find it.
20 ;;
21 ;; 1) modify your startup file (.emacs or whatever) by adding
22 ;; following line:
23 ;;
24 ;; (require 'ruby-electric)
25 ;;
26 ;; note that you need to have font lock enabled beforehand.
27 ;;
28 ;; 2) toggle Ruby Electric Mode on/off with ruby-electric-mode.
29 ;;
30 ;; Changelog:
31 ;;
32 ;; 2005/Jan/14: inserts matching pair delimiters like {, [, (, ', ",
33 ;; ' and | .
34 ;;
35 ;; 2005/Jan/14: added basic Custom support for configuring keywords
36 ;; with electric closing.
37 ;;
38 ;; 2005/Jan/18: more Custom support for configuring characters for
39 ;; which matching expansion should occur.
40 ;;
41 ;; 2005/Jan/18: no longer uses 'looking-back' or regexp character
42 ;; classes like [:space:] since they are not implemented on XEmacs.
43 ;;
44 ;; 2005/Feb/01: explicitly provide default argument of 1 to
45 ;; 'backward-word' as it requires it on Emacs 21.3
46 ;;
47 ;; 2005/Mar/06: now stored inside ruby CVS; customize pages now have
48 ;; ruby as parent; cosmetic fixes.
49
50
51 (require 'ruby-mode)
52
53 (defgroup ruby-electric nil
54 "Minor mode providing electric editing commands for ruby files"
55 :group 'ruby)
56
57 (defconst ruby-electric-expandable-do-re
58 "do\\s-$")
59
60 (defconst ruby-electric-expandable-bar
61 "\\s-\\(do\\|{\\)\\s-+|")
62
63 (defvar ruby-electric-matching-delimeter-alist
64 '((?\[ . ?\])
65 (?\( . ?\))
66 (?\' . ?\')
67 (?\` . ?\`)
68 (?\" . ?\")))
69
70 (defcustom ruby-electric-simple-keywords-re
71 (regexp-opt '("def" "if" "class" "module" "unless" "case" "while" "do" "until" "for" "begin") t)
72 "*Regular expresion matching keywords for which closing 'end'
73 is to be inserted."
74 :type 'regexp :group 'ruby-electric)
75
76 (defcustom ruby-electric-expand-delimiters-list '(all)
77 "*List of contexts where matching delimiter should be
78 inserted. The word 'all' will do all insertions."
79 :type '(set :extra-offset 8
80 (const :tag "Everything" all )
81 (const :tag "Curly brace" ?\{ )
82 (const :tag "Square brace" ?\[ )
83 (const :tag "Round brace" ?\( )
84 (const :tag "Quote" ?\' )
85 (const :tag "Double quote" ?\" )
86 (const :tag "Back quote" ?\` )
87 (const :tag "Vertical bar" ?\| ))
88 :group 'ruby-electric)
89
90 (defcustom ruby-electric-newline-before-closing-bracket nil
91 "*Controls whether a newline should be inserted before the
92 closing bracket or not."
93 :type 'boolean :group 'ruby-electric)
94
95 ;;;###autoload
96 (define-minor-mode ruby-electric-mode
97 "Toggle Ruby Electric minor mode.
98 With no argument, this command toggles the mode. Non-null prefix
99 argument turns on the mode. Null prefix argument turns off the
100 mode.
101
102 When Ruby Electric mode is enabled, an indented 'end' is
103 heuristicaly inserted whenever typing a word like 'module',
104 'class', 'def', 'if', 'unless', 'case', 'until', 'for', 'begin',
105 'do'. Simple, double and back quotes as well as braces are paired
106 auto-magically. Expansion does not occur inside comments and
107 strings. Note that you must have Font Lock enabled."
108 ;; initial value.
109 nil
110 ;;indicator for the mode line.
111 " REl"
112 ;;keymap
113 ruby-mode-map
114 (ruby-electric-setup-keymap))
115
116 (defun ruby-electric-setup-keymap()
117 (define-key ruby-mode-map " " 'ruby-electric-space)
118 (define-key ruby-mode-map "{" 'ruby-electric-curlies)
119 (define-key ruby-mode-map "(" 'ruby-electric-matching-char)
120 (define-key ruby-mode-map "[" 'ruby-electric-matching-char)
121 (define-key ruby-mode-map "\"" 'ruby-electric-matching-char)
122 (define-key ruby-mode-map "\'" 'ruby-electric-matching-char)
123 (define-key ruby-mode-map "|" 'ruby-electric-bar))
124
125 (defun ruby-electric-space (arg)
126 (interactive "P")
127 (self-insert-command (prefix-numeric-value arg))
128 (if (ruby-electric-space-can-be-expanded-p)
129 (save-excursion
130 (ruby-indent-line t)
131 (newline)
31991b0 @technomancy latest clo-mode, ruby-electric fix.
authored
132 (insert "end")
133 (ruby-indent-line t))))
5a9417e @technomancy Added ruby-electric-mode.
authored
134
135 (defun ruby-electric-code-at-point-p()
136 (and ruby-electric-mode
137 (let* ((properties (text-properties-at (point))))
138 (and (null (memq 'font-lock-string-face properties))
139 (null (memq 'font-lock-comment-face properties))))))
140
141 (defun ruby-electric-string-at-point-p()
142 (and ruby-electric-mode
143 (consp (memq 'font-lock-string-face (text-properties-at (point))))))
144
145 (defun ruby-electric-is-last-command-char-expandable-punct-p()
146 (or (memq 'all ruby-electric-expand-delimiters-list)
147 (memq last-command-char ruby-electric-expand-delimiters-list)))
148
149 (defun ruby-electric-space-can-be-expanded-p()
150 (if (ruby-electric-code-at-point-p)
151 (let* ((ruby-electric-keywords-re
152 (concat ruby-electric-simple-keywords-re "\\s-$"))
153 (ruby-electric-single-keyword-in-line-re
154 (concat "\\s-*" ruby-electric-keywords-re)))
155 (save-excursion
156 (backward-word 1)
157 (or (looking-at ruby-electric-expandable-do-re)
158 (and (looking-at ruby-electric-keywords-re)
159 (not (string= "do" (match-string 1)))
160 (progn
161 (beginning-of-line)
162 (looking-at ruby-electric-single-keyword-in-line-re))))))))
163
164
165 (defun ruby-electric-curlies(arg)
166 (interactive "P")
167 (self-insert-command (prefix-numeric-value arg))
168 (if (ruby-electric-is-last-command-char-expandable-punct-p)
169 (cond ((ruby-electric-code-at-point-p)
170 (insert " ")
171 (save-excursion
172 (if ruby-electric-newline-before-closing-bracket
173 (newline))
174 (insert "}")))
175 ((ruby-electric-string-at-point-p)
176 (save-excursion
177 (backward-char 1)
178 (when (char-equal ?\# (preceding-char))
179 (forward-char 1)
180 (insert "}")))))))
181
182 (defun ruby-electric-matching-char(arg)
183 (interactive "P")
184 (self-insert-command (prefix-numeric-value arg))
185 (and (ruby-electric-is-last-command-char-expandable-punct-p)
186 (ruby-electric-code-at-point-p)
187 (save-excursion
188 (insert (cdr (assoc last-command-char
189 ruby-electric-matching-delimeter-alist))))))
190
191 (defun ruby-electric-bar(arg)
192 (interactive "P")
193 (self-insert-command (prefix-numeric-value arg))
194 (and (ruby-electric-is-last-command-char-expandable-punct-p)
195 (ruby-electric-code-at-point-p)
196 (and (save-excursion (re-search-backward ruby-electric-expandable-bar nil t))
197 (= (point) (match-end 0))) ;looking-back is missing on XEmacs
198 (save-excursion
199 (insert "|"))))
200
201
202 (provide 'ruby-electric)
Something went wrong with that request. Please try again.