-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot-emacs
1094 lines (855 loc) · 36.9 KB
/
dot-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
; -*- emacs-lisp -*-
;
; Tips:
;
; - Check Comment style at https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html
; 1 semicolon - same line, after code
; 2 semicolons - the following line
; 3 semicolons - the following block
; 4 semicolons - major section
;
; - On Mac OS, use Homebrew Emacs package to replace the built-in version.
;
;;;; ---------------------------------------------------------------------------
;;;; Packages
;;;; ---------------------------------------------------------------------------
;;; Init package
(if (> emacs-major-version 24) ; be compatible with old emacs
(progn
(require 'package)
(package-initialize)
(setq package-check-signature nil) ; workaround intermittent public key failure
(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/"))
))
;; Fixed in emacs 26
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
;; Instructions
;;
;; (package-refresh-contents)
;;
;; (package-install 'markdown-mode)
;; (package-install 'protobuf-mode)
;; (package-install 'auto-complete)
;; (package-install 'git-gutter)
;;
;; Remarks:
;; 1. Delete ~/.emacs.d/elpa if the installed package is outdated.
;; 2. 'package-install' modifies .emacs automatically.
;; Revert its change and set 'package-selected-packges' in .emacs manually.
;;;; ---------------------------------------------------------------------------
;;;; Environments
;;;; ---------------------------------------------------------------------------
;;; Add emacs-goodies to load-path
(if (eq system-type 'windows-nt)
(setq emacs-goodies-path "C:/msys64/home/Justin/emacs-goodies"))
(if (eq system-type 'gnu/linux)
(setq emacs-goodies-path
(replace-regexp-in-string "\n$" ""
(shell-command-to-string
"readlink -f ~/.emacs | xargs dirname"))))
(if (eq system-type 'darwin)
(setq emacs-goodies-path
(replace-regexp-in-string "\n$" ""
;; OS X readlink doesn't suport -f flag.
(shell-command-to-string "(cd $(readlink -n ~/.emacs | xargs dirname); pwd)"))))
(add-to-list 'load-path emacs-goodies-path)
;;; Language environment
(set-language-environment 'UTF-8)
;;;; ---------------------------------------------------------------------------
;;;; Basic settings
;;;; ---------------------------------------------------------------------------
;; Disable backup and autosave
(setq make-backup-files nil)
(setq auto-save-default nil)
;; Don't ask on symbolic link to git controlled file
(setq vc-follow-symlinks nil)
(setq large-file-warning-threshold 300000000)
;; allow y/n instead of yes/no on prompts
(fset 'yes-or-no-p 'y-or-n-p)
;; show parenthesis
(show-paren-mode 1)
;; only want to split vertically.
(setq split-width-threshold nil)
(setq split-height-threshold 50)
;; don't ring
(setq ring-bell-function 'ignore)
;; truncate lines
(set-default 'truncate-lines t)
;; Narrow-to-region C-x n n and C-x n w
(put 'narrow-to-region 'disabled nil)
(setq column-number-mode t)
(setq fill-column 79)
;; Disable scrollbar, menubar, toolbar
(menu-bar-mode -1)
(if window-system
(progn
(set-scroll-bar-mode nil)
(tool-bar-mode -1))
(progn
(xterm-mouse-mode 1)))
;; Don't indent with tab
(setq default-tab-width 2) ; Google C++ Style
(setq-default indent-tabs-mode nil)
;;;; ---------------------------------------------------------------------------
;;;; Programming Major Modes
;;;; ---------------------------------------------------------------------------
;;; [C Mode]
;; This applies to C, C++, Objective-C, and Java
(add-hook 'c-mode-common-hook
'(lambda ()
(subword-mode 1) ; Google C++ style uses MixedCase
(outline-minor-mode)
(setq show-trailing-whitespace t)
(c-toggle-auto-hungry-state -1)
(c-toggle-auto-newline -1)
(c-toggle-electric-state t)
;; ff-find-other-file will read #include lines, use
;; ff-get-other-file instead.
(local-set-key (kbd "M-h") 'ff-get-other-file) ;; be compatible with ST3
(setq c-tab-always-indent t)
;; C++ style
(setq comment-start "//" comment-end "")
(local-set-key [(return)] 'newline-and-indent)
(set (make-local-variable 'cc-other-file-alist)
'(("\\.m\\'" (".h"))
("\\.mm\\'" (".h"))
("\\.h\\'" (".m" ".mm" ".c" ".cpp" ".cc"))
("\\.c\\'" (".h"))
("\\.cpp\\'" (".h" ".hxx" ".hpp"))
("\\.cc\\'" (".h" ".hxx" ".hpp"))
("\\.cxx\\'" (".h" ".hxx"))
("\\.hxx\\'" (".cc" ".cxx" ".cpp"))
("\\.hpp\\'" (".cc" ".cxx" ".cpp"))
))))
;; Note: if this fails, it's usually caused by bad `emacs-goodies-path` as `load-path`
(require 'google-c-style)
(require 'google-pastebin)
;; Set style for special projects.
(add-hook 'c-mode-common-hook
'(lambda ()
;; Android (interfere with Java code)
;; (if (my-is-android-file-p)
;; (progn
;; (setq c-basic-offset 2)
;; (setq-local fill-column 80)
;; (setq-local whitespace-line-column 80)))
(google-set-c-style)
(if (my-is-linux-file-p)
(progn
(c-set-style "linux")
(subword-mode 0)
(setq comment-start "/*" comment-end "*/")
(setq indent-tabs-mode t)))))
;;; Mojom
(add-to-list 'auto-mode-alist '("\\.mojom$" . c++-mode))
;;; [Protobuf Mode]
(add-to-list 'auto-mode-alist '("\\.pbtxt$" . protobuf-mode))
;;; [Python Mode]
(add-hook 'python-mode-hook
(lambda ()
(outline-minor-mode)
(subword-mode) ; Python uses MixedCase
(setq show-trailing-whitespace t)
(setq tab-width 2) ; python-mode overrides tab-width to 8
(setq-local fill-column 79)
(setq python-indent-offset 2)))
;; Bazel
(add-to-list 'auto-mode-alist '("BUILD$" . python-mode))
(add-to-list 'auto-mode-alist '("WORKSPACE$" . python-mode))
;;; [Java Mode]
(add-hook 'java-mode-hook
(lambda ()
(setq-local fill-column 100)))
;;; [Obj-C Mode]
(add-to-list 'auto-mode-alist '("\\.mm$" . objc-mode))
(add-hook 'objc-mode-hook
'(lambda ()
(subword-mode 1) ; Obj-C uses MixedCase.
;; People don't wrap lines in Obj-C files.
(setq truncate-lines nil)))
;;; [Swift Mode]
(when (require 'swift-mode nil 'noerror)
(add-hook 'swift-mode-hook
'(lambda ()
(subword-mode 1) ; Swift uses MixedCase.
;; People don't wrap lines in Swift.
(setq truncate-lines nil))))
;;; [Go Mode]
;; See http://tleyden.github.io/blog/2014/05/22/configure-emacs-as-a-go-editor-from-scratch/
(when (require 'go-mode nil 'noerror)
(progn
(if (> emacs-major-version 24)
;; It's standard practice to run gofmt when saving.
(add-hook 'before-save-hook 'gofmt-before-save))
(add-hook 'go-mode-hook
'(lambda ()
(subword-mode 1) ; Go uses MixedCase.
(setq truncate-lines nil) ; no line wrapping in Go
))))
;;; [CUDA - C++ Mode]
(add-to-list 'auto-mode-alist '("\\.cu$" . c++-mode))
;;; [GN - Python Mode]
(add-to-list 'auto-mode-alist '("\\.gn$" . python-mode))
;;; [JavaScript - JS2 Mode]
(when (require 'js2-mode nil 'noerror)
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
(add-hook 'js2-mode-hook
(lambda ()
;; Scan the file for nested code blocks
(setq js2-basic-offset 2)
(subword-mode 1)
(setq truncate-lines nil))))
;;; [HTML - Web Mode]
(when (require 'web-mode nil 'noerror)
(add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
(add-hook 'web-mode-hook
'(lambda ()
(setq truncate-lines nil)
(setq web-mode-enable-auto-indentation nil))))
;;; [CSS Mode]
;; Support .less file.
(when (require 'css-mode nil t)
(add-to-list 'auto-mode-alist '("\\.less$" . css-mode)))
(add-hook 'css-mode-hook
(lambda ()
(setq css-indent-offset 4)))
;;; [SGML Mode]
(require 'sgml-mode)
(define-key sgml-mode-map "\\r" 'newline-and-ident)
;;;; ---------------------------------------------------------------------------
;;;; Non-Programming Major Modes
;;;; ---------------------------------------------------------------------------
;;; [Outline Mode]
(add-hook 'outline-mode-hook '(lambda ()
;; Collapse subnodes when opening outline file
(hide-sublevels 1)))
;;; [Markdown Mode]
(when (require 'markdown-mode nil t)
(add-hook 'markdown-mode-hook
(lambda ()
(flyspell-mode)
(outline-minor-mode)
(setq truncate-lines nil)
;; Markdown mode override these keys. Set it back.
(local-set-key "\M-p" '(lambda () (interactive) (previous-line 3)))
(local-set-key "\M-n" '(lambda () (interactive) (next-line 3)))
)))
;;; [Rebase Mode]
;; Disable git-rebase mode.
(setq auto-mode-alist (delete '("git-rebase-todo" . rebase-mode)
auto-mode-alist))
;;; [Dired Mode]
(require 'dired-x)
;; no uid, no gid, no . ..
(setq dired-listing-switches "-AgGh")
;; Use C-x M-o to toggle.
(setq-default dired-omit-mode t) ; this is buffer-local variable
;; Ignore dot files and system files on Mac.
(setq dired-omit-files
(concat dired-omit-files
"\\|^\\..+$\\|desktop.ini\\|__pycache__\\|.DS_Store\\|.git\\|Thumbs.db\\|Icon\015"))
;;; [Occur Mode]
;; Bind p, n keys to be consistent with grep mode.
(add-hook 'occur-mode-hook
'(lambda ()
(define-key occur-mode-map "p" '(lambda () (interactive)
(occur-prev)
(occur-mode-goto-occurrence-other-window)
(other-window 1)))
(define-key occur-mode-map "n" '(lambda () (interactive)
(occur-next)
(occur-mode-goto-occurrence-other-window)
(other-window 1)))))
;;; [Hexl Mode]
(setq hexl-bits 8)
;;;; ---------------------------------------------------------------------------
;;;; Minor Modes
;;;; ---------------------------------------------------------------------------
;;; [Auto Complete Mode]
;; Enable global auto-complete mode
(if (> emacs-major-version 24) ; be compatible with old emacs
(when (require 'auto-complete nil 'noerror)
(progn
(setq package-selected-packages (quote (auto-complete)))
(ac-config-default)
(global-auto-complete-mode t))))
;;; [Git Gutter Mode]
;; (when (require 'git-gutter nil t)
;; (global-git-gutter-mode +1))
;;; [Which Function Mode]
(which-function-mode 1)
;; It has performance penalty in Python.
(setq which-func-modes '(python-mode c-mode c++-mode js2-mode java-mode go-mode))
;;; [Outline Minor Mode]
;; Use C-c C-c as prefix (We use C-c as key prefix in Outline Major Mode)
(add-hook 'outline-minor-mode-hook
(lambda () (local-set-key "\C-c\C-c"
outline-mode-prefix-map)))
;;; [VC mode]
;; Disable vc mode (which is unnecessarily complex)
;; (require 'vc)
;; (setq vc-handled-backends ())
;;; [Whitespace Minor Mode]
;; whitespace mode seems broken
;; (require 'whitespace)
;; (setq-default whitespace-style
;; '(trailing tabs lines-tail))
;; (setq whitespace-line-column nil) ;; use fill-column instead
;; (setq whitespace-display-mappings
;; '((space-mark ?\ [?\xB7] [?.]) ; space
;; (space-mark ?\xA0 [?\xA4] [?_]) ; hard space
;; ))
;;;; ---------------------------------------------------------------------------
;;;; Misc
;;;; ---------------------------------------------------------------------------
;;; [imenu]
;; (require 'imenu)
;; (setq imenu-auto-rescan nil) ; speedup
;;; [Uniquify]
;; better renaming of buffers viewing files with same name in different
;; directories.
(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward)
;;; [Recentf]
(require 'recentf)
(recentf-mode t)
(setq recentf-max-menu-items 1000) ; with ido mode, we can leverage more items.
;;; [Mini-buffer]
;; Makes C-p/n work like Up/Down.
(define-key minibuffer-local-map (kbd "C-p") 'previous-line-or-history-element)
(define-key minibuffer-local-map (kbd "C-n") 'next-line-or-history-element)
;;;; ---------------------------------------------------------------------------
;;;; ido
;;;; ---------------------------------------------------------------------------
;;; Enable ido mode everywhere.
(require 'ido)
(ido-mode 'both)
(ido-everywhere 'both)
;; disabled for being buggy
;; (setq ido-enable-flex-matching t)
;;; Allow opening folder.
(setq ido-show-dot-for-dired t)
;;; Open in current window
;; Be consistent with standard Emacs convention
;; (especially when Emacs has multiple frames in different workspaces).
(setq ido-default-file-method 'selected-window)
(setq ido-default-buffer-method 'selected-window)
;;; Display vertically
(setq ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")))
(add-hook 'ido-setup-hook
(lambda ()
(define-key ido-completion-map [up] 'ido-prev-match)
(define-key ido-completion-map (kbd "C-p") 'ido-prev-match)
(define-key ido-completion-map [down] 'ido-next-match)
(define-key ido-completion-map (kbd "C-n") 'ido-next-match)))
;;;; ---------------------------------------------------------------------------
;;;; Ido find files in source and recentf
;;;; ---------------------------------------------------------------------------
(require 'find-file)
(setq my-source-list '())
(defun my-scan-source ()
"Build input for my-ido-find-file-in-sources"
(interactive)
(setq my-source-list (split-string (shell-command-to-string (concat
"find $(pwd) ! -path '*/.*/*' -and "
" \\( -name '*.cc' "
" -or -name '*.[ch]' "
" -or -name '*.hpp' "
" -or -name '*.cpp' "
" -or -name '*.java' "
" -or -name '*.py' "
" -or -name '*.mojom' "
" -or -name '*.proto' "
" -or -name '*.lua' "
" -or -name '*.mm' \\) ")))))
(defun my-ido-find-file-in-source ()
(interactive)
(let ((initial-input (thing-at-point 'filename)))
(find-file
(expand-file-name
(ido-completing-read
"Project file: " my-source-list nil nil initial-input)))))
(defun my-ido-recent-file ()
"Find a recent file using Ido."
(interactive)
(let ((file
(ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file
(find-file file))))
;;;; ---------------------------------------------------------------------------
;;;; Code Search Integration
;;;; ---------------------------------------------------------------------------
(defun my-is-linux-file-p ()
(let ((result nil))
(dolist (pattern (list "kernel"))
(when (and buffer-file-name
(string-match pattern buffer-file-name))
(setq result t)))
result))
(defun my-is-android-file-p ()
(let ((result nil))
(dolist (pattern (list "droid" ))
(when (and buffer-file-name
(string-match pattern buffer-file-name))
(setq result t)))
result))
(setq browse-url-browser-function 'browse-url-generic
browse-url-generic-program "google-chrome"
browse-url-generic-args '("--disable-gpu"))
(setq my-chrome-workspace-index 9) ;; Where the workspace Chrome lives
;; 'filename' type is better than 'symbol' type in thing-at-point when
;; extracting C++ symbol because it's easy to customize, and it includes colon
;; char (:).
;; Hacky way to override default behavior.
(setq thing-at-point-file-name-chars "[:alnum:]_.") ; no ':'
(setq my-linux-code-search-pattern "http://lxr.free-electrons.com/ident?v=3.14&i=%s")
(setq my-android-code-search-pattern
"https://www.google.com.tw/webhp?ion=1&espv=2&ie=UTF-8#q=aosp %s")
(setq my-default-code-search-pattern
"https://code.google.com/p/chromium/codesearch#search/&q=%s&sq=package:chromium&type=cs")
(defun my-switch-workspace-chrome ()
(interactive)
;; seems wmctrl is no longer needed.
;;(shell-command (format "wmctrl -s %d" my-chrome-workspace-index)))
)
(defun my-code-search (keyword)
"Search keyword in Chromium code search"
(interactive (list
(read-string "Search: " (thing-at-point 'filename) nil nil)))
(browse-url
(format
(cond ((my-is-linux-file-p) my-linux-code-search-pattern)
((my-is-android-file-p) my-android-code-search-pattern)
(t my-default-code-search-pattern))
keyword))
(my-switch-workspace-chrome))
(defun my-stl-doc (stl)
(interactive (list
(read-string "STL: " "" nil nil)))
(browse-url (format
"http://www.cplusplus.com/%s" stl))
(my-switch-workspace-chrome))
;;;; ---------------------------------------------------------------------------
;;;; Functions for simple text editing
;;;; ---------------------------------------------------------------------------
; Insert <a href>
(defun my-insert-ahref-tag ()
(interactive)
(insert "<a href=\"\"></a>")
(backward-char 6))
(defun my-kernel-style ()
"Use buffer-local linux kernel style"
(interactive)
(c-set-style "linux")
(setq indent-tabs-mode t))
(defalias 'my-linux-style 'my-kernel-style)
; Insert current date
(defun my-insert-date ()
(interactive)
(insert (format-time-string "%Y-%m-%d")))
; Insert current time
(defun my-insert-time ()
(interactive)
(insert (format-time-string "%Y-%m-%d %H:%M")))
(defun make-repeated-string (x n)
(apply #'concat
(cl-loop repeat n
collect x)))
(defun my-insert-outline-date-header ()
(interactive)
(insert
(format-time-string "* %B %d, ")))
(defun my-insert-space-date-width ()
(interactive)
(insert
(make-repeated-string " " (length (format-time-string " %B 01, ")))))
(defun my-insert-outline-modeline ()
(interactive)
(insert "-*- mode: outline; eval: (auto-fill-mode 0) ; eval: (visual-line-mode) -*-"))
(defun my-disable-paste-indent ()
(interactive)
(electric-indent-mode -1))
(defun my-kill-start-of-line ()
"kill from point to start of line"
(interactive)
(kill-line 0))
(defun my-indent ()
"indent to right 2 spaces"
(interactive)
(indent-rigidly (region-beginning) (region-end) 2))
(defun my-unindent ()
"indent to left 2 spaces"
(interactive)
(indent-rigidly (region-beginning) (region-end) -2))
(defun my-indent-buffer ()
(interactive)
(indent-region (point-min) (point-max)))
(defun my-reload-file ()
(interactive)
;; Only prompt if buffer is modified.
(revert-buffer t (not (buffer-modified-p)) t))
;; Copied from
;; http://stackoverflow.com/questions/9688748/emacs-comment-uncomment-current-line
(defun my-comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)
(next-line)))
;;;; ---------------------------------------------------------------------------
;;;; Functions for more tools
;;;; ---------------------------------------------------------------------------
(defun my-clear-recentf ()
(interactive)
(delete-file "~/.emacs.d/recentf")
(setq recentf-list ()))
(defun my-grep (command)
"look for string at point in files recursively starting from the work directory"
(interactive (list
(read-string "Run: "
(format
(concat
;; Manual modify from -name '*' to, for example, '*.py' in minibuffer
;; to narrow down file pattern.
;; Ignore hidden files (ex: .git, .static .vscode .env)
"find . -name '*' ! -path '*/.*/*' ! -path '*/models*/*' ! -path '*/build/*' "
;; Ignore program artifacts
" ! -name '*.sqlite3' ! -name '*.pyc' ! -name '*.o' "
;; Ignore editor output.
" ! -name '*.sublime-*' ! -name 'TAGS' "
" ! -name '*.so*' ! -name '*.a' ! -name '*.mo' "
" -type f -print0 | "
"xargs -0 fgrep -inH -e %s")
(thing-at-point 'filename))
nil
nil)))
(grep command))
(defun my-count-lines ()
"Count lines of C++ and Python code"
(interactive)
;;; exclude 'static' folder because we usually collect static files there.
(shell-command (concat "find . -type f -and ! -path '*/.*/*' -and ! -path '*/vendor/*' -and "
" \\( -name '*.proto' "
" -or -name '*.cc' "
" -or -name '*.[ch]' -or -name '*.cpp' "
" -or -name '*.sh' "
" -or -name 'BUILD' "
" -or -name '*.textproto' "
" -or -name '*.kt' "
" -or -name '*.lua' "
" -or -name '*.java' -or -name '*.py' "
" -or -name '*.php' -or -name '*.rb' "
" -or -name '*.less' -or -name '*.html' -or -name '*.js' "
" -or -name '*.vue' "
" -or -name '*.mm' -or -name '*.m' -or -name '*.swift' "
" -or -name '*.go' "
" -or -name '*.mojom' "
" \\) -print0 | xargs -0 cat | wc -l")))
(defun my-occur (word)
"look for word at point in files ending by .cpp and .h
recursively starting from the work directory"
(interactive (list
(read-string (format "occur (%s): " (thing-at-point 'filename))
(thing-at-point 'filename)
nil
(thing-at-point 'filename))))
(occur word))
;; Copied from https://code.google.com/p/chromium/wiki/Emacs
(defun my-summarize-indentation-at-point ()
"Echo a summary of how one gets from the left-most column to
POINT in terms of indentation changes."
(interactive)
(save-excursion
(let ((cur-indent most-positive-fixnum)
(trace '()))
(while (not (bobp))
(let ((current-line (buffer-substring (line-beginning-position)
(line-end-position))))
(when (and (not (string-match "^\\s-*$" current-line))
(< (current-indentation) cur-indent))
(setq cur-indent (current-indentation))
(setq trace (cons current-line trace))
(if (or (string-match "^\\s-*}" current-line)
(string-match "^\\s-*else " current-line)
(string-match "^\\s-*elif " current-line))
(setq cur-indent (1+ cur-indent)))))
(forward-line -1))
(message "%s" (mapconcat 'identity trace "\n")))))
(defun my-git-show (sha)
"git show in diff mode"
(interactive (list
(read-string (format "git-show (%s): " (thing-at-point 'filename))
(thing-at-point 'filename)
nil
(thing-at-point 'filename))))
(shell-command (concat "git show " sha)))
;;;; ---------------------------------------------------------------------------
;;;; Key bindings for window management
;;;; ---------------------------------------------------------------------------
;;; Mac style window management on X11
(global-set-key (kbd "<C-s-268632070>") 'my-maximize-frame)
(global-set-key (kbd "s-n") 'make-frame)
(global-set-key (kbd "s-w") 'delete-frame)
;;;; ---------------------------------------------------------------------------
;;;; Key bindings for mouse
;;;; ---------------------------------------------------------------------------
;;; Xterm mouse mode
(global-set-key "\C-cx" 'xterm-mouse-mode)
;; Mac Terminal triggers mouse-4/mouse-5 events.
(global-set-key (kbd "<mouse-4>") 'scroll-down-command)
(global-set-key (kbd "<mouse-5>") 'scroll-up-command)
;;;; ---------------------------------------------------------------------------
;;;; Key bindings for standard text editing
;;;; ---------------------------------------------------------------------------
;;; Mac style text editing on Mac
(global-set-key (kbd "s-z") 'undo)
(global-set-key (kbd "s-x") 'kill-region)
(global-set-key (kbd "s-c") 'kill-ring-save)
(global-set-key (kbd "s-v") 'yank)
(global-set-key (kbd "s-a") 'mark-whole-buffer)
(global-set-key (kbd "s-/") 'my-comment-or-uncomment-region-or-line) ; Xcode style
(global-set-key (quote [s-up]) 'beginning-of-buffer)
(global-set-key (quote [s-down]) 'end-of-buffer)
(global-set-key (quote [s-left]) 'move-beginning-of-line)
(global-set-key (quote [s-right]) 'move-end-of-line)
(global-set-key (quote [s-backspace]) 'my-kill-start-of-line)
;;; Mac style Option+Left and Option+Right on Terminal and X11
(global-set-key "\M-[1;3C" 'forward-word)
(global-set-key "\M-[1;3D" 'backward-word)
;;; Linux style (Ctrl+Shift+C/V for copy/paste in Gnome Terminal)
(global-set-key (kbd "C-S-c") 'kill-ring-save)
(global-set-key (kbd "C-S-v") 'yank)
;;;; ---------------------------------------------------------------------------
;;;; Key bindings for custom text editing
;;;; ---------------------------------------------------------------------------
;;; My favorite way to jump 3 lines at a time.
(global-set-key "\M-p" '(lambda () (interactive) (previous-line 3)))
(global-set-key "\M-n" '(lambda () (interactive) (next-line 3)))
;;; Select all
(global-set-key "\C-ca" 'mark-whole-buffer)
;;; Comment
(global-set-key "\C-c;" 'my-comment-or-uncomment-region-or-line)
(global-set-key "\C-c/" 'my-comment-or-uncomment-region-or-line)
;;; Indent region
(global-set-key "\C-c>" 'my-indent)
(global-set-key "\C-c<" 'my-unindent)
;;; My shortcut for outline mode
(global-set-key "\C-ch" 'my-insert-outline-date-header)
(global-set-key (kbd "C-c SPC") 'my-insert-space-date-width)
;;; Insert text
;; Insert tab character (C-c TAB)
(global-set-key "\C-c\C-i"
'(lambda nil nil (interactive) (insert-tab)))
;; Insert boilerplates
;; (global-set-key "\C-ch" 'my-insert-ahref-tag)
;;;; ---------------------------------------------------------------------------
;;;; Key binding for split window
;;;; ---------------------------------------------------------------------------
;; This is convenient. But hard to implement on VS Code. Disable it first.
;; (global-set-key "\C-x[" '(lambda () (interactive) (other-window -1)))
;; (global-set-key "\C-x]" '(lambda () (interactive) (other-window 1)))
;; Use for indentation
;; (global-set-key (kbd "s-[") '(lambda () (interactive) (other-window -1)))
;; (global-set-key (kbd "s-]") '(lambda () (interactive) (other-window 1)))
(global-set-key (kbd "s-0") 'delete-window)
(global-set-key (kbd "s-1") 'delete-other-windows)
(global-set-key (kbd "s-2") 'split-window-below)
(global-set-key (kbd "s-3") 'split-window-right)
(global-set-key (kbd "s-w") 'kill-this-buffer)
(global-set-key (kbd "<C-tab>") 'ido-switch-buffer)
(global-set-key (kbd "s-.") '(lambda () (interactive) (find-file ".")))
;;;; ---------------------------------------------------------------------------
;;;; Key binding for code navigation
;;;; ---------------------------------------------------------------------------
;;; Seach in local sources
(global-set-key "\C-cb" 'my-summarize-indentation-at-point) ; back track
(global-set-key "\C-cg" 'my-grep)
(global-set-key "\C-co" 'my-occur)
;;; ido for imenu
(require 'idomenu)
(global-set-key "\C-ci" 'idomenu)
;;; Search on cloud
(global-set-key "\C-cS" 'my-stl-doc)
;;;; ---------------------------------------------------------------------------
;;;; Key bindings to file opening
;;;; ---------------------------------------------------------------------------
;;; Ido mode
(global-set-key "\C-cs" 'my-scan-source)
(global-set-key "\C-cf" 'my-ido-find-file-in-source)
(global-set-key "\C-cr" 'my-ido-recent-file)
;;; Misc
(global-set-key "\C-c\C-r" 'my-reload-file)
(add-hook 'sh-mode-hook
(lambda ()
;; sh-mode overrides C-c C-r. Set it back to my preference.
(local-set-key "\C-c\C-r" 'my-reload-file)))
;; Open the current folder. It's very commonly used. (equivalent to C-x C-f .)
(global-set-key "\C-x." '(lambda () (interactive) (find-file ".")))
;;;; ---------------------------------------------------------------------------
;;;; Key bindings to launch applications
;;;; ---------------------------------------------------------------------------
(global-set-key "\C-c!" 'shell-command) ; M-! doesn't work in Chromium ssh
(global-set-key "\C-cm" 'manual-entry)
(global-set-key "\C-cc" 'calculator)
(global-set-key "\C-cd" 'calendar)
(global-set-key "\C-ct" '(lambda () (interactive) (ansi-term "/bin/bash" "Term")))
(global-set-key "\C-cH" '(lambda () (interactive) (shell-command "hostname")))
;;;; ---------------------------------------------------------------------------
;;;; rename tmux window name with buffer name
;;;; ---------------------------------------------------------------------------
(defun inside-tmux-p ()
"Return non-nil if Emacs is running inside a tmux session."
(getenv "TMUX"))
(defun my-truncate-string (string max-length)
(if (<= (length string) max-length)
string
(concat (substring string 0 max-length) "..")))
(defun update-tmux-title ()
"Update tmux window name to the current Emacs buffer's name."
(when (inside-tmux-p)
(let ((filename (or (buffer-file-name) (buffer-name))))
(call-process-shell-command (concat "tmux rename-window " "'e:" (my-truncate-string (file-name-nondirectory filename) 15) "'")))))
(add-hook 'window-configuration-change-hook 'update-tmux-title)
;;;; ---------------------------------------------------------------------------
;;;; Platform-dependent settings
;;;; ---------------------------------------------------------------------------
(if window-system
(progn
(blink-cursor-mode -1)
;; Maximize all frames by default.
(modify-all-frames-parameters '((fullscreen . maximized))))
(progn
))
(if (eq system-type 'windows-nt)
(defun my-set-font (font-height)
nil))
(if (eq system-type 'gnu/linux)
(progn
(defun my-maximize-frame (&optional f)
(interactive)
;; only work with GTK+ Emacs (Emacs must be configured with --with-x-toolkit=gtk)
(set-frame-parameter f 'fullscreen 'maximized))
;; This doesn't work. Don't know why.
;; (add-hook 'after-make-frame-functions
;; '(lambda ()
;; (shell-command "wmctrl -r :ACTIVE: -btoggle,fullscreen")))
;; Set the best Linux programming font - DejaVu Sans Mono.
(defun my-set-font (font-height)
(custom-set-faces
`(default ((t (:inherit nil :stipple nil :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height ,(* font-height 10) :width normal :family "DejaVu Sans Mono" :embolden t))))))
;; Shell in dired mode.
(setq dired-guess-shell-alist-user
`(
(".rar" "unrar x")
(".pdf" "evince")
(".avi" "gxine")
(".jpg" "eog")
(".bmp" "eog")
(".csv" "xdg-open")
(".xlsx" "xdg-open")
(".xls" "xdg-open")
(".htm" "google-chrome")
(".html" "google-chrome")
))
))
(if (eq system-type 'darwin)
(progn
(defun my-maximize-frame (&optional f)
(interactive)
(toggle-frame-fullscreen)) ;; This method is only available after Emacs 24.4
(global-set-key (kbd "C-s-f") 'my-maximize-frame)
;; SF Mono fonts must be install manually on OS X
;; $ cd /System/Applications/Utilities/Terminal.app/Contents/Resources/Fonts
;; $ open .
;; In finder, select all fonts and 'Open' => 'Install'.
(defun my-set-font (height)
(set-face-attribute 'default nil :font (format "SF Mono-%d" height))
)
;; Use Option key as Meta key (consistent with Mac key bindings).
(setq mac-option-modifier 'meta)
;; PC style handling of Home/End key.
(global-set-key (kbd "<home>") 'move-beginning-of-line)
(global-set-key (kbd "<end>") 'move-end-of-line)
;; "Ctrl+F4" in Mac is similar to "Alt-Tab" within the same workspace.
(global-set-key (kbd "<C-f4>") 'other-frame)
;; Shell in dired mode.