emacs - rainbow-delimiters package to color parentheses
[dotfiles.git] / emacs.d_2 / config.org
1 #+TITLE: Emacs Configuration file
2 #+AUTHOR: Peng Li
3 #+EMAIL: seudut@gmail.com
4
5 * Introduction
6
7 Most config are just copied from [[https://github.com/howardabrams/dot-files][howardabrams]]'s and [[https://github.com/abo-abo/oremacs][abo-abo's]] dotfiles
8
9 * Basic Settings
10
11 ** Setting loading Path
12
13 Set system PATH and emacs exec path
14
15 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
16
17   (setenv "PATH" (concat (getenv "PATH")
18                          ":" "/usr/local/bin"
19                          ":" "/Library/TeX/texbin"))
20   (setq exec-path (append exec-path '("/usr/local/bin")))
21   (setq exec-path (append exec-path '("/Library/TeX/texbin/")))
22
23 #+END_SRC
24
25 Set the emacs load path
26
27 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
28
29   (add-to-list 'load-path "~/.emacs.d/elisp")
30
31 #+END_SRC
32
33 ** Package Initialization
34
35 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
36
37   (require 'package)
38
39   (setq package-archives '(("mepla" . "http://melpa.milkbox.net/packages/")
40                            ("gnu" . "http://elpa.gnu.org/packages/")
41                            ("org" . "http://orgmode.org/elpa/")))
42
43   (package-initialize)
44
45 #+END_SRC       
46
47 ** General Setting
48
49 Disable scroll bar, tool-bar and menu-bar
50
51 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
52
53   (scroll-bar-mode 0)
54   (tool-bar-mode 0)
55   (menu-bar-mode 1)
56
57   (setq debug-on-error t)
58   (setq inhibit-startup-message t)
59
60   (defalias 'yes-or-no-p 'y-or-n-p)
61   (show-paren-mode 1)
62
63
64
65 #+END_SRC
66
67 Switch the focus to help window when it appears
68
69 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
70
71   (setq help-window-select t)
72
73 #+END_SRC
74
75 * Package Management Tools
76
77 ** Use-package
78
79 Using [[https://github.com/jwiegley/use-package][use-package]] to manage emacs packages
80
81 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
82
83   (unless (package-installed-p 'use-package)
84     (package-refresh-contents)
85     (package-install 'use-package))
86
87   (require 'use-package)
88
89 #+END_SRC
90
91 ** El-get
92
93 [[https://github.com/dimitri/el-get][El-get]] is package management tool, whicl allows to install external elisp package from any git repository not in mepla. 
94 Check out [[http://tapoueh.org/emacs/el-get.html][el-get]].
95
96 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
97
98   (use-package el-get
99     :ensure t
100     :init
101     (add-to-list 'load-path "~/.emacs.d/el-get"))
102
103 #+END_SRC
104
105 * Color and Fonts Settings
106
107 ** highlight current line
108
109 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
110
111   (global-hl-line-mode)
112
113 #+END_SRC
114
115 ** Smart Comments
116
117 [[https://github.com/paldepind/smart-comment][smart-comments]]
118
119 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
120
121   (use-package smart-comment
122     :ensure t
123     :bind ("M-;" . smart-conmment))
124
125 #+END_SRC
126
127 ** Font Setting
128
129 syntax highlighting
130
131 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
132
133   (global-font-lock-mode 1)
134
135 #+END_SRC
136
137 [[https://github.com/i-tu/Hasklig][Hasklig]] and Source Code Pro, defined fonts family
138
139 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
140
141   (if window-system
142       (defvar sd/fixed-font-family
143         (cond ((x-list-fonts "Hasklig")         "Hasklig")
144               ((x-list-fonts "Source Code Pro") "Source Code Pro")
145               ((x-list-fonts "Anonymous Pro")   "Anonymous Pro")
146               ((x-list-fonts "M+ 1mn")          "M+ 1mn"))
147         "The fixed width font based on what is installed, `nil' if not defined."))
148
149 #+END_SRC
150
151 Setting the fonts 
152
153 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
154
155   (if window-system
156       (when sd/fixed-font-family
157         (set-frame-font sd/fixed-font-family)
158         (set-face-attribute 'default nil :font sd/fixed-font-family :height 120)
159         (set-face-font 'default sd/fixed-font-family)))
160
161 #+END_SRC
162
163 ** Color Theme
164
165 Loading theme should be after all required loaded, refere [[https://github.com/jwiegley/use-package][:defer]] in =use-package=
166
167 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
168
169   (setq vc-follow-symlinks t)
170
171   (use-package color-theme
172     :ensure t
173     :init (require 'color-theme)
174     :config (use-package color-theme-sanityinc-tomorrow
175               :ensure t
176               :no-require t
177               :config
178               (load-theme 'sanityinc-tomorrow-bright t)))
179
180   ;(eval-after-load 'color-theme
181   ;  (load-theme 'sanityinc-tomorrow-bright t))
182
183 #+END_SRC
184
185 Change the Org-mode colors 
186
187 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
188
189   (defun org-src-color-blocks-light ()
190     "Colors the block headers and footers to make them stand out more for lighter themes"
191     (interactive)
192     (custom-set-faces
193      '(org-block-begin-line
194       ((t (:underline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
195      '(org-block-background
196        ((t (:background "#FFFFEA"))))
197      '(org-block
198        ((t (:background "#FFFFEA"))))
199      '(org-block-end-line
200        ((t (:overline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
201
202      '(mode-line-buffer-id ((t (:foreground "#005000" :bold t))))
203      '(which-func ((t (:foreground "#008000"))))))
204
205   (defun org-src-color-blocks-dark ()
206     "Colors the block headers and footers to make them stand out more for dark themes"
207     (interactive)
208     (custom-set-faces
209      '(org-block-begin-line
210        ((t (:foreground "#008ED1" :background "#002E41"))))
211      '(org-block-background
212        ((t (:background "#000000"))))
213      '(org-block
214        ((t (:background "#000000"))))
215      '(org-block-end-line
216        ((t (:foreground "#008ED1" :background "#002E41"))))
217
218      '(mode-line-buffer-id ((t (:foreground "black" :bold t))))
219      '(which-func ((t (:foreground "green"))))))
220
221   (org-src-color-blocks-dark)
222
223 #+END_SRC
224
225 improve color for org-mode
226 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
227   (deftheme ha/org-theme "Sub-theme to beautify org mode")
228
229   (if window-system
230       (defvar sd/variable-font-tuple
231         (cond ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
232               ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
233               ((x-list-fonts "Verdana")         '(:font "Verdana"))
234               ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
235               (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro.")))
236         "My variable width font available to org-mode files and whatnot."))
237
238   (defun sd/org-color ()
239     (let* ((sd/fixed-font-tuple (list :font sd/fixed-font-family))
240            (base-font-color     (face-foreground 'default nil 'default))
241            (background-color    (face-background 'default nil 'default))
242            (primary-color       (face-foreground 'mode-line nil))
243            (secondary-color     (face-background 'secondary-selection nil 'region))
244            (base-height         (face-attribute 'default :height))
245            (headline           `(:inherit default :weight bold :foreground ,base-font-color)))
246       (custom-theme-set-faces 'ha/org-theme
247                               `(org-agenda-structure ((t (:inherit default :height 2.0 :underline nil))))
248                               `(org-verbatim ((t (:inherit 'fixed-pitched :foreground "#aef"))))
249                               `(org-table ((t (:inherit 'fixed-pitched))))
250                               `(org-block ((t (:inherit 'fixed-pitched))))
251                               `(org-block-background ((t (:inherit 'fixed-pitched))))
252                               `(org-block-begin-line ((t (:inherit 'fixed-pitched))))
253                               `(org-block-end-line ((t (:inherit 'fixed-pitched))))
254                               `(org-level-8 ((t (,@headline ,@sd/variable-font-tuple))))
255                               `(org-level-7 ((t (,@headline ,@sd/variable-font-tuple))))
256                               `(org-level-6 ((t (,@headline ,@sd/variable-font-tuple))))
257                               `(org-level-5 ((t (,@headline ,@sd/variable-font-tuple))))
258                               `(org-level-4 ((t (,@headline ,@sd/variable-font-tuple
259                                                             :height ,(round (* 1.1 base-height))))))
260                               `(org-level-3 ((t (,@headline ,@sd/variable-font-tuple
261                                                             :height ,(round (* 1.25 base-height))))))
262                               `(org-level-2 ((t (,@headline ,@sd/variable-font-tuple
263                                                             :height ,(round (* 1.5 base-height))))))
264                               `(org-level-1 ((t (,@headline ,@sd/variable-font-tuple
265                                                             :height ,(round (* 1.75 base-height))))))
266                               `(org-document-title ((t (,@headline ,@sd/variable-font-tuple :height 1.5 :underline nil)))))))
267
268
269 #+END_SRC
270
271 ** Rainbow-delimiter
272
273 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
274
275   (use-package rainbow-delimiters
276     :ensure t
277     :init
278     (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))
279
280 #+END_SRC
281 * Org-mode Settings
282
283 ** Org-mode Basic setting
284
285 Always indents header, and hide header leading starts so that no need type =#+STATUP: indent= 
286
287 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
288
289   (use-package org
290     :ensure t
291     :init
292     (setq org-startup-indented t)
293     (setq org-hide-leading-starts t)
294     (setq org-src-fontify-natively t)
295     (setq org-src-tab-acts-natively t)
296     (setq org-confirm-babel-evaluate nil)
297     (setq org-use-speed-commands t)
298     (setq org-completion-use-ido t))
299
300   (org-babel-do-load-languages
301    'org-babel-load-languages
302    '((python . t)
303      (C . t)
304      (perl . t)
305      (calc . t)
306      (latex . t)
307      (java . t)
308      (ruby . t)
309      (lisp . t)
310      (scheme . t)
311      (sh . t)
312      (sqlite . t)
313      (js . t)))
314
315   ;; use current window for org source buffer editting
316   (setq org-src-window-setup 'current-window )
317
318 #+END_SRC
319
320 ** Org-bullets
321
322 use [[https://github.com/sabof/org-bullets][org-bullets]] package to show utf-8 charactes
323
324 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
325
326   (use-package org-bullets
327     :ensure t
328     :init
329     (add-hook 'org-mode-hook
330               (lambda ()
331                 (org-bullets-mode t))))
332
333 #+END_SRC
334
335 ** Worf Mode
336
337 [[https://github.com/abo-abo/worf][worf]] mode is an extension of vi-like binding for org-mode. 
338 In =worf-mode=, it is mapping =[=, =]= as =worf-backward= and =worf-forward= in global, wich
339 cause we cannot input =[= and =]=, so here I unset this mappings. And redifined this two to
340 =M-[= and =M-]=. see this [[https://github.com/abo-abo/worf/issues/19#issuecomment-223756599][issue]]
341
342 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
343
344   (use-package worf
345     :ensure t
346     :commands worf-mode
347     :init (add-hook 'org-mode-hook 'worf-mode)
348     ;; :config
349     ;; (define-key worf-mode-map "[" nil)
350     ;; (define-key worf-mode-map "]" nil)
351     ;; (define-key worf-mode-map (kbd "M-[") 'worf-backward)
352     ;; (define-key worf-mode-map (kbd "M-]") 'worf-forward)
353     )
354
355 #+END_SRC
356
357 ** Task Management
358
359 ** Capture
360
361 ** Export PDF
362
363 Install MacTex-basic and some tex packages
364
365 #+BEGIN_SRC bash 
366
367   sudo tlmgr update --self
368
369   sudo tlmgr install titlesec framed threeparttable wrapfig multirow enumitem bbding titling tabu mdframed tcolorbox textpos import varwidth needspace tocloft ntheorem environ trimspaces collection-fontsrecommended capt-of
370
371 #+END_SRC
372
373 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
374   ;; ;; allow for export=>beamer by placing
375
376   ;; http://emacs-fu.blogspot.com/2011/04/nice-looking-pdfs-with-org-mode-and.html
377   ;; #+LaTeX_CLASS: beamer in org files
378   (unless (boundp 'org-export-latex-classes)
379     (setq org-export-latex-classes nil))
380   (add-to-list 'org-export-latex-classes
381     ;; beamer class, for presentations
382     '("beamer"
383        "\\documentclass[11pt]{beamer}\n
384         \\mode<{{{beamermode}}}>\n
385         \\usetheme{{{{beamertheme}}}}\n
386         \\usecolortheme{{{{beamercolortheme}}}}\n
387         \\beamertemplateballitem\n
388         \\setbeameroption{show notes}
389         \\usepackage[utf8]{inputenc}\n
390         \\usepackage[T1]{fontenc}\n
391         \\usepackage{hyperref}\n
392         \\usepackage{color}
393         \\usepackage{listings}
394         \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
395     frame=single,
396     basicstyle=\\small,
397     showspaces=false,showstringspaces=false,
398     showtabs=false,
399     keywordstyle=\\color{blue}\\bfseries,
400     commentstyle=\\color{red},
401     }\n
402         \\usepackage{verbatim}\n
403         \\institute{{{{beamerinstitute}}}}\n          
404          \\subject{{{{beamersubject}}}}\n"
405
406        ("\\section{%s}" . "\\section*{%s}")
407  
408        ("\\begin{frame}[fragile]\\frametitle{%s}"
409          "\\end{frame}"
410          "\\begin{frame}[fragile]\\frametitle{%s}"
411          "\\end{frame}")))
412
413     ;; letter class, for formal letters
414
415     (add-to-list 'org-export-latex-classes
416
417     '("letter"
418        "\\documentclass[11pt]{letter}\n
419         \\usepackage[utf8]{inputenc}\n
420         \\usepackage[T1]{fontenc}\n
421         \\usepackage{color}"
422  
423        ("\\section{%s}" . "\\section*{%s}")
424        ("\\subsection{%s}" . "\\subsection*{%s}")
425        ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
426        ("\\paragraph{%s}" . "\\paragraph*{%s}")
427        ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
428
429
430   (require 'ox-md)
431   (require 'ox-beamer)
432
433   (setq org-latex-pdf-process
434         '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
435           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
436           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
437
438   (setq TeX-parse-self t)
439
440   (setq TeX-PDF-mode t)
441   (add-hook 'LaTeX-mode-hook
442             (lambda ()
443               (LaTeX-math-mode)
444               (setq TeX-master t)))
445
446 #+END_SRC
447
448 * Magit
449
450 [[https://github.com/magit/magit][Magit]] is a very cool git interface on Emacs.
451
452 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
453
454   (use-package magit
455     :ensure t
456     :commands magit-status magit-blame)
457
458
459 #+END_SRC
460
461 * IDO & SMEX
462
463 ** IDO
464
465 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
466
467   (use-package ido
468     :ensure t
469     :init (setq ido-enable-flex-matching t
470                 ido-ignore-extensions t
471                 ido-use-virtual-buffers t
472                 ido-everywhere t)
473     :config
474     (ido-mode 1)
475     (ido-everywhere 1)
476     (add-to-list 'completion-ignored-extensions ".pyc"))
477
478   (icomplete-mode t)
479
480 #+END_SRC
481
482 ** FLX
483
484 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
485
486   (use-package flx-ido
487     :ensure t
488     :init (setq ido-enable-flex-matching t
489                 ido-use-faces nil)
490     :config (flx-ido-mode 1))
491
492 #+END_SRC
493
494 ** IDO-vertically
495
496 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
497
498   (use-package ido-vertical-mode
499     :ensure t
500     :init
501     (setq ido-vertical-define-keys 'C-n-C-p-up-and-down)
502     :config
503     (ido-vertical-mode 1))
504
505 #+END_SRC
506
507 ** SMEX
508
509 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
510
511   (use-package smex
512     :ensure t
513     :init (smex-initialize)
514     :bind
515     ("M-x" . smex)
516     ("M-X" . smex-major-mode-commands))
517
518 #+END_SRC
519
520 ** Ido-ubiquitous
521
522 Use [[https://github.com/DarwinAwardWinner/ido-ubiquitous][ido-ubiquitous]] for ido everywhere. It makes =describe-function= can also use ido
523
524 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
525
526   (use-package ido-ubiquitous
527     :ensure t
528     :init
529     (setq magit-completing-read-function 'magit-ido-completing-read)
530     (setq gnus-completing-read-function 'gnus-ido-completing-read)
531     :config
532     (ido-ubiquitous-mode 1))
533
534 #+END_SRC
535
536 ** Ido-exit-target
537
538 [[https://github.com/waymondo/ido-exit-target][ido-exit-target]] let you open file/buffer on =other-windows= when call =ido-switch-buffer=
539
540 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
541
542   (use-package ido-exit-target
543     :ensure t
544     :init
545     (define-key ido-common-completion-map (kbd "C-j") #'ido-exit-target-split-window-right)
546     (define-key ido-common-completion-map (kbd "C-l") #'ido-exit-target-split-window-below))
547
548 #+END_SRC
549
550 * Key bindings
551
552 ** Remove prefix =ESC=, refer [[http://emacs.stackexchange.com/questions/14755/how-to-remove-bindings-to-the-esc-prefix-key][here]]
553
554 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
555
556   ;; (define-key key-translation-map (kbd "ESC") (kbd "C-g"))
557
558 #+END_SRC
559
560 ** Esc on Minibuffer
561
562 Use =ESC= to exit minibuffer. Also I map =Super-h= the same as =C-g=
563
564 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
565
566   (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
567   (define-key minibuffer-local-map [escape]  'keyboard-escape-quit)
568   (define-key minibuffer-local-ns-map [escape]  'keyboard-escape-quit)
569   (define-key minibuffer-local-isearch-map [escape]  'keyboard-escape-quit)
570   (define-key minibuffer-local-completion-map [escape]  'keyboard-escape-quit)
571   (define-key minibuffer-local-must-match-map [escape]  'keyboard-escape-quit)
572   (define-key minibuffer-local-must-match-filename-map [escape]  'keyboard-escape-quit)
573   (define-key minibuffer-local-filename-completion-map [escape]  'keyboard-escape-quit)
574   (define-key minibuffer-local-filename-must-match-map [escape]  'keyboard-escape-quit)
575
576   ;; Also map s-h same as C-g
577   (define-key minibuffer-local-map (kbd "s-h") 'keyboard-escape-quit)
578
579 #+END_SRC
580
581 ** =Ctrl= key bindings
582
583 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
584   ;;
585
586   ;; C-h    help    
587   ;; C-j    newline and indent
588   ;; C-k    kill line
589   ;; C-l    recenter-top-bottom
590   ;; (global-set-key (kbd "C-;") 'ido-switch-buffer)
591   ;; C-;
592   ;; C-'   
593   ;; C-ret  
594
595   ;; C-n    next-line
596   ;; C-m
597   ;; C-,
598   ;; C-.
599   ;; C-/
600
601   ;; C-y
602   ;; C-u
603   ;; C-i
604   ;; C-o
605   ;; C-p
606   ;; C-[
607   ;; C-]
608   ;; C-\
609
610   ;; C-=
611   ;; C--
612   ;; C-0
613   ;; C-9
614   ;; C-8
615   ;; C-7
616
617   ;; C-Space
618
619
620
621
622
623
624
625 #+END_SRC
626
627 ** =Super= bindings for file, buffer and windows
628
629 Some global bindings on =Super=, on Mac, it is =Command=
630
631 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
632
633   (global-set-key (kbd "s-h") 'keyboard-quit)
634   (global-set-key (kbd "s-j") 'ido-switch-buffer)
635   (global-set-key (kbd "s-k") 'ido-find-file)
636   ;; s-k  -->  kill-this-buffer
637   (global-set-key (kbd "s-l") (lambda ()
638                                 (interactive)
639                                 (if (> (length (window-list)) 1) 
640                                     (delete-window)
641                                   (message "Only one Windows now!"))))
642   ;; s-l  -->  goto-line
643   (global-set-key (kbd "s-;") 'swiper)
644   ;; s-;  -->
645   ;; s-'  -->  'next-multiframe-window
646   (global-set-key (kbd "<s-return>") 'toggle-frame-fullscreen)
647
648   ;; (global-set-key (kbd "s-y") 'projectile-find-file)
649   (global-set-key (kbd "s-f") 'projectile-find-file)
650   (global-set-key (kbd "s-[") 'next-buffer)
651   (global-set-key (kbd "s-]") 'previous-buffer)
652
653   (global-set-key (kbd "s-`") 'mode-line-other-buffer)
654
655
656
657   ;; someothers default mapping on super (command) key
658   ;; s-s save-buffer
659   ;; s-k kill-this-buffer
660
661
662   ;; s-h  -->  ns-do-hide-emacs
663   ;; s-j  -->  ido-switch-buffer  +
664   ;; s-k  -->  kill-this-buffer
665   ;; s-l  -->  goto-line
666   ;; s-;  -->  undefined
667   ;; s-'  -->  next-multiframe-window
668   ;; s-ret --> toggle-frame-fullscreen +
669
670   ;; s-y  -->  ns-paste-secondary
671   ;; s-u  -->  revert-buffer
672   ;; s-i  -->  undefined - but used for iterm globally
673   ;; s-o  -->  used for emacs globally
674   ;; s-p  -->  projectile-persp-switch-project  +  
675   ;; s-[  -->  next-buffer  +    
676   ;; s-]  -->  previous-buffer +
677
678   ;; s-0  -->  undefined
679   ;; s-9  -->  undefined
680   ;; s-8  -->  undefined
681   ;; s-7  -->  undefined
682   ;; s-6  -->  undefined
683   ;; s--  -->  center-line
684   ;; s-=  -->  undefined
685
686   ;; s-n  -->  make-frame
687   ;; s-m  -->  iconify-frame
688   ;; s-b  -->  undefined
689   ;; s-,  -->  customize
690   ;; s-.  -->  undefined
691   ;; s-/  -->  undefined
692
693   ;; s-g  -->  isearch-repeat-forward
694   ;; s-f  -->  projectile-find-file   +
695   ;; s-d  -->  isearch-repeat-background
696   ;; s-s  -->  save-buffer
697   ;; s-a  -->  make-whole-buffer
698
699   ;; s-b  -->  undefined
700   ;; s-v  -->  yank
701   ;; s-c  -->  ns-copy-including-secondary
702
703   ;; s-t  -->  ns-popup-font-panel
704   ;; s-r  -->  undefined
705   ;; s-e  -->  isearch-yanqk-kill
706   ;; s-w  -->  delete-frame
707   ;; s-q  -->  same-buffers-kill-emacs
708
709   ;; s-`  -->  other-frame
710 #+END_SRC
711
712 ** =M-s= bindings for searching
713
714 I use the prefix =M-s= for searching in buffers
715
716 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
717
718   (defun pl-make-keymap (key bindings)
719     (setq keymap (make-sparse-keymap))
720     (dolist (binding bindings)
721       (define-key keymap (car binding) (cdr binding)))
722     (global-set-key key keymap))
723
724   (define-key minibuffer-local-map "\M-s" nil)
725
726   (global-set-key (kbd "M-s s") 'isearch-forward-regexp)
727   (define-key isearch-mode-map "\M-s" 'isearch-repeat-forward)
728   (global-set-key (kbd "M-s r") 'isearch-backward-regexp)
729   (define-key isearch-mode-map "\M-r" 'isearch-repeat-backward)
730
731   (global-set-key (kbd "s-/") 'isearch-forward-regexp)
732   (define-key isearch-mode-map (kbd  "s-/") 'isearch-repeat-forward)
733   (define-key isearch-mode-map (kbd  "C-n") 'isearch-repeat-forward)
734
735
736   (set-face-background 'ido-first-match "white")
737
738   ;; M-s o  -->  occur
739   ;; M-s s  -->  isearch-forward-regexp
740   ;; M-s r  -->  isearch-backward-regexp
741   ;; M-s w  -->  isearch-forward-word
742   ;; M-s .  -->  isearch-forward-symbol-at-point
743   ;; M-s _  -->  isearch-forward-symbol
744
745   ;; highlight bindings
746   ;; M-s h .  -->  highlight-symbol-at-point
747   ;; M-s h r  -->  highlight-regexp
748   ;; M-s h u  -->  unhighlight-regexp
749   ;; M-s h l  -->  highlight-lines-match-regexp
750   ;; M-s h p  -->  highlight-phrase
751   ;; M-s h f  -->  hi-lock-find-patterns
752
753   ;; 
754   ;; (global-set-key (kbd "M-s M-r") 'isearch-backward-regexp)
755   ;;
756
757   ;; M-c
758   ;; M-r
759   ;; M-t
760   ;; M-u, 
761 #+END_SRC
762
763 Occur search key bindings
764
765 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
766
767   (define-key occur-mode-map (kbd "C-n")
768     (lambda ()
769       (interactive)
770       (occur-next)
771       (occur-mode-goto-occurrence-other-window)
772       (recenter)
773       (other-window 1)))
774
775   (define-key occur-mode-map (kbd "C-p")
776     (lambda ()
777       (interactive)
778       (occur-prev)
779       (occur-mode-goto-occurrence-other-window)
780       (recenter)
781       (other-window 1)))
782
783 #+END_SRC
784
785
786 ** =M-o= as prefix key for windows
787
788 ** =M-g= as prefix key for launcher
789
790 ** others
791
792 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
793
794
795   ;; C-'    undefined
796   ;; C-.    undefined
797 #+END_SRC
798
799 * Eshell
800
801 Eshell alias
802
803 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
804
805   (defalias 'e 'ido-find-file)
806   (defalias 'ff 'ido-find-file)
807   (defalias 'ee 'ido-find-file-other-window)
808
809 #+END_SRC
810
811 Quickly start eshll in split window below, refer [[http://www.howardism.org/Technical/Emacs/eshell-fun.html][eshell-here]]
812
813 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
814
815   (defun eshell-x ()
816     (insert "exit")
817     (eshell-send-input)
818     (delete-window))
819
820   (defun eshell-here ()
821     "Opens up a new shell in the directory associated with the
822   current buffer's file. The eshell is renamed to match that
823   directory to make multiple eshell windows easier."
824     (interactive)
825     (let* ((parent (if (buffer-file-name)
826                        (file-name-directory (buffer-file-name))
827                      default-directory))
828            (height (/ (window-total-height) 3))
829            (name   (car (last (split-string parent "/" t))))
830            (eshell-name (concat "*eshell: " name "*")))
831       (split-window-vertically (- height))
832       (other-window 1)
833       (if (get-buffer eshell-name)
834           (progn
835             (message "buffer exist")
836             (switch-to-buffer eshell-name))
837         (progn
838           (eshell "new")
839           (rename-buffer eshell-name)
840
841           (insert (concat "ls"))
842           (eshell-send-input)))))
843
844   (global-set-key (kbd "M-`") (lambda ()
845                                 (interactive)
846                                 (eshell-here)))
847
848 #+END_SRC
849
850 * Misc Settings
851
852 ** [[https://github.com/abo-abo/hydra][Hydra]]
853
854 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
855
856   (use-package hydra
857     :ensure t)
858
859 #+END_SRC
860
861 *** Font Zoom
862
863 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
864
865   (defhydra sd/font-zoom (global-map "<f2>")
866     "zoom"
867     ("g" text-scale-increase "in")
868     ("l" text-scale-decrease "out"))
869
870 #+END_SRC
871
872 *** Windmove Splitter
873
874 Refer [[https://github.com/abo-abo/hydra/blob/master/hydra-examples.el][hydra-example]], to enlarge or shrink the windows splitter
875
876 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
877
878   (defun hydra-move-splitter-left (arg)
879     "Move window splitter left."
880     (interactive "p")
881     (if (let ((windmove-wrap-around))
882           (windmove-find-other-window 'right))
883         (shrink-window-horizontally arg)
884       (enlarge-window-horizontally arg)))
885
886   (defun hydra-move-splitter-right (arg)
887     "Move window splitter right."
888     (interactive "p")
889     (if (let ((windmove-wrap-around))
890           (windmove-find-other-window 'right))
891         (enlarge-window-horizontally arg)
892       (shrink-window-horizontally arg)))
893
894   (defun hydra-move-splitter-up (arg)
895     "Move window splitter up."
896     (interactive "p")
897     (if (let ((windmove-wrap-around))
898           (windmove-find-other-window 'up))
899         (enlarge-window arg)
900       (shrink-window arg)))
901
902   (defun hydra-move-splitter-down (arg)
903     "Move window splitter down."
904     (interactive "p")
905     (if (let ((windmove-wrap-around))
906           (windmove-find-other-window 'up))
907         (shrink-window arg)
908       (enlarge-window arg)))
909
910 #+END_SRC
911
912 *** hydra-window
913
914 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
915
916   (winner-mode 1)
917
918   (defhydra sd/hydra-window (:color red :columns nil)
919     "window"
920     ("h" windmove-left nil)
921     ("j" windmove-down nil)
922     ("k" windmove-up nil)
923     ("l" windmove-right nil)
924     ("H" hydra-move-splitter-left nil)
925     ("J" hydra-move-splitter-down nil)
926     ("K" hydra-move-splitter-up nil)
927     ("L" hydra-move-splitter-right nil)
928     ("v" (lambda ()
929            (interactive)
930            (split-window-right)
931            (windmove-right))
932      "vert")
933     ("x" (lambda ()
934            (interactive)
935            (split-window-below)
936            (windmove-down))
937      "horz")
938     ("o" delete-other-windows "one" :exit t)
939     ("a" ace-window "ace")
940     ("s" ace-swap-window "swap")
941     ("d" ace-delete-window "ace-one" :exit t)
942     ("i" ace-maximize-window "ace-one" :exit t)
943     ("b" ido-switch-buffer "buf")
944     ("m" headlong-bookmark-jump "bmk")
945     ("q" nil "cancel")
946     ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
947     ("r" (progn (winner-redo) (setq this-command 'winner-redo)) "redo")
948     ("f" nil))
949
950   (global-unset-key (kbd "M-o"))
951   (global-set-key (kbd "M-o") 'sd/hydra-window/body)
952
953   (defun triggle-windows-max-size ()
954     (interactive)
955     (if (> (length (window-list)) 1)
956         (delete-other-windows)
957       (winner-undo)))
958
959 #+END_SRC
960
961 ** Line Number
962
963 Enable linum mode on programming modes
964
965 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
966
967   (add-hook 'prog-mode-hook 'linum-mode)
968
969 #+END_SRC
970
971 Fix the font size of line number
972
973 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
974
975   (defun fix-linum-size ()
976        (interactive)
977        (set-face-attribute 'linum nil :height 110))
978
979   (add-hook 'linum-mode-hook 'fix-linum-size)
980
981 #+END_SRC
982
983 I like [[https://github.com/coldnew/linum-relative][linum-relative]], just like the =set relativenumber= on =vim=
984
985 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
986
987   (use-package linum-relative
988     :ensure t
989     :config
990     (defun linum-new-mode ()
991       "If line numbers aren't displayed, then display them.
992   Otherwise, toggle between absolute and relative numbers."
993       (interactive)
994       (if linum-mode
995           (linum-relative-toggle)
996         (linum-mode 1)))
997
998     :bind
999     ("A-k" . linum-new-mode))
1000
1001   ;; auto enable linum-new-mode in programming modes
1002   (add-hook 'prog-mode-hook 'linum-relative-mode)
1003
1004 #+END_SRC
1005
1006 ** Save File Position
1007
1008 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1009
1010   (require 'saveplace)
1011   (setq-default save-place t)
1012   (setq save-place-forget-unreadable-files t)
1013   (setq save-place-skip-check-regexp "\\`/\\(?:cdrom\\|floppy\\|mnt\\|/[0-9]\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)")
1014
1015 #+END_SRC
1016
1017 ** Multi-term
1018
1019 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1020
1021   (use-package multi-term
1022     :ensure t)
1023
1024 #+END_SRC
1025
1026 ** ace-link
1027
1028 [[https://github.com/abo-abo/ace-link][ace-link]] is a package written by [[https://github.com/abo-abo][Oleh Krehel]]. It is convenient to jump to link in help mode, info-mode, etc
1029 Type =o= to go to the link
1030
1031 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1032
1033   (use-package ace-link
1034     :ensure t
1035     :init
1036     (ace-link-setup-default))
1037
1038 #+END_SRC
1039
1040 ** Emux
1041
1042 [[https://github.com/re5et/emux][emux]] is 
1043
1044 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1045
1046   (el-get-bundle re5et/emux)
1047
1048 #+END_SRC
1049
1050 ** Smart Parens
1051
1052 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1053
1054   (use-package smartparens
1055     :ensure t
1056     :config
1057     (progn
1058       (require 'smartparens-config)
1059       (add-hook 'prog-mode-hook 'smartparens-mode)))
1060
1061 #+END_SRC
1062
1063 ** Ace-Windows
1064
1065 [[https://github.com/abo-abo/ace-window][ace-window]] 
1066
1067 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1068
1069   (use-package ace-window
1070     :ensure t
1071     :defer t
1072   ;  :init
1073   ;  (global-set-key (kbd "M-o") 'ace-window)
1074     :config
1075     (setq aw-keys '(?a ?s ?d ?f ?j ?k ?l)))
1076
1077 #+END_SRC
1078
1079 ** Projectile
1080
1081 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1082
1083   (use-package projectile
1084     :ensure t
1085     :init
1086     (setq projectile-enable-caching t)
1087     :config
1088     (projectile-global-mode t))
1089
1090   (use-package persp-projectile
1091     :ensure t
1092     :config
1093     (persp-mode)
1094     :bind
1095     (:map projectile-mode-map
1096           ("s-p" . projectile-persp-switch-project)))
1097
1098   ;; projectile-find-file
1099   ;; projectile-switch-buffer
1100   ;; projectile-find-file-other-window
1101 #+END_SRC
1102
1103 ** Which key
1104
1105 [[https://github.com/justbur/emacs-which-key][which-key]] show the key bindings 
1106
1107 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1108
1109   (use-package which-key
1110     :ensure t
1111     :config
1112     (which-key-mode))
1113
1114 #+END_SRC
1115
1116 ** Emms
1117
1118 We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
1119
1120 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1121
1122   (use-package emms
1123     :ensure t
1124     :init
1125     (setq emms-source-file-default-directory "~/Music/emms/")
1126     :config
1127     (emms-standard)
1128     (emms-default-players)
1129     (define-emms-simple-player mplayer '(file url)
1130       (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
1131                     ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
1132                     "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
1133                     ".m4a" ".flv" ".ogv" ".pls"))
1134       "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
1135     (emms-history-load))
1136
1137 #+END_SRC
1138
1139 ** GnoGo
1140
1141 Play Go in Emacs, gnugo xpm refert [[https://github.com/okanotor/dotemacs/blob/f95b774cb292d1169748bc0a62ba647bbd8c0652/etc/my-inits/my-inits-gnugo.el][to here]]. start at image display mode and grid mode
1142
1143 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1144
1145   (use-package gnugo
1146     :ensure t
1147     :defer t
1148     :init
1149     (require 'gnugo-imgen)
1150     (setq gnugo-xpms 'gnugo-imgen-create-xpms)
1151     (add-hook 'gnugo-start-game-hook '(lambda ()
1152                                         (gnugo-image-display-mode)
1153                                         (gnugo-grid-mode)))
1154       :config
1155     (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
1156
1157 #+END_SRC
1158
1159 ** undo-tree
1160
1161 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1162
1163   (use-package undo-tree
1164     :ensure t
1165     :config
1166     (global-undo-tree-mode 1))
1167
1168 #+END_SRC
1169
1170 ** swiper
1171
1172 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1173
1174   (use-package swiper
1175     :ensure t)
1176
1177
1178   (ivy-mode 1)
1179   (setq ivy-use-virtual-buffers t)
1180   (global-set-key "\C-s" 'swiper)
1181   (global-set-key (kbd "C-c C-r") 'ivy-resume)
1182   (global-set-key (kbd "<f6>") 'ivy-resume)
1183   ;; (global-set-key (kbd "M-x") 'counsel-M-x)
1184   ;; (global-set-key (kbd "C-x C-f") 'counsel-find-file)
1185   (global-set-key (kbd "<f1> f") 'counsel-describe-function)
1186   (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
1187   (global-set-key (kbd "<f1> l") 'counsel-load-library)
1188   (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
1189   (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
1190   (global-set-key (kbd "C-c g") 'counsel-git)
1191   (global-set-key (kbd "C-c j") 'counsel-git-grep)
1192   (global-set-key (kbd "C-c k") 'counsel-ag)
1193   (global-set-key (kbd "C-x l") 'counsel-locate)
1194   (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
1195   ;; (define-key read-expression-map (kbd "C-r") 'counsel-expression-history)
1196
1197 #+END_SRC
1198
1199 * Programming
1200
1201 ** Languages
1202
1203 *** Emacs Lisp
1204
1205 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1206
1207   (use-package color-identifiers-mode
1208     :ensure t
1209     :init
1210     (add-hook 'emacs-lisp-mode-hook 'color-identifiers-mode)
1211
1212     :diminish color-identifiers-mode)
1213
1214   (global-prettify-symbols-mode t)
1215
1216 #+END_SRC
1217
1218 **** Lispy Mode
1219
1220 In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
1221 bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
1222
1223 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1224
1225   (use-package lispy
1226     :ensure t
1227     :init
1228     (eval-after-load 'lispy
1229       '(progn
1230          (define-key lispy-mode-map (kbd "M-o") nil)))
1231     :config
1232     (add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1))))
1233
1234 #+END_SRC
1235
1236 *** Perl
1237
1238 [[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
1239
1240 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1241
1242   (defalias 'perl-mode 'cperl-mode)
1243
1244   ;(setq cperl-hairy t) ;; Turns on most of the CPerlMode options
1245   (setq cperl-auto-newline t)
1246   (setq cperl-highlight-variables-indiscriminately t)
1247   ;(setq cperl-indent-level 4)
1248   ;(setq cperl-continued-statement-offset 4)
1249   (setq cperl-close-paren-offset -4)
1250   (setq cperl-indent-parents-as-block t)
1251   (setq cperl-tab-always-indent t)
1252   ;(setq cperl-brace-offset  0)
1253
1254   (add-hook 'cperl-mode-hook
1255             '(lambda ()
1256                (cperl-set-style "C++")))
1257
1258   ;(require 'template)
1259   ;(template-initialize)
1260   ;(require 'perlnow)
1261
1262 #+END_SRC
1263
1264 - auto insert
1265 - run script 
1266
1267 Change the compile-command to set the default command run when call =compile=
1268 Mapping =s-r= (on Mac, it's =Command + R= to run the script. Here =current-prefix-arg= is set
1269 to call =compilation=  interactively.
1270
1271 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1272
1273   (defun my-perl-hook ()
1274     (progn
1275       (setq-local compilation-read-command nil)
1276       (set (make-local-variable 'compile-command)
1277            (concat "/usr/bin/perl "
1278                    (if buffer-file-name
1279                        (shell-quote-argument buffer-file-name))))
1280       (local-set-key (kbd "s-r")
1281                        (lambda ()
1282                          (interactive)
1283   ;                       (setq current-prefix-arg '(4)) ; C-u
1284                          (call-interactively 'compile)))))
1285
1286   (add-hook 'cperl-mode-hook 'my-perl-hook)
1287
1288
1289 #+END_SRC
1290
1291 *** C & C++
1292
1293 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1294
1295   (setq c-default-style "stroustrup"
1296         c-basic-offset 4)
1297
1298 #+END_SRC
1299
1300 ** Compile
1301
1302 Set the environments vairables in compilation mode
1303
1304 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1305
1306   (use-package compile
1307     :commands compile
1308     :config
1309     (setq compilation-environment (cons "LC_ALL=C" compilation-environment)))
1310
1311 #+END_SRC
1312
1313 ** Auto-Insert
1314
1315 Enable auto-insert mode
1316
1317 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1318
1319   (auto-insert-mode t)
1320   (setq auto-insert-query nil)
1321
1322 #+END_SRC
1323
1324 *** C++ Auto Insert
1325
1326 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1327
1328   (eval-after-load 'autoinsert
1329     '(define-auto-insert '("\\.cpp\\'" . "C++ skeleton")
1330        '(
1331          "Short description:"
1332          "/*"
1333          "\n * " (file-name-nondirectory (buffer-file-name))
1334          "\n */" > \n \n
1335          "#include <iostream>" \n
1336          "#include \""
1337          (file-name-sans-extension
1338           (file-name-nondirectory (buffer-file-name)))
1339          ".hpp\"" \n \n
1340          "using namespace std;" \n \n
1341          "int main ()"
1342          "\n{" \n 
1343          > _ \n
1344          "return 1;"
1345          "\n}" > \n
1346          )))
1347
1348   (eval-after-load 'autoinsert
1349     '(define-auto-insert '("\\.c\\'" . "C skeleton")
1350        '(
1351          "Short description:"
1352          "/*\n"
1353          " * " (file-name-nondirectory (buffer-file-name)) "\n"
1354          " */" > \n \n
1355          "#include <stdio.h>" \n
1356          "#include \""
1357          (file-name-sans-extension
1358           (file-name-nondirectory (buffer-file-name)))
1359          ".h\"" \n \n
1360          "int main ()\n"
1361          "{" \n
1362          > _ \n
1363          "return 1;\n"
1364          "}" > \n
1365          )))
1366        
1367 #+END_SRC
1368
1369 *** Perl Auto Insert
1370
1371 Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
1372
1373 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1374
1375   (eval-after-load 'autoinsert
1376     '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
1377        '(
1378          "Description: "
1379          "#!/usr/bin/perl -w" \n
1380          \n
1381          "use strict;" \n \n
1382          )))
1383
1384 #+END_SRC
1385
1386 ** Completion
1387
1388 company mode
1389
1390 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1391
1392   (use-package company
1393     :ensure t
1394     :diminish company-mode
1395     :init (setq company-idle-delay 0.1)
1396     :config
1397     (global-company-mode))
1398
1399 #+END_SRC
1400
1401 * Todo 
1402
1403 - change M-o to trigger to delete other windows and restore previous config
1404