Mercurial > emacs
annotate lisp/progmodes/ruby-mode.el @ 110085:fc3e98cf87b7
gnus-process-put: Fix previous commit.
author | Katsumi Yamaoka <yamaoka@jpl.org> |
---|---|
date | Wed, 01 Sep 2010 04:48:51 +0000 |
parents | 4653f09a70cf |
children | b10051866f51 |
rev | line source |
---|---|
100329 | 1 ;;; ruby-mode.el --- Major mode for editing Ruby files |
2 | |
3 ;; Copyright (C) 1994, 1995, 1996 1997, 1998, 1999, 2000, 2001, | |
106815 | 4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
100329 | 5 ;; Free Software Foundation, Inc. |
6 | |
101087
ffd82d2cb8ec
Comments (minor fixes of some header conventions).
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
7 ;; Authors: Yukihiro Matsumoto |
ffd82d2cb8ec
Comments (minor fixes of some header conventions).
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
8 ;; Nobuyoshi Nakada |
100329 | 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 | |
109967
4653f09a70cf
* progmodes/ruby-mode.el (ruby): Add defgroup.
Chong Yidong <cyd@stupidchicken.com>
parents:
109060
diff
changeset
|
46 (defgroup ruby nil |
4653f09a70cf
* progmodes/ruby-mode.el (ruby): Add defgroup.
Chong Yidong <cyd@stupidchicken.com>
parents:
109060
diff
changeset
|
47 "Major mode for editing Ruby code." |
4653f09a70cf
* progmodes/ruby-mode.el (ruby): Add defgroup.
Chong Yidong <cyd@stupidchicken.com>
parents:
109060
diff
changeset
|
48 :prefix "ruby-" |
4653f09a70cf
* progmodes/ruby-mode.el (ruby): Add defgroup.
Chong Yidong <cyd@stupidchicken.com>
parents:
109060
diff
changeset
|
49 :group 'languages) |
4653f09a70cf
* progmodes/ruby-mode.el (ruby): Add defgroup.
Chong Yidong <cyd@stupidchicken.com>
parents:
109060
diff
changeset
|
50 |
100329 | 51 (defconst ruby-keyword-end-re |
52 (if (string-match "\\_>" "ruby") | |
53 "\\_>" | |
54 "\\>")) | |
55 | |
56 (defconst ruby-block-beg-keywords | |
57 '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do") | |
58 "Keywords at the beginning of blocks.") | |
59 | |
60 (defconst ruby-block-beg-re | |
61 (regexp-opt ruby-block-beg-keywords) | |
62 "Regexp to match the beginning of blocks.") | |
63 | |
64 (defconst ruby-non-block-do-re | |
65 (concat (regexp-opt '("while" "until" "for" "rescue") t) ruby-keyword-end-re) | |
66 "Regexp to match keywords that nest without blocks.") | |
67 | |
68 (defconst ruby-indent-beg-re | |
69 (concat "\\(\\s *" (regexp-opt '("class" "module" "def") t) "\\)\\|" | |
70 (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin"))) | |
71 "Regexp to match where the indentation gets deeper.") | |
72 | |
73 (defconst ruby-modifier-beg-keywords | |
74 '("if" "unless" "while" "until") | |
75 "Modifiers that are the same as the beginning of blocks.") | |
76 | |
77 (defconst ruby-modifier-beg-re | |
78 (regexp-opt ruby-modifier-beg-keywords) | |
79 "Regexp to match modifiers same as the beginning of blocks.") | |
80 | |
81 (defconst ruby-modifier-re | |
82 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords)) | |
83 "Regexp to match modifiers.") | |
84 | |
85 (defconst ruby-block-mid-keywords | |
86 '("then" "else" "elsif" "when" "rescue" "ensure") | |
87 "Keywords where the indentation gets shallower in middle of block statements.") | |
88 | |
89 (defconst ruby-block-mid-re | |
90 (regexp-opt ruby-block-mid-keywords) | |
91 "Regexp to match where the indentation gets shallower in middle of block statements.") | |
92 | |
93 (defconst ruby-block-op-keywords | |
94 '("and" "or" "not") | |
95 "Regexp to match boolean keywords.") | |
96 | |
97 (defconst ruby-block-hanging-re | |
98 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords)) | |
99 "Regexp to match hanging block modifiers.") | |
100 | |
101 (defconst ruby-block-end-re "\\<end\\>") | |
102 | |
103 (defconst ruby-here-doc-beg-re | |
104 "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)" | |
105 "Regexp to match the beginning of a heredoc.") | |
106 | |
107 (defconst ruby-here-doc-end-re | |
108 "^\\([ \t]+\\)?\\(.*\\)\\(.\\)$" | |
109 "Regexp to match the end of heredocs. | |
110 | |
111 This will actually match any line with one or more characters. | |
112 It's useful in that it divides up the match string so that | |
113 `ruby-here-doc-beg-match' can search for the beginning of the heredoc.") | |
114 | |
115 (defun ruby-here-doc-end-match () | |
116 "Return a regexp to find the end of a heredoc. | |
117 | |
118 This should only be called after matching against `ruby-here-doc-beg-re'." | |
119 (concat "^" | |
120 (if (match-string 2) "[ \t]*" nil) | |
121 (regexp-quote | |
122 (or (match-string 4) | |
123 (match-string 5) | |
124 (match-string 6))))) | |
125 | |
126 (defun ruby-here-doc-beg-match () | |
127 "Return a regexp to find the beginning of a heredoc. | |
128 | |
129 This should only be called after matching against `ruby-here-doc-end-re'." | |
130 (let ((contents (regexp-quote (concat (match-string 2) (match-string 3))))) | |
131 (concat "<<" | |
132 (let ((match (match-string 1))) | |
133 (if (and match (> (length match) 0)) | |
134 (concat "\\(?:-\\([\"']?\\)\\|\\([\"']\\)" (match-string 1) "\\)" | |
135 contents "\\b\\(\\1\\|\\2\\)") | |
136 (concat "-?\\([\"']\\|\\)" contents "\\b\\1")))))) | |
137 | |
138 (defconst ruby-delimiter | |
139 (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\<\\(" | |
140 ruby-block-beg-re | |
141 "\\)\\>\\|" ruby-block-end-re | |
142 "\\|^=begin\\|" ruby-here-doc-beg-re)) | |
143 | |
144 (defconst ruby-negative | |
145 (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\>\\|" | |
146 ruby-block-end-re "\\|}\\|\\]\\)") | |
147 "Regexp to match where the indentation gets shallower.") | |
148 | |
149 (defconst ruby-operator-re "[-,.+*/%&|^~=<>:]" | |
150 "Regexp to match operators.") | |
151 | |
152 (defconst ruby-symbol-chars "a-zA-Z0-9_" | |
153 "List of characters that symbol names may contain.") | |
154 (defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]") | |
155 "Regexp to match symbols.") | |
156 | |
157 (defvar ruby-mode-abbrev-table nil | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
158 "Abbrev table in use in Ruby mode buffers.") |
100329 | 159 |
160 (define-abbrev-table 'ruby-mode-abbrev-table ()) | |
161 | |
162 (defvar ruby-mode-map | |
163 (let ((map (make-sparse-keymap))) | |
164 (define-key map "{" 'ruby-electric-brace) | |
165 (define-key map "}" 'ruby-electric-brace) | |
166 (define-key map (kbd "M-C-a") 'ruby-beginning-of-defun) | |
167 (define-key map (kbd "M-C-e") 'ruby-end-of-defun) | |
168 (define-key map (kbd "M-C-b") 'ruby-backward-sexp) | |
169 (define-key map (kbd "M-C-f") 'ruby-forward-sexp) | |
170 (define-key map (kbd "M-C-p") 'ruby-beginning-of-block) | |
171 (define-key map (kbd "M-C-n") 'ruby-end-of-block) | |
172 (define-key map (kbd "M-C-h") 'ruby-mark-defun) | |
173 (define-key map (kbd "M-C-q") 'ruby-indent-exp) | |
174 (define-key map (kbd "C-M-h") 'backward-kill-word) | |
175 (define-key map (kbd "C-j") 'reindent-then-newline-and-indent) | |
176 (define-key map (kbd "C-m") 'newline) | |
177 map) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
178 "Keymap used in Ruby mode.") |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
209 "Syntax table to use in Ruby mode.") |
100329 | 210 |
211 (defcustom ruby-indent-tabs-mode nil | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
212 "Indentation can insert tabs in Ruby mode if this is non-nil." |
100329 | 213 :type 'boolean :group 'ruby) |
214 | |
215 (defcustom ruby-indent-level 2 | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
216 "Indentation of Ruby statements." |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
229 "Deep indent lists in parenthesis when non-nil. |
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
230 The value t means continuous line. |
100329 | 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 | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
243 "Insert a magic Emacs 'coding' comment upon save if this is non-nil." |
100329 | 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 () | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
306 "Set up initial buffer-local variables for Ruby mode." |
100329 | 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 "^#![^\n]*ruby") (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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
366 "Correct the indentation of the current Ruby line." |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
458 "TODO: document." |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
469 "TODO: document throughout function body." |
100329 | 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)) | |
103462
3c0923fbcb01
* progmodes/ruby-mode.el (ruby-parse-partial, ruby-font-lock-keywords): Support overloadable negative operators (Bug#3587).
Chong Yidong <cyd@stupidchicken.com>
parents:
101295
diff
changeset
|
620 ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)") |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
693 "Return the indentation level in spaces NEST levels deeper than POS." |
100329 | 694 (+ pos (* (or nest 1) ruby-indent-level))) |
695 | |
696 (defun ruby-calculate-indent (&optional parse-start) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
697 "Return the proper indentation level of the current line." |
100329 | 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))))) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
755 |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
860 "Insert a brace and re-indent the current line." |
100329 | 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. | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
868 With ARG, move backward multiple defuns. Negative ARG means |
100329 | 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. | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
877 With ARG, move forward multiple defuns. Negative ARG means |
100329 | 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) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
895 "Move to the beginning (N < 0) or the end (N > 0) of the current block |
100329 | 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). | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
944 With ARG, do it many times. Negative ARG means move backward." |
100329 | 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 (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ") | |
954 (goto-char (match-end 0))) | |
955 ((progn | |
956 (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*") | |
957 (looking-at "\\s(")) | |
958 (goto-char (scan-sexps (point) 1))) | |
959 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>")) | |
960 (not (eq (char-before (point)) ?.)) | |
961 (not (eq (char-before (point)) ?:))) | |
962 (ruby-end-of-block) | |
963 (forward-word 1)) | |
964 ((looking-at "\\(\\$\\|@@?\\)?\\sw") | |
965 (while (progn | |
966 (while (progn (forward-word 1) (looking-at "_"))) | |
967 (cond ((looking-at "::") (forward-char 2) t) | |
968 ((> (skip-chars-forward ".") 0)) | |
969 ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)") | |
970 (forward-char 1) nil))))) | |
971 ((let (state expr) | |
972 (while | |
973 (progn | |
974 (setq expr (or expr (ruby-expr-beg) | |
975 (looking-at "%\\sw?\\Sw\\|[\"'`/]"))) | |
976 (nth 1 (setq state (apply 'ruby-parse-partial nil state)))) | |
977 (setq expr t) | |
978 (skip-chars-forward "<")) | |
979 (not expr)))) | |
980 (setq i (1- i))) | |
981 ((error) (forward-word 1))) | |
982 i))) | |
983 | |
984 (defun ruby-backward-sexp (&optional arg) | |
985 "Move backward across one balanced expression (sexp). | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
986 With ARG, do it many times. Negative ARG means move forward." |
100329 | 987 ;; TODO: Document body |
988 (interactive "p") | |
989 (if (and (numberp arg) (< arg 0)) | |
990 (ruby-forward-sexp (- arg)) | |
991 (let ((i (or arg 1))) | |
992 (condition-case nil | |
993 (while (> i 0) | |
994 (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*") | |
995 (forward-char -1) | |
996 (cond ((looking-at "\\s)") | |
997 (goto-char (scan-sexps (1+ (point)) -1)) | |
998 (case (char-before) | |
999 (?% (forward-char -1)) | |
1000 ('(?q ?Q ?w ?W ?r ?x) | |
1001 (if (eq (char-before (1- (point))) ?%) (forward-char -2)))) | |
1002 nil) | |
1003 ((looking-at "\\s\"\\|\\\\\\S_") | |
1004 (let ((c (char-to-string (char-before (match-end 0))))) | |
1005 (while (and (search-backward c) | |
1006 (eq (logand (skip-chars-backward "\\") 1) | |
1007 1)))) | |
1008 nil) | |
1009 ((looking-at "\\s.\\|\\s\\") | |
1010 (if (ruby-special-char-p) (forward-char -1))) | |
1011 ((looking-at "\\s(") nil) | |
1012 (t | |
1013 (forward-char 1) | |
1014 (while (progn (forward-word -1) | |
1015 (case (char-before) | |
1016 (?_ t) | |
1017 (?. (forward-char -1) t) | |
1018 ((?$ ?@) | |
1019 (forward-char -1) | |
1020 (and (eq (char-before) (char-after)) (forward-char -1))) | |
1021 (?: | |
1022 (forward-char -1) | |
1023 (eq (char-before) :))))) | |
1024 (if (looking-at ruby-block-end-re) | |
1025 (ruby-beginning-of-block)) | |
1026 nil)) | |
1027 (setq i (1- i))) | |
1028 ((error))) | |
1029 i))) | |
1030 | |
1031 (defun ruby-mark-defun () | |
1032 "Put mark at end of this Ruby function, point at beginning." | |
1033 (interactive) | |
1034 (push-mark (point)) | |
1035 (ruby-end-of-defun) | |
1036 (push-mark (point) nil t) | |
1037 (ruby-beginning-of-defun) | |
1038 (re-search-backward "^\n" (- (point) 1) t)) | |
1039 | |
1040 (defun ruby-indent-exp (&optional shutup-p) | |
1041 "Indent each line in the balanced expression following the point. | |
1042 If a prefix arg is given or SHUTUP-P is non-nil, no errors | |
1043 are signalled if a balanced expression isn't found." | |
1044 (interactive "*P") | |
1045 (let ((here (point-marker)) start top column (nest t)) | |
1046 (set-marker-insertion-type here t) | |
1047 (unwind-protect | |
1048 (progn | |
1049 (beginning-of-line) | |
1050 (setq start (point) top (current-indentation)) | |
1051 (while (and (not (eobp)) | |
1052 (progn | |
1053 (setq column (ruby-calculate-indent start)) | |
1054 (cond ((> column top) | |
1055 (setq nest t)) | |
1056 ((and (= column top) nest) | |
1057 (setq nest nil) t)))) | |
1058 (ruby-indent-to column) | |
1059 (beginning-of-line 2))) | |
1060 (goto-char here) | |
1061 (set-marker here nil)))) | |
1062 | |
1063 (defun ruby-add-log-current-method () | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1064 "Return the current method name as a string. |
100329 | 1065 This string includes all namespaces. |
1066 | |
1067 For example: | |
1068 | |
1069 #exit | |
1070 String#gsub | |
1071 Net::HTTP#active? | |
1072 File::open. | |
1073 | |
1074 See `add-log-current-defun-function'." | |
1075 ;; TODO: Document body | |
1076 ;; Why does this append a period to class methods? | |
1077 (condition-case nil | |
1078 (save-excursion | |
1079 (let (mname mlist (indent 0)) | |
1080 ;; get current method (or class/module) | |
1081 (if (re-search-backward | |
1082 (concat "^[ \t]*\\(def\\|class\\|module\\)[ \t]+" | |
1083 "\\(" | |
1084 ;; \\. and :: for class method | |
1085 "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)" | |
1086 "+\\)") | |
1087 nil t) | |
1088 (progn | |
1089 (setq mname (match-string 2)) | |
1090 (unless (string-equal "def" (match-string 1)) | |
1091 (setq mlist (list mname) mname nil)) | |
1092 (goto-char (match-beginning 1)) | |
1093 (setq indent (current-column)) | |
1094 (beginning-of-line))) | |
1095 ;; nest class/module | |
1096 (while (and (> indent 0) | |
1097 (re-search-backward | |
1098 (concat | |
1099 "^[ \t]*\\(class\\|module\\)[ \t]+" | |
1100 "\\([A-Z]" ruby-symbol-re "*\\)") | |
1101 nil t)) | |
1102 (goto-char (match-beginning 1)) | |
1103 (if (< (current-column) indent) | |
1104 (progn | |
1105 (setq mlist (cons (match-string 2) mlist)) | |
1106 (setq indent (current-column)) | |
1107 (beginning-of-line)))) | |
1108 (when mname | |
1109 (let ((mn (split-string mname "\\.\\|::"))) | |
1110 (if (cdr mn) | |
1111 (progn | |
1112 (cond | |
1113 ((string-equal "" (car mn)) | |
1114 (setq mn (cdr mn) mlist nil)) | |
1115 ((string-equal "self" (car mn)) | |
1116 (setq mn (cdr mn))) | |
1117 ((let ((ml (nreverse mlist))) | |
1118 (while ml | |
1119 (if (string-equal (car ml) (car mn)) | |
1120 (setq mlist (nreverse (cdr ml)) ml nil)) | |
1121 (or (setq ml (cdr ml)) (nreverse mlist)))))) | |
1122 (if mlist | |
1123 (setcdr (last mlist) mn) | |
1124 (setq mlist mn)) | |
1125 (setq mn (last mn 2)) | |
1126 (setq mname (concat "." (cadr mn))) | |
1127 (setcdr mn nil)) | |
1128 (setq mname (concat "#" mname))))) | |
1129 ;; generate string | |
1130 (if (consp mlist) | |
1131 (setq mlist (mapconcat (function identity) mlist "::"))) | |
1132 (if mname | |
1133 (if mlist (concat mlist mname) mname) | |
1134 mlist))))) | |
1135 | |
1136 (defconst ruby-font-lock-syntactic-keywords | |
1137 `(;; #{ }, #$hoge, #@foo are not comments | |
1138 ("\\(#\\)[{$@]" 1 (1 . nil)) | |
1139 ;; the last $', $", $` in the respective string is not variable | |
1140 ;; the last ?', ?", ?` in the respective string is not ascii code | |
1141 ("\\(^\\|[\[ \t\n<+\(,=]\\)\\(['\"`]\\)\\(\\\\.\\|\\2\\|[^'\"`\n\\\\]\\)*?\\\\?[?$]\\(\\2\\)" | |
1142 (2 (7 . nil)) | |
1143 (4 (7 . nil))) | |
1144 ;; $' $" $` .... are variables | |
1145 ;; ?' ?" ?` are ascii codes | |
1146 ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil)) | |
1147 ;; regexps | |
1148 ("\\(^\\|[=(,~?:;<>]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)" | |
1149 (4 (7 . ?/)) | |
1150 (6 (7 . ?/))) | |
1151 ("^=en\\(d\\)\\_>" 1 "!") | |
1152 ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax)) | |
1153 ;; Currently, the following case is highlighted incorrectly: | |
1154 ;; | |
1155 ;; <<FOO | |
1156 ;; FOO | |
1157 ;; <<BAR | |
1158 ;; <<BAZ | |
1159 ;; BAZ | |
1160 ;; BAR | |
1161 ;; | |
1162 ;; This is because all here-doc beginnings are highlighted before any endings, | |
1163 ;; so although <<BAR is properly marked as a beginning, when we get to <<BAZ | |
1164 ;; it thinks <<BAR is part of a string so it's marked as well. | |
1165 ;; | |
1166 ;; This may be fixable by modifying ruby-in-here-doc-p to use | |
1167 ;; ruby-in-non-here-doc-string-p rather than syntax-ppss-context, | |
1168 ;; but I don't want to try that until we've got unit tests set up | |
1169 ;; to make sure I don't break anything else. | |
1170 (,(concat ruby-here-doc-beg-re ".*\\(\n\\)") | |
1171 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re)) | |
1172 (ruby-here-doc-beg-syntax)) | |
1173 (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax))) | |
100355 | 1174 "Syntactic keywords for Ruby mode. See `font-lock-syntactic-keywords'.") |
100329 | 1175 |
1176 (defun ruby-comment-beg-syntax () | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1177 "Return the syntax cell for a the first character of a =begin. |
100329 | 1178 See the definition of `ruby-font-lock-syntactic-keywords'. |
1179 | |
1180 This returns a comment-delimiter cell as long as the =begin | |
1181 isn't in a string or another comment." | |
1182 (when (not (nth 3 (syntax-ppss))) | |
1183 (string-to-syntax "!"))) | |
1184 | |
1185 (unless (functionp 'syntax-ppss) | |
1186 (defun syntax-ppss (&optional pos) | |
1187 (parse-partial-sexp (point-min) (or pos (point))))) | |
1188 | |
1189 (defun ruby-in-ppss-context-p (context &optional ppss) | |
1190 (let ((ppss (or ppss (syntax-ppss (point))))) | |
1191 (if (cond | |
1192 ((eq context 'anything) | |
1193 (or (nth 3 ppss) | |
1194 (nth 4 ppss))) | |
1195 ((eq context 'string) | |
1196 (nth 3 ppss)) | |
1197 ((eq context 'heredoc) | |
1198 (and (nth 3 ppss) | |
1199 ;; If it's generic string, it's a heredoc and we don't care | |
1200 ;; See `parse-partial-sexp' | |
1201 (not (numberp (nth 3 ppss))))) | |
1202 ((eq context 'non-heredoc) | |
1203 (and (ruby-in-ppss-context-p 'anything) | |
1204 (not (ruby-in-ppss-context-p 'heredoc)))) | |
1205 ((eq context 'comment) | |
1206 (nth 4 ppss)) | |
1207 (t | |
1208 (error (concat | |
1209 "Internal error on `ruby-in-ppss-context-p': " | |
1210 "context name `" (symbol-name context) "' is unknown")))) | |
1211 t))) | |
1212 | |
1213 (defun ruby-in-here-doc-p () | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1214 "Return whether or not the point is in a heredoc." |
100329 | 1215 (save-excursion |
1216 (let ((old-point (point)) (case-fold-search nil)) | |
1217 (beginning-of-line) | |
1218 (catch 'found-beg | |
1219 (while (re-search-backward ruby-here-doc-beg-re nil t) | |
1220 (if (not (or (ruby-in-ppss-context-p 'anything) | |
1221 (ruby-here-doc-find-end old-point))) | |
1222 (throw 'found-beg t))))))) | |
1223 | |
1224 (defun ruby-here-doc-find-end (&optional limit) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1225 "Expects the point to be on a line with one or more heredoc openers. |
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1226 Returns the buffer position at which all heredocs on the line |
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1227 are terminated, or nil if they aren't terminated before the |
100329 | 1228 buffer position `limit' or the end of the buffer." |
1229 (save-excursion | |
1230 (beginning-of-line) | |
1231 (catch 'done | |
1232 (let ((eol (save-excursion (end-of-line) (point))) | |
1233 (case-fold-search nil) | |
1234 ;; Fake match data such that (match-end 0) is at eol | |
1235 (end-match-data (progn (looking-at ".*$") (match-data))) | |
1236 beg-match-data end-re) | |
1237 (while (re-search-forward ruby-here-doc-beg-re eol t) | |
1238 (setq beg-match-data (match-data)) | |
1239 (setq end-re (ruby-here-doc-end-match)) | |
1240 | |
1241 (set-match-data end-match-data) | |
1242 (goto-char (match-end 0)) | |
1243 (unless (re-search-forward end-re limit t) (throw 'done nil)) | |
1244 (setq end-match-data (match-data)) | |
1245 | |
1246 (set-match-data beg-match-data) | |
1247 (goto-char (match-end 0))) | |
1248 (set-match-data end-match-data) | |
1249 (goto-char (match-end 0)) | |
1250 (point))))) | |
1251 | |
1252 (defun ruby-here-doc-beg-syntax () | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1253 "Return the syntax cell for a line that may begin a heredoc. |
100329 | 1254 See the definition of `ruby-font-lock-syntactic-keywords'. |
1255 | |
1256 This sets the syntax cell for the newline ending the line | |
1257 containing the heredoc beginning so that cases where multiple | |
1258 heredocs are started on one line are handled correctly." | |
1259 (save-excursion | |
1260 (goto-char (match-beginning 0)) | |
1261 (unless (or (ruby-in-ppss-context-p 'non-heredoc) | |
1262 (ruby-in-here-doc-p)) | |
1263 (string-to-syntax "|")))) | |
1264 | |
1265 (defun ruby-here-doc-end-syntax () | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1266 "Return the syntax cell for a line that may end a heredoc. |
100329 | 1267 See the definition of `ruby-font-lock-syntactic-keywords'." |
1268 (let ((pss (syntax-ppss)) (case-fold-search nil)) | |
1269 ;; If we aren't in a string, we definitely aren't ending a heredoc, | |
1270 ;; so we can just give up. | |
1271 ;; This means we aren't doing a full-document search | |
1272 ;; every time we enter a character. | |
1273 (when (ruby-in-ppss-context-p 'heredoc pss) | |
1274 (save-excursion | |
1275 (goto-char (nth 8 pss)) ; Go to the beginning of heredoc. | |
1276 (let ((eol (point))) | |
1277 (beginning-of-line) | |
1278 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line... | |
1279 (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment... | |
1280 (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line... | |
1281 (not (re-search-forward ruby-here-doc-beg-re eol t)))) | |
1282 (string-to-syntax "|"))))))) | |
1283 | |
1284 (if (featurep 'xemacs) | |
1285 (put 'ruby-mode 'font-lock-defaults | |
1286 '((ruby-font-lock-keywords) | |
1287 nil nil nil | |
1288 beginning-of-line | |
1289 (font-lock-syntactic-keywords | |
1290 . ruby-font-lock-syntactic-keywords)))) | |
1291 | |
1292 (defvar ruby-font-lock-syntax-table | |
1293 (let ((tbl (copy-syntax-table ruby-mode-syntax-table))) | |
1294 (modify-syntax-entry ?_ "w" tbl) | |
1295 tbl) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1296 "The syntax table to use for fontifying Ruby mode buffers. |
100329 | 1297 See `font-lock-syntax-table'.") |
1298 | |
1299 (defconst ruby-font-lock-keywords | |
1300 (list | |
1301 ;; functions | |
1302 '("^\\s *def\\s +\\([^( \t\n]+\\)" | |
1303 1 font-lock-function-name-face) | |
1304 ;; keywords | |
1305 (cons (concat | |
1306 "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(defined\\?\\|" | |
1307 (regexp-opt | |
1308 '("alias_method" | |
1309 "alias" | |
1310 "and" | |
1311 "begin" | |
1312 "break" | |
1313 "case" | |
1314 "catch" | |
1315 "class" | |
1316 "def" | |
1317 "do" | |
1318 "elsif" | |
1319 "else" | |
1320 "fail" | |
1321 "ensure" | |
1322 "for" | |
1323 "end" | |
1324 "if" | |
1325 "in" | |
1326 "module_function" | |
1327 "module" | |
1328 "next" | |
1329 "not" | |
1330 "or" | |
1331 "public" | |
1332 "private" | |
1333 "protected" | |
1334 "raise" | |
1335 "redo" | |
1336 "rescue" | |
1337 "retry" | |
1338 "return" | |
1339 "then" | |
1340 "throw" | |
1341 "super" | |
1342 "unless" | |
1343 "undef" | |
1344 "until" | |
1345 "when" | |
1346 "while" | |
1347 "yield") | |
1348 t) | |
1349 "\\)" | |
1350 ruby-keyword-end-re) | |
1351 2) | |
1352 ;; here-doc beginnings | |
1353 (list ruby-here-doc-beg-re 0 'font-lock-string-face) | |
1354 ;; variables | |
1355 '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>" | |
1356 2 font-lock-variable-name-face) | |
1357 ;; variables | |
1358 '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W" | |
1359 1 font-lock-variable-name-face) | |
1360 '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+" | |
1361 0 font-lock-variable-name-face) | |
1362 ;; general delimited string | |
1363 '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)" | |
1364 (2 font-lock-string-face)) | |
1365 ;; constants | |
1366 '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)" | |
1367 2 font-lock-type-face) | |
1368 ;; symbols | |
103462
3c0923fbcb01
* progmodes/ruby-mode.el (ruby-parse-partial, ruby-font-lock-keywords): Support overloadable negative operators (Bug#3587).
Chong Yidong <cyd@stupidchicken.com>
parents:
101295
diff
changeset
|
1369 '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)" |
100329 | 1370 2 font-lock-reference-face) |
1371 ;; expression expansion | |
1372 '("#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)" | |
1373 0 font-lock-variable-name-face t) | |
1374 ;; warn lower camel case | |
1375 ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)" | |
1376 ; 0 font-lock-warning-face) | |
1377 ) | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1378 "Additional expressions to highlight in Ruby mode.") |
100329 | 1379 |
1380 ;;;###autoload | |
1381 (defun ruby-mode () | |
1382 "Major mode for editing Ruby scripts. | |
1383 \\[ruby-indent-line] properly indents subexpressions of multi-line | |
1384 class, module, def, if, while, for, do, and case statements, taking | |
1385 nesting into account. | |
1386 | |
100354
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1387 The variable `ruby-indent-level' controls the amount of indentation. |
89bba37a3188
* progmodes/ruby-mode.el (ruby-mode-abbrev-table, ruby-mode-map)
Juanma Barranquero <lekktu@gmail.com>
parents:
100347
diff
changeset
|
1388 |
100329 | 1389 \\{ruby-mode-map}" |
1390 (interactive) | |
1391 (kill-all-local-variables) | |
1392 (use-local-map ruby-mode-map) | |
1393 (setq mode-name "Ruby") | |
1394 (setq major-mode 'ruby-mode) | |
1395 (ruby-mode-variables) | |
1396 | |
109060
c1e50e6f89af
* progmodes/ruby-mode.el (ruby-mode-map): Don't bind TAB.
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
1397 (set (make-local-variable 'indent-line-function) |
c1e50e6f89af
* progmodes/ruby-mode.el (ruby-mode-map): Don't bind TAB.
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
1398 'ruby-indent-line) |
100329 | 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 | |
105765
db5e4a5897ec
* textmodes/tex-mode.el (tex-dvi-view-command)
Dan Nicolaescu <dann@ics.uci.edu>
parents:
103462
diff
changeset
|
1428 (add-to-list 'auto-mode-alist (cons (purecopy "\\.rb\\'") 'ruby-mode)) |
100329 | 1429 |
1430 ;;;###autoload | |
101295
b7a4fb563960
Autoload other interpreter names.
Chong Yidong <cyd@stupidchicken.com>
parents:
101087
diff
changeset
|
1431 (dolist (name (list "ruby" "rbx" "jruby" "ruby1.9" "ruby1.8")) |
105765
db5e4a5897ec
* textmodes/tex-mode.el (tex-dvi-view-command)
Dan Nicolaescu <dann@ics.uci.edu>
parents:
103462
diff
changeset
|
1432 (add-to-list 'interpreter-mode-alist (cons (purecopy name) 'ruby-mode))) |
100329 | 1433 |
1434 (provide 'ruby-mode) | |
1435 | |
100372 | 1436 ;; arch-tag: e6ecc893-8005-420c-b7f9-34ab99a1fff9 |
100329 | 1437 ;;; ruby-mode.el ends here |