-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-editing-utils.el
executable file
·277 lines (233 loc) · 10.6 KB
/
init-editing-utils.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
;;----------------------------------------------------------------------------
;; Some basic preferences
;;----------------------------------------------------------------------------
(setq-default
blink-cursor-delay 0
blink-cursor-interval 0.4
bookmark-default-file "~/.emacs.d/.bookmarks.el"
buffers-menu-max-size 30
case-fold-search t
compilation-scroll-output t
ediff-split-window-function 'split-window-horizontally
ediff-window-setup-function 'ediff-setup-windows-plain
grep-highlight-matches t
grep-scroll-output t
indent-tabs-mode nil
line-spacing 0.2
make-backup-files nil
mouse-yank-at-point t
set-mark-command-repeat-pop t
show-trailing-whitespace t
tooltip-delay 1.5
truncate-lines nil
truncate-partial-width-windows nil
visible-bell t)
(transient-mark-mode t)
;;----------------------------------------------------------------------------
;; Zap *up* to char is a more sensible default
;;----------------------------------------------------------------------------
(autoload 'zap-up-to-char "misc" "Kill up to, but not including ARGth occurrence of CHAR.")
(global-set-key (kbd "M-z") 'zap-up-to-char)
(global-set-key (kbd "M-Z") 'zap-to-char)
;;----------------------------------------------------------------------------
;; Don't disable narrowing commands
;;----------------------------------------------------------------------------
(put 'narrow-to-region 'disabled nil)
(put 'narrow-to-page 'disabled nil)
(put 'narrow-to-defun 'disabled nil)
;;----------------------------------------------------------------------------
;; Show matching parens
;;----------------------------------------------------------------------------
(paren-activate) ; activating mic-paren
;;----------------------------------------------------------------------------
;; Autopair quotes and parentheses
;;----------------------------------------------------------------------------
(require 'autopair)
(setq autopair-autowrap t)
(autopair-global-mode t)
(defun inhibit-autopair ()
"Prevent autopair from enabling in the current buffer."
(setq autopair-dont-activate t)
(autopair-mode -1))
;;----------------------------------------------------------------------------
;; Fix per-window memory of buffer point positions
;;----------------------------------------------------------------------------
(global-pointback-mode)
;;----------------------------------------------------------------------------
;; Don't disable case-change functions
;;----------------------------------------------------------------------------
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
;;----------------------------------------------------------------------------
;; Rectangle selections, and overwrite text when the selection is active
;;----------------------------------------------------------------------------
;;http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
(cua-selection-mode t) ; for rectangles, CUA is nice
;;C-x r k 剪切一个矩形块
;; C-x r y 粘贴一个矩形块
;; C-x r o 插入一个矩形块
;; C-x r c 清除一个矩形块(使其变成空白)
;; C-x r t 在选定区域的所有列前插入样的字符
;; [M-a]: 将rect中的文字左对齐
;; [M-c]: 去掉所有行左侧的空格
;; [M-f]: 用单个字符替换所有rect中的字符(提示输入一个字符)
;; [M-i]: 对每行中第一个找到的数字进行加1操作(自动把0x开头的当作十六进制数字)
;; [M-k]: 剪切rect
;; [M-m]: 拷贝rect
;; [M-n]: 用一串自增的数字替换rect中的每一行(这个功能可以用来给每行编号)
;; [M-o]: rect的内容右移,选中的rect用空格填充
;; [M-R]: 上下反转<br />[M-s]: 把rect中的每一行替换为一个字符串(提示输入)
;; [M-t]: 把rect整个替换为一个字符串(提示输入)<br />
;;----------------------------------------------------------------------------
;; Handy key bindings
;;----------------------------------------------------------------------------
;; To be able to M-x without meta
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
;; Vimmy alternatives to M-^ and C-u M-^
(global-set-key (kbd "C-c j") 'join-line)
(global-set-key (kbd "C-c J") (lambda () (interactive) (join-line 1)))
(global-set-key (kbd "M-T") 'transpose-lines)
(global-set-key (kbd "C-.") 'set-mark-command)
(global-set-key (kbd "C-x C-.") 'pop-global-mark)
(global-set-key (kbd "C-;") 'ace-jump-mode)
(global-set-key (kbd "C-:") 'ace-jump-word-mode)
;; Mark-multiple and friends
(global-set-key (kbd "C-x r t") 'inline-string-rectangle)
(global-set-key (kbd "C-<") 'mark-previous-like-this)
(global-set-key (kbd "C->") 'mark-next-like-this)
(global-set-key (kbd "C-M-m") 'mark-more-like-this)
(defun duplicate-line ()
(interactive)
(save-excursion
(let ((line-text (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(move-end-of-line 1)
(newline)
(insert line-text))))
(global-set-key (kbd "C-c p") 'duplicate-line)
;; Train myself to use M-f and M-b instead
(global-unset-key [M-left])
(global-unset-key [M-right])
;;----------------------------------------------------------------------------
;; Fill column indicator
;;----------------------------------------------------------------------------
(defun sanityinc/prog-mode-fci-settings ()
(turn-on-fci-mode)
(when show-trailing-whitespace
(set (make-local-variable 'whitespace-style) '(face trailing))
(whitespace-mode 1))
(defvar sanityinc/fci-mode-suppressed nil)
(defadvice popup-create (before suppress-fci-mode activate)
"Suspend fci-mode while popups are visible"
(set (make-local-variable 'sanityinc/fci-mode-suppressed) fci-mode)
(when fci-mode
(turn-off-fci-mode)))
(defadvice popup-delete (after restore-fci-mode activate)
"Restore fci-mode when all popups have closed"
(when (and (not popup-instances) sanityinc/fci-mode-suppressed)
(setq sanityinc/fci-mode-suppressed nil)
(turn-on-fci-mode)))
)
(if (>= emacs-major-version 24)
(add-hook 'prog-mode-hook 'sanityinc/prog-mode-fci-settings)
)
;;----------------------------------------------------------------------------
;; Shift lines up and down with M-up and M-down
;;----------------------------------------------------------------------------
(move-text-default-bindings)
;;----------------------------------------------------------------------------
;; Fix backward-up-list to understand quotes, see http://bit.ly/h7mdIL
;;----------------------------------------------------------------------------
(defun backward-up-sexp (arg)
(interactive "p")
(let ((ppss (syntax-ppss)))
(cond ((elt ppss 3)
(goto-char (elt ppss 8))
(backward-up-sexp (1- arg)))
((backward-up-list arg)))))
(global-set-key [remap backward-up-list] 'backward-up-sexp) ; C-M-u, C-M-up
;;----------------------------------------------------------------------------
;; Cut/copy the current line if no region is active
;;----------------------------------------------------------------------------
(whole-line-or-region-mode t)
(diminish 'whole-line-or-region-mode)
(make-variable-buffer-local 'whole-line-or-region-mode)
(defun suspend-mode-during-cua-rect-selection (mode-name)
"Add an advice to suspend `MODE-NAME' while selecting a CUA rectangle."
(let ((flagvar (intern (format "%s-was-active-before-cua-rectangle" mode-name)))
(advice-name (intern (format "suspend-%s" mode-name))))
(eval-after-load 'cua-rect
`(progn
(defvar ,flagvar nil)
(make-variable-buffer-local ',flagvar)
(defadvice cua--activate-rectangle (after ,advice-name activate)
(setq ,flagvar (and (boundp ',mode-name) ,mode-name))
(when ,flagvar
(,mode-name 0)))
(defadvice cua--deactivate-rectangle (after ,advice-name activate)
(when ,flagvar
(,mode-name 1)))))))
(suspend-mode-during-cua-rect-selection 'whole-line-or-region-mode)
;;----------------------------------------------------------------------------
;; Random line sorting
;;----------------------------------------------------------------------------
(defun sort-lines-random (beg end)
"Sort lines in region randomly."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(let ;; To make `end-of-line' and etc. to ignore fields.
((inhibit-field-text-motion t))
(sort-subr nil 'forward-line 'end-of-line nil nil
(lambda (s1 s2) (eq (random 2) 0)))))))
;; copy word, check function mark-sexp
; @see http://www.emacswiki.org/emacs/CopyWithoutSelection
;; http://emacser.com/torture-emacs.htm
(defun qiang-comment-dwim-line (&optional arg)
"Replacement for the comment-dwim command.
If no region is selected and current line is not blank and we are not at the
end of the line, then comment current line. Replaces default behaviour of
comment-dwim, when it inserts comment at the end of the line."
(interactive "*P")
(comment-normalize-vars)
(if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
(comment-or-uncomment-region
(line-beginning-position) (line-end-position))
(comment-dwim arg)))
(global-set-key "\M-;" 'qiang-comment-dwim-line)
;; line copy, if no region active, it simply copy the current whole line
(defadvice kill-line (before check-position activate)
(if (member major-mode
'(emacs-lisp-mode scheme-mode lisp-mode
c-mode c++-mode objc-mode js-mode
latex-mode plain-tex-mode))
(if (and (eolp) (not (bolp)))
(progn (forward-char 1)
(just-one-space 0)
(backward-char 1)))))
(defadvice kill-ring-save (before slick-copy activate compile)
"When called with no active region,copy a single line instead."
(interactive (if mark-active (list (region-beginning) (region-end))
(message "Copied line")
(list (line-beginning-position)
(line-beginning-position 2)))))
(defadvice kill-region (before slick-cut activate compile)
"When called with no active region, kill a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))
;; Copy line from point to the end, exclude the line break
(defun qiang-copy-line (arg)
"Copy lines (as many as prefix argument) in the kill ring"
(interactive "p")
(kill-ring-save (point)
(line-end-position))
(message "%d line%s copied" arg (if (= 1 arg) "" "s")))
(global-set-key (kbd "M-k") 'qiang-copy-line)
;need install browse-kill-ring
(browse-kill-ring-default-keybindings)
(provide 'init-editing-utils)