Merge from trunk.
[bpt/emacs.git] / lisp / progmodes / ruby-mode.el
CommitLineData
c7fbbc3c
CY
1;;; ruby-mode.el --- Major mode for editing Ruby files
2
acaf905b 3;; Copyright (C) 1994-2012 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
10;; Version: 1.0
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(eval-when-compile (require 'cl))
43
44a41a47
KR
44(defgroup ruby nil
45 "Major mode for editing Ruby code."
46 :prefix "ruby-"
47 :group 'languages)
48
c7fbbc3c
CY
49(defconst ruby-keyword-end-re
50 (if (string-match "\\_>" "ruby")
51 "\\_>"
52 "\\>"))
53
54(defconst ruby-block-beg-keywords
55 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
56 "Keywords at the beginning of blocks.")
57
58(defconst ruby-block-beg-re
59 (regexp-opt ruby-block-beg-keywords)
60 "Regexp to match the beginning of blocks.")
61
62(defconst ruby-non-block-do-re
63 (concat (regexp-opt '("while" "until" "for" "rescue") t) ruby-keyword-end-re)
64 "Regexp to match keywords that nest without blocks.")
65
66(defconst ruby-indent-beg-re
0ba2d4b6
DG
67 (concat "^\\s *" (regexp-opt '("class" "module" "def" "if" "unless" "case"
68 "while" "until" "for" "begin")) "\\_>")
c7fbbc3c
CY
69 "Regexp to match where the indentation gets deeper.")
70
71(defconst ruby-modifier-beg-keywords
72 '("if" "unless" "while" "until")
73 "Modifiers that are the same as the beginning of blocks.")
74
75(defconst ruby-modifier-beg-re
76 (regexp-opt ruby-modifier-beg-keywords)
77 "Regexp to match modifiers same as the beginning of blocks.")
78
79(defconst ruby-modifier-re
80 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords))
81 "Regexp to match modifiers.")
82
83(defconst ruby-block-mid-keywords
84 '("then" "else" "elsif" "when" "rescue" "ensure")
85 "Keywords where the indentation gets shallower in middle of block statements.")
86
87(defconst ruby-block-mid-re
88 (regexp-opt ruby-block-mid-keywords)
89 "Regexp to match where the indentation gets shallower in middle of block statements.")
90
91(defconst ruby-block-op-keywords
92 '("and" "or" "not")
93 "Regexp to match boolean keywords.")
94
95(defconst ruby-block-hanging-re
96 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords))
97 "Regexp to match hanging block modifiers.")
98
39675016 99(defconst ruby-block-end-re "\\_<end\\_>")
c7fbbc3c 100
8f06acce
DG
101(defconst ruby-defun-beg-re
102 '"\\(def\\|class\\|module\\)"
103 "Regexp to match the beginning of a defun, in the general sense.")
104
cf38dd42
SM
105(eval-and-compile
106 (defconst ruby-here-doc-beg-re
c7fbbc3c 107 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)"
cf38dd42 108 "Regexp to match the beginning of a heredoc."))
c7fbbc3c
CY
109
110(defun ruby-here-doc-end-match ()
111 "Return a regexp to find the end of a heredoc.
112
113This should only be called after matching against `ruby-here-doc-beg-re'."
114 (concat "^"
115 (if (match-string 2) "[ \t]*" nil)
116 (regexp-quote
117 (or (match-string 4)
118 (match-string 5)
119 (match-string 6)))))
120
c7fbbc3c 121(defconst ruby-delimiter
39675016 122 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
c7fbbc3c 123 ruby-block-beg-re
39675016 124 "\\)\\_>\\|" ruby-block-end-re
c7fbbc3c
CY
125 "\\|^=begin\\|" ruby-here-doc-beg-re))
126
127(defconst ruby-negative
128 (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\>\\|"
129 ruby-block-end-re "\\|}\\|\\]\\)")
130 "Regexp to match where the indentation gets shallower.")
131
132(defconst ruby-operator-re "[-,.+*/%&|^~=<>:]"
133 "Regexp to match operators.")
134
135(defconst ruby-symbol-chars "a-zA-Z0-9_"
136 "List of characters that symbol names may contain.")
137(defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]")
138 "Regexp to match symbols.")
139
2122161f 140(define-abbrev-table 'ruby-mode-abbrev-table ()
ee61fe97 141 "Abbrev table in use in Ruby mode buffers.")
c7fbbc3c 142
c7fbbc3c
CY
143(defvar ruby-mode-map
144 (let ((map (make-sparse-keymap)))
c7fbbc3c
CY
145 (define-key map (kbd "M-C-b") 'ruby-backward-sexp)
146 (define-key map (kbd "M-C-f") 'ruby-forward-sexp)
147 (define-key map (kbd "M-C-p") 'ruby-beginning-of-block)
148 (define-key map (kbd "M-C-n") 'ruby-end-of-block)
c7fbbc3c 149 (define-key map (kbd "M-C-q") 'ruby-indent-exp)
0d9e2599 150 (define-key map (kbd "C-c {") 'ruby-toggle-block)
c7fbbc3c 151 map)
ee61fe97 152 "Keymap used in Ruby mode.")
c7fbbc3c
CY
153
154(defvar ruby-mode-syntax-table
155 (let ((table (make-syntax-table)))
156 (modify-syntax-entry ?\' "\"" table)
157 (modify-syntax-entry ?\" "\"" table)
158 (modify-syntax-entry ?\` "\"" table)
159 (modify-syntax-entry ?# "<" table)
160 (modify-syntax-entry ?\n ">" table)
161 (modify-syntax-entry ?\\ "\\" table)
162 (modify-syntax-entry ?$ "." table)
163 (modify-syntax-entry ?? "_" table)
164 (modify-syntax-entry ?_ "_" table)
39675016 165 (modify-syntax-entry ?: "_" table)
c7fbbc3c
CY
166 (modify-syntax-entry ?< "." table)
167 (modify-syntax-entry ?> "." table)
168 (modify-syntax-entry ?& "." table)
169 (modify-syntax-entry ?| "." table)
170 (modify-syntax-entry ?% "." table)
171 (modify-syntax-entry ?= "." table)
172 (modify-syntax-entry ?/ "." table)
173 (modify-syntax-entry ?+ "." table)
174 (modify-syntax-entry ?* "." table)
175 (modify-syntax-entry ?- "." table)
176 (modify-syntax-entry ?\; "." table)
177 (modify-syntax-entry ?\( "()" table)
178 (modify-syntax-entry ?\) ")(" table)
179 (modify-syntax-entry ?\{ "(}" table)
180 (modify-syntax-entry ?\} "){" table)
181 (modify-syntax-entry ?\[ "(]" table)
182 (modify-syntax-entry ?\] ")[" table)
183 table)
ee61fe97 184 "Syntax table to use in Ruby mode.")
c7fbbc3c
CY
185
186(defcustom ruby-indent-tabs-mode nil
ee61fe97 187 "Indentation can insert tabs in Ruby mode if this is non-nil."
c7fbbc3c
CY
188 :type 'boolean :group 'ruby)
189
190(defcustom ruby-indent-level 2
ee61fe97 191 "Indentation of Ruby statements."
c7fbbc3c
CY
192 :type 'integer :group 'ruby)
193
194(defcustom ruby-comment-column 32
195 "Indentation column of comments."
196 :type 'integer :group 'ruby)
197
198(defcustom ruby-deep-arglist t
199 "Deep indent lists in parenthesis when non-nil.
200Also ignores spaces after parenthesis when 'space."
201 :group 'ruby)
202
203(defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
ee61fe97
JB
204 "Deep indent lists in parenthesis when non-nil.
205The value t means continuous line.
c7fbbc3c
CY
206Also ignores spaces after parenthesis when 'space."
207 :group 'ruby)
208
209(defcustom ruby-deep-indent-paren-style 'space
210 "Default deep indent style."
211 :options '(t nil space) :group 'ruby)
212
213(defcustom ruby-encoding-map '((shift_jis . cp932) (shift-jis . cp932))
214 "Alist to map encoding name from Emacs to Ruby."
215 :group 'ruby)
216
217(defcustom ruby-insert-encoding-magic-comment t
ee61fe97 218 "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
c7fbbc3c
CY
219 :type 'boolean :group 'ruby)
220
221(defcustom ruby-use-encoding-map t
222 "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
223 :type 'boolean :group 'ruby)
224
225;; Safe file variables
226(put 'ruby-indent-tabs-mode 'safe-local-variable 'booleanp)
227(put 'ruby-indent-level 'safe-local-variable 'integerp)
228(put 'ruby-comment-column 'safe-local-variable 'integerp)
229(put 'ruby-deep-arglist 'safe-local-variable 'booleanp)
230
231(defun ruby-imenu-create-index-in-block (prefix beg end)
232 "Create an imenu index of methods inside a block."
233 (let ((index-alist '()) (case-fold-search nil)
234 name next pos decl sing)
235 (goto-char beg)
236 (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
237 (setq sing (match-beginning 3))
238 (setq decl (match-string 5))
239 (setq next (match-end 0))
240 (setq name (or (match-string 4) (match-string 6)))
241 (setq pos (match-beginning 0))
242 (cond
243 ((string= "alias" decl)
244 (if prefix (setq name (concat prefix name)))
245 (push (cons name pos) index-alist))
246 ((string= "def" decl)
247 (if prefix
248 (setq name
249 (cond
250 ((string-match "^self\." name)
251 (concat (substring prefix 0 -1) (substring name 4)))
252 (t (concat prefix name)))))
253 (push (cons name pos) index-alist)
254 (ruby-accurate-end-of-block end))
255 (t
256 (if (string= "self" name)
257 (if prefix (setq name (substring prefix 0 -1)))
258 (if prefix (setq name (concat (substring prefix 0 -1) "::" name)))
259 (push (cons name pos) index-alist))
260 (ruby-accurate-end-of-block end)
261 (setq beg (point))
262 (setq index-alist
263 (nconc (ruby-imenu-create-index-in-block
264 (concat name (if sing "." "#"))
265 next beg) index-alist))
266 (goto-char beg))))
267 index-alist))
268
269(defun ruby-imenu-create-index ()
270 "Create an imenu index of all methods in the buffer."
271 (nreverse (ruby-imenu-create-index-in-block nil (point-min) nil)))
272
273(defun ruby-accurate-end-of-block (&optional end)
274 "TODO: document."
275 (let (state
276 (end (or end (point-max))))
277 (while (and (setq state (apply 'ruby-parse-partial end state))
278 (>= (nth 2 state) 0) (< (point) end)))))
279
280(defun ruby-mode-variables ()
ee61fe97 281 "Set up initial buffer-local variables for Ruby mode."
c7fbbc3c
CY
282 (set-syntax-table ruby-mode-syntax-table)
283 (setq local-abbrev-table ruby-mode-abbrev-table)
284 (setq indent-tabs-mode ruby-indent-tabs-mode)
285 (set (make-local-variable 'indent-line-function) 'ruby-indent-line)
286 (set (make-local-variable 'require-final-newline) t)
287 (set (make-local-variable 'comment-start) "# ")
288 (set (make-local-variable 'comment-end) "")
289 (set (make-local-variable 'comment-column) ruby-comment-column)
290 (set (make-local-variable 'comment-start-skip) "#+ *")
291 (set (make-local-variable 'parse-sexp-ignore-comments) t)
292 (set (make-local-variable 'parse-sexp-lookup-properties) t)
293 (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
294 (set (make-local-variable 'paragraph-separate) paragraph-start)
295 (set (make-local-variable 'paragraph-ignore-fill-prefix) t))
296
297(defun ruby-mode-set-encoding ()
298 "Insert a magic comment header with the proper encoding if necessary."
299 (save-excursion
300 (widen)
301 (goto-char (point-min))
302 (when (re-search-forward "[^\0-\177]" nil t)
303 (goto-char (point-min))
304 (let ((coding-system
305 (or coding-system-for-write
306 buffer-file-coding-system)))
307 (if coding-system
308 (setq coding-system
309 (or (coding-system-get coding-system 'mime-charset)
310 (coding-system-change-eol-conversion coding-system nil))))
311 (setq coding-system
312 (if coding-system
313 (symbol-name
314 (or (and ruby-use-encoding-map
315 (cdr (assq coding-system ruby-encoding-map)))
316 coding-system))
317 "ascii-8bit"))
cc9c9831 318 (if (looking-at "^#!") (beginning-of-line 2))
c7fbbc3c
CY
319 (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
320 (unless (string= (match-string 2) coding-system)
321 (goto-char (match-beginning 2))
322 (delete-region (point) (match-end 2))
323 (and (looking-at "-\*-")
324 (let ((n (skip-chars-backward " ")))
325 (cond ((= n 0) (insert " ") (backward-char))
326 ((= n -1) (insert " "))
327 ((forward-char)))))
328 (insert coding-system)))
329 ((looking-at "\\s *#.*coding\\s *[:=]"))
330 (t (when ruby-insert-encoding-magic-comment
331 (insert "# -*- coding: " coding-system " -*-\n"))))))))
332
333(defun ruby-current-indentation ()
334 "Return the indentation level of current line."
335 (save-excursion
336 (beginning-of-line)
337 (back-to-indentation)
338 (current-column)))
339
cf38dd42 340(defun ruby-indent-line (&optional ignored)
ee61fe97 341 "Correct the indentation of the current Ruby line."
c7fbbc3c
CY
342 (interactive)
343 (ruby-indent-to (ruby-calculate-indent)))
344
345(defun ruby-indent-to (column)
346 "Indent the current line to COLUMN."
347 (when column
348 (let (shift top beg)
349 (and (< column 0) (error "invalid nest"))
350 (setq shift (current-column))
351 (beginning-of-line)
352 (setq beg (point))
353 (back-to-indentation)
354 (setq top (current-column))
355 (skip-chars-backward " \t")
356 (if (>= shift top) (setq shift (- shift top))
357 (setq shift 0))
358 (if (and (bolp)
359 (= column top))
360 (move-to-column (+ column shift))
361 (move-to-column top)
362 (delete-region beg (point))
363 (beginning-of-line)
364 (indent-to column)
365 (move-to-column (+ column shift))))))
366
367(defun ruby-special-char-p (&optional pos)
368 "Return t if the character before POS is a special character.
369If omitted, POS defaults to the current point.
370Special characters are `?', `$', `:' when preceded by whitespace,
371and `\\' when preceded by `?'."
372 (setq pos (or pos (point)))
373 (let ((c (char-before pos)) (b (and (< (point-min) pos)
374 (char-before (1- pos)))))
375 (cond ((or (eq c ??) (eq c ?$)))
376 ((and (eq c ?:) (or (not b) (eq (char-syntax b) ? ))))
377 ((eq c ?\\) (eq b ??)))))
378
9cd80478
DG
379(defun ruby-singleton-class-p (&optional pos)
380 (save-excursion
381 (when pos (goto-char pos))
382 (forward-word -1)
383 (and (or (bolp) (not (eq (char-before (point)) ?_)))
384 (looking-at "class\\s *<<"))))
385
c7fbbc3c
CY
386(defun ruby-expr-beg (&optional option)
387 "TODO: document."
388 (save-excursion
389 (store-match-data nil)
9cd80478
DG
390 (let ((space (skip-chars-backward " \t"))
391 (start (point)))
c7fbbc3c
CY
392 (cond
393 ((bolp) t)
394 ((progn
395 (forward-char -1)
396 (and (looking-at "\\?")
397 (or (eq (char-syntax (char-before (point))) ?w)
398 (ruby-special-char-p))))
399 nil)
9cd80478
DG
400 ((and (eq option 'heredoc) (< space 0))
401 (not (progn (goto-char start) (ruby-singleton-class-p))))
c7fbbc3c
CY
402 ((or (looking-at ruby-operator-re)
403 (looking-at "[\\[({,;]")
404 (and (looking-at "[!?]")
405 (or (not (eq option 'modifier))
406 (bolp)
407 (save-excursion (forward-char -1) (looking-at "\\Sw$"))))
408 (and (looking-at ruby-symbol-re)
409 (skip-chars-backward ruby-symbol-chars)
410 (cond
411 ((looking-at (regexp-opt
412 (append ruby-block-beg-keywords
413 ruby-block-op-keywords
414 ruby-block-mid-keywords)
415 'words))
416 (goto-char (match-end 0))
9cd80478 417 (not (looking-at "\\s_\\|!")))
c7fbbc3c
CY
418 ((eq option 'expr-qstr)
419 (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
420 ((eq option 'expr-re)
421 (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
422 (t nil)))))))))
423
424(defun ruby-forward-string (term &optional end no-error expand)
425 "TODO: document."
426 (let ((n 1) (c (string-to-char term))
427 (re (if expand
428 (concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
429 (concat "[^\\]\\(\\\\\\\\\\)*[" term "]"))))
430 (while (and (re-search-forward re end no-error)
431 (if (match-beginning 3)
432 (ruby-forward-string "}{" end no-error nil)
433 (> (setq n (if (eq (char-before (point)) c)
434 (1- n) (1+ n))) 0)))
435 (forward-char -1))
436 (cond ((zerop n))
437 (no-error nil)
438 ((error "unterminated string")))))
439
440(defun ruby-deep-indent-paren-p (c)
ee61fe97 441 "TODO: document."
c7fbbc3c
CY
442 (cond ((listp ruby-deep-indent-paren)
443 (let ((deep (assoc c ruby-deep-indent-paren)))
444 (cond (deep
445 (or (cdr deep) ruby-deep-indent-paren-style))
446 ((memq c ruby-deep-indent-paren)
447 ruby-deep-indent-paren-style))))
448 ((eq c ruby-deep-indent-paren) ruby-deep-indent-paren-style)
449 ((eq c ?\( ) ruby-deep-arglist)))
450
451(defun ruby-parse-partial (&optional end in-string nest depth pcol indent)
ee61fe97 452 "TODO: document throughout function body."
c7fbbc3c
CY
453 (or depth (setq depth 0))
454 (or indent (setq indent 0))
455 (when (re-search-forward ruby-delimiter end 'move)
456 (let ((pnt (point)) w re expand)
457 (goto-char (match-beginning 0))
458 (cond
459 ((and (memq (char-before) '(?@ ?$)) (looking-at "\\sw"))
460 (goto-char pnt))
461 ((looking-at "[\"`]") ;skip string
462 (cond
463 ((and (not (eobp))
464 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t))
465 nil)
466 (t
467 (setq in-string (point))
468 (goto-char end))))
469 ((looking-at "'")
470 (cond
471 ((and (not (eobp))
472 (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t))
473 nil)
474 (t
475 (setq in-string (point))
476 (goto-char end))))
477 ((looking-at "/=")
478 (goto-char pnt))
479 ((looking-at "/")
480 (cond
481 ((and (not (eobp)) (ruby-expr-beg 'expr-re))
482 (if (ruby-forward-string "/" end t t)
483 nil
484 (setq in-string (point))
485 (goto-char end)))
486 (t
487 (goto-char pnt))))
488 ((looking-at "%")
489 (cond
490 ((and (not (eobp))
491 (ruby-expr-beg 'expr-qstr)
492 (not (looking-at "%="))
493 (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
494 (goto-char (match-beginning 1))
495 (setq expand (not (memq (char-before) '(?q ?w))))
496 (setq w (match-string 1))
497 (cond
498 ((string= w "[") (setq re "]["))
499 ((string= w "{") (setq re "}{"))
500 ((string= w "(") (setq re ")("))
501 ((string= w "<") (setq re "><"))
502 ((and expand (string= w "\\"))
503 (setq w (concat "\\" w))))
504 (unless (cond (re (ruby-forward-string re end t expand))
505 (expand (ruby-forward-string w end t t))
506 (t (re-search-forward
507 (if (string= w "\\")
508 "\\\\[^\\]*\\\\"
509 (concat "[^\\]\\(\\\\\\\\\\)*" w))
510 end t)))
511 (setq in-string (point))
512 (goto-char end)))
513 (t
514 (goto-char pnt))))
515 ((looking-at "\\?") ;skip ?char
516 (cond
517 ((and (ruby-expr-beg)
518 (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
519 (goto-char (match-end 0)))
520 (t
521 (goto-char pnt))))
522 ((looking-at "\\$") ;skip $char
523 (goto-char pnt)
524 (forward-char 1))
525 ((looking-at "#") ;skip comment
526 (forward-line 1)
527 (goto-char (point))
528 )
529 ((looking-at "[\\[{(]")
530 (let ((deep (ruby-deep-indent-paren-p (char-after))))
531 (if (and deep (or (not (eq (char-after) ?\{)) (ruby-expr-beg)))
532 (progn
533 (and (eq deep 'space) (looking-at ".\\s +[^# \t\n]")
534 (setq pnt (1- (match-end 0))))
535 (setq nest (cons (cons (char-after (point)) pnt) nest))
536 (setq pcol (cons (cons pnt depth) pcol))
537 (setq depth 0))
538 (setq nest (cons (cons (char-after (point)) pnt) nest))
539 (setq depth (1+ depth))))
540 (goto-char pnt)
541 )
542 ((looking-at "[])}]")
543 (if (ruby-deep-indent-paren-p (matching-paren (char-after)))
544 (setq depth (cdr (car pcol)) pcol (cdr pcol))
545 (setq depth (1- depth)))
546 (setq nest (cdr nest))
547 (goto-char pnt))
548 ((looking-at ruby-block-end-re)
549 (if (or (and (not (bolp))
550 (progn
551 (forward-char -1)
552 (setq w (char-after (point)))
553 (or (eq ?_ w)
554 (eq ?. w))))
555 (progn
556 (goto-char pnt)
557 (setq w (char-after (point)))
558 (or (eq ?_ w)
559 (eq ?! w)
560 (eq ?? w))))
561 nil
562 (setq nest (cdr nest))
563 (setq depth (1- depth)))
564 (goto-char pnt))
565 ((looking-at "def\\s +[^(\n;]*")
566 (if (or (bolp)
567 (progn
568 (forward-char -1)
569 (not (eq ?_ (char-after (point))))))
570 (progn
571 (setq nest (cons (cons nil pnt) nest))
572 (setq depth (1+ depth))))
573 (goto-char (match-end 0)))
39675016 574 ((looking-at (concat "\\_<\\(" ruby-block-beg-re "\\)\\_>"))
c7fbbc3c
CY
575 (and
576 (save-match-data
577 (or (not (looking-at (concat "do" ruby-keyword-end-re)))
578 (save-excursion
579 (back-to-indentation)
580 (not (looking-at ruby-non-block-do-re)))))
581 (or (bolp)
582 (progn
583 (forward-char -1)
584 (setq w (char-after (point)))
585 (not (or (eq ?_ w)
586 (eq ?. w)))))
587 (goto-char pnt)
588 (setq w (char-after (point)))
c7fbbc3c 589 (not (eq ?! w))
c7fbbc3c
CY
590 (skip-chars-forward " \t")
591 (goto-char (match-beginning 0))
592 (or (not (looking-at ruby-modifier-re))
593 (ruby-expr-beg 'modifier))
594 (goto-char pnt)
595 (setq nest (cons (cons nil pnt) nest))
596 (setq depth (1+ depth)))
597 (goto-char pnt))
598 ((looking-at ":\\(['\"]\\)")
599 (goto-char (match-beginning 1))
c28662a8 600 (ruby-forward-string (match-string 1) end t))
11473529 601 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
c7fbbc3c
CY
602 (goto-char (match-end 0)))
603 ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
604 (goto-char (match-end 0)))
605 ((or (looking-at "\\.\\.\\.?")
606 (looking-at "\\.[0-9]+")
607 (looking-at "\\.[a-zA-Z_0-9]+")
608 (looking-at "\\."))
609 (goto-char (match-end 0)))
610 ((looking-at "^=begin")
611 (if (re-search-forward "^=end" end t)
612 (forward-line 1)
613 (setq in-string (match-end 0))
614 (goto-char end)))
615 ((looking-at "<<")
616 (cond
617 ((and (ruby-expr-beg 'heredoc)
618 (looking-at "<<\\(-\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
619 (setq re (regexp-quote (or (match-string 4) (match-string 2))))
620 (if (match-beginning 1) (setq re (concat "\\s *" re)))
621 (let* ((id-end (goto-char (match-end 0)))
e180ab9f 622 (line-end-position (point-at-eol))
c7fbbc3c
CY
623 (state (list in-string nest depth pcol indent)))
624 ;; parse the rest of the line
625 (while (and (> line-end-position (point))
626 (setq state (apply 'ruby-parse-partial
627 line-end-position state))))
628 (setq in-string (car state)
629 nest (nth 1 state)
630 depth (nth 2 state)
631 pcol (nth 3 state)
632 indent (nth 4 state))
633 ;; skip heredoc section
634 (if (re-search-forward (concat "^" re "$") end 'move)
635 (forward-line 1)
636 (setq in-string id-end)
637 (goto-char end))))
638 (t
639 (goto-char pnt))))
640 ((looking-at "^__END__$")
641 (goto-char pnt))
642 ((and (looking-at ruby-here-doc-beg-re)
643 (boundp 'ruby-indent-point))
644 (if (re-search-forward (ruby-here-doc-end-match)
645 ruby-indent-point t)
646 (forward-line 1)
647 (setq in-string (match-end 0))
648 (goto-char ruby-indent-point)))
649 (t
650 (error (format "bad string %s"
651 (buffer-substring (point) pnt)
652 ))))))
653 (list in-string nest depth pcol))
654
655(defun ruby-parse-region (start end)
656 "TODO: document."
657 (let (state)
658 (save-excursion
659 (if start
660 (goto-char start)
661 (ruby-beginning-of-indent))
662 (save-restriction
663 (narrow-to-region (point) end)
664 (while (and (> end (point))
665 (setq state (apply 'ruby-parse-partial end state))))))
666 (list (nth 0 state) ; in-string
667 (car (nth 1 state)) ; nest
668 (nth 2 state) ; depth
669 (car (car (nth 3 state))) ; pcol
670 ;(car (nth 5 state)) ; indent
671 )))
672
673(defun ruby-indent-size (pos nest)
ee61fe97 674 "Return the indentation level in spaces NEST levels deeper than POS."
c7fbbc3c
CY
675 (+ pos (* (or nest 1) ruby-indent-level)))
676
677(defun ruby-calculate-indent (&optional parse-start)
ee61fe97 678 "Return the proper indentation level of the current line."
c7fbbc3c
CY
679 ;; TODO: Document body
680 (save-excursion
681 (beginning-of-line)
682 (let ((ruby-indent-point (point))
683 (case-fold-search nil)
cf38dd42 684 state eol begin op-end
c7fbbc3c
CY
685 (paren (progn (skip-syntax-forward " ")
686 (and (char-after) (matching-paren (char-after)))))
687 (indent 0))
688 (if parse-start
689 (goto-char parse-start)
690 (ruby-beginning-of-indent)
691 (setq parse-start (point)))
692 (back-to-indentation)
693 (setq indent (current-column))
694 (setq state (ruby-parse-region parse-start ruby-indent-point))
695 (cond
696 ((nth 0 state) ; within string
697 (setq indent nil)) ; do nothing
698 ((car (nth 1 state)) ; in paren
699 (goto-char (setq begin (cdr (nth 1 state))))
700 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state)))))
701 (if deep
702 (cond ((and (eq deep t) (eq (car (nth 1 state)) paren))
703 (skip-syntax-backward " ")
704 (setq indent (1- (current-column))))
705 ((let ((s (ruby-parse-region (point) ruby-indent-point)))
706 (and (nth 2 s) (> (nth 2 s) 0)
707 (or (goto-char (cdr (nth 1 s))) t)))
708 (forward-word -1)
709 (setq indent (ruby-indent-size (current-column)
710 (nth 2 state))))
711 (t
712 (setq indent (current-column))
713 (cond ((eq deep 'space))
714 (paren (setq indent (1- indent)))
715 (t (setq indent (ruby-indent-size (1- indent) 1))))))
716 (if (nth 3 state) (goto-char (nth 3 state))
717 (goto-char parse-start) (back-to-indentation))
718 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
719 (and (eq (car (nth 1 state)) paren)
720 (ruby-deep-indent-paren-p (matching-paren paren))
721 (search-backward (char-to-string paren))
722 (setq indent (current-column)))))
723 ((and (nth 2 state) (> (nth 2 state) 0)) ; in nest
724 (if (null (cdr (nth 1 state)))
725 (error "invalid nest"))
726 (goto-char (cdr (nth 1 state)))
727 (forward-word -1) ; skip back a keyword
728 (setq begin (point))
729 (cond
730 ((looking-at "do\\>[^_]") ; iter block is a special case
731 (if (nth 3 state) (goto-char (nth 3 state))
732 (goto-char parse-start) (back-to-indentation))
733 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
734 (t
735 (setq indent (+ (current-column) ruby-indent-level)))))
ee61fe97 736
c7fbbc3c
CY
737 ((and (nth 2 state) (< (nth 2 state) 0)) ; in negative nest
738 (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
739 (when indent
740 (goto-char ruby-indent-point)
741 (end-of-line)
742 (setq eol (point))
743 (beginning-of-line)
744 (cond
745 ((and (not (ruby-deep-indent-paren-p paren))
746 (re-search-forward ruby-negative eol t))
747 (and (not (eq ?_ (char-after (match-end 0))))
748 (setq indent (- indent ruby-indent-level))))
749 ((and
750 (save-excursion
751 (beginning-of-line)
752 (not (bobp)))
753 (or (ruby-deep-indent-paren-p t)
754 (null (car (nth 1 state)))))
755 ;; goto beginning of non-empty no-comment line
756 (let (end done)
757 (while (not done)
758 (skip-chars-backward " \t\n")
759 (setq end (point))
760 (beginning-of-line)
761 (if (re-search-forward "^\\s *#" end t)
762 (beginning-of-line)
763 (setq done t))))
c7fbbc3c
CY
764 (end-of-line)
765 ;; skip the comment at the end
766 (skip-chars-backward " \t")
767 (let (end (pos (point)))
768 (beginning-of-line)
769 (while (and (re-search-forward "#" pos t)
770 (setq end (1- (point)))
771 (or (ruby-special-char-p end)
772 (and (setq state (ruby-parse-region parse-start end))
773 (nth 0 state))))
774 (setq end nil))
775 (goto-char (or end pos))
776 (skip-chars-backward " \t")
777 (setq begin (if (and end (nth 0 state)) pos (cdr (nth 1 state))))
778 (setq state (ruby-parse-region parse-start (point))))
779 (or (bobp) (forward-char -1))
780 (and
781 (or (and (looking-at ruby-symbol-re)
782 (skip-chars-backward ruby-symbol-chars)
783 (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>"))
784 (not (eq (point) (nth 3 state)))
785 (save-excursion
786 (goto-char (match-end 0))
787 (not (looking-at "[a-z_]"))))
788 (and (looking-at ruby-operator-re)
789 (not (ruby-special-char-p))
dfbd787f 790 ;; Operator at the end of line.
c7fbbc3c
CY
791 (let ((c (char-after (point))))
792 (and
793;; (or (null begin)
794;; (save-excursion
795;; (goto-char begin)
796;; (skip-chars-forward " \t")
797;; (not (or (eolp) (looking-at "#")
798;; (and (eq (car (nth 1 state)) ?{)
799;; (looking-at "|"))))))
e636fafe 800 ;; Not a regexp or percent literal.
dfbd787f
SM
801 (null (nth 0 (ruby-parse-region (or begin parse-start)
802 (point))))
c7fbbc3c
CY
803 (or (not (eq ?| (char-after (point))))
804 (save-excursion
805 (or (eolp) (forward-char -1))
806 (cond
807 ((search-backward "|" nil t)
808 (skip-chars-backward " \t\n")
809 (and (not (eolp))
810 (progn
811 (forward-char -1)
812 (not (looking-at "{")))
813 (progn
814 (forward-word -1)
815 (not (looking-at "do\\>[^_]")))))
816 (t t))))
817 (not (eq ?, c))
818 (setq op-end t)))))
819 (setq indent
820 (cond
821 ((and
822 (null op-end)
823 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>")))
824 (eq (ruby-deep-indent-paren-p t) 'space)
825 (not (bobp)))
826 (widen)
827 (goto-char (or begin parse-start))
828 (skip-syntax-forward " ")
829 (current-column))
830 ((car (nth 1 state)) indent)
831 (t
832 (+ indent ruby-indent-level))))))))
833 (goto-char ruby-indent-point)
834 (beginning-of-line)
835 (skip-syntax-forward " ")
836 (if (looking-at "\\.[^.]")
837 (+ indent ruby-indent-level)
838 indent))))
839
c7fbbc3c
CY
840(defun ruby-beginning-of-defun (&optional arg)
841 "Move backward to the beginning of the current top-level defun.
ee61fe97 842With ARG, move backward multiple defuns. Negative ARG means
c7fbbc3c
CY
843move forward."
844 (interactive "p")
8f06acce 845 (and (re-search-backward (concat "^\\s *" ruby-defun-beg-re "\\_>")
d81ceaaf 846 nil t (or arg 1))
c7fbbc3c
CY
847 (beginning-of-line)))
848
849(defun ruby-end-of-defun (&optional arg)
850 "Move forward to the end of the current top-level defun.
ee61fe97 851With ARG, move forward multiple defuns. Negative ARG means
c7fbbc3c
CY
852move backward."
853 (interactive "p")
8f06acce
DG
854 (ruby-forward-sexp)
855 (when (looking-back (concat "^\\s *" ruby-block-end-re))
856 (forward-line 1)))
c7fbbc3c
CY
857
858(defun ruby-beginning-of-indent ()
0ba2d4b6
DG
859 "Backtrack to a line which can be used as a reference for
860calculating indentation on the lines after it."
861 (while (and (re-search-backward ruby-indent-beg-re nil 'move)
862 (if (ruby-in-ppss-context-p 'anything)
863 t
864 ;; We can stop, then.
865 (beginning-of-line)))))
c7fbbc3c
CY
866
867(defun ruby-move-to-block (n)
ee61fe97 868 "Move to the beginning (N < 0) or the end (N > 0) of the current block
c7fbbc3c
CY
869or blocks containing the current block."
870 ;; TODO: Make this work for n > 1,
871 ;; make it not loop for n = 0,
872 ;; document body
0d9e2599
NN
873 (let ((orig (point))
874 (start (ruby-calculate-indent))
875 (down (looking-at (if (< n 0) ruby-block-end-re
876 (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))))
877 pos done)
c7fbbc3c
CY
878 (while (and (not done) (not (if (< n 0) (bobp) (eobp))))
879 (forward-line n)
880 (cond
881 ((looking-at "^\\s *$"))
882 ((looking-at "^\\s *#"))
883 ((and (> n 0) (looking-at "^=begin\\>"))
884 (re-search-forward "^=end\\>"))
885 ((and (< n 0) (looking-at "^=end\\>"))
886 (re-search-backward "^=begin\\>"))
887 (t
888 (setq pos (current-indentation))
889 (cond
890 ((< start pos)
891 (setq down t))
892 ((and down (= pos start))
893 (setq done t))
894 ((> start pos)
895 (setq done t)))))
896 (if done
897 (save-excursion
898 (back-to-indentation)
899 (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>"))
0d9e2599 900 (setq done nil)))))
d1e1e53d 901 (back-to-indentation)))
c7fbbc3c
CY
902
903(defun ruby-beginning-of-block (&optional arg)
904 "Move backward to the beginning of the current block.
905With ARG, move up multiple blocks."
906 (interactive "p")
907 (ruby-move-to-block (- (or arg 1))))
908
909(defun ruby-end-of-block (&optional arg)
910 "Move forward to the end of the current block.
911With ARG, move out of multiple blocks."
912 ;; Passing a value > 1 to ruby-move-to-block currently doesn't work.
913 (interactive)
914 (ruby-move-to-block (or arg 1)))
915
916(defun ruby-forward-sexp (&optional arg)
917 "Move forward across one balanced expression (sexp).
ee61fe97 918With ARG, do it many times. Negative ARG means move backward."
c7fbbc3c
CY
919 ;; TODO: Document body
920 (interactive "p")
921 (if (and (numberp arg) (< arg 0))
922 (ruby-backward-sexp (- arg))
923 (let ((i (or arg 1)))
924 (condition-case nil
925 (while (> i 0)
926 (skip-syntax-forward " ")
cc9c9831 927 (if (looking-at ",\\s *") (goto-char (match-end 0)))
c7fbbc3c
CY
928 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
929 (goto-char (match-end 0)))
930 ((progn
931 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
932 (looking-at "\\s("))
933 (goto-char (scan-sexps (point) 1)))
934 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
935 (not (eq (char-before (point)) ?.))
936 (not (eq (char-before (point)) ?:)))
937 (ruby-end-of-block)
938 (forward-word 1))
939 ((looking-at "\\(\\$\\|@@?\\)?\\sw")
940 (while (progn
941 (while (progn (forward-word 1) (looking-at "_")))
942 (cond ((looking-at "::") (forward-char 2) t)
943 ((> (skip-chars-forward ".") 0))
944 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
945 (forward-char 1) nil)))))
946 ((let (state expr)
947 (while
948 (progn
949 (setq expr (or expr (ruby-expr-beg)
950 (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
951 (nth 1 (setq state (apply 'ruby-parse-partial nil state))))
952 (setq expr t)
953 (skip-chars-forward "<"))
954 (not expr))))
955 (setq i (1- i)))
956 ((error) (forward-word 1)))
957 i)))
958
959(defun ruby-backward-sexp (&optional arg)
960 "Move backward across one balanced expression (sexp).
ee61fe97 961With ARG, do it many times. Negative ARG means move forward."
c7fbbc3c
CY
962 ;; TODO: Document body
963 (interactive "p")
964 (if (and (numberp arg) (< arg 0))
965 (ruby-forward-sexp (- arg))
966 (let ((i (or arg 1)))
967 (condition-case nil
968 (while (> i 0)
969 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
970 (forward-char -1)
971 (cond ((looking-at "\\s)")
972 (goto-char (scan-sexps (1+ (point)) -1))
973 (case (char-before)
974 (?% (forward-char -1))
0adf5618 975 ((?q ?Q ?w ?W ?r ?x)
c7fbbc3c
CY
976 (if (eq (char-before (1- (point))) ?%) (forward-char -2))))
977 nil)
978 ((looking-at "\\s\"\\|\\\\\\S_")
979 (let ((c (char-to-string (char-before (match-end 0)))))
980 (while (and (search-backward c)
981 (eq (logand (skip-chars-backward "\\") 1)
982 1))))
983 nil)
984 ((looking-at "\\s.\\|\\s\\")
985 (if (ruby-special-char-p) (forward-char -1)))
986 ((looking-at "\\s(") nil)
987 (t
988 (forward-char 1)
989 (while (progn (forward-word -1)
990 (case (char-before)
991 (?_ t)
992 (?. (forward-char -1) t)
993 ((?$ ?@)
994 (forward-char -1)
995 (and (eq (char-before) (char-after)) (forward-char -1)))
996 (?:
997 (forward-char -1)
998 (eq (char-before) :)))))
999 (if (looking-at ruby-block-end-re)
1000 (ruby-beginning-of-block))
1001 nil))
1002 (setq i (1- i)))
1003 ((error)))
1004 i)))
1005
cf38dd42
SM
1006(defun ruby-indent-exp (&optional ignored)
1007 "Indent each line in the balanced expression following the point."
c7fbbc3c
CY
1008 (interactive "*P")
1009 (let ((here (point-marker)) start top column (nest t))
1010 (set-marker-insertion-type here t)
1011 (unwind-protect
1012 (progn
1013 (beginning-of-line)
1014 (setq start (point) top (current-indentation))
1015 (while (and (not (eobp))
1016 (progn
1017 (setq column (ruby-calculate-indent start))
1018 (cond ((> column top)
1019 (setq nest t))
1020 ((and (= column top) nest)
1021 (setq nest nil) t))))
1022 (ruby-indent-to column)
1023 (beginning-of-line 2)))
1024 (goto-char here)
1025 (set-marker here nil))))
1026
1027(defun ruby-add-log-current-method ()
ee61fe97 1028 "Return the current method name as a string.
c7fbbc3c
CY
1029This string includes all namespaces.
1030
1031For example:
1032
1033 #exit
1034 String#gsub
1035 Net::HTTP#active?
5745cae6 1036 File.open
c7fbbc3c
CY
1037
1038See `add-log-current-defun-function'."
c7fbbc3c
CY
1039 (condition-case nil
1040 (save-excursion
1041 (let (mname mlist (indent 0))
5745cae6 1042 ;; Get the current method definition (or class/module).
c7fbbc3c 1043 (if (re-search-backward
8f06acce 1044 (concat "^[ \t]*" ruby-defun-beg-re "[ \t]+"
c7fbbc3c 1045 "\\("
5745cae6
DG
1046 ;; \\. and :: for class methods
1047 "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
1048 "+\\)")
c7fbbc3c
CY
1049 nil t)
1050 (progn
1051 (setq mname (match-string 2))
1052 (unless (string-equal "def" (match-string 1))
1053 (setq mlist (list mname) mname nil))
1054 (goto-char (match-beginning 1))
1055 (setq indent (current-column))
1056 (beginning-of-line)))
5745cae6 1057 ;; Walk up the class/module nesting.
c7fbbc3c
CY
1058 (while (and (> indent 0)
1059 (re-search-backward
1060 (concat
1061 "^[ \t]*\\(class\\|module\\)[ \t]+"
1062 "\\([A-Z]" ruby-symbol-re "*\\)")
1063 nil t))
1064 (goto-char (match-beginning 1))
1065 (if (< (current-column) indent)
1066 (progn
1067 (setq mlist (cons (match-string 2) mlist))
1068 (setq indent (current-column))
1069 (beginning-of-line))))
5745cae6 1070 ;; Process the method name.
c7fbbc3c
CY
1071 (when mname
1072 (let ((mn (split-string mname "\\.\\|::")))
1073 (if (cdr mn)
1074 (progn
5745cae6
DG
1075 (unless (string-equal "self" (car mn)) ; def self.foo
1076 ;; def C.foo
1077 (let ((ml (nreverse mlist)))
1078 ;; If the method name references one of the
1079 ;; containing modules, drop the more nested ones.
c7fbbc3c
CY
1080 (while ml
1081 (if (string-equal (car ml) (car mn))
1082 (setq mlist (nreverse (cdr ml)) ml nil))
5745cae6
DG
1083 (or (setq ml (cdr ml)) (nreverse mlist))))
1084 (if mlist
1085 (setcdr (last mlist) (butlast mn))
1086 (setq mlist (butlast mn))))
1087 (setq mname (concat "." (car (last mn)))))
c7fbbc3c 1088 (setq mname (concat "#" mname)))))
5745cae6 1089 ;; Generate the string.
c7fbbc3c
CY
1090 (if (consp mlist)
1091 (setq mlist (mapconcat (function identity) mlist "::")))
1092 (if mname
1093 (if mlist (concat mlist mname) mname)
1094 mlist)))))
1095
c3268831
DG
1096(defun ruby-brace-to-do-end (orig end)
1097 (let (beg-marker end-marker)
1098 (goto-char end)
1099 (when (eq (char-before) ?\})
1100 (delete-char -1)
32fb8162
DG
1101 (when (save-excursion
1102 (skip-chars-backward " \t")
1103 (not (bolp)))
c3268831
DG
1104 (insert "\n"))
1105 (insert "end")
1106 (setq end-marker (point-marker))
1107 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w))
1108 (insert " "))
1109 (goto-char orig)
1110 (delete-char 1)
1111 (when (eq (char-syntax (char-before)) ?w)
1112 (insert " "))
1113 (insert "do")
1114 (setq beg-marker (point-marker))
1115 (when (looking-at "\\(\\s \\)*|")
1116 (unless (match-beginning 1)
1117 (insert " "))
1118 (goto-char (1+ (match-end 0)))
1119 (search-forward "|"))
1120 (unless (looking-at "\\s *$")
1121 (insert "\n"))
1122 (indent-region beg-marker end-marker)
1123 (goto-char beg-marker)
1124 t)))
1125
1126(defun ruby-do-end-to-brace (orig end)
32fb8162
DG
1127 (let (beg-marker end-marker beg-pos end-pos)
1128 (goto-char (- end 3))
1129 (when (looking-at ruby-block-end-re)
1130 (delete-char 3)
1131 (setq end-marker (point-marker))
1132 (insert "}")
1133 (goto-char orig)
1134 (delete-char 2)
1135 (insert "{")
1136 (setq beg-marker (point-marker))
1137 (when (looking-at "\\s +|")
1138 (delete-char (- (match-end 0) (match-beginning 0) 1))
1139 (forward-char)
1140 (re-search-forward "|" (line-end-position) t))
1141 (save-excursion
1142 (skip-chars-forward " \t\n\r")
1143 (setq beg-pos (point))
1144 (goto-char end-marker)
1145 (skip-chars-backward " \t\n\r")
1146 (setq end-pos (point)))
1147 (when (or
1148 (< end-pos beg-pos)
1149 (and (= (line-number-at-pos beg-pos) (line-number-at-pos end-pos))
1150 (< (+ (current-column) (- end-pos beg-pos) 2) fill-column)))
1151 (just-one-space -1)
1152 (goto-char end-marker)
1153 (just-one-space -1))
1154 (goto-char beg-marker)
1155 t)))
0d9e2599
NN
1156
1157(defun ruby-toggle-block ()
c3268831
DG
1158 "Toggle block type from do-end to braces or back.
1159The block must begin on the current line or above it and end after the point.
1160If the result is do-end block, it will always be multiline."
0d9e2599 1161 (interactive)
c3268831
DG
1162 (let ((start (point)) beg end)
1163 (end-of-line)
1164 (unless
1165 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1166 (progn
1167 (setq beg (point))
1168 (save-match-data (ruby-forward-sexp))
1169 (setq end (point))
1170 (> end start)))
1171 (if (match-beginning 1)
1172 (ruby-brace-to-do-end beg end)
1173 (ruby-do-end-to-brace beg end)))
1174 (goto-char start))))
0d9e2599 1175
f4ff702e 1176(declare-function ruby-syntax-propertize-heredoc "ruby-mode" (limit))
e636fafe
DG
1177(declare-function ruby-syntax-enclosing-percent-literal "ruby-mode" (limit))
1178(declare-function ruby-syntax-propertize-percent-literal "ruby-mode" (limit))
f4ff702e 1179
cf38dd42
SM
1180(if (eval-when-compile (fboundp #'syntax-propertize-rules))
1181 ;; New code that works independently from font-lock.
1182 (progn
e636fafe
DG
1183 (eval-and-compile
1184 (defconst ruby-percent-literal-beg-re
1185 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
f063063a
DG
1186 "Regexp to match the beginning of percent literal.")
1187
1188 (defconst ruby-syntax-methods-before-regexp
1189 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1190 "assert_match" "Given" "Then" "When")
1191 "Methods that can take regexp as the first argument.
1192It will be properly highlighted even when the call omits parens."))
e636fafe 1193
cf38dd42
SM
1194 (defun ruby-syntax-propertize-function (start end)
1195 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1196 (goto-char start)
1197 (ruby-syntax-propertize-heredoc end)
e636fafe 1198 (ruby-syntax-enclosing-percent-literal end)
cf38dd42
SM
1199 (funcall
1200 (syntax-propertize-rules
dfbd787f
SM
1201 ;; $' $" $` .... are variables.
1202 ;; ?' ?" ?` are ascii codes.
21bb5ce0
SM
1203 ("\\([?$]\\)[#\"'`]"
1204 (1 (unless (save-excursion
1205 ;; Not within a string.
1206 (nth 3 (syntax-ppss (match-beginning 0))))
1207 (string-to-syntax "\\"))))
f063063a
DG
1208 ;; Regexps: regexps are distinguished from division because
1209 ;; of the keyword, symbol, or method name before them.
dfbd787f
SM
1210 ((concat
1211 ;; Special tokens that can't be followed by a division operator.
f063063a
DG
1212 "\\(^\\|[[=(,~?:;<>]"
1213 ;; Control flow keywords and operators following bol or whitespace.
1214 "\\|\\(?:^\\|\\s \\)"
dfbd787f 1215 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
f063063a
DG
1216 "or" "not" "&&" "||"))
1217 ;; Method name from the list.
1218 "\\|\\_<"
1219 (regexp-opt ruby-syntax-methods-before-regexp)
1220 "\\)\\s *"
dfbd787f 1221 ;; The regular expression itself.
f063063a
DG
1222 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1223 (2 (string-to-syntax "\"/"))
1224 (3 (string-to-syntax "\"/")))
cf38dd42
SM
1225 ("^=en\\(d\\)\\_>" (1 "!"))
1226 ("^\\(=\\)begin\\_>" (1 "!"))
1227 ;; Handle here documents.
1228 ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
9cd80478
DG
1229 (7 (unless (ruby-singleton-class-p (match-beginning 0))
1230 (put-text-property (match-beginning 7) (match-end 7)
1231 'syntax-table (string-to-syntax "\""))
1232 (ruby-syntax-propertize-heredoc end))))
85222d44 1233 ;; Handle percent literals: %w(), %q{}, etc.
e636fafe
DG
1234 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re)
1235 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end)))))
cf38dd42
SM
1236 (point) end))
1237
1238 (defun ruby-syntax-propertize-heredoc (limit)
1239 (let ((ppss (syntax-ppss))
1240 (res '()))
1241 (when (eq ?\n (nth 3 ppss))
1242 (save-excursion
1243 (goto-char (nth 8 ppss))
1244 (beginning-of-line)
1245 (while (re-search-forward ruby-here-doc-beg-re
1246 (line-end-position) t)
9cd80478
DG
1247 (unless (ruby-singleton-class-p (match-beginning 0))
1248 (push (concat (ruby-here-doc-end-match) "\n") res))))
cf38dd42
SM
1249 (let ((start (point)))
1250 ;; With multiple openers on the same line, we don't know in which
1251 ;; part `start' is, so we have to go back to the beginning.
1252 (when (cdr res)
1253 (goto-char (nth 8 ppss))
1254 (setq res (nreverse res)))
1255 (while (and res (re-search-forward (pop res) limit 'move))
1256 (if (null res)
1257 (put-text-property (1- (point)) (point)
1258 'syntax-table (string-to-syntax "\""))))
1259 ;; Make extra sure we don't move back, lest we could fall into an
1260 ;; inf-loop.
1261 (if (< (point) start) (goto-char start))))))
85222d44 1262
e636fafe
DG
1263 (defun ruby-syntax-enclosing-percent-literal (limit)
1264 (let ((state (syntax-ppss))
1265 (start (point)))
1266 ;; When already inside percent literal, re-propertize it.
85222d44
DG
1267 (when (eq t (nth 3 state))
1268 (goto-char (nth 8 state))
e636fafe
DG
1269 (when (looking-at ruby-percent-literal-beg-re)
1270 (ruby-syntax-propertize-percent-literal limit))
1271 (when (< (point) start) (goto-char start)))))
85222d44 1272
e636fafe 1273 (defun ruby-syntax-propertize-percent-literal (limit)
85222d44 1274 (goto-char (match-beginning 2))
e636fafe
DG
1275 ;; Not inside a simple string or comment.
1276 (when (eq t (nth 3 (syntax-ppss)))
1277 (let* ((op (char-after))
1278 (ops (char-to-string op))
1279 (cl (or (cdr (aref (syntax-table) op))
1280 (cdr (assoc op '((?< . ?>))))))
1281 parse-sexp-lookup-properties)
1282 (condition-case nil
1283 (progn
1284 (if cl ; Paired delimiters.
1285 ;; Delimiter pairs of the same kind can be nested
1286 ;; inside the literal, as long as they are balanced.
1287 ;; Create syntax table that ignores other characters.
1288 (with-syntax-table (make-char-table 'syntax-table nil)
1289 (modify-syntax-entry op (concat "(" (char-to-string cl)))
1290 (modify-syntax-entry cl (concat ")" ops))
1291 (modify-syntax-entry ?\\ "\\")
1292 (save-restriction
1293 (narrow-to-region (point) limit)
1294 (forward-list))) ; skip to the paired character
1295 ;; Single character delimiter.
1296 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1297 (regexp-quote ops)) limit nil))
1298 ;; Found the closing delimiter.
1299 (put-text-property (1- (point)) (point) 'syntax-table
1300 (string-to-syntax "|")))
1301 ;; Unclosed literal, leave the following text unpropertized.
1302 ((scan-error search-failed) (goto-char limit))))))
cf38dd42 1303 )
f4ff702e 1304
cf38dd42
SM
1305 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1306 ;; fallback on the old font-lock-syntactic-keywords stuff.
1307
1308 (defconst ruby-here-doc-end-re
1309 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1310 "Regexp to match the end of heredocs.
1311
1312This will actually match any line with one or more characters.
1313It's useful in that it divides up the match string so that
1314`ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1315
1316 (defun ruby-here-doc-beg-match ()
1317 "Return a regexp to find the beginning of a heredoc.
1318
1319This should only be called after matching against `ruby-here-doc-end-re'."
06d8ace5
GM
1320 (let ((contents (concat
1321 (regexp-quote (concat (match-string 2) (match-string 3)))
1322 (if (string= (match-string 3) "_") "\\B" "\\b"))))
cf38dd42
SM
1323 (concat "<<"
1324 (let ((match (match-string 1)))
1325 (if (and match (> (length match) 0))
06d8ace5
GM
1326 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1327 (match-string 1) "\\)"
1328 contents "\\(\\1\\|\\2\\)")
1329 (concat "-?\\([\"']\\|\\)" contents "\\1"))))))
cf38dd42
SM
1330
1331 (defconst ruby-font-lock-syntactic-keywords
0ba2d4b6 1332 `(
c7fbbc3c
CY
1333 ;; the last $', $", $` in the respective string is not variable
1334 ;; the last ?', ?", ?` in the respective string is not ascii code
1335 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1336 (2 (7 . nil))
1337 (4 (7 . nil)))
1338 ;; $' $" $` .... are variables
1339 ;; ?' ?" ?` are ascii codes
1340 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil))
1341 ;; regexps
cc9c9831 1342 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
c7fbbc3c
CY
1343 (4 (7 . ?/))
1344 (6 (7 . ?/)))
1345 ("^=en\\(d\\)\\_>" 1 "!")
e636fafe 1346 ;; Percent literal.
85222d44
DG
1347 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1348 (3 "\"")
1349 (5 "\""))
c7fbbc3c
CY
1350 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1351 ;; Currently, the following case is highlighted incorrectly:
1352 ;;
1353 ;; <<FOO
1354 ;; FOO
1355 ;; <<BAR
1356 ;; <<BAZ
1357 ;; BAZ
1358 ;; BAR
1359 ;;
1360 ;; This is because all here-doc beginnings are highlighted before any endings,
1361 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1362 ;; it thinks <<BAR is part of a string so it's marked as well.
1363 ;;
1364 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1365 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1366 ;; but I don't want to try that until we've got unit tests set up
1367 ;; to make sure I don't break anything else.
1368 (,(concat ruby-here-doc-beg-re ".*\\(\n\\)")
1369 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re))
1370 (ruby-here-doc-beg-syntax))
1371 (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax)))
94584c34 1372 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
cf38dd42
SM
1373
1374 (defun ruby-comment-beg-syntax ()
ee61fe97 1375 "Return the syntax cell for a the first character of a =begin.
c7fbbc3c
CY
1376See the definition of `ruby-font-lock-syntactic-keywords'.
1377
1378This returns a comment-delimiter cell as long as the =begin
1379isn't in a string or another comment."
cf38dd42
SM
1380 (when (not (nth 3 (syntax-ppss)))
1381 (string-to-syntax "!")))
1382
1383 (defun ruby-in-here-doc-p ()
1384 "Return whether or not the point is in a heredoc."
1385 (save-excursion
1386 (let ((old-point (point)) (case-fold-search nil))
1387 (beginning-of-line)
1388 (catch 'found-beg
9cd80478
DG
1389 (while (and (re-search-backward ruby-here-doc-beg-re nil t)
1390 (not (ruby-singleton-class-p)))
cf38dd42
SM
1391 (if (not (or (ruby-in-ppss-context-p 'anything)
1392 (ruby-here-doc-find-end old-point)))
1393 (throw 'found-beg t)))))))
1394
1395 (defun ruby-here-doc-find-end (&optional limit)
1396 "Expects the point to be on a line with one or more heredoc openers.
1397Returns the buffer position at which all heredocs on the line
1398are terminated, or nil if they aren't terminated before the
1399buffer position `limit' or the end of the buffer."
1400 (save-excursion
1401 (beginning-of-line)
1402 (catch 'done
e180ab9f 1403 (let ((eol (point-at-eol))
cf38dd42
SM
1404 (case-fold-search nil)
1405 ;; Fake match data such that (match-end 0) is at eol
1406 (end-match-data (progn (looking-at ".*$") (match-data)))
1407 beg-match-data end-re)
1408 (while (re-search-forward ruby-here-doc-beg-re eol t)
1409 (setq beg-match-data (match-data))
1410 (setq end-re (ruby-here-doc-end-match))
1411
1412 (set-match-data end-match-data)
1413 (goto-char (match-end 0))
1414 (unless (re-search-forward end-re limit t) (throw 'done nil))
1415 (setq end-match-data (match-data))
1416
1417 (set-match-data beg-match-data)
1418 (goto-char (match-end 0)))
1419 (set-match-data end-match-data)
1420 (goto-char (match-end 0))
1421 (point)))))
1422
1423 (defun ruby-here-doc-beg-syntax ()
1424 "Return the syntax cell for a line that may begin a heredoc.
1425See the definition of `ruby-font-lock-syntactic-keywords'.
1426
1427This sets the syntax cell for the newline ending the line
1428containing the heredoc beginning so that cases where multiple
1429heredocs are started on one line are handled correctly."
1430 (save-excursion
1431 (goto-char (match-beginning 0))
1432 (unless (or (ruby-in-ppss-context-p 'non-heredoc)
1433 (ruby-in-here-doc-p))
1434 (string-to-syntax "\""))))
1435
1436 (defun ruby-here-doc-end-syntax ()
1437 "Return the syntax cell for a line that may end a heredoc.
1438See the definition of `ruby-font-lock-syntactic-keywords'."
1439 (let ((pss (syntax-ppss)) (case-fold-search nil))
1440 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1441 ;; so we can just give up.
1442 ;; This means we aren't doing a full-document search
1443 ;; every time we enter a character.
1444 (when (ruby-in-ppss-context-p 'heredoc pss)
1445 (save-excursion
1446 (goto-char (nth 8 pss)) ; Go to the beginning of heredoc.
1447 (let ((eol (point)))
1448 (beginning-of-line)
1449 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line...
1450 (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment...
1451 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1452 (not (re-search-forward ruby-here-doc-beg-re eol t))))
1453 (string-to-syntax "\"")))))))
c7fbbc3c 1454
cf38dd42
SM
1455 (unless (functionp 'syntax-ppss)
1456 (defun syntax-ppss (&optional pos)
1457 (parse-partial-sexp (point-min) (or pos (point)))))
1458 )
c7fbbc3c
CY
1459
1460(defun ruby-in-ppss-context-p (context &optional ppss)
1461 (let ((ppss (or ppss (syntax-ppss (point)))))
1462 (if (cond
1463 ((eq context 'anything)
1464 (or (nth 3 ppss)
1465 (nth 4 ppss)))
1466 ((eq context 'string)
1467 (nth 3 ppss))
1468 ((eq context 'heredoc)
cf38dd42 1469 (eq ?\n (nth 3 ppss)))
c7fbbc3c
CY
1470 ((eq context 'non-heredoc)
1471 (and (ruby-in-ppss-context-p 'anything)
1472 (not (ruby-in-ppss-context-p 'heredoc))))
1473 ((eq context 'comment)
1474 (nth 4 ppss))
1475 (t
1476 (error (concat
1477 "Internal error on `ruby-in-ppss-context-p': "
1478 "context name `" (symbol-name context) "' is unknown"))))
1479 t)))
1480
c7fbbc3c
CY
1481(if (featurep 'xemacs)
1482 (put 'ruby-mode 'font-lock-defaults
1483 '((ruby-font-lock-keywords)
1484 nil nil nil
1485 beginning-of-line
1486 (font-lock-syntactic-keywords
1487 . ruby-font-lock-syntactic-keywords))))
1488
1489(defvar ruby-font-lock-syntax-table
1490 (let ((tbl (copy-syntax-table ruby-mode-syntax-table)))
1491 (modify-syntax-entry ?_ "w" tbl)
1492 tbl)
ee61fe97 1493 "The syntax table to use for fontifying Ruby mode buffers.
c7fbbc3c
CY
1494See `font-lock-syntax-table'.")
1495
1496(defconst ruby-font-lock-keywords
1497 (list
1498 ;; functions
1499 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1500 1 font-lock-function-name-face)
1501 ;; keywords
1502 (cons (concat
1503 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1504 (regexp-opt
1505 '("alias_method"
1506 "alias"
1507 "and"
1508 "begin"
1509 "break"
1510 "case"
1511 "catch"
1512 "class"
1513 "def"
1514 "do"
1515 "elsif"
1516 "else"
1517 "fail"
1518 "ensure"
1519 "for"
1520 "end"
1521 "if"
1522 "in"
1523 "module_function"
1524 "module"
1525 "next"
1526 "not"
1527 "or"
1528 "public"
1529 "private"
1530 "protected"
1531 "raise"
1532 "redo"
1533 "rescue"
1534 "retry"
1535 "return"
1536 "then"
1537 "throw"
1538 "super"
1539 "unless"
1540 "undef"
1541 "until"
1542 "when"
1543 "while"
1544 "yield")
1545 t)
1546 "\\)"
1547 ruby-keyword-end-re)
1548 2)
1549 ;; here-doc beginnings
f178c32d
DG
1550 `(,ruby-here-doc-beg-re 0 (unless (ruby-singleton-class-p (match-beginning 0))
1551 'font-lock-string-face))
c7fbbc3c
CY
1552 ;; variables
1553 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1554 2 font-lock-variable-name-face)
0ba2d4b6
DG
1555 ;; symbols
1556 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
6c27f0f8 1557 2 font-lock-constant-face)
c7fbbc3c
CY
1558 ;; variables
1559 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1560 1 font-lock-variable-name-face)
1561 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1562 0 font-lock-variable-name-face)
c7fbbc3c
CY
1563 ;; constants
1564 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1565 2 font-lock-type-face)
6c27f0f8 1566 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face)
c7fbbc3c 1567 ;; expression expansion
0ba2d4b6 1568 '(ruby-match-expression-expansion
c7fbbc3c
CY
1569 0 font-lock-variable-name-face t)
1570 ;; warn lower camel case
1571 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1572 ; 0 font-lock-warning-face)
1573 )
ee61fe97 1574 "Additional expressions to highlight in Ruby mode.")
c7fbbc3c 1575
0ba2d4b6
DG
1576(defun ruby-match-expression-expansion (limit)
1577 (when (re-search-forward "[^\\]\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)" limit 'move)
616c6c36 1578 (or (ruby-in-ppss-context-p 'string)
e6aca219 1579 (ruby-match-expression-expansion limit))))
0ba2d4b6 1580
c7fbbc3c 1581;;;###autoload
cf38dd42 1582(define-derived-mode ruby-mode prog-mode "Ruby"
c7fbbc3c
CY
1583 "Major mode for editing Ruby scripts.
1584\\[ruby-indent-line] properly indents subexpressions of multi-line
1585class, module, def, if, while, for, do, and case statements, taking
1586nesting into account.
1587
ee61fe97
JB
1588The variable `ruby-indent-level' controls the amount of indentation.
1589
c7fbbc3c 1590\\{ruby-mode-map}"
c7fbbc3c
CY
1591 (ruby-mode-variables)
1592
1593 (set (make-local-variable 'imenu-create-index-function)
1594 'ruby-imenu-create-index)
1595 (set (make-local-variable 'add-log-current-defun-function)
1596 'ruby-add-log-current-method)
4489104f
DG
1597 (set (make-local-variable 'beginning-of-defun-function)
1598 'ruby-beginning-of-defun)
1599 (set (make-local-variable 'end-of-defun-function)
1600 'ruby-end-of-defun)
c7fbbc3c
CY
1601
1602 (add-hook
cf38dd42 1603 (cond ((boundp 'before-save-hook) 'before-save-hook)
c7fbbc3c
CY
1604 ((boundp 'write-contents-functions) 'write-contents-functions)
1605 ((boundp 'write-contents-hooks) 'write-contents-hooks))
cf38dd42
SM
1606 'ruby-mode-set-encoding nil 'local)
1607
1608 (set (make-local-variable 'electric-indent-chars)
2122161f 1609 (append '(?\{ ?\}) electric-indent-chars))
c7fbbc3c
CY
1610
1611 (set (make-local-variable 'font-lock-defaults)
1612 '((ruby-font-lock-keywords) nil nil))
1613 (set (make-local-variable 'font-lock-keywords)
1614 ruby-font-lock-keywords)
1615 (set (make-local-variable 'font-lock-syntax-table)
1616 ruby-font-lock-syntax-table)
c7fbbc3c 1617
cf38dd42
SM
1618 (if (eval-when-compile (fboundp 'syntax-propertize-rules))
1619 (set (make-local-variable 'syntax-propertize-function)
1620 #'ruby-syntax-propertize-function)
1621 (set (make-local-variable 'font-lock-syntactic-keywords)
1622 ruby-font-lock-syntactic-keywords)))
c7fbbc3c
CY
1623
1624;;; Invoke ruby-mode when appropriate
1625
1626;;;###autoload
a7610c52 1627(add-to-list 'auto-mode-alist (cons (purecopy "\\.rb\\'") 'ruby-mode))
cabc040a
JD
1628;;;###autoload
1629(add-to-list 'auto-mode-alist '("Rakefile\\'" . ruby-mode))
c7fbbc3c
CY
1630
1631;;;###autoload
4d270537 1632(dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
a7610c52 1633 (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'ruby-mode)))
c7fbbc3c
CY
1634
1635(provide 'ruby-mode)
1636
1637;;; ruby-mode.el ends here