1 ;;; ruby-mode.el --- Major mode for editing Ruby files
3 ;; Copyright (C) 1994-2012 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-keyword-end-re
50 (if (string-match "\\_>" "ruby")
54 (defconst ruby-block-beg-keywords
55 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
56 "Keywords at the beginning of blocks.")
58 (defconst ruby-block-beg-re
59 (regexp-opt ruby-block-beg-keywords
)
60 "Regexp to match the beginning of blocks.")
62 (defconst ruby-non-block-do-re
63 (concat (regexp-opt '("while" "until" "for" "rescue") t
) ruby-keyword-end-re
)
64 "Regexp to match keywords that nest without blocks.")
66 (defconst ruby-indent-beg-re
67 (concat "^\\s *" (regexp-opt '("class" "module" "def" "if" "unless" "case"
68 "while" "until" "for" "begin")) "\\_>")
69 "Regexp to match where the indentation gets deeper.")
71 (defconst ruby-modifier-beg-keywords
72 '("if" "unless" "while" "until")
73 "Modifiers that are the same as the beginning of blocks.")
75 (defconst ruby-modifier-beg-re
76 (regexp-opt ruby-modifier-beg-keywords
)
77 "Regexp to match modifiers same as the beginning of blocks.")
79 (defconst ruby-modifier-re
80 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords
))
81 "Regexp to match modifiers.")
83 (defconst ruby-block-mid-keywords
84 '("then" "else" "elsif" "when" "rescue" "ensure")
85 "Keywords where the indentation gets shallower in middle of block statements.")
87 (defconst ruby-block-mid-re
88 (regexp-opt ruby-block-mid-keywords
)
89 "Regexp to match where the indentation gets shallower in middle of block statements.")
91 (defconst ruby-block-op-keywords
93 "Regexp to match boolean keywords.")
95 (defconst ruby-block-hanging-re
96 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords
))
97 "Regexp to match hanging block modifiers.")
99 (defconst ruby-block-end-re
"\\_<end\\_>")
101 (defconst ruby-defun-beg-re
102 '"\\(def\\|class\\|module\\)"
103 "Regexp to match the beginning of a defun, in the general sense.")
106 (defconst ruby-here-doc-beg-re
107 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
108 "Regexp to match the beginning of a heredoc."))
110 (defun ruby-here-doc-end-match ()
111 "Return a regexp to find the end of a heredoc.
113 This should only be called after matching against `ruby-here-doc-beg-re'."
115 (if (match-string 2) "[ \t]*" nil
)
121 (defconst ruby-delimiter
122 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
124 "\\)\\_>\\|" ruby-block-end-re
125 "\\|^=begin\\|" ruby-here-doc-beg-re
))
127 (defconst ruby-negative
128 (concat "^[ \t]*\\(\\(" ruby-block-mid-re
"\\)\\>\\|"
129 ruby-block-end-re
"\\|}\\|\\]\\)")
130 "Regexp to match where the indentation gets shallower.")
132 (defconst ruby-operator-re
"[-,.+*/%&|^~=<>:]"
133 "Regexp to match operators.")
135 (defconst ruby-symbol-chars
"a-zA-Z0-9_"
136 "List of characters that symbol names may contain.")
137 (defconst ruby-symbol-re
(concat "[" ruby-symbol-chars
"]")
138 "Regexp to match symbols.")
140 (define-abbrev-table 'ruby-mode-abbrev-table
()
141 "Abbrev table in use in Ruby mode buffers.")
143 (defvar ruby-mode-map
144 (let ((map (make-sparse-keymap)))
145 (define-key map
(kbd "M-C-b") 'ruby-backward-sexp
)
146 (define-key map
(kbd "M-C-f") 'ruby-forward-sexp
)
147 (define-key map
(kbd "M-C-p") 'ruby-beginning-of-block
)
148 (define-key map
(kbd "M-C-n") 'ruby-end-of-block
)
149 (define-key map
(kbd "M-C-q") 'ruby-indent-exp
)
150 (define-key map
(kbd "C-c {") 'ruby-toggle-block
)
152 "Keymap used in Ruby mode.")
154 (defvar ruby-mode-syntax-table
155 (let ((table (make-syntax-table)))
156 (modify-syntax-entry ?
\' "\"" table
)
157 (modify-syntax-entry ?
\" "\"" table
)
158 (modify-syntax-entry ?\
` "\"" table
)
159 (modify-syntax-entry ?
# "<" table
)
160 (modify-syntax-entry ?
\n ">" table
)
161 (modify-syntax-entry ?
\\ "\\" table
)
162 (modify-syntax-entry ?$
"." table
)
163 (modify-syntax-entry ??
"_" table
)
164 (modify-syntax-entry ?_
"_" table
)
165 (modify-syntax-entry ?
: "_" table
)
166 (modify-syntax-entry ?
< "." table
)
167 (modify-syntax-entry ?
> "." table
)
168 (modify-syntax-entry ?
& "." table
)
169 (modify-syntax-entry ?|
"." table
)
170 (modify-syntax-entry ?%
"." table
)
171 (modify-syntax-entry ?
= "." table
)
172 (modify-syntax-entry ?
/ "." table
)
173 (modify-syntax-entry ?
+ "." table
)
174 (modify-syntax-entry ?
* "." table
)
175 (modify-syntax-entry ?-
"." table
)
176 (modify-syntax-entry ?\
; "." table)
177 (modify-syntax-entry ?\
( "()" table
)
178 (modify-syntax-entry ?\
) ")(" table
)
179 (modify-syntax-entry ?\
{ "(}" table
)
180 (modify-syntax-entry ?\
} "){" table
)
181 (modify-syntax-entry ?\
[ "(]" table
)
182 (modify-syntax-entry ?\
] ")[" table
)
184 "Syntax table to use in Ruby mode.")
186 (defcustom ruby-indent-tabs-mode nil
187 "Indentation can insert tabs in Ruby mode if this is non-nil."
188 :type
'boolean
:group
'ruby
)
190 (defcustom ruby-indent-level
2
191 "Indentation of Ruby statements."
192 :type
'integer
:group
'ruby
)
194 (defcustom ruby-comment-column
32
195 "Indentation column of comments."
196 :type
'integer
:group
'ruby
)
198 (defcustom ruby-deep-arglist t
199 "Deep indent lists in parenthesis when non-nil.
200 Also ignores spaces after parenthesis when 'space."
203 (defcustom ruby-deep-indent-paren
'(?\
( ?\
[ ?\
] t
)
204 "Deep indent lists in parenthesis when non-nil.
205 The value t means continuous line.
206 Also ignores spaces after parenthesis when 'space."
209 (defcustom ruby-deep-indent-paren-style
'space
210 "Default deep indent style."
211 :options
'(t nil space
) :group
'ruby
)
213 (defcustom ruby-encoding-map
'((shift_jis . cp932
) (shift-jis . cp932
))
214 "Alist to map encoding name from Emacs to Ruby."
217 (defcustom ruby-insert-encoding-magic-comment t
218 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
219 :type
'boolean
:group
'ruby
)
221 (defcustom ruby-use-encoding-map t
222 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
223 :type
'boolean
:group
'ruby
)
225 ;; Safe file variables
226 (put 'ruby-indent-tabs-mode
'safe-local-variable
'booleanp
)
227 (put 'ruby-indent-level
'safe-local-variable
'integerp
)
228 (put 'ruby-comment-column
'safe-local-variable
'integerp
)
229 (put 'ruby-deep-arglist
'safe-local-variable
'booleanp
)
231 (defun ruby-imenu-create-index-in-block (prefix beg end
)
232 "Create an imenu index of methods inside a block."
233 (let ((index-alist '()) (case-fold-search nil
)
234 name next pos decl sing
)
236 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t
)
237 (setq sing
(match-beginning 3))
238 (setq decl
(match-string 5))
239 (setq next
(match-end 0))
240 (setq name
(or (match-string 4) (match-string 6)))
241 (setq pos
(match-beginning 0))
243 ((string= "alias" decl
)
244 (if prefix
(setq name
(concat prefix name
)))
245 (push (cons name pos
) index-alist
))
246 ((string= "def" decl
)
250 ((string-match "^self\." name
)
251 (concat (substring prefix
0 -
1) (substring name
4)))
252 (t (concat prefix name
)))))
253 (push (cons name pos
) index-alist
)
254 (ruby-accurate-end-of-block end
))
256 (if (string= "self" name
)
257 (if prefix
(setq name
(substring prefix
0 -
1)))
258 (if prefix
(setq name
(concat (substring prefix
0 -
1) "::" name
)))
259 (push (cons name pos
) index-alist
))
260 (ruby-accurate-end-of-block end
)
263 (nconc (ruby-imenu-create-index-in-block
264 (concat name
(if sing
"." "#"))
265 next beg
) index-alist
))
269 (defun ruby-imenu-create-index ()
270 "Create an imenu index of all methods in the buffer."
271 (nreverse (ruby-imenu-create-index-in-block nil
(point-min) nil
)))
273 (defun ruby-accurate-end-of-block (&optional end
)
276 (end (or end
(point-max))))
277 (while (and (setq state
(apply 'ruby-parse-partial end state
))
278 (>= (nth 2 state
) 0) (< (point) end
)))))
280 (defun ruby-mode-variables ()
281 "Set up initial buffer-local variables for Ruby mode."
282 (set-syntax-table ruby-mode-syntax-table
)
283 (setq local-abbrev-table ruby-mode-abbrev-table
)
284 (setq indent-tabs-mode ruby-indent-tabs-mode
)
285 (set (make-local-variable 'indent-line-function
) 'ruby-indent-line
)
286 (set (make-local-variable 'require-final-newline
) t
)
287 (set (make-local-variable 'comment-start
) "# ")
288 (set (make-local-variable 'comment-end
) "")
289 (set (make-local-variable 'comment-column
) ruby-comment-column
)
290 (set (make-local-variable 'comment-start-skip
) "#+ *")
291 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
292 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
293 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
294 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
295 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
))
297 (defun ruby-mode-set-encoding ()
298 "Insert a magic comment header with the proper encoding if necessary."
301 (goto-char (point-min))
302 (when (re-search-forward "[^\0-\177]" nil t
)
303 (goto-char (point-min))
305 (or coding-system-for-write
306 buffer-file-coding-system
)))
309 (or (coding-system-get coding-system
'mime-charset
)
310 (coding-system-change-eol-conversion coding-system nil
))))
314 (or (and ruby-use-encoding-map
315 (cdr (assq coding-system ruby-encoding-map
)))
318 (if (looking-at "^#!") (beginning-of-line 2))
319 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
320 (unless (string= (match-string 2) coding-system
)
321 (goto-char (match-beginning 2))
322 (delete-region (point) (match-end 2))
323 (and (looking-at "-\*-")
324 (let ((n (skip-chars-backward " ")))
325 (cond ((= n
0) (insert " ") (backward-char))
326 ((= n -
1) (insert " "))
328 (insert coding-system
)))
329 ((looking-at "\\s *#.*coding\\s *[:=]"))
330 (t (when ruby-insert-encoding-magic-comment
331 (insert "# -*- coding: " coding-system
" -*-\n"))))))))
333 (defun ruby-current-indentation ()
334 "Return the indentation level of current line."
337 (back-to-indentation)
340 (defun ruby-indent-line (&optional ignored
)
341 "Correct the indentation of the current Ruby line."
343 (ruby-indent-to (ruby-calculate-indent)))
345 (defun ruby-indent-to (column)
346 "Indent the current line to COLUMN."
349 (and (< column
0) (error "invalid nest"))
350 (setq shift
(current-column))
353 (back-to-indentation)
354 (setq top
(current-column))
355 (skip-chars-backward " \t")
356 (if (>= shift top
) (setq shift
(- shift top
))
360 (move-to-column (+ column shift
))
362 (delete-region beg
(point))
365 (move-to-column (+ column shift
))))))
367 (defun ruby-special-char-p (&optional pos
)
368 "Return t if the character before POS is a special character.
369 If omitted, POS defaults to the current point.
370 Special characters are `?', `$', `:' when preceded by whitespace,
371 and `\\' when preceded by `?'."
372 (setq pos
(or pos
(point)))
373 (let ((c (char-before pos
)) (b (and (< (point-min) pos
)
374 (char-before (1- pos
)))))
375 (cond ((or (eq c ??
) (eq c ?$
)))
376 ((and (eq c ?
:) (or (not b
) (eq (char-syntax b
) ?
))))
377 ((eq c ?
\\) (eq b ??
)))))
379 (defun ruby-singleton-class-p (&optional pos
)
381 (when pos
(goto-char pos
))
383 (and (or (bolp) (not (eq (char-before (point)) ?_
)))
384 (looking-at "class\\s *<<"))))
386 (defun ruby-expr-beg (&optional option
)
387 "Check if point is possibly at the beginning of an expression.
388 OPTION specifies the type of the expression.
389 Can be one of `heredoc', `modifier', `expr-qstr', `expr-re'."
391 (store-match-data nil
)
392 (let ((space (skip-chars-backward " \t"))
398 (and (looking-at "\\?")
399 (or (eq (char-syntax (char-before (point))) ?w
)
400 (ruby-special-char-p))))
402 ((looking-at ruby-operator-re
))
403 ((eq option
'heredoc
)
404 (and (< space
0) (not (ruby-singleton-class-p start
))))
405 ((or (looking-at "[\\[({,;]")
406 (and (looking-at "[!?]")
407 (or (not (eq option
'modifier
))
409 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
410 (and (looking-at ruby-symbol-re
)
411 (skip-chars-backward ruby-symbol-chars
)
413 ((looking-at (regexp-opt
414 (append ruby-block-beg-keywords
415 ruby-block-op-keywords
416 ruby-block-mid-keywords
)
418 (goto-char (match-end 0))
419 (not (looking-at "\\s_\\|!")))
420 ((eq option
'expr-qstr
)
421 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
422 ((eq option
'expr-re
)
423 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
426 (defun ruby-forward-string (term &optional end no-error expand
)
428 (let ((n 1) (c (string-to-char term
))
430 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term
"]\\|\\(#{\\)\\)")
431 (concat "[^\\]\\(\\\\\\\\\\)*[" term
"]"))))
432 (while (and (re-search-forward re end no-error
)
433 (if (match-beginning 3)
434 (ruby-forward-string "}{" end no-error nil
)
435 (> (setq n
(if (eq (char-before (point)) c
)
440 ((error "unterminated string")))))
442 (defun ruby-deep-indent-paren-p (c)
444 (cond ((listp ruby-deep-indent-paren
)
445 (let ((deep (assoc c ruby-deep-indent-paren
)))
447 (or (cdr deep
) ruby-deep-indent-paren-style
))
448 ((memq c ruby-deep-indent-paren
)
449 ruby-deep-indent-paren-style
))))
450 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
451 ((eq c ?\
( ) ruby-deep-arglist
)))
453 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
454 "TODO: document throughout function body."
455 (or depth
(setq depth
0))
456 (or indent
(setq indent
0))
457 (when (re-search-forward ruby-delimiter end
'move
)
458 (let ((pnt (point)) w re expand
)
459 (goto-char (match-beginning 0))
461 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
463 ((looking-at "[\"`]") ;skip string
466 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t
))
469 (setq in-string
(point))
474 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
477 (setq in-string
(point))
483 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
484 (if (ruby-forward-string "/" end t t
)
486 (setq in-string
(point))
493 (ruby-expr-beg 'expr-qstr
)
494 (not (looking-at "%="))
495 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
496 (goto-char (match-beginning 1))
497 (setq expand
(not (memq (char-before) '(?q ?w
))))
498 (setq w
(match-string 1))
500 ((string= w
"[") (setq re
"]["))
501 ((string= w
"{") (setq re
"}{"))
502 ((string= w
"(") (setq re
")("))
503 ((string= w
"<") (setq re
"><"))
504 ((and expand
(string= w
"\\"))
505 (setq w
(concat "\\" w
))))
506 (unless (cond (re (ruby-forward-string re end t expand
))
507 (expand (ruby-forward-string w end t t
))
508 (t (re-search-forward
511 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
513 (setq in-string
(point))
517 ((looking-at "\\?") ;skip ?char
519 ((and (ruby-expr-beg)
520 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
521 (goto-char (match-end 0)))
524 ((looking-at "\\$") ;skip $char
527 ((looking-at "#") ;skip comment
531 ((looking-at "[\\[{(]")
532 (let ((deep (ruby-deep-indent-paren-p (char-after))))
533 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
535 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
536 (setq pnt
(1- (match-end 0))))
537 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
538 (setq pcol
(cons (cons pnt depth
) pcol
))
540 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
541 (setq depth
(1+ depth
))))
544 ((looking-at "[])}]")
545 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
546 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
547 (setq depth
(1- depth
)))
548 (setq nest
(cdr nest
))
550 ((looking-at ruby-block-end-re
)
551 (if (or (and (not (bolp))
554 (setq w
(char-after (point)))
559 (setq w
(char-after (point)))
564 (setq nest
(cdr nest
))
565 (setq depth
(1- depth
)))
567 ((looking-at "def\\s +[^(\n;]*")
571 (not (eq ?_
(char-after (point))))))
573 (setq nest
(cons (cons nil pnt
) nest
))
574 (setq depth
(1+ depth
))))
575 (goto-char (match-end 0)))
576 ((looking-at (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
579 (or (not (looking-at (concat "do" ruby-keyword-end-re
)))
581 (back-to-indentation)
582 (not (looking-at ruby-non-block-do-re
)))))
586 (setq w
(char-after (point)))
590 (setq w
(char-after (point)))
592 (skip-chars-forward " \t")
593 (goto-char (match-beginning 0))
594 (or (not (looking-at ruby-modifier-re
))
595 (ruby-expr-beg 'modifier
))
597 (setq nest
(cons (cons nil pnt
) nest
))
598 (setq depth
(1+ depth
)))
600 ((looking-at ":\\(['\"]\\)")
601 (goto-char (match-beginning 1))
602 (ruby-forward-string (match-string 1) end t
))
603 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
604 (goto-char (match-end 0)))
605 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
606 (goto-char (match-end 0)))
607 ((or (looking-at "\\.\\.\\.?")
608 (looking-at "\\.[0-9]+")
609 (looking-at "\\.[a-zA-Z_0-9]+")
611 (goto-char (match-end 0)))
612 ((looking-at "^=begin")
613 (if (re-search-forward "^=end" end t
)
615 (setq in-string
(match-end 0))
619 ((and (ruby-expr-beg 'heredoc
)
620 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
621 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
622 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
623 (let* ((id-end (goto-char (match-end 0)))
624 (line-end-position (point-at-eol))
625 (state (list in-string nest depth pcol indent
)))
626 ;; parse the rest of the line
627 (while (and (> line-end-position
(point))
628 (setq state
(apply 'ruby-parse-partial
629 line-end-position state
))))
630 (setq in-string
(car state
)
634 indent
(nth 4 state
))
635 ;; skip heredoc section
636 (if (re-search-forward (concat "^" re
"$") end
'move
)
638 (setq in-string id-end
)
642 ((looking-at "^__END__$")
644 ((and (looking-at ruby-here-doc-beg-re
)
645 (boundp 'ruby-indent-point
))
646 (if (re-search-forward (ruby-here-doc-end-match)
649 (setq in-string
(match-end 0))
650 (goto-char ruby-indent-point
)))
652 (error (format "bad string %s"
653 (buffer-substring (point) pnt
)
655 (list in-string nest depth pcol
))
657 (defun ruby-parse-region (start end
)
663 (ruby-beginning-of-indent))
665 (narrow-to-region (point) end
)
666 (while (and (> end
(point))
667 (setq state
(apply 'ruby-parse-partial end state
))))))
668 (list (nth 0 state
) ; in-string
669 (car (nth 1 state
)) ; nest
670 (nth 2 state
) ; depth
671 (car (car (nth 3 state
))) ; pcol
672 ;(car (nth 5 state)) ; indent
675 (defun ruby-indent-size (pos nest
)
676 "Return the indentation level in spaces NEST levels deeper than POS."
677 (+ pos
(* (or nest
1) ruby-indent-level
)))
679 (defun ruby-calculate-indent (&optional parse-start
)
680 "Return the proper indentation level of the current line."
681 ;; TODO: Document body
684 (let ((ruby-indent-point (point))
685 (case-fold-search nil
)
686 state eol begin op-end
687 (paren (progn (skip-syntax-forward " ")
688 (and (char-after) (matching-paren (char-after)))))
691 (goto-char parse-start
)
692 (ruby-beginning-of-indent)
693 (setq parse-start
(point)))
694 (back-to-indentation)
695 (setq indent
(current-column))
696 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
698 ((nth 0 state
) ; within string
699 (setq indent nil
)) ; do nothing
700 ((car (nth 1 state
)) ; in paren
701 (goto-char (setq begin
(cdr (nth 1 state
))))
702 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
704 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
705 (skip-syntax-backward " ")
706 (setq indent
(1- (current-column))))
707 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
708 (and (nth 2 s
) (> (nth 2 s
) 0)
709 (or (goto-char (cdr (nth 1 s
))) t
)))
711 (setq indent
(ruby-indent-size (current-column)
714 (setq indent
(current-column))
715 (cond ((eq deep
'space
))
716 (paren (setq indent
(1- indent
)))
717 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
718 (if (nth 3 state
) (goto-char (nth 3 state
))
719 (goto-char parse-start
) (back-to-indentation))
720 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
721 (and (eq (car (nth 1 state
)) paren
)
722 (ruby-deep-indent-paren-p (matching-paren paren
))
723 (search-backward (char-to-string paren
))
724 (setq indent
(current-column)))))
725 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
726 (if (null (cdr (nth 1 state
)))
727 (error "invalid nest"))
728 (goto-char (cdr (nth 1 state
)))
729 (forward-word -
1) ; skip back a keyword
732 ((looking-at "do\\>[^_]") ; iter block is a special case
733 (if (nth 3 state
) (goto-char (nth 3 state
))
734 (goto-char parse-start
) (back-to-indentation))
735 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
737 (setq indent
(+ (current-column) ruby-indent-level
)))))
739 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
740 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
742 (goto-char ruby-indent-point
)
747 ((and (not (ruby-deep-indent-paren-p paren
))
748 (re-search-forward ruby-negative eol t
))
749 (and (not (eq ?_
(char-after (match-end 0))))
750 (setq indent
(- indent ruby-indent-level
))))
755 (or (ruby-deep-indent-paren-p t
)
756 (null (car (nth 1 state
)))))
757 ;; goto beginning of non-empty no-comment line
760 (skip-chars-backward " \t\n")
763 (if (re-search-forward "^\\s *#" end t
)
767 ;; skip the comment at the end
768 (skip-chars-backward " \t")
769 (let (end (pos (point)))
771 (while (and (re-search-forward "#" pos t
)
772 (setq end
(1- (point)))
773 (or (ruby-special-char-p end
)
774 (and (setq state
(ruby-parse-region parse-start end
))
777 (goto-char (or end pos
))
778 (skip-chars-backward " \t")
779 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
780 (setq state
(ruby-parse-region parse-start
(point))))
781 (or (bobp) (forward-char -
1))
783 (or (and (looking-at ruby-symbol-re
)
784 (skip-chars-backward ruby-symbol-chars
)
785 (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>"))
786 (not (eq (point) (nth 3 state
)))
788 (goto-char (match-end 0))
789 (not (looking-at "[a-z_]"))))
790 (and (looking-at ruby-operator-re
)
791 (not (ruby-special-char-p))
792 ;; Operator at the end of line.
793 (let ((c (char-after (point))))
798 ;; (skip-chars-forward " \t")
799 ;; (not (or (eolp) (looking-at "#")
800 ;; (and (eq (car (nth 1 state)) ?{)
801 ;; (looking-at "|"))))))
802 ;; Not a regexp or percent literal.
803 (null (nth 0 (ruby-parse-region (or begin parse-start
)
805 (or (not (eq ?|
(char-after (point))))
807 (or (eolp) (forward-char -
1))
809 ((search-backward "|" nil t
)
810 (skip-chars-backward " \t\n")
814 (not (looking-at "{")))
817 (not (looking-at "do\\>[^_]")))))
825 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>")))
826 (eq (ruby-deep-indent-paren-p t
) 'space
)
829 (goto-char (or begin parse-start
))
830 (skip-syntax-forward " ")
832 ((car (nth 1 state
)) indent
)
834 (+ indent ruby-indent-level
))))))))
835 (goto-char ruby-indent-point
)
837 (skip-syntax-forward " ")
838 (if (looking-at "\\.[^.]")
839 (+ indent ruby-indent-level
)
842 (defun ruby-beginning-of-defun (&optional arg
)
843 "Move backward to the beginning of the current top-level defun.
844 With ARG, move backward multiple defuns. Negative ARG means
847 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re
"\\_>")
849 (beginning-of-line)))
851 (defun ruby-end-of-defun (&optional arg
)
852 "Move forward to the end of the current top-level defun.
853 With ARG, move forward multiple defuns. Negative ARG means
857 (when (looking-back (concat "^\\s *" ruby-block-end-re
))
860 (defun ruby-beginning-of-indent ()
861 "Backtrack to a line which can be used as a reference for
862 calculating indentation on the lines after it."
863 (while (and (re-search-backward ruby-indent-beg-re nil
'move
)
864 (if (ruby-in-ppss-context-p 'anything
)
866 ;; We can stop, then.
867 (beginning-of-line)))))
869 (defun ruby-move-to-block (n)
870 "Move to the beginning (N < 0) or the end (N > 0) of the
871 current block, a sibling block, or an outer block. Do that (abs N) times."
873 (start (ruby-calculate-indent))
874 (signum (if (> n
0) 1 -
1))
879 (setq down
(save-excursion
880 (back-to-indentation)
881 ;; There is a block start or block end keyword on this
882 ;; line, don't need to look for another block.
883 (and (re-search-forward
884 (if backward ruby-block-end-re
885 (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
886 (line-end-position) t
)
887 (not (nth 8 (syntax-ppss))))))
888 (while (and (not done
) (not (if backward
(bobp) (eobp))))
889 (forward-line signum
)
891 ;; Skip empty and commented out lines.
892 ((looking-at "^\\s *$"))
893 ((looking-at "^\\s *#"))
894 ;; Skip block comments;
895 ((and (not backward
) (looking-at "^=begin\\>"))
896 (re-search-forward "^=end\\>"))
897 ((and backward
(looking-at "^=end\\>"))
898 (re-search-backward "^=begin\\>"))
900 (setq pos
(current-indentation))
902 ;; Deeper indentation, we found a block.
903 ;; FIXME: We can't recognize empty blocks this way.
906 ;; Block found, and same indentation as when started, stop.
907 ((and down
(= pos start
))
909 ;; Shallower indentation, means outer block, can stop now.
914 (back-to-indentation)
915 ;; Not really at the first or last line of the block, move on.
916 (if (looking-at (concat "\\<\\(" ruby-block-mid-re
"\\)\\>"))
918 (back-to-indentation)))
920 (defun ruby-beginning-of-block (&optional arg
)
921 "Move backward to the beginning of the current block.
922 With ARG, move up multiple blocks."
924 (ruby-move-to-block (- (or arg
1))))
926 (defun ruby-end-of-block (&optional arg
)
927 "Move forward to the end of the current block.
928 With ARG, move out of multiple blocks."
930 (ruby-move-to-block (or arg
1)))
932 (defun ruby-forward-sexp (&optional arg
)
933 "Move forward across one balanced expression (sexp).
934 With ARG, do it many times. Negative ARG means move backward."
935 ;; TODO: Document body
937 (if (and (numberp arg
) (< arg
0))
938 (ruby-backward-sexp (- arg
))
939 (let ((i (or arg
1)))
942 (skip-syntax-forward " ")
943 (if (looking-at ",\\s *") (goto-char (match-end 0)))
944 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
945 (goto-char (match-end 0)))
947 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
949 (goto-char (scan-sexps (point) 1)))
950 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
951 (not (eq (char-before (point)) ?.
))
952 (not (eq (char-before (point)) ?
:)))
955 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
957 (while (progn (forward-word 1) (looking-at "_")))
958 (cond ((looking-at "::") (forward-char 2) t
)
959 ((> (skip-chars-forward ".") 0))
960 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
961 (forward-char 1) nil
)))))
965 (setq expr
(or expr
(ruby-expr-beg)
966 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
967 (nth 1 (setq state
(apply 'ruby-parse-partial nil state
))))
969 (skip-chars-forward "<"))
972 ((error) (forward-word 1)))
975 (defun ruby-backward-sexp (&optional arg
)
976 "Move backward across one balanced expression (sexp).
977 With ARG, do it many times. Negative ARG means move forward."
978 ;; TODO: Document body
980 (if (and (numberp arg
) (< arg
0))
981 (ruby-forward-sexp (- arg
))
982 (let ((i (or arg
1)))
985 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
987 (cond ((looking-at "\\s)")
988 (goto-char (scan-sexps (1+ (point)) -
1))
990 (?%
(forward-char -
1))
992 (if (eq (char-before (1- (point))) ?%
) (forward-char -
2))))
994 ((looking-at "\\s\"\\|\\\\\\S_")
995 (let ((c (char-to-string (char-before (match-end 0)))))
996 (while (and (search-backward c
)
997 (eq (logand (skip-chars-backward "\\") 1)
1000 ((looking-at "\\s.\\|\\s\\")
1001 (if (ruby-special-char-p) (forward-char -
1)))
1002 ((looking-at "\\s(") nil
)
1005 (while (progn (forward-word -
1)
1008 (?.
(forward-char -
1) t
)
1011 (and (eq (char-before) (char-after)) (forward-char -
1)))
1014 (eq (char-before) :)))))
1015 (if (looking-at ruby-block-end-re
)
1016 (ruby-beginning-of-block))
1022 (defun ruby-indent-exp (&optional ignored
)
1023 "Indent each line in the balanced expression following the point."
1025 (let ((here (point-marker)) start top column
(nest t
))
1026 (set-marker-insertion-type here t
)
1030 (setq start
(point) top
(current-indentation))
1031 (while (and (not (eobp))
1033 (setq column
(ruby-calculate-indent start
))
1034 (cond ((> column top
)
1036 ((and (= column top
) nest
)
1037 (setq nest nil
) t
))))
1038 (ruby-indent-to column
)
1039 (beginning-of-line 2)))
1041 (set-marker here nil
))))
1043 (defun ruby-add-log-current-method ()
1044 "Return the current method name as a string.
1045 This string includes all namespaces.
1054 See `add-log-current-defun-function'."
1057 (let (mname mlist
(indent 0))
1058 ;; Get the current method definition (or class/module).
1059 (if (re-search-backward
1060 (concat "^[ \t]*" ruby-defun-beg-re
"[ \t]+"
1062 ;; \\. and :: for class methods
1063 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1067 (setq mname
(match-string 2))
1068 (unless (string-equal "def" (match-string 1))
1069 (setq mlist
(list mname
) mname nil
))
1070 (goto-char (match-beginning 1))
1071 (setq indent
(current-column))
1072 (beginning-of-line)))
1073 ;; Walk up the class/module nesting.
1074 (while (and (> indent
0)
1077 "^[ \t]*\\(class\\|module\\)[ \t]+"
1078 "\\([A-Z]" ruby-symbol-re
"*\\)")
1080 (goto-char (match-beginning 1))
1081 (if (< (current-column) indent
)
1083 (setq mlist
(cons (match-string 2) mlist
))
1084 (setq indent
(current-column))
1085 (beginning-of-line))))
1086 ;; Process the method name.
1088 (let ((mn (split-string mname
"\\.\\|::")))
1091 (unless (string-equal "self" (car mn
)) ; def self.foo
1093 (let ((ml (nreverse mlist
)))
1094 ;; If the method name references one of the
1095 ;; containing modules, drop the more nested ones.
1097 (if (string-equal (car ml
) (car mn
))
1098 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1099 (or (setq ml
(cdr ml
)) (nreverse mlist
))))
1101 (setcdr (last mlist
) (butlast mn
))
1102 (setq mlist
(butlast mn
))))
1103 (setq mname
(concat "." (car (last mn
)))))
1104 (setq mname
(concat "#" mname
)))))
1105 ;; Generate the string.
1107 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1109 (if mlist
(concat mlist mname
) mname
)
1112 (defun ruby-brace-to-do-end (orig end
)
1113 (let (beg-marker end-marker
)
1115 (when (eq (char-before) ?\
})
1117 (when (save-excursion
1118 (skip-chars-backward " \t")
1122 (setq end-marker
(point-marker))
1123 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w
))
1127 (when (eq (char-syntax (char-before)) ?w
)
1130 (setq beg-marker
(point-marker))
1131 (when (looking-at "\\(\\s \\)*|")
1132 (unless (match-beginning 1)
1134 (goto-char (1+ (match-end 0)))
1135 (search-forward "|"))
1136 (unless (looking-at "\\s *$")
1138 (indent-region beg-marker end-marker
)
1139 (goto-char beg-marker
)
1142 (defun ruby-do-end-to-brace (orig end
)
1143 (let (beg-marker end-marker beg-pos end-pos
)
1144 (goto-char (- end
3))
1145 (when (looking-at ruby-block-end-re
)
1147 (setq end-marker
(point-marker))
1152 (setq beg-marker
(point-marker))
1153 (when (looking-at "\\s +|")
1154 (delete-char (- (match-end 0) (match-beginning 0) 1))
1156 (re-search-forward "|" (line-end-position) t
))
1158 (skip-chars-forward " \t\n\r")
1159 (setq beg-pos
(point))
1160 (goto-char end-marker
)
1161 (skip-chars-backward " \t\n\r")
1162 (setq end-pos
(point)))
1165 (and (= (line-number-at-pos beg-pos
) (line-number-at-pos end-pos
))
1166 (< (+ (current-column) (- end-pos beg-pos
) 2) fill-column
)))
1168 (goto-char end-marker
)
1169 (just-one-space -
1))
1170 (goto-char beg-marker
)
1173 (defun ruby-toggle-block ()
1174 "Toggle block type from do-end to braces or back.
1175 The block must begin on the current line or above it and end after the point.
1176 If the result is do-end block, it will always be multiline."
1178 (let ((start (point)) beg end
)
1181 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1184 (save-match-data (ruby-forward-sexp))
1187 (if (match-beginning 1)
1188 (ruby-brace-to-do-end beg end
)
1189 (ruby-do-end-to-brace beg end
)))
1190 (goto-char start
))))
1192 (declare-function ruby-syntax-propertize-heredoc
"ruby-mode" (limit))
1193 (declare-function ruby-syntax-enclosing-percent-literal
"ruby-mode" (limit))
1194 (declare-function ruby-syntax-propertize-percent-literal
"ruby-mode" (limit))
1196 (if (eval-when-compile (fboundp #'syntax-propertize-rules
))
1197 ;; New code that works independently from font-lock.
1200 (defconst ruby-percent-literal-beg-re
1201 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
1202 "Regexp to match the beginning of percent literal.")
1204 (defconst ruby-syntax-methods-before-regexp
1205 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1206 "assert_match" "Given" "Then" "When")
1207 "Methods that can take regexp as the first argument.
1208 It will be properly highlighted even when the call omits parens."))
1210 (defun ruby-syntax-propertize-function (start end
)
1211 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1213 (ruby-syntax-propertize-heredoc end
)
1214 (ruby-syntax-enclosing-percent-literal end
)
1216 (syntax-propertize-rules
1217 ;; $' $" $` .... are variables.
1218 ;; ?' ?" ?` are ascii codes.
1219 ("\\([?$]\\)[#\"'`]"
1220 (1 (unless (save-excursion
1221 ;; Not within a string.
1222 (nth 3 (syntax-ppss (match-beginning 0))))
1223 (string-to-syntax "\\"))))
1224 ;; Regexps: regexps are distinguished from division because
1225 ;; of the keyword, symbol, or method name before them.
1227 ;; Special tokens that can't be followed by a division operator.
1228 "\\(^\\|[[=(,~?:;<>]"
1229 ;; Control flow keywords and operators following bol or whitespace.
1230 "\\|\\(?:^\\|\\s \\)"
1231 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1232 "or" "not" "&&" "||"))
1233 ;; Method name from the list.
1235 (regexp-opt ruby-syntax-methods-before-regexp
)
1237 ;; The regular expression itself.
1238 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1239 (2 (string-to-syntax "\"/"))
1240 (3 (string-to-syntax "\"/")))
1241 ("^=en\\(d\\)\\_>" (1 "!"))
1242 ("^\\(=\\)begin\\_>" (1 "!"))
1243 ;; Handle here documents.
1244 ((concat ruby-here-doc-beg-re
".*\\(\n\\)")
1245 (7 (unless (ruby-singleton-class-p (match-beginning 0))
1246 (put-text-property (match-beginning 7) (match-end 7)
1247 'syntax-table
(string-to-syntax "\""))
1248 (ruby-syntax-propertize-heredoc end
))))
1249 ;; Handle percent literals: %w(), %q{}, etc.
1250 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re
)
1251 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end
)))))
1254 (defun ruby-syntax-propertize-heredoc (limit)
1255 (let ((ppss (syntax-ppss))
1257 (when (eq ?
\n (nth 3 ppss
))
1259 (goto-char (nth 8 ppss
))
1261 (while (re-search-forward ruby-here-doc-beg-re
1262 (line-end-position) t
)
1263 (unless (ruby-singleton-class-p (match-beginning 0))
1264 (push (concat (ruby-here-doc-end-match) "\n") res
))))
1265 (let ((start (point)))
1266 ;; With multiple openers on the same line, we don't know in which
1267 ;; part `start' is, so we have to go back to the beginning.
1269 (goto-char (nth 8 ppss
))
1270 (setq res
(nreverse res
)))
1271 (while (and res
(re-search-forward (pop res
) limit
'move
))
1273 (put-text-property (1- (point)) (point)
1274 'syntax-table
(string-to-syntax "\""))))
1275 ;; Make extra sure we don't move back, lest we could fall into an
1277 (if (< (point) start
) (goto-char start
))))))
1279 (defun ruby-syntax-enclosing-percent-literal (limit)
1280 (let ((state (syntax-ppss))
1282 ;; When already inside percent literal, re-propertize it.
1283 (when (eq t
(nth 3 state
))
1284 (goto-char (nth 8 state
))
1285 (when (looking-at ruby-percent-literal-beg-re
)
1286 (ruby-syntax-propertize-percent-literal limit
))
1287 (when (< (point) start
) (goto-char start
)))))
1289 (defun ruby-syntax-propertize-percent-literal (limit)
1290 (goto-char (match-beginning 2))
1291 ;; Not inside a simple string or comment.
1292 (when (eq t
(nth 3 (syntax-ppss)))
1293 (let* ((op (char-after))
1294 (ops (char-to-string op
))
1295 (cl (or (cdr (aref (syntax-table) op
))
1296 (cdr (assoc op
'((?
< . ?
>))))))
1297 parse-sexp-lookup-properties
)
1300 (if cl
; Paired delimiters.
1301 ;; Delimiter pairs of the same kind can be nested
1302 ;; inside the literal, as long as they are balanced.
1303 ;; Create syntax table that ignores other characters.
1304 (with-syntax-table (make-char-table 'syntax-table nil
)
1305 (modify-syntax-entry op
(concat "(" (char-to-string cl
)))
1306 (modify-syntax-entry cl
(concat ")" ops
))
1307 (modify-syntax-entry ?
\\ "\\")
1309 (narrow-to-region (point) limit
)
1310 (forward-list))) ; skip to the paired character
1311 ;; Single character delimiter.
1312 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1313 (regexp-quote ops
)) limit nil
))
1314 ;; Found the closing delimiter.
1315 (put-text-property (1- (point)) (point) 'syntax-table
1316 (string-to-syntax "|")))
1317 ;; Unclosed literal, leave the following text unpropertized.
1318 ((scan-error search-failed
) (goto-char limit
))))))
1321 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1322 ;; fallback on the old font-lock-syntactic-keywords stuff.
1324 (defconst ruby-here-doc-end-re
1325 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1326 "Regexp to match the end of heredocs.
1328 This will actually match any line with one or more characters.
1329 It's useful in that it divides up the match string so that
1330 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1332 (defun ruby-here-doc-beg-match ()
1333 "Return a regexp to find the beginning of a heredoc.
1335 This should only be called after matching against `ruby-here-doc-end-re'."
1336 (let ((contents (concat
1337 (regexp-quote (concat (match-string 2) (match-string 3)))
1338 (if (string= (match-string 3) "_") "\\B" "\\b"))))
1340 (let ((match (match-string 1)))
1341 (if (and match
(> (length match
) 0))
1342 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1343 (match-string 1) "\\)"
1344 contents
"\\(\\1\\|\\2\\)")
1345 (concat "-?\\([\"']\\|\\)" contents
"\\1"))))))
1347 (defconst ruby-font-lock-syntactic-keywords
1349 ;; the last $', $", $` in the respective string is not variable
1350 ;; the last ?', ?", ?` in the respective string is not ascii code
1351 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1354 ;; $' $" $` .... are variables
1355 ;; ?' ?" ?` are ascii codes
1356 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil
))
1358 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1361 ("^=en\\(d\\)\\_>" 1 "!")
1363 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1366 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1367 ;; Currently, the following case is highlighted incorrectly:
1376 ;; This is because all here-doc beginnings are highlighted before any endings,
1377 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1378 ;; it thinks <<BAR is part of a string so it's marked as well.
1380 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1381 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1382 ;; but I don't want to try that until we've got unit tests set up
1383 ;; to make sure I don't break anything else.
1384 (,(concat ruby-here-doc-beg-re
".*\\(\n\\)")
1385 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re
))
1386 (ruby-here-doc-beg-syntax))
1387 (,ruby-here-doc-end-re
3 (ruby-here-doc-end-syntax)))
1388 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1390 (defun ruby-comment-beg-syntax ()
1391 "Return the syntax cell for a the first character of a =begin.
1392 See the definition of `ruby-font-lock-syntactic-keywords'.
1394 This returns a comment-delimiter cell as long as the =begin
1395 isn't in a string or another comment."
1396 (when (not (nth 3 (syntax-ppss)))
1397 (string-to-syntax "!")))
1399 (defun ruby-in-here-doc-p ()
1400 "Return whether or not the point is in a heredoc."
1402 (let ((old-point (point)) (case-fold-search nil
))
1405 (while (and (re-search-backward ruby-here-doc-beg-re nil t
)
1406 (not (ruby-singleton-class-p)))
1407 (if (not (or (ruby-in-ppss-context-p 'anything
)
1408 (ruby-here-doc-find-end old-point
)))
1409 (throw 'found-beg t
)))))))
1411 (defun ruby-here-doc-find-end (&optional limit
)
1412 "Expects the point to be on a line with one or more heredoc openers.
1413 Returns the buffer position at which all heredocs on the line
1414 are terminated, or nil if they aren't terminated before the
1415 buffer position `limit' or the end of the buffer."
1419 (let ((eol (point-at-eol))
1420 (case-fold-search nil
)
1421 ;; Fake match data such that (match-end 0) is at eol
1422 (end-match-data (progn (looking-at ".*$") (match-data)))
1423 beg-match-data end-re
)
1424 (while (re-search-forward ruby-here-doc-beg-re eol t
)
1425 (setq beg-match-data
(match-data))
1426 (setq end-re
(ruby-here-doc-end-match))
1428 (set-match-data end-match-data
)
1429 (goto-char (match-end 0))
1430 (unless (re-search-forward end-re limit t
) (throw 'done nil
))
1431 (setq end-match-data
(match-data))
1433 (set-match-data beg-match-data
)
1434 (goto-char (match-end 0)))
1435 (set-match-data end-match-data
)
1436 (goto-char (match-end 0))
1439 (defun ruby-here-doc-beg-syntax ()
1440 "Return the syntax cell for a line that may begin a heredoc.
1441 See the definition of `ruby-font-lock-syntactic-keywords'.
1443 This sets the syntax cell for the newline ending the line
1444 containing the heredoc beginning so that cases where multiple
1445 heredocs are started on one line are handled correctly."
1447 (goto-char (match-beginning 0))
1448 (unless (or (ruby-in-ppss-context-p 'non-heredoc
)
1449 (ruby-in-here-doc-p))
1450 (string-to-syntax "\""))))
1452 (defun ruby-here-doc-end-syntax ()
1453 "Return the syntax cell for a line that may end a heredoc.
1454 See the definition of `ruby-font-lock-syntactic-keywords'."
1455 (let ((pss (syntax-ppss)) (case-fold-search nil
))
1456 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1457 ;; so we can just give up.
1458 ;; This means we aren't doing a full-document search
1459 ;; every time we enter a character.
1460 (when (ruby-in-ppss-context-p 'heredoc pss
)
1462 (goto-char (nth 8 pss
)) ; Go to the beginning of heredoc.
1463 (let ((eol (point)))
1465 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t
) ; If there is a heredoc that matches this line...
1466 (not (ruby-in-ppss-context-p 'anything
)) ; And that's not inside a heredoc/string/comment...
1467 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1468 (not (re-search-forward ruby-here-doc-beg-re eol t
))))
1469 (string-to-syntax "\"")))))))
1471 (unless (functionp 'syntax-ppss
)
1472 (defun syntax-ppss (&optional pos
)
1473 (parse-partial-sexp (point-min) (or pos
(point)))))
1476 (defun ruby-in-ppss-context-p (context &optional ppss
)
1477 (let ((ppss (or ppss
(syntax-ppss (point)))))
1479 ((eq context
'anything
)
1482 ((eq context
'string
)
1484 ((eq context
'heredoc
)
1485 (eq ?
\n (nth 3 ppss
)))
1486 ((eq context
'non-heredoc
)
1487 (and (ruby-in-ppss-context-p 'anything
)
1488 (not (ruby-in-ppss-context-p 'heredoc
))))
1489 ((eq context
'comment
)
1493 "Internal error on `ruby-in-ppss-context-p': "
1494 "context name `" (symbol-name context
) "' is unknown"))))
1497 (if (featurep 'xemacs
)
1498 (put 'ruby-mode
'font-lock-defaults
1499 '((ruby-font-lock-keywords)
1502 (font-lock-syntactic-keywords
1503 . ruby-font-lock-syntactic-keywords
))))
1505 (defvar ruby-font-lock-syntax-table
1506 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1507 (modify-syntax-entry ?_
"w" tbl
)
1509 "The syntax table to use for fontifying Ruby mode buffers.
1510 See `font-lock-syntax-table'.")
1512 (defconst ruby-font-lock-keywords
1515 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1516 1 font-lock-function-name-face
)
1519 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1563 ruby-keyword-end-re
)
1565 ;; here-doc beginnings
1566 `(,ruby-here-doc-beg-re
0 (unless (ruby-singleton-class-p (match-beginning 0))
1567 'font-lock-string-face
))
1569 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1570 2 font-lock-variable-name-face
)
1572 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1573 2 font-lock-constant-face
)
1575 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1576 1 font-lock-variable-name-face
)
1577 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1578 0 font-lock-variable-name-face
)
1580 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1581 2 font-lock-type-face
)
1582 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face
)
1583 ;; expression expansion
1584 '(ruby-match-expression-expansion
1585 0 font-lock-variable-name-face t
)
1586 ;; warn lower camel case
1587 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1588 ; 0 font-lock-warning-face)
1590 "Additional expressions to highlight in Ruby mode.")
1592 (defun ruby-match-expression-expansion (limit)
1593 (when (re-search-forward "[^\\]\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)" limit
'move
)
1594 (or (ruby-in-ppss-context-p 'string
)
1595 (ruby-match-expression-expansion limit
))))
1598 (define-derived-mode ruby-mode prog-mode
"Ruby"
1599 "Major mode for editing Ruby scripts.
1600 \\[ruby-indent-line] properly indents subexpressions of multi-line
1601 class, module, def, if, while, for, do, and case statements, taking
1602 nesting into account.
1604 The variable `ruby-indent-level' controls the amount of indentation.
1607 (ruby-mode-variables)
1609 (set (make-local-variable 'imenu-create-index-function
)
1610 'ruby-imenu-create-index
)
1611 (set (make-local-variable 'add-log-current-defun-function
)
1612 'ruby-add-log-current-method
)
1613 (set (make-local-variable 'beginning-of-defun-function
)
1614 'ruby-beginning-of-defun
)
1615 (set (make-local-variable 'end-of-defun-function
)
1619 (cond ((boundp 'before-save-hook
) 'before-save-hook
)
1620 ((boundp 'write-contents-functions
) 'write-contents-functions
)
1621 ((boundp 'write-contents-hooks
) 'write-contents-hooks
))
1622 'ruby-mode-set-encoding nil
'local
)
1624 (set (make-local-variable 'electric-indent-chars
)
1625 (append '(?\
{ ?\
}) electric-indent-chars
))
1627 (set (make-local-variable 'font-lock-defaults
)
1628 '((ruby-font-lock-keywords) nil nil
))
1629 (set (make-local-variable 'font-lock-keywords
)
1630 ruby-font-lock-keywords
)
1631 (set (make-local-variable 'font-lock-syntax-table
)
1632 ruby-font-lock-syntax-table
)
1634 (if (eval-when-compile (fboundp 'syntax-propertize-rules
))
1635 (set (make-local-variable 'syntax-propertize-function
)
1636 #'ruby-syntax-propertize-function
)
1637 (set (make-local-variable 'font-lock-syntactic-keywords
)
1638 ruby-font-lock-syntactic-keywords
)))
1640 ;;; Invoke ruby-mode when appropriate
1643 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.rb\\'") 'ruby-mode
))
1645 (add-to-list 'auto-mode-alist
'("Rakefile\\'" . ruby-mode
))
1648 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
1649 (add-to-list 'interpreter-mode-alist
(cons (purecopy name
) 'ruby-mode
)))
1651 (provide 'ruby-mode
)
1653 ;;; ruby-mode.el ends here