forked from meteor1113/dotemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-basic.el
1452 lines (1368 loc) · 59.9 KB
/
init-basic.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
;;; -*- mode: emacs-lisp; mode: goto-address; coding: utf-8; -*-
;; Copyright (C) 2008-2011 Meteor Liu
;;
;; This code has been released into the Public Domain.
;; You may do whatever you like with it.
;;
;; @file
;; @author Meteor Liu <meteor1113@gmail.com>
;; @date 2009-08-08
;; @URL http://github.com/meteor1113/dotemacs
;;; global setting
;; user information
(setq user-full-name "Meteor Liu")
(setq user-mail-address "meteor1113@gmail.com")
;; path
;; (when (eq system-type 'windows-nt)
;; (let* ((dir (file-name-directory (directory-file-name data-directory)))
;; (bin-dir (expand-file-name "bin" dir)))
;; (setenv "PATH" (concat bin-dir ";" (getenv "PATH"))))) ; for "| grep"
(let* ((dir (file-name-directory (or load-file-name (buffer-file-name))))
(bin-dir (expand-file-name "bin" dir)))
(setenv "PATH" (concat bin-dir
(if (eq system-type 'windows-nt) ";" ":")
(getenv "PATH")))
;; (setq exec-path (append exec-path (list bin-dir)))
(add-to-list 'exec-path bin-dir))
(let ((cedet-possible-dirs '("~/.emacs.d/cedet-1.0pre6"
"~/.emacs.d/cedet-1.0pre7"
"~/.emacs.d/cedet-1.0"
"~/.emacs.d/cedet-1.1")))
(dolist (dir cedet-possible-dirs)
(let ((default-directory (expand-file-name dir)))
(when (file-exists-p default-directory)
(add-to-list 'load-path default-directory)
(if (fboundp 'normal-top-level-add-subdirs-to-load-path)
(normal-top-level-add-subdirs-to-load-path))))))
;; c/c++ include dir (ffap use mingw dirs)
(defvar user-include-dirs
'("." "./include" "./inc" "./common" "./public"
".." "../include" "../inc" "../common" "../public"
"../.." "../../include" "../../inc" "../../common" "../../public"
"C:/MinGW/include"
"C:/MinGW/include/c++/3.4.5"
"C:/MinGW/include/c++/3.4.5/mingw32"
"C:/MinGW/include/c++/3.4.5/backward"
"C:/MinGW/lib/gcc/mingw32/3.4.5/include"
"C:/Program Files/Microsoft Visual Studio/VC98/Include"
"C:/Program Files/Microsoft Visual Studio/VC98/MFC/Include"
;; "C:/Program Files/Microsoft Visual Studio 10.0/VC/include"
)
"User include dirs for c/c++ mode")
(defvar c-preprocessor-symbol-files
'("C:/MinGW/include/c++/3.4.5/mingw32/bits/c++config.h"
"C:/Program Files/Microsoft Visual Studio/VC98/Include/xstddef"
;; "C:/Program Files/Microsoft Visual Studio 10.0/VC/include/yvals.h"
;; "C:/Program Files/Microsoft Visual Studio 10.0/VC/include/crtdefs.h"
)
"Preprocessor symbol files for cedet")
;; ui
(when (fboundp 'tool-bar-mode)
(tool-bar-mode t))
(when (fboundp 'set-scroll-bar-mode)
(set-scroll-bar-mode 'right))
(setq scroll-step 1)
;; (setq scroll-margin 3)
;; (setq scroll-conservatively 10000)
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
(column-number-mode t)
(size-indication-mode 1)
;; (setq display-time-24hr-format t)
;; (setq display-time-day-and-date t)
;; (display-time-mode t)
(which-function-mode t)
(setq buffers-menu-max-size 30)
;; (setq linum-eager nil)
;; (when (fboundp 'global-linum-mode)
;; (global-linum-mode 1))
;; (setq-default cursor-type 'bar)
;; (blink-cursor-mode -1)
(setq x-stretch-cursor t)
(xterm-mouse-mode 1) ; (if window-system -1 1)
(add-hook 'after-make-frame-functions
(lambda (frame)
(with-selected-frame frame
(xterm-mouse-mode 1))))
;; (mouse-avoidance-mode 'animate)
;; (setq mouse-autoselect-window t)
(setq-default indicate-buffer-boundaries (quote left))
(when (fboundp 'winner-mode)
(winner-mode 1))
(setq frame-title-format
'((:eval (or buffer-file-name (buffer-name)))
(:eval (if (buffer-modified-p) " * " " - "))
invocation-name
"@"
system-name))
(when window-system (set-background-color "honeydew")) ; #f0fff0
(add-hook 'after-make-frame-functions
(lambda (frame)
(when (and (framep frame)
(not (eq (framep frame) t)))
(with-selected-frame frame
;; (when window-system
(set-background-color "honeydew")))))
;; (unless window-system
;; (setq frame-background-mode 'dark))
;; (set-frame-parameter (selected-frame) 'alpha (list 85 50)) ; Transparent
;; (add-to-list 'default-frame-alist (cons 'alpha (list 85 50)))
;; edit
(setq-default tab-width 4)
(setq-default comment-column 40) ; [C-x ;] (set-comment-column)
(setq-default major-mode 'text-mode) ; (setq default-major-mode 'text-mode)
(setq bookmark-save-flag 1)
(global-auto-revert-mode t)
;; (setq require-final-newline 'ask)
(setq mode-require-final-newline nil)
;; (add-hook 'text-mode-hook (lambda () (setq require-final-newline nil)))
(find-function-setup-keys)
(when (fboundp 'ido-mode)
(ido-mode t)
;; (ido-everywhere t) ; For GUI
(setq ido-use-filename-at-point 'guess
ido-use-url-at-point t))
(icomplete-mode t)
(show-paren-mode t)
;; (setq show-paren-style 'expression)
;; (custom-set-faces
;; '(show-paren-match
;; ((((class color) (background light)) (:background "azure2")))))
(global-cwarn-mode 1)
(setq compilation-auto-jump-to-first-error t)
(setq compilation-scroll-output t)
(add-hook 'write-file-hooks 'time-stamp)
(setq time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %U")
;; (global-set-key "<" 'skeleton-pair-insert-maybe)
;; (global-set-key "(" 'skeleton-pair-insert-maybe)
;; (global-set-key "[" 'skeleton-pair-insert-maybe)
;; (global-set-key "{" 'skeleton-pair-insert-maybe)
;; (setq skeleton-pair t)
;; (setq enable-recursive-minibuffers t)
(put 'set-goal-column 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(put 'dired-find-alternate-file 'disabled nil)
;; input
(if (fboundp 'cua-mode)
(progn
(setq cua-rectangle-mark-key [C-M-return])
(cua-mode t)
(setq cua-keep-region-after-copy t))
(when (fboundp 'pc-selection-mode)
(setq pc-select-selection-keys-only t)
(pc-selection-mode)))
(setq mouse-drag-copy-region nil)
(setq x-select-enable-clipboard t)
;; (setq mouse-yank-at-point t)
(defalias 'yes-or-no-p 'y-or-n-p)
;; coding
;; (setq system-time-locale "C")
(when (eq system-type 'windows-nt)
(let ((code (or file-name-coding-system default-file-name-coding-system)))
(setq default-process-coding-system (cons code code))))
(setq erc-server-coding-system '(utf-8 . utf-8))
;; (when (daemonp)
;; (add-hook 'after-make-frame-functions
;; (lambda (frame)
;; (with-selected-frame frame
;; (set-locale-environment (getenv "LANG"))))))
;; session
(require 'saveplace)
(setq-default save-place t)
(when (fboundp 'savehist-mode)
(savehist-mode t))
(setq recentf-menu-open-all-flag t
recentf-max-saved-items 100
recentf-max-menu-items 30)
(recentf-mode t)
(defadvice recentf-track-closed-file (after push-beginning activate)
"Move current buffer to the beginning of the recent list after killed."
(recentf-track-opened-file))
(defun undo-kill-buffer (arg)
"Re-open the last buffer killed. With ARG, re-open the nth buffer."
(interactive "p")
(let ((recently-killed-list (copy-sequence recentf-list))
(buffer-files-list
(delq nil (mapcar (lambda (buf)
(when (buffer-file-name buf)
(expand-file-name (buffer-file-name buf))))
(buffer-list)))))
(mapc
(lambda (buf-file)
(setq recently-killed-list
(delete buf-file recently-killed-list)))
buffer-files-list)
(find-file (nth (- arg 1) recently-killed-list))))
(and (fboundp 'desktop-save-mode)
(not (daemonp))
(desktop-save-mode (if window-system 1 -1)))
;; backup
(setq make-backup-files t)
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
;; (setq backup-by-copying t)
;; (setq delete-old-versions t)
;; (setq kept-old-versions 2)
;; (setq kept-new-versions 5)
;; (setq version-control t)
;; highlight
(when (fboundp 'global-font-lock-mode)
(global-font-lock-mode t))
;; (setq jit-lock-defer-time 0.05) ; Make c mode faster
(when (fboundp 'transient-mark-mode)
(transient-mark-mode t))
;; (setq hl-line-face 'underline) ; for highlight-symbol
;; (global-hl-line-mode 1) ; (if window-system 1 -1)
;; (global-highlight-changes-mode t) ; use cedet instead
(dolist (mode '(c-mode c++-mode objc-mode java-mode jde-mode
perl-mode cperl-mode php-mode python-mode ruby-mode
lisp-mode emacs-lisp-mode xml-mode nxml-mode html-mode
lisp-interaction-mode sh-mode sgml-mode))
(font-lock-add-keywords
mode
'(("\\<\\(FIXME\\|TODO\\|Todo\\)\\>" 1 font-lock-warning-face prepend)
("\\<\\(FIXME\\|TODO\\|Todo\\):" 1 font-lock-warning-face prepend))))
;; (setq-default show-trailing-whitespace t) ; use whitespace-mode instead
(setq whitespace-style '(face trailing lines-tail newline empty tab-mark))
(when window-system
(setq whitespace-style (append whitespace-style '(tabs newline-mark))))
;; (global-whitespace-mode t)
(eval-after-load "whitespace"
`(defun whitespace-post-command-hook ()
"Hack whitespace, it's very slow in c++-mode."))
;; file
;; (setq ffap-require-prefix t
;; dired-at-point-require-prefix t)
;; (ffap-bindings) ; Use ido to call ffap
(autoload 'dired-jump "dired-x" nil t)
(setq kmacro-call-mouse-event nil)
(global-set-key [S-mouse-3] 'ffap-at-mouse)
(global-set-key [C-S-mouse-3] 'ffap-menu)
(eval-after-load "ffap"
'(setq ffap-c-path (append ffap-c-path user-include-dirs)))
(eval-after-load "filecache"
'(progn (file-cache-add-directory-list load-path)
(file-cache-add-directory-list user-include-dirs)
(file-cache-add-directory "/usr/include")
(file-cache-add-directory-recursively "/usr/include/c++")
(file-cache-add-directory-recursively "/usr/local/include")))
(filesets-init)
(add-to-list 'filesets-data
(list "~/"
(list :files
"~/.emacs"
"~/.emacs.desktop"
"~/.emacs-places"
"~/.notes"
"~/.recentf"
"~/.profile"
"~/.bash_profile"
"~/.bashrc")))
(add-to-list 'filesets-data
'("~/.emacs.d/" (:tree "~/.emacs.d/" "^.+\\.*$")))
(add-to-list 'filesets-data
(list "dotemacs/"
(list :tree
(file-name-directory (or load-file-name
(buffer-file-name)))
"^.+\\.*$")))
(add-to-list 'filesets-data
(list "windows"
(list :files
"C:/WINDOWS/system32/drivers/etc/hosts"
"C:/boot.ini")))
(add-to-list 'filesets-data
(list "linux"
(list :files
"/etc/hosts"
"/etc/fstab"
"/etc/passwd"
"/etc/group"
"/boot/grub2/grub.cfg")))
(add-to-list 'filesets-data (list "temp" (list :files)))
;; calendar
(setq calendar-chinese-all-holidays-flag t)
(setq mark-holidays-in-calendar t)
;; (setq calendar-week-start-day 1)
;; (setq mark-diary-entries-in-calendar t)
(setq diary-file "~/.emacs.d/diary")
;; (add-hook 'diary-hook 'appt-make-list)
;; (setq appt-display-format 'window)
;; (setq appt-display-mode-line t)
;; (setq appt-display-diary nil)
(setq appt-display-duration (* 365 24 60 60))
(unless (daemonp)
(appt-activate 1)
(delete-other-windows))
;; (diary 0)
;; autoinsert
(auto-insert-mode 1)
;; (setq auto-insert t)
;; (setq auto-insert-query nil)
(setq auto-insert-directory
(file-name-as-directory
(expand-file-name "etc/templates"
(file-name-directory (or buffer-file-name
load-file-name)))))
(setq auto-insert-expansion-alist
'(("(>>>DIR<<<)" . (file-name-directory buffer-file-name))
("(>>>FILE<<<)" . (file-name-nondirectory buffer-file-name))
("(>>>FILE_SANS<<<)" . (file-name-sans-extension
(file-name-nondirectory buffer-file-name)))
("(>>>FILE_UPCASE<<<)" . (upcase
(file-name-sans-extension
(file-name-nondirectory buffer-file-name))))
("(>>>FILE_UPCASE_INIT<<<)" . (upcase-initials
(file-name-sans-extension
(file-name-nondirectory buffer-file-name))))
("(>>>FILE_EXT<<<)" . (file-name-extension buffer-file-name))
("(>>>FILE_EXT_UPCASE<<<)" . (upcase (file-name-extension buffer-file-name)))
("(>>>DATE<<<)" . (format-time-string "%d %b %Y"))
("(>>>TIME<<<)" . (format-time-string "%T"))
("(>>>VC_DATE<<<)" . (let ((ret ""))
(set-time-zone-rule "UTC")
(setq ret (format-time-string "%Y/%m/%d %T"))
(set-time-zone-rule nil)
ret))
("(>>>YEAR<<<)" . (format-time-string "%Y"))
("(>>>ISO_DATE<<<)" . (format-time-string "%Y-%m-%d"))
("(>>>AUTHOR<<<)" . (or user-mail-address
(and (fboundp 'user-mail-address)
(user-mail-address))
(concat (user-login-name) "@" (system-name))))
("(>>>USER_NAME<<<)" . (or (and (boundp 'user-full-name)
user-full-name)
(user-full-name)))
("(>>>LOGIN_NAME<<<)" . (user-login-name))
("(>>>HOST_ADDR<<<)" . (or (and (boundp 'mail-host-address)
(stringp mail-host-address)
mail-host-address)
(system-name)))))
(defun auto-insert-expand ()
(dolist (val auto-insert-expansion-alist)
(let ((from (car val))
(to (eval (cdr val))))
(goto-char (point-min))
(replace-string from to))))
(define-auto-insert "\\.\\([Hh]\\|hh\\|hpp\\)\\'"
["h.tpl" auto-insert-expand])
(define-auto-insert "\\.\\([Cc]\\|cc\\|cpp\\)\\'"
["cpp.tpl" auto-insert-expand])
(define-auto-insert "\\.java\\'"
["java.tpl" auto-insert-expand])
(define-auto-insert "\\.py\\'"
["py.tpl" auto-insert-expand])
;; misc
(setq inhibit-startup-message t) ; for no desktop
(setq inhibit-default-init t) ; for frame-title-format
(setq generic-define-mswindows-modes t)
(setq generic-define-unix-modes t)
(require 'generic-x nil 'noerror)
(setq ring-bell-function 'ignore)
(auto-image-file-mode t)
;; (setq message-log-max t)
;; (add-hook 'find-file-hook 'goto-address-mode)
;; (setq max-specpdl-size 4000)
;; (setq max-lisp-eval-depth 4000)
;; (setq debug-on-error t)
(autoload 'zone-when-idle "zone" nil t)
(zone-when-idle 600)
;; zone-pgm-stress will destroy the clipboard
(setq zone-programs (append zone-programs nil))
(setq zone-programs (remq 'zone-pgm-stress zone-programs))
(setq zone-programs (remq 'zone-pgm-stress-destress zone-programs))
(add-hook 'after-init-hook
(lambda ()
(message "emacs-init-time: %s" (emacs-init-time))))
(defadvice find-tag (before tags-file-name-advice activate)
"Find TAGS file in ./ or ../ or ../../ dirs"
(let ((list (mapcar 'expand-file-name '("./TAGS" "../TAGS" "../../TAGS"))))
(while list
(if (file-exists-p (car list))
(progn
(setq tags-file-name (car list))
(setq list nil))
(setq list (cdr list))))))
(defun find-dotemacs-file ()
"Open .emacs file"
(interactive)
(let* ((paths '("~/.emacs" "~/.emacs.el" "~/.emacs.d/init.el" "~/_emacs"))
(dotemacs-path))
(dolist (path paths)
(and (not dotemacs-path)
(file-exists-p path)
(setq dotemacs-path path)))
(find-file (or dotemacs-path
(locate-file "site-start.el" load-path)
"~/.emacs"))))
(defun move-line-up (p)
"Move current line up, copy from crazycool@smth"
(interactive "*p")
(let ((c (current-column)))
(beginning-of-line)
(kill-line 1)
(previous-line p)
(beginning-of-line)
(yank)
(previous-line 1)
(move-to-column c)))
(defun move-line-down (p)
"Move current line down, copy from crazycool@smth"
(interactive "*p")
(let ((c (current-column)))
(beginning-of-line)
(kill-line 1)
(next-line p)
(beginning-of-line)
(yank)
(previous-line 1)
(move-to-column c)))
(defun format-region ()
"Format region, if no region actived, format current buffer.
Like eclipse's Ctrl+Alt+F."
(interactive)
(let ((start (point-min))
(end (point-max)))
(if (and (fboundp 'region-active-p) (region-active-p))
(progn (setq start (region-beginning))
(setq end (region-end)))
(progn (when (fboundp 'whitespace-cleanup)
(whitespace-cleanup))
(setq end (point-max))))
(save-excursion
(save-restriction
(narrow-to-region (point-min) end)
(push-mark (point))
(push-mark (point-max) nil t)
(goto-char start)
(when (fboundp 'whitespace-cleanup)
(whitespace-cleanup))
(untabify start (point-max))
(indent-region start (point-max) nil)))))
(defun cxx-file-p (file)
(let ((file-extension (file-name-extension file)))
(and file-extension
(string= file (file-name-sans-versions file))
(find file-extension
'("h" "hpp" "hxx" "c" "cpp" "cxx")
:test 'string=))))
(defun format-cxx-file (file)
"Format a c/c++ file."
(interactive "F")
(if (cxx-file-p file)
(let ((buffer (find-file-noselect file))) ;; open buffer
(save-excursion
(set-buffer buffer)
;; (mark-whole-buffer)
(when (fboundp 'whitespace-cleanup)
(whitespace-cleanup))
(untabify (point-min) (point-max))
(indent-region (point-min) (point-max))
(save-buffer)
(kill-buffer)
(message "Formated c++ file:%s" file)))
(message "%s isn't a c++ file" file)))
(defun format-cxx-directory (dirname)
"Format all c/c++ file in a directory."
(interactive "D")
;; (message "directory:%s" dirname)
(let ((files (directory-files dirname t)))
(dolist (x files)
(if (not (string= "." (substring (file-name-nondirectory x) 0 1)))
(if (file-directory-p x)
(format-cxx-directory x)
(if (and (file-regular-p x)
(not (file-symlink-p x))
(cxx-file-p x))
(format-cxx-file x)))))))
(autoload 'grep-tag-default "grep")
(autoload 'grep-apply-setting "grep")
(defvar grep-dir-format
(cond ((eq system-type 'aix)
"grep -inrH '%s' . | grep -vE \"\.svn/|\.git/|\.hg/|\.bzr/|CVS/\"")
;; ((eq system-type 'gnu/linux)
;; "grep -inrHI '%s' . | grep -vE \"\.svn/|\.git/|\.hg/|\.bzr/|CVS/\"")
;; ((eq system-type 'windows-nt)
;; "grep --exclude-dir=.svn --exclude-dir=.git --exclude-dir=.hg \
;; --exclude-dir=.bzr --exclude-dir=CVS -inrHI \"%s\" .")
(t
"grep --exclude-dir=.svn --exclude-dir=.git --exclude-dir=.hg \
--exclude-dir=.bzr --exclude-dir=CVS -inrHI '%s' .")))
(defun grep-current-dir (&optional prompt wd)
"Run `grep' to find current word in current directory."
(interactive "P")
(let* ((word (or wd
(and (fboundp 'region-active-p)
(region-active-p)
(buffer-substring-no-properties (region-beginning)
(region-end)))
(grep-tag-default)))
(cmd (format grep-dir-format word)))
(grep-apply-setting 'grep-use-null-device nil)
(if (or prompt (= (length word) 0))
(grep (read-shell-command
"Run grep (like this): " cmd 'grep-history))
(if (= 0 (length word))
(message "Word is blank.")
(grep cmd)))))
(defun grep-todo-current-dir ()
"Run `grep' to find 'TODO' in current directory."
(interactive)
(grep-current-dir nil "TODO|FIXME"))
(defun moccur-word-all-buffers (regexp)
"Run `multi-occur' to find regexp in all buffers."
(if (= 0 (length regexp))
(message "Regexp is blank.")
(let ((buffers (buffer-list)))
(dolist (buffer buffers)
(let ((pos (string-match " *\\*" (buffer-name buffer))))
(when (and pos (= 0 pos))
(setq buffers (remq buffer buffers)))))
(multi-occur buffers regexp))))
(defun moccur-all-buffers (&optional prompt)
"Run `multi-occur' to find current word in all buffers."
(interactive "P")
(let ((word (grep-tag-default)))
(when (or prompt (= (length word) 0))
(setq word (read-regexp "List lines matching regexp" word)))
(moccur-word-all-buffers word)))
(defun moccur-todo-all-buffers ()
"Run `multi-occur' to find 'TODO' in all buffers."
(interactive)
(moccur-word-all-buffers
"\\<\\([Tt][Oo][Dd][Oo]\\|[Ff][Ii][Xx][Mm][Ee]\\)\\>"))
(defun switch-to-other-buffer ()
"Switch to (other-buffer)."
(interactive)
(switch-to-buffer (other-buffer)))
(defadvice switch-to-other-buffer (after pulse-advice activate)
"After switch-to-other-buffer, pulse the line the cursor lands on."
(when (and (boundp 'pulse-command-advice-flag) pulse-command-advice-flag
(interactive-p))
(pulse-momentary-highlight-one-line (point))))
(defun mark-current-line ()
"Put point at beginning of this line, mark at end."
(interactive)
(move-beginning-of-line 1)
(set-mark (point))
(move-end-of-line 1))
(defun mark-current-line-mouse (ev)
"Mark current line with a mouse click. EV is the mouse event."
(interactive "e")
(mouse-set-point ev)
(mark-current-line))
;; (defun goto-match-paren (arg)
;; "Go to the matching parenthesis if on parenthesis, otherwise insert %.
;; vi style of % jumping to matching brace."
;; (interactive "p")
;; (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
;; ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
;; (t (self-insert-command (or arg 1)))))
(defun goto-match-paren (arg)
"Go to the matching if on (){}[], similar to vi style of % "
(interactive "p")
;; first, check for "outside of bracket" positions expected by forward-sexp, etc.
(cond ((looking-at "[\[\(\{]") (forward-sexp))
((looking-back "[\]\)\}]" 1) (backward-sexp))
;; now, try to succeed from inside of a bracket
((looking-at "[\]\)\}]") (forward-char) (backward-sexp))
((looking-back "[\[\(\{]" 1) (backward-char) (forward-sexp))
(t nil)))
(defun toggle-fullscreen (&optional f)
(interactive)
(let ((current-value (frame-parameter nil 'fullscreen)))
(set-frame-parameter nil 'fullscreen
(if (equal 'fullboth current-value)
(if (boundp 'old-fullscreen) old-fullscreen nil)
(progn (setq old-fullscreen current-value)
'fullboth)))))
;; global key bindings
(global-set-key (kbd "M-SPC") 'set-mark-command)
(define-key cua-global-keymap (kbd "M-SPC") 'cua-set-mark)
(global-set-key (kbd "<M-up>") 'move-line-up)
(global-set-key (kbd "<M-down>") 'move-line-down)
(global-set-key (kbd "<find>") 'move-beginning-of-line) ; putty
(global-set-key (kbd "<select>") 'move-end-of-line) ; putty
(global-set-key (kbd "<C-wheel-down>") 'text-scale-decrease)
(global-set-key (kbd "<C-wheel-up>") 'text-scale-increase)
(global-set-key (kbd "<C-mouse-4>") 'text-scale-decrease)
(global-set-key (kbd "<C-mouse-5>") 'text-scale-increase)
(unless (key-binding [mouse-4])
(global-set-key [mouse-4] 'mwheel-scroll)) ; putty
(unless (key-binding [mouse-5])
(global-set-key [mouse-5] 'mwheel-scroll)) ; putty
(global-set-key (kbd "C-=") 'align)
(global-set-key (kbd "C-S-u") 'upcase-region)
(global-set-key (kbd "C-S-l") 'downcase-region)
;; (global-set-key (kbd "C-M-;") 'comment-or-uncomment-region)
;; (global-set-key (kbd "ESC M-;") 'comment-or-uncomment-region) ; putty
(global-set-key [M-f8] 'format-region)
(global-set-key (kbd "ESC <f8>") 'format-region) ; putty
(global-set-key (kbd "C-S-f") 'format-region)
;; (global-set-key (kbd "M-P") 'previous-buffer)
;; (global-set-key (kbd "M-N") 'next-buffer)
(global-set-key [C-prior] 'previous-buffer)
(global-set-key [C-next] 'next-buffer)
(global-set-key [(control tab)] 'switch-to-other-buffer)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "C-c q") 'auto-fill-mode)
(define-key global-map "\C-x\C-j" 'dired-jump)
;; (global-set-key [f4] 'next-error)
(global-set-key [f4] (lambda (&optional previous)
(interactive "P")
(if previous
(previous-error)
(next-error))))
(global-set-key [S-f4] 'previous-error)
(global-set-key [f16] 'previous-error) ; S-f4
(global-set-key [C-f4] 'kill-this-buffer)
(global-set-key (kbd "ESC <f4>") 'kill-this-buffer) ; putty
(global-set-key [(control ?.)] 'repeat)
(global-set-key [f6] 'grep-current-dir)
(global-set-key [C-f6] 'moccur-all-buffers)
(global-set-key [M-f6] 'grep-todo-current-dir)
;; (lambda () (interactive) (grep-current-dir nil "TODO|FIXME")))
(global-set-key (kbd "ESC <f6>") (key-binding [M-f6]))
(global-set-key [C-M-f6] 'moccur-todo-all-buffers)
;; '(lambda ()
;; (interactive)
;; (moccur-word-all-buffers
;; "\\<\\([Tt][Oo][Dd][Oo]\\|[Ff][Ii][Xx][Mm][Ee]\\)\\>")))
(global-set-key (kbd "ESC <C-f6>") (key-binding [C-M-f6]))
(global-set-key [f7] '(lambda () (interactive) (compile compile-command)))
(global-set-key [f11] 'toggle-fullscreen)
;; (global-set-key [header-line double-mouse-1] 'kill-this-buffer)
(global-set-key [header-line double-mouse-1]
'(lambda ()
(interactive)
(let* ((i 1)
(name (format "new %d" i)))
(while (get-buffer name)
(setq i (1+ i))
(setq name (format "new %d" i)))
(switch-to-buffer name))))
;; (global-set-key [header-line double-mouse-1]
;; '(lambda () (interactive) (switch-to-buffer "new")))
(global-set-key [header-line mouse-3] 'kill-this-buffer)
(global-set-key [mouse-2] nil)
(global-set-key [left-fringe mouse-2] nil)
(global-set-key [left-margin mouse-2] nil)
(global-set-key [mouse-3] menu-bar-edit-menu)
(global-set-key (kbd "<left-margin> <mouse-2>") 'mark-current-line-mouse)
(global-set-key (kbd "C-S-t") 'undo-kill-buffer)
(global-set-key (kbd "C-c C-v") 'view-mode)
(global-set-key [(control %)] 'goto-match-paren)
(when (eq system-type 'aix)
(global-set-key (kbd "C-d") 'backward-delete-char-untabify)
(eval-after-load "cc-mode"
'(progn
(define-key c-mode-base-map "\C-d" 'c-electric-backspace)))
(eval-after-load "comint"
'(progn
(define-key comint-mode-map "\C-d" 'delete-backward-char))))
;;; special mode setting
(add-to-list 'auto-mode-alist
'("\\.\\(exe\\|vsd\\|so\\|dll\\)$" . hexl-mode))
(defvar text-imenu-generic-expression
`((nil ,"^ \\{0,4\\}\\([一二三四五六七八九十]+[、. )]\\)+ *[^,。,]+?$" 0)
(nil ,"^ \\{0,4\\}\\([0-9]+[、. )]\\)+ *[^,。,]+?$" 0)))
(add-hook 'text-mode-hook
(lambda ()
(setq imenu-generic-expression text-imenu-generic-expression)
(imenu-add-menubar-index)))
(setq dired-dwim-target t)
;; (add-hook 'dired-mode-hook
;; (lambda ()
;; (define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file)
;; (define-key dired-mode-map (kbd "^")
;; (lambda () (interactive) (find-alternate-file "..")))))
(defadvice dired-find-file (around dired-find-file-single-buffer activate)
"Replace current buffer if file is a directory."
(interactive)
(let ((orig (current-buffer))
(filename (dired-get-file-for-visit)))
ad-do-it
(when (and (file-directory-p filename)
(not (eq (current-buffer) orig)))
(kill-buffer orig))))
(defadvice dired-up-directory (around dired-up-directory-single-buffer activate)
"Replace current buffer if file is a directory."
(interactive)
(let ((orig (current-buffer)))
ad-do-it
(kill-buffer orig)))
(add-hook 'change-log-mode-hook 'turn-on-auto-fill)
(setq org-log-done 'time)
(setq org-export-with-archived-trees t)
(setq org-startup-truncated nil)
(setq org-src-fontify-natively t)
(add-hook 'org-mode-hook
(lambda ()
(setq comment-start nil)
(setq indent-tabs-mode nil)
;; (when (fboundp 'whitespace-mode)
;; (whitespace-mode 1))
;; (auto-fill-mode t)
(imenu-add-menubar-index)))
(eval-after-load "org"
`(progn
(define-key org-mode-map [(control tab)] nil)
(define-key org-mode-map (kbd "<C-S-iso-lefttab>")
'org-force-cycle-archived)
(define-key org-mode-map (kbd "<C-S-tab>") 'org-force-cycle-archived)))
(when (fboundp 'nxml-mode)
(add-to-list 'auto-mode-alist
'("\\.\\(xml\\|xsl\\|rng\\|xhtml\\)\\'" . nxml-mode))
(setq nxml-bind-meta-tab-to-complete-flag t)
(add-hook 'nxml-mode-hook
'(lambda ()
;; (when (fboundp 'whitespace-mode)
;; (whitespace-mode t))
(linum-mode 1)
(require 'sgml-mode)
(set-syntax-table sgml-mode-syntax-table)))
(defun format-xml ()
"Format XML markup in region.
The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive)
(let ((start (point-min))
(end (point-max)))
(if (and (fboundp 'region-active-p) (region-active-p))
(progn (setq start (region-beginning))
(setq end (region-end)))
(progn (when (fboundp 'whitespace-cleanup)
(whitespace-cleanup))
(setq end (point-max))))
(save-excursion
(save-restriction
(narrow-to-region (point-min) end)
(push-mark (point))
(push-mark (point-max) nil t)
(goto-char start)
;; split <foo><foo> or </foo><foo>, but not <foo></foo>
(while (search-forward-regexp ">[ \t]*<[^/]" end t)
(backward-char 2) (insert "\n") (incf end))
;; split <foo/></foo> and </foo></foo>
(goto-char start)
(while (search-forward-regexp "<.*?/.*?>[ \t]*<" end t)
(backward-char) (insert "\n") (incf end))
(when (fboundp 'whitespace-cleanup)
(goto-char start)
(whitespace-cleanup))
(indent-region start (point-max) nil)))))
(eval-after-load "nxml-mode"
`(progn
(define-key nxml-mode-map [M-f8] 'format-xml)
(define-key nxml-mode-map (kbd "ESC <f8>") 'format-xml) ; putty
(define-key nxml-mode-map (kbd "C-S-f") 'format-xml))))
(defadvice artist-coord-win-to-buf (before tabbar-mode activate compile)
"Hack artist-mode's wrong position when tabbar-mode."
(if tabbar-mode (setq coord (cons (car coord) (1- (cdr coord))))))
(defvar hs--overlay-keymap nil "keymap for folding overlay")
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] 'hs-show-block)
(setq hs--overlay-keymap map))
(setq hs-set-up-overlay
(defun my-display-code-line-counts (ov)
(when (eq 'code (overlay-get ov 'hs))
(overlay-put ov 'display
(propertize
(format "...<%d lines>"
(count-lines (overlay-start ov)
(overlay-end ov)))
'face 'mode-line))
(overlay-put ov 'priority (overlay-end ov))
(overlay-put ov 'keymap hs--overlay-keymap)
(overlay-put ov 'pointer 'hand))))
(eval-after-load "hideshow"
'(progn (define-key hs-minor-mode-map [(shift mouse-2)] nil)
(define-key hs-minor-mode-map (kbd "C-+") 'hs-toggle-hiding)
(define-key hs-minor-mode-map (kbd "<left-fringe> <mouse-2>")
'hs-mouse-toggle-hiding)))
;; (global-set-key (kbd "C-?") 'hs-minor-mode)
(defun chmod+x ()
(and (save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(save-match-data
(looking-at "^#!"))))
(not (file-executable-p buffer-file-name))
(shell-command (format "chmod +x '%s'" buffer-file-name))
(kill-buffer "*Shell Command Output*")))
(when (executable-find "chmod")
(add-hook 'after-save-hook 'chmod+x))
(defun prog-common-function ()
(setq indent-tabs-mode nil)
;; (local-set-key (kbd "<return>") 'newline-and-indent)
(when (fboundp 'whitespace-mode)
(whitespace-mode t))
(linum-mode 1)
(hs-minor-mode t)
(ignore-errors (imenu-add-menubar-index)))
(add-hook 'c-mode-common-hook 'prog-common-function)
(defun my-c-mode-font-lock-if0 (limit)
(save-restriction
(widen)
(save-excursion
(goto-char (point-min))
(let ((depth 0) str start start-depth)
(while (re-search-forward "^\\s-*#\\s-*\\(if\\|else\\|endif\\)" limit 'move)
(setq str (match-string 1))
(if (string= str "if")
(progn
(setq depth (1+ depth))
(when (and (null start) (looking-at "\\s-+0"))
(setq start (match-end 0)
start-depth depth)))
(when (and start (= depth start-depth))
(c-put-font-lock-face start
(match-beginning 0)
'font-lock-comment-face)
(setq start nil))
(when (string= str "endif")
(setq depth (1- depth)))))
(when (and start (> depth 0))
(c-put-font-lock-face start (point) 'font-lock-comment-face)))))
nil)
(defun my-c-mode-common-hook-if0 ()
(font-lock-add-keywords
nil
'((my-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-if0)
(add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c++-mode))
(add-hook 'c-mode-hook (lambda () (c-set-style "stroustrup")))
(add-hook 'c++-mode-hook
(lambda ()
(c-set-style "stroustrup")
;; (c-toggle-auto-hungry-state 1)
(c-set-offset 'innamespace 0)))
(add-hook 'java-mode-hook (lambda () (c-set-style "java")))
(when (boundp 'magic-mode-alist)
(add-to-list 'magic-mode-alist '("\\(.\\|\n\\)*@implementation" . objc-mode))
(add-to-list 'magic-mode-alist '("\\(.\\|\n\\)*@interface" . objc-mode))
(add-to-list 'magic-mode-alist '("\\(.\\|\n\\)*@protocol" . objc-mode)))
;; (add-to-list 'magic-mode-alist '("\\(.\\|\n\\)*#import" . objc-mode))
(add-to-list 'auto-mode-alist '("\\.mm\\'" . objc-mode))
(add-hook 'objc-mode-hook (lambda () (c-set-style "stroustrup")))
(add-to-list 'auto-mode-alist '("\\.[pP][rR][cC]\\'" . sql-mode))
(add-hook 'sql-mode-hook 'prog-common-function)
(add-hook 'emacs-lisp-mode-hook
(lambda ()
(prog-common-function)
(turn-on-eldoc-mode)))
(add-hook 'python-mode-hook 'prog-common-function)
(add-hook 'sh-mode-hook 'prog-common-function)
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on t)
(add-hook 'makefile-mode-hook
(lambda ()
(when (fboundp 'whitespace-mode)
(whitespace-mode t))
(linum-mode 1)
(imenu-add-menubar-index)))
(add-hook 'autoconf-mode-hook
(lambda ()
(when (fboundp 'whitespace-mode)
(whitespace-mode t))
(linum-mode 1)))
(add-hook 'perl-mode-hook 'prog-common-function)
(add-to-list 'auto-mode-alist
'("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))
(add-hook 'cperl-mode-hook
'(lambda ()
(prog-common-function)
(cperl-set-style "PerlStyle")
(setq cperl-continued-brace-offset -4)
(abbrev-mode t)))
;; flymake
(autoload 'flymake-find-file-hook "flymake" "" t)
;; (add-hook 'find-file-hook 'flymake-find-file-hook)
(setq flymake-allowed-file-name-masks '())
(setq flymake-gui-warnings-enabled nil)
(setq flymake-log-level 0)
(setq flymake-no-changes-timeout 5.0)
(setq flymake-master-file-dirs
'("." "./src" "../src" "../../src"
"./source" "../source" "../../source"
"./Source" "../Source" "../../Source"
"./test" "../test" "../../test"
"./Test" "../Test" "../../Test"
"./UnitTest" "../UnitTest" "../../UnitTest"))
(defvar flymake-mode-map (make-sparse-keymap))
(define-key flymake-mode-map (kbd "C-c <f4>") 'flymake-goto-next-error)
(define-key flymake-mode-map (kbd "C-c <S-f4>") 'flymake-goto-prev-error)
(define-key flymake-mode-map (kbd "C-c <C-f4>")
'flymake-display-err-menu-for-current-line)
(or (assoc 'flymake-mode minor-mode-map-alist)
(setq minor-mode-map-alist
(cons (cons 'flymake-mode flymake-mode-map)
minor-mode-map-alist)))
(defadvice flymake-goto-prev-error (after display activate)
(message (get-char-property (point) 'help-echo)))
(defadvice flymake-goto-next-error (after display activate)
(message (get-char-property (point) 'help-echo)))
(when (executable-find "texify")
(add-to-list 'flymake-allowed-file-name-masks
'("\\.tex\\'" flymake-simple-tex-init))
(add-to-list 'flymake-allowed-file-name-masks
'("[0-9]+\\.tex\\'"
flymake-master-tex-init flymake-master-cleanup)))
(when (executable-find "xml")
(add-to-list 'flymake-allowed-file-name-masks
'("\\.xml\\'" flymake-xml-init))
(add-to-list 'flymake-allowed-file-name-masks
'("\\.html?\\'" flymake-xml-init)))
(when (executable-find "perl")
(add-to-list 'flymake-allowed-file-name-masks
'("\\.p[ml]\\'" flymake-perl-init)))
(when (executable-find "php")
(add-to-list 'flymake-allowed-file-name-masks
'("\\.php[345]?\\'" flymake-php-init)))
(when (executable-find "make")
(add-to-list 'flymake-allowed-file-name-masks
'("\\.idl\\'" flymake-simple-make-init))
(add-to-list 'flymake-allowed-file-name-masks
'("\\.java\\'"
flymake-simple-make-java-init flymake-simple-java-cleanup))
(add-to-list 'flymake-allowed-file-name-masks