-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsmart-region.el
119 lines (105 loc) · 4.08 KB
/
smart-region.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
107
108
109
110
111
112
113
114
115
116
117
118
119
;;; smart-region.el --- Smartly select region, rectangle, multi cursors
;;-------------------------------------------------------------------
;;
;; Copyright (C) 2015 Yuuki Arisawa
;;
;; This file is NOT part of Emacs.
;;
;; 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 2 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;
;;-------------------------------------------------------------------
;; Author: Yuuki Arisawa <yuuki.ari@gmail.com>
;; URL: https://github.com/uk-ar/smart-region
;; Package-Requires: ((emacs "24.4") (expand-region "0.10.0") (multiple-cursors "1.3.0") (cl-lib "0.5"))
;; Created: 1 April 2015
;; Version: 1.0
;; Keywords: marking region
;;; Compatibility: GNU Emacs 24.4
;;; Commentary:
;; Smart region guesses what you want to select by one command:
;; - If you call this command multiple times at the same position, it
;; expands the selected region (with `er/expand-region').
;; - Else, if you move from the mark and call this command, it selects
;; the region rectangular (with `rectangle-mark-mode').
;; - Else, if you move from the mark and call this command at the same
;; column as mark, it adds a cursor to each line (with `mc/edit-lines').
;; This basic concept is from sense-region: https://gist.github.com/tnoda/1776988.
(require 'expand-region)
(require 'multiple-cursors)
(require 'cl-lib)
;;; Code:
(defun smart-region-check-er(func)
(let ((before (cons (mark) (point))))
(funcall func)
(not (equal (cons (mark) (point)) before))))
;;TODO: u C-SPC for pop mark
;;;###autoload
(defun smart-region (arg)
"Smart region guess what you want to select by one command.
If you call this command multiple times at the same position, it expands
selected region (it calls `er/expand-region').
Else, if you move from the mark and call this command, it select the
region rectangular (it call `rectangle-mark-mode').
Else, if you move from the mark and call this command at same column as
mark, it add cursor to each line (it call `mc/edit-lines')."
(interactive "P")
(cond
;;region not exist
((not (region-active-p))
(setq this-command 'set-mark-command)
(call-interactively 'set-mark-command))
;;region exist & single line
((= (line-number-at-pos) (line-number-at-pos (mark)))
;;(setq this-command 'er/expand-region)
;;https://github.com/magnars/expand-region.el/issues/31
(cl-case (char-syntax (char-after))
(?\"
(unless (smart-region-check-er 'er/mark-outside-quotes)
(call-interactively 'er/expand-region)))
((?\) ?\()
(unless (smart-region-check-er 'er/mark-outside-pairs)
(call-interactively 'er/expand-region)))
(t (call-interactively 'er/expand-region))))
;; region exist & multi line
(t
(let ((column-of-mark
(save-excursion
(goto-char (mark))
(current-column))))
(if (eq column-of-mark (current-column))
(call-interactively 'mc/edit-lines)
(call-interactively 'rectangle-mark-mode))))))
;;;###autoload
(defun smart-region-on ()
"Set C-SPC to smart-region."
(interactive)
(define-key global-map [remap set-mark-command] 'smart-region)
)
;;;###autoload
(defun smart-region-off ()
"Reset C-SPC to original command."
(interactive)
(define-key global-map [remap set-mark-command] nil)
)
(add-to-list 'mc/cmds-to-run-once
'smart-region)
;; settings
(setq mc/cmds-to-run-for-all
(delq 'smart-region
mc/cmds-to-run-for-all))
(mc/save-lists)
(provide 'smart-region)
;;; smart-region.el ends here