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