forked from redguardtoo/emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-misc.el
948 lines (827 loc) · 33.9 KB
/
init-misc.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
;; {{ swiper&ivy-mode
(defun swiper-the-thing ()
(interactive)
(swiper (my-use-selected-string-or-ask "")))
;; }}
;; {{ shell and conf
(add-to-list 'auto-mode-alist '("\\.[^b][^a][a-zA-Z]*rc$" . conf-mode))
(add-to-list 'auto-mode-alist '("\\.aspell\\.en\\.pws\\'" . conf-mode))
(add-to-list 'auto-mode-alist '("\\.meta\\'" . conf-mode))
(add-to-list 'auto-mode-alist '("\\.?muttrc\\'" . conf-mode))
(add-to-list 'auto-mode-alist '("\\.ctags\\'" . conf-mode))
(add-to-list 'auto-mode-alist '("\\.mailcap\\'" . conf-mode))
;; }}
;; {{ auto-yasnippet
;; Use C-q instead tab to complete snippet
;; - `aya-create' at first, input ~ to mark the thing next
;; - `aya-expand' to expand snippet
;; - `aya-open-line' to finish
(global-set-key (kbd "C-q") #'aya-open-line)
;; }}
;; {{ ace-link
(ace-link-setup-default)
(global-set-key (kbd "M-o") 'ace-link-addr)
;; }}
;; open header file under cursor
(global-set-key (kbd "C-x C-o") 'ffap)
;; java
(add-to-list 'auto-mode-alist '("\\.aj\\'" . java-mode))
;; makefile
(add-to-list 'auto-mode-alist '("\\.ninja$" . makefile-gmake-mode))
;; {{ support MY packages which are not included in melpa
(setq org2nikola-use-verbose-metadata t) ; for nikola 7.7+
;; }}
(define-key global-map (kbd "RET") 'newline-and-indent)
;; {{ isearch
;; Use regex to search by default
(global-set-key (kbd "C-s") 'isearch-forward-regexp)
(global-set-key (kbd "C-r") 'isearch-backward-regexp)
(global-set-key (kbd "C-M-s") 'isearch-forward)
(global-set-key (kbd "C-M-r") 'isearch-backward)
(define-key isearch-mode-map (kbd "C-o") 'isearch-occur)
;; }}
(setq-default buffers-menu-max-size 30
case-fold-search t
compilation-scroll-output t
ediff-split-window-function 'split-window-horizontally
ediff-window-setup-function 'ediff-setup-windows-plain
save-interprogram-paste-before-kill t
grep-highlight-matches t
grep-scroll-output t
indent-tabs-mode nil
line-spacing 0
mouse-yank-at-point t
set-mark-command-repeat-pop t
tooltip-delay 1.5
;; void problems with crontabs, etc.
;; require-final-newline t ; bad idea, could accidentally edit others' code
truncate-lines nil
truncate-partial-width-windows nil
;; visible-bell has some issue
;; @see https://github.com/redguardtoo/mastering-emacs-in-one-year-guide/issues/9#issuecomment-97848938
visible-bell nil)
;; @see http://www.emacswiki.org/emacs/SavePlace
(require 'saveplace)
(setq-default save-place t)
;; {{ find-file-in-project (ffip)
(defun my-git-show-selected-commit ()
"Run 'git show selected-commit' in shell."
(let* ((git-cmd "git --no-pager log --date=short --pretty=format:'%h|%ad|%s|%an'")
(git-cmd-rlts (split-string (shell-command-to-string git-cmd) "\n" t))
(line (ivy-read "git log:" git-cmd-rlts)))
(shell-command-to-string (format "git show %s"
(car (split-string line "|" t))))))
(defun my-git-diff-current-file ()
"Run 'git diff version:current-file current-file'."
(let* ((git-cmd (concat "git --no-pager log --date=short --pretty=format:'%h|%ad|%s|%an' "
buffer-file-name))
(git-root (locate-dominating-file default-directory ".git"))
(git-cmd-rlts (nconc (split-string (shell-command-to-string "git branch --no-color --all") "\n" t)
(split-string (shell-command-to-string git-cmd) "\n" t)))
(line (ivy-read "git diff same file with version" git-cmd-rlts)))
(shell-command-to-string (format "git --no-pager diff %s:%s %s"
(replace-regexp-in-string "^ *\\*? *" "" (car (split-string line "|" t)))
(file-relative-name buffer-file-name git-root)
buffer-file-name))))
(setq ffip-match-path-instead-of-filename t)
;; I only use git
(setq ffip-diff-backends '(my-git-show-selected-commit
my-git-diff-current-file
;; `git log -p' current file
(shell-command-to-string (format "cd $(git rev-parse --show-toplevel) && git --no-pager log --date=short -p '%s'"
(buffer-file-name)))
"cd $(git rev-parse --show-toplevel) && git diff"
"cd $(git rev-parse --show-toplevel) && git diff --cached"
(shell-command-to-string (format "cd $(git rev-parse --show-toplevel) && git --no-pager log --date=short -S'%s' -p"
(read-string "Git search string:")))
(car kill-ring)))
(defun neotree-project-dir ()
"Open NeoTree using the git root."
(interactive)
(let ((project-dir (ffip-get-project-root-directory))
(file-name (buffer-file-name)))
(if project-dir
(progn
(neotree-dir project-dir)
(neotree-find file-name))
(message "Could not find git project root."))))
;; }}
;; {{ groovy-mode
(add-to-list 'auto-mode-alist '("\\.groovy\\'" . groovy-mode))
(add-to-list 'auto-mode-alist '("\\.gradle\\'" . groovy-mode))
;; }}
;; {{ https://github.com/browse-kill-ring/browse-kill-ring
(require 'browse-kill-ring)
;; no duplicates
(setq browse-kill-ring-display-style 'one-line
browse-kill-ring-display-duplicates nil
;; preview is annoying
browse-kill-ring-show-preview nil)
(browse-kill-ring-default-keybindings)
;; hotkeys:
;; n/p => next/previous
;; s/r => search
;; l => filter with regex
;; g => update/refresh
;; }}
;; {{ gradle
(defun my-run-gradle-in-shell (cmd)
(interactive "sEnter a string:")
(let ((root-dir (locate-dominating-file default-directory
"build.gradle")))
(if root-dir
(let ((default-directory root-dir))
(shell-command (concat "gradle " cmd "&"))))
))
;; }}
;; {{ crontab
;; in shell "EDITOR='emacs -nw' crontab -e" to edit cron job
(add-to-list 'auto-mode-alist '("crontab.*\\'" . crontab-mode))
;; }}
;; cmake
(setq auto-mode-alist (append '(("CMakeLists\\.txt\\'" . cmake-mode))
'(("\\.cmake\\'" . cmake-mode))
auto-mode-alist))
(defun back-to-previous-buffer ()
(interactive)
(switch-to-buffer nil))
;; {{ dictionary setup
(defun my-lookup-dict-org ()
(interactive)
(dictionary-new-search (cons (my-use-selected-string-or-ask "Input word for dict.org:")
dictionary-default-dictionary)))
;; }}
;; {{ bookmark
;; use my own bmk if it exists
(if (file-exists-p (file-truename "~/.emacs.bmk"))
(setq bookmark-file (file-truename "~/.emacs.bmk")))
;; }}
(defun insert-lorem ()
(interactive)
(insert "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sem mauris, aliquam vel interdum in, faucibus non libero. Asunt in anim uis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in anim id est laborum. Allamco laboris nisi ut aliquip ex ea commodo consequat."))
(defun my-gud-gdb ()
(interactive)
(gud-gdb (concat "gdb --fullname \"" (cppcm-get-exe-path-current-buffer) "\"")))
(defun my-overview-of-current-buffer ()
(interactive)
(set-selective-display (if selective-display nil 1)))
(defun lookup-doc-in-man ()
(interactive)
(man (concat "-k " (my-use-selected-string-or-ask ""))))
;; @see http://blog.binchen.org/posts/effective-code-navigation-for-web-development.html
;; don't let the cursor go into minibuffer prompt
(setq minibuffer-prompt-properties (quote (read-only t point-entered minibuffer-avoid-prompt face minibuffer-prompt)))
;; Don't echo passwords when communicating with interactive programs:
;; Github prompt is like "Password for 'https://user@github.com/':"
(setq comint-password-prompt-regexp (format "%s\\|^ *Password for .*: *$" comint-password-prompt-regexp))
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt)
;; {{ which-key-mode
(require 'which-key)
(setq which-key-allow-imprecise-window-fit t) ; performance
(setq which-key-separator ":")
(which-key-mode 1)
;; }}
;; smex or counsel-M-x?
(defvar my-use-smex nil
"Use `smex' instead of `counsel-M-x' when press M-x.")
(defun my-M-x ()
(interactive)
(cond
(my-use-smex
(smex))
((fboundp 'counsel-M-x)
;; `counsel-M-x' will use `smex' to remember history
(counsel-M-x))
((fboundp 'smex)
(smex))
(t
(execute-extended-command))))
(global-set-key (kbd "M-x") 'my-M-x)
(global-set-key (kbd "C-x C-m") 'my-M-x)
(defun compilation-finish-hide-buffer-on-success (buf str)
"Could be reused by other major-mode after compilation."
(if (string-match "exited abnormally" str)
;;there were errors
(message "compilation errors, press C-x ` to visit")
;;no errors, make the compilation window go away in 0.5 seconds
(when (and (buffer-name buf)
(string-match "*compilation*" (buffer-name buf)))
;; @see http://emacswiki.org/emacs/ModeCompile#toc2
(bury-buffer "*compilation*")
(winner-undo)
(message "NO COMPILATION ERRORS!"))))
(defun generic-prog-mode-hook-setup ()
;; turn off `linum-mode' when there are more than 5000 lines
(if (buffer-too-big-p) (linum-mode -1))
(unless (is-buffer-file-temp)
;; @see http://xugx2007.blogspot.com.au/2007/06/benjamin-rutts-emacs-c-development-tips.html
(setq compilation-window-height 8)
(setq compilation-finish-functions
'(compilation-finish-hide-buffer-on-success))
;; fic-mode has performance issue on 5000 line C++, we can always use swiper instead
;; don't spell check double words
(setq flyspell-check-doublon nil)
;; enable for all programming modes
;; http://emacsredux.com/blog/2013/04/21/camelcase-aware-editing/
(subword-mode)
(setq-default electric-pair-inhibit-predicate 'electric-pair-conservative-inhibit)
(electric-pair-mode 1)
;; eldoc, show API doc in minibuffer echo area
;; (turn-on-eldoc-mode)
;; show trailing spaces in a programming mod
(setq show-trailing-whitespace t)))
(add-hook 'prog-mode-hook 'generic-prog-mode-hook-setup)
;; some major-modes NOT inherited from prog-mode
(add-hook 'css-mode-hook 'generic-prog-mode-hook-setup)
;; {{ display long lines in truncated style (end line with $)
(defun truncate-lines-setup ()
(toggle-truncate-lines 1))
(add-hook 'grep-mode-hook 'truncate-lines-setup)
;; (add-hook 'org-mode-hook 'truncate-lines-setup)
;; }}
;; turns on auto-fill-mode, don't use text-mode-hook because for some
;; mode (org-mode for example), this will make the exported document
;; ugly!
;; (add-hook 'markdown-mode-hook 'turn-on-auto-fill)
(add-hook 'change-log-mode-hook 'turn-on-auto-fill)
(add-hook 'cc-mode-hook 'turn-on-auto-fill)
(global-set-key (kbd "C-c q") 'auto-fill-mode)
;; some project prefer tab, so be it
;; @see http://stackoverflow.com/questions/69934/set-4-space-indent-in-emacs-in-text-mode
(setq-default tab-width 4)
(setq history-delete-duplicates t)
;;----------------------------------------------------------------------------
(fset 'yes-or-no-p 'y-or-n-p)
;; NO automatic new line when scrolling down at buffer bottom
(setq next-line-add-newlines nil)
;; @see http://stackoverflow.com/questions/4222183/emacs-how-to-jump-to-function-definition-in-el-file
(global-set-key (kbd "C-h C-f") 'find-function)
;; {{ time format
;; If you want to customize time format, read documantation of `format-time-string'
;; and customize `display-time-format'.
;; (setq display-time-format "%a %b %e")
;; from RobinH, Time management
(setq display-time-24hr-format t) ; the date in modeline is English too, magic!
(setq display-time-day-and-date t)
(display-time) ; show date in modeline
;; }}
;;a no-op function to bind to if you want to set a keystroke to null
(defun void () "this is a no-op" (interactive))
(defalias 'list-buffers 'ibuffer)
;effective emacs item 7; no scrollbar, no menubar, no toolbar
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(defun my-download-subtitles ()
(interactive)
(shell-command "periscope.py -l en *.mkv *.mp4 *.avi &"))
;; {{ @see http://emacsredux.com/blog/2013/04/21/edit-files-as-root/
(defun sudo-edit (&optional arg)
"Edit currently visited file as root.
With a prefix ARG prompt for a file to visit.
Will also prompt for a file to visit if current
buffer is not visiting a file.
You may insert below line into ~/.authinfo.gpg to type less:
machine 127.0.0.1 login root password ****** port sudo
See \"Reusing passwords for several connections\" from INFO.
"
(interactive "P")
(if (or arg (not buffer-file-name))
(find-file (concat "/sudo:root@127.0.0.1:"
(read-file-name "Find file(as root): ")))
(find-alternate-file (concat "/sudo:@127.0.0.1:"
buffer-file-name))))
(defadvice ido-find-file (after find-file-sudo activate)
"Find file as root if necessary."
(if (and (not (and buffer-file-name
(file-writable-p buffer-file-name)))
;; sudo edit only physical file
buffer-file-name
;; sudo edit only /etc/**/*
(string-match-p "^/etc/" buffer-file-name))
(find-alternate-file (concat "/sudo:root@127.0.0.1:"
buffer-file-name))))
;; }}
;; edit confluence wiki
(add-to-list 'auto-mode-alist '("\\.wiki\\'" . confluence-edit-mode))
(defun erase-specific-buffer (num buf-name)
(let ((message-buffer (get-buffer buf-name))
(old-buffer (current-buffer)))
(save-excursion
(if (buffer-live-p message-buffer)
(progn
(switch-to-buffer message-buffer)
(if (not (null num))
(progn
(end-of-buffer)
(dotimes (i num)
(previous-line))
(set-register t (buffer-substring (point) (point-max)))
(erase-buffer)
(insert (get-register t))
(switch-to-buffer old-buffer))
(progn
(erase-buffer)
(switch-to-buffer old-buffer))))
(error "Message buffer doesn't exists!")
))))
;; {{ message buffer things
(defun erase-message-buffer (&optional num)
"Erase the content of the *Messages* buffer in emacs.
Keep the last num lines if argument num if given."
(interactive "p")
(let ((buf (cond
((eq 'ruby-mode major-mode) "*server*")
(t "*Messages*"))))
(erase-specific-buffer num buf)))
;; turn off read-only-mode in *Message* buffer, a "feature" in v24.4
(when (fboundp 'messages-buffer-mode)
(defun messages-buffer-mode-hook-setup ()
(message "messages-buffer-mode-hook-setup called")
(read-only-mode -1))
(add-hook 'messages-buffer-mode-hook 'messages-buffer-mode-hook-setup))
;; }}
;; increase and decrease font size in GUI emacs
(when (display-graphic-p)
(global-set-key (kbd "C-=") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease))
;; vimrc
(add-to-list 'auto-mode-alist '("\\.?vim\\(rc\\)?$" . vimrc-mode))
;; {{ show email sent by `git send-email' in gnus
(eval-after-load 'gnus
'(progn
(require 'gnus-article-treat-patch)
(setq gnus-article-patch-conditions
'( "^@@ -[0-9]+,[0-9]+ \\+[0-9]+,[0-9]+ @@" ))
))
;; }}
(defun toggle-full-window()
"Toggle the full view of selected window"
(interactive)
;; @see http://www.gnu.org/software/emacs/manual/html_node/elisp/Splitting-Windows.html
(if (window-parent)
(delete-other-windows)
(winner-undo)
))
(defun add-pwd-into-load-path ()
"add current directory into load-path, useful for elisp developers"
(interactive)
(let ((dir (expand-file-name default-directory)))
(if (not (memq dir load-path))
(add-to-list 'load-path dir)
)
(message "Directory added into load-path:%s" dir)
)
)
(setq system-time-locale "C")
(setq imenu-max-item-length 256)
;; {{ recentf-mode
(setq recentf-keep '(file-remote-p file-readable-p))
(setq recentf-max-saved-items 2048
recentf-exclude '("/tmp/"
"/ssh:"
"/sudo:"
;; ctags
"/TAGS$"
;; global
"/GTAGS$"
"/GRAGS$"
"/GPATH$"
;; binary
"\\.mkv$"
"\\.mp[34]$"
"\\.avi$"
"\\.pdf$"
;; sub-titles
"\\.sub$"
"\\.srt$"
"\\.ass$"
;; ~/.emacs.d/**/*.el included
;; "/home/[a-z]\+/\\.[a-df-z]" ; configuration file should not be excluded
))
;; }}
;; {{ popup functions
(defun my-which-file ()
"Return current file name for Yasnippets."
(if (buffer-file-name) (format "%s:" (file-name-nondirectory (buffer-file-name)))
""))
(defun my-which-function ()
"Return current function name."
;; clean the imenu cache
;; @see http://stackoverflow.com/questions/13426564/how-to-force-a-rescan-in-imenu-by-a-function
(setq imenu--index-alist nil)
(which-function))
(defun popup-which-function ()
(interactive)
(let ((msg (my-which-function)))
(popup-tip msg)
(copy-yank-str msg)))
;; }}
;; {{ music
(defun mpc-which-song ()
(interactive)
(let ((msg (car (split-string (shell-command-to-string "mpc") "\n+"))))
(message msg)
(copy-yank-str msg)))
(defun mpc-next-prev-song (&optional prev)
(interactive)
(message (car (split-string (shell-command-to-string
(concat "mpc " (if prev "prev" "next"))) "\n+"))))
(defun lyrics()
"Prints the lyrics for the current song"
(interactive)
(let ((song (shell-command-to-string "lyrics")))
(if (equal song "")
(message "No lyrics - Opening browser.")
(switch-to-buffer (create-file-buffer "Lyrics"))
(insert song)
(goto-line 0))))
;; }}
;; @see http://www.emacswiki.org/emacs/EasyPG#toc4
;; besides, use gnupg 1.4.9 instead of 2.0
(defadvice epg--start (around advice-epg-disable-agent disable)
"Make epg--start not able to find a gpg-agent"
(let ((agent (getenv "GPG_AGENT_INFO")))
(setenv "GPG_AGENT_INFO" nil)
ad-do-it
(setenv "GPG_AGENT_INFO" agent)))
;; https://github.com/abo-abo/ace-window
;; `M-x ace-window ENTER m` to swap window
(global-set-key (kbd "C-x o") 'ace-window)
;; {{ move focus between sub-windows
(require 'window-numbering)
(custom-set-faces '(window-numbering-face ((t (:foreground "DeepPink" :underline "DeepPink" :weight bold)))))
(window-numbering-mode 1)
;; }}
;; {{ avy, jump between texts, like easymotion in vim
;; @see http://emacsredux.com/blog/2015/07/19/ace-jump-mode-is-dead-long-live-avy/ for more tips
;; dired
(eval-after-load "dired"
'(progn
(define-key dired-mode-map (kbd ";") 'avy-goto-subword-1)))
;; }}
;; ANSI-escape coloring in compilation-mode
;; {{ http://stackoverflow.com/questions/13397737/ansi-coloring-in-compilation-mode
(ignore-errors
(require 'ansi-color)
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
(add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
;; }}
;; @see http://emacs.stackexchange.com/questions/14129/which-keyboard-shortcut-to-use-for-navigating-out-of-a-string
(defun font-face-is-similar (f1 f2)
(let (rlt)
;; (message "f1=%s f2=%s" f1 f2)
;; in emacs-lisp-mode, the '^' from "^abde" has list of faces:
;; (font-lock-negation-char-face font-lock-string-face)
(if (listp f1) (setq f1 (nth 1 f1)))
(if (listp f2) (setq f2 (nth 1 f2)))
(if (eq f1 f2) (setq rlt t)
;; C++ comment has different font face for limit and content
;; f1 or f2 could be a function object because of rainbow mode
(if (and (string-match "-comment-" (format "%s" f1)) (string-match "-comment-" (format "%s" f2)))
(setq rlt t)))
rlt))
;; {{
(defun goto-edge-by-comparing-font-face (&optional step)
"Goto either the begin or end of string/comment/whatever.
If step is -1, go backward."
(interactive "P")
(let ((cf (get-text-property (point) 'face))
(p (point))
rlt
found
end)
(unless step (setq step 1)) ;default value
(setq end (if (> step 0) (point-max) (point-min)))
(while (and (not found) (not (= end p)))
(if (not (font-face-is-similar (get-text-property p 'face) cf))
(setq found t)
(setq p (+ p step))))
(if found (setq rlt (- p step))
(setq rlt p))
;; (message "rlt=%s found=%s" rlt found)
(goto-char rlt)))
;; }}
(defun my-minibuffer-setup-hook ()
(local-set-key (kbd "M-y") 'paste-from-x-clipboard)
(local-set-key (kbd "C-k") 'kill-line)
(setq gc-cons-threshold most-positive-fixnum))
(defun my-minibuffer-exit-hook ()
;; evil-mode also use minibuf
(setq gc-cons-threshold best-gc-cons-threshold))
;; @see http://bling.github.io/blog/2016/01/18/why-are-you-changing-gc-cons-threshold/
(add-hook 'minibuffer-setup-hook #'my-minibuffer-setup-hook)
(add-hook 'minibuffer-exit-hook #'my-minibuffer-exit-hook)
;; {{ string-edit-mode
(defun string-edit-at-point-hook-setup ()
(let ((major-mode-list (remove major-mode '(web-mode js2-mode js-mode css-mode emacs-lisp-mode)))
(str (my-buffer-str)))
;; (ivy-read "directories:" collection :action 'dired)
;; (message "original=%s" (se/find-original))
;; (message "major-mode-list=%s major-mode=%s" major-mode-list major-mode)
(save-excursion
(cond
((string-match-p "<[a-zA-Z]" str)
(web-mode))
((string-match-p "function(\\| var \\|" str)
(js-mode))))))
(add-hook 'string-edit-at-point-hook 'string-edit-at-point-hook-setup)
;; }}
;; Diff two regions
;; Step 1: Select a region and `M-x diff-region-tag-selected-as-a'
;; Step 2: Select another region and `M-x diff-region-compare-with-b'
;; Press "q" in evil-mode or "C-c C-c" to exit the diff output buffer
(defun diff-region-format-region-boundary (b e)
"Make sure lines are selected and B is less than E"
(let (tmp rlt)
;; swap b e, make sure b < e
(when (> b e)
(setq tmp b)
(setq b e)
(set e tmp))
;; select lines
(save-excursion
;; Another workaround for evil-visual-line bug:
;; In evil-mode, if we use hotkey V or `M-x evil-visual-line` to select line,
;; the (line-beginning-position) of the line which is after the last selected
;; line is always (region-end)! Don't know why.
(if (and (> e b)
(save-excursion (goto-char e) (= e (line-beginning-position)))
(boundp 'evil-state) (eq evil-state 'visual))
(setq e (1- e)))
(goto-char b)
(setq b (line-beginning-position))
(goto-char e)
(setq e (line-end-position)))
(setq rlt (list b e))
rlt))
(defun diff-region-tag-selected-as-a ()
"Select a region to compare"
(interactive)
(when (region-active-p)
(let (tmp buf)
;; select lines
(setq tmp (diff-region-format-region-boundary (region-beginning) (region-end)))
(setq buf (get-buffer-create "*Diff-regionA*"))
(save-current-buffer
(set-buffer buf)
(erase-buffer))
(append-to-buffer buf (car tmp) (cadr tmp))))
(message "Now select other region to compare and run `diff-region-compare-with-b`"))
(defun diff-region-compare-with-b ()
"Compare current region with region selected by `diff-region-tag-selected-as-a' "
(interactive)
(if (region-active-p)
(let (rlt-buf
diff-output
(fa (make-temp-file (expand-file-name "scor"
(or small-temporary-file-directory
temporary-file-directory))))
(fb (make-temp-file (expand-file-name "scor"
(or small-temporary-file-directory
temporary-file-directory)))))
;; save current content as file B
(when fb
(setq tmp (diff-region-format-region-boundary (region-beginning) (region-end)))
(write-region (car tmp) (cadr tmp) fb))
(when (and fa (file-exists-p fa) fb (file-exists-p fb))
;; save region A as file A
(save-current-buffer
(set-buffer (get-buffer-create "*Diff-regionA*"))
(write-region (point-min) (point-max) fa))
;; diff NOW!
;; show the diff output
(if (string= (setq diff-output (shell-command-to-string (format "diff -Nabur %s %s" fa fb))) "")
;; two regions are same
(message "Two regions are SAME!")
;; show the diff
(diff-region-open-diff-output diff-output
"*Diff-region-output*")))
;; clean the temporary files
(if (and fa (file-exists-p fa))
(delete-file fa))
(if (and fb (file-exists-p fb))
(delete-file fb)))
(message "Please select region at first!")))
;; {{ cliphist.el
(setq cliphist-use-ivy t)
(defun my-select-cliphist-item (num str)
(my-pclip str))
(setq cliphist-select-item-callback 'my-select-cliphist-item)
;; }}
(defun extract-list-from-package-json ()
"Extract package list from package.json."
(interactive)
(let* ((str (my-use-selected-string-or-ask "")))
(message "my-select-cliphist-item called => %s" str)
(setq str (replace-regexp-in-string ":.*$\\|\"" "" str))
;; join lines
(setq str (replace-regexp-in-string "[\r\n \t]+" " " str))
(copy-yank-str str)
(message "%s => clipboard & yank ring" str)))
(defun pabs()
"Relative path to full path."
(interactive)
(let* ((str (my-use-selected-string-or-ask "Input relative path:"))
(path (file-truename str)))
(copy-yank-str path)
(message "%s => clipboard & yank ring" path)))
(defun prel()
"Full path to relative path."
(interactive)
(let* ((str (my-use-selected-string-or-ask "Input absolute path:"))
(path (file-relative-name str)))
(copy-yank-str path)
(message "%s => clipboard & yank ring" path)))
;; indention management
(defun my-toggle-indentation ()
(interactive)
(setq indent-tabs-mode (not indent-tabs-mode))
(message "indent-tabs-mode=%s" indent-tabs-mode))
;; {{ auto-save.el
(require 'auto-save)
(auto-save-enable)
(setq auto-save-slient t)
;; }}
;; {{ csv
(add-auto-mode 'csv-mode "\\.[Cc][Ss][Vv]\\'")
(setq csv-separators '("," ";" "|" " "))
;; }}
;; {{ regular expression tools
(defun my-create-regex-from-kill-ring (&optional n)
"Create extended regex from first N items of `kill-ring'."
(interactive "p")
(when (and kill-ring (> (length kill-ring) 0))
(if (> n (length kill-ring))
(setq n (length kill-ring)))
(let* ((rlt (mapconcat 'identity (subseq kill-ring 0 n) "|")))
(setq rlt (replace-regexp-in-string "(" "\\\\(" rlt))
(copy-yank-str rlt)
(message (format "%s => kill-ring&clipboard" rlt)))))
;; }}
;; {{ perforce utilities
(defvar p4-file-to-url '("" "")
"(car p4-file-to-url) is the original file prefix
(cadr p4-file-to-url) is the url prefix")
(defun p4-current-file-url ()
(replace-regexp-in-string (car p4-file-to-url)
(cadr p4-file-to-url)
buffer-file-name))
(defun p4-generate-cmd (opts)
(format "p4 %s %s" opts (p4-current-file-url)))
(defun p4edit ()
"p4 edit current file."
(interactive)
(shell-command (p4-generate-cmd "edit"))
(read-only-mode -1))
(defun p4submit (&optional file-opened)
"p4 submit current file.
If FILE-OPENED, current file is still opened."
(interactive "P")
(let* ((msg (read-string "Say (ENTER to abort):"))
(open-opts (if file-opened "-f leaveunchanged+reopen -r" ""))
(full-opts (format "submit -d '%s' %s" msg open-opts)))
;; (message "(p4-generate-cmd full-opts)=%s" (p4-generate-cmd full-opts))
(if (string= "" msg)
(message "Abort submit.")
(shell-command (p4-generate-cmd full-opts))
(unless file-opened (read-only-mode 1))
(message (format "%s submitted."
(file-name-nondirectory buffer-file-name))))))
(defun p4revert ()
"p4 revert current file."
(interactive)
(shell-command (p4-generate-cmd "revert"))
(read-only-mode 1))
(defun p4-show-changelist-patch (line)
(let* ((chg (nth 1 (split-string line "[\t ]+")))
(url (p4-current-file-url))
(pattern "^==== //.*====$")
sep
seps
(start 0)
(original (if chg (shell-command-to-string (format "p4 describe -du %s" chg)) ""))
rlt)
(while (setq sep (string-match pattern original start))
(let* ((str (match-string 0 original)))
(setq start (+ sep (length str)))
(add-to-list 'seps (list sep str) t)))
(setq rlt (substring original 0 (car (nth 0 seps))))
(let* ((i 0) found)
(while (and (not found)
(< i (length seps)))
(when (string-match url (cadr (nth i seps)))
(setq rlt (concat rlt (substring original
(car (nth i seps))
(if (= i (- (length seps) 1))
(length original)
(car (nth (+ 1 i) seps))))))
;; out of loop now since current file patch found
(setq found t))
(setq i (+ 1 i))))
;; remove p4 verbose bullshit
(setq rlt (replace-regexp-in-string "^\\(Affected\\|Moved\\) files \.\.\.[\r\n]+\\(\.\.\. .*[\r\n]+\\)+"
""
rlt))
(setq rlt (replace-regexp-in-string "Differences \.\.\.[\r\n]+" "" rlt))
;; one line short description of change list
(setq rlt (replace-regexp-in-string "Change \\([0-9]+\\) by \\([^ @]+\\)@[^ @]+ on \\([^ \r\n]*\\).*[\r\n \t]+\\([^ \t].*\\)" "\\1 by \\2@\\3 \\4" rlt))
rlt))
(defun p4--create-buffer (buf-name content &optional enable-imenu)
(let* (rlt-buf)
(if (get-buffer buf-name)
(kill-buffer buf-name))
(setq rlt-buf (get-buffer-create buf-name))
(save-current-buffer
(switch-to-buffer-other-window rlt-buf)
(set-buffer rlt-buf)
(erase-buffer)
(insert content)
(diff-mode)
(goto-char (point-min))
;; nice imenu output
(if enable-imenu
(setq imenu-create-index-function
(lambda ()
(save-excursion
(imenu--generic-function '((nil "^[0-9]+ by .*" 0)))))))
;; quit easily in evil-mode
(evil-local-set-key 'normal "q" (lambda () (interactive) (quit-window t))))))
(defun p4diff ()
"Show diff of current file like `git diff'."
(interactive)
(let* ((content (shell-command-to-string (p4-generate-cmd "diff -du -db"))))
(p4--create-buffer "*p4diff*" content)))
(defun p4history ()
"Show history of current file like `git log -p'."
(interactive)
(let* ((changes (split-string (shell-command-to-string (p4-generate-cmd "changes")) "\n"))
(content (mapconcat 'p4-show-changelist-patch
changes
"\n\n")))
(p4--create-buffer "*p4log*" content t)))
;; }}
(defun my-get-total-hours ()
(interactive)
(let* ((str (if (region-active-p) (my-selected-str)
(my-buffer-str)))
(total-hours 0)
(lines (split-string str "\n")))
(dolist (l lines)
(if (string-match " \\([0-9][0-9.]*\\)h[ \t]*$" l)
(setq total-hours (+ total-hours (string-to-number (match-string 1 l))))))
(message "total-hours=%s" total-hours)))
;; {{ emmet (auto-complete html tags)
;; @see https://github.com/rooney/zencoding for original tutorial
;; @see https://github.com/smihica/emmet for new tutorial
;; C-j or C-return to expand the line
(add-hook 'html-mode-hook 'emmet-mode)
(add-hook 'sgml-mode-hook 'emmet-mode)
(add-hook 'web-mode-hook 'emmet-mode)
(add-hook 'css-mode-hook 'emmet-mode)
(add-hook 'rjsx-mode-hook 'emmet-mode)
;; }}
(autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
(add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
;; {{ xterm
(defun run-after-make-frame-hooks (frame)
(select-frame frame)
(unless window-system
;; Mouse in a terminal (Use shift to paste with middle button)
(xterm-mouse-mode 1)))
(add-hook 'after-make-frame-functions 'run-after-make-frame-hooks)
;; }}
;; flymake
(setq flymake-gui-warnings-enabled nil)
;; {{ check attachments
(defun my-message-current-line-cited-p ()
"Indicate whether the line at point is a cited line."
(save-match-data
(string-match (concat "^" message-cite-prefix-regexp)
(buffer-substring (line-beginning-position) (line-end-position)))))
(defun my-message-says-attachment-p ()
"Return t if the message suggests there can be an attachment."
(save-excursion
(goto-char (point-min))
(save-match-data
(let (search-result)
(while
(and (setq search-result (re-search-forward "\\(attach\\|pdf\\|file\\|screen ?shot\\)" nil t))
(my-message-current-line-cited-p)))
search-result))))
(defun my-message-has-attachment-p ()
"Return t if the message has an attachment."
(save-excursion
(goto-char (point-min))
(save-match-data
(re-search-forward "<#part" nil t))))
(defun my-message-pre-send-check-attachment ()
(when (and (my-message-says-attachment-p)
(not (my-message-has-attachment-p)))
(unless
(y-or-n-p "The message suggests that you may want to attach something, but no attachment is found. Send anyway?")
(error "It seems that an attachment is needed, but none was found. Aborting sending."))))
(add-hook 'message-send-hook 'my-message-pre-send-check-attachment)
;; }}
(provide 'init-misc)