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