-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.el
1261 lines (977 loc) · 40.4 KB
/
init.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; init.el -*- lexical-binding: t; -*-
;; =======
;; load-path
;; ---------
(setq init-dir (expand-file-name (concat "~" init-file-user "/.emacs.d")))
(setq load-path (append
(list (concat init-dir "/lisp"))
load-path))
;; melpa.org packages
;; ------------------
(require 'package)
(setq package-archives '(("melpa" . "http://melpa.org/packages/")
("gnu" . "http://elpa.gnu.org/packages/")))
(package-initialize)
;; This is only needed once, near the top of the file
(eval-when-compile
;; Following line is not needed if use-package.el is in ~/.emacs.d
(require 'use-package))
;; enable or disable some options
;; ------------------------------
(setq *local-tabbar-enabled* nil)
;; my font size
;; ------------
(defun my-font-height ()
"compute font height based on screeen height"
(let* ((h (display-pixel-height))
(selected (cond
((>= h 1000) 160)
((>= h 900) 160)
(t 120))))
(message "display height = %d => %d" h selected)
selected))
;; menubar/toolbar
;; ---------------
(tool-bar-mode 0)
(menu-bar-mode 0)
;; special options for X11
;; -----------------------
(when window-system
(set-frame-position (selected-frame) 0 0)
(set-frame-parameter (selected-frame) 'mouse-color "red")
(set-frame-parameter (selected-frame) 'fullscreen 'maximized))
;; special options for non-X11
;; ---------------------------
(unless window-system
(setq inhibit-splash-screen t)
(menu-bar-mode -1)
(toggle-scroll-bar -1)
(setq backup-inhibited t)
(setq auto-save-default nil)
)
;; function keys
;; -------------
(message "init.el: Function keys")
;;(global-set-key (kbd "<f1>") 'save-buffers-kill-emacs)
(global-set-key (kbd "<f2>") #'(lambda () "save buffer" (interactive) (delete-other-windows) (save-buffer)))
(global-set-key (kbd "<f3>") 'match-paren)
(global-set-key (kbd "<f4>") #'(lambda () "toggle between buffers" (interactive) (switch-to-buffer nil)))
(global-set-key (kbd "<f5>") 'toggle-case-char-at-point)
(global-set-key (kbd "S-<f5>") #'(lambda (arg)
"add a fix this note"
(interactive "p")
(let ((tag "***** FIX THIS: "))
(comment-indent)
(unless (looking-at tag) (insert tag)))))
(global-set-key (kbd "<f6>") #'(lambda (arg)
"set up flyspell"
(interactive "p")
(flyspell-mode 1)
(flyspell-buffer)))
(global-set-key (kbd "<f7>") 'set-mark-command)
(global-set-key (kbd "S-SPC") 'set-mark-command)
(global-set-key (kbd "<f8>") 'call-last-kbd-macro)
(global-set-key (kbd "<f9>") 'delete-trailing-whitespace)
(global-set-key (kbd "<f10>") 'bury-buffer)
(global-set-key (kbd "<f11>") 'my-server-edit)
(global-set-key (kbd "<f12>") 'delete-other-windows)
;; miscellaneous keys
;; ------------------
(message "init.el: Global keys")
(global-set-key (kbd "M-z") 'save-buffer)
(global-set-key (kbd "M-g") 'goto-line)
(global-set-key (kbd "<kp_5>") 'goto-line) ; 5
(global-set-key (kbd "<home>") 'goto-line) ; 5
(global-set-key (kbd "C-\\") 'undo)
(global-set-key (kbd "M-ESC") 'keyboard-quit) ; Esc-Esc
(global-set-key (kbd "S-<pause>") 'eval-region) ; Shift Pause
(global-set-key (kbd "C-<pause>") 'eval-defun) ; Ctrl Pause
(global-set-key (kbd "C-<prior>") 'beginning-of-buffer) ; CTRL-Page Up
(global-set-key (kbd "C-<next>") 'end-of-buffer) ; CTRL-Page Down
(global-set-key (kbd "<insert>") 'comment-region) ; Insert comment markers
(global-set-key (kbd "C-<insert>") 'uncomment-region) ; Remove comment markers
(global-set-key (kbd "s-r") #'(lambda () (interactive) (revert-buffer t t nil))) ; Win-r
(global-set-key (kbd "s-h") 'hoogle) ; Win-h
(global-set-key (kbd "C-<print>") 'my-control-print-screen)
;; in cases where mode-name is a structure (e.g., in "Elisp" mode)
(defun mode-name-string ()
"if list, return the car else return the string"
(or (and (stringp mode-name) mode-name) (car mode-name)))
;; PrtScn key handler
(defun my-control-print-screen (arg)
"Do something useful with the Ctrl-PrtScn"
(interactive "P")
(let ((mn (mode-name-string)))
(cond ((string= mn "Markdown")
(my-markdown-to-pdf arg))
((string= mn "Org")
(my-org-to-html arg))
(t
(my-recompile arg) ; on t580 <print> is in the <menu> position
))))
;;(message "init.el: my-control-print-screen: unsupported mode: ~p", mode-name)))))
(when window-system
(global-unset-key (kbd "C-z")) ; iconify-or-deiconify-frame (C-x C-z)
)
;; ido mode
;; --------
(require 'ido)
(ido-mode t)
(global-set-key "\C-x\C-b" 'ibuffer)
(ido-toggle-prefix) ; ensure start in sub-string match
;; project-explorer
;; ----------------
;; toggle on/off a sidebar of files and directories in current directory
(when (require 'project-explorer "project-explorer" t)
(global-set-key (kbd "S-<f8>") 'project-explorer-toggle))
;; compilation keys
;; ----------------
(message "init.el: compilation setup")
;; Makefile detect in current or higher directory
;; then recompile.
;; no prefix -> make
;; - -> make test
;; zero -> make all
;; positive -> make emacs-N
;; negative -> make test-N
(require 'ansi-color)
(defun my-colour-compilation-buffer ()
(ansi-color-apply-on-region (point-min) (point-max))
(toggle-truncate-lines 1))
(add-hook 'compilation-filter-hook 'my-colour-compilation-buffer)
(defvar my-last-compilation-exit-status 0 "records the exit status of the last compile")
;; add a hook to remove compilation window if success
(defun my-compilation-finish (buffer status)
(with-current-buffer buffer
(save-excursion
(let ((status (replace-regexp-in-string "\n?$" "" status)))
(message "compilation finish: '%s' '%s'" buffer status)
(goto-char (point-min)) ; find compilation-dir: DIR-NAME in make output and override THIS_DIR as base
(when (search-forward-regexp "compilation-dir:[[:space:]]+\\(.*\\)[[:space:]]" (point-max) t 1)
(message "compilation-dir: '%s'" (match-string 1))
(cd (match-string 1)))
(if (and (= compilation-num-errors-found 0)
(= compilation-num-warnings-found 0)
(= compilation-num-infos-found 0)
(= my-last-compilation-exit-status 0))
(let ((w (get-buffer-window)))
(message "compilation success: '%s' '%s'" buffer status)
(when w
(goto-char (point-min)) ; find close-compilation-window
(when (not (search-forward-regexp "close-compilation-window:[[:space:]]+no[[:space:]]" (point-max) t 1))
(sleep-for 2) ; allow time to view success
(delete-window w)))))))))
(add-hook 'compilation-finish-functions 'my-compilation-finish)
(defun my-capture-exit-status (process-status exit-status msg)
"save exit status to global variable"
(setq my-last-compilation-exit-status exit-status)
(cons msg exit-status))
(setq compilation-exit-message-function 'my-capture-exit-status)
(defun my-testcompile (arg)
"run the test-1 target"
(interactive "P")
(my-recompile -1))
(defun my-recompile (arg)
"Search for Makefile and recompile"
(interactive "P")
(let*
((base-directory-for-compile default-directory)
(target
(cond
((null arg) "")
((equal '- arg) "test")
((zerop arg) "all")
((> arg 0) (format "emacs-%d" arg))
((< arg 0) (format "test-%d" (- arg)))
(t "qqq")))
(the-makefile "Emakefile"))
(message "recompile target = %s" target)
(if (file-exists-p the-makefile)
(set (make-local-variable 'compile-command)
(concat "make -f " the-makefile " THIS_DIR=. PROJECTS_DIR=. " target))
(cl-loop for the-dir = default-directory
then (file-name-directory (directory-file-name the-dir))
until (or (null the-dir) (string-equal "/" the-dir))
when (file-exists-p (concat the-dir "/../" the-makefile))
do (progn
(message "dir = %s makefile = %s" the-dir the-makefile)
(set 'base-directory-for-compile (directory-file-name the-dir))
(set (make-local-variable 'compile-command)
(concat "make -f '../" the-makefile
"' -C '" base-directory-for-compile
"' THIS_DIR='" default-directory
"' PROJECTS_DIR='" (file-truename (concat base-directory-for-compile "/.."))
"' " target))
(cl-return))))
(message "recompile = %s" compile-command)
(delete-other-windows)
(recompile)
(with-current-buffer (get-buffer-create "*compilation*") ; so find error can work
(cd base-directory-for-compile))))
; standard Menu key
(global-set-key (kbd "C-<menu>") 'my-recompile) ; CTRL-Menu
(global-set-key (kbd "ESC <menu>") 'my-testcompile) ; Esc Menu
(global-set-key (kbd "M-<menu>") 'next-error) ; ALT-Menu
(global-set-key (kbd "S-<menu>") 'previous-error) ; Shift-Menu
; alternative Menu key
(global-set-key (kbd "C-<XF86MenuKB>") 'my-recompile) ; CTRL-Menu
(global-set-key (kbd "ESC <XF86MenuKB>") 'my-testcompile) ; Esc Menu
(global-set-key (kbd "M-<XF86MenuKB>") 'next-error) ; ALT-Menu
(global-set-key (kbd "S-<XF86MenuKB>") 'previous-error) ; Shift-Menu
;;*(global-set-key (kbd "C-<print>") 'my-recompile) ; CTRL-Print
;;* ^ see above
(global-set-key (kbd "M-<print>") 'next-error) ; ALT-Print
(global-set-key (kbd "S-<print>") 'previous-error) ; Shift-Print
;; compilation window
(setq split-height-threshold nil)
(setq split-width-threshold 0)
;; on compile scroll the split window
;;(setq compilation-scroll-output t)
;; on compile scroll the split window and scroll to first error
(setq compilation-scroll-output 'first-error)
;; mouse wheel
;; -----------
(message "init.el: Mouse wheel")
(defun my-mouse-wheel-up (arg)
"Scroll display"
(interactive "p")
(scroll-up 1)
)
(defun my-mouse-wheel-down (arg)
"Scroll display"
(interactive "p")
(scroll-down 1)
)
(global-set-key (kbd "<mouse-4>") 'my-mouse-wheel-down)
(global-set-key (kbd "<mouse-5>") 'my-mouse-wheel-up)
;; Unicode
;; -------
(message "init.el: unicode characters")
(defun unicode-symbol (name)
"Translate a symbolic name for a Unicode character -- e.g., LEFT-ARROW
or GREATER-THAN into an actual Unicode character code."
(decode-char 'ucs (cl-case name
;; arrows
(left-arrow 8592)
(up-arrow 8593)
(right-arrow 8594)
(down-arrow 8595)
(leftwards-double-arrow #X21D0)
(upwards-double-arrow #X21D1)
(rightwards-double-arrow #X21D2)
(downwards-double-arrow #X21D3)
;; boxes
(double-vertical-bar #X2551)
;; relational operators
(equal #X003d)
(not-equal #X2260)
(identical #X2261)
(not-identical #X2262)
(less-than #X003c)
(greater-than #X003e)
(less-than-or-equal-to #X2264)
(greater-than-or-equal-to #X2265)
(equivalent-to #X224D)
(strictly-equivalent-to #X2263)
;; logical operators
(logical-and #X2227)
(logical-or #X2228)
(logical-neg #X00AC)
;; :=
(colon-equals #X2254)
(equals-colon #X2255)
(func #X0192)
;; misc
(void #X2205)
(horizontal-ellipsis #X2026)
(double-exclamation #X203C)
(prime #X2032)
(double-prime #X2033)
(for-all #X2200)
(there-exists #X2203)
(element-of #X2208)
(double-plus #X29FA)
;; mathematical operators
(square-root #X221A)
(squared #X00B2)
(cubed #X00B3)
)))
(defun substitute-pattern-with-unicode (pattern symbol)
"Add a font lock hook to replace the matched part of PATTERN with the
Unicode symbol SYMBOL looked up with UNICODE-SYMBOL."
(interactive)
(font-lock-add-keywords
nil `((,pattern (0 (progn (compose-region (match-beginning 1) (match-end 1)
,(unicode-symbol symbol))
nil))))))
(defun substitute-patterns-with-unicode (patterns)
"Call SUBSTITUTE-PATTERN-WITH-UNICODE repeatedly."
(mapcar #'(lambda (x)
(substitute-pattern-with-unicode (car x)
(cdr x)))
patterns))
;; Lisp mode
;; ---------
(message "init.el: lisp greek text")
(defun pretty-greek ()
(let ((greek '("alpha" "beta" "gamma" "delta" "epsilon" "zeta" "eta" "theta" "iota" "kappa" "lambda" "mu" "nu" "xi" "omicron" "pi" "rho" "sigma_final" "sigma" "tau" "upsilon" "phi" "chi" "psi" "omega")))
(cl-loop for word in greek
for code = 97 then (+ 1 code)
do (let ((greek-char (make-char 'greek-iso8859-7 code)))
(font-lock-add-keywords nil
`((,(cl-concatenate 'string "\\(^\\|[^a-zA-Z0-9]\\)\\(" word "\\)[a-zA-Z]")
(0 (progn (decompose-region (match-beginning 2) (match-end 2))
nil)))))
(font-lock-add-keywords nil
`((,(cl-concatenate 'string "\\(^\\|[^a-zA-Z0-9]\\)\\(" word "\\)[^a-zA-Z]")
(0 (progn (compose-region (match-beginning 2) (match-end 2)
,greek-char)
nil)))))))))
(add-hook 'lisp-mode-hook 'pretty-greek)
(add-hook 'emacs-lisp-mode-hook 'pretty-greek)
;; rainbow mode
;; ------------
(when (require 'rainbow-delimiters "rainbow-delimiters" t)
(message "init.el: rainbow-delimiters")
(add-hook 'prog-mode-hook 'rainbow-delimiters-mode))
(when (require 'rainbow-identifiers "rainbow-identifiers" t)
(message "init.el: rainbow-identifiers")
(add-hook 'prog-mode-hook 'rainbow-identifiers-mode))
;; enable slime
;; ------------
(add-to-list 'load-path "/usr/share/common-lisp/source/slime/")
(when (require 'slime "slime" t)
(message "init.el: SLIME")
(setq slime-startup-animation nil)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))
;(add-hook 'inferior-lisp-mode-hook (lambda () (inferior-slime-mode t)))
(setq inferior-lisp-program "sbcl")
;; set up to use inferior lisp buffer to auto-load swank server on M-x slime
;; uses a custom core file (see comments below)
(setq slime-lisp-implementations
;; '((cmucl ("cmucl" "-quiet"))
;; (sbcl ("sbcl") :coding-system utf-8-unix)))
;; (sbcl ("sbcl" "--core" "~/sbcl.core-for-slime"))))
(list (list 'sbcl (list "sbcl" "--core" (expand-file-name "~/sbcl.core-with-swank"))
:init #'(lambda (port-file _)
(format "(swank:start-server %S)\n" port-file))
:coding-system 'utf-8-unix)))
(slime-setup '(slime-fancy slime-asdf))
;; creating the above custom core images
;; $ sbcl
;; * (mapc ’require ’(sb-bsd-sockets sb-posix sb-introspect sb-cltl2 asdf))
;; * (save-lisp-and-die "sbcl.core-for-slime")
;; $ sbcl
;; * (mapc ’require ’(sb-bsd-sockets sb-posix sb-introspect sb-cltl2 asdf swank))
;; * (swank-loader:dump-image "sbcl.core-with-swank")
;; * CTRL-D
;; Running an external sblc wath the swank server (for M-x slime-connect)
;; $ sbcl --core /path/to/sbcl.core-with-swank
;; * (swank:create-server :port 4005 :style :spawn :dont-close t)
)
;; Ocaml mode
;; ----------
(when (require 'caml-font "caml-font" t)
(message "init.el: ocaml")
(add-to-list 'auto-mode-alist '("\\.ml[iylp]?$" . tuareg-mode))
(defun ocaml-unicode ()
(interactive)
(substitute-patterns-with-unicode
(list (cons "\\(<-\\)" 'left-arrow)
(cons "\\(->\\)" 'right-arrow)
(cons "[^=]\\(=\\)[^=]" 'equal)
(cons "\\(==\\)" 'identical)
(cons "\\(\\!=\\)" 'not-identical)
(cons "\\(<>\\)" 'not-equal)
(cons "\\(()\\)" 'void)
(cons "\\<\\(sqrt\\)\\>" 'square-root)
(cons "\\(&&\\)" 'logical-and)
(cons "\\(||\\)" 'logical-or)
(cons "\\<\\(not\\)\\>" 'logical-neg)
(cons "\\(>\\)[^=]" 'greater-than)
(cons "\\(<\\)[^=]" 'less-than)
(cons "\\(>=\\)" 'greater-than-or-equal-to)
(cons "\\(<=\\)" 'less-than-or-equal-to)
(cons "\\(''\\)" 'double-prime)
(cons "\\('\\)" 'prime)
(cons "\\<\\(List.for_all\\)\\>" 'for-all)
(cons "\\<\\(List.exists\\)\\>" 'there-exists)
(cons "\\<\\(List.mem\\)\\>" 'element-of)
(cons "^ +\\(|\\)" 'double-vertical-bar))))
(add-hook 'caml-mode-hook 'ocaml-unicode)
(add-hook 'caml-mode-hook 'pretty-greek)
(add-hook 'tuareg-mode-hook 'ocaml-unicode)
(add-hook 'tuareg-mode-hook 'pretty-greek)
(autoload 'caml-mode "caml" "Major mode for editing Caml code." t)
(autoload 'run-caml "inf-caml" "Run an inferior Caml process." t)
;;(autoload 'caml-hilit "caml-hilight" "Hilit19 patterns used for Caml mode" t)
)
;; Merlin for Ocaml
(let ((opam-share (ignore-errors (car (process-lines "opam" "config" "var" "share")))))
(when (and opam-share (file-directory-p opam-share))
;; Register Merlin
(add-to-list 'load-path (expand-file-name "emacs/site-lisp" opam-share))
(autoload 'merlin-mode "merlin" nil t nil)
;; Automatically start it in OCaml buffers
(add-hook 'tuareg-mode-hook 'merlin-mode t)
(add-hook 'caml-mode-hook 'merlin-mode t)
;; Use opam switch to lookup ocamlmerlin binary
(setq merlin-command 'opam)))
;; Haskell mode
;; ------------
(message "init.el: Haskell")
(add-to-list 'auto-mode-alist '("\\.[hg]s$" . haskell-mode))
(add-to-list 'auto-mode-alist '("\\.hi$" . haskell-mode))
(add-to-list 'auto-mode-alist '("\\.l[hg]s$" . literate-haskell-mode))
(autoload 'haskell-mode "haskell-mode"
"Major mode for editing Haskell scripts." t)
(autoload 'literate-haskell-mode "haskell-mode"
"Major mode for editing literate Haskell scripts." t)
;; Add the following lines according to which modules you want to use:
(add-hook 'haskell-mode-hook 'turn-on-haskell-decl-scan)
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
;; Note that the two indentation modules are mutually exclusive:
;; add at most one.
(if t
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
)
;; from mrd's blog (sequence.complete.org)
;; (add-hook 'haskell-mode-hook 'turn-on-font-lock)
;; (add-to-list 'auto-mode-alist '("\\.hs\\'" . haskell-mode))
;; I wrote my own "newline and indent" function which brings any
;; code you split onto the newline back up to the same indentation
;; level it was at previously.
(when nil
(remove-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(remove-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
;; Just use tab-stop indentation, 2-space tabs
(add-hook 'haskell-mode-hook
(lambda ()
(turn-on-haskell-doc-mode)
(turn-on-haskell-simple-indent)
(setq indent-line-function 'tab-to-tab-stop)
(setq tab-stop-list
(cl-loop for i from 2 upto 120 by 2 collect i))
(local-set-key (kbd "RET") 'newline-and-indent-relative))
)
)
(defun newline-and-indent-relative ()
(interactive)
(newline)
(indent-to-column (save-excursion
(forward-line -1)
(back-to-indentation)
(current-column))))
;; change the display of some characters
;; commented out items are missing from the fonts
(defun haskell-unicode ()
(interactive)
(substitute-patterns-with-unicode
(list (cons "[^<]\\(<-\\)" 'left-arrow)
(cons "[^-]\\(->\\)[^>]" 'right-arrow)
(cons "\\(==\\)" 'identical)
(cons "\\(/=\\)" 'not-identical)
(cons "\\(()\\)" 'void)
(cons "\\<\\(sqrt\\)\\>" 'square-root)
(cons "\\(&&\\)" 'logical-and)
(cons "[^|]\\(||\\)[^|]" 'logical-or)
(cons "\\<\\(not\\)\\>" 'logical-neg)
(cons "\\(>\\)[^=]" 'greater-than)
(cons "\\(<\\)[^=]" 'less-than)
(cons "[^>]\\(>=\\)" 'greater-than-or-equal-to)
(cons "[^<]\\(<=\\)" 'less-than-or-equal-to)
(cons "\\(\\+\\+\\)" 'double-plus)
(cons "\\(''\\)" 'double-prime)
(cons "\\('\\)" 'prime)
(cons "\\(!!\\)" 'double-exclamation)
(cons "\\(\\.\\.\\)" 'horizontal-ellipsis))))
(add-hook 'haskell-mode-hook 'haskell-unicode)
(add-hook 'haskell-mode-hook 'pretty-greek)
;; Go mode
;; -------
(defun compact-go-imports ()
"remove extraneous blank lines from go imports"
(interactive)
(save-excursion
(goto-char (point-min))
(when (re-search-forward "^[[:space:]]*import[[:space:]]*[(]" nil t)
(end-of-line)
(let ((p1 (point)))
(when (re-search-forward "^[[:space:]]*[)][[:space:]]*$")
(let ((bound (point)))
(goto-char p1)
(while (re-search-forward "\n\\([[:space:]]*\n\\)\\{1,\\}" bound t)
(replace-match "\n" nil nil))))))))
(defun fix-go-break-continue ()
"ensure all break and continue have labels - to prevent break in case/select causing memory leaks"
(interactive)
(save-excursion
(goto-char (point-min))
(when (re-search-forward "^//[[:space:]]*Copyright[[:space:]]+(c)[[:space:]]+.*Bitmark[[:space:]]+Inc\\.[[:space:]]*$" nil t)
(goto-char (point-min))
(while (re-search-forward "^[[:space:]]*\\(break\\|continue\\)[[:space:]]*\\(//.*\\)?$" nil t)
(beginning-of-line)
(forward-word)
(insert " \"***")
(insert (upcase (match-string 1)))
(insert " MISSING LABEL***\"")
(end-of-line)))))
(defun go-tidy-up ()
"run all go tidying processes"
(let ((mn (mode-name-string)))
(when (string= mn "Go")
(compact-go-imports)
(fix-go-break-continue)
(setq gofmt-command "gofmt")
(setq gofmt-args (list "-s" "-r=(a) -> a"))
(gofmt-before-save)
(setq gofmt-command "goimports")
(setq gofmt-args (if (boundp 'goimports-local)
(list "-local" goimports-local)))
(gofmt-before-save))))
(global-set-key (kbd "S-<f7>") #'(lambda (arg)
"insert if err → return"
(interactive "p")
(mapc #'(lambda (s) (newline 1 t) (insert s))
'("if err != nil {" "return err" "}"))
(newline 1 t)))
(defun golang-unicode ()
(interactive)
(substitute-patterns-with-unicode
(list (cons "[^<]\\(<-\\)" 'left-arrow)
(cons "\\(->\\)[^>]" 'right-arrow)
;;(cons "[^<]\\(<=\\)" 'leftwards-double-arrow)
;;(cons "\\(=>\\)[^>]" 'rightwards-double-arrow)
;;(cons "\\(=:=\\)" 'identical)
;;(cons "\\(=/=\\)" 'not-identical)
(cons "\\(:=\\)" 'colon-equals)
(cons "[^=]\\(==\\)[^=]" 'identical)
(cons "[^=]\\(!=\\)" 'not-equal)
;;(cons "[^=/<>]\\(=\\)[^=/<>]" 'equivalent-to)
;;(cons "\\(\\[\\]\\)" 'void)
(cons "\\<\\(func\\)\\>" 'func)
(cons "\\<\\(nil\\)\\>" 'void)
(cons "\\<\\(sqrt\\)\\>" 'square-root)
(cons "\\(&&\\)" 'logical-and)
(cons "\\(||\\)" 'logical-or)
;;(cons "\\<\\(not\\)\\>" 'logical-neg)
(cons "\\(>\\)[^=]" 'greater-than)
(cons "\\(<\\)[^=]" 'less-than)
(cons "\\(>=\\)" 'greater-than-or-equal-to)
(cons "\\(<=\\)" 'less-than-or-equal-to)
)))
(when (require 'go-mode "go-mode" t)
(message "init.el: go mode")
;;(require 'auto-complete)
;;(require 'go-autocomplete)
;;(add-hook 'go-mode-hook #'go-eldoc-setup)
(add-hook 'go-mode-hook #'golang-unicode)
(add-hook 'before-save-hook #'go-tidy-up t))
;; Fundamental mode
;; ----------------
(setq flyspell-issue-welcome-flag nil) ;; workaround bug #619015 in ubuntu 10.10
(defun do-commit-header ()
"move to line end and turn on spell check"
(move-end-of-line nil)
(flyspell-mode))
(defun activate-flyspell ()
"activate commit mode"
(let ((fn (file-name-nondirectory buffer-file-name)))
(cond
((string-equal fn "COMMIT_EDITMSG")
(do-commit-header))
((string-equal fn "svn-commit.tmp")
(do-commit-header))
((string-equal fn "changelog")
(setq indent-tabs-mode nil
tab-width 2
left-margin 2)
(do-commit-header))
((string-match "[.]\\(go\\|cpp\\|h\\|c\\|hs\\)$" fn)
(flyspell-prog-mode)))))
(add-hook 'find-file-hook 'activate-flyspell)
;; Lua mode
;; --------
(when (require 'lua-mode "lua-mode" t)
(message "init.el: lua-mode available"))
;; SQL mode
;; --------
(when (require 'sqlup-mode "sqlup-mode" t)
;; (modify-syntax-entry ?" "\"" sql-mode-syntax-table)
(message "init.el: sqlup-mode available")
(add-hook 'sql-mode-hook 'sqlup-mode)
(add-hook 'sql-interactive-mode-hook 'sqlup-mode))
;; C mode
;; ------
(message "init.el: C styles")
;; A partial list of the better known styles:
;; “gnu” The default style for GNU projects
;; “k&r” What Kernighan and Ritchie, the authors of C used in their book
;; “bsd” What BSD developers use, aka “Allman style” after Eric Allman.
;; “stroustrup” What Stroustrup, the author of C++ used in his book
;; “linux” What the Linux developers use for kernel development
;; “python” What Python developers use for extension modules
;; “java” The default style for java-mode (see below)
;; “user” When you want to define your own style
(setq c-default-style '((java-mode . "java")
(awk-mode . "awk")
(other . "bsd")))
(defun my-tabify ()
"tabify the buffer for certain file types"
(interactive)
(let ((mn (mode-name-string)))
(cond
((or (string= (substring mn 0 (min 2 (length mn))) "C/")
(string= (substring mn 0 (min 4 (length mn))) "C++/"))
(message "tabifying buffer before save")
(save-excursion (clang-format-buffer)))
((or (string= (substring mn 0 (min 7 (length mn))) "Haskell")
(string= (substring mn 0 (min 10 (length mn))) "Emacs-Lisp"))
(message "untabifying buffer before save")
(save-excursion
(untabify (point-min) (point-max)))
)
((or (string= (substring mn 0 (min 3 (length mn))) "SQL")
(string= (substring mn 0 (min 3 (length mn))) "sql"))
(message "untabifying buffer before save")
(save-excursion
(untabify (point-min) (point-max))
;;;(sqlup-capitalize-keywords-in-region (point-min) (point-max))
)
)
)
))
(setq tabify-regexp "^\t* [ \t]+")
(add-hook 'before-save-hook 'my-tabify)
;; for clang-format
;; ----------------
(require 'clang-format)
;;(fset 'c-indent-region 'clang-format-region)
(defun my-clang-setup ()
"change tab to use clang-format"
(define-key c++-mode-map (kbd "<tab>") #'clang-format)
(define-key c-mode-map (kbd "<tab>") #'clang-format))
(add-hook 'c-mode-hook #'my-clang-setup)
(add-hook 'c++-mode-hook #'my-clang-setup)
;; exuberant ctags
;; ---------------
(message "init.el: ctags")
;;(require 'auto-complete-exuberant-ctags)
;;(ac-exuberant-ctags-setup)
(autoload 'turn-on-ctags-auto-update-mode "ctags-update" "turn on `ctags-auto-update-mode'." t)
;;(add-hook 'c-mode-common-hook 'turn-on-ctags-auto-update-mode)
;;(add-hook 'emacs-lisp-mode-hook 'turn-on-ctags-auto-update-mode)
;;(add-hook 'go-mode-hook 'turn-on-ctags-auto-update-mode)
(autoload 'ctags-update "ctags-update" "update TAGS using ctags" t)
(global-set-key "\C-cE" #'ctags-update)
;; awk mode
;; --------
(message "init.el: awk styles")
(add-hook 'awk-mode-hook
#'(lambda ()
(setq c-basic-offset 4)))
;; rust mode
;; ---------
(message "init.el: rust setup")
(add-hook 'after-save-hook 'my-show-rustfmt)
(defun my-show-rustfmt ()
"if rust format failed split window to show errors"
(let ((mn (mode-name-string)))
(when (string= mn "Rust")
(delete-other-windows)
(when (get-buffer "*rustfmt*")
(set-window-buffer (split-window-sensibly) "*rustfmt*")))))
(defun rust-unicode ()
(interactive)
(substitute-patterns-with-unicode
(list (cons "[^<]\\(<-\\)" 'left-arrow)
(cons "\\(->\\)[^>]" 'right-arrow)
(cons "\\(=>\\)[^>]" 'rightwards-double-arrow)
(cons "\\(==\\)" 'identical)
(cons "\\(/=\\)" 'not-identical)
(cons "[^a-zA-Z0-9_>:]\\(()\\)" 'void)
(cons "\\<\\(sqrt\\)\\>" 'square-root)
(cons "\\(&&\\)" 'logical-and)
(cons "[^|]\\(||\\)[^|]" 'logical-or)
(cons "\\<\\(not\\)\\>" 'logical-neg)
(cons "\\(>\\)[^=]" 'greater-than)
(cons "\\(<\\)[^=]" 'less-than)
(cons "[^>]\\(>=\\)" 'greater-than-or-equal-to)
(cons "[^<]\\(<=\\)" 'less-than-or-equal-to)
(cons "\\(\\+\\+\\)" 'double-plus)
(cons "\\(''\\)" 'double-prime)
(cons "\\('\\)" 'prime)
(cons "\\(!!\\)" 'double-exclamation)
(cons "\\(\\.\\.\\)" 'horizontal-ellipsis))))
(add-hook 'rust-mode-hook 'rust-unicode)
(add-hook 'rust-mode-hook 'pretty-greek)
;; miscellaneous items
;; -------------------
(message "init.el: miscellaneous")
(line-number-mode 1)
(setq display-time-24hr-format 'T)
(display-time)
(setq vc-initial-comment 'T)
(mouse-avoidance-mode 'animate)
(put 'overwrite-mode 'disabled t) ; disable nasty overstrike mode
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)
(setq abbrev-file-name ; tell emacs where to read abbrev
"~/.emacs.d/abbrev_defs") ; definitions from...
(setq save-abbrevs t) ; save abbrevs when files are saved
(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checking" t)
(autoload 'folding-mode "folding" "Folding mode" t)
(autoload 'turn-off-folding-mode "folding" "Folding mode" t)
(autoload 'turn-on-folding-mode "folding" "Folding mode" t)
;; Tramp
;; -----
(message "init.el: Tramp set up")
(require 'tramp)
(setq tramp-debug-buffer t)
(setq tramp-verbose 10)
(setq tramp-default-method "ssh")
;;; preserve initial login path for tramp
(add-to-list 'tramp-remote-path 'tramp-own-remote-path)
(add-to-list 'tramp-remote-path (expand-file-name "~/go/bin") t)
;; Org mode
;; --------
(message "init.el: org-mode available")
(defun my-org-to-html (arg)
"convert the current markdown buffer to a pdf file"
(interactive "P")
(org-export-dispatch (universal-argument)))
;; Markdown mode
;; -------------
(when (require 'markdown-mode "markdown-mode" t)
(message "init.el: markdown-mode available"))
(defun my-markdown-to-pdf (arg)
"convert the current markdown buffer to a pdf file"
(interactive "p")
(let ((pdf-viewer "evince --presentation")
(markdown-command (concat
"pandoc --from=markdown "
"--to=latex --pdf-engine=xelatex "
"--number-sections "
"--standalone --self-contained"))
(output-file-name (concat (file-name-base) ".pdf"))
(pandoc-errors (get-buffer-create "*Pandoc Errors*")))
(message "creating PDF in: %s" output-file-name)
(with-current-buffer pandoc-errors (erase-buffer))
(save-window-excursion
(let ((begin-region)
(end-region))
(if (markdown-use-region-p)
(setq begin-region (region-beginning)
end-region (region-end))
(setq begin-region (point-min)
end-region (point-max)))
;; Pass region to `markdown-command' via stdin
(call-process-region begin-region end-region
shell-file-name nil (list pandoc-errors t) nil
shell-command-switch