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