1 ;;; ruby-mode.el --- Major mode for editing Ruby files
3 ;; Copyright (C) 1994-2013 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.
42 (eval-when-compile (require 'cl
))
45 "Major mode for editing Ruby code."
49 (defconst ruby-block-beg-keywords
50 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
51 "Keywords at the beginning of blocks.")
53 (defconst ruby-block-beg-re
54 (regexp-opt ruby-block-beg-keywords
)
55 "Regexp to match the beginning of blocks.")
57 (defconst ruby-non-block-do-re
58 (regexp-opt '("while" "until" "for" "rescue") 'symbols
)
59 "Regexp to match keywords that nest without blocks.")
61 (defconst ruby-indent-beg-re
62 (concat "^\\(\\s *" (regexp-opt '("class" "module" "def")) "\\|"
63 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin"))
65 "Regexp to match where the indentation gets deeper.")
67 (defconst ruby-modifier-beg-keywords
68 '("if" "unless" "while" "until")
69 "Modifiers that are the same as the beginning of blocks.")
71 (defconst ruby-modifier-beg-re
72 (regexp-opt ruby-modifier-beg-keywords
)
73 "Regexp to match modifiers same as the beginning of blocks.")
75 (defconst ruby-modifier-re
76 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords
))
77 "Regexp to match modifiers.")
79 (defconst ruby-block-mid-keywords
80 '("then" "else" "elsif" "when" "rescue" "ensure")
81 "Keywords where the indentation gets shallower in middle of block statements.")
83 (defconst ruby-block-mid-re
84 (regexp-opt ruby-block-mid-keywords
)
85 "Regexp to match where the indentation gets shallower in middle of block statements.")
87 (defconst ruby-block-op-keywords
89 "Regexp to match boolean keywords.")
91 (defconst ruby-block-hanging-re
92 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords
))
93 "Regexp to match hanging block modifiers.")
95 (defconst ruby-block-end-re
"\\_<end\\_>")
97 (defconst ruby-defun-beg-re
98 '"\\(def\\|class\\|module\\)"
99 "Regexp to match the beginning of a defun, in the general sense.")
101 (defconst ruby-singleton-class-re
103 "Regexp to match the beginning of a singleton class context.")
106 (defconst ruby-here-doc-beg-re
107 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
108 "Regexp to match the beginning of a heredoc.")
110 (defconst ruby-expression-expansion-re
111 "\\(?:[^\\]\\|\\=\\)\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)"))
113 (defun ruby-here-doc-end-match ()
114 "Return a regexp to find the end of a heredoc.
116 This should only be called after matching against `ruby-here-doc-beg-re'."
118 (if (match-string 2) "[ \t]*" nil
)
124 (defconst ruby-delimiter
125 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
127 "\\)\\_>\\|" ruby-block-end-re
128 "\\|^=begin\\|" ruby-here-doc-beg-re
))
130 (defconst ruby-negative
131 (concat "^[ \t]*\\(\\(" ruby-block-mid-re
"\\)\\>\\|"
132 ruby-block-end-re
"\\|}\\|\\]\\)")
133 "Regexp to match where the indentation gets shallower.")
135 (defconst ruby-operator-re
"[-,.+*/%&|^~=<>:]\\|\\\\$"
136 "Regexp to match operators.")
138 (defconst ruby-symbol-chars
"a-zA-Z0-9_"
139 "List of characters that symbol names may contain.")
141 (defconst ruby-symbol-re
(concat "[" ruby-symbol-chars
"]")
142 "Regexp to match symbols.")
144 (defvar ruby-use-smie t
)
146 (defvar ruby-mode-map
147 (let ((map (make-sparse-keymap)))
148 (unless ruby-use-smie
149 (define-key map
(kbd "M-C-b") 'ruby-backward-sexp
)
150 (define-key map
(kbd "M-C-f") 'ruby-forward-sexp
)
151 (define-key map
(kbd "M-C-q") 'ruby-indent-exp
))
153 (define-key map
(kbd "M-C-d") 'smie-down-list
))
154 (define-key map
(kbd "M-C-p") 'ruby-beginning-of-block
)
155 (define-key map
(kbd "M-C-n") 'ruby-end-of-block
)
156 (define-key map
(kbd "C-c {") 'ruby-toggle-block
)
158 "Keymap used in Ruby mode.")
165 ["Beginning of Block" ruby-beginning-of-block t
]
166 ["End of Block" ruby-end-of-block t
]
167 ["Toggle Block" ruby-toggle-block t
]
169 ["Backward Sexp" ruby-backward-sexp
170 :visible
(not ruby-use-smie
)]
171 ["Backward Sexp" backward-sexp
172 :visible ruby-use-smie
]
173 ["Forward Sexp" ruby-forward-sexp
174 :visible
(not ruby-use-smie
)]
175 ["Forward Sexp" forward-sexp
176 :visible ruby-use-smie
]
177 ["Indent Sexp" ruby-indent-exp
178 :visible
(not ruby-use-smie
)]
179 ["Indent Sexp" prog-indent-sexp
180 :visible ruby-use-smie
]))
182 (defvar ruby-mode-syntax-table
183 (let ((table (make-syntax-table)))
184 (modify-syntax-entry ?
\' "\"" table
)
185 (modify-syntax-entry ?
\" "\"" table
)
186 (modify-syntax-entry ?\
` "\"" table
)
187 (modify-syntax-entry ?
# "<" table
)
188 (modify-syntax-entry ?
\n ">" 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
)
208 (modify-syntax-entry ?\
} "){" table
)
209 (modify-syntax-entry ?\
[ "(]" table
)
210 (modify-syntax-entry ?\
] ")[" table
)
212 "Syntax table to use in Ruby mode.")
214 (defcustom ruby-indent-tabs-mode nil
215 "Indentation can insert tabs in Ruby mode if this is non-nil."
220 (defcustom ruby-indent-level
2
221 "Indentation of Ruby statements."
226 (defcustom ruby-comment-column
(default-value 'comment-column
)
227 "Indentation column of comments."
232 (defcustom ruby-deep-arglist t
233 "Deep indent lists in parenthesis when non-nil.
234 Also ignores spaces after parenthesis when 'space."
239 (defcustom ruby-deep-indent-paren
'(?\
( ?\
[ ?\
] t
)
240 "Deep indent lists in parenthesis when non-nil.
241 The value t means continuous line.
242 Also ignores spaces after parenthesis when 'space."
245 (defcustom ruby-deep-indent-paren-style
'space
246 "Default deep indent style."
247 :options
'(t nil space
) :group
'ruby
)
249 (defcustom ruby-encoding-map
250 '((us-ascii . nil
) ;; Do not put coding: us-ascii
251 (shift-jis . cp932
) ;; Emacs charset name of Shift_JIS
252 (shift_jis . cp932
) ;; MIME charset name of Shift_JIS
253 (japanese-cp932 . cp932
)) ;; Emacs charset name of CP932
254 "Alist to map encoding name from Emacs to Ruby.
255 Associating an encoding name with nil means it needs not be
256 explicitly declared in magic comment."
257 :type
'(repeat (cons (symbol :tag
"From") (symbol :tag
"To")))
260 (defcustom ruby-insert-encoding-magic-comment t
261 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
262 :type
'boolean
:group
'ruby
)
264 (defcustom ruby-use-encoding-map t
265 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
266 :type
'boolean
:group
'ruby
)
272 ;; Here's a simplified BNF grammar, for reference:
273 ;; http://www.cse.buffalo.edu/~regan/cse305/RubyBNF.pdf
274 (defconst ruby-smie-grammar
279 (insts (inst) (insts ";" insts
))
280 (inst (exp) (inst "iuwu-mod" exp
))
281 (exp (exp1) (exp "," exp
) (exp "=" exp
)
284 (exp1 (exp2) (exp2 "?" exp1
":" exp1
))
285 (exp2 ("def" insts
"end")
286 ("begin" insts-rescue-insts
"end")
288 ("class" insts
"end") ("module" insts
"end")
289 ("for" for-body
"end")
293 ("while" insts
"end")
294 ("until" insts
"end")
295 ("unless" insts
"end")
297 ("case" cases
"end"))
298 (formal-params ("opening-|" exp
"|"))
299 (for-body (for-head ";" insts
))
300 (for-head (id "in" exp
))
301 (cases (exp "then" insts
) ;; FIXME: Ruby also allows (exp ":" insts).
302 (cases "when" cases
) (insts "else" insts
))
303 (expseq (exp) );;(expseq "," expseq)
304 (hashvals (id "=>" exp1
) (hashvals "," hashvals
))
305 (insts-rescue-insts (insts)
306 (insts-rescue-insts "rescue" insts-rescue-insts
)
307 (insts-rescue-insts "ensure" insts-rescue-insts
))
308 (itheni (insts) (exp "then" insts
))
309 (ielsei (itheni) (itheni "else" insts
))
310 (if-body (ielsei) (if-body "elsif" if-body
)))
311 '((nonassoc "in") (assoc ";") (right " @ ")
312 (assoc ",") (right "=") (assoc "."))
315 '((assoc "rescue" "ensure"))
320 (right "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^="
321 "<<=" ">>=" "&&=" "||=")
324 (left "*" "/" "%" "**")
325 ;; (left "|") ; FIXME: Conflicts with | after block parameters.
328 (nonassoc ">" ">=" "<" "<=")
329 (nonassoc "==" "===" "!=")
332 (left "&&" "||"))))))
334 (defun ruby-smie--bosp ()
335 (save-excursion (skip-chars-backward " \t")
336 (or (bolp) (memq (char-before) '(?\
; ?=)))))
338 (defun ruby-smie--implicit-semi-p ()
340 (skip-chars-backward " \t")
342 (and (memq (char-before)
343 '(?\
; ?- ?+ ?* ?/ ?: ?. ?, ?\[ ?\( ?\{ ?\\ ?& ?> ?< ?% ?~))
344 ;; Make sure it's not the end of a regexp.
345 (not (eq (car (syntax-after (1- (point)))) 7)))
346 (and (eq (char-before) ?
\?)
347 (equal (save-excursion (ruby-smie--backward-token)) "?"))
348 (and (eq (char-before) ?
=)
349 (string-match "\\`\\s." (save-excursion
350 (ruby-smie--backward-token))))
351 (and (eq (car (syntax-after (1- (point)))) 2)
352 (equal (save-excursion (ruby-smie--backward-token))
356 (eq (char-after) ?.
))))))
358 (defun ruby-smie--redundant-do-p (&optional skip
)
360 (if skip
(backward-word 1))
361 (member (nth 2 (smie-backward-sexp ";")) '("while" "until" "for"))))
363 (defun ruby-smie--opening-pipe-p ()
365 (if (eq ?|
(char-before)) (forward-char -
1))
366 (skip-chars-backward " \t\n")
367 (or (eq ?\
{ (char-before))
368 (looking-back "\\_<do" (- (point) 2)))))
370 (defun ruby-smie--args-separator-p (pos)
372 (< pos
(line-end-position))
373 (or (eq (char-syntax (preceding-char)) '?w
)
374 ;; FIXME: Check that the preceding token is not a keyword.
375 ;; This isn't very important most of the time, though.
376 (and (memq (preceding-char) '(?
! ??
))
377 (eq (char-syntax (char-before (1- (point)))) '?w
)))
378 (or (and (eq (char-syntax (char-after pos
)) ?w
)
379 (not (looking-at (regexp-opt '("unless" "if" "while" "until"
380 "else" "elsif" "do" "end")
382 (memq (syntax-after pos
) '(7 15))
385 (looking-at "\\s(\\|[-+!~:]\\sw")))))
387 (defun ruby-smie--at-dot-call ()
388 (and (eq ?w
(char-syntax (following-char)))
389 (eq (char-before) ?.
)
390 (not (eq (char-before (1- (point))) ?.
))))
392 (defun ruby-smie--forward-token ()
394 (skip-chars-forward " \t")
396 ((looking-at "\\s\"") "") ;A heredoc or a string.
397 ((and (looking-at "[\n#]")
398 (ruby-smie--implicit-semi-p)) ;Only add implicit ; when needed.
399 (if (eolp) (forward-char 1) (forward-comment 1))
402 (forward-comment (point-max))
404 ((looking-at ":\\s.+")
405 (goto-char (match-end 0)) (match-string 0)) ;; bug#15208.
406 ((and (< pos
(point))
408 (ruby-smie--args-separator-p (prog1 (point) (goto-char pos
)))))
411 (let ((dot (ruby-smie--at-dot-call))
412 (tok (smie-default-forward-token)))
414 (setq tok
(concat "." tok
)))
416 ((member tok
'("unless" "if" "while" "until"))
417 (if (save-excursion (forward-word -
1) (ruby-smie--bosp))
420 (if (ruby-smie--opening-pipe-p) "opening-|" tok
))
421 ((and (equal tok
"") (looking-at "\\\\\n"))
422 (goto-char (match-end 0)) (ruby-smie--forward-token))
425 ((not (ruby-smie--redundant-do-p 'skip
)) tok
)
426 ((> (save-excursion (forward-comment (point-max)) (point))
428 (ruby-smie--forward-token)) ;Fully redundant.
432 (defun ruby-smie--backward-token ()
434 (forward-comment (- (point)))
436 ((and (> pos
(line-end-position)) (ruby-smie--implicit-semi-p))
437 (skip-chars-forward " \t") ";")
438 ((and (bolp) (not (bobp))) "") ;Presumably a heredoc.
439 ((and (> pos
(point)) (not (bolp))
440 (ruby-smie--args-separator-p pos
))
441 ;; We have "ID SPC ID", which is a method call, but it binds less tightly
442 ;; than commas, since a method call can also be "ID ARG1, ARG2, ARG3".
443 ;; In some textbooks, "e1 @ e2" is used to mean "call e1 with arg e2".
446 (let ((tok (smie-default-backward-token))
447 (dot (ruby-smie--at-dot-call)))
449 (setq tok
(concat "." tok
)))
450 (when (and (eq ?
: (char-before)) (string-match "\\`\\s." tok
))
451 (forward-char -
1) (setq tok
(concat ":" tok
))) ;; bug#15208.
453 ((member tok
'("unless" "if" "while" "until"))
454 (if (ruby-smie--bosp)
457 (if (ruby-smie--opening-pipe-p) "opening-|" tok
))
458 ((and (equal tok
"") (eq ?
\\ (char-before)) (looking-at "\n"))
459 (forward-char -
1) (ruby-smie--backward-token))
462 ((not (ruby-smie--redundant-do-p)) tok
)
463 ((> (save-excursion (forward-word 1)
464 (forward-comment (point-max)) (point))
466 (ruby-smie--backward-token)) ;Fully redundant.
470 (defun ruby-smie-rules (kind token
)
471 (pcase (cons kind token
)
472 (`(:elem . basic
) ruby-indent-level
)
473 ;; "foo" "bar" is the concatenation of the two strings, so the second
474 ;; should be aligned with the first.
475 (`(:elem . args
) (if (looking-at "\\s\"") 0))
476 ;; (`(:after . ",") (smie-rule-separator kind))
479 ((smie-rule-parent-p "def" "begin" "do" "class" "module" "for"
480 "while" "until" "unless"
481 "if" "then" "elsif" "else" "when"
482 "rescue" "ensure" "{")
483 (smie-rule-parent ruby-indent-level
))
484 ;; For (invalid) code between switch and case.
485 ;; (if (smie-parent-p "switch") 4)
487 (`(:before .
,(or `"(" `"[" `"{"))
489 ((and (equal token
"{")
490 (not (smie-rule-prev-p "(" "{" "[" "," "=>" "=" "return" ";")))
491 ;; Curly block opener.
493 ((smie-rule-hanging-p)
494 ;; Treat purely syntactic block-constructs as being part of their parent,
495 ;; when the opening statement is hanging.
496 (let ((state (smie-backward-sexp 'halfsexp
)))
497 (when (eq t
(car state
)) (goto-char (cadr state
))))
498 (cons 'column
(smie-indent-virtual)))))
499 (`(:after .
,(or "=" "iuwu-mod")) 2)
500 (`(:after .
" @ ") (smie-rule-parent))
501 (`(:before .
"do") (smie-rule-parent))
502 (`(,(or :before
:after
) .
".")
503 (unless (smie-rule-parent-p ".")
504 (smie-rule-parent ruby-indent-level
)))
505 (`(:before .
,(or `"else" `"then" `"elsif" `"rescue" `"ensure")) 0)
506 (`(:before .
,(or `"when"))
507 (if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level
508 (`(:after .
"+") ;FIXME: Probably applicable to most infix operators.
509 (if (smie-rule-parent-p ";") ruby-indent-level
))
512 (defun ruby-imenu-create-index-in-block (prefix beg end
)
513 "Create an imenu index of methods inside a block."
514 (let ((index-alist '()) (case-fold-search nil
)
515 name next pos decl sing
)
517 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t
)
518 (setq sing
(match-beginning 3))
519 (setq decl
(match-string 5))
520 (setq next
(match-end 0))
521 (setq name
(or (match-string 4) (match-string 6)))
522 (setq pos
(match-beginning 0))
524 ((string= "alias" decl
)
525 (if prefix
(setq name
(concat prefix name
)))
526 (push (cons name pos
) index-alist
))
527 ((string= "def" decl
)
531 ((string-match "^self\." name
)
532 (concat (substring prefix
0 -
1) (substring name
4)))
533 (t (concat prefix name
)))))
534 (push (cons name pos
) index-alist
)
535 (ruby-accurate-end-of-block end
))
537 (if (string= "self" name
)
538 (if prefix
(setq name
(substring prefix
0 -
1)))
539 (if prefix
(setq name
(concat (substring prefix
0 -
1) "::" name
)))
540 (push (cons name pos
) index-alist
))
541 (ruby-accurate-end-of-block end
)
544 (nconc (ruby-imenu-create-index-in-block
545 (concat name
(if sing
"." "#"))
546 next beg
) index-alist
))
550 (defun ruby-imenu-create-index ()
551 "Create an imenu index of all methods in the buffer."
552 (nreverse (ruby-imenu-create-index-in-block nil
(point-min) nil
)))
554 (defun ruby-accurate-end-of-block (&optional end
)
557 (end (or end
(point-max))))
558 (while (and (setq state
(apply 'ruby-parse-partial end state
))
559 (>= (nth 2 state
) 0) (< (point) end
)))))
561 (defun ruby-mode-variables ()
562 "Set up initial buffer-local variables for Ruby mode."
563 (setq indent-tabs-mode ruby-indent-tabs-mode
)
565 (smie-setup ruby-smie-grammar
#'ruby-smie-rules
566 :forward-token
#'ruby-smie--forward-token
567 :backward-token
#'ruby-smie--backward-token
)
568 (set (make-local-variable 'indent-line-function
) 'ruby-indent-line
))
569 (set (make-local-variable 'require-final-newline
) t
)
570 (set (make-local-variable 'comment-start
) "# ")
571 (set (make-local-variable 'comment-end
) "")
572 (set (make-local-variable 'comment-column
) ruby-comment-column
)
573 (set (make-local-variable 'comment-start-skip
) "#+ *")
574 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
575 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
576 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
577 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
578 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
))
580 (defun ruby-mode-set-encoding ()
581 "Insert a magic comment header with the proper encoding if necessary."
584 (goto-char (point-min))
585 (when (re-search-forward "[^\0-\177]" nil t
)
586 (goto-char (point-min))
588 (or save-buffer-coding-system
589 buffer-file-coding-system
)))
592 (or (coding-system-get coding-system
'mime-charset
)
593 (coding-system-change-eol-conversion coding-system nil
))))
597 (if ruby-use-encoding-map
598 (let ((elt (assq coding-system ruby-encoding-map
)))
599 (if elt
(cdr elt
) coding-system
))
603 (if (looking-at "^#!") (beginning-of-line 2))
604 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
605 (unless (string= (match-string 2) coding-system
)
606 (goto-char (match-beginning 2))
607 (delete-region (point) (match-end 2))
608 (and (looking-at "-\*-")
609 (let ((n (skip-chars-backward " ")))
610 (cond ((= n
0) (insert " ") (backward-char))
611 ((= n -
1) (insert " "))
613 (insert coding-system
)))
614 ((looking-at "\\s *#.*coding\\s *[:=]"))
615 (t (when ruby-insert-encoding-magic-comment
616 (insert "# -*- coding: " coding-system
" -*-\n"))))
617 (when (buffer-modified-p)
618 (basic-save-buffer-1)))))))
620 (defun ruby-current-indentation ()
621 "Return the indentation level of current line."
624 (back-to-indentation)
627 (defun ruby-indent-line (&optional ignored
)
628 "Correct the indentation of the current Ruby line."
630 (ruby-indent-to (ruby-calculate-indent)))
632 (defun ruby-indent-to (column)
633 "Indent the current line to COLUMN."
636 (and (< column
0) (error "invalid nest"))
637 (setq shift
(current-column))
640 (back-to-indentation)
641 (setq top
(current-column))
642 (skip-chars-backward " \t")
643 (if (>= shift top
) (setq shift
(- shift top
))
647 (move-to-column (+ column shift
))
649 (delete-region beg
(point))
652 (move-to-column (+ column shift
))))))
654 (defun ruby-special-char-p (&optional pos
)
655 "Return t if the character before POS is a special character.
656 If omitted, POS defaults to the current point.
657 Special characters are `?', `$', `:' when preceded by whitespace,
658 and `\\' when preceded by `?'."
659 (setq pos
(or pos
(point)))
660 (let ((c (char-before pos
)) (b (and (< (point-min) pos
)
661 (char-before (1- pos
)))))
662 (cond ((or (eq c ??
) (eq c ?$
)))
663 ((and (eq c ?
:) (or (not b
) (eq (char-syntax b
) ?
))))
664 ((eq c ?
\\) (eq b ??
)))))
666 (defun ruby-singleton-class-p (&optional pos
)
668 (when pos
(goto-char pos
))
670 (and (or (bolp) (not (eq (char-before (point)) ?_
)))
671 (looking-at ruby-singleton-class-re
))))
673 (defun ruby-expr-beg (&optional option
)
674 "Check if point is possibly at the beginning of an expression.
675 OPTION specifies the type of the expression.
676 Can be one of `heredoc', `modifier', `expr-qstr', `expr-re'."
678 (store-match-data nil
)
679 (let ((space (skip-chars-backward " \t"))
685 (and (looking-at "\\?")
686 (or (eq (char-syntax (char-before (point))) ?w
)
687 (ruby-special-char-p))))
689 ((looking-at ruby-operator-re
))
690 ((eq option
'heredoc
)
691 (and (< space
0) (not (ruby-singleton-class-p start
))))
692 ((or (looking-at "[\\[({,;]")
693 (and (looking-at "[!?]")
694 (or (not (eq option
'modifier
))
696 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
697 (and (looking-at ruby-symbol-re
)
698 (skip-chars-backward ruby-symbol-chars
)
700 ((looking-at (regexp-opt
701 (append ruby-block-beg-keywords
702 ruby-block-op-keywords
703 ruby-block-mid-keywords
)
705 (goto-char (match-end 0))
706 (not (looking-at "\\s_\\|!")))
707 ((eq option
'expr-qstr
)
708 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
709 ((eq option
'expr-re
)
710 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
713 (defun ruby-forward-string (term &optional end no-error expand
)
715 (let ((n 1) (c (string-to-char term
))
717 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term
"]\\|\\(#{\\)\\)")
718 (concat "[^\\]\\(\\\\\\\\\\)*[" term
"]"))))
719 (while (and (re-search-forward re end no-error
)
720 (if (match-beginning 3)
721 (ruby-forward-string "}{" end no-error nil
)
722 (> (setq n
(if (eq (char-before (point)) c
)
727 ((error "unterminated string")))))
729 (defun ruby-deep-indent-paren-p (c)
731 (cond ((listp ruby-deep-indent-paren
)
732 (let ((deep (assoc c ruby-deep-indent-paren
)))
734 (or (cdr deep
) ruby-deep-indent-paren-style
))
735 ((memq c ruby-deep-indent-paren
)
736 ruby-deep-indent-paren-style
))))
737 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
738 ((eq c ?\
( ) ruby-deep-arglist
)))
740 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
741 "TODO: document throughout function body."
742 (or depth
(setq depth
0))
743 (or indent
(setq indent
0))
744 (when (re-search-forward ruby-delimiter end
'move
)
745 (let ((pnt (point)) w re expand
)
746 (goto-char (match-beginning 0))
748 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
750 ((looking-at "[\"`]") ;skip string
753 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t
))
756 (setq in-string
(point))
761 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
764 (setq in-string
(point))
770 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
771 (if (ruby-forward-string "/" end t t
)
773 (setq in-string
(point))
780 (ruby-expr-beg 'expr-qstr
)
781 (not (looking-at "%="))
782 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
783 (goto-char (match-beginning 1))
784 (setq expand
(not (memq (char-before) '(?q ?w
))))
785 (setq w
(match-string 1))
787 ((string= w
"[") (setq re
"]["))
788 ((string= w
"{") (setq re
"}{"))
789 ((string= w
"(") (setq re
")("))
790 ((string= w
"<") (setq re
"><"))
791 ((and expand
(string= w
"\\"))
792 (setq w
(concat "\\" w
))))
793 (unless (cond (re (ruby-forward-string re end t expand
))
794 (expand (ruby-forward-string w end t t
))
795 (t (re-search-forward
798 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
800 (setq in-string
(point))
804 ((looking-at "\\?") ;skip ?char
806 ((and (ruby-expr-beg)
807 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
808 (goto-char (match-end 0)))
811 ((looking-at "\\$") ;skip $char
814 ((looking-at "#") ;skip comment
818 ((looking-at "[\\[{(]")
819 (let ((deep (ruby-deep-indent-paren-p (char-after))))
820 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
822 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
823 (setq pnt
(1- (match-end 0))))
824 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
825 (setq pcol
(cons (cons pnt depth
) pcol
))
827 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
828 (setq depth
(1+ depth
))))
831 ((looking-at "[])}]")
832 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
833 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
834 (setq depth
(1- depth
)))
835 (setq nest
(cdr nest
))
837 ((looking-at ruby-block-end-re
)
838 (if (or (and (not (bolp))
841 (setq w
(char-after (point)))
846 (setq w
(char-after (point)))
851 (setq nest
(cdr nest
))
852 (setq depth
(1- depth
)))
854 ((looking-at "def\\s +[^(\n;]*")
858 (not (eq ?_
(char-after (point))))))
860 (setq nest
(cons (cons nil pnt
) nest
))
861 (setq depth
(1+ depth
))))
862 (goto-char (match-end 0)))
863 ((looking-at (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
866 (or (not (looking-at "do\\_>"))
868 (back-to-indentation)
869 (not (looking-at ruby-non-block-do-re
)))))
873 (setq w
(char-after (point)))
877 (not (eq ?
! (char-after (point))))
878 (skip-chars-forward " \t")
879 (goto-char (match-beginning 0))
880 (or (not (looking-at ruby-modifier-re
))
881 (ruby-expr-beg 'modifier
))
883 (setq nest
(cons (cons nil pnt
) nest
))
884 (setq depth
(1+ depth
)))
886 ((looking-at ":\\(['\"]\\)")
887 (goto-char (match-beginning 1))
888 (ruby-forward-string (match-string 1) end t
))
889 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
890 (goto-char (match-end 0)))
891 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
892 (goto-char (match-end 0)))
893 ((or (looking-at "\\.\\.\\.?")
894 (looking-at "\\.[0-9]+")
895 (looking-at "\\.[a-zA-Z_0-9]+")
897 (goto-char (match-end 0)))
898 ((looking-at "^=begin")
899 (if (re-search-forward "^=end" end t
)
901 (setq in-string
(match-end 0))
905 ((and (ruby-expr-beg 'heredoc
)
906 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
907 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
908 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
909 (let* ((id-end (goto-char (match-end 0)))
910 (line-end-position (point-at-eol))
911 (state (list in-string nest depth pcol indent
)))
912 ;; parse the rest of the line
913 (while (and (> line-end-position
(point))
914 (setq state
(apply 'ruby-parse-partial
915 line-end-position state
))))
916 (setq in-string
(car state
)
920 indent
(nth 4 state
))
921 ;; skip heredoc section
922 (if (re-search-forward (concat "^" re
"$") end
'move
)
924 (setq in-string id-end
)
928 ((looking-at "^__END__$")
930 ((and (looking-at ruby-here-doc-beg-re
)
931 (boundp 'ruby-indent-point
))
932 (if (re-search-forward (ruby-here-doc-end-match)
935 (setq in-string
(match-end 0))
936 (goto-char ruby-indent-point
)))
938 (error (format "bad string %s"
939 (buffer-substring (point) pnt
)
941 (list in-string nest depth pcol
))
943 (defun ruby-parse-region (start end
)
949 (ruby-beginning-of-indent))
951 (narrow-to-region (point) end
)
952 (while (and (> end
(point))
953 (setq state
(apply 'ruby-parse-partial end state
))))))
954 (list (nth 0 state
) ; in-string
955 (car (nth 1 state
)) ; nest
956 (nth 2 state
) ; depth
957 (car (car (nth 3 state
))) ; pcol
958 ;(car (nth 5 state)) ; indent
961 (defun ruby-indent-size (pos nest
)
962 "Return the indentation level in spaces NEST levels deeper than POS."
963 (+ pos
(* (or nest
1) ruby-indent-level
)))
965 (defun ruby-calculate-indent (&optional parse-start
)
966 "Return the proper indentation level of the current line."
967 ;; TODO: Document body
970 (let ((ruby-indent-point (point))
971 (case-fold-search nil
)
972 state eol begin op-end
973 (paren (progn (skip-syntax-forward " ")
974 (and (char-after) (matching-paren (char-after)))))
977 (goto-char parse-start
)
978 (ruby-beginning-of-indent)
979 (setq parse-start
(point)))
980 (back-to-indentation)
981 (setq indent
(current-column))
982 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
984 ((nth 0 state
) ; within string
985 (setq indent nil
)) ; do nothing
986 ((car (nth 1 state
)) ; in paren
987 (goto-char (setq begin
(cdr (nth 1 state
))))
988 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
990 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
991 (skip-syntax-backward " ")
992 (setq indent
(1- (current-column))))
993 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
994 (and (nth 2 s
) (> (nth 2 s
) 0)
995 (or (goto-char (cdr (nth 1 s
))) t
)))
997 (setq indent
(ruby-indent-size (current-column)
1000 (setq indent
(current-column))
1001 (cond ((eq deep
'space
))
1002 (paren (setq indent
(1- indent
)))
1003 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
1004 (if (nth 3 state
) (goto-char (nth 3 state
))
1005 (goto-char parse-start
) (back-to-indentation))
1006 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
1007 (and (eq (car (nth 1 state
)) paren
)
1008 (ruby-deep-indent-paren-p (matching-paren paren
))
1009 (search-backward (char-to-string paren
))
1010 (setq indent
(current-column)))))
1011 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
1012 (if (null (cdr (nth 1 state
)))
1013 (error "invalid nest"))
1014 (goto-char (cdr (nth 1 state
)))
1015 (forward-word -
1) ; skip back a keyword
1016 (setq begin
(point))
1018 ((looking-at "do\\>[^_]") ; iter block is a special case
1019 (if (nth 3 state
) (goto-char (nth 3 state
))
1020 (goto-char parse-start
) (back-to-indentation))
1021 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
1023 (setq indent
(+ (current-column) ruby-indent-level
)))))
1025 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
1026 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
1028 (goto-char ruby-indent-point
)
1033 ((and (not (ruby-deep-indent-paren-p paren
))
1034 (re-search-forward ruby-negative eol t
))
1035 (and (not (eq ?_
(char-after (match-end 0))))
1036 (setq indent
(- indent ruby-indent-level
))))
1041 (or (ruby-deep-indent-paren-p t
)
1042 (null (car (nth 1 state
)))))
1043 ;; goto beginning of non-empty no-comment line
1046 (skip-chars-backward " \t\n")
1049 (if (re-search-forward "^\\s *#" end t
)
1053 ;; skip the comment at the end
1054 (skip-chars-backward " \t")
1055 (let (end (pos (point)))
1057 (while (and (re-search-forward "#" pos t
)
1058 (setq end
(1- (point)))
1059 (or (ruby-special-char-p end
)
1060 (and (setq state
(ruby-parse-region parse-start end
))
1063 (goto-char (or end pos
))
1064 (skip-chars-backward " \t")
1065 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
1066 (setq state
(ruby-parse-region parse-start
(point))))
1067 (or (bobp) (forward-char -
1))
1069 (or (and (looking-at ruby-symbol-re
)
1070 (skip-chars-backward ruby-symbol-chars
)
1071 (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>"))
1072 (not (eq (point) (nth 3 state
)))
1074 (goto-char (match-end 0))
1075 (not (looking-at "[a-z_]"))))
1076 (and (looking-at ruby-operator-re
)
1077 (not (ruby-special-char-p))
1080 (or (not (looking-at ruby-operator-re
))
1081 (not (eq (char-before) ?
:))))
1082 ;; Operator at the end of line.
1083 (let ((c (char-after (point))))
1087 ;; (goto-char begin)
1088 ;; (skip-chars-forward " \t")
1089 ;; (not (or (eolp) (looking-at "#")
1090 ;; (and (eq (car (nth 1 state)) ?{)
1091 ;; (looking-at "|"))))))
1092 ;; Not a regexp or percent literal.
1093 (null (nth 0 (ruby-parse-region (or begin parse-start
)
1095 (or (not (eq ?|
(char-after (point))))
1097 (or (eolp) (forward-char -
1))
1099 ((search-backward "|" nil t
)
1100 (skip-chars-backward " \t\n")
1104 (not (looking-at "{")))
1107 (not (looking-at "do\\>[^_]")))))
1115 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>")))
1116 (eq (ruby-deep-indent-paren-p t
) 'space
)
1119 (goto-char (or begin parse-start
))
1120 (skip-syntax-forward " ")
1122 ((car (nth 1 state
)) indent
)
1124 (+ indent ruby-indent-level
))))))))
1125 (goto-char ruby-indent-point
)
1127 (skip-syntax-forward " ")
1128 (if (looking-at "\\.[^.]")
1129 (+ indent ruby-indent-level
)
1132 (defun ruby-beginning-of-defun (&optional arg
)
1133 "Move backward to the beginning of the current defun.
1134 With ARG, move backward multiple defuns. Negative ARG means
1137 (let (case-fold-search)
1138 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re
"\\_>")
1140 (beginning-of-line))))
1142 (defun ruby-end-of-defun ()
1143 "Move point to the end of the current defun.
1144 The defun begins at or after the point. This function is called
1148 (let (case-fold-search)
1149 (when (looking-back (concat "^\\s *" ruby-block-end-re
))
1152 (defun ruby-beginning-of-indent ()
1153 "Backtrack to a line which can be used as a reference for
1154 calculating indentation on the lines after it."
1155 (while (and (re-search-backward ruby-indent-beg-re nil
'move
)
1156 (if (ruby-in-ppss-context-p 'anything
)
1158 ;; We can stop, then.
1159 (beginning-of-line)))))
1161 (defun ruby-move-to-block (n)
1162 "Move to the beginning (N < 0) or the end (N > 0) of the
1163 current block, a sibling block, or an outer block. Do that (abs N) times."
1164 (back-to-indentation)
1165 (let ((signum (if (> n
0) 1 -
1))
1167 (depth (or (nth 2 (ruby-parse-region (point) (line-end-position))) 0))
1170 (when (looking-at ruby-block-mid-re
)
1171 (setq depth
(+ depth signum
)))
1172 (when (< (* depth signum
) 0)
1173 ;; Moving end -> end or beginning -> beginning.
1175 (dotimes (_ (abs n
))
1177 (setq down
(save-excursion
1178 (back-to-indentation)
1179 ;; There is a block start or block end keyword on this
1180 ;; line, don't need to look for another block.
1181 (and (re-search-forward
1182 (if backward ruby-block-end-re
1183 (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
1184 (line-end-position) t
)
1185 (not (nth 8 (syntax-ppss))))))
1186 (while (and (not done
) (not (if backward
(bobp) (eobp))))
1187 (forward-line signum
)
1189 ;; Skip empty and commented out lines.
1190 ((looking-at "^\\s *$"))
1191 ((looking-at "^\\s *#"))
1192 ;; Skip block comments;
1193 ((and (not backward
) (looking-at "^=begin\\>"))
1194 (re-search-forward "^=end\\>"))
1195 ((and backward
(looking-at "^=end\\>"))
1196 (re-search-backward "^=begin\\>"))
1197 ;; Jump over a multiline literal.
1198 ((ruby-in-ppss-context-p 'string
)
1199 (goto-char (nth 8 (syntax-ppss)))
1202 (when (bolp) (forward-char -
1)))) ; After a heredoc.
1204 (let ((state (ruby-parse-region (point) (line-end-position))))
1205 (unless (car state
) ; Line ends with unfinished string.
1206 (setq depth
(+ (nth 2 state
) depth
))))
1208 ;; Increased depth, we found a block.
1209 ((> (* signum depth
) 0)
1211 ;; We're at the same depth as when we started, and we've
1212 ;; encountered a block before. Stop.
1213 ((and down
(zerop depth
))
1215 ;; Lower depth, means outer block, can stop now.
1216 ((< (* signum depth
) 0)
1218 (back-to-indentation)))
1220 (defun ruby-beginning-of-block (&optional arg
)
1221 "Move backward to the beginning of the current block.
1222 With ARG, move up multiple blocks."
1224 (ruby-move-to-block (- (or arg
1))))
1226 (defun ruby-end-of-block (&optional arg
)
1227 "Move forward to the end of the current block.
1228 With ARG, move out of multiple blocks."
1230 (ruby-move-to-block (or arg
1)))
1232 (defun ruby-forward-sexp (&optional arg
)
1233 "Move forward across one balanced expression (sexp).
1234 With ARG, do it many times. Negative ARG means move backward."
1235 ;; TODO: Document body
1238 (ruby-use-smie (forward-sexp arg
))
1239 ((and (numberp arg
) (< arg
0)) (ruby-backward-sexp (- arg
)))
1241 (let ((i (or arg
1)))
1244 (skip-syntax-forward " ")
1245 (if (looking-at ",\\s *") (goto-char (match-end 0)))
1246 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
1247 (goto-char (match-end 0)))
1249 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
1250 (looking-at "\\s("))
1251 (goto-char (scan-sexps (point) 1)))
1252 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
1253 (not (eq (char-before (point)) ?.
))
1254 (not (eq (char-before (point)) ?
:)))
1257 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
1259 (while (progn (forward-word 1) (looking-at "_")))
1260 (cond ((looking-at "::") (forward-char 2) t
)
1261 ((> (skip-chars-forward ".") 0))
1262 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
1263 (forward-char 1) nil
)))))
1267 (setq expr
(or expr
(ruby-expr-beg)
1268 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
1269 (nth 1 (setq state
(apply 'ruby-parse-partial nil state
))))
1271 (skip-chars-forward "<"))
1274 ((error) (forward-word 1)))
1277 (defun ruby-backward-sexp (&optional arg
)
1278 "Move backward across one balanced expression (sexp).
1279 With ARG, do it many times. Negative ARG means move forward."
1280 ;; TODO: Document body
1283 (ruby-use-smie (backward-sexp arg
))
1284 ((and (numberp arg
) (< arg
0)) (ruby-forward-sexp (- arg
)))
1286 (let ((i (or arg
1)))
1289 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
1291 (cond ((looking-at "\\s)")
1292 (goto-char (scan-sexps (1+ (point)) -
1))
1294 (?%
(forward-char -
1))
1295 ((?q ?Q ?w ?W ?r ?x
)
1296 (if (eq (char-before (1- (point))) ?%
) (forward-char -
2))))
1298 ((looking-at "\\s\"\\|\\\\\\S_")
1299 (let ((c (char-to-string (char-before (match-end 0)))))
1300 (while (and (search-backward c
)
1301 (eq (logand (skip-chars-backward "\\") 1)
1304 ((looking-at "\\s.\\|\\s\\")
1305 (if (ruby-special-char-p) (forward-char -
1)))
1306 ((looking-at "\\s(") nil
)
1309 (while (progn (forward-word -
1)
1312 (?.
(forward-char -
1) t
)
1315 (and (eq (char-before) (char-after)) (forward-char -
1)))
1318 (eq (char-before) :)))))
1319 (if (looking-at ruby-block-end-re
)
1320 (ruby-beginning-of-block))
1326 (defun ruby-indent-exp (&optional ignored
)
1327 "Indent each line in the balanced expression following the point."
1329 (let ((here (point-marker)) start top column
(nest t
))
1330 (set-marker-insertion-type here t
)
1334 (setq start
(point) top
(current-indentation))
1335 (while (and (not (eobp))
1337 (setq column
(ruby-calculate-indent start
))
1338 (cond ((> column top
)
1340 ((and (= column top
) nest
)
1341 (setq nest nil
) t
))))
1342 (ruby-indent-to column
)
1343 (beginning-of-line 2)))
1345 (set-marker here nil
))))
1347 (defun ruby-add-log-current-method ()
1348 "Return the current method name as a string.
1349 This string includes all namespaces.
1358 See `add-log-current-defun-function'."
1361 (let* ((indent 0) mname mlist
1365 (concat "^[ \t]*" re
"[ \t]+"
1367 ;; \\. and :: for class methods
1368 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1370 (definition-re (funcall make-definition-re ruby-defun-beg-re
))
1371 (module-re (funcall make-definition-re
"\\(class\\|module\\)")))
1372 ;; Get the current method definition (or class/module).
1373 (when (re-search-backward definition-re nil t
)
1374 (goto-char (match-beginning 1))
1375 (if (not (string-equal "def" (match-string 1)))
1376 (setq mlist
(list (match-string 2)))
1377 ;; We're inside the method. For classes and modules,
1378 ;; this check is skipped for performance.
1379 (when (ruby-block-contains-point start
)
1380 (setq mname
(match-string 2))))
1381 (setq indent
(current-column))
1382 (beginning-of-line))
1383 ;; Walk up the class/module nesting.
1384 (while (and (> indent
0)
1385 (re-search-backward module-re nil t
))
1386 (goto-char (match-beginning 1))
1387 (when (< (current-column) indent
)
1388 (setq mlist
(cons (match-string 2) mlist
))
1389 (setq indent
(current-column))
1390 (beginning-of-line)))
1391 ;; Process the method name.
1393 (let ((mn (split-string mname
"\\.\\|::")))
1396 (unless (string-equal "self" (car mn
)) ; def self.foo
1398 (let ((ml (nreverse mlist
)))
1399 ;; If the method name references one of the
1400 ;; containing modules, drop the more nested ones.
1402 (if (string-equal (car ml
) (car mn
))
1403 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1404 (or (setq ml
(cdr ml
)) (nreverse mlist
))))
1406 (setcdr (last mlist
) (butlast mn
))
1407 (setq mlist
(butlast mn
))))
1408 (setq mname
(concat "." (car (last mn
)))))
1409 ;; See if the method is in singleton class context.
1410 (let ((in-singleton-class
1411 (when (re-search-forward ruby-singleton-class-re start t
)
1412 (goto-char (match-beginning 0))
1413 ;; FIXME: Optimize it out, too?
1414 ;; This can be slow in a large file, but
1415 ;; unlike class/module declaration
1416 ;; indentations, method definitions can be
1417 ;; intermixed with these, and may or may not
1418 ;; be additionally indented after visibility
1420 (ruby-block-contains-point start
))))
1422 (if in-singleton-class
"." "#")
1424 ;; Generate the string.
1426 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1428 (if mlist
(concat mlist mname
) mname
)
1431 (defun ruby-block-contains-point (pt)
1437 (defun ruby-brace-to-do-end (orig end
)
1438 (let (beg-marker end-marker
)
1440 (when (eq (char-before) ?\
})
1442 (when (save-excursion
1443 (skip-chars-backward " \t")
1447 (setq end-marker
(point-marker))
1448 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w
))
1452 (when (eq (char-syntax (char-before)) ?w
)
1455 (setq beg-marker
(point-marker))
1456 (when (looking-at "\\(\\s \\)*|")
1457 (unless (match-beginning 1)
1459 (goto-char (1+ (match-end 0)))
1460 (search-forward "|"))
1461 (unless (looking-at "\\s *$")
1463 (indent-region beg-marker end-marker
)
1464 (goto-char beg-marker
)
1467 (defun ruby-do-end-to-brace (orig end
)
1468 (let (beg-marker end-marker beg-pos end-pos
)
1469 (goto-char (- end
3))
1470 (when (looking-at ruby-block-end-re
)
1472 (setq end-marker
(point-marker))
1476 ;; Maybe this should be customizable, let's see if anyone asks.
1478 (setq beg-marker
(point-marker))
1479 (when (looking-at "\\s +|")
1480 (delete-char (- (match-end 0) (match-beginning 0) 1))
1482 (re-search-forward "|" (line-end-position) t
))
1484 (skip-chars-forward " \t\n\r")
1485 (setq beg-pos
(point))
1486 (goto-char end-marker
)
1487 (skip-chars-backward " \t\n\r")
1488 (setq end-pos
(point)))
1491 (and (= (line-number-at-pos beg-pos
) (line-number-at-pos end-pos
))
1492 (< (+ (current-column) (- end-pos beg-pos
) 2) fill-column
)))
1494 (goto-char end-marker
)
1495 (just-one-space -
1))
1496 (goto-char beg-marker
)
1499 (defun ruby-toggle-block ()
1500 "Toggle block type from do-end to braces or back.
1501 The block must begin on the current line or above it and end after the point.
1502 If the result is do-end block, it will always be multiline."
1504 (let ((start (point)) beg end
)
1507 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1510 (save-match-data (ruby-forward-sexp))
1513 (if (match-beginning 1)
1514 (ruby-brace-to-do-end beg end
)
1515 (ruby-do-end-to-brace beg end
)))
1516 (goto-char start
))))
1518 (declare-function ruby-syntax-propertize-heredoc
"ruby-mode" (limit))
1519 (declare-function ruby-syntax-enclosing-percent-literal
"ruby-mode" (limit))
1520 (declare-function ruby-syntax-propertize-percent-literal
"ruby-mode" (limit))
1521 ;; Unusual code layout confuses the byte-compiler.
1522 (declare-function ruby-syntax-propertize-expansion
"ruby-mode" ())
1523 (declare-function ruby-syntax-expansion-allowed-p
"ruby-mode" (parse-state))
1524 (declare-function ruby-syntax-propertize-function
"ruby-mode" (start end
))
1526 (if (eval-when-compile (fboundp #'syntax-propertize-rules
))
1527 ;; New code that works independently from font-lock.
1530 (defconst ruby-percent-literal-beg-re
1531 "\\(%\\)[qQrswWxIi]?\\([[:punct:]]\\)"
1532 "Regexp to match the beginning of percent literal.")
1534 (defconst ruby-syntax-methods-before-regexp
1535 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1536 "assert_match" "Given" "Then" "When")
1537 "Methods that can take regexp as the first argument.
1538 It will be properly highlighted even when the call omits parens.")
1540 (defvar ruby-syntax-before-regexp-re
1542 ;; Special tokens that can't be followed by a division operator.
1544 ;; Distinguish ternary operator tokens.
1545 ;; FIXME: They don't really have to be separated with spaces.
1547 ;; Control flow keywords and operators following bol or whitespace.
1548 "\\|\\(?:^\\|\\s \\)"
1549 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1550 "or" "not" "&&" "||"))
1551 ;; Method name from the list.
1553 (regexp-opt ruby-syntax-methods-before-regexp
)
1555 "Regexp to match text that can be followed by a regular expression."))
1557 (defun ruby-syntax-propertize-function (start end
)
1558 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1559 (let (case-fold-search)
1561 (remove-text-properties start end
'(ruby-expansion-match-data))
1562 (ruby-syntax-propertize-heredoc end
)
1563 (ruby-syntax-enclosing-percent-literal end
)
1565 (syntax-propertize-rules
1566 ;; $' $" $` .... are variables.
1567 ;; ?' ?" ?` are character literals (one-char strings in 1.9+).
1568 ("\\([?$]\\)[#\"'`]"
1569 (1 (unless (save-excursion
1570 ;; Not within a string.
1571 (nth 3 (syntax-ppss (match-beginning 0))))
1572 (string-to-syntax "\\"))))
1573 ;; Regular expressions. Start with matching unescaped slash.
1574 ("\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*\\(/\\)"
1575 (1 (let ((state (save-excursion (syntax-ppss (match-beginning 1)))))
1577 ;; Beginning of a regexp.
1578 (and (null (nth 8 state
))
1581 (looking-back ruby-syntax-before-regexp-re
1583 ;; End of regexp. We don't match the whole
1584 ;; regexp at once because it can have
1585 ;; string interpolation inside, or span
1587 (eq ?
/ (nth 3 state
)))
1588 (string-to-syntax "\"/")))))
1589 ;; Expression expansions in strings. We're handling them
1590 ;; here, so that the regexp rule never matches inside them.
1591 (ruby-expression-expansion-re
1592 (0 (ignore (ruby-syntax-propertize-expansion))))
1593 ("^=en\\(d\\)\\_>" (1 "!"))
1594 ("^\\(=\\)begin\\_>" (1 "!"))
1595 ;; Handle here documents.
1596 ((concat ruby-here-doc-beg-re
".*\\(\n\\)")
1597 (7 (unless (or (nth 8 (save-excursion
1598 (syntax-ppss (match-beginning 0))))
1599 (ruby-singleton-class-p (match-beginning 0)))
1600 (put-text-property (match-beginning 7) (match-end 7)
1601 'syntax-table
(string-to-syntax "\""))
1602 (ruby-syntax-propertize-heredoc end
))))
1603 ;; Handle percent literals: %w(), %q{}, etc.
1604 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re
)
1605 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end
)))))
1608 (defun ruby-syntax-propertize-heredoc (limit)
1609 (let ((ppss (syntax-ppss))
1611 (when (eq ?
\n (nth 3 ppss
))
1613 (goto-char (nth 8 ppss
))
1615 (while (re-search-forward ruby-here-doc-beg-re
1616 (line-end-position) t
)
1617 (unless (ruby-singleton-class-p (match-beginning 0))
1618 (push (concat (ruby-here-doc-end-match) "\n") res
))))
1620 ;; With multiple openers on the same line, we don't know in which
1621 ;; part `start' is, so we have to go back to the beginning.
1623 (goto-char (nth 8 ppss
))
1624 (setq res
(nreverse res
)))
1625 (while (and res
(re-search-forward (pop res
) limit
'move
))
1627 (put-text-property (1- (point)) (point)
1628 'syntax-table
(string-to-syntax "\""))))
1629 ;; End up at bol following the heredoc openers.
1630 ;; Propertize expression expansions from this point forward.
1633 (defun ruby-syntax-enclosing-percent-literal (limit)
1634 (let ((state (syntax-ppss))
1636 ;; When already inside percent literal, re-propertize it.
1637 (when (eq t
(nth 3 state
))
1638 (goto-char (nth 8 state
))
1639 (when (looking-at ruby-percent-literal-beg-re
)
1640 (ruby-syntax-propertize-percent-literal limit
))
1641 (when (< (point) start
) (goto-char start
)))))
1643 (defun ruby-syntax-propertize-percent-literal (limit)
1644 (goto-char (match-beginning 2))
1645 ;; Not inside a simple string or comment.
1646 (when (eq t
(nth 3 (syntax-ppss)))
1647 (let* ((op (char-after))
1648 (ops (char-to-string op
))
1649 (cl (or (cdr (aref (syntax-table) op
))
1650 (cdr (assoc op
'((?
< . ?
>))))))
1651 parse-sexp-lookup-properties
)
1655 (if cl
; Paired delimiters.
1656 ;; Delimiter pairs of the same kind can be nested
1657 ;; inside the literal, as long as they are balanced.
1658 ;; Create syntax table that ignores other characters.
1659 (with-syntax-table (make-char-table 'syntax-table nil
)
1660 (modify-syntax-entry op
(concat "(" (char-to-string cl
)))
1661 (modify-syntax-entry cl
(concat ")" ops
))
1662 (modify-syntax-entry ?
\\ "\\")
1664 (narrow-to-region (point) limit
)
1665 (forward-list))) ; skip to the paired character
1666 ;; Single character delimiter.
1667 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1668 (regexp-quote ops
)) limit nil
))
1669 ;; Found the closing delimiter.
1670 (put-text-property (1- (point)) (point) 'syntax-table
1671 (string-to-syntax "|")))
1672 ;; Unclosed literal, do nothing.
1673 ((scan-error search-failed
)))))))
1675 (defun ruby-syntax-propertize-expansion ()
1676 ;; Save the match data to a text property, for font-locking later.
1677 ;; Set the syntax of all double quotes and backticks to punctuation.
1678 (let* ((beg (match-beginning 2))
1680 (state (and beg
(save-excursion (syntax-ppss beg
)))))
1681 (when (ruby-syntax-expansion-allowed-p state
)
1682 (put-text-property beg
(1+ beg
) 'ruby-expansion-match-data
1685 (while (re-search-forward "[\"`]" end
'move
)
1686 (put-text-property (match-beginning 0) (match-end 0)
1687 'syntax-table
(string-to-syntax "."))))))
1689 (defun ruby-syntax-expansion-allowed-p (parse-state)
1690 "Return non-nil if expression expansion is allowed."
1691 (let ((term (nth 3 parse-state
)))
1693 ((memq term
'(?
\" ?
` ?
\n ?
/)))
1697 (goto-char (nth 8 parse-state
))
1698 (looking-at "%\\(?:[QWrxI]\\|\\W\\)")))))))
1700 (defun ruby-syntax-propertize-expansions (start end
)
1703 (while (re-search-forward ruby-expression-expansion-re end
'move
)
1704 (ruby-syntax-propertize-expansion))))
1707 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1708 ;; fallback on the old font-lock-syntactic-keywords stuff.
1710 (defconst ruby-here-doc-end-re
1711 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1712 "Regexp to match the end of heredocs.
1714 This will actually match any line with one or more characters.
1715 It's useful in that it divides up the match string so that
1716 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1718 (defun ruby-here-doc-beg-match ()
1719 "Return a regexp to find the beginning of a heredoc.
1721 This should only be called after matching against `ruby-here-doc-end-re'."
1722 (let ((contents (concat
1723 (regexp-quote (concat (match-string 2) (match-string 3)))
1724 (if (string= (match-string 3) "_") "\\B" "\\b"))))
1726 (let ((match (match-string 1)))
1727 (if (and match
(> (length match
) 0))
1728 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1729 (match-string 1) "\\)"
1730 contents
"\\(\\1\\|\\2\\)")
1731 (concat "-?\\([\"']\\|\\)" contents
"\\1"))))))
1733 (defconst ruby-font-lock-syntactic-keywords
1735 ;; the last $', $", $` in the respective string is not variable
1736 ;; the last ?', ?", ?` in the respective string is not ascii code
1737 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1740 ;; $' $" $` .... are variables
1741 ;; ?' ?" ?` are ascii codes
1742 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil
))
1744 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1747 ("^=en\\(d\\)\\_>" 1 "!")
1749 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1752 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1753 ;; Currently, the following case is highlighted incorrectly:
1762 ;; This is because all here-doc beginnings are highlighted before any endings,
1763 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1764 ;; it thinks <<BAR is part of a string so it's marked as well.
1766 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1767 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1768 ;; but I don't want to try that until we've got unit tests set up
1769 ;; to make sure I don't break anything else.
1770 (,(concat ruby-here-doc-beg-re
".*\\(\n\\)")
1771 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re
))
1772 (ruby-here-doc-beg-syntax))
1773 (,ruby-here-doc-end-re
3 (ruby-here-doc-end-syntax)))
1774 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1776 (defun ruby-comment-beg-syntax ()
1777 "Return the syntax cell for a the first character of a =begin.
1778 See the definition of `ruby-font-lock-syntactic-keywords'.
1780 This returns a comment-delimiter cell as long as the =begin
1781 isn't in a string or another comment."
1782 (when (not (nth 3 (syntax-ppss)))
1783 (string-to-syntax "!")))
1785 (defun ruby-in-here-doc-p ()
1786 "Return whether or not the point is in a heredoc."
1788 (let ((old-point (point)) (case-fold-search nil
))
1791 (while (and (re-search-backward ruby-here-doc-beg-re nil t
)
1792 (not (ruby-singleton-class-p)))
1793 (if (not (or (ruby-in-ppss-context-p 'anything
)
1794 (ruby-here-doc-find-end old-point
)))
1795 (throw 'found-beg t
)))))))
1797 (defun ruby-here-doc-find-end (&optional limit
)
1798 "Expects the point to be on a line with one or more heredoc openers.
1799 Returns the buffer position at which all heredocs on the line
1800 are terminated, or nil if they aren't terminated before the
1801 buffer position `limit' or the end of the buffer."
1805 (let ((eol (point-at-eol))
1806 (case-fold-search nil
)
1807 ;; Fake match data such that (match-end 0) is at eol
1808 (end-match-data (progn (looking-at ".*$") (match-data)))
1809 beg-match-data end-re
)
1810 (while (re-search-forward ruby-here-doc-beg-re eol t
)
1811 (setq beg-match-data
(match-data))
1812 (setq end-re
(ruby-here-doc-end-match))
1814 (set-match-data end-match-data
)
1815 (goto-char (match-end 0))
1816 (unless (re-search-forward end-re limit t
) (throw 'done nil
))
1817 (setq end-match-data
(match-data))
1819 (set-match-data beg-match-data
)
1820 (goto-char (match-end 0)))
1821 (set-match-data end-match-data
)
1822 (goto-char (match-end 0))
1825 (defun ruby-here-doc-beg-syntax ()
1826 "Return the syntax cell for a line that may begin a heredoc.
1827 See the definition of `ruby-font-lock-syntactic-keywords'.
1829 This sets the syntax cell for the newline ending the line
1830 containing the heredoc beginning so that cases where multiple
1831 heredocs are started on one line are handled correctly."
1833 (goto-char (match-beginning 0))
1834 (unless (or (ruby-in-ppss-context-p 'non-heredoc
)
1835 (ruby-in-here-doc-p))
1836 (string-to-syntax "\""))))
1838 (defun ruby-here-doc-end-syntax ()
1839 "Return the syntax cell for a line that may end a heredoc.
1840 See the definition of `ruby-font-lock-syntactic-keywords'."
1841 (let ((pss (syntax-ppss)) (case-fold-search nil
))
1842 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1843 ;; so we can just give up.
1844 ;; This means we aren't doing a full-document search
1845 ;; every time we enter a character.
1846 (when (ruby-in-ppss-context-p 'heredoc pss
)
1848 (goto-char (nth 8 pss
)) ; Go to the beginning of heredoc.
1849 (let ((eol (point)))
1851 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t
) ; If there is a heredoc that matches this line...
1852 (not (ruby-in-ppss-context-p 'anything
)) ; And that's not inside a heredoc/string/comment...
1853 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1854 (not (re-search-forward ruby-here-doc-beg-re eol t
))))
1855 (string-to-syntax "\"")))))))
1857 (unless (functionp 'syntax-ppss
)
1858 (defun syntax-ppss (&optional pos
)
1859 (parse-partial-sexp (point-min) (or pos
(point)))))
1862 (defun ruby-in-ppss-context-p (context &optional ppss
)
1863 (let ((ppss (or ppss
(syntax-ppss (point)))))
1865 ((eq context
'anything
)
1868 ((eq context
'string
)
1870 ((eq context
'heredoc
)
1871 (eq ?
\n (nth 3 ppss
)))
1872 ((eq context
'non-heredoc
)
1873 (and (ruby-in-ppss-context-p 'anything
)
1874 (not (ruby-in-ppss-context-p 'heredoc
))))
1875 ((eq context
'comment
)
1879 "Internal error on `ruby-in-ppss-context-p': "
1880 "context name `" (symbol-name context
) "' is unknown"))))
1883 (if (featurep 'xemacs
)
1884 (put 'ruby-mode
'font-lock-defaults
1885 '((ruby-font-lock-keywords)
1888 (font-lock-syntactic-keywords
1889 . ruby-font-lock-syntactic-keywords
))))
1891 (defvar ruby-font-lock-syntax-table
1892 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1893 (modify-syntax-entry ?_
"w" tbl
)
1895 "The syntax table to use for fontifying Ruby mode buffers.
1896 See `font-lock-syntax-table'.")
1898 (defconst ruby-font-lock-keyword-beg-re
"\\(?:^\\|[^.@$]\\|\\.\\.\\)")
1900 (defconst ruby-font-lock-keywords
1903 '("^\\s *def\\s +\\(?:[^( \t\n.]*\\.\\)?\\([^( \t\n]+\\)"
1904 1 font-lock-function-name-face
)
1907 ruby-font-lock-keyword-beg-re
1943 1 'font-lock-keyword-face
)
1944 ;; some core methods
1946 ruby-font-lock-keyword-beg-re
1948 '(;; built-in methods on Kernel
1992 ;; keyword-like private methods on Module
2009 1 'font-lock-builtin-face
)
2010 ;; here-doc beginnings
2011 `(,ruby-here-doc-beg-re
0 (unless (ruby-singleton-class-p (match-beginning 0))
2012 'font-lock-string-face
))
2013 ;; Perl-ish keywords
2014 "\\_<\\(?:BEGIN\\|END\\)\\_>\\|^__END__$"
2016 `(,(concat ruby-font-lock-keyword-beg-re
2017 "\\_<\\(nil\\|self\\|true\\|false\\)\\>")
2018 1 font-lock-variable-name-face
)
2019 ;; keywords that evaluate to certain values
2020 '("\\_<__\\(?:LINE\\|ENCODING\\|FILE\\)__\\_>" 0 font-lock-variable-name-face
)
2022 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
2023 2 font-lock-constant-face
)
2025 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
2026 1 font-lock-variable-name-face
)
2027 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
2028 0 font-lock-variable-name-face
)
2030 '("\\(?:\\_<\\|::\\)\\([A-Z]+\\(\\w\\|_\\)*\\)"
2031 1 (unless (eq ?\
( (char-after)) font-lock-type-face
))
2032 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face
)
2033 ;; conversion methods on Kernel
2034 (list (concat ruby-font-lock-keyword-beg-re
2035 (regexp-opt '("Array" "Complex" "Float" "Hash"
2036 "Integer" "Rational" "String") 'symbols
))
2037 1 font-lock-builtin-face
)
2038 ;; expression expansion
2039 '(ruby-match-expression-expansion
2040 2 font-lock-variable-name-face t
)
2042 '("[^[:alnum:]_]\\(!\\)[^=]"
2043 1 font-lock-negation-char-face
)
2044 ;; character literals
2045 ;; FIXME: Support longer escape sequences.
2046 '("\\_<\\?\\\\?\\S " 0 font-lock-string-face
)
2048 "Additional expressions to highlight in Ruby mode.")
2050 (defun ruby-match-expression-expansion (limit)
2051 (let* ((prop 'ruby-expansion-match-data
)
2052 (pos (next-single-char-property-change (point) prop nil limit
))
2054 (when (and pos
(> pos
(point)))
2056 (or (and (setq value
(get-text-property pos prop
))
2057 (progn (set-match-data value
) t
))
2058 (ruby-match-expression-expansion limit
)))))
2061 (define-derived-mode ruby-mode prog-mode
"Ruby"
2062 "Major mode for editing Ruby scripts.
2063 \\[ruby-indent-line] properly indents subexpressions of multi-line
2064 class, module, def, if, while, for, do, and case statements, taking
2065 nesting into account.
2067 The variable `ruby-indent-level' controls the amount of indentation.
2070 (ruby-mode-variables)
2072 (set (make-local-variable 'imenu-create-index-function
)
2073 'ruby-imenu-create-index
)
2074 (set (make-local-variable 'add-log-current-defun-function
)
2075 'ruby-add-log-current-method
)
2076 (set (make-local-variable 'beginning-of-defun-function
)
2077 'ruby-beginning-of-defun
)
2078 (set (make-local-variable 'end-of-defun-function
)
2081 (add-hook 'after-save-hook
'ruby-mode-set-encoding nil
'local
)
2083 (set (make-local-variable 'electric-indent-chars
)
2084 (append '(?\
{ ?\
}) electric-indent-chars
))
2086 (set (make-local-variable 'font-lock-defaults
)
2087 '((ruby-font-lock-keywords) nil nil
))
2088 (set (make-local-variable 'font-lock-keywords
)
2089 ruby-font-lock-keywords
)
2090 (set (make-local-variable 'font-lock-syntax-table
)
2091 ruby-font-lock-syntax-table
)
2093 (if (eval-when-compile (fboundp 'syntax-propertize-rules
))
2094 (set (make-local-variable 'syntax-propertize-function
)
2095 #'ruby-syntax-propertize-function
)
2096 (set (make-local-variable 'font-lock-syntactic-keywords
)
2097 ruby-font-lock-syntactic-keywords
)))
2099 ;;; Invoke ruby-mode when appropriate
2102 (add-to-list 'auto-mode-alist
2103 (cons (purecopy (concat "\\(?:\\."
2104 "rb\\|ru\\|rake\\|thor"
2105 "\\|jbuilder\\|gemspec"
2107 "\\(?:Gem\\|Rake\\|Cap\\|Thor"
2108 "Vagrant\\|Guard\\)file"
2109 "\\)\\'")) 'ruby-mode
))
2112 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
2113 (add-to-list 'interpreter-mode-alist
(cons (purecopy name
) 'ruby-mode
)))
2115 (provide 'ruby-mode
)
2117 ;;; ruby-mode.el ends here