forked from cjohansen/.emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefuns.el
383 lines (323 loc) · 12.1 KB
/
defuns.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
(require 'imenu)
;; Network
(defun view-url ()
"Open a new buffer containing the contents of URL."
(interactive)
(let* ((default (thing-at-point-url-at-point))
(url (read-from-minibuffer "URL: " default)))
(switch-to-buffer (url-retrieve-synchronously url))
(rename-buffer url t)
;; TODO: switch to nxml/nxhtml mode
(cond ((search-forward "<?xml" nil t) (xml-mode))
((search-forward "<html" nil t) (html-mode)))))
;; Buffer-related
(defun ido-imenu ()
"Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
(let ((symbol-at-point (thing-at-point 'symbol)))
(when symbol-at-point
(let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
(matching-symbols (delq nil (mapcar (lambda (symbol)
(if (string-match regexp symbol) symbol))
symbol-names))))
(when matching-symbols
(sort matching-symbols (lambda (a b) (> (length a) (length b))))
(mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
matching-symbols)))))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))
;;; These belong in coding-hook:
;; We have a number of turn-on-* functions since it's advised that lambda
;; functions not go in hooks. Repeatedly evaling an add-to-list with a
;; hook value will repeatedly add it since there's no way to ensure
;; that a lambda doesn't already exist in the list.
(defun local-column-number-mode ()
(make-local-variable 'column-number-mode)
(column-number-mode t))
(defun local-comment-auto-fill ()
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(auto-fill-mode t))
(defun turn-on-hl-line-mode ()
(if window-system (hl-line-mode t)))
(defun turn-on-save-place-mode ()
(setq save-place t))
(defun turn-on-whitespace ()
(whitespace-mode t))
(defun turn-off-tool-bar ()
(tool-bar-mode -1))
(add-hook 'coding-hook 'local-column-number-mode)
(add-hook 'coding-hook 'local-comment-auto-fill)
(add-hook 'coding-hook 'turn-on-hl-line-mode)
(defun untabify-buffer ()
(interactive)
(untabify (point-min) (point-max)))
(defun indent-buffer ()
(interactive)
(indent-region (point-min) (point-max)))
(defun cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer."
(interactive)
(indent-buffer)
(untabify-buffer)
(delete-trailing-whitespace))
(defun recentf-ido-find-file ()
"Find a recent file using ido."
(interactive)
(let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file
(find-file file))))
;; Other
(defun eval-and-replace ()
"Replace the preceding sexp with its value."
(interactive)
(backward-kill-sexp)
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(current-buffer))
(error (message "Invalid expression")
(insert (current-kill 0)))))
(defun recompile-init ()
"Byte-compile all your dotfiles again."
(interactive)
(byte-recompile-directory dotfiles-dir 0)
;; TODO: remove elpa-to-submit once everything's submitted.
(byte-recompile-directory (concat dotfiles-dir "elpa-to-submit/" 0)))
(defun sudo-edit (&optional arg)
(interactive "p")
(if (or arg (not buffer-file-name))
(find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
(defun esk-paredit-nonlisp ()
"Turn on paredit mode for non-lisps."
(set (make-local-variable 'paredit-space-for-delimiter-predicate)
(lambda (endp delimiter)
(equal (char-syntax (char-before)) ?\")))
(paredit-mode 1))
(defun new-line-above ()
(interactive)
(beginning-of-line)
(newline)
(previous-line)
(indent-for-tab-command))
(defun new-line-below ()
(interactive)
(if (eolp)
(newline)
(end-of-line)
(newline)
(indent-for-tab-command)))
(defun new-line-in-between ()
(interactive)
(newline)
(save-excursion
(newline)
(indent-for-tab-command))
(indent-for-tab-command))
(defun duplicate-current-line-or-region (arg)
"Duplicates the current line or region ARG times.
If there's no region, the current line will be duplicated."
(interactive "p")
(if (region-active-p)
(duplicate-region arg)
(duplicate-current-line arg)))
(defun duplicate-region (num &optional start end)
"Duplicates the region bounded by START and END NUM times.
If no START and END is provided, the current region-beginning and
region-end is used. Adds the duplicated text to the kill ring."
(interactive "p")
(let* ((start (or start (region-beginning)))
(end (or end (region-end)))
(region (buffer-substring start end)))
(kill-ring-save start end)
(goto-char end)
(dotimes (i num)
(insert region))))
(defun duplicate-current-line (num)
"Duplicate the current line NUM times."
(interactive "p")
(duplicate-region num (point-at-bol) (1+ (point-at-eol)))
(goto-char (1- (point))))
(defun add-find-file-hook-with-pattern (pattern fn &optional contents)
"Add a find-file-hook that calls FN for files where PATTERN
matches the file name, and optionally, where CONTENT matches file contents.
Both PATTERN and CONTENTS are matched as regular expressions."
(lexical-let ((re-pattern pattern)
(fun fn)
(re-content contents))
(add-hook 'find-file-hook
(lambda ()
(if (and
(string-match re-pattern (buffer-file-name))
(or (null re-content)
(string-match re-content
(buffer-substring (point-min) (point-max)))))
(apply fun ()))))))
(defun move-line-down ()
(interactive)
(let ((col (current-column)))
(save-excursion
(next-line)
(transpose-lines 1))
(next-line)
(move-to-column col)))
(defun move-line-up ()
(interactive)
(let ((col (current-column)))
(save-excursion
(next-line)
(transpose-lines -1))
(move-to-column col)))
(defun yank-as-line ()
(interactive)
(save-excursion
(insert "\n")
(indent-for-tab-command))
(yank))
(defun yank-indented ()
(interactive)
(let ((start (point)))
(yank)
(indent-region start (point))))
;; toggle quotes
(defun current-quotes-char ()
(nth 3 (syntax-ppss)))
(defalias 'point-is-in-string-p 'current-quotes-char)
(defun move-point-forward-out-of-string ()
(while (point-is-in-string-p) (forward-char)))
(defun move-point-backward-out-of-string ()
(while (point-is-in-string-p) (backward-char)))
(defun alternate-quotes-char ()
(if (eq ?' (current-quotes-char)) ?\" ?'))
(defun toggle-quotes ()
(interactive)
(if (point-is-in-string-p)
(let ((old-quotes (char-to-string (current-quotes-char)))
(new-quotes (char-to-string (alternate-quotes-char)))
(start (make-marker))
(end (make-marker)))
(save-excursion
(move-point-forward-out-of-string)
(backward-delete-char 1)
(set-marker end (point))
(insert new-quotes)
(move-point-backward-out-of-string)
(delete-char 1)
(insert new-quotes)
(set-marker start (point))
(replace-string new-quotes (concat "\\" new-quotes) nil start end)
(replace-string (concat "\\" old-quotes) old-quotes nil start end)))
(error "Point isn't in a string")))
(defun linkify-region-from-kill-ring (start end)
(interactive "r")
(let ((text (buffer-substring start end)))
(delete-region start end)
(insert "<a href=\"")
(yank)
(insert (concat "\">" text "</a>"))))
(defun html-from-console-string (start end)
"Turn a string output from e.g. a test runner into unescaped markup."
(interactive "r")
(query-replace-all "\\\"" "\"" start end)
(query-replace-all "\\n" "\n" start end))
(defun query-replace-all (from-string to-string start end)
(save-excursion
(goto-char start)
(while (search-forward from-string end t)
(replace-match to-string))))
;; kill region if active, otherwise kill backward word
(defun kill-region-or-backward-word ()
(interactive)
(if (region-active-p)
(kill-region (region-beginning) (region-end))
(backward-kill-word 1)))
;; copy region if active
;; otherwise copy to end of current line
;; * with prefix, copy N whole lines
(defun copy-to-end-of-line ()
(interactive)
(kill-ring-save (point)
(line-end-position))
(message "Copied to end of line"))
(defun copy-whole-lines (arg)
"Copy lines (as many as prefix argument) in the kill ring"
(interactive "p")
(kill-ring-save (line-beginning-position)
(line-beginning-position (+ 1 arg)))
(message "%d line%s copied" arg (if (= 1 arg) "" "s")))
(defun copy-line (arg)
"Copy to end of line, or as many lines as prefix argument"
(interactive "P")
(if (null arg)
(copy-to-end-of-line)
(copy-whole-lines (prefix-numeric-value arg))))
(defun save-region-or-current-line (arg)
(interactive "P")
(if (region-active-p)
(kill-ring-save (region-beginning) (region-end))
(copy-line arg)))
(defvar magit-status-fullscreen-window-configuration-register
?b
"The register to store the current window configuration in when
entering fullscreen magit-status.")
(defvar magit-status-fullscreen-register
?g
"The register to store the fullscreen magit-status
window configuration in.")
(defun magit-status-fullscreen ()
"Save the current window configuration, run magit-status
and delete other windows, providing a fullscreen git mode.
The previous window configuration is stored in the register
specified by the magit-status-fullscreen-window-configuration-register
variable. The fullscreen magit status configuration is stored
in register specified by the magit-status-register variable."
(interactive)
(window-configuration-to-register magit-status-fullscreen-window-configuration-register)
(magit-status (magit-get-top-dir default-directory))
(delete-other-windows)
(window-configuration-to-register magit-status-fullscreen-register))
(defun magit-quit-session ()
"Restores the previous window configuration and kills the
magit buffer"
(interactive)
(let ((magit-buffer (current-buffer)))
(jump-to-register magit-status-fullscreen-window-configuration-register)
(kill-buffer magit-buffer)))
(defun kill-and-retry-line ()
"Kill the entire current line and reposition point at indentation"
(interactive)
(back-to-indentation)
(kill-line))
(defun replace-next-underscore-with-camel (arg)
(interactive "p")
(if (> arg 0)
(setq arg (1+ arg))) ; 1-based index to get eternal loop with 0
(while (not (= arg 1))
(search-forward-regexp "_\\sw")
(forward-char -2)
(delete-char 1)
(capitalize-word 1)
(setq arg (1- arg))))
(provide 'defuns)