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-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")) "\\|"
68 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin"))
70 "Regexp to match where the indentation gets deeper.")
72 (defconst ruby-modifier-beg-keywords
73 '("if" "unless" "while" "until")
74 "Modifiers that are the same as the beginning of blocks.")
76 (defconst ruby-modifier-beg-re
77 (regexp-opt ruby-modifier-beg-keywords
)
78 "Regexp to match modifiers same as the beginning of blocks.")
80 (defconst ruby-modifier-re
81 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords
))
82 "Regexp to match modifiers.")
84 (defconst ruby-block-mid-keywords
85 '("then" "else" "elsif" "when" "rescue" "ensure")
86 "Keywords where the indentation gets shallower in middle of block statements.")
88 (defconst ruby-block-mid-re
89 (regexp-opt ruby-block-mid-keywords
)
90 "Regexp to match where the indentation gets shallower in middle of block statements.")
92 (defconst ruby-block-op-keywords
94 "Regexp to match boolean keywords.")
96 (defconst ruby-block-hanging-re
97 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords
))
98 "Regexp to match hanging block modifiers.")
100 (defconst ruby-block-end-re
"\\_<end\\_>")
102 (defconst ruby-defun-beg-re
103 '"\\(def\\|class\\|module\\)"
104 "Regexp to match the beginning of a defun, in the general sense.")
106 (defconst ruby-singleton-class-re
108 "Regexp to match the beginning of a singleton class context.")
111 (defconst ruby-here-doc-beg-re
112 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
113 "Regexp to match the beginning of a heredoc.")
115 (defconst ruby-expression-expansion-re
116 "[^\\]\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)"))
118 (defun ruby-here-doc-end-match ()
119 "Return a regexp to find the end of a heredoc.
121 This should only be called after matching against `ruby-here-doc-beg-re'."
123 (if (match-string 2) "[ \t]*" nil
)
129 (defconst ruby-delimiter
130 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
132 "\\)\\_>\\|" ruby-block-end-re
133 "\\|^=begin\\|" ruby-here-doc-beg-re
))
135 (defconst ruby-negative
136 (concat "^[ \t]*\\(\\(" ruby-block-mid-re
"\\)\\>\\|"
137 ruby-block-end-re
"\\|}\\|\\]\\)")
138 "Regexp to match where the indentation gets shallower.")
140 (defconst ruby-operator-re
"[-,.+*/%&|^~=<>:]"
141 "Regexp to match operators.")
143 (defconst ruby-symbol-chars
"a-zA-Z0-9_"
144 "List of characters that symbol names may contain.")
145 (defconst ruby-symbol-re
(concat "[" ruby-symbol-chars
"]")
146 "Regexp to match symbols.")
148 (define-abbrev-table 'ruby-mode-abbrev-table
()
149 "Abbrev table in use in Ruby mode buffers.")
151 (defvar ruby-mode-map
152 (let ((map (make-sparse-keymap)))
153 (define-key map
(kbd "M-C-b") 'ruby-backward-sexp
)
154 (define-key map
(kbd "M-C-f") 'ruby-forward-sexp
)
155 (define-key map
(kbd "M-C-p") 'ruby-beginning-of-block
)
156 (define-key map
(kbd "M-C-n") 'ruby-end-of-block
)
157 (define-key map
(kbd "M-C-q") 'ruby-indent-exp
)
158 (define-key map
(kbd "C-c {") 'ruby-toggle-block
)
160 "Keymap used in Ruby mode.")
162 (defvar ruby-mode-syntax-table
163 (let ((table (make-syntax-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 ?
\n ">" table
)
169 (modify-syntax-entry ?
\\ "\\" table
)
170 (modify-syntax-entry ?$
"." table
)
171 (modify-syntax-entry ??
"_" table
)
172 (modify-syntax-entry ?_
"_" table
)
173 (modify-syntax-entry ?
: "_" table
)
174 (modify-syntax-entry ?
< "." table
)
175 (modify-syntax-entry ?
> "." table
)
176 (modify-syntax-entry ?
& "." table
)
177 (modify-syntax-entry ?|
"." table
)
178 (modify-syntax-entry ?%
"." table
)
179 (modify-syntax-entry ?
= "." table
)
180 (modify-syntax-entry ?
/ "." table
)
181 (modify-syntax-entry ?
+ "." table
)
182 (modify-syntax-entry ?
* "." table
)
183 (modify-syntax-entry ?-
"." table
)
184 (modify-syntax-entry ?\
; "." table)
185 (modify-syntax-entry ?\
( "()" table
)
186 (modify-syntax-entry ?\
) ")(" table
)
187 (modify-syntax-entry ?\
{ "(}" table
)
188 (modify-syntax-entry ?\
} "){" table
)
189 (modify-syntax-entry ?\
[ "(]" table
)
190 (modify-syntax-entry ?\
] ")[" table
)
192 "Syntax table to use in Ruby mode.")
194 (defcustom ruby-indent-tabs-mode nil
195 "Indentation can insert tabs in Ruby mode if this is non-nil."
196 :type
'boolean
:group
'ruby
)
198 (defcustom ruby-indent-level
2
199 "Indentation of Ruby statements."
200 :type
'integer
:group
'ruby
)
202 (defcustom ruby-comment-column
32
203 "Indentation column of comments."
204 :type
'integer
:group
'ruby
)
206 (defcustom ruby-deep-arglist t
207 "Deep indent lists in parenthesis when non-nil.
208 Also ignores spaces after parenthesis when 'space."
211 (defcustom ruby-deep-indent-paren
'(?\
( ?\
[ ?\
] t
)
212 "Deep indent lists in parenthesis when non-nil.
213 The value t means continuous line.
214 Also ignores spaces after parenthesis when 'space."
217 (defcustom ruby-deep-indent-paren-style
'space
218 "Default deep indent style."
219 :options
'(t nil space
) :group
'ruby
)
221 (defcustom ruby-encoding-map
'((shift_jis . cp932
) (shift-jis . cp932
))
222 "Alist to map encoding name from Emacs to Ruby."
225 (defcustom ruby-insert-encoding-magic-comment t
226 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
227 :type
'boolean
:group
'ruby
)
229 (defcustom ruby-use-encoding-map t
230 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
231 :type
'boolean
:group
'ruby
)
233 ;; Safe file variables
234 (put 'ruby-indent-tabs-mode
'safe-local-variable
'booleanp
)
235 (put 'ruby-indent-level
'safe-local-variable
'integerp
)
236 (put 'ruby-comment-column
'safe-local-variable
'integerp
)
237 (put 'ruby-deep-arglist
'safe-local-variable
'booleanp
)
239 (defun ruby-imenu-create-index-in-block (prefix beg end
)
240 "Create an imenu index of methods inside a block."
241 (let ((index-alist '()) (case-fold-search nil
)
242 name next pos decl sing
)
244 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t
)
245 (setq sing
(match-beginning 3))
246 (setq decl
(match-string 5))
247 (setq next
(match-end 0))
248 (setq name
(or (match-string 4) (match-string 6)))
249 (setq pos
(match-beginning 0))
251 ((string= "alias" decl
)
252 (if prefix
(setq name
(concat prefix name
)))
253 (push (cons name pos
) index-alist
))
254 ((string= "def" decl
)
258 ((string-match "^self\." name
)
259 (concat (substring prefix
0 -
1) (substring name
4)))
260 (t (concat prefix name
)))))
261 (push (cons name pos
) index-alist
)
262 (ruby-accurate-end-of-block end
))
264 (if (string= "self" name
)
265 (if prefix
(setq name
(substring prefix
0 -
1)))
266 (if prefix
(setq name
(concat (substring prefix
0 -
1) "::" name
)))
267 (push (cons name pos
) index-alist
))
268 (ruby-accurate-end-of-block end
)
271 (nconc (ruby-imenu-create-index-in-block
272 (concat name
(if sing
"." "#"))
273 next beg
) index-alist
))
277 (defun ruby-imenu-create-index ()
278 "Create an imenu index of all methods in the buffer."
279 (nreverse (ruby-imenu-create-index-in-block nil
(point-min) nil
)))
281 (defun ruby-accurate-end-of-block (&optional end
)
284 (end (or end
(point-max))))
285 (while (and (setq state
(apply 'ruby-parse-partial end state
))
286 (>= (nth 2 state
) 0) (< (point) end
)))))
288 (defun ruby-mode-variables ()
289 "Set up initial buffer-local variables for Ruby mode."
290 (set-syntax-table ruby-mode-syntax-table
)
291 (setq local-abbrev-table ruby-mode-abbrev-table
)
292 (setq indent-tabs-mode ruby-indent-tabs-mode
)
293 (set (make-local-variable 'indent-line-function
) 'ruby-indent-line
)
294 (set (make-local-variable 'require-final-newline
) t
)
295 (set (make-local-variable 'comment-start
) "# ")
296 (set (make-local-variable 'comment-end
) "")
297 (set (make-local-variable 'comment-column
) ruby-comment-column
)
298 (set (make-local-variable 'comment-start-skip
) "#+ *")
299 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
300 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
301 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
302 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
303 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
))
305 (defun ruby-mode-set-encoding ()
306 "Insert a magic comment header with the proper encoding if necessary."
309 (goto-char (point-min))
310 (when (re-search-forward "[^\0-\177]" nil t
)
311 (goto-char (point-min))
313 (or coding-system-for-write
314 buffer-file-coding-system
)))
317 (or (coding-system-get coding-system
'mime-charset
)
318 (coding-system-change-eol-conversion coding-system nil
))))
322 (or (and ruby-use-encoding-map
323 (cdr (assq coding-system ruby-encoding-map
)))
326 (if (looking-at "^#!") (beginning-of-line 2))
327 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
328 (unless (string= (match-string 2) coding-system
)
329 (goto-char (match-beginning 2))
330 (delete-region (point) (match-end 2))
331 (and (looking-at "-\*-")
332 (let ((n (skip-chars-backward " ")))
333 (cond ((= n
0) (insert " ") (backward-char))
334 ((= n -
1) (insert " "))
336 (insert coding-system
)))
337 ((looking-at "\\s *#.*coding\\s *[:=]"))
338 (t (when ruby-insert-encoding-magic-comment
339 (insert "# -*- coding: " coding-system
" -*-\n"))))))))
341 (defun ruby-current-indentation ()
342 "Return the indentation level of current line."
345 (back-to-indentation)
348 (defun ruby-indent-line (&optional ignored
)
349 "Correct the indentation of the current Ruby line."
351 (ruby-indent-to (ruby-calculate-indent)))
353 (defun ruby-indent-to (column)
354 "Indent the current line to COLUMN."
357 (and (< column
0) (error "invalid nest"))
358 (setq shift
(current-column))
361 (back-to-indentation)
362 (setq top
(current-column))
363 (skip-chars-backward " \t")
364 (if (>= shift top
) (setq shift
(- shift top
))
368 (move-to-column (+ column shift
))
370 (delete-region beg
(point))
373 (move-to-column (+ column shift
))))))
375 (defun ruby-special-char-p (&optional pos
)
376 "Return t if the character before POS is a special character.
377 If omitted, POS defaults to the current point.
378 Special characters are `?', `$', `:' when preceded by whitespace,
379 and `\\' when preceded by `?'."
380 (setq pos
(or pos
(point)))
381 (let ((c (char-before pos
)) (b (and (< (point-min) pos
)
382 (char-before (1- pos
)))))
383 (cond ((or (eq c ??
) (eq c ?$
)))
384 ((and (eq c ?
:) (or (not b
) (eq (char-syntax b
) ?
))))
385 ((eq c ?
\\) (eq b ??
)))))
387 (defun ruby-singleton-class-p (&optional pos
)
389 (when pos
(goto-char pos
))
391 (and (or (bolp) (not (eq (char-before (point)) ?_
)))
392 (looking-at ruby-singleton-class-re
))))
394 (defun ruby-expr-beg (&optional option
)
395 "Check if point is possibly at the beginning of an expression.
396 OPTION specifies the type of the expression.
397 Can be one of `heredoc', `modifier', `expr-qstr', `expr-re'."
399 (store-match-data nil
)
400 (let ((space (skip-chars-backward " \t"))
406 (and (looking-at "\\?")
407 (or (eq (char-syntax (char-before (point))) ?w
)
408 (ruby-special-char-p))))
410 ((looking-at ruby-operator-re
))
411 ((eq option
'heredoc
)
412 (and (< space
0) (not (ruby-singleton-class-p start
))))
413 ((or (looking-at "[\\[({,;]")
414 (and (looking-at "[!?]")
415 (or (not (eq option
'modifier
))
417 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
418 (and (looking-at ruby-symbol-re
)
419 (skip-chars-backward ruby-symbol-chars
)
421 ((looking-at (regexp-opt
422 (append ruby-block-beg-keywords
423 ruby-block-op-keywords
424 ruby-block-mid-keywords
)
426 (goto-char (match-end 0))
427 (not (looking-at "\\s_\\|!")))
428 ((eq option
'expr-qstr
)
429 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
430 ((eq option
'expr-re
)
431 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
434 (defun ruby-forward-string (term &optional end no-error expand
)
436 (let ((n 1) (c (string-to-char term
))
438 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term
"]\\|\\(#{\\)\\)")
439 (concat "[^\\]\\(\\\\\\\\\\)*[" term
"]"))))
440 (while (and (re-search-forward re end no-error
)
441 (if (match-beginning 3)
442 (ruby-forward-string "}{" end no-error nil
)
443 (> (setq n
(if (eq (char-before (point)) c
)
448 ((error "unterminated string")))))
450 (defun ruby-deep-indent-paren-p (c)
452 (cond ((listp ruby-deep-indent-paren
)
453 (let ((deep (assoc c ruby-deep-indent-paren
)))
455 (or (cdr deep
) ruby-deep-indent-paren-style
))
456 ((memq c ruby-deep-indent-paren
)
457 ruby-deep-indent-paren-style
))))
458 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
459 ((eq c ?\
( ) ruby-deep-arglist
)))
461 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
462 "TODO: document throughout function body."
463 (or depth
(setq depth
0))
464 (or indent
(setq indent
0))
465 (when (re-search-forward ruby-delimiter end
'move
)
466 (let ((pnt (point)) w re expand
)
467 (goto-char (match-beginning 0))
469 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
471 ((looking-at "[\"`]") ;skip string
474 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t
))
477 (setq in-string
(point))
482 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
485 (setq in-string
(point))
491 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
492 (if (ruby-forward-string "/" end t t
)
494 (setq in-string
(point))
501 (ruby-expr-beg 'expr-qstr
)
502 (not (looking-at "%="))
503 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
504 (goto-char (match-beginning 1))
505 (setq expand
(not (memq (char-before) '(?q ?w
))))
506 (setq w
(match-string 1))
508 ((string= w
"[") (setq re
"]["))
509 ((string= w
"{") (setq re
"}{"))
510 ((string= w
"(") (setq re
")("))
511 ((string= w
"<") (setq re
"><"))
512 ((and expand
(string= w
"\\"))
513 (setq w
(concat "\\" w
))))
514 (unless (cond (re (ruby-forward-string re end t expand
))
515 (expand (ruby-forward-string w end t t
))
516 (t (re-search-forward
519 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
521 (setq in-string
(point))
525 ((looking-at "\\?") ;skip ?char
527 ((and (ruby-expr-beg)
528 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
529 (goto-char (match-end 0)))
532 ((looking-at "\\$") ;skip $char
535 ((looking-at "#") ;skip comment
539 ((looking-at "[\\[{(]")
540 (let ((deep (ruby-deep-indent-paren-p (char-after))))
541 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
543 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
544 (setq pnt
(1- (match-end 0))))
545 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
546 (setq pcol
(cons (cons pnt depth
) pcol
))
548 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
549 (setq depth
(1+ depth
))))
552 ((looking-at "[])}]")
553 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
554 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
555 (setq depth
(1- depth
)))
556 (setq nest
(cdr nest
))
558 ((looking-at ruby-block-end-re
)
559 (if (or (and (not (bolp))
562 (setq w
(char-after (point)))
567 (setq w
(char-after (point)))
572 (setq nest
(cdr nest
))
573 (setq depth
(1- depth
)))
575 ((looking-at "def\\s +[^(\n;]*")
579 (not (eq ?_
(char-after (point))))))
581 (setq nest
(cons (cons nil pnt
) nest
))
582 (setq depth
(1+ depth
))))
583 (goto-char (match-end 0)))
584 ((looking-at (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
587 (or (not (looking-at (concat "do" ruby-keyword-end-re
)))
589 (back-to-indentation)
590 (not (looking-at ruby-non-block-do-re
)))))
594 (setq w
(char-after (point)))
598 (not (eq ?
! (char-after (point))))
599 (skip-chars-forward " \t")
600 (goto-char (match-beginning 0))
601 (or (not (looking-at ruby-modifier-re
))
602 (ruby-expr-beg 'modifier
))
604 (setq nest
(cons (cons nil pnt
) nest
))
605 (setq depth
(1+ depth
)))
607 ((looking-at ":\\(['\"]\\)")
608 (goto-char (match-beginning 1))
609 (ruby-forward-string (match-string 1) end t
))
610 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
611 (goto-char (match-end 0)))
612 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
613 (goto-char (match-end 0)))
614 ((or (looking-at "\\.\\.\\.?")
615 (looking-at "\\.[0-9]+")
616 (looking-at "\\.[a-zA-Z_0-9]+")
618 (goto-char (match-end 0)))
619 ((looking-at "^=begin")
620 (if (re-search-forward "^=end" end t
)
622 (setq in-string
(match-end 0))
626 ((and (ruby-expr-beg 'heredoc
)
627 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
628 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
629 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
630 (let* ((id-end (goto-char (match-end 0)))
631 (line-end-position (point-at-eol))
632 (state (list in-string nest depth pcol indent
)))
633 ;; parse the rest of the line
634 (while (and (> line-end-position
(point))
635 (setq state
(apply 'ruby-parse-partial
636 line-end-position state
))))
637 (setq in-string
(car state
)
641 indent
(nth 4 state
))
642 ;; skip heredoc section
643 (if (re-search-forward (concat "^" re
"$") end
'move
)
645 (setq in-string id-end
)
649 ((looking-at "^__END__$")
651 ((and (looking-at ruby-here-doc-beg-re
)
652 (boundp 'ruby-indent-point
))
653 (if (re-search-forward (ruby-here-doc-end-match)
656 (setq in-string
(match-end 0))
657 (goto-char ruby-indent-point
)))
659 (error (format "bad string %s"
660 (buffer-substring (point) pnt
)
662 (list in-string nest depth pcol
))
664 (defun ruby-parse-region (start end
)
670 (ruby-beginning-of-indent))
672 (narrow-to-region (point) end
)
673 (while (and (> end
(point))
674 (setq state
(apply 'ruby-parse-partial end state
))))))
675 (list (nth 0 state
) ; in-string
676 (car (nth 1 state
)) ; nest
677 (nth 2 state
) ; depth
678 (car (car (nth 3 state
))) ; pcol
679 ;(car (nth 5 state)) ; indent
682 (defun ruby-indent-size (pos nest
)
683 "Return the indentation level in spaces NEST levels deeper than POS."
684 (+ pos
(* (or nest
1) ruby-indent-level
)))
686 (defun ruby-calculate-indent (&optional parse-start
)
687 "Return the proper indentation level of the current line."
688 ;; TODO: Document body
691 (let ((ruby-indent-point (point))
692 (case-fold-search nil
)
693 state eol begin op-end
694 (paren (progn (skip-syntax-forward " ")
695 (and (char-after) (matching-paren (char-after)))))
698 (goto-char parse-start
)
699 (ruby-beginning-of-indent)
700 (setq parse-start
(point)))
701 (back-to-indentation)
702 (setq indent
(current-column))
703 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
705 ((nth 0 state
) ; within string
706 (setq indent nil
)) ; do nothing
707 ((car (nth 1 state
)) ; in paren
708 (goto-char (setq begin
(cdr (nth 1 state
))))
709 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
711 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
712 (skip-syntax-backward " ")
713 (setq indent
(1- (current-column))))
714 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
715 (and (nth 2 s
) (> (nth 2 s
) 0)
716 (or (goto-char (cdr (nth 1 s
))) t
)))
718 (setq indent
(ruby-indent-size (current-column)
721 (setq indent
(current-column))
722 (cond ((eq deep
'space
))
723 (paren (setq indent
(1- indent
)))
724 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
725 (if (nth 3 state
) (goto-char (nth 3 state
))
726 (goto-char parse-start
) (back-to-indentation))
727 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
728 (and (eq (car (nth 1 state
)) paren
)
729 (ruby-deep-indent-paren-p (matching-paren paren
))
730 (search-backward (char-to-string paren
))
731 (setq indent
(current-column)))))
732 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
733 (if (null (cdr (nth 1 state
)))
734 (error "invalid nest"))
735 (goto-char (cdr (nth 1 state
)))
736 (forward-word -
1) ; skip back a keyword
739 ((looking-at "do\\>[^_]") ; iter block is a special case
740 (if (nth 3 state
) (goto-char (nth 3 state
))
741 (goto-char parse-start
) (back-to-indentation))
742 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
744 (setq indent
(+ (current-column) ruby-indent-level
)))))
746 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
747 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
749 (goto-char ruby-indent-point
)
754 ((and (not (ruby-deep-indent-paren-p paren
))
755 (re-search-forward ruby-negative eol t
))
756 (and (not (eq ?_
(char-after (match-end 0))))
757 (setq indent
(- indent ruby-indent-level
))))
762 (or (ruby-deep-indent-paren-p t
)
763 (null (car (nth 1 state
)))))
764 ;; goto beginning of non-empty no-comment line
767 (skip-chars-backward " \t\n")
770 (if (re-search-forward "^\\s *#" end t
)
774 ;; skip the comment at the end
775 (skip-chars-backward " \t")
776 (let (end (pos (point)))
778 (while (and (re-search-forward "#" pos t
)
779 (setq end
(1- (point)))
780 (or (ruby-special-char-p end
)
781 (and (setq state
(ruby-parse-region parse-start end
))
784 (goto-char (or end pos
))
785 (skip-chars-backward " \t")
786 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
787 (setq state
(ruby-parse-region parse-start
(point))))
788 (or (bobp) (forward-char -
1))
790 (or (and (looking-at ruby-symbol-re
)
791 (skip-chars-backward ruby-symbol-chars
)
792 (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>"))
793 (not (eq (point) (nth 3 state
)))
795 (goto-char (match-end 0))
796 (not (looking-at "[a-z_]"))))
797 (and (looking-at ruby-operator-re
)
798 (not (ruby-special-char-p))
799 ;; Operator at the end of line.
800 (let ((c (char-after (point))))
805 ;; (skip-chars-forward " \t")
806 ;; (not (or (eolp) (looking-at "#")
807 ;; (and (eq (car (nth 1 state)) ?{)
808 ;; (looking-at "|"))))))
809 ;; Not a regexp or percent literal.
810 (null (nth 0 (ruby-parse-region (or begin parse-start
)
812 (or (not (eq ?|
(char-after (point))))
814 (or (eolp) (forward-char -
1))
816 ((search-backward "|" nil t
)
817 (skip-chars-backward " \t\n")
821 (not (looking-at "{")))
824 (not (looking-at "do\\>[^_]")))))
832 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>")))
833 (eq (ruby-deep-indent-paren-p t
) 'space
)
836 (goto-char (or begin parse-start
))
837 (skip-syntax-forward " ")
839 ((car (nth 1 state
)) indent
)
841 (+ indent ruby-indent-level
))))))))
842 (goto-char ruby-indent-point
)
844 (skip-syntax-forward " ")
845 (if (looking-at "\\.[^.]")
846 (+ indent ruby-indent-level
)
849 (defun ruby-beginning-of-defun (&optional arg
)
850 "Move backward to the beginning of the current defun.
851 With ARG, move backward multiple defuns. Negative ARG means
854 (let (case-fold-search)
855 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re
"\\_>")
857 (beginning-of-line))))
859 (defun ruby-end-of-defun ()
860 "Move point to the end of the current defun.
861 The defun begins at or after the point. This function is called
865 (let (case-fold-search)
866 (when (looking-back (concat "^\\s *" ruby-block-end-re
))
869 (defun ruby-beginning-of-indent ()
870 "Backtrack to a line which can be used as a reference for
871 calculating indentation on the lines after it."
872 (while (and (re-search-backward ruby-indent-beg-re nil
'move
)
873 (if (ruby-in-ppss-context-p 'anything
)
875 ;; We can stop, then.
876 (beginning-of-line)))))
878 (defun ruby-move-to-block (n)
879 "Move to the beginning (N < 0) or the end (N > 0) of the
880 current block, a sibling block, or an outer block. Do that (abs N) times."
881 (let ((signum (if (> n
0) 1 -
1))
883 (depth (or (nth 2 (ruby-parse-region (line-beginning-position)
884 (line-end-position)))
888 (when (< (* depth signum
) 0)
889 ;; Moving end -> end or beginning -> beginning.
893 (setq down
(save-excursion
894 (back-to-indentation)
895 ;; There is a block start or block end keyword on this
896 ;; line, don't need to look for another block.
897 (and (re-search-forward
898 (if backward ruby-block-end-re
899 (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
900 (line-end-position) t
)
901 (not (nth 8 (syntax-ppss))))))
902 (while (and (not done
) (not (if backward
(bobp) (eobp))))
903 (forward-line signum
)
905 ;; Skip empty and commented out lines.
906 ((looking-at "^\\s *$"))
907 ((looking-at "^\\s *#"))
908 ;; Skip block comments;
909 ((and (not backward
) (looking-at "^=begin\\>"))
910 (re-search-forward "^=end\\>"))
911 ((and backward
(looking-at "^=end\\>"))
912 (re-search-backward "^=begin\\>"))
913 ;; Jump over a multiline literal.
914 ((ruby-in-ppss-context-p 'string
)
915 (goto-char (nth 8 (syntax-ppss)))
918 (when (bolp) (forward-char -
1)))) ; After a heredoc.
920 (let ((state (ruby-parse-region (point) (line-end-position))))
921 (unless (car state
) ; Line ends with unfinished string.
922 (setq depth
(+ (nth 2 state
) depth
))))
924 ;; Deeper indentation, we found a block.
925 ;; FIXME: We can't recognize empty blocks this way.
926 ((> (* signum depth
) 0)
928 ;; Block found, and same indentation as when started, stop.
929 ((and down
(zerop depth
))
931 ;; Shallower indentation, means outer block, can stop now.
932 ((< (* signum depth
) 0)
936 (back-to-indentation)
937 ;; Not really at the first or last line of the block, move on.
938 (if (looking-at (concat "\\<\\(" ruby-block-mid-re
"\\)\\>"))
940 (back-to-indentation)))
942 (defun ruby-beginning-of-block (&optional arg
)
943 "Move backward to the beginning of the current block.
944 With ARG, move up multiple blocks."
946 (ruby-move-to-block (- (or arg
1))))
948 (defun ruby-end-of-block (&optional arg
)
949 "Move forward to the end of the current block.
950 With ARG, move out of multiple blocks."
952 (ruby-move-to-block (or arg
1)))
954 (defun ruby-forward-sexp (&optional arg
)
955 "Move forward across one balanced expression (sexp).
956 With ARG, do it many times. Negative ARG means move backward."
957 ;; TODO: Document body
959 (if (and (numberp arg
) (< arg
0))
960 (ruby-backward-sexp (- arg
))
961 (let ((i (or arg
1)))
964 (skip-syntax-forward " ")
965 (if (looking-at ",\\s *") (goto-char (match-end 0)))
966 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
967 (goto-char (match-end 0)))
969 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
971 (goto-char (scan-sexps (point) 1)))
972 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
973 (not (eq (char-before (point)) ?.
))
974 (not (eq (char-before (point)) ?
:)))
977 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
979 (while (progn (forward-word 1) (looking-at "_")))
980 (cond ((looking-at "::") (forward-char 2) t
)
981 ((> (skip-chars-forward ".") 0))
982 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
983 (forward-char 1) nil
)))))
987 (setq expr
(or expr
(ruby-expr-beg)
988 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
989 (nth 1 (setq state
(apply 'ruby-parse-partial nil state
))))
991 (skip-chars-forward "<"))
994 ((error) (forward-word 1)))
997 (defun ruby-backward-sexp (&optional arg
)
998 "Move backward across one balanced expression (sexp).
999 With ARG, do it many times. Negative ARG means move forward."
1000 ;; TODO: Document body
1002 (if (and (numberp arg
) (< arg
0))
1003 (ruby-forward-sexp (- arg
))
1004 (let ((i (or arg
1)))
1007 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
1009 (cond ((looking-at "\\s)")
1010 (goto-char (scan-sexps (1+ (point)) -
1))
1012 (?%
(forward-char -
1))
1013 ((?q ?Q ?w ?W ?r ?x
)
1014 (if (eq (char-before (1- (point))) ?%
) (forward-char -
2))))
1016 ((looking-at "\\s\"\\|\\\\\\S_")
1017 (let ((c (char-to-string (char-before (match-end 0)))))
1018 (while (and (search-backward c
)
1019 (eq (logand (skip-chars-backward "\\") 1)
1022 ((looking-at "\\s.\\|\\s\\")
1023 (if (ruby-special-char-p) (forward-char -
1)))
1024 ((looking-at "\\s(") nil
)
1027 (while (progn (forward-word -
1)
1030 (?.
(forward-char -
1) t
)
1033 (and (eq (char-before) (char-after)) (forward-char -
1)))
1036 (eq (char-before) :)))))
1037 (if (looking-at ruby-block-end-re
)
1038 (ruby-beginning-of-block))
1044 (defun ruby-indent-exp (&optional ignored
)
1045 "Indent each line in the balanced expression following the point."
1047 (let ((here (point-marker)) start top column
(nest t
))
1048 (set-marker-insertion-type here t
)
1052 (setq start
(point) top
(current-indentation))
1053 (while (and (not (eobp))
1055 (setq column
(ruby-calculate-indent start
))
1056 (cond ((> column top
)
1058 ((and (= column top
) nest
)
1059 (setq nest nil
) t
))))
1060 (ruby-indent-to column
)
1061 (beginning-of-line 2)))
1063 (set-marker here nil
))))
1065 (defun ruby-add-log-current-method ()
1066 "Return the current method name as a string.
1067 This string includes all namespaces.
1076 See `add-log-current-defun-function'."
1079 (let* ((indent 0) mname mlist
1083 (concat "^[ \t]*" re
"[ \t]+"
1085 ;; \\. and :: for class methods
1086 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1088 (definition-re (funcall make-definition-re ruby-defun-beg-re
))
1089 (module-re (funcall make-definition-re
"\\(class\\|module\\)")))
1090 ;; Get the current method definition (or class/module).
1091 (when (re-search-backward definition-re nil t
)
1092 (goto-char (match-beginning 1))
1093 (if (not (string-equal "def" (match-string 1)))
1094 (setq mlist
(list (match-string 2)))
1095 ;; We're inside the method. For classes and modules,
1096 ;; this check is skipped for performance.
1097 (when (ruby-block-contains-point start
)
1098 (setq mname
(match-string 2))))
1099 (setq indent
(current-column))
1100 (beginning-of-line))
1101 ;; Walk up the class/module nesting.
1102 (while (and (> indent
0)
1103 (re-search-backward module-re nil t
))
1104 (goto-char (match-beginning 1))
1105 (when (< (current-column) indent
)
1106 (setq mlist
(cons (match-string 2) mlist
))
1107 (setq indent
(current-column))
1108 (beginning-of-line)))
1109 ;; Process the method name.
1111 (let ((mn (split-string mname
"\\.\\|::")))
1114 (unless (string-equal "self" (car mn
)) ; def self.foo
1116 (let ((ml (nreverse mlist
)))
1117 ;; If the method name references one of the
1118 ;; containing modules, drop the more nested ones.
1120 (if (string-equal (car ml
) (car mn
))
1121 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1122 (or (setq ml
(cdr ml
)) (nreverse mlist
))))
1124 (setcdr (last mlist
) (butlast mn
))
1125 (setq mlist
(butlast mn
))))
1126 (setq mname
(concat "." (car (last mn
)))))
1127 ;; See if the method is in singleton class context.
1128 (let ((in-singleton-class
1129 (when (re-search-forward ruby-singleton-class-re start t
)
1130 (goto-char (match-beginning 0))
1131 ;; FIXME: Optimize it out, too?
1132 ;; This can be slow in a large file, but
1133 ;; unlike class/module declaration
1134 ;; indentations, method definitions can be
1135 ;; intermixed with these, and may or may not
1136 ;; be additionally indented after visibility
1138 (ruby-block-contains-point start
))))
1140 (if in-singleton-class
"." "#")
1142 ;; Generate the string.
1144 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1146 (if mlist
(concat mlist mname
) mname
)
1149 (defun ruby-block-contains-point (pt)
1155 (defun ruby-brace-to-do-end (orig end
)
1156 (let (beg-marker end-marker
)
1158 (when (eq (char-before) ?\
})
1160 (when (save-excursion
1161 (skip-chars-backward " \t")
1165 (setq end-marker
(point-marker))
1166 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w
))
1170 (when (eq (char-syntax (char-before)) ?w
)
1173 (setq beg-marker
(point-marker))
1174 (when (looking-at "\\(\\s \\)*|")
1175 (unless (match-beginning 1)
1177 (goto-char (1+ (match-end 0)))
1178 (search-forward "|"))
1179 (unless (looking-at "\\s *$")
1181 (indent-region beg-marker end-marker
)
1182 (goto-char beg-marker
)
1185 (defun ruby-do-end-to-brace (orig end
)
1186 (let (beg-marker end-marker beg-pos end-pos
)
1187 (goto-char (- end
3))
1188 (when (looking-at ruby-block-end-re
)
1190 (setq end-marker
(point-marker))
1195 (setq beg-marker
(point-marker))
1196 (when (looking-at "\\s +|")
1197 (delete-char (- (match-end 0) (match-beginning 0) 1))
1199 (re-search-forward "|" (line-end-position) t
))
1201 (skip-chars-forward " \t\n\r")
1202 (setq beg-pos
(point))
1203 (goto-char end-marker
)
1204 (skip-chars-backward " \t\n\r")
1205 (setq end-pos
(point)))
1208 (and (= (line-number-at-pos beg-pos
) (line-number-at-pos end-pos
))
1209 (< (+ (current-column) (- end-pos beg-pos
) 2) fill-column
)))
1211 (goto-char end-marker
)
1212 (just-one-space -
1))
1213 (goto-char beg-marker
)
1216 (defun ruby-toggle-block ()
1217 "Toggle block type from do-end to braces or back.
1218 The block must begin on the current line or above it and end after the point.
1219 If the result is do-end block, it will always be multiline."
1221 (let ((start (point)) beg end
)
1224 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1227 (save-match-data (ruby-forward-sexp))
1230 (if (match-beginning 1)
1231 (ruby-brace-to-do-end beg end
)
1232 (ruby-do-end-to-brace beg end
)))
1233 (goto-char start
))))
1235 (declare-function ruby-syntax-propertize-heredoc
"ruby-mode" (limit))
1236 (declare-function ruby-syntax-enclosing-percent-literal
"ruby-mode" (limit))
1237 (declare-function ruby-syntax-propertize-percent-literal
"ruby-mode" (limit))
1239 (if (eval-when-compile (fboundp #'syntax-propertize-rules
))
1240 ;; New code that works independently from font-lock.
1243 (defconst ruby-percent-literal-beg-re
1244 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
1245 "Regexp to match the beginning of percent literal.")
1247 (defconst ruby-syntax-methods-before-regexp
1248 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1249 "assert_match" "Given" "Then" "When")
1250 "Methods that can take regexp as the first argument.
1251 It will be properly highlighted even when the call omits parens."))
1253 (defun ruby-syntax-propertize-function (start end
)
1254 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1256 (ruby-syntax-propertize-heredoc end
)
1257 (ruby-syntax-enclosing-percent-literal end
)
1259 (syntax-propertize-rules
1260 ;; $' $" $` .... are variables.
1261 ;; ?' ?" ?` are ascii codes.
1262 ("\\([?$]\\)[#\"'`]"
1263 (1 (unless (save-excursion
1264 ;; Not within a string.
1265 (nth 3 (syntax-ppss (match-beginning 0))))
1266 (string-to-syntax "\\"))))
1267 ;; Regexps: regexps are distinguished from division because
1268 ;; of the keyword, symbol, or method name before them.
1270 ;; Special tokens that can't be followed by a division operator.
1271 "\\(^\\|[[=(,~?:;<>]"
1272 ;; Control flow keywords and operators following bol or whitespace.
1273 "\\|\\(?:^\\|\\s \\)"
1274 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1275 "or" "not" "&&" "||"))
1276 ;; Method name from the list.
1278 (regexp-opt ruby-syntax-methods-before-regexp
)
1280 ;; The regular expression itself.
1281 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1282 (3 (unless (nth 3 (syntax-ppss (match-beginning 2)))
1283 (put-text-property (match-beginning 2) (match-end 2)
1284 'syntax-table
(string-to-syntax "\"/"))
1285 (string-to-syntax "\"/"))))
1286 ("^=en\\(d\\)\\_>" (1 "!"))
1287 ("^\\(=\\)begin\\_>" (1 "!"))
1288 ;; Handle here documents.
1289 ((concat ruby-here-doc-beg-re
".*\\(\n\\)")
1290 (7 (unless (ruby-singleton-class-p (match-beginning 0))
1291 (put-text-property (match-beginning 7) (match-end 7)
1292 'syntax-table
(string-to-syntax "\""))
1293 (ruby-syntax-propertize-heredoc end
))))
1294 ;; Handle percent literals: %w(), %q{}, etc.
1295 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re
)
1296 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end
)))))
1298 (ruby-syntax-propertize-expansions start end
))
1300 (defun ruby-syntax-propertize-heredoc (limit)
1301 (let ((ppss (syntax-ppss))
1303 (when (eq ?
\n (nth 3 ppss
))
1305 (goto-char (nth 8 ppss
))
1307 (while (re-search-forward ruby-here-doc-beg-re
1308 (line-end-position) t
)
1309 (unless (ruby-singleton-class-p (match-beginning 0))
1310 (push (concat (ruby-here-doc-end-match) "\n") res
))))
1311 (let ((start (point)))
1312 ;; With multiple openers on the same line, we don't know in which
1313 ;; part `start' is, so we have to go back to the beginning.
1315 (goto-char (nth 8 ppss
))
1316 (setq res
(nreverse res
)))
1317 (while (and res
(re-search-forward (pop res
) limit
'move
))
1319 (put-text-property (1- (point)) (point)
1320 'syntax-table
(string-to-syntax "\""))))
1321 ;; Make extra sure we don't move back, lest we could fall into an
1323 (if (< (point) start
) (goto-char start
))))))
1325 (defun ruby-syntax-enclosing-percent-literal (limit)
1326 (let ((state (syntax-ppss))
1328 ;; When already inside percent literal, re-propertize it.
1329 (when (eq t
(nth 3 state
))
1330 (goto-char (nth 8 state
))
1331 (when (looking-at ruby-percent-literal-beg-re
)
1332 (ruby-syntax-propertize-percent-literal limit
))
1333 (when (< (point) start
) (goto-char start
)))))
1335 (defun ruby-syntax-propertize-percent-literal (limit)
1336 (goto-char (match-beginning 2))
1337 ;; Not inside a simple string or comment.
1338 (when (eq t
(nth 3 (syntax-ppss)))
1339 (let* ((op (char-after))
1340 (ops (char-to-string op
))
1341 (cl (or (cdr (aref (syntax-table) op
))
1342 (cdr (assoc op
'((?
< . ?
>))))))
1343 parse-sexp-lookup-properties
)
1346 (if cl
; Paired delimiters.
1347 ;; Delimiter pairs of the same kind can be nested
1348 ;; inside the literal, as long as they are balanced.
1349 ;; Create syntax table that ignores other characters.
1350 (with-syntax-table (make-char-table 'syntax-table nil
)
1351 (modify-syntax-entry op
(concat "(" (char-to-string cl
)))
1352 (modify-syntax-entry cl
(concat ")" ops
))
1353 (modify-syntax-entry ?
\\ "\\")
1355 (narrow-to-region (point) limit
)
1356 (forward-list))) ; skip to the paired character
1357 ;; Single character delimiter.
1358 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1359 (regexp-quote ops
)) limit nil
))
1360 ;; Found the closing delimiter.
1361 (put-text-property (1- (point)) (point) 'syntax-table
1362 (string-to-syntax "|")))
1363 ;; Unclosed literal, leave the following text unpropertized.
1364 ((scan-error search-failed
) (goto-char limit
))))))
1366 (defun ruby-syntax-propertize-expansions (start end
)
1367 (remove-text-properties start end
'(ruby-expansion-match-data))
1369 ;; Find all expression expansions and
1370 ;; - save the match data to a text property, for font-locking later,
1371 ;; - set the syntax of all double quotes and backticks to punctuation.
1372 (while (re-search-forward ruby-expression-expansion-re end
'move
)
1373 (let ((beg (match-beginning 2))
1374 (end (match-end 2)))
1375 (when (and beg
(save-excursion (nth 3 (syntax-ppss beg
))))
1376 (put-text-property beg
(1+ beg
) 'ruby-expansion-match-data
1379 (while (re-search-forward "[\"`]" end
'move
)
1380 (put-text-property (match-beginning 0) (match-end 0)
1381 'syntax-table
(string-to-syntax ".")))))))
1384 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1385 ;; fallback on the old font-lock-syntactic-keywords stuff.
1387 (defconst ruby-here-doc-end-re
1388 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1389 "Regexp to match the end of heredocs.
1391 This will actually match any line with one or more characters.
1392 It's useful in that it divides up the match string so that
1393 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1395 (defun ruby-here-doc-beg-match ()
1396 "Return a regexp to find the beginning of a heredoc.
1398 This should only be called after matching against `ruby-here-doc-end-re'."
1399 (let ((contents (concat
1400 (regexp-quote (concat (match-string 2) (match-string 3)))
1401 (if (string= (match-string 3) "_") "\\B" "\\b"))))
1403 (let ((match (match-string 1)))
1404 (if (and match
(> (length match
) 0))
1405 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1406 (match-string 1) "\\)"
1407 contents
"\\(\\1\\|\\2\\)")
1408 (concat "-?\\([\"']\\|\\)" contents
"\\1"))))))
1410 (defconst ruby-font-lock-syntactic-keywords
1412 ;; the last $', $", $` in the respective string is not variable
1413 ;; the last ?', ?", ?` in the respective string is not ascii code
1414 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1417 ;; $' $" $` .... are variables
1418 ;; ?' ?" ?` are ascii codes
1419 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil
))
1421 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1424 ("^=en\\(d\\)\\_>" 1 "!")
1426 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1429 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1430 ;; Currently, the following case is highlighted incorrectly:
1439 ;; This is because all here-doc beginnings are highlighted before any endings,
1440 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1441 ;; it thinks <<BAR is part of a string so it's marked as well.
1443 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1444 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1445 ;; but I don't want to try that until we've got unit tests set up
1446 ;; to make sure I don't break anything else.
1447 (,(concat ruby-here-doc-beg-re
".*\\(\n\\)")
1448 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re
))
1449 (ruby-here-doc-beg-syntax))
1450 (,ruby-here-doc-end-re
3 (ruby-here-doc-end-syntax)))
1451 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1453 (defun ruby-comment-beg-syntax ()
1454 "Return the syntax cell for a the first character of a =begin.
1455 See the definition of `ruby-font-lock-syntactic-keywords'.
1457 This returns a comment-delimiter cell as long as the =begin
1458 isn't in a string or another comment."
1459 (when (not (nth 3 (syntax-ppss)))
1460 (string-to-syntax "!")))
1462 (defun ruby-in-here-doc-p ()
1463 "Return whether or not the point is in a heredoc."
1465 (let ((old-point (point)) (case-fold-search nil
))
1468 (while (and (re-search-backward ruby-here-doc-beg-re nil t
)
1469 (not (ruby-singleton-class-p)))
1470 (if (not (or (ruby-in-ppss-context-p 'anything
)
1471 (ruby-here-doc-find-end old-point
)))
1472 (throw 'found-beg t
)))))))
1474 (defun ruby-here-doc-find-end (&optional limit
)
1475 "Expects the point to be on a line with one or more heredoc openers.
1476 Returns the buffer position at which all heredocs on the line
1477 are terminated, or nil if they aren't terminated before the
1478 buffer position `limit' or the end of the buffer."
1482 (let ((eol (point-at-eol))
1483 (case-fold-search nil
)
1484 ;; Fake match data such that (match-end 0) is at eol
1485 (end-match-data (progn (looking-at ".*$") (match-data)))
1486 beg-match-data end-re
)
1487 (while (re-search-forward ruby-here-doc-beg-re eol t
)
1488 (setq beg-match-data
(match-data))
1489 (setq end-re
(ruby-here-doc-end-match))
1491 (set-match-data end-match-data
)
1492 (goto-char (match-end 0))
1493 (unless (re-search-forward end-re limit t
) (throw 'done nil
))
1494 (setq end-match-data
(match-data))
1496 (set-match-data beg-match-data
)
1497 (goto-char (match-end 0)))
1498 (set-match-data end-match-data
)
1499 (goto-char (match-end 0))
1502 (defun ruby-here-doc-beg-syntax ()
1503 "Return the syntax cell for a line that may begin a heredoc.
1504 See the definition of `ruby-font-lock-syntactic-keywords'.
1506 This sets the syntax cell for the newline ending the line
1507 containing the heredoc beginning so that cases where multiple
1508 heredocs are started on one line are handled correctly."
1510 (goto-char (match-beginning 0))
1511 (unless (or (ruby-in-ppss-context-p 'non-heredoc
)
1512 (ruby-in-here-doc-p))
1513 (string-to-syntax "\""))))
1515 (defun ruby-here-doc-end-syntax ()
1516 "Return the syntax cell for a line that may end a heredoc.
1517 See the definition of `ruby-font-lock-syntactic-keywords'."
1518 (let ((pss (syntax-ppss)) (case-fold-search nil
))
1519 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1520 ;; so we can just give up.
1521 ;; This means we aren't doing a full-document search
1522 ;; every time we enter a character.
1523 (when (ruby-in-ppss-context-p 'heredoc pss
)
1525 (goto-char (nth 8 pss
)) ; Go to the beginning of heredoc.
1526 (let ((eol (point)))
1528 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t
) ; If there is a heredoc that matches this line...
1529 (not (ruby-in-ppss-context-p 'anything
)) ; And that's not inside a heredoc/string/comment...
1530 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1531 (not (re-search-forward ruby-here-doc-beg-re eol t
))))
1532 (string-to-syntax "\"")))))))
1534 (unless (functionp 'syntax-ppss
)
1535 (defun syntax-ppss (&optional pos
)
1536 (parse-partial-sexp (point-min) (or pos
(point)))))
1539 (defun ruby-in-ppss-context-p (context &optional ppss
)
1540 (let ((ppss (or ppss
(syntax-ppss (point)))))
1542 ((eq context
'anything
)
1545 ((eq context
'string
)
1547 ((eq context
'heredoc
)
1548 (eq ?
\n (nth 3 ppss
)))
1549 ((eq context
'non-heredoc
)
1550 (and (ruby-in-ppss-context-p 'anything
)
1551 (not (ruby-in-ppss-context-p 'heredoc
))))
1552 ((eq context
'comment
)
1556 "Internal error on `ruby-in-ppss-context-p': "
1557 "context name `" (symbol-name context
) "' is unknown"))))
1560 (if (featurep 'xemacs
)
1561 (put 'ruby-mode
'font-lock-defaults
1562 '((ruby-font-lock-keywords)
1565 (font-lock-syntactic-keywords
1566 . ruby-font-lock-syntactic-keywords
))))
1568 (defvar ruby-font-lock-syntax-table
1569 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1570 (modify-syntax-entry ?_
"w" tbl
)
1572 "The syntax table to use for fontifying Ruby mode buffers.
1573 See `font-lock-syntax-table'.")
1575 (defconst ruby-font-lock-keywords
1578 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1579 1 font-lock-function-name-face
)
1582 "\\(^\\|[^.@$]\\|\\.\\.\\)\\_<\\(defined\\?\\|"
1626 ruby-keyword-end-re
)
1628 ;; here-doc beginnings
1629 `(,ruby-here-doc-beg-re
0 (unless (ruby-singleton-class-p (match-beginning 0))
1630 'font-lock-string-face
))
1632 '("\\(^\\|[^.@$]\\|\\.\\.\\)\\_<\\(nil\\|self\\|true\\|false\\)\\>"
1633 2 font-lock-variable-name-face
)
1635 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1636 2 font-lock-constant-face
)
1638 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1639 1 font-lock-variable-name-face
)
1640 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1641 0 font-lock-variable-name-face
)
1643 '("\\(?:\\_<\\|::\\)\\([A-Z]+\\(\\w\\|_\\)*\\)"
1644 1 font-lock-type-face
)
1645 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face
)
1646 ;; expression expansion
1647 '(ruby-match-expression-expansion
1648 2 font-lock-variable-name-face t
)
1649 ;; warn lower camel case
1650 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1651 ; 0 font-lock-warning-face)
1653 "Additional expressions to highlight in Ruby mode.")
1655 (defun ruby-match-expression-expansion (limit)
1656 (let* ((prop 'ruby-expansion-match-data
)
1657 (pos (next-single-char-property-change (point) prop nil limit
))
1659 (when (and pos
(> pos
(point)))
1661 (or (and (setq value
(get-text-property pos prop
))
1662 (progn (set-match-data value
) t
))
1663 (ruby-match-expression-expansion limit
)))))
1666 (define-derived-mode ruby-mode prog-mode
"Ruby"
1667 "Major mode for editing Ruby scripts.
1668 \\[ruby-indent-line] properly indents subexpressions of multi-line
1669 class, module, def, if, while, for, do, and case statements, taking
1670 nesting into account.
1672 The variable `ruby-indent-level' controls the amount of indentation.
1675 (ruby-mode-variables)
1677 (set (make-local-variable 'imenu-create-index-function
)
1678 'ruby-imenu-create-index
)
1679 (set (make-local-variable 'add-log-current-defun-function
)
1680 'ruby-add-log-current-method
)
1681 (set (make-local-variable 'beginning-of-defun-function
)
1682 'ruby-beginning-of-defun
)
1683 (set (make-local-variable 'end-of-defun-function
)
1687 (cond ((boundp 'before-save-hook
) 'before-save-hook
)
1688 ((boundp 'write-contents-functions
) 'write-contents-functions
)
1689 ((boundp 'write-contents-hooks
) 'write-contents-hooks
))
1690 'ruby-mode-set-encoding nil
'local
)
1692 (set (make-local-variable 'electric-indent-chars
)
1693 (append '(?\
{ ?\
}) electric-indent-chars
))
1695 (set (make-local-variable 'font-lock-defaults
)
1696 '((ruby-font-lock-keywords) nil nil
))
1697 (set (make-local-variable 'font-lock-keywords
)
1698 ruby-font-lock-keywords
)
1699 (set (make-local-variable 'font-lock-syntax-table
)
1700 ruby-font-lock-syntax-table
)
1702 (if (eval-when-compile (fboundp 'syntax-propertize-rules
))
1703 (set (make-local-variable 'syntax-propertize-function
)
1704 #'ruby-syntax-propertize-function
)
1705 (set (make-local-variable 'font-lock-syntactic-keywords
)
1706 ruby-font-lock-syntactic-keywords
)))
1708 ;;; Invoke ruby-mode when appropriate
1711 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.rb\\'") 'ruby-mode
))
1713 (add-to-list 'auto-mode-alist
(cons (purecopy "Rakefile\\'") 'ruby-mode
))
1715 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.gemspec\\'") 'ruby-mode
))
1718 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
1719 (add-to-list 'interpreter-mode-alist
(cons (purecopy name
) 'ruby-mode
)))
1721 (provide 'ruby-mode
)
1723 ;;; ruby-mode.el ends here