6883926ec123b464226175c8914543b4df03d896
[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 * Org-mode Settings
272
273 ** Org-mode Basic setting
274
275 Always indents header, and hide header leading starts so that no need type =#+STATUP: indent= 
276
277 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
278
279   (use-package org
280     :ensure t
281     :init
282     (setq org-startup-indented t)
283     (setq org-hide-leading-starts t)
284     (setq org-src-fontify-natively t)
285     (setq org-src-tab-acts-natively t)
286     (setq org-confirm-babel-evaluate nil)
287     (setq org-use-speed-commands t)
288     (setq org-completion-use-ido t))
289
290   (org-babel-do-load-languages
291    'org-babel-load-languages
292    '((python . t)
293      (C . t)
294      (perl . t)
295      (calc . t)
296      (latex . t)
297      (java . t)
298      (ruby . t)
299      (lisp . t)
300      (scheme . t)
301      (sh . t)
302      (sqlite . t)
303      (js . t)))
304
305   ;; use current window for org source buffer editting
306   (setq org-src-window-setup 'current-window )
307
308 #+END_SRC
309
310 ** Org-bullets
311
312 use [[https://github.com/sabof/org-bullets][org-bullets]] package to show utf-8 charactes
313
314 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
315
316   (use-package org-bullets
317     :ensure t
318     :init
319     (add-hook 'org-mode-hook
320               (lambda ()
321                 (org-bullets-mode t))))
322
323 #+END_SRC
324
325 ** Worf Mode
326
327 [[https://github.com/abo-abo/worf][worf]] mode is an extension of vi-like binding for org-mode. 
328 In =worf-mode=, it is mapping =[=, =]= as =worf-backward= and =worf-forward= in global, wich
329 cause we cannot input =[= and =]=, so here I unset this mappings. And redifined this two to
330 =M-[= and =M-]=. see this [[https://github.com/abo-abo/worf/issues/19#issuecomment-223756599][issue]]
331
332 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
333
334   (use-package worf
335     :ensure t
336     :commands worf-mode
337     :init (add-hook 'org-mode-hook 'worf-mode)
338     ;; :config
339     ;; (define-key worf-mode-map "[" nil)
340     ;; (define-key worf-mode-map "]" nil)
341     ;; (define-key worf-mode-map (kbd "M-[") 'worf-backward)
342     ;; (define-key worf-mode-map (kbd "M-]") 'worf-forward)
343     )
344
345 #+END_SRC
346
347 ** Task Management
348
349 ** Capture
350
351 ** Export PDF
352
353 Install MacTex-basic and some tex packages
354
355 #+BEGIN_SRC bash 
356
357   sudo tlmgr update --self
358
359   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
360
361 #+END_SRC
362
363 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
364   ;; ;; allow for export=>beamer by placing
365
366   ;; http://emacs-fu.blogspot.com/2011/04/nice-looking-pdfs-with-org-mode-and.html
367   ;; #+LaTeX_CLASS: beamer in org files
368   (unless (boundp 'org-export-latex-classes)
369     (setq org-export-latex-classes nil))
370   (add-to-list 'org-export-latex-classes
371     ;; beamer class, for presentations
372     '("beamer"
373        "\\documentclass[11pt]{beamer}\n
374         \\mode<{{{beamermode}}}>\n
375         \\usetheme{{{{beamertheme}}}}\n
376         \\usecolortheme{{{{beamercolortheme}}}}\n
377         \\beamertemplateballitem\n
378         \\setbeameroption{show notes}
379         \\usepackage[utf8]{inputenc}\n
380         \\usepackage[T1]{fontenc}\n
381         \\usepackage{hyperref}\n
382         \\usepackage{color}
383         \\usepackage{listings}
384         \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
385     frame=single,
386     basicstyle=\\small,
387     showspaces=false,showstringspaces=false,
388     showtabs=false,
389     keywordstyle=\\color{blue}\\bfseries,
390     commentstyle=\\color{red},
391     }\n
392         \\usepackage{verbatim}\n
393         \\institute{{{{beamerinstitute}}}}\n          
394          \\subject{{{{beamersubject}}}}\n"
395
396        ("\\section{%s}" . "\\section*{%s}")
397  
398        ("\\begin{frame}[fragile]\\frametitle{%s}"
399          "\\end{frame}"
400          "\\begin{frame}[fragile]\\frametitle{%s}"
401          "\\end{frame}")))
402
403     ;; letter class, for formal letters
404
405     (add-to-list 'org-export-latex-classes
406
407     '("letter"
408        "\\documentclass[11pt]{letter}\n
409         \\usepackage[utf8]{inputenc}\n
410         \\usepackage[T1]{fontenc}\n
411         \\usepackage{color}"
412  
413        ("\\section{%s}" . "\\section*{%s}")
414        ("\\subsection{%s}" . "\\subsection*{%s}")
415        ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
416        ("\\paragraph{%s}" . "\\paragraph*{%s}")
417        ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
418
419
420   (require 'ox-md)
421   (require 'ox-beamer)
422
423   (setq org-latex-pdf-process
424         '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
425           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
426           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
427
428   (setq TeX-parse-self t)
429
430   (setq TeX-PDF-mode t)
431   (add-hook 'LaTeX-mode-hook
432             (lambda ()
433               (LaTeX-math-mode)
434               (setq TeX-master t)))
435
436 #+END_SRC
437
438 * Magit
439
440 [[https://github.com/magit/magit][Magit]] is a very cool git interface on Emacs.
441
442 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
443
444   (use-package magit
445     :ensure t
446     :commands magit-status magit-blame)
447
448
449 #+END_SRC
450
451 * IDO & SMEX
452
453 ** IDO
454
455 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
456
457   (use-package ido
458     :ensure t
459     :init (setq ido-enable-flex-matching t
460                 ido-ignore-extensions t
461                 ido-use-virtual-buffers t
462                 ido-everywhere t)
463     :config
464     (ido-mode 1)
465     (ido-everywhere 1)
466     (add-to-list 'completion-ignored-extensions ".pyc"))
467
468   (icomplete-mode t)
469
470 #+END_SRC
471
472 ** FLX
473
474 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
475
476   (use-package flx-ido
477     :ensure t
478     :init (setq ido-enable-flex-matching t
479                 ido-use-faces nil)
480     :config (flx-ido-mode 1))
481
482 #+END_SRC
483
484 ** IDO-vertically
485
486 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
487
488   (use-package ido-vertical-mode
489     :ensure t
490     :init
491     (setq ido-vertical-define-keys 'C-n-C-p-up-and-down)
492     :config
493     (ido-vertical-mode 1))
494
495 #+END_SRC
496
497 ** SMEX
498
499 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
500
501   (use-package smex
502     :ensure t
503     :init (smex-initialize)
504     :bind
505     ("M-x" . smex)
506     ("M-X" . smex-major-mode-commands))
507
508 #+END_SRC
509
510 ** Ido-ubiquitous
511
512 Use [[https://github.com/DarwinAwardWinner/ido-ubiquitous][ido-ubiquitous]] for ido everywhere. It makes =describe-function= can also use ido
513
514 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
515
516   (use-package ido-ubiquitous
517     :ensure t
518     :init
519     (setq magit-completing-read-function 'magit-ido-completing-read)
520     (setq gnus-completing-read-function 'gnus-ido-completing-read)
521     :config
522     (ido-ubiquitous-mode 1))
523
524 #+END_SRC
525
526 ** Ido-exit-target
527
528 [[https://github.com/waymondo/ido-exit-target][ido-exit-target]] let you open file/buffer on =other-windows= when call =ido-switch-buffer=
529
530 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
531
532   (use-package ido-exit-target
533     :ensure t
534     :init
535     (define-key ido-common-completion-map (kbd "C-j") #'ido-exit-target-split-window-right)
536     (define-key ido-common-completion-map (kbd "C-l") #'ido-exit-target-split-window-below))
537
538 #+END_SRC
539
540 * Key bindings
541
542 ** Remove prefix =ESC=, refer [[http://emacs.stackexchange.com/questions/14755/how-to-remove-bindings-to-the-esc-prefix-key][here]]
543
544 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
545
546   ;; (define-key key-translation-map (kbd "ESC") (kbd "C-g"))
547
548 #+END_SRC
549
550 ** Esc on Minibuffer
551
552 Use =ESC= to exit minibuffer. Also I map =Super-h= the same as =C-g=
553
554 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
555
556   (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
557   (define-key minibuffer-local-map [escape]  'keyboard-escape-quit)
558   (define-key minibuffer-local-ns-map [escape]  'keyboard-escape-quit)
559   (define-key minibuffer-local-isearch-map [escape]  'keyboard-escape-quit)
560   (define-key minibuffer-local-completion-map [escape]  'keyboard-escape-quit)
561   (define-key minibuffer-local-must-match-map [escape]  'keyboard-escape-quit)
562   (define-key minibuffer-local-must-match-filename-map [escape]  'keyboard-escape-quit)
563   (define-key minibuffer-local-filename-completion-map [escape]  'keyboard-escape-quit)
564   (define-key minibuffer-local-filename-must-match-map [escape]  'keyboard-escape-quit)
565
566   ;; Also map s-h same as C-g
567   (define-key minibuffer-local-map (kbd "s-h") 'keyboard-escape-quit)
568
569 #+END_SRC
570
571 ** =Ctrl= key bindings
572
573 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
574   ;;
575
576   ;; C-h    help    
577   ;; C-j    newline and indent
578   ;; C-k    kill line
579   ;; C-l    recenter-top-bottom
580   ;; (global-set-key (kbd "C-;") 'ido-switch-buffer)
581   ;; C-;
582   ;; C-'   
583   ;; C-ret  
584
585   ;; C-n    next-line
586   ;; C-m
587   ;; C-,
588   ;; C-.
589   ;; C-/
590
591   ;; C-y
592   ;; C-u
593   ;; C-i
594   ;; C-o
595   ;; C-p
596   ;; C-[
597   ;; C-]
598   ;; C-\
599
600   ;; C-=
601   ;; C--
602   ;; C-0
603   ;; C-9
604   ;; C-8
605   ;; C-7
606
607   ;; C-Space
608
609
610
611
612
613
614
615 #+END_SRC
616
617 ** =Super= bindings for file, buffer and windows
618
619 Some global bindings on =Super=, on Mac, it is =Command=
620
621 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
622
623   (global-set-key (kbd "s-h") 'keyboard-quit)
624   (global-set-key (kbd "s-j") 'ido-switch-buffer)
625   (global-set-key (kbd "s-k") 'ido-find-file)
626   ;; s-k  -->  kill-this-buffer
627   (global-set-key (kbd "s-l") (lambda ()
628                                 (interactive)
629                                 (if (> (length (window-list)) 1) 
630                                     (delete-window)
631                                   (message "Only one Windows now!"))))
632   ;; s-l  -->  goto-line
633   (global-set-key (kbd "s-;") 'swiper)
634   ;; s-;  -->
635   ;; s-'  -->  'next-multiframe-window
636   (global-set-key (kbd "<s-return>") 'toggle-frame-fullscreen)
637
638   ;; (global-set-key (kbd "s-y") 'projectile-find-file)
639   (global-set-key (kbd "s-f") 'projectile-find-file)
640   (global-set-key (kbd "s-[") 'next-buffer)
641   (global-set-key (kbd "s-]") 'previous-buffer)
642
643   (global-set-key (kbd "s-`") 'mode-line-other-buffer)
644
645
646
647   ;; someothers default mapping on super (command) key
648   ;; s-s save-buffer
649   ;; s-k kill-this-buffer
650
651
652   ;; s-h  -->  ns-do-hide-emacs
653   ;; s-j  -->  ido-switch-buffer  +
654   ;; s-k  -->  kill-this-buffer
655   ;; s-l  -->  goto-line
656   ;; s-;  -->  undefined
657   ;; s-'  -->  next-multiframe-window
658   ;; s-ret --> toggle-frame-fullscreen +
659
660   ;; s-y  -->  ns-paste-secondary
661   ;; s-u  -->  revert-buffer
662   ;; s-i  -->  undefined - but used for iterm globally
663   ;; s-o  -->  used for emacs globally
664   ;; s-p  -->  projectile-persp-switch-project  +  
665   ;; s-[  -->  next-buffer  +    
666   ;; s-]  -->  previous-buffer +
667
668   ;; s-0  -->  undefined
669   ;; s-9  -->  undefined
670   ;; s-8  -->  undefined
671   ;; s-7  -->  undefined
672   ;; s-6  -->  undefined
673   ;; s--  -->  center-line
674   ;; s-=  -->  undefined
675
676   ;; s-n  -->  make-frame
677   ;; s-m  -->  iconify-frame
678   ;; s-b  -->  undefined
679   ;; s-,  -->  customize
680   ;; s-.  -->  undefined
681   ;; s-/  -->  undefined
682
683   ;; s-g  -->  isearch-repeat-forward
684   ;; s-f  -->  projectile-find-file   +
685   ;; s-d  -->  isearch-repeat-background
686   ;; s-s  -->  save-buffer
687   ;; s-a  -->  make-whole-buffer
688
689   ;; s-b  -->  undefined
690   ;; s-v  -->  yank
691   ;; s-c  -->  ns-copy-including-secondary
692
693   ;; s-t  -->  ns-popup-font-panel
694   ;; s-r  -->  undefined
695   ;; s-e  -->  isearch-yanqk-kill
696   ;; s-w  -->  delete-frame
697   ;; s-q  -->  same-buffers-kill-emacs
698
699   ;; s-`  -->  other-frame
700 #+END_SRC
701
702 ** =M-s= bindings for searching
703
704 I use the prefix =M-s= for searching in buffers
705
706 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
707
708   (defun pl-make-keymap (key bindings)
709     (setq keymap (make-sparse-keymap))
710     (dolist (binding bindings)
711       (define-key keymap (car binding) (cdr binding)))
712     (global-set-key key keymap))
713
714   (define-key minibuffer-local-map "\M-s" nil)
715
716   (global-set-key (kbd "M-s s") 'isearch-forward-regexp)
717   (define-key isearch-mode-map "\M-s" 'isearch-repeat-forward)
718   (global-set-key (kbd "M-s r") 'isearch-backward-regexp)
719   (define-key isearch-mode-map "\M-r" 'isearch-repeat-backward)
720
721   (global-set-key (kbd "s-/") 'isearch-forward-regexp)
722   (define-key isearch-mode-map (kbd  "s-/") 'isearch-repeat-forward)
723   (define-key isearch-mode-map (kbd  "C-n") 'isearch-repeat-forward)
724
725
726   (set-face-background 'ido-first-match "white")
727
728   ;; M-s o  -->  occur
729   ;; M-s s  -->  isearch-forward-regexp
730   ;; M-s r  -->  isearch-backward-regexp
731   ;; M-s w  -->  isearch-forward-word
732   ;; M-s .  -->  isearch-forward-symbol-at-point
733   ;; M-s _  -->  isearch-forward-symbol
734
735   ;; highlight bindings
736   ;; M-s h .  -->  highlight-symbol-at-point
737   ;; M-s h r  -->  highlight-regexp
738   ;; M-s h u  -->  unhighlight-regexp
739   ;; M-s h l  -->  highlight-lines-match-regexp
740   ;; M-s h p  -->  highlight-phrase
741   ;; M-s h f  -->  hi-lock-find-patterns
742
743   ;; 
744   ;; (global-set-key (kbd "M-s M-r") 'isearch-backward-regexp)
745   ;;
746
747   ;; M-c
748   ;; M-r
749   ;; M-t
750   ;; M-u, 
751 #+END_SRC
752
753 Occur search key bindings
754
755 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
756
757   (define-key occur-mode-map (kbd "C-n")
758     (lambda ()
759       (interactive)
760       (occur-next)
761       (occur-mode-goto-occurrence-other-window)
762       (recenter)
763       (other-window 1)))
764
765   (define-key occur-mode-map (kbd "C-p")
766     (lambda ()
767       (interactive)
768       (occur-prev)
769       (occur-mode-goto-occurrence-other-window)
770       (recenter)
771       (other-window 1)))
772
773 #+END_SRC
774
775
776 ** =M-o= as prefix key for windows
777
778 ** =M-g= as prefix key for launcher
779
780 ** others
781
782 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
783
784
785   ;; C-'    undefined
786   ;; C-.    undefined
787 #+END_SRC
788
789 * Eshell
790
791 Eshell alias
792
793 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
794
795   (defalias 'e 'ido-find-file)
796   (defalias 'ff 'ido-find-file)
797   (defalias 'ee 'ido-find-file-other-window)
798
799 #+END_SRC
800
801 Quickly start eshll in split window below, refer [[http://www.howardism.org/Technical/Emacs/eshell-fun.html][eshell-here]]
802
803 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
804
805   (defun eshell-x ()
806     (insert "exit")
807     (eshell-send-input)
808     (delete-window))
809
810   (defun eshell-here ()
811     "Opens up a new shell in the directory associated with the
812   current buffer's file. The eshell is renamed to match that
813   directory to make multiple eshell windows easier."
814     (interactive)
815     (let* ((parent (if (buffer-file-name)
816                        (file-name-directory (buffer-file-name))
817                      default-directory))
818            (height (/ (window-total-height) 3))
819            (name   (car (last (split-string parent "/" t))))
820            (eshell-name (concat "*eshell: " name "*")))
821       (split-window-vertically (- height))
822       (other-window 1)
823       (if (get-buffer eshell-name)
824           (progn
825             (message "buffer exist")
826             (switch-to-buffer eshell-name))
827         (progn
828           (eshell "new")
829           (rename-buffer eshell-name)
830
831           (insert (concat "ls"))
832           (eshell-send-input)))))
833
834   (global-set-key (kbd "M-`") (lambda ()
835                                 (interactive)
836                                 (eshell-here)))
837
838 #+END_SRC
839
840 * Misc Settings
841
842 ** [[https://github.com/abo-abo/hydra][Hydra]]
843
844 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
845
846   (use-package hydra
847     :ensure t)
848
849 #+END_SRC
850
851 *** Font Zoom
852
853 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
854
855   (defhydra sd/font-zoom (global-map "<f2>")
856     "zoom"
857     ("g" text-scale-increase "in")
858     ("l" text-scale-decrease "out"))
859
860 #+END_SRC
861
862 *** Windmove Splitter
863
864 Refer [[https://github.com/abo-abo/hydra/blob/master/hydra-examples.el][hydra-example]], to enlarge or shrink the windows splitter
865
866 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
867
868   (defun hydra-move-splitter-left (arg)
869     "Move window splitter left."
870     (interactive "p")
871     (if (let ((windmove-wrap-around))
872           (windmove-find-other-window 'right))
873         (shrink-window-horizontally arg)
874       (enlarge-window-horizontally arg)))
875
876   (defun hydra-move-splitter-right (arg)
877     "Move window splitter right."
878     (interactive "p")
879     (if (let ((windmove-wrap-around))
880           (windmove-find-other-window 'right))
881         (enlarge-window-horizontally arg)
882       (shrink-window-horizontally arg)))
883
884   (defun hydra-move-splitter-up (arg)
885     "Move window splitter up."
886     (interactive "p")
887     (if (let ((windmove-wrap-around))
888           (windmove-find-other-window 'up))
889         (enlarge-window arg)
890       (shrink-window arg)))
891
892   (defun hydra-move-splitter-down (arg)
893     "Move window splitter down."
894     (interactive "p")
895     (if (let ((windmove-wrap-around))
896           (windmove-find-other-window 'up))
897         (shrink-window arg)
898       (enlarge-window arg)))
899
900 #+END_SRC
901
902 *** hydra-window
903
904 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
905
906   (winner-mode 1)
907
908   (defhydra sd/hydra-window (:color red :columns nil)
909     "window"
910     ("h" windmove-left nil)
911     ("j" windmove-down nil)
912     ("k" windmove-up nil)
913     ("l" windmove-right nil)
914     ("H" hydra-move-splitter-left nil)
915     ("J" hydra-move-splitter-down nil)
916     ("K" hydra-move-splitter-up nil)
917     ("L" hydra-move-splitter-right nil)
918     ("v" (lambda ()
919            (interactive)
920            (split-window-right)
921            (windmove-right))
922      "vert")
923     ("x" (lambda ()
924            (interactive)
925            (split-window-below)
926            (windmove-down))
927      "horz")
928     ("o" delete-other-windows "one" :exit t)
929     ("a" ace-window "ace")
930     ("s" ace-swap-window "swap")
931     ("d" ace-delete-window "ace-one" :exit t)
932     ("i" ace-maximize-window "ace-one" :exit t)
933     ("b" ido-switch-buffer "buf")
934     ("m" headlong-bookmark-jump "bmk")
935     ("q" nil "cancel")
936     ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
937     ("r" (progn (winner-redo) (setq this-command 'winner-redo)) "redo")
938     ("f" nil))
939
940   (global-unset-key (kbd "M-o"))
941   (global-set-key (kbd "M-o") 'sd/hydra-window/body)
942
943   (defun triggle-windows-max-size ()
944     (interactive)
945     (if (> (length (window-list)) 1)
946         (delete-other-windows)
947       (winner-undo)))
948
949 #+END_SRC
950
951 ** Line Number
952
953 Enable linum mode on programming modes
954
955 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
956
957   (add-hook 'prog-mode-hook 'linum-mode)
958
959 #+END_SRC
960
961 Fix the font size of line number
962
963 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
964
965   (defun fix-linum-size ()
966        (interactive)
967        (set-face-attribute 'linum nil :height 110))
968
969   (add-hook 'linum-mode-hook 'fix-linum-size)
970
971 #+END_SRC
972
973 I like [[https://github.com/coldnew/linum-relative][linum-relative]], just like the =set relativenumber= on =vim=
974
975 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
976
977   (use-package linum-relative
978     :ensure t
979     :config
980     (defun linum-new-mode ()
981       "If line numbers aren't displayed, then display them.
982   Otherwise, toggle between absolute and relative numbers."
983       (interactive)
984       (if linum-mode
985           (linum-relative-toggle)
986         (linum-mode 1)))
987
988     :bind
989     ("A-k" . linum-new-mode))
990
991   ;; auto enable linum-new-mode in programming modes
992   (add-hook 'prog-mode-hook 'linum-relative-mode)
993
994 #+END_SRC
995
996 ** Save File Position
997
998 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
999
1000   (require 'saveplace)
1001   (setq-default save-place t)
1002   (setq save-place-forget-unreadable-files t)
1003   (setq save-place-skip-check-regexp "\\`/\\(?:cdrom\\|floppy\\|mnt\\|/[0-9]\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)")
1004
1005 #+END_SRC
1006
1007 ** Multi-term
1008
1009 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1010
1011   (use-package multi-term
1012     :ensure t)
1013
1014 #+END_SRC
1015
1016 ** ace-link
1017
1018 [[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
1019 Type =o= to go to the link
1020
1021 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1022
1023   (use-package ace-link
1024     :ensure t
1025     :init
1026     (ace-link-setup-default))
1027
1028 #+END_SRC
1029
1030 ** Emux
1031
1032 [[https://github.com/re5et/emux][emux]] is 
1033
1034 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1035
1036   (el-get-bundle re5et/emux)
1037
1038 #+END_SRC
1039
1040 ** Smart Parens
1041
1042 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1043
1044   (use-package smartparens
1045     :ensure t
1046     :config
1047     (progn
1048       (require 'smartparens-config)
1049       (add-hook 'prog-mode-hook 'smartparens-mode)))
1050
1051 #+END_SRC
1052
1053 ** Ace-Windows
1054
1055 [[https://github.com/abo-abo/ace-window][ace-window]] 
1056
1057 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1058
1059   (use-package ace-window
1060     :ensure t
1061     :defer t
1062   ;  :init
1063   ;  (global-set-key (kbd "M-o") 'ace-window)
1064     :config
1065     (setq aw-keys '(?a ?s ?d ?f ?j ?k ?l)))
1066
1067 #+END_SRC
1068
1069 ** Projectile
1070
1071 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1072
1073   (use-package projectile
1074     :ensure t
1075     :init
1076     (setq projectile-enable-caching t)
1077     :config
1078     (projectile-global-mode t))
1079
1080   (use-package persp-projectile
1081     :ensure t
1082     :config
1083     (persp-mode)
1084     :bind
1085     (:map projectile-mode-map
1086           ("s-p" . projectile-persp-switch-project)))
1087
1088   ;; projectile-find-file
1089   ;; projectile-switch-buffer
1090   ;; projectile-find-file-other-window
1091 #+END_SRC
1092
1093 ** Which key
1094
1095 [[https://github.com/justbur/emacs-which-key][which-key]] show the key bindings 
1096
1097 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1098
1099   (use-package which-key
1100     :ensure t
1101     :config
1102     (which-key-mode))
1103
1104 #+END_SRC
1105
1106 ** Emms
1107
1108 We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
1109
1110 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1111
1112   (use-package emms
1113     :ensure t
1114     :init
1115     (setq emms-source-file-default-directory "~/Music/emms/")
1116     :config
1117     (emms-standard)
1118     (emms-default-players)
1119     (define-emms-simple-player mplayer '(file url)
1120       (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
1121                     ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
1122                     "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
1123                     ".m4a" ".flv" ".ogv" ".pls"))
1124       "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
1125     (emms-history-load))
1126
1127 #+END_SRC
1128
1129 ** GnoGo
1130
1131 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
1132
1133 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1134
1135   (use-package gnugo
1136     :ensure t
1137     :defer t
1138     :init
1139     (require 'gnugo-imgen)
1140     (setq gnugo-xpms 'gnugo-imgen-create-xpms)
1141     (add-hook 'gnugo-start-game-hook '(lambda ()
1142                                         (gnugo-image-display-mode)
1143                                         (gnugo-grid-mode)))
1144       :config
1145     (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
1146
1147 #+END_SRC
1148
1149 ** undo-tree
1150
1151 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1152
1153   (use-package undo-tree
1154     :ensure t
1155     :config
1156     (global-undo-tree-mode 1))
1157
1158 #+END_SRC
1159
1160 ** swiper
1161
1162 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1163
1164   (use-package swiper
1165     :ensure t)
1166
1167
1168   (ivy-mode 1)
1169   (setq ivy-use-virtual-buffers t)
1170   (global-set-key "\C-s" 'swiper)
1171   (global-set-key (kbd "C-c C-r") 'ivy-resume)
1172   (global-set-key (kbd "<f6>") 'ivy-resume)
1173   ;; (global-set-key (kbd "M-x") 'counsel-M-x)
1174   ;; (global-set-key (kbd "C-x C-f") 'counsel-find-file)
1175   (global-set-key (kbd "<f1> f") 'counsel-describe-function)
1176   (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
1177   (global-set-key (kbd "<f1> l") 'counsel-load-library)
1178   (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
1179   (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
1180   (global-set-key (kbd "C-c g") 'counsel-git)
1181   (global-set-key (kbd "C-c j") 'counsel-git-grep)
1182   (global-set-key (kbd "C-c k") 'counsel-ag)
1183   (global-set-key (kbd "C-x l") 'counsel-locate)
1184   (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
1185   ;; (define-key read-expression-map (kbd "C-r") 'counsel-expression-history)
1186
1187 #+END_SRC
1188
1189 * Programming
1190
1191 ** Languages
1192
1193 *** Emacs Lisp
1194
1195 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1196
1197   (use-package color-identifiers-mode
1198     :ensure t
1199     :init
1200     (add-hook 'emacs-lisp-mode-hook 'color-identifiers-mode)
1201
1202     :diminish color-identifiers-mode)
1203
1204   (global-prettify-symbols-mode t)
1205
1206 #+END_SRC
1207
1208 **** Lispy Mode
1209
1210 In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
1211 bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
1212
1213 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1214
1215   (use-package lispy
1216     :ensure t
1217     :init
1218     (eval-after-load 'lispy
1219       '(progn
1220          (define-key lispy-mode-map (kbd "M-o") nil)))
1221     :config
1222     (add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1))))
1223
1224 #+END_SRC
1225
1226 *** Perl
1227
1228 [[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
1229
1230 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1231
1232   (defalias 'perl-mode 'cperl-mode)
1233
1234   ;(setq cperl-hairy t) ;; Turns on most of the CPerlMode options
1235   (setq cperl-auto-newline t)
1236   (setq cperl-highlight-variables-indiscriminately t)
1237   ;(setq cperl-indent-level 4)
1238   ;(setq cperl-continued-statement-offset 4)
1239   (setq cperl-close-paren-offset -4)
1240   (setq cperl-indent-parents-as-block t)
1241   (setq cperl-tab-always-indent t)
1242   ;(setq cperl-brace-offset  0)
1243
1244   (add-hook 'cperl-mode-hook
1245             '(lambda ()
1246                (cperl-set-style "C++")))
1247
1248   ;(require 'template)
1249   ;(template-initialize)
1250   ;(require 'perlnow)
1251
1252 #+END_SRC
1253
1254 - auto insert
1255 - run script 
1256
1257 Change the compile-command to set the default command run when call =compile=
1258 Mapping =s-r= (on Mac, it's =Command + R= to run the script. Here =current-prefix-arg= is set
1259 to call =compilation=  interactively.
1260
1261 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1262
1263   (defun my-perl-hook ()
1264     (progn
1265       (setq-local compilation-read-command nil)
1266       (set (make-local-variable 'compile-command)
1267            (concat "/usr/bin/perl "
1268                    (if buffer-file-name
1269                        (shell-quote-argument buffer-file-name))))
1270       (local-set-key (kbd "s-r")
1271                        (lambda ()
1272                          (interactive)
1273   ;                       (setq current-prefix-arg '(4)) ; C-u
1274                          (call-interactively 'compile)))))
1275
1276   (add-hook 'cperl-mode-hook 'my-perl-hook)
1277
1278
1279 #+END_SRC
1280
1281 *** C & C++
1282
1283 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1284
1285   (setq c-default-style "stroustrup"
1286         c-basic-offset 4)
1287
1288 #+END_SRC
1289
1290 ** Compile
1291
1292 Set the environments vairables in compilation mode
1293
1294 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1295
1296   (use-package compile
1297     :commands compile
1298     :config
1299     (setq compilation-environment (cons "LC_ALL=C" compilation-environment)))
1300
1301 #+END_SRC
1302
1303 ** Auto-Insert
1304
1305 Enable auto-insert mode
1306
1307 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1308
1309   (auto-insert-mode t)
1310   (setq auto-insert-query nil)
1311
1312 #+END_SRC
1313
1314 *** C++ Auto Insert
1315
1316 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1317
1318   (eval-after-load 'autoinsert
1319     '(define-auto-insert '("\\.cpp\\'" . "C++ skeleton")
1320        '(
1321          "Short description:"
1322          "/*"
1323          "\n * " (file-name-nondirectory (buffer-file-name))
1324          "\n */" > \n \n
1325          "#include <iostream>" \n
1326          "#include \""
1327          (file-name-sans-extension
1328           (file-name-nondirectory (buffer-file-name)))
1329          ".hpp\"" \n \n
1330          "using namespace std;" \n \n
1331          "int main ()"
1332          "\n{" \n 
1333          > _ \n
1334          "return 1;"
1335          "\n}" > \n
1336          )))
1337
1338   (eval-after-load 'autoinsert
1339     '(define-auto-insert '("\\.c\\'" . "C skeleton")
1340        '(
1341          "Short description:"
1342          "/*\n"
1343          " * " (file-name-nondirectory (buffer-file-name)) "\n"
1344          " */" > \n \n
1345          "#include <stdio.h>" \n
1346          "#include \""
1347          (file-name-sans-extension
1348           (file-name-nondirectory (buffer-file-name)))
1349          ".h\"" \n \n
1350          "int main ()\n"
1351          "{" \n
1352          > _ \n
1353          "return 1;\n"
1354          "}" > \n
1355          )))
1356        
1357 #+END_SRC
1358
1359 *** Perl Auto Insert
1360
1361 Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
1362
1363 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1364
1365   (eval-after-load 'autoinsert
1366     '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
1367        '(
1368          "Description: "
1369          "#!/usr/bin/perl -w" \n
1370          \n
1371          "use strict;" \n \n
1372          )))
1373
1374 #+END_SRC
1375
1376 ** Completion
1377
1378 company mode
1379
1380 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1381
1382   (use-package company
1383     :ensure t
1384     :diminish company-mode
1385     :init (setq company-idle-delay 0.1)
1386     :config
1387     (global-company-mode))
1388
1389 #+END_SRC
1390
1391 * Todo 
1392
1393 - change M-o to trigger to delete other windows and restore previous config
1394