-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.el
More file actions
executable file
·1303 lines (1092 loc) · 41.7 KB
/
Copy pathinit.el
File metadata and controls
executable file
·1303 lines (1092 loc) · 41.7 KB
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
;; package.el and MELPA setup
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/")
t)
(package-initialize)
;; Install use-package via package.el if missing
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
;; If there are no archived package contents, refresh them
(when (not package-archive-contents)
(package-refresh-contents))
(setq byte-compile-warnings '(cl-functions))
(require 'cl-lib)
;; spacemacs dark theme
(use-package spacemacs-theme
:ensure t
:config
(load-theme 'spacemacs-dark t))
;; spacemacs mode line
(use-package spaceline
:ensure t
:config
(require 'spaceline-config)
(spaceline-spacemacs-theme)
(spaceline-helm-mode 1))
;; have mode icons instead of names
;; (mode-icons-mode)
;; -------------------------------------------- org
;; org-bullets: prettier org headings
(use-package org-bullets
:ensure t
:hook (org-mode . org-bullets-mode))
;; Tell org-agenda which file to use
(setq org-agenda-files '("~/org/agenda.org"))
;; Custom faces for org-level fontsizes (keep as-is)
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(org-level-1 ((t (:inherit outline-1 :height 1.2))))
'(org-level-2 ((t (:inherit outline-2 :height 1.1)))))
;; Org TODO keywords and colors (keep as-is)
(setq org-todo-keywords
'((sequence "TODO(t!)" "IN-PROGRESS(i!)" "PROCESSING(p!)" "WAITING(w!)" "CANCELED(c!)" "DONE(d!)")))
(setq org-todo-keyword-faces
'(("IN-PROGRESS" . "orange")
("PROCESSING" . "orange")
("WAITING" . "orange")
("CANCELED" . "yellow")))
;; Keep logbook entries (C-c C-z) hidden in a logbook drawer
(setq org-log-into-drawer t)
(setq inhibit-startup-screen t)
;; Enable org-indent-mode in org buffers
(add-hook 'org-mode-hook #'org-indent-mode)
(setq org-log-done 'time)
;; Spell checking in org-mode
(use-package flyspell-correct-ivy
:ensure t
:demand t)
(add-hook 'org-mode-hook #'turn-on-flyspell)
(eval-after-load 'org
'(progn
;; Correct word keybinding
(define-key org-mode-map (kbd "C-c c") #'flyspell-correct-word-before-point)
;; Insert org link with title of page found in URL
(define-key org-mode-map (kbd "C-c C-i") 'org-cliplink)
;; Jump to org header (imenu)
(define-key org-mode-map (kbd "C-c i") 'imenu)
;; Ctrl-Up/Down changes priority if on a priority cookie
(define-key org-mode-map (kbd "C-<up>")
(lambda ()
(interactive)
(if (save-excursion
(beginning-of-line)
(re-search-forward org-priority-regexp (line-end-position) t)
(<= (match-beginning 0) (point))
(<= (point) (match-end 0)))
(org-priority-up)
(call-interactively 'org-metaup))))
(define-key org-mode-map (kbd "C-<down>")
(lambda ()
(interactive)
(if (save-excursion
(beginning-of-line)
(re-search-forward org-priority-regexp (line-end-position) t)
(<= (match-beginning 0) (point))
(<= (point) (match-end 0)))
(org-priority-down)
(call-interactively 'org-metadown))))
;; Archive subtree
(define-key org-mode-map (kbd "C-x a") #'org-archive-subtree)
))
;; Org agenda settings
(setq org-agenda-inhibit-startup t)
(global-set-key (kbd "C-c a") 'org-agenda)
(setq org-agenda-custom-commands
;; Keep your full custom commands as is
`(("A" "Daily agenda and top priority tasks"
((tags-todo "*"
((org-agenda-skip-function '(org-agenda-skip-if nil '(timestamp)))
(org-agenda-skip-function
`(org-agenda-skip-entry-if
'notregexp ,(format "\\[#%s\\]" (char-to-string org-priority-highest))))
(org-agenda-block-separator nil)
(org-agenda-overriding-header "Important tasks without a date\n")))
(agenda "" ((org-agenda-span 1)
(org-deadline-warning-days 0)
(org-agenda-block-separator nil)
(org-scheduled-past-days 0)
(org-agenda-day-face-function (lambda (date) 'org-agenda-date))
(org-agenda-format-date "%A %-e %B %Y")
(org-agenda-overriding-header "\nToday's agenda\n")))
;; ... other agenda blocks ...
))))
;; Fix windmove conflicts in org-mode by rebinding keys locally
(add-hook 'org-mode-hook
(lambda ()
(local-set-key (kbd "S-<right>") 'windmove-right)
(local-set-key (kbd "S-<left>") 'windmove-left)
(local-set-key (kbd "C-<up>") 'org-shiftup)
(local-set-key (kbd "S-<up>") 'windmove-up)
(local-set-key (kbd "C-<down>") 'org-shiftdown)
(local-set-key (kbd "S-<down>") 'windmove-down)))
;; Quick notes function
(defun open-notes ()
(interactive)
(let ((daily-name (format-time-string "%y%m%d_%H%M%S")))
(find-file (format "~/notes/notes_%s.org" daily-name))))
;; Move the current notes (usually opened with open-notes) to the
;; archived notes folder (a given note ends up either deleted or
;; archived)
(defun send-notes ()
(interactive)
(if (not (buffer-file-name))
(user-error "Buffer is not visiting a file")
(let* ((source-file (buffer-file-name))
(filename (file-name-nondirectory source-file))
(target-dir "~/archived_notes")
(target-file (concat target-dir filename)))
;; Ensure target directory exists
(unless (file-directory-p target-dir)
(make-directory target-dir t))
;; Check for conflicts to prevent data loss
(if (file-exists-p target-file)
(user-error "File %s already exists in target directory" filename)
(progn
;; 'rename-file' moves the file physically (delete from source)
(rename-file source-file target-file)
;; Update the buffer to point to the new location
(set-visited-file-name target-file)
(message "Note moved to %s" target-file))))))
;; org-alert setup
(use-package org-alert
:ensure t
:config
(setq alert-default-style 'libnotify
org-alert-interval 300
org-alert-notify-cutoff 30
org-alert-notify-after-event-cutoff 10)
(org-alert-enable))
;; Enable auto-fill mode in org buffers
(add-hook 'org-mode-hook #'auto-fill-mode)
(setq-default fill-column 70)
;; -------------------------------------------- rg
(use-package rg
:ensure t
:commands (rg rg-project rg-dwim)
:bind (("C-c s" . rg-menu)
:map rg-mode-map
("e" . wgrep-change-to-wgrep-mode)
("C-c C-c" . wgrep-finish-edit))
:init
(setq rg-group-result t
rg-ignore-case 'smart
rg-custom-type-aliases
'(("elisp" . "*.el")
("conf" . "*.conf *.cfg")))
:config
;; wgrep integration
(with-eval-after-load 'wgrep
(autoload 'wgrep-rg-setup "wgrep-rg")
(add-hook 'rg-mode-hook #'wgrep-rg-setup))
;; optional toggles
(rg-define-toggle "--multiline --multiline-dotall" "m")
(rg-define-toggle "--word-regexp" "w")
(rg-define-toggle "--files-with-matches" "L"))
;; override C-x f globally to run rg
(global-set-key (kbd "C-x f") #'rg)
;; -------------------------------------------- yaml
(use-package yaml-mode
:ensure t
:mode ("\\.yml\\'" . yaml-mode))
;; -------------------------------------------- json
(use-package json-mode
:ensure t
:mode ("\\.json\\'" . json-mode))
;; -------------------------------------------- tex
(use-package tex-site
:ensure auctex
:defer t
:hook
(LaTeX-mode . reftex-mode)
(LaTeX-mode . flyspell-mode)
(LaTeX-mode . turn-on-cdlatex)
(LaTeX-mode . auto-fill-mode)
(flyspell-mode . flyspell-buffer)
:config
;; set font size for the different section titles
(with-eval-after-load 'font-latex
(dolist (face '(font-latex-sectioning-0-face ; \part
font-latex-sectioning-1-face ; \chapter
font-latex-sectioning-2-face ; \section
font-latex-sectioning-3-face ; \subsection
font-latex-sectioning-4-face ; \subsubsection
font-latex-sectioning-5-face)) ; \paragraph
(when (facep face)
(set-face-attribute face nil :height 1.1))))
(setq reftex-plug-into-AUCTeX t)
(setq fill-column 70)
(setq ring-bell-function 'ignore)
;; Setup TeX view programs
(setq TeX-view-program-list
'(("Okular" ("output-pdf" "okular"))
("okular" ("okular" (mode-io-correlate " -p %(outpage)") "%o"))))
(setq TeX-view-program-selection
'(((output-dvi style-pstricks) "dvips and gv")
(output-dvi "xdvi")
(output-pdf "okular")
(output-html "xdg-open")))
;; Keybindings for LaTeX mode
(eval-after-load 'latex
'(progn
(define-key LaTeX-mode-map (kbd "C-c c") #'flyspell-correct-word-before-point)
(define-key LaTeX-mode-map (kbd "C-c t") #'get-synonyms)
(define-key LaTeX-mode-map (kbd "C-c C-p") #'citar-insert-citation)))
;; Enable TeX parsing
(setq TeX-parse-self t))
;; Cdlatex autoloads
(use-package cdlatex
:defer t)
;; citar setup for citations
(use-package citar
:ensure t
:custom
(citar-bibliography '("~/bib/references.bib"))
:hook ((LaTeX-mode . citar-capf-setup)
(org-mode . citar-capf-setup)))
;; prevent cdlatex from inserting sub/superscripts
(with-eval-after-load 'cdlatex
(define-key cdlatex-mode-map "_" nil)
(define-key cdlatex-mode-map "^" nil))
;; -------------------------------------------- useful global settings
;; restore the last saved desktop on startup
(desktop-save-mode 1)
;; remember recent files
(recentf-mode 1)
(setq recentf-max-menu-items 30)
(setq recentf-max-saved-items 30)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)
;; Save minibuffer prompts (M-n and M-p to access them)
(setq history-length 50)
(savehist-mode 1)
;; Remember and restore the last cursor location of opened files
(save-place-mode 1)
;; Revert Dired and other buffers
(setq global-auto-revert-non-file-buffers t)
;; hide menu-bar and tool-bar forever
(menu-bar-mode -1)
(tool-bar-mode -1)
;; show parentheses
(show-paren-mode 1)
;; enable icicle
;; (add-to-list 'load-path "~/.emacs.d/icicles")
;; (require 'icicles)
;;; format elisp code
;; (add-hook 'emacs-lisp-mode-hook (lambda ()
;; (add-hook 'after-save-hook
;; 'elisp-format-buffer)))
;; always reload files if they changed on disk
(setq global-auto-revert-mode t)
;; make Pg-Up Pg-Down return to the same point
(setq scroll-preserve-screen-position t)
;; start emacs as server
(server-start)
;; simple window traveling
(windmove-default-keybindings)
;; Default mode for unknown files
(setq default-major-mode 'text-mode)
;; Don’t compact font caches during GC.
(setq inhibit-compacting-font-caches t)
;; Jump multiple lines at once (efficient travelling)
(defun my/move-up-10-lines ()
"Move cursor up 10 lines."
(interactive)
(previous-line 10))
(defun my/move-down-10-lines ()
"Move cursor up 10 lines."
(interactive)
(next-line 10))
(global-set-key (kbd "C-d") 'my/move-up-10-lines)
(global-set-key (kbd "C-f") 'my/move-down-10-lines)
(global-set-key (kbd "C-c C-<up>") #'scroll-down-command)
(global-set-key (kbd "C-c C-<down>") #'scroll-up-command)
;; move to the middle of the current line
(defun my/move-to-middle ()
(interactive)
(let* ((begin (line-beginning-position))
(end (line-end-position))
(middle (/ (+ end begin) 2)))
(goto-char middle)))
(global-set-key (kbd "M-s") 'my/move-to-middle)
;; Revert buffers when the underlying file has changed
(global-auto-revert-mode 1)
;; revert but keep undo history
(defun revert-buffer-keep-undo (&rest -)
"Revert buffer but keep undo history."
(interactive)
(let ((inhibit-read-only t))
(erase-buffer)
(insert-file-contents (buffer-file-name))
(set-visited-file-modtime (visited-file-modtime))
(set-buffer-modified-p nil)))
(setq revert-buffer-function 'revert-buffer-keep-undo)
;; some upper limits on sizes
(setq max-lisp-eval-depth '40000)
(setq max-specpdl-size '100000)
(setq undo-limit 400000
undo-outer-limit 80000000
undo-strong-limit 1000000)
;; simple window switch
(windmove-default-keybindings)
;; auto bullet mode
(use-package org-bullets
:ensure t
:hook (org-mode . org-bullets-mode))
;; enable dumb jump (M-.)
(use-package dumb-jump
:ensure t
:hook (xref-backend-functions . dumb-jump-xref-activate))
;; show current match and number of matches
(use-package anzu
:ensure t
:config
(global-anzu-mode +1)
;; let spaceline take car of matches
(setq anzu-cons-mode-line-p nil))
;; activate csv-mode for csv and txt files
(use-package csv-mode
:ensure t
:mode ("\\.csv\\'" "\\.txt\\'")
:config
;; add some other common separators
(setq csv-separators '(";" "," "\t")))
;; give a DOI, get a bibtex entry (complementary to org-ref)
(defun get-bibtex-from-doi (doi)
"Get a BibTeX entry from the DOI"
(interactive "MDOI: ")
(let ((url-mime-accept-string "text/bibliography;style=bibtex"))
(with-current-buffer
(url-retrieve-synchronously
(format "http://dx.doi.org/%s"
(replace-regexp-in-string
"http://dx.doi.org/" "" doi)))
(switch-to-buffer (current-buffer))
(goto-char (point-max))
(setq bibtex-entry
(buffer-substring
(string-match "@" (buffer-string))
(point)))
(kill-buffer (current-buffer))))
(insert (decode-coding-string bibtex-entry 'utf-8))
(bibtex-fill-entry))
(global-set-key (kbd "C-c b") 'get-bibtex-from-doi)
;; write bibtex entry in bib from DOI
(global-set-key (kbd "C-c u") 'org-ref-url-html-to-bibtex)
;; run a shell command quickly
(global-set-key (kbd "C-c s") 'shell-command)
;; facilitate string replacement
(global-set-key (kbd "C-x r") 'replace-string)
;; git blame
(defun vc-msg-hook-setup (vcs-type commit-info)
;; copy commit id to clipboard
(message (format "%s\n%s\n%s\n%s"
(plist-get commit-info :id)
(plist-get commit-info :author)
(plist-get commit-info :author-time)
(plist-get commit-info :author-summary))))
(add-hook 'vc-msg-hook 'vc-msg-hook-setup)
;; show file VC historic
(global-set-key (kbd "C-x c") 'magit-log-buffer-file)
;; list pattern occurences in current buffer and go
(use-package loccur
:ensure t
:bind (("C-o" . loccur-isearch)))
;; show clipboard history (in buffer)
(global-set-key (kbd "M-y") 'helm-show-kill-ring)
;; loop over clipboard history (in minibuffer)
(global-set-key (kbd "C-t") 'yank-pop)
;; use helm mainly for pattern search
(use-package helm
:ensure t
:config
(setq helm-locate-command "locate %s -wAe --regex %s")
(setq helm-find-files-sort-directories t)
(setq helm-semantic-fuzzy-match t)
(setq helm-completion-in-region-fuzzy-match t)
(global-set-key (kbd "M-X") 'helm-M-x)
;; Function: copy full path from helm-find-files
(defun helm-find-files-copy-filename ()
"Copy the full path of the selected candidate in `helm-find-files`."
(interactive)
(let ((fname (helm-get-selection)))
(kill-new fname)
(message "Copied: %s" fname)))
;; Bind the key after load
(with-eval-after-load 'helm-files
(define-key helm-find-files-map (kbd "C-c C-k")
#'helm-find-files-copy-filename))
(defun my/right-window ()
"Return the rightmost window in the current frame."
(car (last (window-list))))
;; run helm-for-files in right window instead of minibuffer
(setq helm-display-function
(lambda (buf _)
(let ((win (my/right-window)))
(if (window-live-p win)
(set-window-buffer win buf)
(display-buffer-pop-up-window buf)))))
(global-set-key (kbd "C-x C-d") 'helm-for-files)
(global-set-key (kbd "M-y") 'helm-show-kill-ring))
;; use color-moccur from MELPA
(use-package color-moccur
:ensure t)
;; vertical completion in minibuffer
(use-package vertico
:ensure t
:config
(setq vertico-cycle t)
(setq vertico-resize nil)
(vertico-mode 1))
;; add useful annotations to completion candidates
(use-package marginalia
:ensure t
:config
(marginalia-mode 1))
;; use bufler to group buffers per project
(use-package bufler
:ensure t
:bind ("C-c C-b" . bufler-list))
;; updatedb settings for better helm locate
;; 0 * * * * updatedb -o ~/.cache/mydb.db -U $HOME
(setq locate-db-file "~/.cache/locate.db")
;; (completion-preview-mode 1)
(global-set-key (kbd "C-x C-d") 'helm-for-files)
(global-set-key (kbd "C-o") 'helm-occur)
(put 'scroll-left 'disabled nil)
(scroll-bar-mode -1)
(define-key override-global-map (kbd "C-,") #'beginning-of-buffer)
(define-key override-global-map (kbd "C-.") #'end-of-buffer)
;; -------------------------------------------- git
(use-package shell
:config
;; Make shell-command pick up .bashrc aliases
(setq shell-file-name "bash")
(setq shell-command-switch "-ic")
;; Easy git add, commit, and push
(defun push-all (comment)
(interactive "Mcomment:")
(save-some-buffers t) ;; save all buffers
(shell-command (format
"git add -A; git commit -a -m \" %s\"; git push &"
comment)))
(global-set-key (kbd "C-c g") #'push-all)
;; Pull in vterm from current project/repo root, default to merge
(defun git-pull-vterm ()
"Open a vterm in the root of the current Git repository and
run `git pull --no-rebase`."
(interactive)
(let* ((default-directory
;; Find the Git root of the current directory
(or (locate-dominating-file default-directory ".git")
default-directory))
(buffer-name "*git-pull*"))
;; Open or switch to vterm buffer
(if (get-buffer buffer-name)
(switch-to-buffer buffer-name)
(vterm buffer-name))
;; Ensure we're in the repo root
(cd default-directory)
;; Run git pull with --no-rebase
(vterm-send-string "git pull --no-rebase")
(vterm-send-return)))
;; Easy git add, commit, push after updating .gitignore cache
(defun update-gitignore (comment)
(interactive "Mcomment:")
(save-some-buffers t)
(shell-command (format
"git rm -r --cached .; git add -A; git commit -a -m \" %s\"; git push &"
comment)))
(global-set-key (kbd "C-c t") #'update-gitignore))
(use-package consult-gh
:ensure t
:after consult
:config
;; Add main GitHub account
(unless (boundp 'consult-gh-default-orgs-list)
(defvar consult-gh-default-orgs-list nil))
(unless (member "wehrad" consult-gh-default-orgs-list)
(add-to-list 'consult-gh-default-orgs-list "wehrad"))
;; Add all GitHub organizations to the default list
(setq consult-gh-default-orgs-list
(append consult-gh-default-orgs-list
(remove "" (split-string
(or (consult-gh--command-to-string "org" "list") "")
"\n"))))
;; Default clone directory
(setq consult-gh-default-clone-directory "~/")
;; Enable previews and highlighting
(setq consult-gh-show-preview t)
(setq consult-gh-highlight-matches t)
;; Preview key and buffer mode
(setq consult-gh-preview-key "M-o")
(setq consult-gh-preview-buffer-mode 'org-mode)
;; Code, file, and repo actions open in Emacs buffers
(setq consult-gh-code-action #'consult-gh--code-view-action)
(setq consult-gh-file-action #'consult-gh--files-view-action)
(setq consult-gh-repo-action #'consult-gh--repo-browse-files-action)
;; Search only own codebase
(defun consult-gh-search-my-code (&optional initial repo noaction)
"Search my own code only."
(interactive)
(let ((consult-gh-search-code-args
(append consult-gh-search-code-args
'("--owner=wehrad"))))
(consult-gh-search-code initial repo noaction)))
(global-set-key (kbd "C-c f") #'consult-gh-search-my-code))
(defun my/git-clean-large-files (repo-path pattern)
"Rewrite Git history in REPO-PATH, removing files matching PATTERN.
Automatically commits and pushes current changes first, then removes
large files, cleans up, restores 'origin', and force-pushes rewritten
history."
(interactive
(list
(read-directory-name "Repository path: ")
(read-string "File glob pattern to remove: ")))
;; Normalize repo path
(setq repo-path (directory-file-name repo-path))
;; Ensure it is a git repo
(unless (file-directory-p (concat repo-path "/.git"))
(error "%s is not a Git repository" repo-path))
;; Read origin URL *before* filter-repo removes it
(let* ((config-file (concat repo-path "/.git/config"))
(origin-url nil))
(with-temp-buffer
(insert-file-contents config-file)
(goto-char (point-min))
(when (re-search-forward
"\\[remote \"origin\"\\][^[]*url = \\(.*?\\)$" nil t)
(setq origin-url (string-trim (match-string 1)))))
(unless origin-url
(error "Could not find origin URL in %s/.git/config" repo-path))
(let ((default-directory repo-path))
;; Commit all local changes first
(shell-command "git add -A")
(shell-command "git commit -m \"Backup before cleaning large files\" 2>/dev/null") ; ignore if no changes
;; Push current state to origin
(message "Pushing current state to origin as backup...")
(shell-command "git push origin main")
;; Run filter-repo
(message "Removing large files from history...")
(shell-command
(format "git filter-repo --force --path-glob '%s' --invert-paths" pattern))
;; Cleanup
(shell-command "git reflog expire --expire=now --all")
(shell-command "git gc --prune=now --aggressive")
;; Restore origin
(shell-command "git remote remove origin 2>/dev/null")
(shell-command (format "git remote add origin \"%s\"" origin-url))
;; Force-push rewritten history
(message "Force-pushing rewritten history to origin...")
(shell-command "git push origin --force --all")
(shell-command "git push origin --force --tags")
(message "Done cleaning Git history in %s (origin restored: %s)"
repo-path origin-url))))
;; -------------------------------------------- MOOSE
;; syntax highlighting for MOOSE input and test files
(use-package moose-mode
:load-path "~/.emacs.d/lisp/emacs-moose-mode"
:mode ("\\.i\\'" . moose-mode))
(with-eval-after-load 'moose-mode
(add-to-list 'auto-mode-alist '("\\.i\\'" . moose-mode)))
;; -------------------------------------------- C++
;; override to keep global keybindings
(with-eval-after-load 'cc-mode
(define-key c++-mode-map (kbd "C-d") 'my/move-up-10-lines))
(with-eval-after-load 'cc-mode
(define-key c++-mode-map (kbd "C-f") 'my/move-down-10-lines))
;; -------------------------------------------- modify buffers
;; Originally from stevey, adapted to support moving to a new directory.
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive
(progn
(if (not (buffer-file-name))
(error "Buffer '%s' is not visiting a file!" (buffer-name)))
;; Disable ido auto merge since it too frequently jumps back to
;; the original file name if you pause while typing.
;; Reenable with C-z C-z in the prompt.
(let ((ido-auto-merge-work-directories-length -1))
(list (read-file-name
(format "Rename %s to: "
(file-name-nondirectory (buffer-file-name))))))))
(if (equal new-name "")
(error "Aborted rename"))
(setq new-name
(if (file-directory-p new-name)
(expand-file-name
(file-name-nondirectory (buffer-file-name)) new-name)
(expand-file-name new-name)))
;; Only rename if the file was saved before. Update the
;; buffer name and visited file in all cases.
(if (file-exists-p (buffer-file-name))
(rename-file (buffer-file-name) new-name 1))
(let ((was-modified (buffer-modified-p)))
;; This also renames the buffer, and works with uniquify
(set-visited-file-name new-name)
(if was-modified
(save-buffer)
;; Clear buffer-modified flag caused by set-visited-file-name
(set-buffer-modified-p nil)))
(setq default-directory (file-name-directory new-name))
(message "Renamed to %s." new-name))
(global-set-key (kbd "C-c r") #'rename-file-and-buffer)
(defun delete-file-and-buffer ()
"Kill the current buffer and deletes the file it is visiting."
(interactive)
(let ((filename (buffer-file-name)))
(when filename
(if (vc-backend filename)
(vc-delete-file filename)
(progn
(delete-file filename)
(message "Deleted file %s" filename)
(kill-buffer))))))
(global-set-key (kbd "C-c d") #'delete-file-and-buffer)
;; -------------------------------------------- ibuffer
(require 'ibuffer)
(global-set-key (kbd "C-x C-b") 'ibuffer) ;; Use Ibuffer for Buffer List
(setq ibuffer-saved-filter-groups
'(("default"
("elisp" (mode . emacs-lisp-mode))
("org" (or (mode . org-mode) (filename . "OrgMode")))
("python" (or (mode . python-mode)))
("C++" (or (mode . c-mode) (mode . c++-mode)))
("folders" (mode . dired-mode))
("tex" (or (mode . latex-mode)
(mode . LaTeX-mode)))
("csv" (mode . csv-mode))
("txt" (mode . text-mode))
("bash" (mode . sh-mode))
("yml" (mode . yaml-mode))
("julia" (mode . julia-mode))
("vterminals" (mode . vterm-mode))
("magit" (or (mode . magit-status-mode)
(mode . magit-diff-mode)
(mode . magit-revision-mode)))
("MOOSE" (mode . moose-mode)))))
(setq ibuffer-expert t)
(setq ibuffer-show-empty-filter-groups nil)
(add-hook 'ibuffer-mode-hook
(lambda ()
(ibuffer-switch-to-saved-filter-groups "default")))
;; automatically refresh ibuffer
(add-hook 'ibuffer-mode-hook (lambda () (ibuffer-auto-mode 1)))
;; human readable size column
(defun ajv/human-readable-file-sizes-to-bytes (string)
"Convert a human-readable file size into bytes."
(interactive)
(cond
((string-suffix-p "G" string t)
(* 1000000000
(string-to-number (substring string 0 (- (length string) 1)))))
((string-suffix-p "M" string t)
(* 1000000
(string-to-number (substring string 0 (- (length string) 1)))))
((string-suffix-p "K" string t)
(* 1000
(string-to-number (substring string 0 (- (length string) 1)))))
(t (string-to-number (substring string 0 (- (length string) 1))))))
(defun ajv/bytes-to-human-readable-file-sizes (bytes)
"Convert number of bytes to human-readable file size."
(interactive)
(cond
((> bytes 1000000000) (format "%10.1fG" (/ bytes 1000000000.0)))
((> bytes 100000000) (format "%10.0fM" (/ bytes 1000000.0)))
((> bytes 1000000) (format "%10.1fM" (/ bytes 1000000.0)))
((> bytes 100000) (format "%10.0fk" (/ bytes 1000.0)))
((> bytes 1000) (format "%10.1fk" (/ bytes 1000.0)))
(t (format "%10d" bytes))))
;; Use human readable Size column instead of original one
(define-ibuffer-column size-h
(:name "Size"
:inline t
:summarizer
(lambda (column-strings)
(let ((total 0))
(dolist (string column-strings)
(setq total
;; like, ewww ...
(+ (float (ajv/human-readable-file-sizes-to-bytes string))
total)))
(ajv/bytes-to-human-readable-file-sizes total))))
(ajv/bytes-to-human-readable-file-sizes (buffer-size)))
;; Modify the default ibuffer-formats
(setq ibuffer-formats
'((mark modified read-only locked
" "
(name 20 20 :left :elide)
" "
(size-h 11 -1 :right)
" "
(mode 16 16 :left :elide)
" "
filename-and-process)
(mark " " (name 16 -1) " " filename)))
(defun ibuffer-advance-motion (direction)
(forward-line direction)
(beginning-of-line)
(if (not (get-text-property (point) 'ibuffer-filter-group-name))
t
(ibuffer-skip-properties '(ibuffer-filter-group-name) direction)
nil))
;; Improve line movement in ibuffer
(defun ibuffer-previous-line (&optional arg)
"Move backwards ARG lines, wrapping around the list if necessary."
(interactive "P")
(or arg (setq arg 1))
(let (err1 err2)
(while (> arg 0)
(cl-decf arg)
(setq err1 (ibuffer-advance-motion -1)
err2 (if (not (get-text-property (point) 'ibuffer-title))
t
(goto-char (point-max))
(beginning-of-line)
(ibuffer-skip-properties '(ibuffer-summary ibuffer-filter-group-name) -1)
nil)))
(and err1 err2)))
(defun ibuffer-next-line (&optional arg)
"Move forward ARG lines, wrapping around the list if necessary."
(interactive "P")
(or arg (setq arg 1))
(let (err1 err2)
(while (> arg 0)
(cl-decf arg)
(setq err1 (ibuffer-advance-motion 1)
err2 (if (not (get-text-property (point) 'ibuffer-summary))
t
(goto-char (point-min))
(beginning-of-line)
(ibuffer-skip-properties
'(ibuffer-summary ibuffer-filter-group-name ibuffer-title) 1)
nil)))
(and err1 err2)))
;; Improve header movement in ibuffer
(defun ibuffer-next-header ()
(interactive)
(while (ibuffer-next-line)))
(defun ibuffer-previous-header ()
(interactive)
(while (ibuffer-previous-line)))
(define-key ibuffer-mode-map (kbd "<up>") 'ibuffer-previous-line)
(define-key ibuffer-mode-map (kbd "<down>") 'ibuffer-next-line)
(define-key ibuffer-mode-map (kbd "<right>") 'ibuffer-next-header)
(define-key ibuffer-mode-map (kbd "<left>") 'ibuffer-previous-header)
;; Group buffers by version control project on request
(require 'ibuffer-vc)
(defvar my/ibuffer-vc-groups-active nil
"Non-nil if VC grouping is active in ibuffer.")
(defun my/ibuffer-toggle-vc-groups ()
"Toggle VC root grouping in ibuffer."
(interactive)
(if my/ibuffer-vc-groups-active
(progn
(ibuffer-switch-to-saved-filter-groups "default")
(setq my/ibuffer-vc-groups-active nil)
(message "ibuffer: default filter groups"))
(ibuffer-vc-set-filter-groups-by-vc-root)
(setq my/ibuffer-vc-groups-active t)
(message "ibuffer: VC root filter groups"))
(ibuffer-update nil t))
(with-eval-after-load 'ibuffer
(define-key ibuffer-mode-map (kbd "v") 'my/ibuffer-toggle-vc-groups))
;; -------------------------------------------- julia
(use-package julia-mode
:ensure t
:mode "\\.jl\\'"
:hook ((julia-mode . julia-repl-mode)
(julia-mode . eglot-jl-init)
(julia-mode . eglot-ensure)
(julia-mode .
(lambda ()
(local-set-key (kbd "C-d") #'julia-repl-send-line)
(local-set-key (kbd "C-c C-c") #'julia-repl-send-buffer)))))
;; -------------------------------------------- matlab/octave
;; octave mode on matlab files
(setq auto-mode-alist
(cons '("\\.m$" . octave-mode) auto-mode-alist))
;; turn on the abbrevs, auto-fill and font-lock features
(add-hook 'octave-mode-hook
(lambda ()
(abbrev-mode 1)
(auto-fill-mode 1)
(if (eq window-system 'x)
(font-lock-mode 1))))
;; run line and block
(add-hook 'octave-mode-hook '(lambda ()
(local-set-key (kbd "<C-return>") 'octave-send-line)
(local-set-key (kbd "C-c C-c") 'octave-send-region)))
;; -------------------------------------------- python
;; Disable startup message
(setq inhibit-startup-message t)
;; Enable elpy with use-package
(use-package elpy
:ensure t
:init
(elpy-enable)
:hook