* lisp/progmodes/ruby-mode.el (ruby-font-lock-keywords): Never
[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?
1036 File::open.
1037
1038See `add-log-current-defun-function'."
1039 ;; TODO: Document body
1040 ;; Why does this append a period to class methods?
1041 (condition-case nil
1042 (save-excursion
1043 (let (mname mlist (indent 0))
1044 ;; get current method (or class/module)
1045 (if (re-search-backward
8f06acce 1046 (concat "^[ \t]*" ruby-defun-beg-re "[ \t]+"
c7fbbc3c
CY
1047 "\\("
1048 ;; \\. and :: for class method
1049 "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
1050 "+\\)")
1051 nil t)
1052 (progn
1053 (setq mname (match-string 2))
1054 (unless (string-equal "def" (match-string 1))
1055 (setq mlist (list mname) mname nil))
1056 (goto-char (match-beginning 1))
1057 (setq indent (current-column))
1058 (beginning-of-line)))
1059 ;; nest class/module
1060 (while (and (> indent 0)
1061 (re-search-backward
1062 (concat
1063 "^[ \t]*\\(class\\|module\\)[ \t]+"
1064 "\\([A-Z]" ruby-symbol-re "*\\)")
1065 nil t))
1066 (goto-char (match-beginning 1))
1067 (if (< (current-column) indent)
1068 (progn
1069 (setq mlist (cons (match-string 2) mlist))
1070 (setq indent (current-column))
1071 (beginning-of-line))))
1072 (when mname
1073 (let ((mn (split-string mname "\\.\\|::")))
1074 (if (cdr mn)
1075 (progn
1076 (cond
1077 ((string-equal "" (car mn))
1078 (setq mn (cdr mn) mlist nil))
1079 ((string-equal "self" (car mn))
1080 (setq mn (cdr mn)))
1081 ((let ((ml (nreverse mlist)))
1082 (while ml
1083 (if (string-equal (car ml) (car mn))
1084 (setq mlist (nreverse (cdr ml)) ml nil))
1085 (or (setq ml (cdr ml)) (nreverse mlist))))))
1086 (if mlist
1087 (setcdr (last mlist) mn)
1088 (setq mlist mn))
1089 (setq mn (last mn 2))
1090 (setq mname (concat "." (cadr mn)))
1091 (setcdr mn nil))
1092 (setq mname (concat "#" mname)))))
1093 ;; generate string
1094 (if (consp mlist)
1095 (setq mlist (mapconcat (function identity) mlist "::")))
1096 (if mname
1097 (if mlist (concat mlist mname) mname)
1098 mlist)))))
1099
c3268831
DG
1100(defun ruby-brace-to-do-end (orig end)
1101 (let (beg-marker end-marker)
1102 (goto-char end)
1103 (when (eq (char-before) ?\})
1104 (delete-char -1)
32fb8162
DG
1105 (when (save-excursion
1106 (skip-chars-backward " \t")
1107 (not (bolp)))
c3268831
DG
1108 (insert "\n"))
1109 (insert "end")
1110 (setq end-marker (point-marker))
1111 (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w))
1112 (insert " "))
1113 (goto-char orig)
1114 (delete-char 1)
1115 (when (eq (char-syntax (char-before)) ?w)
1116 (insert " "))
1117 (insert "do")
1118 (setq beg-marker (point-marker))
1119 (when (looking-at "\\(\\s \\)*|")
1120 (unless (match-beginning 1)
1121 (insert " "))
1122 (goto-char (1+ (match-end 0)))
1123 (search-forward "|"))
1124 (unless (looking-at "\\s *$")
1125 (insert "\n"))
1126 (indent-region beg-marker end-marker)
1127 (goto-char beg-marker)
1128 t)))
1129
1130(defun ruby-do-end-to-brace (orig end)
32fb8162
DG
1131 (let (beg-marker end-marker beg-pos end-pos)
1132 (goto-char (- end 3))
1133 (when (looking-at ruby-block-end-re)
1134 (delete-char 3)
1135 (setq end-marker (point-marker))
1136 (insert "}")
1137 (goto-char orig)
1138 (delete-char 2)
1139 (insert "{")
1140 (setq beg-marker (point-marker))
1141 (when (looking-at "\\s +|")
1142 (delete-char (- (match-end 0) (match-beginning 0) 1))
1143 (forward-char)
1144 (re-search-forward "|" (line-end-position) t))
1145 (save-excursion
1146 (skip-chars-forward " \t\n\r")
1147 (setq beg-pos (point))
1148 (goto-char end-marker)
1149 (skip-chars-backward " \t\n\r")
1150 (setq end-pos (point)))
1151 (when (or
1152 (< end-pos beg-pos)
1153 (and (= (line-number-at-pos beg-pos) (line-number-at-pos end-pos))
1154 (< (+ (current-column) (- end-pos beg-pos) 2) fill-column)))
1155 (just-one-space -1)
1156 (goto-char end-marker)
1157 (just-one-space -1))
1158 (goto-char beg-marker)
1159 t)))
0d9e2599
NN
1160
1161(defun ruby-toggle-block ()
c3268831
DG
1162 "Toggle block type from do-end to braces or back.
1163The block must begin on the current line or above it and end after the point.
1164If the result is do-end block, it will always be multiline."
0d9e2599 1165 (interactive)
c3268831
DG
1166 (let ((start (point)) beg end)
1167 (end-of-line)
1168 (unless
1169 (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
1170 (progn
1171 (setq beg (point))
1172 (save-match-data (ruby-forward-sexp))
1173 (setq end (point))
1174 (> end start)))
1175 (if (match-beginning 1)
1176 (ruby-brace-to-do-end beg end)
1177 (ruby-do-end-to-brace beg end)))
1178 (goto-char start))))
0d9e2599 1179
f4ff702e 1180(declare-function ruby-syntax-propertize-heredoc "ruby-mode" (limit))
e636fafe
DG
1181(declare-function ruby-syntax-enclosing-percent-literal "ruby-mode" (limit))
1182(declare-function ruby-syntax-propertize-percent-literal "ruby-mode" (limit))
f4ff702e 1183
cf38dd42
SM
1184(if (eval-when-compile (fboundp #'syntax-propertize-rules))
1185 ;; New code that works independently from font-lock.
1186 (progn
e636fafe
DG
1187 (eval-and-compile
1188 (defconst ruby-percent-literal-beg-re
1189 "\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
f063063a
DG
1190 "Regexp to match the beginning of percent literal.")
1191
1192 (defconst ruby-syntax-methods-before-regexp
1193 '("gsub" "gsub!" "sub" "sub!" "scan" "split" "split!" "index" "match"
1194 "assert_match" "Given" "Then" "When")
1195 "Methods that can take regexp as the first argument.
1196It will be properly highlighted even when the call omits parens."))
e636fafe 1197
cf38dd42
SM
1198 (defun ruby-syntax-propertize-function (start end)
1199 "Syntactic keywords for Ruby mode. See `syntax-propertize-function'."
1200 (goto-char start)
1201 (ruby-syntax-propertize-heredoc end)
e636fafe 1202 (ruby-syntax-enclosing-percent-literal end)
cf38dd42
SM
1203 (funcall
1204 (syntax-propertize-rules
dfbd787f
SM
1205 ;; $' $" $` .... are variables.
1206 ;; ?' ?" ?` are ascii codes.
21bb5ce0
SM
1207 ("\\([?$]\\)[#\"'`]"
1208 (1 (unless (save-excursion
1209 ;; Not within a string.
1210 (nth 3 (syntax-ppss (match-beginning 0))))
1211 (string-to-syntax "\\"))))
f063063a
DG
1212 ;; Regexps: regexps are distinguished from division because
1213 ;; of the keyword, symbol, or method name before them.
dfbd787f
SM
1214 ((concat
1215 ;; Special tokens that can't be followed by a division operator.
f063063a
DG
1216 "\\(^\\|[[=(,~?:;<>]"
1217 ;; Control flow keywords and operators following bol or whitespace.
1218 "\\|\\(?:^\\|\\s \\)"
dfbd787f 1219 (regexp-opt '("if" "elsif" "unless" "while" "until" "when" "and"
f063063a
DG
1220 "or" "not" "&&" "||"))
1221 ;; Method name from the list.
1222 "\\|\\_<"
1223 (regexp-opt ruby-syntax-methods-before-regexp)
1224 "\\)\\s *"
dfbd787f 1225 ;; The regular expression itself.
f063063a
DG
1226 "\\(/\\)[^/\n\\\\]*\\(?:\\\\.[^/\n\\\\]*\\)*\\(/\\)")
1227 (2 (string-to-syntax "\"/"))
1228 (3 (string-to-syntax "\"/")))
cf38dd42
SM
1229 ("^=en\\(d\\)\\_>" (1 "!"))
1230 ("^\\(=\\)begin\\_>" (1 "!"))
1231 ;; Handle here documents.
1232 ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
9cd80478
DG
1233 (7 (unless (ruby-singleton-class-p (match-beginning 0))
1234 (put-text-property (match-beginning 7) (match-end 7)
1235 'syntax-table (string-to-syntax "\""))
1236 (ruby-syntax-propertize-heredoc end))))
85222d44 1237 ;; Handle percent literals: %w(), %q{}, etc.
e636fafe
DG
1238 ((concat "\\(?:^\\|[[ \t\n<+(,=]\\)" ruby-percent-literal-beg-re)
1239 (1 (prog1 "|" (ruby-syntax-propertize-percent-literal end)))))
cf38dd42
SM
1240 (point) end))
1241
1242 (defun ruby-syntax-propertize-heredoc (limit)
1243 (let ((ppss (syntax-ppss))
1244 (res '()))
1245 (when (eq ?\n (nth 3 ppss))
1246 (save-excursion
1247 (goto-char (nth 8 ppss))
1248 (beginning-of-line)
1249 (while (re-search-forward ruby-here-doc-beg-re
1250 (line-end-position) t)
9cd80478
DG
1251 (unless (ruby-singleton-class-p (match-beginning 0))
1252 (push (concat (ruby-here-doc-end-match) "\n") res))))
cf38dd42
SM
1253 (let ((start (point)))
1254 ;; With multiple openers on the same line, we don't know in which
1255 ;; part `start' is, so we have to go back to the beginning.
1256 (when (cdr res)
1257 (goto-char (nth 8 ppss))
1258 (setq res (nreverse res)))
1259 (while (and res (re-search-forward (pop res) limit 'move))
1260 (if (null res)
1261 (put-text-property (1- (point)) (point)
1262 'syntax-table (string-to-syntax "\""))))
1263 ;; Make extra sure we don't move back, lest we could fall into an
1264 ;; inf-loop.
1265 (if (< (point) start) (goto-char start))))))
85222d44 1266
e636fafe
DG
1267 (defun ruby-syntax-enclosing-percent-literal (limit)
1268 (let ((state (syntax-ppss))
1269 (start (point)))
1270 ;; When already inside percent literal, re-propertize it.
85222d44
DG
1271 (when (eq t (nth 3 state))
1272 (goto-char (nth 8 state))
e636fafe
DG
1273 (when (looking-at ruby-percent-literal-beg-re)
1274 (ruby-syntax-propertize-percent-literal limit))
1275 (when (< (point) start) (goto-char start)))))
85222d44 1276
e636fafe 1277 (defun ruby-syntax-propertize-percent-literal (limit)
85222d44 1278 (goto-char (match-beginning 2))
e636fafe
DG
1279 ;; Not inside a simple string or comment.
1280 (when (eq t (nth 3 (syntax-ppss)))
1281 (let* ((op (char-after))
1282 (ops (char-to-string op))
1283 (cl (or (cdr (aref (syntax-table) op))
1284 (cdr (assoc op '((?< . ?>))))))
1285 parse-sexp-lookup-properties)
1286 (condition-case nil
1287 (progn
1288 (if cl ; Paired delimiters.
1289 ;; Delimiter pairs of the same kind can be nested
1290 ;; inside the literal, as long as they are balanced.
1291 ;; Create syntax table that ignores other characters.
1292 (with-syntax-table (make-char-table 'syntax-table nil)
1293 (modify-syntax-entry op (concat "(" (char-to-string cl)))
1294 (modify-syntax-entry cl (concat ")" ops))
1295 (modify-syntax-entry ?\\ "\\")
1296 (save-restriction
1297 (narrow-to-region (point) limit)
1298 (forward-list))) ; skip to the paired character
1299 ;; Single character delimiter.
1300 (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
1301 (regexp-quote ops)) limit nil))
1302 ;; Found the closing delimiter.
1303 (put-text-property (1- (point)) (point) 'syntax-table
1304 (string-to-syntax "|")))
1305 ;; Unclosed literal, leave the following text unpropertized.
1306 ((scan-error search-failed) (goto-char limit))))))
cf38dd42 1307 )
f4ff702e 1308
cf38dd42
SM
1309 ;; For Emacsen where syntax-propertize-rules is not (yet) available,
1310 ;; fallback on the old font-lock-syntactic-keywords stuff.
1311
1312 (defconst ruby-here-doc-end-re
1313 "^\\([ \t]+\\)?\\(.*\\)\\(\n\\)"
1314 "Regexp to match the end of heredocs.
1315
1316This will actually match any line with one or more characters.
1317It's useful in that it divides up the match string so that
1318`ruby-here-doc-beg-match' can search for the beginning of the heredoc.")
1319
1320 (defun ruby-here-doc-beg-match ()
1321 "Return a regexp to find the beginning of a heredoc.
1322
1323This should only be called after matching against `ruby-here-doc-end-re'."
06d8ace5
GM
1324 (let ((contents (concat
1325 (regexp-quote (concat (match-string 2) (match-string 3)))
1326 (if (string= (match-string 3) "_") "\\B" "\\b"))))
cf38dd42
SM
1327 (concat "<<"
1328 (let ((match (match-string 1)))
1329 (if (and match (> (length match) 0))
06d8ace5
GM
1330 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)"
1331 (match-string 1) "\\)"
1332 contents "\\(\\1\\|\\2\\)")
1333 (concat "-?\\([\"']\\|\\)" contents "\\1"))))))
cf38dd42
SM
1334
1335 (defconst ruby-font-lock-syntactic-keywords
0ba2d4b6 1336 `(
c7fbbc3c
CY
1337 ;; the last $', $", $` in the respective string is not variable
1338 ;; the last ?', ?", ?` in the respective string is not ascii code
1339 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)"
1340 (2 (7 . nil))
1341 (4 (7 . nil)))
1342 ;; $' $" $` .... are variables
1343 ;; ?' ?" ?` are ascii codes
1344 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil))
1345 ;; regexps
cc9c9831 1346 ("\\(^\\|[[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
c7fbbc3c
CY
1347 (4 (7 . ?/))
1348 (6 (7 . ?/)))
1349 ("^=en\\(d\\)\\_>" 1 "!")
e636fafe 1350 ;; Percent literal.
85222d44
DG
1351 ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1352 (3 "\"")
1353 (5 "\""))
c7fbbc3c
CY
1354 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
1355 ;; Currently, the following case is highlighted incorrectly:
1356 ;;
1357 ;; <<FOO
1358 ;; FOO
1359 ;; <<BAR
1360 ;; <<BAZ
1361 ;; BAZ
1362 ;; BAR
1363 ;;
1364 ;; This is because all here-doc beginnings are highlighted before any endings,
1365 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ
1366 ;; it thinks <<BAR is part of a string so it's marked as well.
1367 ;;
1368 ;; This may be fixable by modifying ruby-in-here-doc-p to use
1369 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context,
1370 ;; but I don't want to try that until we've got unit tests set up
1371 ;; to make sure I don't break anything else.
1372 (,(concat ruby-here-doc-beg-re ".*\\(\n\\)")
1373 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re))
1374 (ruby-here-doc-beg-syntax))
1375 (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax)))
94584c34 1376 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.")
cf38dd42
SM
1377
1378 (defun ruby-comment-beg-syntax ()
ee61fe97 1379 "Return the syntax cell for a the first character of a =begin.
c7fbbc3c
CY
1380See the definition of `ruby-font-lock-syntactic-keywords'.
1381
1382This returns a comment-delimiter cell as long as the =begin
1383isn't in a string or another comment."
cf38dd42
SM
1384 (when (not (nth 3 (syntax-ppss)))
1385 (string-to-syntax "!")))
1386
1387 (defun ruby-in-here-doc-p ()
1388 "Return whether or not the point is in a heredoc."
1389 (save-excursion
1390 (let ((old-point (point)) (case-fold-search nil))
1391 (beginning-of-line)
1392 (catch 'found-beg
9cd80478
DG
1393 (while (and (re-search-backward ruby-here-doc-beg-re nil t)
1394 (not (ruby-singleton-class-p)))
cf38dd42
SM
1395 (if (not (or (ruby-in-ppss-context-p 'anything)
1396 (ruby-here-doc-find-end old-point)))
1397 (throw 'found-beg t)))))))
1398
1399 (defun ruby-here-doc-find-end (&optional limit)
1400 "Expects the point to be on a line with one or more heredoc openers.
1401Returns the buffer position at which all heredocs on the line
1402are terminated, or nil if they aren't terminated before the
1403buffer position `limit' or the end of the buffer."
1404 (save-excursion
1405 (beginning-of-line)
1406 (catch 'done
e180ab9f 1407 (let ((eol (point-at-eol))
cf38dd42
SM
1408 (case-fold-search nil)
1409 ;; Fake match data such that (match-end 0) is at eol
1410 (end-match-data (progn (looking-at ".*$") (match-data)))
1411 beg-match-data end-re)
1412 (while (re-search-forward ruby-here-doc-beg-re eol t)
1413 (setq beg-match-data (match-data))
1414 (setq end-re (ruby-here-doc-end-match))
1415
1416 (set-match-data end-match-data)
1417 (goto-char (match-end 0))
1418 (unless (re-search-forward end-re limit t) (throw 'done nil))
1419 (setq end-match-data (match-data))
1420
1421 (set-match-data beg-match-data)
1422 (goto-char (match-end 0)))
1423 (set-match-data end-match-data)
1424 (goto-char (match-end 0))
1425 (point)))))
1426
1427 (defun ruby-here-doc-beg-syntax ()
1428 "Return the syntax cell for a line that may begin a heredoc.
1429See the definition of `ruby-font-lock-syntactic-keywords'.
1430
1431This sets the syntax cell for the newline ending the line
1432containing the heredoc beginning so that cases where multiple
1433heredocs are started on one line are handled correctly."
1434 (save-excursion
1435 (goto-char (match-beginning 0))
1436 (unless (or (ruby-in-ppss-context-p 'non-heredoc)
1437 (ruby-in-here-doc-p))
1438 (string-to-syntax "\""))))
1439
1440 (defun ruby-here-doc-end-syntax ()
1441 "Return the syntax cell for a line that may end a heredoc.
1442See the definition of `ruby-font-lock-syntactic-keywords'."
1443 (let ((pss (syntax-ppss)) (case-fold-search nil))
1444 ;; If we aren't in a string, we definitely aren't ending a heredoc,
1445 ;; so we can just give up.
1446 ;; This means we aren't doing a full-document search
1447 ;; every time we enter a character.
1448 (when (ruby-in-ppss-context-p 'heredoc pss)
1449 (save-excursion
1450 (goto-char (nth 8 pss)) ; Go to the beginning of heredoc.
1451 (let ((eol (point)))
1452 (beginning-of-line)
1453 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line...
1454 (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment...
1455 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1456 (not (re-search-forward ruby-here-doc-beg-re eol t))))
1457 (string-to-syntax "\"")))))))
c7fbbc3c 1458
cf38dd42
SM
1459 (unless (functionp 'syntax-ppss)
1460 (defun syntax-ppss (&optional pos)
1461 (parse-partial-sexp (point-min) (or pos (point)))))
1462 )
c7fbbc3c
CY
1463
1464(defun ruby-in-ppss-context-p (context &optional ppss)
1465 (let ((ppss (or ppss (syntax-ppss (point)))))
1466 (if (cond
1467 ((eq context 'anything)
1468 (or (nth 3 ppss)
1469 (nth 4 ppss)))
1470 ((eq context 'string)
1471 (nth 3 ppss))
1472 ((eq context 'heredoc)
cf38dd42 1473 (eq ?\n (nth 3 ppss)))
c7fbbc3c
CY
1474 ((eq context 'non-heredoc)
1475 (and (ruby-in-ppss-context-p 'anything)
1476 (not (ruby-in-ppss-context-p 'heredoc))))
1477 ((eq context 'comment)
1478 (nth 4 ppss))
1479 (t
1480 (error (concat
1481 "Internal error on `ruby-in-ppss-context-p': "
1482 "context name `" (symbol-name context) "' is unknown"))))
1483 t)))
1484
c7fbbc3c
CY
1485(if (featurep 'xemacs)
1486 (put 'ruby-mode 'font-lock-defaults
1487 '((ruby-font-lock-keywords)
1488 nil nil nil
1489 beginning-of-line
1490 (font-lock-syntactic-keywords
1491 . ruby-font-lock-syntactic-keywords))))
1492
1493(defvar ruby-font-lock-syntax-table
1494 (let ((tbl (copy-syntax-table ruby-mode-syntax-table)))
1495 (modify-syntax-entry ?_ "w" tbl)
1496 tbl)
ee61fe97 1497 "The syntax table to use for fontifying Ruby mode buffers.
c7fbbc3c
CY
1498See `font-lock-syntax-table'.")
1499
1500(defconst ruby-font-lock-keywords
1501 (list
1502 ;; functions
1503 '("^\\s *def\\s +\\([^( \t\n]+\\)"
1504 1 font-lock-function-name-face)
1505 ;; keywords
1506 (cons (concat
1507 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|"
1508 (regexp-opt
1509 '("alias_method"
1510 "alias"
1511 "and"
1512 "begin"
1513 "break"
1514 "case"
1515 "catch"
1516 "class"
1517 "def"
1518 "do"
1519 "elsif"
1520 "else"
1521 "fail"
1522 "ensure"
1523 "for"
1524 "end"
1525 "if"
1526 "in"
1527 "module_function"
1528 "module"
1529 "next"
1530 "not"
1531 "or"
1532 "public"
1533 "private"
1534 "protected"
1535 "raise"
1536 "redo"
1537 "rescue"
1538 "retry"
1539 "return"
1540 "then"
1541 "throw"
1542 "super"
1543 "unless"
1544 "undef"
1545 "until"
1546 "when"
1547 "while"
1548 "yield")
1549 t)
1550 "\\)"
1551 ruby-keyword-end-re)
1552 2)
1553 ;; here-doc beginnings
f178c32d
DG
1554 `(,ruby-here-doc-beg-re 0 (unless (ruby-singleton-class-p (match-beginning 0))
1555 'font-lock-string-face))
c7fbbc3c
CY
1556 ;; variables
1557 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
1558 2 font-lock-variable-name-face)
0ba2d4b6
DG
1559 ;; symbols
1560 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
6c27f0f8 1561 2 font-lock-constant-face)
c7fbbc3c
CY
1562 ;; variables
1563 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1564 1 font-lock-variable-name-face)
1565 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1566 0 font-lock-variable-name-face)
c7fbbc3c
CY
1567 ;; constants
1568 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
1569 2 font-lock-type-face)
6c27f0f8 1570 '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face)
c7fbbc3c 1571 ;; expression expansion
0ba2d4b6 1572 '(ruby-match-expression-expansion
c7fbbc3c
CY
1573 0 font-lock-variable-name-face t)
1574 ;; warn lower camel case
1575 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1576 ; 0 font-lock-warning-face)
1577 )
ee61fe97 1578 "Additional expressions to highlight in Ruby mode.")
c7fbbc3c 1579
0ba2d4b6
DG
1580(defun ruby-match-expression-expansion (limit)
1581 (when (re-search-forward "[^\\]\\(\\\\\\\\\\)*\\(#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)\\)" limit 'move)
616c6c36 1582 (or (ruby-in-ppss-context-p 'string)
e6aca219 1583 (ruby-match-expression-expansion limit))))
0ba2d4b6 1584
c7fbbc3c 1585;;;###autoload
cf38dd42 1586(define-derived-mode ruby-mode prog-mode "Ruby"
c7fbbc3c
CY
1587 "Major mode for editing Ruby scripts.
1588\\[ruby-indent-line] properly indents subexpressions of multi-line
1589class, module, def, if, while, for, do, and case statements, taking
1590nesting into account.
1591
ee61fe97
JB
1592The variable `ruby-indent-level' controls the amount of indentation.
1593
c7fbbc3c 1594\\{ruby-mode-map}"
c7fbbc3c
CY
1595 (ruby-mode-variables)
1596
1597 (set (make-local-variable 'imenu-create-index-function)
1598 'ruby-imenu-create-index)
1599 (set (make-local-variable 'add-log-current-defun-function)
1600 'ruby-add-log-current-method)
4489104f
DG
1601 (set (make-local-variable 'beginning-of-defun-function)
1602 'ruby-beginning-of-defun)
1603 (set (make-local-variable 'end-of-defun-function)
1604 'ruby-end-of-defun)
c7fbbc3c
CY
1605
1606 (add-hook
cf38dd42 1607 (cond ((boundp 'before-save-hook) 'before-save-hook)
c7fbbc3c
CY
1608 ((boundp 'write-contents-functions) 'write-contents-functions)
1609 ((boundp 'write-contents-hooks) 'write-contents-hooks))
cf38dd42
SM
1610 'ruby-mode-set-encoding nil 'local)
1611
1612 (set (make-local-variable 'electric-indent-chars)
2122161f 1613 (append '(?\{ ?\}) electric-indent-chars))
c7fbbc3c
CY
1614
1615 (set (make-local-variable 'font-lock-defaults)
1616 '((ruby-font-lock-keywords) nil nil))
1617 (set (make-local-variable 'font-lock-keywords)
1618 ruby-font-lock-keywords)
1619 (set (make-local-variable 'font-lock-syntax-table)
1620 ruby-font-lock-syntax-table)
c7fbbc3c 1621
cf38dd42
SM
1622 (if (eval-when-compile (fboundp 'syntax-propertize-rules))
1623 (set (make-local-variable 'syntax-propertize-function)
1624 #'ruby-syntax-propertize-function)
1625 (set (make-local-variable 'font-lock-syntactic-keywords)
1626 ruby-font-lock-syntactic-keywords)))
c7fbbc3c
CY
1627
1628;;; Invoke ruby-mode when appropriate
1629
1630;;;###autoload
a7610c52 1631(add-to-list 'auto-mode-alist (cons (purecopy "\\.rb\\'") 'ruby-mode))
cabc040a
JD
1632;;;###autoload
1633(add-to-list 'auto-mode-alist '("Rakefile\\'" . ruby-mode))
c7fbbc3c
CY
1634
1635;;;###autoload
4d270537 1636(dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8"))
a7610c52 1637 (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'ruby-mode)))
c7fbbc3c
CY
1638
1639(provide 'ruby-mode)
1640
1641;;; ruby-mode.el ends here