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