annotate lisp/progmodes/ruby-mode.el @ 106104:682bede83101

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