├── .gitignore
├── README
├── ruby-additional.el
├── ruby-mkmf-log-mode.el
├── ruby-mode.el
├── rubydb2x.el
└── rubydb3x.el
/.gitignore:
--------------------------------------------------------------------------------
1 | *.elc
2 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | README this file
2 | ruby-mode.el ruby mode for emacs
3 | rubydb2x.el ruby debugger support for emacs 19.2x or before
4 | rubydb3x.el ruby debugger support for emacs 19.3x or later
5 | ruby-additional.el ruby-mode extensions yet to be merged into Emacs
6 | ruby-mkmf-log-mode.el mkmf.log viewing mode
7 |
8 | Check out the following repositories also.
9 |
10 | - https://github.com/ruby-debug/
11 | - https://github.com/ruby/elisp-ruby-electric
12 | - https://github.com/ruby/elisp-rdoc-mode
13 | - https://github.com/nonsequitur/inf-ruby
14 |
--------------------------------------------------------------------------------
/ruby-additional.el:
--------------------------------------------------------------------------------
1 | ;;; ruby-additional.el --- ruby-mode extensions yet to be merged into Emacs
2 |
3 | ;; Authors: Yukihiro Matsumoto, Nobuyoshi Nakada, Akinori MUSHA
4 | ;; URL: https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/misc/
5 | ;; Created: 3 Sep 2012
6 | ;; Package-Requires: ((emacs "24.3") (ruby-mode "1.2"))
7 | ;; Keywords: ruby, languages
8 |
9 | ;;; Commentary:
10 | ;;
11 | ;; This package contains ruby-mode extensions yet to be merged into
12 | ;; the latest released version of Emacs distribution. For older
13 | ;; versions of Emacs, use ruby-mode.el bundled with CRuby.
14 |
15 | ;;; Code:
16 |
17 | (eval-when-compile
18 | (require 'ruby-mode))
19 |
20 | (eval-after-load 'ruby-mode
21 | '(progn
22 | (define-key ruby-mode-map "\C-c\C-e" 'ruby-insert-end)
23 |
24 | (defun ruby-insert-end ()
25 | (interactive)
26 | (let ((s (save-excursion (backward-up-list) (matching-paren (char-after)))))
27 | (if s
28 | (insert (char-to-string s))
29 | (if (eq (char-syntax (preceding-char)) ?w)
30 | (insert " "))
31 | (insert "end")
32 | (if (eq (char-syntax (following-char)) ?w)
33 | (insert " "))))
34 | (save-excursion
35 | (ruby-indent-line t)
36 | (end-of-line)))
37 |
38 | (defconst ruby-default-encoding-map
39 | '((us-ascii . nil) ;; Do not put coding: us-ascii
40 | (utf-8 . nil) ;; Do not put coding: utf-8
41 | (shift-jis . cp932) ;; Emacs charset name of Shift_JIS
42 | (shift_jis . cp932) ;; MIME charset name of Shift_JIS
43 | (japanese-cp932 . cp932)) ;; Emacs charset name of CP932
44 | )
45 |
46 | (custom-set-default 'ruby-encoding-map ruby-default-encoding-map)
47 |
48 | (defcustom ruby-encoding-map ruby-default-encoding-map
49 | "Alist to map encoding name from Emacs to Ruby.
50 | Associating an encoding name with nil means it needs not be
51 | explicitly declared in magic comment."
52 | :type '(repeat (cons (symbol :tag "From") (symbol :tag "To")))
53 | :group 'ruby)
54 |
55 | (defun ruby-mode-set-encoding ()
56 | "Insert or update a magic comment header with the proper encoding.
57 | `ruby-encoding-map' is looked up to convert an encoding name from
58 | Emacs to Ruby."
59 | (let* ((nonascii
60 | (save-excursion
61 | (widen)
62 | (goto-char (point-min))
63 | (re-search-forward "[^\0-\177]" nil t)))
64 | (coding-system
65 | (or coding-system-for-write
66 | buffer-file-coding-system))
67 | (coding-system
68 | (and coding-system
69 | (coding-system-change-eol-conversion coding-system nil)))
70 | (coding-system
71 | (and coding-system
72 | (or
73 | (coding-system-get coding-system :mime-charset)
74 | (let ((coding-type (coding-system-get coding-system :coding-type)))
75 | (cond ((eq coding-type 'undecided)
76 | (if nonascii
77 | (or (and (coding-system-get coding-system :prefer-utf-8)
78 | 'utf-8)
79 | (coding-system-get default-buffer-file-coding-system :coding-type)
80 | 'ascii-8bit)))
81 | ((memq coding-type '(utf-8 shift-jis))
82 | coding-type)
83 | (t coding-system))))))
84 | (coding-system
85 | (or coding-system
86 | 'us-ascii))
87 | (coding-system
88 | (let ((cons (assq coding-system ruby-encoding-map)))
89 | (if cons (cdr cons) coding-system)))
90 | (coding-system
91 | (and coding-system
92 | (symbol-name coding-system))))
93 | (if coding-system
94 | (save-excursion
95 | (widen)
96 | (goto-char (point-min))
97 | (if (looking-at "^#!") (beginning-of-line 2))
98 | (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
99 | (unless (string= (match-string 2) coding-system)
100 | (goto-char (match-beginning 2))
101 | (delete-region (point) (match-end 2))
102 | (and (looking-at "-\*-")
103 | (let ((n (skip-chars-backward " ")))
104 | (cond ((= n 0) (insert " ") (backward-char))
105 | ((= n -1) (insert " "))
106 | ((forward-char)))))
107 | (insert coding-system)))
108 | ((looking-at "\\s *#.*coding\\s *[:=]"))
109 | (t (when ruby-insert-encoding-magic-comment
110 | (insert "# -*- coding: " coding-system " -*-\n"))))))))
111 |
112 | (define-key ruby-mode-map "\C-cU" 'ruby-encode-decode-unicode)
113 |
114 | (defun ruby-encode-unicode (beg end)
115 | "Convert non-ascii string in the given region to \\u{} form."
116 | (interactive "r")
117 | (setq end (set-marker (make-marker) end))
118 | (goto-char beg)
119 | (while (and (< (point) end)
120 | (re-search-forward "\\([\C-@-\C-I\C-K\C-_\C-?]+\\)\\|[^\C-@-\C-?]+" end t))
121 | (let ((str (match-string-no-properties 0)) sep b e f)
122 | (if (match-beginning 1)
123 | (setq b "" e "" sep ""
124 | f (lambda (c)
125 | (cond ((= c ?\t) "\\t")
126 | ((= c ?\r) "\\r")
127 | ((= c ?\e) "\\e")
128 | ((= c ?\f) "\\f")
129 | ((= c ?\b) "\\b")
130 | ((= c ?\v) "\\v")
131 | ((= c ?\C-?) "\\c?")
132 | ((concat "\\c" (char-to-string (logior c #x40)))))))
133 | (setq b "\\u{" e "}" sep " " f (lambda (c) (format "%x" c))))
134 | (setq str (mapconcat f str sep))
135 | (delete-region (match-beginning 0) (match-end 0))
136 | (insert b str e))))
137 |
138 | (defun ruby-decode-unicode (beg end)
139 | "Convert escaped Unicode in the given region to raw string."
140 | (interactive "r")
141 | (setq end (set-marker (make-marker) end))
142 | (goto-char beg)
143 | (while (and (< (point) end)
144 | (re-search-forward "\\\\u\\([0-9a-fA-F]\\{4\\}\\)\\|\\\\u{\\([0-9a-fA-F \t]+\\)}" end t))
145 | (let ((b (match-beginning 0)) (e (match-end 0))
146 | (s (match-string-no-properties 1)))
147 | (if s
148 | (setq s (cons s nil))
149 | (goto-char (match-beginning 2))
150 | (while (looking-at "[ \t]*\\([0-9a-fA-F]+\\)")
151 | (setq s (cons (match-string-no-properties 1) s))
152 | (goto-char (match-end 0))))
153 | (setq s (mapconcat (lambda (c) (format "%c" (string-to-number c 16)))
154 | (nreverse s) ""))
155 | (delete-region b e)
156 | (insert s))
157 | ))
158 |
159 | (defun ruby-encode-decode-unicode (dec beg end)
160 | "Convert Unicode <-> \\u{} in the given region."
161 | (interactive "P\nr")
162 | (if dec (ruby-decode-unicode beg end) (ruby-encode-unicode beg end)))
163 |
164 | (defun ruby-insert-heredoc-code-block (arg)
165 | "Insert indented here document code block"
166 | (interactive "P")
167 | (let ((c (if arg "~" "-")))
168 | (insert "\"#{<<" c "\"{#\"}\\n#{<<" c "'};'}\""))
169 | (end-of-line)
170 | (if (eobp) (insert "\n") (forward-char))
171 | (indent-region (point)
172 | (progn (insert "{#\n" "};\n") (point)))
173 | (beginning-of-line 0))
174 | (define-key ruby-mode-map "\C-cH" 'ruby-insert-heredoc-code-block)
175 | ))
176 |
177 | ;; monkey-patching ruby-mode.el in Emacs 24, as r49872.
178 | (when (and (boundp 'ruby-syntax-before-regexp-re)
179 | (not (string-match ruby-syntax-before-regexp-re "foo {|" 1)))
180 | (replace-regexp-in-string "\\[\\[" "\\&{|" ruby-syntax-before-regexp-re))
181 |
182 | (provide 'ruby-additional)
183 |
184 | ;;; ruby-additional.el ends here
185 |
--------------------------------------------------------------------------------
/ruby-mkmf-log-mode.el:
--------------------------------------------------------------------------------
1 | ;; ruby-mkmf-log-mode.el --- Major mode for viewing Ruby mkmf.log files ;; -*- lexical-binding: t -*-
2 | ;;
3 | ;; Authors: Nobuyoshi Nakada
4 | ;; Created: Mon Jan 9 21:58:19 JST 2023
5 |
6 | ;;; Code:
7 |
8 | (require 'cl-lib)
9 | (require 'compile) ; for faces
10 |
11 | (defgroup ruby-mkmf-log nil
12 | "View mode for Ruby mkmf.log"
13 | :prefix "ruby-mkmf-log-"
14 | :group 'languages)
15 |
16 | (defconst ruby-mkmf-log-methods
17 | '("convertible_int" "what_type?" "pkg_config")
18 | "Method names defined in mkmf.rb and shown at messages in
19 | addition to have_/check_/find_ methods")
20 |
21 | (defconst ruby-mkmf-log-methods-re
22 | (concat
23 | "^\\(?:"
24 | "\\(?:"
25 | "\\(?:test\\|have\\|check\\|find\\|append\\)_[_a-zA-Z0-9]*\\??\\|"
26 | (regexp-opt ruby-mkmf-log-methods)
27 | "\\)"
28 | ": \\)?checking .*?\\(?: in \\(.*\\)\\)?\\.\\.\\. \\(--------------------\\) \\(.*\\)")
29 | "Regexp to match the beginning of each checks")
30 |
31 | (defconst ruby-mkmf-log-end-re
32 | "\n\n--------------------\n$"
33 | "Regexp to match the end of each checks")
34 |
35 | (defconst ruby-mkmf-log-hidden-mark "…" "Mark when hiding check content")
36 | (defconst ruby-mkmf-log-visible-mark "" "Mark when showing check content")
37 |
38 | (defcustom ruby-mkmf-log-check-heading-face
39 | '(:weight bold)
40 | "Alist of face for check heading"
41 | :type '(face)
42 | :group 'ruby-mkmf-log-mode)
43 | (defcustom ruby-mkmf-log-check-content-face
44 | '(:height 0.8)
45 | "Alist of face for check content"
46 | :type '(face)
47 | :group 'ruby-mkmf-log-mode)
48 |
49 | (defun ruby-mkmf-log-toggle-visible ()
50 | "Toggle visibility of the current check"
51 | (interactive)
52 | (save-excursion
53 | (let ((ovs (overlays-at (point))))
54 | (mapcar
55 | (lambda (o)
56 | (setq o (overlay-get o 'hide-ruby-mkmf-log))
57 | (if (overlayp o)
58 | (let ((visible (overlay-get o 'invisible)))
59 | (overlay-put o 'invisible (not visible))
60 | ;; (overlay-put o 'before-string
61 | ;; (if visible ruby-mkmf-log-visible-mark ruby-mkmf-log-hidden-mark))
62 | )))
63 | ovs))))
64 |
65 | (defvar ruby-mkmf-log-overlay-keymap (make-keymap) "Keymap to toggle checks")
66 | (define-key ruby-mkmf-log-overlay-keymap "\r" 'ruby-mkmf-log-toggle-visible)
67 | (define-key ruby-mkmf-log-overlay-keymap "\t" 'ruby-mkmf-log-toggle-visible)
68 |
69 | (defun ruby-mkmf-log-hide-clear (start end)
70 | "Clear existing overlays for ruby-mkmf-log"
71 | (interactive
72 | (if (use-region-p)
73 | (list (region-beginning) (region-end))
74 | (list (point-min) (point-max))))
75 | (mapcar
76 | (lambda (o)
77 | (if (overlay-get o 'hide-ruby-mkmf-log)
78 | (delete-overlay o)))
79 | (overlays-in start end)))
80 |
81 | (defun ruby-mkmf-log-make-overlay (ov start end &optional front-advance rear-advance no-keymap)
82 | (let ((o (make-overlay start end nil front-advance rear-advance)))
83 | (overlay-put o 'category 'ruby-mkmf-log)
84 | (overlay-put o 'hide-ruby-mkmf-log (or ov o))
85 | (unless no-keymap
86 | (overlay-put o 'keymap ruby-mkmf-log-overlay-keymap))
87 | o))
88 |
89 | (defun ruby-mkmf-log-decorate (&optional start end)
90 | "Decorate Ruby mkmf checks"
91 | (interactive
92 | (if (use-region-p)
93 | (list (region-beginning) (region-end))
94 | (list (point-min) (point-max))))
95 | (save-excursion
96 | (or end (setq end (point-max)))
97 | (goto-char (or start (point-min)))
98 | (ruby-mkmf-log-hide-clear (point) end)
99 | (let (s b e b1 e1 b2 e2 bh eh o result ov)
100 | (while (and (setq b (re-search-forward ruby-mkmf-log-methods-re end t)
101 | bh (match-beginning 1) eh (match-end 1)
102 | b1 (match-beginning 2) e1 (match-end 2)
103 | b2 (match-beginning 3) e2 (match-end 3)
104 | result (match-string 3)
105 | s (point-at-bol))
106 | (setq e (re-search-forward ruby-mkmf-log-end-re end t)))
107 | (setq ov (ruby-mkmf-log-make-overlay nil b e t))
108 | (overlay-put ov 'face ruby-mkmf-log-check-content-face)
109 | ;; (overlay-put ov 'before-string ruby-mkmf-log-hidden-mark)
110 | (overlay-put ov 'invisible 'hide-ruby-mkmf-log)
111 |
112 | (setq o (ruby-mkmf-log-make-overlay ov s b))
113 | (overlay-put o 'face ruby-mkmf-log-check-heading-face)
114 | (when bh
115 | (setq o (ruby-mkmf-log-make-overlay t bh eh))
116 | (overlay-put o 'before-string ruby-mkmf-log-hidden-mark)
117 | (overlay-put o 'help-echo (match-string 1))
118 | (overlay-put o 'invisible 'hide-ruby-mkmf-log)
119 | )
120 | (setq o (ruby-mkmf-log-make-overlay ov b1 e1 nil nil t))
121 | (overlay-put o 'face ruby-mkmf-log-check-content-face)
122 | (setq o (ruby-mkmf-log-make-overlay ov b2 e2 nil nil t))
123 | (let ((face
124 | (cond
125 | ((string= result "yes") 'compilation-info)
126 | ((string= result "no") 'compilation-error)
127 | (t 'compilation-warning))))
128 | (overlay-put o 'face face))
129 | ))
130 | (when (re-search-forward "^\\(extconf\\.h\\) is:\n/\\* begin \\*/\n\\(\\(?:.*\n\\)*\\)/\\* end \\*/$" end t)
131 | (let ((o (ruby-mkmf-log-make-overlay t (match-beginning 1) (match-end 1))))
132 | (overlay-put o 'face 'compilation-info))
133 | (let ((b (match-beginning 2)) (e (match-end 2)))
134 | (goto-char b)
135 | (while (re-search-forward "^ *[1-9][0-9]*: " e t)
136 | (let ((o (ruby-mkmf-log-make-overlay t (match-beginning 0) (match-end 0))))
137 | (overlay-put o 'face 'compilation-line-number)))))
138 | ))
139 |
140 | ;;;###autoload
141 | (define-derived-mode ruby-mkmf-log-mode text-mode "MKMF"
142 | "Major mode for Ruby mkmf.log"
143 | (ruby-mkmf-log-decorate)
144 | (read-only-mode))
145 |
146 | ;;;###autoload
147 | (add-to-list 'auto-mode-alist (cons "/mkmf\\.log\\'" 'ruby-mkmf-log-mode))
148 |
149 | (provide 'ruby-mkmf-log-mode)
150 |
--------------------------------------------------------------------------------
/ruby-mode.el:
--------------------------------------------------------------------------------
1 | ;;; ruby-mode.el --- Major mode for editing Ruby files
2 |
3 | ;; Copyright (C) 1994, 1995, 1996 1997, 1998, 1999, 2000, 2001,
4 | ;; 2002,2003, 2004, 2005, 2006, 2007, 2008
5 | ;; Free Software Foundation, Inc.
6 |
7 | ;; Authors: Yukihiro Matsumoto, Nobuyoshi Nakada
8 | ;; URL: http://www.emacswiki.org/cgi-bin/wiki/RubyMode
9 | ;; Created: Fri Feb 4 14:49:13 JST 1994
10 | ;; Keywords: languages ruby
11 | ;; Version: 0.9
12 |
13 | ;; This file is not part of GNU Emacs. However, a newer version of
14 | ;; ruby-mode is included in recent releases of GNU Emacs (version 23
15 | ;; and up), but the new version is not guaranteed to be compatible
16 | ;; with older versions of Emacs or XEmacs. This file is the last
17 | ;; version that aims to keep this compatibility.
18 |
19 | ;; You can also get the latest version from the Emacs Lisp Package
20 | ;; Archive: http://tromey.com/elpa
21 |
22 | ;; This file is free software: you can redistribute it and/or modify
23 | ;; it under the terms of the GNU General Public License as published by
24 | ;; the Free Software Foundation, either version 3 of the License, or
25 | ;; (at your option) any later version.
26 |
27 | ;; It is distributed in the hope that it will be useful, but WITHOUT
28 | ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
29 | ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
30 | ;; License for more details.
31 |
32 | ;; You should have received a copy of the GNU General Public License
33 | ;; along with it. If not, see .
34 |
35 | ;;; Commentary:
36 |
37 | ;; Provides font-locking, indentation support, and navigation for Ruby code.
38 | ;;
39 | ;; If you're installing manually, you should add this to your .emacs
40 | ;; file after putting it on your load path:
41 | ;;
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))
45 | ;;
46 |
47 | ;;; Code:
48 |
49 | (defconst ruby-mode-revision "$Revision$"
50 | "Ruby mode revision string.")
51 |
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)))
55 | "Ruby mode version number.")
56 |
57 | (defconst ruby-keyword-end-re
58 | (if (string-match "\\_>" "ruby")
59 | "\\_>"
60 | "\\>"))
61 |
62 | (defconst ruby-block-beg-keywords
63 | '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do")
64 | "Keywords at the beginning of blocks.")
65 |
66 | (defconst ruby-block-beg-re
67 | (regexp-opt ruby-block-beg-keywords)
68 | "Regexp to match the beginning of blocks.")
69 |
70 | (defconst ruby-non-block-do-re
71 | (concat (regexp-opt '("while" "until" "for" "rescue") t) ruby-keyword-end-re)
72 | "Regexp to match")
73 |
74 | (defconst ruby-indent-beg-re
75 | (concat "\\(\\s *" (regexp-opt '("class" "module" "def") t) "\\)\\|"
76 | (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin")))
77 | "Regexp to match where the indentation gets deeper.")
78 |
79 | (defconst ruby-modifier-beg-keywords
80 | '("if" "unless" "while" "until")
81 | "Modifiers that are the same as the beginning of blocks.")
82 |
83 | (defconst ruby-modifier-beg-re
84 | (regexp-opt ruby-modifier-beg-keywords)
85 | "Regexp to match modifiers same as the beginning of blocks.")
86 |
87 | (defconst ruby-modifier-re
88 | (regexp-opt (cons "rescue" ruby-modifier-beg-keywords))
89 | "Regexp to match modifiers.")
90 |
91 | (defconst ruby-block-mid-keywords
92 | '("then" "else" "elsif" "when" "rescue" "ensure")
93 | "Keywords where the indentation gets shallower in middle of block statements.")
94 |
95 | (defconst ruby-block-mid-re
96 | (regexp-opt ruby-block-mid-keywords)
97 | "Regexp to match where the indentation gets shallower in middle of block statements.")
98 |
99 | (defconst ruby-block-op-keywords
100 | '("and" "or" "not")
101 | "Block operators.")
102 |
103 | (defconst ruby-block-hanging-re
104 | (regexp-opt (append ruby-modifier-beg-keywords ruby-block-op-keywords))
105 | "Regexp to match hanging block modifiers.")
106 |
107 | (defconst ruby-block-end-re "\\_")
108 |
109 | (defconst ruby-here-doc-beg-re
110 | "\\(<\\)<\\([-~]\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)")
111 |
112 | (defconst ruby-here-doc-end-re
113 | "^\\([ \t]+\\)?\\(.*\\)\\(.\\)$")
114 |
115 | (defun ruby-here-doc-end-match ()
116 | (concat "^"
117 | (if (match-string 2) "[ \t]*" nil)
118 | (regexp-quote
119 | (or (match-string 4)
120 | (match-string 5)
121 | (match-string 6)))))
122 |
123 | (defun ruby-here-doc-beg-match ()
124 | (let ((contents (concat
125 | (regexp-quote (concat (match-string 2) (match-string 3)))
126 | (if (string= (match-string 3) "_") "\\B" "\\b"))))
127 | (concat "<<"
128 | (let ((match (match-string 1)))
129 | (if (and match (> (length match) 0))
130 | (concat "\\(?:[-~]\\([\"']?\\)\\|\\([\"']\\)" (match-string 1) "\\)"
131 | contents "\\(\\1\\|\\2\\)")
132 | (concat "[-~]?\\([\"']\\|\\)" contents "\\1"))))))
133 |
134 | (defconst ruby-delimiter
135 | (concat "[?$/%(){}#\"'`.:]\\|<<\\|\\[\\|\\]\\|\\_<\\("
136 | ruby-block-beg-re
137 | "\\)\\_>\\|" ruby-block-end-re
138 | "\\|^=begin\\|" ruby-here-doc-beg-re)
139 | )
140 |
141 | (defconst ruby-negative
142 | (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\>\\|"
143 | ruby-block-end-re "\\|}\\|\\]\\)")
144 | "Regexp to match where the indentation gets shallower.")
145 |
146 | (defconst ruby-operator-chars "-,.+*/%&|^~=<>:")
147 | (defconst ruby-operator-re (concat "[" ruby-operator-chars "]"))
148 |
149 | (defconst ruby-symbol-chars "a-zA-Z0-9_")
150 | (defconst ruby-symbol-re (concat "[" ruby-symbol-chars "]"))
151 |
152 | (defvar ruby-mode-abbrev-table nil
153 | "Abbrev table in use in ruby-mode buffers.")
154 |
155 | (define-abbrev-table 'ruby-mode-abbrev-table ())
156 |
157 | (defvar ruby-mode-map nil "Keymap used in ruby mode.")
158 |
159 | (if ruby-mode-map
160 | nil
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))
177 |
178 | (defvar ruby-mode-syntax-table nil
179 | "Syntax table in use in ruby-mode buffers.")
180 |
181 | (if ruby-mode-syntax-table
182 | ()
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)
211 | )
212 |
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)
217 |
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)
222 |
223 | (defcustom ruby-comment-column 32
224 | "*Indentation column of comments."
225 | :type 'integer :group 'ruby)
226 | (put 'ruby-comment-column 'safe-local-variable 'integerp)
227 |
228 | (defcustom ruby-deep-arglist t
229 | "*Deep indent lists in parenthesis when non-nil.
230 | Also ignores spaces after parenthesis when 'space."
231 | :group 'ruby)
232 | (put 'ruby-deep-arglist 'safe-local-variable 'booleanp)
233 |
234 | (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
235 | "*Deep indent lists in parenthesis when non-nil. t means continuous line.
236 | Also ignores spaces after parenthesis when 'space."
237 | :group 'ruby)
238 |
239 | (defcustom ruby-deep-indent-paren-style 'space
240 | "Default deep indent style."
241 | :options '(t nil space) :group 'ruby)
242 |
243 | (defcustom ruby-encoding-map
244 | '((us-ascii . nil) ;; Do not put coding: us-ascii
245 | (utf-8 . nil) ;; Do not put coding: utf-8
246 | (shift-jis . cp932) ;; Emacs charset name of Shift_JIS
247 | (shift_jis . cp932) ;; MIME charset name of Shift_JIS
248 | (japanese-cp932 . cp932)) ;; Emacs charset name of CP932
249 | "Alist to map encoding name from Emacs to Ruby.
250 | Associating an encoding name with nil means it needs not be
251 | explicitly declared in magic comment."
252 | :type '(repeat (cons (symbol :tag "From") (symbol :tag "To")))
253 | :group 'ruby)
254 |
255 | (defcustom ruby-use-encoding-map t
256 | "*Use `ruby-encoding-map' to set encoding magic comment if this is non-nil."
257 | :type 'boolean :group 'ruby)
258 |
259 | (defvar ruby-indent-point nil "internal variable")
260 |
261 | (eval-when-compile (require 'cl))
262 | (defun ruby-imenu-create-index-in-block (prefix beg end)
263 | (let ((index-alist '()) (case-fold-search nil)
264 | name next pos decl sing)
265 | (goto-char beg)
266 | (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
267 | (setq sing (match-beginning 3))
268 | (setq decl (match-string 5))
269 | (setq next (match-end 0))
270 | (setq name (or (match-string 4) (match-string 6)))
271 | (setq pos (match-beginning 0))
272 | (cond
273 | ((string= "alias" decl)
274 | (if prefix (setq name (concat prefix name)))
275 | (push (cons name pos) index-alist))
276 | ((string= "def" decl)
277 | (if prefix
278 | (setq name
279 | (cond
280 | ((string-match "^self\." name)
281 | (concat (substring prefix 0 -1) (substring name 4)))
282 | (t (concat prefix name)))))
283 | (push (cons name pos) index-alist)
284 | (ruby-accurate-end-of-block end))
285 | (t
286 | (if (string= "self" name)
287 | (if prefix (setq name (substring prefix 0 -1)))
288 | (if prefix (setq name (concat (substring prefix 0 -1) "::" name)))
289 | (push (cons name pos) index-alist))
290 | (ruby-accurate-end-of-block end)
291 | (setq beg (point))
292 | (setq index-alist
293 | (nconc (ruby-imenu-create-index-in-block
294 | (concat name (if sing "." "#"))
295 | next beg) index-alist))
296 | (goto-char beg))))
297 | index-alist))
298 |
299 | (defun ruby-imenu-create-index ()
300 | (nreverse (ruby-imenu-create-index-in-block nil (point-min) nil)))
301 |
302 | (defun ruby-accurate-end-of-block (&optional end)
303 | (let (state)
304 | (or end (setq end (point-max)))
305 | (while (and (setq state (apply 'ruby-parse-partial end state))
306 | (>= (nth 2 state) 0) (< (point) end)))))
307 |
308 | (defun ruby-mode-variables ()
309 | (set-syntax-table ruby-mode-syntax-table)
310 | (setq show-trailing-whitespace t)
311 | (setq local-abbrev-table ruby-mode-abbrev-table)
312 | (make-local-variable 'indent-line-function)
313 | (setq indent-line-function 'ruby-indent-line)
314 | (make-local-variable 'require-final-newline)
315 | (setq require-final-newline t)
316 | (make-local-variable 'comment-start)
317 | (setq comment-start "# ")
318 | (make-local-variable 'comment-end)
319 | (setq comment-end "")
320 | (make-local-variable 'comment-column)
321 | (setq comment-column ruby-comment-column)
322 | (make-local-variable 'comment-start-skip)
323 | (setq comment-start-skip "#+ *")
324 | (setq indent-tabs-mode ruby-indent-tabs-mode)
325 | (make-local-variable 'parse-sexp-ignore-comments)
326 | (setq parse-sexp-ignore-comments t)
327 | (make-local-variable 'parse-sexp-lookup-properties)
328 | (setq parse-sexp-lookup-properties t)
329 | (make-local-variable 'paragraph-start)
330 | (setq paragraph-start (concat "$\\|" page-delimiter))
331 | (make-local-variable 'paragraph-separate)
332 | (setq paragraph-separate paragraph-start)
333 | (make-local-variable 'paragraph-ignore-fill-prefix)
334 | (setq paragraph-ignore-fill-prefix t))
335 |
336 | (defun ruby-mode-set-encoding ()
337 | "Insert or update a magic comment header with the proper encoding.
338 | `ruby-encoding-map' is looked up to convert an encoding name from
339 | Emacs to Ruby."
340 | (let* ((nonascii
341 | (save-excursion
342 | (widen)
343 | (goto-char (point-min))
344 | (re-search-forward "[^\0-\177]" nil t)))
345 | (coding-system
346 | (or coding-system-for-write
347 | buffer-file-coding-system))
348 | (coding-system
349 | (and coding-system
350 | (coding-system-change-eol-conversion coding-system nil)))
351 | (coding-system
352 | (and coding-system
353 | (or
354 | (coding-system-get coding-system :mime-charset)
355 | (let ((coding-type (coding-system-get coding-system :coding-type)))
356 | (cond ((eq coding-type 'undecided)
357 | (if nonascii
358 | (or (and (coding-system-get coding-system :prefer-utf-8)
359 | 'utf-8)
360 | (coding-system-get default-buffer-file-coding-system :coding-type)
361 | 'ascii-8bit)))
362 | ((memq coding-type '(utf-8 shift-jis))
363 | coding-type)
364 | (t coding-system))))))
365 | (coding-system
366 | (or coding-system
367 | 'us-ascii))
368 | (coding-system
369 | (let ((cons (assq coding-system ruby-encoding-map)))
370 | (if cons (cdr cons) coding-system)))
371 | (coding-system
372 | (and coding-system
373 | (symbol-name coding-system))))
374 | (if coding-system
375 | (save-excursion
376 | (widen)
377 | (goto-char (point-min))
378 | (if (looking-at "^#!") (beginning-of-line 2))
379 | (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
380 | (unless (string= (match-string 2) coding-system)
381 | (goto-char (match-beginning 2))
382 | (delete-region (point) (match-end 2))
383 | (and (looking-at "-\*-")
384 | (let ((n (skip-chars-backward " ")))
385 | (cond ((= n 0) (insert " ") (backward-char))
386 | ((= n -1) (insert " "))
387 | ((forward-char)))))
388 | (insert coding-system)))
389 | ((looking-at "\\s *#.*coding\\s *[:=]"))
390 | (t (when ruby-insert-encoding-magic-comment
391 | (insert "# -*- coding: " coding-system " -*-\n"))))))))
392 |
393 | (defun ruby-current-indentation ()
394 | (save-excursion
395 | (beginning-of-line)
396 | (back-to-indentation)
397 | (current-column)))
398 |
399 | (defun ruby-indent-line (&optional flag)
400 | "Correct indentation of the current ruby line."
401 | (ruby-indent-to (ruby-calculate-indent)))
402 |
403 | (defun ruby-indent-command ()
404 | (interactive)
405 | (ruby-indent-line t))
406 |
407 | (defun ruby-indent-to (x)
408 | (if x
409 | (let (shift top beg)
410 | (and (< x 0) (error "invalid nest"))
411 | (setq shift (current-column))
412 | (beginning-of-line)
413 | (setq beg (point))
414 | (back-to-indentation)
415 | (setq top (current-column))
416 | (skip-chars-backward " \t")
417 | (if (>= shift top) (setq shift (- shift top))
418 | (setq shift 0))
419 | (if (and (bolp)
420 | (= x top))
421 | (move-to-column (+ x shift))
422 | (move-to-column top)
423 | (delete-region beg (point))
424 | (beginning-of-line)
425 | (indent-to x)
426 | (move-to-column (+ x shift))))))
427 |
428 | (defun ruby-special-char-p (&optional pnt)
429 | (setq pnt (or pnt (point)))
430 | (let ((c (char-before pnt)) (b (and (< (point-min) pnt) (char-before (1- pnt)))))
431 | (cond ((or (eq c ??) (eq c ?$)))
432 | ((and (eq c ?:) (or (not b) (eq (char-syntax b) ? ))))
433 | ((eq c ?\\) (eq b ??)))))
434 |
435 | (defun ruby-singleton-class-p ()
436 | (save-excursion
437 | (forward-word -1)
438 | (and (or (bolp) (not (eq (char-before (point)) ?_)))
439 | (looking-at "class\\s *<<"))))
440 |
441 | (defun ruby-expr-beg (&optional option)
442 | (save-excursion
443 | (store-match-data nil)
444 | (let ((space (skip-chars-backward " \t"))
445 | (start (point)))
446 | (cond
447 | ((bolp) t)
448 | ((progn
449 | (forward-char -1)
450 | (and (looking-at "\\?")
451 | (or (eq (char-syntax (preceding-char)) ?w)
452 | (ruby-special-char-p))))
453 | nil)
454 | ((and (eq option 'heredoc) (< space 0))
455 | (not (progn (goto-char start) (ruby-singleton-class-p))))
456 | ((or (looking-at ruby-operator-re)
457 | (looking-at "[\\[({,;]")
458 | (and (looking-at "[!?]")
459 | (or (not (eq option 'modifier))
460 | (bolp)
461 | (save-excursion (forward-char -1) (looking-at "\\Sw$"))))
462 | (and (looking-at ruby-symbol-re)
463 | (skip-chars-backward ruby-symbol-chars)
464 | (cond
465 | ((looking-at (regexp-opt
466 | (append ruby-block-beg-keywords
467 | ruby-block-op-keywords
468 | ruby-block-mid-keywords)
469 | 'words))
470 | (goto-char (match-end 0))
471 | (not (looking-at "\\s_\\|[!?:]")))
472 | ((eq option 'expr-qstr)
473 | (looking-at "[a-zA-Z][a-zA-z0-9_]* +%[^ \t]"))
474 | ((eq option 'expr-re)
475 | (looking-at "[a-zA-Z][a-zA-z0-9_]* +/[^ \t]"))
476 | (t nil)))))))))
477 |
478 | (defun ruby-forward-string (term &optional end no-error expand)
479 | (let ((n 1) (c (string-to-char term))
480 | (re (if expand
481 | (concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
482 | (concat "[^\\]\\(\\\\\\\\\\)*[" term "]"))))
483 | (while (and (re-search-forward re end no-error)
484 | (if (match-beginning 3)
485 | (ruby-forward-string "}{" end no-error nil)
486 | (> (setq n (if (eq (char-before (point)) c)
487 | (1- n) (1+ n))) 0)))
488 | (forward-char -1))
489 | (cond ((zerop n))
490 | (no-error nil)
491 | ((error "unterminated string")))))
492 |
493 | (defun ruby-deep-indent-paren-p (c &optional pos)
494 | (cond ((save-excursion
495 | (if pos (goto-char pos))
496 | (ruby-expr-beg))
497 | nil)
498 | ((listp ruby-deep-indent-paren)
499 | (let ((deep (assoc c ruby-deep-indent-paren)))
500 | (cond (deep
501 | (or (cdr deep) ruby-deep-indent-paren-style))
502 | ((memq c ruby-deep-indent-paren)
503 | ruby-deep-indent-paren-style))))
504 | ((eq c ruby-deep-indent-paren) ruby-deep-indent-paren-style)
505 | ((eq c ?\( ) ruby-deep-arglist)))
506 |
507 | (defun ruby-parse-partial (&optional end in-string nest depth pcol indent)
508 | (or depth (setq depth 0))
509 | (or indent (setq indent 0))
510 | (when (re-search-forward ruby-delimiter end 'move)
511 | (let ((pnt (point)) w re expand)
512 | (goto-char (match-beginning 0))
513 | (cond
514 | ((and (memq (char-before) '(?@ ?$)) (looking-at "\\sw"))
515 | (goto-char pnt))
516 | ((looking-at "[\"`]") ;skip string
517 | (cond
518 | ((and (not (eobp))
519 | (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t))
520 | nil)
521 | (t
522 | (setq in-string (point))
523 | (goto-char end))))
524 | ((looking-at "'")
525 | (cond
526 | ((and (not (eobp))
527 | (re-search-forward "[^\\]\\(\\\\\\\\\\)*'" end t))
528 | nil)
529 | (t
530 | (setq in-string (point))
531 | (goto-char end))))
532 | ((looking-at "/=")
533 | (goto-char pnt))
534 | ((looking-at "/")
535 | (cond
536 | ((and (not (eobp)) (ruby-expr-beg 'expr-re))
537 | (if (ruby-forward-string "/" end t t)
538 | nil
539 | (setq in-string (point))
540 | (goto-char end)))
541 | (t
542 | (goto-char pnt))))
543 | ((looking-at "%")
544 | (cond
545 | ((and (not (eobp))
546 | (ruby-expr-beg 'expr-qstr)
547 | (not (looking-at "%="))
548 | (looking-at "%[QqrxWw]?\\([^a-zA-Z0-9 \t\n]\\)"))
549 | (goto-char (match-beginning 1))
550 | (setq expand (not (memq (char-before) '(?q ?w))))
551 | (setq w (match-string 1))
552 | (cond
553 | ((string= w "[") (setq re "]["))
554 | ((string= w "{") (setq re "}{"))
555 | ((string= w "(") (setq re ")("))
556 | ((string= w "<") (setq re "><"))
557 | ((and expand (string= w "\\"))
558 | (setq w (concat "\\" w))))
559 | (unless (cond (re (ruby-forward-string re end t expand))
560 | (expand (ruby-forward-string w end t t))
561 | (t (re-search-forward
562 | (if (string= w "\\")
563 | "\\\\[^\\]*\\\\"
564 | (concat "[^\\]\\(\\\\\\\\\\)*" w))
565 | end t)))
566 | (setq in-string (point))
567 | (goto-char end)))
568 | (t
569 | (goto-char pnt))))
570 | ((looking-at "\\?") ;skip ?char
571 | (cond
572 | ((and (ruby-expr-beg)
573 | (looking-at "?\\(\\\\C-\\|\\\\M-\\)*\\\\?."))
574 | (goto-char (match-end 0)))
575 | (t
576 | (goto-char pnt))))
577 | ((looking-at "\\$") ;skip $char
578 | (goto-char pnt)
579 | (forward-char 1))
580 | ((looking-at "#") ;skip comment
581 | (forward-line 1)
582 | (goto-char (point))
583 | )
584 | ((looking-at "[\\[{(]")
585 | (let ((deep (ruby-deep-indent-paren-p (char-after))))
586 | (if (and deep (or (not (eq (char-after) ?\{)) (ruby-expr-beg)))
587 | (progn
588 | (and (eq deep 'space) (looking-at ".\\s +[^# \t\n]")
589 | (setq pnt (1- (match-end 0))))
590 | (setq nest (cons (cons (char-after (point)) (point)) nest))
591 | (setq pcol (cons (cons pnt depth) pcol))
592 | (setq depth 0))
593 | (setq nest (cons (cons (char-after (point)) pnt) nest))
594 | (setq depth (1+ depth))))
595 | (goto-char pnt)
596 | )
597 | ((looking-at "[])}]")
598 | (if (ruby-deep-indent-paren-p (matching-paren (char-after))
599 | (if nest
600 | (cdr (nth 0 nest))
601 | (save-excursion
602 | (forward-char)
603 | (ruby-backward-sexp)
604 | (point))))
605 | (setq depth (cdr (car pcol)) pcol (cdr pcol))
606 | (setq depth (1- depth)))
607 | (setq nest (cdr nest))
608 | (goto-char pnt))
609 | ((looking-at ruby-block-end-re)
610 | (if (or (and (not (bolp))
611 | (progn
612 | (forward-char -1)
613 | (setq w (char-after (point)))
614 | (or (eq ?_ w)
615 | (eq ?. w))))
616 | (progn
617 | (goto-char pnt)
618 | (setq w (char-after (point)))
619 | (or (eq ?_ w)
620 | (eq ?! w)
621 | (eq ?? w))))
622 | nil
623 | (setq nest (cdr nest))
624 | (setq depth (1- depth)))
625 | (goto-char pnt))
626 | ((looking-at "def\\s +[^(\n;]*")
627 | (if (or (bolp)
628 | (progn
629 | (forward-char -1)
630 | (not (eq ?_ (char-after (point))))))
631 | (progn
632 | (setq nest (cons (cons nil pnt) nest))
633 | (setq depth (1+ depth))))
634 | (goto-char (match-end 0)))
635 | ((looking-at (concat "\\_<\\(" ruby-block-beg-re "\\)\\_>"))
636 | (and
637 | (save-match-data
638 | (or (not (looking-at (concat "do" ruby-keyword-end-re)))
639 | (save-excursion
640 | (back-to-indentation)
641 | (not (looking-at ruby-non-block-do-re)))))
642 | (or (bolp)
643 | (progn
644 | (forward-char -1)
645 | (setq w (char-after (point)))
646 | (not (or (eq ?_ w)
647 | (eq ?. w)))))
648 | (goto-char pnt)
649 | (setq w (char-after (point)))
650 | (not (eq ?_ w))
651 | (not (eq ?! w))
652 | (not (eq ?? w))
653 | (not (eq ?: w))
654 | (skip-chars-forward " \t")
655 | (goto-char (match-beginning 0))
656 | (or (not (looking-at ruby-modifier-re))
657 | (ruby-expr-beg 'modifier))
658 | (goto-char pnt)
659 | (setq nest (cons (cons nil pnt) nest))
660 | (setq depth (1+ depth)))
661 | (goto-char pnt))
662 | ((looking-at ":\\(['\"]\\)")
663 | (goto-char (match-beginning 1))
664 | (ruby-forward-string (buffer-substring (match-beginning 1) (match-end 1)) end))
665 | ((looking-at ":\\([-,.+*/%&|^~<>]=?\\|===?\\|<=>\\|![~=]?\\)")
666 | (goto-char (match-end 0)))
667 | ((looking-at ":\\([a-zA-Z_][a-zA-Z_0-9]*[!?=]?\\)?")
668 | (goto-char (match-end 0)))
669 | ((or (looking-at "\\.\\.\\.?")
670 | (looking-at "\\.[0-9]+")
671 | (looking-at "\\.[a-zA-Z_0-9]+")
672 | (looking-at "\\."))
673 | (goto-char (match-end 0)))
674 | ((looking-at "^=begin")
675 | (if (re-search-forward "^=end" end t)
676 | (forward-line 1)
677 | (setq in-string (match-end 0))
678 | (goto-char end)))
679 | ((looking-at "<<")
680 | (cond
681 | ((and (ruby-expr-beg 'heredoc)
682 | (looking-at "<<\\([-~]\\)?\\(\\([\"'`]\\)\\([^\n]+?\\)\\3\\|\\(?:\\sw\\|\\s_\\)+\\)"))
683 | (setq re (regexp-quote (or (match-string 4) (match-string 2))))
684 | (if (match-beginning 1) (setq re (concat "\\s *" re)))
685 | (let* ((id-end (goto-char (match-end 0)))
686 | (line-end-position (save-excursion (end-of-line) (point)))
687 | (state (list in-string nest depth pcol indent)))
688 | ;; parse the rest of the line
689 | (while (and (> line-end-position (point))
690 | (setq state (apply 'ruby-parse-partial
691 | line-end-position state))))
692 | (setq in-string (car state)
693 | nest (nth 1 state)
694 | depth (nth 2 state)
695 | pcol (nth 3 state)
696 | indent (nth 4 state))
697 | ;; skip heredoc section
698 | (if (re-search-forward (concat "^" re "$") end 'move)
699 | (forward-line 1)
700 | (setq in-string id-end)
701 | (goto-char end))))
702 | (t
703 | (goto-char pnt))))
704 | ((looking-at "^__END__$")
705 | (goto-char pnt))
706 | ((looking-at ruby-here-doc-beg-re)
707 | (if (re-search-forward (ruby-here-doc-end-match)
708 | ruby-indent-point t)
709 | (forward-line 1)
710 | (setq in-string (match-end 0))
711 | (goto-char ruby-indent-point)))
712 | (t
713 | (error (format "bad string %s"
714 | (buffer-substring (point) pnt)
715 | ))))))
716 | (list in-string nest depth pcol))
717 |
718 | (defun ruby-parse-region (start end)
719 | (let (state)
720 | (save-excursion
721 | (if start
722 | (goto-char start)
723 | (ruby-beginning-of-indent))
724 | (save-restriction
725 | (narrow-to-region (point) end)
726 | (while (and (> end (point))
727 | (setq state (apply 'ruby-parse-partial end state))))))
728 | (list (nth 0 state) ; in-string
729 | (car (nth 1 state)) ; nest
730 | (nth 2 state) ; depth
731 | (car (car (nth 3 state))) ; pcol
732 | ;(car (nth 5 state)) ; indent
733 | )))
734 |
735 | (defun ruby-indent-size (pos nest)
736 | (+ pos (* (or nest 1) ruby-indent-level)))
737 |
738 | (defun ruby-calculate-indent (&optional parse-start)
739 | (save-excursion
740 | (beginning-of-line)
741 | (let ((ruby-indent-point (point))
742 | (case-fold-search nil)
743 | state bol eol begin op-end
744 | (paren (progn (skip-syntax-forward " ")
745 | (and (char-after) (matching-paren (char-after)))))
746 | (indent 0))
747 | (if parse-start
748 | (goto-char parse-start)
749 | (ruby-beginning-of-indent)
750 | (setq parse-start (point)))
751 | (back-to-indentation)
752 | (setq indent (current-column))
753 | (setq state (ruby-parse-region parse-start ruby-indent-point))
754 | (cond
755 | ((nth 0 state) ; within string
756 | (setq indent nil)) ; do nothing
757 | ((car (nth 1 state)) ; in paren
758 | (goto-char (setq begin (cdr (nth 1 state))))
759 | (let ((deep (ruby-deep-indent-paren-p (car (nth 1 state))
760 | (1- (cdr (nth 1 state))))))
761 | (if deep
762 | (cond ((and (eq deep t) (eq (car (nth 1 state)) paren))
763 | (skip-syntax-backward " ")
764 | (setq indent (1- (current-column))))
765 | ((eq deep 'space)
766 | (goto-char (cdr (nth 1 state)))
767 | (setq indent (1+ (current-column))))
768 | ((let ((s (ruby-parse-region (point) ruby-indent-point)))
769 | (and (nth 2 s) (> (nth 2 s) 0)
770 | (or (goto-char (cdr (nth 1 s))) t)))
771 | (forward-word -1)
772 | (setq indent (ruby-indent-size (current-column) (nth 2 state))))
773 | (t
774 | (setq indent (current-column))
775 | (cond ((eq deep 'space))
776 | (paren (setq indent (1- indent)))
777 | (t (setq indent (ruby-indent-size (1- indent) 1))))))
778 | (if (nth 3 state) (goto-char (nth 3 state))
779 | (goto-char parse-start) (back-to-indentation))
780 | (setq indent (ruby-indent-size (current-column) (nth 2 state))))
781 | (and (eq (car (nth 1 state)) paren)
782 | (ruby-deep-indent-paren-p (matching-paren paren)
783 | (1- (cdr (nth 1 state))))
784 | (search-backward (char-to-string paren))
785 | (setq indent (current-column)))))
786 | ((and (nth 2 state) (> (nth 2 state) 0)) ; in nest
787 | (if (null (cdr (nth 1 state)))
788 | (error "invalid nest"))
789 | (goto-char (cdr (nth 1 state)))
790 | (forward-word -1) ; skip back a keyword
791 | (setq begin (point))
792 | (cond
793 | ((looking-at "do\\>[^_]") ; iter block is a special case
794 | (if (nth 3 state) (goto-char (nth 3 state))
795 | (goto-char parse-start) (back-to-indentation))
796 | (setq indent (ruby-indent-size (current-column) (nth 2 state))))
797 | (t
798 | (setq indent (+ (current-column) ruby-indent-level)))))
799 |
800 | ((and (nth 2 state) (< (nth 2 state) 0)) ; in negative nest
801 | (setq indent (ruby-indent-size (current-column) (nth 2 state)))))
802 | (when indent
803 | (goto-char ruby-indent-point)
804 | (end-of-line)
805 | (setq eol (point))
806 | (beginning-of-line)
807 | (cond
808 | ((and (not (ruby-deep-indent-paren-p paren
809 | (and (cdr (nth 1 state))
810 | (1- (cdr (nth 1 state))))))
811 | (re-search-forward ruby-negative eol t))
812 | (and (not (eq ?_ (char-after (match-end 0))))
813 | (setq indent (- indent ruby-indent-level))))
814 | ((and
815 | (save-excursion
816 | (beginning-of-line)
817 | (not (bobp)))
818 | (or (ruby-deep-indent-paren-p t)
819 | (null (car (nth 1 state)))))
820 | ;; goto beginning of non-empty no-comment line
821 | (let (end done)
822 | (while (not done)
823 | (skip-chars-backward " \t\n")
824 | (setq end (point))
825 | (beginning-of-line)
826 | (if (re-search-forward "^\\s *#" end t)
827 | (beginning-of-line)
828 | (setq done t))))
829 | (setq bol (point))
830 | (end-of-line)
831 | ;; skip the comment at the end
832 | (skip-chars-backward " \t")
833 | (let (end (pos (point)))
834 | (beginning-of-line)
835 | (while (and (re-search-forward "#" pos t)
836 | (setq end (1- (point)))
837 | (or (ruby-special-char-p end)
838 | (and (setq state (ruby-parse-region parse-start end))
839 | (nth 0 state))))
840 | (setq end nil))
841 | (goto-char (or end pos))
842 | (skip-chars-backward " \t")
843 | (setq begin (if (and end (nth 0 state)) pos (cdr (nth 1 state))))
844 | (setq state (ruby-parse-region parse-start (point))))
845 | (or (bobp) (forward-char -1))
846 | (and
847 | (or (and (looking-at ruby-symbol-re)
848 | (skip-chars-backward ruby-symbol-chars)
849 | (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>"))
850 | (not (eq (point) (nth 3 state)))
851 | (save-excursion
852 | (goto-char (match-end 0))
853 | (not (looking-at "[a-z_]"))))
854 | (and (looking-at ruby-operator-re)
855 | (not (ruby-special-char-p))
856 | ;; operator at the end of line
857 | (let ((c (char-after (point))))
858 | (and
859 | ;; (or (null begin)
860 | ;; (save-excursion
861 | ;; (goto-char begin)
862 | ;; (skip-chars-forward " \t")
863 | ;; (not (or (eolp) (looking-at "#")
864 | ;; (and (eq (car (nth 1 state)) ?{)
865 | ;; (looking-at "|"))))))
866 | (or (not (eq ?/ c))
867 | (null (nth 0 (ruby-parse-region (or begin parse-start) (point)))))
868 | (or (not (eq ?| (char-after (point))))
869 | (save-excursion
870 | (or (eolp) (forward-char -1))
871 | (cond
872 | ((search-backward "|" nil t)
873 | (skip-chars-backward " \t\n")
874 | (and (not (eolp))
875 | (progn
876 | (forward-char -1)
877 | (not (looking-at "{")))
878 | (progn
879 | (forward-word -1)
880 | (not (looking-at "do\\>[^_]")))))
881 | (t t))))
882 | (not (eq ?, c))
883 | (setq op-end t)))))
884 | (setq indent
885 | (cond
886 | ((and
887 | (null op-end)
888 | (not (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>")))
889 | (eq (ruby-deep-indent-paren-p t) 'space)
890 | (not (bobp)))
891 | (widen)
892 | (goto-char (or begin parse-start))
893 | (skip-syntax-forward " ")
894 | (current-column))
895 | ((car (nth 1 state)) indent)
896 | (t
897 | (+ indent ruby-indent-level))))))))
898 | (goto-char ruby-indent-point)
899 | (beginning-of-line)
900 | (skip-syntax-forward " ")
901 | (if (looking-at "\\.[^.]\\|&\\.")
902 | (+ indent ruby-indent-level)
903 | indent))))
904 |
905 | (defun ruby-electric-brace (arg)
906 | (interactive "P")
907 | (insert-char last-command-event 1)
908 | (ruby-indent-line t)
909 | (delete-char -1)
910 | (self-insert-command (prefix-numeric-value arg)))
911 |
912 | (eval-when-compile
913 | (defmacro defun-region-command (func args &rest body)
914 | (let ((intr (car body)))
915 | (when (featurep 'xemacs)
916 | (if (stringp intr) (setq intr (cadr body)))
917 | (and (eq (car intr) 'interactive)
918 | (setq intr (cdr intr))
919 | (setcar intr (concat "_" (car intr)))))
920 | (cons 'defun (cons func (cons args body))))))
921 |
922 | (defun-region-command ruby-beginning-of-defun (&optional arg)
923 | "Move backward to next beginning-of-defun.
924 | With argument, do this that many times.
925 | Returns t unless search stops due to end of buffer."
926 | (interactive "p")
927 | (and (re-search-backward (concat "^\\(" ruby-block-beg-re "\\)\\_>")
928 | nil 'move (or arg 1))
929 | (progn (beginning-of-line) t)))
930 |
931 | (defun ruby-beginning-of-indent ()
932 | (and (re-search-backward (concat "^\\(" ruby-indent-beg-re "\\)\\_>")
933 | nil 'move)
934 | (progn
935 | (beginning-of-line)
936 | t)))
937 |
938 | (defun-region-command ruby-end-of-defun (&optional arg)
939 | "Move forward to next end of defun.
940 | An end of a defun is found by moving forward from the beginning of one."
941 | (interactive "p")
942 | (and (re-search-forward (concat "^\\(" ruby-block-end-re "\\)\\($\\|\\b[^_]\\)")
943 | nil 'move (or arg 1))
944 | (progn (beginning-of-line) t))
945 | (forward-line 1))
946 |
947 | (defun ruby-move-to-block (n)
948 | (let (start pos done down (orig (point)))
949 | (setq start (ruby-calculate-indent))
950 | (setq down (looking-at (if (< n 0) ruby-block-end-re
951 | (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))))
952 | (while (and (not done) (not (if (< n 0) (bobp) (eobp))))
953 | (forward-line n)
954 | (cond
955 | ((looking-at "^\\s *$"))
956 | ((looking-at "^\\s *#"))
957 | ((and (> n 0) (looking-at "^=begin\\>"))
958 | (re-search-forward "^=end\\>"))
959 | ((and (< n 0) (looking-at "^=end\\>"))
960 | (re-search-backward "^=begin\\>"))
961 | (t
962 | (setq pos (current-indentation))
963 | (cond
964 | ((< start pos)
965 | (setq down t))
966 | ((and down (= pos start))
967 | (setq done t))
968 | ((> start pos)
969 | (setq done t)))))
970 | (if done
971 | (save-excursion
972 | (back-to-indentation)
973 | (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>"))
974 | (setq done nil)))))
975 | (back-to-indentation)
976 | (when (< n 0)
977 | (let ((eol (point-at-eol)) state next)
978 | (if (< orig eol) (setq eol orig))
979 | (setq orig (point))
980 | (while (and (setq next (apply 'ruby-parse-partial eol state))
981 | (< (point) eol))
982 | (setq state next))
983 | (when (cdaadr state)
984 | (goto-char (cdaadr state)))
985 | (backward-word)))))
986 |
987 | (defun-region-command ruby-beginning-of-block (&optional arg)
988 | "Move backward to next beginning-of-block"
989 | (interactive "p")
990 | (ruby-move-to-block (- (or arg 1))))
991 |
992 | (defun-region-command ruby-end-of-block (&optional arg)
993 | "Move forward to next beginning-of-block"
994 | (interactive "p")
995 | (ruby-move-to-block (or arg 1)))
996 |
997 | (defun-region-command ruby-forward-sexp (&optional cnt)
998 | (interactive "p")
999 | (if (and (numberp cnt) (< cnt 0))
1000 | (ruby-backward-sexp (- cnt))
1001 | (let ((i (or cnt 1)))
1002 | (condition-case nil
1003 | (while (> i 0)
1004 | (skip-syntax-forward " ")
1005 | (if (looking-at ",\\s *") (goto-char (match-end 0)))
1006 | (cond ((looking-at "\\?\\(\\\\[CM]-\\)*\\\\?\\S ")
1007 | (goto-char (match-end 0)))
1008 | ((progn
1009 | (skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
1010 | (looking-at "\\s("))
1011 | (goto-char (scan-sexps (point) 1)))
1012 | ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
1013 | (not (eq (char-before (point)) ?.))
1014 | (not (eq (char-before (point)) ?:)))
1015 | (ruby-end-of-block)
1016 | (forward-word 1))
1017 | ((looking-at "\\(\\$\\|@@?\\)?\\sw")
1018 | (while (progn
1019 | (while (progn (forward-word 1) (looking-at "_")))
1020 | (cond ((looking-at "::") (forward-char 2) t)
1021 | ((> (skip-chars-forward ".") 0))
1022 | ((looking-at "\\?\\|!\\(=[~=>]\\|[^~=]\\)")
1023 | (forward-char 1) nil)))))
1024 | ((let (state expr)
1025 | (while
1026 | (progn
1027 | (setq expr (or expr (ruby-expr-beg)
1028 | (looking-at "%\\sw?\\Sw\\|[\"'`/]")))
1029 | (nth 1 (setq state (apply 'ruby-parse-partial nil state))))
1030 | (setq expr t)
1031 | (skip-chars-forward "<"))
1032 | (not expr))))
1033 | (setq i (1- i)))
1034 | ((error) (forward-word 1)))
1035 | i)))
1036 |
1037 | (defun-region-command ruby-backward-sexp (&optional cnt)
1038 | (interactive "p")
1039 | (if (and (numberp cnt) (< cnt 0))
1040 | (ruby-forward-sexp (- cnt))
1041 | (let ((i (or cnt 1)))
1042 | (condition-case nil
1043 | (while (> i 0)
1044 | (skip-chars-backward " \t\n,.:;|&^~=!?\\+\\-\\*")
1045 | (forward-char -1)
1046 | (cond ((looking-at "\\s)")
1047 | (goto-char (scan-sexps (1+ (point)) -1))
1048 | (case (char-before)
1049 | (?% (forward-char -1))
1050 | ('(?q ?Q ?w ?W ?r ?x)
1051 | (if (eq (char-before (1- (point))) ?%) (forward-char -2))))
1052 | nil)
1053 | ((looking-at "\\s\"\\|\\\\\\S_")
1054 | (let ((c (char-to-string (char-before (match-end 0)))))
1055 | (while (and (search-backward c)
1056 | (oddp (skip-chars-backward "\\")))))
1057 | nil)
1058 | ((looking-at "\\s.\\|\\s\\")
1059 | (if (ruby-special-char-p) (forward-char -1)))
1060 | ((looking-at "\\s(") nil)
1061 | (t
1062 | (forward-char 1)
1063 | (while (progn (forward-word -1)
1064 | (case (char-before)
1065 | (?_ t)
1066 | (?. (forward-char -1) t)
1067 | ((?$ ?@)
1068 | (forward-char -1)
1069 | (and (eq (char-before) (char-after)) (forward-char -1)))
1070 | (?:
1071 | (forward-char -1)
1072 | (eq (char-before) :)))))
1073 | (if (looking-at ruby-block-end-re)
1074 | (ruby-beginning-of-block))
1075 | nil))
1076 | (setq i (1- i)))
1077 | ((error)))
1078 | i)))
1079 |
1080 | (defun ruby-reindent-then-newline-and-indent ()
1081 | (interactive "*")
1082 | (newline)
1083 | (save-excursion
1084 | (end-of-line 0)
1085 | (indent-according-to-mode)
1086 | (delete-region (point) (progn (skip-chars-backward " \t") (point))))
1087 | (indent-according-to-mode))
1088 |
1089 | (fset 'ruby-encomment-region (symbol-function 'comment-region))
1090 |
1091 | (defun ruby-decomment-region (beg end)
1092 | (interactive "r")
1093 | (save-excursion
1094 | (goto-char beg)
1095 | (while (re-search-forward "^\\([ \t]*\\)#" end t)
1096 | (replace-match "\\1" nil nil)
1097 | (save-excursion
1098 | (ruby-indent-line)))))
1099 |
1100 | (defun ruby-insert-end ()
1101 | (interactive)
1102 | (insert "end")
1103 | (ruby-indent-line t)
1104 | (end-of-line))
1105 |
1106 | (defun ruby-mark-defun ()
1107 | "Put mark at end of this Ruby function, point at beginning."
1108 | (interactive)
1109 | (push-mark (point))
1110 | (ruby-end-of-defun)
1111 | (push-mark (point) nil t)
1112 | (ruby-beginning-of-defun)
1113 | (re-search-backward "^\n" (- (point) 1) t))
1114 |
1115 | (defun ruby-indent-exp (&optional shutup-p)
1116 | "Indent each line in the balanced expression following point syntactically.
1117 | If optional SHUTUP-P is non-nil, no errors are signalled if no
1118 | balanced expression is found."
1119 | (interactive "*P")
1120 | (let ((here (point-marker)) start top column (nest t))
1121 | (set-marker-insertion-type here t)
1122 | (unwind-protect
1123 | (progn
1124 | (beginning-of-line)
1125 | (setq start (point) top (current-indentation))
1126 | (while (and (not (eobp))
1127 | (progn
1128 | (setq column (ruby-calculate-indent start))
1129 | (cond ((> column top)
1130 | (setq nest t))
1131 | ((and (= column top) nest)
1132 | (setq nest nil) t))))
1133 | (ruby-indent-to column)
1134 | (beginning-of-line 2)))
1135 | (goto-char here)
1136 | (set-marker here nil))))
1137 |
1138 | (defun ruby-add-log-current-method ()
1139 | "Return current method string."
1140 | (condition-case nil
1141 | (save-excursion
1142 | (let (mname mlist (indent 0))
1143 | ;; get current method (or class/module)
1144 | (if (re-search-backward
1145 | (concat "^[ \t]*\\(def\\|class\\|module\\)[ \t]+"
1146 | "\\("
1147 | ;; \\. and :: for class method
1148 | "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
1149 | "+\\)")
1150 | nil t)
1151 | (progn
1152 | (setq mname (match-string 2))
1153 | (unless (string-equal "def" (match-string 1))
1154 | (setq mlist (list mname) mname nil))
1155 | (goto-char (match-beginning 1))
1156 | (setq indent (current-column))
1157 | (beginning-of-line)))
1158 | ;; nest class/module
1159 | (while (and (> indent 0)
1160 | (re-search-backward
1161 | (concat
1162 | "^[ \t]*\\(class\\|module\\)[ \t]+"
1163 | "\\([A-Z]" ruby-symbol-re "*\\)")
1164 | nil t))
1165 | (goto-char (match-beginning 1))
1166 | (if (< (current-column) indent)
1167 | (progn
1168 | (setq mlist (cons (match-string 2) mlist))
1169 | (setq indent (current-column))
1170 | (beginning-of-line))))
1171 | (when mname
1172 | (let ((mn (split-string mname "\\.\\|::")))
1173 | (if (cdr mn)
1174 | (progn
1175 | (cond
1176 | ((string-equal "" (car mn))
1177 | (setq mn (cdr mn) mlist nil))
1178 | ((string-equal "self" (car mn))
1179 | (setq mn (cdr mn)))
1180 | ((let ((ml (nreverse mlist)))
1181 | (while ml
1182 | (if (string-equal (car ml) (car mn))
1183 | (setq mlist (nreverse (cdr ml)) ml nil))
1184 | (or (setq ml (cdr ml)) (nreverse mlist))))))
1185 | (if mlist
1186 | (setcdr (last mlist) mn)
1187 | (setq mlist mn))
1188 | (setq mn (last mn 2))
1189 | (setq mname (concat "." (cadr mn)))
1190 | (setcdr mn nil))
1191 | (setq mname (concat "#" mname)))))
1192 | ;; generate string
1193 | (if (consp mlist)
1194 | (setq mlist (mapconcat (function identity) mlist "::")))
1195 | (if mname
1196 | (if mlist (concat mlist mname) mname)
1197 | mlist)))))
1198 |
1199 | (defun ruby-brace-to-do-end ()
1200 | (when (looking-at "{")
1201 | (let ((orig (point)) (end (progn (ruby-forward-sexp) (point)))
1202 | oneline (end (make-marker)))
1203 | (setq oneline (and (eolp) (<= (point-at-bol) orig)))
1204 | (when (eq (char-before) ?\})
1205 | (delete-char -1)
1206 | (cond
1207 | (oneline
1208 | (insert "\n")
1209 | (set-marker end (point)))
1210 | ((eq (char-syntax (preceding-char)) ?w)
1211 | (insert " ")))
1212 | (insert "end")
1213 | (if (eq (char-syntax (following-char)) ?w)
1214 | (insert " "))
1215 | (goto-char orig)
1216 | (delete-char 1)
1217 | (if (eq (char-syntax (preceding-char)) ?w)
1218 | (insert " "))
1219 | (insert "do")
1220 | (when (looking-at "\\sw\\||")
1221 | (insert " ")
1222 | (backward-char))
1223 | (when oneline
1224 | (setq orig (point))
1225 | (when (cond
1226 | ((looking-at "\\s *|")
1227 | (goto-char (match-end 0))
1228 | (and (search-forward "|" (point-at-eol) 'move)
1229 | (not (eolp))))
1230 | (t))
1231 | (while (progn
1232 | (insert "\n")
1233 | (ruby-forward-sexp)
1234 | (looking-at "\\s *;\\s *"))
1235 | (delete-char (- (match-end 0) (match-beginning 0))))
1236 | (goto-char orig)
1237 | (beginning-of-line 2)
1238 | (indent-region (point) end))
1239 | (goto-char orig))
1240 | t))))
1241 |
1242 | (defun ruby-do-end-to-brace ()
1243 | (when (and (or (bolp)
1244 | (not (memq (char-syntax (preceding-char)) '(?w ?_))))
1245 | (looking-at "\\]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
1308 | (4 (7 . ?/))
1309 | (6 (7 . ?/)))
1310 | ("^\\(=\\)begin\\(\\s \\|$\\)" 1 (7 . nil))
1311 | ("^\\(=\\)end\\(\\s \\|$\\)" 1 (7 . nil))
1312 | (,(concat ruby-here-doc-beg-re ".*\\(\n\\)")
1313 | ,(+ 1 (regexp-opt-depth ruby-here-doc-beg-re))
1314 | (ruby-here-doc-beg-syntax))
1315 | (,ruby-here-doc-end-re 3 (ruby-here-doc-end-syntax))))
1316 |
1317 | (unless (functionp 'syntax-ppss)
1318 | (defun syntax-ppss (&optional pos)
1319 | (parse-partial-sexp (point-min) (or pos (point)))))
1320 |
1321 | (defun ruby-in-ppss-context-p (context &optional ppss)
1322 | (let ((ppss (or ppss (syntax-ppss (point)))))
1323 | (if (cond
1324 | ((eq context 'anything)
1325 | (or (nth 3 ppss)
1326 | (nth 4 ppss)))
1327 | ((eq context 'string)
1328 | (nth 3 ppss))
1329 | ((eq context 'heredoc)
1330 | (and (nth 3 ppss)
1331 | ;; If it's generic string, it's a heredoc and we don't care
1332 | ;; See `parse-partial-sexp'
1333 | (not (numberp (nth 3 ppss)))))
1334 | ((eq context 'non-heredoc)
1335 | (and (ruby-in-ppss-context-p 'anything)
1336 | (not (ruby-in-ppss-context-p 'heredoc))))
1337 | ((eq context 'comment)
1338 | (nth 4 ppss))
1339 | (t
1340 | (error (concat
1341 | "Internal error on `ruby-in-ppss-context-p': "
1342 | "context name `" (symbol-name context) "' is unknown"))))
1343 | t)))
1344 |
1345 | (defun ruby-in-here-doc-p ()
1346 | (save-excursion
1347 | (let ((old-point (point)) (case-fold-search nil))
1348 | (beginning-of-line)
1349 | (catch 'found-beg
1350 | (while (and (re-search-backward ruby-here-doc-beg-re nil t)
1351 | (not (ruby-singleton-class-p)))
1352 | (if (not (or (ruby-in-ppss-context-p 'anything)
1353 | (ruby-here-doc-find-end old-point)))
1354 | (throw 'found-beg t)))))))
1355 |
1356 | (defun ruby-here-doc-find-end (&optional limit)
1357 | "Expects the point to be on a line with one or more heredoc
1358 | openers. Returns the buffer position at which all heredocs on the
1359 | line are terminated, or nil if they aren't terminated before the
1360 | buffer position `limit' or the end of the buffer."
1361 | (save-excursion
1362 | (beginning-of-line)
1363 | (catch 'done
1364 | (let ((eol (save-excursion (end-of-line) (point)))
1365 | (case-fold-search nil)
1366 | ;; Fake match data such that (match-end 0) is at eol
1367 | (end-match-data (progn (looking-at ".*$") (match-data)))
1368 | beg-match-data end-re)
1369 | (while (re-search-forward ruby-here-doc-beg-re eol t)
1370 | (setq beg-match-data (match-data))
1371 | (setq end-re (ruby-here-doc-end-match))
1372 |
1373 | (set-match-data end-match-data)
1374 | (goto-char (match-end 0))
1375 | (unless (re-search-forward end-re limit t) (throw 'done nil))
1376 | (setq end-match-data (match-data))
1377 |
1378 | (set-match-data beg-match-data)
1379 | (goto-char (match-end 0)))
1380 | (set-match-data end-match-data)
1381 | (goto-char (match-end 0))
1382 | (point)))))
1383 |
1384 | (defun ruby-here-doc-beg-syntax ()
1385 | (save-excursion
1386 | (goto-char (match-beginning 0))
1387 | (unless (or (ruby-in-ppss-context-p 'non-heredoc)
1388 | (ruby-in-here-doc-p))
1389 | (string-to-syntax "|"))))
1390 |
1391 | (defun ruby-here-doc-end-syntax ()
1392 | (let ((pss (syntax-ppss)) (case-fold-search nil))
1393 | (when (ruby-in-ppss-context-p 'heredoc pss)
1394 | (save-excursion
1395 | (goto-char (nth 8 pss)) ; Go to the beginning of heredoc.
1396 | (let ((eol (point)))
1397 | (beginning-of-line)
1398 | (if (and (re-search-forward (ruby-here-doc-beg-match) eol t) ; If there is a heredoc that matches this line...
1399 | (not (ruby-in-ppss-context-p 'anything)) ; And that's not inside a heredoc/string/comment...
1400 | (progn (goto-char (match-end 0)) ; And it's the last heredoc on its line...
1401 | (not (re-search-forward ruby-here-doc-beg-re eol t))))
1402 | (string-to-syntax "|")))))))
1403 |
1404 | (eval-when-compile
1405 | (put 'ruby-mode 'font-lock-defaults
1406 | '((ruby-font-lock-keywords)
1407 | nil nil nil
1408 | beginning-of-line
1409 | (font-lock-syntactic-keywords
1410 | . ruby-font-lock-syntactic-keywords))))
1411 |
1412 | (defun ruby-font-lock-docs (limit)
1413 | (if (re-search-forward "^=begin\\(\\s \\|$\\)" limit t)
1414 | (let (beg)
1415 | (beginning-of-line)
1416 | (setq beg (point))
1417 | (forward-line 1)
1418 | (if (re-search-forward "^=end\\(\\s \\|$\\)" limit t)
1419 | (progn
1420 | (set-match-data (list beg (point)))
1421 | t)))))
1422 |
1423 | (defun ruby-font-lock-maybe-docs (limit)
1424 | (let (beg)
1425 | (save-excursion
1426 | (if (and (re-search-backward "^=\\(begin\\|end\\)\\(\\s \\|$\\)" nil t)
1427 | (string= (match-string 1) "begin"))
1428 | (progn
1429 | (beginning-of-line)
1430 | (setq beg (point)))))
1431 | (if (and beg (and (re-search-forward "^=\\(begin\\|end\\)\\(\\s \\|$\\)" nil t)
1432 | (string= (match-string 1) "end")))
1433 | (progn
1434 | (set-match-data (list beg (point)))
1435 | t)
1436 | nil)))
1437 |
1438 | (defvar ruby-font-lock-syntax-table
1439 | (let* ((tbl (copy-syntax-table ruby-mode-syntax-table)))
1440 | (modify-syntax-entry ?_ "w" tbl)
1441 | tbl))
1442 |
1443 | (defconst ruby-font-lock-keywords
1444 | (list
1445 | ;; functions
1446 | '("^\\s *def\\s +\\([^( \t\n]+\\)"
1447 | 1 font-lock-function-name-face)
1448 | ;; keywords
1449 | (cons (concat
1450 | "\\(^\\|[^_:.@$]\\|\\.\\.\\)\\_<\\(defined\\?\\|"
1451 | (regexp-opt
1452 | '("alias"
1453 | "and"
1454 | "begin"
1455 | "break"
1456 | "case"
1457 | "catch"
1458 | "class"
1459 | "def"
1460 | "do"
1461 | "elsif"
1462 | "else"
1463 | "fail"
1464 | "ensure"
1465 | "for"
1466 | "end"
1467 | "if"
1468 | "in"
1469 | "module"
1470 | "next"
1471 | "not"
1472 | "or"
1473 | "raise"
1474 | "redo"
1475 | "rescue"
1476 | "retry"
1477 | "return"
1478 | "then"
1479 | "throw"
1480 | "super"
1481 | "unless"
1482 | "undef"
1483 | "until"
1484 | "when"
1485 | "while"
1486 | "yield"
1487 | )
1488 | t)
1489 | "\\)"
1490 | ruby-keyword-end-re)
1491 | 2)
1492 | ;; here-doc beginnings
1493 | (list ruby-here-doc-beg-re 0 'font-lock-string-face)
1494 | ;; variables
1495 | '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\_<\\(nil\\|self\\|true\\|false\\)\\>"
1496 | 2 font-lock-variable-name-face)
1497 | ;; variables
1498 | '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1499 | 1 font-lock-variable-name-face)
1500 | '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
1501 | 0 font-lock-variable-name-face)
1502 | ;; embedded document
1503 | '(ruby-font-lock-docs
1504 | 0 font-lock-comment-face t)
1505 | '(ruby-font-lock-maybe-docs
1506 | 0 font-lock-comment-face t)
1507 | ;; general delimited string
1508 | '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
1509 | (2 font-lock-string-face))
1510 | ;; constants
1511 | '("\\(^\\|[^_]\\)\\_<\\([A-Z]+\\(\\w\\|_\\)*\\)"
1512 | 2 font-lock-type-face)
1513 | ;; symbols
1514 | '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
1515 | 2 font-lock-reference-face)
1516 | '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-reference-face)
1517 | ;; expression expansion
1518 | '("#\\({[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\|\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+\\)"
1519 | 0 font-lock-variable-name-face t)
1520 | ;; warn lower camel case
1521 | ;'("\\<[a-z]+[a-z0-9]*[A-Z][A-Za-z0-9]*\\([!?]?\\|\\>\\)"
1522 | ; 0 font-lock-warning-face)
1523 | )
1524 | "*Additional expressions to highlight in ruby mode."))
1525 |
1526 | (eval-when-hilit19-available
1527 | (hilit-set-mode-patterns
1528 | 'ruby-mode
1529 | '(("[^$\\?]\\(\"[^\\\"]*\\(\\\\\\(.\\|\n\\)[^\\\"]*\\)*\"\\)" 1 string)
1530 | ("[^$\\?]\\('[^\\']*\\(\\\\\\(.\\|\n\\)[^\\']*\\)*'\\)" 1 string)
1531 | ("[^$\\?]\\(`[^\\`]*\\(\\\\\\(.\\|\n\\)[^\\`]*\\)*`\\)" 1 string)
1532 | ("^\\s *#.*$" nil comment)
1533 | ("[^$@?\\]\\(#[^$@{\n].*$\\)" 1 comment)
1534 | ("[^a-zA-Z_]\\(\\?\\(\\\\[CM]-\\)*.\\)" 1 string)
1535 | ("^\\s *\\(require\\|load\\).*$" nil include)
1536 | ("^\\s *\\(include\\|alias\\|undef\\).*$" nil decl)
1537 | ("^\\s *\\<\\(class\\|def\\|module\\)\\>" "[)\n;]" defun)
1538 | ("[^_]\\<\\(begin\\|case\\|else\\|elsif\\|end\\|ensure\\|for\\|if\\|unless\\|rescue\\|then\\|when\\|while\\|until\\|do\\|yield\\)\\>\\([^_]\\|$\\)" 1 defun)
1539 | ("[^_]\\<\\(and\\|break\\|next\\|raise\\|fail\\|in\\|not\\|or\\|redo\\|retry\\|return\\|super\\|yield\\|catch\\|throw\\|self\\|nil\\)\\>\\([^_]\\|$\\)" 1 keyword)
1540 | ("\\$\\(.\\|\\sw+\\)" nil type)
1541 | ("[$@].[a-zA-Z_0-9]*" nil struct)
1542 | ("^__END__" nil label))))
1543 |
1544 |
1545 | ;;;###autoload
1546 | (defun ruby-mode ()
1547 | "Major mode for editing ruby scripts.
1548 | \\[ruby-indent-command] properly indents subexpressions of multi-line
1549 | class, module, def, if, while, for, do, and case statements, taking
1550 | nesting into account.
1551 |
1552 | The variable ruby-indent-level controls the amount of indentation.
1553 | \\{ruby-mode-map}"
1554 | (interactive)
1555 | (kill-all-local-variables)
1556 | (use-local-map ruby-mode-map)
1557 | (setq mode-name "Ruby")
1558 | (setq major-mode 'ruby-mode)
1559 | (ruby-mode-variables)
1560 |
1561 | (make-local-variable 'imenu-create-index-function)
1562 | (setq imenu-create-index-function 'ruby-imenu-create-index)
1563 |
1564 | (make-local-variable 'add-log-current-defun-function)
1565 | (setq add-log-current-defun-function 'ruby-add-log-current-method)
1566 |
1567 | (add-hook
1568 | (cond ((boundp 'before-save-hook)
1569 | (make-local-variable 'before-save-hook)
1570 | 'before-save-hook)
1571 | ((boundp 'write-contents-functions) 'write-contents-functions)
1572 | ((boundp 'write-contents-hooks) 'write-contents-hooks))
1573 | 'ruby-mode-set-encoding)
1574 |
1575 | (set (make-local-variable 'font-lock-defaults) '((ruby-font-lock-keywords) nil nil))
1576 | (set (make-local-variable 'font-lock-keywords) ruby-font-lock-keywords)
1577 | (set (make-local-variable 'font-lock-syntax-table) ruby-font-lock-syntax-table)
1578 | (set (make-local-variable 'font-lock-syntactic-keywords) ruby-font-lock-syntactic-keywords)
1579 |
1580 | (if (fboundp 'run-mode-hooks)
1581 | (run-mode-hooks 'ruby-mode-hook)
1582 | (run-hooks 'ruby-mode-hook)))
1583 |
1584 | (provide 'ruby-mode)
1585 |
--------------------------------------------------------------------------------
/rubydb2x.el:
--------------------------------------------------------------------------------
1 | (require 'gud)
2 | (provide 'rubydb)
3 |
4 | ;; ======================================================================
5 | ;; rubydb functions
6 |
7 | ;;; History of argument lists passed to rubydb.
8 | (defvar gud-rubydb-history nil)
9 |
10 | (defun gud-rubydb-massage-args (file args)
11 | (cons "-I" (cons "." (cons "-r" (cons "debug" (cons file args))))))
12 |
13 | ;; There's no guarantee that Emacs will hand the filter the entire
14 | ;; marker at once; it could be broken up across several strings. We
15 | ;; might even receive a big chunk with several markers in it. If we
16 | ;; receive a chunk of text which looks like it might contain the
17 | ;; beginning of a marker, we save it here between calls to the
18 | ;; filter.
19 | (defvar gud-rubydb-marker-acc "")
20 |
21 | (defun gud-rubydb-marker-filter (string)
22 | (save-match-data
23 | (setq gud-marker-acc (concat gud-marker-acc string))
24 | (let ((output ""))
25 |
26 | ;; Process all the complete markers in this chunk.
27 | (while (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
28 | gud-marker-acc)
29 | (setq
30 |
31 | ;; Extract the frame position from the marker.
32 | gud-last-frame
33 | (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
34 | (string-to-int (substring gud-marker-acc
35 | (match-beginning 2)
36 | (match-end 2))))
37 |
38 | ;; Append any text before the marker to the output we're going
39 | ;; to return - we don't include the marker in this text.
40 | output (concat output
41 | (substring gud-marker-acc 0 (match-beginning 0)))
42 |
43 | ;; Set the accumulator to the remaining text.
44 | gud-marker-acc (substring gud-marker-acc (match-end 0))))
45 |
46 | ;; Does the remaining text look like it might end with the
47 | ;; beginning of another marker? If it does, then keep it in
48 | ;; gud-marker-acc until we receive the rest of it. Since we
49 | ;; know the full marker regexp above failed, it's pretty simple to
50 | ;; test for marker starts.
51 | (if (string-match "\032.*\\'" gud-marker-acc)
52 | (progn
53 | ;; Everything before the potential marker start can be output.
54 | (setq output (concat output (substring gud-marker-acc
55 | 0 (match-beginning 0))))
56 |
57 | ;; Everything after, we save, to combine with later input.
58 | (setq gud-marker-acc
59 | (substring gud-marker-acc (match-beginning 0))))
60 |
61 | (setq output (concat output gud-marker-acc)
62 | gud-marker-acc ""))
63 |
64 | output)))
65 |
66 | (defun gud-rubydb-find-file (f)
67 | (find-file-noselect f))
68 |
69 | (defvar rubydb-command-name "ruby"
70 | "File name for executing ruby.")
71 |
72 | ;;;###autoload
73 | (defun rubydb (command-line)
74 | "Run rubydb on program FILE in buffer *gud-FILE*.
75 | The directory containing FILE becomes the initial working directory
76 | and source-file directory for your debugger."
77 | (interactive
78 | (list (read-from-minibuffer "Run rubydb (like this): "
79 | (if (consp gud-rubydb-history)
80 | (car gud-rubydb-history)
81 | (concat rubydb-command-name " "))
82 | nil nil
83 | '(gud-rubydb-history . 1))))
84 |
85 | (gud-overload-functions '((gud-massage-args . gud-rubydb-massage-args)
86 | (gud-marker-filter . gud-rubydb-marker-filter)
87 | (gud-find-file . gud-rubydb-find-file)
88 | ))
89 | (gud-common-init command-line)
90 |
91 | (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
92 | ; (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
93 | (gud-def gud-step "s" "\C-s" "Step one source line with display.")
94 | (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
95 | (gud-def gud-cont "c" "\C-r" "Continue with display.")
96 | (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
97 | (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
98 | (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
99 | (gud-def gud-print "p %e" "\C-p" "Evaluate ruby expression at point.")
100 |
101 | (setq comint-prompt-regexp "^(rdb:-) ")
102 | (setq paragraph-start comint-prompt-regexp)
103 | (run-hooks 'rubydb-mode-hook)
104 | )
105 |
--------------------------------------------------------------------------------
/rubydb3x.el:
--------------------------------------------------------------------------------
1 | (require 'gud)
2 | (provide 'rubydb)
3 |
4 | ;; ======================================================================
5 | ;; rubydb functions
6 |
7 | ;;; History of argument lists passed to rubydb.
8 | (defvar gud-rubydb-history nil)
9 |
10 | (if (fboundp 'gud-overload-functions)
11 | (defun gud-rubydb-massage-args (file args)
12 | (cons "-r" (cons "debug" (cons file args))))
13 | (defun gud-rubydb-massage-args (file args)
14 | (cons "-r" (cons "debug" args))))
15 |
16 | ;; There's no guarantee that Emacs will hand the filter the entire
17 | ;; marker at once; it could be broken up across several strings. We
18 | ;; might even receive a big chunk with several markers in it. If we
19 | ;; receive a chunk of text which looks like it might contain the
20 | ;; beginning of a marker, we save it here between calls to the
21 | ;; filter.
22 | (defvar gud-rubydb-marker-acc "")
23 | (make-variable-buffer-local 'gud-rubydb-marker-acc)
24 |
25 | (defun gud-rubydb-marker-filter (string)
26 | (setq gud-rubydb-marker-acc (concat gud-rubydb-marker-acc string))
27 | (let ((output ""))
28 |
29 | ;; Process all the complete markers in this chunk.
30 | (while (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
31 | gud-rubydb-marker-acc)
32 | (setq
33 |
34 | ;; Extract the frame position from the marker.
35 | gud-last-frame
36 | (cons (substring gud-rubydb-marker-acc (match-beginning 1) (match-end 1))
37 | (string-to-number (substring gud-rubydb-marker-acc
38 | (match-beginning 2)
39 | (match-end 2))))
40 |
41 | ;; Append any text before the marker to the output we're going
42 | ;; to return - we don't include the marker in this text.
43 | output (concat output
44 | (substring gud-rubydb-marker-acc 0 (match-beginning 0)))
45 |
46 | ;; Set the accumulator to the remaining text.
47 | gud-rubydb-marker-acc (substring gud-rubydb-marker-acc (match-end 0))))
48 |
49 | ;; Does the remaining text look like it might end with the
50 | ;; beginning of another marker? If it does, then keep it in
51 | ;; gud-rubydb-marker-acc until we receive the rest of it. Since we
52 | ;; know the full marker regexp above failed, it's pretty simple to
53 | ;; test for marker starts.
54 | (if (string-match "\032.*\\'" gud-rubydb-marker-acc)
55 | (progn
56 | ;; Everything before the potential marker start can be output.
57 | (setq output (concat output (substring gud-rubydb-marker-acc
58 | 0 (match-beginning 0))))
59 |
60 | ;; Everything after, we save, to combine with later input.
61 | (setq gud-rubydb-marker-acc
62 | (substring gud-rubydb-marker-acc (match-beginning 0))))
63 |
64 | (setq output (concat output gud-rubydb-marker-acc)
65 | gud-rubydb-marker-acc ""))
66 |
67 | output))
68 |
69 | (defun gud-rubydb-find-file (f)
70 | (save-excursion
71 | (let ((buf (find-file-noselect f)))
72 | (set-buffer buf)
73 | ;; (gud-make-debug-menu)
74 | buf)))
75 |
76 | (defvar rubydb-command-name "ruby"
77 | "File name for executing ruby.")
78 |
79 | ;;;###autoload
80 | (defun rubydb (command-line)
81 | "Run rubydb on program FILE in buffer *gud-FILE*.
82 | The directory containing FILE becomes the initial working directory
83 | and source-file directory for your debugger."
84 | (interactive
85 | (list (read-from-minibuffer "Run rubydb (like this): "
86 | (if (consp gud-rubydb-history)
87 | (car gud-rubydb-history)
88 | (concat rubydb-command-name " "))
89 | nil nil
90 | '(gud-rubydb-history . 1))))
91 |
92 | (if (not (fboundp 'gud-overload-functions))
93 | (gud-common-init command-line 'gud-rubydb-massage-args
94 | 'gud-rubydb-marker-filter 'gud-rubydb-find-file)
95 | (gud-overload-functions '((gud-massage-args . gud-rubydb-massage-args)
96 | (gud-marker-filter . gud-rubydb-marker-filter)
97 | (gud-find-file . gud-rubydb-find-file)))
98 | (gud-common-init command-line rubydb-command-name))
99 |
100 | (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
101 | ; (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
102 | (gud-def gud-step "s" "\C-s" "Step one source line with display.")
103 | (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
104 | (gud-def gud-cont "c" "\C-r" "Continue with display.")
105 | (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
106 | (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
107 | (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
108 | (gud-def gud-print "p %e" "\C-p" "Evaluate ruby expression at point.")
109 |
110 | (setq comint-prompt-regexp "^(rdb:-) ")
111 | (if (boundp 'comint-last-output-start)
112 | (set-marker comint-last-output-start (point)))
113 | (set (make-local-variable 'paragraph-start) comint-prompt-regexp)
114 | (run-hooks 'rubydb-mode-hook)
115 | )
116 |
--------------------------------------------------------------------------------