1 ;;; ruby-mode.el --- Major mode for editing Ruby files
3 ;; Copyright (C) 1994-2014 Free Software Foundation, Inc.
5 ;; Authors: Yukihiro Matsumoto
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/RubyMode
8 ;; Created: Fri Feb 4 14:49:13 JST 1994
9 ;; Keywords: languages ruby
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Provides font-locking, indentation support, and navigation for Ruby code.
31 ;; If you're installing manually, you should add this to your .emacs
32 ;; file after putting it on your load path:
34 ;; (autoload 'ruby-mode "ruby-mode" "Major mode for ruby files" t)
35 ;; (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
36 ;; (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
38 ;; Still needs more docstrings; search below for TODO.
43 "Major mode for editing Ruby code."
47 (defconst ruby-block-beg-keywords
48 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
49 "Keywords at the beginning of blocks.")
51 (defconst ruby-block-beg-re
52 (regexp-opt ruby-block-beg-keywords
)
53 "Regexp to match the beginning of blocks.")
55 (defconst ruby-non-block-do-re
56 (regexp-opt '("while" "until" "for" "rescue") 'symbols
)
57 "Regexp to match keywords that nest without blocks.")
59 (defconst ruby-indent-beg-re
60 (concat "^\\(\\s *" (regexp-opt '("class" "module" "def")) "\\|"
61 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin"))
63 "Regexp to match where the indentation gets deeper.")
65 (defconst ruby-modifier-beg-keywords
66 '("if" "unless" "while" "until")
67 "Modifiers that are the same as the beginning of blocks.")
69 (defconst ruby-modifier-beg-re
70 (regexp-opt ruby-modifier-beg-keywords
)
71 "Regexp to match modifiers same as the beginning of blocks.")
73 (defconst ruby-modifier-re
74 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords
))
75 "Regexp to match modifiers.")
77 (defconst ruby-block-mid-keywords
78 '("then" "else" "elsif" "when" "rescue" "ensure")
79 "Keywords where the indentation gets shallower in middle of block statements.")
81 (defconst ruby-block-mid-re
82 (regexp-opt ruby-block-mid-keywords
)
83 "Regexp to match where the indentation gets shallower in middle of block statements.")
85 (defconst ruby-block-op-keywords
87 "Regexp to match boolean keywords.")
89 (defconst ruby-block-hanging-re
90 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords
))
91 "Regexp to match hanging block modifiers.")
93 (defconst ruby-block-end-re
"\\_<end\\_>")
95 (defconst ruby-defun-beg-re
96 '"\\(def\\|class\\|module\\)"
97 "Regexp to match the beginning of a defun, in the general sense.")
99 (defconst ruby-singleton-class-re
101 "Regexp to match the beginning of a singleton class context.")
104 (defconst ruby-here-doc-beg-re
105 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
106 "Regexp to match the beginning of a heredoc.")
108 (defconst ruby-expression-expansion-re
109 "\\(?:[^\\]\\|\\=\\)\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)"))
111 (defun ruby-here-doc-end-match ()
112 "Return a regexp to find the end of a heredoc.
114 This should only be called after matching against `ruby-here-doc-beg-re'."
116 (if (match-string 2) "[ \t]*" nil
)
122 (defconst ruby-delimiter
123 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
125 "\\)\\_>\\|" ruby-block-end-re
126 "\\|^=begin\\|" ruby-here-doc-beg-re
))
128 (defconst ruby-negative
129 (concat "^[ \t]*\\(\\(" ruby-block-mid-re
"\\)\\>\\|"
130 ruby-block-end-re
"\\|}\\|\\]\\)")
131 "Regexp to match where the indentation gets shallower.")
133 (defconst ruby-operator-re
"[-,.+*/%&|^~=<>:]\\|\\\\$"
134 "Regexp to match operators.")
136 (defconst ruby-symbol-chars
"a-zA-Z0-9_"
137 "List of characters that symbol names may contain.")
139 (defconst ruby-symbol-re
(concat "[" ruby-symbol-chars
"]")
140 "Regexp to match symbols.")
142 (defvar ruby-use-smie t
)
144 (defvar ruby-mode-map
145 (let ((map (make-sparse-keymap)))
146 (unless ruby-use-smie
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-q") 'ruby-indent-exp
))
151 (define-key map
(kbd "M-C-d") 'smie-down-list
))
152 (define-key map
(kbd "M-C-p") 'ruby-beginning-of-block
)
153 (define-key map
(kbd "M-C-n") 'ruby-end-of-block
)
154 (define-key map
(kbd "C-c {") 'ruby-toggle-block
)
156 "Keymap used in Ruby mode.")
163 ["Beginning of Block" ruby-beginning-of-block t
]
164 ["End of Block" ruby-end-of-block t
]
165 ["Toggle Block" ruby-toggle-block t
]
167 ["Backward Sexp" ruby-backward-sexp
168 :visible
(not ruby-use-smie
)]
169 ["Backward Sexp" backward-sexp
170 :visible ruby-use-smie
]
171 ["Forward Sexp" ruby-forward-sexp
172 :visible
(not ruby-use-smie
)]
173 ["Forward Sexp" forward-sexp
174 :visible ruby-use-smie
]
175 ["Indent Sexp" ruby-indent-exp
176 :visible
(not ruby-use-smie
)]
177 ["Indent Sexp" prog-indent-sexp
178 :visible ruby-use-smie
]))
180 (defvar ruby-mode-syntax-table
181 (let ((table (make-syntax-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 ?
\n ">" 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
)
203 (modify-syntax-entry ?\
) ")(" table
)
204 (modify-syntax-entry ?\
{ "(}" table
)
205 (modify-syntax-entry ?\
} "){" table
)
206 (modify-syntax-entry ?\
[ "(]" table
)
207 (modify-syntax-entry ?\
] ")[" table
)
209 "Syntax table to use in Ruby mode.")
211 (defcustom ruby-indent-tabs-mode nil
212 "Indentation can insert tabs in Ruby mode if this is non-nil."
217 (defcustom ruby-indent-level
2
218 "Indentation of Ruby statements."
223 (defcustom ruby-comment-column
(default-value 'comment-column
)
224 "Indentation column of comments."
229 (defconst ruby-alignable-keywords
'(if while unless until begin case for def
)
230 "Keywords that can be used in `ruby-align-to-stmt-keywords'.")
232 (defcustom ruby-align-to-stmt-keywords
'(def)
233 "Keywords after which we align the expression body to statement.
235 When nil, an expression that begins with one these keywords is
236 indented to the column of the keyword. Example:
244 If this value is t or contains a symbol with the name of given
245 keyword, the expression is indented to align to the beginning of
254 Only has effect when `ruby-use-smie' is t.
257 (const :tag
"None" nil
)
259 (repeat :tag
"User defined"
261 (lambda (kw) (list 'const kw
))
262 ruby-alignable-keywords
))))
267 (defcustom ruby-deep-arglist t
268 "Deep indent lists in parenthesis when non-nil.
269 Also ignores spaces after parenthesis when `space'.
270 Only has effect when `ruby-use-smie' is nil."
275 ;; FIXME Woefully under documented. What is the point of the last `t'?.
276 (defcustom ruby-deep-indent-paren
'(?\
( ?\
[ ?\
] t
)
277 "Deep indent lists in parenthesis when non-nil.
278 The value t means continuous line.
279 Also ignores spaces after parenthesis when `space'.
280 Only has effect when `ruby-use-smie' is nil."
281 :type
'(choice (const nil
)
283 (repeat (choice character
284 (cons character
(choice (const nil
)
290 (defcustom ruby-deep-indent-paren-style
'space
291 "Default deep indent style.
292 Only has effect when `ruby-use-smie' is nil."
293 :type
'(choice (const t
) (const nil
) (const space
))
296 (defcustom ruby-encoding-map
297 '((us-ascii . nil
) ;; Do not put coding: us-ascii
298 (shift-jis . cp932
) ;; Emacs charset name of Shift_JIS
299 (shift_jis . cp932
) ;; MIME charset name of Shift_JIS
300 (japanese-cp932 . cp932
)) ;; Emacs charset name of CP932
301 "Alist to map encoding name from Emacs to Ruby.
302 Associating an encoding name with nil means it needs not be
303 explicitly declared in magic comment."
304 :type
'(repeat (cons (symbol :tag
"From") (symbol :tag
"To")))
307 (defcustom ruby-insert-encoding-magic-comment t
308 "Insert a magic Ruby encoding comment upon save if this is non-nil.
309 The encoding will be auto-detected. The format of the encoding comment
310 is customizable via `ruby-encoding-magic-comment-style'.
312 When set to `always-utf8' an utf-8 comment will always be added,
313 even if it's not required."
314 :type
'boolean
:group
'ruby
)
316 (defcustom ruby-encoding-magic-comment-style
'ruby
317 "The style of the magic encoding comment to use."
319 (const :tag
"Emacs Style" emacs
)
320 (const :tag
"Ruby Style" ruby
)
321 (const :tag
"Custom Style" custom
))
325 (defcustom ruby-custom-encoding-magic-comment-template
"# encoding: %s"
326 "A custom encoding comment template.
327 It is used when `ruby-encoding-magic-comment-style' is set to `custom'."
332 (defcustom ruby-use-encoding-map t
333 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
334 :type
'boolean
:group
'ruby
)
340 ;; Here's a simplified BNF grammar, for reference:
341 ;; http://www.cse.buffalo.edu/~regan/cse305/RubyBNF.pdf
342 (defconst ruby-smie-grammar
347 (insts (inst) (insts ";" insts
))
348 (inst (exp) (inst "iuwu-mod" exp
)
349 ;; Somewhat incorrect (both can be used multiple times),
350 ;; but avoids lots of conflicts:
351 (exp "and" exp
) (exp "or" exp
))
352 (exp (exp1) (exp "," exp
) (exp "=" exp
)
355 (exp1 (exp2) (exp2 "?" exp1
":" exp1
))
356 (exp2 ("def" insts
"end")
357 ("begin" insts-rescue-insts
"end")
359 ("class" insts
"end") ("module" insts
"end")
360 ("for" for-body
"end")
364 ("while" insts
"end")
365 ("until" insts
"end")
366 ("unless" insts
"end")
368 ("case" cases
"end"))
369 (formal-params ("opening-|" exp
"closing-|"))
370 (for-body (for-head ";" insts
))
371 (for-head (id "in" exp
))
372 (cases (exp "then" insts
)
373 (cases "when" cases
) (insts "else" insts
))
374 (expseq (exp) );;(expseq "," expseq)
375 (hashvals (id "=>" exp1
) (hashvals "," hashvals
))
376 (insts-rescue-insts (insts)
377 (insts-rescue-insts "rescue" insts-rescue-insts
)
378 (insts-rescue-insts "ensure" insts-rescue-insts
))
379 (itheni (insts) (exp "then" insts
))
380 (ielsei (itheni) (itheni "else" insts
))
381 (if-body (ielsei) (if-body "elsif" if-body
)))
382 '((nonassoc "in") (assoc ";") (right " @ ")
383 (assoc ",") (right "=") (assoc "."))
386 '((assoc "rescue" "ensure"))
391 (right "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^="
392 "<<=" ">>=" "&&=" "||=")
395 (left "*" "/" "%" "**")
399 (nonassoc ">" ">=" "<" "<=")
400 (nonassoc "==" "===" "!=")
402 (left "<<" ">>"))))))
404 (defun ruby-smie--bosp ()
405 (save-excursion (skip-chars-backward " \t")
406 (or (bolp) (memq (char-before) '(?\
; ?=)))))
408 (defun ruby-smie--implicit-semi-p ()
410 (skip-chars-backward " \t")
412 (and (memq (char-before)
413 '(?\
; ?- ?+ ?* ?/ ?: ?. ?, ?\[ ?\( ?\\ ?& ?> ?< ?%
415 ;; Not the end of a regexp or a percent literal.
416 (not (memq (car (syntax-after (1- (point)))) '(7 15))))
417 (and (eq (char-before) ?
\?)
418 (equal (save-excursion (ruby-smie--backward-token)) "?"))
419 (and (eq (char-before) ?
=)
420 (string-match "\\`\\s." (save-excursion
421 (ruby-smie--backward-token))))
422 (and (eq (char-before) ?|
)
423 (member (save-excursion (ruby-smie--backward-token))
425 (and (eq (car (syntax-after (1- (point)))) 2)
426 (member (save-excursion (ruby-smie--backward-token))
427 '("iuwu-mod" "and" "or")))
430 (eq (char-after) ?.
))))))
432 (defun ruby-smie--redundant-do-p (&optional skip
)
434 (if skip
(backward-word 1))
435 (member (nth 2 (smie-backward-sexp ";")) '("while" "until" "for"))))
437 (defun ruby-smie--opening-pipe-p ()
439 (if (eq ?|
(char-before)) (forward-char -
1))
440 (skip-chars-backward " \t\n")
441 (or (eq ?\
{ (char-before))
442 (looking-back "\\_<do" (- (point) 2)))))
444 (defun ruby-smie--closing-pipe-p ()
446 (if (eq ?|
(char-before)) (forward-char -
1))
447 (and (re-search-backward "|" (line-beginning-position) t
)
448 (ruby-smie--opening-pipe-p))))
450 (defun ruby-smie--args-separator-p (pos)
452 (< pos
(line-end-position))
453 (or (eq (char-syntax (preceding-char)) '?w
)
454 ;; FIXME: Check that the preceding token is not a keyword.
455 ;; This isn't very important most of the time, though.
456 (and (memq (preceding-char) '(?
! ??
))
457 (eq (char-syntax (char-before (1- (point)))) '?w
)))
460 (or (and (eq (char-syntax (char-after)) ?w
)
461 (not (looking-at (regexp-opt '("unless" "if" "while" "until" "or"
462 "else" "elsif" "do" "end" "and")
464 (memq (car (syntax-after pos
)) '(7 15))
465 (looking-at "[([]\\|[-+!~]\\sw\\|:\\(?:\\sw\\|\\s.\\)")))))
467 (defun ruby-smie--at-dot-call ()
468 (and (eq ?w
(char-syntax (following-char)))
469 (eq (char-before) ?.
)
470 (not (eq (char-before (1- (point))) ?.
))))
472 (defun ruby-smie--forward-token ()
474 (skip-chars-forward " \t")
476 ((and (looking-at "\n") (looking-at "\\s\"")) ;A heredoc.
477 ;; Tokenize the whole heredoc as semicolon.
478 (goto-char (scan-sexps (point) 1))
480 ((and (looking-at "[\n#]")
481 (ruby-smie--implicit-semi-p)) ;Only add implicit ; when needed.
482 (if (eolp) (forward-char 1) (forward-comment 1))
485 (forward-comment (point-max))
487 ((and (< pos
(point))
489 (ruby-smie--args-separator-p (prog1 (point) (goto-char pos
)))))
491 ((looking-at ":\\s.+")
492 (goto-char (match-end 0)) (match-string 0)) ;bug#15208.
493 ((looking-at "\\s\"") "") ;A string.
495 (let ((dot (ruby-smie--at-dot-call))
496 (tok (smie-default-forward-token)))
498 (setq tok
(concat "." tok
)))
500 ((member tok
'("unless" "if" "while" "until"))
501 (if (save-excursion (forward-word -
1) (ruby-smie--bosp))
503 ((string-match-p "\\`|[*&]?\\'" tok
)
504 (forward-char (- 1 (length tok
)))
507 ((ruby-smie--opening-pipe-p) "opening-|")
508 ((ruby-smie--closing-pipe-p) "closing-|")
510 ((and (equal tok
"") (looking-at "\\\\\n"))
511 (goto-char (match-end 0)) (ruby-smie--forward-token))
514 ((not (ruby-smie--redundant-do-p 'skip
)) tok
)
515 ((> (save-excursion (forward-comment (point-max)) (point))
517 (ruby-smie--forward-token)) ;Fully redundant.
521 (defun ruby-smie--backward-token ()
523 (forward-comment (- (point)))
525 ((and (> pos
(line-end-position)) (ruby-smie--implicit-semi-p))
526 (skip-chars-forward " \t") ";")
527 ((and (bolp) (not (bobp))) ;Presumably a heredoc.
528 ;; Tokenize the whole heredoc as semicolon.
529 (goto-char (scan-sexps (point) -
1))
531 ((and (> pos
(point)) (not (bolp))
532 (ruby-smie--args-separator-p pos
))
533 ;; We have "ID SPC ID", which is a method call, but it binds less tightly
534 ;; than commas, since a method call can also be "ID ARG1, ARG2, ARG3".
535 ;; In some textbooks, "e1 @ e2" is used to mean "call e1 with arg e2".
538 (let ((tok (smie-default-backward-token))
539 (dot (ruby-smie--at-dot-call)))
541 (setq tok
(concat "." tok
)))
542 (when (and (eq ?
: (char-before)) (string-match "\\`\\s." tok
))
543 (forward-char -
1) (setq tok
(concat ":" tok
))) ;; bug#15208.
545 ((member tok
'("unless" "if" "while" "until"))
546 (if (ruby-smie--bosp)
550 ((ruby-smie--opening-pipe-p) "opening-|")
551 ((ruby-smie--closing-pipe-p) "closing-|")
553 ((string-match-p "\\`|[*&]\\'" tok
)
556 ((and (equal tok
"") (eq ?
\\ (char-before)) (looking-at "\n"))
557 (forward-char -
1) (ruby-smie--backward-token))
560 ((not (ruby-smie--redundant-do-p)) tok
)
561 ((> (save-excursion (forward-word 1)
562 (forward-comment (point-max)) (point))
564 (ruby-smie--backward-token)) ;Fully redundant.
568 (defun ruby-smie--indent-to-stmt ()
570 (smie-backward-sexp ";")
571 (cons 'column
(smie-indent-virtual))))
573 (defun ruby-smie--indent-to-stmt-p (keyword)
574 (or (eq t ruby-align-to-stmt-keywords
)
575 (memq (intern keyword
) ruby-align-to-stmt-keywords
)))
577 (defun ruby-smie-rules (kind token
)
578 (pcase (cons kind token
)
579 (`(:elem . basic
) ruby-indent-level
)
580 ;; "foo" "bar" is the concatenation of the two strings, so the second
581 ;; should be aligned with the first.
582 (`(:elem . args
) (if (looking-at "\\s\"") 0))
583 ;; (`(:after . ",") (smie-rule-separator kind))
586 ((smie-rule-parent-p "def" "begin" "do" "class" "module" "for"
587 "while" "until" "unless"
588 "if" "then" "elsif" "else" "when"
589 "rescue" "ensure" "{")
590 (smie-rule-parent ruby-indent-level
))
591 ;; For (invalid) code between switch and case.
592 ;; (if (smie-parent-p "switch") 4)
594 (`(:before .
,(or `"(" `"[" `"{"))
596 ((and (equal token
"{")
597 (not (smie-rule-prev-p "(" "{" "[" "," "=>" "=" "return" ";"))
600 (not (eq (preceding-char) ?
:))))
601 ;; Curly block opener.
602 (ruby-smie--indent-to-stmt))
603 ((smie-rule-hanging-p)
604 ;; Treat purely syntactic block-constructs as being part of their parent,
605 ;; when the opening token is hanging and the parent is not an
608 ((eq (car (smie-indent--parent)) t
) nil
)
609 ;; When after `.', let's always de-indent,
610 ;; because when `.' is inside the line, the
611 ;; additional indentation from it looks out of place.
612 ((smie-rule-parent-p ".") (smie-rule-parent (- ruby-indent-level
)))
613 (t (smie-rule-parent))))))
614 (`(:after .
,(or `"(" "[" "{"))
615 ;; FIXME: Shouldn't this be the default behavior of
616 ;; `smie-indent-after-keyword'?
619 (skip-chars-forward " \t")
620 ;; `smie-rule-hanging-p' is not good enough here,
621 ;; because we want to reject hanging tokens at bol, too.
622 (unless (or (eolp) (forward-comment 1))
623 (cons 'column
(current-column)))))
624 (`(:before .
"do") (ruby-smie--indent-to-stmt))
625 (`(:before .
".") ruby-indent-level
)
626 (`(:after .
"=>") ruby-indent-level
)
627 (`(:before .
,(or `"else" `"then" `"elsif" `"rescue" `"ensure"))
630 ;; Align to the previous `when', but look up the virtual
631 ;; indentation of `case'.
632 (if (smie-rule-sibling-p) 0 (smie-rule-parent)))
633 (`(:after .
,(or "=" "iuwu-mod" "+" "-" "*" "/" "&&" "||" "%" "**" "^" "&"
634 "<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>"
635 "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|"
636 "<<=" ">>=" "&&=" "||=" "and" "or"))
637 (and (smie-rule-parent-p ";" nil
)
638 (smie-indent--hanging-p)
640 (`(:after .
,(or "?" ":")) ruby-indent-level
)
641 (`(:before .
,(guard (memq (intern-soft token
) ruby-alignable-keywords
)))
642 (when (not (ruby--at-indentation-p))
643 (if (ruby-smie--indent-to-stmt-p token
)
644 (ruby-smie--indent-to-stmt)
645 (cons 'column
(current-column)))))
648 (defun ruby--at-indentation-p (&optional point
)
650 (unless point
(setq point
(point)))
652 (skip-chars-forward " \t")
655 (defun ruby-imenu-create-index-in-block (prefix beg end
)
656 "Create an imenu index of methods inside a block."
657 (let ((index-alist '()) (case-fold-search nil
)
658 name next pos decl sing
)
660 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t
)
661 (setq sing
(match-beginning 3))
662 (setq decl
(match-string 5))
663 (setq next
(match-end 0))
664 (setq name
(or (match-string 4) (match-string 6)))
665 (setq pos
(match-beginning 0))
667 ((string= "alias" decl
)
668 (if prefix
(setq name
(concat prefix name
)))
669 (push (cons name pos
) index-alist
))
670 ((string= "def" decl
)
674 ((string-match "^self\." name
)
675 (concat (substring prefix
0 -
1) (substring name
4)))
676 (t (concat prefix name
)))))
677 (push (cons name pos
) index-alist
)
678 (ruby-accurate-end-of-block end
))
680 (if (string= "self" name
)
681 (if prefix
(setq name
(substring prefix
0 -
1)))
682 (if prefix
(setq name
(concat (substring prefix
0 -
1) "::" name
)))
683 (push (cons name pos
) index-alist
))
684 (ruby-accurate-end-of-block end
)
687 (nconc (ruby-imenu-create-index-in-block
688 (concat name
(if sing
"." "#"))
689 next beg
) index-alist
))
693 (defun ruby-imenu-create-index ()
694 "Create an imenu index of all methods in the buffer."
695 (nreverse (ruby-imenu-create-index-in-block nil
(point-min) nil
)))
697 (defun ruby-accurate-end-of-block (&optional end
)
698 "Jump to the end of the current block or END, whichever is closer."
700 (end (or end
(point-max))))
703 (back-to-indentation)
704 (narrow-to-region (point) end
)
706 (while (and (setq state
(apply 'ruby-parse-partial end state
))
707 (>= (nth 2 state
) 0) (< (point) end
))))))
709 (defun ruby-mode-variables ()
710 "Set up initial buffer-local variables for Ruby mode."
711 (setq indent-tabs-mode ruby-indent-tabs-mode
)
713 (smie-setup ruby-smie-grammar
#'ruby-smie-rules
714 :forward-token
#'ruby-smie--forward-token
715 :backward-token
#'ruby-smie--backward-token
)
716 (setq-local indent-line-function
'ruby-indent-line
))
717 (setq-local require-final-newline t
)
718 (setq-local comment-start
"# ")
719 (setq-local comment-end
"")
720 (setq-local comment-column ruby-comment-column
)
721 (setq-local comment-start-skip
"#+ *")
722 (setq-local parse-sexp-ignore-comments t
)
723 (setq-local parse-sexp-lookup-properties t
)
724 (setq-local paragraph-start
(concat "$\\|" page-delimiter
))
725 (setq-local paragraph-separate paragraph-start
)
726 (setq-local paragraph-ignore-fill-prefix t
))
728 (defun ruby--insert-coding-comment (encoding)
729 "Insert a magic coding comment for ENCODING.
730 The style of the comment is controlled by `ruby-encoding-magic-comment-style'."
731 (let ((encoding-magic-comment-template
732 (pcase ruby-encoding-magic-comment-style
733 (`ruby
"# coding: %s")
734 (`emacs
"# -*- coding: %s -*-")
736 ruby-custom-encoding-magic-comment-template
))))
738 (format encoding-magic-comment-template encoding
)
741 (defun ruby--detect-encoding ()
742 (if (eq ruby-insert-encoding-magic-comment
'always-utf8
)
745 (or save-buffer-coding-system
746 buffer-file-coding-system
)))
749 (or (coding-system-get coding-system
'mime-charset
)
750 (coding-system-change-eol-conversion coding-system nil
))))
753 (if ruby-use-encoding-map
754 (let ((elt (assq coding-system ruby-encoding-map
)))
755 (if elt
(cdr elt
) coding-system
))
759 (defun ruby--encoding-comment-required-p ()
760 (or (eq ruby-insert-encoding-magic-comment
'always-utf8
)
761 (re-search-forward "[^\0-\177]" nil t
)))
763 (defun ruby-mode-set-encoding ()
764 "Insert a magic comment header with the proper encoding if necessary."
767 (goto-char (point-min))
768 (when (ruby--encoding-comment-required-p)
769 (goto-char (point-min))
770 (let ((coding-system (ruby--detect-encoding)))
772 (if (looking-at "^#!") (beginning-of-line 2))
773 (cond ((looking-at "\\s *#\\s *.*\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)")
774 ;; update existing encoding comment if necessary
775 (unless (string= (match-string 2) coding-system
)
776 (goto-char (match-beginning 2))
777 (delete-region (point) (match-end 2))
778 (insert coding-system
)))
779 ((looking-at "\\s *#.*coding\\s *[:=]"))
780 (t (when ruby-insert-encoding-magic-comment
781 (ruby--insert-coding-comment coding-system
))))
782 (when (buffer-modified-p)
783 (basic-save-buffer-1)))))))
785 (defvar ruby--electric-indent-chars
'(?. ?\
) ?
} ?\
]))
787 (defun ruby--electric-indent-p (char)
789 ((memq char ruby--electric-indent-chars
)
790 ;; Reindent after typing a char affecting indentation.
791 (ruby--at-indentation-p (1- (point))))
792 ((memq (char-after) ruby--electric-indent-chars
)
793 ;; Reindent after inserting something in front of the above.
794 (ruby--at-indentation-p (1- (point))))
795 ((or (and (>= char ?a
) (<= char ?z
)) (memq char
'(?_ ?? ?
! ?
:)))
798 (skip-chars-backward "[:alpha:]:_?!")
799 (and (ruby--at-indentation-p)
800 (looking-at (regexp-opt (cons "end" ruby-block-mid-keywords
)))
801 ;; Outdent after typing a keyword.
802 (or (eq (match-end 0) pt
)
803 ;; Reindent if it wasn't a keyword after all.
804 (eq (match-end 0) (1- pt
)))))))))
806 ;; FIXME: Remove this? It's unused here, but some redefinitions of
807 ;; `ruby-calculate-indent' in user init files still call it.
808 (defun ruby-current-indentation ()
809 "Return the indentation level of current line."
812 (back-to-indentation)
815 (defun ruby-indent-line (&optional ignored
)
816 "Correct the indentation of the current Ruby line."
818 (ruby-indent-to (ruby-calculate-indent)))
820 (defun ruby-indent-to (column)
821 "Indent the current line to COLUMN."
824 (and (< column
0) (error "Invalid nesting"))
825 (setq shift
(current-column))
828 (back-to-indentation)
829 (setq top
(current-column))
830 (skip-chars-backward " \t")
831 (if (>= shift top
) (setq shift
(- shift top
))
835 (move-to-column (+ column shift
))
837 (delete-region beg
(point))
840 (move-to-column (+ column shift
))))))
842 (defun ruby-special-char-p (&optional pos
)
843 "Return t if the character before POS is a special character.
844 If omitted, POS defaults to the current point.
845 Special characters are `?', `$', `:' when preceded by whitespace,
846 and `\\' when preceded by `?'."
847 (setq pos
(or pos
(point)))
848 (let ((c (char-before pos
)) (b (and (< (point-min) pos
)
849 (char-before (1- pos
)))))
850 (cond ((or (eq c ??
) (eq c ?$
)))
851 ((and (eq c ?
:) (or (not b
) (eq (char-syntax b
) ?
))))
852 ((eq c ?
\\) (eq b ??
)))))
854 (defun ruby-singleton-class-p (&optional pos
)
856 (when pos
(goto-char pos
))
858 (and (or (bolp) (not (eq (char-before (point)) ?_
)))
859 (looking-at ruby-singleton-class-re
))))
861 (defun ruby-expr-beg (&optional option
)
862 "Check if point is possibly at the beginning of an expression.
863 OPTION specifies the type of the expression.
864 Can be one of `heredoc', `modifier', `expr-qstr', `expr-re'."
866 (store-match-data nil
)
867 (let ((space (skip-chars-backward " \t"))
873 (and (looking-at "\\?")
874 (or (eq (char-syntax (char-before (point))) ?w
)
875 (ruby-special-char-p))))
877 ((looking-at ruby-operator-re
))
878 ((eq option
'heredoc
)
879 (and (< space
0) (not (ruby-singleton-class-p start
))))
880 ((or (looking-at "[\\[({,;]")
881 (and (looking-at "[!?]")
882 (or (not (eq option
'modifier
))
884 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
885 (and (looking-at ruby-symbol-re
)
886 (skip-chars-backward ruby-symbol-chars
)
888 ((looking-at (regexp-opt
889 (append ruby-block-beg-keywords
890 ruby-block-op-keywords
891 ruby-block-mid-keywords
)
893 (goto-char (match-end 0))
894 (not (looking-at "\\s_")))
895 ((eq option
'expr-qstr
)
896 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
897 ((eq option
'expr-re
)
898 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
901 (defun ruby-forward-string (term &optional end no-error expand
)
902 "Move forward across one balanced pair of string delimiters.
903 Skips escaped delimiters. If EXPAND is non-nil, also ignores
904 delimiters in interpolated strings.
906 TERM should be a string containing either a single, self-matching
907 delimiter (e.g. \"/\"), or a pair of matching delimiters with the
908 close delimiter first (e.g. \"][\").
910 When non-nil, search is bounded by position END.
912 Throws an error if a balanced match is not found, unless NO-ERROR
913 is non-nil, in which case nil will be returned.
915 This command assumes the character after point is an opening
917 (let ((n 1) (c (string-to-char term
))
918 (re (concat "[^\\]\\(\\\\\\\\\\)*\\("
919 (if (string= term
"^") ;[^] is not a valid regexp
921 (concat "[" term
"]"))
922 (when expand
"\\|\\(#{\\)")
924 (while (and (re-search-forward re end no-error
)
925 (if (match-beginning 3)
926 (ruby-forward-string "}{" end no-error nil
)
927 (> (setq n
(if (eq (char-before (point)) c
)
932 ((error "Unterminated string")))))
934 (defun ruby-deep-indent-paren-p (c)
936 (cond ((listp ruby-deep-indent-paren
)
937 (let ((deep (assoc c ruby-deep-indent-paren
)))
939 (or (cdr deep
) ruby-deep-indent-paren-style
))
940 ((memq c ruby-deep-indent-paren
)
941 ruby-deep-indent-paren-style
))))
942 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
943 ((eq c ?\
( ) ruby-deep-arglist
)))
945 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
946 "TODO: document throughout function body."
947 (or depth
(setq depth
0))
948 (or indent
(setq indent
0))
949 (when (re-search-forward ruby-delimiter end
'move
)
950 (let ((pnt (point)) w re expand
)
951 (goto-char (match-beginning 0))
953 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
955 ((looking-at "[\"`]") ;skip string
958 (ruby-forward-string (buffer-substring (point) (1+ (point)))
962 (setq in-string
(point))
967 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
970 (setq in-string
(point))
976 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
977 (if (ruby-forward-string "/" end t t
)
979 (setq in-string
(point))
986 (ruby-expr-beg 'expr-qstr
)
987 (not (looking-at "%="))
988 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
989 (goto-char (match-beginning 1))
990 (setq expand
(not (memq (char-before) '(?q ?w
))))
991 (setq w
(match-string 1))
993 ((string= w
"[") (setq re
"]["))
994 ((string= w
"{") (setq re
"}{"))
995 ((string= w
"(") (setq re
")("))
996 ((string= w
"<") (setq re
"><"))
997 ((and expand
(string= w
"\\"))
998 (setq w
(concat "\\" w
))))
999 (unless (cond (re (ruby-forward-string re end t expand
))
1000 (expand (ruby-forward-string w end t t
))
1001 (t (re-search-forward
1002 (if (string= w
"\\")
1004 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
1006 (setq in-string
(point))
1010 ((looking-at "\\?") ;skip ?char
1012 ((and (ruby-expr-beg)
1013 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
1014 (goto-char (match-end 0)))
1017 ((looking-at "\\$") ;skip $char
1020 ((looking-at "#") ;skip comment
1024 ((looking-at "[\\[{(]")
1025 (let ((deep (ruby-deep-indent-paren-p (char-after))))
1026 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
1028 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
1029 (setq pnt
(1- (match-end 0))))
1030 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
1031 (setq pcol
(cons (cons pnt depth
) pcol
))
1033 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
1034 (setq depth
(1+ depth
))))
1037 ((looking-at "[])}]")
1038 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
1039 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
1040 (setq depth
(1- depth
)))
1041 (setq nest
(cdr nest
))
1043 ((looking-at ruby-block-end-re
)
1044 (if (or (and (not (bolp))
1047 (setq w
(char-after (point)))
1052 (setq w
(char-after (point)))
1057 (setq nest
(cdr nest
))
1058 (setq depth
(1- depth
)))
1060 ((looking-at "def\\s +[^(\n;]*")
1064 (not (eq ?_
(char-after (point))))))
1066 (setq nest
(cons (cons nil pnt
) nest
))
1067 (setq depth
(1+ depth
))))
1068 (goto-char (match-end 0)))
1069 ((looking-at (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
1072 (or (not (looking-at "do\\_>"))
1074 (back-to-indentation)
1075 (not (looking-at ruby-non-block-do-re
)))))
1079 (setq w
(char-after (point)))
1083 (not (eq ?
! (char-after (point))))
1084 (skip-chars-forward " \t")
1085 (goto-char (match-beginning 0))
1086 (or (not (looking-at ruby-modifier-re
))
1087 (ruby-expr-beg 'modifier
))
1089 (setq nest
(cons (cons nil pnt
) nest
))
1090 (setq depth
(1+ depth
)))
1092 ((looking-at ":\\(['\"]\\)")
1093 (goto-char (match-beginning 1))
1094 (ruby-forward-string (match-string 1) end t
))
1095 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
1096 (goto-char (match-end 0)))
1097 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
1098 (goto-char (match-end 0)))
1099 ((or (looking-at "\\.\\.\\.?")
1100 (looking-at "\\.[0-9]+")
1101 (looking-at "\\.[a-zA-Z_0-9]+")
1103 (goto-char (match-end 0)))
1104 ((looking-at "^=begin")
1105 (if (re-search-forward "^=end" end t
)
1107 (setq in-string
(match-end 0))
1111 ((and (ruby-expr-beg 'heredoc
)
1112 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
1113 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
1114 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
1115 (let* ((id-end (goto-char (match-end 0)))
1116 (line-end-position (point-at-eol))
1117 (state (list in-string nest depth pcol indent
)))
1118 ;; parse the rest of the line
1119 (while (and (> line-end-position
(point))
1120 (setq state
(apply 'ruby-parse-partial
1121 line-end-position state
))))
1122 (setq in-string
(car state
)
1126 indent
(nth 4 state
))
1127 ;; skip heredoc section
1128 (if (re-search-forward (concat "^" re
"$") end
'move
)
1130 (setq in-string id-end
)
1134 ((looking-at "^__END__$")
1136 ((and (looking-at ruby-here-doc-beg-re
)
1137 (boundp 'ruby-indent-point
))
1138 (if (re-search-forward (ruby-here-doc-end-match)
1139 ruby-indent-point t
)
1141 (setq in-string
(match-end 0))
1142 (goto-char ruby-indent-point
)))
1144 (error (format "Bad string %s"
1145 (buffer-substring (point) pnt
)
1147 (list in-string nest depth pcol
))
1149 (defun ruby-parse-region (start end
)
1155 (ruby-beginning-of-indent))
1157 (narrow-to-region (point) end
)
1158 (while (and (> end
(point))
1159 (setq state
(apply 'ruby-parse-partial end state
))))))
1160 (list (nth 0 state
) ; in-string
1161 (car (nth 1 state
)) ; nest
1162 (nth 2 state
) ; depth
1163 (car (car (nth 3 state
))) ; pcol
1164 ;(car (nth 5 state)) ; indent
1167 (defun ruby-indent-size (pos nest
)
1168 "Return the indentation level in spaces NEST levels deeper than POS."
1169 (+ pos
(* (or nest
1) ruby-indent-level
)))
1171 (defun ruby-calculate-indent (&optional parse-start
)
1172 "Return the proper indentation level of the current line."
1173 ;; TODO: Document body
1176 (let ((ruby-indent-point (point))
1177 (case-fold-search nil
)
1178 state eol begin op-end
1179 (paren (progn (skip-syntax-forward " ")
1180 (and (char-after) (matching-paren (char-after)))))
1183 (goto-char parse-start
)
1184 (ruby-beginning-of-indent)
1185 (setq parse-start
(point)))
1186 (back-to-indentation)
1187 (setq indent
(current-column))
1188 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
1190 ((nth 0 state
) ; within string
1191 (setq indent nil
)) ; do nothing
1192 ((car (nth 1 state
)) ; in paren
1193 (goto-char (setq begin
(cdr (nth 1 state
))))
1194 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
1196 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
1197 (skip-syntax-backward " ")
1198 (setq indent
(1- (current-column))))
1199 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
1200 (and (nth 2 s
) (> (nth 2 s
) 0)
1201 (or (goto-char (cdr (nth 1 s
))) t
)))
1203 (setq indent
(ruby-indent-size (current-column)
1206 (setq indent
(current-column))
1207 (cond ((eq deep
'space
))
1208 (paren (setq indent
(1- indent
)))
1209 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
1210 (if (nth 3 state
) (goto-char (nth 3 state
))
1211 (goto-char parse-start
) (back-to-indentation))
1212 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
1213 (and (eq (car (nth 1 state
)) paren
)
1214 (ruby-deep-indent-paren-p (matching-paren paren
))
1215 (search-backward (char-to-string paren
))
1216 (setq indent
(current-column)))))
1217 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
1218 (if (null (cdr (nth 1 state
)))
1219 (error "Invalid nesting"))
1220 (goto-char (cdr (nth 1 state
)))
1221 (forward-word -
1) ; skip back a keyword
1222 (setq begin
(point))
1224 ((looking-at "do\\>[^_]") ; iter block is a special case
1225 (if (nth 3 state
) (goto-char (nth 3 state
))
1226 (goto-char parse-start
) (back-to-indentation))
1227 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
1229 (setq indent
(+ (current-column) ruby-indent-level
)))))
1231 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
1232 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
1234 (goto-char ruby-indent-point
)
1239 ((and (not (ruby-deep-indent-paren-p paren
))
1240 (re-search-forward ruby-negative eol t
))
1241 (and (not (eq ?_
(char-after (match-end 0))))
1242 (setq indent
(- indent ruby-indent-level
))))
1247 (or (ruby-deep-indent-paren-p t
)
1248 (null (car (nth 1 state
)))))
1249 ;; goto beginning of non-empty no-comment line
1252 (skip-chars-backward " \t\n")
1255 (if (re-search-forward "^\\s *#" end t
)
1259 ;; skip the comment at the end
1260 (skip-chars-backward " \t")
1261 (let (end (pos (point)))
1263 (while (and (re-search-forward "#" pos t
)
1264 (setq end
(1- (point)))
1265 (or (ruby-special-char-p end
)
1266 (and (setq state
(ruby-parse-region
1270 (goto-char (or end pos
))
1271 (skip-chars-backward " \t")
1272 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
1273 (setq state
(ruby-parse-region parse-start
(point))))
1274 (or (bobp) (forward-char -
1))
1276 (or (and (looking-at ruby-symbol-re
)
1277 (skip-chars-backward ruby-symbol-chars
)
1278 (looking-at (concat "\\<\\(" ruby-block-hanging-re
1280 (not (eq (point) (nth 3 state
)))
1282 (goto-char (match-end 0))
1283 (not (looking-at "[a-z_]"))))
1284 (and (looking-at ruby-operator-re
)
1285 (not (ruby-special-char-p))
1288 (or (not (looking-at ruby-operator-re
))
1289 (not (eq (char-before) ?
:))))
1290 ;; Operator at the end of line.
1291 (let ((c (char-after (point))))
1295 ;; (goto-char begin)
1296 ;; (skip-chars-forward " \t")
1297 ;; (not (or (eolp) (looking-at "#")
1298 ;; (and (eq (car (nth 1 state)) ?{)
1299 ;; (looking-at "|"))))))
1300 ;; Not a regexp or percent literal.
1301 (null (nth 0 (ruby-parse-region (or begin parse-start
)
1303 (or (not (eq ?|
(char-after (point))))
1305 (or (eolp) (forward-char -
1))
1307 ((search-backward "|" nil t
)
1308 (skip-chars-backward " \t\n")
1312 (not (looking-at "{")))
1315 (not (looking-at "do\\>[^_]")))))
1323 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
1325 (eq (ruby-deep-indent-paren-p t
) 'space
)
1328 (goto-char (or begin parse-start
))
1329 (skip-syntax-forward " ")
1331 ((car (nth 1 state
)) indent
)
1333 (+ indent ruby-indent-level
))))))))
1334 (goto-char ruby-indent-point
)
1336 (skip-syntax-forward " ")
1337 (if (looking-at "\\.[^.]")
1338 (+ indent ruby-indent-level
)
1341 (defun ruby-beginning-of-defun (&optional arg
)
1342 "Move backward to the beginning of the current defun.
1343 With ARG, move backward multiple defuns. Negative ARG means
1346 (let (case-fold-search)
1347 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re
"\\_>")
1349 (beginning-of-line))))
1351 (defun ruby-end-of-defun ()
1352 "Move point to the end of the current defun.
1353 The defun begins at or after the point. This function is called
1357 (let (case-fold-search)
1358 (when (looking-back (concat "^\\s *" ruby-block-end-re
))
1361 (defun ruby-beginning-of-indent ()
1362 "Backtrack to a line which can be used as a reference for
1363 calculating indentation on the lines after it."
1364 (while (and (re-search-backward ruby-indent-beg-re nil
'move
)
1365 (if (ruby-in-ppss-context-p 'anything
)
1367 ;; We can stop, then.
1368 (beginning-of-line)))))
1370 (defun ruby-move-to-block (n)
1371 "Move to the beginning (N < 0) or the end (N > 0) of the
1372 current block, a sibling block, or an outer block. Do that (abs N) times."
1373 (back-to-indentation)
1374 (let ((signum (if (> n
0) 1 -
1))
1376 (depth (or (nth 2 (ruby-parse-region (point) (line-end-position))) 0))
1379 (when (looking-at ruby-block-mid-re
)
1380 (setq depth
(+ depth signum
)))
1381 (when (< (* depth signum
) 0)
1382 ;; Moving end -> end or beginning -> beginning.
1384 (dotimes (_ (abs n
))
1386 (setq down
(save-excursion
1387 (back-to-indentation)
1388 ;; There is a block start or block end keyword on this
1389 ;; line, don't need to look for another block.
1390 (and (re-search-forward
1391 (if backward ruby-block-end-re
1392 (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
1393 (line-end-position) t
)
1394 (not (nth 8 (syntax-ppss))))))
1395 (while (and (not done
) (not (if backward
(bobp) (eobp))))
1396 (forward-line signum
)
1398 ;; Skip empty and commented out lines.
1399 ((looking-at "^\\s *$"))
1400 ((looking-at "^\\s *#"))
1401 ;; Skip block comments;
1402 ((and (not backward
) (looking-at "^=begin\\>"))
1403 (re-search-forward "^=end\\>"))
1404 ((and backward
(looking-at "^=end\\>"))
1405 (re-search-backward "^=begin\\>"))
1406 ;; Jump over a multiline literal.
1407 ((ruby-in-ppss-context-p 'string
)
1408 (goto-char (nth 8 (syntax-ppss)))
1411 (when (bolp) (forward-char -
1)))) ; After a heredoc.
1413 (let ((state (ruby-parse-region (point) (line-end-position))))
1414 (unless (car state
) ; Line ends with unfinished string.
1415 (setq depth
(+ (nth 2 state
) depth
))))
1417 ;; Increased depth, we found a block.
1418 ((> (* signum depth
) 0)
1420 ;; We're at the same depth as when we started, and we've
1421 ;; encountered a block before. Stop.
1422 ((and down
(zerop depth
))
1424 ;; Lower depth, means outer block, can stop now.
1425 ((< (* signum depth
) 0)
1427 (back-to-indentation)))
1429 (defun ruby-beginning-of-block (&optional arg
)
1430 "Move backward to the beginning of the current block.
1431 With ARG, move up multiple blocks."
1433 (ruby-move-to-block (- (or arg
1))))
1435 (defun ruby-end-of-block (&optional arg
)
1436 "Move forward to the end of the current block.
1437 With ARG, move out of multiple blocks."
1439 (ruby-move-to-block (or arg
1)))
1441 (defun ruby-forward-sexp (&optional arg
)
1442 "Move forward across one balanced expression (sexp).
1443 With ARG, do it many times. Negative ARG means move backward."
1444 ;; TODO: Document body
1447 (ruby-use-smie (forward-sexp arg
))
1448 ((and (numberp arg
) (< arg
0)) (ruby-backward-sexp (- arg
)))
1450 (let ((i (or arg
1)))
1453 (skip-syntax-forward " ")
1454 (if (looking-at ",\\s *") (goto-char (match-end 0)))
1455 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
1456 (goto-char (match-end 0)))
1458 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
1459 (looking-at "\\s("))
1460 (goto-char (scan-sexps (point) 1)))
1461 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
1463 (not (eq (char-before (point)) ?.
))
1464 (not (eq (char-before (point)) ?
:)))
1467 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
1469 (while (progn (forward-word 1) (looking-at "_")))
1470 (cond ((looking-at "::") (forward-char 2) t
)
1471 ((> (skip-chars-forward ".") 0))
1472 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
1473 (forward-char 1) nil
)))))
1477 (setq expr
(or expr
(ruby-expr-beg)
1478 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
1479 (nth 1 (setq state
(apply #'ruby-parse-partial
1482 (skip-chars-forward "<"))
1485 ((error) (forward-word 1)))
1488 (defun ruby-backward-sexp (&optional arg
)
1489 "Move backward across one balanced expression (sexp).
1490 With ARG, do it many times. Negative ARG means move forward."
1491 ;; TODO: Document body
1494 (ruby-use-smie (backward-sexp arg
))
1495 ((and (numberp arg
) (< arg
0)) (ruby-forward-sexp (- arg
)))
1497 (let ((i (or arg
1)))
1500 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
1502 (cond ((looking-at "\\s)")
1503 (goto-char (scan-sexps (1+ (point)) -
1))
1504 (pcase (char-before)
1505 (`?%
(forward-char -
1))
1506 ((or `?q
`?Q
`?w
`?W
`?r
`?x
)
1507 (if (eq (char-before (1- (point))) ?%
)
1508 (forward-char -
2))))
1510 ((looking-at "\\s\"\\|\\\\\\S_")
1511 (let ((c (char-to-string (char-before (match-end 0)))))
1512 (while (and (search-backward c
)
1513 (eq (logand (skip-chars-backward "\\") 1)
1516 ((looking-at "\\s.\\|\\s\\")
1517 (if (ruby-special-char-p) (forward-char -
1)))
1518 ((looking-at "\\s(") nil
)
1521 (while (progn (forward-word -
1)
1522 (pcase (char-before)
1524 (`?.
(forward-char -
1) t
)
1527 (and (eq (char-before) (char-after))
1531 (eq (char-before) :)))))
1532 (if (looking-at ruby-block-end-re
)
1533 (ruby-beginning-of-block))
1539 (defun ruby-indent-exp (&optional ignored
)
1540 "Indent each line in the balanced expression following the point."
1542 (let ((here (point-marker)) start top column
(nest t
))
1543 (set-marker-insertion-type here t
)
1547 (setq start
(point) top
(current-indentation))
1548 (while (and (not (eobp))
1550 (setq column
(ruby-calculate-indent start
))
1551 (cond ((> column top
)
1553 ((and (= column top
) nest
)
1554 (setq nest nil
) t
))))
1555 (ruby-indent-to column
)
1556 (beginning-of-line 2)))
1558 (set-marker here nil
))))
1560 (defun ruby-add-log-current-method ()
1561 "Return the current method name as a string.
1562 This string includes all namespaces.
1571 See `add-log-current-defun-function'."
1574 (let* ((indent 0) mname mlist
1578 (concat "^[ \t]*" re
"[ \t]+"
1580 ;; \\. and :: for class methods
1581 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1583 (definition-re (funcall make-definition-re ruby-defun-beg-re
))
1584 (module-re (funcall make-definition-re
"\\(class\\|module\\)")))
1585 ;; Get the current method definition (or class/module).
1586 (when (re-search-backward definition-re nil t
)
1587 (goto-char (match-beginning 1))
1588 (if (not (string-equal "def" (match-string 1)))
1589 (setq mlist
(list (match-string 2)))
1590 ;; We're inside the method. For classes and modules,
1591 ;; this check is skipped for performance.
1592 (when (ruby-block-contains-point start
)
1593 (setq mname
(match-string 2))))
1594 (setq indent
(current-column))
1595 (beginning-of-line))
1596 ;; Walk up the class/module nesting.
1597 (while (and (> indent
0)
1598 (re-search-backward module-re nil t
))
1599 (goto-char (match-beginning 1))
1600 (when (< (current-column) indent
)
1601 (setq mlist
(cons (match-string 2) mlist
))
1602 (setq indent
(current-column))
1603 (beginning-of-line)))
1604 ;; Process the method name.
1606 (let ((mn (split-string mname
"\\.\\|::")))
1609 (unless (string-equal "self" (car mn
)) ; def self.foo
1611 (let ((ml (nreverse mlist
)))
1612 ;; If the method name references one of the
1613 ;; containing modules, drop the more nested ones.
1615 (if (string-equal (car ml
) (car mn
))
1616 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1617 (or (setq ml
(cdr ml
)) (nreverse mlist
))))
1619 (setcdr (last mlist
) (butlast mn
))
1620 (setq mlist
(butlast mn
))))
1621 (setq mname
(concat "." (car (last mn
)))))
1622 ;; See if the method is in singleton class context.
1623 (let ((in-singleton-class
1624 (when (re-search-forward ruby-singleton-class-re start t
)
1625 (goto-char (match-beginning 0))
1626 ;; FIXME: Optimize it out, too?
1627 ;; This can be slow in a large file, but
1628 ;; unlike class/module declaration
1629 ;; indentations, method definitions can be
1630 ;; intermixed with these, and may or may not
1631 ;; be additionally indented after visibility
1633 (ruby-block-contains-point start
))))
1635 (if in-singleton-class
"." "#")
1637 ;; Generate the string.
1639 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1641 (if mlist
(concat mlist mname
) mname
)
1644 (defun ruby-block-contains-point (pt)
1650 (defun ruby-brace-to-do-end (orig end
)
1651 (let (beg-marker end-marker
)
1653 (when (eq (char-before) ?\
})
1655 (when (save-excursion
1656 (skip-chars-backward " \t")
1660 (setq end-marker
(point-marker))
1661 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w
))
1665 (when (eq (char-syntax (char-before)) ?w
)
1668 (setq beg-marker
(point-marker))
1669 (when (looking-at "\\(\\s \\)*|")
1670 (unless (match-beginning 1)
1672 (goto-char (1+ (match-end 0)))
1673 (search-forward "|"))
1674 (unless (looking-at "\\s *$")
1676 (indent-region beg-marker end-marker
)
1677 (goto-char beg-marker
)
1680 (defun ruby-do-end-to-brace (orig end
)
1681 (let (beg-marker end-marker beg-pos end-pos
)
1682 (goto-char (- end
3))
1683 (when (looking-at ruby-block-end-re
)
1685 (setq end-marker
(point-marker))
1689 ;; Maybe this should be customizable, let's see if anyone asks.
1691 (setq beg-marker
(point-marker))
1692 (when (looking-at "\\s +|")
1693 (delete-char (- (match-end 0) (match-beginning 0) 1))
1695 (re-search-forward "|" (line-end-position) t
))
1697 (skip-chars-forward " \t\n\r")
1698 (setq beg-pos
(point))
1699 (goto-char end-marker
)
1700 (skip-chars-backward " \t\n\r")
1701 (setq end-pos
(point)))
1704 (and (= (line-number-at-pos beg-pos
) (line-number-at-pos end-pos
))
1705 (< (+ (current-column) (- end-pos beg-pos
) 2) fill-column
)))
1707 (goto-char end-marker
)
1708 (just-one-space -
1))
1709 (goto-char beg-marker
)
1712 (defun ruby-toggle-block ()
1713 "Toggle block type from do-end to braces or back.
1714 The block must begin on the current line or above it and end after the point.
1715 If the result is do-end block, it will always be multiline."
1717 (let ((start (point)) beg end
)
1720 (if (and (re-search-backward "\\(?:[^#]\\)\\({\\)\\|\\(\\_<do\\_>\\)")
1722 (goto-char (or (match-beginning 1) (match-beginning 2)))
1724 (save-match-data (ruby-forward-sexp))
1727 (if (match-beginning 1)
1728 (ruby-brace-to-do-end beg end
)
1729 (ruby-do-end-to-brace beg end
)))
1730 (goto-char start
))))
1733 (defconst ruby-percent-literal-beg-re
1734 "\\(%\\)[qQrswWxIi]?\\([[:punct:]]\\)"
1735 "Regexp to match the beginning of percent literal.")
1737 (defconst ruby-syntax-methods-before-regexp
1738 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1739 "assert_match" "Given" "Then" "When")
1740 "Methods that can take regexp as the first argument.
1741 It will be properly highlighted even when the call omits parens.")
1743 (defvar ruby-syntax-before-regexp-re
1745 ;; Special tokens that can't be followed by a division operator.
1747 ;; Distinguish ternary operator tokens.
1748 ;; FIXME: They don't really have to be separated with spaces.
1750 ;; Control flow keywords and operators following bol or whitespace.
1751 "\\|\\(?:^\\|\\s \\)"
1752 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1753 "or" "not" "&&" "||"))
1754 ;; Method name from the list.
1756 (regexp-opt ruby-syntax-methods-before-regexp
)
1758 "Regexp to match text that can be followed by a regular expression."))
1760 (defun ruby-syntax-propertize-function (start end
)
1761 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1762 (let (case-fold-search)
1764 (remove-text-properties start end
'(ruby-expansion-match-data))
1765 (ruby-syntax-propertize-heredoc end
)
1766 (ruby-syntax-enclosing-percent-literal end
)
1768 (syntax-propertize-rules
1769 ;; $' $" $` .... are variables.
1770 ;; ?' ?" ?` are character literals (one-char strings in 1.9+).
1771 ("\\([?$]\\)[#\"'`]"
1772 (1 (unless (save-excursion
1773 ;; Not within a string.
1774 (nth 3 (syntax-ppss (match-beginning 0))))
1775 (string-to-syntax "\\"))))
1776 ;; Part of symbol when at the end of a method name.
1778 (0 (unless (save-excursion
1779 (or (nth 8 (syntax-ppss (match-beginning 0)))
1780 (let (parse-sexp-lookup-properties)
1781 (zerop (skip-syntax-backward "w_")))
1782 (memq (preceding-char) '(?
@ ?$
))))
1783 (string-to-syntax "_"))))
1784 ;; Regular expressions. Start with matching unescaped slash.
1785 ("\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*\\(/\\)"
1786 (1 (let ((state (save-excursion (syntax-ppss (match-beginning 1)))))
1788 ;; Beginning of a regexp.
1789 (and (null (nth 8 state
))
1792 (looking-back ruby-syntax-before-regexp-re
1794 ;; End of regexp. We don't match the whole
1795 ;; regexp at once because it can have
1796 ;; string interpolation inside, or span
1798 (eq ?
/ (nth 3 state
)))
1799 (string-to-syntax "\"/")))))
1800 ;; Expression expansions in strings. We're handling them
1801 ;; here, so that the regexp rule never matches inside them.
1802 (ruby-expression-expansion-re
1803 (0 (ignore (ruby-syntax-propertize-expansion))))
1804 ("^=en\\(d\\)\\_>" (1 "!"))
1805 ("^\\(=\\)begin\\_>" (1 "!"))
1806 ;; Handle here documents.
1807 ((concat ruby-here-doc-beg-re
".*\\(\n\\)")
1808 (7 (unless (or (nth 8 (save-excursion
1809 (syntax-ppss (match-beginning 0))))
1810 (ruby-singleton-class-p (match-beginning 0)))
1811 (put-text-property (match-beginning 7) (match-end 7)
1812 'syntax-table
(string-to-syntax "\""))
1813 (ruby-syntax-propertize-heredoc end
))))
1814 ;; Handle percent literals: %w(), %q{}, etc.
1815 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re
)
1816 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end
)))))
1819 (defun ruby-syntax-propertize-heredoc (limit)
1820 (let ((ppss (syntax-ppss))
1822 (when (eq ?
\n (nth 3 ppss
))
1824 (goto-char (nth 8 ppss
))
1826 (while (re-search-forward ruby-here-doc-beg-re
1827 (line-end-position) t
)
1828 (unless (ruby-singleton-class-p (match-beginning 0))
1829 (push (concat (ruby-here-doc-end-match) "\n") res
))))
1831 ;; With multiple openers on the same line, we don't know in which
1832 ;; part `start' is, so we have to go back to the beginning.
1834 (goto-char (nth 8 ppss
))
1835 (setq res
(nreverse res
)))
1836 (while (and res
(re-search-forward (pop res
) limit
'move
))
1838 (put-text-property (1- (point)) (point)
1839 'syntax-table
(string-to-syntax "\""))))
1840 ;; End up at bol following the heredoc openers.
1841 ;; Propertize expression expansions from this point forward.
1844 (defun ruby-syntax-enclosing-percent-literal (limit)
1845 (let ((state (syntax-ppss))
1847 ;; When already inside percent literal, re-propertize it.
1848 (when (eq t
(nth 3 state
))
1849 (goto-char (nth 8 state
))
1850 (when (looking-at ruby-percent-literal-beg-re
)
1851 (ruby-syntax-propertize-percent-literal limit
))
1852 (when (< (point) start
) (goto-char start
)))))
1854 (defun ruby-syntax-propertize-percent-literal (limit)
1855 (goto-char (match-beginning 2))
1856 ;; Not inside a simple string or comment.
1857 (when (eq t
(nth 3 (syntax-ppss)))
1858 (let* ((op (char-after))
1859 (ops (char-to-string op
))
1860 (cl (or (cdr (aref (syntax-table) op
))
1861 (cdr (assoc op
'((?
< . ?
>))))))
1862 parse-sexp-lookup-properties
)
1866 (if cl
; Paired delimiters.
1867 ;; Delimiter pairs of the same kind can be nested
1868 ;; inside the literal, as long as they are balanced.
1869 ;; Create syntax table that ignores other characters.
1870 (with-syntax-table (make-char-table 'syntax-table nil
)
1871 (modify-syntax-entry op
(concat "(" (char-to-string cl
)))
1872 (modify-syntax-entry cl
(concat ")" ops
))
1873 (modify-syntax-entry ?
\\ "\\")
1875 (narrow-to-region (point) limit
)
1876 (forward-list))) ; skip to the paired character
1877 ;; Single character delimiter.
1878 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1879 (regexp-quote ops
)) limit nil
))
1880 ;; Found the closing delimiter.
1881 (put-text-property (1- (point)) (point) 'syntax-table
1882 (string-to-syntax "|")))
1883 ;; Unclosed literal, do nothing.
1884 ((scan-error search-failed
)))))))
1886 (defun ruby-syntax-propertize-expansion ()
1887 ;; Save the match data to a text property, for font-locking later.
1888 ;; Set the syntax of all double quotes and backticks to punctuation.
1889 (let* ((beg (match-beginning 2))
1891 (state (and beg
(save-excursion (syntax-ppss beg
)))))
1892 (when (ruby-syntax-expansion-allowed-p state
)
1893 (put-text-property beg
(1+ beg
) 'ruby-expansion-match-data
1896 (while (re-search-forward "[\"`]" end
'move
)
1897 (put-text-property (match-beginning 0) (match-end 0)
1898 'syntax-table
(string-to-syntax "."))))))
1900 (defun ruby-syntax-expansion-allowed-p (parse-state)
1901 "Return non-nil if expression expansion is allowed."
1902 (let ((term (nth 3 parse-state
)))
1904 ((memq term
'(?
\" ?
` ?
\n ?
/)))
1908 (goto-char (nth 8 parse-state
))
1909 (looking-at "%\\(?:[QWrxI]\\|\\W\\)")))))))
1911 (defun ruby-syntax-propertize-expansions (start end
)
1914 (while (re-search-forward ruby-expression-expansion-re end
'move
)
1915 (ruby-syntax-propertize-expansion))))
1917 (defun ruby-in-ppss-context-p (context &optional ppss
)
1918 (let ((ppss (or ppss
(syntax-ppss (point)))))
1920 ((eq context
'anything
)
1923 ((eq context
'string
)
1925 ((eq context
'heredoc
)
1926 (eq ?
\n (nth 3 ppss
)))
1927 ((eq context
'non-heredoc
)
1928 (and (ruby-in-ppss-context-p 'anything
)
1929 (not (ruby-in-ppss-context-p 'heredoc
))))
1930 ((eq context
'comment
)
1934 "Internal error on `ruby-in-ppss-context-p': "
1935 "context name `" (symbol-name context
) "' is unknown"))))
1938 (defvar ruby-font-lock-syntax-table
1939 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1940 (modify-syntax-entry ?_
"w" tbl
)
1942 "The syntax table to use for fontifying Ruby mode buffers.
1943 See `font-lock-syntax-table'.")
1945 (defconst ruby-font-lock-keyword-beg-re
"\\(?:^\\|[^.@$]\\|\\.\\.\\)")
1947 (defconst ruby-font-lock-keywords
1949 ("^\\s *def\\s +\\(?:[^( \t\n.]*\\.\\)?\\([^( \t\n]+\\)"
1950 1 font-lock-function-name-face
)
1953 ruby-font-lock-keyword-beg-re
1989 (1 font-lock-keyword-face
))
1990 ;; Some core methods.
1992 ruby-font-lock-keyword-beg-re
1994 '( ;; built-in methods on Kernel
2038 ;; keyword-like private methods on Module
2055 (1 font-lock-builtin-face
))
2056 ;; Here-doc beginnings.
2057 (,ruby-here-doc-beg-re
2058 (0 (unless (ruby-singleton-class-p (match-beginning 0))
2059 'font-lock-string-face
)))
2060 ;; Perl-ish keywords.
2061 "\\_<\\(?:BEGIN\\|END\\)\\_>\\|^__END__$"
2063 (,(concat ruby-font-lock-keyword-beg-re
2064 "\\_<\\(nil\\|self\\|true\\|false\\)\\_>")
2065 1 font-lock-variable-name-face
)
2066 ;; Keywords that evaluate to certain values.
2067 ("\\_<__\\(?:LINE\\|ENCODING\\|FILE\\)__\\_>"
2068 (0 font-lock-variable-name-face
))
2070 ("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
2071 2 font-lock-constant-face
)
2073 ("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
2074 1 font-lock-variable-name-face
)
2075 ("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
2076 0 font-lock-variable-name-face
)
2078 ("\\(?:\\_<\\|::\\)\\([A-Z]+\\(\\w\\|_\\)*\\)"
2079 1 (unless (eq ?\
( (char-after)) font-lock-type-face
))
2080 ("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]"
2081 (2 font-lock-constant-face
))
2082 ;; Conversion methods on Kernel.
2083 (,(concat ruby-font-lock-keyword-beg-re
2084 (regexp-opt '("Array" "Complex" "Float" "Hash"
2085 "Integer" "Rational" "String") 'symbols
))
2086 (1 font-lock-builtin-face
))
2087 ;; Expression expansion.
2088 (ruby-match-expression-expansion
2089 2 font-lock-variable-name-face t
)
2091 ("[^[:alnum:]_]\\(!\\)[^=]"
2092 1 font-lock-negation-char-face
)
2093 ;; Character literals.
2094 ;; FIXME: Support longer escape sequences.
2095 ("\\_<\\?\\\\?\\S " 0 font-lock-string-face
)
2097 "Additional expressions to highlight in Ruby mode.")
2099 (defun ruby-match-expression-expansion (limit)
2100 (let* ((prop 'ruby-expansion-match-data
)
2101 (pos (next-single-char-property-change (point) prop nil limit
))
2103 (when (and pos
(> pos
(point)))
2105 (or (and (setq value
(get-text-property pos prop
))
2106 (progn (set-match-data value
) t
))
2107 (ruby-match-expression-expansion limit
)))))
2110 (define-derived-mode ruby-mode prog-mode
"Ruby"
2111 "Major mode for editing Ruby code.
2114 (ruby-mode-variables)
2116 (setq-local imenu-create-index-function
'ruby-imenu-create-index
)
2117 (setq-local add-log-current-defun-function
'ruby-add-log-current-method
)
2118 (setq-local beginning-of-defun-function
'ruby-beginning-of-defun
)
2119 (setq-local end-of-defun-function
'ruby-end-of-defun
)
2121 (add-hook 'after-save-hook
'ruby-mode-set-encoding nil
'local
)
2122 (add-hook 'electric-indent-functions
'ruby--electric-indent-p nil
'local
)
2124 (setq-local font-lock-defaults
'((ruby-font-lock-keywords) nil nil
))
2125 (setq-local font-lock-keywords ruby-font-lock-keywords
)
2126 (setq-local font-lock-syntax-table ruby-font-lock-syntax-table
)
2128 (setq-local syntax-propertize-function
#'ruby-syntax-propertize-function
))
2130 ;;; Invoke ruby-mode when appropriate
2133 (add-to-list 'auto-mode-alist
2134 (cons (purecopy (concat "\\(?:\\."
2135 "rb\\|ru\\|rake\\|thor"
2136 "\\|jbuilder\\|gemspec\\|podspec"
2138 "\\(?:Gem\\|Rake\\|Cap\\|Thor"
2139 "Vagrant\\|Guard\\|Pod\\)file"
2140 "\\)\\'")) 'ruby-mode
))
2143 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
2144 (add-to-list 'interpreter-mode-alist
(cons (purecopy name
) 'ruby-mode
)))
2146 (provide 'ruby-mode
)
2148 ;;; ruby-mode.el ends here