Skip to content
Newer
Older
100644 198 lines (147 sloc) 7.77 KB
b8d34c3 @magnars Initial commit
authored
1 ;;; expand-region.el --- Increase selected region by semantic units.
2
3 ;; Copyright (C) 2011 Magnar Sveen
4
5 ;; Author: Magnar Sveen <magnars@gmail.com>
6 ;; Keywords: marking region
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
9b73c7c @magnars Update readme.
authored
23 ;; Expand region increases the selected region by semantic units. Just keep
24 ;; pressing the key until it selects what you want.
25
26 ;; An example:
27
28 ;; (setq alphabet-start "abc def")
29
30 ;; With the cursor at the `c`, it starts by marking the entire word `abc`, then
31 ;; expand to the contents of the quotes `abc def`, then to the entire quote
32 ;; `"abc def"`, then to the contents of the sexp `setq alphabet-start "abc def"`
b8d34c3 @magnars Initial commit
authored
33 ;; and finally to the entire sexp.
9b73c7c @magnars Update readme.
authored
34
b8d34c3 @magnars Initial commit
authored
35 ;; You can set it up like this:
9b73c7c @magnars Update readme.
authored
36
b8d34c3 @magnars Initial commit
authored
37 ;; (require 'expand-region)
f4a08cc @magnars Change suggested keybinding to C-=
authored
38 ;; (global-set-key (kbd "C-=") 'er/expand-region)
b8d34c3 @magnars Initial commit
authored
39
a08c85f @magnars Update readme with video.
authored
40 ;; There's also `er/contract-region` if you expand too far.
41
42 ;; ## Video
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
43
a08c85f @magnars Update readme with video.
authored
44 ;; You can [watch an intro to expand-region at Emacs Rocks](http://emacsrocks.com/e09.html).
45
9b73c7c @magnars Update readme.
authored
46 ;; ## Language support
47
b8d34c3 @magnars Initial commit
authored
48 ;; Expand region works fairly well with most languages, due to the general
49 ;; nature of the basic expansions:
9b73c7c @magnars Update readme.
authored
50
b8d34c3 @magnars Initial commit
authored
51 ;; er/mark-word
52 ;; er/mark-symbol
53 ;; er/mark-method-call
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
54 ;; er/mark-comment
55 ;; er/mark-comment-block
b8d34c3 @magnars Initial commit
authored
56 ;; er/mark-inside-quotes
57 ;; er/mark-outside-quotes
58 ;; er/mark-inside-pairs
59 ;; er/mark-outside-pairs
9b73c7c @magnars Update readme.
authored
60
b8d34c3 @magnars Initial commit
authored
61 ;; However, most languages also will benefit from some specially crafted
62 ;; expansions. For instance, expand-region comes with these extra expansions for
63 ;; html-mode:
9b73c7c @magnars Update readme.
authored
64
b8d34c3 @magnars Initial commit
authored
65 ;; er/mark-html-attribute
66 ;; er/mark-inner-tag
67 ;; er/mark-outer-tag
9b73c7c @magnars Update readme.
authored
68
b8d34c3 @magnars Initial commit
authored
69 ;; You can add your own expansions to the languages of your choice simply by
70 ;; creating a function that looks around point to see if it's inside or looking
71 ;; at the construct you want to mark, and if so - mark it.
9b73c7c @magnars Update readme.
authored
72
b8d34c3 @magnars Initial commit
authored
73 ;; There's plenty of examples to look at in these files.
9b73c7c @magnars Update readme.
authored
74
b8d34c3 @magnars Initial commit
authored
75 ;; After you make your function, add it to a buffer-local version of
9b73c7c @magnars Update readme.
authored
76 ;; the `er/try-expand-list`.
77
78 ;; **Example:**
79
b8d34c3 @magnars Initial commit
authored
80 ;; Let's say you want expand-region to also mark paragraphs and pages in
9b73c7c @magnars Update readme.
authored
81 ;; text-mode. Incidentally Emacs already comes with `mark-paragraph` and
82 ;; `mark-page`. To add it to the try-list, do this:
83
b8d34c3 @magnars Initial commit
authored
84 ;; (defun er/add-text-mode-expansions ()
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
85 ;; (make-variable-buffer-local 'er/try-expand-list)
86 ;; (setq er/try-expand-list (append
87 ;; er/try-expand-list
88 ;; '(mark-paragraph
89 ;; mark-page))))
90
b8d34c3 @magnars Initial commit
authored
91 ;; (add-hook 'text-mode-hook 'er/add-text-mode-expansions)
9b73c7c @magnars Update readme.
authored
92
93 ;; Add that to its own file, and require it at the bottom of this one,
b8d34c3 @magnars Initial commit
authored
94 ;; where it says "Mode-specific expansions"
95
9b73c7c @magnars Update readme.
authored
96 ;; **Warning:** Badly written expansions might slow down expand-region
b8d34c3 @magnars Initial commit
authored
97 ;; dramatically. Remember to exit quickly before you start traversing
98 ;; the entire document looking for constructs to mark.
99
fbda159 @magnars Updated contributors.
authored
100 ;; ## Contribute
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
101
b8d34c3 @magnars Initial commit
authored
102 ;; If you make some nice expansions for your favorite mode, it would be
103 ;; great if you opened a pull-request. The repo is at:
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
104
9b73c7c @magnars Update readme.
authored
105 ;; https://github.com/magnars/expand-region.el
b8d34c3 @magnars Initial commit
authored
106
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
107 ;; Changes to `expand-region-core` itself must be accompanied by feature tests.
108 ;; They are written in [Ecukes](http://ecukes.info), a Cucumber for Emacs.
109
110 ;; To fetch the test dependencies:
111
112 ;; $ cd /path/to/expand-region
113 ;; $ git submodule init
114 ;; $ git submodule update
115
116 ;; Run the tests with:
117
118 ;; $ ./util/ecukes/ecukes features
119
120 ;; If you want to add feature-tests for your mode-specific expansions as well,
121 ;; that is utterly excellent.
122
fbda159 @magnars Updated contributors.
authored
123 ;; ## Contributors
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
124
fbda159 @magnars Updated contributors.
authored
125 ;; * [Josh Johnston](https://github.com/joshwnj) contributed `er/contract-region`
126 ;; * [Le Wang](https://github.com/lewang) contributed consistent handling of the mark ring, expanding into pairs/quotes just left of the cursor, and general code clean-up.
08206a3 @magnars Updated contributors in expand-region.el commentary.
authored
127 ;; * [Matt Briggs](https://github.com/mbriggs) contributed expansions for ruby-mode.
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
128 ;; * [Ivan Andrus](https://github.com/gvol) contributed expansions for python-mode, text-mode, LaTeX-mode and nxml-mode.
129 ;; * [Raimon Grau](https://github.com/kidd) added support for when transient-mark-mode is off.
130 ;; * [Gleb Peregud](https://github.com/gleber) contributed expansions for erlang-mode.
2e59b05 @magnars Added Damien Cassou to list of contributors.
authored
131 ;; * [fgeller](https://github.com/fgeller) and [edmccard](https://github.com/edmccard) contributed better support for python and its multiple modes.
132 ;; * [François Févotte](https://github.com/ffevotte) contributed expansions for C and C++.
133 ;; * [Roland Walker](https://github.com/rolandwalker) added option to copy the contents of the most recent action to a register, and some fixes.
134 ;; * [Damien Cassou](https://github.com/DamienCassou) added option to continue expanding/contracting with fast keys after initial expand.
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
135
fbda159 @magnars Updated contributors.
authored
136 ;; Thanks!
137
b8d34c3 @magnars Initial commit
authored
138 ;;; Code:
139
1be3769 @magnars Loosen dependency between core and mode expansions.
authored
140 (require 'expand-region-core)
572b560 @fgeller Splitting customization variables into a separate file.
fgeller authored
141 (require 'expand-region-custom)
146a378 @fgeller Add customization variable to select a preferred python mode.
fgeller authored
142
69a2a4d @magnars Move interactive shell of expand-region out into expand-region.el to …
authored
143 ;;;###autoload
144 (defun er/expand-region (arg)
145 "Increase selected region by semantic units.
146
147 With prefix argument expands the region that many times.
148 If prefix argument is negative calls `er/contract-region'.
149 If prefix argument is 0 it resets point and mark to their state
150 before calling `er/expand-region' for the first time."
151 (interactive "p")
152 (if (< arg 1)
153 (er/contract-region (- arg))
154 (er--prepare-expanding)
155 (while (>= arg 1)
156 (setq arg (- arg 1))
3584409 @magnars Add customization expand-region-fast-keys-enabled
authored
157 (er--expand-region-1))
158 (when expand-region-fast-keys-enabled
159 (er/prepare-for-more-expansions))))
69a2a4d @magnars Move interactive shell of expand-region out into expand-region.el to …
authored
160
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
authored
161 (eval-after-load "clojure-mode" '(require 'clojure-mode-expansions))
162 (eval-after-load "css-mode" '(require 'css-mode-expansions))
163 (eval-after-load "erlang-mode" '(require 'erlang-mode-expansions))
164 (eval-after-load "feature-mode" '(require 'feature-mode-expansions))
8fc42c8 @magnars html-mode is loaded as part of sgml-mode.el
authored
165 (eval-after-load "sgml-mode" '(require 'html-mode-expansions)) ;; html-mode is defined in sgml-mode.el
166 (eval-after-load "rhtml-mode" '(require 'html-mode-expansions))
167 (eval-after-load "nxhtml-mode" '(require 'html-mode-expansions))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
authored
168 (eval-after-load "js2-mode" '(require 'js-mode-expansions))
e234ffa @magnars js2-mode specific expansions.
authored
169 (eval-after-load "js2-mode" '(require 'js2-mode-expansions))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
authored
170 (eval-after-load "js3-mode" '(require 'js-mode-expansions))
3a04a17 @thisirs expand-region.el: Fix LaTeX's eval-after-load
thisirs authored
171 (eval-after-load "latex" '(require 'latex-mode-expansions))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
authored
172 (eval-after-load "nxml-mode" '(require 'nxml-mode-expansions))
146a378 @fgeller Add customization variable to select a preferred python mode.
fgeller authored
173 (eval-after-load "python" '(progn
174 (when expand-region-guess-python-mode
175 (expand-region-guess-python-mode))
176 (if (eq 'python expand-region-preferred-python-mode)
177 (require 'python-el-expansions)
178 (require 'python-el-fgallina-expansions))))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
authored
179 (eval-after-load "python-mode" '(require 'python-mode-expansions))
180 (eval-after-load "ruby-mode" '(require 'ruby-mode-expansions))
28b41b4 @hron Replace "org-mode" with "org" in eval-after-load.
hron authored
181 (eval-after-load "org" '(require 'org-mode-expansions))
2e8aaa1 @ffevotte Renamed c-mode-expansions to cc-mode-expansions
ffevotte authored
182 (eval-after-load "cc-mode" '(require 'cc-mode-expansions))
588a0fc @magnars Turn off text-mode-expansions by default.
authored
183
d157d7f @magnars Support built-in javascript-mode
authored
184 (defadvice javascript-mode (after enable-expand-region activate)
185 (require 'js-mode-expansions)
186 (er/add-js-mode-expansions))
187
588a0fc @magnars Turn off text-mode-expansions by default.
authored
188 ;; unfortunately html-mode inherits from text-mode
189 ;; and text-mode-expansions don't work well in html-mode
190 ;; so if you want text-mode-expansions, add this to your
191 ;; own init:
192 ;;
193 ;; (eval-after-load "text-mode" '(require 'text-mode-expansions))
b8d34c3 @magnars Initial commit
authored
194
195 (provide 'expand-region)
196
f44de5a use `make-local-variable' instead of `make-variable-buffer-local'
Le Wang authored
197 ;;; expand-region.el ends here
Something went wrong with that request. Please try again.