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.")
107 (defconst ruby-here-doc-beg-re
108 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
109 "Regexp to match the beginning of a heredoc."))
111 (defun ruby-here-doc-end-match ()
112 "Return a regexp to find the end of a heredoc.
114 This should only be called after matching against `ruby-here-doc-beg-re'."
116 (if (match-string 2) "[ \t]*" nil
)
122 (defconst ruby-delimiter
123 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
125 "\\)\\_>\\|" ruby-block-end-re
126 "\\|^=begin\\|" ruby-here-doc-beg-re
))
128 (defconst ruby-negative
129 (concat "^[ \t]*\\(\\(" ruby-block-mid-re
"\\)\\>\\|"
130 ruby-block-end-re
"\\|}\\|\\]\\)")
131 "Regexp to match where the indentation gets shallower.")
133 (defconst ruby-operator-re
"[-,.+*/%&|^~=<>:]"
134 "Regexp to match operators.")
136 (defconst ruby-symbol-chars
"a-zA-Z0-9_"
137 "List of characters that symbol names may contain.")
138 (defconst ruby-symbol-re
(concat "[" ruby-symbol-chars
"]")
139 "Regexp to match symbols.")
141 (define-abbrev-table 'ruby-mode-abbrev-table
()
142 "Abbrev table in use in Ruby mode buffers.")
144 (defvar ruby-mode-map
145 (let ((map (make-sparse-keymap)))
146 (define-key map
(kbd "M-C-b") 'ruby-backward-sexp
)
147 (define-key map
(kbd "M-C-f") 'ruby-forward-sexp
)
148 (define-key map
(kbd "M-C-p") 'ruby-beginning-of-block
)
149 (define-key map
(kbd "M-C-n") 'ruby-end-of-block
)
150 (define-key map
(kbd "M-C-q") 'ruby-indent-exp
)
151 (define-key map
(kbd "C-c {") 'ruby-toggle-block
)
153 "Keymap used in Ruby mode.")
155 (defvar ruby-mode-syntax-table
156 (let ((table (make-syntax-table)))
157 (modify-syntax-entry ?
\' "\"" table
)
158 (modify-syntax-entry ?
\" "\"" table
)
159 (modify-syntax-entry ?\
` "\"" table
)
160 (modify-syntax-entry ?
# "<" table
)
161 (modify-syntax-entry ?
\n ">" 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
)
183 (modify-syntax-entry ?\
] ")[" table
)
185 "Syntax table to use in Ruby mode.")
187 (defcustom ruby-indent-tabs-mode nil
188 "Indentation can insert tabs in Ruby mode if this is non-nil."
189 :type
'boolean
:group
'ruby
)
191 (defcustom ruby-indent-level
2
192 "Indentation of Ruby statements."
193 :type
'integer
:group
'ruby
)
195 (defcustom ruby-comment-column
32
196 "Indentation column of comments."
197 :type
'integer
:group
'ruby
)
199 (defcustom ruby-deep-arglist t
200 "Deep indent lists in parenthesis when non-nil.
201 Also ignores spaces after parenthesis when 'space."
204 (defcustom ruby-deep-indent-paren
'(?\
( ?\
[ ?\
] t
)
205 "Deep indent lists in parenthesis when non-nil.
206 The value t means continuous line.
207 Also ignores spaces after parenthesis when 'space."
210 (defcustom ruby-deep-indent-paren-style
'space
211 "Default deep indent style."
212 :options
'(t nil space
) :group
'ruby
)
214 (defcustom ruby-encoding-map
'((shift_jis . cp932
) (shift-jis . cp932
))
215 "Alist to map encoding name from Emacs to Ruby."
218 (defcustom ruby-insert-encoding-magic-comment t
219 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
220 :type
'boolean
:group
'ruby
)
222 (defcustom ruby-use-encoding-map t
223 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
224 :type
'boolean
:group
'ruby
)
226 ;; Safe file variables
227 (put 'ruby-indent-tabs-mode
'safe-local-variable
'booleanp
)
228 (put 'ruby-indent-level
'safe-local-variable
'integerp
)
229 (put 'ruby-comment-column
'safe-local-variable
'integerp
)
230 (put 'ruby-deep-arglist
'safe-local-variable
'booleanp
)
232 (defun ruby-imenu-create-index-in-block (prefix beg end
)
233 "Create an imenu index of methods inside a block."
234 (let ((index-alist '()) (case-fold-search nil
)
235 name next pos decl sing
)
237 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t
)
238 (setq sing
(match-beginning 3))
239 (setq decl
(match-string 5))
240 (setq next
(match-end 0))
241 (setq name
(or (match-string 4) (match-string 6)))
242 (setq pos
(match-beginning 0))
244 ((string= "alias" decl
)
245 (if prefix
(setq name
(concat prefix name
)))
246 (push (cons name pos
) index-alist
))
247 ((string= "def" decl
)
251 ((string-match "^self\." name
)
252 (concat (substring prefix
0 -
1) (substring name
4)))
253 (t (concat prefix name
)))))
254 (push (cons name pos
) index-alist
)
255 (ruby-accurate-end-of-block end
))
257 (if (string= "self" name
)
258 (if prefix
(setq name
(substring prefix
0 -
1)))
259 (if prefix
(setq name
(concat (substring prefix
0 -
1) "::" name
)))
260 (push (cons name pos
) index-alist
))
261 (ruby-accurate-end-of-block end
)
264 (nconc (ruby-imenu-create-index-in-block
265 (concat name
(if sing
"." "#"))
266 next beg
) index-alist
))
270 (defun ruby-imenu-create-index ()
271 "Create an imenu index of all methods in the buffer."
272 (nreverse (ruby-imenu-create-index-in-block nil
(point-min) nil
)))
274 (defun ruby-accurate-end-of-block (&optional end
)
277 (end (or end
(point-max))))
278 (while (and (setq state
(apply 'ruby-parse-partial end state
))
279 (>= (nth 2 state
) 0) (< (point) end
)))))
281 (defun ruby-mode-variables ()
282 "Set up initial buffer-local variables for Ruby mode."
283 (set-syntax-table ruby-mode-syntax-table
)
284 (setq local-abbrev-table ruby-mode-abbrev-table
)
285 (setq indent-tabs-mode ruby-indent-tabs-mode
)
286 (set (make-local-variable 'indent-line-function
) 'ruby-indent-line
)
287 (set (make-local-variable 'require-final-newline
) t
)
288 (set (make-local-variable 'comment-start
) "# ")
289 (set (make-local-variable 'comment-end
) "")
290 (set (make-local-variable 'comment-column
) ruby-comment-column
)
291 (set (make-local-variable 'comment-start-skip
) "#+ *")
292 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
293 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
294 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
295 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
296 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
))
298 (defun ruby-mode-set-encoding ()
299 "Insert a magic comment header with the proper encoding if necessary."
302 (goto-char (point-min))
303 (when (re-search-forward "[^\0-\177]" nil t
)
304 (goto-char (point-min))
306 (or coding-system-for-write
307 buffer-file-coding-system
)))
310 (or (coding-system-get coding-system
'mime-charset
)
311 (coding-system-change-eol-conversion coding-system nil
))))
315 (or (and ruby-use-encoding-map
316 (cdr (assq coding-system ruby-encoding-map
)))
319 (if (looking-at "^#!") (beginning-of-line 2))
320 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
321 (unless (string= (match-string 2) coding-system
)
322 (goto-char (match-beginning 2))
323 (delete-region (point) (match-end 2))
324 (and (looking-at "-\*-")
325 (let ((n (skip-chars-backward " ")))
326 (cond ((= n
0) (insert " ") (backward-char))
327 ((= n -
1) (insert " "))
329 (insert coding-system
)))
330 ((looking-at "\\s *#.*coding\\s *[:=]"))
331 (t (when ruby-insert-encoding-magic-comment
332 (insert "# -*- coding: " coding-system
" -*-\n"))))))))
334 (defun ruby-current-indentation ()
335 "Return the indentation level of current line."
338 (back-to-indentation)
341 (defun ruby-indent-line (&optional ignored
)
342 "Correct the indentation of the current Ruby line."
344 (ruby-indent-to (ruby-calculate-indent)))
346 (defun ruby-indent-to (column)
347 "Indent the current line to COLUMN."
350 (and (< column
0) (error "invalid nest"))
351 (setq shift
(current-column))
354 (back-to-indentation)
355 (setq top
(current-column))
356 (skip-chars-backward " \t")
357 (if (>= shift top
) (setq shift
(- shift top
))
361 (move-to-column (+ column shift
))
363 (delete-region beg
(point))
366 (move-to-column (+ column shift
))))))
368 (defun ruby-special-char-p (&optional pos
)
369 "Return t if the character before POS is a special character.
370 If omitted, POS defaults to the current point.
371 Special characters are `?', `$', `:' when preceded by whitespace,
372 and `\\' when preceded by `?'."
373 (setq pos
(or pos
(point)))
374 (let ((c (char-before pos
)) (b (and (< (point-min) pos
)
375 (char-before (1- pos
)))))
376 (cond ((or (eq c ??
) (eq c ?$
)))
377 ((and (eq c ?
:) (or (not b
) (eq (char-syntax b
) ?
))))
378 ((eq c ?
\\) (eq b ??
)))))
380 (defun ruby-singleton-class-p (&optional pos
)
382 (when pos
(goto-char pos
))
384 (and (or (bolp) (not (eq (char-before (point)) ?_
)))
385 (looking-at "class\\s *<<"))))
387 (defun ruby-expr-beg (&optional option
)
390 (store-match-data nil
)
391 (let ((space (skip-chars-backward " \t"))
397 (and (looking-at "\\?")
398 (or (eq (char-syntax (char-before (point))) ?w
)
399 (ruby-special-char-p))))
401 ((and (eq option
'heredoc
) (< space
0))
402 (not (progn (goto-char start
) (ruby-singleton-class-p))))
403 ((or (looking-at ruby-operator-re
)
404 (looking-at "[\\[({,;]")
405 (and (looking-at "[!?]")
406 (or (not (eq option
'modifier
))
408 (save-excursion (forward-char -
1) (looking-at "\\Sw$"))))
409 (and (looking-at ruby-symbol-re
)
410 (skip-chars-backward ruby-symbol-chars
)
412 ((looking-at (regexp-opt
413 (append ruby-block-beg-keywords
414 ruby-block-op-keywords
415 ruby-block-mid-keywords
)
417 (goto-char (match-end 0))
418 (not (looking-at "\\s_\\|!")))
419 ((eq option
'expr-qstr
)
420 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
421 ((eq option
'expr-re
)
422 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
425 (defun ruby-forward-string (term &optional end no-error expand
)
427 (let ((n 1) (c (string-to-char term
))
429 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term
"]\\|\\(#{\\)\\)")
430 (concat "[^\\]\\(\\\\\\\\\\)*[" term
"]"))))
431 (while (and (re-search-forward re end no-error
)
432 (if (match-beginning 3)
433 (ruby-forward-string "}{" end no-error nil
)
434 (> (setq n
(if (eq (char-before (point)) c
)
439 ((error "unterminated string")))))
441 (defun ruby-deep-indent-paren-p (c)
443 (cond ((listp ruby-deep-indent-paren
)
444 (let ((deep (assoc c ruby-deep-indent-paren
)))
446 (or (cdr deep
) ruby-deep-indent-paren-style
))
447 ((memq c ruby-deep-indent-paren
)
448 ruby-deep-indent-paren-style
))))
449 ((eq c ruby-deep-indent-paren
) ruby-deep-indent-paren-style
)
450 ((eq c ?\
( ) ruby-deep-arglist
)))
452 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent
)
453 "TODO: document throughout function body."
454 (or depth
(setq depth
0))
455 (or indent
(setq indent
0))
456 (when (re-search-forward ruby-delimiter end
'move
)
457 (let ((pnt (point)) w re expand
)
458 (goto-char (match-beginning 0))
460 ((and (memq (char-before) '(?
@ ?$
)) (looking-at "\\sw"))
462 ((looking-at "[\"`]") ;skip string
465 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t
))
468 (setq in-string
(point))
473 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t
))
476 (setq in-string
(point))
482 ((and (not (eobp)) (ruby-expr-beg 'expr-re
))
483 (if (ruby-forward-string "/" end t t
)
485 (setq in-string
(point))
492 (ruby-expr-beg 'expr-qstr
)
493 (not (looking-at "%="))
494 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
495 (goto-char (match-beginning 1))
496 (setq expand
(not (memq (char-before) '(?q ?w
))))
497 (setq w
(match-string 1))
499 ((string= w
"[") (setq re
"]["))
500 ((string= w
"{") (setq re
"}{"))
501 ((string= w
"(") (setq re
")("))
502 ((string= w
"<") (setq re
"><"))
503 ((and expand
(string= w
"\\"))
504 (setq w
(concat "\\" w
))))
505 (unless (cond (re (ruby-forward-string re end t expand
))
506 (expand (ruby-forward-string w end t t
))
507 (t (re-search-forward
510 (concat "[^\\]\\(\\\\\\\\\\)*" w
))
512 (setq in-string
(point))
516 ((looking-at "\\?") ;skip ?char
518 ((and (ruby-expr-beg)
519 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
520 (goto-char (match-end 0)))
523 ((looking-at "\\$") ;skip $char
526 ((looking-at "#") ;skip comment
530 ((looking-at "[\\[{(]")
531 (let ((deep (ruby-deep-indent-paren-p (char-after))))
532 (if (and deep
(or (not (eq (char-after) ?\
{)) (ruby-expr-beg)))
534 (and (eq deep
'space
) (looking-at ".\\s +[^# \t\n]")
535 (setq pnt
(1- (match-end 0))))
536 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
537 (setq pcol
(cons (cons pnt depth
) pcol
))
539 (setq nest
(cons (cons (char-after (point)) pnt
) nest
))
540 (setq depth
(1+ depth
))))
543 ((looking-at "[])}]")
544 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
545 (setq depth
(cdr (car pcol
)) pcol
(cdr pcol
))
546 (setq depth
(1- depth
)))
547 (setq nest
(cdr nest
))
549 ((looking-at ruby-block-end-re
)
550 (if (or (and (not (bolp))
553 (setq w
(char-after (point)))
558 (setq w
(char-after (point)))
563 (setq nest
(cdr nest
))
564 (setq depth
(1- depth
)))
566 ((looking-at "def\\s +[^(\n;]*")
570 (not (eq ?_
(char-after (point))))))
572 (setq nest
(cons (cons nil pnt
) nest
))
573 (setq depth
(1+ depth
))))
574 (goto-char (match-end 0)))
575 ((looking-at (concat "\\_<\\(" ruby-block-beg-re
"\\)\\_>"))
578 (or (not (looking-at (concat "do" ruby-keyword-end-re
)))
580 (back-to-indentation)
581 (not (looking-at ruby-non-block-do-re
)))))
585 (setq w
(char-after (point)))
589 (setq w
(char-after (point)))
591 (skip-chars-forward " \t")
592 (goto-char (match-beginning 0))
593 (or (not (looking-at ruby-modifier-re
))
594 (ruby-expr-beg 'modifier
))
596 (setq nest
(cons (cons nil pnt
) nest
))
597 (setq depth
(1+ depth
)))
599 ((looking-at ":\\(['\"]\\)")
600 (goto-char (match-beginning 1))
601 (ruby-forward-string (match-string 1) end t
))
602 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
603 (goto-char (match-end 0)))
604 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
605 (goto-char (match-end 0)))
606 ((or (looking-at "\\.\\.\\.?")
607 (looking-at "\\.[0-9]+")
608 (looking-at "\\.[a-zA-Z_0-9]+")
610 (goto-char (match-end 0)))
611 ((looking-at "^=begin")
612 (if (re-search-forward "^=end" end t
)
614 (setq in-string
(match-end 0))
618 ((and (ruby-expr-beg 'heredoc
)
619 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
620 (setq re
(regexp-quote (or (match-string 4) (match-string 2))))
621 (if (match-beginning 1) (setq re
(concat "\\s *" re
)))
622 (let* ((id-end (goto-char (match-end 0)))
623 (line-end-position (point-at-eol))
624 (state (list in-string nest depth pcol indent
)))
625 ;; parse the rest of the line
626 (while (and (> line-end-position
(point))
627 (setq state
(apply 'ruby-parse-partial
628 line-end-position state
))))
629 (setq in-string
(car state
)
633 indent
(nth 4 state
))
634 ;; skip heredoc section
635 (if (re-search-forward (concat "^" re
"$") end
'move
)
637 (setq in-string id-end
)
641 ((looking-at "^__END__$")
643 ((and (looking-at ruby-here-doc-beg-re
)
644 (boundp 'ruby-indent-point
))
645 (if (re-search-forward (ruby-here-doc-end-match)
648 (setq in-string
(match-end 0))
649 (goto-char ruby-indent-point
)))
651 (error (format "bad string %s"
652 (buffer-substring (point) pnt
)
654 (list in-string nest depth pcol
))
656 (defun ruby-parse-region (start end
)
662 (ruby-beginning-of-indent))
664 (narrow-to-region (point) end
)
665 (while (and (> end
(point))
666 (setq state
(apply 'ruby-parse-partial end state
))))))
667 (list (nth 0 state
) ; in-string
668 (car (nth 1 state
)) ; nest
669 (nth 2 state
) ; depth
670 (car (car (nth 3 state
))) ; pcol
671 ;(car (nth 5 state)) ; indent
674 (defun ruby-indent-size (pos nest
)
675 "Return the indentation level in spaces NEST levels deeper than POS."
676 (+ pos
(* (or nest
1) ruby-indent-level
)))
678 (defun ruby-calculate-indent (&optional parse-start
)
679 "Return the proper indentation level of the current line."
680 ;; TODO: Document body
683 (let ((ruby-indent-point (point))
684 (case-fold-search nil
)
685 state eol begin op-end
686 (paren (progn (skip-syntax-forward " ")
687 (and (char-after) (matching-paren (char-after)))))
690 (goto-char parse-start
)
691 (ruby-beginning-of-indent)
692 (setq parse-start
(point)))
693 (back-to-indentation)
694 (setq indent
(current-column))
695 (setq state
(ruby-parse-region parse-start ruby-indent-point
))
697 ((nth 0 state
) ; within string
698 (setq indent nil
)) ; do nothing
699 ((car (nth 1 state
)) ; in paren
700 (goto-char (setq begin
(cdr (nth 1 state
))))
701 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state
)))))
703 (cond ((and (eq deep t
) (eq (car (nth 1 state
)) paren
))
704 (skip-syntax-backward " ")
705 (setq indent
(1- (current-column))))
706 ((let ((s (ruby-parse-region (point) ruby-indent-point
)))
707 (and (nth 2 s
) (> (nth 2 s
) 0)
708 (or (goto-char (cdr (nth 1 s
))) t
)))
710 (setq indent
(ruby-indent-size (current-column)
713 (setq indent
(current-column))
714 (cond ((eq deep
'space
))
715 (paren (setq indent
(1- indent
)))
716 (t (setq indent
(ruby-indent-size (1- indent
) 1))))))
717 (if (nth 3 state
) (goto-char (nth 3 state
))
718 (goto-char parse-start
) (back-to-indentation))
719 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
720 (and (eq (car (nth 1 state
)) paren
)
721 (ruby-deep-indent-paren-p (matching-paren paren
))
722 (search-backward (char-to-string paren
))
723 (setq indent
(current-column)))))
724 ((and (nth 2 state
) (> (nth 2 state
) 0)) ; in nest
725 (if (null (cdr (nth 1 state
)))
726 (error "invalid nest"))
727 (goto-char (cdr (nth 1 state
)))
728 (forward-word -
1) ; skip back a keyword
731 ((looking-at "do\\>[^_]") ; iter block is a special case
732 (if (nth 3 state
) (goto-char (nth 3 state
))
733 (goto-char parse-start
) (back-to-indentation))
734 (setq indent
(ruby-indent-size (current-column) (nth 2 state
))))
736 (setq indent
(+ (current-column) ruby-indent-level
)))))
738 ((and (nth 2 state
) (< (nth 2 state
) 0)) ; in negative nest
739 (setq indent
(ruby-indent-size (current-column) (nth 2 state
)))))
741 (goto-char ruby-indent-point
)
746 ((and (not (ruby-deep-indent-paren-p paren
))
747 (re-search-forward ruby-negative eol t
))
748 (and (not (eq ?_
(char-after (match-end 0))))
749 (setq indent
(- indent ruby-indent-level
))))
754 (or (ruby-deep-indent-paren-p t
)
755 (null (car (nth 1 state
)))))
756 ;; goto beginning of non-empty no-comment line
759 (skip-chars-backward " \t\n")
762 (if (re-search-forward "^\\s *#" end t
)
766 ;; skip the comment at the end
767 (skip-chars-backward " \t")
768 (let (end (pos (point)))
770 (while (and (re-search-forward "#" pos t
)
771 (setq end
(1- (point)))
772 (or (ruby-special-char-p end
)
773 (and (setq state
(ruby-parse-region parse-start end
))
776 (goto-char (or end pos
))
777 (skip-chars-backward " \t")
778 (setq begin
(if (and end
(nth 0 state
)) pos
(cdr (nth 1 state
))))
779 (setq state
(ruby-parse-region parse-start
(point))))
780 (or (bobp) (forward-char -
1))
782 (or (and (looking-at ruby-symbol-re
)
783 (skip-chars-backward ruby-symbol-chars
)
784 (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>"))
785 (not (eq (point) (nth 3 state
)))
787 (goto-char (match-end 0))
788 (not (looking-at "[a-z_]"))))
789 (and (looking-at ruby-operator-re
)
790 (not (ruby-special-char-p))
791 ;; Operator at the end of line.
792 (let ((c (char-after (point))))
797 ;; (skip-chars-forward " \t")
798 ;; (not (or (eolp) (looking-at "#")
799 ;; (and (eq (car (nth 1 state)) ?{)
800 ;; (looking-at "|"))))))
801 ;; Not a regexp or percent literal.
802 (null (nth 0 (ruby-parse-region (or begin parse-start
)
804 (or (not (eq ?|
(char-after (point))))
806 (or (eolp) (forward-char -
1))
808 ((search-backward "|" nil t
)
809 (skip-chars-backward " \t\n")
813 (not (looking-at "{")))
816 (not (looking-at "do\\>[^_]")))))
824 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
"\\)\\>")))
825 (eq (ruby-deep-indent-paren-p t
) 'space
)
828 (goto-char (or begin parse-start
))
829 (skip-syntax-forward " ")
831 ((car (nth 1 state
)) indent
)
833 (+ indent ruby-indent-level
))))))))
834 (goto-char ruby-indent-point
)
836 (skip-syntax-forward " ")
837 (if (looking-at "\\.[^.]")
838 (+ indent ruby-indent-level
)
841 (defun ruby-beginning-of-defun (&optional arg
)
842 "Move backward to the beginning of the current top-level defun.
843 With ARG, move backward multiple defuns. Negative ARG means
846 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re
"\\_>")
848 (beginning-of-line)))
850 (defun ruby-end-of-defun (&optional arg
)
851 "Move forward to the end of the current top-level defun.
852 With ARG, move forward multiple defuns. Negative ARG means
856 (when (looking-back (concat "^\\s *" ruby-block-end-re
))
859 (defun ruby-beginning-of-indent ()
860 "Backtrack to a line which can be used as a reference for
861 calculating indentation on the lines after it."
862 (while (and (re-search-backward ruby-indent-beg-re nil
'move
)
863 (if (ruby-in-ppss-context-p 'anything
)
865 ;; We can stop, then.
866 (beginning-of-line)))))
868 (defun ruby-move-to-block (n)
869 "Move to the beginning (N < 0) or the end (N > 0) of the current block
870 or blocks containing the current block."
871 ;; TODO: Make this work for n > 1,
872 ;; make it not loop for n = 0,
875 (start (ruby-calculate-indent))
876 (down (looking-at (if (< n
0) ruby-block-end-re
877 (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))))
879 (while (and (not done
) (not (if (< n
0) (bobp) (eobp))))
882 ((looking-at "^\\s *$"))
883 ((looking-at "^\\s *#"))
884 ((and (> n
0) (looking-at "^=begin\\>"))
885 (re-search-forward "^=end\\>"))
886 ((and (< n
0) (looking-at "^=end\\>"))
887 (re-search-backward "^=begin\\>"))
889 (setq pos
(current-indentation))
893 ((and down
(= pos start
))
899 (back-to-indentation)
900 (if (looking-at (concat "\\<\\(" ruby-block-mid-re
"\\)\\>"))
902 (back-to-indentation)))
904 (defun ruby-beginning-of-block (&optional arg
)
905 "Move backward to the beginning of the current block.
906 With ARG, move up multiple blocks."
908 (ruby-move-to-block (- (or arg
1))))
910 (defun ruby-end-of-block (&optional arg
)
911 "Move forward to the end of the current block.
912 With ARG, move out of multiple blocks."
913 ;; Passing a value > 1 to ruby-move-to-block currently doesn't work.
915 (ruby-move-to-block (or arg
1)))
917 (defun ruby-forward-sexp (&optional arg
)
918 "Move forward across one balanced expression (sexp).
919 With ARG, do it many times. Negative ARG means move backward."
920 ;; TODO: Document body
922 (if (and (numberp arg
) (< arg
0))
923 (ruby-backward-sexp (- arg
))
924 (let ((i (or arg
1)))
927 (skip-syntax-forward " ")
928 (if (looking-at ",\\s *") (goto-char (match-end 0)))
929 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
930 (goto-char (match-end 0)))
932 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
934 (goto-char (scan-sexps (point) 1)))
935 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
"\\)\\>"))
936 (not (eq (char-before (point)) ?.
))
937 (not (eq (char-before (point)) ?
:)))
940 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
942 (while (progn (forward-word 1) (looking-at "_")))
943 (cond ((looking-at "::") (forward-char 2) t
)
944 ((> (skip-chars-forward ".") 0))
945 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
946 (forward-char 1) nil
)))))
950 (setq expr
(or expr
(ruby-expr-beg)
951 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
952 (nth 1 (setq state
(apply 'ruby-parse-partial nil state
))))
954 (skip-chars-forward "<"))
957 ((error) (forward-word 1)))
960 (defun ruby-backward-sexp (&optional arg
)
961 "Move backward across one balanced expression (sexp).
962 With ARG, do it many times. Negative ARG means move forward."
963 ;; TODO: Document body
965 (if (and (numberp arg
) (< arg
0))
966 (ruby-forward-sexp (- arg
))
967 (let ((i (or arg
1)))
970 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
972 (cond ((looking-at "\\s)")
973 (goto-char (scan-sexps (1+ (point)) -
1))
975 (?%
(forward-char -
1))
977 (if (eq (char-before (1- (point))) ?%
) (forward-char -
2))))
979 ((looking-at "\\s\"\\|\\\\\\S_")
980 (let ((c (char-to-string (char-before (match-end 0)))))
981 (while (and (search-backward c
)
982 (eq (logand (skip-chars-backward "\\") 1)
985 ((looking-at "\\s.\\|\\s\\")
986 (if (ruby-special-char-p) (forward-char -
1)))
987 ((looking-at "\\s(") nil
)
990 (while (progn (forward-word -
1)
993 (?.
(forward-char -
1) t
)
996 (and (eq (char-before) (char-after)) (forward-char -
1)))
999 (eq (char-before) :)))))
1000 (if (looking-at ruby-block-end-re
)
1001 (ruby-beginning-of-block))
1007 (defun ruby-indent-exp (&optional ignored
)
1008 "Indent each line in the balanced expression following the point."
1010 (let ((here (point-marker)) start top column
(nest t
))
1011 (set-marker-insertion-type here t
)
1015 (setq start
(point) top
(current-indentation))
1016 (while (and (not (eobp))
1018 (setq column
(ruby-calculate-indent start
))
1019 (cond ((> column top
)
1021 ((and (= column top
) nest
)
1022 (setq nest nil
) t
))))
1023 (ruby-indent-to column
)
1024 (beginning-of-line 2)))
1026 (set-marker here nil
))))
1028 (defun ruby-add-log-current-method ()
1029 "Return the current method name as a string.
1030 This string includes all namespaces.
1039 See `add-log-current-defun-function'."
1040 ;; TODO: Document body
1041 ;; Why does this append a period to class methods?
1044 (let (mname mlist
(indent 0))
1045 ;; get current method (or class/module)
1046 (if (re-search-backward
1047 (concat "^[ \t]*" ruby-defun-beg-re
"[ \t]+"
1049 ;; \\. and :: for class method
1050 "\\([A-Za-z_]" ruby-symbol-re
"*\\|\\.\\|::" "\\)"
1054 (setq mname
(match-string 2))
1055 (unless (string-equal "def" (match-string 1))
1056 (setq mlist
(list mname
) mname nil
))
1057 (goto-char (match-beginning 1))
1058 (setq indent
(current-column))
1059 (beginning-of-line)))
1060 ;; nest class/module
1061 (while (and (> indent
0)
1064 "^[ \t]*\\(class\\|module\\)[ \t]+"
1065 "\\([A-Z]" ruby-symbol-re
"*\\)")
1067 (goto-char (match-beginning 1))
1068 (if (< (current-column) indent
)
1070 (setq mlist
(cons (match-string 2) mlist
))
1071 (setq indent
(current-column))
1072 (beginning-of-line))))
1074 (let ((mn (split-string mname
"\\.\\|::")))
1078 ((string-equal "" (car mn
))
1079 (setq mn
(cdr mn
) mlist nil
))
1080 ((string-equal "self" (car mn
))
1082 ((let ((ml (nreverse mlist
)))
1084 (if (string-equal (car ml
) (car mn
))
1085 (setq mlist
(nreverse (cdr ml
)) ml nil
))
1086 (or (setq ml
(cdr ml
)) (nreverse mlist
))))))
1088 (setcdr (last mlist
) mn
)
1090 (setq mn
(last mn
2))
1091 (setq mname
(concat "." (cadr mn
)))
1093 (setq mname
(concat "#" mname
)))))
1096 (setq mlist
(mapconcat (function identity
) mlist
"::")))
1098 (if mlist
(concat mlist mname
) mname
)
1101 (defun ruby-brace-to-do-end (orig end
)
1102 (let (beg-marker end-marker
)
1104 (when (eq (char-before) ?\
})
1106 (when (save-excursion
1107 (skip-chars-backward " \t")
1111 (setq end-marker
(point-marker))
1112 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w
))
1116 (when (eq (char-syntax (char-before)) ?w
)
1119 (setq beg-marker
(point-marker))
1120 (when (looking-at "\\(\\s \\)*|")
1121 (unless (match-beginning 1)
1123 (goto-char (1+ (match-end 0)))
1124 (search-forward "|"))
1125 (unless (looking-at "\\s *$")
1127 (indent-region beg-marker end-marker
)
1128 (goto-char beg-marker
)
1131 (defun ruby-do-end-to-brace (orig end
)
1132 (let (beg-marker end-marker beg-pos end-pos
)
1133 (goto-char (- end
3))
1134 (when (looking-at ruby-block-end-re
)
1136 (setq end-marker
(point-marker))
1141 (setq beg-marker
(point-marker))
1142 (when (looking-at "\\s +|")
1143 (delete-char (- (match-end 0) (match-beginning 0) 1))
1145 (re-search-forward "|" (line-end-position) t
))
1147 (skip-chars-forward " \t\n\r")
1148 (setq beg-pos
(point))
1149 (goto-char end-marker
)
1150 (skip-chars-backward " \t\n\r")
1151 (setq end-pos
(point)))
1154 (and (= (line-number-at-pos beg-pos
) (line-number-at-pos end-pos
))
1155 (< (+ (current-column) (- end-pos beg-pos
) 2) fill-column
)))
1157 (goto-char end-marker
)
1158 (just-one-space -
1))
1159 (goto-char beg-marker
)
1162 (defun ruby-toggle-block ()
1163 "Toggle block type from do-end to braces or back.
1164 The block must begin on the current line or above it and end after the point.
1165 If the result is do-end block, it will always be multiline."
1167 (let ((start (point)) beg end
)
1170 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1173 (save-match-data (ruby-forward-sexp))
1176 (if (match-beginning 1)
1177 (ruby-brace-to-do-end beg end
)
1178 (ruby-do-end-to-brace beg end
)))
1179 (goto-char start
))))
1181 (declare-function ruby-syntax-propertize-heredoc
"ruby-mode" (limit))
1182 (declare-function ruby-syntax-enclosing-percent-literal
"ruby-mode" (limit))
1183 (declare-function ruby-syntax-propertize-percent-literal
"ruby-mode" (limit))
1185 (if (eval-when-compile (fboundp #'syntax-propertize-rules
))
1186 ;; New code that works independently from font-lock.
1189 (defconst ruby-percent-literal-beg-re
1190 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
1191 "Regexp to match the beginning of percent literal.")
1193 (defconst ruby-syntax-methods-before-regexp
1194 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1195 "assert_match" "Given" "Then" "When")
1196 "Methods that can take regexp as the first argument.
1197 It will be properly highlighted even when the call omits parens."))
1199 (defun ruby-syntax-propertize-function (start end
)
1200 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1202 (ruby-syntax-propertize-heredoc end
)
1203 (ruby-syntax-enclosing-percent-literal end
)
1205 (syntax-propertize-rules
1206 ;; $' $" $` .... are variables.
1207 ;; ?' ?" ?` are ascii codes.
1208 ("\\([?$]\\)[#\"'`]"
1209 (1 (unless (save-excursion
1210 ;; Not within a string.
1211 (nth 3 (syntax-ppss (match-beginning 0))))
1212 (string-to-syntax "\\"))))
1213 ;; Regexps: regexps are distinguished from division because
1214 ;; of the keyword, symbol, or method name before them.
1216 ;; Special tokens that can't be followed by a division operator.
1217 "\\(^\\|[[=(,~?:;<>]"
1218 ;; Control flow keywords and operators following bol or whitespace.
1219 "\\|\\(?:^\\|\\s \\)"
1220 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1221 "or" "not" "&&" "||"))
1222 ;; Method name from the list.
1224 (regexp-opt ruby-syntax-methods-before-regexp
)
1226 ;; The regular expression itself.
1227 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1228 (2 (string-to-syntax "\"/"))
1229 (3 (string-to-syntax "\"/")))
1230 ("^=en\\(d\\)\\_>" (1 "!"))
1231 ("^\\(=\\)begin\\_>" (1 "!"))
1232 ;; Handle here documents.
1233 ((concat ruby-here-doc-beg-re
".*\\(\n\\)")
1234 (7 (unless (ruby-singleton-class-p (match-beginning 0))
1235 (put-text-property (match-beginning 7) (match-end 7)
1236 'syntax-table
(string-to-syntax "\""))
1237 (ruby-syntax-propertize-heredoc end
))))
1238 ;; Handle percent literals: %w(), %q{}, etc.
1239 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re
)
1240 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end
)))))
1243 (defun ruby-syntax-propertize-heredoc (limit)
1244 (let ((ppss (syntax-ppss))
1246 (when (eq ?
\n (nth 3 ppss
))
1248 (goto-char (nth 8 ppss
))
1250 (while (re-search-forward ruby-here-doc-beg-re
1251 (line-end-position) t
)
1252 (unless (ruby-singleton-class-p (match-beginning 0))
1253 (push (concat (ruby-here-doc-end-match) "\n") res
))))
1254 (let ((start (point)))
1255 ;; With multiple openers on the same line, we don't know in which
1256 ;; part `start' is, so we have to go back to the beginning.
1258 (goto-char (nth 8 ppss
))
1259 (setq res
(nreverse res
)))
1260 (while (and res
(re-search-forward (pop res
) limit
'move
))
1262 (put-text-property (1- (point)) (point)
1263 'syntax-table
(string-to-syntax "\""))))
1264 ;; Make extra sure we don't move back, lest we could fall into an
1266 (if (< (point) start
) (goto-char start
))))))
1268 (defun ruby-syntax-enclosing-percent-literal (limit)
1269 (let ((state (syntax-ppss))
1271 ;; When already inside percent literal, re-propertize it.
1272 (when (eq t
(nth 3 state
))
1273 (goto-char (nth 8 state
))
1274 (when (looking-at ruby-percent-literal-beg-re
)
1275 (ruby-syntax-propertize-percent-literal limit
))
1276 (when (< (point) start
) (goto-char start
)))))
1278 (defun ruby-syntax-propertize-percent-literal (limit)
1279 (goto-char (match-beginning 2))
1280 ;; Not inside a simple string or comment.
1281 (when (eq t
(nth 3 (syntax-ppss)))
1282 (let* ((op (char-after))
1283 (ops (char-to-string op
))
1284 (cl (or (cdr (aref (syntax-table) op
))
1285 (cdr (assoc op
'((?
< . ?
>))))))
1286 parse-sexp-lookup-properties
)
1289 (if cl
; Paired delimiters.
1290 ;; Delimiter pairs of the same kind can be nested
1291 ;; inside the literal, as long as they are balanced.
1292 ;; Create syntax table that ignores other characters.
1293 (with-syntax-table (make-char-table 'syntax-table nil
)
1294 (modify-syntax-entry op
(concat "(" (char-to-string cl
)))
1295 (modify-syntax-entry cl
(concat ")" ops
))
1296 (modify-syntax-entry ?
\\ "\\")
1298 (narrow-to-region (point) limit
)
1299 (forward-list))) ; skip to the paired character
1300 ;; Single character delimiter.
1301 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1302 (regexp-quote ops
)) limit nil
))
1303 ;; Found the closing delimiter.
1304 (put-text-property (1- (point)) (point) 'syntax-table
1305 (string-to-syntax "|")))
1306 ;; Unclosed literal, leave the following text unpropertized.
1307 ((scan-error search-failed
) (goto-char limit
))))))
1310 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1311 ;; fallback on the old font-lock-syntactic-keywords stuff.
1313 (defconst ruby-here-doc-end-re
1314 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1315 "Regexp to match the end of heredocs.
1317 This will actually match any line with one or more characters.
1318 It's useful in that it divides up the match string so that
1319 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1321 (defun ruby-here-doc-beg-match ()
1322 "Return a regexp to find the beginning of a heredoc.
1324 This should only be called after matching against `ruby-here-doc-end-re'."
1325 (let ((contents (concat
1326 (regexp-quote (concat (match-string 2) (match-string 3)))
1327 (if (string= (match-string 3) "_") "\\B" "\\b"))))
1329 (let ((match (match-string 1)))
1330 (if (and match
(> (length match
) 0))
1331 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1332 (match-string 1) "\\)"
1333 contents
"\\(\\1\\|\\2\\)")
1334 (concat "-?\\([\"']\\|\\)" contents
"\\1"))))))
1336 (defconst ruby-font-lock-syntactic-keywords
1338 ;; the last $', $", $` in the respective string is not variable
1339 ;; the last ?', ?", ?` in the respective string is not ascii code
1340 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1343 ;; $' $" $` .... are variables
1344 ;; ?' ?" ?` are ascii codes
1345 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil
))
1347 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1350 ("^=en\\(d\\)\\_>" 1 "!")
1352 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1355 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1356 ;; Currently, the following case is highlighted incorrectly:
1365 ;; This is because all here-doc beginnings are highlighted before any endings,
1366 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1367 ;; it thinks <<BAR is part of a string so it's marked as well.
1369 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1370 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1371 ;; but I don't want to try that until we've got unit tests set up
1372 ;; to make sure I don't break anything else.
1373 (,(concat ruby-here-doc-beg-re
".*\\(\n\\)")
1374 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re
))
1375 (ruby-here-doc-beg-syntax))
1376 (,ruby-here-doc-end-re
3 (ruby-here-doc-end-syntax)))
1377 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
1379 (defun ruby-comment-beg-syntax ()
1380 "Return the syntax cell for a the first character of a =begin.
1381 See the definition of `ruby-font-lock-syntactic-keywords'.
1383 This returns a comment-delimiter cell as long as the =begin
1384 isn't in a string or another comment."
1385 (when (not (nth 3 (syntax-ppss)))
1386 (string-to-syntax "!")))
1388 (defun ruby-in-here-doc-p ()
1389 "Return whether or not the point is in a heredoc."
1391 (let ((old-point (point)) (case-fold-search nil
))
1394 (while (and (re-search-backward ruby-here-doc-beg-re nil t
)
1395 (not (ruby-singleton-class-p)))
1396 (if (not (or (ruby-in-ppss-context-p 'anything
)
1397 (ruby-here-doc-find-end old-point
)))
1398 (throw 'found-beg t
)))))))
1400 (defun ruby-here-doc-find-end (&optional limit
)
1401 "Expects the point to be on a line with one or more heredoc openers.
1402 Returns the buffer position at which all heredocs on the line
1403 are terminated, or nil if they aren't terminated before the
1404 buffer position `limit' or the end of the buffer."
1408 (let ((eol (point-at-eol))
1409 (case-fold-search nil
)
1410 ;; Fake match data such that (match-end 0) is at eol
1411 (end-match-data (progn (looking-at ".*$") (match-data)))
1412 beg-match-data end-re
)
1413 (while (re-search-forward ruby-here-doc-beg-re eol t
)
1414 (setq beg-match-data
(match-data))
1415 (setq end-re
(ruby-here-doc-end-match))
1417 (set-match-data end-match-data
)
1418 (goto-char (match-end 0))
1419 (unless (re-search-forward end-re limit t
) (throw 'done nil
))
1420 (setq end-match-data
(match-data))
1422 (set-match-data beg-match-data
)
1423 (goto-char (match-end 0)))
1424 (set-match-data end-match-data
)
1425 (goto-char (match-end 0))
1428 (defun ruby-here-doc-beg-syntax ()
1429 "Return the syntax cell for a line that may begin a heredoc.
1430 See the definition of `ruby-font-lock-syntactic-keywords'.
1432 This sets the syntax cell for the newline ending the line
1433 containing the heredoc beginning so that cases where multiple
1434 heredocs are started on one line are handled correctly."
1436 (goto-char (match-beginning 0))
1437 (unless (or (ruby-in-ppss-context-p 'non-heredoc
)
1438 (ruby-in-here-doc-p))
1439 (string-to-syntax "\""))))
1441 (defun ruby-here-doc-end-syntax ()
1442 "Return the syntax cell for a line that may end a heredoc.
1443 See the definition of `ruby-font-lock-syntactic-keywords'."
1444 (let ((pss (syntax-ppss)) (case-fold-search nil
))
1445 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1446 ;; so we can just give up.
1447 ;; This means we aren't doing a full-document search
1448 ;; every time we enter a character.
1449 (when (ruby-in-ppss-context-p 'heredoc pss
)
1451 (goto-char (nth 8 pss
)) ; Go to the beginning of heredoc.
1452 (let ((eol (point)))
1454 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t
) ; If there is a heredoc that matches this line...
1455 (not (ruby-in-ppss-context-p 'anything
)) ; And that's not inside a heredoc/string/comment...
1456 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1457 (not (re-search-forward ruby-here-doc-beg-re eol t
))))
1458 (string-to-syntax "\"")))))))
1460 (unless (functionp 'syntax-ppss
)
1461 (defun syntax-ppss (&optional pos
)
1462 (parse-partial-sexp (point-min) (or pos
(point)))))
1465 (defun ruby-in-ppss-context-p (context &optional ppss
)
1466 (let ((ppss (or ppss
(syntax-ppss (point)))))
1468 ((eq context
'anything
)
1471 ((eq context
'string
)
1473 ((eq context
'heredoc
)
1474 (eq ?
\n (nth 3 ppss
)))
1475 ((eq context
'non-heredoc
)
1476 (and (ruby-in-ppss-context-p 'anything
)
1477 (not (ruby-in-ppss-context-p 'heredoc
))))
1478 ((eq context
'comment
)
1482 "Internal error on `ruby-in-ppss-context-p': "
1483 "context name `" (symbol-name context
) "' is unknown"))))
1486 (if (featurep 'xemacs
)
1487 (put 'ruby-mode
'font-lock-defaults
1488 '((ruby-font-lock-keywords)
1491 (font-lock-syntactic-keywords
1492 . ruby-font-lock-syntactic-keywords
))))
1494 (defvar ruby-font-lock-syntax-table
1495 (let ((tbl (copy-syntax-table ruby-mode-syntax-table
)))
1496 (modify-syntax-entry ?_
"w" tbl
)
1498 "The syntax table to use for fontifying Ruby mode buffers.
1499 See `font-lock-syntax-table'.")
1501 (defconst ruby-font-lock-keywords
1504 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1505 1 font-lock-function-name-face
)
1508 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1552 ruby-keyword-end-re
)
1554 ;; here-doc beginnings
1555 (list ruby-here-doc-beg-re
0 'font-lock-string-face
)
1557 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1558 2 font-lock-variable-name-face
)
1560 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1561 2 font-lock-constant-face
)
1563 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1564 1 font-lock-variable-name-face
)
1565 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1566 0 font-lock-variable-name-face
)
1568 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1569 2 font-lock-type-face
)
1570 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face
)
1571 ;; expression expansion
1572 '(ruby-match-expression-expansion
1573 0 font-lock-variable-name-face t
)
1574 ;; warn lower camel case
1575 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1576 ; 0 font-lock-warning-face)
1578 "Additional expressions to highlight in Ruby mode.")
1580 (defun ruby-match-expression-expansion (limit)
1581 (when (re-search-forward "[^\\]\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)" limit
'move
)
1582 (or (ruby-in-ppss-context-p 'string
)
1583 (ruby-match-expression-expansion limit
))))
1586 (define-derived-mode ruby-mode prog-mode
"Ruby"
1587 "Major mode for editing Ruby scripts.
1588 \\[ruby-indent-line] properly indents subexpressions of multi-line
1589 class, module, def, if, while, for, do, and case statements, taking
1590 nesting into account.
1592 The variable `ruby-indent-level' controls the amount of indentation.
1595 (ruby-mode-variables)
1597 (set (make-local-variable 'imenu-create-index-function
)
1598 'ruby-imenu-create-index
)
1599 (set (make-local-variable 'add-log-current-defun-function
)
1600 'ruby-add-log-current-method
)
1601 (set (make-local-variable 'beginning-of-defun-function
)
1602 'ruby-beginning-of-defun
)
1603 (set (make-local-variable 'end-of-defun-function
)
1607 (cond ((boundp 'before-save-hook
) 'before-save-hook
)
1608 ((boundp 'write-contents-functions
) 'write-contents-functions
)
1609 ((boundp 'write-contents-hooks
) 'write-contents-hooks
))
1610 'ruby-mode-set-encoding nil
'local
)
1612 (set (make-local-variable 'electric-indent-chars
)
1613 (append '(?\
{ ?\
}) electric-indent-chars
))
1615 (set (make-local-variable 'font-lock-defaults
)
1616 '((ruby-font-lock-keywords) nil nil
))
1617 (set (make-local-variable 'font-lock-keywords
)
1618 ruby-font-lock-keywords
)
1619 (set (make-local-variable 'font-lock-syntax-table
)
1620 ruby-font-lock-syntax-table
)
1622 (if (eval-when-compile (fboundp 'syntax-propertize-rules
))
1623 (set (make-local-variable 'syntax-propertize-function
)
1624 #'ruby-syntax-propertize-function
)
1625 (set (make-local-variable 'font-lock-syntactic-keywords
)
1626 ruby-font-lock-syntactic-keywords
)))
1628 ;;; Invoke ruby-mode when appropriate
1631 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.rb\\'") 'ruby-mode
))
1634 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
1635 (add-to-list 'interpreter-mode-alist
(cons (purecopy name
) 'ruby-mode
)))
1637 (provide 'ruby-mode
)
1639 ;;; ruby-mode.el ends here