forked from c02y/dotemacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
2854 lines (2718 loc) · 103 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-2014 Cody Chan <cody.chan.cz@gmail.com>
;;
;; Author: Cody Chan <cody.chan.cz@gmail.com>
;; URL: https://github.com/c0dy/dotemacs.d
;; Keywords: convenience
;;
;; 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 "S-C-<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
;; C-c e to 'show-ws-toggle-show-trailing-whitespace
;; 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
;; S-C-<left> to 'shrink-window-horizontally
;; S-C-<right> to 'enlarge-window-horizontally
;; S-C-<down> to 'shrink-window
;; S-C-<up> to 'enlarge-window
;; 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-c a to 'align-regexp
;; 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
;;; Code:
;; (setq debug-on-error t)
;; make starup quicker
(setq gc-cons-threshold (* 100 1024 1024))
(let ((file-name-handler-alist nil)) "~/.emacs.d/init.elc")
;; proxy goagent
;; (setq url-proxy-services '(("http*" . "127.0.0.1:8087")))
;; Makes *scratch* empty.
;;(setq initial-scratch-message "")
;; encode, the last line will be the highest priority
(set-language-environment '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)
(require 'bind-key)
(add-to-list 'load-path "~/.emacs.d/lisp/")
(defalias 'man 'woman)
;; 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))
(bind-keys*
("C-c C-e" . (lambda () (interactive) (find-file "~/.emacs.d/init.el")))
("C-c C-u" . (lambda () (interactive) (find-file "/run/media/chz/UDISK/WORK-HOME")))
("C-c C-r" . (lambda () (interactive) (load-file "~/.emacs.d/init.elc")))
;; C-h e to switch to *Message* buffer
("C-x M-z" . (lambda () (interactive) (switch-to-buffer "*scratch*"))))
;; assembly
(add-to-list 'auto-mode-alist '("\\.\\(asm\\|s\\|S\\)$" . nasm-mode))
;; 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.
First split the current source code window in a given size if
no existed window contains *compilation* buffer.
With a prefix argument or no last time, this acts like M-x compile,
and you can reconfigure the compile args."
(interactive "p")
(if (not (get-buffer-window "*compilation*"))
(split-window-vertically -10))
(if (and (eq ARG 1) compilation-last-buffer)
(recompile)
(call-interactively 'compile)))
(bind-key* "C-x C-m" 'compile-again)
;; 1.
;; (defun compilation-exit-autoclose (STATUS code msg)
;; "Close the compilation window if there was no error at all."
;; (when (and (eq STATUS 'exit) (zerop code))
;; (bury-buffer)
;; (delete-window (get-buffer-window (get-buffer "*compilation*" t))))
;; (cons msg code))
;; (setq compilation-exit-message-function 'compilation-exit-autoclose)
;; 2.
(setq compilation-finish-functions
(lambda (buf str)
(if (null (string-match ".*exited abnormally.*" str))
;;no errors, make the compilation window go away in a few seconds
(progn
(run-at-time
"1 sec" nil 'delete-windows-on
(get-buffer-create "*compilation*"))
(message "No Compilation Errors!")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; Emacs Face Setting
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq bookmark-save-flag t)
(setq column-number-mode t)
(setq-default fill-column 80)
(add-hook 'prog-mode-hook 'highlight-beyond-fill-column)
(custom-set-faces '(highlight-beyond-fill-column-face
((t (:foreground "red" )))))
;; C-x </> 'scroll-left/right if line is too long
(put 'scroll-left 'disabled nil)
(setq comment-style 'extra-line)
;; 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 'font-lock-comment-face)
(tool-bar-mode 0)
(scroll-bar-mode 0)
(menu-bar-mode 0)
(bind-keys*
("C-S-m" . menu-bar-mode)
("C-S-l" . global-linum-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
(global-set-key (kbd "C-S-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))
(linum-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
(global-set-key (kbd "C-x C-s")
'(lambda ()
(interactive)
(progn
(if (file-writable-p buffer-file-name) (save-buffer)
(write-file (concat "/sudo:root@localhost:" buffer-file-name))))))
;; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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
;;
;; the following will get rid of prompt when M-x load-theme, treat all
;; themes as safe
(setq custom-safe-themes t)
;;
;; 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
(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)
;;
;; 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
;; use 120 char wide window for largish displays
;; and smaller 80 column windows for smaller displays
;; pick whatever numbers make sense for you
(if (> (x-display-pixel-width) 1500)
;; (add-to-list 'default-frame-alist (cons 'width 85))
;; (add-to-list 'default-frame-alist (cons 'width 85)))
(setq default-frame-alist
'((top . 0) (left . 0)
(width . 95) (height . 48)
;; or Monaco, Bitstream Vera Sans Mono, Liberation Mono
(font . "Input Mono Compressed-14")))
(setq default-frame-alist
'((top . 0) (left . 0)
(width . 85) (height . 37)
(font . "Input Mono Compressed-13.5")
;; (font . "PragmataPro-13")
;; (:family "Menlo-Italic")
)))))
;; 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")
)
;;
(set-frame-size-according-to-resolution)
;;
;; 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
(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")
;; repalce the 8 with other number to change the position
(let ((which-func '(which-func-mode ("" which-func-format " "))))
(setq-default mode-line-format
(remove which-func mode-line-format))
(setq-default mode-line-misc-info
(remove which-func mode-line-misc-info))
(setq cell (last mode-line-format 8)) ;; just next to buffer name
(setcdr cell (cons which-func (cdr cell))))
;;
;; 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)
;; mode-line color
(custom-set-faces
;; no special for which-func
'(which-func ((t (:background nil :foreground nil))))
'(mode-line ((t (:background "dim gray" :foreground "white"))))
'(mode-line-inactive ((t (:background nil))))
'(mode-line-buffer-id ((t (:foreground nil :background nil))))
)
;; 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)
" " mode-line-modes
mode-line-misc-info
"%-"))
(global-hl-line-mode 1)
(set-face-attribute hl-line-face nil :underline t)
(set-default 'cursor-type '(bar . 3))
;; 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)
(save-excursion
(goto-char (point-min))
(call-interactively 'query-replace)))
(bind-key* "M-%" 'query-replace-from-top)
;; flush blank lines
(defun flush-blank-lines (start end)
(interactive "r")
(flush-lines "^\\s-*$" start end nil))
(bind-keys*
("C-S-a" . beginning-of-visual-line)
("C-S-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-keys*
("S-M-<up>" . increment-region)
("S-M-<down>" . decrement-region))
;; 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)
;; You can do M-c/u/l the whole word in any position inside the word
(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 toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Toggles between: “all lower”, “Init Caps”, “ALL CAPS”.
Based on the comment of http://ergoemacs.org/emacs/modernization_upcase-word.html"
(interactive)
(let (p1 p2 (deactivate-mark nil) (case-fold-search nil))
(if (region-active-p)
(setq p1 (region-beginning) p2 (region-end))
(let ((bds (bounds-of-thing-at-point 'word)))
(setq p1 (car bds) p2 (cdr bds))))
(when (and p1 p2)
(when (not (eq last-command this-command))
(save-excursion
(goto-char p1)
(cond
((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps"))
((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps"))
((looking-at "[[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]]") (put this-command 'state "all caps"))
(t (put this-command 'state "all lower")))))
(cond
((string= "all lower" (get this-command 'state))
(upcase-initials-region p1 p2) (put this-command 'state "init caps"))
((string= "init caps" (get this-command 'state))
(upcase-region p1 p2) (put this-command 'state "all caps"))
((string= "all caps" (get this-command 'state))
(downcase-region p1 p2) (put this-command 'state "all lower"))))))
(bind-key* "C-x M-c" 'toggle-letter-case)
;; 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 'text-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)
;; convert from head of the word
(unless (looking-back "\\s-")
(backward-word))
(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)
(call-interactively 'capitalize-word))
(unless (eolp) (forward-char 1)))
(defun endless/downcase ()
"Downcase region or word.
Also converts full stops to commas."
(interactive)
;; convert from head of the word
(unless (looking-back "\\s-")
(backward-word))
(endless/convert-punctuation "\\." ",")
(if (use-region-p)
(call-interactively 'downcase-region)
;; (call-interactively 'subword-downcase)
(call-interactively 'downcase-word))
(unless (eolp) (forward-char 1)))
(defun endless/upcase ()
"Upcase region or word."
(interactive)
;; convert from head of the word
(unless (looking-back "\\s-")
(backward-word))
(if (use-region-p)
(call-interactively 'upcase-region)
;; (call-interactively 'subword-upcase)
(call-interactively 'upcase-word))
(unless (eolp) (forward-char 1)))
(bind-keys*
("M-c" . endless/capitalize)
("M-l" . endless/downcase)
("M-u" . endless/upcase))
;; 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 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))
;; Display trailing whitespace at end of lines
(defun toggle-trailing-whitespace-display ()
"Toggle the display of trailing whitespace, by changing the
buffer-local variable `show-trailing-whitespace'."
(interactive)
(save-excursion
(if show-trailing-whitespace
(setq show-trailing-whitespace nil)
(setq show-trailing-whitespace t))
(force-window-update (current-buffer)))
(message (concat "Display of EOL spaces "
(if show-trailing-whitespace
"enabled" "disabled"))))
(bind-key* "C-c e" 'show-ws-toggle-show-trailing-whitespace)
;; 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
(defun shrink-whitespaces ()
"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 blank lines to just one.
Repeat the function will remove the remaining one space or blank line.
If current line is a single space, remove that space.
`shrink-whitespaces` combine `delete-blank-lines`, `just-one-space`, `fixup-whitespace`,
`delete-horizontal-space`, and `cycle-spacing`(in emacs 24.4) into one.
--URL http://ergoemacs.org/emacs/emacs_shrink_whitespace.html version 2015-11-04"
(interactive)
(let ((pos (point))
line-has-char-p ; current line contains non-white space chars
has-space-tab-neighbor-p
whitespace-begin whitespace-end
space-or-tab-begin space-or-tab-end)
(save-excursion
(setq has-space-tab-neighbor-p (if (or (looking-at " \\|\t") (looking-back " \\|\t")) t nil))
(beginning-of-line)
(setq line-has-char-p (search-forward-regexp "[[:graph:]]" (line-end-position) t))
(goto-char pos)
(skip-chars-backward "\t ")
(setq space-or-tab-begin (point))
(skip-chars-backward "\t \n")
(setq whitespace-begin (point))
(goto-char pos)
(skip-chars-forward "\t ")
(setq space-or-tab-end (point))
(skip-chars-forward "\t \n")
(setq whitespace-end (point)))
(if line-has-char-p
(if has-space-tab-neighbor-p
(let (deleted-text)
;; remove all whitespaces in the range
(setq deleted-text
(delete-and-extract-region space-or-tab-begin space-or-tab-end))
;; insert a whitespace only if we have removed something different than a simple whitespace
(when (not (string= deleted-text " "))
(insert " ")))
(progn
(when (equal (char-before) 10) (delete-char -1))
(when (equal (char-after) 10) (delete-char 1))))
(progn (delete-blank-lines)))))
(bind-key* "C-<backspace>" 'shrink-whitespaces)
;;;
;; delete not kill it into kill-ring
;; http://ergoemacs.org/emacs/emacs_kill-ring.html
(defun delete-word (arg)
"Delete(not kill) characters forward until encountering the end of the syntax-subword.
With argument, do this many times."
(interactive "p")
;; eolpp is about the end-of-line, because if you are at the eol and doing
;; M-Backspace, if will combine the following line with the current
(let (eolpp) (setq eolpp 1))
(setq eolpp (if (eolp) 1 nil))
(shrink-whitespaces)
(delete-region (point) (progn (syntax-subword-forward-syntax arg) (point)))
(if eolpp (if (not (eolp)) (open-line 1))))
(defun delete-word-backward (arg)
"Delete(not kill) characters backward until encountering the beginning of the syntax-subword.
With argument, do this that many times."
(interactive "p")
(delete-word (- arg)))
(defun delete-line (arg)
"Delete text from current position to end of line char.
With argument, forward ARG lines."
(interactive "p")
(let (x1 x2)
(setq x1 (point))
(if (eolp) (forward-line arg) (forward-line (- arg 1)))
(move-end-of-line 1)
(setq x2 (point))
(delete-region x1 x2))
(when (bolp) (delete-char 1)))
(defun delete-line-backward (arg)
"Delete text between the beginning of the line to the cursor position.
With argument, backward ARG lines."
(interactive "p")
(let (x1 x2)
(setq x1 (point))
(if (bolp) (forward-line (- arg)) (forward-line (- 1 arg)))
(move-beginning-of-line 1)
(setq x2 (point))
(delete-region x1 x2)))
(bind-keys*
("M-d" . delete-word)
("<M-backspace>" . delete-word-backward)
("C-k" . delete-line)
("C-S-k" . delete-line-backward))
(defun toggle-fill-paragraph ()
;; Based on http://xahlee.org/emacs/modernization_fill-paragraph.html
"Fill or unfill the current paragraph, depending upon the current line length.
When there is a text selection, act on the region.
See `fill-paragraph' and `fill-region'."
(interactive)
;; We set a property 'currently-filled-p on this command's symbol
;; (i.e. on 'toggle-fill-paragraph), thus avoiding the need to
;; create a variable for remembering the current fill state.
(save-excursion
(let* ((deactivate-mark nil)
(line-length (- (line-end-position) (line-beginning-position)))
(currently-filled (if (eq last-command this-command)
(get this-command 'currently-filled-p)
(< line-length fill-column)))
(fill-column (if currently-filled
most-positive-fixnum
fill-column)))
(if (region-active-p)
(fill-region (region-beginning) (region-end))
(fill-paragraph))
(put this-command 'currently-filled-p (not currently-filled)))))
(bind-key* "M-q" 'toggle-fill-paragraph)
;; C-c e to 'show-ws-toggle-show-trailing-whitespace
(defun cleanup-buffer ()
"Cleanup the buffer:
1. yafolding-show-all to avoid date loss
2. delete-trailing-whitespace
"
(interactive)
(yafolding-show-all) ;; avoid data loss
(delete-trailing-whitespace))
(bind-key* "C-c d" 'cleanup-buffer)
(defvar all-make-modes
'(makefile-makepp-mode makefile-bsdmake-mode makefile-imake-mode
makefile-automake-mode makefile-mode makefile-gmake-mode)
"A list of the makefile major modes")
(defun indent-buffer-safe ()
"Indent the whole buffer unless it is a Makefile,
Emacs by default won't treat the TAB as indent"
(interactive)
;; indent the whole buffer but not Makefile because of must TAB
(when (and (derived-mode-p 'prog-mode 'web-mode)
(not (member major-mode all-make-modes)))
(indent-region (point-min) (point-max))))
(bind-key* "C-c C-w" 'indent-buffer-safe)
(add-hook 'before-save-hook
(lambda ()
(cleanup-buffer)
(indent-buffer-safe)))
;; indent marked files in dirs
;; C-u C-x d dir --> -lsR --> * / --> * t (then unmark the files no needed)
;; --> M-x indent-marked-files
;; C-M-\ 'indent-region(mark first)
(defun indent-marked-files ()
(interactive)
(dolist (file (dired-get-marked-files))
(find-file file)
(indent-region (point-min) (point-max))
(save-buffer)
(kill-buffer nil)))
;; C-x k to kill a buffer specified
(bind-key* "C-S-d" 'kill-this-buffer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; minibuffer & buffers & dired
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; dired
;;
;; Rename files in a directory, the permission can also be changed
;; 1. C-x C-j 'dired-jump goto that dir
;; 2. C-x j 'wdired-change-to-wdired-mode, change it to editable mode, Start rename by editing
;; 3. C-c C-c 'wdired-finish-edit, Commit changes
;; 4. C-c ESC 'wdired-abort-changes, Abort changes
;; 5. you can use M-% 'query-replace or C-M-% 'query-replace-regexp
;; Actually, when you're in dired-mode, you can use C-x C-q
;; 'dired-toggle-read-only to make the dir editable/un-editable
;; same effect as wdired-change-to-wdired-mode, but the latter cannot disable the editable-mode
;; (even if you didn't change a thing)until you save your change or abort.
;; (global-set-key (kbd "C-x j") 'wdired-change-to-wdired-mode)
;; sort dirs first in dired-mode
(defun dired-sort-dirs-first ()
"Dired sort hook to list directories first."
(save-excursion
(let (buffer-read-only)
(forward-line 2) ;; beyond dir. header
(sort-regexp-fields t "^.*$" "[ ]*." (point) (point-max))))
(and (featurep 'xemacs)
(fboundp 'dired-insert-set-properties)
(dired-insert-set-properties (point-min) (point-max)))
(set-buffer-modified-p nil))
(add-hook 'dired-after-readin-hook 'dired-sort-dirs-first)
;; change the format of the files(dirs)
(setq dired-listing-switches "-Al --time-style long-iso")
;; in dired, hide hidden files by default, toggle them using `C-x M-o`
(require 'dired-x)
(defun dired-get-size ()
"Get the size of a directory or a series of marked files and directories."
(interactive)
(let ((files (dired-get-marked-files)))
(with-temp-buffer
(apply 'call-process "/usr/bin/du" nil t nil "-sch" files)
(message
"Size of all marked files: %s"
(progn