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