Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (98 sloc) 3.691 kb
7f4dc40 Matt Briggs missed a js in the readme >.>
mbriggs authored
1 ;;; ruby-mode-expansions.el --- ruby-specific expansions for expand-region
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
2
3 ;; Copyright (C) 2011 Magnar Sveen
4
5 ;; Author: Matt Briggs
6 ;; Based on js-mode-expansions by: Magnar Sveen <magnars@gmail.com>
7 ;; Keywords: marking region
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24
25 ;; Idiomatic ruby has a lot of nested blocks, and its function marking seems a bit buggy.
26 ;;
27 ;; Expansions:
28 ;;
29 ;;
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
30 ;; er/mark-ruby-block
31 ;; er/mark-ruby-function
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
32
33 ;;; Code:
1be3769 Magnar Sveen Loosen dependency between core and mode expansions.
magnars authored
34
35 (require 'expand-region-core)
36
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
37 (defun er/mark-ruby-block ()
2756147 Fix previous botched commit for PR.
Le Wang authored
38 (interactive)
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
39 (forward-line 1)
40 (beginning-of-line)
41 (ruby-beginning-of-block)
42 (set-mark (point))
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
43 (ruby-end-of-block)
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
44 (end-of-line)
45 (exchange-point-and-mark))
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
46
ca212f6 Magnar Sveen Add support for expanding to ruby symbols, eg. :symbol
magnars authored
47 (defun er/mark-ruby-symbol ()
48 "Mark the entire symbol around or in front of point."
49 (interactive)
50 (let ((symbol-regexp ":\\|\\s_\\|\\sw"))
51 (when (or (looking-at symbol-regexp)
52 (looking-back symbol-regexp))
53 (while (looking-at symbol-regexp)
54 (forward-char))
55 (set-mark (point))
56 (while (looking-back symbol-regexp)
57 (backward-char)))))
58
5e4bfda James Edward Gray II Adding a crude start at choose-your-own-quotes style matching. It curre...
authored
59 (defun er/search-forward-for-closed-nesting (open close)
60 "A helper function for matching nested constructs."
61 (while (and (not (looking-at (regexp-quote close)))
62 (< (point) (point-max)))
63 (cond ((looking-at (regexp-quote open))
64 (forward-char)
65 (er/search-forward-for-closed-nesting open close))
66 (t (forward-char))))
67 (if (looking-at (regexp-quote close)) (forward-char)))
68
69 (defun er/mark-ruby-choose-your-own-quotes ()
70 "Mark choose-your-own-quotes style string, arrays, regexen, etc."
71 (interactive)
72 (let ((quote_start)
73 (quote_end))
74 (save-excursion
75 (while (and (not (looking-back "%[qQwWr]{"))
76 (> (point) (point-min)))
77 (backward-char))
78 (when (looking-back "%[qQwWr]{")
79 (setq quote_start (- (point) 3))
80 (er/search-forward-for-closed-nesting "{" "}")
81 (when (looking-back "}")
82 (setq quote_end (point)))))
83 (when (and quote_start
84 quote_end
85 (<= quote_start (point))
86 (>= quote_end (point)))
87 (set-mark quote_end)
88 (goto-char quote_start))))
89
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
90 (defun er/mark-ruby-function ()
91 "Mark the current Ruby function."
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
92 (interactive)
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
93 (condition-case nil
94 (forward-char 3)
95 (error nil))
96 (let ((ruby-method-regex "^[\t ]*def\\_>"))
97 (word-search-backward ruby-method-regex)
98 (while (syntax-ppss-context (syntax-ppss))
99 (word-search-backward ruby-method-regex)))
100 (set-mark (point))
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
101 (ruby-end-of-block)
37a060c Magnar Sveen Revert botched ruby-mode updates (pull request #36 and #37)
magnars authored
102 (end-of-line)
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
103 (exchange-point-and-mark))
104
105 (defun er/add-ruby-mode-expansions ()
106 "Adds Ruby-specific expansions for buffers in ruby-mode"
5e4bfda James Edward Gray II Adding a crude start at choose-your-own-quotes style matching. It curre...
authored
107 (set (make-local-variable 'er/try-expand-list)
108 (append er/try-expand-list
109 '(er/mark-ruby-symbol
110 er/mark-ruby-choose-your-own-quotes
111 er/mark-ruby-block
112 er/mark-ruby-function))))
dd95c21 Matt Briggs adds ruby-mode expansions
mbriggs authored
113
114 (add-hook 'ruby-mode-hook 'er/add-ruby-mode-expansions)
115
116 (provide 'ruby-mode-expansions)
Something went wrong with that request. Please try again.