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