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