-
Notifications
You must be signed in to change notification settings - Fork 3
/
.emacs
2218 lines (1882 loc) · 83.2 KB
/
.emacs
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
;;
;; Akkana's ancient and grizzled GNU Emacs initialization file
;;
(add-to-list 'load-path "~/.emacs.d/lisp/")
;; Show errors in this file:
(setq debug-on-error t)
(setq stack-trace-on-error t)
; (setq load-path (cons "~/.emacs.d/lisp/" load-path))
;; Automatically uncompress .gz files
;; -- this seems to have stopped working unless I do it by hand.
(auto-compression-mode 1)
;; Disable all version control handling
(setq vc-handled-backends nil)
;; Don't prompt all the time for y e s \n
(fset 'yes-or-no-p 'y-or-n-p)
;; In emacs 28, this will work:
(setq use-short-answers t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set window size and font according to screen size. Adapted from
;; http://stackoverflow.com/questions/92971/how-do-i-set-the-size-of-emacs-window
(defun set-frame-size-by-resolution ()
(interactive)
(if (display-graphic-p) ; older: (if window-system
(progn
;; Emacs can't accept some fonts via Xdefaults. Here's how to set them here,
;; and we'd want to do it differently based on screen size:
;(message (number-to-string (x-display-pixel-height)))
;(sleep-for 2)
;; Small laptops
(if (<= (x-display-pixel-height) 768)
(set-frame-font "-misc-fixed-bold-r-normal-*-14-*-*-*-*-*-*-*")
;; X1C display
(if (<= (x-display-pixel-height) 1080)
;; JetBrains doesn't display the same in emacs as in urxvt,
;; and ends up taking up a lot of extra vertical space.
; (set-frame-font "Monoid HalfTight-8:bold")
(set-frame-font "JetBrains Mono-11:bold")
;; full monitor
(set-frame-font "Monoid HalfTight-7.5")
; (set-frame-font "JetBrains Mono-9")
))
;; for the height, subtract a couple hundred pixels
;; from the screen height (for panels, menubars and
;; whatnot), then divide by the height of a char to
;; get the height we want
(let ((newheight (+ (floor (/ (* (x-display-pixel-height) 0.55)
(frame-char-height)))
22)))
(message (format "New height: %d" newheight))
(set-frame-height (selected-frame) newheight)
)
;; Always use a width of 80.
;; You can't tell what the width is from xwininfo -- emacs reports 79
;; when it's really 80. So use the line above.
(set-frame-width (selected-frame) 80)
)))
;; To set initial window position too:
;; (set-frame-position (selected-frame) 10 30)
(set-frame-size-by-resolution)
;; Make it easy to zoom in and out, like when moving windows between
;; a laptop screen and large monitor.
;; Taking the adgice in zoom-frm to rebind the keys
;; associated with text-scale-adjust.
(load "zoom-frm")
;;
;; Basic key bindings
;; Emacs apparently doesn't have any way to make a truly global key binding:
;; global-set-key will be overridden by mode hooks.
;; However, general.el might be worth investigating,
;; https://github.com/noctuid/general.el
(defun global-settings ()
(global-set-key "\C-h" 'delete-backward-char)
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-c" 'save-buffers-kill-emacs)
(global-set-key "\C-x\C-v" 'find-file-other-window)
(global-set-key "\C-xs" 'save-buffer)
(global-set-key "\M-w" 'kill-region)
(global-set-key "\C-m" 'newline-and-indent)
(global-set-key (kbd "RET") 'newline-and-indent)
(global-set-key "\C-cc" 'comment-region)
(global-set-key "\C-cC" 'uncomment-region)
(global-set-key (kbd "C-c a") 'org-agenda)
;; Zooming
(global-set-key (kbd "C-x C-=") 'zoom-in)
(global-set-key (kbd "C-x C--") 'zoom-out)
;(global-set-key (kbd "C-x C-0") 'zoom-frm-unzoom)
(global-set-key (kbd "C-x C-0") 'set-frame-size-by-resolution)
(setq indent-tabs-mode nil)
) ;; end global-key-bindings
(global-settings)
(add-hook 'prog-mode-hook 'global-settings)
(add-hook 'text-mode-hook 'global-settings)
;;;;;;;;;;;;; X Selection / Clipboard behavior
;; Copy to the kill ring (and primary) when you select with the mouse:
(setq mouse-drag-copy-region t)
;; Make default yank use the primary selection, not clipboard.
(setq select-enable-clipboard nil)
(setq select-enable-primary t)
;; However, that isn't enough. yank (for PRIMARY) and clipboard-paste
;; (for CLIPBOARD) each sometimes replaces the other's selection.
;; Here's a more reliable way to do it, and also turn off
;; emacs' annoying habit of pasting colors from unrelated buffers.
;; (For a solution to that, see yank-excluded-properties, later.)
;; HOWEVER: x-selection and gui-get-selection both ignore the
;; current charset, e.g. they paste 326 instead of Ö.
;; (global-set-key (kbd "C-y")
;; (lambda () (interactive)
;; ;; (insert (x-selection 'PRIMARY))))
;; (insert (gui-get-selection 'PRIMARY))))
;; But this doesn't handle any nonascii characters that might be
;; in the selection.
;; mouse-yank-primary does the right thing.
(global-set-key (kbd "C-S-v")
(lambda () (interactive)
(insert (gui-get-selection 'CLIPBOARD))))
;; If emacs goes back to retaining syntax highlighting colors
;; inappropriately on paste, change both of those like this:
;; (insert (substring-no-properties (x-selection 'PRIMARY)))
;; When something is selected and you type, replace the selected text.
(delete-selection-mode t)
;; This copies whatever's selected in emacs or in the kill ring
;; to both primary and clipboard X selections.
(global-set-key (kbd "C-S-c") 'clipboard-kill-ring-save)
;; Leaving a buffer tends to stomp the primary selection.
;; The only way I've found to stop that is to ensure that
;; nothing is selected when changing buffers:
(defun deselect-then (what-then)
"Deselect any region in the current buffer, then call something else"
(deactivate-mark)
;; deactivate-mark leaves us at the region's end.
;; It might be nicer to end up at the region's beginning.
;; This doesn't work -- sets it to the end (why?) --
(if (region-active-p) (push-mark (region-beginning)))
;;(if (region-active-p) (push-mark (region-end)))
(call-interactively what-then)
)
(global-set-key "\C-xb"
(lambda () (interactive) (deselect-then 'switch-to-buffer)))
(global-set-key "\C-x\C-f"
(lambda () (interactive) (deselect-then 'find-file)))
;;;;;; end selection/clipboard hackery ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lately exchange-point-and-mark selects everything in between the two points,
;; clobbering anything in the primary selection.
;; But you can turn that off by passing t to activate "Transient Mark mode".
(global-set-key "\C-x\C-x" (lambda () (interactive)
(exchange-point-and-mark t)))
;; I am forever hitting this by accident, when my finger slips off
;; Ctrl-z and grazes C-x at the same time. Disable it:
(global-unset-key "\C-x\C-z")
;; I seem to spend half my free time chasing after various broken
;; electric indents in emacs. And really, the only time I ever want
;; electric indent is for }. So maybe the answer is to turn off
;; electrics everywhere, then rebind } to indent the current line
;; after inserting. Of course this doesn't really turn off electrics
;; everywhere, anyway.
;(setq electric-indent-mode nil)
;; Someone on #emacs suggests that this works better:
;(electric-indent-mode nil)
;; Despite the previous two lines, electric mode always ends up on anyway.
;; https://emacs.stackexchange.com/a/20899 claims this will tame
;; the worst part of electric indent, the re-indentation of the
;; current line, which I never ever want under any circumstances:
(setq-default electric-indent-inhibit t)
;; Electric mode has recently taken over (newline), so we have to do this:
(if (fboundp 'electric-indent-just-newline)
(global-set-key (kbd "<S-return>") 'electric-indent-just-newline)
(global-set-key (kbd "<S-return>") 'newline))
(global-set-key "\M-n" 'goto-line)
(global-set-key "\M-N" 'what-line)
(global-set-key "\M-?" 'help-for-help)
(global-set-key "\C-x\C-k" 'kill-buffer)
;; (global-set-key "\M-/" 'apropos)
(global-set-key "\C-c\C-c" 'kill-emacs)
(global-set-key "\C-r" 'isearch-backward)
(global-set-key "\C-x\C-i" 'indent-region)
(global-set-key "\C-x%" 'match-paren)
;; Use home/end to go to beginning/end of file, not line;
;; because ^A/^E are easy to hit but M-<> are not
;; (especially on a mini laptop).
(global-set-key [home] 'beginning-of-buffer)
(global-set-key [end] 'end-of-buffer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set a few key bindings that override everything and can't get
;; overridden by minor modes.
;; http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs
;; but that doesn't have all the details.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar global-keys-minor-mode-map (make-sparse-keymap)
"global-keys-minor-mode keymap.")
(define-key global-keys-minor-mode-map "\C-c\C-r" 'revert-buffer)
;; Insert the date
(define-key global-keys-minor-mode-map (kbd "C-;") 'insert-today-date)
(define-key global-keys-minor-mode-map (kbd "C-:") 'insert-yesterday-date)
;; Allow date insertion in the minibuffer too
(define-key minibuffer-local-map (kbd "C-;") 'insert-today-date)
(define-key minibuffer-local-map (kbd "C-:") 'insert-yesterday-date)
;; I'm always forgetting that I'm in the minibuffer and doing a ^Xb
;; to change buffers, which visits the file in a new window and doesn't
;; put the focus back in the minibuffer.
;; Ignoring it might help; otherwise, maybe make something that beeps
;; or otherwise alerts me.
(define-key minibuffer-local-map "\C-xb" 'ignore)
(define-minor-mode global-keys-minor-mode
"A minor mode so that global key settings override annoying major modes."
t "" 'global-keys-minor-mode-map)
;; name is set to "" since it gets shoved into the modeline for everything.
(global-keys-minor-mode 1)
;; A keymap that's supposed to be consulted before the first
;; minor-mode-map-alist.
(defconst global-minor-mode-alist (list (cons 'global-keys-minor-mode
global-keys-minor-mode-map)))
;; Next line used to use setf but emacs23 doesn't have setf.
(setq emulation-mode-map-alists '(global-minor-mode-alist))
;; Not sure if this part is actually needed.
;; It might depend on what key bindings are in global-keys-minor-mode-map.
(defun my-minibuffer-setup-hook ()
(global-keys-minor-mode 0))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
;;;;;;;;;;;;;;;;;;;;;;;;;; end global-keys code ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Packages
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(if (>= emacs-major-version 24)
; (progn
(require 'package)
(add-to-list 'package-archives
'("melpa-stable" . "https://stable.melpa.org/packages/"))
(package-initialize)
; ))
;;;;;;;;;;;;;;;;;;;;;;;;;; Undo handling ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; I keep hitting ^Z accidentally, and at the same time I can
;; never remember what undo is normally bound to.
;; Unfortunately this will suck if I run emacs from the shell,
;; so I'd really like to run this only if we're in GUI mode.
;; (if (display-graphic-p)
;; (global-set-key "\C-z" 'undo))
;; Redo mode allows real undo/redo: http://www.emacswiki.org/emacs/RedoMode
;(load "redo.el")
;(global-set-key "\M-z" 'redo)
;(global-set-key "\C-z" 'undo)
;(global-set-key "\M-z" 'redo)
;; Supposedly undo-tree is better.
;; To use it, install it using emacs' built-in package system,
;; since it's not shipped with emacs nor available in debian:
;; M-x package-install undo-tree
;; Then use package-initialize to enable it: require doesn't work
;; with emacs-installed packages. Then you have to read the comments
;; at the beginning of undo-tree.el to figure out how to use it since
;; there's no online documentation. *eyeroll*
(if (>= emacs-major-version 24)
(progn
; (package-initialize) ; Already done earlier
(undo-tree-mode 1)
(global-set-key "\C-z" 'undo-tree-undo)
(global-set-key "\M-z" 'undo-tree-redo)
)
(progn
(load "redo.el")
(global-set-key "\C-z" 'undo)
(global-set-key "\M-z" 'redo)
) )
;;;;;;;;;;;;;;;;;;;;;;;; end Undo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Make space do what tab does when autocompleting,
;; NOT stopping at punctuation:
(define-key minibuffer-local-completion-map " " 'minibuffer-complete)
;; better handling of duplicate filenames
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
;(setq default-major-mode 'text-mode)
(setq fill-column 75)
;;; Set the mode format at the bottom
;;; Used to be "%*%*%* " emacs-version " %b %M %[(%m)%] line=%5l %3p %-"
;(setq default-modeline-format
; (list "%*%*%* %b %M %[(%m)%] line=%5l %3p %-"))
(setq line-number-mode t)
;(setq mode-line-format default-mode-line-format)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Custom colors
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Color themes: search for color-theme.el
;(load "color-theme-akk.el")
;(color-theme-akk)
;; C-u C-x = or customize-face will let you customize whatever's at point,
;; and will add whatever it is to your .emacs.
;; However, it adds it with this caveat:
;; If there is more than one, they won't work right.
;; so I don't yet know what happens if you need to change more than one.
;; It may be that it's okay as long as you put all your face customizations
;; inside this one custom-set-faces call.
;;
;(set-background-color "grey90")
; Some decent colors: grey90, Alice Blue, light cyan, mint cream, Honeydew
;(set-background-color "mint cream")
(set-background-color "#e9fffa")
;; XXX Colors and fonts don't apply to new windows created with make-frame.
;; Apparently it would need to set default-frame properties or define
;; a theme in order to apply to new frames.
;; In the meantime, C-x C-0 should usually set things right.
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(flyspell-duplicate ((((class color)) (:foreground "red" :underline t :weight bold))))
'(font-lock-comment-face ((((class color) (min-colors 88) (background light)) (:foreground "blue"))))
;; Markdown faces
'(markdown-bold-face ((t (:family "Monoid HalfTight-7.5" :foreground "dark magenta" :weight bold :height 1.1))))
'(markdown-code-face ((t (:inherit fixed-pitch :background "ivory1"))))
'(markdown-header-face ((t (:family "Liberation Serif" :height 1.5 :weight bold))))
'(markdown-header-face-1 ((t (:inherit markdown-header-face :height 1.8 :foreground "navy blue"))))
'(markdown-header-face-2 ((t (:inherit markdown-header-face :height 1.6 :foreground "medium violet red"))))
'(markdown-header-face-3 ((t (:inherit markdown-header-face :height 1.4 :foreground "dark red"))))
'(markdown-header-face-4 ((t (:inherit markdown-header-face :height 1.2 :foreground "indian red"))))
'(markdown-inline-code-face ((t (:inherit font-lock-constant-face :background "gainsboro"))))
'(markdown-italic-face ((t (:foreground "dark green" :slant italic :height 1.1))))
'(markdown-link-face ((t (:inherit link))))
'(markdown-pre-face ((t (:background "ivory1" :family "monoid"))))
;; org mode fonts and colors
'(org-level-1 ((t (:family "Liberation Serif" :height 2.3 :foreground "navy blue" :weight bold))))
'(org-level-2 ((t (:family "Liberation Serif" :height 2.0 :foreground "dark magenta" :weight bold))))
'(org-level-3 ((t (:family "Liberation Serif" :height 1.7 :foreground "dark red" :weight bold))))
'(org-level-4 ((t (:family "Liberation Serif" :height 1.3 :foreground "indian red" :weight bold))))
'(org-link ((t (:underline t :slant italic :background "white" :foreground "blue"))))
'(whitespace-trailing ((t (:background "cyan" :foreground "yellow" :weight bold)))))
(set-face-foreground 'mode-line "yellow")
(set-face-background 'mode-line "purple")
(set-face-background 'mode-line-inactive "light blue")
(set-face-background 'trailing-whitespace "cornsilk")
(set-face-attribute 'region nil :background "#8df" :foreground "black")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; turning off annoyances
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; I'd rather not force this, since I occasionally edit binary files,
;; but it's just too annoying how emacs asks, then doesn't actually
;; add the newline so I have to go and do it myself.
;; Ideally I should do this only for text and Fundamental modes.
(setq require-final-newline t)
;; My KVM switch uses scroll lock, and emacs complains about it.
;; So silence that. 'noop worked in emacs21 but is gone in emacs22.
;; and this has stopped working again in emacs23.
;(global-set-key [scroll-lock] 'ignore)
(global-set-key (kbd "<Scroll_Lock>") 'ignore)
;; Get rid of keys I hit accidentally:
(global-unset-key "\M-c") ; don't want the capitalize thing
;; undefine the keys that do narrow-to-page and narrow-to-region,
;; because who would ever want such a stupid function??
;; And they're easy to hit accidentally.
(global-unset-key "\C-xp")
(global-unset-key "\C-xn")
;; Recent versions of emacs always blink the cursor. Yuck!
;(blink-cursor-mode nil)
;(customize-set-variable 'blink-cursor nil) ;; thanks to sachac!
; But that no longer works in emacs 22, so let's try this:
(if (fboundp 'blink-cursor-mode) (blink-cursor-mode 0))
;; Turn off that big useless toolbar
(tool-bar-mode 0)
(menu-bar-mode 0)
;; and the irritating scrollbar that claims to have somewhere to
;; scroll even on new 1-line files:
(toggle-scroll-bar -1)
;; and the mousewheel progressive speed:
(setq mouse-wheel-progressive-speed nil)
;; Emacs's html mode always asks for email address. Why??
(setq query-user-mail-address nil)
;; xemacs v21 is forever leaving .saves- files with long names
;; which screw up my directory listings. Make it put them
;; somewhere else:
(setq auto-save-list-file-prefix "~/.emacs-saves/.saves-")
;; or not do it at all (I can ^X^S frequently):
(setq auto-save-default nil)
;; make sure the tab width is right:
;(set-variable "tab-width" 8)
;(set-variable "default-tab-width" 8)
;(set-variable "indent-tabs-mode" nil)
(setq-default indent-tabs-mode nil)
(setq tabify nil)
;; Highlight trailing whitespace.
;; This may be too annoying on files edited by mac/win people.
(setq-default show-trailing-whitespace t)
;; Also show tabs. https://www.emacswiki.org/emacs/ShowWhiteSpace
;; but this is annoying because it also shows up in things like
;; autocomplete of directories. XXX Make it happen in coding modes only.
;; (defface extra-whitespace-face
;; '((t (:background "honeydew2")))
;; "Used for tabs and such.")
;; (defvar bad-whitespace
;; '(("\t" . 'extra-whitespace-face)))
;; Draw tabs with the same color as trailing whitespace
(add-hook 'font-lock-mode-hook
(lambda ()
(font-lock-add-keywords
nil
'(("\t" 0 'trailing-whitespace prepend)))))
;; Emacs 23 changed up/down behavior, so it goes to the next screen
;; line instead of the next buffer line (on lines long enough to wrap).
;; Revert to old behavior:
(setq line-move-visual nil)
;; Around 24.5, emacs developed the annoying habit that every time I
;; switch into a buffer, it primary-selects whatever region is active.
;; Nobody seems to know how to turn this off, so instead, bind C-x b
;; to something that eliminates any active region before switching out.
;; This is a much better alternative to the more drastic solution:
;; (transient-mark-mode 0)
;;
;; Unfortunately we need to do this for every way of switching buffers:
;; C-x b (switch-to-buffer)
;; C-x C-f (find-file)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A couple of functions to try to guess coding systems based on newlines.
;; Emacs' code for this is broken: it fails on any file that doesn't
;; end with a newline, which includes many formats like GPX files
;; and Spektrum SPM files.
(defun count-pat (pat &optional start end)
"Return the number of times the given pattern occurs in the region or buffer"
(interactive "sPattern: ")
(save-excursion
(let ((patcount 0)
(rstart (if start start (if (region-active-p) (region-beginning) 0)))
(rend (if end end (if (region-active-p) (region-end) (point-max))))
)
(goto-char rstart)
(while (and (< (point) rend)
(re-search-forward pat rend t))
(setq patcount (1+ patcount)))
;(message "%d occurrences of %s" patcount pat)
patcount
)))
(defun guess-line-endings ()
"Guess whether a file has Unix, Mac or Windows line endings. If the file ends with a newline/cr, Emacs can handle this on its own; but on files with no final newline, Emacs gets confused."
(interactive)
;; (message "Trying to guess line endings on %S" (buffer-name))
;; (sleep-for 1)
;; (message "")
(save-excursion
(goto-char (point-max))
;; (let ((lastchar (char-before)))
;; (if (or (char-equal lastchar ?\n) (char-equal lastchar ?\r))
;; (progn
;; (message "the file ends with a newline")
;; (sleep-for 1)
;; nil)
(let* ((win (count-pat "\r\n"))
(unix (- (count-pat "\n") win))
(mac (- (count-pat "\r") win))
(few 5)
)
(message "unix %d mac %d win %d" unix mac win)
(sleep-for 4)
(cond
((and (> unix few) (< mac few) (< win few)) 'utf-8-unix)
((and (> mac few) (< unix few) (< win few)) 'utf-8-mac)
((and (> win few) (< unix few) (< mac few)) 'utf-8-dos)
(t nil))
))) ;;)
;; Run on every file to as a pre-check for newlines/coding system:
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Default-Coding-Systems.html
;; but this doesn't actually help, e.g. Spektrum SPM files
;; and this doesn't make it automatically run, anyway.
;;(set-variable 'auto-coding-functions (list 'guess-line-endings))
;; The preceding sounds nice, but doesn't actually do anything.
;; Force mac line break interpretation for SPM files.
(modify-coding-system-alist 'file "\\.SPM\\'" 'utf-8-mac)
;; Allow copy from urxvt into emacs to use UTF-8 nonascii characters.
;; But apparently this is for things copied IN emacs,
;; not things copied in other apps and pasted into emacs.
(set-variable 'selection-coding-system 'utf-8)
;; If that proves inadequate (and it does), some other suggestions from
;; https://stackoverflow.com/a/2903256
;; XXX Note that these only affect mouse pasting; they don't help
;; ctrl-Y insert from selection, which does:
;; (insert (gui-get-selection (quote PRIMARY)))
(setq utf-translate-cjk-mode nil) ; disable CJK coding/encoding (Chinese/Japanese/Korean characters)
(set-language-environment 'utf-8)
; (set-keyboard-coding-system 'utf-8-mac) ; For old Carbon emacs on OS X only
(set-keyboard-coding-system 'utf-8)
(setq locale-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
;; XXX How can I get (x-selection 'PRIMARY) to use utf-8?
;; F1 C RET (or M-x describe-coding-system RET) lists coding systems in use:
;; they're all utf-8 except
;; Coding system for keyboard input:
;; U -- utf-8-unix (alias: mule-utf-8-unix cp65001-unix)
;; https://ftp.gnu.org/old-gnu/Manuals/emacs-20.7/html_node/emacs_197.html
;; says I can change that with
;; C-x RET k utf-8 RET
;; but it doesn't work: after that sequence, F1 C RET still
;; lists they keyboard input coding system as utf-8-unix.
;;;;;;;;;;;;; end coding system/newline guessing ;;;;;;;;;;;;;;;;;;;;;;
;; don't paste syntax highlight color into buffers where it's meaningless.
;; In emacs 25 this seems to work, but it doesn't work in emacs 24.
(add-to-list 'yank-excluded-properties 'font)
(add-to-list 'yank-excluded-properties 'face)
(add-to-list 'yank-excluded-properties 'font-lock-face)
(add-to-list 'yank-excluded-properties 'mouse-face)
;; In emacs24, yank-excluded-properties doesn't work.
;; However, this works for Ctrl-Y:
;; (if (and (boundp 'emacs-major-version)
;; (< emacs-major-version 25))
;; (progn
;; (defun yank-without-colors () (interactive)
;; (insert (substring-no-properties
;; (x-selection 'PRIMARY))))
;; (global-set-key "\C-y" 'yank-without-colors)
;; ;; Unfortunately there seems to be no way in emacs24 to get
;; ;; middlemouse to paste without colors.
;; ;; You can try things like this:
;; ;;
;; ;; (defun mouse-paste-without-colors (e)
;; ;; (interactive "e")
;; ;; (goto-char (posn-point (event-start e)))
;; ;; (insert (substring-no-properties (x-selection 'PRIMARY)))
;; ;; )
;; ;; (global-set-key (kbd "<mouse-2>") 'mouse-paste-without-colors)
;; ;;
;; ;; but then something happens after the paste that stomps the
;; ;; primary X selection and replaces it with a bunch of other crap.
;; ;; That happens in emacs25 too, but fortunately emacs25 does
;; ;; the right thing with yank-excluded-properties
;; ;; so we don't need these rebindings.
;; ;; Nobody seems to know why this is happening.
;; ;; There's something called <down-mouse-2> but it happens
;; ;; before <mouse-2>, not after, and binding it to 'nop doesn't help.
;; ;; There's no <up-mouse-2>.
;; )
;; )
; You can use this to turn off colors on a block of text:
(defun decolorize () (interactive)
(set-text-properties (point) (mark) nil))
(defun decolorize2 () (interactive)
(remove-text-properties (point) (mark)))
;;;;;;;;;;;;; End X Selection / Clipboard behavior
;(setq default-case-fold-search nil)
(setq delete-auto-save-files t)
(setq make-backup-files nil)
;; and some extensions for SGI's additions to c++-mode
(setq c++-hanging-member-init-colon t)
;; stop prompting me when I try to edit a link to an svn file
(setq vc-follow-symlinks t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; End annoyances
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Auto-insert for various modes
;;
;; auto-insert is cool but the documentation is terrible.
;; An auto-insert stanza goes like this:
;; (add-to-list 'auto-insert-alist
;; '(("\\.c$" . "C Program")
;; "Parameter"
;; "strings"
;; ))
;; If "Parameter" is non-nil, it will be used as a prompt, and whatever
;; you type in response to the prompt will be inserted wherever you
;; have "str" in the auto-insert data.
;; An underscore _ says to put the cursor there after inserting.
;; > indents to the current indent level according to the mode.
;; Some other variables and functions that might be useful to include:
;; (file-name-nondirectory (file-name-sans-extension buffer-file-name))
;; (substring (current-time-string) -4)
;; (user-full-name)
;;
;; You can also define autoinsert using define-auto-insert
;; but the syntax is a bit more opaque.
;;
;; https://www.emacswiki.org/emacs/AutoInsertMode
;; https://www.gnu.org/software/emacs/manual/html_node/autotype/Skeleton-Language.html#Skeleton-Language
;; https://www.emacswiki.org/emacs/SkeletonMode
(auto-insert-mode)
;; Auto-insert mode has tons of defaults and it's hard to get a list
;; of even what they are. I don't want auto-insert unless I've defined it.
(setq auto-insert-alist '())
;; Don't prompt before every auto-insertion:
(setq auto-insert-query nil)
;; Templates for files of specific types:
(add-to-list 'auto-insert-alist
'(python-mode
nil
"#!/usr/bin/env python3\n"
"\n"
_ "\n"
"\n"
"if __name__ == '__main__':\n"
> "\n\n"
))
;; Is this still needed?
;; (autoload 'sgml-mode "my-sgml-mode" "Load custom sgml-mode")
;;
;; auto-insert for HTML: check for a blank.html in the directory
;; and insert it if it exists. Otherwise insert a template.
;;
(defun insert-html-template ()
"Insert the contents of blank.html if it exists"
(interactive)
(let ((file-name (concat
(file-name-directory (buffer-file-name (current-buffer)))
"blank.html")))
(if (file-exists-p file-name)
(progn
(message (concat "Inserting: " file-name))
(insert-file-contents file-name))
; else insert standard HTML boilerplate
(let ((str (read-string "Title: ")))
(message "Inserting standard HTML")
(insert
;; "<!DOCTYPE html>\n"
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
"<html>\n"
"<head>\n"
"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
"<title>" str "</title>\n"
"</head>\n\n"
"<body>\n\n"
"<h1>" str "</h1>\n\n"
"<p>\n"
"\n\n" ;; The skeleton had a _ before the string, for point
"</body>\n"
"</html>\n")
(goto-char (point-max))
(forward-line -4)
))))
(add-to-list 'auto-insert-alist
'(("\\.html$" . "HTML file")
. insert-html-template
))
;; Override the standard HTML content for some files.
(add-to-list 'auto-insert-alist
'((".*app/templates.*\\.html$" . "HTML file")
nil
"{% extends \"base.html\" %}\n"
"\n"
"{% block content %}\n"
"\n"
"\n"
"{% endblock %}\n"
))
(add-to-list 'auto-insert-alist
'((".*\\(lwvweb\\|fairdistrictsnm\\|jemezdarkskies\\).*\\.html$" . "LWVNM HTML file")
"Title: "
"<?php\n"
" $title = \"" str "\";\n"
"\n"
" require ($_SERVER['DOCUMENT_ROOT'] . \"/php/header.php\");\n"
"?>\n"
"\n"
"<p>\n"
_ "\n"
"\n"
"<?php\n"
"require ($_SERVER['DOCUMENT_ROOT'] . \"/php/footer.php\");\n"
"?>\n"
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Useful utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Match paren. from http://grok2.tripod.com/
;; bind to C-x %
(defun match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
(defun fixm ()
"Change line breaks to Unix style. This no longer works because emacs tries to figure out line encodings on its own now, often gets it wrong but that overrides attempts to change things."
(interactive)
(set-buffer-file-coding-system 'utf-8-unix t)
(replace-string "\r" "")
;(message "fixm")
)
;; https://www.emacswiki.org/emacs/EndOfLineTips
(defun unix-file ()
"Change the current buffer to Unix line-ends."
(interactive)
(set-buffer-file-coding-system 'unix t))
(defun dos-file ()
"Change the current buffer to DOS line-ends."
(interactive)
(set-buffer-file-coding-system 'dos t))
;; A fix for DOuble CApitals from a slow left pinky.
;; Emacs is wonderful. :-)
;; https://emacs.stackexchange.com/questions/13970/fixing-double-capitals-as-i-type/13975
(defun dcaps-to-scaps ()
"Convert word in DOuble CApitals to Single Capitals."
(interactive)
(and (= ?w (char-syntax (char-before)))
(save-excursion
(and (if (called-interactively-p)
(skip-syntax-backward "w")
(= -3 (skip-syntax-backward "w")))
(let (case-fold-search)
(looking-at "\\b[[:upper:]]\\{2\\}[[:lower:]]"))
(capitalize-word 1)))))
(add-hook 'post-self-insert-hook #'dcaps-to-scaps nil 'local)
(define-minor-mode dubcaps-mode
"Toggle `dubcaps-mode'. Converts words in DOuble CApitals to Single Capitals as you type."
:init-value nil
:lighter (" dubcaps")
(if dubcaps-mode
(add-hook 'post-self-insert-hook #'dcaps-to-scaps nil 'local)
(remove-hook 'post-self-insert-hook #'dcaps-to-scaps 'local)))
;; Kill all Buffers without prompting.
;; Modified from kill-some-buffers in files.el, which prompts too much.
(defun kill-all-buffers ()
"Kill all buffers without prompting."
(interactive)
(let ((list (buffer-list)))
(while list
(let* ((buffer (car list))
(name (buffer-name buffer)))
(kill-buffer buffer))
(setq list (cdr list)))))
;;
;; For composing in emacs then pasting into a word processor,
;; this un-fills all the paragraphs (i.e. turns each paragraph
;; into one very long line) and removes any blank lines that
;; previously separated paragraphs.
;;
(defun wp-munge () "un-fill paragraphs and remove blank lines" (interactive)
(if (not (use-region-p)) (mark-whole-buffer))
(let ((save-fill-column fill-column)
(rstart (region-beginning))
(rend (region-end)))
;(message (format "beginning: %s, end: %s" rstart rend))
;(sleep-for 3)
; For all short lines, add an extra line break after them.
; This should get a lot of things like list items, titles etc.
(replace-regexp "^\\(.\\{1,45\\}\\)$" "\\1\n" nil rstart rend)
; Lines that start with - or * or something like 1. or a.
; followed by a space followed by more content are list lines, and
; have to be separated by blank lines, otherwise fill-* will merge them.
(replace-regexp "^\\s-*\\(-\\|\\*\\|[1-9a-zA-z]+\\.\\)\\s-+\\(.+\\)$"
"\n\\1 \\2" nil rstart rend)
; Fill all paragraphs
(set-fill-column 1000000)
(fill-individual-paragraphs rstart rend)
;; Now try to eliminate some of those extra lines we added earlier.
; Remove those blank lines we added after short lines.
(replace-regexp "^\\(.\\{1,45\\}\\)\n\n" "\\1\n" nil rstart rend)
; Replace runs of more than one blank line with a single one
(replace-regexp "\n\\{3,\\}" "\n\n" nil rstart rend)
; Remove extra newline after long list lines:
(replace-regexp "^\\s-*\\(-\\|\\*\\|[1-9a-zA-z]+\\.\\)\\s-+\\(.\\{46,\\}\\)\n\n"
"\\1 \\2\n" nil rstart rend)
; Deletion of blank lines currently disabled
;(delete-matching-lines "^$")
; restore the previous fill column
(set-fill-column save-fill-column)
))
(defun wp-unmunge () "fill paragraphs and separate them with blank lines"
(interactive)
(if (not (use-region-p)) (mark-whole-buffer))
(replace-regexp "\\(.$\\)" "\\1\n" nil (region-beginning) (region-end))
(fill-individual-paragraphs (region-beginning) (region-end))
;; This sometimes ends up with doubled blank lines, so:
(goto-char 1)
(replace-regexp "\n\n+" "\n\n")
)
(defun unfill () "un-fill paragraphs" (interactive)
(let ((save-fill-column fill-column))
(set-fill-column 1000000)
(mark-whole-buffer)
(fill-individual-paragraphs (point-min) (point-max))
(set-fill-column save-fill-column)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Special code for html and text files
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq sentence-end-double-space nil)
(define-derived-mode text-wrap-mode text-mode "Text wrap mode"
"Text mode, plus autofill and flyspell"
(auto-fill-mode)
(if (<= (buffer-size) 10000)
(progn (flyspell-mode 1)
(flyspell-buffer) )
(message "Buffer too big, not spellchecking")
)
)
;; Don't autofill when hitting return, only on space:
(set-char-table-range auto-fill-chars 10 nil)
;; In text mode, I don't want it auto-indenting for the first
;; line in the file, or lines following blank lines.
;; Everywhere else is okay.
(defun newline-and-text-indent ()
"Insert a newline, then indent the next line sensibly for text"
(interactive)
(cond
;; Beginning of buffer, or beginning of an existing line, don't indent:
((or (bobp) (bolp)) (newline))
;; If we're on a whitespace-only line,
((and (eolp)
(save-excursion (re-search-backward "^\\(\\s \\)*$"
(line-beginning-position) t)))
;; ... delete the whitespace, then add another newline.
;; Don't use (kill-line 0) here because that saves the whitespace
;; to the kill ring and stomps the X selection.
(delete-region (line-beginning-position) (line-end-position))
(newline))
;; Else (not on whitespace-only) insert a newline,
;; then add the appropriate indent:
(t (newline-and-indent))
;; If the previous set-char-table-range doesn't work to prevent Return
;; from indenting the current line, use this instead of previous line:
;;(t (insert "\n")
;; (indent-according-to-mode))
))
(defun text-hook ()
(local-set-key "\C-m" 'newline-and-text-indent)
; Initializing flyspell on a large buffer takes forever -- like, MINUTES.
;; So, only use it when we really need it.
;(flyspell-mode 1)
;(flyspell-buffer)
(local-set-key (kbd "C-;") 'insert-today-date)
(global-set-key (kbd "C-;") 'insert-today-date)
(local-set-key (kbd "C-:") 'insert-yesterday-date)
(global-set-key (kbd "C-:") 'insert-yesterday-date)
(dubcaps-mode t)
(toggle-word-wrap)
)
(add-hook 'text-mode-hook 'text-hook)
;;
;; Browsing to URLs -- used in html and org modes
;; (maybe some day I'll find a way to do it from markdown-mode).
;;
;; browse-url-of-buffer is on C-c C-v. Set it to quickbrowse:
;; (setq browse-url-browser-function 'browse-url-generic
;; browse-url-generic-program "quickbrowse")
;; default is xdg-open, set with xdg-settings get|set default-web-browser
;; If that ever starts getting called when saving,
;; it's probably because html-autoview-mode got mistakenly toggled on;
;; toggle it back off with C-c C-s.
;; For debugging such things: F1 m shows which minor modes are active,
;; and also shows key bindings related to those modes.
;; (setq browse-url-browser-function 'browse-url-generic
;; browse-url-generic-program "web-browser")
;; (defun my-browse-url-firefox-new-tab (url &optional new-window)
;; "Open URL in a new tab in Mozilla."
;; (interactive (browse-url-interactive-arg "URL: "))
;; (unless
;; (string= ""
;; (shell-command-to-string
;; (concat "mozilla-firefox -a firefox -new-tab 'openURL("
;; url ",new-tab)'")))