forked from magnars/expand-region.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clojure-mode-expansions.el
106 lines (89 loc) · 3.63 KB
/
clojure-mode-expansions.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
;;; clojure-mode-expansions.el --- Clojure-specific expansions for expand-region
;; Copyright (C) 2011 Magnar Sveen
;; Author: Magnar Sveen <[email protected]>
;; Keywords: marking region
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Extra expansions for clojure-mode:
;;
;; * `er/mark-clj-word` - includes dashes, but not slashes.
;; * `er/mark-clj-regexp-literal`
;; * `er/mark-clj-function-literal`
;;
;; Feel free to contribute any other expansions for Clojure at
;;
;; https://github.com/magnars/expand-region.el
;;; Code:
(require 'expand-region-core)
(require 'er-basic-expansions)
(defun er/mark-clj-word ()
"Mark the entire word around or in front of point, including dashes."
(interactive)
(let ((word-regexp "\\(\\sw\\|-\\)"))
(when (or (looking-at word-regexp)
(er/looking-back-on-line word-regexp))
(while (looking-at word-regexp)
(forward-char))
(set-mark (point))
(while (er/looking-back-on-line word-regexp)
(backward-char)))))
(defun er/mark-clj-set-literal ()
"Mark clj-set-literal presumes that point is outside the brackets.
If point is inside the brackets, those will be marked first anyway."
(interactive)
(when (or (looking-at "#{")
(er/looking-back-exact "#"))
(forward-char 1)
(search-backward "#")
(set-mark (point))
(search-forward "{")
(forward-char -1)
(forward-list 1)
(exchange-point-and-mark)))
(defun er/mark-clj-regexp-literal ()
"Mark clj-regexp-literal presumes that point is outside the string.
If point is inside the string, the quotes will be marked first anyway."
(interactive)
(when (or (looking-at "#\"")
(er/looking-back-exact "#"))
(forward-char 1)
(search-backward "#")
(set-mark (point))
(search-forward "\"")
(forward-char 1)
(er--move-point-forward-out-of-string)
(exchange-point-and-mark)))
(defun er/mark-clj-function-literal ()
"Mark clj-function-literal presumes that point is outside the parens.
If point is inside the parens, they will be marked first anyway."
(interactive)
(when (or (looking-at "#(")
(er/looking-back-exact "#"))
(forward-char)
(search-backward "#")
(set-mark (point))
(search-forward "(")
(backward-char)
(forward-list)
(exchange-point-and-mark)))
(defun er/add-clojure-mode-expansions ()
"Adds clojure-specific expansions for buffers in clojure-mode"
(set (make-local-variable 'er/try-expand-list) (append
er/try-expand-list
'(er/mark-clj-word
er/mark-clj-regexp-literal
er/mark-clj-set-literal
er/mark-clj-function-literal))))
(er/enable-mode-expansions 'clojure-mode 'er/add-clojure-mode-expansions)
(er/enable-mode-expansions 'nrepl-mode 'er/add-clojure-mode-expansions)
(provide 'clojure-mode-expansions)
;; clojure-mode-expansions.el ends here