ruby-mode: Align def...end to the beginning of statement
[bpt/emacs.git] / lisp / progmodes / ruby-mode.el
CommitLineData
c7fbbc3c
CY
1;;; ruby-mode.el --- Major mode for editing Ruby files
2
ba318903 3;; Copyright (C) 1994-2014 Free Software Foundation, Inc.
c7fbbc3c 4
c5220417
GM
5;; Authors: Yukihiro Matsumoto
6;; Nobuyoshi Nakada
c7fbbc3c
CY
7;; URL: http://www.emacswiki.org/cgi-bin/wiki/RubyMode
8;; Created: Fri Feb 4 14:49:13 JST 1994
9;; Keywords: languages ruby
ea0857a1 10;; Version: 1.2
c7fbbc3c
CY
11
12;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27;;; Commentary:
28
29;; Provides font-locking, indentation support, and navigation for Ruby code.
30;;
31;; If you're installing manually, you should add this to your .emacs
32;; file after putting it on your load path:
33;;
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))
37;;
38;; Still needs more docstrings; search below for TODO.
39
40;;; Code:
41
44a41a47
KR
42(defgroup ruby nil
43 "Major mode for editing Ruby code."
44 :prefix "ruby-"
45 :group 'languages)
46
c7fbbc3c
CY
47(defconst ruby-block-beg-keywords
48 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
49 "Keywords at the beginning of blocks.")
50
51(defconst ruby-block-beg-re
52 (regexp-opt ruby-block-beg-keywords)
53 "Regexp to match the beginning of blocks.")
54
55(defconst ruby-non-block-do-re
ac72c08d 56 (regexp-opt '("while" "until" "for" "rescue") 'symbols)
c7fbbc3c
CY
57 "Regexp to match keywords that nest without blocks.")
58
59(defconst ruby-indent-beg-re
db590ef6
DG
60 (concat "^\\(\\s *" (regexp-opt '("class" "module" "def")) "\\|"
61 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin"))
62 "\\)\\_>")
c7fbbc3c
CY
63 "Regexp to match where the indentation gets deeper.")
64
65(defconst ruby-modifier-beg-keywords
66 '("if" "unless" "while" "until")
67 "Modifiers that are the same as the beginning of blocks.")
68
69(defconst ruby-modifier-beg-re
70 (regexp-opt ruby-modifier-beg-keywords)
71 "Regexp to match modifiers same as the beginning of blocks.")
72
73(defconst ruby-modifier-re
74 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords))
75 "Regexp to match modifiers.")
76
77(defconst ruby-block-mid-keywords
78 '("then" "else" "elsif" "when" "rescue" "ensure")
79 "Keywords where the indentation gets shallower in middle of block statements.")
80
81(defconst ruby-block-mid-re
82 (regexp-opt ruby-block-mid-keywords)
83 "Regexp to match where the indentation gets shallower in middle of block statements.")
84
85(defconst ruby-block-op-keywords
86 '("and" "or" "not")
87 "Regexp to match boolean keywords.")
88
89(defconst ruby-block-hanging-re
90 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords))
91 "Regexp to match hanging block modifiers.")
92
39675016 93(defconst ruby-block-end-re "\\_<end\\_>")
c7fbbc3c 94
8f06acce
DG
95(defconst ruby-defun-beg-re
96 '"\\(def\\|class\\|module\\)"
97 "Regexp to match the beginning of a defun, in the general sense.")
98
bb808526
DG
99(defconst ruby-singleton-class-re
100 "class\\s *<<"
101 "Regexp to match the beginning of a singleton class context.")
102
cf38dd42
SM
103(eval-and-compile
104 (defconst ruby-here-doc-beg-re
c7fbbc3c 105 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
c62792e7
DG
106 "Regexp to match the beginning of a heredoc.")
107
108 (defconst ruby-expression-expansion-re
1a0a0a8a 109 "\\(?:[^\\]\\|\\=\\)\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)"))
c7fbbc3c
CY
110
111(defun ruby-here-doc-end-match ()
112 "Return a regexp to find the end of a heredoc.
113
114This should only be called after matching against `ruby-here-doc-beg-re'."
115 (concat "^"
116 (if (match-string 2) "[ \t]*" nil)
117 (regexp-quote
118 (or (match-string 4)
119 (match-string 5)
120 (match-string 6)))))
121
c7fbbc3c 122(defconst ruby-delimiter
39675016 123 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
c7fbbc3c 124 ruby-block-beg-re
39675016 125 "\\)\\_>\\|" ruby-block-end-re
c7fbbc3c
CY
126 "\\|^=begin\\|" ruby-here-doc-beg-re))
127
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.")
132
578c21bc 133(defconst ruby-operator-re "[-,.+*/%&|^~=<>:]\\|\\\\$"
c7fbbc3c
CY
134 "Regexp to match operators.")
135
136(defconst ruby-symbol-chars "a-zA-Z0-9_"
137 "List of characters that symbol names may contain.")
88527bc0 138
c7fbbc3c
CY
139(defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]")
140 "Regexp to match symbols.")
141
34d1a133 142(defvar ruby-use-smie t)
a9e4425b 143
c7fbbc3c
CY
144(defvar ruby-mode-map
145 (let ((map (make-sparse-keymap)))
a9e4425b
SM
146 (unless ruby-use-smie
147 (define-key map (kbd "M-C-b") 'ruby-backward-sexp)
148 (define-key map (kbd "M-C-f") 'ruby-forward-sexp)
149 (define-key map (kbd "M-C-q") 'ruby-indent-exp))
8c1ae481
DG
150 (when ruby-use-smie
151 (define-key map (kbd "M-C-d") 'smie-down-list))
c7fbbc3c
CY
152 (define-key map (kbd "M-C-p") 'ruby-beginning-of-block)
153 (define-key map (kbd "M-C-n") 'ruby-end-of-block)
0d9e2599 154 (define-key map (kbd "C-c {") 'ruby-toggle-block)
c7fbbc3c 155 map)
ee61fe97 156 "Keymap used in Ruby mode.")
c7fbbc3c 157
f73754c9
JA
158(easy-menu-define
159 ruby-mode-menu
160 ruby-mode-map
161 "Ruby Mode Menu"
162 '("Ruby"
963ce636
DG
163 ["Beginning of Block" ruby-beginning-of-block t]
164 ["End of Block" ruby-end-of-block t]
f73754c9
JA
165 ["Toggle Block" ruby-toggle-block t]
166 "--"
167 ["Backward Sexp" ruby-backward-sexp
963ce636
DG
168 :visible (not ruby-use-smie)]
169 ["Backward Sexp" backward-sexp
170 :visible ruby-use-smie]
f73754c9 171 ["Forward Sexp" ruby-forward-sexp
963ce636
DG
172 :visible (not ruby-use-smie)]
173 ["Forward Sexp" forward-sexp
174 :visible ruby-use-smie]
175 ["Indent Sexp" ruby-indent-exp
176 :visible (not ruby-use-smie)]
177 ["Indent Sexp" prog-indent-sexp
178 :visible ruby-use-smie]))
f73754c9 179
c7fbbc3c
CY
180(defvar ruby-mode-syntax-table
181 (let ((table (make-syntax-table)))
182 (modify-syntax-entry ?\' "\"" table)
183 (modify-syntax-entry ?\" "\"" table)
184 (modify-syntax-entry ?\` "\"" table)
185 (modify-syntax-entry ?# "<" table)
186 (modify-syntax-entry ?\n ">" table)
187 (modify-syntax-entry ?\\ "\\" table)
188 (modify-syntax-entry ?$ "." table)
c7fbbc3c 189 (modify-syntax-entry ?_ "_" table)
39675016 190 (modify-syntax-entry ?: "_" table)
c7fbbc3c
CY
191 (modify-syntax-entry ?< "." table)
192 (modify-syntax-entry ?> "." table)
193 (modify-syntax-entry ?& "." table)
194 (modify-syntax-entry ?| "." table)
195 (modify-syntax-entry ?% "." table)
196 (modify-syntax-entry ?= "." table)
197 (modify-syntax-entry ?/ "." table)
198 (modify-syntax-entry ?+ "." table)
199 (modify-syntax-entry ?* "." table)
200 (modify-syntax-entry ?- "." table)
201 (modify-syntax-entry ?\; "." table)
202 (modify-syntax-entry ?\( "()" table)
203 (modify-syntax-entry ?\) ")(" table)
204 (modify-syntax-entry ?\{ "(}" table)
205 (modify-syntax-entry ?\} "){" table)
206 (modify-syntax-entry ?\[ "(]" table)
207 (modify-syntax-entry ?\] ")[" table)
208 table)
ee61fe97 209 "Syntax table to use in Ruby mode.")
c7fbbc3c
CY
210
211(defcustom ruby-indent-tabs-mode nil
ee61fe97 212 "Indentation can insert tabs in Ruby mode if this is non-nil."
983d0df5
BB
213 :type 'boolean
214 :group 'ruby
215 :safe 'booleanp)
c7fbbc3c
CY
216
217(defcustom ruby-indent-level 2
ee61fe97 218 "Indentation of Ruby statements."
983d0df5
BB
219 :type 'integer
220 :group 'ruby
221 :safe 'integerp)
c7fbbc3c 222
34d1a133 223(defcustom ruby-comment-column (default-value 'comment-column)
c7fbbc3c 224 "Indentation column of comments."
983d0df5
BB
225 :type 'integer
226 :group 'ruby
227 :safe 'integerp)
c7fbbc3c 228
fb3d479c
DG
229(defconst ruby-alignable-keywords '(if while unless until begin case for def)
230 "Keywords that can be used in `ruby-align-to-stmt-keywords'.")
231
232(defcustom ruby-align-to-stmt-keywords '(def)
65a1da00
DG
233 "Keywords after which we align the expression body to statement.
234
b520f210
DG
235When nil, an expression that begins with one these keywords is
236indented to the column of the keyword. Example:
237
238 tee = if foo
239 bar
240 else
241 qux
242 end
243
244If this value is t or contains a symbol with the name of given
245keyword, the expression is indented to align to the beginning of
246the statement:
247
248 tee = if foo
249 bar
250 else
251 qux
252 end
253
254Only has effect when `ruby-use-smie' is t.
255"
fb3d479c 256 :type `(choice
b520f210
DG
257 (const :tag "None" nil)
258 (const :tag "All" t)
259 (repeat :tag "User defined"
fb3d479c
DG
260 (choice ,@(mapcar
261 (lambda (kw) (list 'const kw))
262 ruby-alignable-keywords))))
b520f210
DG
263 :group 'ruby
264 :safe 'listp
265 :version "24.4")
266
c7fbbc3c
CY
267(defcustom ruby-deep-arglist t
268 "Deep indent lists in parenthesis when non-nil.
b520f210
DG
269Also ignores spaces after parenthesis when `space'.
270Only has effect when `ruby-use-smie' is nil."
983d0df5
BB
271 :type 'boolean
272 :group 'ruby
273 :safe 'booleanp)
c7fbbc3c 274
9c5a5c77 275;; FIXME Woefully under documented. What is the point of the last `t'?.
c7fbbc3c 276(defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
ee61fe97
JB
277 "Deep indent lists in parenthesis when non-nil.
278The value t means continuous line.
b520f210
DG
279Also ignores spaces after parenthesis when `space'.
280Only has effect when `ruby-use-smie' is nil."
9c5a5c77
GM
281 :type '(choice (const nil)
282 character
283 (repeat (choice character
284 (cons character (choice (const nil)
285 (const t)))
286 (const t) ; why?
287 )))
c7fbbc3c
CY
288 :group 'ruby)
289
290(defcustom ruby-deep-indent-paren-style 'space
b520f210
DG
291 "Default deep indent style.
292Only has effect when `ruby-use-smie' is nil."
9c5a5c77
GM
293 :type '(choice (const t) (const nil) (const space))
294 :group 'ruby)
c7fbbc3c 295
e70181b8
AM
296(defcustom ruby-encoding-map
297 '((us-ascii . nil) ;; Do not put coding: us-ascii
298 (shift-jis . cp932) ;; Emacs charset name of Shift_JIS
299 (shift_jis . cp932) ;; MIME charset name of Shift_JIS
300 (japanese-cp932 . cp932)) ;; Emacs charset name of CP932
301 "Alist to map encoding name from Emacs to Ruby.
302Associating an encoding name with nil means it needs not be
303explicitly declared in magic comment."
304 :type '(repeat (cons (symbol :tag "From") (symbol :tag "To")))
c7fbbc3c
CY
305 :group 'ruby)
306
307(defcustom ruby-insert-encoding-magic-comment t
6c1bf086
BB
308 "Insert a magic Ruby encoding comment upon save if this is non-nil.
309The encoding will be auto-detected. The format of the encoding comment
310is customizable via `ruby-encoding-magic-comment-style'.
311
312When set to `always-utf8' an utf-8 comment will always be added,
313even if it's not required."
c7fbbc3c
CY
314 :type 'boolean :group 'ruby)
315
71731c03
BB
316(defcustom ruby-encoding-magic-comment-style 'ruby
317 "The style of the magic encoding comment to use."
318 :type '(choice
319 (const :tag "Emacs Style" emacs)
320 (const :tag "Ruby Style" ruby)
321 (const :tag "Custom Style" custom))
15ba2182
BB
322 :group 'ruby
323 :version "24.4")
71731c03 324
638af3a1 325(defcustom ruby-custom-encoding-magic-comment-template "# encoding: %s"
d19ffd64
BB
326 "A custom encoding comment template.
327It is used when `ruby-encoding-magic-comment-style' is set to `custom'."
71731c03 328 :type 'string
15ba2182
BB
329 :group 'ruby
330 :version "24.4")
71731c03 331
c7fbbc3c
CY
332(defcustom ruby-use-encoding-map t
333 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
334 :type 'boolean :group 'ruby)
335
a9e4425b
SM
336;;; SMIE support
337
338(require 'smie)
339
cfef16c0
DG
340;; Here's a simplified BNF grammar, for reference:
341;; http://www.cse.buffalo.edu/~regan/cse305/RubyBNF.pdf
a9e4425b 342(defconst ruby-smie-grammar
a9e4425b 343 (smie-prec2->grammar
cfef16c0
DG
344 (smie-merge-prec2s
345 (smie-bnf->prec2
346 '((id)
347 (insts (inst) (insts ";" insts))
1f923923
DG
348 (inst (exp) (inst "iuwu-mod" exp)
349 ;; Somewhat incorrect (both can be used multiple times),
350 ;; but avoids lots of conflicts:
351 (exp "and" exp) (exp "or" exp))
cfef16c0 352 (exp (exp1) (exp "," exp) (exp "=" exp)
18cacc39 353 (id " @ " exp)
b420ccfc 354 (exp "." id))
cfef16c0
DG
355 (exp1 (exp2) (exp2 "?" exp1 ":" exp1))
356 (exp2 ("def" insts "end")
357 ("begin" insts-rescue-insts "end")
358 ("do" insts "end")
359 ("class" insts "end") ("module" insts "end")
360 ("for" for-body "end")
361 ("[" expseq "]")
362 ("{" hashvals "}")
363 ("{" insts "}")
364 ("while" insts "end")
365 ("until" insts "end")
366 ("unless" insts "end")
367 ("if" if-body "end")
368 ("case" cases "end"))
7b08f97e 369 (formal-params ("opening-|" exp "closing-|"))
cfef16c0
DG
370 (for-body (for-head ";" insts))
371 (for-head (id "in" exp))
7b08f97e 372 (cases (exp "then" insts)
cfef16c0
DG
373 (cases "when" cases) (insts "else" insts))
374 (expseq (exp) );;(expseq "," expseq)
375 (hashvals (id "=>" exp1) (hashvals "," hashvals))
376 (insts-rescue-insts (insts)
377 (insts-rescue-insts "rescue" insts-rescue-insts)
378 (insts-rescue-insts "ensure" insts-rescue-insts))
379 (itheni (insts) (exp "then" insts))
380 (ielsei (itheni) (itheni "else" insts))
381 (if-body (ielsei) (if-body "elsif" if-body)))
382 '((nonassoc "in") (assoc ";") (right " @ ")
18cacc39 383 (assoc ",") (right "=") (assoc "."))
cfef16c0
DG
384 '((assoc "when"))
385 '((assoc "elsif"))
386 '((assoc "rescue" "ensure"))
387 '((assoc ",")))
388
389 (smie-precs->prec2
390 '((right "=")
391 (right "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^="
392 "<<=" ">>=" "&&=" "||=")
393 (left ".." "...")
394 (left "+" "-")
395 (left "*" "/" "%" "**")
1f923923 396 (left "&&" "||")
7b08f97e 397 (left "^" "&" "|")
cfef16c0
DG
398 (nonassoc "<=>")
399 (nonassoc ">" ">=" "<" "<=")
400 (nonassoc "==" "===" "!=")
401 (nonassoc "=~" "!~")
1f923923 402 (left "<<" ">>"))))))
a9e4425b
SM
403
404(defun ruby-smie--bosp ()
405 (save-excursion (skip-chars-backward " \t")
c8c605ac 406 (or (bolp) (memq (char-before) '(?\; ?=)))))
a9e4425b
SM
407
408(defun ruby-smie--implicit-semi-p ()
409 (save-excursion
410 (skip-chars-backward " \t")
411 (not (or (bolp)
34d1a133 412 (and (memq (char-before)
f2351498 413 '(?\; ?- ?+ ?* ?/ ?: ?. ?, ?\[ ?\( ?\\ ?& ?> ?< ?%
ad16897c 414 ?~ ?^))
8212d9c0
DG
415 ;; Not the end of a regexp or a percent literal.
416 (not (memq (car (syntax-after (1- (point)))) '(7 15))))
b68e2926
DG
417 (and (eq (char-before) ?\?)
418 (equal (save-excursion (ruby-smie--backward-token)) "?"))
419 (and (eq (char-before) ?=)
420 (string-match "\\`\\s." (save-excursion
421 (ruby-smie--backward-token))))
1629a329 422 (and (eq (char-before) ?|)
7b08f97e
DG
423 (member (save-excursion (ruby-smie--backward-token))
424 '("|" "||")))
238150c8 425 (and (eq (car (syntax-after (1- (point)))) 2)
1d1c86da
DG
426 (member (save-excursion (ruby-smie--backward-token))
427 '("iuwu-mod" "and" "or")))
0ea1599d
DG
428 (save-excursion
429 (forward-comment 1)
430 (eq (char-after) ?.))))))
f069bba8 431
7ccae3b1
SM
432(defun ruby-smie--redundant-do-p (&optional skip)
433 (save-excursion
434 (if skip (backward-word 1))
b68e2926 435 (member (nth 2 (smie-backward-sexp ";")) '("while" "until" "for"))))
7ccae3b1 436
f069bba8
SM
437(defun ruby-smie--opening-pipe-p ()
438 (save-excursion
439 (if (eq ?| (char-before)) (forward-char -1))
440 (skip-chars-backward " \t\n")
441 (or (eq ?\{ (char-before))
442 (looking-back "\\_<do" (- (point) 2)))))
a9e4425b 443
7b08f97e
DG
444(defun ruby-smie--closing-pipe-p ()
445 (save-excursion
446 (if (eq ?| (char-before)) (forward-char -1))
447 (and (re-search-backward "|" (line-beginning-position) t)
448 (ruby-smie--opening-pipe-p))))
449
650fa7bf
SM
450(defun ruby-smie--args-separator-p (pos)
451 (and
8c1ae481 452 (< pos (line-end-position))
1eda1d8d 453 (or (eq (char-syntax (preceding-char)) '?w)
bae91342
DG
454 ;; FIXME: Check that the preceding token is not a keyword.
455 ;; This isn't very important most of the time, though.
1eda1d8d
DG
456 (and (memq (preceding-char) '(?! ??))
457 (eq (char-syntax (char-before (1- (point)))) '?w)))
1d1c86da
DG
458 (save-excursion
459 (goto-char pos)
460 (or (and (eq (char-syntax (char-after)) ?w)
ad16897c
SM
461 (not (looking-at (regexp-opt '("unless" "if" "while" "until" "or"
462 "else" "elsif" "do" "end" "and")
1d1c86da 463 'symbols))))
7e1549c9
DG
464 (memq (car (syntax-after pos)) '(7 15))
465 (looking-at "[([]\\|[-+!~]\\sw\\|:\\(?:\\sw\\|\\s.\\)")))))
650fa7bf 466
18cacc39 467(defun ruby-smie--at-dot-call ()
ee4282cd 468 (and (eq ?w (char-syntax (following-char)))
18cacc39
DG
469 (eq (char-before) ?.)
470 (not (eq (char-before (1- (point))) ?.))))
7ccae3b1 471
a9e4425b 472(defun ruby-smie--forward-token ()
650fa7bf
SM
473 (let ((pos (point)))
474 (skip-chars-forward " \t")
475 (cond
7e1549c9
DG
476 ((and (looking-at "\n") (looking-at "\\s\"")) ;A heredoc.
477 ;; Tokenize the whole heredoc as semicolon.
478 (goto-char (scan-sexps (point) 1))
479 ";")
650fa7bf
SM
480 ((and (looking-at "[\n#]")
481 (ruby-smie--implicit-semi-p)) ;Only add implicit ; when needed.
482 (if (eolp) (forward-char 1) (forward-comment 1))
483 ";")
484 (t
485 (forward-comment (point-max))
486 (cond
650fa7bf
SM
487 ((and (< pos (point))
488 (save-excursion
489 (ruby-smie--args-separator-p (prog1 (point) (goto-char pos)))))
490 " @ ")
7e1549c9
DG
491 ((looking-at ":\\s.+")
492 (goto-char (match-end 0)) (match-string 0)) ;bug#15208.
493 ((looking-at "\\s\"") "") ;A string.
650fa7bf 494 (t
18cacc39
DG
495 (let ((dot (ruby-smie--at-dot-call))
496 (tok (smie-default-forward-token)))
497 (when dot
498 (setq tok (concat "." tok)))
7ccae3b1 499 (cond
650fa7bf
SM
500 ((member tok '("unless" "if" "while" "until"))
501 (if (save-excursion (forward-word -1) (ruby-smie--bosp))
502 tok "iuwu-mod"))
ffa2df72 503 ((string-match-p "\\`|[*&]?\\'" tok)
1f923923
DG
504 (forward-char (- 1 (length tok)))
505 (setq tok "|")
7b08f97e
DG
506 (cond
507 ((ruby-smie--opening-pipe-p) "opening-|")
508 ((ruby-smie--closing-pipe-p) "closing-|")
509 (t tok)))
650fa7bf
SM
510 ((and (equal tok "") (looking-at "\\\\\n"))
511 (goto-char (match-end 0)) (ruby-smie--forward-token))
512 ((equal tok "do")
513 (cond
514 ((not (ruby-smie--redundant-do-p 'skip)) tok)
515 ((> (save-excursion (forward-comment (point-max)) (point))
516 (line-end-position))
517 (ruby-smie--forward-token)) ;Fully redundant.
518 (t ";")))
650fa7bf 519 (t tok)))))))))
a9e4425b
SM
520
521(defun ruby-smie--backward-token ()
522 (let ((pos (point)))
523 (forward-comment (- (point)))
34d1a133
SM
524 (cond
525 ((and (> pos (line-end-position)) (ruby-smie--implicit-semi-p))
526 (skip-chars-forward " \t") ";")
dca01b09
DG
527 ((and (bolp) (not (bobp))) ;Presumably a heredoc.
528 ;; Tokenize the whole heredoc as semicolon.
529 (goto-char (scan-sexps (point) -1))
530 ";")
650fa7bf
SM
531 ((and (> pos (point)) (not (bolp))
532 (ruby-smie--args-separator-p pos))
533 ;; We have "ID SPC ID", which is a method call, but it binds less tightly
534 ;; than commas, since a method call can also be "ID ARG1, ARG2, ARG3".
535 ;; In some textbooks, "e1 @ e2" is used to mean "call e1 with arg e2".
536 " @ ")
34d1a133 537 (t
18cacc39
DG
538 (let ((tok (smie-default-backward-token))
539 (dot (ruby-smie--at-dot-call)))
540 (when dot
238150c8 541 (setq tok (concat "." tok)))
f069bba8
SM
542 (when (and (eq ?: (char-before)) (string-match "\\`\\s." tok))
543 (forward-char -1) (setq tok (concat ":" tok))) ;; bug#15208.
a9e4425b
SM
544 (cond
545 ((member tok '("unless" "if" "while" "until"))
546 (if (ruby-smie--bosp)
547 tok "iuwu-mod"))
f069bba8 548 ((equal tok "|")
7b08f97e
DG
549 (cond
550 ((ruby-smie--opening-pipe-p) "opening-|")
551 ((ruby-smie--closing-pipe-p) "closing-|")
552 (t tok)))
ffa2df72 553 ((string-match-p "\\`|[*&]\\'" tok)
1f923923
DG
554 (forward-char 1)
555 (substring tok 1))
34d1a133
SM
556 ((and (equal tok "") (eq ?\\ (char-before)) (looking-at "\n"))
557 (forward-char -1) (ruby-smie--backward-token))
7ccae3b1
SM
558 ((equal tok "do")
559 (cond
560 ((not (ruby-smie--redundant-do-p)) tok)
561 ((> (save-excursion (forward-word 1)
562 (forward-comment (point-max)) (point))
563 (line-end-position))
564 (ruby-smie--backward-token)) ;Fully redundant.
565 (t ";")))
34d1a133 566 (t tok)))))))
a9e4425b 567
1f923923
DG
568(defun ruby-smie--indent-to-stmt ()
569 (save-excursion
da3b328d
DG
570 (smie-backward-sexp ";")
571 (cons 'column (smie-indent-virtual))))
1f923923 572
b520f210
DG
573(defun ruby-smie--indent-to-stmt-p (keyword)
574 (or (eq t ruby-align-to-stmt-keywords)
575 (memq (intern keyword) ruby-align-to-stmt-keywords)))
576
a9e4425b
SM
577(defun ruby-smie-rules (kind token)
578 (pcase (cons kind token)
579 (`(:elem . basic) ruby-indent-level)
34d1a133
SM
580 ;; "foo" "bar" is the concatenation of the two strings, so the second
581 ;; should be aligned with the first.
582 (`(:elem . args) (if (looking-at "\\s\"") 0))
583 ;; (`(:after . ",") (smie-rule-separator kind))
7790a270 584 (`(:before . ";")
8c1ae481
DG
585 (cond
586 ((smie-rule-parent-p "def" "begin" "do" "class" "module" "for"
587 "while" "until" "unless"
588 "if" "then" "elsif" "else" "when"
369bbf71 589 "rescue" "ensure" "{")
8c1ae481 590 (smie-rule-parent ruby-indent-level))
8c1ae481
DG
591 ;; For (invalid) code between switch and case.
592 ;; (if (smie-parent-p "switch") 4)
bc4aaa31 593 ))
34d1a133 594 (`(:before . ,(or `"(" `"[" `"{"))
8c1ae481
DG
595 (cond
596 ((and (equal token "{")
1f923923
DG
597 (not (smie-rule-prev-p "(" "{" "[" "," "=>" "=" "return" ";"))
598 (save-excursion
599 (forward-comment -1)
600 (not (eq (preceding-char) ?:))))
8c1ae481 601 ;; Curly block opener.
1f923923 602 (ruby-smie--indent-to-stmt))
8c1ae481
DG
603 ((smie-rule-hanging-p)
604 ;; Treat purely syntactic block-constructs as being part of their parent,
7e1549c9
DG
605 ;; when the opening token is hanging and the parent is not an
606 ;; open-paren.
607 (cond
608 ((eq (car (smie-indent--parent)) t) nil)
609 ;; When after `.', let's always de-indent,
610 ;; because when `.' is inside the line, the
611 ;; additional indentation from it looks out of place.
612 ((smie-rule-parent-p ".") (smie-rule-parent (- ruby-indent-level)))
613 (t (smie-rule-parent))))))
276bc333
DG
614 (`(:after . ,(or `"(" "[" "{"))
615 ;; FIXME: Shouldn't this be the default behavior of
616 ;; `smie-indent-after-keyword'?
617 (save-excursion
618 (forward-char 1)
619 (skip-chars-forward " \t")
620 ;; `smie-rule-hanging-p' is not good enough here,
660efa1a 621 ;; because we want to reject hanging tokens at bol, too.
276bc333
DG
622 (unless (or (eolp) (forward-comment 1))
623 (cons 'column (current-column)))))
1f923923 624 (`(:before . "do") (ruby-smie--indent-to-stmt))
7e1549c9 625 (`(:before . ".") ruby-indent-level)
65a1da00 626 (`(:after . "=>") ruby-indent-level)
ce41edb4
DG
627 (`(:before . ,(or `"else" `"then" `"elsif" `"rescue" `"ensure"))
628 (smie-rule-parent))
629 (`(:before . "when")
b520f210
DG
630 ;; Align to the previous `when', but look up the virtual
631 ;; indentation of `case'.
632 (if (smie-rule-sibling-p) 0 (smie-rule-parent)))
1d1c86da
DG
633 (`(:after . ,(or "=" "iuwu-mod" "+" "-" "*" "/" "&&" "||" "%" "**" "^" "&"
634 "<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>"
7b08f97e 635 "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|"
1d1c86da 636 "<<=" ">>=" "&&=" "||=" "and" "or"))
7e1549c9
DG
637 (and (smie-rule-parent-p ";" nil)
638 (smie-indent--hanging-p)
639 ruby-indent-level))
35b249a6 640 (`(:after . ,(or "?" ":")) ruby-indent-level)
fb3d479c 641 (`(:before . ,(guard (memq (intern-soft token) ruby-alignable-keywords)))
6cddebc1 642 (when (not (ruby--at-indentation-p))
b520f210
DG
643 (if (ruby-smie--indent-to-stmt-p token)
644 (ruby-smie--indent-to-stmt)
645 (cons 'column (current-column)))))
7ccae3b1 646 ))
a9e4425b 647
6cddebc1
DG
648(defun ruby--at-indentation-p (&optional point)
649 (save-excursion
650 (unless point (setq point (point)))
651 (forward-line 0)
652 (skip-chars-forward " \t")
653 (eq (point) point)))
654
c7fbbc3c
CY
655(defun ruby-imenu-create-index-in-block (prefix beg end)
656 "Create an imenu index of methods inside a block."
657 (let ((index-alist '()) (case-fold-search nil)
658 name next pos decl sing)
659 (goto-char beg)
660 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
661 (setq sing (match-beginning 3))
662 (setq decl (match-string 5))
663 (setq next (match-end 0))
664 (setq name (or (match-string 4) (match-string 6)))
665 (setq pos (match-beginning 0))
666 (cond
667 ((string= "alias" decl)
668 (if prefix (setq name (concat prefix name)))
669 (push (cons name pos) index-alist))
670 ((string= "def" decl)
671 (if prefix
672 (setq name
673 (cond
674 ((string-match "^self\." name)
675 (concat (substring prefix 0 -1) (substring name 4)))
676 (t (concat prefix name)))))
677 (push (cons name pos) index-alist)
678 (ruby-accurate-end-of-block end))
679 (t
680 (if (string= "self" name)
681 (if prefix (setq name (substring prefix 0 -1)))
682 (if prefix (setq name (concat (substring prefix 0 -1) "::" name)))
683 (push (cons name pos) index-alist))
684 (ruby-accurate-end-of-block end)
685 (setq beg (point))
686 (setq index-alist
687 (nconc (ruby-imenu-create-index-in-block
688 (concat name (if sing "." "#"))
689 next beg) index-alist))
690 (goto-char beg))))
691 index-alist))
692
693(defun ruby-imenu-create-index ()
694 "Create an imenu index of all methods in the buffer."
695 (nreverse (ruby-imenu-create-index-in-block nil (point-min) nil)))
696
697(defun ruby-accurate-end-of-block (&optional end)
ff8c9764 698 "Jump to the end of the current block or END, whichever is closer."
c7fbbc3c
CY
699 (let (state
700 (end (or end (point-max))))
ff8c9764
DG
701 (if ruby-use-smie
702 (save-restriction
703 (back-to-indentation)
704 (narrow-to-region (point) end)
705 (smie-forward-sexp))
706 (while (and (setq state (apply 'ruby-parse-partial end state))
707 (>= (nth 2 state) 0) (< (point) end))))))
c7fbbc3c
CY
708
709(defun ruby-mode-variables ()
ee61fe97 710 "Set up initial buffer-local variables for Ruby mode."
c7fbbc3c 711 (setq indent-tabs-mode ruby-indent-tabs-mode)
a9e4425b
SM
712 (if ruby-use-smie
713 (smie-setup ruby-smie-grammar #'ruby-smie-rules
714 :forward-token #'ruby-smie--forward-token
715 :backward-token #'ruby-smie--backward-token)
a3996a2e
BB
716 (setq-local indent-line-function 'ruby-indent-line))
717 (setq-local require-final-newline t)
718 (setq-local comment-start "# ")
719 (setq-local comment-end "")
720 (setq-local comment-column ruby-comment-column)
721 (setq-local comment-start-skip "#+ *")
722 (setq-local parse-sexp-ignore-comments t)
723 (setq-local parse-sexp-lookup-properties t)
724 (setq-local paragraph-start (concat "$\\|" page-delimiter))
725 (setq-local paragraph-separate paragraph-start)
726 (setq-local paragraph-ignore-fill-prefix t))
c7fbbc3c 727
6c1bf086
BB
728(defun ruby--insert-coding-comment (encoding)
729 "Insert a magic coding comment for ENCODING.
730The style of the comment is controlled by `ruby-encoding-magic-comment-style'."
731 (let ((encoding-magic-comment-template
732 (pcase ruby-encoding-magic-comment-style
733 (`ruby "# coding: %s")
734 (`emacs "# -*- coding: %s -*-")
735 (`custom
736 ruby-custom-encoding-magic-comment-template))))
737 (insert
738 (format encoding-magic-comment-template encoding)
739 "\n")))
740
741(defun ruby--detect-encoding ()
742 (if (eq ruby-insert-encoding-magic-comment 'always-utf8)
743 "utf-8"
744 (let ((coding-system
745 (or save-buffer-coding-system
746 buffer-file-coding-system)))
747 (if coding-system
748 (setq coding-system
749 (or (coding-system-get coding-system 'mime-charset)
750 (coding-system-change-eol-conversion coding-system nil))))
751 (if coding-system
752 (symbol-name
753 (if ruby-use-encoding-map
754 (let ((elt (assq coding-system ruby-encoding-map)))
755 (if elt (cdr elt) coding-system))
756 coding-system))
757 "ascii-8bit"))))
758
759(defun ruby--encoding-comment-required-p ()
760 (or (eq ruby-insert-encoding-magic-comment 'always-utf8)
761 (re-search-forward "[^\0-\177]" nil t)))
762
c7fbbc3c
CY
763(defun ruby-mode-set-encoding ()
764 "Insert a magic comment header with the proper encoding if necessary."
765 (save-excursion
766 (widen)
767 (goto-char (point-min))
6c1bf086 768 (when (ruby--encoding-comment-required-p)
c7fbbc3c 769 (goto-char (point-min))
6c1bf086 770 (let ((coding-system (ruby--detect-encoding)))
e70181b8
AM
771 (when coding-system
772 (if (looking-at "^#!") (beginning-of-line 2))
99f5d074
BB
773 (cond ((looking-at "\\s *#\\s *.*\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)")
774 ;; update existing encoding comment if necessary
e70181b8
AM
775 (unless (string= (match-string 2) coding-system)
776 (goto-char (match-beginning 2))
777 (delete-region (point) (match-end 2))
e70181b8
AM
778 (insert coding-system)))
779 ((looking-at "\\s *#.*coding\\s *[:=]"))
780 (t (when ruby-insert-encoding-magic-comment
6c1bf086 781 (ruby--insert-coding-comment coding-system))))
e70181b8
AM
782 (when (buffer-modified-p)
783 (basic-save-buffer-1)))))))
c7fbbc3c 784
6cddebc1
DG
785(defvar ruby--electric-indent-chars '(?. ?\) ?} ?\]))
786
787(defun ruby--electric-indent-p (char)
788 (cond
789 ((memq char ruby--electric-indent-chars)
1f87a56c 790 ;; Reindent after typing a char affecting indentation.
6cddebc1
DG
791 (ruby--at-indentation-p (1- (point))))
792 ((memq (char-after) ruby--electric-indent-chars)
1f87a56c 793 ;; Reindent after inserting something in front of the above.
6cddebc1 794 (ruby--at-indentation-p (1- (point))))
1f87a56c 795 ((or (and (>= char ?a) (<= char ?z)) (memq char '(?_ ?? ?! ?:)))
6cddebc1
DG
796 (let ((pt (point)))
797 (save-excursion
1f87a56c 798 (skip-chars-backward "[:alpha:]:_?!")
6cddebc1
DG
799 (and (ruby--at-indentation-p)
800 (looking-at (regexp-opt (cons "end" ruby-block-mid-keywords)))
801 ;; Outdent after typing a keyword.
802 (or (eq (match-end 0) pt)
803 ;; Reindent if it wasn't a keyword after all.
804 (eq (match-end 0) (1- pt)))))))))
805
806;; FIXME: Remove this? It's unused here, but some redefinitions of
807;; `ruby-calculate-indent' in user init files still call it.
c7fbbc3c
CY
808(defun ruby-current-indentation ()
809 "Return the indentation level of current line."
810 (save-excursion
811 (beginning-of-line)
812 (back-to-indentation)
813 (current-column)))
814
cf38dd42 815(defun ruby-indent-line (&optional ignored)
ee61fe97 816 "Correct the indentation of the current Ruby line."
c7fbbc3c
CY
817 (interactive)
818 (ruby-indent-to (ruby-calculate-indent)))
819
820(defun ruby-indent-to (column)
821 "Indent the current line to COLUMN."
822 (when column
823 (let (shift top beg)
03177f98 824 (and (< column 0) (error "Invalid nesting"))
c7fbbc3c
CY
825 (setq shift (current-column))
826 (beginning-of-line)
827 (setq beg (point))
828 (back-to-indentation)
829 (setq top (current-column))
830 (skip-chars-backward " \t")
831 (if (>= shift top) (setq shift (- shift top))
832 (setq shift 0))
833 (if (and (bolp)
834 (= column top))
835 (move-to-column (+ column shift))
836 (move-to-column top)
837 (delete-region beg (point))
838 (beginning-of-line)
839 (indent-to column)
840 (move-to-column (+ column shift))))))
841
842(defun ruby-special-char-p (&optional pos)
843 "Return t if the character before POS is a special character.
844If omitted, POS defaults to the current point.
845Special characters are `?', `$', `:' when preceded by whitespace,
846and `\\' when preceded by `?'."
847 (setq pos (or pos (point)))
848 (let ((c (char-before pos)) (b (and (< (point-min) pos)
849 (char-before (1- pos)))))
850 (cond ((or (eq c ??) (eq c ?$)))
851 ((and (eq c ?:) (or (not b) (eq (char-syntax b) ? ))))
852 ((eq c ?\\) (eq b ??)))))
853
9cd80478
DG
854(defun ruby-singleton-class-p (&optional pos)
855 (save-excursion
856 (when pos (goto-char pos))
857 (forward-word -1)
858 (and (or (bolp) (not (eq (char-before (point)) ?_)))
bb808526 859 (looking-at ruby-singleton-class-re))))
9cd80478 860
c7fbbc3c 861(defun ruby-expr-beg (&optional option)
8619323f
DG
862 "Check if point is possibly at the beginning of an expression.
863OPTION specifies the type of the expression.
864Can be one of `heredoc', `modifier', `expr-qstr', `expr-re'."
c7fbbc3c
CY
865 (save-excursion
866 (store-match-data nil)
9cd80478
DG
867 (let ((space (skip-chars-backward " \t"))
868 (start (point)))
c7fbbc3c
CY
869 (cond
870 ((bolp) t)
871 ((progn
872 (forward-char -1)
873 (and (looking-at "\\?")
874 (or (eq (char-syntax (char-before (point))) ?w)
875 (ruby-special-char-p))))
876 nil)
8619323f
DG
877 ((looking-at ruby-operator-re))
878 ((eq option 'heredoc)
879 (and (< space 0) (not (ruby-singleton-class-p start))))
880 ((or (looking-at "[\\[({,;]")
c7fbbc3c
CY
881 (and (looking-at "[!?]")
882 (or (not (eq option 'modifier))
883 (bolp)
884 (save-excursion (forward-char -1) (looking-at "\\Sw$"))))
885 (and (looking-at ruby-symbol-re)
886 (skip-chars-backward ruby-symbol-chars)
887 (cond
888 ((looking-at (regexp-opt
889 (append ruby-block-beg-keywords
890 ruby-block-op-keywords
891 ruby-block-mid-keywords)
892 'words))
893 (goto-char (match-end 0))
af67e79a 894 (not (looking-at "\\s_")))
c7fbbc3c
CY
895 ((eq option 'expr-qstr)
896 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
897 ((eq option 'expr-re)
898 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
899 (t nil)))))))))
900
901(defun ruby-forward-string (term &optional end no-error expand)
8f48d131
CD
902 "Move forward across one balanced pair of string delimiters.
903Skips escaped delimiters. If EXPAND is non-nil, also ignores
904delimiters in interpolated strings.
905
906TERM should be a string containing either a single, self-matching
907delimiter (e.g. \"/\"), or a pair of matching delimiters with the
908close delimiter first (e.g. \"][\").
909
910When non-nil, search is bounded by position END.
911
912Throws an error if a balanced match is not found, unless NO-ERROR
913is non-nil, in which case nil will be returned.
914
915This command assumes the character after point is an opening
916delimiter."
c7fbbc3c 917 (let ((n 1) (c (string-to-char term))
8f48d131
CD
918 (re (concat "[^\\]\\(\\\\\\\\\\)*\\("
919 (if (string= term "^") ;[^] is not a valid regexp
920 "\\^"
921 (concat "[" term "]"))
922 (when expand "\\|\\(#{\\)")
923 "\\)")))
c7fbbc3c
CY
924 (while (and (re-search-forward re end no-error)
925 (if (match-beginning 3)
926 (ruby-forward-string "}{" end no-error nil)
927 (> (setq n (if (eq (char-before (point)) c)
928 (1- n) (1+ n))) 0)))
929 (forward-char -1))
930 (cond ((zerop n))
931 (no-error nil)
03177f98 932 ((error "Unterminated string")))))
c7fbbc3c
CY
933
934(defun ruby-deep-indent-paren-p (c)
ee61fe97 935 "TODO: document."
c7fbbc3c
CY
936 (cond ((listp ruby-deep-indent-paren)
937 (let ((deep (assoc c ruby-deep-indent-paren)))
938 (cond (deep
939 (or (cdr deep) ruby-deep-indent-paren-style))
940 ((memq c ruby-deep-indent-paren)
941 ruby-deep-indent-paren-style))))
942 ((eq c ruby-deep-indent-paren) ruby-deep-indent-paren-style)
943 ((eq c ?\( ) ruby-deep-arglist)))
944
945(defun ruby-parse-partial (&optional end in-string nest depth pcol indent)
ee61fe97 946 "TODO: document throughout function body."
c7fbbc3c
CY
947 (or depth (setq depth 0))
948 (or indent (setq indent 0))
949 (when (re-search-forward ruby-delimiter end 'move)
950 (let ((pnt (point)) w re expand)
951 (goto-char (match-beginning 0))
952 (cond
953 ((and (memq (char-before) '(?@ ?$)) (looking-at "\\sw"))
954 (goto-char pnt))
955 ((looking-at "[\"`]") ;skip string
956 (cond
957 ((and (not (eobp))
ad16897c
SM
958 (ruby-forward-string (buffer-substring (point) (1+ (point)))
959 end t t))
c7fbbc3c
CY
960 nil)
961 (t
962 (setq in-string (point))
963 (goto-char end))))
964 ((looking-at "'")
965 (cond
966 ((and (not (eobp))
967 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t))
968 nil)
969 (t
970 (setq in-string (point))
971 (goto-char end))))
972 ((looking-at "/=")
973 (goto-char pnt))
974 ((looking-at "/")
975 (cond
976 ((and (not (eobp)) (ruby-expr-beg 'expr-re))
977 (if (ruby-forward-string "/" end t t)
978 nil
979 (setq in-string (point))
980 (goto-char end)))
981 (t
982 (goto-char pnt))))
983 ((looking-at "%")
984 (cond
985 ((and (not (eobp))
986 (ruby-expr-beg 'expr-qstr)
987 (not (looking-at "%="))
988 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
989 (goto-char (match-beginning 1))
990 (setq expand (not (memq (char-before) '(?q ?w))))
991 (setq w (match-string 1))
992 (cond
993 ((string= w "[") (setq re "]["))
994 ((string= w "{") (setq re "}{"))
995 ((string= w "(") (setq re ")("))
996 ((string= w "<") (setq re "><"))
997 ((and expand (string= w "\\"))
998 (setq w (concat "\\" w))))
999 (unless (cond (re (ruby-forward-string re end t expand))
1000 (expand (ruby-forward-string w end t t))
1001 (t (re-search-forward
1002 (if (string= w "\\")
1003 "\\\\[^\\]*\\\\"
1004 (concat "[^\\]\\(\\\\\\\\\\)*" w))
1005 end t)))
1006 (setq in-string (point))
1007 (goto-char end)))
1008 (t
1009 (goto-char pnt))))
1010 ((looking-at "\\?") ;skip ?char
1011 (cond
1012 ((and (ruby-expr-beg)
1013 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
1014 (goto-char (match-end 0)))
1015 (t
1016 (goto-char pnt))))
1017 ((looking-at "\\$") ;skip $char
1018 (goto-char pnt)
1019 (forward-char 1))
1020 ((looking-at "#") ;skip comment
1021 (forward-line 1)
1022 (goto-char (point))
1023 )
1024 ((looking-at "[\\[{(]")
1025 (let ((deep (ruby-deep-indent-paren-p (char-after))))
1026 (if (and deep (or (not (eq (char-after) ?\{)) (ruby-expr-beg)))
1027 (progn
1028 (and (eq deep 'space) (looking-at ".\\s +[^# \t\n]")
1029 (setq pnt (1- (match-end 0))))
1030 (setq nest (cons (cons (char-after (point)) pnt) nest))
1031 (setq pcol (cons (cons pnt depth) pcol))
1032 (setq depth 0))
1033 (setq nest (cons (cons (char-after (point)) pnt) nest))
1034 (setq depth (1+ depth))))
1035 (goto-char pnt)
1036 )
1037 ((looking-at "[])}]")
1038 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
1039 (setq depth (cdr (car pcol)) pcol (cdr pcol))
1040 (setq depth (1- depth)))
1041 (setq nest (cdr nest))
1042 (goto-char pnt))
1043 ((looking-at ruby-block-end-re)
1044 (if (or (and (not (bolp))
1045 (progn
1046 (forward-char -1)
1047 (setq w (char-after (point)))
1048 (or (eq ?_ w)
1049 (eq ?. w))))
1050 (progn
1051 (goto-char pnt)
1052 (setq w (char-after (point)))
1053 (or (eq ?_ w)
1054 (eq ?! w)
1055 (eq ?? w))))
1056 nil
1057 (setq nest (cdr nest))
1058 (setq depth (1- depth)))
1059 (goto-char pnt))
1060 ((looking-at "def\\s +[^(\n;]*")
1061 (if (or (bolp)
1062 (progn
1063 (forward-char -1)
1064 (not (eq ?_ (char-after (point))))))
1065 (progn
1066 (setq nest (cons (cons nil pnt) nest))
1067 (setq depth (1+ depth))))
1068 (goto-char (match-end 0)))
39675016 1069 ((looking-at (concat "\\_<\\(" ruby-block-beg-re "\\)\\_>"))
c7fbbc3c
CY
1070 (and
1071 (save-match-data
ac72c08d 1072 (or (not (looking-at "do\\_>"))
c7fbbc3c
CY
1073 (save-excursion
1074 (back-to-indentation)
1075 (not (looking-at ruby-non-block-do-re)))))
1076 (or (bolp)
1077 (progn
1078 (forward-char -1)
1079 (setq w (char-after (point)))
1080 (not (or (eq ?_ w)
1081 (eq ?. w)))))
1082 (goto-char pnt)
7132e457 1083 (not (eq ?! (char-after (point))))
c7fbbc3c
CY
1084 (skip-chars-forward " \t")
1085 (goto-char (match-beginning 0))
1086 (or (not (looking-at ruby-modifier-re))
1087 (ruby-expr-beg 'modifier))
1088 (goto-char pnt)
1089 (setq nest (cons (cons nil pnt) nest))
1090 (setq depth (1+ depth)))
1091 (goto-char pnt))
1092 ((looking-at ":\\(['\"]\\)")
1093 (goto-char (match-beginning 1))
c28662a8 1094 (ruby-forward-string (match-string 1) end t))
11473529 1095 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
c7fbbc3c
CY
1096 (goto-char (match-end 0)))
1097 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
1098 (goto-char (match-end 0)))
1099 ((or (looking-at "\\.\\.\\.?")
1100 (looking-at "\\.[0-9]+")
1101 (looking-at "\\.[a-zA-Z_0-9]+")
1102 (looking-at "\\."))
1103 (goto-char (match-end 0)))
1104 ((looking-at "^=begin")
1105 (if (re-search-forward "^=end" end t)
1106 (forward-line 1)
1107 (setq in-string (match-end 0))
1108 (goto-char end)))
1109 ((looking-at "<<")
1110 (cond
1111 ((and (ruby-expr-beg 'heredoc)
1112 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
1113 (setq re (regexp-quote (or (match-string 4) (match-string 2))))
1114 (if (match-beginning 1) (setq re (concat "\\s *" re)))
1115 (let* ((id-end (goto-char (match-end 0)))
e180ab9f 1116 (line-end-position (point-at-eol))
c7fbbc3c
CY
1117 (state (list in-string nest depth pcol indent)))
1118 ;; parse the rest of the line
1119 (while (and (> line-end-position (point))
1120 (setq state (apply 'ruby-parse-partial
1121 line-end-position state))))
1122 (setq in-string (car state)
1123 nest (nth 1 state)
1124 depth (nth 2 state)
1125 pcol (nth 3 state)
1126 indent (nth 4 state))
1127 ;; skip heredoc section
1128 (if (re-search-forward (concat "^" re "$") end 'move)
1129 (forward-line 1)
1130 (setq in-string id-end)
1131 (goto-char end))))
1132 (t
1133 (goto-char pnt))))
1134 ((looking-at "^__END__$")
1135 (goto-char pnt))
1136 ((and (looking-at ruby-here-doc-beg-re)
1137 (boundp 'ruby-indent-point))
1138 (if (re-search-forward (ruby-here-doc-end-match)
1139 ruby-indent-point t)
1140 (forward-line 1)
1141 (setq in-string (match-end 0))
1142 (goto-char ruby-indent-point)))
1143 (t
03177f98 1144 (error (format "Bad string %s"
c7fbbc3c
CY
1145 (buffer-substring (point) pnt)
1146 ))))))
1147 (list in-string nest depth pcol))
1148
1149(defun ruby-parse-region (start end)
1150 "TODO: document."
1151 (let (state)
1152 (save-excursion
1153 (if start
1154 (goto-char start)
1155 (ruby-beginning-of-indent))
1156 (save-restriction
1157 (narrow-to-region (point) end)
1158 (while (and (> end (point))
1159 (setq state (apply 'ruby-parse-partial end state))))))
1160 (list (nth 0 state) ; in-string
1161 (car (nth 1 state)) ; nest
1162 (nth 2 state) ; depth
1163 (car (car (nth 3 state))) ; pcol
1164 ;(car (nth 5 state)) ; indent
1165 )))
1166
1167(defun ruby-indent-size (pos nest)
ee61fe97 1168 "Return the indentation level in spaces NEST levels deeper than POS."
c7fbbc3c
CY
1169 (+ pos (* (or nest 1) ruby-indent-level)))
1170
1171(defun ruby-calculate-indent (&optional parse-start)
ee61fe97 1172 "Return the proper indentation level of the current line."
c7fbbc3c
CY
1173 ;; TODO: Document body
1174 (save-excursion
1175 (beginning-of-line)
1176 (let ((ruby-indent-point (point))
1177 (case-fold-search nil)
cf38dd42 1178 state eol begin op-end
c7fbbc3c
CY
1179 (paren (progn (skip-syntax-forward " ")
1180 (and (char-after) (matching-paren (char-after)))))
1181 (indent 0))
1182 (if parse-start
1183 (goto-char parse-start)
1184 (ruby-beginning-of-indent)
1185 (setq parse-start (point)))
1186 (back-to-indentation)
1187 (setq indent (current-column))
1188 (setq state (ruby-parse-region parse-start ruby-indent-point))
1189 (cond
1190 ((nth 0 state) ; within string
1191 (setq indent nil)) ; do nothing
1192 ((car (nth 1 state)) ; in paren
1193 (goto-char (setq begin (cdr (nth 1 state))))
1194 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state)))))
1195 (if deep
1196 (cond ((and (eq deep t) (eq (car (nth 1 state)) paren))
1197 (skip-syntax-backward " ")
1198 (setq indent (1- (current-column))))
1199 ((let ((s (ruby-parse-region (point) ruby-indent-point)))
1200 (and (nth 2 s) (> (nth 2 s) 0)
1201 (or (goto-char (cdr (nth 1 s))) t)))
1202 (forward-word -1)
1203 (setq indent (ruby-indent-size (current-column)
1204 (nth 2 state))))
1205 (t
1206 (setq indent (current-column))
1207 (cond ((eq deep 'space))
1208 (paren (setq indent (1- indent)))
1209 (t (setq indent (ruby-indent-size (1- indent) 1))))))
1210 (if (nth 3 state) (goto-char (nth 3 state))
1211 (goto-char parse-start) (back-to-indentation))
1212 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
1213 (and (eq (car (nth 1 state)) paren)
1214 (ruby-deep-indent-paren-p (matching-paren paren))
1215 (search-backward (char-to-string paren))
1216 (setq indent (current-column)))))
1217 ((and (nth 2 state) (> (nth 2 state) 0)) ; in nest
1218 (if (null (cdr (nth 1 state)))
03177f98 1219 (error "Invalid nesting"))
c7fbbc3c
CY
1220 (goto-char (cdr (nth 1 state)))
1221 (forward-word -1) ; skip back a keyword
1222 (setq begin (point))
1223 (cond
1224 ((looking-at "do\\>[^_]") ; iter block is a special case
1225 (if (nth 3 state) (goto-char (nth 3 state))
1226 (goto-char parse-start) (back-to-indentation))
1227 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
1228 (t
1229 (setq indent (+ (current-column) ruby-indent-level)))))
ee61fe97 1230
c7fbbc3c
CY
1231 ((and (nth 2 state) (< (nth 2 state) 0)) ; in negative nest
1232 (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
1233 (when indent
1234 (goto-char ruby-indent-point)
1235 (end-of-line)
1236 (setq eol (point))
1237 (beginning-of-line)
1238 (cond
1239 ((and (not (ruby-deep-indent-paren-p paren))
1240 (re-search-forward ruby-negative eol t))
1241 (and (not (eq ?_ (char-after (match-end 0))))
1242 (setq indent (- indent ruby-indent-level))))
1243 ((and
1244 (save-excursion
1245 (beginning-of-line)
1246 (not (bobp)))
1247 (or (ruby-deep-indent-paren-p t)
1248 (null (car (nth 1 state)))))
1249 ;; goto beginning of non-empty no-comment line
1250 (let (end done)
1251 (while (not done)
1252 (skip-chars-backward " \t\n")
1253 (setq end (point))
1254 (beginning-of-line)
1255 (if (re-search-forward "^\\s *#" end t)
1256 (beginning-of-line)
1257 (setq done t))))
c7fbbc3c
CY
1258 (end-of-line)
1259 ;; skip the comment at the end
1260 (skip-chars-backward " \t")
1261 (let (end (pos (point)))
1262 (beginning-of-line)
1263 (while (and (re-search-forward "#" pos t)
1264 (setq end (1- (point)))
1265 (or (ruby-special-char-p end)
ad16897c
SM
1266 (and (setq state (ruby-parse-region
1267 parse-start end))
c7fbbc3c
CY
1268 (nth 0 state))))
1269 (setq end nil))
1270 (goto-char (or end pos))
1271 (skip-chars-backward " \t")
1272 (setq begin (if (and end (nth 0 state)) pos (cdr (nth 1 state))))
1273 (setq state (ruby-parse-region parse-start (point))))
1274 (or (bobp) (forward-char -1))
1275 (and
1276 (or (and (looking-at ruby-symbol-re)
1277 (skip-chars-backward ruby-symbol-chars)
ad16897c
SM
1278 (looking-at (concat "\\<\\(" ruby-block-hanging-re
1279 "\\)\\>"))
c7fbbc3c
CY
1280 (not (eq (point) (nth 3 state)))
1281 (save-excursion
1282 (goto-char (match-end 0))
1283 (not (looking-at "[a-z_]"))))
1284 (and (looking-at ruby-operator-re)
1285 (not (ruby-special-char-p))
88527bc0
DG
1286 (save-excursion
1287 (forward-char -1)
1288 (or (not (looking-at ruby-operator-re))
1289 (not (eq (char-before) ?:))))
dfbd787f 1290 ;; Operator at the end of line.
c7fbbc3c
CY
1291 (let ((c (char-after (point))))
1292 (and
1293;; (or (null begin)
1294;; (save-excursion
1295;; (goto-char begin)
1296;; (skip-chars-forward " \t")
1297;; (not (or (eolp) (looking-at "#")
1298;; (and (eq (car (nth 1 state)) ?{)
1299;; (looking-at "|"))))))
e636fafe 1300 ;; Not a regexp or percent literal.
dfbd787f
SM
1301 (null (nth 0 (ruby-parse-region (or begin parse-start)
1302 (point))))
c7fbbc3c
CY
1303 (or (not (eq ?| (char-after (point))))
1304 (save-excursion
1305 (or (eolp) (forward-char -1))
1306 (cond
1307 ((search-backward "|" nil t)
1308 (skip-chars-backward " \t\n")
1309 (and (not (eolp))
1310 (progn
1311 (forward-char -1)
1312 (not (looking-at "{")))
1313 (progn
1314 (forward-word -1)
1315 (not (looking-at "do\\>[^_]")))))
1316 (t t))))
1317 (not (eq ?, c))
1318 (setq op-end t)))))
1319 (setq indent
1320 (cond
1321 ((and
1322 (null op-end)
ad16897c
SM
1323 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
1324 "\\)\\>")))
c7fbbc3c
CY
1325 (eq (ruby-deep-indent-paren-p t) 'space)
1326 (not (bobp)))
1327 (widen)
1328 (goto-char (or begin parse-start))
1329 (skip-syntax-forward " ")
1330 (current-column))
1331 ((car (nth 1 state)) indent)
1332 (t
1333 (+ indent ruby-indent-level))))))))
1334 (goto-char ruby-indent-point)
1335 (beginning-of-line)
1336 (skip-syntax-forward " ")
1337 (if (looking-at "\\.[^.]")
1338 (+ indent ruby-indent-level)
1339 indent))))
1340
c7fbbc3c 1341(defun ruby-beginning-of-defun (&optional arg)
fb549d64 1342 "Move backward to the beginning of the current defun.
ee61fe97 1343With ARG, move backward multiple defuns. Negative ARG means
c7fbbc3c
CY
1344move forward."
1345 (interactive "p")
fb549d64
DG
1346 (let (case-fold-search)
1347 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re "\\_>")
1348 nil t (or arg 1))
1349 (beginning-of-line))))
1350
1351(defun ruby-end-of-defun ()
1352 "Move point to the end of the current defun.
1353The defun begins at or after the point. This function is called
1354by `end-of-defun'."
c7fbbc3c 1355 (interactive "p")
8f06acce 1356 (ruby-forward-sexp)
fb549d64
DG
1357 (let (case-fold-search)
1358 (when (looking-back (concat "^\\s *" ruby-block-end-re))
1359 (forward-line 1))))
c7fbbc3c
CY
1360
1361(defun ruby-beginning-of-indent ()
0ba2d4b6
DG
1362 "Backtrack to a line which can be used as a reference for
1363calculating indentation on the lines after it."
1364 (while (and (re-search-backward ruby-indent-beg-re nil 'move)
1365 (if (ruby-in-ppss-context-p 'anything)
1366 t
1367 ;; We can stop, then.
1368 (beginning-of-line)))))
c7fbbc3c
CY
1369
1370(defun ruby-move-to-block (n)
5e9419e8
DG
1371 "Move to the beginning (N < 0) or the end (N > 0) of the
1372current block, a sibling block, or an outer block. Do that (abs N) times."
3086ca2e 1373 (back-to-indentation)
7132e457 1374 (let ((signum (if (> n 0) 1 -1))
5e9419e8 1375 (backward (< n 0))
3086ca2e 1376 (depth (or (nth 2 (ruby-parse-region (point) (line-end-position))) 0))
fb549d64 1377 case-fold-search
7132e457 1378 down done)
3086ca2e
DG
1379 (when (looking-at ruby-block-mid-re)
1380 (setq depth (+ depth signum)))
7132e457
DG
1381 (when (< (* depth signum) 0)
1382 ;; Moving end -> end or beginning -> beginning.
1383 (setq depth 0))
5e9419e8
DG
1384 (dotimes (_ (abs n))
1385 (setq done nil)
1386 (setq down (save-excursion
1387 (back-to-indentation)
1388 ;; There is a block start or block end keyword on this
1389 ;; line, don't need to look for another block.
1390 (and (re-search-forward
1391 (if backward ruby-block-end-re
1392 (concat "\\_<\\(" ruby-block-beg-re "\\)\\_>"))
1393 (line-end-position) t)
1394 (not (nth 8 (syntax-ppss))))))
1395 (while (and (not done) (not (if backward (bobp) (eobp))))
1396 (forward-line signum)
c7fbbc3c 1397 (cond
5e9419e8
DG
1398 ;; Skip empty and commented out lines.
1399 ((looking-at "^\\s *$"))
1400 ((looking-at "^\\s *#"))
1401 ;; Skip block comments;
1402 ((and (not backward) (looking-at "^=begin\\>"))
1403 (re-search-forward "^=end\\>"))
1404 ((and backward (looking-at "^=end\\>"))
1405 (re-search-backward "^=begin\\>"))
53ca88c4
DG
1406 ;; Jump over a multiline literal.
1407 ((ruby-in-ppss-context-p 'string)
1408 (goto-char (nth 8 (syntax-ppss)))
1409 (unless backward
1410 (forward-sexp)
1411 (when (bolp) (forward-char -1)))) ; After a heredoc.
5e9419e8 1412 (t
53ca88c4
DG
1413 (let ((state (ruby-parse-region (point) (line-end-position))))
1414 (unless (car state) ; Line ends with unfinished string.
1415 (setq depth (+ (nth 2 state) depth))))
5e9419e8 1416 (cond
3086ca2e 1417 ;; Increased depth, we found a block.
7132e457 1418 ((> (* signum depth) 0)
5e9419e8 1419 (setq down t))
3086ca2e
DG
1420 ;; We're at the same depth as when we started, and we've
1421 ;; encountered a block before. Stop.
7132e457 1422 ((and down (zerop depth))
5e9419e8 1423 (setq done t))
3086ca2e 1424 ;; Lower depth, means outer block, can stop now.
7132e457 1425 ((< (* signum depth) 0)
3086ca2e 1426 (setq done t)))))))
d1e1e53d 1427 (back-to-indentation)))
c7fbbc3c
CY
1428
1429(defun ruby-beginning-of-block (&optional arg)
1430 "Move backward to the beginning of the current block.
1431With ARG, move up multiple blocks."
1432 (interactive "p")
1433 (ruby-move-to-block (- (or arg 1))))
1434
1435(defun ruby-end-of-block (&optional arg)
1436 "Move forward to the end of the current block.
1437With ARG, move out of multiple blocks."
5e9419e8 1438 (interactive "p")
c7fbbc3c
CY
1439 (ruby-move-to-block (or arg 1)))
1440
1441(defun ruby-forward-sexp (&optional arg)
1442 "Move forward across one balanced expression (sexp).
ee61fe97 1443With ARG, do it many times. Negative ARG means move backward."
c7fbbc3c
CY
1444 ;; TODO: Document body
1445 (interactive "p")
34d1a133
SM
1446 (cond
1447 (ruby-use-smie (forward-sexp arg))
1448 ((and (numberp arg) (< arg 0)) (ruby-backward-sexp (- arg)))
1449 (t
c7fbbc3c
CY
1450 (let ((i (or arg 1)))
1451 (condition-case nil
1452 (while (> i 0)
1453 (skip-syntax-forward " ")
cc9c9831 1454 (if (looking-at ",\\s *") (goto-char (match-end 0)))
c7fbbc3c
CY
1455 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
1456 (goto-char (match-end 0)))
1457 ((progn
1458 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
1459 (looking-at "\\s("))
1460 (goto-char (scan-sexps (point) 1)))
ad16897c
SM
1461 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
1462 "\\)\\>"))
c7fbbc3c
CY
1463 (not (eq (char-before (point)) ?.))
1464 (not (eq (char-before (point)) ?:)))
1465 (ruby-end-of-block)
1466 (forward-word 1))
1467 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
1468 (while (progn
1469 (while (progn (forward-word 1) (looking-at "_")))
1470 (cond ((looking-at "::") (forward-char 2) t)
1471 ((> (skip-chars-forward ".") 0))
1472 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
1473 (forward-char 1) nil)))))
1474 ((let (state expr)
1475 (while
1476 (progn
1477 (setq expr (or expr (ruby-expr-beg)
1478 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
ad16897c
SM
1479 (nth 1 (setq state (apply #'ruby-parse-partial
1480 nil state))))
c7fbbc3c
CY
1481 (setq expr t)
1482 (skip-chars-forward "<"))
1483 (not expr))))
1484 (setq i (1- i)))
1485 ((error) (forward-word 1)))
34d1a133 1486 i))))
c7fbbc3c
CY
1487
1488(defun ruby-backward-sexp (&optional arg)
1489 "Move backward across one balanced expression (sexp).
ee61fe97 1490With ARG, do it many times. Negative ARG means move forward."
c7fbbc3c
CY
1491 ;; TODO: Document body
1492 (interactive "p")
34d1a133
SM
1493 (cond
1494 (ruby-use-smie (backward-sexp arg))
1495 ((and (numberp arg) (< arg 0)) (ruby-forward-sexp (- arg)))
1496 (t
c7fbbc3c
CY
1497 (let ((i (or arg 1)))
1498 (condition-case nil
1499 (while (> i 0)
1500 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
1501 (forward-char -1)
1502 (cond ((looking-at "\\s)")
1503 (goto-char (scan-sexps (1+ (point)) -1))
ad16897c
SM
1504 (pcase (char-before)
1505 (`?% (forward-char -1))
1506 ((or `?q `?Q `?w `?W `?r `?x)
1507 (if (eq (char-before (1- (point))) ?%)
1508 (forward-char -2))))
c7fbbc3c
CY
1509 nil)
1510 ((looking-at "\\s\"\\|\\\\\\S_")
1511 (let ((c (char-to-string (char-before (match-end 0)))))
1512 (while (and (search-backward c)
1513 (eq (logand (skip-chars-backward "\\") 1)
1514 1))))
1515 nil)
1516 ((looking-at "\\s.\\|\\s\\")
1517 (if (ruby-special-char-p) (forward-char -1)))
1518 ((looking-at "\\s(") nil)
1519 (t
1520 (forward-char 1)
1521 (while (progn (forward-word -1)
ad16897c
SM
1522 (pcase (char-before)
1523 (`?_ t)
1524 (`?. (forward-char -1) t)
1525 ((or `?$ `?@)
c7fbbc3c 1526 (forward-char -1)
ad16897c
SM
1527 (and (eq (char-before) (char-after))
1528 (forward-char -1)))
1529 (`?:
c7fbbc3c
CY
1530 (forward-char -1)
1531 (eq (char-before) :)))))
1532 (if (looking-at ruby-block-end-re)
1533 (ruby-beginning-of-block))
1534 nil))
1535 (setq i (1- i)))
1536 ((error)))
34d1a133 1537 i))))
c7fbbc3c 1538
cf38dd42
SM
1539(defun ruby-indent-exp (&optional ignored)
1540 "Indent each line in the balanced expression following the point."
c7fbbc3c
CY
1541 (interactive "*P")
1542 (let ((here (point-marker)) start top column (nest t))
1543 (set-marker-insertion-type here t)
1544 (unwind-protect
1545 (progn
1546 (beginning-of-line)
1547 (setq start (point) top (current-indentation))
1548 (while (and (not (eobp))
1549 (progn
1550 (setq column (ruby-calculate-indent start))
1551 (cond ((> column top)
1552 (setq nest t))
1553 ((and (= column top) nest)
1554 (setq nest nil) t))))
1555 (ruby-indent-to column)
1556 (beginning-of-line 2)))
1557 (goto-char here)
1558 (set-marker here nil))))
1559
1560(defun ruby-add-log-current-method ()
ee61fe97 1561 "Return the current method name as a string.
c7fbbc3c
CY
1562This string includes all namespaces.
1563
1564For example:
1565
1566 #exit
1567 String#gsub
1568 Net::HTTP#active?
5745cae6 1569 File.open
c7fbbc3c
CY
1570
1571See `add-log-current-defun-function'."
c7fbbc3c
CY
1572 (condition-case nil
1573 (save-excursion
71a048c1
DG
1574 (let* ((indent 0) mname mlist
1575 (start (point))
1576 (make-definition-re
1577 (lambda (re)
1578 (concat "^[ \t]*" re "[ \t]+"
1579 "\\("
1580 ;; \\. and :: for class methods
1581 "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
1582 "+\\)")))
1583 (definition-re (funcall make-definition-re ruby-defun-beg-re))
1584 (module-re (funcall make-definition-re "\\(class\\|module\\)")))
5745cae6 1585 ;; Get the current method definition (or class/module).
bb808526
DG
1586 (when (re-search-backward definition-re nil t)
1587 (goto-char (match-beginning 1))
71a048c1
DG
1588 (if (not (string-equal "def" (match-string 1)))
1589 (setq mlist (list (match-string 2)))
1590 ;; We're inside the method. For classes and modules,
1591 ;; this check is skipped for performance.
1592 (when (ruby-block-contains-point start)
1593 (setq mname (match-string 2))))
bb808526
DG
1594 (setq indent (current-column))
1595 (beginning-of-line))
5745cae6 1596 ;; Walk up the class/module nesting.
c7fbbc3c 1597 (while (and (> indent 0)
71a048c1 1598 (re-search-backward module-re nil t))
c7fbbc3c 1599 (goto-char (match-beginning 1))
71a048c1 1600 (when (< (current-column) indent)
bb808526
DG
1601 (setq mlist (cons (match-string 2) mlist))
1602 (setq indent (current-column))
1603 (beginning-of-line)))
5745cae6 1604 ;; Process the method name.
c7fbbc3c
CY
1605 (when mname
1606 (let ((mn (split-string mname "\\.\\|::")))
1607 (if (cdr mn)
1608 (progn
5745cae6
DG
1609 (unless (string-equal "self" (car mn)) ; def self.foo
1610 ;; def C.foo
1611 (let ((ml (nreverse mlist)))
1612 ;; If the method name references one of the
1613 ;; containing modules, drop the more nested ones.
c7fbbc3c
CY
1614 (while ml
1615 (if (string-equal (car ml) (car mn))
1616 (setq mlist (nreverse (cdr ml)) ml nil))
5745cae6
DG
1617 (or (setq ml (cdr ml)) (nreverse mlist))))
1618 (if mlist
1619 (setcdr (last mlist) (butlast mn))
1620 (setq mlist (butlast mn))))
1621 (setq mname (concat "." (car (last mn)))))
bb808526
DG
1622 ;; See if the method is in singleton class context.
1623 (let ((in-singleton-class
1624 (when (re-search-forward ruby-singleton-class-re start t)
1625 (goto-char (match-beginning 0))
71a048c1
DG
1626 ;; FIXME: Optimize it out, too?
1627 ;; This can be slow in a large file, but
1628 ;; unlike class/module declaration
1629 ;; indentations, method definitions can be
1630 ;; intermixed with these, and may or may not
1631 ;; be additionally indented after visibility
1632 ;; keywords.
bb808526
DG
1633 (ruby-block-contains-point start))))
1634 (setq mname (concat
1635 (if in-singleton-class "." "#")
1636 mname))))))
5745cae6 1637 ;; Generate the string.
c7fbbc3c
CY
1638 (if (consp mlist)
1639 (setq mlist (mapconcat (function identity) mlist "::")))
1640 (if mname
1641 (if mlist (concat mlist mname) mname)
1642 mlist)))))
1643
bb808526
DG
1644(defun ruby-block-contains-point (pt)
1645 (save-excursion
1646 (save-match-data
1647 (ruby-forward-sexp)
1648 (> (point) pt))))
1649
c3268831
DG
1650(defun ruby-brace-to-do-end (orig end)
1651 (let (beg-marker end-marker)
1652 (goto-char end)
1653 (when (eq (char-before) ?\})
1654 (delete-char -1)
32fb8162
DG
1655 (when (save-excursion
1656 (skip-chars-backward " \t")
1657 (not (bolp)))
c3268831
DG
1658 (insert "\n"))
1659 (insert "end")
1660 (setq end-marker (point-marker))
1661 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w))
1662 (insert " "))
1663 (goto-char orig)
1664 (delete-char 1)
1665 (when (eq (char-syntax (char-before)) ?w)
1666 (insert " "))
1667 (insert "do")
1668 (setq beg-marker (point-marker))
1669 (when (looking-at "\\(\\s \\)*|")
1670 (unless (match-beginning 1)
1671 (insert " "))
1672 (goto-char (1+ (match-end 0)))
1673 (search-forward "|"))
1674 (unless (looking-at "\\s *$")
1675 (insert "\n"))
1676 (indent-region beg-marker end-marker)
1677 (goto-char beg-marker)
1678 t)))
1679
1680(defun ruby-do-end-to-brace (orig end)
32fb8162
DG
1681 (let (beg-marker end-marker beg-pos end-pos)
1682 (goto-char (- end 3))
1683 (when (looking-at ruby-block-end-re)
1684 (delete-char 3)
1685 (setq end-marker (point-marker))
1686 (insert "}")
1687 (goto-char orig)
1688 (delete-char 2)
963ce636
DG
1689 ;; Maybe this should be customizable, let's see if anyone asks.
1690 (insert "{ ")
32fb8162
DG
1691 (setq beg-marker (point-marker))
1692 (when (looking-at "\\s +|")
1693 (delete-char (- (match-end 0) (match-beginning 0) 1))
1694 (forward-char)
1695 (re-search-forward "|" (line-end-position) t))
1696 (save-excursion
1697 (skip-chars-forward " \t\n\r")
1698 (setq beg-pos (point))
1699 (goto-char end-marker)
1700 (skip-chars-backward " \t\n\r")
1701 (setq end-pos (point)))
1702 (when (or
1703 (< end-pos beg-pos)
1704 (and (= (line-number-at-pos beg-pos) (line-number-at-pos end-pos))
1705 (< (+ (current-column) (- end-pos beg-pos) 2) fill-column)))
1706 (just-one-space -1)
1707 (goto-char end-marker)
1708 (just-one-space -1))
1709 (goto-char beg-marker)
1710 t)))
0d9e2599
NN
1711
1712(defun ruby-toggle-block ()
c3268831
DG
1713 "Toggle block type from do-end to braces or back.
1714The block must begin on the current line or above it and end after the point.
1715If the result is do-end block, it will always be multiline."
0d9e2599 1716 (interactive)
c3268831
DG
1717 (let ((start (point)) beg end)
1718 (end-of-line)
1719 (unless
56cd894e 1720 (if (and (re-search-backward "\\(?:[^#]\\)\\({\\)\\|\\(\\_<do\\_>\\)")
c3268831 1721 (progn
56cd894e 1722 (goto-char (or (match-beginning 1) (match-beginning 2)))
c3268831
DG
1723 (setq beg (point))
1724 (save-match-data (ruby-forward-sexp))
1725 (setq end (point))
1726 (> end start)))
1727 (if (match-beginning 1)
1728 (ruby-brace-to-do-end beg end)
1729 (ruby-do-end-to-brace beg end)))
1730 (goto-char start))))
0d9e2599 1731
7ffd3721
DG
1732(eval-and-compile
1733 (defconst ruby-percent-literal-beg-re
1734 "\\(%\\)[qQrswWxIi]?\\([[:punct:]]\\)"
1735 "Regexp to match the beginning of percent literal.")
1736
1737 (defconst ruby-syntax-methods-before-regexp
1738 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1739 "assert_match" "Given" "Then" "When")
1740 "Methods that can take regexp as the first argument.
1a0a0a8a
DG
1741It will be properly highlighted even when the call omits parens.")
1742
7ffd3721
DG
1743 (defvar ruby-syntax-before-regexp-re
1744 (concat
1745 ;; Special tokens that can't be followed by a division operator.
1746 "\\(^\\|[[=(,~;<>]"
1747 ;; Distinguish ternary operator tokens.
1748 ;; FIXME: They don't really have to be separated with spaces.
1749 "\\|[?:] "
1750 ;; Control flow keywords and operators following bol or whitespace.
1751 "\\|\\(?:^\\|\\s \\)"
1752 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
1753 "or" "not" "&&" "||"))
1754 ;; Method name from the list.
1755 "\\|\\_<"
1756 (regexp-opt ruby-syntax-methods-before-regexp)
1757 "\\)\\s *")
1758 "Regexp to match text that can be followed by a regular expression."))
1759
1760(defun ruby-syntax-propertize-function (start end)
1761 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1762 (let (case-fold-search)
1763 (goto-char start)
1764 (remove-text-properties start end '(ruby-expansion-match-data))
1765 (ruby-syntax-propertize-heredoc end)
1766 (ruby-syntax-enclosing-percent-literal end)
1767 (funcall
1768 (syntax-propertize-rules
1769 ;; $' $" $` .... are variables.
1770 ;; ?' ?" ?` are character literals (one-char strings in 1.9+).
1771 ("\\([?$]\\)[#\"'`]"
1772 (1 (unless (save-excursion
1773 ;; Not within a string.
1774 (nth 3 (syntax-ppss (match-beginning 0))))
1775 (string-to-syntax "\\"))))
af67e79a
DG
1776 ;; Part of symbol when at the end of a method name.
1777 ("[!?]"
1778 (0 (unless (save-excursion
1779 (or (nth 8 (syntax-ppss (match-beginning 0)))
1780 (let (parse-sexp-lookup-properties)
fa834a93
DG
1781 (zerop (skip-syntax-backward "w_")))
1782 (memq (preceding-char) '(?@ ?$))))
af67e79a 1783 (string-to-syntax "_"))))
7ffd3721
DG
1784 ;; Regular expressions. Start with matching unescaped slash.
1785 ("\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*\\(/\\)"
1786 (1 (let ((state (save-excursion (syntax-ppss (match-beginning 1)))))
1787 (when (or
1788 ;; Beginning of a regexp.
1789 (and (null (nth 8 state))
1790 (save-excursion
1791 (forward-char -1)
1792 (looking-back ruby-syntax-before-regexp-re
1793 (point-at-bol))))
1794 ;; End of regexp. We don't match the whole
1795 ;; regexp at once because it can have
1796 ;; string interpolation inside, or span
1797 ;; several lines.
1798 (eq ?/ (nth 3 state)))
1799 (string-to-syntax "\"/")))))
1800 ;; Expression expansions in strings. We're handling them
1801 ;; here, so that the regexp rule never matches inside them.
1802 (ruby-expression-expansion-re
1803 (0 (ignore (ruby-syntax-propertize-expansion))))
1804 ("^=en\\(d\\)\\_>" (1 "!"))
1805 ("^\\(=\\)begin\\_>" (1 "!"))
1806 ;; Handle here documents.
1807 ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
1808 (7 (unless (or (nth 8 (save-excursion
1809 (syntax-ppss (match-beginning 0))))
1810 (ruby-singleton-class-p (match-beginning 0)))
1811 (put-text-property (match-beginning 7) (match-end 7)
1812 'syntax-table (string-to-syntax "\""))
1813 (ruby-syntax-propertize-heredoc end))))
1814 ;; Handle percent literals: %w(), %q{}, etc.
1815 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re)
1816 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end)))))
1817 (point) end)))
1818
1819(defun ruby-syntax-propertize-heredoc (limit)
1820 (let ((ppss (syntax-ppss))
1821 (res '()))
1822 (when (eq ?\n (nth 3 ppss))
1823 (save-excursion
1824 (goto-char (nth 8 ppss))
cf38dd42 1825 (beginning-of-line)
7ffd3721
DG
1826 (while (re-search-forward ruby-here-doc-beg-re
1827 (line-end-position) t)
1828 (unless (ruby-singleton-class-p (match-beginning 0))
1829 (push (concat (ruby-here-doc-end-match) "\n") res))))
1830 (save-excursion
1831 ;; With multiple openers on the same line, we don't know in which
1832 ;; part `start' is, so we have to go back to the beginning.
1833 (when (cdr res)
1834 (goto-char (nth 8 ppss))
1835 (setq res (nreverse res)))
1836 (while (and res (re-search-forward (pop res) limit 'move))
1837 (if (null res)
1838 (put-text-property (1- (point)) (point)
1839 'syntax-table (string-to-syntax "\""))))
1840 ;; End up at bol following the heredoc openers.
1841 ;; Propertize expression expansions from this point forward.
1842 ))))
1843
1844(defun ruby-syntax-enclosing-percent-literal (limit)
1845 (let ((state (syntax-ppss))
1846 (start (point)))
1847 ;; When already inside percent literal, re-propertize it.
1848 (when (eq t (nth 3 state))
1849 (goto-char (nth 8 state))
1850 (when (looking-at ruby-percent-literal-beg-re)
1851 (ruby-syntax-propertize-percent-literal limit))
1852 (when (< (point) start) (goto-char start)))))
1853
1854(defun ruby-syntax-propertize-percent-literal (limit)
1855 (goto-char (match-beginning 2))
1856 ;; Not inside a simple string or comment.
1857 (when (eq t (nth 3 (syntax-ppss)))
1858 (let* ((op (char-after))
1859 (ops (char-to-string op))
1860 (cl (or (cdr (aref (syntax-table) op))
1861 (cdr (assoc op '((?< . ?>))))))
1862 parse-sexp-lookup-properties)
1863 (save-excursion
1864 (condition-case nil
1865 (progn
1866 (if cl ; Paired delimiters.
1867 ;; Delimiter pairs of the same kind can be nested
1868 ;; inside the literal, as long as they are balanced.
1869 ;; Create syntax table that ignores other characters.
1870 (with-syntax-table (make-char-table 'syntax-table nil)
1871 (modify-syntax-entry op (concat "(" (char-to-string cl)))
1872 (modify-syntax-entry cl (concat ")" ops))
1873 (modify-syntax-entry ?\\ "\\")
1874 (save-restriction
1875 (narrow-to-region (point) limit)
1876 (forward-list))) ; skip to the paired character
1877 ;; Single character delimiter.
1878 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1879 (regexp-quote ops)) limit nil))
1880 ;; Found the closing delimiter.
1881 (put-text-property (1- (point)) (point) 'syntax-table
1882 (string-to-syntax "|")))
1883 ;; Unclosed literal, do nothing.
1884 ((scan-error search-failed)))))))
1885
1886(defun ruby-syntax-propertize-expansion ()
1887 ;; Save the match data to a text property, for font-locking later.
1888 ;; Set the syntax of all double quotes and backticks to punctuation.
1889 (let* ((beg (match-beginning 2))
1890 (end (match-end 2))
1891 (state (and beg (save-excursion (syntax-ppss beg)))))
1892 (when (ruby-syntax-expansion-allowed-p state)
1893 (put-text-property beg (1+ beg) 'ruby-expansion-match-data
1894 (match-data))
1895 (goto-char beg)
1896 (while (re-search-forward "[\"`]" end 'move)
1897 (put-text-property (match-beginning 0) (match-end 0)
1898 'syntax-table (string-to-syntax "."))))))
1899
1900(defun ruby-syntax-expansion-allowed-p (parse-state)
1901 "Return non-nil if expression expansion is allowed."
1902 (let ((term (nth 3 parse-state)))
1903 (cond
1904 ((memq term '(?\" ?` ?\n ?/)))
1905 ((eq term t)
1906 (save-match-data
cf38dd42 1907 (save-excursion
7ffd3721
DG
1908 (goto-char (nth 8 parse-state))
1909 (looking-at "%\\(?:[QWrxI]\\|\\W\\)")))))))
c7fbbc3c 1910
7ffd3721
DG
1911(defun ruby-syntax-propertize-expansions (start end)
1912 (save-excursion
1913 (goto-char start)
1914 (while (re-search-forward ruby-expression-expansion-re end 'move)
1915 (ruby-syntax-propertize-expansion))))
c7fbbc3c
CY
1916
1917(defun ruby-in-ppss-context-p (context &optional ppss)
1918 (let ((ppss (or ppss (syntax-ppss (point)))))
1919 (if (cond
1920 ((eq context 'anything)
1921 (or (nth 3 ppss)
1922 (nth 4 ppss)))
1923 ((eq context 'string)
1924 (nth 3 ppss))
1925 ((eq context 'heredoc)
cf38dd42 1926 (eq ?\n (nth 3 ppss)))
c7fbbc3c
CY
1927 ((eq context 'non-heredoc)
1928 (and (ruby-in-ppss-context-p 'anything)
1929 (not (ruby-in-ppss-context-p 'heredoc))))
1930 ((eq context 'comment)
1931 (nth 4 ppss))
1932 (t
1933 (error (concat
1934 "Internal error on `ruby-in-ppss-context-p': "
1935 "context name `" (symbol-name context) "' is unknown"))))
1936 t)))
1937
c7fbbc3c
CY
1938(defvar ruby-font-lock-syntax-table
1939 (let ((tbl (copy-syntax-table ruby-mode-syntax-table)))
1940 (modify-syntax-entry ?_ "w" tbl)
1941 tbl)
ee61fe97 1942 "The syntax table to use for fontifying Ruby mode buffers.
c7fbbc3c
CY
1943See `font-lock-syntax-table'.")
1944
ac72c08d
DG
1945(defconst ruby-font-lock-keyword-beg-re "\\(?:^\\|[^.@$]\\|\\.\\.\\)")
1946
c7fbbc3c 1947(defconst ruby-font-lock-keywords
ad16897c
SM
1948 `(;; Functions.
1949 ("^\\s *def\\s +\\(?:[^( \t\n.]*\\.\\)?\\([^( \t\n]+\\)"
c7fbbc3c 1950 1 font-lock-function-name-face)
ad16897c
SM
1951 ;; Keywords.
1952 (,(concat
1953 ruby-font-lock-keyword-beg-re
1954 (regexp-opt
1955 '("alias"
1956 "and"
1957 "begin"
1958 "break"
1959 "case"
1960 "class"
1961 "def"
1962 "defined?"
1963 "do"
1964 "elsif"
1965 "else"
1966 "fail"
1967 "ensure"
1968 "for"
1969 "end"
1970 "if"
1971 "in"
1972 "module"
1973 "next"
1974 "not"
1975 "or"
1976 "redo"
1977 "rescue"
1978 "retry"
1979 "return"
1980 "then"
1981 "super"
1982 "unless"
1983 "undef"
1984 "until"
1985 "when"
1986 "while"
1987 "yield")
1988 'symbols))
1989 (1 font-lock-keyword-face))
1990 ;; Some core methods.
1991 (,(concat
1992 ruby-font-lock-keyword-beg-re
1993 (regexp-opt
1994 '( ;; built-in methods on Kernel
1995 "__callee__"
1996 "__dir__"
1997 "__method__"
1998 "abort"
1999 "at_exit"
2000 "autoload"
2001 "autoload?"
2002 "binding"
2003 "block_given?"
2004 "caller"
2005 "catch"
2006 "eval"
2007 "exec"
2008 "exit"
2009 "exit!"
2010 "fail"
2011 "fork"
2012 "format"
2013 "lambda"
2014 "load"
2015 "loop"
2016 "open"
2017 "p"
2018 "print"
2019 "printf"
2020 "proc"
2021 "putc"
2022 "puts"
2023 "raise"
2024 "rand"
2025 "readline"
2026 "readlines"
2027 "require"
2028 "require_relative"
2029 "sleep"
2030 "spawn"
2031 "sprintf"
2032 "srand"
2033 "syscall"
2034 "system"
2035 "throw"
2036 "trap"
2037 "warn"
2038 ;; keyword-like private methods on Module
2039 "alias_method"
2040 "attr"
2041 "attr_accessor"
2042 "attr_reader"
2043 "attr_writer"
2044 "define_method"
2045 "extend"
2046 "include"
2047 "module_function"
2048 "prepend"
2049 "private"
2050 "protected"
2051 "public"
2052 "refine"
2053 "using")
2054 'symbols))
2055 (1 font-lock-builtin-face))
2056 ;; Here-doc beginnings.
2057 (,ruby-here-doc-beg-re
2058 (0 (unless (ruby-singleton-class-p (match-beginning 0))
2059 'font-lock-string-face)))
2060 ;; Perl-ish keywords.
2061 "\\_<\\(?:BEGIN\\|END\\)\\_>\\|^__END__$"
2062 ;; Variables.
2063 (,(concat ruby-font-lock-keyword-beg-re
43cebc23 2064 "\\_<\\(nil\\|self\\|true\\|false\\)\\_>")
ac72c08d 2065 1 font-lock-variable-name-face)
ad16897c
SM
2066 ;; Keywords that evaluate to certain values.
2067 ("\\_<__\\(?:LINE\\|ENCODING\\|FILE\\)__\\_>"
2068 (0 font-lock-variable-name-face))
2069 ;; Symbols.
2070 ("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
6c27f0f8 2071 2 font-lock-constant-face)
ad16897c
SM
2072 ;; Variables.
2073 ("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
c7fbbc3c 2074 1 font-lock-variable-name-face)
ad16897c 2075 ("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
c7fbbc3c 2076 0 font-lock-variable-name-face)
ad16897c
SM
2077 ;; Constants.
2078 ("\\(?:\\_<\\|::\\)\\([A-Z]+\\(\\w\\|_\\)*\\)"
48186177 2079 1 (unless (eq ?\( (char-after)) font-lock-type-face))
ad16897c
SM
2080 ("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]"
2081 (2 font-lock-constant-face))
2082 ;; Conversion methods on Kernel.
2083 (,(concat ruby-font-lock-keyword-beg-re
2084 (regexp-opt '("Array" "Complex" "Float" "Hash"
2085 "Integer" "Rational" "String") 'symbols))
2086 (1 font-lock-builtin-face))
2087 ;; Expression expansion.
2088 (ruby-match-expression-expansion
c62792e7 2089 2 font-lock-variable-name-face t)
ad16897c
SM
2090 ;; Negation char.
2091 ("[^[:alnum:]_]\\(!\\)[^=]"
70c46b28 2092 1 font-lock-negation-char-face)
ad16897c
SM
2093 ;; Character literals.
2094 ;; FIXME: Support longer escape sequences.
2095 ("\\_<\\?\\\\?\\S " 0 font-lock-string-face)
2096 )
ee61fe97 2097 "Additional expressions to highlight in Ruby mode.")
c7fbbc3c 2098
0ba2d4b6 2099(defun ruby-match-expression-expansion (limit)
dbb530d9
DG
2100 (let* ((prop 'ruby-expansion-match-data)
2101 (pos (next-single-char-property-change (point) prop nil limit))
2102 value)
2103 (when (and pos (> pos (point)))
c62792e7
DG
2104 (goto-char pos)
2105 (or (and (setq value (get-text-property pos prop))
2106 (progn (set-match-data value) t))
2107 (ruby-match-expression-expansion limit)))))
0ba2d4b6 2108
c7fbbc3c 2109;;;###autoload
cf38dd42 2110(define-derived-mode ruby-mode prog-mode "Ruby"
2ea53115 2111 "Major mode for editing Ruby code.
c7fbbc3c 2112
ae93bc74 2113\\{ruby-mode-map}"
c7fbbc3c
CY
2114 (ruby-mode-variables)
2115
a3996a2e
BB
2116 (setq-local imenu-create-index-function 'ruby-imenu-create-index)
2117 (setq-local add-log-current-defun-function 'ruby-add-log-current-method)
2118 (setq-local beginning-of-defun-function 'ruby-beginning-of-defun)
2119 (setq-local end-of-defun-function 'ruby-end-of-defun)
c7fbbc3c 2120
a9ba094b 2121 (add-hook 'after-save-hook 'ruby-mode-set-encoding nil 'local)
6cddebc1 2122 (add-hook 'electric-indent-functions 'ruby--electric-indent-p nil 'local)
c7fbbc3c 2123
a3996a2e
BB
2124 (setq-local font-lock-defaults '((ruby-font-lock-keywords) nil nil))
2125 (setq-local font-lock-keywords ruby-font-lock-keywords)
2126 (setq-local font-lock-syntax-table ruby-font-lock-syntax-table)
c7fbbc3c 2127
7ffd3721 2128 (setq-local syntax-propertize-function #'ruby-syntax-propertize-function))
c7fbbc3c
CY
2129
2130;;; Invoke ruby-mode when appropriate
2131
2132;;;###autoload
5cf8176d
DG
2133(add-to-list 'auto-mode-alist
2134 (cons (purecopy (concat "\\(?:\\."
f72e2fdb 2135 "rb\\|ru\\|rake\\|thor"
dd806710 2136 "\\|jbuilder\\|gemspec\\|podspec"
5cf8176d 2137 "\\|/"
f72e2fdb 2138 "\\(?:Gem\\|Rake\\|Cap\\|Thor"
dd806710 2139 "Vagrant\\|Guard\\|Pod\\)file"
5cf8176d 2140 "\\)\\'")) 'ruby-mode))
c7fbbc3c
CY
2141
2142;;;###autoload
2a08047a
GM
2143(dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
2144 (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'ruby-mode)))
c7fbbc3c
CY
2145
2146(provide 'ruby-mode)
2147
2148;;; ruby-mode.el ends here