Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 171 lines (123 sloc) 6.653 kb
b8d34c3 @magnars Initial commit
magnars 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.
magnars 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
magnars authored
33 ;; and finally to the entire sexp.
9b73c7c @magnars Update readme.
magnars authored
34
b8d34c3 @magnars Initial commit
magnars authored
35 ;; You can set it up like this:
9b73c7c @magnars Update readme.
magnars authored
36
b8d34c3 @magnars Initial commit
magnars authored
37 ;; (require 'expand-region)
f4a08cc @magnars Change suggested keybinding to C-=
magnars authored
38 ;; (global-set-key (kbd "C-=") 'er/expand-region)
b8d34c3 @magnars Initial commit
magnars authored
39
a08c85f @magnars Update readme with video.
magnars 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.
magnars authored
43
a08c85f @magnars Update readme with video.
magnars authored
44 ;; You can [watch an intro to expand-region at Emacs Rocks](http://emacsrocks.com/e09.html).
45
9b73c7c @magnars Update readme.
magnars authored
46 ;; ## Language support
47
b8d34c3 @magnars Initial commit
magnars authored
48 ;; Expand region works fairly well with most languages, due to the general
49 ;; nature of the basic expansions:
9b73c7c @magnars Update readme.
magnars authored
50
b8d34c3 @magnars Initial commit
magnars authored
51 ;; er/mark-word
52 ;; er/mark-symbol
53 ;; er/mark-method-call
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars authored
54 ;; er/mark-comment
55 ;; er/mark-comment-block
b8d34c3 @magnars Initial commit
magnars 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.
magnars authored
60
b8d34c3 @magnars Initial commit
magnars 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.
magnars authored
64
b8d34c3 @magnars Initial commit
magnars authored
65 ;; er/mark-html-attribute
66 ;; er/mark-inner-tag
67 ;; er/mark-outer-tag
9b73c7c @magnars Update readme.
magnars authored
68
b8d34c3 @magnars Initial commit
magnars 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.
magnars authored
72
b8d34c3 @magnars Initial commit
magnars authored
73 ;; There's plenty of examples to look at in these files.
9b73c7c @magnars Update readme.
magnars authored
74
b8d34c3 @magnars Initial commit
magnars authored
75 ;; After you make your function, add it to a buffer-local version of
9b73c7c @magnars Update readme.
magnars authored
76 ;; the `er/try-expand-list`.
77
78 ;; **Example:**
79
b8d34c3 @magnars Initial commit
magnars authored
80 ;; Let's say you want expand-region to also mark paragraphs and pages in
9b73c7c @magnars Update readme.
magnars 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
magnars authored
84 ;; (defun er/add-text-mode-expansions ()
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars 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
magnars authored
91 ;; (add-hook 'text-mode-hook 'er/add-text-mode-expansions)
9b73c7c @magnars Update readme.
magnars authored
92
93 ;; Add that to its own file, and require it at the bottom of this one,
b8d34c3 @magnars Initial commit
magnars authored
94 ;; where it says "Mode-specific expansions"
95
9b73c7c @magnars Update readme.
magnars authored
96 ;; **Warning:** Badly written expansions might slow down expand-region
b8d34c3 @magnars Initial commit
magnars 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.
magnars authored
100 ;; ## Contribute
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars authored
101
b8d34c3 @magnars Initial commit
magnars 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.
magnars authored
104
9b73c7c @magnars Update readme.
magnars authored
105 ;; https://github.com/magnars/expand-region.el
b8d34c3 @magnars Initial commit
magnars authored
106
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars 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.
magnars authored
123 ;; ## Contributors
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars authored
124
fbda159 @magnars Updated contributors.
magnars 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.
magnars authored
127 ;; * [Matt Briggs](https://github.com/mbriggs) contributed expansions for ruby-mode.
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars 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.
131
fbda159 @magnars Updated contributors.
magnars authored
132 ;; Thanks!
133
b8d34c3 @magnars Initial commit
magnars authored
134 ;;; Code:
135
1be3769 @magnars Loosen dependency between core and mode expansions.
magnars authored
136 (require 'expand-region-core)
572b560 @fgeller Splitting customization variables into a separate file.
fgeller authored
137 (require 'expand-region-custom)
146a378 @fgeller Add customization variable to select a preferred python mode.
fgeller authored
138
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
magnars authored
139 (eval-after-load "clojure-mode" '(require 'clojure-mode-expansions))
140 (eval-after-load "css-mode" '(require 'css-mode-expansions))
141 (eval-after-load "erlang-mode" '(require 'erlang-mode-expansions))
142 (eval-after-load "feature-mode" '(require 'feature-mode-expansions))
8fc42c8 @magnars html-mode is loaded as part of sgml-mode.el
magnars authored
143 (eval-after-load "sgml-mode" '(require 'html-mode-expansions)) ;; html-mode is defined in sgml-mode.el
144 (eval-after-load "rhtml-mode" '(require 'html-mode-expansions))
145 (eval-after-load "nxhtml-mode" '(require 'html-mode-expansions))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
magnars authored
146 (eval-after-load "js2-mode" '(require 'js-mode-expansions))
e234ffa @magnars js2-mode specific expansions.
magnars authored
147 (eval-after-load "js2-mode" '(require 'js2-mode-expansions))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
magnars authored
148 (eval-after-load "js3-mode" '(require 'js-mode-expansions))
149 (eval-after-load "LaTeX-mode" '(require 'latex-mode-expansions))
150 (eval-after-load "nxml-mode" '(require 'nxml-mode-expansions))
146a378 @fgeller Add customization variable to select a preferred python mode.
fgeller authored
151 (eval-after-load "python" '(progn
152 (when expand-region-guess-python-mode
153 (expand-region-guess-python-mode))
154 (if (eq 'python expand-region-preferred-python-mode)
155 (require 'python-el-expansions)
156 (require 'python-el-fgallina-expansions))))
555e5b6 @magnars Use eval-after-loads to avoid loading unused code.
magnars authored
157 (eval-after-load "python-mode" '(require 'python-mode-expansions))
158 (eval-after-load "ruby-mode" '(require 'ruby-mode-expansions))
28b41b4 @hron Replace "org-mode" with "org" in eval-after-load.
hron authored
159 (eval-after-load "org" '(require 'org-mode-expansions))
588a0fc @magnars Turn off text-mode-expansions by default.
magnars authored
160
161 ;; unfortunately html-mode inherits from text-mode
162 ;; and text-mode-expansions don't work well in html-mode
163 ;; so if you want text-mode-expansions, add this to your
164 ;; own init:
165 ;;
166 ;; (eval-after-load "text-mode" '(require 'text-mode-expansions))
b8d34c3 @magnars Initial commit
magnars authored
167
168 (provide 'expand-region)
169
f44de5a use `make-local-variable' instead of `make-variable-buffer-local'
Le Wang authored
170 ;;; expand-region.el ends here
Something went wrong with that request. Please try again.