-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
7819 lines (7061 loc) · 285 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
;; -*- lexical-binding: t; -*-
;;* Early
(when (version< emacs-version "27")
(load (concat user-emacs-directory "early-init.el")))
;; (setq debug-on-error '(wrong-type-argument) debug-on-signal t)
;; (setq debug-on-error t)
;; https://github.com/bkaestner/.emacs.d/blob/37c75bfe3a199594ad89504d870e68f6f424764f/early-init.el
(setq gc-cons-threshold most-positive-fixnum ; 2^61 bytes
gc-cons-percentage 0.6)
(defun my-cleanup-gc ()
"Clean up gc."
(setq gc-cons-threshold 16777216)
(garbage-collect))
;; After Emacs has completely started, reset the values to more sensible ones.
(add-hook 'emacs-startup-hook
(lambda ()
(progn
(run-with-idle-timer 4 nil #'my-cleanup-gc)
(setq gc-cons-threshold 16777216
gc-cons-percentage 0.1))))
;; (setq straight-check-for-modifications '(check-on-save find-when-checking))
(defvar elpaca-installer-version 0.7)
(defvar elpaca-directory (expand-file-name "elpaca/" user-emacs-directory))
(defvar elpaca-builds-directory (expand-file-name "builds/" elpaca-directory))
(defvar elpaca-repos-directory (expand-file-name "repos/" elpaca-directory))
(defvar elpaca-order '(elpaca :repo "https://github.com/progfolio/elpaca.git"
:ref nil :depth 1
:files (:defaults "elpaca-test.el" (:exclude "extensions"))
:build (:not elpaca--activate-package)))
(let* ((repo (expand-file-name "elpaca/" elpaca-repos-directory))
(build (expand-file-name "elpaca/" elpaca-builds-directory))
(order (cdr elpaca-order))
(default-directory repo))
(add-to-list 'load-path (if (file-exists-p build) build repo))
(unless (file-exists-p repo)
(make-directory repo t)
(when (< emacs-major-version 28) (require 'subr-x))
(condition-case-unless-debug err
(if-let ((buffer (pop-to-buffer-same-window "*elpaca-bootstrap*"))
((zerop (apply #'call-process `("git" nil ,buffer t "clone"
,@(when-let ((depth (plist-get order :depth)))
(list (format "--depth=%d" depth) "--no-single-branch"))
,(plist-get order :repo) ,repo))))
((zerop (call-process "git" nil buffer t "checkout"
(or (plist-get order :ref) "--"))))
(emacs (concat invocation-directory invocation-name))
((zerop (call-process emacs nil buffer nil "-Q" "-L" "." "--batch"
"--eval" "(byte-recompile-directory \".\" 0 'force)")))
((require 'elpaca))
((elpaca-generate-autoloads "elpaca" repo)))
(progn (message "%s" (buffer-string)) (kill-buffer buffer))
(error "%s" (with-current-buffer buffer (buffer-string))))
((error) (warn "%s" err) (delete-directory repo 'recursive))))
(unless (require 'elpaca-autoloads nil t)
(require 'elpaca)
(elpaca-generate-autoloads "elpaca" repo)
(load "./elpaca-autoloads")))
(add-hook 'after-init-hook #'elpaca-process-queues)
(elpaca `(,@elpaca-order))
;;* Straight bootstrap
;; (defvar bootstrap-version)
;; (let ((bootstrap-file
;; (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
;; (bootstrap-version 5))
;; (unless (file-exists-p bootstrap-file)
;; (with-current-buffer
;; (url-retrieve-synchronously
;; "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
;; 'silent 'inhibit-cookies)
;; (goto-char (point-max))
;; (eval-print-last-sexp)))
;; (load bootstrap-file nil 'nomessage))
;; ;;* Straight config
;; (setq straight-use-package-by-default t)
;; (setq straight-host-usernames '((github . "cheerio-pixel")))
;;* vc-follows-symlinks
(setq vc-follow-symlinks t)
;; Install use-package support
(elpaca elpaca-use-package
;; Enable use-package :ensure support for Elpaca.
(elpaca-use-package-mode))
;;* Use package installation
;; (straight-use-package '(org :type built-in))
;; (straight-use-package 'use-package)
;; (eval-when-compile (require 'use-package))
;;* Use package configuration
(if init-file-debug
(setq use-package-verbose t
use-package-expand-minimally nil
use-package-compute-statistics t
debug-on-error t)
(setq use-package-verbose nil
use-package-expand-minimally t))
;;* Require
(require 'cl-lib)
;;* Load path
(add-to-list 'load-path (concat user-emacs-directory "elisp/"))
;;* Important packages
;; This packages need to be loaded before all the others
(use-package general
:ensure t
:config
(general-override-mode)
)
(use-package exec-path-from-shell
:ensure t
:config
(add-to-list 'exec-path-from-shell-variables "ANDROID_HOME")
(add-to-list 'exec-path-from-shell-variables "ANDROID_SDK_ROOT")
(when (or (memq window-system '(mac ns x))
(daemonp))
(exec-path-from-shell-initialize)))
;; Explicit patching of functions and variables.
(use-package el-patch
:ensure (:wait t))
;; Bring a little bit of clojure and more
(use-package dash :ensure t :config (global-dash-fontify-mode))
(use-package s :ensure t)
(use-package hydra
:after ryo-modal
:ensure t
;;; Not in use
:config
;; (ryo-modal-key
;; "q f" :hydra
;; '(hydra-fastmoving ()
;; "Generic fast moving"
;; ("n" scroll-up)
;; ("u" scroll-down)
;; ("U" ccm-scroll-down)
;; ("N" ccm-scroll-up)
;; ("]" forward-paragraph)
;; ("[" backward-paragraph)
;; ("q" nil "cancel" :color blue)))
(ryo-modal-key
"q i" :hydra
'(hydra-indent ()
"Indent Mode"
("n" mymy/elpy-nav-move-line-or-region-down)
("u" mymy/elpy-nav-move-line-or-region-up)
("N" shift-left)
("U" shift-right)
("q" nil "cancel" :color blue)
("." nil "cancel" :color blue)))
)
;;* Alias
(defalias 'yes-or-no-p 'y-or-n-p)
(defalias 'gsetq 'general-setq)
(defalias 'gsetq-local 'general-setq-local)
(defalias 'gsetq-default 'general-setq-default)
;;* Configs
;; (set-frame-parameter nil 'fullscreen 'fullboth) ; Fullscreen
;;* Modes
(global-hl-line-mode)
(setq-default show-trailing-whitespace t)
(setq ring-bell-function #'ignore)
(setq redisplay-dont-pause t)
(setq frame-resize-pixelwise t)
;; Increase the amount of bytes that emacs can read from an extenarl process
(setq read-process-output-max (* 1024 1024)) ;; 1mb
(tool-bar-mode -1) ; This is much easier
(menu-bar-mode -1) ; than needing to change
(scroll-bar-mode -1) ; this on every OS
(setq byte-compile-warnings '(not obsolete)) ;; Cl warnings
(setq save-abbrevs 'silently)
(setq-default abbrev-mode t)
(setq native-comp-async-report-warnings-errors 'silent)
(setq create-lockfiles nil)
(setq abbrev-suggest t)
;; I finally caught on. This is annoying when it tries to.
(setq require-final-newline nil)
(setq mode-require-final-newline nil)
;; Stopped being useful
;; (global-whitespace-mode)
(xterm-mouse-mode)
(savehist-mode)
(save-place-mode)
(setq-default abbrev-mode t)
;;* GC and minibuffer
(defun my-minibuffer-setup-hook ()
(setq gc-cons-threshold most-positive-fixnum))
(defun my-minibuffer-exit-hook ()
(setq gc-cons-threshold 100000000))
;; Let's test commenting this out
;; (add-hook 'minibuffer-setup-hook #'my-minibuffer-setup-hook)
;; (add-hook 'minibuffer-exit-hook #'my-minibuffer-exit-hook)
;;* My variables
;; TODO: Set all of this in the defconst or defvar form
(setq dropbox-dir
(pcase system-type
('windows-nt "c:/Users/frail/Dropbox/")
('gnu/linux "~/Dropbox (Maestral)/")))
(setq main-dropbox-dir (concat dropbox-dir "Creativè/"))
(setq mymy-org-roam-dir (concat main-dropbox-dir "Notes/"))
(defvar mymy-organization-system-directory (concat dropbox-dir "org/")
"General purpose root directory of notes")
;; Check
(unless (file-exists-p mymy-organization-system-directory)
(error "Cannot find '%s'. Directory doesn't exist " mymy-organization-system-directory))
(defvar mymy-organization-system-directory-attachments
(concat mymy-organization-system-directory "attachments/")
"Attachment directory")
(defvar mymy-bibliography-system-directory
(concat mymy-organization-system-directory "bibliography_system/")
"Diretory of bibliography references.")
(unless (file-exists-p mymy-bibliography-system-directory)
(error "Cannot find '%s'. Directory doesn't exist " mymy-bibliography-system-directory))
(when (and (file-exists-p mymy-organization-system-directory)
(not (file-exists-p mymy-organization-system-directory-attachments)))
(make-directory mymy-organization-system-directory-attachments))
;;* Variables
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'dired-find-alternate-file 'disabled nil)
;;* Modeline
(use-package nyan-mode ;; Nyan, simple nyan
;; The fact that this works is incredible, but it has no more use
:disabled
:diminish nyan-mode
:init
(setq nyan-animate-nyancat t
nyan-wavy-trail t)
:hook
(elpaca-after-init . nyan-mode))
(defcustom powerline-buffer-size-suffix t
"Display the buffer size suffix."
:group 'powerline
:type 'boolean)
(defun powerline-vc ()
(when (and (buffer-file-name (current-buffer)) vc-mode)
(format " %s%s"
(char-to-string #xe0a0)
(format-mode-line '(vc-mode vc-mode)))))
(defun powerline-buffer-size ()
(propertize
(if powerline-buffer-size-suffix
"%I"
"%i")
'mouse-face 'mode-line-highlight
'local-map (make-mode-line-mouse-map
'mouse-1 (lambda () (interactive)
(setq powerline-buffer-size-suffix
(not powerline-buffer-size-suffix))
(force-mode-line-update)))))
(defun mode-line-align (left right)
"Return a string with LEFT and RIGHT at the edges of the
current window."
(format (format "%%s %%%ds" (- (window-total-width) (length left) 2))
left right))
(setq-default
mode-line-format
'((:eval
(mode-line-align
(format-mode-line
(list " " (powerline-buffer-size)
" "
" " mode-line-buffer-identification
" " mode-line-modified
" "
" " mode-line-percent-position "%%"
" " "%n"
" "
))
(format-mode-line
(list mode-name
" " mode-line-misc-info
(powerline-vc)
;; " " (format-time-string "%H:%M")
))))))
(add-hook
'elpaca-after-init-hook
;; Darkula messing up with my modeline look
(lambda ()
(set-face-attribute
'mode-line nil
:overline "#EFEFF7"
:box nil
:underline nil
:background "#2B2B2B")))
;;* Frame
(add-to-list 'default-frame-alist
'(internal-border-width . 20))
(set-frame-parameter (selected-frame) 'internal-border-width 20)
(add-to-list 'default-frame-alist
'(alpha-background . 99))
;;* Abbrevs
(setq abbrev-file-name (concat dropbox-dir "emacs/abbrev_defs"))
;; (let ((mymy-abbrevs
;; '(("bc" "because")
;; ("wo" "without")
;; ("ex" "For example,")
;; ("zk" "Zettelkasten")
;; ;; ("col" "collection")
;; ;; ("perm" "permanent")
;; ;; ("lit" "literature")
;; ;; ("sd" "software development")
;; ;; ("diff" "different")
;; ("pv" "previous")
;; ("bf" "before")
;; ("hw" "however")
;; )))
;; (mapc (lambda (x) (define-global-abbrev (car x) (cadr x))) mymy-abbrevs))
;;* Macros
(defmacro comment (&rest args)
"Clojure-style comment block"
nil)
(defmacro section (description &rest body)
"Progn but with a requisite of a string describing the body"
(declare (indent 1))
(or (stringp description) (error "Named-section should have a description"))
`(progn ,@body))
;;* Functions
(defun mymy-path->qualified-name (path)
(replace-regexp-in-string
(regexp-quote "/")
"."
path))
(defun not-mymy-find-nearest-chsarp-project (&optional _dir)
"Graciously stolen from https://github.com/sebasmonia/sharper/blob/master/sharper.el, all credit to the guy"
(let ((start-from (or (buffer-file-name)
(when (eq major-mode
'dired-mode)
default-directory))))
(when start-from
(locate-dominating-file
start-from
(lambda (directory)
(when (file-directory-p directory)
;; Strangely enough, in Windows directory-files ignores a path
;; that is a file, but under Linux it fails. Adding a guard...
(let ((files (directory-files directory t)))
(cl-some (lambda (filename)
(let ((extension (file-name-extension filename)))
(member extension '("csproj" "fsproj"))))
files))))))))
(defun mymy-find-nearest-solution-file (&optional _dir)
(let ((start-from (or (buffer-file-name)
(when (eq major-mode
'dired-mode)
default-directory))))
(when start-from
(locate-dominating-file
start-from
(lambda (directory)
(when (file-directory-p directory)
;; Strangely enough, in Windows directory-files ignores a path
;; that is a file, but under Linux it fails. Adding a guard...
(let ((files (directory-files directory t)))
(cl-some (lambda (filename)
(let ((extension (file-name-extension filename)))
(member extension '("sln"))))
files))))))))
(defvar mymy-process-to-json-script (concat
user-emacs-directory
"ps-aux-to-json.sh"
))
;; Linux
(defun mymy-processes-in-json ()
"Get a list of process as list of plists
Schema: user, pid, cpu, mem, vsz, rss, tty, stat, start, time, command
"
(json-parse-string
(shell-command-to-string
(concat "bash "
mymy-process-to-json-script
)
)
:object-type 'plist)
)
(defun mymy-select-process (&optional filter-fn)
"Select a running process.
FILTER-FN: Takes a plist of an object and returns true."
(let* ((candidates-process (append (mymy-processes-in-json) nil))
(candidates-process (if filter-fn (-filter filter-fn candidates-process)
candidates-process
))
(candidates (--map (cons (plist-get it :command) it) candidates-process))
(selected (completing-read "Select process: " candidates nil t)))
(cdr (assoc selected candidates))))
(comment
;; Doesn't work for some reason
(defun xah-open-in-external-app (&optional Fname)
"Open the current file or dired marked files in external app.
When called in emacs lisp, if Fname is given, open that.
URL `http://xahlee.info/emacs/emacs/emacs_dired_open_file_in_ext_apps.html'
Version: 2019-11-04 2023-04-05 2023-06-26"
(interactive)
(let (xfileList xdoIt)
(setq xfileList
(if Fname
(list Fname)
(if (eq major-mode 'dired-mode)
(dired-get-marked-files)
(list buffer-file-name))))
(setq xdoIt (if (<= (length xfileList) 10) t (y-or-n-p "Open more than 10 files? ")))
(when xdoIt
(cond
((eq system-type 'windows-nt)
(let ((xoutBuf (get-buffer-create "*xah open in external app*"))
(xcmdlist (list "PowerShell" "-Command" "Invoke-Item" "-LiteralPath")))
(mapc
(lambda (x)
(message "%s" x)
(apply 'start-process (append (list "xah open in external app" xoutBuf) xcmdlist (list (format "'%s'" (if (string-match "'" x) (replace-match "`'" t t x) x))) nil)))
xfileList)
;; (switch-to-buffer-other-window xoutBuf)
)
;; old code. calling shell. also have a bug if filename contain apostrophe
;; (mapc (lambda (xfpath) (shell-command (concat "PowerShell -Command \"Invoke-Item -LiteralPath\" " "'" (shell-quote-argument (expand-file-name xfpath)) "'"))) xfileList)
)
((eq system-type 'darwin)
(mapc (lambda (xfpath) (shell-command (concat "open " (shell-quote-argument xfpath)))) xfileList))
((eq system-type 'gnu/linux)
(mapc (lambda (xfpath)
(call-process shell-file-name nil 0 nil
shell-command-switch
(format "%s %s"
"xdg-open"
(shell-quote-argument xfpath))))
xfileList))
((eq system-type 'berkeley-unix)
(mapc (lambda (xfpath) (let ((process-connection-type nil)) (start-process "" nil "xdg-open" xfpath))) xfileList)))))))
(defvar mymy-readonly-directories
'()
)
(with-eval-after-load 'straight
(add-to-list 'mymy-readonly-directories (straight--dir))
)
(with-eval-after-load 'elpaca
(add-to-list 'mymy-readonly-directories elpaca-directory)
)
(defun mymy-hook-to-read-only-in-selected-dirs ()
"Open files under straight as read-only"
;; (when (string-match-p (straight--dir) (buffer-file-name))
;; (read-only-mode 1))
(when (-first (lambda (d) (string-match-p d (buffer-file-name)))
mymy-readonly-directories
)
(read-only-mode 1)
)
)
(add-hook 'find-file-hook #'mymy-hook-to-read-only-in-selected-dirs)
;; (defun append-date-to-file (file)
;; "docstring"
;; (interactive (buffer-file-name))
;; (let (end-hook)
;; (when (eq 'dired-mode major-mode)
;; (setq file (dired-get-filename))
;; (add-hook end-hook #'revert-buffer nil t))
;; (unless (f-file-p file)
;; (user-error "Current file is not valid"))
;; (when (f-dir-p file)
;; (user-error "Cannot rename directory with date"))
;; (let ((filename (f-filename file))
;; (path (concat (f-dirname file) "/"))
;; ;; If you change one, obviously you have to change the other
;; (file-date-regex "^[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}_")
;; (date-string (format-time-string "%Y-%m-%d_"))
;; new-filename)
;; (if (string-match-p file-date-regex filename)
;; (setq new-filename (concat path date-string filename))
;; (setq new-filename (concat path (replace-regexp-in-string
;; (concat file-date-regex "\\'") date-string filename))))
;; (rename-file file new-filename)
;; (run-hooks end-hookk))))
(defun increment-number-at-point (arg)
(interactive "p")
(let ((old-point (point)))
(unwind-protect
(progn
(skip-chars-backward "0-9")
(or (looking-at "[0-9]+")
(error "No number at point"))
(replace-match (number-to-string (+ (or arg 1) (string-to-number (match-string 0))))))
(goto-char old-point))))
(defun decrement-number-at-point (arg)
(interactive "p")
(let ((old-point (point)))
(unwind-protect
(progn
(skip-chars-backward "0-9")
(or (looking-at "[0-9]+")
(error "No number at point"))
(replace-match (number-to-string (- (string-to-number (match-string 0)) (or arg 1)))))
(goto-char old-point))))
;; I use this too much
(defun mymy-kill-new (s)
(kill-new (format "%S" s)))
(defvar killed-file-list nil
"List of recently killed files.")
(defun add-file-to-killed-file-list ()
"If buffer is associated with a file name, add that file to the
`killed-file-list' when killing the buffer."
(when buffer-file-name
(push buffer-file-name killed-file-list)))
(add-hook 'kill-buffer-hook #'add-file-to-killed-file-list)
(defun reopen-killed-file ()
"Reopen the most recently killed file, if one exists."
(interactive)
(if killed-file-list
(find-file (pop killed-file-list))
(error "No recently-killed files to reopen")))
(defun reopen-killed-file-fancy ()
"Pick a file to revisit from a list of files killed during this
Emacs session."
(interactive)
(if killed-file-list
(let ((file (completing-read "Reopen killed file: " killed-file-list
nil nil nil nil (car killed-file-list))))
(when file
(setq killed-file-list (cl-delete file killed-file-list :test #'equal))
(find-file file)))
(error "No recently-killed files to reopen")))
(defun my-save-word ()
(interactive)
(let ((current-location (point))
(word (flyspell-get-word)))
(when (consp word)
(flyspell-do-correct 'save nil (car word) current-location (cadr word) (caddr word) current-location))))
(defun smarter-move-beginning-of-line (arg)
"Move point back to indentation of beginning of line.
Move point to the first non-whitespace character on this line.
If point is already there, move to the beginning of the line.
Effectively toggle between the first non-whitespace character and
the beginning of the line.
If ARG is not nil or 1, move forward ARG - 1 lines first. If
point reaches the beginning or end of the buffer, stop there."
(interactive "^p")
(setq arg (or arg 1))
;; Move lines first
(when (/= arg 1)
(let ((line-move-visual nil))
(forward-line (1- arg))))
(let ((orig-point (point)))
(back-to-indentation)
(when (= orig-point (point))
(move-beginning-of-line 1))))
;; remap C-a to `smarter-move-beginning-of-line'
(global-set-key [remap move-beginning-of-line]
'smarter-move-beginning-of-line)
;;** Delete functions
(defun backward-delete-word (arg)
"Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (backward-word arg) (point))))
(defun delete-word (arg)
"Delete characters forwards until encountering the beginning of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (forward-word arg) (point))))
(defun backward-delete-line (arg)
"Delete (not kill) the current line, backwards from cursor.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (beginning-of-visual-line arg) (point))))
(defun delete-line (arg)
"Delete (not kill) the current line, forwards from cursor.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (end-of-visual-line arg) (point))))
(defun my-delete-whole-line (&optional arg)
"Delete current line.
With prefix ARG, delete that many lines starting from the current line.
If ARG is negative, delete backward. Also delete the preceding newline.
\(This is meant to make \\[repeat] work well with negative arguments.)
If ARG is zero, delete current line but exclude the trailing newline."
(interactive "p")
(or arg (setq arg 1))
(if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
(signal 'end-of-buffer nil))
(if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
(signal 'beginning-of-buffer nil))
(cond ((zerop arg)
;; I just need to understand elisp to discard what it's not needed
(save-excursion
(delete-region (point) (progn (forward-visible-line 0) (point))))
(delete-region (point) (progn (end-of-visible-line) (point))))
((< arg 0)
(save-excursion
(delete-region (point) (progn (end-of-visible-line) (point))))
(delete-region (point)
(progn (forward-visible-line (1+ arg))
(unless (bobp) (backward-char))
(point))))
(t
(save-excursion
(delete-region (point) (progn (forward-visible-line 0) (point))))
(delete-region (point)
(progn (forward-visible-line arg) (point))))))
(defun duplicate-current-line (arg)
"Duplicate current line, leaving point in lower line."
(interactive "*p")
;; save the point for undo
(setq buffer-undo-list (cons (point) buffer-undo-list))
;; local variables for start and end of line
(let ((bol (save-excursion (beginning-of-line) (point)))
eol)
(save-excursion
;; don't use forward-line for this, because you would have
;; to check whether you are at the end of the buffer
(end-of-line)
(setq eol (point))
;; store the line and disable the recording of undo information
(let ((line (buffer-substring bol eol))
(buffer-undo-list t)
(count arg))
;; insert the line arg times
(while (> count 0)
(newline) ;; because there is no newline in 'line'
(insert line)
(setq count (1- count))))
;; create the undo information
(setq buffer-undo-list (cons (cons eol (point)) buffer-undo-list))))) ; end-of-let
;; put the point in the lowest line and return
;; (next-line arg)
(defun replace-next-line ()
"Replace the next line with kill ring."
(interactive)
(save-excursion
(forward-line)
(beginning-of-line)
(delete-line 1)
(yank)))
(defun replace-current-line ()
"Replace current line with kill ring"
(interactive)
(beginning-of-line)
(delete-line 1)
(yank))
(defun paste-primary-selection ()
(interactive)
(insert (gui-get-primary-selection)))
(defun yank-at-point ()
"Yank but do not move the point"
(interactive)
(yank)
(pop-global-mark))
(defun smart-open-line ()
"Insert an empty line after the current line.
Position the cursor at its beginning, according to the current mode."
(interactive)
(move-end-of-line nil)
(newline-and-indent))
(defun switch-to-last-buffer ()
(interactive)
(switch-to-buffer nil))
(defun create-scratch-buffer nil
"create a scratch buffer"
(interactive)
(switch-to-buffer (get-buffer-create "*scratch*"))
(lisp-interaction-mode))
(defun mymy-lispy-different ()
"Exchange point between parenthesis."
(interactive)
(cond ((looking-at "\\s\(")
(forward-list))
((looking-back "\\s\)")
(backward-list))
((looking-at "\\(\"\\|'\\)")
(forward-sexp))
((looking-back "\\(\"\\|'\\)")
;; Has problem with actually going to the other side.
(backward-sexp))
((derived-mode-p 'web-mode)
(if (called-interactively-p)
(call-interactively #'web-mode-navigate)
(funcall #'web-mode-navigate)))
(t nil)))
;;* Ryo modal
(use-package ryo-modal
:ensure t
:demand t
;; DONE: Make ryo-modal-key create a function when passed a lambda
;; The name of the function will be from the keyword :lambda-name
;; So ryo-modal-key will check if TARGET is a lambda and then theck
;; if lambda-name exists, if not then complain
:commands ryo-modal-mode
:bind
;; ("S-SPC" . ryo-modal-global-mode)
("ç" . ryo-modal-global-mode)
("C-t" . ryo-modal-global-mode)
:config
(setq ryo-modal-cursor-color nil)
(setq ryo-modal-cursor-type 'hollow)
(defun mymy-ryo-modal-update-default-after-theme-change-ad (&rest _)
"If the theme is changed after loading ryo modal, then the default
cursor face is going to be configured as the previous theme cursor
face. This advice is to hook on the function `enable-theme'"
(setq ryo-modal-default-cursor-color (face-attribute 'cursor :background)))
(advice-add #'enable-theme :after #'mymy-ryo-modal-update-default-after-theme-change-ad)
;; Raise Ryo Modal map priority
;; Cannot do that, since ryo modal relies on making other modes to make its magic
;; (add-to-ordered-list 'emulation-mode-map-alists `((ryo-modal-mode . ,ryo-modal-mode-map)))
:config
(require 'ryo-modal-patch)
(defvar ryo-modal-excluded-modes '()
"A list of modes that the global mode should exclude.
By default the minibuffer is excluded.")
(define-globalized-minor-mode ryo-modal-global-mode ryo-modal-mode
(lambda ()
(when (and (not (minibufferp (current-buffer)))
(not (member major-mode ryo-modal-excluded-modes)))
(ryo-modal-mode t))))
:config
;; r, s, R, S are reserved for major modes
(with-eval-after-load 'iedit-mode
(ryo-modal-key "f" #'iedit-mode))
(defun mymy-downcase-char (arg)
(interactive "p")
(save-excursion
(downcase-region (point) (progn (forward-char arg) (point)))))
(ryo-modal-keys
("C-\\" toggle-input-method :exit 1)
("gd" remember)
("gD" remember-notes)
("gi" quoted-insert)
("/" undo)
("p" keyboard-quit)
("," ryo-modal-repeat)
("m" newline)
("M" (("o" split-line)
("m" smart-open-line)
("n" open-line)))
("z" (("u" upcase-char)
("U" upcase-region)
("v" upcase-initials-region)
("n" mymy-downcase-char)
("N" downcase-region)
("+" increment-number-at-point)
("-" decrement-number-at-point)))
("Z" (("v" fill-region)
("s" my-save-word))))
(ryo-modal-keys
("ot" (("s" bookmark-set)
("d" bookmark-delete))))
(ryo-modal-keys ;;; Navigation
(:norepeat t)
("n" next-line)
("u" previous-line)
("N" backward-char)
("U" forward-char)
("M-n" forward-sentence)
("M-u" backward-sentence)
("a" smarter-move-beginning-of-line)
("e" end-of-line)
("A" point-to-register)
;; ("E" jump-to-register)
("E" mymy-lispy-different)
;; ("c" forward-same-syntax)
("c" forward-to-word)
("C" forward-word)
("X" backward-to-word)
("x" backward-word)
("g" (("a" beginning-of-buffer)
("e" end-of-buffer)
("u" scroll-down)
("n" scroll-up))))
;; delete
(ryo-modal-keys
("D" (("t" my-delete-whole-line)
("d" backward-delete-word)
("v" delete-region)
("f" delete-word)
("s" delete-char)
("r" backward-delete-char)
("h" delete-line)
("(" delete-pair)
("SPC" delete-horizontal-space)
("/" delete-blank-lines))))
;; kill
(ryo-modal-keys
("k" kill-line)
("K" (("a" kill-whole-line)
("v" kill-region)
("w" kill-ring-save))))
;; yank
(ryo-modal-keys
("y" yank)
("Y" (("an" duplicate-current-line)
("nd" replace-next-line)
("ad" replace-current-line)
("m" paste-primary-selection)
("M" yank-at-point))))
;; Goodies
(ryo-modal-keys
("o" (("k" (("u" kill-buffer)
("n" switch-to-last-buffer)
("c" kill-current-buffer)
("s" save-buffer :norepeat t)
("l" create-scratch-buffer)
("x" reopen-killed-file)
("X" reopen-killed-file-fancy)) :name "Buffers, Files & and M-x")
("a" (("t" ignore :read t :name "insert text")
("c" comment-dwim :name "Comment")
("i" string-rectangle)) :name "Insert")
("w" (("r" split-window-below)
("s" split-window-right)
("d" delete-window)
("a" delete-other-windows)
("t" make-frame)) :name "Windows"))))
;; digit-arguments
(ryo-modal-keys
;; first argument to ryo-modal-keys may be a list of keywords.
;; these keywords will be applied to all keybindings.
(:norepeat t)
("0" "M-0")
("1" "M-1")
("2" "M-2")
("3" "M-3")
("4" "M-4")
("5" "M-5")
("6" "M-6")
("7" "M-7")
("8" "M-8")
("9" "M-9"))
;; Region
(ryo-modal-keys
(:norepeat t)
("v" set-mark-command)
)
;; emacs-lisp
(ryo-modal-major-mode-keys
'emacs-lisp-mode
("s" (("s" eval-buffer)
("r" eval-region)
("f" eval-defun)))))
(use-package yasnippet
:after ryo-modal
:ensure t
;; :bind (("TAB" . yas-expand))
:config
;; (add-to-list 'company-backends 'company-yasnippet t)
;; (add-to-list 'warning-suppress-types '(yasnippet backquote-change))
(defun mymy-yasnippet-hook ()
(yas-define-snippets
'prog-mode
'(;; "Key" "Template" "Name"
; git1: To give context the moment I do a commit
; git2: Make "g" snippet use comment depending on the mode
("g" "`(org-trim comment-start)` git: " "Git reminder")))
(yas-define-snippets
'emacs-lisp-mode
'(;; "Key" "Template" "Name"
("up" "(use-package ${1:package-name}$0)" "use-package")
(":c" ":config" "Use package :config")
(":i" ":init" "Use package :init")
(":b" ":bind (($1))" "Use package :bind")
(":bd" "(\"${1:keys}\" . ${2:function})" "Use package :bind binding")
(":s" ":ensure " "Use package :straigth")
(":ss" ":ensure (:host github :type git :repo \"${1:repo}\" :files (${2:files}))" ":ensure repo")
(":h" ":hook" "Use package :hook")))
(yas-define-snippets
'python-mode
'(;; "Key" "Template" "Name"
("pr" "print(${1:`(when (region-active-p) (yas-selected-text))`})$0" "print")))
(yas-define-snippets
'org-mode
'(;; "Key" "Template" "Name"
("clj" "#+begin_src clojure\n\n#+end_src\n" "Clojure source block")
("<co" "#+STARTUP: content" "Content")
("<n" "#+STARTUP: nofold" "No fold")
;; Git: More easy to type
(">el" "\\begin{${1:env}}\n\\end{${1:$(yas-field-value 1)}}" "Enviroment")
(">a" "align" "Aling env")
(">a*" "align*" "Align* env")
(">e" "equation" "Equation env")
(">e*" "equation*" "Equation* env")
(">>frac" "\\frac{${1:a}}{${2:b}}" "Fraction")
(">>div" "{${1:a} \\div ${2:b}}" "Division")
(">>cdot" "{${1:a} \\cdot ${2:b}}" "Multiplication")
(">>=" "{${1:a} = ${2:b}}" "Equality")
(">>nck" "{${1:n} \\choose ${2:k}}" "Choose function")
(">>int" "\\int_{${1:a}}^{${2:b}} ${3:f(x)} \\,dx" "Integrals")
(">>sum" "\\sum_{${1:a}}^{${2:b}}" "Summation"))))
(add-hook 'prog-mode-hook #'yas-minor-mode)
;; (add-hook 'prog-mode-hook #'yas-minor-mode)
;; (add-hook 'text-mode-hook #'yas-minor-mode)
;; (add-hook 'yas-minor-mode-hook #'mymy-yasnippet-hook)
(ryo-modal-keys
("gy" yas-visit-snippet-file)
("Gy" yas-insert-snippet))