-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.el
3951 lines (3770 loc) · 152 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 --- Emacs configuration of Cody Chan
;;
;; Copyright (c) 2012-2019 Cody Chan <cody.chan.cz@gmail.com>
;;
;; Author: Cody Chan <cody.chan.cz@gmail.com>
;; URL: https://github.com/c0dy/dotemacs.d
;;
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 3 of the License, or (at your option) any later
;; version.
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;; You should have received a copy of the GNU General Public License along with
;; GNU Emacs; see the file COPYING. If not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;; USA.
;;
;; If you want to using package manager like Vundle, use
;; https://github.com/lunaryorn/.emacs.d/blob/master/init.el#L176
;; http://oli.me.uk/2014/10/20/making-package-el-behave-like-vundle/
;; https://github.com/Wolfy87/dotfiles/blob/d24591ebd7b3a36f629fb5a4ebd921c72f2b5b91/emacs/init.el#L61-L96
;; http://www.reddit.com/r/emacs/comments/2jtojf/packageel_didnt_prune_my_unused_packages_so_i/
;; http://www.lonecpluspluscoder.com/2014/11/set-emacs-use-melpa-melpa-stable
;; byte compile emacs lisp files of current dir
;; emacs -batch -f batch-byte-compile *.el
;; I just found that you can just use the MENU
;; (the one between the Right-Alt and Right-Ctrl key) key
;; to replace M-x
;;
;; http://tuhdo.github.io/emacs-tutor3.html
;; Three ways to set a global key
;; (global-set-key (kbd "C-x C-b") 'ibuffer) ;; recommended
;; (global-set-key "\C-x\C-b" 'ibuffer)
;; (global-set-key [?\C-x?\C-b] 'ibuffer)
;;
;; the kbd issue
;; F1-f edmacro-mode
;; (kbd "C-<backspace>")
;; (kbd "<f7>")
;; [(f8)]
;; [f9]
;; "\C-ce"
;; "\M-n"
;; "\C-x\ \C-r"
;; (kbd "SPC")
;; (kbd "")
;; (kbd "C-x C-b")
;; (kbd "RET")
;; (kbd "<end/home>") ; End/Home
;; (kbd "<prior/next>") ; PageUp/Down
;; (kbd "<backtab") ;; S-TAB or C-iso-tab
;; (kbd "<S-return>")
;; (kbd "C-S-<left>")
;; (kbd "C-x <up>")
;; (kbd "C-{")
;; (kbd "C-<tab>") -- C-S-tab
;; (kbd "C-S-<iso-lefttab>")
;; (kbd "C->")
;; [(meta control S)]
;; [(meta control s)]
;; C-h b to show all the shortkeys
;;
;; shortcuts summary:
;; M-x check-parens to quickly check for mismatched parentheses
;; M-x info-apropos to search all info manuals
;; C-h e switch to buffer *Message*
;; C-h m 'describe-mode show all active modes and brief description
;; C-M-a/e 'beginning/end-of-defun
;; C-M-h 'mark-defun
;; C-S-m for 'menu-bar-mode
;; Enter or C-j to 'newline-and-indent
;; F7 to 'switch-to-minibuffer-window
;; F8 to make the frame transparent
;; F9 to 'search-all-buffers
;; C-c r to 'rev('revert-buffer)
;; C-x s to 'sh('shell)
;; C-x C-r to 'recentf-open-files
;; C-k to 'kill-line to the end of the line
;; M-k to 'kill-line to the beginning of the line
;; C-x c to 'emacs-lisp-byte-compile-and-load
;; C-c d to 'delete-trailing-whitespace
;; C-x C-j to 'dired-jump
;; C-c y to 'yas-reload-all
;; C-M-n/p Move forward/backward over a parenthetical group
;; C-M-u/d Move up/down in parenthesis structure
;; M-$ -> i -> y to insert the string into personal dictionary
;; the personal dictionary asides in ~/.hunspell_en_US
;; file has already been linked to .emacs.d/
;; in the comment, if you want to insert another comment line, use M-j
;; M-m 'back-to-indentation move point to first non-whitespace character
;; M-x find-library will lead you to the right .el file
;; Windows style line endings (DOS support)
;; C-x RET f undecided-dos RET --> \r\n (windows)
;; C-x RET f undecided-unix RET --> \n (unix/Linux)
;; M-x tabify/untabify convert from spaces to tabs and vice verse
;; NOTE: call untabify/tabify with prefix argument, it will convert for the entire buffer
;; If Emacs stucks at startup, uncomment the following lines or put
;; 255.255.255.255 host.does.not.exist
;; in /etc/hosts
;; (setq tramp-default-method "ssh")
;; (setq tramp-ssh-controlmaster-options "-o ControlMaster=auto -o ControlPath='tramp.%%C' -o ControlPersist=no")
(let ((benchmark-init.el "~/.emacs.d/elpa/benchmark-init-20150905.938/benchmark-init.el"))
(when (file-exists-p benchmark-init.el)
(load benchmark-init.el)))
(defalias 'eit-list 'benchmark-init/show-durations-tabulated)
;; Speed up emacs startup
(let ((file-name-handler-alist nil)) "~/.emacs.d/init.elc")
(setq package-enable-at-startup nil)
(defvar file-name-handler-alist-old file-name-handler-alist)
(setq file-name-handler-alist nil
gc-cons-threshold 402653184
gc-cons-percentage 0.6)
(add-hook 'after-init-hook
(lambda ()
(setq file-name-handler-alist file-name-handler-alist-old
gc-cons-threshold 16777216
gc-cons-percentage 0.1)))
;; (setq debug-on-error t)
;; M-x profiler-start and do your thing and M-x profiler-report to show the
;; performance report, the following improves the structure of the report
(setq profiler-report-cpu-line-format '((80 left) (24 right ((19 right) (5 right)))))
(setq profiler-report-memory-line-format '((80 left) (24 right ((19 right) (5 right)))))
;; proxy goagent
;; (setq url-proxy-services '(("http*" . "127.0.0.1:8087")))
;; hide the welcome/home page when startup
(setq inhibit-startup-screen t)
;; Makes *scratch* empty.
;;(setq initial-scratch-message "")
;; encode, the last line will be the highest priority
(set-language-environment 'UTF-8)
;; this the current file is not utf-8, when saving it, it will prompt to select
;; the coding system, use this following line to choose utf-8 always
(setq coding-system-for-write 'utf-8)
(setq-default path-coding-system 'utf-8)
(setq file-name-coding-system 'utf-8)
(prefer-coding-system 'cp950)
(prefer-coding-system 'gb2312)
(prefer-coding-system 'cp936)
(prefer-coding-system 'gb18030)
;;(prefer-coding-system 'utf-16le-with-signature)
(prefer-coding-system 'utf-16)
(prefer-coding-system 'utf-8)
;; uncomment for CJK utf-8 support for non-Asian users
;; (require 'un-define)
;; http://www.toryanderson.com/tech/upgrading-emacs-built-org-mode-4-easy-steps
;; Add this before setting any Org option(loading org-mode)
;; and M-x package-install under `emacs -q`(prevents it from loading my .emacs file,
;; which includes many references to org-mode stuff.)
;; to prevent two versions of org-mode messed-up
(package-initialize)
;; Use variables such as PATH defined in fish/bash
;; exec-path-from-shell package
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize))
(require 'bind-key)
(add-to-list 'load-path "~/.emacs.d/lisp/")
(defalias 'man 'woman)
;; colors for man page
(require 'man)
(set-face-attribute 'Man-overstrike nil :inherit font-lock-type-face :bold t)
(set-face-attribute 'Man-underline nil :inherit font-lock-keyword-face :underline t)
;; use man for a function inside Emacs
(dolist (hook
'(
;; c-mode-hook
;; c++-mode-hook
c-mode-common-hook))
(add-hook hook
(lambda ()
(local-set-key
(kbd "C-h d")
(lambda ()
(interactive)
(manual-entry (current-word)))))))
(setq byte-compile-warnings nil)
(defalias 'eit 'emacs-init-time)
;; re/compile every elisp file when saving it
(add-hook 'emacs-lisp-mode-hook
(lambda ()
(add-hook 'after-save-hook 'emacs-lisp-byte-compile t t)))
;; Delete the existed/no-existed .emacs.elc and recompile and reload
(defun byte-compile-init-file ()
(when (equal user-init-file buffer-file-name)
(when (file-exists-p (concat user-init-file ".elc"))
(delete-file (concat user-init-file ".elc")))
(emacs-lisp-byte-compile-and-load)))
(add-hook 'after-save-hook 'byte-compile-init-file)
;; find ~/.emacs.d -name "*.elc" | xargs rm -rfv
;; C-0 M-x bd or M-x bd C-0 to bd
(defalias 'bd 'byte-recompile-directory)
;; byte-comple and load *.el using "C-x c"
(bind-keys :map emacs-lisp-mode-map
("C-x c" . emacs-lisp-byte-compile-and-load)
("C-c c" . eval-buffer)
("C-c C-r" . (lambda () (interactive) (load-file "~/.emacs.d/init.elc")))
)
(bind-keys :map lisp-interaction-mode-map
("C-c C-e" . (lambda () (interactive) (find-file "~/.emacs.d/init.el")))
("C-c C-r" . (lambda () (interactive) (load-file "~/.emacs.d/init.elc")))
;; C-h e to switch to *Message* Buffer
)
(bind-key* "C-x M-z" (lambda () (interactive) (switch-to-buffer "*scratch*")))
;; assembly
;; replace auto-mode-alist for multiple extensions
(mapc
(lambda (file)
(add-to-list 'auto-mode-alist
(cons (concat (regexp-quote file) "\\'") 'org-mode)))
'(".asm" ".s" ".S"))
;; To set your own indentation level to 4:
(add-hook 'nasm-mode-hook
(lambda () (setq-default nasm-basic-offset 4)))
;; compile
;; use `C-c ! n/p` 'flycheck-next/previous-error to navigate errors
;; or use M-g n/p for next/previous-error
(require 'compile)
(setq compilation-last-buffer nil)
;; save all modified buffers without asking before compilation
(setq compilation-ask-about-save nil)
(defun compile-again (ARG)
"Run the same compile as the last time.
With a prefix argument or no last time, this acts like M-x compile,
and you can reconfigure the compile args."
(interactive "p")
;; the following two lines create bug: split a new window every time
;; (if (not (get-buffer-window "*compilation*"))
;; (split-window-vertically -10))
(if (and (eq ARG 1) compilation-last-buffer)
(recompile)
(call-interactively 'smart-compile)))
(bind-key* "C-x C-m" 'compile-again)
;; create a new small frame to show the compilation info
;; will be auto closed if no error
(setq special-display-buffer-names
`(("*compilation*" . ((name . "*compilation*")
,@default-frame-alist
(left . (- 1))
(top . 0)))))
(setq compilation-auto-jump-to-first-error t)
(setq compilation-finish-functions
(lambda (buf str)
(if (null (string-match ".*exited abnormally.*" str))
(message "No Compilation Errors!")
;; no errors, make the compilation window go away in a few seconds
(progn
(run-at-time
"0 sec" nil 'delete-windows-on
"*compilation*")))))
;; 2015-07-04 Emacs Bug: Pasting into Emacs Freezes Emacs
;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=16737#17
;; http://ergoemacs.org/misc/emacs_bug_cant_paste_2015.html
(setq x-selection-timeout 100)
;; ;; omit the result to STDOUT after return when using emacs/emacsclient -e "expression"
;; (when (not (display-graphic-p))
;; (define-advice server-eval-and-print (:filter-args (args) no-print)
;; (list (car args) nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; Emacs Face Setting
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq bookmark-save-flag t)
(setq column-number-mode t)
(setq-default fill-column 80)
(require 'highlight-beyond-fill-column)
(add-hook 'prog-mode-hook 'highlight-beyond-fill-column)
;; highlight links in any mode
(define-global-minor-mode global-goto-address-mode
goto-address-mode
(lambda ()
(goto-address-mode 1)))
(global-goto-address-mode t)
;; C-x </> 'scroll-left/right if line is too long
(put 'scroll-left 'disabled nil)
;; file size in mode line
(setq size-indication-mode t)
;; symbol to indicate the end of the buffer
(setq-default indicate-empty-lines t)
;; change the color and symbol of the tilde
(progn
(define-fringe-bitmap 'tilde [0 0 0 113 219 142 0 0] nil nil 'center)
(setcdr (assq 'empty-line fringe-indicator-alist) 'tilde))
(set-fringe-bitmap-face 'tilde nil)
(set-fringe-mode '(8 . 0))
(tool-bar-mode 0)
(xterm-mouse-mode 1) ;; enable mouse in terminal Emacs
(scroll-bar-mode 0)
(menu-bar-mode 0)
(bind-keys*
("C-S-m" . menu-bar-mode)
("C-x M-l" . global-display-line-numbers-mode))
;; scroll text up/down by one line, not cursor
(global-set-key (kbd "C-M-n") (kbd "C-u 1 C-v"))
(global-set-key (kbd "C-M-p") (kbd "C-u 1 M-v"))
;; in c-mode
(setq c-backspace-function 'backward-delete-char)
;; Toggle which-function-mode and projectile-global-mode, useful after finishing using tramp.
;; Do not use when using tramp, it will stuck tramp a little bit
(bind-key* "C-x M-p"
(lambda ()
(interactive)
(if (bound-and-true-p which-function-mode)
(which-function-mode -1)
(which-function-mode 1))
(if (bound-and-true-p projectile-global-mode)
(projectile-global-mode -1)
(projectile-global-mode 1))))
;; line space between lines, default to 0
;; (setq line-spacing 2)
;;
;; display buffer name or absolute file path name in the frame tittle
;; NOTE: you should comment the last line of
;; /usr/share/emacs/site-lisp/default.el, or this setting won't work
;; and put time in frame-title to make the mode line clean
(display-time-mode 1)
(setq display-time-24hr-format t)
(setq display-time-day-and-date t)
(setq global-mode-string nil)
;; this will not always show the day of week, weird
;; (setq frame-title-format
;; '("%b@%f" "--" display-time-string))
(setq frame-title-format
'("%b" (:eval (if (buffer-file-name)
(concat "@"
(abbreviate-file-name default-directory))))
" - " display-time-string))
;;
;; syntax highlight
(global-font-lock-mode t)
;; highlight TODO:/NOTE:/FIXME:/BUG: keywords
(dolist (hook '(prog-mode-hook org-mode-hook))
(add-hook hook
(lambda ()
(font-lock-add-keywords
nil
;; '(("\\<\\(TODO\\|FIXME\\|BUG\\):" 1
'(("\\<\\(TODO:\\|NOTE:\\|FIXME:\\|BUG:\\)" 1
font-lock-warning-face t))))))
;; Turn on font lock mode in all the files
(setq font-lock-maximum-decoration t)
;;
;; Improve performance when editing large size of file
(defadvice helm-find-files (after helm-find-files activate)
;; "If a file is over a given size, turn off minor modes."
(progn
(when (> (buffer-size) (* 1024 100)) ;; 100 KB
(when (> (buffer-size) (* 1024 1024)) ;; 1 MB
(require 'vlf)
(vlf-mode))
(display-line-numbers-mode -1))))
;; C-x C-s to use save-buffer for regular files and use sudo to prompt passwd to
;; save file need root permission, C-x C-q to edit the root file first
;; Note that this C-x C-s will fail if the buffer is not a file, in this case,
;; use C-x C-w instead
(bind-key* "C-x C-s"
(lambda ()
(interactive)
(progn
;; check if the buffer is a file or like *scratch*
(if (buffer-file-name)
(if (file-writable-p buffer-file-name) (save-buffer)
(write-file (concat "/sudo::" buffer-file-name)))
(save-buffer)))))
;; displays the argument list for current func, work for all languages
(eldoc-mode)
(dolist (mode '(prog-mode-hook python-mode-hook ielm-mode-hook))
(add-hook mode
'(lambda ()
(eldoc-mode))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; theme & font
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; font and size of startup
;;
;; List all fonts available to emacs
;; (print (font-family-list))
;;
;; Test font in current session;
;; Set font for all windows, keep window size fixed
;; (set-frame-font "PragmataPro-10" t t)
;; set font for all windows, don't keep window size fixed
;; (set-frame-font "PragmataPro-10" nil t)
(defun set-frame-size-according-to-resolution ()
(interactive)
(if window-system
(progn
;; Setting font size will affect frame size, so set font size first
;; But it still slightly affect the frame size even using this order
;; or Monaco, Bitstream Vera Sans Mono, Liberation Mono
(if (> (x-display-pixel-width) 1500)
(cond
((find-font (font-spec :name "PragmataPro"))
(set-frame-font "PragmataPro-13.5"))
((find-font (font-spec :name "Input Mono Compressed"))
(set-frame-font "Input Mono Compressed-13.5")))
(cond
((find-font (font-spec :name "PragmataPro"))
(set-frame-font "PragmataPro-12"))
((find-font (font-spec :name "Input Mono Compressed"))
(set-frame-font "Input Mono Compressed-12"))))
(setq width-chars (/ (/ (x-display-pixel-width) (frame-char-width)) 2))
(setq height-lines (- (/ (x-display-pixel-height) (frame-char-height)) 4))
(setq default-frame-alist
`((top . 0) (left . 0)
(width . ,width-chars)
(height . ,height-lines)))
;; the following two settings are specifically for afternoon-theme
;; the combination colors of highlighted line and comments
;; (custom-set-faces
;; '(font-lock-comment-face
;; ((t (:foreground "gray60" :slant italic :weight normal :family "Menlo")))
;; ))
;; (set-face-background 'highlight "gray30")
)))
;;
;; theme
;;
;; Enabling a light theme temporarily, use M-x load-theme <TAB> flatui if you
;; want to enable it after start up, add the two lines like below
;;
;;
;; afternoon
;; (require 'afternoon-theme)
;;
;; ;; molokai
;; (load-theme 'molokai t)
;; (require 'molokai-theme)
;;
;; moe-theme, a very colorful and powerful theme
;; for more setting at https://github.com/kuanyui/moe-theme.el
;; Put all theme configuration into the display-graphic-p block
(if (daemonp)
;; for emacs --deamon, do not load theme/font in terminal Emacs
(add-hook 'after-make-frame-functions
(lambda (frame)
(select-frame frame)
(when (display-graphic-p)
(set-frame-size-according-to-resolution)
;; the following will get rid of prompt when M-x load-theme, treat all
;; themes as safe
(setq custom-safe-themes t)
(require 'moe-theme)
;; Resize titles
(setq moe-theme-resize-markdown-title '(1.3 1.2 1.1 1.0 1.0 1.0))
(setq moe-theme-resize-org-title '(1.3 1.2 1.1 1.0 1.0 1.0 1.0 1.0 1.0))
;; disable default mode-line buffer-id highlight
(setq moe-theme-highlight-buffer-id nil)
(moe-dark))))
(when (display-graphic-p)
(progn
(set-frame-size-according-to-resolution)
;; the following will get rid of prompt when M-x load-theme, treat all
;; themes as safe
(setq custom-safe-themes t)
(require 'moe-theme)
;; Resize titles
(setq moe-theme-resize-markdown-title '(1.3 1.2 1.1 1.0 1.0 1.0))
(setq moe-theme-resize-org-title '(1.3 1.2 1.1 1.0 1.0 1.0 1.0 1.0 1.0))
;; disable default mode-line buffer-id highlight
(setq moe-theme-highlight-buffer-id nil)
(moe-dark))))
;;
;; disable scroll-bar-mode in newly created frame
(add-hook 'after-make-frame-functions
'(lambda (frame)
(modify-frame-parameters
frame
'((vertical-scroll-bars . nil)
(horizontal-scroll-bars . nil)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; all about mode line
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; do no just use ("%2b"), or stick-buffer function won't work
(setq-default
mode-line-buffer-identification
'(#("%2b" 0 3
(local-map
(keymap
(header-line keymap
(mouse-3 . mode-line-next-buffer)
(down-mouse-3 . ignore)
(mouse-1 . mode-line-previous-buffer)
(down-mouse-1 . ignore))
(mode-line keymap
(mouse-3 . mode-line-next-buffer)
(mouse-1 . mode-line-previous-buffer)))
mouse-face mode-line-highlight help-echo
"Buffer name\nmouse-1: Previous buffer\nmouse-3: Next buffer"
face mode-line-buffer-id))))
;; show which function in mode-line
(which-function-mode 1)
;; make which-function-mode work only for specific modes
(with-eval-after-load "which-func"
(setq which-func-modes '(c-mode c++-mode emacs-lisp-mode python-mode)))
;; replace ??? to n/a
(setq which-func-unknown "n/a")
(set-face-attribute 'which-func nil :background nil :foreground nil)
;; line/column/percent/size, just "(%l,%c)[%p/%I]" if not highlight
(setq-default mode-line-position
'(("(%l_"
(:eval (propertize "%c" 'face
(if (>= (current-column) 80)
'mode-line-80col-face
'mode-line-position-face)))
"|%p_%I) ")))
;; highlight when point is over 80th column
(make-face 'mode-line-80col-face)
(make-face 'mode-line-position-face)
(set-face-attribute 'mode-line-80col-face nil :background "red1")
(set-face-attribute 'mode-line-position-face nil)
(set-face-attribute 'mode-line nil :background "dim gray" :foreground "white")
(set-face-attribute 'mode-line-buffer-id nil :foreground nil :background nil)
(set-face-attribute 'mode-line-inactive nil :background nil)
;; mode-line color
;; make the code inside #if 0/#else/#endif the same color as comment
(defun c-mode-font-lock-if0 (limit)
(save-restriction
(widen)
(save-excursion
(goto-char (point-min))
(let ((depth 0) str start start-depth)
;; Search #if/#else/#endif using regular expression.
(while (re-search-forward "^\\s-*#\\s-*\\(if\\|else\\|endif\\)" limit 'move)
(setq str (match-string 1))
;; Handle #if.
(if (string= str "if")
(progn
(setq depth (1+ depth))
;; Handle neariest 0.
(when (and (null start) (looking-at "\\s-+0"))
(setq start (match-end 0)
start-depth depth)))
;; Handle #else, here we can decorate #if 0->#else block using 'font-lock-comment-face'.
(when (and start (= depth start-depth))
(c-put-font-lock-face start (match-beginning 0) 'font-lock-comment-face)
(setq start nil))
;; Handle #endif, return to upper block if possible.
(when (string= str "endif")
(setq depth (1- depth)))))
;; Corner case when there are only #if 0 (May be you are coding now:))
(when (and start (> depth 0))
(c-put-font-lock-face start (point) 'font-lock-comment-face)))))
nil)
(defun my-c-mode-common-hook ()
(font-lock-add-keywords
nil
'((c-mode-font-lock-if0 (0 font-lock-comment-face prepend))) 'add-to-end))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
;; whole structure of mode line
(setq-default mode-line-format
'(
"%e"
mode-line-front-space
mode-line-mule-info
mode-line-client
mode-line-modified
mode-line-remote
mode-line-frame-identification
mode-line-buffer-identification
(which-func-mode
("" which-func-format " "))
" " mode-line-position
(vc-mode vc-mode)
(t org-mode-line-string) ;; show org info such as clock in mode line
" " mode-line-modes
"%-"))
(global-hl-line-mode 1)
(set-face-attribute hl-line-face nil :underline t)
;; make cursor the width of the character it is under i.e. full width of a TAB
(setq x-stretch-cursor t)
;; make cursor fixed
;; (set-default 'cursor-type '(bar . 3))
;; (setq-default cursor-in-non-selected-windows 1)
;; highlight the active window
;; flash the active window
(bind-key* "s-<f12>" 'flash-active-buffer)
(make-face 'flash-active-buffer-face)
(set-face-attribute 'flash-active-buffer-face nil
:background "blue" :foreground nil)
(defun flash-active-buffer ()
(interactive)
(run-at-time "150 millisec" nil
(lambda (remap-cookie)
(face-remap-remove-relative remap-cookie))
(face-remap-add-relative 'default 'flash-active-buffer-face)))
;; another way to highlight the active window
(defun highlight-active-window ()
"Highlight active window with a different background color."
(walk-windows (lambda (w)
(unless (eq w (selected-window))
(with-current-buffer (window-buffer w)
(buffer-face-set '(:background "#111"))))))
(buffer-face-set 'default))
(add-hook 'buffer-list-update-hook 'highlight-active-window)
;; using a visible bell when error occurs
;;(setq visible-bell t)
;; Using F8 to make the face transparent
(bind-key* "<f8>" 'loop-alpha)
(setq alpha-list '((70 70) (100 100)))
;; When showing warning: reference to free variable `alpha-list'
;; add the `(defvar alphs-list)`
(defvar alpha-list)
(defun loop-alpha ()
(interactive)
(let ((h (car alpha-list)))
((lambda (a ab)
(set-frame-parameter (selected-frame) 'alpha (list a ab))
(add-to-list 'default-frame-alist
(cons 'alpha (list a ab))))
(car h) (car (cdr h)))
(setq alpha-list (cdr (append alpha-list (list h))))))
;; ;; smooth-scrolling, just deal with C-n/p and arrow
(setq redisplay-dont-pause t
scroll-margin 1
scroll-step 1
scroll-conservatively 10000
auto-window-vscroll nil
scroll-preserve-screen-position 1)
(setq-default
scroll-up-aggressively 0.01
scroll-down-aggressively 0.01)
;; deal with C/M-v and mouse/touchpad
(require 'smooth-scroll)
(smooth-scroll-mode t)
(setq mouse-wheel-scroll-amount '(0.08)
mouse-wheel-progressive-speed nil)
;; set the query-replace from top
(defun query-replace-from-top ()
(interactive)
(let ((orig-point (point)))
(save-excursion
(goto-char (point-min))
(call-interactively 'query-replace))
(message "Back to old point.")
(goto-char orig-point)))
(bind-key* "M-%" 'query-replace-from-top)
;; flush blank lines
(defun flush-blank-lines (start end)
"Mark a block and delete all blank/empty lines inside it."
(interactive "r")
(flush-lines "^\\s-*$" start end nil))
(bind-keys*
("C-x M-a" . beginning-of-visual-line)
("C-x M-e" .
(lambda ()
(interactive)
(end-of-visual-line)
(backward-char))))
(defun keep-beginning-of-line (ARG)
"Make `C-a` keep going to first non-whitespace character _and_then_ beginning of
next line(previous with C-u).
It will not work as expected in comment block because of goddamn rebox2"
(interactive "P")
(when (bolp) (forward-line (if ARG -1 1)))
(let ((orig-point (point)))
(back-to-indentation)
(when (= orig-point (point))
(move-beginning-of-line 1))))
(defun keep-end-of-line (ARG)
"Make `C-e` keep going to end of next line(previous with C-u).
It will become normal in comment block because of goddamn rebox2"
(interactive "P")
(when (eolp) (forward-line (if ARG -1 1)))
(move-end-of-line nil))
;; (global-set-key [remap move-beginning-of-line] #'keep-beginning-of-line)
;; (global-set-key [remap move-end-of-line] #'keep-end-of-line)
(bind-keys*
("C-a" . keep-beginning-of-line)
("C-e" . keep-end-of-line))
(defun increment-region (&optional beg end ARG)
"Increment all decimal numbers in region between `beg' and `end' by `ARG'.
If no prefix ARG is given, increment by 1.
If the mark is not active, try to build a region using `symbol-at-point'."
(interactive "r\np")
(or ARG (setq ARG 1))
(unless (and mark-active transient-mark-mode)
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds (setq beg (car bounds) end (cdr bounds)))))
(if (< end beg)
(let ((tmp end))
(setq beg end end tmp)))
(save-excursion
(goto-char beg)
(while (re-search-forward "-?[0-9]+" end t)
(replace-match (number-to-string (+ ARG (string-to-number (match-string 0)))))))
(setq deactivate-mark nil))
;;
(defun decrement-region (&optional beg end ARG)
"Decrement all decimal numbers in region between `beg' and `end' by `ARG'.
If no prefix ARG is given, increment by 1.
If the mark is not active, try to build a region using `symbol-at-point'."
(interactive "r\np")
(or ARG (setq ARG 1))
(unless (and mark-active transient-mark-mode)
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds (setq beg (car bounds) end (cdr bounds)))))
(increment-region beg end (- ARG)))
(bind-key* "C-c $"
(defhydra hydra-change-num (:hint nil)
""
("<up>" increment-region "increment")
("<down>" decrement-region "decrement")
("q" nil)))
;; make the default sentence ending with two spaces concept nil
;; Now it work for expand-region to expand sentence
(setq sentence-end-double-space nil)
(defadvice endless/upcase (before upcase-word-advice activate)
(unless (looking-back "\\b")
(backward-word)))
(defadvice endless/downcase (before downcase-word-advice activate)
(unless (looking-back "\\b")
(backward-word)))
(defadvice endless/capitalize (before capitalize-word-advice activate)
(unless (looking-back "\\b")
(backward-word)))
;; TODO: make the following function accept arg-num
(defun xah-toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Always cycle in this order: Init Caps, ALL CAPS, all lower.
URL `http://ergoemacs.org/emacs/modernization_upcase-word.html'
Version 2017-04-19"
(interactive)
(let (
(deactivate-mark nil)
$p1 $p2)
(if (use-region-p)
(setq $p1 (region-beginning)
$p2 (region-end))
(save-excursion
(skip-chars-backward "[:alnum:]-_")
(setq $p1 (point))
(skip-chars-forward "[:alnum:]-_")
(setq $p2 (point))))
(when (not (eq last-command this-command))
(put this-command 'state 0))
(cond
((equal 0 (get this-command 'state))
(upcase-initials-region $p1 $p2)
(put this-command 'state 1))
((equal 1 (get this-command 'state))
(upcase-region $p1 $p2)
(put this-command 'state 2))
((equal 2 (get this-command 'state))
(downcase-region $p1 $p2)
(put this-command 'state 0)))))
;; automatically convert the comma/dot once downcase/upcase next character
(defun endless/convert-punctuation (rg rp)
"Look for regexp RG around point, and replace with RP.
Only applies to text-mode."
(let ((f "\\(%s\\)\\(%s\\)")
(space "?:[[:blank:]\n\r]*"))
;; We obviously don't want to do this in prog-mode.
(if (and (derived-mode-p 'org-mode)
(or (looking-at (format f space rg))
(looking-back (format f rg space))))
(replace-match rp nil nil nil 1))))
(defun endless/capitalize ()
"Capitalize region or word.
Also converts commas to full stops, and kills
extraneous space at beginning of line."
(interactive)
(endless/convert-punctuation "," ".")
(if (use-region-p)
(call-interactively 'capitalize-region)
;; A single space at the start of a line:
(when (looking-at "^\\s-\\b")
;; get rid of it!
(delete-char 1))
(call-interactively 'subword-capitalize)))
(defun endless/downcase ()
"Downcase region or word.
Also converts full stops to commas."
(interactive)
(endless/convert-punctuation "\\." ",")
(if (use-region-p)
(call-interactively 'downcase-region)
(call-interactively 'subword-downcase)))
(defun endless/upcase ()
"Upcase region or word."
(interactive)
(if (use-region-p)
(call-interactively 'upcase-region)
(call-interactively 'subword-upcase)))
(bind-key* "C-c @"
(defhydra hydra-change-case (:hint nil)
""
("x" xah-toggle-letter-case "loop")
("c" endless/capitalize "capitalize")
("l" endless/downcase "downcase")
("u" endless/upcase "upcase")
("z" undo-tree-undo "undo")
("Z" undo-tree-redo "redo")
("q" nil)))
;; use M-x list-processes then d to delete
(defalias 'lps 'list-processes)
(defun delete-process-at-point ()
(interactive)
(let ((process (get-text-property (point) 'tabulated-list-id)))
(cond ((and process
(processp process))
(delete-process process)
(revert-buffer))
(t
(error "no process at point!")))))
(bind-key "d" 'delete-process-at-point process-menu-mode-map)
;; Removing duplicated lines
;; Note that the last line should contain the EOF
(defun delete-duplicated-lines (beg end)
"Unique lines in region.
Called from a program, there are two arguments:
BEG and END (region to sort)."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(kill-line 1)
(yank)
(let ((next-line (point)))
(while
(re-search-forward
(format "^%s" (regexp-quote (car kill-ring))) nil t)
(replace-match "" nil nil))
(goto-char next-line))))))
(defalias 'ddl 'delete-duplicated-lines)
(defun duplicate-line-or-region (&optional n)
"Duplicate current line, or region if active.
With argument N, make N copies.
With negative N, comment out original line and use the absolute value."
(interactive "*p")
(let ((use-region (use-region-p)))
(save-excursion
(let ((text (if use-region ;Get region if active, otherwise line
(buffer-substring (region-beginning) (region-end))
(prog1 (thing-at-point 'line)
(end-of-line)
(if (< 0 (forward-line 1)) ;Go to beginning of next line,
;or make a new one
(newline))))))
(dotimes (i (abs (or n 1))) ;Insert N times, or once if not
;specified
(insert text))))
(if use-region nil ;Only if we're working with a line (not a region)
(let ((pos (- (point) (line-beginning-position)))) ;Save column
(if (> 0 n) ;Comment out original with negative arg
(comment-region (line-beginning-position) (line-end-position)))
(forward-line 1)
(forward-char pos)))))
(bind-key* "C-c d" 'duplicate-line-or-region)
;; convert DOS to UNIX
(defun dos2unix ()
"Not exactly but it's easier to remember"
(interactive)
(set-buffer-file-coding-system 'unix 't))
(defun copy-name ()
"Copy the name (NOT full path) of current buffer file to the clipboard."
(interactive)
(let* ((filename (file-name-nondirectory buffer-file-name)))
(when filename
(kill-new filename)
(message "'%s' name copied!" filename))))
(defun copy-path-short ()
"Copy the path of current buffer file to the clipboard."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
;; abbreviate-file-name will replace /home/user with ~
;; also works with directory
(if buffer-file-name
(abbreviate-file-name buffer-file-name)
(user-error "Current buffer is not a file/directory in disk!" "exit")))))
(when filename
(kill-new filename)
(message "'%s' path copied!" filename))))
(defun copy-path ()
"Copy the full path of current buffer file to the clipboard."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(if filename
(progn
(kill-new filename)
(message "'%s' path copied!" filename))
(message "Current buffer is not a file/directory in disk!"))))
(defun copy-path-html ()
"Copy the html of according to the current buffer, this is useful when exporting org file to html."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(if filename
(progn
(kill-new filename)
(message "'%s' path copied!" filename))
;; print error message and exit/return the function
(user-error "Current buffer is not a file in disk!" "exit")))
(let ((html-file (concat (file-name-sans-extension (buffer-file-name)) ".html")))
(if (file-exists-p html-file)
(progn
(kill-new html-file)
(message "'%s' path copied!" html-file))
(message "'%s' is not a file in disk!" html-file))))
(defun copy-path-pdf ()
"Copy the pdf of according to the current buffer, this is useful when exporting org file to html."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(if filename
(progn
(kill-new filename)
(message "'%s' path copied!" filename))
;; print error message and exit/return the function
(user-error "Current buffer is not a file in disk!" "exit")))
(let ((pdf-file (concat (file-name-sans-extension (buffer-file-name)) ".pdf")))
(if (file-exists-p pdf-file)
(progn
(kill-new pdf-file)
(message "'%s' path copied!" pdf-file))
(message "'%s' is not a file in disk!" pdf-file))))
(defun insert-date-or-time (ARG)
"Without prefix, print `2016-08-11'
With C-u, print `15:39:35'
With C--, print `2016-08-11 15:39:43'
With C-u C-u, print `Thu, 11. Aug 2016'"
(interactive "P")
(let ((format (cond
((not ARG) "%Y-%m-%d")
((equal ARG '-) "%Y-%m-%d %H:%M:%S")
((equal ARG '(4)) "%H:%M:%S")
((equal ARG '(16)) "%a, %d. %b %Y")))
(system-time-locale "en_US"))
(insert (format-time-string format))))
;; M-^ delete Up to Non-Whitespace Character, 'delete-indentation, combine two lines
;; M-Backspace delete to the previous word 'backword-kill-word
;; M-\ delete kill _all_ spaces at point 'delete-horizontal-space
;; Remove whitespaces around cursor to just one or none. If current line does
;; have visible characters: shrink whitespace around cursor to just one space.
;; If current line does not have visible chars, then shrink all neighboring