-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.el
2369 lines (2056 loc) · 85 KB
/
init.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
;;; init.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2022 Abhishek(Compro) Prasad
;; Author: Abhishek(Compro) Prasad
;; Keywords: emacs, configuration, elisp
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Comment
;;; Code:
(require 'seq)
(setq is-windows
(seq-find
(lambda (x) (string= system-type x))
'("ms-dos" "windows-nt" "cygwin")))
(setq is-unix
(seq-find
(lambda (x) (string= system-type x))
'("gnu" "gnu/linux" "gnu/kfreebsd" "darwin" "cygwin")))
(setq is-gnu
(seq-find
(lambda (x) (string= system-type x))
'("gnu" "gnu/linux" "gnu/kfreebsd")))
(setq is-linux
(or
(string= system-type "gnu")
(string= system-type "gnu/linux")))
(setq is-mac (string= system-type "darwin"))
(setq is-bsd
(or
(string= system-type "gnu/kfreebsd")
(string= system-type "darwin")))
(defun tangle-README.org-to-init.el ()
"Tangle README.org to init.el"
(let ((readme (ft (concat emacs-d "README.org")))
(current-file (ft (buffer-file-name))))
(when (string= readme current-file)
(call-interactively 'org-babel-tangle))))
(defun early-init ()
"Return `early-init.el' if greater than Emacs 27.
Else it will return `init.el'. Useful for tangling source code."
(if (< emacs-major-version 27)
"init.el"
"early-init.el"))
(add-hook 'after-save-hook 'tangle-README.org-to-init.el)
(require 'package)
(defvar sslp (and (not (memq system-type '(windows-nt ms-dos)))
(gnutls-available-p))
"Tells if SSL is enabled or not.")
(defvar protocol (if sslp "https" "http")
"Protocol value as string.")
(defun compro/add-package-list (name url)
"Add NAME and URL to `package-archives'.
URL should not have http:// or https:// as a prefix."
(setf (alist-get name package-archives nil nil 'string=) (concat protocol "://" url)))
(compro/add-package-list "elpa-devel" "elpa.gnu.org/devel/")
(compro/add-package-list "melpa" "melpa.org/packages/")
(compro/add-package-list "nongnu" "elpa.nongnu.org/nongnu/")
(package-initialize)
(when (< emacs-major-version 29)
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package)))
(use-package f :ensure t)
(use-package s :ensure t)
(defun mplist-remove (plist prop)
"Return a copy of a modified PLIST without PROP and its values.
If there are multiple properties with the same keyword, only the first property
and its values are removed."
(let ((tail plist)
result)
(while (and (consp tail) (not (eq prop (car tail))))
(push (pop tail) result))
(when (eq prop (car tail))
(pop tail)
(while (and (consp tail) (not (keywordp (car tail))))
(pop tail)))
(while (consp tail)
(push (pop tail) result))
(nreverse result)))
(defun set-default-font (plists)
"Set the font given the passed PLISTS.
PLISTS has either the form (\"fontname\" :prop1 val1 :prop2 val2 ...)
or is a list of such. The first font that can be found will be used.
The return value is nil if no font was found, truthy otherwise."
(unless (listp (car plists))
(setq plists (list plists)))
(catch 'break
(dolist (plist plists)
(when (find-font (font-spec :name (car plist)))
(let* ((font (car plist))
(props (cdr plist))
(font-props (mplist-remove
;; although this keyword does not exist anymore
;; we keep it for backward compatibility
(mplist-remove props :powerline-scale)
:powerline-offset))
(fontspec (apply 'font-spec :name font font-props)))
(set-frame-font fontspec nil t)
(push `(font . ,(frame-parameter nil 'font)) default-frame-alist)
(pcase system-type
(`gnu/linux
(setq fallback-font-name "NanumGothic")
(setq fallback-font-name2 "NanumGothic"))
(`darwin
(setq fallback-font-name "Arial Unicode MS")
(setq fallback-font-name2 "Arial Unicode MS"))
(`windows-nt
(setq fallback-font-name "MS Gothic")
(setq fallback-font-name2 "Lucida Sans Unicode"))
(`cygwin
(setq fallback-font-name "MS Gothic")
(setq fallback-font-name2 "Lucida Sans Unicode"))
(other
(setq fallback-font-name nil)
(setq fallback-font-name2 nil)))
(when (and fallback-font-name fallback-font-name2)
;; remove any size or height properties in order to be able to
;; scale the fallback fonts with the default one (for zoom-in/out
;; for instance)
(let* ((fallback-props (mplist-remove
(mplist-remove font-props :size)
:height))
(fallback-spec (apply 'font-spec
:name fallback-font-name
fallback-props))
(fallback-spec2 (apply 'font-spec
:name fallback-font-name2
fallback-props)))
;; window numbers
(set-fontset-font "fontset-default"
'(#x2776 . #x2793) fallback-spec nil 'prepend)
;; mode-line circled letters
(set-fontset-font "fontset-default"
'(#x24b6 . #x24fe) fallback-spec nil 'prepend)
;; mode-line additional characters
(set-fontset-font "fontset-default"
'(#x2295 . #x22a1) fallback-spec nil 'prepend)
;; new version lighter
(set-fontset-font "fontset-default"
'(#x2190 . #x2200) fallback-spec2 nil 'prepend))))
(throw 'break t)))
nil))
(defun compro/comint/kill-word (arg)
(interactive "p")
(unless buffer-read-only
(let ((beg (point))
(end (save-excursion (forward-word arg) (point)))
(point (save-excursion (goto-char
(if (> arg 0)
(next-single-char-property-change
(point) 'read-only)
(previous-single-char-property-change
(point) 'read-only)))
(point))))
(unless (get-char-property (point) 'read-only)
(if (if (> arg 0) (< point end) (> point end))
(kill-region beg point)
(kill-region beg end))))))
(defun compro/comint/preoutput-read-only (text)
(propertize text 'read-only t))
(defun compro/shell-kill-buffer-sentinel (process event)
(when (and (memq (process-status process) '(exit signal))
(buffer-live-p (process-buffer process)))
(kill-buffer)))
(defun compro/kill-process-buffer-on-exit ()
(set-process-sentinel (get-buffer-process (current-buffer))
#'compro/shell-kill-buffer-sentinel))
(dolist (hook '(ielm-mode-hook term-exec-hook comint-exec-hook))
(add-hook hook 'compro/kill-process-buffer-on-exit))
(defun compro/get-empty-pkgs ()
"Get 0 bytes .el packages."
(let ((default-directory package-user-dir))
(seq-reduce
(lambda (value-list file)
(if (= (file-attribute-size (file-attributes file)) 0)
(cons file value-list)
value-list))
(seq-filter
(apply-partially #'s-suffix-p ".el")
(seq-reduce
(lambda (value-list file)
(if (and
(not (s-prefix-p "." file))
(file-accessible-directory-p file))
(append
(seq-map
(apply-partially #'concat file "/")
(directory-files file))
value-list)
value-list))
(directory-files "")
'()))
'())))
(defun compro/redownload-empty-pkgs ()
"Redownload empty packages."
(interactive)
(let* ((pkgs (compro/get-empty-pkgs))
(default-directory package-user-dir)
(choice-list (list
(cons (intern "Delete and re-download all") 1)
(cons (intern "Manually select for re-downloading") 2)
(cons (intern "Fix everything manually") 3)))
(choice (if pkgs
(alist-get
(intern
(completing-read
(concat
"Some files were not properly downloaded namely "
(s-join ", " pkgs)
". What action do you want to take? ")
choice-list))
choice-list)
3)))
(if (= choice 3)
(when (null pkgs)
(message "No empty packages were found"))
(package-refresh-contents)
(seq-each
(lambda (file)
(let* ((values (s-split "/" file))
(dir-name (car values))
(pkg-values (s-split "-" dir-name))
(pkg-name (s-join "-" (butlast pkg-values 1)))
(each-choice
(if (= choice 1)
t
(yes-or-no-p
(concat "Delete and re-download " dir-name "? ")))))
(when each-choice
(delete-directory dir-name t)
(ignore-errors
(package-reinstall (intern pkg-name))))))
pkgs))))
(defun re-download (pkg &optional arg)
"Advice for package-install."
(let* ((pkg-name (symbol-name (if (package-desc-p pkg)
(package-desc-name pkg)
pkg)))
(file-name (car
(sort
(seq-filter
(apply-partially #'s-prefix-p pkg-name)
(compro/get-empty-pkgs))
#'string-greaterp)))
(dir (when file-name (car (s-split "/" file-name)))))
(when dir
(delete-directory dir)
(ignore-errors (package-reinstall pkg)))))
(advice-add 'package-install :after 're-download)
(defun switch-to-buffer-current-major-mode ()
"Switch to buffer like functionality based on current major mode."
(interactive)
(let* ((m-mode major-mode)
(prompt (concat (symbol-name m-mode) " buffers: ")))
(read-buffer
prompt nil (confirm-nonexistent-file-or-buffer)
(lambda (buf)
(with-current-buffer (cdr buf)
(eq m-mode major-mode))))))
(global-set-key (kbd "C-x C-b") 'switch-to-buffer-current-major-mode)
(setq compro/laptop-p (equal system-name "hp-archlinux"))
(use-package general :ensure t)
(remove-hook 'file-name-at-point-functions 'ffap-guess-file-name-at-point)
(use-package tab-bar
:when (> emacs-major-version 27)
:bind (("C-t" . tab-bar-new-tab-event)
([C-f4] . tab-bar-close-tab)
("C-S-t" . tab-bar-undo-close-tab)
([C-tab] . tab-next)
([C-backtab] . tab-previous)
([C-S-tab] . tab-previous)
([C-iso-lefttab] . tab-previous))
:init
(defun switch-to-untitled-buffer ()
(interactive)
(let ((buf (format "untitled-%d" (random 100000))))
(generate-new-buffer buf)
(switch-to-buffer buf)
(setq buffer-offer-save 'always)))
(defvar tab-bar-new-commands
'((?p "Project" project-switch-project)
(?n "New buffer" switch-to-untitled-buffer)
(?f "List Files" find-file)
(?b "List Buffers" switch-to-buffer)
(?r "Run command" execute-extended-command)
(?q "Do nothing" ignore)))
(defun tab-bar-new--keymap-prompt ()
"Return a prompt for the project swithing dispatch menu."
(mapconcat
(pcase-lambda (`(,key ,label))
(format "%s %s"
(propertize
(key-description `(,(concat " " (char-to-string key) " ")))
'face '(:foreground "black" :background "cyan" :weight bold))
label))
tab-bar-new-commands
" "))
(defun tab-bar-new-tab-event ()
(interactive)
(when-let ((choice (assq (read-event (tab-bar-new--keymap-prompt))
tab-bar-new-commands))
(inhibit-quit t))
(tab-bar-new-tab)
(when (not (char-equal (nth 0 choice) ?q))
(switch-to-buffer "waiting...")
(insert "Churning data or waiting for IO")
(with-local-quit (call-interactively (nth 2 choice)))
(kill-buffer "waiting..."))
(message "New tab created with `%s' option" (nth 1 choice))))
:config
(setq tab-bar-format
'(tab-bar-format-history
tab-bar-separator tab-bar-separator
tab-bar-format-tabs
tab-bar-separator tab-bar-separator tab-bar-separator
tab-bar-format-add-tab
tab-bar-separator tab-bar-separator tab-bar-separator
tab-bar-format-global))
(tab-bar-mode))
(use-package dired
:hook (dired-mode-hook . dired-hide-details-mode)
:bind (:map dired-mode-map
("C-c C-c" . dired-collapse-mode)
("C-c C-d C-u" . dired-du-mode)
("." . dired-hide-dotfiles-mode)
("<tab>" . dired-subtree-toggle)
("q" . kill-current-buffer)
("RET" . compro/dired-open-dir)
("^" . compro/dired-up-dir)
("DEL" . compro/dired-up-dir)
("<left>" . compro/dired-up-dir)
("C-x <C-j>" . dired-jump))
:init
(use-package dired-collapse :ensure t)
(use-package dired-du :ensure t :after dired)
(use-package dired-dups :ensure t :after dired)
(use-package dired-filetype-face :ensure t :after dired)
(use-package dired-hide-dotfiles :ensure t
:after dired
:hook (dired-mode-hook . dired-hide-dotfiles-mode))
(use-package dired-subtree :ensure t :after dired)
(defun compro/dired-up-dir ()
(interactive)
(find-alternate-file ".."))
(defun compro/dired-open-dir ()
(interactive)
(set-buffer-modified-p nil)
(let ((file-or-dir (dired-get-file-for-visit)))
(if (f-dir-p file-or-dir)
(find-alternate-file file-or-dir)
(find-file file-or-dir))))
(defun compro/dired/mp3-to-ogg ()
"Used in dired to convert mp3 files to ogg"
(interactive)
(let* ((files (dired-get-marked-files)))
(dolist (file files)
(let* ((basename (file-name-nondirectory file))
(file-base (file-name-base file))
(dirname (file-name-directory file))
(extension (file-name-extension file))
(ogg-file (concat dirname file-base ".ogg"))
(command (format "mpg123 -s -v \"%s\" | oggenc --raw -o \"%s\" -" file ogg-file)))
(if (string= "mp3" (downcase extension))
(progn
(shell-command command nil nil)
(message command)
(if (file-exists-p ogg-file)
(delete-file file))))))))
:config
(setq dired-dwim-target t)
(defun mydired-sort ()
"Sort dired listings with directories first."
(save-excursion
(let (buffer-read-only)
(forward-line 2) ;; beyond dir. header
(sort-regexp-fields t "^.*$" "[ ]*." (point) (point-max)))
(set-buffer-modified-p nil)))
(defadvice dired-readin
(after dired-after-updating-hook first () activate)
"Sort dired listings with directories first before adding marks."
(mydired-sort)))
(when compro/laptop-p
(setq user-mail-address "[email protected]"
user-full-name "Compro Prasad"))
(setq-default
;;; Use spaces and not tabs for indentation
indent-tabs-mode nil
;;; Don't highlight trailing whitespaces by default
show-trailing-whitespace nil
;;; Org
org-src-fontify-natively t ;; Fontify source blocks
;;; More number of characters on a single line
fill-column 80
)
(setq
;;; Load newer files
load-prefer-newer t
;;; Initial major mode for *scratch* buffer
initial-major-mode 'fundamental-mode
;;; Only use ~/.authinfo.gpg
auth-sources (list (ft "~/.authinfo.gpg"))
;;; Security settings
gnutls-verify-error t
;;; Customizations go to this file
custom-file (expand-file-name "custom.el" cache-d)
;;; Follow symlinks to the actual file
find-file-visit-truename t
vc-follow-symlinks t
;;; Don't redisplay if input is in buffer. Makes scrolling smoother.
redisplay-skip-fontification-on-input t
;;; Jump by words separated by punctuations
global-subword-mode t
;;; Prompt GNUPG passwords in the minibuffer only
epg-pinentry-mode 'loopback
;;; Show keystrokes in minibuffer after 0.5 seconds
echo-keystrokes 0.5
;;; Turn on every disabled function
disabled-command-function nil
;;; Use UTF-8 characters in buffer
buffer-file-coding-system 'utf-8
;;; Disable bidirectional text for tiny performance boost
bidi-display-reordering nil
;;; Don't blink parens
blink-matching-paren nil
;;; Hide cursors in other windows
cursor-in-non-selected-windows nil
;;; Prevent frames from automatically resizing themselves
frame-inhibit-implied-resize t
;;; Clipboard length
kill-ring-max 1024
;;; Stretch cursor according to the character under it
x-stretch-cursor t
;;; Time to wait before start of stealth fontify
jit-lock-stealth-time 120
;;; Sentences are separated by single space after dot(.)
sentence-end-double-space nil
;;; Don't compact font cache during GC to optimize redisplay
inhibit-compacting-font-caches t
;;; GC triggers per 100 MB increase in memory
gc-cons-threshold (* 100 1024 1024)
gc-cons-threshold-bak gc-cons-threshold ;; Backup
;;; Increase buffer size for reading output of processes (5 MB)
read-process-output-max (* 5 1024 1024)
;;; Prevent recursion limits
max-lisp-eval-depth 700
max-specpdl-size 700
;;; No bells
ring-bell-function 'ignore
visible-bell nil
;;; Themes are safe after all
custom-safe-themes t
;;; No startup show off
inhibit-startup-screen t
;;; Show line number for any normal width line
line-number-display-limit-width 10000000
;;; Some TLS connections might have larger PRIME bits
gnutls-min-prime-bits 4096
;;; Better unique names of similar filenames and buffer-names
uniquify-buffer-name-style 'forward
;;; We can use TCP connection to connect to remote Emacs instance
server-use-tcp t
;;; Server location
server-auth-dir (concat cache-d "server/")
;;; Save existing interprogram clipboard text before replacing it
save-interprogram-paste-before-kill t
;;; Set REPL programs' prompt as read only
comint-prompt-read-only t
;;; Read more output from a process (2mb)
read-process-output-max 2097152
;;; Use commands when in in minibuffer
enable-recursive-minibuffers t
;;; Scroll one line at a time no matter what
scroll-conservatively 10000
;;; Increase update time
idle-update-delay 1.0
;;; Initial scratch message is nil
initial-scratch-message ""
;;; Use directory local variables in tramp session
enable-remote-dir-locals t
;;; Backup configuration
tramp-persistency-file-name (concat cache-d "tramp")
backup-directory-alist `(("." . ,(concat cache-d "backups")))
delete-old-versions -1
version-control t
vc-make-backup-files t
vc-handled-backends '(Git)
auto-save-file-name-transforms `((".*" ,(concat cache-d "auto-save-list") t))
auto-save-list-file-prefix (concat cache-d "auto-save-list/saves-")
;;; ERC configurations
erc-hide-list '("PART" "QUIT" "JOIN")
erc-server "irc.libera.chat"
erc-nick "compro"
;;; Dired
dired-dwim-target t
dired-listing-switches "-lAh --group-directories-first"
;;; Ediff
ediff-window-setup-function 'ediff-setup-windows-plain ;; Single frame ediff session
;;; Ido mode
ido-enable-flex-matching t
ido-save-directory-list-file (concat cache-d "ido.last")
;;; TAB cycle if there are only few candidates
completion-cycle-threshold 5
;;; Complete after indenting
tab-always-indent 'complete
;;; Increase interval at which eldoc is shown
eldoc-idle-delay 1.5
)
(if (>= emacs-major-version 28)
(setq use-short-answers t)
(fset 'yes-or-no-p 'y-or-n-p))
(when (file-readable-p custom-file)
(load custom-file))
(when (file-readable-p "~/.git-tokens")
(load-file "~/.git-tokens"))
(set-language-environment 'utf-8)
(set-default-coding-systems 'utf-8)
(set-selection-coding-system 'utf-8)
(set-locale-environment "en.UTF-8")
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
(defun compro/unset-keys ()
(general-define-key
:keymaps 'input-decode-map
[?\C-m] [C-m]
[?\C-i] [C-i]
[?\C-j] [C-j]
[?\C-\[] (kbd "<C-[>"))
(remove-hook 'server-after-make-frame-hook 'compro/unset-keys))
;; For daemon / server sessions
(add-hook 'server-after-make-frame-hook 'compro/unset-keys)
;; For non daemon / server sessions
(compro/unset-keys)
(general-define-key
"C-z" 'undo
"C-x C-o" 'ff-find-other-file
[C-m] 'delete-other-windows
"<C-S-mouse-1>" 'imenu
"C-c r" 'imenu
"M-/" 'hippie-expand
"M-^" 'compile)
(if (< emacs-major-version 28)
(global-set-key [mouse-3] menu-bar-edit-menu)
(context-menu-mode 1))
(use-package autorevert
:config
(setq auto-revert-remote-files nil)
(global-auto-revert-mode t))
(use-package paren
:config
(setq show-paren-style 'mixed
show-paren-when-point-inside-paren t
show-paren-when-point-in-periphery t)
(show-paren-mode t))
(if (>= emacs-major-version 26)
(add-hook 'prog-mode-hook 'display-line-numbers-mode)
(add-hook 'prog-mode-hook 'linum-mode))
(add-hook 'prog-mode-hook 'which-function-mode)
(add-hook 'prog-mode-hook 'electric-pair-mode)
(when (>= emacs-major-version 27)
(add-hook 'prog-mode-hook 'display-fill-column-indicator-mode))
(require 'ansi-color)
(defun colorize-compilation-buffer ()
"Colorize the compilation buffer with ANSI escape sequences."
(read-only-mode)
(ansi-color-apply-on-region (point-min) (point-max))
(read-only-mode))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
(defun compro/rename-file-buffer (&optional arg)
"Rename current buffer and the file it is linked to.
If no prefix argument is provided simple string input is provided
using `read-string' function.
If a prefix argument (\\[universal-argument]) is provided full
featured `read-file-name' is used to read the filename. This is
useful if you want to move the file from one directory to another."
(interactive "p")
(when (null (buffer-file-name))
(error "Buffer `%s' is not linked to a file" (buffer-name)))
(let* ((filepath (buffer-file-name))
(filename (f-filename filepath))
(filedir (file-name-directory (directory-file-name filepath)))
(prompt (concat "Rename '" filename "' to: "))
(move-p (> arg 1))
(new-location (if move-p
(read-file-name prompt filedir filepath)
(read-string prompt filename)))
(new-filepath (if (string-suffix-p "/" new-location)
(concat new-location filename)
new-location)))
(rename-file filename new-location 1)
(set-visited-file-name new-filepath t t)))
(global-set-key (kbd "C-c f r") 'compro/rename-file-buffer)
(use-package simple
:bind (("C-a" . compro/beginning-of-line)
("C-S-p" . list-processes)
("" . list-processes))
:init
(defun compro/beginning-of-line ()
(interactive)
(if (bolp)
(back-to-indentation)
(let ((pos (point))
npos)
(save-excursion
(back-to-indentation)
(setq npos (point)))
(if (= pos npos)
(beginning-of-line)
(back-to-indentation))))))
(with-eval-after-load 'comint
(general-define-key
:kemaps 'comint-mode-map
"<remap> <kill-word>" 'compro/comint/kill-word))
(add-hook 'comint-preoutput-filter-functions
'compro/comint/preoutput-read-only)
(require 'savehist)
(setq history-length t
history-delete-duplicates t
savehist-file (concat cache-d "savehist")
save-place-file (concat cache-d "saveplace")
savehist-additional-variables (nconc savehist-additional-variables
'(kill-ring
extended-command-history
global-mark-ring
mark-ring
regexp-search-ring
search-ring)))
(save-place-mode 1)
(savehist-mode 1)
(require 'recentf)
(setq recentf-max-saved-items 512
recentf-save-file (concat cache-d "recentf"))
(add-to-list 'recentf-exclude
(concat (regexp-quote (ft (format cache-d))) ".*"))
(recentf-mode 1)
(use-package xwidget :when (fboundp 'xwidget-webkit-browse-url)
:bind
(:map xwidget-webkit-mode-map
("<mouse-4>" . xwidget-webkit-scroll-down)
("<mouse-5>" . xwidget-webkit-scroll-up)
("<up>" . xwidget-webkit-scroll-down)
("<down>" . xwidget-webkit-scroll-up)
("M-w" . xwidget-webkit-copy-selection-as-kill)
("C-c" . xwidget-webkit-copy-selection-as-kill))
:hook
(window-configuration-change-hook . compro/xwidget-webkit/adjust-size)
:init
;; by default, xwidget reuses previous xwidget window,
;; thus overriding your current website, unless a prefix argument
;; is supplied
;; This function always opens a new website in a new window
(defun xwidget-browse-url-no-reuse (url &optional session)
(interactive
(progn
(require 'browse-url)
(browse-url-interactive-arg "xwidget-webkit URL: ")))
(xwidget-webkit-browse-url url t))
(defun compro/xwidget-webkit/adjust-size ()
(when (equal major-mode 'xwidget-webkit-mode)
(xwidget-webkit-adjust-size-dispatch))))
(add-hook 'tabulated-list-mode-hook 'hl-line-mode)
(use-package winner :config (winner-mode 1))
(defun compro/set-show-whitespace-mode ()
"Show white space in current buffer"
(setq show-trailing-whitespace t))
;; Show whitespaces only in buffers pointing to specific files
(add-hook 'find-file-hook 'compro/set-show-whitespace-mode)
;; Remove the trailing whitespaces on save
(add-hook 'before-save-hook
#'(lambda ()
(when (not (eq major-mode 'org-mode))
(delete-trailing-whitespace))))
(defun my/minibuffer-setup-hook ()
(setq gc-cons-threshold most-positive-fixnum))
(defun my/minibuffer-exit-hook ()
(setq gc-cons-threshold gc-cons-threshold-bak)
(garbage-collect))
(add-hook 'minibuffer-setup-hook #'my/minibuffer-setup-hook)
(add-hook 'minibuffer-exit-hook #'my/minibuffer-exit-hook)
(c-add-style "mylinux"
'("linux"
(tab-width . 4)
(c-basic-offset . 4)
(fill-column . 80)
(c-hanging-semi&comma-criteria . my/c-semi&comma)
(c-cleanup-list empty-defun-braces ;; {}
brace-else-brace ;; } else {
brace-elseif-brace ;; } else if {
;;defun-close-semi ;; };
)
(c-hanging-braces-alist (brace-list-open)
(brace-entry-open)
(substatement-open after)
(block-close . c-snug-do-while)
(arglist-cont-nonempty)
(class-open . (after))
(class-close . (before)))
(c-offsets-alist (inline-open . 0)
(comment-intro . 0))))
(setq-default c-default-style
'((java-mode . "java")
(awk-mode . "awk")
(other . "mylinux")))
(defun default-ansi-term-opener (dir)
(let ((buf-name (funcall terminal-buffer-name-generator dir "ansi-term" nil))
(*buf-name* (concat "*" buf-name "*")))
(if (get-buffer *buf-name*)
`(,*buf-name* . nil)
(ansi-term "/bin/bash" buf-name)
`(,*buf-name* . t))))
(defun default-eshell-opener (dir)
(let ((eshell-buffer-name (funcall terminal-buffer-name-generator dir "eshell" t)))
(if (get-buffer eshell-buffer-name)
`(,eshell-buffer-name . nil)
(eshell)
`(,eshell-buffer-name . t))))
(defun default-vterm-opener (dir)
(let ((buf-name (funcall terminal-buffer-name-generator dir "vterm" t)))
(if (get-buffer buf-name)
`(,buf-name . nil)
(vterm buf-name)
`(,buf-name . t))))
(defun default-terminal-buffer-name-generator (dir terminal-type use-star)
(if use-star
(concat "*" terminal-type "-" dir "*")
(concat terminal-type "-" dir)))
(defun detect-terminal-and-open (dir)
(if (featurep 'vterm)
(default-vterm-opener dir)
(prog1
(default-eshell-opener dir)
(message "Opened eshell because vterm was not installed"))))
(defun below-window-checker (buffer action)
(with-current-buffer buffer
(or
(s-starts-with? "*eshell" buffer)
(s-starts-with? "*vterm" buffer)
(s-starts-with? "*ansi-term" buffer))))
(defvar terminal-buffer-name-generator 'default-terminal-buffer-name-generator)
(defvar terminal-generator 'detect-terminal-and-open)
(defun toggle-terminal ()
"Toggles terminal like VS Code does.
TODO:
1. Creating new terminals in the same project for different purposes.
2. Editing path in which the terminal is opened. Currently best guess it taken.
3. Allow switching between different terminals in the same path with ease.
"
(interactive)
(let* ((prj (project-current))
(dir (if prj
(expand-file-name (project-root prj))
default-directory))
(ret-val (let ((default-directory dir)) (funcall terminal-generator dir)))
(created (cdr ret-val))
(buf-name (car ret-val))
(buf-obj (get-buffer buf-name))
(buf-win (get-buffer-window buf-name nil)))
(if buf-win
(if (and (not created) (eq buf-win (selected-window)))
(progn (message "Hiding terminal window") (delete-window buf-win))
(progn (message "Selecting terminal window") (select-window buf-win t)))
(message "Showing terminal buffer in a window")
(display-buffer-in-side-window buf-obj '((side . bottom)))
(select-window (get-buffer-window buf-obj) t))))
(defun create-new-terminal ()
"WIP"
(let* ((prj (project-current))
(dir (if prj
(expand-file-name (project-root prj))
default-directory))
(ret-val (funcall terminal-generator dir))
(created (cdr ret-val))
(buf-name (car ret-val))
(buf-obj (get-buffer buf-name))
(buf-win (get-buffer-window buf-name nil)))
(if buf-win
(if (and (not created) (eq buf-win (selected-window)))
(progn (message "Hiding terminal window") (delete-window buf-win))
(progn (message "Selecting terminal window") (select-window buf-win t)))
(message "Showing terminal buffer in a window")
(display-buffer-in-side-window buf-obj '((side . bottom)))
(select-window (get-buffer-window buf-obj) t))))
(add-to-list
'display-buffer-alist
'(below-window-checker
display-buffer-in-side-window
(side . bottom)
;; (slot . 1)
;; (window-width . 0.33)
;; (reusable-frames . nil)
))
(global-set-key (kbd "C-`") 'toggle-terminal)
(global-set-key (kbd "C-c `") 'toggle-terminal)
(use-package restclient :ensure t)
(use-package hydra :ensure t)
(global-set-key
(kbd "C-c u")
(defhydra hydra-ui (:hint nil)
"
^Emacs^ ^Move to window^ ^Move window to^ ^Buffer^
^^^^-----------------------------------------------------------------------
_M-+_: Inc font _<left>_ _S-<left>_ _f_: Col indicator
_M-=_: Inc font _<right>_ _S-<right>_ _l_: Line numbers
_M--_: Dec font _<up>_ _S-<up>_ _+_: Inc font
_F_: Col indicator _<down>_ _S-<down>_ _=_: Inc font
_L_: Line numbers ^ ^ ^ ^ _-_: Dec font
_t_: Tabs
_T_: Toolbar
_m_: Menubar
_s_: Scrollbar"
("+" text-scale-increase)
("=" text-scale-increase)
("-" text-scale-decrease)
("M-+" default-text-scale-increase)
("M-=" default-text-scale-increase)
("M--" default-text-scale-decrease)
("t" tab-bar-mode)
("m" menu-bar-mode)
("s" scroll-bar-mode)
("f" display-fill-column-indicator-mode)
("l" display-line-numbers-mode)
("F" global-display-fill-column-indicator-mode)
("L" global-display-line-numbers-mode)
("T" tool-bar-mode)
("<left>" windmove-left)
("<right>" windmove-right)
("<up>" windmove-up)
("<down>" windmove-down)
("S-<left>" buf-move-left)
("S-<right>" buf-move-right)
("S-<up>" buf-move-up)
("S-<down>" buf-move-down)))