summaryrefslogtreecommitdiffstats
path: root/init.el
blob: 3cede245a93a97ea082e0e65c84b2c7205a62711 (plain)
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
;;; init --- Test
;;; Commentary:

;;; Code:

;; -------- PACKAGE MANAGEMENT ---------------------------------------

(require 'package)

(setq package-archives '(("gnu"   . "https://elpa.gnu.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")))

(package-initialize)

(or (file-exists-p package-user-dir)
    (package-refresh-contents))

(defun ensure-package-installed (&rest packages)
  "Ensure every PACKAGES is installed.

Ask for installation if it’s
not.  Return a list of installed packages or nil for every skipped
package."
  (mapcar
   (lambda (package)
     (if (package-installed-p package)
         nil
       (package-install package))
     package)
   packages))

(ensure-package-installed 'use-package)
(ensure-package-installed 'diminish)

(require 'diminish)
(eval-when-compile (require 'use-package))

;; -------- UI SET-UP ------------------------------------------------
(scroll-bar-mode -1)
(tool-bar-mode -1)
(menu-bar-mode -1)
(setq inhibit-startup-screen t)
(setq use-dialog-box nil)

(setq lazy-highlight-initial-delay 0)

;; Disable blinking cursor
(blink-cursor-mode -1)
(setq scroll-margin 5
      scroll-conservatively 9999
      scroll-step 1)

;; Line numbers
(global-display-line-numbers-mode t)
(setq display-line-numbers-width-start t)
(setq column-number-mode t)

;; current line highlighting
(global-hl-line-mode t)

;; identing
(setq-default electric-indent-inhibit t)

;; Show 80th columns in prog-mode
(use-package fill-column-indicator
  :ensure t
  :diminish fci-mode
  :hook ((prog-mode . (lambda ()
                        (fci-mode)
                        (set-fill-column 80)))
         (org-mode . (lambda ()
                       (auto-fill-mode)
                       (fci-mode)
                       (set-fill-column 80)))))

;; Rainbow mode
(use-package rainbow-mode
  :defer t
  :ensure t)

;; Powerline
(use-package powerline
  :ensure t
  :config
  (powerline-center-evil-theme))

;; Ligatures
(use-package composite
  :defer t
  :init
  (defvar composition-ligature-table (make-char-table nil))
  :hook
  (((prog-mode text-mode coq-response-mode coq-goals-mode help-mode)
    . (lambda () (setq-local composition-function-table composition-ligature-table))))
  :config
  ;; support ligatures, some toned down to prevent hang
  (when (version<= "27.0" emacs-version)
    (let ((alist
           '((33 . ".\\(?:\\(==\\|[!=]\\)[!=]?\\)")
             (35 . ".\\(?:\\(###?\\|_(\\|[(:=?[_{]\\)[#(:=?[_{]?\\)")
             (36 . ".\\(?:\\(>\\)>?\\)")
             (37 . ".\\(?:\\(%\\)%?\\)")
             (38 . ".\\(?:\\(&\\)&?\\)")
             (42 . ".\\(?:\\(\\*\\*\\|[*>]\\)[*>]?\\)")
             ;; (42 . ".\\(?:\\(\\*\\*\\|[*/>]\\).?\\)")
             (43 . ".\\(?:\\([>]\\)>?\\)")
             ;; (43 . ".\\(?:\\(\\+\\+\\|[+>]\\).?\\)")
             (45 . ".\\(?:\\(-[->]\\|<<\\|>>\\|[-<>|~]\\)[-<>|~]?\\)")
             ;; (46 . ".\\(?:\\(\\.[.<]\\|[-.=]\\)[-.<=]?\\)")
             (46 . ".\\(?:\\(\\.<\\|[-=]\\)[-<=]?\\)")
             (47 . ".\\(?:\\(//\\|==\\|[=>]\\)[/=>]?\\)")
             ;; (47 . ".\\(?:\\(//\\|==\\|[*/=>]\\).?\\)")
             (48 . ".\\(?:\\(x[a-fA-F0-9]\\).?\\)")
             (58 . ".\\(?:\\(::\\|[:<=>]\\)[:<=>]?\\)")
             (59 . ".\\(?:\\(;\\);?\\)")
             (60 . ".\\(?:\\(!--\\|\\$>\\|\\*>\\|\\+>\\|-[-<>|]\\|/>\\|<[-<=]\\|=[<>|]\\|==>?\\||>\\||||?\\|~[>~]\\|[$*+/:<=>|~-]\\)[$*+/:<=>|~-]?\\)")
             (61 . ".\\(?:\\(!=\\|/=\\|:=\\|<<\\|=[=>]\\|>>\\|[=>]\\)[=<>]?\\)")
             (62 . ".\\(?:\\(->\\|=>\\|>[-=>]\\|[-:=>]\\)[-:=>]?\\)")
             (63 . ".\\(?:\\([.:=?]\\)[.:=?]?\\)")
             (91 . ".\\(?:\\(|\\)[]|]?\\)")
             ;; (92 . ".\\(?:\\([\\n]\\)[\\]?\\)")
             (94 . ".\\(?:\\(=\\)=?\\)")
             (95 . ".\\(?:\\(|_\\|[_]\\)_?\\)")
             (119 . ".\\(?:\\(ww\\)w?\\)")
             (123 . ".\\(?:\\(|\\)[|}]?\\)")
             (124 . ".\\(?:\\(->\\|=>\\||[-=>]\\||||*>\\|[]=>|}-]\\).?\\)")
             (126 . ".\\(?:\\(~>\\|[-=>@~]\\)[-=>@~]?\\)"))))
      (dolist (char-regexp alist)
        (set-char-table-range composition-ligature-table (car char-regexp)
                              `([,(cdr char-regexp) 0 font-shape-gstring]))))
    (set-char-table-parent composition-ligature-table composition-function-table)))

;; display whitespace (disable by default)
(use-package whitespace
  :ensure t
  :diminish whitespace-mode
  :config
  (setq whitespace-display-mappings
        '((tab-mark 9 [ 9] [92 9])
          (newline-mark 10 [182 10])
          (space-mark 32 [] [46])
          (space-mark ?\xA0 [?~] [46])))
  (setq whitespace-style '(face spaces tabs newline tab-mark newline-mark space-mark)))

(use-package all-the-icons
  :ensure t)

(use-package centaur-tabs
  :ensure t
  :after evil
  :config
  (setq centaur-tabs-style "bar")
  (setq centaur-tabs-set-bar 'under)
  (setq centaur-tabs-set-icons t)
  (setq centaur-tabs-height 32)
  (setq x-underline-at-descent-line t)
  (setq centaur-tabs-set-modified-marker t)
  (setq centaur-tabs-modified-marker "●")
  (setq centaur-tabs-cycle-scope 'tabs)
  (centaur-tabs-group-by-projectile-project)
  :hook
  (after-init . centaur-tabs-mode)
  :bind
  (:map evil-normal-state-map
        ("<C-tab>" . centaur-tabs-forward)
        ("<C-iso-lefttab>" . centaur-tabs-backward)))

;; -------- THEMES ---------------------------------------------------

;; On Melpa
(use-package nordless-theme :ensure t :defer t)
(use-package lavenderless-theme :ensure t :defer t)

;; Not yet on melpa
(use-package hydrangealess-theme :load-path "~/.emacs.d/lisp")
(use-package darkless-theme :load-path "~/.emacs.d/lisp")
(use-package nofrils-darkless-theme :load-path "~/.emacs.d/lisp")
(use-package chocolateless-theme :load-path "~/.emacs.d/lisp")

(defcustom lthms-current-theme nil
  "The theme currently used."
  :type 'symbol
  :group 'faces)

(defun load-theme-and-reset-ui (theme)
  "Clean-up loading of the colorless theme THEME and update the value SYM."
  (load-theme theme t)
  (powerline-reset)
  (when centaur-tabs-mode
    (progn
      (centaur-tabs-mode -1)
      (centaur-tabs-mode t)))
  (when fci-mode
    (progn
      (fci-mode -1)
      (fci-mode t))))

(defun change-default-theme (theme)
  "Change the default theme used by this Emacs to THEME."
  (disable-theme lthms-current-theme)
  (customize-save-variable 'lthms-current-theme theme)
  (load-theme-and-reset-ui theme))

;; -------- TEXT EDITOR SET-UP ---------------------------------------

;; We use utf-8
(set-language-environment "UTF-8")
(set-default-coding-systems 'utf-8)

;; Please, no backup files
(setq backup-directory-alist `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms `((".*" ,temporary-file-directory t)))

(add-hook 'before-save-hook 'delete-trailing-whitespace)
(add-hook 'prog-mode-hook 'electric-pair-mode)

;; Indentaton management
(setq-default indent-tabs-mode nil)
(use-package dtrt-indent
  :ensure t
  :diminish dtrt-indent-mode
  :hook
  (prog-mode . dtrt-indent-mode))

;; Parentheses that mach
(use-package paren-face
  :ensure t
  :hook (prog-mode . show-paren-mode)
  :config
  (setq blink-matching-paren -1)
  (setq blink-matching-delay 0))

(use-package ivy
  :ensure t
  :config
  (ivy-mode 1)
  (setq ivy-re-builders-alist
        '((t . ivy--regex-fuzzy)))
  (setq ivy-use-virtual-buffers t)
  (setq enable-recursive-minibuffers t))

(use-package counsel
  :ensure t)

;; -------- BUFFERS MANAGEMENT ---------------------------------------

(use-package perspective
  :ensure t
  :config
  (persp-mode))

(use-package projectile
  :ensure t
  :diminish projectile-mode
  :after (ivy counsel)
  :hook (grep-mode . (lambda () (setq truncate-lines t)))
  :config
  (setq projectile-completion-system 'ivy)
  (projectile-mode)

  (defun unified-find-file (&optional args)
    "Choose between projectile and ivy to explore file and pass ARGS to the choice."
    (interactive)
    (if (projectile-project-p)
        (projectile-find-file args)
      (counsel-find-file args)))

  ;; When trying to find a file within a project, start from the project
  ;; root.
  (add-hook 'find-file-hook
            (lambda ()
              (if (and (projectile-project-p)
                       (projectile-project-root))
                  (setq default-directory (projectile-project-root))))))

;; -------- KEYBOARD SET-UP ------------------------------------------

;; Interact with the clipboard
(defun copy-to-clipboard ()
  "Copy the selection to clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (message "Yanked region to x-clipboard!")
        (call-interactively 'clipboard-kill-ring-save)
        )
    (if (region-active-p)
        (progn
          (shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
          (message "Yanked region to clipboard!")
          (deactivate-mark))
      (message "No region active; can't yank to clipboard!"))))

(setq select-enable-clipboard nil)
(defun paste-from-clipboard ()
  "Paste the clipboard content into the buffer at cursor."
  (interactive)
  (setq select-enable-clipboard t)
  (yank)
  (setq select-enable-clipboard nil))

(defun minibuffer-keyboard-quit ()
  "Abort recursive edit.
In Delete Selection mode, if the mark is active, just deactivate it;
then it takes a second \\[keyboard-quit] to abort the minibuffer."
  (interactive)
  (if (and delete-selection-mode transient-mark-mode mark-active)
      (setq deactivate-mark  t)
    (when (get-buffer "*Completions*") (delete-windows-on "*Completions*"))
    (abort-recursive-edit)))

;; I wouldn’t use emacs if evil was not a thing.
;; It is heavily configured to work smoothly with the bépo layout
(use-package evil
  :ensure t
  :diminish evil-mode
  :init
  (setq evil-want-abbrev-expand-on-insert-exit nil)
  :config
  (evil-mode t)

  (define-key evil-normal-state-map "l" 'evil-replace)
  (define-key evil-visual-state-map "l" 'evil-replace)
  (define-key evil-normal-state-map "L" 'evil-replace-state)
  (define-key evil-motion-state-map "j" 'evil-find-char-to)

  (define-key evil-normal-state-map "t" 'evil-backward-char)
  (define-key evil-motion-state-map "t" 'evil-backward-char)
  (define-key evil-visual-state-map "t" 'evil-backward-char)
  (define-key evil-normal-state-map "s" 'evil-next-visual-line)
  (define-key evil-motion-state-map "s" 'evil-next-visual-line)
  (define-key evil-visual-state-map "s" 'evil-next-visual-line)
  (define-key evil-normal-state-map "r" 'evil-previous-visual-line)
  (define-key evil-motion-state-map "r" 'evil-previous-visual-line)
  (define-key evil-visual-state-map "r" 'evil-previous-visual-line)
  (define-key evil-normal-state-map "n" 'evil-forward-char)
  (define-key evil-motion-state-map "n" 'evil-forward-char)
  (define-key evil-visual-state-map "n" 'evil-forward-char)

  (define-key evil-normal-state-map "m" 'evil-search-next)
  (define-key evil-motion-state-map "m" 'evil-search-next)
  (define-key evil-visual-state-map "m" 'evil-search-next)
  (define-key evil-normal-state-map "M" 'evil-search-previous)
  (define-key evil-motion-state-map "M" 'evil-search-previous)
  (define-key evil-visual-state-map "M" 'evil-search-previous)

  (define-key evil-motion-state-map (kbd "à t") 'evil-window-left)
  (define-key evil-motion-state-map (kbd "à s") 'evil-window-down)
  (define-key evil-motion-state-map (kbd "à r") 'evil-window-up)
  (define-key evil-motion-state-map (kbd "à n") 'evil-window-right)
  (define-key evil-motion-state-map (kbd "à à") 'evil-window-next)
  (define-key evil-motion-state-map (kbd "à q") 'kill-this-buffer)

  (define-key evil-normal-state-map "|" 'split-window-horizontally)
  (define-key evil-normal-state-map "¦" 'split-window-vertically)

  (define-key evil-normal-state-map "é" 'evil-forward-word-begin)
  (define-key evil-motion-state-map "é" 'evil-forward-word-begin)
  (define-key evil-visual-state-map "é" 'evil-forward-word-begin)
  (define-key evil-normal-state-map "É" 'evil-forward-WORD-begin)
  (define-key evil-motion-state-map "É" 'evil-forward-WORD-begin)
  (define-key evil-visual-state-map "É" 'evil-forward-WORD-begin)
  (define-key evil-operator-state-map "é" 'evil-a-word)
  (define-key evil-operator-state-map "É" 'evil-a-WORD)

  (define-key evil-normal-state-map (kbd "M-c") 'copy-to-clipboard)
  (define-key evil-insert-state-map (kbd "M-c") 'copy-to-clipboard)
  (define-key evil-normal-state-map (kbd "M-v") 'paste-from-clipboard)
  (define-key evil-insert-state-map (kbd "M-v") 'paste-from-clipboard)

  (define-key evil-normal-state-map [escape] 'keyboard-quit)
  (define-key evil-visual-state-map [escape] 'keyboard-quit)
  (define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
  (global-set-key [escape] 'evil-exit-emacs-state)

  (evil-define-key 'normal dired-mode-map
    "t" 'evil-backward-char
    "s" 'evil-next-visual-line
    "r" 'evil-previous-visual-line
    "n" 'evil-forward-char
    "m" 'evil-search-next
    "M" 'evil-search-previous)

  (global-set-key (kbd "C-+") 'text-scale-increase)
  (global-set-key (kbd "C--") 'text-scale-decrease))

(use-package evil-numbers
  :ensure t
  :after evil
  :config
  (define-key evil-normal-state-map "+" 'evil-numbers/inc-at-pt)
  (define-key evil-normal-state-map "-" 'evil-numbers/dec-at-pt))

;; hydra/mode is expected to be personnalized by major mode
(defvar-local hydra/mode (lambda () (interactive) nil))

(use-package hydra
  :ensure t
  :after (evil projectile perspective)
  :config
  (add-hook 'after-init-hook
            (lambda ()
              (if lthms-current-theme
                  (load-theme-and-reset-ui lthms-current-theme))))

  (defhydra hydra-themes (:color teal :hint nil)
    "Load a colorless theme"
    ("c"  (change-default-theme 'chocolateless) "Chocolateless")
    ("d"  (change-default-theme 'darkless) "Darkless")
    ("D"  (change-default-theme 'nofrils-darkless) "Nofrils Darkless")
    ("h"  (change-default-theme 'hydrangealess) "Hydrangealess")
    ("l"  (change-default-theme 'lavenderless) "Lavenderless")
    ("n"  (change-default-theme 'nordless) "Nordlessless")
    ("SPC"  nil "Quit" :exit t))

  (defhydra hydra-main (:color teal :hint nil)
    "
     ^^Global            ^^Buffer             ^^Project           ^^Text
--------------------------------------------------------------------------------
  _m_: Major Mode     _e_: Open file       _g_: Magit          _c_: Comment
  _t_: Load Theme     _q_: Kill window     _G_: Grep project
  _p_: Packages       _x_: Kill buffer
                    ^^_n_: Next error
                    ^^_à_: Navigation

"
    ("c"   comment-dwim)
    ("p"   package-list-packages)
    ("q"   delete-window)
    ("x"   kill-this-buffer)
    ("e"   unified-find-file)
    ("t"   hydra-themes/body)
    ("m"   (call-interactively hydra/mode))
    ("n"   flycheck-next-error)
    ("à"   hydra-perspective/body)
    ("G"   projectile-grep)
    ("g"   hydra-magit/body)
    ("SPC"  nil "Quit" :exit t))

  (defhydra hydra-magit (:color teal :exit t :hint nil)
    "
       ^^Diff                 ^^Commit
--------------------------------------------------------------------
     _u_: Diff unstaged    _c_: Commit
     _s_: Diff staged      _f_: Fixup
                         ^^_a_: Amend
"
    ("u"  magit-diff-unstaged)
    ("s"  magit-diff-staged)
    ("c"  magit-commit-create)
    ("f"  magit-commit-fixup)
    ("a"  magit-commit-amend)
    ("g"  magit-dispatch       "Magit menu")
    ("SPC"  nil "Quit" :exit t))

  (defhydra hydra-perspective (:color teal :hint nil)
    "
       ^^Perspective                ^^Projectile
--------------------------------------------------------------------
    _à_: Change perspective      _s_: Change project
    _k_: Kill perspective        _b_: Change buffer
"
    ("à"  persp-switch)
    ("k"  persp-kill)
    ("s"  projectile-switch-project)
    ("b"  projectile-switch-to-buffer)
    ("q"  nil "Quit" :exit t))

  (define-key dired-mode-map (kbd "SPC") 'hydra-main/body)
  (define-key evil-visual-state-map (kbd "SPC") 'hydra-main/body)
  (define-key evil-normal-state-map (kbd "SPC") 'hydra-main/body))

(use-package undo-tree :diminish undo-tree-mode)

;; -------- IDE SET-UP -----------------------------------------------

;; git
(use-package diff-hl
  :ensure t
  :config
  (global-diff-hl-mode +1)
  (diff-hl-flydiff-mode +1))

(use-package git-commit
  :ensure t
  :config
  (add-hook 'git-commit-setup-hook (lambda ()
                                     (fci-mode)
                                     (flyspell-mode)
                                     (set-fill-column 70))))

(use-package magit
  :ensure t
  :defer t
  :config
  (setq-default magit-diff-refine-hunk 'all)
  (setq-default magit-diff-refine-ignore-whitespace t)
  (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh))

;; Doc suggestion in the echo area
(use-package eldoc
  :diminish eldoc-mode
  :hook (prog-mode . eldoc-mode))

;; yasnippet
(use-package yasnippet
  :diminish yas-minor-mode)

;; Company
(defvar my-prev-whitespace-mode nil)

(use-package company
  :ensure t
  :diminish company-mode
  :after evil
  :hook
  (prog-mode . company-mode)
  :config
  (setq company-idle-delay 0)
  (setq company-minimum-prefix-length 1)
  (define-key company-active-map (kbd "C-n") 'company-select-next)
  (define-key company-active-map (kbd "C-p") 'company-select-previous)
  (defvar-local company-fci-mode-on-p nil)

  (defun company-turn-off-fci (&rest ignore)
    (when (boundp 'fci-mode)
      (setq company-fci-mode-on-p fci-mode)
      (when fci-mode (fci-mode -1))))

  (defun company-maybe-turn-on-fci (&rest ignore)
    (when company-fci-mode-on-p (fci-mode 1)))

  (add-hook 'company-completion-started-hook 'company-turn-off-fci)
  (add-hook 'company-completion-finished-hook 'company-maybe-turn-on-fci)
  (add-hook 'company-completion-cancelled-hook 'company-maybe-turn-on-fci)

  (make-variable-buffer-local 'my-prev-whitespace-mode)
  (defun pre-popup-draw ()
    "Turn off whitespace mode before showing company complete tooltip"
    (if whitespace-mode
        (progn
          (setq my-prev-whitespace-mode t)
          (whitespace-mode -1))))
  (defun post-popup-draw ()
    "Restore previous whitespace mode after showing company tooltip"
    (if my-prev-whitespace-mode
        (progn
          (whitespace-mode 1)
          (setq my-prev-whitespace-mode nil))))
  (advice-add 'company-pseudo-tooltip-unhide :before #'pre-popup-draw)
  (advice-add 'company-pseudo-tooltip-hide :after #'post-popup-draw))

(use-package flycheck
  :ensure t
  :diminish flycheck-mode
  :hook (prog-mode . flycheck-mode)
  :config
  (define-fringe-bitmap 'flycheck-fringe-bitmap-ball
    (vector #b00000000
            #b00000000
            #b00000000
            #b00000000
            #b00000000
            #b00111000
            #b01111100
            #b11111110
            #b11111110
            #b01111100
            #b00111000
            #b00000000
            #b00000000
            #b00000000
            #b00000000
            #b00000000
            #b00000000))

  (flycheck-define-error-level 'error
    :severity 100
    :compilation-level 2
    :overlay-category 'flycheck-error-overlay
    :fringe-bitmap 'flycheck-fringe-bitmap-ball
    :fringe-face 'flycheck-fringe-error
    :error-list-face 'flycheck-error-list-error)
  (flycheck-define-error-level 'warning
    :severity 100
    :compilation-level 1
    :overlay-category 'flycheck-warning-overlay
    :fringe-bitmap 'flycheck-fringe-bitmap-ball
    :fringe-face 'flycheck-fringe-warning
    :warning-list-face 'flycheck-warning-list-warning)
  (flycheck-define-error-level 'info
    :severity 100
    :compilation-level 0
    :overlay-category 'flycheck-info-overlay
    :fringe-bitmap 'flycheck-fringe-bitmap-ball
    :fringe-face 'flycheck-fringe-info
    :info-list-face 'flycheck-info-list-info))


(use-package flycheck-pos-tip
  :ensure t
  :config
  (setq-default flycheck-pos-tip-timeout 60)
  :hook
  (flycheck-mode . flycheck-pos-tip-mode))

;; -------- PROGRAMMING LANGUAGE SET-UP ------------------------------

;; Coq
(defconst opam-switch
  (shell-command-to-string "opam switch show | tr -d '\n'"))

(defun coq-exe (name)
  "Give the absolute path of NAME executable."
  (concat (getenv "HOME")
          "/.opam/"
          opam-switch
          "/bin/"
          name))

(use-package proof-general
  :ensure t
  :mode ("\\.v\\'" . coq-mode)
  :init
  (progn
    (setq-default proof-splash-enable nil)
    (setq-default coq-compile-before-require t)
    (setq-default proof-disappearing-proofs t)
    (setq-default coq-prog-name (coq-exe "coqtop"))
    (setq-default coq-compiler (coq-exe "coqc"))
    (setq-default coq-dependency-analyzer (coq-exe "coqdep"))))

(use-package company-coq
  :ensure t
  :defines coq-one-command-per-line
  :init
  (progn
    (defhydra hydra-coq (:color teal :hint nil)
      "
     ^^Commands               ^^Company-Coq
------------------------------------------------
  _a_: About               _e_: Extract Current Goal
  _p_: Print               _d_: Diff
 _ss_: Search Pattern     _sd_: Show Documentation
  _x_: Exit Coq           _jd_: Jump to Definition
                        ^^_rd_: Return from Definition
"
      ("a"   coq-About)
      ("p"   coq-Print)
      ("ss"  coq-SearchIsos)
      ("x"   proof-shell-exit)
      ("e"   company-coq-lemma-from-goal)
      ("d"   company-coq-diff-dwim)
      ("sd"  company-coq-doc)
      ("jd"  company-coq-jump-to-definition)
      ("rd"  xref-pop-marker-stack)
      ("SPC" nil "Quit" :exit t)))
  :init
  (setq company-coq-disabled-features '(prettify-symbols smart-subscripts))
  (setq coq-one-command-per-line nil)
  :config
  (company-coq-initialize)
  :hook
  (((coq-response-mode coq-goals-mode) . centaur-tabs-local-mode)
   (coq-mode . company-coq-mode)
   (coq-mode . (lambda () (setq-local hydra/mode 'hydra-coq/body)))))

;; Rust
(use-package rust-mode
  :ensure t)

(use-package cargo
  :ensure t
  :after rust-mode
  :hook ((rust-mode . cargo-minor-mode)
         (cargo-process-mode . visual-line-mode)))

(use-package racer
  :ensure t
  :after rust-mode
  :hook
  ((rust-mode . racer-mode)
   (racer-mode . (lambda () (setq-local hydra/mode 'hydra-rust/body))))
  :config
  (defhydra hydra-rust (:color teal :hint nil)
    "
     ^^Racer                          ^^Rust
------------------------------------------------
 _jd_: Jump to Definition          _t_: Run Tests
 _rd_: Return from Definition      _c_: Run Clippy
  _d_: Show Documentation
"
    ("jd"   racer-find-definition)
    ("rd"   xref-pop-marker-stack)
    ("d"    racer-describe)
    ("t"    rust-test)
    ("c"    rust-run-clippy)
    ("SPC" nil "Quit" :exit t))

  (defvar rustup-default
    (replace-regexp-in-string "/bin/rustc\n*\\'"
                              ""
                              (shell-command-to-string "rustup which rustc"))
    "The rust toolchain currently installed according to rustup")

  (setq racer-cmd (executable-find "racer"))
  (setq racer-rust-src-path
        (concat rustup-default "/lib/rustlib/src/rust/src")))

(use-package company-racer
  :ensure t
  :after (cargo racer)
  :config
  (setq company-tooltip-align-annotations t)
  (add-to-list 'company-backends 'company-racer))

(use-package flycheck-rust
  :ensure t
  :hook
  (flycheck-mode . flycheck-rust-setup))

;; Latex
(use-package tex
  :ensure auctex
  :config
  (setq-default font-latex-fontify-script nil)
  (defhydra hydra-tex (:color teal :hint nil)
      "
     ^^Format
------------------------------------------------
 _fp_: Format Paragraph
 _fs_: Format Section
 _fe_: Format Environment
"
    ("fp"   LaTeX-fill-paragraph)
    ("fs"   LaTeX-fill-section)
    ("fe"   LaTeX-fill-environment)
    ("SPC" nil "Quit" :exit t))
  :hook
  (TeX-mode . (lambda ()
                (fci-mode)
                (set-fill-column 80)
                (setq-local hydra/mode 'hydra-tex/body))))

;; Ogmarkup
(use-package writeroom-mode
  :ensure t)

(use-package ogmarkup
  :load-path "~/.emacs.d/lisp"
  :after writeroom-mode
  :config
  (defhydra hydra-ogmarkup (:exit t)
    "Ogmarkup major mode"
    ("d"  ogmarkup/toggle-wr "Distraction-free setting")
    ("SPC" nil "Quit" :exit t))
  :hook
  (ogmarkup-mode . (lambda () (setq-local hydra/mode 'hydra-ogmarkup/body))))

;; Emacs Lisp
(use-package package-lint
  :ensure t)

;; Ocaml
(use-package tuareg
  :ensure t)

(use-package ocp-indent
  :ensure t
  :after tuareg)

(use-package merlin
  :ensure t
  :after company
  :config
  (add-to-list 'company-backends 'merlin-company-backend)
  (defhydra hydra-ocaml (:color teal :hint nil)
      "
    ^^Merlir
--------------------------------------------------------------------------------
 _i_: Switch to .mli
 _l_: Switch to .ml
 _o_: Find occurences
 _d_: Show documentation
 _t_: Type under cursor
 _T_: Type of expression
"
      ("i" merlin-switch-to-mli)
      ("l" merlin-switch-to-ml)
      ("o" merlin-occurrences)
      ("t" merlin-type-enclosing)
      ("T" merlin-type-expr)
      ("d" merlin-document)


      ("SPC" nil "Quit" :exit t))
  :hook (((caml-mode tuareg-mode) . merlin-mode)
         (merlin-mode . (lambda () (setq-local hydra/mode 'hydra-ocaml/body)))))

(use-package flycheck-ocaml
  :ensure t
  :hook
  (merlin-mode . (lambda ()
                   (setq merlin-error-after-save nil)
                   (flycheck-ocaml-setup))))

;; Haskell
(use-package dante
  :ensure t
  :after haskell-mode
  :hook (((haskell-mode haskell-literate-mode) . dante-mode)))

;; org
(use-package org
  :ensure t
  :mode ("\\.org\\'" . org-mode)
  :hook
  (org-mode . org-toggle-link-display)
  :config
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((shell . t)))
  (setq org-adapt-indentation nil)
  (setq org-src-preserve-indentation t))

(use-package ox-reveal
  :ensure t
  :after org)

;; Lua
(use-package lua-mode
  :ensure t)

;; SASS
(use-package sass-mode
  :ensure t)

;; Common Lisp
(use-package slime
  :ensure t
  :config
  (slime-setup)
  (defhydra hydra-slime (:color teal :hint nil)
      "
    ^^Slime              ^^Compile                      ^^Evaluate
--------------------------------------------------------------------------------
 _s_: Start slime    _cf_: Compile function          _d_: Describe symbol
 _x_: Stop slime     _cr_: Compile region           _hd_: HyperSpec Lookup
                   ^^_cc_: Compile file
                   ^^_cC_: Compile and load file
"
    ("s"   slime)
    ("x"   slime-quit-lisp)

    ("cf"  slime-compile-defun)
    ("cr"  slime-compile-region)
    ("cc"  slime-compile-file)
    ("cC"  slime-compile-and-load-file)
    ("be"  slime-eval-buffer)

    ("d"   slime-describe-symbol)
    ("hd"  slime-hyperspec-lookup)
    ("SPC" nil "Quit" :exit t))

  (load (expand-file-name "~/quicklisp/slime-helper.el"))
  (setq-default inferior-lisp-program "/usr/bin/sbcl")
  (setq-default slime-contribs '(slime-fancy))
  :hook
  (slime-mode . (lambda () (setq-local hydra/mode 'hydra-slime/body))))

;; Emails
(add-to-list 'auto-mode-alist '("\\`/tmp/neomutt-" . mail-mode))
(add-hook 'mail-mode-hook (lambda () (set-fill-column 72)))

;;; init.el ends here