forked from vjohansen/emacs-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvj-functions.el
executable file
·455 lines (374 loc) · 13.7 KB
/
vj-functions.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
(defun my-scroll-up-hook ()
"My scroll-up."
(interactive)
(scroll-up 2)
)
(defun my-scroll-down-hook ()
"My scroll-down."
(interactive)
(scroll-down 2)
)
(defun vj-os-open (filename)
"Let the OS open the file with the appropriate program."
(cond ((equal system-type 'darwin)
(shell-command (concat "Open " (shell-quote-argument
(expand-file-name filename)))))
((equal system-type 'windows-nt)
(w32-shell-execute "open" (substitute ?\\ ?/ (expand-file-name filename))))
(t
(start-process "vj-os-open" nil "/usr/bin/open" (expand-file-name filename)))))
(defun eshell/lt () "vj 2009" (eshell/ls "-lrt"))
(defun eshell/open (FILE)
"Let the OS determine how open a file."
(vj-os-open FILE))
(defun eshell/e (filename)
"alias e to find-file"
(find-file filename))
(global-set-key "\M-:" 'vj-find-tag)
(defun vj-find-tag ()
"My find-tag wrapper for easy repetition (VJO 2003).
Call `find-tag' with current word first time and after that call
find-tag with NEXT-P set to t (if called repeatedly)"
(interactive)
(vps-auto-change-project t) ;can change last-command
;; (message "vj-find-tag tag %s, cmd %s" last-tag last-command)
(if (eq last-command 'vj-find-tag)
(find-tag nil t)
(find-tag (current-word) current-prefix-arg)))
(defun kill-whole-line ()
"Kills the entire current line.
With prefix argument, kill that many lines from point."
(interactive)
(beginning-of-line)
(let ((beg (point)))
(end-of-line)
(if (eq current-prefix-arg nil)
(forward-line 1)
(forward-line current-prefix-arg))
(kill-region beg (point)))
)
(defun copy-line-as-kill ()
"Save the entire line as if killed, but keep it in buffer."
(interactive)
(let ((org (point)))
(beginning-of-line)
(let ((beg (point)))
(end-of-line)
(forward-line 1)
(copy-region-as-kill beg (point)))
(goto-char org))
)
(defun vj-insert-local-variables-section ()
""
(interactive)
(goto-char (point-max))
(insert (concat comment-start " Local " "Variables: ***\n"))
(insert (concat comment-start " compile-command: " (format "\"%s\"" compile-command) " ***\n"))
(insert (concat comment-start " End: ***\n"))
(previous-line 2)
(end-of-line)
(backward-word 1))
(defun list-buffers-change-window ( )
"Display a list of names of existing buffers and
jumps to that window."
(interactive)
(list-buffers)
(other-window 1)
(next-line 2)
)
(defun vjo-forward-current-word-keep-offset ()
" (VJO 1999)"
(interactive)
(let ((re-curword) (curword) (offset (point))
(old-case-fold-search case-fold-search) )
(setq curword (thing-at-point 'symbol))
(setq re-curword (concat "\\<" (thing-at-point 'symbol) "\\>") )
(beginning-of-thing 'symbol)
(setq offset (- offset (point))) ; offset from start of symbol/word
(setq offset (- (length curword) offset)) ; offset from end
;; (message "VJO-SEARCH-FWD: %s %d" curword offset)
(forward-char)
(setq case-fold-search nil)
(if (re-search-forward re-curword nil t)
(backward-char offset)
;; else
(progn (goto-char (point-min))
(if (re-search-forward re-curword nil t)
(progn (message "Searching from top. %s" (what-line))
(backward-char offset))
;; else
(message "Searching from top: Not found"))
))
(setq case-fold-search old-case-fold-search)
))
(defun vjo-backward-current-word-keep-offset ()
" (VJO 2002)"
(interactive)
(let ((re-curword) (curword) (offset (point))
(old-case-fold-search case-fold-search) )
(setq curword (thing-at-point 'symbol))
(setq re-curword (concat "\\<" curword "\\>") )
(beginning-of-thing 'symbol)
(setq offset (- offset (point))) ; offset from start of symbol/word
;; (message "VJO-SEARCH-BACK: %s %d" curword offset)
(forward-char)
(setq case-fold-search nil)
(if (re-search-backward re-curword nil t)
(forward-char offset)
;; else
(progn (goto-char (point-max))
(if (re-search-backward re-curword nil t)
(progn (message "Searching from bottom. %s" (what-line))
(forward-char offset))
;; else
(message "Searching from bottom: Not found"))
))
(setq case-fold-search old-case-fold-search)
))
;show ascii table
; From http://www.dotemacs.de/dotfiles/BenjaminRutt.emacs.html
(defun ascii-table ()
"Print the ascii table. Based on a defun by Alex Schroeder <[email protected]>"
(interactive)
(switch-to-buffer "*ASCII*")
(erase-buffer)
(insert (format "ASCII characters up to number %d.\n" 254))
(let ((i 0))
(while (< i 254)
(setq i (+ i 1))
(insert (format "0x%02x %3d %c\n" i i i))))
(beginning-of-buffer))
;;; Original author: [email protected], 28-Jan-1996
(defun copy-and-inc-line-no-arg ()
"Copies line, preserving cursor column, and increments any numbers found.
This should probably be generalized in the future."
(interactive)
(let* ((col (current-column))
(bol (progn (beginning-of-line) (point)))
(eol (progn (end-of-line) (point)))
(line (buffer-substring bol eol)))
(beginning-of-line)
(while (re-search-forward "[0-9]+" eol 1)
(let ((num (string-to-number (buffer-substring
(match-beginning 0) (match-end 0)))))
(replace-match (number-to-string (1+ num)))))
(beginning-of-line)
(insert line "\n")
(move-to-column col)))
(defun copy-and-inc-line ()
"Copy and yank current line with support for prearg (VJO 2002)"
(interactive)
(if current-prefix-arg
(while (not (= current-prefix-arg 0))
(progn
(setq current-prefix-arg (- current-prefix-arg 1))
(copy-and-inc-line-no-arg)
))
(copy-and-inc-line-no-arg)))
(defun my-increment-number-at-point (&optional amount)
"Increment the number under point by `amount'"
(interactive "p")
(let ((num (number-at-point)))
(when (numberp num)
(let ((newnum (+ num amount))
(p (point)))
(save-excursion
(skip-chars-backward "-.0123456789")
(delete-region (point) (+ (point) (length (number-to-string num))))
(insert (number-to-string newnum)))
(goto-char p)))))
(defun backslash-replace-on-region (beg end)
"(VJO 2002)"
(interactive "r")
(replace-string "\\" "/" nil beg end))
;; ------------------------------------------------------------
(defun ext-unfill-paragraph ()
"Do the opposite of `fill-paragraph'; stuff all lines in the current paragraph into a single long line."
(interactive)
(let ((fill-column most-positive-fixnum))
(fill-paragraph nil)))
(defun vj-tv-mode ()
(interactive)
(set-frame-parameter (selected-frame) 'alpha '(45 70))
(tool-bar-mode -1)
(menu-bar-mode -1)
)
(defun occur-non-ascii ()
"Find any non-ascii characters in the current buffer."
(interactive)
(occur "[^[:ascii:]]"))
;;(trace-function-background 'query-replace)
;; trick -- trace ignore -- (ignore "interesting" "stuff")
;; ------------------------------------------------------------
(defun vjo-forward-current-word-keep-offset ()
" (VJO 1999)"
(interactive)
(let ((re-curword) (curword) (offset (point))
(old-case-fold-search case-fold-search) )
(setq curword (thing-at-point 'symbol))
(setq re-curword (concat "\\<" (thing-at-point 'symbol) "\\>") )
(beginning-of-thing 'symbol)
(setq offset (- offset (point))) ; offset from start of symbol/word
(setq offset (- (length curword) offset)) ; offset from end
;; (message "VJO-SEARCH-FWD: %s %d" curword offset)
(forward-char)
(setq case-fold-search nil)
(if (re-search-forward re-curword nil t)
(backward-char offset)
;; else
(progn (goto-char (point-min))
(if (re-search-forward re-curword nil t)
(progn (message "Searching from top. %s" (what-line))
(backward-char offset))
;; else
(message "Searching from top: Not found"))
))
(setq case-fold-search old-case-fold-search)
))
(defun vjo-backward-current-word-keep-offset ()
" (VJO 2002)"
(interactive)
(let ((re-curword) (curword) (offset (point))
(old-case-fold-search case-fold-search) )
(setq curword (thing-at-point 'symbol))
(setq re-curword (concat "\\<" curword "\\>") )
(beginning-of-thing 'symbol)
(setq offset (- offset (point))) ; offset from start of symbol/word
;; (message "VJO-SEARCH-BACK: %s %d" curword offset)
(forward-char)
(setq case-fold-search nil)
(if (re-search-backward re-curword nil t)
(forward-char offset)
;; else
(progn (goto-char (point-max))
(if (re-search-backward re-curword nil t)
(progn (message "Searching from bottom. %s" (what-line))
(forward-char offset))
;; else
(message "Searching from bottom: Not found"))
))
(setq case-fold-search old-case-fold-search)
))
(defun insert-char-above (&optional n)
"Insert the character above point.
With a prefix arg, insert the N characters above point.
(Kevin Rodgers)"
(interactive "p")
(insert (save-excursion
(next-line -1)
(buffer-substring (point)
(progn
(forward-char n)
(point))))))
;; ------------------------------------------------------------
;;; TODO: only print "/* str */" after endif if more than 10 lines apart
;;; Should check if mark is active (`mark-active')
(defun vj-ifdef-insert(str)
"my ifdef insertion (VJO 1997)"
(interactive "*sEnter define: ")
(if mark-active
(save-excursion
(if (> (mark) (point))
(exchange-point-and-mark))
(if (eq current-prefix-arg nil)
(insert "#endif /* " str " */\n")
(insert "#endif /* not " str " */\n"))
(exchange-point-and-mark)
(if (eq current-prefix-arg nil)
(insert "#ifdef " str "\n")
(insert "#ifndef " str "\n"))
)))
(defun vj-ifndef-insert ()
(interactive)
(save-excursion
(goto-char (point-min))
(re-search-forward "^\\( \\|$\\)" nil t)
(forward-line 1) ;skip file header
(insert (concat "#ifndef " (tempo-c-cpp-hfilename) "\n"))
(insert (concat "#define " (tempo-c-cpp-hfilename) "\n"))
(goto-char (point-max))
(insert (concat "\n#endif /* not " (tempo-c-cpp-hfilename) " */"))))
;; ------------------------------------------------------------
(defun slash-replace-on-region (beg end)
"(VJO 2012)"
(interactive "r")
(let (from to)
(save-excursion
(goto-char beg)
(when (search-forward-regexp "[/\\]+")
(setq from (match-string-no-properties 0))
(cond
((equal from "/") (setq to "\\"))
((equal from "\\") (setq to "\\\\"))
((equal from "\\\\") (setq to "/"))
(t (message "No slash found"))))) ;never happens
(if to
(save-excursion
(replace-string from to nil beg end)))))
(defvar vj-perl-history
'("perl -MText::Autoformat -e 'autoformat'"
"$S=','; @F=split(qr($S)); $F[1]= ; $_=join($S,@F);" ; field manip
"$_ = \"$. - $_\"" ; number lines
"s///g"))
(defun perl-on-region (command)
(interactive
(let ((string (read-string "perl -pe " nil 'vj-perl-history)))
(list string)))
;; (kill-ring-save (mark) (point))
(let ((coding-system-for-write 'raw-text))
(shell-command-on-region (mark) (point)
(format "perl -pe %s" (shell-quote-argument command)) nil t)))
(defun vj-git-grep ()
(interactive)
(let ((grep-use-null-device)
(dir (vj-chomp (shell-command-to-string "git rev-parse --show-toplevel"))))
(grep (format "cd %s && git --no-pager grep -n %s" dir (thing-at-point 'symbol)))))
;; ------------------------------------------------------------
(defun vj-line-at-point ()
(buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(defun vj-mark-paragraph ()
(interactive)
(if (string-match "^[ \\t]*$" (vj-line-at-point))
(re-search-forward "[^ \t\n]" nil t)
;;
(end-of-line)
(re-search-backward "^[ \\t]*$" nil t)
(next-line 1))
(beginning-of-line)
(set-mark-command nil)
(re-search-forward "^[ \\t]*$" nil t)
(exchange-point-and-mark))
;; ------------------------------------------------------------
(setq vj-package-list '( json-reformat
company
company-quickhelp
company-statistics
helm iedit
guide-key
gitconfig-mode
gitignore-mode))
(defun vj-install-missing-packages ()
(interactive)
;; Refresh
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Install the missing packages
(dolist (package vj-package-list)
(unless (package-installed-p package)
(package-install package))))
;; ------------------------------------------------------------
;; based on code from https://github.com/magit/magit/issues/1972
(require 'color)
(defun adjust-fg-colors (percent face-list)
(interactive)
(dolist (face face-list)
(if (face-foreground face)
(set-face-foreground face
(color-lighten-name (face-foreground face) percent)))))
(defun adjust-bg-colors (percent face-list)
(interactive)
(dolist (face face-list)
(if (face-background face)
(set-face-background face
(color-lighten-name (face-background face) percent)))))