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
)
389 (store-match-data nil
)
390 (let ((space (skip-chars-backward " \t"))
396 (and (looking-at "\\?")
397 (or (eq (char-syntax (char-before (point))) ?w
)
398 (ruby-special-char-p))))
400 ((and (eq option
'heredoc
) (< space
0))
401 (not (progn (goto-char start
) (ruby-singleton-class-p))))
402 ((or (looking-at ruby-operator-re
)
403 (looking-at "[\\[({,;]")
404 (and (looking-at "[!?]")
405 (or (not (eq option
'modifier
))
407 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
408 (and (looking-at ruby-symbol-re
)
409 (skip-chars-backward ruby-symbol-chars
)
411 ((looking-at (regexp-opt
412 (append ruby-block-beg-keywords
413 ruby-block-op-keywords
414 ruby-block-mid-keywords
)
416 (goto-char (match-end 0))
417 (not (looking-at "\\s_\\|!")))
418 ((eq option
'expr-qstr
)
419 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
420 ((eq option
'expr-re
)
421 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
424 (defun ruby-forward-string (term &optional end no-error expand
)
426 (let ((n 1) (c (string-to-char term
))
428 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term
"]\\|\\(#{\\)\\)")
429 (concat "[^\\]\\(\\\\\\\\\\)*[" term
"]"))))
430 (while (and (re-search-forward re end no-error
)
431 (if (match-beginning 3)
432 (ruby-forward-string "}{" end no-error nil
)
433 (> (setq n
(if (eq (char-before (point)) c
)
438 ((error "unterminated string")))))
440 (defun ruby-deep-indent-paren-p (c)
442 (cond ((listp ruby-deep-indent-paren
)
443 (let ((deep (assoc c ruby-deep-indent-paren
)))
445 (or (cdr deep
) ruby-deep-indent-paren-style
))
446 ((memq c ruby-deep-indent-paren
)
447 ruby-deep-indent-paren-style
))))
448 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
449 ((eq c ?\
( ) ruby-deep-arglist
)))
451 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
452 "TODO: document throughout function body."
453 (or depth
(setq depth
0))
454 (or indent
(setq indent
0))
455 (when (re-search-forward ruby-delimiter end
'move
)
456 (let ((pnt (point)) w re expand
)
457 (goto-char (match-beginning 0))
459 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
461 ((looking-at "[\"`]") ;skip string
464 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t
))
467 (setq in-string
(point))
472 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
475 (setq in-string
(point))
481 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
482 (if (ruby-forward-string "/" end t t
)
484 (setq in-string
(point))
491 (ruby-expr-beg 'expr-qstr
)
492 (not (looking-at "%="))
493 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
494 (goto-char (match-beginning 1))
495 (setq expand
(not (memq (char-before) '(?q ?w
))))
496 (setq w
(match-string 1))
498 ((string= w
"[") (setq re
"]["))
499 ((string= w
"{") (setq re
"}{"))
500 ((string= w
"(") (setq re
")("))
501 ((string= w
"<") (setq re
"><"))
502 ((and expand
(string= w
"\\"))
503 (setq w
(concat "\\" w
))))
504 (unless (cond (re (ruby-forward-string re end t expand
))
505 (expand (ruby-forward-string w end t t
))
506 (t (re-search-forward
509 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
511 (setq in-string
(point))
515 ((looking-at "\\?") ;skip ?char
517 ((and (ruby-expr-beg)
518 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
519 (goto-char (match-end 0)))
522 ((looking-at "\\$") ;skip $char
525 ((looking-at "#") ;skip comment
529 ((looking-at "[\\[{(]")
530 (let ((deep (ruby-deep-indent-paren-p (char-after))))
531 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
533 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
534 (setq pnt
(1- (match-end 0))))
535 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
536 (setq pcol
(cons (cons pnt depth
) pcol
))
538 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
539 (setq depth
(1+ depth
))))
542 ((looking-at "[])}]")
543 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
544 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
545 (setq depth
(1- depth
)))
546 (setq nest
(cdr nest
))
548 ((looking-at ruby-block-end-re
)
549 (if (or (and (not (bolp))
552 (setq w
(char-after (point)))
557 (setq w
(char-after (point)))
562 (setq nest
(cdr nest
))
563 (setq depth
(1- depth
)))
565 ((looking-at "def\\s +[^(\n;]*")
569 (not (eq ?_
(char-after (point))))))
571 (setq nest
(cons (cons nil pnt
) nest
))
572 (setq depth
(1+ depth
))))
573 (goto-char (match-end 0)))
574 ((looking-at (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
577 (or (not (looking-at (concat "do" ruby-keyword-end-re
)))
579 (back-to-indentation)
580 (not (looking-at ruby-non-block-do-re
)))))
584 (setq w
(char-after (point)))
588 (setq w
(char-after (point)))
590 (skip-chars-forward " \t")
591 (goto-char (match-beginning 0))
592 (or (not (looking-at ruby-modifier-re
))
593 (ruby-expr-beg 'modifier
))
595 (setq nest
(cons (cons nil pnt
) nest
))
596 (setq depth
(1+ depth
)))
598 ((looking-at ":\\(['\"]\\)")
599 (goto-char (match-beginning 1))
600 (ruby-forward-string (match-string 1) end t
))
601 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
602 (goto-char (match-end 0)))
603 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
604 (goto-char (match-end 0)))
605 ((or (looking-at "\\.\\.\\.?")
606 (looking-at "\\.[0-9]+")
607 (looking-at "\\.[a-zA-Z_0-9]+")
609 (goto-char (match-end 0)))
610 ((looking-at "^=begin")
611 (if (re-search-forward "^=end" end t
)
613 (setq in-string
(match-end 0))
617 ((and (ruby-expr-beg 'heredoc
)
618 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
619 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
620 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
621 (let* ((id-end (goto-char (match-end 0)))
622 (line-end-position (point-at-eol))
623 (state (list in-string nest depth pcol indent
)))
624 ;; parse the rest of the line
625 (while (and (> line-end-position
(point))
626 (setq state
(apply 'ruby-parse-partial
627 line-end-position state
))))
628 (setq in-string
(car state
)
632 indent
(nth 4 state
))
633 ;; skip heredoc section
634 (if (re-search-forward (concat "^" re
"$") end
'move
)
636 (setq in-string id-end
)
640 ((looking-at "^__END__$")
642 ((and (looking-at ruby-here-doc-beg-re
)
643 (boundp 'ruby-indent-point
))
644 (if (re-search-forward (ruby-here-doc-end-match)
647 (setq in-string
(match-end 0))
648 (goto-char ruby-indent-point
)))
650 (error (format "bad string %s"
651 (buffer-substring (point) pnt
)
653 (list in-string nest depth pcol
))
655 (defun ruby-parse-region (start end
)
661 (ruby-beginning-of-indent))
663 (narrow-to-region (point) end
)
664 (while (and (> end
(point))
665 (setq state
(apply 'ruby-parse-partial end state
))))))
666 (list (nth 0 state
) ; in-string
667 (car (nth 1 state
)) ; nest
668 (nth 2 state
) ; depth
669 (car (car (nth 3 state
))) ; pcol
670 ;(car (nth 5 state)) ; indent
673 (defun ruby-indent-size (pos nest
)
674 "Return the indentation level in spaces NEST levels deeper than POS."
675 (+ pos
(* (or nest
1) ruby-indent-level
)))
677 (defun ruby-calculate-indent (&optional parse-start
)
678 "Return the proper indentation level of the current line."
679 ;; TODO: Document body
682 (let ((ruby-indent-point (point))
683 (case-fold-search nil
)
684 state eol begin op-end
685 (paren (progn (skip-syntax-forward " ")
686 (and (char-after) (matching-paren (char-after)))))
689 (goto-char parse-start
)
690 (ruby-beginning-of-indent)
691 (setq parse-start
(point)))
692 (back-to-indentation)
693 (setq indent
(current-column))
694 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
696 ((nth 0 state
) ; within string
697 (setq indent nil
)) ; do nothing
698 ((car (nth 1 state
)) ; in paren
699 (goto-char (setq begin
(cdr (nth 1 state
))))
700 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
702 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
703 (skip-syntax-backward " ")
704 (setq indent
(1- (current-column))))
705 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
706 (and (nth 2 s
) (> (nth 2 s
) 0)
707 (or (goto-char (cdr (nth 1 s
))) t
)))
709 (setq indent
(ruby-indent-size (current-column)
712 (setq indent
(current-column))
713 (cond ((eq deep
'space
))
714 (paren (setq indent
(1- indent
)))
715 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
716 (if (nth 3 state
) (goto-char (nth 3 state
))
717 (goto-char parse-start
) (back-to-indentation))
718 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
719 (and (eq (car (nth 1 state
)) paren
)
720 (ruby-deep-indent-paren-p (matching-paren paren
))
721 (search-backward (char-to-string paren
))
722 (setq indent
(current-column)))))
723 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
724 (if (null (cdr (nth 1 state
)))
725 (error "invalid nest"))
726 (goto-char (cdr (nth 1 state
)))
727 (forward-word -
1) ; skip back a keyword
730 ((looking-at "do\\>[^_]") ; iter block is a special case
731 (if (nth 3 state
) (goto-char (nth 3 state
))
732 (goto-char parse-start
) (back-to-indentation))
733 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
735 (setq indent
(+ (current-column) ruby-indent-level
)))))
737 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
738 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
740 (goto-char ruby-indent-point
)
745 ((and (not (ruby-deep-indent-paren-p paren
))
746 (re-search-forward ruby-negative eol t
))
747 (and (not (eq ?_
(char-after (match-end 0))))
748 (setq indent
(- indent ruby-indent-level
))))
753 (or (ruby-deep-indent-paren-p t
)
754 (null (car (nth 1 state
)))))
755 ;; goto beginning of non-empty no-comment line
758 (skip-chars-backward " \t\n")
761 (if (re-search-forward "^\\s *#" end t
)
765 ;; skip the comment at the end
766 (skip-chars-backward " \t")
767 (let (end (pos (point)))
769 (while (and (re-search-forward "#" pos t
)
770 (setq end
(1- (point)))
771 (or (ruby-special-char-p end
)
772 (and (setq state
(ruby-parse-region parse-start end
))
775 (goto-char (or end pos
))
776 (skip-chars-backward " \t")
777 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
778 (setq state
(ruby-parse-region parse-start
(point))))
779 (or (bobp) (forward-char -
1))
781 (or (and (looking-at ruby-symbol-re
)
782 (skip-chars-backward ruby-symbol-chars
)
783 (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>"))
784 (not (eq (point) (nth 3 state
)))
786 (goto-char (match-end 0))
787 (not (looking-at "[a-z_]"))))
788 (and (looking-at ruby-operator-re
)
789 (not (ruby-special-char-p))
790 ;; Operator at the end of line.
791 (let ((c (char-after (point))))
796 ;; (skip-chars-forward " \t")
797 ;; (not (or (eolp) (looking-at "#")
798 ;; (and (eq (car (nth 1 state)) ?{)
799 ;; (looking-at "|"))))))
800 ;; Not a regexp or percent literal.
801 (null (nth 0 (ruby-parse-region (or begin parse-start
)
803 (or (not (eq ?|
(char-after (point))))
805 (or (eolp) (forward-char -
1))
807 ((search-backward "|" nil t
)
808 (skip-chars-backward " \t\n")
812 (not (looking-at "{")))
815 (not (looking-at "do\\>[^_]")))))
823 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>")))
824 (eq (ruby-deep-indent-paren-p t
) 'space
)
827 (goto-char (or begin parse-start
))
828 (skip-syntax-forward " ")
830 ((car (nth 1 state
)) indent
)
832 (+ indent ruby-indent-level
))))))))
833 (goto-char ruby-indent-point
)
835 (skip-syntax-forward " ")
836 (if (looking-at "\\.[^.]")
837 (+ indent ruby-indent-level
)
840 (defun ruby-beginning-of-defun (&optional arg
)
841 "Move backward to the beginning of the current top-level defun.
842 With ARG, move backward multiple defuns. Negative ARG means
845 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re
"\\_>")
847 (beginning-of-line)))
849 (defun ruby-end-of-defun (&optional arg
)
850 "Move forward to the end of the current top-level defun.
851 With ARG, move forward multiple defuns. Negative ARG means
855 (when (looking-back (concat "^\\s *" ruby-block-end-re
))
858 (defun ruby-beginning-of-indent ()
859 "Backtrack to a line which can be used as a reference for
860 calculating indentation on the lines after it."
861 (while (and (re-search-backward ruby-indent-beg-re nil
'move
)
862 (if (ruby-in-ppss-context-p 'anything
)
864 ;; We can stop, then.
865 (beginning-of-line)))))
867 (defun ruby-move-to-block (n)
868 "Move to the beginning (N < 0) or the end (N > 0) of the current block
869 or blocks containing the current block."
870 ;; TODO: Make this work for n > 1,
871 ;; make it not loop for n = 0,
874 (start (ruby-calculate-indent))
875 (down (looking-at (if (< n
0) ruby-block-end-re
876 (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))))
878 (while (and (not done
) (not (if (< n
0) (bobp) (eobp))))
881 ((looking-at "^\\s *$"))
882 ((looking-at "^\\s *#"))
883 ((and (> n
0) (looking-at "^=begin\\>"))
884 (re-search-forward "^=end\\>"))
885 ((and (< n
0) (looking-at "^=end\\>"))
886 (re-search-backward "^=begin\\>"))
888 (setq pos
(current-indentation))
892 ((and down
(= pos start
))
898 (back-to-indentation)
899 (if (looking-at (concat "\\<\\(" ruby-block-mid-re
"\\)\\>"))
901 (back-to-indentation)))
903 (defun ruby-beginning-of-block (&optional arg
)
904 "Move backward to the beginning of the current block.
905 With ARG, move up multiple blocks."
907 (ruby-move-to-block (- (or arg
1))))
909 (defun ruby-end-of-block (&optional arg
)
910 "Move forward to the end of the current block.
911 With ARG, move out of multiple blocks."
912 ;; Passing a value > 1 to ruby-move-to-block currently doesn't work.
914 (ruby-move-to-block (or arg
1)))
916 (defun ruby-forward-sexp (&optional arg
)
917 "Move forward across one balanced expression (sexp).
918 With ARG, do it many times. Negative ARG means move backward."
919 ;; TODO: Document body
921 (if (and (numberp arg
) (< arg
0))
922 (ruby-backward-sexp (- arg
))
923 (let ((i (or arg
1)))
926 (skip-syntax-forward " ")
927 (if (looking-at ",\\s *") (goto-char (match-end 0)))
928 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
929 (goto-char (match-end 0)))
931 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
933 (goto-char (scan-sexps (point) 1)))
934 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
935 (not (eq (char-before (point)) ?.
))
936 (not (eq (char-before (point)) ?
:)))
939 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
941 (while (progn (forward-word 1) (looking-at "_")))
942 (cond ((looking-at "::") (forward-char 2) t
)
943 ((> (skip-chars-forward ".") 0))
944 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
945 (forward-char 1) nil
)))))
949 (setq expr
(or expr
(ruby-expr-beg)
950 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
951 (nth 1 (setq state
(apply 'ruby-parse-partial nil state
))))
953 (skip-chars-forward "<"))
956 ((error) (forward-word 1)))
959 (defun ruby-backward-sexp (&optional arg
)
960 "Move backward across one balanced expression (sexp).
961 With ARG, do it many times. Negative ARG means move forward."
962 ;; TODO: Document body
964 (if (and (numberp arg
) (< arg
0))
965 (ruby-forward-sexp (- arg
))
966 (let ((i (or arg
1)))
969 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
971 (cond ((looking-at "\\s)")
972 (goto-char (scan-sexps (1+ (point)) -
1))
974 (?%
(forward-char -
1))
976 (if (eq (char-before (1- (point))) ?%
) (forward-char -
2))))
978 ((looking-at "\\s\"\\|\\\\\\S_")
979 (let ((c (char-to-string (char-before (match-end 0)))))
980 (while (and (search-backward c
)
981 (eq (logand (skip-chars-backward "\\") 1)
984 ((looking-at "\\s.\\|\\s\\")
985 (if (ruby-special-char-p) (forward-char -
1)))
986 ((looking-at "\\s(") nil
)
989 (while (progn (forward-word -
1)
992 (?.
(forward-char -
1) t
)
995 (and (eq (char-before) (char-after)) (forward-char -
1)))
998 (eq (char-before) :)))))
999 (if (looking-at ruby-block-end-re
)
1000 (ruby-beginning-of-block))
1006 (defun ruby-indent-exp (&optional ignored
)
1007 "Indent each line in the balanced expression following the point."
1009 (let ((here (point-marker)) start top column
(nest t
))
1010 (set-marker-insertion-type here t
)
1014 (setq start
(point) top
(current-indentation))
1015 (while (and (not (eobp))
1017 (setq column
(ruby-calculate-indent start
))
1018 (cond ((> column top
)
1020 ((and (= column top
) nest
)
1021 (setq nest nil
) t
))))
1022 (ruby-indent-to column
)
1023 (beginning-of-line 2)))
1025 (set-marker here nil
))))
1027 (defun ruby-add-log-current-method ()
1028 "Return the current method name as a string.
1029 This string includes all namespaces.
1038 See `add-log-current-defun-function'."
1041 (let (mname mlist
(indent 0))
1042 ;; Get the current method definition (or class/module).
1043 (if (re-search-backward
1044 (concat "^[ \t]*" ruby-defun-beg-re
"[ \t]+"
1046 ;; \\. and :: for class methods
1047 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1051 (setq mname
(match-string 2))
1052 (unless (string-equal "def" (match-string 1))
1053 (setq mlist
(list mname
) mname nil
))
1054 (goto-char (match-beginning 1))
1055 (setq indent
(current-column))
1056 (beginning-of-line)))
1057 ;; Walk up the class/module nesting.
1058 (while (and (> indent
0)
1061 "^[ \t]*\\(class\\|module\\)[ \t]+"
1062 "\\([A-Z]" ruby-symbol-re
"*\\)")
1064 (goto-char (match-beginning 1))
1065 (if (< (current-column) indent
)
1067 (setq mlist
(cons (match-string 2) mlist
))
1068 (setq indent
(current-column))
1069 (beginning-of-line))))
1070 ;; Process the method name.
1072 (let ((mn (split-string mname
"\\.\\|::")))
1075 (unless (string-equal "self" (car mn
)) ; def self.foo
1077 (let ((ml (nreverse mlist
)))
1078 ;; If the method name references one of the
1079 ;; containing modules, drop the more nested ones.
1081 (if (string-equal (car ml
) (car mn
))
1082 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1083 (or (setq ml
(cdr ml
)) (nreverse mlist
))))
1085 (setcdr (last mlist
) (butlast mn
))
1086 (setq mlist
(butlast mn
))))
1087 (setq mname
(concat "." (car (last mn
)))))
1088 (setq mname
(concat "#" mname
)))))
1089 ;; Generate the string.
1091 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1093 (if mlist
(concat mlist mname
) mname
)
1096 (defun ruby-brace-to-do-end (orig end
)
1097 (let (beg-marker end-marker
)
1099 (when (eq (char-before) ?\
})
1101 (when (save-excursion
1102 (skip-chars-backward " \t")
1106 (setq end-marker
(point-marker))
1107 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w
))
1111 (when (eq (char-syntax (char-before)) ?w
)
1114 (setq beg-marker
(point-marker))
1115 (when (looking-at "\\(\\s \\)*|")
1116 (unless (match-beginning 1)
1118 (goto-char (1+ (match-end 0)))
1119 (search-forward "|"))
1120 (unless (looking-at "\\s *$")
1122 (indent-region beg-marker end-marker
)
1123 (goto-char beg-marker
)
1126 (defun ruby-do-end-to-brace (orig end
)
1127 (let (beg-marker end-marker beg-pos end-pos
)
1128 (goto-char (- end
3))
1129 (when (looking-at ruby-block-end-re
)
1131 (setq end-marker
(point-marker))
1136 (setq beg-marker
(point-marker))
1137 (when (looking-at "\\s +|")
1138 (delete-char (- (match-end 0) (match-beginning 0) 1))
1140 (re-search-forward "|" (line-end-position) t
))
1142 (skip-chars-forward " \t\n\r")
1143 (setq beg-pos
(point))
1144 (goto-char end-marker
)
1145 (skip-chars-backward " \t\n\r")
1146 (setq end-pos
(point)))
1149 (and (= (line-number-at-pos beg-pos
) (line-number-at-pos end-pos
))
1150 (< (+ (current-column) (- end-pos beg-pos
) 2) fill-column
)))
1152 (goto-char end-marker
)
1153 (just-one-space -
1))
1154 (goto-char beg-marker
)
1157 (defun ruby-toggle-block ()
1158 "Toggle block type from do-end to braces or back.
1159 The block must begin on the current line or above it and end after the point.
1160 If the result is do-end block, it will always be multiline."
1162 (let ((start (point)) beg end
)
1165 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1168 (save-match-data (ruby-forward-sexp))
1171 (if (match-beginning 1)
1172 (ruby-brace-to-do-end beg end
)
1173 (ruby-do-end-to-brace beg end
)))
1174 (goto-char start
))))
1176 (declare-function ruby-syntax-propertize-heredoc
"ruby-mode" (limit))
1177 (declare-function ruby-syntax-enclosing-percent-literal
"ruby-mode" (limit))
1178 (declare-function ruby-syntax-propertize-percent-literal
"ruby-mode" (limit))
1180 (if (eval-when-compile (fboundp #'syntax-propertize-rules
))
1181 ;; New code that works independently from font-lock.
1184 (defconst ruby-percent-literal-beg-re
1185 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
1186 "Regexp to match the beginning of percent literal.")
1188 (defconst ruby-syntax-methods-before-regexp
1189 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1190 "assert_match" "Given" "Then" "When")
1191 "Methods that can take regexp as the first argument.
1192 It will be properly highlighted even when the call omits parens."))
1194 (defun ruby-syntax-propertize-function (start end
)
1195 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1197 (ruby-syntax-propertize-heredoc end
)
1198 (ruby-syntax-enclosing-percent-literal end
)
1200 (syntax-propertize-rules
1201 ;; $' $" $` .... are variables.
1202 ;; ?' ?" ?` are ascii codes.
1203 ("\\([?$]\\)[#\"'`]"
1204 (1 (unless (save-excursion
1205 ;; Not within a string.
1206 (nth 3 (syntax-ppss (match-beginning 0))))
1207 (string-to-syntax "\\"))))
1208 ;; Regexps: regexps are distinguished from division because
1209 ;; of the keyword, symbol, or method name before them.
1211 ;; Special tokens that can't be followed by a division operator.
1212 "\\(^\\|[[=(,~?:;<>]"
1213 ;; Control flow keywords and operators following bol or whitespace.
1214 "\\|\\(?:^\\|\\s \\)"
1215 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1216 "or" "not" "&&" "||"))
1217 ;; Method name from the list.
1219 (regexp-opt ruby-syntax-methods-before-regexp
)
1221 ;; The regular expression itself.
1222 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1223 (2 (string-to-syntax "\"/"))
1224 (3 (string-to-syntax "\"/")))
1225 ("^=en\\(d\\)\\_>" (1 "!"))
1226 ("^\\(=\\)begin\\_>" (1 "!"))
1227 ;; Handle here documents.
1228 ((concat ruby-here-doc-beg-re
".*\\(\n\\)")
1229 (7 (unless (ruby-singleton-class-p (match-beginning 0))
1230 (put-text-property (match-beginning 7) (match-end 7)
1231 'syntax-table
(string-to-syntax "\""))
1232 (ruby-syntax-propertize-heredoc end
))))
1233 ;; Handle percent literals: %w(), %q{}, etc.
1234 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re
)
1235 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end
)))))
1238 (defun ruby-syntax-propertize-heredoc (limit)
1239 (let ((ppss (syntax-ppss))
1241 (when (eq ?
\n (nth 3 ppss
))
1243 (goto-char (nth 8 ppss
))
1245 (while (re-search-forward ruby-here-doc-beg-re
1246 (line-end-position) t
)
1247 (unless (ruby-singleton-class-p (match-beginning 0))
1248 (push (concat (ruby-here-doc-end-match) "\n") res
))))
1249 (let ((start (point)))
1250 ;; With multiple openers on the same line, we don't know in which
1251 ;; part `start' is, so we have to go back to the beginning.
1253 (goto-char (nth 8 ppss
))
1254 (setq res
(nreverse res
)))
1255 (while (and res
(re-search-forward (pop res
) limit
'move
))
1257 (put-text-property (1- (point)) (point)
1258 'syntax-table
(string-to-syntax "\""))))
1259 ;; Make extra sure we don't move back, lest we could fall into an
1261 (if (< (point) start
) (goto-char start
))))))
1263 (defun ruby-syntax-enclosing-percent-literal (limit)
1264 (let ((state (syntax-ppss))
1266 ;; When already inside percent literal, re-propertize it.
1267 (when (eq t
(nth 3 state
))
1268 (goto-char (nth 8 state
))
1269 (when (looking-at ruby-percent-literal-beg-re
)
1270 (ruby-syntax-propertize-percent-literal limit
))
1271 (when (< (point) start
) (goto-char start
)))))
1273 (defun ruby-syntax-propertize-percent-literal (limit)
1274 (goto-char (match-beginning 2))
1275 ;; Not inside a simple string or comment.
1276 (when (eq t
(nth 3 (syntax-ppss)))
1277 (let* ((op (char-after))
1278 (ops (char-to-string op
))
1279 (cl (or (cdr (aref (syntax-table) op
))
1280 (cdr (assoc op
'((?
< . ?
>))))))
1281 parse-sexp-lookup-properties
)
1284 (if cl
; Paired delimiters.
1285 ;; Delimiter pairs of the same kind can be nested
1286 ;; inside the literal, as long as they are balanced.
1287 ;; Create syntax table that ignores other characters.
1288 (with-syntax-table (make-char-table 'syntax-table nil
)
1289 (modify-syntax-entry op
(concat "(" (char-to-string cl
)))
1290 (modify-syntax-entry cl
(concat ")" ops
))
1291 (modify-syntax-entry ?
\\ "\\")
1293 (narrow-to-region (point) limit
)
1294 (forward-list))) ; skip to the paired character
1295 ;; Single character delimiter.
1296 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1297 (regexp-quote ops
)) limit nil
))
1298 ;; Found the closing delimiter.
1299 (put-text-property (1- (point)) (point) 'syntax-table
1300 (string-to-syntax "|")))
1301 ;; Unclosed literal, leave the following text unpropertized.
1302 ((scan-error search-failed
) (goto-char limit
))))))
1305 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1306 ;; fallback on the old font-lock-syntactic-keywords stuff.
1308 (defconst ruby-here-doc-end-re
1309 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1310 "Regexp to match the end of heredocs.
1312 This will actually match any line with one or more characters.
1313 It's useful in that it divides up the match string so that
1314 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1316 (defun ruby-here-doc-beg-match ()
1317 "Return a regexp to find the beginning of a heredoc.
1319 This should only be called after matching against `ruby-here-doc-end-re'."
1320 (let ((contents (concat
1321 (regexp-quote (concat (match-string 2) (match-string 3)))
1322 (if (string= (match-string 3) "_") "\\B" "\\b"))))
1324 (let ((match (match-string 1)))
1325 (if (and match
(> (length match
) 0))
1326 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1327 (match-string 1) "\\)"
1328 contents
"\\(\\1\\|\\2\\)")
1329 (concat "-?\\([\"']\\|\\)" contents
"\\1"))))))
1331 (defconst ruby-font-lock-syntactic-keywords
1333 ;; the last $', $", $` in the respective string is not variable
1334 ;; the last ?', ?", ?` in the respective string is not ascii code
1335 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1338 ;; $' $" $` .... are variables
1339 ;; ?' ?" ?` are ascii codes
1340 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil
))
1342 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1345 ("^=en\\(d\\)\\_>" 1 "!")
1347 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1350 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1351 ;; Currently, the following case is highlighted incorrectly:
1360 ;; This is because all here-doc beginnings are highlighted before any endings,
1361 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1362 ;; it thinks <<BAR is part of a string so it's marked as well.
1364 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1365 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1366 ;; but I don't want to try that until we've got unit tests set up
1367 ;; to make sure I don't break anything else.
1368 (,(concat ruby-here-doc-beg-re
".*\\(\n\\)")
1369 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re
))
1370 (ruby-here-doc-beg-syntax))
1371 (,ruby-here-doc-end-re
3 (ruby-here-doc-end-syntax)))
1372 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1374 (defun ruby-comment-beg-syntax ()
1375 "Return the syntax cell for a the first character of a =begin.
1376 See the definition of `ruby-font-lock-syntactic-keywords'.
1378 This returns a comment-delimiter cell as long as the =begin
1379 isn't in a string or another comment."
1380 (when (not (nth 3 (syntax-ppss)))
1381 (string-to-syntax "!")))
1383 (defun ruby-in-here-doc-p ()
1384 "Return whether or not the point is in a heredoc."
1386 (let ((old-point (point)) (case-fold-search nil
))
1389 (while (and (re-search-backward ruby-here-doc-beg-re nil t
)
1390 (not (ruby-singleton-class-p)))
1391 (if (not (or (ruby-in-ppss-context-p 'anything
)
1392 (ruby-here-doc-find-end old-point
)))
1393 (throw 'found-beg t
)))))))
1395 (defun ruby-here-doc-find-end (&optional limit
)
1396 "Expects the point to be on a line with one or more heredoc openers.
1397 Returns the buffer position at which all heredocs on the line
1398 are terminated, or nil if they aren't terminated before the
1399 buffer position `limit' or the end of the buffer."
1403 (let ((eol (point-at-eol))
1404 (case-fold-search nil
)
1405 ;; Fake match data such that (match-end 0) is at eol
1406 (end-match-data (progn (looking-at ".*$") (match-data)))
1407 beg-match-data end-re
)
1408 (while (re-search-forward ruby-here-doc-beg-re eol t
)
1409 (setq beg-match-data
(match-data))
1410 (setq end-re
(ruby-here-doc-end-match))
1412 (set-match-data end-match-data
)
1413 (goto-char (match-end 0))
1414 (unless (re-search-forward end-re limit t
) (throw 'done nil
))
1415 (setq end-match-data
(match-data))
1417 (set-match-data beg-match-data
)
1418 (goto-char (match-end 0)))
1419 (set-match-data end-match-data
)
1420 (goto-char (match-end 0))
1423 (defun ruby-here-doc-beg-syntax ()
1424 "Return the syntax cell for a line that may begin a heredoc.
1425 See the definition of `ruby-font-lock-syntactic-keywords'.
1427 This sets the syntax cell for the newline ending the line
1428 containing the heredoc beginning so that cases where multiple
1429 heredocs are started on one line are handled correctly."
1431 (goto-char (match-beginning 0))
1432 (unless (or (ruby-in-ppss-context-p 'non-heredoc
)
1433 (ruby-in-here-doc-p))
1434 (string-to-syntax "\""))))
1436 (defun ruby-here-doc-end-syntax ()
1437 "Return the syntax cell for a line that may end a heredoc.
1438 See the definition of `ruby-font-lock-syntactic-keywords'."
1439 (let ((pss (syntax-ppss)) (case-fold-search nil
))
1440 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1441 ;; so we can just give up.
1442 ;; This means we aren't doing a full-document search
1443 ;; every time we enter a character.
1444 (when (ruby-in-ppss-context-p 'heredoc pss
)
1446 (goto-char (nth 8 pss
)) ; Go to the beginning of heredoc.
1447 (let ((eol (point)))
1449 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t
) ; If there is a heredoc that matches this line...
1450 (not (ruby-in-ppss-context-p 'anything
)) ; And that's not inside a heredoc/string/comment...
1451 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1452 (not (re-search-forward ruby-here-doc-beg-re eol t
))))
1453 (string-to-syntax "\"")))))))
1455 (unless (functionp 'syntax-ppss
)
1456 (defun syntax-ppss (&optional pos
)
1457 (parse-partial-sexp (point-min) (or pos
(point)))))
1460 (defun ruby-in-ppss-context-p (context &optional ppss
)
1461 (let ((ppss (or ppss
(syntax-ppss (point)))))
1463 ((eq context
'anything
)
1466 ((eq context
'string
)
1468 ((eq context
'heredoc
)
1469 (eq ?
\n (nth 3 ppss
)))
1470 ((eq context
'non-heredoc
)
1471 (and (ruby-in-ppss-context-p 'anything
)
1472 (not (ruby-in-ppss-context-p 'heredoc
))))
1473 ((eq context
'comment
)
1477 "Internal error on `ruby-in-ppss-context-p': "
1478 "context name `" (symbol-name context
) "' is unknown"))))
1481 (if (featurep 'xemacs
)
1482 (put 'ruby-mode
'font-lock-defaults
1483 '((ruby-font-lock-keywords)
1486 (font-lock-syntactic-keywords
1487 . ruby-font-lock-syntactic-keywords
))))
1489 (defvar ruby-font-lock-syntax-table
1490 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1491 (modify-syntax-entry ?_
"w" tbl
)
1493 "The syntax table to use for fontifying Ruby mode buffers.
1494 See `font-lock-syntax-table'.")
1496 (defconst ruby-font-lock-keywords
1499 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1500 1 font-lock-function-name-face
)
1503 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1547 ruby-keyword-end-re
)
1549 ;; here-doc beginnings
1550 `(,ruby-here-doc-beg-re
0 (unless (ruby-singleton-class-p (match-beginning 0))
1551 'font-lock-string-face
))
1553 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1554 2 font-lock-variable-name-face
)
1556 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1557 2 font-lock-constant-face
)
1559 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1560 1 font-lock-variable-name-face
)
1561 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1562 0 font-lock-variable-name-face
)
1564 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1565 2 font-lock-type-face
)
1566 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face
)
1567 ;; expression expansion
1568 '(ruby-match-expression-expansion
1569 0 font-lock-variable-name-face t
)
1570 ;; warn lower camel case
1571 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1572 ; 0 font-lock-warning-face)
1574 "Additional expressions to highlight in Ruby mode.")
1576 (defun ruby-match-expression-expansion (limit)
1577 (when (re-search-forward "[^\\]\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)" limit
'move
)
1578 (or (ruby-in-ppss-context-p 'string
)
1579 (ruby-match-expression-expansion limit
))))
1582 (define-derived-mode ruby-mode prog-mode
"Ruby"
1583 "Major mode for editing Ruby scripts.
1584 \\[ruby-indent-line] properly indents subexpressions of multi-line
1585 class, module, def, if, while, for, do, and case statements, taking
1586 nesting into account.
1588 The variable `ruby-indent-level' controls the amount of indentation.
1591 (ruby-mode-variables)
1593 (set (make-local-variable 'imenu-create-index-function
)
1594 'ruby-imenu-create-index
)
1595 (set (make-local-variable 'add-log-current-defun-function
)
1596 'ruby-add-log-current-method
)
1597 (set (make-local-variable 'beginning-of-defun-function
)
1598 'ruby-beginning-of-defun
)
1599 (set (make-local-variable 'end-of-defun-function
)
1603 (cond ((boundp 'before-save-hook
) 'before-save-hook
)
1604 ((boundp 'write-contents-functions
) 'write-contents-functions
)
1605 ((boundp 'write-contents-hooks
) 'write-contents-hooks
))
1606 'ruby-mode-set-encoding nil
'local
)
1608 (set (make-local-variable 'electric-indent-chars
)
1609 (append '(?\
{ ?\
}) electric-indent-chars
))
1611 (set (make-local-variable 'font-lock-defaults
)
1612 '((ruby-font-lock-keywords) nil nil
))
1613 (set (make-local-variable 'font-lock-keywords
)
1614 ruby-font-lock-keywords
)
1615 (set (make-local-variable 'font-lock-syntax-table
)
1616 ruby-font-lock-syntax-table
)
1618 (if (eval-when-compile (fboundp 'syntax-propertize-rules
))
1619 (set (make-local-variable 'syntax-propertize-function
)
1620 #'ruby-syntax-propertize-function
)
1621 (set (make-local-variable 'font-lock-syntactic-keywords
)
1622 ruby-font-lock-syntactic-keywords
)))
1624 ;;; Invoke ruby-mode when appropriate
1627 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.rb\\'") 'ruby-mode
))
1629 (add-to-list 'auto-mode-alist
'("Rakefile\\'" . ruby-mode
))
1632 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
1633 (add-to-list 'interpreter-mode-alist
(cons (purecopy name
) 'ruby-mode
)))
1635 (provide 'ruby-mode
)
1637 ;;; ruby-mode.el ends here