• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /macosx-10.9.5/ruby-104/ruby/misc/

Lines Matching defs:ruby

0 ;;; ruby-mode.el --- Major mode for editing Ruby files
10 ;; Keywords: languages ruby
14 ;; ruby-mode is included in recent releases of GNU Emacs (version 23
42 ;; (autoload 'ruby-mode "ruby-mode" "Major mode for ruby files" t)
43 ;; (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
44 ;; (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
49 (defconst ruby-mode-revision "$Revision: 38150 $"
52 (defconst ruby-mode-version
53 (and (string-match "[0-9.]+" ruby-mode-revision)
54 (substring ruby-mode-revision (match-beginning 0) (match-end 0)))
57 (defconst ruby-keyword-end-re
58 (if (string-match "\\_>" "ruby")
62 (defconst ruby-block-beg-keywords
66 (defconst ruby-block-beg-re
67 (regexp-opt ruby-block-beg-keywords)
70 (defconst ruby-non-block-do-re
71 (concat (regexp-opt '("while" "until" "for" "rescue") t) ruby-keyword-end-re)
74 (defconst ruby-indent-beg-re
79 (defconst ruby-modifier-beg-keywords
83 (defconst ruby-modifier-beg-re
84 (regexp-opt ruby-modifier-beg-keywords)
87 (defconst ruby-modifier-re
88 (regexp-opt (cons "rescue" ruby-modifier-beg-keywords))
91 (defconst ruby-block-mid-keywords
95 (defconst ruby-block-mid-re
96 (regexp-opt ruby-block-mid-keywords)
99 (defconst ruby-block-op-keywords
103 (defconst ruby-block-hanging-re
104 (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords))
107 (defconst ruby-block-end-re "\\_<end\\_>")
109 (defconst ruby-here-doc-beg-re
112 (defconst ruby-here-doc-end-re
115 (defun ruby-here-doc-end-match ()
123 (defun ruby-here-doc-beg-match ()
134 (defconst ruby-delimiter
136 ruby-block-beg-re
137 "\\)\\_>\\|" ruby-block-end-re
138 "\\|^=begin\\|" ruby-here-doc-beg-re)
141 (defconst ruby-negative
142 (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\>\\|"
143 ruby-block-end-re "\\|}\\|\\]\\)")
146 (defconst ruby-operator-chars "-,.+*/%&|^~=<>:")
147 (defconst ruby-operator-re (concat "[" ruby-operator-chars "]"))
149 (defconst ruby-symbol-chars "a-zA-Z0-9_")
150 (defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]"))
152 (defvar ruby-mode-abbrev-table nil
153 "Abbrev table in use in ruby-mode buffers.")
155 (define-abbrev-table 'ruby-mode-abbrev-table ())
157 (defvar ruby-mode-map nil "Keymap used in ruby mode.")
159 (if ruby-mode-map
161 (setq ruby-mode-map (make-sparse-keymap))
162 (define-key ruby-mode-map "{" 'ruby-electric-brace)
163 (define-key ruby-mode-map "}" 'ruby-electric-brace)
164 (define-key ruby-mode-map "\e\C-a" 'ruby-beginning-of-defun)
165 (define-key ruby-mode-map "\e\C-e" 'ruby-end-of-defun)
166 (define-key ruby-mode-map "\e\C-b" 'ruby-backward-sexp)
167 (define-key ruby-mode-map "\e\C-f" 'ruby-forward-sexp)
168 (define-key ruby-mode-map "\e\C-p" 'ruby-beginning-of-block)
169 (define-key ruby-mode-map "\e\C-n" 'ruby-end-of-block)
170 (define-key ruby-mode-map "\e\C-h" 'ruby-mark-defun)
171 (define-key ruby-mode-map "\e\C-q" 'ruby-indent-exp)
172 (define-key ruby-mode-map "\t" 'ruby-indent-command)
173 (define-key ruby-mode-map "\C-c\C-e" 'ruby-insert-end)
174 (define-key ruby-mode-map "\C-j" 'ruby-reindent-then-newline-and-indent)
175 (define-key ruby-mode-map "\C-c{" 'ruby-toggle-block)
176 (define-key ruby-mode-map "\C-c\C-u" 'uncomment-region))
178 (defvar ruby-mode-syntax-table nil
179 "Syntax table in use in ruby-mode buffers.")
181 (if ruby-mode-syntax-table
183 (setq ruby-mode-syntax-table (make-syntax-table))
184 (modify-syntax-entry ?\' "\"" ruby-mode-syntax-table)
185 (modify-syntax-entry ?\" "\"" ruby-mode-syntax-table)
186 (modify-syntax-entry ?\` "\"" ruby-mode-syntax-table)
187 (modify-syntax-entry ?# "<" ruby-mode-syntax-table)
188 (modify-syntax-entry ?\n ">" ruby-mode-syntax-table)
189 (modify-syntax-entry ?\\ "\\" ruby-mode-syntax-table)
190 (modify-syntax-entry ?$ "." ruby-mode-syntax-table)
191 (modify-syntax-entry ?? "_" ruby-mode-syntax-table)
192 (modify-syntax-entry ?_ "_" ruby-mode-syntax-table)
193 (modify-syntax-entry ?: "_" ruby-mode-syntax-table)
194 (modify-syntax-entry ?< "." ruby-mode-syntax-table)
195 (modify-syntax-entry ?> "." ruby-mode-syntax-table)
196 (modify-syntax-entry ?& "." ruby-mode-syntax-table)
197 (modify-syntax-entry ?| "." ruby-mode-syntax-table)
198 (modify-syntax-entry ?% "." ruby-mode-syntax-table)
199 (modify-syntax-entry ?= "." ruby-mode-syntax-table)
200 (modify-syntax-entry ?/ "." ruby-mode-syntax-table)
201 (modify-syntax-entry ?+ "." ruby-mode-syntax-table)
202 (modify-syntax-entry ?* "." ruby-mode-syntax-table)
203 (modify-syntax-entry ?- "." ruby-mode-syntax-table)
204 (modify-syntax-entry ?\; "." ruby-mode-syntax-table)
205 (modify-syntax-entry ?\( "()" ruby-mode-syntax-table)
206 (modify-syntax-entry ?\) ")(" ruby-mode-syntax-table)
207 (modify-syntax-entry ?\{ "(}" ruby-mode-syntax-table)
208 (modify-syntax-entry ?\} "){" ruby-mode-syntax-table)
209 (modify-syntax-entry ?\[ "(]" ruby-mode-syntax-table)
210 (modify-syntax-entry ?\] ")[" ruby-mode-syntax-table)
213 (defcustom ruby-indent-tabs-mode nil
214 "*Indentation can insert tabs in ruby mode if this is non-nil."
215 :type 'boolean :group 'ruby)
216 (put 'ruby-indent-tabs-mode 'safe-local-variable 'booleanp)
218 (defcustom ruby-indent-level 2
219 "*Indentation of ruby statements."
220 :type 'integer :group 'ruby)
221 (put 'ruby-indent-level 'safe-local-variable 'integerp)
223 (defcustom ruby-comment-column 32
225 :type 'integer :group 'ruby)
226 (put 'ruby-comment-column 'safe-local-variable 'integerp)
228 (defcustom ruby-deep-arglist t
231 :group 'ruby)
232 (put 'ruby-deep-arglist 'safe-local-variable 'booleanp)
234 (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
237 :group 'ruby)
239 (defcustom ruby-deep-indent-paren-style 'space
241 :options '(t nil space) :group 'ruby)
243 (defcustom ruby-encoding-map '((shift_jis . cp932) (shift-jis . cp932))
244 "Alist to map encoding name from emacs to ruby."
245 :group 'ruby)
247 (defcustom ruby-use-encoding-map t
248 "*Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
249 :type 'boolean :group 'ruby)
251 (defvar ruby-indent-point nil "internal variable")
254 (defun ruby-imenu-create-index-in-block (prefix beg end)
276 (ruby-accurate-end-of-block end))
282 (ruby-accurate-end-of-block end)
285 (nconc (ruby-imenu-create-index-in-block
291 (defun ruby-imenu-create-index ()
292 (nreverse (ruby-imenu-create-index-in-block nil (point-min) nil)))
294 (defun ruby-accurate-end-of-block (&optional end)
297 (while (and (setq state (apply 'ruby-parse-partial end state))
300 (defun ruby-mode-variables ()
301 (set-syntax-table ruby-mode-syntax-table)
303 (setq local-abbrev-table ruby-mode-abbrev-table)
305 (setq indent-line-function 'ruby-indent-line)
313 (setq comment-column ruby-comment-column)
316 (setq indent-tabs-mode ruby-indent-tabs-mode)
328 (defun ruby-mode-set-encoding ()
344 (or (and ruby-use-encoding-map
345 (cdr (assq coding-system ruby-encoding-map)))
363 (defun ruby-current-indentation ()
369 (defun ruby-indent-line (&optional flag)
370 "Correct indentation of the current ruby line."
371 (ruby-indent-to (ruby-calculate-indent)))
373 (defun ruby-indent-command ()
375 (ruby-indent-line t))
377 (defun ruby-indent-to (x)
398 (defun ruby-special-char-p (&optional pnt)
405 (defun ruby-singleton-class-p ()
411 (defun ruby-expr-beg (&optional option)
422 (ruby-special-char-p))))
425 (not (progn (goto-char start) (ruby-singleton-class-p))))
426 ((or (looking-at ruby-operator-re)
432 (and (looking-at ruby-symbol-re)
433 (skip-chars-backward ruby-symbol-chars)
436 (append ruby-block-beg-keywords
437 ruby-block-op-keywords
438 ruby-block-mid-keywords)
448 (defun ruby-forward-string (term &optional end no-error expand)
455 (ruby-forward-string "}{" end no-error nil)
463 (defun ruby-deep-indent-paren-p (c &optional pos)
466 (ruby-expr-beg))
468 ((listp ruby-deep-indent-paren)
469 (let ((deep (assoc c ruby-deep-indent-paren)))
471 (or (cdr deep) ruby-deep-indent-paren-style))
472 ((memq c ruby-deep-indent-paren)
473 ruby-deep-indent-paren-style))))
474 ((eq c ruby-deep-indent-paren) ruby-deep-indent-paren-style)
475 ((eq c ?\( ) ruby-deep-arglist)))
477 (defun ruby-parse-partial (&optional end in-string nest depth pcol indent)
480 (when (re-search-forward ruby-delimiter end 'move)
489 (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t))
506 ((and (not (eobp)) (ruby-expr-beg 'expr-re))
507 (if (ruby-forward-string "/" end t t)
516 (ruby-expr-beg 'expr-qstr)
529 (unless (cond (re (ruby-forward-string re end t expand))
530 (expand (ruby-forward-string w end t t))
542 ((and (ruby-expr-beg)
555 (let ((deep (ruby-deep-indent-paren-p (char-after))))
556 (if (and deep (or (not (eq (char-after) ?\{)) (ruby-expr-beg)))
568 (if (ruby-deep-indent-paren-p (matching-paren (char-after))
573 (ruby-backward-sexp)
579 ((looking-at ruby-block-end-re)
605 ((looking-at (concat "\\_<\\(" ruby-block-beg-re "\\)\\_>"))
608 (or (not (looking-at (concat "do" ruby-keyword-end-re)))
611 (not (looking-at ruby-non-block-do-re)))))
626 (or (not (looking-at ruby-modifier-re))
627 (ruby-expr-beg 'modifier))
634 (ruby-forward-string (buffer-substring (match-beginning 1) (match-end 1)) end))
651 ((and (ruby-expr-beg 'heredoc)
660 (setq state (apply 'ruby-parse-partial
676 ((looking-at ruby-here-doc-beg-re)
677 (if (re-search-forward (ruby-here-doc-end-match)
678 ruby-indent-point t)
681 (goto-char ruby-indent-point)))
688 (defun ruby-parse-region (start end)
693 (ruby-beginning-of-indent))
697 (setq state (apply 'ruby-parse-partial end state))))))
705 (defun ruby-indent-size (pos nest)
706 (+ pos (* (or nest 1) ruby-indent-level)))
708 (defun ruby-calculate-indent (&optional parse-start)
711 (let ((ruby-indent-point (point))
719 (ruby-beginning-of-indent)
723 (setq state (ruby-parse-region parse-start ruby-indent-point))
729 (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state))
738 ((let ((s (ruby-parse-region (point) ruby-indent-point)))
742 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
747 (t (setq indent (ruby-indent-size (1- indent) 1))))))
750 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
752 (ruby-deep-indent-paren-p (matching-paren paren)
766 (setq indent (ruby-indent-size (current-column) (nth 2 state))))
768 (setq indent (+ (current-column) ruby-indent-level)))))
771 (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
773 (goto-char ruby-indent-point)
778 ((and (not (ruby-deep-indent-paren-p paren
781 (re-search-forward ruby-negative eol t))
783 (setq indent (- indent ruby-indent-level))))
788 (or (ruby-deep-indent-paren-p t)
807 (or (ruby-special-char-p end)
808 (and (setq state (ruby-parse-region parse-start end))
814 (setq state (ruby-parse-region parse-start (point))))
817 (or (and (looking-at ruby-symbol-re)
818 (skip-chars-backward ruby-symbol-chars)
819 (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>"))
824 (and (looking-at ruby-operator-re)
825 (not (ruby-special-char-p))
837 (null (nth 0 (ruby-parse-region (or begin parse-start) (point)))))
858 (not (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>")))
859 (eq (ruby-deep-indent-paren-p t) 'space)
867 (+ indent ruby-indent-level))))))))
868 (goto-char ruby-indent-point)
872 (+ indent ruby-indent-level)
875 (defun ruby-electric-brace (arg)
878 (ruby-indent-line t)
892 (defun-region-command ruby-beginning-of-defun (&optional arg)
897 (and (re-search-backward (concat "^\\(" ruby-block-beg-re "\\)\\_>")
901 (defun ruby-beginning-of-indent ()
902 (and (re-search-backward (concat "^\\(" ruby-indent-beg-re "\\)\\_>")
908 (defun-region-command ruby-end-of-defun (&optional arg)
912 (and (re-search-forward (concat "^\\(" ruby-block-end-re "\\)\\($\\|\\b[^_]\\)")
917 (defun ruby-move-to-block (n)
919 (setq start (ruby-calculate-indent))
920 (setq down (looking-at (if (< n 0) ruby-block-end-re
921 (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))))
943 (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>"))
950 (while (and (setq next (apply 'ruby-parse-partial eol state))
957 (defun-region-command ruby-beginning-of-block (&optional arg)
960 (ruby-move-to-block (- (or arg 1))))
962 (defun-region-command ruby-end-of-block (&optional arg)
965 (ruby-move-to-block (or arg 1)))
967 (defun-region-command ruby-forward-sexp (&optional cnt)
970 (ruby-backward-sexp (- cnt))
982 ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
985 (ruby-end-of-block)
997 (setq expr (or expr (ruby-expr-beg)
999 (nth 1 (setq state (apply 'ruby-parse-partial nil state))))
1007 (defun-region-command ruby-backward-sexp (&optional cnt)
1010 (ruby-forward-sexp (- cnt))
1029 (if (ruby-special-char-p) (forward-char -1)))
1043 (if (looking-at ruby-block-end-re)
1044 (ruby-beginning-of-block))
1050 (defun ruby-reindent-then-newline-and-indent ()
1059 (fset 'ruby-encomment-region (symbol-function 'comment-region))
1061 (defun ruby-decomment-region (beg end)
1068 (ruby-indent-line)))))
1070 (defun ruby-insert-end ()
1073 (ruby-indent-line t)
1076 (defun ruby-mark-defun ()
1080 (ruby-end-of-defun)
1082 (ruby-beginning-of-defun)
1085 (defun ruby-indent-exp (&optional shutup-p)
1098 (setq column (ruby-calculate-indent start))
1103 (ruby-indent-to column)
1108 (defun ruby-add-log-current-method ()
1118 "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
1133 "\\([A-Z]" ruby-symbol-re "*\\)")
1169 (defun ruby-brace-to-do-end ()
1171 (let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
1189 (defun ruby-do-end-to-brace ()
1193 (let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
1195 (when (looking-at ruby-block-end-re)
1205 (defun ruby-toggle-block ()
1207 (or (ruby-brace-to-do-end)
1208 (ruby-do-end-to-brace)))
1224 (defconst ruby-font-lock-syntactic-keywords
1242 (,(concat ruby-here-doc-beg-re ".*\\(\n\\)")
1243 ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re))
1244 (ruby-here-doc-beg-syntax))
1245 (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax))))
1251 (defun ruby-in-ppss-context-p (context &optional ppss)
1265 (and (ruby-in-ppss-context-p 'anything)
1266 (not (ruby-in-ppss-context-p 'heredoc))))
1271 "Internal error on `ruby-in-ppss-context-p': "
1275 (defun ruby-in-here-doc-p ()
1280 (while (and (re-search-backward ruby-here-doc-beg-re nil t)
1281 (not (ruby-singleton-class-p)))
1282 (if (not (or (ruby-in-ppss-context-p 'anything)
1283 (ruby-here-doc-find-end old-point)))
1286 (defun ruby-here-doc-find-end (&optional limit)
1299 (while (re-search-forward ruby-here-doc-beg-re eol t)
1301 (setq end-re (ruby-here-doc-end-match))
1314 (defun ruby-here-doc-beg-syntax ()
1317 (unless (or (ruby-in-ppss-context-p 'non-heredoc)
1318 (ruby-in-here-doc-p))
1321 (defun ruby-here-doc-end-syntax ()
1323 (when (ruby-in-ppss-context-p 'heredoc pss)
1328 (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line...
1329 (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment...
1331 (not (re-search-forward ruby-here-doc-beg-re eol t))))
1335 (put 'ruby-mode 'font-lock-defaults
1336 '((ruby-font-lock-keywords)
1340 . ruby-font-lock-syntactic-keywords))))
1342 (defun ruby-font-lock-docs (limit)
1353 (defun ruby-font-lock-maybe-docs (limit)
1368 (defvar ruby-font-lock-syntax-table
1369 (let* ((tbl (copy-syntax-table ruby-mode-syntax-table)))
1373 (defconst ruby-font-lock-keywords
1420 ruby-keyword-end-re)
1423 (list ruby-here-doc-beg-re 0 'font-lock-string-face)
1433 '(ruby-font-lock-docs
1435 '(ruby-font-lock-maybe-docs
1454 "*Additional expressions to highlight in ruby mode."))
1458 'ruby-mode
1476 (defun ruby-mode ()
1477 "Major mode for editing ruby scripts.
1478 \\[ruby-indent-command] properly indents subexpressions of multi-line
1482 The variable ruby-indent-level controls the amount of indentation.
1483 \\{ruby-mode-map}"
1486 (use-local-map ruby-mode-map)
1488 (setq major-mode 'ruby-mode)
1489 (ruby-mode-variables)
1492 (setq imenu-create-index-function 'ruby-imenu-create-index)
1495 (setq add-log-current-defun-function 'ruby-add-log-current-method)
1503 'ruby-mode-set-encoding)
1505 (set (make-local-variable 'font-lock-defaults) '((ruby-font-lock-keywords) nil nil))
1506 (set (make-local-variable 'font-lock-keywords) ruby-font-lock-keywords)
1507 (set (make-local-variable 'font-lock-syntax-table) ruby-font-lock-syntax-table)
1508 (set (make-local-variable 'font-lock-syntactic-keywords) ruby-font-lock-syntactic-keywords)
1511 (run-mode-hooks 'ruby-mode-hook)
1512 (run-hooks 'ruby-mode-hook)))
1514 (provide 'ruby-mode)