1 ;;; ruby-mode.el --- Major mode for editing Ruby files
3 ;; Copyright (C) 1994, 1995, 1996 1997, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Authors: Yukihiro Matsumoto, Nobuyoshi Nakada
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/RubyMode
9 ;; Created: Fri Feb 4 14:49:13 JST 1994
10 ;; Keywords: languages ruby
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;; Provides font-locking, indentation support, and navigation for Ruby code.
32 ;; If you're installing manually, you should add this to your .emacs
33 ;; file after putting it on your load path:
35 ;; (autoload 'ruby-mode "ruby-mode" "Major mode for ruby files" t)
36 ;; (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
37 ;; (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
39 ;; Still needs more docstrings; search below for TODO.
43 (eval-when-compile (require 'cl
))
45 (defconst ruby-keyword-end-re
46 (if (string-match "\\_>" "ruby")
50 (defconst ruby-block-beg-keywords
51 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
52 "Keywords at the beginning of blocks.")
54 (defconst ruby-block-beg-re
55 (regexp-opt ruby-block-beg-keywords
)
56 "Regexp to match the beginning of blocks.")
58 (defconst ruby-non-block-do-re
59 (concat (regexp-opt '("while" "until" "for" "rescue") t
) ruby-keyword-end-re
)
60 "Regexp to match keywords that nest without blocks.")
62 (defconst ruby-indent-beg-re
63 (concat "\\(\\s *" (regexp-opt '("class" "module" "def") t
) "\\)\\|"
64 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin")))
65 "Regexp to match where the indentation gets deeper.")
67 (defconst ruby-modifier-beg-keywords
68 '("if" "unless" "while" "until")
69 "Modifiers that are the same as the beginning of blocks.")
71 (defconst ruby-modifier-beg-re
72 (regexp-opt ruby-modifier-beg-keywords
)
73 "Regexp to match modifiers same as the beginning of blocks.")
75 (defconst ruby-modifier-re
76 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords
))
77 "Regexp to match modifiers.")
79 (defconst ruby-block-mid-keywords
80 '("then" "else" "elsif" "when" "rescue" "ensure")
81 "Keywords where the indentation gets shallower in middle of block statements.")
83 (defconst ruby-block-mid-re
84 (regexp-opt ruby-block-mid-keywords
)
85 "Regexp to match where the indentation gets shallower in middle of block statements.")
87 (defconst ruby-block-op-keywords
89 "Regexp to match boolean keywords.")
91 (defconst ruby-block-hanging-re
92 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords
))
93 "Regexp to match hanging block modifiers.")
95 (defconst ruby-block-end-re
"\\<end\\>")
97 (defconst ruby-here-doc-beg-re
98 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
99 "Regexp to match the beginning of a heredoc.")
101 (defconst ruby-here-doc-end-re
102 "^\\([ \t]+\\)?\\(.*\\)\\(.\\)$"
103 "Regexp to match the end of heredocs.
105 This will actually match any line with one or more characters.
106 It's useful in that it divides up the match string so that
107 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
109 (defun ruby-here-doc-end-match ()
110 "Return a regexp to find the end of a heredoc.
112 This should only be called after matching against `ruby-here-doc-beg-re'."
114 (if (match-string 2) "[ \t]*" nil
)
120 (defun ruby-here-doc-beg-match ()
121 "Return a regexp to find the beginning of a heredoc.
123 This should only be called after matching against `ruby-here-doc-end-re'."
124 (let ((contents (regexp-quote (concat (match-string 2) (match-string 3)))))
126 (let ((match (match-string 1)))
127 (if (and match
(> (length match
) 0))
128 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)" (match-string 1) "\\)"
129 contents
"\\b\\(\\1\\|\\2\\)")
130 (concat "-?\\([\"']\\|\\)" contents
"\\b\\1"))))))
132 (defconst ruby-delimiter
133 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\<\\("
135 "\\)\\>\\|" ruby-block-end-re
136 "\\|^=begin\\|" ruby-here-doc-beg-re
))
138 (defconst ruby-negative
139 (concat "^[ \t]*\\(\\(" ruby-block-mid-re
"\\)\\>\\|"
140 ruby-block-end-re
"\\|}\\|\\]\\)")
141 "Regexp to match where the indentation gets shallower.")
143 (defconst ruby-operator-re
"[-,.+*/%&|^~=<>:]"
144 "Regexp to match operators.")
146 (defconst ruby-symbol-chars
"a-zA-Z0-9_"
147 "List of characters that symbol names may contain.")
148 (defconst ruby-symbol-re
(concat "[" ruby-symbol-chars
"]")
149 "Regexp to match symbols.")
151 (defvar ruby-mode-abbrev-table nil
152 "Abbrev table in use in Ruby mode buffers.")
154 (define-abbrev-table 'ruby-mode-abbrev-table
())
156 (defvar ruby-mode-map
157 (let ((map (make-sparse-keymap)))
158 (define-key map
"{" 'ruby-electric-brace
)
159 (define-key map
"}" 'ruby-electric-brace
)
160 (define-key map
(kbd "M-C-a") 'ruby-beginning-of-defun
)
161 (define-key map
(kbd "M-C-e") 'ruby-end-of-defun
)
162 (define-key map
(kbd "M-C-b") 'ruby-backward-sexp
)
163 (define-key map
(kbd "M-C-f") 'ruby-forward-sexp
)
164 (define-key map
(kbd "M-C-p") 'ruby-beginning-of-block
)
165 (define-key map
(kbd "M-C-n") 'ruby-end-of-block
)
166 (define-key map
(kbd "M-C-h") 'ruby-mark-defun
)
167 (define-key map
(kbd "M-C-q") 'ruby-indent-exp
)
168 (define-key map
(kbd "TAB") 'ruby-indent-line
)
169 (define-key map
(kbd "C-M-h") 'backward-kill-word
)
170 (define-key map
(kbd "C-j") 'reindent-then-newline-and-indent
)
171 (define-key map
(kbd "C-m") 'newline
)
173 "Keymap used in Ruby mode.")
175 (defvar ruby-mode-syntax-table
176 (let ((table (make-syntax-table)))
177 (modify-syntax-entry ?
\' "\"" table
)
178 (modify-syntax-entry ?
\" "\"" table
)
179 (modify-syntax-entry ?\
` "\"" table
)
180 (modify-syntax-entry ?
# "<" table
)
181 (modify-syntax-entry ?
\n ">" table
)
182 (modify-syntax-entry ?
\\ "\\" table
)
183 (modify-syntax-entry ?$
"." table
)
184 (modify-syntax-entry ??
"_" table
)
185 (modify-syntax-entry ?_
"_" table
)
186 (modify-syntax-entry ?
< "." table
)
187 (modify-syntax-entry ?
> "." table
)
188 (modify-syntax-entry ?
& "." table
)
189 (modify-syntax-entry ?|
"." table
)
190 (modify-syntax-entry ?%
"." table
)
191 (modify-syntax-entry ?
= "." table
)
192 (modify-syntax-entry ?
/ "." table
)
193 (modify-syntax-entry ?
+ "." table
)
194 (modify-syntax-entry ?
* "." table
)
195 (modify-syntax-entry ?-
"." table
)
196 (modify-syntax-entry ?\
; "." table)
197 (modify-syntax-entry ?\
( "()" table
)
198 (modify-syntax-entry ?\
) ")(" table
)
199 (modify-syntax-entry ?\
{ "(}" table
)
200 (modify-syntax-entry ?\
} "){" table
)
201 (modify-syntax-entry ?\
[ "(]" table
)
202 (modify-syntax-entry ?\
] ")[" table
)
204 "Syntax table to use in Ruby mode.")
206 (defcustom ruby-indent-tabs-mode nil
207 "Indentation can insert tabs in Ruby mode if this is non-nil."
208 :type
'boolean
:group
'ruby
)
210 (defcustom ruby-indent-level
2
211 "Indentation of Ruby statements."
212 :type
'integer
:group
'ruby
)
214 (defcustom ruby-comment-column
32
215 "Indentation column of comments."
216 :type
'integer
:group
'ruby
)
218 (defcustom ruby-deep-arglist t
219 "Deep indent lists in parenthesis when non-nil.
220 Also ignores spaces after parenthesis when 'space."
223 (defcustom ruby-deep-indent-paren
'(?\
( ?\
[ ?\
] t
)
224 "Deep indent lists in parenthesis when non-nil.
225 The value t means continuous line.
226 Also ignores spaces after parenthesis when 'space."
229 (defcustom ruby-deep-indent-paren-style
'space
230 "Default deep indent style."
231 :options
'(t nil space
) :group
'ruby
)
233 (defcustom ruby-encoding-map
'((shift_jis . cp932
) (shift-jis . cp932
))
234 "Alist to map encoding name from Emacs to Ruby."
237 (defcustom ruby-insert-encoding-magic-comment t
238 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
239 :type
'boolean
:group
'ruby
)
241 (defcustom ruby-use-encoding-map t
242 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
243 :type
'boolean
:group
'ruby
)
245 ;; Safe file variables
246 (put 'ruby-indent-tabs-mode
'safe-local-variable
'booleanp
)
247 (put 'ruby-indent-level
'safe-local-variable
'integerp
)
248 (put 'ruby-comment-column
'safe-local-variable
'integerp
)
249 (put 'ruby-deep-arglist
'safe-local-variable
'booleanp
)
251 (defun ruby-imenu-create-index-in-block (prefix beg end
)
252 "Create an imenu index of methods inside a block."
253 (let ((index-alist '()) (case-fold-search nil
)
254 name next pos decl sing
)
256 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t
)
257 (setq sing
(match-beginning 3))
258 (setq decl
(match-string 5))
259 (setq next
(match-end 0))
260 (setq name
(or (match-string 4) (match-string 6)))
261 (setq pos
(match-beginning 0))
263 ((string= "alias" decl
)
264 (if prefix
(setq name
(concat prefix name
)))
265 (push (cons name pos
) index-alist
))
266 ((string= "def" decl
)
270 ((string-match "^self\." name
)
271 (concat (substring prefix
0 -
1) (substring name
4)))
272 (t (concat prefix name
)))))
273 (push (cons name pos
) index-alist
)
274 (ruby-accurate-end-of-block end
))
276 (if (string= "self" name
)
277 (if prefix
(setq name
(substring prefix
0 -
1)))
278 (if prefix
(setq name
(concat (substring prefix
0 -
1) "::" name
)))
279 (push (cons name pos
) index-alist
))
280 (ruby-accurate-end-of-block end
)
283 (nconc (ruby-imenu-create-index-in-block
284 (concat name
(if sing
"." "#"))
285 next beg
) index-alist
))
289 (defun ruby-imenu-create-index ()
290 "Create an imenu index of all methods in the buffer."
291 (nreverse (ruby-imenu-create-index-in-block nil
(point-min) nil
)))
293 (defun ruby-accurate-end-of-block (&optional end
)
296 (end (or end
(point-max))))
297 (while (and (setq state
(apply 'ruby-parse-partial end state
))
298 (>= (nth 2 state
) 0) (< (point) end
)))))
300 (defun ruby-mode-variables ()
301 "Set up initial buffer-local variables for Ruby mode."
302 (set-syntax-table ruby-mode-syntax-table
)
303 (setq local-abbrev-table ruby-mode-abbrev-table
)
304 (setq indent-tabs-mode ruby-indent-tabs-mode
)
305 (set (make-local-variable 'indent-line-function
) 'ruby-indent-line
)
306 (set (make-local-variable 'require-final-newline
) t
)
307 (set (make-local-variable 'comment-start
) "# ")
308 (set (make-local-variable 'comment-end
) "")
309 (set (make-local-variable 'comment-column
) ruby-comment-column
)
310 (set (make-local-variable 'comment-start-skip
) "#+ *")
311 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
312 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
313 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
314 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
315 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
))
317 (defun ruby-mode-set-encoding ()
318 "Insert a magic comment header with the proper encoding if necessary."
321 (goto-char (point-min))
322 (when (re-search-forward "[^\0-\177]" nil t
)
323 (goto-char (point-min))
325 (or coding-system-for-write
326 buffer-file-coding-system
)))
329 (or (coding-system-get coding-system
'mime-charset
)
330 (coding-system-change-eol-conversion coding-system nil
))))
334 (or (and ruby-use-encoding-map
335 (cdr (assq coding-system ruby-encoding-map
)))
338 (if (looking-at "^#![^\n]*ruby") (beginning-of-line 2))
339 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
340 (unless (string= (match-string 2) coding-system
)
341 (goto-char (match-beginning 2))
342 (delete-region (point) (match-end 2))
343 (and (looking-at "-\*-")
344 (let ((n (skip-chars-backward " ")))
345 (cond ((= n
0) (insert " ") (backward-char))
346 ((= n -
1) (insert " "))
348 (insert coding-system
)))
349 ((looking-at "\\s *#.*coding\\s *[:=]"))
350 (t (when ruby-insert-encoding-magic-comment
351 (insert "# -*- coding: " coding-system
" -*-\n"))))))))
353 (defun ruby-current-indentation ()
354 "Return the indentation level of current line."
357 (back-to-indentation)
360 (defun ruby-indent-line (&optional flag
)
361 "Correct the indentation of the current Ruby line."
363 (ruby-indent-to (ruby-calculate-indent)))
365 (defun ruby-indent-to (column)
366 "Indent the current line to COLUMN."
369 (and (< column
0) (error "invalid nest"))
370 (setq shift
(current-column))
373 (back-to-indentation)
374 (setq top
(current-column))
375 (skip-chars-backward " \t")
376 (if (>= shift top
) (setq shift
(- shift top
))
380 (move-to-column (+ column shift
))
382 (delete-region beg
(point))
385 (move-to-column (+ column shift
))))))
387 (defun ruby-special-char-p (&optional pos
)
388 "Return t if the character before POS is a special character.
389 If omitted, POS defaults to the current point.
390 Special characters are `?', `$', `:' when preceded by whitespace,
391 and `\\' when preceded by `?'."
392 (setq pos
(or pos
(point)))
393 (let ((c (char-before pos
)) (b (and (< (point-min) pos
)
394 (char-before (1- pos
)))))
395 (cond ((or (eq c ??
) (eq c ?$
)))
396 ((and (eq c ?
:) (or (not b
) (eq (char-syntax b
) ?
))))
397 ((eq c ?
\\) (eq b ??
)))))
399 (defun ruby-expr-beg (&optional option
)
402 (store-match-data nil
)
403 (let ((space (skip-chars-backward " \t"))
409 (and (looking-at "\\?")
410 (or (eq (char-syntax (char-before (point))) ?w
)
411 (ruby-special-char-p))))
413 ((and (eq option
'heredoc
) (< space
0)) t
)
414 ((or (looking-at ruby-operator-re
)
415 (looking-at "[\\[({,;]")
416 (and (looking-at "[!?]")
417 (or (not (eq option
'modifier
))
419 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
420 (and (looking-at ruby-symbol-re
)
421 (skip-chars-backward ruby-symbol-chars
)
423 ((looking-at (regexp-opt
424 (append ruby-block-beg-keywords
425 ruby-block-op-keywords
426 ruby-block-mid-keywords
)
428 (goto-char (match-end 0))
429 (not (looking-at "\\s_")))
430 ((eq option
'expr-qstr
)
431 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
432 ((eq option
'expr-re
)
433 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
436 (defun ruby-forward-string (term &optional end no-error expand
)
438 (let ((n 1) (c (string-to-char term
))
440 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term
"]\\|\\(#{\\)\\)")
441 (concat "[^\\]\\(\\\\\\\\\\)*[" term
"]"))))
442 (while (and (re-search-forward re end no-error
)
443 (if (match-beginning 3)
444 (ruby-forward-string "}{" end no-error nil
)
445 (> (setq n
(if (eq (char-before (point)) c
)
450 ((error "unterminated string")))))
452 (defun ruby-deep-indent-paren-p (c)
454 (cond ((listp ruby-deep-indent-paren
)
455 (let ((deep (assoc c ruby-deep-indent-paren
)))
457 (or (cdr deep
) ruby-deep-indent-paren-style
))
458 ((memq c ruby-deep-indent-paren
)
459 ruby-deep-indent-paren-style
))))
460 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
461 ((eq c ?\
( ) ruby-deep-arglist
)))
463 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
464 "TODO: document throughout function body."
465 (or depth
(setq depth
0))
466 (or indent
(setq indent
0))
467 (when (re-search-forward ruby-delimiter end
'move
)
468 (let ((pnt (point)) w re expand
)
469 (goto-char (match-beginning 0))
471 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
473 ((looking-at "[\"`]") ;skip string
476 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t
))
479 (setq in-string
(point))
484 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
487 (setq in-string
(point))
493 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
494 (if (ruby-forward-string "/" end t t
)
496 (setq in-string
(point))
503 (ruby-expr-beg 'expr-qstr
)
504 (not (looking-at "%="))
505 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
506 (goto-char (match-beginning 1))
507 (setq expand
(not (memq (char-before) '(?q ?w
))))
508 (setq w
(match-string 1))
510 ((string= w
"[") (setq re
"]["))
511 ((string= w
"{") (setq re
"}{"))
512 ((string= w
"(") (setq re
")("))
513 ((string= w
"<") (setq re
"><"))
514 ((and expand
(string= w
"\\"))
515 (setq w
(concat "\\" w
))))
516 (unless (cond (re (ruby-forward-string re end t expand
))
517 (expand (ruby-forward-string w end t t
))
518 (t (re-search-forward
521 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
523 (setq in-string
(point))
527 ((looking-at "\\?") ;skip ?char
529 ((and (ruby-expr-beg)
530 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
531 (goto-char (match-end 0)))
534 ((looking-at "\\$") ;skip $char
537 ((looking-at "#") ;skip comment
541 ((looking-at "[\\[{(]")
542 (let ((deep (ruby-deep-indent-paren-p (char-after))))
543 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
545 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
546 (setq pnt
(1- (match-end 0))))
547 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
548 (setq pcol
(cons (cons pnt depth
) pcol
))
550 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
551 (setq depth
(1+ depth
))))
554 ((looking-at "[])}]")
555 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
556 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
557 (setq depth
(1- depth
)))
558 (setq nest
(cdr nest
))
560 ((looking-at ruby-block-end-re
)
561 (if (or (and (not (bolp))
564 (setq w
(char-after (point)))
569 (setq w
(char-after (point)))
574 (setq nest
(cdr nest
))
575 (setq depth
(1- depth
)))
577 ((looking-at "def\\s +[^(\n;]*")
581 (not (eq ?_
(char-after (point))))))
583 (setq nest
(cons (cons nil pnt
) nest
))
584 (setq depth
(1+ depth
))))
585 (goto-char (match-end 0)))
586 ((looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
589 (or (not (looking-at (concat "do" ruby-keyword-end-re
)))
591 (back-to-indentation)
592 (not (looking-at ruby-non-block-do-re
)))))
596 (setq w
(char-after (point)))
600 (setq w
(char-after (point)))
604 (skip-chars-forward " \t")
605 (goto-char (match-beginning 0))
606 (or (not (looking-at ruby-modifier-re
))
607 (ruby-expr-beg 'modifier
))
609 (setq nest
(cons (cons nil pnt
) nest
))
610 (setq depth
(1+ depth
)))
612 ((looking-at ":\\(['\"]\\)")
613 (goto-char (match-beginning 1))
614 (ruby-forward-string (buffer-substring (match-beginning 1) (match-end 1)) end
))
615 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\)")
616 (goto-char (match-end 0)))
617 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
618 (goto-char (match-end 0)))
619 ((or (looking-at "\\.\\.\\.?")
620 (looking-at "\\.[0-9]+")
621 (looking-at "\\.[a-zA-Z_0-9]+")
623 (goto-char (match-end 0)))
624 ((looking-at "^=begin")
625 (if (re-search-forward "^=end" end t
)
627 (setq in-string
(match-end 0))
631 ((and (ruby-expr-beg 'heredoc
)
632 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
633 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
634 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
635 (let* ((id-end (goto-char (match-end 0)))
636 (line-end-position (save-excursion (end-of-line) (point)))
637 (state (list in-string nest depth pcol indent
)))
638 ;; parse the rest of the line
639 (while (and (> line-end-position
(point))
640 (setq state
(apply 'ruby-parse-partial
641 line-end-position state
))))
642 (setq in-string
(car state
)
646 indent
(nth 4 state
))
647 ;; skip heredoc section
648 (if (re-search-forward (concat "^" re
"$") end
'move
)
650 (setq in-string id-end
)
654 ((looking-at "^__END__$")
656 ((and (looking-at ruby-here-doc-beg-re
)
657 (boundp 'ruby-indent-point
))
658 (if (re-search-forward (ruby-here-doc-end-match)
661 (setq in-string
(match-end 0))
662 (goto-char ruby-indent-point
)))
664 (error (format "bad string %s"
665 (buffer-substring (point) pnt
)
667 (list in-string nest depth pcol
))
669 (defun ruby-parse-region (start end
)
675 (ruby-beginning-of-indent))
677 (narrow-to-region (point) end
)
678 (while (and (> end
(point))
679 (setq state
(apply 'ruby-parse-partial end state
))))))
680 (list (nth 0 state
) ; in-string
681 (car (nth 1 state
)) ; nest
682 (nth 2 state
) ; depth
683 (car (car (nth 3 state
))) ; pcol
684 ;(car (nth 5 state)) ; indent
687 (defun ruby-indent-size (pos nest
)
688 "Return the indentation level in spaces NEST levels deeper than POS."
689 (+ pos
(* (or nest
1) ruby-indent-level
)))
691 (defun ruby-calculate-indent (&optional parse-start
)
692 "Return the proper indentation level of the current line."
693 ;; TODO: Document body
696 (let ((ruby-indent-point (point))
697 (case-fold-search nil
)
698 state bol eol begin op-end
699 (paren (progn (skip-syntax-forward " ")
700 (and (char-after) (matching-paren (char-after)))))
703 (goto-char parse-start
)
704 (ruby-beginning-of-indent)
705 (setq parse-start
(point)))
706 (back-to-indentation)
707 (setq indent
(current-column))
708 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
710 ((nth 0 state
) ; within string
711 (setq indent nil
)) ; do nothing
712 ((car (nth 1 state
)) ; in paren
713 (goto-char (setq begin
(cdr (nth 1 state
))))
714 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
716 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
717 (skip-syntax-backward " ")
718 (setq indent
(1- (current-column))))
719 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
720 (and (nth 2 s
) (> (nth 2 s
) 0)
721 (or (goto-char (cdr (nth 1 s
))) t
)))
723 (setq indent
(ruby-indent-size (current-column)
726 (setq indent
(current-column))
727 (cond ((eq deep
'space
))
728 (paren (setq indent
(1- indent
)))
729 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
730 (if (nth 3 state
) (goto-char (nth 3 state
))
731 (goto-char parse-start
) (back-to-indentation))
732 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
733 (and (eq (car (nth 1 state
)) paren
)
734 (ruby-deep-indent-paren-p (matching-paren paren
))
735 (search-backward (char-to-string paren
))
736 (setq indent
(current-column)))))
737 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
738 (if (null (cdr (nth 1 state
)))
739 (error "invalid nest"))
740 (goto-char (cdr (nth 1 state
)))
741 (forward-word -
1) ; skip back a keyword
744 ((looking-at "do\\>[^_]") ; iter block is a special case
745 (if (nth 3 state
) (goto-char (nth 3 state
))
746 (goto-char parse-start
) (back-to-indentation))
747 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
749 (setq indent
(+ (current-column) ruby-indent-level
)))))
751 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
752 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
754 (goto-char ruby-indent-point
)
759 ((and (not (ruby-deep-indent-paren-p paren
))
760 (re-search-forward ruby-negative eol t
))
761 (and (not (eq ?_
(char-after (match-end 0))))
762 (setq indent
(- indent ruby-indent-level
))))
767 (or (ruby-deep-indent-paren-p t
)
768 (null (car (nth 1 state
)))))
769 ;; goto beginning of non-empty no-comment line
772 (skip-chars-backward " \t\n")
775 (if (re-search-forward "^\\s *#" end t
)
780 ;; skip the comment at the end
781 (skip-chars-backward " \t")
782 (let (end (pos (point)))
784 (while (and (re-search-forward "#" pos t
)
785 (setq end
(1- (point)))
786 (or (ruby-special-char-p end
)
787 (and (setq state
(ruby-parse-region parse-start end
))
790 (goto-char (or end pos
))
791 (skip-chars-backward " \t")
792 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
793 (setq state
(ruby-parse-region parse-start
(point))))
794 (or (bobp) (forward-char -
1))
796 (or (and (looking-at ruby-symbol-re
)
797 (skip-chars-backward ruby-symbol-chars
)
798 (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>"))
799 (not (eq (point) (nth 3 state
)))
801 (goto-char (match-end 0))
802 (not (looking-at "[a-z_]"))))
803 (and (looking-at ruby-operator-re
)
804 (not (ruby-special-char-p))
805 ;; operator at the end of line
806 (let ((c (char-after (point))))
811 ;; (skip-chars-forward " \t")
812 ;; (not (or (eolp) (looking-at "#")
813 ;; (and (eq (car (nth 1 state)) ?{)
814 ;; (looking-at "|"))))))
816 (null (nth 0 (ruby-parse-region (or begin parse-start
) (point)))))
817 (or (not (eq ?|
(char-after (point))))
819 (or (eolp) (forward-char -
1))
821 ((search-backward "|" nil t
)
822 (skip-chars-backward " \t\n")
826 (not (looking-at "{")))
829 (not (looking-at "do\\>[^_]")))))
837 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>")))
838 (eq (ruby-deep-indent-paren-p t
) 'space
)
841 (goto-char (or begin parse-start
))
842 (skip-syntax-forward " ")
844 ((car (nth 1 state
)) indent
)
846 (+ indent ruby-indent-level
))))))))
847 (goto-char ruby-indent-point
)
849 (skip-syntax-forward " ")
850 (if (looking-at "\\.[^.]")
851 (+ indent ruby-indent-level
)
854 (defun ruby-electric-brace (arg)
855 "Insert a brace and re-indent the current line."
857 (self-insert-command (prefix-numeric-value arg
))
858 (ruby-indent-line t
))
860 ;; TODO: Why isn't one ruby-*-of-defun written in terms of the other?
861 (defun ruby-beginning-of-defun (&optional arg
)
862 "Move backward to the beginning of the current top-level defun.
863 With ARG, move backward multiple defuns. Negative ARG means
866 (and (re-search-backward (concat "^\\(" ruby-block-beg-re
"\\)\\b")
867 nil
'move
(or arg
1))
868 (beginning-of-line)))
870 (defun ruby-end-of-defun (&optional arg
)
871 "Move forward to the end of the current top-level defun.
872 With ARG, move forward multiple defuns. Negative ARG means
875 (and (re-search-forward (concat "^\\(" ruby-block-end-re
"\\)\\($\\|\\b[^_]\\)")
876 nil
'move
(or arg
1))
880 (defun ruby-beginning-of-indent ()
882 ;; I don't understand this function.
883 ;; It seems like it should move to the line where indentation should deepen,
884 ;; but ruby-indent-beg-re only accounts for whitespace before class, module and def,
885 ;; so this will only match other block beginners at the beginning of the line.
886 (and (re-search-backward (concat "^\\(" ruby-indent-beg-re
"\\)\\b") nil
'move
)
887 (beginning-of-line)))
889 (defun ruby-move-to-block (n)
890 "Move to the beginning (N < 0) or the end (N > 0) of the current block
891 or blocks containing the current block."
892 ;; TODO: Make this work for n > 1,
893 ;; make it not loop for n = 0,
895 (let (start pos done down
)
896 (setq start
(ruby-calculate-indent))
897 (setq down
(looking-at (if (< n
0) ruby-block-end-re
898 (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))))
899 (while (and (not done
) (not (if (< n
0) (bobp) (eobp))))
902 ((looking-at "^\\s *$"))
903 ((looking-at "^\\s *#"))
904 ((and (> n
0) (looking-at "^=begin\\>"))
905 (re-search-forward "^=end\\>"))
906 ((and (< n
0) (looking-at "^=end\\>"))
907 (re-search-backward "^=begin\\>"))
909 (setq pos
(current-indentation))
913 ((and down
(= pos start
))
919 (back-to-indentation)
920 (if (looking-at (concat "\\<\\(" ruby-block-mid-re
"\\)\\>"))
922 (back-to-indentation))
924 (defun ruby-beginning-of-block (&optional arg
)
925 "Move backward to the beginning of the current block.
926 With ARG, move up multiple blocks."
928 (ruby-move-to-block (- (or arg
1))))
930 (defun ruby-end-of-block (&optional arg
)
931 "Move forward to the end of the current block.
932 With ARG, move out of multiple blocks."
933 ;; Passing a value > 1 to ruby-move-to-block currently doesn't work.
935 (ruby-move-to-block (or arg
1)))
937 (defun ruby-forward-sexp (&optional arg
)
938 "Move forward across one balanced expression (sexp).
939 With ARG, do it many times. Negative ARG means move backward."
940 ;; TODO: Document body
942 (if (and (numberp arg
) (< arg
0))
943 (ruby-backward-sexp (- arg
))
944 (let ((i (or arg
1)))
947 (skip-syntax-forward " ")
948 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
949 (goto-char (match-end 0)))
951 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
953 (goto-char (scan-sexps (point) 1)))
954 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
955 (not (eq (char-before (point)) ?.
))
956 (not (eq (char-before (point)) ?
:)))
959 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
961 (while (progn (forward-word 1) (looking-at "_")))
962 (cond ((looking-at "::") (forward-char 2) t
)
963 ((> (skip-chars-forward ".") 0))
964 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
965 (forward-char 1) nil
)))))
969 (setq expr
(or expr
(ruby-expr-beg)
970 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
971 (nth 1 (setq state
(apply 'ruby-parse-partial nil state
))))
973 (skip-chars-forward "<"))
976 ((error) (forward-word 1)))
979 (defun ruby-backward-sexp (&optional arg
)
980 "Move backward across one balanced expression (sexp).
981 With ARG, do it many times. Negative ARG means move forward."
982 ;; TODO: Document body
984 (if (and (numberp arg
) (< arg
0))
985 (ruby-forward-sexp (- arg
))
986 (let ((i (or arg
1)))
989 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
991 (cond ((looking-at "\\s)")
992 (goto-char (scan-sexps (1+ (point)) -
1))
994 (?%
(forward-char -
1))
995 ('(?q ?Q ?w ?W ?r ?x
)
996 (if (eq (char-before (1- (point))) ?%
) (forward-char -
2))))
998 ((looking-at "\\s\"\\|\\\\\\S_")
999 (let ((c (char-to-string (char-before (match-end 0)))))
1000 (while (and (search-backward c
)
1001 (eq (logand (skip-chars-backward "\\") 1)
1004 ((looking-at "\\s.\\|\\s\\")
1005 (if (ruby-special-char-p) (forward-char -
1)))
1006 ((looking-at "\\s(") nil
)
1009 (while (progn (forward-word -
1)
1012 (?.
(forward-char -
1) t
)
1015 (and (eq (char-before) (char-after)) (forward-char -
1)))
1018 (eq (char-before) :)))))
1019 (if (looking-at ruby-block-end-re
)
1020 (ruby-beginning-of-block))
1026 (defun ruby-mark-defun ()
1027 "Put mark at end of this Ruby function, point at beginning."
1031 (push-mark (point) nil t
)
1032 (ruby-beginning-of-defun)
1033 (re-search-backward "^\n" (- (point) 1) t
))
1035 (defun ruby-indent-exp (&optional shutup-p
)
1036 "Indent each line in the balanced expression following the point.
1037 If a prefix arg is given or SHUTUP-P is non-nil, no errors
1038 are signalled if a balanced expression isn't found."
1040 (let ((here (point-marker)) start top column
(nest t
))
1041 (set-marker-insertion-type here t
)
1045 (setq start
(point) top
(current-indentation))
1046 (while (and (not (eobp))
1048 (setq column
(ruby-calculate-indent start
))
1049 (cond ((> column top
)
1051 ((and (= column top
) nest
)
1052 (setq nest nil
) t
))))
1053 (ruby-indent-to column
)
1054 (beginning-of-line 2)))
1056 (set-marker here nil
))))
1058 (defun ruby-add-log-current-method ()
1059 "Return the current method name as a string.
1060 This string includes all namespaces.
1069 See `add-log-current-defun-function'."
1070 ;; TODO: Document body
1071 ;; Why does this append a period to class methods?
1074 (let (mname mlist
(indent 0))
1075 ;; get current method (or class/module)
1076 (if (re-search-backward
1077 (concat "^[ \t]*\\(def\\|class\\|module\\)[ \t]+"
1079 ;; \\. and :: for class method
1080 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1084 (setq mname
(match-string 2))
1085 (unless (string-equal "def" (match-string 1))
1086 (setq mlist
(list mname
) mname nil
))
1087 (goto-char (match-beginning 1))
1088 (setq indent
(current-column))
1089 (beginning-of-line)))
1090 ;; nest class/module
1091 (while (and (> indent
0)
1094 "^[ \t]*\\(class\\|module\\)[ \t]+"
1095 "\\([A-Z]" ruby-symbol-re
"*\\)")
1097 (goto-char (match-beginning 1))
1098 (if (< (current-column) indent
)
1100 (setq mlist
(cons (match-string 2) mlist
))
1101 (setq indent
(current-column))
1102 (beginning-of-line))))
1104 (let ((mn (split-string mname
"\\.\\|::")))
1108 ((string-equal "" (car mn
))
1109 (setq mn
(cdr mn
) mlist nil
))
1110 ((string-equal "self" (car mn
))
1112 ((let ((ml (nreverse mlist
)))
1114 (if (string-equal (car ml
) (car mn
))
1115 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1116 (or (setq ml
(cdr ml
)) (nreverse mlist
))))))
1118 (setcdr (last mlist
) mn
)
1120 (setq mn
(last mn
2))
1121 (setq mname
(concat "." (cadr mn
)))
1123 (setq mname
(concat "#" mname
)))))
1126 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1128 (if mlist
(concat mlist mname
) mname
)
1131 (defconst ruby-font-lock-syntactic-keywords
1132 `(;; #{ }, #$hoge, #@foo are not comments
1133 ("\\(#\\)[{$@]" 1 (1 . nil
))
1134 ;; the last $', $", $` in the respective string is not variable
1135 ;; the last ?', ?", ?` in the respective string is not ascii code
1136 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1139 ;; $' $" $` .... are variables
1140 ;; ?' ?" ?` are ascii codes
1141 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil
))
1143 ("\\(^\\|[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1146 ("^=en\\(d\\)\\_>" 1 "!")
1147 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1148 ;; Currently, the following case is highlighted incorrectly:
1157 ;; This is because all here-doc beginnings are highlighted before any endings,
1158 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1159 ;; it thinks <<BAR is part of a string so it's marked as well.
1161 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1162 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1163 ;; but I don't want to try that until we've got unit tests set up
1164 ;; to make sure I don't break anything else.
1165 (,(concat ruby-here-doc-beg-re
".*\\(\n\\)")
1166 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re
))
1167 (ruby-here-doc-beg-syntax))
1168 (,ruby-here-doc-end-re
3 (ruby-here-doc-end-syntax)))
1169 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1171 (defun ruby-comment-beg-syntax ()
1172 "Return the syntax cell for a the first character of a =begin.
1173 See the definition of `ruby-font-lock-syntactic-keywords'.
1175 This returns a comment-delimiter cell as long as the =begin
1176 isn't in a string or another comment."
1177 (when (not (nth 3 (syntax-ppss)))
1178 (string-to-syntax "!")))
1180 (unless (functionp 'syntax-ppss
)
1181 (defun syntax-ppss (&optional pos
)
1182 (parse-partial-sexp (point-min) (or pos
(point)))))
1184 (defun ruby-in-ppss-context-p (context &optional ppss
)
1185 (let ((ppss (or ppss
(syntax-ppss (point)))))
1187 ((eq context
'anything
)
1190 ((eq context
'string
)
1192 ((eq context
'heredoc
)
1194 ;; If it's generic string, it's a heredoc and we don't care
1195 ;; See `parse-partial-sexp'
1196 (not (numberp (nth 3 ppss
)))))
1197 ((eq context
'non-heredoc
)
1198 (and (ruby-in-ppss-context-p 'anything
)
1199 (not (ruby-in-ppss-context-p 'heredoc
))))
1200 ((eq context
'comment
)
1204 "Internal error on `ruby-in-ppss-context-p': "
1205 "context name `" (symbol-name context
) "' is unknown"))))
1208 (defun ruby-in-here-doc-p ()
1209 "Return whether or not the point is in a heredoc."
1211 (let ((old-point (point)) (case-fold-search nil
))
1214 (while (re-search-backward ruby-here-doc-beg-re nil t
)
1215 (if (not (or (ruby-in-ppss-context-p 'anything
)
1216 (ruby-here-doc-find-end old-point
)))
1217 (throw 'found-beg t
)))))))
1219 (defun ruby-here-doc-find-end (&optional limit
)
1220 "Expects the point to be on a line with one or more heredoc openers.
1221 Returns the buffer position at which all heredocs on the line
1222 are terminated, or nil if they aren't terminated before the
1223 buffer position `limit' or the end of the buffer."
1227 (let ((eol (save-excursion (end-of-line) (point)))
1228 (case-fold-search nil
)
1229 ;; Fake match data such that (match-end 0) is at eol
1230 (end-match-data (progn (looking-at ".*$") (match-data)))
1231 beg-match-data end-re
)
1232 (while (re-search-forward ruby-here-doc-beg-re eol t
)
1233 (setq beg-match-data
(match-data))
1234 (setq end-re
(ruby-here-doc-end-match))
1236 (set-match-data end-match-data
)
1237 (goto-char (match-end 0))
1238 (unless (re-search-forward end-re limit t
) (throw 'done nil
))
1239 (setq end-match-data
(match-data))
1241 (set-match-data beg-match-data
)
1242 (goto-char (match-end 0)))
1243 (set-match-data end-match-data
)
1244 (goto-char (match-end 0))
1247 (defun ruby-here-doc-beg-syntax ()
1248 "Return the syntax cell for a line that may begin a heredoc.
1249 See the definition of `ruby-font-lock-syntactic-keywords'.
1251 This sets the syntax cell for the newline ending the line
1252 containing the heredoc beginning so that cases where multiple
1253 heredocs are started on one line are handled correctly."
1255 (goto-char (match-beginning 0))
1256 (unless (or (ruby-in-ppss-context-p 'non-heredoc
)
1257 (ruby-in-here-doc-p))
1258 (string-to-syntax "|"))))
1260 (defun ruby-here-doc-end-syntax ()
1261 "Return the syntax cell for a line that may end a heredoc.
1262 See the definition of `ruby-font-lock-syntactic-keywords'."
1263 (let ((pss (syntax-ppss)) (case-fold-search nil
))
1264 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1265 ;; so we can just give up.
1266 ;; This means we aren't doing a full-document search
1267 ;; every time we enter a character.
1268 (when (ruby-in-ppss-context-p 'heredoc pss
)
1270 (goto-char (nth 8 pss
)) ; Go to the beginning of heredoc.
1271 (let ((eol (point)))
1273 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t
) ; If there is a heredoc that matches this line...
1274 (not (ruby-in-ppss-context-p 'anything
)) ; And that's not inside a heredoc/string/comment...
1275 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1276 (not (re-search-forward ruby-here-doc-beg-re eol t
))))
1277 (string-to-syntax "|")))))))
1279 (if (featurep 'xemacs
)
1280 (put 'ruby-mode
'font-lock-defaults
1281 '((ruby-font-lock-keywords)
1284 (font-lock-syntactic-keywords
1285 . ruby-font-lock-syntactic-keywords
))))
1287 (defvar ruby-font-lock-syntax-table
1288 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1289 (modify-syntax-entry ?_
"w" tbl
)
1291 "The syntax table to use for fontifying Ruby mode buffers.
1292 See `font-lock-syntax-table'.")
1294 (defconst ruby-font-lock-keywords
1297 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1298 1 font-lock-function-name-face
)
1301 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1345 ruby-keyword-end-re
)
1347 ;; here-doc beginnings
1348 (list ruby-here-doc-beg-re
0 'font-lock-string-face
)
1350 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1351 2 font-lock-variable-name-face
)
1353 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1354 1 font-lock-variable-name-face
)
1355 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1356 0 font-lock-variable-name-face
)
1357 ;; general delimited string
1358 '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1359 (2 font-lock-string-face
))
1361 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1362 2 font-lock-type-face
)
1364 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|\\[\\]=?\\|\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1365 2 font-lock-reference-face
)
1366 ;; expression expansion
1367 '("#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)"
1368 0 font-lock-variable-name-face t
)
1369 ;; warn lower camel case
1370 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1371 ; 0 font-lock-warning-face)
1373 "Additional expressions to highlight in Ruby mode.")
1377 "Major mode for editing Ruby scripts.
1378 \\[ruby-indent-line] properly indents subexpressions of multi-line
1379 class, module, def, if, while, for, do, and case statements, taking
1380 nesting into account.
1382 The variable `ruby-indent-level' controls the amount of indentation.
1386 (kill-all-local-variables)
1387 (use-local-map ruby-mode-map
)
1388 (setq mode-name
"Ruby")
1389 (setq major-mode
'ruby-mode
)
1390 (ruby-mode-variables)
1392 (set (make-local-variable 'imenu-create-index-function
)
1393 'ruby-imenu-create-index
)
1394 (set (make-local-variable 'add-log-current-defun-function
)
1395 'ruby-add-log-current-method
)
1398 (cond ((boundp 'before-save-hook
)
1399 (make-local-variable 'before-save-hook
)
1401 ((boundp 'write-contents-functions
) 'write-contents-functions
)
1402 ((boundp 'write-contents-hooks
) 'write-contents-hooks
))
1403 'ruby-mode-set-encoding
)
1405 (set (make-local-variable 'font-lock-defaults
)
1406 '((ruby-font-lock-keywords) nil nil
))
1407 (set (make-local-variable 'font-lock-keywords
)
1408 ruby-font-lock-keywords
)
1409 (set (make-local-variable 'font-lock-syntax-table
)
1410 ruby-font-lock-syntax-table
)
1411 (set (make-local-variable 'font-lock-syntactic-keywords
)
1412 ruby-font-lock-syntactic-keywords
)
1414 (if (fboundp 'run-mode-hooks
)
1415 (run-mode-hooks 'ruby-mode-hook
)
1416 (run-hooks 'ruby-mode-hook
)))
1418 ;;; Invoke ruby-mode when appropriate
1421 (add-to-list 'auto-mode-alist
'("\\.rb\\'" . ruby-mode
))
1424 (add-to-list 'interpreter-mode-alist
'("ruby" . ruby-mode
))
1425 (add-to-list 'interpreter-mode-alist
'("rbx" . ruby-mode
))
1426 (add-to-list 'interpreter-mode-alist
'("jruby" . ruby-mode
))
1427 (add-to-list 'interpreter-mode-alist
'("ruby1.9" . ruby-mode
))
1428 (add-to-list 'interpreter-mode-alist
'("ruby1.8" . ruby-mode
))
1430 (provide 'ruby-mode
)
1432 ;; arch-tag: e6ecc893-8005-420c-b7f9-34ab99a1fff9
1433 ;;; ruby-mode.el ends here