forked from purcell/emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit-editing-utils.el
279 lines (234 loc) · 9.88 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
278
279
;;----------------------------------------------------------------------------
;; 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
size-indication-mode t
truncate-lines t
truncate-partial-width-windows nil
visible-bell nil
column-number-mode nil
transient-mark-mode t
goto-address-mail-face 'link
revert-without-query '(".*")
align-text-modes (quote (text-mode outline-mode org-mode))
blink-matching-paren t
electric-pair-mode -1
electric-indent-mode -1
scroll-conservatively 0)
(add-hook 'find-file-hooks 'goto-address-prog-mode)
;; (add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)
(setq whitespace-style
'(face
tabs
spaces
trailing
lines
space-before-tab
newline
indentation
empty ; remove all empty lines at beginning/end of buffer
space-after-tab
space-mark
tab-mark
newline-mark))
(defalias 'yes-or-no-p 'y-or-n-p)
;;----------------------------------------------------------------------------
;; auto-save game
;;----------------------------------------------------------------------------
(set-variable 'auto-save-default t)
(defvar autosave-dir
(concat "~/.emacs.d/saved"))
(make-directory autosave-dir t)
(defun auto-save-file-name-p (filename)
(string-match "^#.*#$" (file-name-nondirectory filename)))
(defun make-auto-save-file-name ()
(concat autosave-dir
(if buffer-file-name
(concat "#" (file-name-nondirectory buffer-file-name) "#")
(expand-file-name (concat "#%" (buffer-name) "#")))))
(defvar backup-dir (concat "~/.emacs.d/saved"))
(setq backup-directory-alist (list (cons "." backup-dir)))
(setq delete-old-versions nil)
;;----------------------------------------------------------------------------
;; Zap *up* to char is a more sensible default
;;----------------------------------------------------------------------------
(autoload 'zap-up-to-char "misc" "Kill up to 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
;;----------------------------------------------------------------------------
(show-paren-mode t)
(paren-activate) ; activating mic-paren
;;----------------------------------------------------------------------------
;; 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
;;----------------------------------------------------------------------------
(cua-selection-mode t) ; for rectangles, CUA is nice
(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))))
;;----------------------------------------------------------------------------
;; 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)
;;----------------------------------------------------------------------------
;; Unfill regions or paragraphs (see http://xahlee.org/emacs/emacs_unfill-paragraph.html)
;;----------------------------------------------------------------------------
(defun unfill-paragraph ()
"Replace newline chars in current paragraph by single spaces.
This command does the reverse of `fill-paragraph'."
(interactive)
(let ((fill-column most-positive-fixnum))
(fill-paragraph nil)))
(defun unfill-region (start end)
"Replace newline chars in region by single spaces.
This command does the reverse of `fill-region'."
(interactive "r")
(let ((fill-column most-positive-fixnum))
(fill-region start end)))
;;----------------------------------------------------------------------------
;; 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)))))))
(defun goto-match-paren (arg)
"Go to the matching parenthesis if on parenthesis, otherwise insert
the character typed."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1))) ))
;; this uses `forward-whitespace' from `thingatpt.el'
(defun fg/backward-whitespace ()
(interactive)
(forward-whitespace -1))
;; http://dorophone.blogspot.com/2011/11/how-to-make-emacs-scratch-buffer.html
(defvar persistent-scratch-filename
"~/.emacs.d/emacs-persistent-scratch"
"Location of *scratch* file contents for persistent-scratch.")
(defvar persistent-scratch-backup-directory
"~/.emacs.d/emacs-persistent-scratch-backups/"
"Location of backups of the *scratch* buffer contents for
persistent-scratch.")
(defun make-persistent-scratch-backup-name ()
"Create a filename to backup the current scratch file by
concatenating PERSISTENT-SCRATCH-BACKUP-DIRECTORY with the
current date and time."
(concat
persistent-scratch-backup-directory
(replace-regexp-in-string
(regexp-quote " ") "-" (format-time-string "%d%m%y_%H%M%S"))))
(defun save-persistent-scratch ()
"Write the contents of *scratch* to the file name
PERSISTENT-SCRATCH-FILENAME, making a backup copy in
PERSISTENT-SCRATCH-BACKUP-DIRECTORY."
(with-current-buffer (get-buffer "*scratch*")
(if (file-exists-p persistent-scratch-filename)
(copy-file persistent-scratch-filename
(make-persistent-scratch-backup-name)))
(write-region (point-min) (point-max)
persistent-scratch-filename)))
(defun load-persistent-scratch ()
"Load the contents of PERSISTENT-SCRATCH-FILENAME into the
scratch buffer, clearing its contents first."
(if (file-exists-p persistent-scratch-filename)
(with-current-buffer (get-buffer "*scratch*")
(delete-region (point-min) (point-max))
(insert-file-contents persistent-scratch-filename))))
;; Load persisted scratch buffer upon start-up
(load-persistent-scratch)
;; Hook into saving for persisting
(push #'save-persistent-scratch kill-emacs-hook)
(require 'inline-string-rectangle)
(require 'mark-more-like-this)
(require 'ctags-update)
(require 'expand-region)
(require 'bookmark+)
(require 'wrap-region)
(wrap-region-add-wrapper "`" "'")
(wrap-region-add-wrapper "(" ")")
(wrap-region-add-wrapper "[" "]")
(wrap-region-add-wrapper "{" "}")
(require 'undo-tree)
(global-undo-tree-mode 1)
(provide 'init-editing-utils)