58e13b4b313415e9d693a0cfc32e1965b0cba398
[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       (split-window-vertically (- height))
821       (other-window 1)
822       (if (get-buffer (concat "*eshell: " name "*"))
823           (progn
824             (message "buffer exist")
825             (switch-to-buffer (concat "*eshell: " name "*")))
826         (progn
827           (eshell "new")
828           (rename-buffer (concat "*eshell: " name "*"))
829
830           (insert (concat "ls"))
831           (eshell-send-input)))))
832
833   ;; (global-set-key (kbd "M-`") (lambda ()
834   ;;                               (interactive)
835   ;;                               (if (buffer-name))))
836
837 #+END_SRC
838
839
840
841 * Misc Settings
842
843 ** [[https://github.com/abo-abo/hydra][Hydra]]
844
845 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
846
847   (use-package hydra
848     :ensure t)
849
850 #+END_SRC
851
852 *** Font Zoom
853
854 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
855
856   (defhydra sd/font-zoom (global-map "<f2>")
857     "zoom"
858     ("g" text-scale-increase "in")
859     ("l" text-scale-decrease "out"))
860
861 #+END_SRC
862
863 *** Windmove Splitter
864
865 Refer [[https://github.com/abo-abo/hydra/blob/master/hydra-examples.el][hydra-example]], to enlarge or shrink the windows splitter
866
867 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
868
869   (defun hydra-move-splitter-left (arg)
870     "Move window splitter left."
871     (interactive "p")
872     (if (let ((windmove-wrap-around))
873           (windmove-find-other-window 'right))
874         (shrink-window-horizontally arg)
875       (enlarge-window-horizontally arg)))
876
877   (defun hydra-move-splitter-right (arg)
878     "Move window splitter right."
879     (interactive "p")
880     (if (let ((windmove-wrap-around))
881           (windmove-find-other-window 'right))
882         (enlarge-window-horizontally arg)
883       (shrink-window-horizontally arg)))
884
885   (defun hydra-move-splitter-up (arg)
886     "Move window splitter up."
887     (interactive "p")
888     (if (let ((windmove-wrap-around))
889           (windmove-find-other-window 'up))
890         (enlarge-window arg)
891       (shrink-window arg)))
892
893   (defun hydra-move-splitter-down (arg)
894     "Move window splitter down."
895     (interactive "p")
896     (if (let ((windmove-wrap-around))
897           (windmove-find-other-window 'up))
898         (shrink-window arg)
899       (enlarge-window arg)))
900
901 #+END_SRC
902
903 *** hydra-window
904
905 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
906
907   (winner-mode 1)
908
909   (defhydra sd/hydra-window (:color red :columns nil)
910     "window"
911     ("h" windmove-left nil)
912     ("j" windmove-down nil)
913     ("k" windmove-up nil)
914     ("l" windmove-right nil)
915     ("H" hydra-move-splitter-left nil)
916     ("J" hydra-move-splitter-down nil)
917     ("K" hydra-move-splitter-up nil)
918     ("L" hydra-move-splitter-right nil)
919     ("v" (lambda ()
920            (interactive)
921            (split-window-right)
922            (windmove-right))
923      "vert")
924     ("x" (lambda ()
925            (interactive)
926            (split-window-below)
927            (windmove-down))
928      "horz")
929     ("o" delete-other-windows "one" :exit t)
930     ("a" ace-window "ace")
931     ("s" ace-swap-window "swap")
932     ("d" ace-delete-window "ace-one" :exit t)
933     ("i" ace-maximize-window "ace-one" :exit t)
934     ("b" ido-switch-buffer "buf")
935     ("m" headlong-bookmark-jump "bmk")
936     ("q" nil "cancel")
937     ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
938     ("r" (progn (winner-redo) (setq this-command 'winner-redo)) "redo")
939     ("f" nil))
940
941   (global-unset-key (kbd "M-o"))
942   (global-set-key (kbd "M-o") 'sd/hydra-window/body)
943
944   (defun triggle-windows-max-size ()
945     (interactive)
946     (if (> (length (window-list)) 1)
947         (delete-other-windows)
948       (winner-undo)))
949
950 #+END_SRC
951
952 ** Line Number
953
954 Enable linum mode on programming modes
955
956 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
957
958   (add-hook 'prog-mode-hook 'linum-mode)
959
960 #+END_SRC
961
962 Fix the font size of line number
963
964 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
965
966   (defun fix-linum-size ()
967        (interactive)
968        (set-face-attribute 'linum nil :height 110))
969
970   (add-hook 'linum-mode-hook 'fix-linum-size)
971
972 #+END_SRC
973
974 I like [[https://github.com/coldnew/linum-relative][linum-relative]], just like the =set relativenumber= on =vim=
975
976 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
977
978   (use-package linum-relative
979     :ensure t
980     :config
981     (defun linum-new-mode ()
982       "If line numbers aren't displayed, then display them.
983   Otherwise, toggle between absolute and relative numbers."
984       (interactive)
985       (if linum-mode
986           (linum-relative-toggle)
987         (linum-mode 1)))
988
989     :bind
990     ("A-k" . linum-new-mode))
991
992   ;; auto enable linum-new-mode in programming modes
993   (add-hook 'prog-mode-hook 'linum-relative-mode)
994
995 #+END_SRC
996
997 ** Save File Position
998
999 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1000
1001   (require 'saveplace)
1002   (setq-default save-place t)
1003   (setq save-place-forget-unreadable-files t)
1004   (setq save-place-skip-check-regexp "\\`/\\(?:cdrom\\|floppy\\|mnt\\|/[0-9]\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)")
1005
1006 #+END_SRC
1007
1008 ** Multi-term
1009
1010 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1011
1012   (use-package multi-term
1013     :ensure t)
1014
1015 #+END_SRC
1016
1017 ** ace-link
1018
1019 [[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
1020 Type =o= to go to the link
1021
1022 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1023
1024   (use-package ace-link
1025     :ensure t
1026     :init
1027     (ace-link-setup-default))
1028
1029 #+END_SRC
1030
1031 ** Emux
1032
1033 [[https://github.com/re5et/emux][emux]] is 
1034
1035 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1036
1037   (el-get-bundle re5et/emux)
1038
1039 #+END_SRC
1040
1041 ** Smart Parens
1042
1043 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1044
1045   (use-package smartparens
1046     :ensure t
1047     :config
1048     (progn
1049       (require 'smartparens-config)
1050       (add-hook 'prog-mode-hook 'smartparens-mode)))
1051
1052 #+END_SRC
1053
1054 ** Ace-Windows
1055
1056 [[https://github.com/abo-abo/ace-window][ace-window]] 
1057
1058 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1059
1060   (use-package ace-window
1061     :ensure t
1062     :defer t
1063   ;  :init
1064   ;  (global-set-key (kbd "M-o") 'ace-window)
1065     :config
1066     (setq aw-keys '(?a ?s ?d ?f ?j ?k ?l)))
1067
1068 #+END_SRC
1069
1070 ** Projectile
1071
1072 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1073
1074   (use-package projectile
1075     :ensure t
1076     :init
1077     (setq projectile-enable-caching t)
1078     :config
1079     (projectile-global-mode t))
1080
1081   (use-package persp-projectile
1082     :ensure t
1083     :config
1084     (persp-mode)
1085     :bind
1086     (:map projectile-mode-map
1087           ("s-p" . projectile-persp-switch-project)))
1088
1089   ;; projectile-find-file
1090   ;; projectile-switch-buffer
1091   ;; projectile-find-file-other-window
1092 #+END_SRC
1093
1094 ** Which key
1095
1096 [[https://github.com/justbur/emacs-which-key][which-key]] show the key bindings 
1097
1098 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1099
1100   (use-package which-key
1101     :ensure t
1102     :config
1103     (which-key-mode))
1104
1105 #+END_SRC
1106
1107 ** Emms
1108
1109 We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
1110
1111 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1112
1113   (use-package emms
1114     :ensure t
1115     :init
1116     (setq emms-source-file-default-directory "~/Music/emms/")
1117     :config
1118     (emms-standard)
1119     (emms-default-players)
1120     (define-emms-simple-player mplayer '(file url)
1121       (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
1122                     ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
1123                     "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
1124                     ".m4a" ".flv" ".ogv" ".pls"))
1125       "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
1126     (emms-history-load))
1127
1128 #+END_SRC
1129
1130 ** GnoGo
1131
1132 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
1133
1134 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1135
1136   (use-package gnugo
1137     :ensure t
1138     :defer t
1139     :init
1140     (require 'gnugo-imgen)
1141     (setq gnugo-xpms 'gnugo-imgen-create-xpms)
1142     (add-hook 'gnugo-start-game-hook '(lambda ()
1143                                         (gnugo-image-display-mode)
1144                                         (gnugo-grid-mode)))
1145       :config
1146     (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
1147
1148 #+END_SRC
1149
1150 ** undo-tree
1151
1152 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1153
1154   (use-package undo-tree
1155     :ensure t
1156     :config
1157     (global-undo-tree-mode 1))
1158
1159 #+END_SRC
1160
1161 ** swiper
1162
1163 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1164
1165   (use-package swiper
1166     :ensure t)
1167
1168
1169   (ivy-mode 1)
1170   (setq ivy-use-virtual-buffers t)
1171   (global-set-key "\C-s" 'swiper)
1172   (global-set-key (kbd "C-c C-r") 'ivy-resume)
1173   (global-set-key (kbd "<f6>") 'ivy-resume)
1174   ;; (global-set-key (kbd "M-x") 'counsel-M-x)
1175   ;; (global-set-key (kbd "C-x C-f") 'counsel-find-file)
1176   (global-set-key (kbd "<f1> f") 'counsel-describe-function)
1177   (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
1178   (global-set-key (kbd "<f1> l") 'counsel-load-library)
1179   (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
1180   (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
1181   (global-set-key (kbd "C-c g") 'counsel-git)
1182   (global-set-key (kbd "C-c j") 'counsel-git-grep)
1183   (global-set-key (kbd "C-c k") 'counsel-ag)
1184   (global-set-key (kbd "C-x l") 'counsel-locate)
1185   (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
1186   ;; (define-key read-expression-map (kbd "C-r") 'counsel-expression-history)
1187
1188 #+END_SRC
1189
1190 * Programming
1191
1192 ** Languages
1193
1194 *** Emacs Lisp
1195
1196 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1197
1198   (use-package color-identifiers-mode
1199     :ensure t
1200     :init
1201     (add-hook 'emacs-lisp-mode-hook 'color-identifiers-mode)
1202
1203     :diminish color-identifiers-mode)
1204
1205   (global-prettify-symbols-mode t)
1206
1207 #+END_SRC
1208
1209 **** Lispy Mode
1210
1211 In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
1212 bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
1213
1214 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1215
1216   (use-package lispy
1217     :ensure t
1218     :init
1219     (eval-after-load 'lispy
1220       '(progn
1221          (define-key lispy-mode-map (kbd "M-o") nil)))
1222     :config
1223     (add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1))))
1224
1225 #+END_SRC
1226
1227 *** Perl
1228
1229 [[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
1230
1231 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1232
1233   (defalias 'perl-mode 'cperl-mode)
1234
1235   ;(setq cperl-hairy t) ;; Turns on most of the CPerlMode options
1236   (setq cperl-auto-newline t)
1237   (setq cperl-highlight-variables-indiscriminately t)
1238   ;(setq cperl-indent-level 4)
1239   ;(setq cperl-continued-statement-offset 4)
1240   (setq cperl-close-paren-offset -4)
1241   (setq cperl-indent-parents-as-block t)
1242   (setq cperl-tab-always-indent t)
1243   ;(setq cperl-brace-offset  0)
1244
1245   (add-hook 'cperl-mode-hook
1246             '(lambda ()
1247                (cperl-set-style "C++")))
1248
1249   ;(require 'template)
1250   ;(template-initialize)
1251   ;(require 'perlnow)
1252
1253 #+END_SRC
1254
1255 - auto insert
1256 - run script 
1257
1258 Change the compile-command to set the default command run when call =compile=
1259 Mapping =s-r= (on Mac, it's =Command + R= to run the script. Here =current-prefix-arg= is set
1260 to call =compilation=  interactively.
1261
1262 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1263
1264   (defun my-perl-hook ()
1265     (progn
1266       (setq-local compilation-read-command nil)
1267       (set (make-local-variable 'compile-command)
1268            (concat "/usr/bin/perl "
1269                    (if buffer-file-name
1270                        (shell-quote-argument buffer-file-name))))
1271       (local-set-key (kbd "s-r")
1272                        (lambda ()
1273                          (interactive)
1274   ;                       (setq current-prefix-arg '(4)) ; C-u
1275                          (call-interactively 'compile)))))
1276
1277   (add-hook 'cperl-mode-hook 'my-perl-hook)
1278
1279
1280 #+END_SRC
1281
1282 *** C & C++
1283
1284 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1285
1286   (setq c-default-style "stroustrup"
1287         c-basic-offset 4)
1288
1289 #+END_SRC
1290
1291 ** Compile
1292
1293 Set the environments vairables in compilation mode
1294
1295 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1296
1297   (use-package compile
1298     :commands compile
1299     :config
1300     (setq compilation-environment (cons "LC_ALL=C" compilation-environment)))
1301
1302 #+END_SRC
1303
1304 ** Auto-Insert
1305
1306 Enable auto-insert mode
1307
1308 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1309
1310   (auto-insert-mode t)
1311   (setq auto-insert-query nil)
1312
1313 #+END_SRC
1314
1315 *** C++ Auto Insert
1316
1317 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1318
1319   (eval-after-load 'autoinsert
1320     '(define-auto-insert '("\\.cpp\\'" . "C++ skeleton")
1321        '(
1322          "Short description:"
1323          "/*"
1324          "\n * " (file-name-nondirectory (buffer-file-name))
1325          "\n */" > \n \n
1326          "#include <iostream>" \n
1327          "#include \""
1328          (file-name-sans-extension
1329           (file-name-nondirectory (buffer-file-name)))
1330          ".hpp\"" \n \n
1331          "using namespace std;" \n \n
1332          "int main ()"
1333          "\n{" \n 
1334          > _ \n
1335          "return 1;"
1336          "\n}" > \n
1337          )))
1338
1339   (eval-after-load 'autoinsert
1340     '(define-auto-insert '("\\.c\\'" . "C skeleton")
1341        '(
1342          "Short description:"
1343          "/*\n"
1344          " * " (file-name-nondirectory (buffer-file-name)) "\n"
1345          " */" > \n \n
1346          "#include <stdio.h>" \n
1347          "#include \""
1348          (file-name-sans-extension
1349           (file-name-nondirectory (buffer-file-name)))
1350          ".h\"" \n \n
1351          "int main ()\n"
1352          "{" \n
1353          > _ \n
1354          "return 1;\n"
1355          "}" > \n
1356          )))
1357        
1358 #+END_SRC
1359
1360 *** Perl Auto Insert
1361
1362 Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
1363
1364 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1365
1366   (eval-after-load 'autoinsert
1367     '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
1368        '(
1369          "Description: "
1370          "#!/usr/bin/perl -w" \n
1371          \n
1372          "use strict;" \n \n
1373          )))
1374
1375 #+END_SRC
1376
1377 ** Completion
1378
1379 company mode
1380
1381 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1382
1383   (use-package company
1384     :ensure t
1385     :diminish company-mode
1386     :init (setq company-idle-delay 0.1)
1387     :config
1388     (global-company-mode))
1389
1390 #+END_SRC
1391
1392 * Todo 
1393
1394 - change M-o to trigger to delete other windows and restore previous config
1395