-
Notifications
You must be signed in to change notification settings - Fork 1
/
emacs.el
1533 lines (1316 loc) · 49 KB
/
emacs.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
;; -*- emacs-lisp -*-
;;; Note: this file should be loaded by bootstrap-init.el.
(require 'cl-lib)
(defmacro when-fboundp-call (f &rest args)
"If F is bound, calls it with ARGS."
`(when (fboundp (function ,f))
(funcall (function ,f) ,@args)))
(defmacro unless-boundp-setq (var val)
"If VAR is not bound, sets it to VAL."
`(unless (boundp (quote ,var)) (setq ,var ,val)))
(defun blankp (val)
"Return nil if VAL is `nil', the empty list, or an empty or
whitespace-only string."
(cond ((not val) t)
((and (stringp val) (string-blank-p val)) t)
(t nil)))
(defun val-or-default (val default)
"Return VAL if it is not `blankp', else return DEFAULT."
(if (not (blankp val)) val default))
(setq bookmark-set-fringe-mark nil)
;;; Version-specific configuration
(if (version< emacs-version "24")
(let ((f (expand-file-name "~/.emacs.d/elpa/package.el")))
(when (file-exists-p f)
(load f)))
(require 'package))
;; Close a security vulnerability
;; https://lists.gnu.org/archive/html/emacs-devel/2017-09/msg00211.html
(when (version< emacs-version "25.3")
(eval-after-load "enriched"
'(defun enriched-decode-display-prop (start end &optional param)
(list start end))))
(when (fboundp #'package-initialize)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
(when (< emacs-major-version 27)
(setq package-enable-at-startup nil ; prevent initializing twice
package--init-file-ensured t)) ; avoid check for being in init.el
;; https://emacs.stackexchange.com/questions/60560/error-retrieving-https-elpa-gnu-org-packages-archive-contents-error-http-400
;; says that this should be fixed in v27, but I need this for at least
;; 27.2.2.
;; (when (and (version< emacs-version "26.3") (>= libgnutls-version 30603))
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
;; )
(package-initialize))
;;
;; PIM
;;
(defvar *my-pim-dir* "~/pim/")
(setq diary-file (concat *my-pim-dir* "diary"))
(when-fboundp-call appt-activate 1) ; appointment notification
(defun address (str)
"Find STR in my address book file. Looks first for STR as the
beginning of a name, then at the beginning of any line. If not
found, looks for the first occurrence of STR anywhere.
This function is also used by an Org Mode custom link."
(interactive "sSearch string: ")
(let ((search-str (replace-regexp-in-string "+" " " str))
(current-buf (current-buffer)))
(find-file (concat *my-pim-dir* "orgs/address_book.org"))
(goto-char (point-min))
(or
(search-forward (concat "\n\n" search-str) nil t)
(search-forward (concat "\n" search-str) nil t)
(search-forward search-str nil t)
(error (concat "\"" search-str "\" not found")))))
;;; Signatures
;;;
;;; This section must come before my eshell initialization.
(defvar *my-signature-file* (concat *my-pim-dir* "signatures"))
(defun random-signature ()
"Returns a random signature from my *my-signature-file* file."
(interactive)
(when (file-exists-p *my-signature-file*)
(let ((sigs (with-temp-buffer
(insert-file-contents *my-signature-file*)
(split-string (buffer-string) "\n\n" t))))
(nth (random (length sigs)) sigs))))
(defun my-start-shell ()
(interactive)
(shell)
(set-process-query-on-exit-flag (get-process "shell") nil))
;; Possible values include #'shell, #'eshell, #'switch-to-terminal, and
;; #'my-start-shell
(defvar my-shell #'eshell
"The shell to use inside Emacs; examples include 'shell or 'eshell.")
(defvar my-alternate-shell #'shell
"Alternate shell. Bound to alternate key.")
(when (equal default-directory "/")
(setq default-directory "~/"))
;; Add progmodes subdir to the end of load-path.
(add-to-list 'load-path (concat *my-emacs-lib-dir* "progmodes/") t)
;;; Silent bell: flash mode line instead. Do nothing when caused by certain
;;; functions.
(defvar *do-not-ring-bell-funcs*
'(isearch-abort
abort-recursive-edit
exit-minibuffer
mwheel-scroll
down up
next-line previous-line
backward-char forward-char))
(defun mode-line-visible-bell ()
"This function temporarily inverts the mode line. It does not
do so when `this-command' is one of the commands in
`*do-not-ring-bell-funcs*'."
(unless (memq this-command *do-not-ring-bell-funcs*)
(invert-face 'mode-line)
(run-with-timer 0.1 nil 'invert-face 'mode-line)))
(setq visible-bell nil
ring-bell-function #'mode-line-visible-bell)
;;; mood-line
(when (fboundp #'mood-line-mode)
(mood-line-mode))
;;; 2048-game
(add-hook '2048-mode-hook
(lambda ()
(define-key 2048-mode-map "j" '2048-down)
(define-key 2048-mode-map "k" '2048-up)
(define-key 2048-mode-map "h" '2048-left)
(define-key 2048-mode-map "l" '2048-right)))
;;; ag
(when (fboundp #'ag)
(setq ag-arguments (list "--smart-case" "--nocolor" "--nogroup")))
;;; Clojure
;; ClojureScript
(add-to-list 'auto-mode-alist '("\\.boot$" . clojure-mode))
(eval-after-load "clojure-mode"
(load "my-clojure-mode"))
;;; Common Lisp
(add-hook 'lisp-mode-hook
(lambda ()
(define-key lisp-mode-map "\r" #'newline-and-indent)
(define-key lisp-mode-map "\C-cd" #'debug-comment)))
(add-hook 'clojure-mode-hook
(lambda ()
(when-fboundp-call inf-clojure-minor-mode)
(define-key clojure-mode-map "\r" 'newline-and-indent)
(define-key clojure-mode-map "\C-cd" 'debug-comment)
(define-key clojure-mode-map "\C-ci" 'in-ns-to-inferior-lisp)
(define-key clojure-mode-map "\C-cn" 'ns-to-inferior-lisp)))
;; See also inf-clojure mode
(setq inferior-lisp-program "sbcl")
;; (require 'slime)
;; (slime-setup)
(defun -set-lisp-and-run (command)
(setq inferior-lisp-program command)
(if (fboundp #'slime) (slime) (inferior-lisp command)))
(defun sbcl ()
(interactive)
(-set-lisp-and-run "sbcl"))
(defun clisp ()
(interactive)
(-set-lisp-and-run "clisp"))
(defun clojure ()
(interactive)
(-set-lisp-and-run "lein repl"))
;;; Scheme
(add-hook 'scheme-mode-hook
(lambda ()
(define-key scheme-mode-map "\r" #'newline-and-indent)
(define-key scheme-mode-map "\C-cd" #'debug-comment)))
;;; Emacs Lisp
(add-hook 'emacs-lisp-mode-hook
(lambda ()
(define-key emacs-lisp-mode-map "\C-cd" #'debug-comment)
(define-key emacs-lisp-mode-map "\r" #'newline-and-indent)))
;;; Compilation
(require 'compile)
(setq compilation-error-regexp-alist
(cons
;; Maven 2 error messages are of the form file:[line,column]
'("^\\(/[^:]+\\):\\[\\([0-9]+\\),\\([0-9]+\\)\\]" 1 2 3)
(cons
;; Scala error messages
'("\\(\\([a-zA-Z0-9]*/\\)*\\([a-zA-Z0-9]*\\.scala\\)\\):\\([0-9]*\\).*" 1 2)
compilation-error-regexp-alist)))
;;; ido
(when (fboundp #'ido-mode)
(ido-mode t)
(setq ido-enable-flex-matching t))
;;; fix-ido
(when (boundp #'flx-ido-mode)
(require 'flx-ido)
(ido-mode 1)
(ido-everywhere 1)
(flx-ido-mode 1)
;; disable ido faces to see flx highlights.
(setq ido-enable-flex-matching t)
(setq ido-use-faces nil))
;;; CoffeeScript
(autoload #'coffee-mode "coffee-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.cjsx$" . coffee-mode))
(add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
(add-to-list 'auto-mode-alist '("Cakefile" . coffee-mode))
(defun compile-coffee-buffer ()
(interactive)
(shell-command (concat "coffee"
" -o " (shell-quote-argument (file-name-directory (buffer-file-name)))
" -c " (shell-quote-argument (buffer-file-name)))))
(add-hook 'coffee-mode-hook
(lambda ()
(setq coffee-js-mode #'javascript-mode)
(define-key coffee-mode-map "\C-cx" #'executable-interpret)
(define-key coffee-mode-map "\C-ck" #'compile-coffee-buffer)
(set (make-local-variable 'tab-width) 2)))
;;; dash
(use-package dash
:config
(dash-enable-font-lock))
;;; Magit
(use-package magit
:ensure t)
;;; dumb-jump
(when-fboundp-call dumb-jump-mode)
;;; Elixir
(add-hook 'elixir-mode-hook
(lambda ()
(define-key elixir-mode-map "\C-cd" #'debug-comment)
(define-key elixir-mode-map "\C-cx" 'executable-interpret)
(add-hook 'before-save-hook #'elixir-format nil t)))
;; (when-fboundp-call alchemist-mode)))
;;; Alchemist
(defun -set-dir-var (env_name sym)
"If environment variable ENV_NAME is defined and points to a
directory that exists, set SYM to that directory. Else, SYM is
unchanged."
(let ((dir_env (getenv env_name)))
(when dir_env
(let ((dir (file-name-as-directory dir_env)))
(when (file-exists-p dir)
(set sym dir))))))
(add-hook 'alchemist-mode-hook
(lambda ()
(-set-dir-var "ELIXIR_HOME" alchemist-goto-elixir-source-dir)
(-set-dir-var "ERLANG_HOME" alchemist-goto-erlang-source-dir)
(define-key alchemist-mode-map "\C-c\C-z"
#'alchemist-iex-project-run)
(add-hook 'before-save-hook #'elixir-format nil t)))
;;; EMMS
(when (fboundp #'emms-all)
(defun emms-init ()
(interactive)
(emms-all)
(emms-default-players)
(let ((dbox (getenv "dbox")))
(when dbox
(setq emms-source-file-default-directory
(concat (file-name-directory dbox) "Music/music/"))))
(global-set-key [\C-f7] 'emms-previous)
(global-set-key [\C-f8] 'emms-pause) ; toggles between pause and resume
(global-set-key [\C-f9] 'emms-next)))
;;;
;;; fzf
;;;
(when (fboundp #'fzf)
(let ((local-fzf-executable "~/.fzf/bin/fzf"))
(when (file-exists-p local-fzf-executable)
(setq fzf/executable local-fzf-executable))))
;;; Gnus
(setq gnus-site-init-file (concat *my-emacs-lib-dir* "gnus-init.el"))
(if (zerop (shell-command "which gnutls-cli >/dev/null 2>&1"))
(setq starttls-use-gnutls t
starttls-gnutls-program "gnutls-cli"
starttls-extra-arguments nil))
;;; Go
(autoload #'go-mode "go-mode" t nil)
(add-to-list 'auto-mode-alist '("\\.go$" . go-mode))
(when (fboundp #'gofmt-before-save)
(add-hook 'before-save-hook #'gofmt-before-save))
(add-hook 'go-mode-hook
(lambda ()
(tab-four)
(setq indent-tabs-mode t)))
;;; Haskell
(autoload #'haskell-mode "haskell-mode" "Haskell mode" t nil)
(add-to-list 'auto-mode-alist '("\\.hs$" . haskell-mode))
;; Hexl mode
(defvar hexl-program (concat *my-emacs-lib-dir* "hexlify.rb"))
;;; http-twiddle
(autoload #'http-twiddle-mode "http-twiddle" "HTTP twiddle mode" t nil)
(add-to-list 'auto-mode-alist '("\\.http-twiddle$" . http-twiddle-mode))
;;; Java
(eval-after-load "java"
(load "my-java-mode"))
;;; JavaScript
(autoload #'javascript-mode "javascript" nil t)
(add-to-list 'auto-mode-alist '("\\.[agj]s$" . javascript-mode))
(add-to-list 'auto-mode-alist '("\\.jsx$" . javascript-mode))
(add-hook 'js-mode-hook
(lambda ()
(setq js-indent-level 2 ; need both?????
javascript-indent-level 2)))
;;; JSON
(add-to-list 'auto-mode-alist '("\\.kmst$" . js-json-mode)) ; KeyMaster JSON files
(setq js2-basic-offset 2)
;;(setq js2-use-font-lock-faces t)
;;; TypeScript
(when (fboundp #'typescript-mode)
(add-to-list 'auto-mode-alist '("\\.tsx$" . typescript-mode)))
;;; Markdown
(ignore-errors ; some systems don't have markdown-mode
(require 'markdown-mode) ; so my themes will bind correctly
(add-to-list 'auto-mode-alist '("\\.\\(md\\|markdown\\|mdown\\)$" . markdown-mode))
(add-hook 'markdown-mode-hook
(lambda ()
(set-face-attribute 'markdown-header-face-1 nil :height 1.2 :bold t))))
;;; Objective C
(add-hook 'objc-mode-hook
(lambda ()
(if window-system (font-lock-mode 1))
(setq c-basic-offset 4)))
;;; Org Mode
(load "my-org-mode")
;;; perl-mode
(autoload #'perl-mode "perl-mode" "Perl mode" t nil)
(add-hook 'perl-mode-hook
(lambda ()
(define-key perl-mode-map "\r" #'newline-and-indent)
(define-key perl-mode-map "\M-\C-h" #'backward-kill-word)
(define-key perl-mode-map "\C-cd" #'debug-comment)
(setq c-basic-offset 2
c-tab-always-indent nil)))
;;; projectile
;;; see also keys.el
(when (fboundp #'projectile-global-mode)
(projectile-mode)
(define-key projectile-mode-map (kbd "s-p") 'projectile-command-map)
(setq projectile-enable-caching t
projectile-mode-line-prefix " prj")
(projectile-update-project-type 'rails-rspec :src-dir "app/"))
;;; editorconfig
(when (fboundp #'editorconfig-mode)
(editorconfig-mode 1)
(setq editorconfig-mode-lighter " edconf"))
;;; python-mode
(load "my-python")
;;; ruby-mode
;; Use "M-x run-ruby" to start inf-ruby.
(autoload #'ruby-mode "ruby-mode" "Ruby mode" t nil)
(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\.r\\(b\\(w\\|x\\)?\\|html?\\|js\\)$" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\([Rr]ake\\|[Cc]ap\\|[Gg]em\\)file$" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\.rake$" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\.gem\\(spec\\)?$" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\.duby$" . ruby-mode))
(eval-after-load "ruby-mode"
(load "my-ruby-mode"))
(eval-after-load "inf-ruby"
(load "my-ruby-mode"))
;;; Rust
(setq rust-format-on-save t
rust-rustfmt-switches '("--edition" "2021"))
;;; HAML and SASS
;; Found {haml,sass}-mode.el files in the directory path-to-haml-gem/extra/.
(autoload #'haml-mode "haml-mode" "haml mode")
(autoload #'sass-mode "sass-mode" "sass mode")
(add-to-list 'auto-mode-alist '("\\.haml$" . haml-mode))
(add-to-list 'auto-mode-alist '("\\.s\\(a\\|c\\)?ss$" . sass-mode))
;;; Smex
(when (fboundp #'smex-initialize)
(smex-initialize))
;;; SQL
(add-to-list 'auto-mode-alist '("\\.mysql$" . sql-mode))
(eval-after-load "sql-mode"
(load "my-sql-mode"))
;; My own tools for keeping a daily status file up to date.
(autoload #'status "status" nil t)
;;; Textile
(autoload #'textile-mode "textile-mode" "textile mode")
(add-to-list 'auto-mode-alist '("\\.textile$" . textile-mode))
(add-hook 'textile-mode-hook
(lambda ()
(auto-fill-mode 0)
(visual-line-mode 1)
(set-face-attribute 'textile-h1-face nil :foreground "blue" :height 1.2 :bold t)
(set-face-attribute 'textile-h2-face nil :foreground "brown" :height 1.0)
(set-face-attribute 'textile-h3-face nil :foreground "darkgreen" :height 1.0)
(set-face-attribute 'textile-h4-face nil :foreground "black" :height 1.0)
(set-face-attribute 'textile-h5-face nil :foreground "black" :height 1.0)
(set-face-attribute 'textile-h6-face nil :foreground "black" :height 1.0)))
;;; Uniquify
;; http://trey-jackson.blogspot.com/2008/01/emacs-tip-11-uniquify.html
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse
uniquify-separator "/"
uniquify-after-kill-buffer-p t ; rename after killing uniquified
uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
;;; YASnippet
(when (fboundp #'yas-global-mode)
(yas-global-mode 1)
(let ((snip-dir (concat *my-emacs-lib-dir* "snippets")))
(setq yas-snippet-dirs (list snip-dir))
(yas-load-directory snip-dir t)))
;;; YAML
(autoload #'yaml-mode "yaml-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.ya?ml$" . yaml-mode))
;;; DOS batch, ini files and much more
(require 'generic-x)
(add-to-list 'auto-mode-alist
'("\\.properties$" . java-properties-generic-mode))
;; Ubuntu stuff
;(menu-bar-enable-clipboard)
(setq x-select-enable-clipboard t
mouse-drag-copy-region t)
(when-fboundp-call set-scroll-bar-mode 'right)
(when-fboundp-call mouse-wheel-mode 1)
(when (>= emacs-major-version 23)
;; Time to try transient mark mode for a while
;; (transient-mark-mode -1)
(setq confirm-nonexistent-file-or-buffer nil))
(turn-on-auto-fill)
(auto-fill-mode 1)
(show-paren-mode 1)
(setq save-flag 1 ; see bootstrap-ini for loc of file
sentence-end-double-space nil
column-number-mode nil
make-backup-files nil ; don't make backup files
delete-auto-save-files t ; no "#" files after a save
auto-save-list-file-prefix nil ; don't record sessions
inhibit-startup-screen t ; kill the startup message
initial-scratch-message nil ; used by Emacs 23 and above
initial-major-mode 'org-mode
inhibit-startup-echo-area-message (getenv "USER")
Man-notify 'aggressive ; when man found, jump there *immed*
dabbrev-case-replace nil ; preserve case when expanding
mode-require-final-newline nil ; do not force newlines
ns-pop-up-frames nil ; do not create new frames on Mac
auto-revert-verbose nil ; no message on each auto-revert update
isearch-lax-whitespace nil
version-control 'never ; when to make backup files
vc-handled-backends '() ; disable VC minor mode
frame-title-format '((:eval (if (buffer-file-name)
(abbreviate-file-name (buffer-file-name))
"%b")))
custom-theme-directory (concat *my-emacs-lib-dir* "themes/")
eww-search-prefix "https://www.google.com.com/?q=")
(setq-default fill-column 76
indent-tabs-mode nil)
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'narrow-to-page 'disabled nil)
;; accept simple 'y'/space, 'n'/delete
(if (boundp 'use-short-answers)
(setq use-short-answers t)
(fset #'yes-or-no-p #'y-or-n-p))
(unless (fboundp #'string-match-p) (defalias #'string-match-p #'string-match))
(defun display-startup-echo-area-message ()
(message ""))
(when-fboundp-call tool-bar-mode -1)
(when-fboundp-call tooltip-mode -1)
(when-fboundp-call menu-bar-mode nil)
(global-font-lock-mode t) ; always turn on, where available
(defun unix-file ()
"Change the current buffer to UTF-8 with Unix line-ends."
(interactive)
(set-buffer-file-coding-system 'utf-8-unix t))
(defun dos-file ()
"Change the current buffer to UTF-8 with DOS line-ends."
(interactive)
(set-buffer-file-coding-system 'utf-8-dos t))
(defun snake-case-to-camel-case (str)
"Converts STR, which is a word using snake_case, to CamelCase."
(interactive "S")
(apply #'concat (mapcar #'capitalize (split-string str "_"))))
(defun camel-case-to-snake-case (str)
"Converts STR, which is a word using CamelCase, to snake_case."
(interactive "S")
(let* ((case-fold-search nil)
(snaked
(downcase
(replace-regexp-in-string "[A-Z]"
(lambda (s) (concat "_" (downcase s)))
str))))
(if (and (> (length snaked) 1)
(equal "_" (substring snaked 0 1)))
(substring snaked 1)
snaked)))
;; I prefer zap-upto-char most of the time
(defun zap-upto-char (arg char)
"Kill up to but not including ARGth occurrence of CHAR.
Case is ignored if `case-fold-search' is non-nil in the current buffer.
Goes backward if ARG is negative; error if CHAR not found."
(interactive (list (prefix-numeric-value current-prefix-arg)
(read-char "Zap up to char: " t)))
(zap-to-char arg char)
(insert-char char)
(backward-char))
(defun string-to-clipboard (str)
"Saves string `str' to the kill ring and GUI clipboard."
(interactive)
(with-temp-buffer
(insert str)
(clipboard-kill-ring-save (point-min) (point-max)))
(message str))
(defun generate-random-password (password-length)
"Generate a random password PASSWORD-LENGTH characters long.
Characters are selected from upper- and lower-case letters,
numbers, and punctuation."
(let* ((chars "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!?@#$%^&*-_=+/.,")
(chars-len (length chars)))
(mapconcat (lambda (dummy)
(let ((idx (random chars-len)))
(substring chars idx (1+ idx))))
(number-sequence 0 (1- password-length))
"")))
(defun insert-random-password (arg)
"Generate a random password ARG characters long (16 by default) and
insert it at point. See `generate-random-password`."
(interactive "p")
(insert (generate-random-password arg)))
;;
;; Window movement
;;
;; Uses ace-window
(defun nth-other-window (n)
(interactive)
(let ((wnd-list (aw-window-list)))
(cond
((<= (length wnd-list) 2)
(other-window 1))
(t
(select-window
(cdr (nth n
(mapcar (lambda (wnd) (cons (aw-offset wnd) wnd))
wnd-list))))))))
;;;
;;; Project-level navigation and search.
;;;
;;; Must be loaded after things like fzf are loaded.
;;;
(load "proj")
;;
;; For flipping back and forth between files and headers, or Java/Ruby files
;; and Java/Ruby test files. See also rails-find-other-file.
;;
(defun my-ff-find-other-file (&optional in-other-window ignore-include)
"Find other Java or Ruby file or, if not a Java or Ruby file,
call `ff-find-other-file`."
(interactive "P")
(let* ((fname (buffer-file-name))
(ext (file-name-extension fname)))
(cond ((or (equal "java" ext) (equal "scala" ext))
(find-other-java-file fname ext))
((equal "rb" ext)
(find-other-ruby-file fname))
(t
(ff-find-other-file in-other-window ignore-include)))))
(defun find-other-java-file (&optional file-name ext)
"Visits `Foo.java' when given `FooTest.java' and vice versa.
Default file-name is current buffer's name."
(interactive)
(let* ((fname (file-name-nondirectory (or file-name (buffer-file-name))))
(non-test-from-end (- -5 (length ext)))
(test-from-end (- -1 (length ext)))
(target (if (equal (concat "Test." ext) (substring fname non-test-from-end))
(concat (substring fname 0 non-test-from-end) (concat "." ext))
(concat (substring fname 0 test-from-end) (concat "Test." ext)))))
(ef target default-directory)))
(defun find-other-ruby-file (&optional file-name)
"Visits `foo.rb' when given `foo_(test|spec).rb'. Visits `foo_test.rb' when
given `foo.rb'. Default file-name is current buffer's name."
(interactive)
(let* ((fname (file-name-nondirectory (or file-name (buffer-file-name))))
(target (if (and (> (length fname) 8)
(or (equal "_test.rb" (substring fname -8))
(equal "_spec.rb" (substring fname -8))))
(concat (substring fname 0 -8) ".rb")
(concat (substring fname 0 -3) "_test.rb"))))
(ef target default-directory)))
(setq ff-other-file-alist
'(("\\.cpp$" . ((".hpp" ".hh" ".h")))
("\\.cp$" . ((".hp" ".hh" ".h")))
("\\.hpp$" . ((".cpp" ".cp")))
("\\.icc$" . ((".hpp")))
("\\.m$" . ((".h")))
("\\.cc$" . ((".hh" ".h")))
("\\.hh$" . ((".cc" ".C" ".CC" ".cxx" ".cpp" ".cp" ".m")))
("\\.c$" . ((".h")))
("\\.h$" . ((".c" ".cc" ".C" ".CC" ".cxx" ".cpp" ".cp" ".m")))
("\\.C$" . ((".H" ".hh" ".h")))
("\\.H$" . ((".C" ".CC")))
("\\.CC$" . ((".HH" ".H" ".hh" ".h")))
("\\.HH$" . ((".CC" ".C")))
("\\.cxx$" . ((".hh" ".h" ".hxx")))
))
;;;
;;; Misc.
;;;
;; When saving files, set execute permission if #! is in first line.
(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)
;;
;; Set tab stops to eight chars, not four
;;
(defun eight-tab-stops ()
(interactive)
(setq tab-stop-list
'(8 16 24 32 40 48 56 64 72 80)))
;;
;; Set tab stops to four chars, not eight
;;
(defun four-tab-stops ()
(interactive)
(setq tab-stop-list
'(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80)))
(defun tab-two () (interactive) (setq tab-width 2))
(defun tab-four () (interactive) (setq tab-width 4))
(defun tab-eight () (interactive) (setq tab-width 8))
(defalias #'t2 #'tab-two)
(defalias #'t4 #'tab-four)
(defalias #'t8 #'tab-eight)
(defun capitalize-next-char ()
"Capitalize next character and move point right 1 character."
(interactive "*")
(save-excursion
(forward-char)
(insert " ")
(backward-char 2)
(capitalize-word 1)
(delete-char 1)))
(defun capitalize-first-char-of-string (str)
"Capitalize first character of STR."
(interactive "s")
(concat (capitalize (substring str 0 1)) (substring str 1)))
(defun downcase-first-char-of-string (str)
"Make first character of STR lower-case."
(interactive "s")
(concat (downcase (substring str 0 1)) (substring str 1)))
(defun pluralize (str)
"Pluralize STR, which is assumed to be a single word. This is a
simple algorithm that may grow over time if needed."
(interactive "s")
(let ((len (length str)))
(cond ((equal "y" (substring str (- len 1))) (concat (substring str 0 (- len 1)) "ies"))
((equal "us" (substring str (- len 2))) (concat (substring str 0 (- len 2)) "i"))
(t (concat str "s")))))
(defun singularize (str)
"Singularize STR, which is assumed to be a single word. This is
a simple algorithm that may grow over time if needed."
(interactive "s")
(let ((len (length str)))
(cond ((equal "ies" (substring str (- len 3))) (concat (substring str 0 (- len 3)) "y"))
((equal "i" (substring str (- len 1))) (concat (substring str 0 (- len 1)) "us"))
((equal "s" (substring str (- len 1))) (substring str 0 (- len 1)))
(t str))))
(defun debug-comment ()
"Add a DEBUG comment to the current line."
(interactive "*")
(save-excursion
(comment-dwim nil)
(backward-char)
(let ((is-space (looking-at " ")))
(forward-char)
(unless is-space (insert " ")))
(insert "DEBUG")))
;;
;; Browse away!
;;
(defun my-goto-calendar-date (str)
(interactive "sDate: ")
(let ((date-list (cdddr (parse-time-string str))))
(calendar)
(calendar-goto-date (list (cadr date-list)
(car date-list)
(caddr date-list)))))
;(setq browse-url-generic-program "mozilla-firefox")
(setq browse-url-generic-program "open")
; Java class Javadoc lookup
(defvar *my-javadoc-url*
"http://download.oracle.com/javase/1.5.0/docs/api/"
"The start of the URL to use to open Java API docs. Override this when
you have a local copy, for example.")
(defun my-javadoc-open (&optional klass)
(interactive)
(let* ((klass-str (if (null klass) (read-string "Full class name: ") klass))
(klass-path (replace-regexp-in-string "\\." "/" klass-str))
(url (concat *my-javadoc-url* klass-path ".html")))
(browse-url-generic url)))
;;
;; Tramp
;;
; (setq tramp-default-method "scp")
(defun debug-tramp ()
(interactive)
(setq tramp-debug-buffer t
tramp-verbose 10))
;;
;; ssh-ing
;;
(defun ssh (host)
"Open a shell on a buffer named *HOST* and run ssh HOST. Note that HOST can
be of the form USER@HOST."
(interactive "sHost: ")
(shell (concat "*" host "*"))
(insert (concat "ssh " host))
(comint-send-input))
;;
;; Text-mode
;;
(add-hook 'text-mode-hook
(lambda ()
(auto-fill-mode 1)
(four-tab-stops)))
;;
;; For both C and C++ mode
;;
(add-hook 'c-mode-common-hook
(lambda ()
(setq c-basic-offset 2
c-tab-always-indent nil
c-recognize-knr-p nil)
(local-set-key "\r" #'newline-and-indent)
(autoload #'fh-open-header-file-other-window "find-header"
"Locate header file and load it into other window" t)
(autoload #'fh-open-header-file-other-frame "find-header"
"Locate header file and load it into other frame" t)))
(defun cbo4 ()
(interactive)
(setq c-basic-offset 4
ruby-indent-level 4))
(defun cbo2 ()
(interactive)
(setq c-basic-offset 2
ruby-indent-level 2))
;;
;; C++-mode
;;
(add-to-list 'auto-mode-alist '("\\.[ch]pp?$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.h?$" . c++-mode)) ; mostly C++, now a days
(add-to-list 'auto-mode-alist '("\\.pde$" . c++-mode)) ; Arduino
(add-hook 'c++-mode-hook
(lambda ()
(c-set-style "stroustrup")
(setq c-basic-offset 2)))
;;
;; Erlang-mode
;;
(when (and (boundp '*my-erlang-emacs-tools-dir*)
(file-exists-p *my-erlang-emacs-tools-dir*))
(add-to-list 'load-path *my-erlang-emacs-tools-dir* t))
(autoload #'erlang-mode "erlang" "Erlang mode" t nil)
(add-to-list 'auto-mode-alist '("\\.[he]rl$" . erlang-mode))
(add-to-list 'auto-mode-alist '("\\.yaws$" . erlang-mode))
(add-hook 'erlang-mode-hook
(lambda ()
(setq indent-tabs-mode nil)
(comment-set-column 32)))
;;
;; sh-mode
;;
(add-to-list 'auto-mode-alist '("/\\.env[^/]*" . sh-mode))
;;
;; Eshell-mode
;; must come after defining ef
;;
(when (or (eq my-shell #'eshell)
(eq my-alternate-shell #'eshell))
(load "eshell")
(load "my-eshell"))
;;
;; Shell-mode
;;
;; Don't echo passwords
(add-hook 'comint-output-filter-functions
#'comint-watch-for-password-prompt)
(setq shell-completion-execonly nil) ; Any file is completion candidate
(add-hook 'shell-mode-hook
(lambda ()
(auto-fill-mode -1)
(setq comint-scroll-show-maximum-output nil)))
;;
;; LaTeX-mode
;;
(eval-after-load "latex-mode"
(progn
(setq tex-dvi-view-command "latex-view '*'" ; in my ~/bin dir
;; tex-dvi-view-command "xdvi -expert -hush"
tex-dvi-print-command "dvips -f * | lpr")
(defun my-tex-to-text ()
(interactive)
(save-buffer)
(shell-command (concat
"/usr/bin/dvi2tty "
(file-name-sans-extension (buffer-file-name))
" >"
(file-name-sans-extension (buffer-file-name))
".txt"))
)
(add-hook 'latex-mode-hook
(lambda ()
(define-key latex-mode-map "\C-c\C-p" #'tex-print)
;; (define-key latex-mode-map "\C-c\C-t" #'my-tex-to-text)
(define-key latex-mode-map "\C-c\C-i" #'find-mine)
(define-key latex-mode-map "\C-c\C-s" #'my-tex-slide-dvi-view)))))
;;
;; HTML-mode and SGML-mode
;;
(eval-after-load "html-mode"
(progn
(defun my-html-insert-comment ()
(interactive "*")
(insert "<!-- -->")
(backward-char 4))
(defun unescape-html ()
(interactive "*")
(let ((repl-alist '(("<" . "<") (">" . ">") (""" . "\"")
("'" . "'") ("&" . "&"))))
(save-excursion
(mapcar (lambda (alist)
(goto-char (point-min))
(while (re-search-forward (car alist) nil t)
(replace-match (cdr alist) nil nil)))
repl-alist))))))
(mapcar (lambda (ext)
(add-to-list 'auto-mode-alist
(cons (concat "\\." ext "$") #'sgml-mode)))
; jwcs, application, page: Tapestry
; ftl: FreeMarker
'("xsd" "wsd[ld]" "jwcs" "application" "page" "ftl"))
(eval-after-load "sgml-mode"
(progn
(add-hook 'sgml-mode-hook
(lambda ()
(require 'tex-mode)
(auto-fill-mode 1)))
(add-hook 'html-mode-hook
(lambda ()
(auto-fill-mode 1)
(define-key html-mode-map "\C-c;" #'my-html-insert-comment)
(define-key html-mode-map "\C-cp" #'php-mode)))))
;;
;; CSS-mode
;;
(autoload #'css-mode "css-mode" "CSS mode" t nil)
(add-to-list 'auto-mode-alist '("\\.css$" . css-mode))
(add-to-list 'auto-mode-alist '("\\.less$" . css-mode))
;;
;; Crystal-mode
;;
(defvar *prevent-crystal-formatting* nil
"This is a buffer-local variable that prevents `crystal-format'
from running when it is non-`nil'.")
(defun crystal-format (&optional arg)
"Format the current Crystal buffer.