1 ;;; ruby-mode.el --- Major mode for editing Ruby files
3 ;; Copyright (C) 1994, 1995, 1996 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
7 ;; Authors: Yukihiro Matsumoto
9 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/RubyMode
10 ;; Created: Fri Feb 4 14:49:13 JST 1994
11 ;; Keywords: languages ruby
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; Provides font-locking, indentation support, and navigation for Ruby code.
33 ;; If you're installing manually, you should add this to your .emacs
34 ;; file after putting it on your load path:
36 ;; (autoload 'ruby-mode "ruby-mode" "Major mode for ruby files" t)
37 ;; (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
38 ;; (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
40 ;; Still needs more docstrings; search below for TODO.
44 (eval-when-compile (require 'cl))
47 "Major mode for editing Ruby code."
51 (defconst ruby-keyword-end-re
52 (if (string-match "\\_>" "ruby")
56 (defconst ruby-block-beg-keywords
57 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
58 "Keywords at the beginning of blocks.")
60 (defconst ruby-block-beg-re
61 (regexp-opt ruby-block-beg-keywords)
62 "Regexp to match the beginning of blocks.")
64 (defconst ruby-non-block-do-re
65 (concat (regexp-opt '("while" "until" "for" "rescue") t) ruby-keyword-end-re)
66 "Regexp to match keywords that nest without blocks.")
68 (defconst ruby-indent-beg-re
69 (concat "\\(\\s *" (regexp-opt '("class" "module" "def") t) "\\)\\|"
70 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin")))
71 "Regexp to match where the indentation gets deeper.")
73 (defconst ruby-modifier-beg-keywords
74 '("if" "unless" "while" "until")
75 "Modifiers that are the same as the beginning of blocks.")
77 (defconst ruby-modifier-beg-re
78 (regexp-opt ruby-modifier-beg-keywords)
79 "Regexp to match modifiers same as the beginning of blocks.")
81 (defconst ruby-modifier-re
82 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords))
83 "Regexp to match modifiers.")
85 (defconst ruby-block-mid-keywords
86 '("then" "else" "elsif" "when" "rescue" "ensure")
87 "Keywords where the indentation gets shallower in middle of block statements.")
89 (defconst ruby-block-mid-re
90 (regexp-opt ruby-block-mid-keywords)
91 "Regexp to match where the indentation gets shallower in middle of block statements.")
93 (defconst ruby-block-op-keywords
95 "Regexp to match boolean keywords.")
97 (defconst ruby-block-hanging-re
98 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords))
99 "Regexp to match hanging block modifiers.")
101 (defconst ruby-block-end-re "\\<end\\>")
104 (defconst ruby-here-doc-beg-re
105 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
106 "Regexp to match the beginning of a heredoc."))
108 (defun ruby-here-doc-end-match ()
109 "Return a regexp to find the end of a heredoc.
111 This should only be called after matching against `ruby-here-doc-beg-re'."
113 (if (match-string 2) "[ \t]*" nil)
119 (defconst ruby-delimiter
120 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\<\\("
122 "\\)\\>\\|" ruby-block-end-re
123 "\\|^=begin\\|" ruby-here-doc-beg-re))
125 (defconst ruby-negative
126 (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\>\\|"
127 ruby-block-end-re "\\|}\\|\\]\\)")
128 "Regexp to match where the indentation gets shallower.")
130 (defconst ruby-operator-re "[-,.+*/%&|^~=<>:]"
131 "Regexp to match operators.")
133 (defconst ruby-symbol-chars "a-zA-Z0-9_"
134 "List of characters that symbol names may contain.")
135 (defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]")
136 "Regexp to match symbols.")
138 (define-abbrev-table 'ruby-mode-abbrev-table ()
139 "Abbrev table in use in Ruby mode buffers.")
141 (defvar ruby-mode-map
142 (let ((map (make-sparse-keymap)))
143 (define-key map "{" 'ruby-electric-brace)
144 (define-key map "}" 'ruby-electric-brace)
145 (define-key map (kbd "M-C-a") 'ruby-beginning-of-defun)
146 (define-key map (kbd "M-C-e") 'ruby-end-of-defun)
147 (define-key map (kbd "M-C-b") 'ruby-backward-sexp)
148 (define-key map (kbd "M-C-f") 'ruby-forward-sexp)
149 (define-key map (kbd "M-C-p") 'ruby-beginning-of-block)
150 (define-key map (kbd "M-C-n") 'ruby-end-of-block)
151 (define-key map (kbd "M-C-h") 'ruby-mark-defun)
152 (define-key map (kbd "M-C-q") 'ruby-indent-exp)
153 (define-key map (kbd "C-M-h") 'backward-kill-word)
154 (define-key map (kbd "C-j") 'reindent-then-newline-and-indent)
155 (define-key map (kbd "C-m") 'newline)
156 (define-key map (kbd "C-c C-c") 'comment-region)
158 "Keymap used in Ruby mode.")
160 (defvar ruby-mode-syntax-table
161 (let ((table (make-syntax-table)))
162 (modify-syntax-entry ?\' "\"" table)
163 (modify-syntax-entry ?\" "\"" table)
164 (modify-syntax-entry ?\` "\"" table)
165 (modify-syntax-entry ?# "<" table)
166 (modify-syntax-entry ?\n ">" table)
167 (modify-syntax-entry ?\\ "\\" table)
168 (modify-syntax-entry ?$ "." table)
169 (modify-syntax-entry ?? "_" table)
170 (modify-syntax-entry ?_ "_" table)
171 (modify-syntax-entry ?< "." table)
172 (modify-syntax-entry ?> "." table)
173 (modify-syntax-entry ?& "." table)
174 (modify-syntax-entry ?| "." table)
175 (modify-syntax-entry ?% "." table)
176 (modify-syntax-entry ?= "." 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 ?\; "." 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)
189 "Syntax table to use in Ruby mode.")
191 (defcustom ruby-indent-tabs-mode nil
192 "Indentation can insert tabs in Ruby mode if this is non-nil."
193 :type 'boolean :group 'ruby)
195 (defcustom ruby-indent-level 2
196 "Indentation of Ruby statements."
197 :type 'integer :group 'ruby)
199 (defcustom ruby-comment-column 32
200 "Indentation column of comments."
201 :type 'integer :group 'ruby)
203 (defcustom ruby-deep-arglist t
204 "Deep indent lists in parenthesis when non-nil.
205 Also ignores spaces after parenthesis when 'space."
208 (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
209 "Deep indent lists in parenthesis when non-nil.
210 The value t means continuous line.
211 Also ignores spaces after parenthesis when 'space."
214 (defcustom ruby-deep-indent-paren-style 'space
215 "Default deep indent style."
216 :options '(t nil space) :group 'ruby)
218 (defcustom ruby-encoding-map '((shift_jis . cp932) (shift-jis . cp932))
219 "Alist to map encoding name from Emacs to Ruby."
222 (defcustom ruby-insert-encoding-magic-comment t
223 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
224 :type 'boolean :group 'ruby)
226 (defcustom ruby-use-encoding-map t
227 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
228 :type 'boolean :group 'ruby)
230 ;; Safe file variables
231 (put 'ruby-indent-tabs-mode 'safe-local-variable 'booleanp)
232 (put 'ruby-indent-level 'safe-local-variable 'integerp)
233 (put 'ruby-comment-column 'safe-local-variable 'integerp)
234 (put 'ruby-deep-arglist 'safe-local-variable 'booleanp)
236 (defun ruby-imenu-create-index-in-block (prefix beg end)
237 "Create an imenu index of methods inside a block."
238 (let ((index-alist '()) (case-fold-search nil)
239 name next pos decl sing)
241 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
242 (setq sing (match-beginning 3))
243 (setq decl (match-string 5))
244 (setq next (match-end 0))
245 (setq name (or (match-string 4) (match-string 6)))
246 (setq pos (match-beginning 0))
248 ((string= "alias" decl)
249 (if prefix (setq name (concat prefix name)))
250 (push (cons name pos) index-alist))
251 ((string= "def" decl)
255 ((string-match "^self\." name)
256 (concat (substring prefix 0 -1) (substring name 4)))
257 (t (concat prefix name)))))
258 (push (cons name pos) index-alist)
259 (ruby-accurate-end-of-block end))
261 (if (string= "self" name)
262 (if prefix (setq name (substring prefix 0 -1)))
263 (if prefix (setq name (concat (substring prefix 0 -1) "::" name)))
264 (push (cons name pos) index-alist))
265 (ruby-accurate-end-of-block end)
268 (nconc (ruby-imenu-create-index-in-block
269 (concat name (if sing "." "#"))
270 next beg) index-alist))
274 (defun ruby-imenu-create-index ()
275 "Create an imenu index of all methods in the buffer."
276 (nreverse (ruby-imenu-create-index-in-block nil (point-min) nil)))
278 (defun ruby-accurate-end-of-block (&optional end)
281 (end (or end (point-max))))
282 (while (and (setq state (apply 'ruby-parse-partial end state))
283 (>= (nth 2 state) 0) (< (point) end)))))
285 (defun ruby-mode-variables ()
286 "Set up initial buffer-local variables for Ruby mode."
287 (set-syntax-table ruby-mode-syntax-table)
288 (setq local-abbrev-table ruby-mode-abbrev-table)
289 (setq indent-tabs-mode ruby-indent-tabs-mode)
290 (set (make-local-variable 'indent-line-function) 'ruby-indent-line)
291 (set (make-local-variable 'require-final-newline) t)
292 (set (make-local-variable 'comment-start) "# ")
293 (set (make-local-variable 'comment-end) "")
294 (set (make-local-variable 'comment-column) ruby-comment-column)
295 (set (make-local-variable 'comment-start-skip) "#+ *")
296 (set (make-local-variable 'parse-sexp-ignore-comments) t)
297 (set (make-local-variable 'parse-sexp-lookup-properties) t)
298 (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
299 (set (make-local-variable 'paragraph-separate) paragraph-start)
300 (set (make-local-variable 'paragraph-ignore-fill-prefix) t))
302 (defun ruby-mode-set-encoding ()
303 "Insert a magic comment header with the proper encoding if necessary."
306 (goto-char (point-min))
307 (when (re-search-forward "[^\0-\177]" nil t)
308 (goto-char (point-min))
310 (or coding-system-for-write
311 buffer-file-coding-system)))
314 (or (coding-system-get coding-system 'mime-charset)
315 (coding-system-change-eol-conversion coding-system nil))))
319 (or (and ruby-use-encoding-map
320 (cdr (assq coding-system ruby-encoding-map)))
323 (if (looking-at "^#!") (beginning-of-line 2))
324 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
325 (unless (string= (match-string 2) coding-system)
326 (goto-char (match-beginning 2))
327 (delete-region (point) (match-end 2))
328 (and (looking-at "-\*-")
329 (let ((n (skip-chars-backward " ")))
330 (cond ((= n 0) (insert " ") (backward-char))
331 ((= n -1) (insert " "))
333 (insert coding-system)))
334 ((looking-at "\\s *#.*coding\\s *[:=]"))
335 (t (when ruby-insert-encoding-magic-comment
336 (insert "# -*- coding: " coding-system " -*-\n"))))))))
338 (defun ruby-current-indentation ()
339 "Return the indentation level of current line."
342 (back-to-indentation)
345 (defun ruby-indent-line (&optional ignored)
346 "Correct the indentation of the current Ruby line."
348 (ruby-indent-to (ruby-calculate-indent)))
350 (defun ruby-indent-to (column)
351 "Indent the current line to COLUMN."
354 (and (< column 0) (error "invalid nest"))
355 (setq shift (current-column))
358 (back-to-indentation)
359 (setq top (current-column))
360 (skip-chars-backward " \t")
361 (if (>= shift top) (setq shift (- shift top))
365 (move-to-column (+ column shift))
367 (delete-region beg (point))
370 (move-to-column (+ column shift))))))
372 (defun ruby-special-char-p (&optional pos)
373 "Return t if the character before POS is a special character.
374 If omitted, POS defaults to the current point.
375 Special characters are `?', `$', `:' when preceded by whitespace,
376 and `\\' when preceded by `?'."
377 (setq pos (or pos (point)))
378 (let ((c (char-before pos)) (b (and (< (point-min) pos)
379 (char-before (1- pos)))))
380 (cond ((or (eq c ??) (eq c ?$)))
381 ((and (eq c ?:) (or (not b) (eq (char-syntax b) ? ))))
382 ((eq c ?\\) (eq b ??)))))
384 (defun ruby-expr-beg (&optional option)
387 (store-match-data nil)
388 (let ((space (skip-chars-backward " \t")))
393 (and (looking-at "\\?")
394 (or (eq (char-syntax (char-before (point))) ?w)
395 (ruby-special-char-p))))
397 ((and (eq option 'heredoc) (< space 0)) t)
398 ((or (looking-at ruby-operator-re)
399 (looking-at "[\\[({,;]")
400 (and (looking-at "[!?]")
401 (or (not (eq option 'modifier))
403 (save-excursion (forward-char -1) (looking-at "\\Sw$"))))
404 (and (looking-at ruby-symbol-re)
405 (skip-chars-backward ruby-symbol-chars)
407 ((looking-at (regexp-opt
408 (append ruby-block-beg-keywords
409 ruby-block-op-keywords
410 ruby-block-mid-keywords)
412 (goto-char (match-end 0))
413 (not (looking-at "\\s_")))
414 ((eq option 'expr-qstr)
415 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
416 ((eq option 'expr-re)
417 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
420 (defun ruby-forward-string (term &optional end no-error expand)
422 (let ((n 1) (c (string-to-char term))
424 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
425 (concat "[^\\]\\(\\\\\\\\\\)*[" term "]"))))
426 (while (and (re-search-forward re end no-error)
427 (if (match-beginning 3)
428 (ruby-forward-string "}{" end no-error nil)
429 (> (setq n (if (eq (char-before (point)) c)
434 ((error "unterminated string")))))
436 (defun ruby-deep-indent-paren-p (c)
438 (cond ((listp ruby-deep-indent-paren)
439 (let ((deep (assoc c ruby-deep-indent-paren)))
441 (or (cdr deep) ruby-deep-indent-paren-style))
442 ((memq c ruby-deep-indent-paren)
443 ruby-deep-indent-paren-style))))
444 ((eq c ruby-deep-indent-paren) ruby-deep-indent-paren-style)
445 ((eq c ?\( ) ruby-deep-arglist)))
447 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent)
448 "TODO: document throughout function body."
449 (or depth (setq depth 0))
450 (or indent (setq indent 0))
451 (when (re-search-forward ruby-delimiter end 'move)
452 (let ((pnt (point)) w re expand)
453 (goto-char (match-beginning 0))
455 ((and (memq (char-before) '(?@ ?$)) (looking-at "\\sw"))
457 ((looking-at "[\"`]") ;skip string
460 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t))
463 (setq in-string (point))
468 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t))
471 (setq in-string (point))
477 ((and (not (eobp)) (ruby-expr-beg 'expr-re))
478 (if (ruby-forward-string "/" end t t)
480 (setq in-string (point))
487 (ruby-expr-beg 'expr-qstr)
488 (not (looking-at "%="))
489 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
490 (goto-char (match-beginning 1))
491 (setq expand (not (memq (char-before) '(?q ?w))))
492 (setq w (match-string 1))
494 ((string= w "[") (setq re "]["))
495 ((string= w "{") (setq re "}{"))
496 ((string= w "(") (setq re ")("))
497 ((string= w "<") (setq re "><"))
498 ((and expand (string= w "\\"))
499 (setq w (concat "\\" w))))
500 (unless (cond (re (ruby-forward-string re end t expand))
501 (expand (ruby-forward-string w end t t))
502 (t (re-search-forward
505 (concat "[^\\]\\(\\\\\\\\\\)*" w))
507 (setq in-string (point))
511 ((looking-at "\\?") ;skip ?char
513 ((and (ruby-expr-beg)
514 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
515 (goto-char (match-end 0)))
518 ((looking-at "\\$") ;skip $char
521 ((looking-at "#") ;skip comment
525 ((looking-at "[\\[{(]")
526 (let ((deep (ruby-deep-indent-paren-p (char-after))))
527 (if (and deep (or (not (eq (char-after) ?\{)) (ruby-expr-beg)))
529 (and (eq deep 'space) (looking-at ".\\s +[^# \t\n]")
530 (setq pnt (1- (match-end 0))))
531 (setq nest (cons (cons (char-after (point)) pnt) nest))
532 (setq pcol (cons (cons pnt depth) pcol))
534 (setq nest (cons (cons (char-after (point)) pnt) nest))
535 (setq depth (1+ depth))))
538 ((looking-at "[])}]")
539 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
540 (setq depth (cdr (car pcol)) pcol (cdr pcol))
541 (setq depth (1- depth)))
542 (setq nest (cdr nest))
544 ((looking-at ruby-block-end-re)
545 (if (or (and (not (bolp))
548 (setq w (char-after (point)))
553 (setq w (char-after (point)))
558 (setq nest (cdr nest))
559 (setq depth (1- depth)))
561 ((looking-at "def\\s +[^(\n;]*")
565 (not (eq ?_ (char-after (point))))))
567 (setq nest (cons (cons nil pnt) nest))
568 (setq depth (1+ depth))))
569 (goto-char (match-end 0)))
570 ((looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
573 (or (not (looking-at (concat "do" ruby-keyword-end-re)))
575 (back-to-indentation)
576 (not (looking-at ruby-non-block-do-re)))))
580 (setq w (char-after (point)))
584 (setq w (char-after (point)))
588 (skip-chars-forward " \t")
589 (goto-char (match-beginning 0))
590 (or (not (looking-at ruby-modifier-re))
591 (ruby-expr-beg 'modifier))
593 (setq nest (cons (cons nil pnt) nest))
594 (setq depth (1+ depth)))
596 ((looking-at ":\\(['\"]\\)")
597 (goto-char (match-beginning 1))
598 (ruby-forward-string (buffer-substring (match-beginning 1) (match-end 1)) end))
599 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
600 (goto-char (match-end 0)))
601 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
602 (goto-char (match-end 0)))
603 ((or (looking-at "\\.\\.\\.?")
604 (looking-at "\\.[0-9]+")
605 (looking-at "\\.[a-zA-Z_0-9]+")
607 (goto-char (match-end 0)))
608 ((looking-at "^=begin")
609 (if (re-search-forward "^=end" end t)
611 (setq in-string (match-end 0))
615 ((and (ruby-expr-beg 'heredoc)
616 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
617 (setq re (regexp-quote (or (match-string 4) (match-string 2))))
618 (if (match-beginning 1) (setq re (concat "\\s *" re)))
619 (let* ((id-end (goto-char (match-end 0)))
620 (line-end-position (point-at-eol))
621 (state (list in-string nest depth pcol indent)))
622 ;; parse the rest of the line
623 (while (and (> line-end-position (point))
624 (setq state (apply 'ruby-parse-partial
625 line-end-position state))))
626 (setq in-string (car state)
630 indent (nth 4 state))
631 ;; skip heredoc section
632 (if (re-search-forward (concat "^" re "$") end 'move)
634 (setq in-string id-end)
638 ((looking-at "^__END__$")
640 ((and (looking-at ruby-here-doc-beg-re)
641 (boundp 'ruby-indent-point))
642 (if (re-search-forward (ruby-here-doc-end-match)
645 (setq in-string (match-end 0))
646 (goto-char ruby-indent-point)))
648 (error (format "bad string %s"
649 (buffer-substring (point) pnt)
651 (list in-string nest depth pcol))
653 (defun ruby-parse-region (start end)
659 (ruby-beginning-of-indent))
661 (narrow-to-region (point) end)
662 (while (and (> end (point))
663 (setq state (apply 'ruby-parse-partial end state))))))
664 (list (nth 0 state) ; in-string
665 (car (nth 1 state)) ; nest
666 (nth 2 state) ; depth
667 (car (car (nth 3 state))) ; pcol
668 ;(car (nth 5 state)) ; indent
671 (defun ruby-indent-size (pos nest)
672 "Return the indentation level in spaces NEST levels deeper than POS."
673 (+ pos (* (or nest 1) ruby-indent-level)))
675 (defun ruby-calculate-indent (&optional parse-start)
676 "Return the proper indentation level of the current line."
677 ;; TODO: Document body
680 (let ((ruby-indent-point (point))
681 (case-fold-search nil)
682 state eol begin op-end
683 (paren (progn (skip-syntax-forward " ")
684 (and (char-after) (matching-paren (char-after)))))
687 (goto-char parse-start)
688 (ruby-beginning-of-indent)
689 (setq parse-start (point)))
690 (back-to-indentation)
691 (setq indent (current-column))
692 (setq state (ruby-parse-region parse-start ruby-indent-point))
694 ((nth 0 state) ; within string
695 (setq indent nil)) ; do nothing
696 ((car (nth 1 state)) ; in paren
697 (goto-char (setq begin (cdr (nth 1 state))))
698 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state)))))
700 (cond ((and (eq deep t) (eq (car (nth 1 state)) paren))
701 (skip-syntax-backward " ")
702 (setq indent (1- (current-column))))
703 ((let ((s (ruby-parse-region (point) ruby-indent-point)))
704 (and (nth 2 s) (> (nth 2 s) 0)
705 (or (goto-char (cdr (nth 1 s))) t)))
707 (setq indent (ruby-indent-size (current-column)
710 (setq indent (current-column))
711 (cond ((eq deep 'space))
712 (paren (setq indent (1- indent)))
713 (t (setq indent (ruby-indent-size (1- indent) 1))))))
714 (if (nth 3 state) (goto-char (nth 3 state))
715 (goto-char parse-start) (back-to-indentation))
716 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
717 (and (eq (car (nth 1 state)) paren)
718 (ruby-deep-indent-paren-p (matching-paren paren))
719 (search-backward (char-to-string paren))
720 (setq indent (current-column)))))
721 ((and (nth 2 state) (> (nth 2 state) 0)) ; in nest
722 (if (null (cdr (nth 1 state)))
723 (error "invalid nest"))
724 (goto-char (cdr (nth 1 state)))
725 (forward-word -1) ; skip back a keyword
728 ((looking-at "do\\>[^_]") ; iter block is a special case
729 (if (nth 3 state) (goto-char (nth 3 state))
730 (goto-char parse-start) (back-to-indentation))
731 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
733 (setq indent (+ (current-column) ruby-indent-level)))))
735 ((and (nth 2 state) (< (nth 2 state) 0)) ; in negative nest
736 (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
738 (goto-char ruby-indent-point)
743 ((and (not (ruby-deep-indent-paren-p paren))
744 (re-search-forward ruby-negative eol t))
745 (and (not (eq ?_ (char-after (match-end 0))))
746 (setq indent (- indent ruby-indent-level))))
751 (or (ruby-deep-indent-paren-p t)
752 (null (car (nth 1 state)))))
753 ;; goto beginning of non-empty no-comment line
756 (skip-chars-backward " \t\n")
759 (if (re-search-forward "^\\s *#" end t)
763 ;; skip the comment at the end
764 (skip-chars-backward " \t")
765 (let (end (pos (point)))
767 (while (and (re-search-forward "#" pos t)
768 (setq end (1- (point)))
769 (or (ruby-special-char-p end)
770 (and (setq state (ruby-parse-region parse-start end))
773 (goto-char (or end pos))
774 (skip-chars-backward " \t")
775 (setq begin (if (and end (nth 0 state)) pos (cdr (nth 1 state))))
776 (setq state (ruby-parse-region parse-start (point))))
777 (or (bobp) (forward-char -1))
779 (or (and (looking-at ruby-symbol-re)
780 (skip-chars-backward ruby-symbol-chars)
781 (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>"))
782 (not (eq (point) (nth 3 state)))
784 (goto-char (match-end 0))
785 (not (looking-at "[a-z_]"))))
786 (and (looking-at ruby-operator-re)
787 (not (ruby-special-char-p))
788 ;; operator at the end of line
789 (let ((c (char-after (point))))
794 ;; (skip-chars-forward " \t")
795 ;; (not (or (eolp) (looking-at "#")
796 ;; (and (eq (car (nth 1 state)) ?{)
797 ;; (looking-at "|"))))))
799 (null (nth 0 (ruby-parse-region (or begin parse-start) (point)))))
800 (or (not (eq ?| (char-after (point))))
802 (or (eolp) (forward-char -1))
804 ((search-backward "|" nil t)
805 (skip-chars-backward " \t\n")
809 (not (looking-at "{")))
812 (not (looking-at "do\\>[^_]")))))
820 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>")))
821 (eq (ruby-deep-indent-paren-p t) 'space)
824 (goto-char (or begin parse-start))
825 (skip-syntax-forward " ")
827 ((car (nth 1 state)) indent)
829 (+ indent ruby-indent-level))))))))
830 (goto-char ruby-indent-point)
832 (skip-syntax-forward " ")
833 (if (looking-at "\\.[^.]")
834 (+ indent ruby-indent-level)
837 (defun ruby-electric-brace (arg)
838 "Insert a brace and re-indent the current line."
840 (self-insert-command (prefix-numeric-value arg))
841 (ruby-indent-line t))
843 ;; TODO: Why isn't one ruby-*-of-defun written in terms of the other?
844 (defun ruby-beginning-of-defun (&optional arg)
845 "Move backward to the beginning of the current top-level defun.
846 With ARG, move backward multiple defuns. Negative ARG means
849 (and (re-search-backward (concat "^\\(" ruby-block-beg-re "\\)\\b")
850 nil 'move (or arg 1))
851 (beginning-of-line)))
853 (defun ruby-end-of-defun (&optional arg)
854 "Move forward to the end of the current top-level defun.
855 With ARG, move forward multiple defuns. Negative ARG means
858 (and (re-search-forward (concat "^\\(" ruby-block-end-re "\\)\\($\\|\\b[^_]\\)")
859 nil 'move (or arg 1))
863 (defun ruby-beginning-of-indent ()
865 ;; I don't understand this function.
866 ;; It seems like it should move to the line where indentation should deepen,
867 ;; but ruby-indent-beg-re only accounts for whitespace before class, module and def,
868 ;; so this will only match other block beginners at the beginning of the line.
869 (and (re-search-backward (concat "^\\(" ruby-indent-beg-re "\\)\\b") nil 'move)
870 (beginning-of-line)))
872 (defun ruby-move-to-block (n)
873 "Move to the beginning (N < 0) or the end (N > 0) of the current block
874 or blocks containing the current block."
875 ;; TODO: Make this work for n > 1,
876 ;; make it not loop for n = 0,
878 (let (start pos done down)
879 (setq start (ruby-calculate-indent))
880 (setq down (looking-at (if (< n 0) ruby-block-end-re
881 (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))))
882 (while (and (not done) (not (if (< n 0) (bobp) (eobp))))
885 ((looking-at "^\\s *$"))
886 ((looking-at "^\\s *#"))
887 ((and (> n 0) (looking-at "^=begin\\>"))
888 (re-search-forward "^=end\\>"))
889 ((and (< n 0) (looking-at "^=end\\>"))
890 (re-search-backward "^=begin\\>"))
892 (setq pos (current-indentation))
896 ((and down (= pos start))
902 (back-to-indentation)
903 (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>"))
905 (back-to-indentation))
907 (defun ruby-beginning-of-block (&optional arg)
908 "Move backward to the beginning of the current block.
909 With ARG, move up multiple blocks."
911 (ruby-move-to-block (- (or arg 1))))
913 (defun ruby-end-of-block (&optional arg)
914 "Move forward to the end of the current block.
915 With ARG, move out of multiple blocks."
916 ;; Passing a value > 1 to ruby-move-to-block currently doesn't work.
918 (ruby-move-to-block (or arg 1)))
920 (defun ruby-forward-sexp (&optional arg)
921 "Move forward across one balanced expression (sexp).
922 With ARG, do it many times. Negative ARG means move backward."
923 ;; TODO: Document body
925 (if (and (numberp arg) (< arg 0))
926 (ruby-backward-sexp (- arg))
927 (let ((i (or arg 1)))
930 (skip-syntax-forward " ")
931 (if (looking-at ",\\s *") (goto-char (match-end 0)))
932 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
933 (goto-char (match-end 0)))
935 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
937 (goto-char (scan-sexps (point) 1)))
938 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
939 (not (eq (char-before (point)) ?.))
940 (not (eq (char-before (point)) ?:)))
943 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
945 (while (progn (forward-word 1) (looking-at "_")))
946 (cond ((looking-at "::") (forward-char 2) t)
947 ((> (skip-chars-forward ".") 0))
948 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
949 (forward-char 1) nil)))))
953 (setq expr (or expr (ruby-expr-beg)
954 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
955 (nth 1 (setq state (apply 'ruby-parse-partial nil state))))
957 (skip-chars-forward "<"))
960 ((error) (forward-word 1)))
963 (defun ruby-backward-sexp (&optional arg)
964 "Move backward across one balanced expression (sexp).
965 With ARG, do it many times. Negative ARG means move forward."
966 ;; TODO: Document body
968 (if (and (numberp arg) (< arg 0))
969 (ruby-forward-sexp (- arg))
970 (let ((i (or arg 1)))
973 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
975 (cond ((looking-at "\\s)")
976 (goto-char (scan-sexps (1+ (point)) -1))
978 (?% (forward-char -1))
979 ('(?q ?Q ?w ?W ?r ?x)
980 (if (eq (char-before (1- (point))) ?%) (forward-char -2))))
982 ((looking-at "\\s\"\\|\\\\\\S_")
983 (let ((c (char-to-string (char-before (match-end 0)))))
984 (while (and (search-backward c)
985 (eq (logand (skip-chars-backward "\\") 1)
988 ((looking-at "\\s.\\|\\s\\")
989 (if (ruby-special-char-p) (forward-char -1)))
990 ((looking-at "\\s(") nil)
993 (while (progn (forward-word -1)
996 (?. (forward-char -1) t)
999 (and (eq (char-before) (char-after)) (forward-char -1)))
1002 (eq (char-before) :)))))
1003 (if (looking-at ruby-block-end-re)
1004 (ruby-beginning-of-block))
1010 (defun ruby-mark-defun ()
1011 "Put mark at end of this Ruby function, point at beginning."
1015 (push-mark (point) nil t)
1016 (ruby-beginning-of-defun)
1017 (re-search-backward "^\n" (- (point) 1) t))
1019 (defun ruby-indent-exp (&optional ignored)
1020 "Indent each line in the balanced expression following the point."
1022 (let ((here (point-marker)) start top column (nest t))
1023 (set-marker-insertion-type here t)
1027 (setq start (point) top (current-indentation))
1028 (while (and (not (eobp))
1030 (setq column (ruby-calculate-indent start))
1031 (cond ((> column top)
1033 ((and (= column top) nest)
1034 (setq nest nil) t))))
1035 (ruby-indent-to column)
1036 (beginning-of-line 2)))
1038 (set-marker here nil))))
1040 (defun ruby-add-log-current-method ()
1041 "Return the current method name as a string.
1042 This string includes all namespaces.
1051 See `add-log-current-defun-function'."
1052 ;; TODO: Document body
1053 ;; Why does this append a period to class methods?
1056 (let (mname mlist (indent 0))
1057 ;; get current method (or class/module)
1058 (if (re-search-backward
1059 (concat "^[ \t]*\\(def\\|class\\|module\\)[ \t]+"
1061 ;; \\. and :: for class method
1062 "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
1066 (setq mname (match-string 2))
1067 (unless (string-equal "def" (match-string 1))
1068 (setq mlist (list mname) mname nil))
1069 (goto-char (match-beginning 1))
1070 (setq indent (current-column))
1071 (beginning-of-line)))
1072 ;; nest class/module
1073 (while (and (> indent 0)
1076 "^[ \t]*\\(class\\|module\\)[ \t]+"
1077 "\\([A-Z]" ruby-symbol-re "*\\)")
1079 (goto-char (match-beginning 1))
1080 (if (< (current-column) indent)
1082 (setq mlist (cons (match-string 2) mlist))
1083 (setq indent (current-column))
1084 (beginning-of-line))))
1086 (let ((mn (split-string mname "\\.\\|::")))
1090 ((string-equal "" (car mn))
1091 (setq mn (cdr mn) mlist nil))
1092 ((string-equal "self" (car mn))
1094 ((let ((ml (nreverse mlist)))
1096 (if (string-equal (car ml) (car mn))
1097 (setq mlist (nreverse (cdr ml)) ml nil))
1098 (or (setq ml (cdr ml)) (nreverse mlist))))))
1100 (setcdr (last mlist) mn)
1102 (setq mn (last mn 2))
1103 (setq mname (concat "." (cadr mn)))
1105 (setq mname (concat "#" mname)))))
1108 (setq mlist (mapconcat (function identity) mlist "::")))
1110 (if mlist (concat mlist mname) mname)
1113 (declare-function ruby-syntax-propertize-heredoc "ruby-mode" (limit))
1115 (if (eval-when-compile (fboundp #'syntax-propertize-rules))
1116 ;; New code that works independently from font-lock.
1118 (defun ruby-syntax-propertize-function (start end)
1119 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1121 (ruby-syntax-propertize-heredoc end)
1123 (syntax-propertize-rules
1124 ;; #{ }, #$hoge, #@foo are not comments
1125 ("\\(#\\)[{$@]" (1 "."))
1126 ;; $' $" $` .... are variables
1127 ;; ?' ?" ?` are ascii codes
1128 ("\\([?$]\\)[#\"'`]"
1129 (1 (unless (save-excursion
1130 ;; Not within a string.
1131 (nth 3 (syntax-ppss (match-beginning 0))))
1132 (string-to-syntax "\\"))))
1134 ("\\(^\\|[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1137 ("^=en\\(d\\)\\_>" (1 "!"))
1138 ("^\\(=\\)begin\\_>" (1 "!"))
1139 ;; Handle here documents.
1140 ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
1141 (7 (prog1 "\"" (ruby-syntax-propertize-heredoc end)))))
1144 (defun ruby-syntax-propertize-heredoc (limit)
1145 (let ((ppss (syntax-ppss))
1147 (when (eq ?\n (nth 3 ppss))
1149 (goto-char (nth 8 ppss))
1151 (while (re-search-forward ruby-here-doc-beg-re
1152 (line-end-position) t)
1153 (push (concat (ruby-here-doc-end-match) "\n") res)))
1154 (let ((start (point)))
1155 ;; With multiple openers on the same line, we don't know in which
1156 ;; part `start' is, so we have to go back to the beginning.
1158 (goto-char (nth 8 ppss))
1159 (setq res (nreverse res)))
1160 (while (and res (re-search-forward (pop res) limit 'move))
1162 (put-text-property (1- (point)) (point)
1163 'syntax-table (string-to-syntax "\""))))
1164 ;; Make extra sure we don't move back, lest we could fall into an
1166 (if (< (point) start) (goto-char start))))))
1169 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1170 ;; fallback on the old font-lock-syntactic-keywords stuff.
1172 (defconst ruby-here-doc-end-re
1173 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1174 "Regexp to match the end of heredocs.
1176 This will actually match any line with one or more characters.
1177 It's useful in that it divides up the match string so that
1178 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1180 (defun ruby-here-doc-beg-match ()
1181 "Return a regexp to find the beginning of a heredoc.
1183 This should only be called after matching against `ruby-here-doc-end-re'."
1184 (let ((contents (concat
1185 (regexp-quote (concat (match-string 2) (match-string 3)))
1186 (if (string= (match-string 3) "_") "\\B" "\\b"))))
1188 (let ((match (match-string 1)))
1189 (if (and match (> (length match) 0))
1190 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1191 (match-string 1) "\\)"
1192 contents "\\(\\1\\|\\2\\)")
1193 (concat "-?\\([\"']\\|\\)" contents "\\1"))))))
1195 (defconst ruby-font-lock-syntactic-keywords
1196 `( ;; #{ }, #$hoge, #@foo are not comments
1197 ("\\(#\\)[{$@]" 1 (1 . nil))
1198 ;; the last $', $", $` in the respective string is not variable
1199 ;; the last ?', ?", ?` in the respective string is not ascii code
1200 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1203 ;; $' $" $` .... are variables
1204 ;; ?' ?" ?` are ascii codes
1205 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil))
1207 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1210 ("^=en\\(d\\)\\_>" 1 "!")
1211 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1212 ;; Currently, the following case is highlighted incorrectly:
1221 ;; This is because all here-doc beginnings are highlighted before any endings,
1222 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1223 ;; it thinks <<BAR is part of a string so it's marked as well.
1225 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1226 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1227 ;; but I don't want to try that until we've got unit tests set up
1228 ;; to make sure I don't break anything else.
1229 (,(concat ruby-here-doc-beg-re ".*\\(\n\\)")
1230 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re))
1231 (ruby-here-doc-beg-syntax))
1232 (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax)))
1233 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1235 (defun ruby-comment-beg-syntax ()
1236 "Return the syntax cell for a the first character of a =begin.
1237 See the definition of `ruby-font-lock-syntactic-keywords'.
1239 This returns a comment-delimiter cell as long as the =begin
1240 isn't in a string or another comment."
1241 (when (not (nth 3 (syntax-ppss)))
1242 (string-to-syntax "!")))
1244 (defun ruby-in-here-doc-p ()
1245 "Return whether or not the point is in a heredoc."
1247 (let ((old-point (point)) (case-fold-search nil))
1250 (while (re-search-backward ruby-here-doc-beg-re nil t)
1251 (if (not (or (ruby-in-ppss-context-p 'anything)
1252 (ruby-here-doc-find-end old-point)))
1253 (throw 'found-beg t)))))))
1255 (defun ruby-here-doc-find-end (&optional limit)
1256 "Expects the point to be on a line with one or more heredoc openers.
1257 Returns the buffer position at which all heredocs on the line
1258 are terminated, or nil if they aren't terminated before the
1259 buffer position `limit' or the end of the buffer."
1263 (let ((eol (point-at-eol))
1264 (case-fold-search nil)
1265 ;; Fake match data such that (match-end 0) is at eol
1266 (end-match-data (progn (looking-at ".*$") (match-data)))
1267 beg-match-data end-re)
1268 (while (re-search-forward ruby-here-doc-beg-re eol t)
1269 (setq beg-match-data (match-data))
1270 (setq end-re (ruby-here-doc-end-match))
1272 (set-match-data end-match-data)
1273 (goto-char (match-end 0))
1274 (unless (re-search-forward end-re limit t) (throw 'done nil))
1275 (setq end-match-data (match-data))
1277 (set-match-data beg-match-data)
1278 (goto-char (match-end 0)))
1279 (set-match-data end-match-data)
1280 (goto-char (match-end 0))
1283 (defun ruby-here-doc-beg-syntax ()
1284 "Return the syntax cell for a line that may begin a heredoc.
1285 See the definition of `ruby-font-lock-syntactic-keywords'.
1287 This sets the syntax cell for the newline ending the line
1288 containing the heredoc beginning so that cases where multiple
1289 heredocs are started on one line are handled correctly."
1291 (goto-char (match-beginning 0))
1292 (unless (or (ruby-in-ppss-context-p 'non-heredoc)
1293 (ruby-in-here-doc-p))
1294 (string-to-syntax "\""))))
1296 (defun ruby-here-doc-end-syntax ()
1297 "Return the syntax cell for a line that may end a heredoc.
1298 See the definition of `ruby-font-lock-syntactic-keywords'."
1299 (let ((pss (syntax-ppss)) (case-fold-search nil))
1300 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1301 ;; so we can just give up.
1302 ;; This means we aren't doing a full-document search
1303 ;; every time we enter a character.
1304 (when (ruby-in-ppss-context-p 'heredoc pss)
1306 (goto-char (nth 8 pss)) ; Go to the beginning of heredoc.
1307 (let ((eol (point)))
1309 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line...
1310 (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment...
1311 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1312 (not (re-search-forward ruby-here-doc-beg-re eol t))))
1313 (string-to-syntax "\"")))))))
1315 (unless (functionp 'syntax-ppss)
1316 (defun syntax-ppss (&optional pos)
1317 (parse-partial-sexp (point-min) (or pos (point)))))
1320 (defun ruby-in-ppss-context-p (context &optional ppss)
1321 (let ((ppss (or ppss (syntax-ppss (point)))))
1323 ((eq context 'anything)
1326 ((eq context 'string)
1328 ((eq context 'heredoc)
1329 (eq ?\n (nth 3 ppss)))
1330 ((eq context 'non-heredoc)
1331 (and (ruby-in-ppss-context-p 'anything)
1332 (not (ruby-in-ppss-context-p 'heredoc))))
1333 ((eq context 'comment)
1337 "Internal error on `ruby-in-ppss-context-p': "
1338 "context name `" (symbol-name context) "' is unknown"))))
1341 (if (featurep 'xemacs)
1342 (put 'ruby-mode 'font-lock-defaults
1343 '((ruby-font-lock-keywords)
1346 (font-lock-syntactic-keywords
1347 . ruby-font-lock-syntactic-keywords))))
1349 (defvar ruby-font-lock-syntax-table
1350 (let ((tbl (copy-syntax-table ruby-mode-syntax-table)))
1351 (modify-syntax-entry ?_ "w" tbl)
1353 "The syntax table to use for fontifying Ruby mode buffers.
1354 See `font-lock-syntax-table'.")
1356 (defconst ruby-font-lock-keywords
1359 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1360 1 font-lock-function-name-face)
1363 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1407 ruby-keyword-end-re)
1409 ;; here-doc beginnings
1410 (list ruby-here-doc-beg-re 0 'font-lock-string-face)
1412 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1413 2 font-lock-variable-name-face)
1415 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1416 1 font-lock-variable-name-face)
1417 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1418 0 font-lock-variable-name-face)
1419 ;; general delimited string
1420 '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1421 (2 font-lock-string-face))
1423 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1424 2 font-lock-type-face)
1426 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1427 2 font-lock-reference-face)
1428 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-reference-face)
1429 ;; expression expansion
1430 '("#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)"
1431 0 font-lock-variable-name-face t)
1432 ;; warn lower camel case
1433 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1434 ; 0 font-lock-warning-face)
1436 "Additional expressions to highlight in Ruby mode.")
1439 (define-derived-mode ruby-mode prog-mode "Ruby"
1440 "Major mode for editing Ruby scripts.
1441 \\[ruby-indent-line] properly indents subexpressions of multi-line
1442 class, module, def, if, while, for, do, and case statements, taking
1443 nesting into account.
1445 The variable `ruby-indent-level' controls the amount of indentation.
1448 (ruby-mode-variables)
1450 (set (make-local-variable 'imenu-create-index-function)
1451 'ruby-imenu-create-index)
1452 (set (make-local-variable 'add-log-current-defun-function)
1453 'ruby-add-log-current-method)
1456 (cond ((boundp 'before-save-hook) 'before-save-hook)
1457 ((boundp 'write-contents-functions) 'write-contents-functions)
1458 ((boundp 'write-contents-hooks) 'write-contents-hooks))
1459 'ruby-mode-set-encoding nil 'local)
1461 (set (make-local-variable 'electric-indent-chars)
1462 (append '(?\{ ?\}) electric-indent-chars))
1464 (set (make-local-variable 'font-lock-defaults)
1465 '((ruby-font-lock-keywords) nil nil))
1466 (set (make-local-variable 'font-lock-keywords)
1467 ruby-font-lock-keywords)
1468 (set (make-local-variable 'font-lock-syntax-table)
1469 ruby-font-lock-syntax-table)
1471 (if (eval-when-compile (fboundp 'syntax-propertize-rules))
1472 (set (make-local-variable 'syntax-propertize-function)
1473 #'ruby-syntax-propertize-function)
1474 (set (make-local-variable 'font-lock-syntactic-keywords)
1475 ruby-font-lock-syntactic-keywords)))
1477 ;;; Invoke ruby-mode when appropriate
1480 (add-to-list 'auto-mode-alist (cons (purecopy "\\.rb\\'") 'ruby-mode))
1483 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
1484 (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'ruby-mode)))
1486 (provide 'ruby-mode)
1488 ;;; ruby-mode.el ends here