-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
1312 lines (1105 loc) · 43.5 KB
/
init.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives
'("melpa-stable" . "https://stable.melpa.org/packages/") t)
(add-to-list 'package-archives
'("org" . "http://orgmode.org/elpa/") t)
(package-initialize)
;; Bootstrap `use-package'
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(eval-when-compile
(require 'use-package))
;; macOS settings
(when (memq window-system '(mac ns))
;; Set the environment to the same as the shell
(use-package exec-path-from-shell
:ensure t
:config
(exec-path-from-shell-initialize))
;; Set option key to be meta key
(setq mac-control-modifier 'control)
(setq mac-option-modifier 'meta)
(setq mac-command-modifier 'super)
;; Always use Chrome when opening links
(setq browse-url-browser-function 'browse-url-default-macosx-browser))
;; Max image size when using the builtin viewer
(setq max-image-size 50.0)
;; Disable visual bell
(setq visible-bell nil)
;; Disable backup files
(setq make-backup-files nil)
;; Disable auto save files
(setq auto-save-default nil)
(when window-system
;; Disable pscroll bars in gui emacs
(scroll-bar-mode -1)
;; Disable the tool bar in gui emacs
(tool-bar-mode -1))
;; Change the scratch buffer message to nothing
(setq initial-scratch-message nil)
;; Don't suspend emacs with C-z
(global-unset-key [?\C-z])
;; Use iBuffer by default for C-x C-b which is more readable than default
(global-set-key (kbd "C-x C-b") 'ibuffer)
;; Shortcut for occur
(global-set-key (kbd "C-x C-o") 'occur)
;; Shortcut for rgrep using ripgrep because it's faster
(global-set-key (kbd "C-x C-r") 'ripgrep-regexp)
;; Shortcut for rectangle edits
(global-set-key (kbd "C-x r i") 'string-insert-rectangle)
;; Nice startup screen
(use-package dashboard
:ensure t
:config
(dashboard-setup-startup-hook)
(setq dashboard-banner-logo-title "Welcome back Ender")
(setq dashboard-startup-banner 'logo)
(setq dashboard-center-content t)
(setq dashboard-items '((recents . 5)
(projects . 5)
(agenda . 5)))
(setq dashboard-set-heading-icons t)
(setq dashboard-set-file-icons t)
(setq show-week-agenda-p t))
;; Emdash
(defun insert-em-dash ()
"Insert a em-dash"
(interactive)
(insert "—"))
(global-set-key [(meta _)] 'insert-em-dash)
;; Delete trailing whitespace on save
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;; Show the time
(display-time-mode 1)
;; Git using magit
(use-package magit
:ensure t
:config
(global-set-key (kbd "C-c g") 'magit-status)
;; Disable built in VC mode for better performance
(setq vc-handled-backends nil)
;; Disable refreshing status for better performance
(setq magit-refresh-status-buffer nil)
;; Always open the commit edit message in a new window instead of the
;; *magit* window so you can see the diff
(add-to-list 'display-buffer-alist
'(".*COMMIT_EDITMSG". ((display-buffer-pop-up-window) .
((inhibit-same-window . t))))))
;; Flycheck
(use-package flycheck
:ensure t
:config
;; (global-flycheck-mode)
(setq flycheck-global-modes '(not rust-mode))
(global-set-key (kbd "C-c M-n") 'flycheck-next-error)
(global-set-key (kbd "C-c M-p") 'flycheck-previous-error))
;; Web mode
(use-package web-mode :ensure t
:config
(defun web-mode-customization ()
"Customization for web-mode."
(setq web-mode-markup-indent-offset 2)
(setq web-mode-css-indent-offset 2)
(setq web-mode-code-indent-offset 2)
(setq web-mode-enable-auto-pairing t)
(setq web-mode-enable-css-colorization t))
(add-hook 'web-mode-hook 'web-mode-customization)
;; Javascript
(add-to-list 'auto-mode-alist '("\\.js$" . web-mode))
(add-to-list 'auto-mode-alist '("\\.jsx$" . web-mode))
;; Turn off auto saving because js build tools hate temp files
(add-hook 'web-mode-hook '(lambda () (setq auto-save-default nil)))
;; HTML
(add-to-list 'auto-mode-alist '("\\.html$" . web-mode))
;; CSS
(add-to-list 'auto-mode-alist '("\\.css$" . web-mode)))
(use-package toml-mode
:ensure t
:config
(add-to-list 'auto-mode-alist '("\\.toml$" . toml-mode)))
(use-package rust-mode
:ensure t
:after (company eglot)
:config
(add-hook 'rust-mode-hook #'company-mode)
(define-key rust-mode-map (kbd "TAB") #'company-indent-or-complete-common)
(define-key rust-mode-map (kbd "C-c TAB") #'rust-format-buffer))
(use-package cargo
:ensure t
:config
(add-hook 'rust-mode-hook 'cargo-minor-mode)
(define-key cargo-minor-mode-map (kbd "C-c C-c C-l") 'cargo-process-clippy))
(use-package eglot
:ensure t
:config
(global-set-key (kbd "M-n") 'flymake-goto-next-error)
(global-set-key (kbd "M-p") 'flymake-goto-prev-error)
(define-key eglot-mode-map (kbd "C-c h") 'eglot-help-at-point)
;; Better support for rust projects with multiple sub projects
(defun my-project-try-cargo-toml (dir)
(when-let* ((output
(let ((default-directory dir))
(shell-command-to-string "cargo metadata --no-deps --format-version 1")))
(js (ignore-errors (json-read-from-string output)))
(found (cdr (assq 'workspace_root js))))
(cons 'eglot-project found)))
(cl-defmethod project-roots ((project (head eglot-project)))
(list (cdr project)))
(add-hook 'project-find-functions 'my-project-try-cargo-toml nil nil))
(use-package yasnippet
:ensure t
:config
(yas-global-mode 1))
(use-package lsp-ui
:ensure t
:config
;; Nicer peek and find M-. and M-? respectively
(define-key lsp-ui-mode-map [remap xref-find-definitions] #'lsp-ui-peek-find-definitions)
(define-key lsp-ui-mode-map [remap xref-find-references] #'lsp-ui-peek-find-references))
(use-package lsp-mode
:ensure t
:hook ((rust-mode . lsp))
:config
;; Use rust-analyzer rather than rls
(setq lsp-rust-server 'rust-analyzer)
(setq lsp-enable-file-watchers nil)
(setq lsp-enable-completion-at-point t)
(setq lsp-enable-imenu t)
(setq lsp-rust-analyzer-cargo-watch-enable nil)
;; Use lsp-ui
(add-hook 'rust-mode-hook 'lsp-ui-mode))
;; Elisp
(use-package paredit
:ensure t
:config
(add-hook 'elisp-mode-hook 'paredit-mode)
(add-hook 'clojure-mode-hook 'paredit-mode))
;; Python
(use-package python-mode
:ensure t
:config
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)))
(use-package elpy
:ensure t
:config
(elpy-enable)
(setq elpy-rpc-backend "rope"))
;; Ruby
(use-package robe
:ensure t
:config
;; Sane indenting
(setq ruby-deep-indent-paren nil)
;; Don't auto add file encodings!!!
(setq ruby-insert-encoding-magic-comment nil)
(add-hook 'ruby-mode-hook 'robe-mode)
;; Use specific rubocop
(defun use-custom-rubocop ()
(let* ((root (locate-dominating-file
(or (buffer-file-name) default-directory)
"scripts/bin/rubocop"))
(rubocop (and root
(expand-file-name "scripts/bin/rubocop"
root))))
(when (and rubocop (file-executable-p rubocop))
(setq-local flycheck-ruby-rubocop-executable rubocop))))
(add-hook 'flycheck-mode-hook #'use-custom-rubocop))
;; Format line numbers nicely
(setq linum-format (quote " %3d "))
;; Sass-mode
(use-package sass-mode
:ensure t
:config (setq sass-tab-width 2))
;; Processing mode
(use-package processing-mode
:ensure t
:config
(add-to-list 'auto-mode-alist '("\\.pde$" . processing-mode))
(setq processing-location "~/Library/Processing"))
;; Markdown
(use-package markdown-mode
:ensure t
:config
(add-to-list 'auto-mode-alist '("\\.md$" . markdown-mode)))
;; Nice writing layout
(use-package writeroom-mode
:ensure t
:config
(add-hook 'writeroom-mode-hook
(lambda ()
;; Use custom font face for this buffer only
(defface tmp-buffer-local-face
'((t :family "ETBembo" :height 180))
"Temporary buffer-local face")
(buffer-face-set 'tmp-buffer-local-face)
;; Add padding to the top of the frame
(setq header-line-format " ")
;; Use a skinny cursor
(make-local-variable 'cursor-type)
(setq cursor-type 'bar))))
;; Detect things like weasel words
(use-package writegood-mode
:ensure t
:config
(global-set-key "\C-c\C-gl" 'writegood-grade-level)
(global-set-key "\C-c\C-ge" 'writegood-reading-ease))
;; Unity
(use-package glsl-mode
:ensure t
:config
(add-to-list 'auto-mode-alist '("\\.shader$" . glsl-mode)))
;; Linum mode shortcut
(global-set-key (kbd "C-x l") 'linum-mode)
;; iEdit mode
(global-set-key (kbd "C-x ;") 'iedit-mode)
;; Web browser (eww)
(global-set-key (kbd "C-x M-w") 'eww)
;; Disable auto fill mode because it's annoying
(auto-fill-mode -1)
(remove-hook 'text-mode-hook #'turn-on-auto-fill)
;; Auto-complete company-mode
(use-package company
:ensure t
:config
(add-hook 'after-init-hook 'global-company-mode))
;; Shortcuts for going forward and backwards cycling windows
(global-set-key (kbd "C-x p") 'other-window)
(global-set-key (kbd "C-x o") 'previous-multiframe-window)
;; Expand region
(use-package expand-region
:ensure t
:config
(global-set-key (kbd "\C-x\ =") 'er/expand-region))
;; Ace jump mode
(use-package ace-jump-mode
:ensure t
:config
(define-key global-map (kbd "C-c SPC") 'ace-jump-mode))
;; Auto refresh all buffers when files change ie git branch switching
(global-auto-revert-mode t)
;; Don't use any character as the vertical border divider
(set-display-table-slot standard-display-table
'vertical-border
(make-glyph-code ? ))
;; Gui emacs still sets a line color in between
(set-face-background 'vertical-border "gray32")
(set-face-foreground 'vertical-border (face-background 'vertical-border))
;; When at the end of a buffer don't jump, scroll
(setq scroll-step 1 scroll-conservatively 10000)
(setq scroll-margin 0)
(use-package clojure-mode :ensure t)
;; Kibit for Clojure
;; A convenient command to run "lein kibit" in the project to which
;; the current emacs buffer belongs to.
(defun kibit ()
"Run kibit on the current project.
Display the results in a hyperlinked *compilation* buffer."
(interactive)
(compile "lein kibit"))
;; Rainbow parantheses
(use-package rainbow-delimiters
:ensure t
:config
(add-hook 'prog-mode-hook 'rainbow-delimiters-mode))
;; Cider settings
(use-package cider
:ensure t
:config
(add-hook 'cider-repl-mode-hook 'rainbow-delimiters-mode)
(add-hook 'cider-repl-mode-hook 'paredit-mode)
(add-hook 'cider-repl-mode-hook
'(lambda ()
(set-variable 'nrepl-server-command
"lein with-profile dev repl")))
;; Disable popup stacktraces except in repl
(setq nrepl-popup-stacktraces nil)
(setq nrepl-popup-stacktraces-in-repl t)
;; Disable auto-selection of the error buffer when it's displayed:
(setq cider-auto-select-error-buffer nil)
;; Instructions for M-x cider-jack-in-clojurescript
(setq cider-cljs-lein-repl
"(do (require 'figwheel-sidecar.repl-api)
(figwheel-sidecar.repl-api/start-figwheel!)
(figwheel-sidecar.repl-api/cljs-repl))"))
;; Flyspell mode
(use-package flyspell
:ensure t
:config
(setq ispell-program-name "/usr/local/bin/aspell")
(add-hook 'python-mode-hook (lambda () (flyspell-prog-mode)))
(add-hook 'coffee-mode-hook (lambda () (flyspell-prog-mode)))
(add-hook 'hbs-mode-hook (lambda () (flyspell-prog-mode)))
(add-hook 'org-mode-hook (lambda () (flyspell-prog-mode))))
;; Use spaces instead of tabs when indenting
(setq-default indent-tabs-mode nil)
;; Org-mode
;; Copied from https://github.com/glasserc/etc/commit/3af96f2c780a35d35bdf1b9ac19d80fe2e6ebbf8
;; Work around built-in org-mode so we can load from ELPA.
;; First, remove the built-in org directory from the load-path.
;; Thanks to
;; http://stackoverflow.com/questions/20603578/emacs-does-not-see-new-installation-of-org-mode/20616703#20616703.
;; Without this, use-package will try to require org and succeed.
(eval-when-compile
(require 'cl))
(setq load-path (remove-if (lambda (x) (string-match-p "org$" x)) load-path))
;; Second, trick emacs into forgetting about the fact that org is
;; a "built-in" package by removing it from package--builtins.
;; Without this, package will refuse to install org, since it's
;; "already installed".
;; package--builtins is only initialized when a query needs it.
(package-built-in-p 'org) ;; prime package--builtins
(setq package--builtins (assq-delete-all 'org package--builtins))
(use-package org
:ensure org-plus-contrib
:pin org
:config
(setq org-directory "~/Org")
;; When opening a file make sure everything is expanded
(setq org-startup-folded nil)
;; Always wrap lines
(setq org-startup-truncated nil)
;; Show inline images
(org-display-inline-images t t)
;; Don't show full size images otherwise it's too large when
;; displaying inline
(setq org-image-actual-width nil)
;; Prettify outlines
(add-hook 'org-mode-hook
(lambda ()
(push '("- [ ]" . "☐") prettify-symbols-alist)
(push '("- [X]" . "☑") prettify-symbols-alist)
(prettify-symbols-mode)))
;; Refile to the root of a file
(setq org-refile-use-outline-path 'file)
(setq org-refile-targets '((org-agenda-files :level . 1)))
;; Show the agenda helper and viewer in split screen
(defadvice org-agenda (around split-vertically activate)
(let ((split-width-threshold 80))
ad-do-it))
(defadvice org-agenda-list (around split-vertically activate)
(let ((split-width-threshold 80))
ad-do-it))
(defun org-agenda-and-todos ()
(interactive)
(org-agenda nil "n"))
(defun org-agenda-and-todos-two-weeks ()
(interactive)
(let ((current-prefix-arg 14))
(call-interactively 'org-agenda-and-todos)))
(defun org-agenda-and-todos-last-week ()
(interactive)
(let ((current-prefix-arg -7))
(call-interactively 'org-agenda-and-todos)))
;; Shortcut copy an internal link
(global-set-key (kbd "C-c l") 'org-store-link)
;; Shortcut to show preferred agenda view
(global-set-key (kbd "C-c A") 'org-agenda-and-todos-two-weeks)
;; Show hours:minutes instead of days:hours
(setq org-time-clocksum-format (quote (:hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)))
;; Shortcut to jump to last running clock
(global-set-key (kbd "C-c C-x C-j") 'org-clock-jump-to-current-clock)
;; On startup show the agenda for the next 2 calendar weeks and all
;; todo items
(add-hook 'after-init-hook 'org-agenda-and-todos-two-weeks)
;; Show org timestamps in 12h time
(setq org-agenda-timegrid-use-ampm 1)
;; Always show the current day in org agenda
(setq org-agenda-time-grid (quote
((daily today today)
(800 1000 1200 1400 1600 1800 2000)
"......" "----------------")))
;; Color code the agenda based on type
;; http://dept.stat.lsa.umich.edu/~jerrick/org_agenda_calendar.html
(defun color-org-header (tag col)
(interactive)
(goto-char (point-min))
(while (re-search-forward tag nil t)
(add-text-properties (match-beginning 0) (point-at-eol)
`(face (:foreground ,col)))))
(add-hook 'org-finalize-agenda-hook
(lambda ()
(save-excursion
(color-org-header "notes:" "#66D9EF")
(color-org-header "refile:" "#F92672"))))
;; Use longtable as the default table style when exporting
(setq org-latex-default-table-environment "longtable")
;; Don't position tables in the center
(setq org-latex-tables-centered nil)
;; Fix some org-mode + yasnippet conflicts
;; http://stackoverflow.com/questions/9418148/conflicts-between-org-mode-and-yasnippet
(defun yas/org-very-safe-expand ()
(let ((yas/fallback-behavior 'return-nil)) (yas/expand)))
(add-hook 'org-mode-hook
(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])
(add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
(define-key yas/keymap [tab] 'yas/next-field)))
(add-hook 'org-mode-hook (lambda ()
(make-variable-buffer-local 'visual-line-mode)
(visual-line-mode)))
;; Show syntax highlighting per language native mode in *.org
(setq org-src-fontify-natively t)
;; For languages with significant whitespace like Python:
(setq org-src-preserve-indentation t)
;; Timestamp new todos
(setq org-log-done 'time)
(setq org-startup-indented t)
;; Agenda
(setq org-agenda-text-search-extra-files
'(agenda-archives
"~/Org/notes.org_archive"))
(define-key global-map (kbd "C-c a") 'org-agenda)
(define-key global-map (kbd "C-c C-a") 'org-agenda)
;; In org agenda log view also show recurring tasks
(setq org-agenda-log-mode-items '(closed clock state))
(defun org-archive-done-tasks ()
"Archive all DONE and WONT-DO tasks."
(interactive)
(org-map-entries
(lambda ()
(org-archive-subtree)
(setq org-map-continue-from (outline-previous-heading)))
"+TODO=\"DONE\"" 'tree)
(org-map-entries
(lambda ()
(org-archive-subtree)
(setq org-map-continue-from (outline-previous-heading)))
"+TODO=\"WONT-DO\"" 'tree))
;; Capture
(global-set-key (kbd "C-c c") 'org-capture)
(setq org-default-notes-file "~/Org/refile.org")
;; Allow the creation of parent headings when refiling
(setq org-refile-allow-creating-parent-nodes t)
(setq org-capture-templates
(quote (("t" "To Do" entry (file "~/Org/refile.org")
"* TODO %?\n%U" :clock-in t :clock-resume t)
("n" "Note" entry (file "~/Org/refile.org")
"* %? %T :note:\n%U\n%a\n" :clock-in t :clock-resume t)
("m" "Meeting" entry (file "~/Org/refile.org")
"* Meeting w/%? %T :meeting:\n%U" :clock-in t :clock-resume t)
("i" "Interview" entry (file "~/Org/refile.org")
"* Interview w/%? %T :interview:\n%U" :clock-in t :clock-resume t))))
;; Auto mark parent todos as done if childrend are done
(defun org-summary-todo (n-done n-not-done)
"Switch entry to DONE when all subentries are done, to TODO otherwise."
(let (org-log-done org-log-states) ; turn off logging
(org-todo (if (= n-not-done 0) "DONE" "TODO"))))
;; Refile
; Targets include this file and any file contributing to the agenda -
; up to 9 levels deep
(setq org-refile-targets (quote ((nil :maxlevel . 9)
(org-agenda-files :maxlevel . 9))))
; Use full outline paths for refile targets - we file directly with
; IDO
(setq org-refile-use-outline-path t)
; Targets complete directly with IDO
(setq org-outline-path-complete-in-steps nil)
; Allow refile to create parent tasks with confirmation
(setq org-refile-allow-creating-parent-nodes (quote confirm))
; Use IDO for both buffer and file completion and ido-everywhere to t
(setq org-completion-use-ido t)
; Use the current window for indirect buffer display
(setq org-indirect-buffer-display 'current-window)
;;;; Refile settings
(add-hook 'org-after-todo-statistics-hook 'org-summary-todo)
;; Time format for clock table durations as h:mm
(setq org-duration-format (quote h:mm))
;; Don't prompt for confirmation when exporting babel blocks
(setq org-confirm-babel-evaluate nil))
;; Babel setup
(use-package ob-python
:defer t
:commands (org-babel-execute:python))
(use-package ob-shell
:defer t
:commands
(org-babel-execute:sh
org-babel-expand-body:sh
org-babel-execute:bash
org-babel-expand-body:bash))
(use-package htmlize :ensure t)
;; Org export
(use-package ox-reveal :ensure t)
(use-package ox-jira :ensure t)
(use-package ox-hugo :ensure t :after ox)
;; Heavily modified based on https://github.com/novoid/title-capitalization.el/blob/master/title-capitalization.el
(defun title-capitalization (str)
"Convert str to title case"
(interactive)
(with-temp-buffer
(insert str)
(let* ((beg (point-min))
(end (point-max))
;; Basic list of words which don't get capitalized according to simplified rules
;; http://karl-voit.at/2015/05/25/elisp-title-capitalization/
(do-not-capitalize-basic-words '("a" "ago" "an" "and" "as" "at" "but" "by" "for"
"from" "in" "into" "it" "next" "nor" "of" "off"
"on" "onto" "or" "over" "past" "so" "the" "till"
"to" "up" "yet"
"n" "t" "es" "s"))
;; If user has defined 'my-do-not-capitalize-words, append to basic list
(do-not-capitalize-words (if (boundp 'my-do-not-capitalize-words)
(append do-not-capitalize-basic-words my-do-not-capitalize-words )
do-not-capitalize-basic-words)))
;; Go to begin of first word
(goto-char beg)
(setq continue t)
;; Go through the region, word by word
(while continue
(let ((last-point (point)))
(let ((word (thing-at-point 'word)))
(if (stringp word)
;; Capitalize current word except when it is list member
(if (and (member (downcase word) do-not-capitalize-words)
;; Always capitalize first word
(not (= (point) 1)))
(downcase-word 1)
;; If it's an acronym, don't capitalize
(if (string= word (upcase word))
(progn
(goto-char (+ (point) (length word) 1)))
(capitalize-word 1)))))
(skip-syntax-forward "^w" end)
;; Break if we are at the end of the buffer
(when (= (point) last-point)
(setq continue nil))))
;; Always capitalize the last word
(backward-word 1)
(let ((word (thing-at-point 'word)))
(if (and (>= (point) 0)
(not (member (or word "s")
'("n" "t" "es" "s")))
(not (string= word (upcase word))))
(capitalize-word 1))))
(buffer-string)))
;; Org roam
;; These are specified so they can be dynamically configured
;; by calling emacs in batch mode in a CI context
(setq org-roam-notes-path "~/Org/notes")
(setq org-roam-publish-path "~/Projects/zettel")
(use-package org-roam
:hook
((after-init . org-roam-mode)
;; Need to add advice after-init otherwise they won't take
(after-init . (lambda ()
(advice-add 'org-roam-capture
:after
'my/note-taking-init)
(advice-add 'org-roam-dailies-today
:after
'my/note-taking-init)
(advice-add 'org-roam-find-file
:after
'my/note-taking-init))))
:custom
(org-roam-directory org-roam-notes-path)
:init
;; These functions need to be in :init otherwise they will not be
;; callable in an emacs --batch context which for some reason
;; can't be found in autoloads if it's under :config
(defun my/org-roam--extract-note-body (file)
(with-temp-buffer
(insert-file-contents file)
(org-mode)
(first (org-element-map (org-element-parse-buffer) 'paragraph
(lambda (paragraph)
(let ((begin (plist-get (first (cdr paragraph)) :begin))
(end (plist-get (first (cdr paragraph)) :end)))
(buffer-substring begin end)))))))
;; Include backlinks in org exported notes not tagged as private or
;; draft
(defun my/org-roam--backlinks-list (file)
(if (org-roam--org-roam-file-p file)
(--reduce-from
(concat acc (format "- [[file:%s][%s]]\n#+begin_quote\n%s\n#+end_quote\n"
(file-relative-name (car it) org-roam-directory)
(title-capitalization (org-roam--get-title-or-slug (car it)))
(my/org-roam--extract-note-body (car it))))
""
(org-roam-db-query
[:select :distinct [links:from]
:from links
:left :outer :join tags :on (= links:from tags:file)
:where (and (= to $s1)
(or (is tags:tags nil)
(and
(not-like tags:tags '%private%)
(not-like tags:tags '%draft%))))]
file))
""))
(defun file-path-to-md-file-name (path)
(let ((file-name (first (last (split-string path "/")))))
(concat (first (split-string file-name "\\.")) ".md")))
(defun file-path-to-slug (path)
(let* ((file-name (car (last (split-string path "--"))))
(title (first (split-string file-name "\\."))))
(replace-regexp-in-string (regexp-quote "_") "-" title nil 'literal)))
;; Fetches all org-roam files and exports to hugo markdown
;; files. Adds in necessary hugo properties
;; e.g. HUGO_BASE_DIR. Ignores notes tagged as private or draft
(defun org-roam-to-hugo-md ()
(interactive)
;; Make sure the author is set
(setq user-full-name "Alex Kehayias")
(let ((files (mapcan
(lambda (x) x)
(org-roam-db-query
[:select [files:file]
:from files
:left :outer :join tags :on (= files:file tags:file)
:where (or (is tags:tags nil)
(and
(not-like tags:tags '%private%)
(not-like tags:tags '%draft%)))]))))
(mapc
(lambda (f)
;; Use temporary buffer to prevent a buffer being opened for
;; each note file.
(with-temp-buffer
(message "Working on: %s" f)
(insert-file-contents f)
(goto-char (point-min))
;; Add in hugo tags for export. This lets you write the
;; notes without littering HUGO_* tags everywhere
;; HACK:
;; org-export-output-file-name doesn't play nicely with
;; temp buffers since it attempts to get the file name from
;; the buffer. Instead we explicitely add the name of the
;; exported .md file otherwise you would get prompted for
;; the output file name on every note.
(insert
(format "#+HUGO_BASE_DIR: %s\n#+HUGO_SECTION: ./\n#+HUGO_SLUG: %s\n#+EXPORT_FILE_NAME: %s\n"
org-roam-publish-path
(file-path-to-slug f)
(file-path-to-md-file-name f)))
;; If this is a placeholder note (no content in the
;; body) then add default text. This makes it look ok when
;; showing note previews in the index and avoids a headline
;; followed by a headline in the note detail page.
(if (eq (my/org-roam--extract-note-body f) nil)
(progn
(goto-char (point-max))
(insert "\n/This note does not have a description yet./\n")))
;; Add in backlinks because
;; org-export-before-processing-hook won't be useful the
;; way we are using a temp buffer
(let ((links (my/org-roam--backlinks-list f)))
(unless (string= links "")
(goto-char (point-max))
(insert (concat "\n* Links to this note\n") links)))
(org-hugo-export-to-md)))
files)))
:bind (:map org-roam-mode-map
(("C-c n l" . org-roam)
("C-c n f" . org-roam-find-file)
("C-c n g" . org-roam-graph)
("C-c n c" . org-roam-capture)
("C-c n j" . org-roam-dailies-today)
("C-c n e" . org-roam-to-hugo-md)
;; Full text search notes with an action to insert
;; org-mode link
("C-c n s" . helm-rg))
:map org-mode-map
(("C-c n i" . org-roam-insert))))
:config
(setq org-roam-capture-templates
(quote (("d" "Default" plain (function org-roam--capture-get-point)
"%?"
:file-name "%(format-time-string \"%Y-%m-%d--%H-%M-%SZ--${slug}\" (current-time) t)"
:head "#+TITLE: ${title}\n#+DATE: %<%Y-%m-%d>\n#+ROAM_ALIAS:\n#+ROAM_TAGS:\n\n"
:unnarrowed t))))
(setq org-roam-dailies-capture-templates
(quote (("d" "Default" plain (function org-roam--capture-get-point)
"%?"
:file-name "%(format-time-string \"%Y-%m-%d--%H-%M-%SZ--journal\" (current-time) t)"
:head "#+TITLE: Journal %<%Y-%m-%d>\n#+DATE: %<%Y-%m-%d>\n#+ROAM_ALIAS:\n#+ROAM_TAGS: private journal\n\n"
:unnarrowed t))))
(setq org-roam-completion-system 'helm)
;; Use writeroom mode when capturing new notes. Hide the ugly
;; preamble of org attributes by scrolling up.
(defun my/note-taking-init (&rest r)
(with-current-buffer (current-buffer)
(writeroom-mode)
(scroll-up-command 4)))
(use-package company-org-roam
:ensure t
:config
(push 'company-org-roam company-backends))
(use-package which-key
:ensure t
:config
(which-key-mode))
;; Macro for running a function repeatedly in the back ground
;; https://github.com/punchagan/dot-emacs/blob/master/punchagan.org
(defmacro run-with-timer-when-idle (secs idle-time repeat-time function &rest args)
"Run a function on timer, but only when idle."
`(run-with-timer
,secs
,repeat-time
(lambda () (run-with-idle-timer ,idle-time nil ,function ,@args))))
;; Use golden ratio mode which automatically resizes buffers based on
;; the golden ratio
(use-package golden-ratio
:ensure t
:config
;; (golden-ratio-mode 1)
;; (setq golden-ratio-auto-scale nil)
(defadvice previous-multiframe-window
(after golden-ratio-resize-window)
(golden-ratio) nil))
;; Much better terminal emulator. Requires that emacs is installed
;; --with-modules to work.
(use-package vterm)
(defun new-term (buffer-name)
"Start a terminal and rename buffer."
(interactive "sbuffer name: ")
(vterm)
(rename-buffer buffer-name t))
;; Shortcut to create a new term
(define-key global-map (kbd "C-x M-t") 'new-term)
;; Speed up ansi-term in emacs 24.x by skipping guessing left-to-right input
(add-hook 'term-mode-hook 'my-term-mode-hook)
(defun my-term-mode-hook ()
(setq bidi-paragraph-direction 'left-to-right))
;; Shortcut to jump word and ignore underscore
(define-key global-map (kbd "M-F") 'forward-sexp)
(define-key global-map (kbd "M-B") 'forward-sexp)
;; Highlight changes is removed after save
(defun highlight-changes-remove-after-save ()
"Remove previous changes after save."
(make-local-variable 'after-save-hook)
(add-hook 'after-save-hook
(lambda ()
(highlight-changes-remove-highlight (point-min) (point-max)))))
;; Center text
(defun center-text ()
"Center the text in the middle of the buffer. Works best in full screen"
(interactive)
(set-window-margins (car (get-buffer-window-list (current-buffer) nil t))
(/ (window-width) 4)
(/ (window-width) 4)))
(defun center-text-clear ()
(interactive)
(set-window-margins (car (get-buffer-window-list (current-buffer) nil t))
nil
nil))
(setq centered nil)
(defun center-text-mode ()
(interactive)
(make-local-variable 'centered)
(if (eq centered t)
(progn (center-text-clear)
(setq centered nil))
(progn (center-text)
(setq centered t))))
(define-key global-map (kbd "C-c M-t") 'center-text-mode)
;; Enable system clipboard
(setq x-select-enable-clipboard t)
;; Stackoverflow plugin
(use-package sos :ensure t)
;; Start emacs without all the fanfare
(setq inhibit-startup-echo-area-message t)
(setq inhibit-startup-message t)
;; Rename file and buffer in one shot
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
;; ido
;; Make sure we are using ido mode
(use-package ido
:ensure t
:config
(ido-mode 1)
(ido-everywhere 1)
(setq ido-everywhere t)
(ido-mode (quote both))
(setq ido-use-faces t)
;; Don't magically search for a file that doesn't exist
(setq ido-auto-merge-work-directories-length -1))
;; Use ido inside ido buffer too
(use-package flx-ido
:ensure t
:config
(flx-ido-mode 1)
(setq ido-enable-flex-matching t)