-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht-defuns.el
1383 lines (1211 loc) · 47.3 KB
/
t-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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; t-defuns.el -*- lexical-binding: t; -*-
;;; t-defuns.el --- more or less useful functions
;;; Commentary:
;;; Code:
(defun t/exec-org-block (name vars)
"Excecute `NAME'd org block."
(save-excursion
(goto-char (org-babel-find-named-block name))
(org-babel-execute-src-block nil
(org-babel-lob-get-info)
(seq-map (lambda (var)
(cons :var var))
vars))))
(defun t/shell-command-to-string (cmd)
"Run `CMD' as shell command, trim trailing newlines."
(t/trim-final-newline (shell-command-to-string cmd)))
(defun t/async-shell-command (name cmd &optional fn)
"Execute `CMD' async, call `FN' with the result string."
(lexical-let*
((fn fn)
(buf-name (format "*%s*" name))
(_ (when (get-buffer-create buf-name)
(with-current-buffer buf-name (erase-buffer))))
(p (start-process-shell-command name buf-name cmd)))
(prog1 p
(when fn
(set-process-sentinel p (lambda (process event)
(with-current-buffer buf-name
(let ((res (buffer-substring-no-properties (point-min) (point-max))))
(apply fn (list process
event
(split-string res "\r?\n")))))))))))
(defun t/evil-ex-define-cmd-local (cmd function)
"Locally binds the function FUNCTION to the command CMD."
(unless (local-variable-p 'evil-ex-commands)
(setq-local evil-ex-commands (copy-alist evil-ex-commands)))
(evil-ex-define-cmd cmd function))
(defun t/css-kill-value ()
"kills the attribute of a css property."
(interactive)
(let ((pos (point)))
(move-beginning-of-line 1)
(if (search-forward-regexp ": ?")
(progn
(when (not (looking-at-p ";"))
(kill-word 1)
(just-one-space)))
(move-to-column pos))))
(defmacro t/rename-modeline (package-name mode new-name)
"per package modeline rename for mode."
`(eval-after-load ,package-name
'(defadvice ,mode (after t/rename-modeline activate)
(setq mode-name ,new-name))))
(defun t/json-format ()
"pretty prints json in selected region."
(interactive)
(save-excursion
(shell-command-on-region (mark) (point) "python -m json.tool" (buffer-name) t)))
(defun t/build-tags ()
"Build ctags file for projectile project, call load-tags when done.
Effectively runs this for the current git project, e.g:
ctags -e -R --options=~/.emacs.d/ctags -f ~/.emacs.d/TAGS ~/.emacs.d/
The same can be done for the current folder only to place a TAGS file in it:
ctags -e -R .
Remember to build Emacs --without-ctags and use the one from `brew' instead,
it's the one with the correct options needed to generate ctags that Emacs
understands."
(interactive)
(message "building project tags..")
(lexical-let* ; so lambdas create closures
(;; (ctags (expand-file-name "~/.emacs.d/ctags"))
(root (projectile-project-root))
(tags (shell-quote-argument (concat root "TAGS")))
(process (start-process-shell-command "build ctags asynchronously"
"*ctags async*"
(concat
"ctags -e -R" ; recurse
" --options=" ctags ; use global config
" -f " tags " " ; put it in project/TAGS
" ." ; in the current directory
))))
(set-process-sentinel process (lambda (process event)
(t/load-tags tags)))))
(defun t/load-tags (tags)
"Loads project tags into tag table."
(message "loading project tags..")
(visit-tags-table tags)
(message "project tags loaded"))
(defun t/find-tag-at-point ()
"Go to tag at point, builds and/or load project TAGS file first."
(interactive)
(let* ((root (projectile-project-root))
(tags (shell-quote-argument (concat root "TAGS"))))
(if (file-exists-p tags) (t/load-tags tags) (t/build-tags))
(when (find-tag-default)
(etags-select-find-tag-at-point))))
(defun t/copy-to-clipboard (text &optional push)
"Copy text to os clipboard. Cygwin uses cygutils-extra's `putclip`. Mac uses builtin pbcopy."
(let* ((process-connection-type nil)
(copy-cmd (if is-mac "pbcopy" "putclip"))
(proc (start-process copy-cmd "*Messages*" copy-cmd)))
(process-send-string proc text)
(process-send-eof proc))
text)
(defun t/open-in-desktop ()
"Show current file in desktop (OS's file manager).
URL `http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html'
Version 2015-06-12"
(interactive)
(cond
(is-win (w32-shell-execute "explore" (replace-regexp-in-string "/" "\\" default-directory t t)))
(is-cygwin (shell-command (concat "cygstart " (shell-quote-argument default-directory))))
(is-mac (shell-command "open ."))
(is-linux (shell-command "thunar ."))))
(defun t/hippie-expand-no-case-fold ()
(interactive)
(let ((case-fold-search nil))
(hippie-expand nil)))
(defun t/hippie-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list '(try-expand-line-all-buffers)))
(end-of-line)
(hippie-expand nil)))
(defun t/untabify-buffer ()
"Remove tabs in buffer."
(interactive)
(untabify (point-min) (point-max)))
(defun t/indent-buffer ()
"Correctly indent a buffer."
(interactive)
(indent-region (point-min) (point-max)))
(defun t/cleanup-buffer-whitespace-and-indent ()
"Perform a bunch of operations on the whitespace content of a buffer.
Including indent-buffer, which should not be called automatically on save."
(interactive)
(t/untabify-buffer)
(when (fboundp 'whitespace-cleanup) (whitespace-cleanup))
(t/indent-buffer))
(defun t/eval-region-or-last-sexp ()
(interactive)
(if (region-active-p)
(call-interactively 'eval-region)
(call-interactively 'eval-last-sexp)))
(defun t/eval-and-replace ()
"Evaluate and replace the preceding sexp with its value."
(interactive)
(if (eq major-mode 'clojure-mode)
(cider-eval-last-sexp-and-replace)
(progn
(backward-kill-sexp)
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(current-buffer))
(error (message "Invalid expression")
(insert (current-kill 0)))))))
;; run it with M-x lorem
(define-skeleton lorem
"Insert a lorem ipsum."
nil
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
(defun t/smart-beginning-of-line ()
"Move point to first non-whitespace character or beginning-of-line."
(interactive "^")
(let ((oldpos (point)))
(back-to-indentation)
(and (= oldpos (point))
(beginning-of-line))))
(defun t/delete-frame-or-hide-last-remaining-frame ()
"Delete the selected frame. If the last one, hide it instead."
(interactive)
(condition-case nil
(delete-frame)
(error (if (window-system)
(when is-mac (ns-do-hide-emacs))
"in terminal, use c-z instead"))))
(defun t/delete-window-or-frame-or-hide ()
"Delete selected window or frame.
If its the last window in the frame, delete the frame. If its the last frame,
hide it (on mac)."
(interactive)
(condition-case err
(delete-window)
(error
(if (length= (visible-frame-list) 1)
(if is-mac
(ns-do-hide-emacs)
;; last frame on linux
(error (save-buffers-kill-emacs)))
(condition-case errr
(delete-frame)
(error (save-buffers-kill-emacs)))))))
(defun t/copy-buffer-file-name ()
"Copy the current buffer file name into the kill ring."
(interactive)
(let ((filename (file-name-nondirectory (buffer-file-name))))
(when filename
(kill-new filename)
(message "Copied buffer file name '%s'" filename))))
(defun t/copy-buffer-file-path ()
"Copy the full path to the current file into the kill ring."
(interactive)
(let ((filename (file-name-directory (buffer-file-name))))
(when filename
(kill-new filename)
(message "Copied buffer file path '%s'" filename))))
(defun t/previous-window ()
"Skip back to previous window."
(interactive)
(call-interactively 'other-window))
(defun t/buffer-mode (&optional buffer-or-string)
"Returns the major mode associated with a buffer."
(interactive)
(let ((mode (with-current-buffer (or buffer-or-string (current-buffer))
major-mode)))
(message "major mode: %s" mode)
mode))
(defun t/split-window-right-and-move-there-dammit ()
(interactive)
(split-window-right)
(windmove-right))
(defun t/split-window-below-and-move-there-dammit ()
(interactive)
(split-window-below)
(windmove-down))
(defun t/face-color-b (&optional face)
"Get `:background' color of `FACE'."
(interactive)
(face-attribute (or face (face-at-point)) :background))
(defun t/face-color-f (&optional face)
"Get `:foreground' color of `FACE'."
(interactive)
(face-attribute (or face (face-at-point)) :foreground))
(defun server-remove-kill-buffer-hook ()
;; remove other clients-has-the-file-open-prompt
(remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function))
(defun t/prefix-arg-universal? ()
"Check if the function was called with the c-u universal prefix."
(equal '(4) current-prefix-arg))
(defun t/comint-clear-buffer ()
(interactive)
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer)))
(defun t/uniquify-lines ()
"Remove duplicate adjacent lines in region or current buffer."
(interactive)
(save-excursion
(save-restriction
(let ((beg (if (region-active-p) (region-beginning) (point-min)))
(end (if (region-active-p) (region-end) (point-max))))
(goto-char beg)
(while (re-search-forward "^\\(.*\n\\)\\1+" end t)
(replace-match "\\1"))))))
(defun t/sort-lines ()
"Sort lines in region or current buffer."
(interactive)
(let ((beg (if (region-active-p) (region-beginning) (point-min)))
(end (if (region-active-p) (region-end) (point-max))))
(sort-lines nil beg end)))
(defun t/find-xml-path ()
"Display the hierarchy of XML elements the point is on as a path."
(interactive)
(let ((path nil))
(save-excursion
(save-restriction
(widen)
(while (and (< (point-min) (point)) ;; Doesn't error if point is at beginning of buffer
(condition-case nil
(progn
(nxml-backward-up-element) ; always returns nil
t)
(error nil)))
(setq path (cons (xmltok-start-tag-local-name) path)))
(if (called-interactively-p)
(message "/%s" (mapconcat 'identity path "/"))
(format "/%s" (mapconcat 'identity path "/")))))))
(defun t/date-time ()
"Insert current date-time string in full ISO 8601 format.
Example: 2010-11-29T23:23:35-08:00"
(concat
(format-time-string "%Y-%m-%dT%T")
((lambda (x) (concat (substring x 0 3) ":" (substring x 3 5)))
(format-time-string "%z"))))
(defun t/date-time-for-filename ()
"Return date-time iso 8601 string suitable for filename."
(replace-regexp-in-string ":" "." (t/date-time)))
(defun t/find-org-files-recursively (&optional directory filext)
"Return .org and .org_archive files recursively from DIRECTORY.
If FILEXT is provided, return files with extension FILEXT instead."
(interactive "DDirectory: ")
(let* (org-file-list
(case-fold-search t) ; filesystems are case sensitive
(file-name-regex "^[^.#].*") ; exclude dot, autosave, and backup files
(filext (or filext "org$\\\|org_archive$\\\|\\.txt$"))
(fileregex (format "%s\\.\\(%s\\)" file-name-regex filext))
(cur-dir-list (directory-files directory t file-name-regex)))
;; loop over directory listing
(dolist (file-or-dir cur-dir-list org-file-list) ; returns org-file-list
(cond
((file-regular-p file-or-dir) ; regular files
(if (string-match fileregex file-or-dir) ; org files
(push file-or-dir org-file-list)))
((file-directory-p file-or-dir)
(dolist (org-file (t/find-org-files-recursively file-or-dir filext)
org-file-list) ; add files found to result
(push org-file org-file-list)))))))
(defun t/org-fix-inline-images ()
"Fix redisplaying images after executing org babel code."
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (eq 'org-mode major-mode)
(org-redisplay-inline-images)))))
(defun t/project-root ()
"Get project root without throwing."
(interactive)
(let (projectile-require-project-root strict-p)
(or (projectile-project-root)
default-directory)))
(defun t/volatile-kill-buffer ()
"Kill current buffer unconditionally."
(interactive)
(setq-local kill-buffer-query-functions
(remq 'process-kill-buffer-query-function
kill-buffer-query-functions))
(set-buffer-modified-p nil)
(kill-this-buffer))
(defun t/volatile-kill-buffer-and-window ()
"Kill current buffer and the window unconditionally."
(interactive)
(if (and
(= (bol) (eol))
(= (save-excursion (goto-char (point-max)))
(point)))
(progn
(setq-local kill-buffer-query-functions
(remq 'process-kill-buffer-query-function
kill-buffer-query-functions))
(set-buffer-modified-p nil)
(kill-buffer-and-window))
(when (and
(not (called-interactively-p))
(eq evil-state 'insert)) (delete-char 1))))
(defun t/remove-newlines (str)
(replace-regexp-in-string "[\r\n]+" "" str))
(defun t/grab-url ()
(interactive)
(let* ((browser 'ff)
(fn (if (called-interactively-p) 'message 'identity))
(url (cond
((eq browser 'ff) (t/grab-firefox-url))
((eq browser 'chrome) (t/grab-chrome-url)))))
(funcall fn url)))
(defun t/insert-url ()
(interactive)
(insert (t/grab-url)))
(defun t/grab-chrome-url ()
"Grab the frontmost url out of chrome using `org-mac-grab-link'."
(interactive)
(t/remove-newlines
(shell-command-to-string
"osascript -l JavaScript -e 'Application(\"Google Chrome\").windows.at(0).activeTab.url()'")))
(defun t/grab-firefox-url (&optional browser)
"Grab the frontmost url of firefox using `t/osascript-activate'. Does not work
with browser in full screen."
(interactive)
(let ((browser (or browser "Firefox Developer Edition")))
(t/osascript-activate browser)
(t/async-shell-command "wait-bring-back-emacs" "sleep 0.2 && osascript -e 'tell application \"Emacs\" to activate'")
(sit-for 0.2)
(t/remove-newlines
(t/run-osascript
(concat
"tell application \"System Events\" to get value of UI element 1 of combo box 1 of toolbar \"Navigation\" of first group of front window of application process \"" browser "\"")))))
(defun t/browse-firefox-url-in-eww ()
"Open the frontmost firefox url in `eww'."
(interactive)
(eww (t/grab-firefox-url)))
(defun t/browse-chrome-url-in-eww ()
"Open the frontmost chrome url in `eww'."
(interactive)
(eww (t/grab-chrome-url)))
(defun t/get-url (url)
"Get URL synchronously."
(with-current-buffer (url-retrieve-synchronously url)
(buffer-string)))
(defun t/fetch (url)
"Insert URL contents in current buffer.
Drops headers and 2x empty lines before content."
(interactive "sfetch url:")
(insert (t/get-url url))
(goto-char (point-min))
(kill-sentence)
(kill-line)
(kill-line))
(defun t/fetch-firefox-url ()
"Insert contents of frontmost url of firefox in buffer."
(interactive)
(t/fetch (t/grab-firefox-url)))
(defun t/fetch-chrome-url ()
"Insert contents of frontmost url of chrome in buffer."
(interactive)
(t/fetch (t/grab-chrome-url)))
(defun t/browse-url-at-point ()
"Open url at `point' from everywhere.
Prefix arg will force eww."
(interactive)
(let ((browse-url-browser-function
(if (t/prefix-arg-universal?)
'eww-browse-url
'browse-url-default-browser)))
(cond ((equal major-mode 'eww-mode) (eww-browse-with-external-browser))
((equal major-mode 'elfeed-search-mode) (elfeed-search-browse-url))
((equal major-mode 'elfeed-show-mode) (browse-url (elfeed-entry-link elfeed-show-entry)))
((equal major-mode 'gnus-summary-mode) (gnus-summary-browse-url))
((equal major-mode 'mu4e-headers-mode) (browse-url (shr-url-at-point nil)))
((equal major-mode 'mu4e-view-mode) (browse-url (shr-url-at-point nil)))
((equal major-mode 'magit-status-mode) (call-interactively 'forge-browse-dwim))
((equal major-mode 'hackernews-mode) (hackernews-browse-url-action (button-at (point))))
((equal major-mode 'org-mode) (org-open-at-point))
(t (call-interactively 'browse-url-at-point)))))
(defun t/open-elfeed-in-eww ()
(interactive)
(eww (elfeed-entry-link elfeed-show-entry)))
(defun t/last-weekday-of-month-p ()
(let* ((day-of-week (calendar-day-of-week date))
(month (calendar-extract-month date))
(year (calendar-extract-year date))
(last-month-day (calendar-last-day-of-month month year))
(month-day (cadr date)))
(or
;; it's the last day of the month & it is a weekday
(and (eq month-day last-month-day)
(memq day-of-week '(1 2 3 4 5)))
;; it's a friday, and it's the last-but-one or last-but-two day
;; of the month
(and (eq day-of-week 5)
(or (eq month-day (1- last-month-day))
(eq month-day (1- (1- last-month-day))))))))
(defun t/face-at-point (pos)
"Echo the face at POS point."
(interactive "d")
(let ((face (or (get-char-property (point) 'read-face-name)
(get-char-property (point) 'face))))
(if face (message "Face: %s" face) (message "No face at %d" pos))))
(defun t/font-lock-test-faces ()
"Outputs test strings with all font lock faces to show colors."
(interactive)
(let ((b (get-buffer-create "*t/font-lock-test-faces*")))
(with-current-buffer b
(dolist (face '(font-lock-warning-face
font-lock-function-name-face
font-lock-variable-name-face
font-lock-keyword-face
font-lock-comment-face
font-lock-comment-delimiter-face
font-lock-type-face
font-lock-constant-face
font-lock-builtin-face
font-lock-preprocessor-face
font-lock-string-face
font-lock-doc-face
font-lock-negation-char-face))
(let ((current-string (concat "\n" (symbol-name face)))
(is-font-lock font-lock-mode))
(when is-font-lock (font-lock-mode 0))
(put-text-property 1 (length current-string) 'face face current-string)
(insert current-string)
(when is-font-lock (font-lock-mode 1))) t))
(popwin:display-buffer b t)))
(defun t/run-osascript (s &optional discard-output)
"Run applescript."
(let ((b-stderr "*osascript-err*")
(b-stdout "*osascript*"))
(t/kill-buffer b-stderr)
(t/kill-buffer b-stdout)
(with-current-buffer (get-buffer-create b-stdout)
(shell-command (format "/usr/bin/osascript -e '%s'" s) b-stdout (get-buffer-create b-stderr))
(buffer-string))))
(defun t/kill-buffer (b)
(when (get-buffer b) (kill-buffer b)))
(defun t/osascript-activate (app)
"Run applescript to activate application."
(t/run-osascript (format "tell application \"%s\" to activate" app) t))
(defun t/osascript-show-url (url)
(t/run-osascript
(format "tell application \"Google Chrome\" to set URL of active tab of window 1 to \"%s\"" url)))
(defun t/open-in-intellij ()
"Opens current file in IntelliJ IDEA."
(interactive)
(async-shell-command
(let* ((cmd "/Applications/IntelliJ\\ IDEA.app/Contents/MacOS/idea %s")
(args " --line %d --column %d %s")
(root (t/project-root))
(file-name (buffer-file-name)))
(if file-name
(format (concat cmd args)
root
(line-number-at-pos)
(current-column)
(shell-quote-argument file-name))
(format cmd root))))
(t/osascript-activate "IntelliJ IDEA"))
(defun t/propertize-read-only (str)
(propertize str
'read-only t
'front-sticky 'read-only
'rear-nonsticky 'read-only))
(defun t/strip-text-properties (txt)
(with-temp-buffer
(insert txt)
(buffer-substring-no-properties (point-min) (point-max))))
(defun t/get-file-string (file-path)
"Return `FILE-PATH's file content."
(with-temp-buffer
(insert-file-contents file-path)
(buffer-string)))
(defun t/re-seq (regexp string)
"Get a list of all REGEXP matches (from the first group) in a STRING."
(save-match-data
(let ((pos 0)
matches)
(while (string-match regexp string pos)
(push (match-string 1 string) matches)
(setq pos (match-end 0)))
matches)))
(defun t/re-seq-in-file (regex file)
(t/re-seq regex (t/get-file-string (t/user-emacs-file file))))
(defun t/buffer-finished-p (b)
"Return non-nil value if the buffer B has an ended process."
(string-match-p
"^Process .* \\(finished\\|exited\\).*$"
(car (last
(split-string
(with-current-buffer b
(let ((len (length (buffer-string))))
(if (> len 0)
(buffer-substring-no-properties 1 len)
"")))
"\n")))))
(defun t/term-quit-if-finished (&optional else)
(interactive)
(if (t/buffer-finished-p (current-buffer))
(t/volatile-kill-buffer)
(when (functionp else)
(call-interactively else))))
(defun t/term-kill-if-finished (&optional else)
(interactive)
(if (t/buffer-finished-p (current-buffer))
(t/volatile-kill-buffer-and-window)
(when (functionp else)
(call-interactively else))))
(defun t/newline-expand-braces ()
"Newline like `evil-ret', but expand (), [] and {} with newline in between,
and indent accordingly."
(interactive)
(cond
((or (and (looking-back "(") (looking-at ")"))
(and (looking-back "\\[") (looking-at "\\]"))
(and (looking-back "{") (looking-at "}")))
(progn
(save-excursion (indent-according-to-mode))
(evil-ret)
(save-excursion (indent-according-to-mode))
(t/open-line-above)))
(t (progn
(evil-ret)
(indent-according-to-mode)))))
(defun t/unbind (fn-or-s)
"Unbind function or symbol depending on type."
(interactive)
(cond ((fboundp fn-or-s) (fmakunbound fn-or-s))
((symbolp fn-or-s) (makunbound fn-or-s))
(t (message "Can't unbind %s, not a fboundp or symbolp" fn-or-s))))
(defun t/add-to-list (l item-or-items)
"Adds items to the list `L'."
(if (listp item-or-items)
(dolist (item item-or-items) (add-to-list l item t))
(add-to-list l item-or-items))
l)
(defun t/highlight-logging ()
"Add log highlighting to current major mode."
(interactive)
(defvar t-log-debug-face 'doom-modeline-buffer-path)
(defvar t-log-info-face 'doom-modeline-info)
(defvar t-log-warn-face 'doom-modeline-warning)
(defvar t-log-error-face 'doom-modeline-urgent)
(defvar t-log-comment-face 'doom-modeline-eldoc-bar)
(defvar t-log-keyword-face 'font-lock-keyword-face)
(defvar t-log-hl-face 'org-code)
(font-lock-add-keywords
nil
`(
(,(rx (and "[" (group (one-or-more (or alnum "-"))) "]")) 1 t-log-keyword-face t) ; [module]
(,(rx (group (or "debug" "DEBUG"))) 1 t-log-debug-face t) ; [DEBUG]
(,(rx (group (or "info" "INFO" "SUCCESS"))) 1 t-log-info-face t) ; [INFO]
(,(rx (group (or "warning" "warn" "WARNING" "WARN" "SKIPPED"))) 1 t-log-warn-face t) ; [WARN] [WARNING]
(,(rx (group (or "error" "ERROR" "FAILURE" "Caused by:"))) 1 t-log-error-face t) ; [ERROR]
;; 2017-07-11T14:45:11.067+02
;; 2017-07-11T14:45:11.067+02:00
(,(rx (group (repeat 4 digit) "-" (repeat 2 digit) "-" (repeat 2 digit)
(or " " "T")
(repeat 2 digit) ":" (repeat 2 digit) ":" (repeat 2 digit)
(optional (or "," ".") (repeat 3 digit))
(optional (or "Z"
(and "+"
(repeat 2 digit)
(optional ":" (repeat 2 digit))))))) 1 t-log-hl-face t)
;; 14:15
;; 14:15:11
;; 14:15.11
(,(rx space
(group (repeat 2 digit)
":" (repeat 2 digit)
(optional (or ":" ".") (repeat 2 digit)))) 1 t-log-hl-face t)
;; :modules
;; :some:shadowJar
;; :some:modu-le:shadowJar
(,(rx (group (>= 0 (and ":" (one-or-more (or alpha "-")))))) 1 t-log-keyword-face t)
;; com.some.domain
(,(rx (group (one-or-more letter)
(>= 2 (and "." (one-or-more (or digit letter "$"))))
(optional "@" (one-or-more alnum)))) 1 t-log-comment-face t)
;; Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
(,(rx (and (zero-or-more (any ".")) "Tests run: " (group digit (zero-or-more digit)) (zero-or-more (any ".")))) 1 t-log-info-face t)
(,(rx (and (zero-or-more (any ".")) "Failures: " (group digit (zero-or-more digit)) (zero-or-more (any ".")))) 1 t-log-warn-face t)
(,(rx (and (zero-or-more (any ".")) "Errors: " (group digit (zero-or-more digit)) (zero-or-more (any ".")))) 1 t-log-error-face t)
(,(rx (and (zero-or-more (any ".")) "Skipped: " (group digit (zero-or-more digit)) (zero-or-more (any ".")))) 1 t-log-warn-face t)
;; 0.662 s, 2s, 30m, 10 min, 4h, 20 minutes, 1hour, 20 hours
(,(rx (group (one-or-more digit)
(optional (or "," "." ":") (one-or-more digit))
(optional " ")
(or (or "ms" "millisecond" "milliseconds")
(or "s" "sec" "second" "seconds")
(or "m" "min" "minute" "minutes")
(or "h" "hour" "hours")))
word-boundary) 1 t-log-keyword-face t)
;; 1.2, 2,3, 3,33, 4.8, 4.2
(,(rx space
(group (one-or-more digit)
(or ?, ?.)
(one-or-more digit))
word-boundary) 1 t-log-keyword-face t)
;; 1G, 2M, 3K, 4B, 5% wow
;; 1.2G, 2,3M, 3,33K, 4.8B, 4.2%, 1G wow
(,(rx (group (one-or-more digit)
(optional (or ?, ?.) (one-or-more digit))
(in ?% ?G ?M ?K ?B))
word-boundary) 1 t-log-info-face t)))
(if (fboundp 'font-lock-flush)
(font-lock-flush)
(when font-lock-mode
(with-no-warnings (font-lock-fontify-buffer)))))
(defun t/clone (repo)
"Clone a github REPO to `~/Projects/<repo-name>'."
(interactive "sClone repository: ")
(require 'magit)
(when-let* ((repo-name (or (and (s-starts-with? "https://github." repo)
(replace-regexp-in-string "https://github\\.\\(?:com\\|dev\\)/\\([^/]+\\)/\\([^/]+\\)/?.*?$" "\\1/\\2" repo))
(and (s-ends-with? ".git" repo)
(replace-regexp-in-string "git@github.com:\\(.+\\).git" "\\1" repo)))))
(lexical-let*
((dir (expand-file-name (format "~/Projects/%s/" repo-name)))
(cmd (concat "git " "clone " (format "git@github.com:%s.git" repo-name)))
(msg (concat "Cloning " repo-name ".. ok.")))
(if (file-exists-p dir)
(dired dir)
(progn
(message "Cloning %s.." repo repo-name)
(t/async-shell-command (format "*git-clone %s*" repo-name)
(concat cmd " " (magit-convert-filename-for-git dir))
(lambda (&optional &rest args)
(dired dir)
(message msg))))))))
(defun t/visit-git-link-pulls ()
"Navigate to /pulls for the current git repo."
(interactive)
(let* ((origin (magit-get
"remote"
(or (magit-get-remote "main")
(magit-get-remote "master")) "url"))
(url (replace-regexp-in-string
".+\\.com:\\(.+\\)\\.git"
"\\1"
origin)))
(browse-url
(format "https://www.github.com/%s/pulls" url))))
(defun t/projectile-dired ()
(interactive)
(let ((projects (projectile-load-known-projects)))
(if projects
(projectile-completing-read
"dired in project: "
projects
:action 'dired)
(user-error "No projects found"))))
(defun t/projectile-magit-status ()
(interactive)
(let ((projects (projectile-load-known-projects)))
;; TODO
;; (autoload-do-load 'my-function 'my-file nil t)
(if projects
(projectile-completing-read
"magit status in project: "
projects
:action (lambda (project)
(let ((default-directory project))
(magit-status project))))
(user-error "No projects found"))))
(defun t/projectile-rg ()
(interactive)
(let ((projects (projectile-load-known-projects)))
(if projects
(projectile-completing-read
"rg in project: "
projects
:action (lambda (project)
(let ((default-directory project))
(cond
((fboundp '+default/search-project) (+default/search-project))
((fboundp 'counsel-projectile-rg) (counsel-projectile-rg))))))
(user-error "No projects found"))))
(defun t/projectile-visit-git-link-pulls ()
(interactive)
(let ((projects (projectile-load-known-projects)))
(if projects
(projectile-completing-read
"Visit pulls for project: "
projects
:action (lambda (project)
(let* ((default-directory (expand-file-name project)))
(t/visit-git-link-pulls))))
(user-error "No projects found"))))
(defun t/visit-git-link-pulls ()
"Navigate to /pulls for the current git repo."
(interactive)
(let* ((origin (magit-get
"remote"
(or (magit-get-remote "main")
(magit-get-remote "master")) "url"))
(url (replace-regexp-in-string
".+\\.com:\\(.+\\)\\.git"
"\\1"
origin)))
(browse-url
(format "https://www.github.com/%s/pulls" url))))
(defun t/in-all-windows (fn)
(interactive)
(dolist (w (window-list))
(funcall fn w)))
(defun t/in-all-buffers (fn)
(interactive)
(dolist (b (doom-buffer-list))
(with-current-buffer b
(funcall fn b))))
(defun t/set-all-window-margins (l r)
(dolist (w (window-list))
(set-window-margins w l r)))
(defvar *t-window-margins* 0)
(defun t/reset-window-margins (&optional m)
"Reset window margins"
(interactive)
(when m
(setq *t-window-margins* m))
(t/set-all-window-margins *t-window-margins*
*t-window-margins*))
(defun t/margins-global (&optional m)
"Set global window margins M."
(interactive (list (string-to-number (read-string "Width: " nil nil "0"))))
(let ((m (or m 0)))
(setq-default left-margin-width m
right-margin-width m)
(t/reset-window-margins m)
(add-hook 'window-setup-hook 't/reset-window-margins)
(add-hook 'window-configuration-change-hook 't/reset-window-margins)
(add-hook '+popup-buffer-mode-hook 't/reset-window-margins)))
(defun t/margins-global-decrease ()
"Decrease global window margins."
(interactive)
(let ((w (- left-margin-width 1)))
(when (> w 0)
(t/margins-global w))))
(defun t/margins-global-increase ()
"Increase global frame margins."
(interactive)
(let ((w (+ left-margin-width 1)))
(when (> w 0)
(t/margins-global w))))
(defun t/margins-local (l)
"Set buffer local window margin."
(interactive (list (string-to-number (read-string "Width: " nil nil "0"))))
(let ((l (or l 0)))
(setq-default left-margin-width l
right-margin-width l)
(set-window-margins (get-buffer-window (current-buffer)) l l)))
(defun t/eww-toggle-images ()
"Toggle whether images are loaded and reload the current page fro cache."
(interactive)
(setq-local shr-inhibit-images (not shr-inhibit-images))
(eww-reload t)
(message "Images are now %s"
(if shr-inhibit-images "off" "on")))
(defun t/last-used-window-buffer ()
"Switch to the window that displays the most recently selected buffer."
(interactive)
(let* ((buffers (delq (current-buffer) (buffer-list (selected-frame))))
(windows (delq (selected-window) (delq nil (mapcar #'get-buffer-window buffers)))))
(if windows
(select-window (car windows))
(message "no suitable window to switch to"))))
(defun t/word-at-point ()
"Return word under cursor."
(thing-at-point 'word t))
(defun t/ip ()
(interactive)
(let ((ip (-> "dig +short myip.opendns.com @resolver1.opendns.com" shell-command-to-string s-trim)))
(message ip)
(kill-new ip)))
(defun t/transparency (value)
"Sets the transparency of the frame window. 0=transparent/100=opaque."
(interactive (list
(string-to-number
(read-string (format "Opacity is %s. Enter a value from 0 - 100, or press enter for 100: "
(frame-parameter (selected-frame) 'alpha))
nil
nil
"100"))))
(set-frame-parameter (selected-frame) 'alpha value))
(defun t/clone-github-repo-from-chrome ()
"Clones the github repo of the currently visisble chrome tab."
(interactive)
(t/clone (t/grab-chrome-url)))
(defun t/ediff-use-both ()
(interactive)
(ediff-copy-diff ediff-current-difference nil 'C nil
(concat
(ediff-get-region-contents ediff-current-difference 'A ediff-control-buffer)
(ediff-get-region-contents ediff-current-difference 'B ediff-control-buffer))))
(defun t/lookup-key (key)
"Search for KEY in all known keymaps."
(mapatoms
(lambda (ob)
(when (and (boundp ob) (keymapp (symbol-value ob)))
(when (functionp (lookup-key (symbol-value ob) key))
(message "%S" ob))))
obarray))
(defun t/lookup-key-prefix (key)
"Search for KEY as prefix in all known keymaps."
(mapatoms
(lambda (ob)
(when (and (boundp ob) (keymapp (symbol-value ob)))
(when (let ((m (lookup-key (symbol-value ob) key)))
(and m (or (symbolp m) (keymapp m))))
(message "%S" ob))))
obarray))
(defun t/fns ()
"List functions."
(let ((l))
(mapatoms (lambda (a)
(when (functionp a)
(push a l))))
l))
(defun t/interactive-fns ()
"List interactive functions."
(-filter 'commandp
(t/fns)))
(defun t/locally-disable-cursor ()
"Locally disable cursor in buffer."
(interactive)
(make-local-variable 'cursor-type)
(setq-local cursor-type 'nil)
(setq-local evil-normal-state-cursor 'nil))
(defun t/locally-enable-cursor ()
"Enable locally disabled cursor in buffer."
(interactive)
(kill-local-variable cursor-type)
(setq-local evil-normal-state-cursor 'box))
(defun t/random-line ()
"Goto a random line in the buffer. Useful trying out a random package."
(interactive)
(push-mark)