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