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