-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.el
3155 lines (2603 loc) · 97.9 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 --- Initialization file
;;; Commentary:
;;; Code:
(defvar ff/emacs-start-time (current-time))
;; * Utilities
;; This section contains base utilities upon which the rest builds
;; ** Minimal base
;; Avoid Garbage Collections (they're slow, and RAM is cheap nowadays)
(setq gc-cons-threshold 50000000)
(add-hook 'emacs-startup-hook 'ff/set-gc-threshold)
(defun ff/set-gc-threshold ()
"Set `gc-cons-threshold' back to its default value."
(setq gc-cons-threshold 800000))
;; *** Lisp utilities
(defmacro with-timer (title &rest forms)
"Run the given FORMS, counting the elapsed time.
A message including the given TITLE and the corresponding elapsed
time is displayed."
(declare (indent 1))
(let* ((nowvar (make-symbol "now"))
(errorvar (make-symbol "error"))
(body `(progn
,@forms
(setq ,errorvar nil))))
`(let ((,nowvar (current-time))
(,errorvar t))
(message "%s..." ,title)
(unwind-protect ,body
(let ((elapsed
(float-time (time-subtract (current-time) ,nowvar))))
(message "%s...%s (%.3fs)" ,title (if ,errorvar "FAILED" "done") elapsed))))))
(defmacro progn-safe (title &rest forms)
"Run the given FORMS, gracefully demoting errors to warnings.
A TITLE is used to identify the block in the logs."
(declare (indent 1))
(let ((errvar (make-symbol "err")))
`(condition-case-unless-debug ,errvar
(progn
,(when debug-on-error (list 'message "%s..." title))
,@forms)
(error
(ignore
(message "%s: FAILED" ,title)
(display-warning
'progn-safe
(format "%s: %s" ,title ,errvar)
:error))))))
(defmacro with-timer-safe (title &rest forms)
"Fault-tolerant version of `with-timer'."
(declare (indent 1))
`(progn-safe ,title
(with-timer ,title
,@forms)))
(progn-safe "Highlighting in *Messages* buffer (startup times & errors)"
(font-lock-add-keywords
'messages-buffer-mode
'(("([[:digit:]]+.[[:digit:]]+s)" 0 font-lock-constant-face t)))
(font-lock-add-keywords
'messages-buffer-mode
'(("FAILED" 0 font-lock-warning-face t)))
(with-current-buffer "*Messages*"
(font-lock-fontify-buffer)))
;; *** Filesystem hierarchy
(setq user-init-file (or load-file-name
(buffer-file-name)))
(setq user-emacs-directory (file-name-directory
user-init-file))
(eval-and-compile
(defun ff/emacsd (name)
"Path to a file named NAME in `user-emacs-directory'."
(expand-file-name (concat user-emacs-directory name))))
(add-to-list 'load-path (ff/emacsd "lisp"))
;; **** Configuration files
(defun ff/load-configuration (name)
"Load the configuration for package NAME.
This configuration is located in a file named `setup-NAME.el`
under `user-emacs-directory'."
(load-file (ff/emacsd (concat "elisp/setup-" name ".el"))))
;; **** Persistency files
(defun ff/variable-file (name)
"Path to a variable file of given NAME.
Variable files are located in the \"var\" subdirectory of `user-emacs-directory'"
(expand-file-name (concat user-emacs-directory "var/" name)))
;; ** Packages management
;; *** Configuration
(eval-when-compile
(add-to-list 'load-path (ff/emacsd "packages/use-package"))
(require 'use-package)
(setq use-package-verbose t))
(progn-safe "Disable deferring for testing purposes"
(when debug-on-error
(advice-add
'use-package-handler/:config :before
(lambda (name keyword arg rest state)
(plist-put state :deferred nil)))))
(progn-safe "Highlighting in *Messages* buffer (use-package)"
(font-lock-add-keywords
'messages-buffer-mode
'(("Could not load .*" 0 font-lock-warning-face t)))
(with-current-buffer "*Messages*"
(font-lock-fontify-buffer)))
;; *** Dependencies
(use-package package
:init
(defvar ff/updating-elpa-cache nil
"True when updating the ELPA cache")
(defvar ff/elpa-cache-file (ff/variable-file "elpa-cache.el")
"Location of the ELPA cache")
(setq package-archives
'(("melpa" . "http://melpa.org/packages/")
("gnu" . "http://elpa.gnu.org/packages/"))))
(with-timer-safe "Configuring ELPA"
(defun ff/package-initialize ()
(with-timer "Fully initializing ELPA"
(package-initialize))
(message "Starting ELPA cache update...")
(setq ff/updating-elpa-cache (current-time))
(let ((default-directory user-emacs-directory))
(set-process-sentinel
(start-process "elpa-cache" "*elpa-cache*"
"make" "elpa-update")
(lambda (process event)
(message "Starting ELPA cache update...%s"
(cond ((process-live-p process)
(format " live process received event `%s'" event))
((eq 'signal (process-status process))
(format " process killed (%d)" (process-exit-status process)))
((eq 'exit (process-status process))
(if (= 0 (process-exit-status process))
(format "complete (%.3fs)"
(float-time (time-subtract
(current-time)
ff/updating-elpa-cache)))
(display-warning
'ff/package-initialize
(format
"failed to update ELPA cache; see buffer `%s' for details"
(process-buffer process))
:error)
(format "FAILED (%d)" (process-exit-status process))))
(t
(format " process received event `%s'" event))))))))
(cond
(ff/updating-elpa-cache
(progn
(message "Update ELPA cache")
;; Update ELPA cache -- this is left unprotected as it is always run in
;; another process and we want errors to be reported
(package-initialize)
(with-temp-buffer
(pp
(apply
#'list
'progn
`(setq package-alist ',package-alist)
`(setq package--initialized t)
(mapcar (lambda (dir)
`(add-to-list 'load-path ,dir))
load-path))
(current-buffer))
(write-file ff/elpa-cache-file))))
(noninteractive
(progn
(message "Full ELPA initialization")
(package-initialize)))
(t
;; Try loading the cache for faster startup
(condition-case nil
(progn
(message "Trying fast ELPA setup...")
(load ff/elpa-cache-file)
(unless package--initialized
(error "Package.el left uninitialized"))
(run-with-idle-timer 1 nil #'ff/package-initialize)
(message "Trying fast ELPA setup...done"))
(error
(message "Trying fast ELPA setup...FAILED")
(ff/package-initialize))))))
;; ** Base tools
(use-package htmlize :ensure t :defer t)
(use-package f :ensure t :defer t)
(use-package s :ensure t :defer t)
(use-package dash
:ensure t
:defer t
:commands (--remove
-remove
-reduce-from
-map
-sort
-find-index))
;; *** Key bindings
(eval-when-compile (require 'help-mode))
(defun list-known-bindings (key &optional no-load)
(interactive "kList known bindings for key: \nP")
(when (and (not no-load)
(yes-or-no-p (concat "Try to load every known library"
" (this might take some resources)?")))
(let ((nfeatures 0))
(while (not (= nfeatures (length features)))
(setq nfeatures (length features))
(message "%d features loaded" nfeatures)
(mapatoms (lambda (sym)
(when (and (boundp sym)
(autoloadp (symbol-function sym)))
(autoload-do-load (symbol-function sym))))))))
(with-current-buffer (get-buffer-create "*known bindings*")
(help-mode)
(let ((buffer-read-only nil))
(erase-buffer)
(mapatoms (lambda (sym)
(when (or (eq sym 'global-map)
(and (boundp sym)
(symbol-value sym)
(s-ends-with-p "-mode-map" (symbol-name sym))
(keymapp (symbol-value sym))))
(let ((binding (lookup-key (symbol-value sym) key t)))
(when (and binding
(not (numberp binding)))
(insert (format "%-40s `%s'\n"
(format "`%s'" sym)
(if (keymapp binding)
"KEYMAP"
binding))))))))
(sort-lines nil (point-min) (point-max))
(goto-char (point-min))
(insert
(format "Known bindings for key: %s\n\n" (key-description key))
(format "%-40s %s" "Map" "Binding\n")
(s-repeat 40 "-") " " (s-repeat 30 "-") "\n"))
(let ((help-xref-following t))
(setq help-xref-stack nil
help-xref-forward-stack nil)
(help-make-xrefs)
(help-setup-xref (cons 'list-known-bindings (list key t)) t))
(goto-char (point-min))
(display-buffer (current-buffer))))
;; **** Custom global key bindings
;; Inspired from http://stackoverflow.com/q/683425
(progn-safe "Custom bindings"
(define-minor-mode custom-bindings-mode
"Install custom key bindings.
\\{custom-bindings-mode-map}"
:global t
:init-value t
:keymap (make-sparse-keymap))
(defun custom-set-key (key command)
"Bind KEY to COMMAND in `custom-bindings-mode'."
(define-key custom-bindings-mode-map key command))
;; Ensure `custom-bindings-mode' always has precendence
(advice-add
'load :after
(defun custom-bindings--load-advice--promote (&rest args)
"Make sure MODE appears first in `minor-mode-map-alist'.
This ensures no key bindings defined in MODE can be shadowed by
another minor-mode."
(let* ((mode 'custom-bindings-mode)
(cell (assq mode minor-mode-map-alist)))
(setq minor-mode-map-alist
(cons cell
(assq-delete-all mode minor-mode-map-alist)))))))
;; **** Repeatable commands
;; (http://stackoverflow.com/a/17310748/1225607)
(use-package repeat
:config
(defun make-repeatable-command (cmd)
"Return a new command that is a repeatable version of CMD.
The new command is named CMD-repeat. CMD should be a quoted
command.
This allows you to bind the command to a compound keystroke and
repeat it with just the final key. For example:
(global-set-key (kbd \"C-c a\") (make-repeatable-command 'foo))
will create a new command called foo-repeat. Typing `C-c a' will
just invoke foo. Typing `C-c a a a' will invoke foo three times,
and so on."
(let ((repeatable-command (intern (concat (symbol-name cmd) "/repeat"))))
(fset repeatable-command
`(lambda ,(help-function-arglist cmd)
,(format "A repeatable version of `%s'." (symbol-name cmd))
,(interactive-form cmd)
;; see also repeat-message-function
(setq last-repeatable-command ',cmd)
(repeat nil)))
repeatable-command)))
;; **** Commands dispatching
(use-package hydra
:ensure t)
(progn-safe "Dispatching commands (alternatives)"
(eval-when-compile
(require 's))
(defmacro ff/define-alternatives (name docstring &rest body)
(declare (indent defun))
`(prog1
(defun ,name (&optional argp)
,(apply
'concat
(s-chop-suffixes
'(":" " ")
(let ((lines (s-lines docstring)))
(if (string= "" (first lines))
(second lines)
(first lines))))
" (with alternatives).\n\n"
(format "When called without argument, call `%s'.\n\n"
(second (first body)))
"Otherwise (if ARGP is set), offer a choice between the following commands:\n"
(mapcar (lambda (cmd)
(format "- `%s'\n"
(s-truncate 30 (format "%s" (second cmd)))))
body))
(interactive "P")
(if (null argp)
(call-interactively #',(second (first body)))
(call-interactively (get ',name :alternate-hydra))))
(put ',name :alternate-hydra
(defhydra ,(intern (format "%s--hydra" name))
(:exit t)
,docstring
,@body)))))
;; *** Local rc files
(with-timer-safe "Loading local rc files"
(let* ((fullhostname (system-name))
(hostname (substring fullhostname 0
(progn
(string-match "\\." (concat fullhostname ".domain"))
(- (match-end 0) 1)))))
(load (concat user-emacs-directory "elisp/host-" hostname) 'noerror)))
;; * General settings
;; This section contains everything which is not directly related to text
;; editing.
(progn-safe "General settings"
;; Customization file
(setq custom-file (ff/variable-file "custom.el"))
(with-timer "Loading custom file"
(load custom-file 'noerror 'nomessage))
;; don't suspend emacs on C-z (but C-x C-z still works)
(global-set-key (kbd "C-z") nil))
;; ** Look
;; *** Interface
(progn-safe "Interface"
;; Bare UI
(menu-bar-mode -1)
(when (display-graphic-p)
(declare-function scroll-bar-mode nil)
(scroll-bar-mode -1)
(tool-bar-mode -1))
;; Startup
(setq initial-scratch-message "") ;; Empty scratch buffer
(setq initial-major-mode 'fundamental-mode) ;; ... in fundamental-mode
(let ((scratch (get-buffer "*scratch*")))
(when scratch
(with-current-buffer scratch
(funcall initial-major-mode))))
(setq inhibit-splash-screen t) ;; No fancy splash screen
;; Show line and column numbers
(column-number-mode 1)
(line-number-mode 1)
;; Window title
(setq-default frame-title-format (list "%b - Emacs"))
;; Fringes
(setq-default indicate-buffer-boundaries 'left)
;; Margins
(let ((m-w 1))
(setq-default left-margin-width m-w
right-margin-width m-w)))
;; *** GUI Theme
;; Tango color theme
(use-package naquadah-theme
:ensure t
:if (display-graphic-p)
:config
(load-theme 'naquadah t)
(use-package term
:defer t
:config
(set-face-attribute 'term-color-red nil :foreground "#ef2929")
(set-face-attribute 'term-color-green nil :foreground "#73d216")
(set-face-attribute 'term-color-yellow nil :foreground "#edd400")
(set-face-attribute 'term-color-blue nil :foreground "#729fcf")
(set-face-attribute 'term-color-magenta nil :foreground "#ad7fa8")
(set-face-attribute 'term-color-cyan nil :foreground "#c17d11")
(set-face-attribute 'term-color-white nil :foreground "#babdb6")
(set-face-attribute 'term-color-black nil :background "#262b2c"))
(custom-set-faces
'(fringe ((t (:background "#1f2324")))))
;; Font
(push '(font-backend . "xft") default-frame-alist)
(push '(font . "Iosevka-12") default-frame-alist)
(setq prettify-symbols-unprettify-at-point 'right-edge)
(defun refresh-pretty ()
(prettify-symbols-mode -1)
(prettify-symbols-mode +1))
;; Hooks for modes in which to install the Iosevka ligatures
(ff/load-configuration "iosevka")
(mapc (lambda (hook)
(add-hook hook (lambda () (setup-iosevka-ligatures) (refresh-pretty))))
'(text-mode-hook prog-mode-hook))
(global-prettify-symbols-mode +1))
(defun ff/demo (font-size)
(interactive (list (read-from-minibuffer "Font size: " "14")))
(mapc #'disable-theme custom-enabled-themes)
(push (cons 'font (concat "Iosevka-" font-size)) default-frame-alist)
(with-selected-frame (make-frame)
(sit-for 0)
(delete-other-frames))
(setq sml/theme 'automatic)
(sml/setup)
(require 'term)
(set-face-attribute 'term-color-red nil :foreground "red3")
(set-face-attribute 'term-color-green nil :foreground "green3")
(set-face-attribute 'term-color-yellow nil :foreground "yellow3")
(set-face-attribute 'term-color-blue nil :foreground "blue2")
(set-face-attribute 'term-color-magenta nil :foreground "magenta3")
(set-face-attribute 'term-color-cyan nil :foreground "cyan3")
(set-face-attribute 'term-color-white nil :foreground "white")
(set-face-attribute 'term-color-black nil :background "black"))
;; *** Mode line
(use-package smart-mode-line
:ensure t
:defer 2
:commands (;; BC only
sml/faces-from-theme
sml/theme-p)
:config
(setq mode-line-position nil)
(sml/setup)
;; Shift `mode-line-misc-info' a bit to the left
(add-to-list 'mode-line-misc-info " " 'append))
(use-package diminish
:ensure t)
(progn-safe "Rename major modes in the mode line"
;; From Magnar Sveen:
;; http://whattheemacsd.com/appearance.el-01.html
(defmacro rename-modeline (package-name mode new-name)
`(eval-after-load ,package-name
'(defadvice ,mode (after rename-modeline activate)
(setq mode-name ,new-name))))
(rename-modeline "lisp-mode" emacs-lisp-mode "EL"))
;; ** Windows management
;; *** Switch between windows
;; Use S-<arrows> to switch between windows
(use-package windmove
:config
(windmove-default-keybindings)
(setq windmove-wrap-around t))
;; Reclaim S-<arrows> keys in org-related mode
(use-package org
:defer t
:config
;; Left/Right are directly reclaimed
(define-key org-mode-map (kbd "S-<left>") nil)
(define-key org-mode-map (kbd "S-<right>") nil)
;; Up/Down are kept for places where
;; they are useful (dates and such)
(add-hook 'org-shiftup-final-hook 'windmove-up)
(add-hook 'org-shiftdown-final-hook 'windmove-down)
;; Same thing for org-agenda
(use-package org-agenda
:defer t
:config
(define-key org-agenda-mode-map (kbd "S-<left>") nil)
(define-key org-agenda-mode-map (kbd "S-<right>") nil)
(define-key org-agenda-mode-map (kbd "S-<up>") nil)
(define-key org-agenda-mode-map (kbd "S-<down>") nil)))
;; *** Manage window configurations
;; Navigate through window layouts with C-c <arrows>
(use-package winner
:defer 2
:commands winner-undo
:config
(winner-mode 1)
(custom-set-key
(kbd "C-c <left>")
(make-repeatable-command #'winner-undo)))
;; *** Resize windows
(progn-safe "Resize windows"
(custom-set-key (kbd "C-x {")
(make-repeatable-command 'shrink-window-horizontally))
(custom-set-key (kbd "C-x }")
(make-repeatable-command 'enlarge-window-horizontally))
;; `make-repeatable-command' doesn't work with C functions
(custom-set-key (kbd "C-x ^")
(make-repeatable-command
(defun ff/enlarge-window (size)
"Lisp wrapper around `enlarge-window'"
(interactive "p")
(enlarge-window size)))))
;; *** Swap windows
(progn-safe "Swap windows"
;; Adapted from Magnar Sveen:
;; http://whattheemacsd.com/buffer-defuns.el-02.html
(defun ff/rotate-windows (count)
"Perform COUNT circular permutations of the windows."
(interactive "p")
(let* ((non-dedicated-windows (-remove 'window-dedicated-p (window-list)))
(num-windows (length non-dedicated-windows))
(i 0)
(step (+ num-windows count)))
(cond ((not (> num-windows 1))
(message "You can't rotate a single window!"))
(t
(dotimes (counter (- num-windows 1))
(let* ((next-i (% (+ step i) num-windows))
(w1 (elt non-dedicated-windows i))
(w2 (elt non-dedicated-windows next-i))
(b1 (window-buffer w1))
(b2 (window-buffer w2))
(s1 (window-start w1))
(s2 (window-start w2)))
(set-window-buffer w1 b2)
(set-window-buffer w2 b1)
(set-window-start w1 s2)
(set-window-start w2 s1)
(setq i next-i)))))))
(defun ff/rotate-windows-backwards (count)
"Perform COUNT circular permutations of the windows.
Rotation is done in the opposite order as `ff/rotate-windows'."
(interactive "p")
(ff/rotate-windows (- count)))
(custom-set-key (kbd "H-<left>") 'ff/rotate-windows)
(custom-set-key (kbd "H-<right>") 'ff/rotate-windows-backwards))
;; *** Hydra to wrap all this
(progn-safe "Window management hydra"
(defvar ff/key² (kbd "²")
"Key in the leftmost position of the number row.
Labeled `²' in French keyboards layouts.")
(custom-set-key
ff/key²
(defhydra windows-hydra (:color amaranth)
"
Resize windows ^^^^ Switch to ^^ Window configuration
---------------^^^^ -----------^^ --------------------
^ _<kp-8>_ ^ _b_uffer _0_: delete window
_<kp-4>_ ✜ _<kp-6>_ _f_ile _1_: delete others
^ _<kp-5>_ ^ _r_ecent file _2_: split above/below
^ ^ ^ ^ ^ ^ _3_: split left-right
_=_: balance^ ^ ^ ^ _u_ndo
"
;; Move
("S-<left>" windmove-left nil)
("<left>" windmove-left nil)
("S-<right>" windmove-right nil)
("<right>" windmove-right nil)
("S-<up>" windmove-up nil)
("<up>" windmove-up nil)
("S-<down>" windmove-down nil)
("<down>" windmove-down nil)
("SPC" other-window nil)
;; Rotate windows
("H-<left>" ff/rotate-windows nil)
("H-<right>" ff/rotate-windows-backwards nil)
;; Resize windows
("<kp-4>" shrink-window-horizontally nil)
("<kp-6>" enlarge-window-horizontally nil)
("<kp-8>" shrink-window nil)
("<kp-5>" enlarge-window nil)
("=" balance-windows nil)
;; Change configuration
("<kp-0>" delete-window nil)
("0" delete-window nil)
("à" delete-window nil)
("<kp-1>" delete-other-windows nil)
("1" delete-other-windows nil)
("&" delete-other-windows nil)
("<kp-2>" split-window-below nil)
("2" split-window-below nil)
("é" split-window-below nil)
("<kp-3>" split-window-right nil)
("3" split-window-right nil)
("\"" split-window-right nil)
("u" winner-undo nil)
;; Switch buffers / files
("b" ido-switch-buffer nil)
("f" ido-find-file nil)
("r" helm-recentf nil)
;; Quit
("q" nil "quit" :color blue))))
;; *** Avoid losing the cursor
(use-package beacon
:ensure t
:defer 2
:config
(beacon-mode 1))
;; ** Buffers management
;; *** Scratch buffers
(use-package scratch
:load-path (lambda () (ff/emacsd "packages/scratch"))
:diminish (scratch-mode " #"))
;; *** Find-file and switch-to-buffer in other window
(progn-safe "Prefix argument for find-file & the like"
(custom-set-key
(kbd "C-x C-f")
(ff/define-alternatives ff/find-file
"
Open file:
_c_urrent window other _w_indow _l_iterally
_d_isplay other _f_rame _r_ead only
"
("c" find-file nil)
("d" ido-display-file nil)
("w" find-file-other-window nil)
("f" find-file-other-frame nil)
("l" find-file-literally nil)
("r" find-file-read-only nil)))
(custom-set-key
(kbd "C-x b")
(ff/define-alternatives ff/switch-to-buffer
"
Switch to buffer:
_c_urrent window other _w_indow _s_cratch buffer
_d_isplay other _f_rame _S_cratch buffer (named)
"
("c" scratch-switch-to-buffer nil)
("d" display-buffer nil)
("w" switch-to-buffer-other-window nil)
("f" switch-to-buffer-other-frame nil)
("S" scratch-create nil)
("s" (let ((current-prefix-arg nil))
(call-interactively #'scratch-create))
nil))))
;; *** Uniquify buffer names
(use-package uniquify
:config
(setq uniquify-buffer-name-style 'post-forward
uniquify-separator ":"))
;; *** Buffer switching
;; If a buffer is displayed in another frame, raise it
(setq-default display-buffer-reuse-frames t)
(use-package ibuffer
:defer t
:init (defalias 'list-buffers 'ibuffer)
:config (ff/load-configuration "ibuffer"))
;; ** User interaction
(defalias 'yes-or-no-p 'y-or-n-p) ;; Don't bother typing 'yes'
;; *** recursive minibuffer
(progn-safe "Recursive minibuffer"
(setq enable-recursive-minibuffers t)
(minibuffer-depth-indicate-mode 1))
;; *** ido
(use-package ido
:defines ido-temp-list
:config
(ido-mode 1)
(ido-everywhere 1)
(put 'ido-exit-minibuffer 'disabled nil)
(setq ido-enable-flex-matching t)
(setq ido-auto-merge-work-directories-length -1)
(setq ido-create-new-buffer 'always)
(setq ido-use-filename-at-point 'guess)
(setq ido-default-buffer-method 'selected-window)
(add-hook 'ido-make-buffer-list-hook 'ff/ido-stars-end)
(defvar ff/ido-stars-end)
(defun ff/ido-stars-end ()
"Sort ido candidates to put \"starred\" buffers at the end."
(when (bound-and-true-p ff/ido-stars-end)
(ido-to-end (--filter (s-starts-with-p "*" it)
ido-temp-list))))
(defun ff/advice--ido-stars-end (orig-fun &rest args)
(let ((ff/ido-stars-end t))
(apply orig-fun args)))
(advice-add 'ido-switch-buffer :around 'ff/advice--ido-stars-end)
(advice-add 'scratch-switch-to-buffer :around 'ff/advice--ido-stars-end))
(use-package ido-completing-read+
:ensure t
:config
(ido-ubiquitous-mode 1))
;; *** helm
(use-package helm
:ensure t
:commands (helm-mini helm-recentf helm-M-x helm-imenu)
:init
(custom-set-key (kbd "C-x C-h") 'helm-mini)
(custom-set-key (kbd "C-x C-r") 'helm-recentf)
(custom-set-key (kbd "C-x M-x") 'helm-M-x)
(custom-set-key (kbd "C-x C-i") 'helm-imenu)
:config
(require 'helm-files))
;; *** smex
(use-package smex
:ensure t
:commands smex
:init
(custom-set-key (kbd "M-x") 'smex)
(setq smex-save-file (ff/variable-file "smex-items"))
:config
(smex-initialize))
;; *** Which-key
(use-package which-key
:ensure t
:defer 5
:diminish which-key-mode
:config
(which-key-setup-side-window-right-bottom)
(which-key-mode 1))
;; *** "Toggle" and "Run" key maps
(progn-safe "Toggle keymap"
;; Adapted from Artur Malabara (Endless Parentheses)
;; http://endlessparentheses.com/the-toggle-map-and-wizardry.html
(define-prefix-command 'ff/toggle-map)
(custom-set-key (kbd "C-c t") 'ff/toggle-map)
(define-key ff/toggle-map "e" #'emacs-lisp-mode)
(define-key ff/toggle-map "o" #'org-mode)
(define-key ff/toggle-map "l" #'linum-mode)
(define-key ff/toggle-map "v" #'visual-line-mode)
(define-key ff/toggle-map "d" #'toggle-debug-on-error)
(define-key ff/toggle-map "t" #'toggle-truncate-lines)
(define-key ff/toggle-map "r" #'auto-revert-mode))
(progn-safe "Run keymap"
;; Adapted from Artur Malabara (Endless Parentheses)
;; http://endlessparentheses.com/launcher-keymap-for-standalone-features.html
(define-prefix-command 'ff/run-map)
(custom-set-key (kbd "C-c r") 'ff/run-map)
(custom-set-key (kbd "M-r") 'ff/run-map)
(define-key ff/run-map "f" #'find-dired)
(define-key ff/run-map "g" #'rgrep)
(define-key ff/run-map "G" #'lgrep)
(define-key ff/run-map "m" #'woman)
(define-key ff/run-map "C" #'clone-indirect-buffer))
;; ** Persistency
;; *** Variable files
(progn-safe "Variable files"
(setq url-configuration-directory (ff/variable-file "url/"))
(setq auto-save-list-file-prefix (ff/variable-file "auto-save-list/"))
(setq tramp-persistency-file-name (ff/variable-file "tramp"))
(setq async-byte-compile-log-file (ff/variable-file "async-bytecomp.log")))
;; *** Sessions
(use-package desktop+
:ensure t
:commands (desktop+-load desktop+-create)
:init
(setq desktop+-base-dir (ff/variable-file "desktops/"))
(define-key ff/run-map (kbd "d")
(defhydra desktop+-hydra (:exit t)
"
Manage sessions:
_l_: load session _L_: load autonamed session
_c_: create session _C_: create autonamed session
"
("l" #'desktop+-load nil)
("L" #'desktop+-load-auto nil)
("c" #'desktop+-create nil)
("C" #'desktop+-create-auto nil)))
:config
(ido-ubiquitous-mode 1))
;; *** Recent files
(use-package recentf
:defer 2
:config
(setq recentf-max-saved-items 1000)
(setq recentf-auto-cleanup 60)
(setq recentf-save-file (ff/variable-file "recentf"))
(recentf-mode 1)
;; Add files already opened from the command-line to the recentf list
;; (this is only be necessary because of the deferred loading)
(mapc (lambda (buffer)
(when (buffer-file-name buffer)
(recentf-push (buffer-file-name buffer))))
(buffer-list))
(require 'sync-recentf))
(use-package sync-recentf
:ensure t
:defer t
:config
(use-package helm-for-files
:defer t
:config
(push `(candidates
. ,(lambda ()
(--remove (string= it sync-recentf-marker)
recentf-list)))
helm-source-recentf)))
;; * Text editing
;; This section contains everything related to text editing in general, i.e. not
;; tied to a particular major mode.
;; ** Global configuration
;; *** Enable commands
(progn-safe "Enable commands"
(put 'set-goal-column 'disabled nil) ;; (C-x C-n)
(put 'narrow-to-region 'disabled nil) ;; (C-x n n)
(put 'upcase-region 'disabled nil) ;; (C-x C-u)
(put 'downcase-region 'disabled nil) ;; (C-x C-l)
(put 'erase-buffer 'disabled nil))
;; *** General behaviour
(progn-safe "General editing behaviour"
(setq-default visible-bell t)
(show-paren-mode 1) ;; Parenthesis matching
(setq-default show-paren-style 'mixed) ;; Show the whole expression if it is too large
(setq-default shift-select-mode nil) ;; No shift selection
(setq-default diff-switches "-u") ;; Unified diffs
(define-coding-system-alias 'UTF-8 'utf-8)
;; Useful in combination with expand-region
(pending-delete-mode 1))
;; *** Key bindings
(progn-safe "Editing key bindings"
(custom-set-key (kbd "M-g") 'goto-line) ;; better keybinding for goto-line
(custom-set-key (kbd "C-x g") 'revert-buffer)
(custom-set-key (kbd "C-c q") (make-repeatable-command 'join-line)))
;; ** Point, mark & region handling
;; Easily cycle through the Mark Ring
(setq-default set-mark-command-repeat-pop t)
;; *** Move point
(progn-safe "Move between pages with M-pgUp / M-pgDn"
(global-set-key (kbd "M-<next>") #'forward-page)
(global-set-key (kbd "M-<prior>") #'backward-page))
(progn-safe "Move to BOL or indentation"
(defun ff/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.