Skip to content
This repository was archived by the owner on Jul 24, 2019. It is now read-only.

Commit f08224c

Browse files
committed
added expand-region
1 parent d993f04 commit f08224c

31 files changed

+2767
-0
lines changed

expand-region/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.elc
2+
.rvmrc
3+
/TAGS

expand-region/.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "util/ecukes"]
2+
path = util/ecukes
3+
url = git://github.com/rejeep/ecukes.git
4+
[submodule "util/espuds"]
5+
path = util/espuds
6+
url = git://github.com/rejeep/espuds.git

expand-region/README.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# expand-region.el
2+
3+
Expand region increases the selected region by semantic units. Just keep
4+
pressing the key until it selects what you want.
5+
6+
An example:
7+
8+
(setq alphabet-start "abc def")
9+
10+
With the cursor at the `c`, it starts by marking the entire word `abc`, then
11+
expand to the contents of the quotes `abc def`, then to the entire quote
12+
`"abc def"`, then to the contents of the sexp `setq alphabet-start "abc def"`
13+
and finally to the entire sexp.
14+
15+
You can set it up like this:
16+
17+
(require 'expand-region)
18+
(global-set-key (kbd "C-=") 'er/expand-region)
19+
20+
You can contract the region again with a negative prefix, if you expand too far.
21+
22+
## Video
23+
24+
You can [watch an intro to expand-region at Emacs Rocks](http://emacsrocks.com/e09.html).
25+
26+
## Language support
27+
28+
Expand region works fairly well with most languages, due to the general
29+
nature of the basic expansions:
30+
31+
er/mark-word
32+
er/mark-symbol
33+
er/mark-method-call
34+
er/mark-comment
35+
er/mark-comment-block
36+
er/mark-inside-quotes
37+
er/mark-outside-quotes
38+
er/mark-inside-pairs
39+
er/mark-outside-pairs
40+
41+
However, most languages also will benefit from some specially crafted
42+
expansions. For instance, expand-region comes with these extra expansions for
43+
html-mode:
44+
45+
er/mark-html-attribute
46+
er/mark-inner-tag
47+
er/mark-outer-tag
48+
49+
You can add your own expansions to the languages of your choice simply by
50+
creating a function that looks around point to see if it's inside or looking
51+
at the construct you want to mark, and if so - mark it.
52+
53+
There's plenty of examples to look at in these files.
54+
55+
After you make your function, add it to a buffer-local version of
56+
the `er/try-expand-list`.
57+
58+
**Example:**
59+
60+
Let's say you want expand-region to also mark paragraphs and pages in
61+
text-mode. Incidentally Emacs already comes with `mark-paragraph` and
62+
`mark-page`. To add it to the try-list, do this:
63+
64+
(defun er/add-text-mode-expansions ()
65+
(make-variable-buffer-local 'er/try-expand-list)
66+
(setq er/try-expand-list (append
67+
er/try-expand-list
68+
'(mark-paragraph
69+
mark-page))))
70+
71+
(add-hook 'text-mode-hook 'er/add-text-mode-expansions)
72+
73+
Add that to its own file, and add it to the `expand-region.el`-file,
74+
where it says "Mode-specific expansions"
75+
76+
**Warning:** Badly written expansions might slow down expand-region
77+
dramatically. Remember to exit quickly before you start traversing
78+
the entire document looking for constructs to mark.
79+
80+
## Contribute
81+
82+
If you make some nice expansions for your favorite mode, it would be
83+
great if you opened a pull-request. The repo is at:
84+
85+
https://github.com/magnars/expand-region.el
86+
87+
All changes must be accompanied by feature tests.
88+
They are written in [Ecukes](http://ecukes.info), a Cucumber for Emacs.
89+
90+
To fetch the test dependencies:
91+
92+
$ cd /path/to/expand-region
93+
$ git submodule init
94+
$ git submodule update
95+
96+
Run the tests with:
97+
98+
$ ./util/ecukes/ecukes features
99+
100+
If feature tests are missing for the mode you are changing, please make
101+
sure to add a set of basic tests around the functionality you're changing.
102+
103+
## Contributors
104+
105+
* [Josh Johnston](https://github.com/joshwnj) contributed `er/contract-region`
106+
* [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.
107+
* [Matt Briggs](https://github.com/mbriggs) contributed expansions for ruby-mode.
108+
* [Ivan Andrus](https://github.com/gvol) contributed expansions for python-mode, text-mode, LaTeX-mode and nxml-mode.
109+
* [Raimon Grau](https://github.com/kidd) added support for when transient-mark-mode is off.
110+
* [Gleb Peregud](https://github.com/gleber) contributed expansions for erlang-mode.
111+
* [fgeller](https://github.com/fgeller) and [edmccard](https://github.com/edmccard) contributed better support for python and its multiple modes.
112+
113+
Thanks!
114+
115+
## License
116+
117+
Copyright (C) 2011 Magnar Sveen
118+
119+
Author: Magnar Sveen <[email protected]>
120+
Keywords: marking region
121+
122+
This program is free software; you can redistribute it and/or modify
123+
it under the terms of the GNU General Public License as published by
124+
the Free Software Foundation, either version 3 of the License, or
125+
(at your option) any later version.
126+
127+
This program is distributed in the hope that it will be useful,
128+
but WITHOUT ANY WARRANTY; without even the implied warranty of
129+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130+
GNU General Public License for more details.
131+
132+
You should have received a copy of the GNU General Public License
133+
along with this program. If not, see <http://www.gnu.org/licenses/>.

expand-region/autotest.watchr

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
ENV["WATCHR"] = "1"
2+
system 'clear'
3+
4+
def run(cmd)
5+
`#{cmd}`
6+
end
7+
8+
def run_all_tests
9+
system('clear')
10+
result = run "./util/ecukes/ecukes features"
11+
puts result
12+
end
13+
14+
run_all_tests
15+
watch('.*.(feature|el)') { run_all_tests }
16+
17+
# Ctrl-\
18+
Signal.trap 'QUIT' do
19+
puts " --- Running all tests ---\n\n"
20+
run_all_tests
21+
end
22+
23+
@interrupted = false
24+
25+
# Ctrl-C
26+
Signal.trap 'INT' do
27+
if @interrupted then
28+
@wants_to_quit = true
29+
abort("\n")
30+
else
31+
puts "Interrupt a second time to quit"
32+
@interrupted = true
33+
Kernel.sleep 1.5
34+
# raise Interrupt, nil # let the run loop catch it
35+
run_all_tests
36+
@interrupted = false
37+
end
38+
end
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
;;; clojure-mode-expansions.el --- Clojure-specific expansions for expand-region
2+
3+
;; Copyright (C) 2011 Magnar Sveen
4+
5+
;; Author: Magnar Sveen <[email protected]>
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+
23+
;; Extra expansions for clojure-mode:
24+
;;
25+
;; * `er/mark-clj-word` - includes dashes, but not slashes.
26+
;; * `er/mark-clj-regexp-literal`
27+
;; * `er/mark-clj-function-literal`
28+
;;
29+
;; Feel free to contribute any other expansions for Clojure at
30+
;;
31+
;; https://github.com/magnars/expand-region.el
32+
33+
;;; Code:
34+
35+
(require 'expand-region-core)
36+
37+
(defun er/mark-clj-word ()
38+
"Mark the entire word around or in front of point, including dashes."
39+
(interactive)
40+
(let ((word-regexp "\\(\\sw\\|-\\)"))
41+
(when (or (looking-at word-regexp)
42+
(looking-back word-regexp))
43+
(while (looking-at word-regexp)
44+
(forward-char))
45+
(set-mark (point))
46+
(while (looking-back word-regexp)
47+
(backward-char)))))
48+
49+
(defun er/mark-clj-regexp-literal ()
50+
"Mark clj-regexp-literal presumes that point is outside the string.
51+
If point is inside the string, the quotes will be marked first anyway."
52+
(interactive)
53+
(when (or (looking-at "#\"")
54+
(looking-back "#"))
55+
(forward-char 1)
56+
(search-backward "#")
57+
(set-mark (point))
58+
(search-forward "\"")
59+
(forward-char 1)
60+
(er--move-point-forward-out-of-string)
61+
(exchange-point-and-mark)))
62+
63+
(defun er/mark-clj-function-literal ()
64+
"Mark clj-function-literal presumes that point is outside the parens.
65+
If point is inside the parens, they will be marked first anyway."
66+
(interactive)
67+
(when (or (looking-at "#(")
68+
(looking-back "#"))
69+
(forward-char)
70+
(search-backward "#")
71+
(set-mark (point))
72+
(search-forward "(")
73+
(backward-char)
74+
(forward-list)
75+
(exchange-point-and-mark)))
76+
77+
(defun er/add-clojure-mode-expansions ()
78+
"Adds clojure-specific expansions for buffers in clojure-mode"
79+
(set (make-local-variable 'er/try-expand-list) (append
80+
er/try-expand-list
81+
'(er/mark-clj-word
82+
er/mark-clj-regexp-literal
83+
er/mark-clj-function-literal))))
84+
85+
(add-hook 'clojure-mode-hook 'er/add-clojure-mode-expansions)
86+
87+
(provide 'clojure-mode-expansions)
88+
89+
;; clojure-mode-expansions.el ends here

expand-region/css-mode-expansions.el

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
;;; css-mode-expansions.el --- CSS-specific expansions for expand-region
2+
3+
;; Copyright (C) 2011 Magnar Sveen
4+
5+
;; Author: Magnar Sveen <[email protected]>
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+
23+
;; For now I have only found the need for mark-css-declaration.
24+
;;
25+
;; Feel free to contribute any other expansions for CSS at
26+
;;
27+
;; https://github.com/magnars/expand-region.el
28+
29+
;;; Code:
30+
31+
(require 'expand-region-core)
32+
33+
(defun er/mark-css-declaration ()
34+
"Marks one CSS declaration, eg. font-weight: bold;"
35+
(interactive)
36+
(search-backward-regexp "[;{] ?" (line-beginning-position))
37+
(forward-char)
38+
(set-mark (point))
39+
(search-forward ";" (line-end-position))
40+
(exchange-point-and-mark))
41+
42+
(defun er/add-css-mode-expansions ()
43+
"Adds CSS-specific expansions for buffers in css-mode"
44+
(set (make-local-variable 'er/try-expand-list) (append
45+
er/try-expand-list
46+
'(er/mark-css-declaration))))
47+
48+
(add-hook 'css-mode-hook 'er/add-css-mode-expansions)
49+
50+
(provide 'css-mode-expansions)
51+
52+
;; css-mode-expansions.el ends here
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
;;; erlang-mode-expansions.el --- Erlang-specific expansions for expand-region
2+
3+
;; Copyright (C) 2012 Gleb Peregud
4+
5+
;; Author: Gleb Peregud
6+
;; Based on python-mode-expansions by: Ivan Andrus <[email protected]>
7+
;; Keywords: marking region erlang
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+
;; Feel free to contribute any other expansions for Erlang at
25+
;;
26+
;; https://github.com/magnars/expand-region.el
27+
28+
;;; Bugs:
29+
30+
;; Doesn't handle many Erlang syntax constructs, just the basics
31+
32+
;;; Code:
33+
34+
(require 'expand-region-core)
35+
36+
(defun er/add-erlang-mode-expansions ()
37+
"Adds Erlang-specific expansions for buffers in erlang-mode"
38+
(set (make-local-variable 'er/try-expand-list) (append
39+
er/try-expand-list
40+
'(erlang-mark-function
41+
erlang-mark-clause))))
42+
43+
(add-hook 'erlang-mode-hook 'er/add-erlang-mode-expansions)
44+
45+
(provide 'erlang-mode-expansions)
46+
47+
;; erlang-mode-expansions.el ends here

0 commit comments

Comments
 (0)