aboutsummaryrefslogtreecommitdiff
path: root/emacs/init.org
blob: 424cdcd9c9590dadbbf66ac5a693383413e385bb (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
#+TITLE: mssola's GNU Emacs configuration
#+AUTHOR: Miquel Sabaté Solà
#+EMAIL: mikisabate@gmail.com
#+HTML_HEAD: <link rel="stylesheet" href="http://jo.mssola.com/static/style.css" type="text/css" />
#+HTML_HEAD: <link rel="stylesheet" href="http://jo.mssola.com/static/org.css" type="text/css" />
#+BABEL: :cache yes
#+PROPERTY: header-args :tangle ~/.emacs.d/init.el

* Preface

This is my attempt of getting my configuration organized with literate
programming and =org-babel=. I took the workflow I'm following from [[https://github.com/larstvei][@larstvei]].
The idea is that the =init.el= file will replace itself on the first execution
with the tangled version of this file. This first version will also be
byte-compiled. This means that all changes are to be made on the =init.org=
file *always*. The initial contents of the =init.el= are:

#+BEGIN_SRC elisp :tangle no
;; We can't tangle without org!
(require 'org)

;; Follow symlinks
(setq vc-follow-symlinks t)

;; Open the configuration
(find-file (concat user-emacs-directory "init.org"))

;; Tangle it
(org-babel-tangle)

;; Load it
(load-file (concat user-emacs-directory "init.el"))

;; Finally byte-compile it
(byte-compile-file (concat user-emacs-directory "init.el"))
#+END_SRC

There is no reason to track the init.el file, and no reason to change this
file. In order to ensure this, make sure to apply the following command after
cloning:

#+BEGIN_SRC sh :tangle no
git update-index --assume-unchanged emacs/init.el
#+END_SRC

Moreover, in order to avoid manually tangling and compiling the =init.org= file
on each change, the following function is provided:

#+BEGIN_SRC elisp
(defun tangle-init ()
  "If the current buffer is 'init.org' the code-blocks are
  tangled, and the tangled file is compiled. Morever, an init.html is generated."

  (when (or (equal (buffer-file-name)
                   (expand-file-name (concat user-emacs-directory "init.org")))
            (string-suffix-p "dotfiles/emacs/init.org" (buffer-file-name)))

    ;; Avoid running hooks when tangling
    (let ((prog-mode-hook nil))
      (org-babel-tangle)
      (org-html-export-to-html)
      (byte-compile-file (concat user-emacs-directory "init.el")))))

(add-hook 'after-save-hook 'tangle-init)
#+END_SRC
* Introduction

I'm using GNU Emacs 25, but everything should be working in GNU Emacs 24. There
are some exceptions to this (e.g. webkit support), but these cases are
individually handled.

#+BEGIN_SRC elisp
  (unless (>= emacs-major-version 24)
    (error "Don't be a cheap bastard and upgrade to at least GNU Emacs 24"))
#+END_SRC

Temporarily reduce garbage collection so startup time is lower. Idea taken from
[[https://github.com/purcell][@purcell]].

#+BEGIN_SRC elisp
  (defconst mssola-initial-gc-cons-threshold gc-cons-threshold
    "Initial value of `gc-cons-threshold' at start-up time.")
  (setq gc-cons-threshold (* 1024 1024 1024))
  (add-hook 'after-init-hook
            (lambda () (setq gc-cons-threshold mssola-initial-gc-cons-threshold)))
#+END_SRC

User name and email.

#+BEGIN_SRC elisp
(setq user-full-name "Miquel Sabaté Solà"
      user-mail-address "mikisabate@gmail.com")
#+END_SRC

* General

** GUI

I like a minimalistic GUI. Because of this, almost all GUI elements have been
disabled or tweaked in some custom way.

The frame title is "<login>: <path>". If we are not editing a file, then the
name of the buffer is displayed (e.g. "mssola: *scratch*").

#+BEGIN_SRC elisp
  (setq frame-title-format
    '((:eval
      (concat (user-real-login-name) ": "
        (if (buffer-file-name)
          (abbreviate-file-name (buffer-file-name))
          "%b")))))
#+END_SRC

Disable the menu, scroll and tool bars. At the same time, enable line and column
modes.

#+BEGIN_SRC elisp
  (menu-bar-mode -1)
  (when (fboundp 'set-scroll-bar-mode)
    (set-scroll-bar-mode nil))
  (when (fboundp 'tool-bar-mode)
    (tool-bar-mode -1))
  (when (fboundp 'tooltip-mode)
    (tooltip-mode 0))

  (line-number-mode 1)
  (column-number-mode 1)

  ;; Nice scrolling
  (setq scroll-margin 0
        scroll-conservatively 100000
        scroll-preserve-screen-position 1)
#+END_SRC

** Basic editing configuration

Use UTF-8 *always*.

#+BEGIN_SRC elisp
  (prefer-coding-system 'utf-8)
  (set-default-coding-systems 'utf-8)
  (set-terminal-coding-system 'utf-8)
  (set-keyboard-coding-system 'utf-8)
  (set-language-environment 'utf-8)
#+END_SRC elisp

Some editing tweaks like tabs vs spaces, maximum column width, etc.

#+BEGIN_SRC elisp
  ;; Emacs modes typically provide a standard means to change the indentation
  ;; width (e.g. c-basic-offset). Moreover, even though I prefer tabs over space,
  ;; for most coding conventions this is not the case (e.g. ruby). For this
  ;; reason, I will disable them by default and enabled them back for each
  ;; specific case (e.g. C). I'm also using the smart-tabs-mode package, see
  ;; below in the languages section.
  (setq-default indent-tabs-mode nil)
  (setq-default tab-width 4)

  ;; Maximum 80 columns.
  (setq-default fill-column 80)
  (setq-default auto-fill-function 'do-auto-fill)

  ;; Do not break lines
  (set-default 'truncate-lines t)

  ;; Delete the selection with a keypress.
  (delete-selection-mode t)

  ;; Remove whitespaces at the end of line
  (add-hook 'before-save-hook 'delete-trailing-whitespace)

  ;; Cursor
  (blink-cursor-mode 0)
  (global-hl-line-mode -1)
  (show-paren-mode 1)
#+END_SRC

** Font and theme

I'm using "Droid Sans Mono" simply because I've grown used to it.

#+BEGIN_SRC elisp
  (set-frame-font "Droid Sans Mono Dotted for Powerline-10")
  (add-to-list 'default-frame-alist '(font . "Droid Sans Mono Dotted for Powerline-10"))

  ; Emacs in daemon mode does not like `set-face-attribute` because this is only
  ; applied if there is a frame in place, which doesn't happen when starting the
  ; daemon. Thus, we should call that after the frame has been created (e.g. by
  ; emacsclient).
  ; See: https://lists.gnu.org/archive/html/help-gnu-emacs/2015-03/msg00016.html
  (add-hook 'after-make-frame-functions-hook
    (lambda ()
      (set-face-attribute 'default t :font "Droid Sans Mono Dotted for Powerline-10")))
#+END_SRC

I've hacked my own theme called [[https://github.com/mssola/soria][soria]]. This theme combines the vim theme
[[http://www.vim.org/scripts/script.php?script_id=2140][xoria256]] with the [[http://opensuse.github.io/branding-guidelines/][openSUSE branding guidelines]].

#+BEGIN_SRC elisp
  (load-theme 'soria t)
#+END_SRC

When hacking your own theme, sometimes you want to know what face is the one
that you see on the screen right now. This function from [[https://github.com/thblt/DotFiles][@thblt]] allows me to
get exactly that:

#+BEGIN_SRC elisp
(defun mssola-face-at-point (pos)
  "Writes a message with the name of the face at the current point.  The POS
  argument contains the current position of the cursor."

  (interactive "d")

  (let ((face (or (get-char-property (point) 'read-face-name)
                  (get-char-property (point) 'face))))
    (if face (message "Face: %s" face) (message "No face at %d" pos))))

(global-set-key (kbd "C-c f") 'mssola-face-at-point)
#+END_SRC

** General global key bindings

Use kill-this-buffer instead of kill-buffer.

#+BEGIN_SRC elisp
  (global-set-key (kbd "C-x k") 'kill-this-buffer)
#+END_SRC

Disable C-z. It will later on be picked up by Evil's config as the escape
sequence. This is here to make sure that it will be disabled even if Evil
cannot be loaded due to some error.

#+BEGIN_SRC elisp
  (global-unset-key (kbd "C-z"))
#+END_SRC

Disable all the Fn keys.

#+BEGIN_SRC elisp
  (dotimes (i 12)
    (global-unset-key (kbd (format "<f%d>" (+ i 1)))))
#+END_SRC

Disable overwrite-mode.

#+BEGIN_SRC elisp
  (define-key global-map [(insert)] nil)
#+END_SRC

** Others

Revert buffers automatically when underlying files are changed externally.

#+BEGIN_SRC elisp
  (global-auto-revert-mode t)
#+END_SRC

Follow symlinks.

#+BEGIN_SRC elisp
  (setq vc-follow-symlinks t)
#+END_SRC

Remove the initial message from the scratch buffer.

#+BEGIN_SRC elisp
  (setq initial-scratch-message nil)
#+END_SRC

Never kill the scratch buffer, bury it instead.

#+BEGIN_SRC elisp
  (defadvice kill-buffer (around kill-buffer-around-advice activate)
    (let ((buffer-to-kill (ad-get-arg 0)))
      (if (equal buffer-to-kill "*scratch*")
          (bury-buffer)
        ad-do-it)))
#+END_SRC

No backups

#+BEGIN_SRC elisp
  (setq-default make-backup-files nil)
  (setq-default auto-save-default nil)
#+END_SRC

But at least save the list of recently open files.

#+BEGIN_SRC elisp
(require 'recentf)

(recentf-mode 1)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)

; Save the list every 5 minutes
(run-at-time nil (* 5 60) 'recentf-save-list)
#+END_SRC

No welcome screen

#+BEGIN_SRC elisp
  (setq-default inhibit-startup-message t)
#+END_SRC

Enable y/n answers

#+BEGIN_SRC elisp
  (fset 'yes-or-no-p 'y-or-n-p)
#+END_SRC

Let flyspell be performant.

#+BEGIN_SRC elisp
  (defvar flyspell-issue-message-flag nil)
#+END_SRC

Save custom-variables somewhere else.

#+BEGIN_SRC elisp
  (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
  (if (file-exists-p custom-file)
      (load custom-file))
#+END_SRC

* Calendar

We catalans start our weeks on Monday.

#+BEGIN_SRC elisp
  (defvar calendar-week-start-day 1)
#+END_SRC

Global key binding.

#+BEGIN_SRC elisp
  (global-set-key (kbd "M-c") 'calendar)
#+END_SRC

Fix some stuff for evil mode.

#+BEGIN_SRC elisp
  (with-eval-after-load "evil"
    (evil-set-initial-state 'calendar-mode 'normal)
    (evil-define-key 'normal calendar-mode-map
      "j" 'calendar-forward-week
      "k" 'calendar-backward-week
      "b" 'calendar-backward-day
      "h" 'calendar-backward-day
      "l" 'calendar-forward-day
      "w" 'calendar-forward-day
      "q" 'calendar-exit
      "\C-h" 'evil-window-left
      "\C-l" 'evil-window-right
      "\C-j" 'evil-window-down
      "\C-k" 'evil-window-up
      "\C-n" 'calendar-scroll-left-three-months
      "\C-p" 'calendar-scroll-right-three-months))
#+END_SRC

* General purpose defuns

I want to read the latest news. That's why I define a function that downloads
the =NEWS= file from the git server and then opens it in a buffer.

#+BEGIN_SRC elisp
  (defun mssola-view-emacs-latest-news ()
    "Allow users to fetch the latest Emacs' NEWS file."
    (interactive)

    (url-copy-file
     "http://git.savannah.gnu.org/cgit/emacs.git/plain/etc/NEWS"
     "/tmp/emacs-news" t)

    (find-file-read-only "/tmp/emacs-news" t))
#+END_SRC

Sometimes I want to debug my initialization time.

#+BEGIN_SRC elisp
  (defun emacs-init-time ()
    "Redefine the `emacs-init-time' function so it is more detailed.
  Idea taken from @purcell."

    (interactive)
    (let ((init-time
           (float-time (time-subtract after-init-time before-init-time))))
      (message "%.3fs" init-time)))
#+END_SRC

* Lisp packages
** Custom packages

Compile the =g.el= script and bind it to @@html:<kbd>M-g</kbd>@@.

#+BEGIN_SRC elisp
  (byte-compile-file (concat user-emacs-directory "lisp/g.el") t)
  (global-set-key (kbd "M-g") 'g)
#+END_SRC

** use-package

Initialize package.

#+BEGIN_SRC elisp
  (require 'package)

  (add-to-list 'package-archives
               '("melpa" . "http://melpa.milkbox.net/packages/") t)
  (add-to-list 'package-archives
               '("melpa-stable" . "https://stable.melpa.org/packages/") t)

  (package-initialize)
#+END_SRC

I'm using use-package to handle my installed packages. I don't know if it's
the best option or what because I haven't tested all the package managers
for Emacs out there. After trying some custom functions to handle
package-install, I decided on use-package because I feel more well-organized.

#+BEGIN_SRC elisp
  (unless (package-installed-p 'use-package)
    (package-refresh-contents)
    (package-install 'use-package))
#+END_SRC

Some =use-package= calls require =diminish.el= to be available. So, let's
require it here on the very top.

#+BEGIN_SRC elisp
(use-package diminish
  :ensure t)
#+END_SRC

* Project

First of all, load the silver searcher, which is a convenient and fast searcher.
Ayo silver!

#+BEGIN_SRC elisp
  (use-package ag
    :ensure t
    :config

    ; Avoid some disagreements between ag and evil.
    (with-eval-after-load 'evil
      (add-hook 'ag-mode-hook
                (lambda ()
                  (define-key ag-mode-map (kbd "n") 'evil-search-next)
                  (define-key ag-mode-map (kbd "N") 'evil-search-previous)
                  (define-key ag-mode-map (kbd "gg") 'evil-goto-first-line))))
    (setq ag-reuse-buffers t)
    (setq ag-reuse-window t))
#+END_SRC

Then, for keeping up with my projects I use the Projectile + Helm combination.

#+BEGIN_SRC elisp
(use-package projectile
  :ensure t
  :config
  (projectile-mode 1))

(use-package helm
  :ensure t
  :config
  (setq projectile-completion-system 'helm)

  ; Allow the search pattern to be on the header. Taken from this Reddit thread:
  ; https://www.reddit.com/r/emacs/comments/3asbyn/new_and_very_useful_helm_feature_enter_search/
  (setq helm-echo-input-in-header-line t)

  (defun helm-hide-minibuffer-maybe ()
    "Hide the minibuffer if we are in a Helm session"

    (when (with-helm-buffer helm-echo-input-in-header-line)
      (let ((ov (make-overlay (point-min) (point-max) nil nil t)))
        (overlay-put ov 'window (selected-window))
        (overlay-put ov 'face (let ((bg-color (face-background 'default nil)))
                                `(:background ,bg-color :foreground ,bg-color)))
        (setq-local cursor-type nil))))

  (add-hook 'helm-minibuffer-set-up-hook 'helm-hide-minibuffer-maybe)
  (setq helm-split-window-in-side-p t)

  ; Preview files with tab
  (define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action)

  ; Show available options
  (define-key helm-map (kbd "C-a")  'helm-select-action)

  ; Some vim-like bindings
  (define-key helm-map (kbd "C-j") 'helm-next-line)
  (define-key helm-map (kbd "C-k") 'helm-previous-line)

  (use-package helm-ag
    :ensure t))

(use-package helm-projectile
  :ensure t
  :config
  (helm-projectile-on)

  ; Define M-p as a way to quickly list all the available projects.
  (with-eval-after-load 'evil
    (define-key evil-normal-state-map (kbd "M-p")
      'helm-projectile-switch-project)))
#+END_SRC

I use @@html:<kbd>C-p</kbd>@@ as the binding for listing relevant files. This
binding works either by using =helm-projectile= or the regular =helm-find=
function. As a final touch, this binding also works for listing channels in ERC
buffers.

#+BEGIN_SRC elisp
(defun mssola-erc-helm-buffer-list ()
  "Returns a list with the ERC buffers."
  (mapcar 'buffer-name (erc-buffer-list)))

(defconst mssola-helm-source-erc-channel-list
      '((name . "ERC Channels")
        (candidates . mssola-erc-helm-buffer-list)
        (action . switch-to-buffer)))

(defun mssola-erc-helm-switch-buffer ()
  "Use helm to select an active ERC buffer."

  (interactive)

  (helm :sources '(mssola-helm-source-erc-channel-list)
        :buffer "*helm-erc-channels*"))

(defun mssola-find-file ()
  "Call the proper Helm function for finding files."

  (interactive)

  (if (string= major-mode "erc-mode")
      (mssola-erc-helm-switch-buffer)
    (condition-case nil
        (helm-projectile-find-file)
      (error
       (helm-find-files nil)))))

(with-eval-after-load 'evil
  (define-key evil-normal-state-map (kbd "C-p") 'mssola-find-file))
#+END_SRC

Similarly, =helm-ag= has two functions for applying =ag= on the project. I'm
binding to @@html:<kbd>,a</kbd>@@ a function that calls to the proper function.

#+BEGIN_SRC elisp
  (defun mssola-helm-ag ()
    "Call the right ag command for helm-ag."

    (interactive)

    (condition-case nil
        (helm-ag-project-root)
      (error (helm-ag))))

  (with-eval-after-load 'evil-leader
    (evil-leader/set-key "a" 'mssola-helm-ag))
#+END_SRC

* Edit

In this section I define some useful packages for editing. First of all, one of
the coolest packages out there is =undo-tree=. It allows you to navigate through
the undo history in a tree (because GNU Emacs is cool and keeps track of undo
actions in a tree structure instead of in a stack). This package is included in
recent versions of GNU Emacs.

#+BEGIN_SRC elisp
(with-eval-after-load 'undo-tree
  (global-undo-tree-mode 1)

  (setq undo-tree-visualizer-diff t
        undo-tree-visualizer-timestamps t
        undo-tree-visualizer-relative-timestamps t)

  (diminish 'undo-tree-mode)

  (with-eval-after-load 'evil-leader
    (evil-leader/set-key
      "u" 'undo-tree-visualize)))
#+END_SRC

Another important package is =flycheck=, which is an on-the-fly syntax checking
extension. This works with lots of languages with proper glue code.

#+BEGIN_SRC elisp
(use-package let-alist
  :ensure t)

(use-package flycheck
  :ensure t
  :diminish
  :config
  (add-hook 'after-init-hook 'global-flycheck-mode)

  ;; Only show the errors buffer if it isn't there and if I'm saving the
  ;; buffer.
  (setq flycheck-emacs-lisp-load-path 'inherit)
  (setq flycheck-check-syntax-automatically '(mode-enabled save))
  (setq flycheck-display-errors-function
    #'flycheck-display-error-messages-unless-error-list))
#+END_SRC

A recurring issue in speeches and presentations is that when showing something
with your editor, you have to increase/decrease the fonts. I use the
=default-text-scale= package for this.

#+BEGIN_SRC elisp
  (use-package default-text-scale
    :ensure t
    :config
    (global-set-key (kbd "C-+") 'default-text-scale-increase)
    (global-set-key (kbd "C--") 'default-text-scale-decrease))
#+END_SRC

Some languages use some delimiters a lot (e.g. lisp languages and
parenthesis). For this reason I'm using the =rainbow-delimiters= package, which
properly highlights each level in a different way (provided that your theme
supports it).

#+BEGIN_SRC elisp
  (use-package rainbow-delimiters
    :ensure t)
#+END_SRC

I never use the mouse.

#+BEGIN_SRC elisp
(use-package disable-mouse
  :ensure t
  :config
  (global-disable-mouse-mode)
  (setq global-disable-mouse-mode-lighter ""))
#+END_SRC

Sometimes you begin typing a prefix, but then you forget the following
chord. For this reason =which-key= was created. It will show the available
commands for the current chord as a list.

#+BEGIN_SRC elisp
  (use-package which-key
    :ensure t)
#+END_SRC

For some modes it is important to count the number of words in the text. For
this, we have =wc-mode=.

#+BEGIN_SRC elisp
  (use-package wc-mode
    :ensure t)
#+END_SRC

Next on the list, we have =writeroom-mode=. I honestly don't use this package
that often, but it's cool to have a distraction-free screen from time to time.

#+BEGIN_SRC elisp
  (use-package writeroom-mode
    :ensure t)
#+END_SRC

The =expand-region= package is an extension that is based on a Vim one, which
expands the region of selection with a single key chord. Even if evil covers
most of my uses, it's convenient to have this tool anyways.

#+BEGIN_SRC elisp
(use-package expand-region
  :ensure t
  :config

  ; Set C-e as the expand command (mnemonic: expand). This command will
  ; supercede the evil binding for "move one line", which I never use anyways.
  (global-set-key (kbd "\C-e") 'er/expand-region)
  (with-eval-after-load 'evil
    (define-key evil-normal-state-map (kbd "\C-e") 'er/expand-region)
    (define-key evil-visual-state-map (kbd "\C-e") 'er/expand-region)))

(defun er/add-text-mode-expansions ()
  "This way we can also expand the region into paragraphs & pages in text mode.
Directly taken from: https://github.com/magnars/expand-region.el."

  (set (make-local-variable 'er/try-expand-list) (append
                                                  er/try-expand-list
                                                  '(mark-paragraph
                                                    mark-page))))

(add-hook 'text-mode-hook 'er/add-text-mode-expansions)
#+END_SRC

Last but not least, =YASnippet=. This package allows people to define shortcuts
for writing some common blocks. Moreover, it comes with a set of builtin
snippets already. Since I don't remember some of these snippets, I've mapped
@@html:<kbd>, h</kbd>@@ to =yas-describe-tables=, which shows the available
snippets in another buffer.

#+BEGIN_SRC elisp
(use-package yasnippet
  :ensure t
  :diminish yas-minor-mode
  :init (yas-global-mode)
  :config
  (yas-global-mode 1)
  (with-eval-after-load 'evil-leader
    (evil-leader/set-key "h" 'yas-describe-tables)))
#+END_SRC

* Dired

I use dired mode mainly for attaching document into emails. That being said,
whenever I use it, I want basic evil movement.

#+BEGIN_SRC elisp
  (with-eval-after-load 'evil
    (evil-add-hjkl-bindings dired-mode-map 'normal
      (kbd "w") 'evil-forward-word-begin))
#+END_SRC

I also extend =dired= with some handy tweaks.

#+BEGIN_SRC elisp
(setq directory-free-space-args "-Pkh"
      dired-dwim-target t
      dired-omit-mode nil
      dired-recursive-copies 'always
      dired-recursive-deletes 'always
      delete-old-versions t)
#+END_SRC

And now instruct dired mode how to attach files when using mu4e. This is taken
from the [[https://www.djcbsoftware.nl/code/mu/mu4e/Dired.html#Dired][mu4e documentation]], and it's available by typing
@@html:<kbd>C-c RET C-a</kbd>@@.

#+BEGIN_SRC elisp
  (require 'gnus-dired)

  ;; Make the `gnus-dired-mail-buffers' function also work on message-mode derived
  ;; modes, such as mu4e-compose-mode.
  (defun gnus-dired-mail-buffers ()
    "Return a list of active message buffers."

    (let (buffers)
      (save-current-buffer
        (dolist (buffer (buffer-list t))
          (set-buffer buffer)
          (when (and (derived-mode-p 'message-mode)
                  (null message-sent-message-via))
            (push (buffer-name buffer) buffers))))
      (nreverse buffers)))

  (setq gnus-dired-mail-mode 'mu4e-user-agent)
  (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
#+END_SRC

* Evil

Forgive me, [[https://stallman.org/saint.html][Father]], for I have sinned. I've been exposed to modal editing
through Vim, and that has changed how I view editing for the foreseeable future.
Because of this, I use Evil. The following blocks include some heavy-lifting so
Evil and GNU Emacs work without hitting each other, and it also includes some
Evil extensions.

First of all, let's define a function that will be called whenever Evil is loaded.

#+BEGIN_SRC elisp
(defun mssola-evil ()
  "Configure evil mode."

  ; We can safely remap <C-u> because the counting will be handled a-la Vim.
  (define-key evil-normal-state-map (kbd "C-u") 'evil-scroll-up)

  ; Make window navigation easier.
  (define-key evil-normal-state-map (kbd "C-j") 'evil-window-down)
  (define-key evil-normal-state-map (kbd "C-k") 'evil-window-up)
  (define-key evil-normal-state-map (kbd "C-l") 'evil-window-right)
  (define-key evil-normal-state-map (kbd "C-h") 'evil-window-left)

  ; The window navigation tweaks effectively wipe out the help prefix, which
  ; is bad. Fortunately we can workaround this by providing "M-h" as the new
  ; help prefix. This prefix is only used in emacs mode to mark lines, which is
  ; something already handled by Evil.
  (define-key global-map (kbd "M-h") 'help-command)
  (fset 'help-command help-map)

  ; I use the Super key in combination with j & k to move around i3. Let's unset
  ; M- combos for these two fellows for whenever I misstype them.
  (global-unset-key (kbd "M-j"))
  (global-unset-key (kbd "M-k"))

  ; both this function and the subsequent lines about [escape] are taken from
  ; @aaronbieber configuration.
  (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)))

  ;; Make escape quit everything, whenever possible.
  (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)

  ; I store macros on the <q> register for convenience, so I used to use the
  ; <C-q> combo to execute this macro in Vim. In Emacs though, this combo is
  ; reserved to a rather useful function, and I'd like to keep it that way. So,
  ; now the mapping is set to <C-2> (mnemonic: where the @ symbol is). Moreover,
  ; it's applied as many times as specified by the numeric prefix argument.
  (define-key evil-normal-state-map (kbd "C-2")
    (lambda (n)
      (interactive "p")
      (evil-execute-macro n "@q")))

  ; C-s: switch to normal mode and save the buffer. I know :)
  (define-key evil-normal-state-map (kbd "C-s") 'save-buffer)
  (define-key evil-insert-state-map (kbd "C-s")
    (lambda () (interactive) (save-buffer) (evil-force-normal-state))))
#+END_SRC

Now make sure that Evil is installed, and call the relevant configuration functions.

#+BEGIN_SRC elisp
(use-package evil
  :ensure t
  :config

  (add-hook 'evil-mode-hook 'mssola-evil)
  (evil-mode 1)

  ;; C-z is unused and it's close to my beloved C-c. Since I don't want to mess
  ;; with one of the most sacred Emacs prefixes, I'm moving to C-z.
  (define-key key-translation-map (kbd "C-z") [escape])
  (define-key evil-operator-state-map (kbd "C-z") 'keyboard-quit)
  (set-quit-char "C-z")

  ;; Use the proper initial evil state for the following modes.
  (evil-set-initial-state 'help-mode 'normal)
  (evil-set-initial-state 'debugger-mode 'normal)
  (evil-set-initial-state 'describe-mode 'normal)
  (evil-set-initial-state 'Buffer-menu-mode 'normal)
#+END_SRC

If Evil was properly loaded, then make sure that the following Evil-related
packages are installed and configured as well. I start by defining the
=evil-leader= package, which brings the @@html:<kbd>leader</kbd>@@ feature from
Vim into Evil.

#+BEGIN_SRC elisp
  (use-package evil-leader
    :ensure t
    :config
    (global-evil-leader-mode)
    (evil-leader/set-leader ",")
    (setq evil-leader/in-all-states 1)
    (evil-leader/set-key
      "," 'back-to-indentation
      "c" 'delete-window
      "k" 'kill-buffer-and-window
      "v" 'split-window-right
      "V" (lambda () (interactive) (split-window-right) (other-window 1))
      "f" 'flycheck-list-errors
      "e" 'eval-last-sexp
      "b" 'view-buffer
      "o" 'browse-url-at-point))
#+END_SRC

Another handy Vim plugin that has made it into Evil is =evil-surround=, which
defines a new text object for surrounding characters (e.g. change a string from
having double quotes with single quotes in a single command).

#+BEGIN_SRC elisp
  (use-package evil-surround
    :ensure t
    :config
    (global-evil-surround-mode 1))
#+END_SRC

Next is another Vim plugin that has been ported to Evil: =evil-commentary=. This
package defines a new motion for comments, which is bound to
@@html:<kbd>gc</kbd>@@. So, for example, @@html:<kbd>gcc</kbd>@@ will comment
the current line, regardless of the programming language.

#+BEGIN_SRC elisp
  (use-package evil-commentary
    :ensure t
    :config
    (evil-commentary-mode t))
#+END_SRC

Another cool package is =evil-args= which defines the argument text object. This
text object can be targeted with the =a= character, and we can move backward and
forward through arguments with @@html:<kbd>H</kbd>@@ and @@html:<kbd>L</kbd>@@
respectively.

#+BEGIN_SRC elisp
  (use-package evil-args
    :ensure t
    :config
    ; Configuration taken from the documentation of evil-args.

    ;; Bind evil-args text objects
    (define-key evil-inner-text-objects-map "a" 'evil-inner-arg)
    (define-key evil-outer-text-objects-map "a" 'evil-outer-arg)

    ;; Bind evil-forward/backward-args
    (define-key evil-normal-state-map "L" 'evil-forward-arg)
    (define-key evil-normal-state-map "H" 'evil-backward-arg)
    (define-key evil-motion-state-map "L" 'evil-forward-arg)
    (define-key evil-motion-state-map "H" 'evil-backward-arg))
#+END_SRC

Last but not least, =evil-numbers= brings a couple of bindings available to Vim
into Evil: @@html:<kbd>C-a</kbd>@@ for increasing a number, and
@@html:<kbd>C-c -</kbd>@@ for decreasing it (modified because
@@html:<kbd>C-z</kbd>@@ is my escape sequence).

#+BEGIN_SRC elisp
  (use-package evil-numbers
    :ensure t
    :config
    (define-key evil-normal-state-map (kbd "C-a") 'evil-numbers/inc-at-pt)
    (define-key evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt)))
#+END_SRC

* Magit

A git porcelain for GNU Emacs. Even if I'm still using the git CLI, it's
certainly useful for some common tasks (I guess that I still need some learning).

#+BEGIN_SRC elisp
(use-package magit
  :ensure t
  :config
#+END_SRC

In some cases normal mode is not the right mode (e.g. commit mode). For these
cases, set the proper default mode.

#+BEGIN_SRC elisp
  (with-eval-after-load 'evil
    (add-hook 'git-commit-mode-hook 'evil-insert-state)
    (evil-set-initial-state 'magit-log-edit-mode 'insert))
#+END_SRC

When showing the status, hide the usually-redundant "branch" section and show
the rest.

#+BEGIN_SRC elisp
  (add-hook 'magit-section-set-visibility-hook
            '(lambda (section)
               (if (string= (magit-section-type section) "branch")
                   'hide
                 'show)))
#+END_SRC

Set a key binding for =magit-blame=.

#+BEGIN_SRC elisp
  (global-set-key (kbd "C-c b") 'magit-blame)
#+END_SRC

And repair some key bindings from Evil mode.

#+BEGIN_SRC elisp
  (with-eval-after-load 'evil-leader
    (evil-leader/set-key "s" 'magit-status)

    (use-package evil-magit
      :ensure t
      :config

      ; The magit + evil-magit combo messes up some chords, let's fix this.
      (evil-define-key 'normal magit-mode-map
        "\C-h" 'evil-window-left
        "\C-l" 'evil-window-right
        "\C-j" 'evil-window-down
        "\C-k" 'evil-window-up
        "\M-p"  'helm-projectile-switch-project))))
#+END_SRC

* mu4e

I use [[http://www.djcbsoftware.nl/code/mu/][mu]] and [[http://www.djcbsoftware.nl/code/mu/mu4e.html][mu4e]] to manage my email. The configuration for this has been taken
mainly from the documentation, plus some cool remarks on Reddit. This
configuration makes quite some assumptions. Read the =emacs/README.org= file as
provided in my [[https://github.com/mssola/dotfiles][dotfiles]] project to get more details.

I'm using an RPM that I've built on [[https://build.opensuse.org/package/show/home:mssola/mu][OBS]] which installs mu4e globally.

#+BEGIN_SRC elisp
  (add-to-list 'load-path "/usr/share/emacs/site-lisp/mu4e")
  (require 'mu4e)

  (when (featurep 'mu4e)
#+END_SRC

Diferent SMTP options that will be used for each context.

#+BEGIN_SRC elisp
  (setq message-send-mail-function 'smtpmail-send-it
        mu4e-maildir (expand-file-name "~/.mail")
        starttls-use-gnutls t)
#+END_SRC

After that, I am defining some functions that will be used in various parts of
the configuration.

#+BEGIN_SRC elisp
(defun mssola-smtp (server port)
  "Set SMTP variables depending on the given SERVER and PORT."

  (require 'smtpmail)

  (setq smtpmail-starttls-credentials
        '((server port nil nil))
        smtpmail-auth-credentials
        (expand-file-name "~/.authinfo.gpg")
        smtpmail-default-smtp-server server
        smtpmail-smtp-server server
        smtpmail-smtp-service port))

; https://www.reddit.com/r/emacs/comments/47t9ec/share_your_mu4econtext_configs/d0fsih6
(defun mu4e-message-maildir-matches (msg rx)
  "Returns true if the maildir of MSG matches the given regexp RX."

  (when rx
    (if (listp rx)
        ;; if rx is a list, try each one for a match
        (or (mu4e-message-maildir-matches msg (car rx))
            (mu4e-message-maildir-matches msg (cdr rx)))
      ;; not a list, check rx
      (string-match rx (mu4e-message-field msg :maildir)))))

(defun suse-refile-folder (key)
  "Returns the refile folder for the given SUSE account in the KEY arg"
  (concat "/" key "/Archives/"
          (format-time-string "%Y" (current-time))))
#+END_SRC

Now it's time to define the different contexts that I have. Defining contexts
this way is relatively new (since mu 0.9.16).

#+BEGIN_SRC elisp
  (setq mu4e-contexts
        `(
          ;; GMail
          ,(make-mu4e-context
            :name "gmail"
            :enter-func (lambda ()
                          (mu4e-message "Switching to gmail.com")
                          (mssola-smtp "smtp.gmail.com" 587))
            :match-func (lambda (msg)
                          (when msg
                            (mu4e-message-maildir-matches msg "^/gmail")))
            :vars '(
                    (user-mail-address     . "mikisabate@gmail.com")
                    (mu4e-reply-to-address . "mikisabate@gmail.com")
                    (mu4e-drafts-folder    . "/gmail/Drafts")
                    (mu4e-sent-folder      . "/gmail/Sent")
                    (mu4e-refile-folder    . "/gmail/All")
                    (mu4e-trash-folder     . "/gmail/Trash")))

          ;; suse.com
          ,(make-mu4e-context
            :name "comsuse"
            :enter-func (lambda ()
                          (mu4e-message "Switching to suse.com")
                          (mssola-smtp "smtp.novell.com" 25))
            :match-func (lambda (msg)
                          (when msg
                            (mu4e-message-maildir-matches msg "^/susecom")))
            :vars `(
                    (user-mail-address     . "msabate@suse.com")
                    (mu4e-reply-to-address . "msabate@suse.com")
                    (mu4e-drafts-folder    . "/susecom/Drafts")
                    (mu4e-sent-folder      . "/susecom/Sent")
                    (mu4e-refile-folder    . ,(suse-refile-folder "susecom"))
                    (mu4e-trash-folder     . "/susecom/Trash")))

          ;; suse.de
          ,(make-mu4e-context
            :name "desuse"
            :enter-func (lambda ()
                          (mu4e-message "Switching to suse.de")
                          (mssola-smtp "imap.suse.de" 587))
            :match-func (lambda (msg)
                          (when msg
                            (mu4e-message-maildir-matches msg "^/susede")))
            :vars `(
                    (user-mail-address     . "msabate@suse.de")
                    (mu4e-reply-to-address . "msabate@suse.de")
                    (mu4e-drafts-folder    . "/susede/Drafts")
                    (mu4e-sent-folder      . "/susede/Sent")
                    (mu4e-refile-folder    . ,(suse-refile-folder "susede"))
                    (mu4e-trash-folder     . "/susede/Trash")))))
#+END_SRC

If mu4e cannot figure things out, ask me.

#+BEGIN_SRC elisp
  (setq mu4e-context-policy 'ask)
  (setq mu4e-compose-context-policy 'ask)
#+END_SRC

Fill the =mu4e-user-mail-address-list= variable with the contexts.

#+BEGIN_SRC elisp
  (setq mu4e-user-mail-address-list
        (delq nil
              (mapcar (lambda (context)
                        (when (mu4e-context-vars context)
                          (cdr (assq 'user-mail-address
                                     (mu4e-context-vars context)))))
                      mu4e-contexts)))
#+END_SRC

Setting my bookmarks

#+BEGIN_SRC elisp
  (setq mu4e-bookmarks
        '(("maildir:/gmail/inbox OR maildir:/susecom/inbox OR maildir:/susede/inbox" "Inbox Folders" ?n)
          ("flag:unread AND NOT flag:trashed" "Unread messages" ?u)
          ("date:today..now" "Today's messages" ?t)))
#+END_SRC

The following signature looks alright regardless of whether the client supports
format=flowed or not.

#+BEGIN_SRC elisp
  (setq mu4e-compose-signature
        (concat
         "Miquel Sabaté Solà,\n"
         "PGP: 4096R / 1BA5 3C7A C93D CA2A CFDF DA97 96BE 8C6F D89D 6565\n"))
#+END_SRC

Sign outgoing emails always.

#+BEGIN_SRC elisp
  (add-hook 'message-send-hook 'mml-secure-message-sign-pgpmime)
#+END_SRC

To avoid UID clashes. See [[http://pragmaticemacs.com/emacs/fixing-duplicate-uid-errors-when-using-mbsync-and-mu4e/][this]].

#+BEGIN_SRC elisp
  (setq mu4e-change-filenames-when-moving t)
#+END_SRC

Miscellaneous settings.

#+BEGIN_SRC elisp
  (setq mu4e-html2text-command "w3m -T text/html"
        mu4e-attachment-dir  "~/Downloads"
        mu4e-headers-date-format "%Y-%m-%d %H:%M"
        message-citation-line-format "%N @ %Y-%m-%d %H:%M %Z:\n"
        message-citation-line-function 'message-insert-formatted-citation-line
        message-kill-buffer-on-exit t
        mu4e-get-mail-command "mbsync -aqV"
        mu4e-update-interval 600
        mu4e-compose-dont-reply-to-self t
        mu4e-compose-format-flowed t
        mu4e-headers-skip-duplicates t
        mu4e-headers-include-related t
        mu4e-headers-auto-update t)
#+END_SRC

The headers to show in the headers list a pair of a field and its width,
with `nil' meaning 'unlimited' (better only use that for the last field.
These are the defaults:

#+BEGIN_SRC elisp
  (setq mu4e-headers-fields
        '( (:date          .  18)
           (:mailing-list  .  15)
           (:from-or-to    .  20)
           (:subject       .  nil)))
#+END_SRC

Show images

#+BEGIN_SRC elisp
  (setq mu4e-view-show-images t
        mu4e-view-image-max-width 800)

  ; Use imagemagick, if available
  (when (fboundp 'imagemagick-register-types)
    (imagemagick-register-types))
#+END_SRC

As of 0.9.18 and GNU Emacs 25, the =mu4e-action-with-xwidget= can be used to
render an HTML message with Webkit.

#+BEGIN_SRC elisp
  (if (>= emacs-major-version 25)
      (add-to-list 'mu4e-view-actions
                   '("webkit" . mu4e-action-view-with-xwidget)))
#+END_SRC

Look for =mu4e-msg2pdf= in the exec path. The reason for this is that the OBS
package installs mu's =toys= into the exec path, but =mu4e= doesn't really count
on it.

#+BEGIN_SRC elisp
  (let ((exec (locate-file "msg2pdf" exec-path exec-suffixes)))
    (if exec
        (setq mu4e-msg2pdf exec)))
#+END_SRC

Adding hooks for composing and viewing messages.

#+BEGIN_SRC elisp
  (defun mssola-compose-mode ()
    "My settings for message composition."

    ; If we are composing an email from scratch, it's more convenient to be in
    ; insert mode. Otherwise start with normal mode.
    (with-eval-after-load 'evil
      (if mu4e-compose-parent-message
          (evil-set-initial-state 'mu4e-compose-mode 'normal)
        (evil-set-initial-state 'mu4e-compose-mode 'insert)))

    ; Guess hard newlines
    (use-hard-newlines t 'guess)

    ; So it's easy to encrypt/decrypt emails.
    (epa-mail-mode)

    ; Spellz
    (flyspell-mode))

  (add-hook 'mu4e-compose-mode-hook 'mssola-compose-mode)

  ; I want to read messages in the format that the sender used. I'm also
  ; enabling epa-mail-mode, so it's easy to decrypt received emails.
  (add-hook 'mu4e-view-mode-hook
            (lambda ()
              (epa-mail-mode)
              (visual-line-mode 1)))
#+END_SRC

I want desktop notifications when receiving email.

#+BEGIN_SRC elisp
  (use-package mu4e-alert
    :ensure t
    :config

    ; Notify me for unread emails from my inbox.
    (mu4e-alert-set-default-style 'libnotify)
    (add-hook 'after-init-hook #'mu4e-alert-enable-notifications)
    (add-hook 'after-init-hook #'mu4e-alert-enable-mode-line-display)
    (setq mu4e-alert-interesting-mail-query
          (concat
           "(maildir:/gmail/inbox OR maildir:/susecom/inbox OR maildir:/susede/inbox) "
           "AND flag:unread AND NOT flag:trashed"))
    (setq mu4e-alert-email-notification-types '(count)))

  ; Evil mode in mu4e
  (with-eval-after-load 'evil
    (use-package evil-mu4e
      :ensure t
      :config

      ; Idea taken from evil-mu4e.el
      (defvar mssola-evil-mu4e-mode-map-bindings
        `((,evil-mu4e-state mu4e-headers-mode-map "\C-u" evil-scroll-up)
          (,evil-mu4e-state mu4e-main-mode-map    "\C-u" evil-scroll-up)
          (,evil-mu4e-state mu4e-view-mode-map    "h" evil-backward-char)))

      (dolist (binding mssola-evil-mu4e-mode-map-bindings)
        (evil-define-key
          (nth 0 binding) (nth 1 binding) (nth 2 binding) (nth 3 binding)))))
#+END_SRC

And finally define a proper shortcut.

#+BEGIN_SRC elisp
  ; The trailing parenthesis closes the "(when (featurep 'mu4e)" statement from
  ; the very beginning.
  (global-set-key (kbd "C-c m") 'mu4e))
#+END_SRC

* org

[[http://orgmode.org/][org mode]] is an incredible tool that keeps me organized: TODOs, notes, agenda,
etc. Moreover, it's built in GNU Emacs:

#+BEGIN_SRC elisp
(require 'org)
#+END_SRC

** General settings

First of all, let me define some helper functions.

#+BEGIN_SRC elisp
  (defun mssola-org-skip-if-priority (priority &optional subtree)
    "Skip an agenda item if it has a priority of PRIORITY.
  PRIORITY may be one of the characters ?A, ?B, or ?C.
  Skips the current entry unless SUBTREE is not nil.  This function has been
  copied from @aaronbieber."

    (let ((end (if subtree (save-excursion (org-end-of-subtree t))
                 (save-excursion (progn (outline-next-heading) (1- (point))))))
          (pri-value (* 1000 (- org-lowest-priority priority)))
          (pri-current (org-get-priority (thing-at-point 'line t))))
      (if (= pri-value pri-current)
          end
        nil)))

  (defun mssola-org-skip-if-not-closed-in-day (time &optional subtree)
    "Skip entries that were not closed in the given TIME.
  Skip the current entry unless SUBTREE is not nil, in which case skip
  the entire subtree.  Idea taken from @aaronbieber"

    (let ((end (if subtree (save-excursion (org-end-of-subtree t))
                 (save-excursion (progn (outline-next-heading) (1- (point))))))
          (day-prefix (format-time-string "%Y-%m-%d" time)))

      (if (save-excursion
            (and (re-search-forward org-closed-time-regexp end t)
                 (string= (substring (match-string-no-properties 1) 0 10) day-prefix)))
          nil
        end)))
#+END_SRC

Some general UI settings for org mode.

#+BEGIN_SRC elisp
(setq org-src-tab-acts-natively t
      org-confirm-babel-evaluate nil
      org-edit-src-content-indentation 0)

(setq org-todo-keywords
      '((sequence "TODO(t)"  "|"  "DONE(d!)")
        (sequence "IDEA(i)"  "WORKING(w)"  "|"  "USED(u@/!)"  "DISCARDED(x@/!)")))

(setq org-todo-keyword-faces
      '(("TODO"      . org-todo)
        ("IDEA"      . font-lock-constant-face)
        ("WORKING"   . font-lock-constant-face)
        ("DONE"      . org-done)
        ("USED"      . org-done)
        ("DISCARDED" . org-done)))
#+END_SRC

Logging settings.

#+BEGIN_SRC elisp
  (setq org-log-done t)
  (setq org-log-redeadline (quote time))
  (setq org-log-reschedule (quote time))
#+END_SRC

Where org files reside.

#+BEGIN_SRC elisp
  (setq org-agenda-files '("~/org/"))
#+END_SRC

** Publishing

In order to publish files into HTML, I would like to have =htmlize= installed.
This package allows org to export to HTML in a better way (e.g. allowing code
blocks to be converted into HTML as well, so we can properly colorize it).

#+BEGIN_SRC elisp
(use-package htmlize
  :ensure t)
#+END_SRC

And now let's set all the related settings.

#+BEGIN_SRC elisp
(setq org-src-fontify-natively t
      org-html-include-timestamps nil
      org-html-toplevel-hlevel 2
      org-html-htmlize-output-type 'css
      org-export-with-section-numbers nil
      org-export-with-sub-superscripts nil
      org-export-htmlize-output-type 'css)
#+END_SRC

** Agenda

Custom commands for =org-agenda=.

#+BEGIN_SRC elisp
  (setq org-agenda-custom-commands
        '(("p" "Printed agenda"
           ; Daily agenda with a 2-weeks deadline warning. Tasks are
           ; represented as [ ] items.
           ((agenda ""
                    ((org-agenda-ndays 1)
                     (org-deadline-warning-days 14)
                     (org-agenda-todo-keyword-format "[ ]")
                     (org-agenda-scheduled-leaders '("" ""))))

           ; Display a "High Priority" list of tasks on top.
            (tags "PRIORITY=\"A\""
                  ((org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                   (org-agenda-sorting-strategy '(tag-up priority-down))
                   (org-agenda-todo-keyword-format "")
                   (org-agenda-overriding-header "\nHigh priority\n--------------\n")))


            ; All tasks except those already listed as high priority or
            ; ideas. Scheduled and deadlines are also ignored here.
            (alltodo ""
                     ((org-agenda-skip-function '(or (mssola-org-skip-if-priority ?A)
                                                     (org-agenda-skip-entry-if 'todo '("IDEA" "WORKING"))
                                                     (org-agenda-skip-if nil '(scheduled deadline))))
                      (org-agenda-sorting-strategy '(tag-up priority-down))
                      (org-agenda-todo-keyword-format "")
                      (org-agenda-overriding-header "\nAll tasks\n----------\n")))

            ; List of ideas.
            (todo "IDEA"
                  ((org-agenda-overriding-header "\nIdeas\n------\n")
                   (org-agenda-todo-keyword-format ""))))

           ((org-agenda-compact-blocks t)
            (org-agenda-remove-tags t)))

          ; List of done items. Useful for standups, review meetings, weekly
          ; reports, etc.
          ("d" "Done items"
           ; First show the items done yesterday. Useful for standups.
           ((todo "DONE"
                  ((org-agenda-overriding-header "Done yesterday\n---------------\n")
                   (org-agenda-skip-function
                    '(mssola-org-skip-if-not-closed-in-day
                      (time-subtract (current-time) (seconds-to-time 86400))))
                   (org-agenda-todo-keyword-format "")))

            ; Then show what I've done today.
            (todo "DONE"
                  ((org-agenda-overriding-header "\nDone today\n-----------\n")
                   (org-agenda-skip-function
                    '(mssola-org-skip-if-not-closed-in-day
                      (current-time)))
                   (org-agenda-todo-keyword-format "")))

            ; Finally show what I've been doing in the past 15 days. Useful for
            ; review meetings and weekly reports.
            (todo "DONE"
                  ((org-agenda-start-day "-15d")
                   (org-agenda-span 15)
                   (org-agenda-start-on-weekday nil)
                   (org-agenda-todo-keyword-format "")
                   (org-agenda-scheduled-leaders '("" ""))
                   (org-agenda-overriding-header "\nDone during the past 15 days\n-----------------------------\n"))))

           ((org-agenda-compact-blocks t)
            (org-agenda-remove-tags t)))))
#+END_SRC

The prefix for the different kinds of types being used.

#+BEGIN_SRC elisp
  (setq org-agenda-prefix-format '((agenda . "%t%s")
                                   (tags   . "%c:%s")
                                   (todo   . "%c:%t%s")))
#+END_SRC

Set up a key binding for org-agenda.

#+BEGIN_SRC elisp
(global-set-key (kbd "C-c a") 'org-agenda)
#+END_SRC

** Capture

Set the default notes file and the key binding.

#+BEGIN_SRC elisp
(setq org-default-notes-file (concat org-directory "/notes.org"))
(define-key global-map "\C-cc" 'org-capture)
#+END_SRC

And finally set =org-capture-templates=.

#+BEGIN_SRC elisp
(setq org-capture-templates
      `(("t" "todo" entry (file "") "* TODO %?\n%U\n")
        ("i" "idea" entry (file "") "* %? :IDEA:\n%U\n%a\n")))
#+END_SRC

** Other

Insert a <kbd></kbd> value in org mode. See this [[http://emacs.stackexchange.com/questions/2206/i-want-to-have-the-kbd-tags-for-my-blog-written-in-org-mode][StackExchange answer]].

#+BEGIN_SRC elisp
(defun endless/insert-key (key)
  "Ask for a key then insert its description.
Will work on both org-mode and any mode that accepts plain html."
  (interactive "kType key sequence: ")
  (let* ((is-org-mode (derived-mode-p 'org-mode))
         (tag (if is-org-mode
                  "@@html:<kbd>%s</kbd>@@"
                "<kbd>%s</kbd>")))
    (if (null (equal key "\r"))
        (insert
         (format tag (help-key-description key nil)))
      (insert (format tag ""))
      (forward-char (if is-org-mode -8 -6)))))

(define-key org-mode-map "\C-ck" #'endless/insert-key)
#+END_SRC

** TODO shortcut for making an org link, and transforming a link into a proper org link
** TODO make it work with evil
** TODO Proper keybindings for quick access.

** TODO shortcuts for stuff like: create something urgent for today

* IRC

I'm giving ERC a chance. First of all, I'm setting up basic stuff.

#+BEGIN_SRC elisp
(use-package erc
  :config

  (erc-track-mode t)

  (setq erc-hide-list '("PART")
        erc-prompt (lambda () (concat (buffer-name) ">"))
        erc-track-exclude-types '("JOIN" "NICK" "PART" "QUIT" "MODE")
        erc-server-coding-system '(utf-8 . utf-8)
        erc-kill-buffer-on-part t
        erc-kill-queries-on-quit t
        erc-kill-server-buffer-on-quit t
        erc-fill-column 100
        erc-fill-prefix ""
        erc-log-channels t
        erc-log-write-after-insert t
        erc-log-insert-log-on-open nil
        erc-log-channels-directory "~/.emacs.d/erc/"
        erc-timestamp-format "[%H:%M] "
        erc-insert-timestamp-function 'erc-insert-timestamp-left
        erc-insert-away-timestamp-function 'erc-insert-timestamp-left
        erc-hide-timestamps nil
        erc-whowas-on-nosuchnick t
        erc-public-away-p nil
        erc-echo-notice-always-hook '(erc-echo-notice-in-minibuffer)
        erc-auto-set-away nil
        erc-autoaway-message "%i seconds out..."
        erc-away-nickname "msabate"
        erc-enable-logging t
        erc-query-on-unjoined-chan-privmsg t)

  (setq erc-autojoin-channels-alist
        '(("irc.freenode.net" "#gnu" "#emacs")
          ("irc.suse.de" "#suse" "#docker")))
#+END_SRC

Use the =erc-hl-nicks= package, so highlight support for nicknames is better.

#+BEGIN_SRC elisp
(use-package erc-hl-nicks
  :ensure t
  :init
  (with-eval-after-load 'erc
    (add-to-list 'erc-modules 'hl-nicks)))
#+END_SRC

I want to have a desktop notification whenever someone mentions my name. For
this, I'm using the =erc-notifications= package which is built in ERC since
GNU Emacs 24.3.

#+BEGIN_SRC elisp
(with-eval-after-load 'erc
  (setq erc-notifications-icon
        (concat
         "/usr/share/emacs/"
         (format "%s.%s" emacs-major-version emacs-minor-version)
         "/etc/images/icons/hicolor/24x24/apps/emacs.png"))
  (add-to-list 'erc-modules 'notifications))
#+END_SRC

At this point, we can safely update all the loaded ERC modules.

#+BEGIN_SRC elisp
  (add-hook 'erc-connect-pre-hook
            (lambda (x) (erc-update-modules)))
#+END_SRC

According to the [[https://www.emacswiki.org/emacs/ErcFilling][wiki]], =auto-fill-mode= should be disabled if I'm using
=erc-fill-mode=.

#+BEGIN_SRC elisp
(add-hook 'erc-mode-hook
          (lambda () (auto-fill-mode 0)))
#+END_SRC

And now define a function to connect to both IRC servers.

#+BEGIN_SRC elisp
  (defun mssola-erc ()
    "Join pre-speciifed servers and channels."

    (interactive)

    (erc :server "irc.freenode.net" :port 6667 :nick "mssola")
    (erc-tls :server "irc.suse.de" :port 6697 :nick "mssola"))

  (global-set-key (kbd "C-c i") 'mssola-erc))
#+END_SRC

* Languages

** General

First of all, define a function that identifies some warning keywords
(e.g. TODO). This function can then be applied to the proper mode.

#+BEGIN_SRC elisp
  (defun warnings-mode-hook ()
    "Hook for enabling the warning face on strings with a warning prefix."

    (font-lock-add-keywords nil
      '(("\\(XXX\\|FIXME\\|TODO\\|HACK\\|NOTE\\)"
      1 font-lock-warning-face prepend))))
#+END_SRC

Text mode is not a programming language, but it's used quite often in this
context too. In this case, I want spell check and =wc-mode= activated.

#+BEGIN_SRC elisp
  (add-hook 'text-mode-hook
            (lambda ()
              (flyspell-mode 1)
              (wc-mode 1)))
#+END_SRC

** Lisp

Emacs lisp needs =rainbow-delimiters=, so the amount of parenthesis is less
confusing. Moreover, I'm also enabling =eldoc-mode= and the aforementioned
=warnings-mode-hook=.

#+BEGIN_SRC elisp
  (add-hook 'emacs-lisp-mode-hook
            (lambda ()
              (eldoc-mode 1)
              (warnings-mode-hook)
              (rainbow-delimiters-mode 1)
              ; https://github.com/jhenahan/emacs.d/blob/master/emacs-init.org#emacs-lisp
              (setq mode-name "ξ")))
#+END_SRC

** C and C++

C and C++ only require the =warnings-mode-hook= function, spell checking for the
comments and the usage of tabs instead of spaces.

#+BEGIN_SRC elisp
; Note that C-common includes languages with a similar syntax of C.
(add-hook 'c-mode-common-hook 'warnings-mode-hook)
(add-hook 'c-mode-common-hook (lambda() (flyspell-prog-mode)))

;; C
(add-hook 'c-mode-hook
  (lambda () (setq indent-tabs-mode t)))

;; C++
(add-hook 'c++-mode-hook
  (lambda () (setq indent-tabs-mode t)))
#+END_SRC

** Ruby

Include warning keywords in Ruby.

#+BEGIN_SRC elisp
  (add-hook 'ruby-mode-hook 'warnings-mode-hook)
#+END_SRC

** Go

And now my Go configuration. This includes stuff like the usage of =goimports=,
=gofmt= on save, among many other useful things.

#+BEGIN_SRC elisp
  (defun mssola-go-mode ()
    "My configuration for Go mode."

    ; Use goimports instead of go-fmt
    (setq gofmt-command "goimports")

    ; Call Gofmt before saving
    (add-hook 'before-save-hook 'gofmt-before-save)

    ; Integration flycheck with Go
    (add-to-list 'load-path
      (concat (getenv "GOPATH") "/src/github.com/dougm/goflymake"))
    (require 'go-flycheck)

    (evil-leader/set-key
      "." 'godef-jump-other-window)

    (setq indent-tabs-mode t)
    (flyspell-prog-mode)

    ; eldoc support
    (use-package go-eldoc
      :ensure t
      :config
      (require 'go-eldoc))

    (use-package go-add-tags
      :ensure t
      :config
      (evil-leader/set-key "t" 'go-add-tags)))

  ;; Go
  (use-package go-mode
    :ensure t
    :pin melpa-stable
    :config

    (add-hook 'go-mode-hook 'warnings-mode-hook)
    (add-hook 'go-mode-hook 'go-eldoc-setup)
    (add-hook 'go-mode-hook 'mssola-go-mode))
#+END_SRC

** Tabs vs spaces

Tabs or spaces? [[https://www.emacswiki.org/emacs/TabsSpacesBoth][Both]]. The =smart-tabs-mode= has the philosophy of: tabs for
indentation, spaces for alignment. This is only applied in languages where I'm
usings tabs for indentation (C, C++ and Go).

#+BEGIN_SRC elisp
  (use-package smart-tabs-mode
    :ensure t
    :config
    (smart-tabs-add-language-support golang go-mode-hook
      ((c-indent-line . c-basic-offset)
       (c-indent-region . c-basic-offset)))
    (smart-tabs-insinuate 'c 'c++ 'golang))
#+END_SRC

** Inferior markup languages.

#+BEGIN_SRC elisp
  ;; Markdown mode with preview mode in the browser.
  (use-package markdown-mode
    :ensure t
    :config

    ; This is the one that I got from openSUSE.
    (custom-set-variables
      '(markdown-command "/usr/bin/markdown-calibre"))

    ; Preview mode does its things through websockets, so it's a requirement.
    ; After that, we can safely require it.
    (use-package websocket
      :ensure t
      :config
      (use-package markdown-preview-mode
        :ensure t)))

  ; YAML
  (use-package yaml-mode
    :ensure t
    :config

    (add-hook 'yaml-mode-hook 'warnings-mode-hook))
#+END_SRC

** Web-related stuff.

Slim and SCSS.

#+BEGIN_SRC elisp
(use-package slim-mode
  :ensure t)

(use-package scss-mode
  :ensure t
  :config

  (setq scss-compile-at-save nil)
  (add-hook 'yaml-mode-hook 'warnings-mode-hook))

(use-package coffee-mode
  :ensure t)
#+END_SRC

Languages specific for backend code like PHP, and =web-mode=, which provides a
bundle of features which are interesting for web-related stuff.

#+BEGIN_SRC elisp
  (use-package php-mode
    :ensure t)

  (use-package web-mode
    :ensure t
    :config

    (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.jinja\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))

    (add-hook 'web-mode-hook
              (lambda ()
                (setq web-mode-markup-indent-offset 2)
                (setq web-mode-css-indent-offset 2)
                (setq web-mode-code-indent-offset 2))))
#+END_SRC

The =json-reformat= package provides functions for reformatting JSON strings. It
happens from time to time that I have to read JSON output from responses, and it
can be frustrating without proper formatting.

#+BEGIN_SRC elisp
(use-package json-reformat
  :ensure t)
#+END_SRC

** Others

CMake for y'all.

#+BEGIN_SRC elisp
  (use-package cmake-mode
    :ensure t
    :config

    (setq auto-mode-alist
          (append
           '(("CMakeLists\\.txt\\'" . cmake-mode))
           '(("\\.cmake\\'" . cmake-mode))
           auto-mode-alist))

    (use-package cmake-font-lock
      :ensure t
      :config

      (add-hook 'cmake-mode-hook 'cmake-font-lock-activate)
      (add-hook 'yaml-mode-hook 'warnings-mode-hook)))
#+END_SRC

Some devops stuff, like Docker, terraform, etc. I have salt-mode disabled for
now because it's buggy, but I still have hope.

#+BEGIN_SRC elisp
  (use-package dockerfile-mode
    :ensure t
    :config

    (add-to-list 'auto-mode-alist '("Dockerfile\\'" . dockerfile-mode)))

  (use-package terraform-mode
    :ensure t
    :config

    (terraform-format-on-save-mode))

  (use-package hcl-mode
    :ensure t)

  ;(use-package salt-mode
    ;:ensure t)
#+END_SRC

The [[https;//github.com/mssola/soria][soria]] theme has the =soria-purple-identifiers= hook. This hook instructs
the theme to use purple for identifiers instead of the default color. This is a
remnant from my Vim times, and I only apply it to some languages (random
criteria really).

#+BEGIN_SRC elisp
  (dolist (lang-hook '(ruby-mode-hook
                       php-mode-hook
                       perl-mode-hook
                       emacs-lisp-mode-hook))
    (add-hook lang-hook 'soria-purple-identifiers))
#+END_SRC

* WoMan

=WoMan= is a package that is included inside of GNU Emacs by default, and that
takes care of visualizing man pages. Let's properly setup Evil mode for this:

#+BEGIN_SRC elisp
(with-eval-after-load "evil"
  (evil-set-initial-state 'woman-mode 'normal)
  (evil-define-key 'normal woman-mode-map
    "J" 'Man-next-section
    "K" 'Man-previous-section
    "\C-h" 'evil-window-left
    "\C-l" 'evil-window-right
    "\C-j" 'evil-window-down
    "\C-k" 'evil-window-up
    "\C-u" 'evil-scroll-up))
#+END_SRC

Finally, I want man pages to fill all the frame:

#+BEGIN_SRC elisp
(setq woman-fill-frame t)
#+END_SRC

* Misc

Install a set of useful functions from [[https://github.com/bbatsov][@bbatsov]]. The bindings are following
Emacs style instead of being more Vim-like on purpose (I don't want to put
too many things into my leader and these shortcuts look sensible to me).

#+BEGIN_SRC elisp
(use-package crux
  :ensure t
  :config

  (global-set-key (kbd "C-c d") 'crux-delete-file-and-buffer)
  (global-set-key (kbd "C-c r") 'crux-rename-file-and-buffer)
  (global-set-key (kbd "C-c o") 'crux-open-with))
#+END_SRC

* Credits

I've built this file by simply scavenging from other people's emacs.d/dotfiles
repositories. I have taken lots of pieces from here and there, but most notably:

- [[https://github.com/ereslibre/dotfiles][@ereslibre]]
- [[https://github.com/dmacvicar/dotfiles][@dmacvicar]]
- [[https://github.com/bbatsov/emacs.d][@bbatsov]]
- [[https://github.com/aaronbieber/dotfiles][@aaronbieber]]
- [[https://github.com/purcell/emacs.d][@purcell]]
- [[https://github.com/sachac/.emacs.d][@sachac]] ([[http://pages.sachachua.com/.emacs.d/Sacha.html][HTML version]])
- [[https://github.com/larstvei/dot-emacs][@larstvei]]

* License

#+BEGIN_SRC text :tangle no
  Copyright (C) 2014-2017 Miquel Sabaté Solà <mikisabate@gmail.com>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#+END_SRC