-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvj-functions.el
executable file
·531 lines (441 loc) · 17 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
(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 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 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 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 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)
(unless (use-region-p) ;; If there is no region use current line
(setq
beg (point-at-bol)
end (point-at-eol)))
(save-excursion
(goto-char beg)
(when (search-forward-regexp "[/\\]+" end)
(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
"chomp $_; $_ = \"$_$. = \\\"$_$.\\\"\\n\"" ; 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-root ()
(let ((dir (vj-chomp (shell-command-to-string "git rev-parse --show-toplevel"))))
(if (string-match "^\\([a-zA-Z]:\\|/\\)" dir)
dir)))
(defun vj-git-grep ()
(interactive)
(let ((grep-use-null-device)
(dir (vj-git-root)))
(grep (format "cd %s && git --no-pager grep -n %s" dir (thing-at-point 'symbol)))))
(defun vj-ag (dir word)
(interactive (list (read-string "Search Term: " (current-word))))
(let ((compilation-buffer-name-function (lambda (mode) (concat "*vj ag*"))))
;; Do not want to set compilation-command which `compile' does
(compilation-start (format "%svj-ag.bat --vimgrep %s %s"
vj-emacs-config-dir (shell-quote-argument word) dir))))
;; vj-ag.bat
;;c:\tools\ag --ignore Help --ignore build %* | python %~dp0/vj-ag-filter.py
(defun vj-git-ag (word)
(interactive (list (read-string "Search Term: ")))
(vj-ag (vj-git-root) word))
(defun vj-ag-compilation-mode-finish (buf status)
"Remove common path component (default-directory) in \'*vj sg*\' compilation buffer
Usage:(add-hook 'compilation-finish-functions 'vj-ag-compilation-mode-finish)"
(interactive)
(when (equal (buffer-name buf) "*vj ag*")
(face-remap-add-relative 'compilation-error
:foreground "green2")))
;; Not working : selecting file stops working
;; (save-excursion
;; (set-buffer buf)
;; (goto-char (point-min))
;; (while (re-search-forward
;; (replace-regexp-in-string "[/\\]" "." default-directory)
;; nil t)
;; (delete-region (point-at-bol) (point))
;; (insert "•")))
;; maybe use ;; (progn (add-text-properties (match-beginning 0) (match-end 0) '(invisible t)) 'bold))))
;; ------------------------------------------------------------
(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 '(
beacon
company
company-quickhelp
company-statistics
csharp-mode
flycheck
git-gutter
gitconfig-mode
gitignore-mode
guide-key
helm
helm-git-grep
helm-org-rifle
helm-w32-launcher
helm-xref
htmlize
iedit
magit
perfect-margin
powershell
pyim
;; stack overflow: sos sx
avy ;; In site-lisp
;; all-the-icons-dired dumb-jump elfeed elfeed-score elpher elpy fireplace
dap-mode
edit-indirect
flymake-eslint
json-mode
lsp-mode
lsp-ui
markdown-mode
nodejs-repl
plantuml-mode
prettier
rjsx-mode
tree-sitter
treemacs-icons-dired
vscode-dark-plus-theme
yasnippet
))
(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)))))
;; ------------------------------------------------------------
(defun vjo-clear-buffer ()
(interactive)
(mark-whole-buffer)
(kill-region (point-min) (point-max))
(let ((buffer-modified-p nil)) ;; ignore changes
(save-buffer)))
(defun vj-path-component (dir)
(if (file-exists-p dir)
(concat dir path-separator)
;; else
nil))
(defun vj-add-to-path (add-to-exec-path dirs)
;; 2022 Dec
(if add-to-exec-path
(dolist (dir dirs)
(add-to-list 'exec-path dir)))
(setenv "PATH"
(concat
(apply 'concat (mapcar (lambda (dir) (vj-path-component dir)) dirs))
(getenv "PATH"))))
(defun vj-open-files (days lst)
"Opens log files in LST newer than DAYS in different windows"
(interactive)
(delete-other-windows)
(dolist (fn lst)
(when (< (/ (vps-file-last-modified-duration fn) (* 24 60 60)) days) ;; in days
(find-file fn)
(split-window-vertically)
(other-window 1)))
(delete-window)
(balance-windows))
(defun vj-org-screenshot ()
"Take a screenshot into a time stamped unique-named file in the
same directory as the org-buffer and insert a link to this file."
(interactive)
(setq filename
(concat
(make-temp-name
(concat (buffer-file-name) "_"
(read-string "Context:" "") "_"
(format-time-string "%Y%m%d_%H%M%S_")))
".png"))
;; (shell-command "snippingtool /clip")
(shell-command (concat "powershell -command \"Add-Type -AssemblyName System.Windows.Forms;if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {$image = [System.Windows.Forms.Clipboard]::GetImage();[System.Drawing.Bitmap]$image.Save('" filename "',[System.Drawing.Imaging.ImageFormat]::Png); Write-Output 'clipboard content saved as file'} else {Write-Output 'clipboard does not contain image data'}\""))
(insert (concat "[[file:" filename "]]"))
(org-display-inline-images))
(global-set-key "\C-cs" 'vj-org-screenshot)
(defun vj-goto-location (other-window filename lineno &optional column)
(if other-window
(find-file-other-window filename)
(find-file filename))
(goto-line (if (stringp lineno) (string-to-number lineno) lineno))
(when column
(beginning-of-line)
(forward-char (if (stringp column) (string-to-number column) column)))
(pulse-momentary-highlight-region (point-at-bol) (point-at-eol)))
(defun vj-next-file-by-extension ()
"Toggle between files with same basename but different extension."
(interactive)
(if (buffer-file-name)
(let (files prefix index next
(base (file-name-nondirectory (buffer-file-name))))
(setq prefix (car (split-string base "\\.")))
(setq files (directory-files "." nil (concat "^" prefix "\\.")))
(dotimes (i (length files))
(if (equal (nth i files) base)
(setq index i)))
(setq next (mod (+ 1 index ) (length files)))
(find-file (nth next files)))))