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