emacs - commit unused sd/eshell-here
[dotfiles.git] / emacs.d / 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   (scroll-bar-mode 0)
53   (tool-bar-mode 0)
54   (menu-bar-mode 1)
55
56   (setq debug-on-error t)
57   (setq inhibit-startup-message t)
58
59   (defalias 'yes-or-no-p 'y-or-n-p)
60   (show-paren-mode 1)
61   ;; don't backupf
62   (setq make-backup-files nil)
63 #+END_SRC
64
65 set custom file 
66
67 #+BEGIN_SRC emacs-lisp :tangle yes :results silent 
68
69   (setq custom-file "~/.emacs.d/custom.el")
70   (if (file-exists-p custom-file)
71       (load custom-file))
72
73 #+END_SRC
74
75 Switch the focus to help window when it appears
76
77 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
78
79   (setq help-window-select t)
80
81 #+END_SRC
82
83 Setting scroll right/left
84 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
85   ;  (global-set-key (kbd "C-,") 'scoll-left)
86   ;  (global-set-key (kbd "C-.") 'scoll-right)
87 #+END_SRC
88
89 * Package Management Tools
90
91 ** Use-package
92
93 Using [[https://github.com/jwiegley/use-package][use-package]] to manage emacs packages
94
95 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
96
97   (unless (package-installed-p 'use-package)
98     (package-refresh-contents)
99     (package-install 'use-package))
100
101   (require 'use-package)
102
103 #+END_SRC
104
105 ** El-get
106
107 [[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. 
108 Check out [[http://tapoueh.org/emacs/el-get.html][el-get]].
109
110 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
111
112   (use-package el-get
113     :ensure t
114     :init
115     (add-to-list 'load-path "~/.emacs.d/el-get"))
116
117 #+END_SRC
118
119 * Color and Fonts Settings
120
121 ** highlight current line
122
123 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
124
125   (global-hl-line-mode)
126
127 #+END_SRC
128
129 ** Smart Comments
130
131 [[https://github.com/paldepind/smart-comment][smart-comments]]
132
133 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
134
135   (use-package smart-comment
136     :ensure t
137     :bind ("M-;" . smart-conmment))
138
139 #+END_SRC
140
141 ** Font Setting
142
143 syntax highlighting
144
145 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
146
147   (global-font-lock-mode 1)
148
149 #+END_SRC
150
151 [[https://github.com/i-tu/Hasklig][Hasklig]] and Source Code Pro, defined fonts family
152
153 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
154
155   (if window-system
156       (defvar sd/fixed-font-family
157         (cond ((x-list-fonts "Hasklig")         "Hasklig")
158               ((x-list-fonts "Source Code Pro") "Source Code Pro:weight:light")
159               ((x-list-fonts "Anonymous Pro")   "Anonymous Pro")
160               ((x-list-fonts "M+ 1mn")          "M+ 1mn"))
161         "The fixed width font based on what is installed, `nil' if not defined."))
162
163 #+END_SRC
164
165 Setting the fonts 
166
167 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
168
169   (if window-system
170       (when sd/fixed-font-family
171         (set-frame-font sd/fixed-font-family)
172         (set-face-attribute 'default nil :font sd/fixed-font-family :height 130)
173         (set-face-font 'default sd/fixed-font-family)))
174
175 #+END_SRC
176
177 ** Color Theme
178
179 Loading theme should be after all required loaded, refere [[https://github.com/jwiegley/use-package][:defer]] in =use-package=
180
181 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
182
183   (setq vc-follow-symlinks t)
184
185   (use-package color-theme
186     :ensure t
187     :init (require 'color-theme)
188     :config (use-package color-theme-sanityinc-tomorrow
189               :ensure t
190               :no-require t
191               :config
192               (load-theme 'sanityinc-tomorrow-bright t)))
193
194   ;(eval-after-load 'color-theme
195   ;  (load-theme 'sanityinc-tomorrow-bright t))
196
197 #+END_SRC
198
199 Change the Org-mode colors 
200
201 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
202
203   (defun org-src-color-blocks-light ()
204     "Colors the block headers and footers to make them stand out more for lighter themes"
205     (interactive)
206     (custom-set-faces
207      '(org-block-begin-line
208       ((t (:underline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
209      '(org-block-background
210        ((t (:background "#FFFFEA"))))
211      '(org-block
212        ((t (:background "#FFFFEA"))))
213      '(org-block-end-line
214        ((t (:overline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
215
216      '(mode-line-buffer-id ((t (:foreground "#005000" :bold t))))
217      '(which-func ((t (:foreground "#008000"))))))
218
219   (defun org-src-color-blocks-dark ()
220     "Colors the block headers and footers to make them stand out more for dark themes"
221     (interactive)
222     (custom-set-faces
223      '(org-block-begin-line
224        ((t (:foreground "#008ED1" :background "#002E41"))))
225      '(org-block-background
226        ((t (:background "#000000"))))
227      '(org-block
228        ((t (:background "#000000"))))
229      '(org-block-end-line
230        ((t (:foreground "#008ED1" :background "#002E41"))))
231
232      '(mode-line-buffer-id ((t (:foreground "black" :bold t))))
233      '(which-func ((t (:foreground "green"))))))
234
235   (org-src-color-blocks-dark)
236
237 #+END_SRC
238
239 improve color for org-mode
240 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
241   (deftheme ha/org-theme "Sub-theme to beautify org mode")
242
243   (if window-system
244       (defvar sd/variable-font-tuple
245         (cond ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
246               ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
247               ((x-list-fonts "Verdana")         '(:font "Verdana"))
248               ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
249               (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro.")))
250         "My variable width font available to org-mode files and whatnot."))
251
252   (defun sd/org-color ()
253     (let* ((sd/fixed-font-tuple (list :font sd/fixed-font-family))
254            (base-font-color     (face-foreground 'default nil 'default))
255            (background-color    (face-background 'default nil 'default))
256            (primary-color       (face-foreground 'mode-line nil))
257            (secondary-color     (face-background 'secondary-selection nil 'region))
258            (base-height         (face-attribute 'default :height))
259            (headline           `(:inherit default :weight bold :foreground ,base-font-color)))
260       (custom-theme-set-faces 'ha/org-theme
261                               `(org-agenda-structure ((t (:inherit default :height 2.0 :underline nil))))
262                               `(org-verbatim ((t (:inherit 'fixed-pitched :foreground "#aef"))))
263                               `(org-table ((t (:inherit 'fixed-pitched))))
264                               `(org-block ((t (:inherit 'fixed-pitched))))
265                               `(org-block-background ((t (:inherit 'fixed-pitched))))
266                               `(org-block-begin-line ((t (:inherit 'fixed-pitched))))
267                               `(org-block-end-line ((t (:inherit 'fixed-pitched))))
268                               `(org-level-8 ((t (,@headline ,@sd/variable-font-tuple))))
269                               `(org-level-7 ((t (,@headline ,@sd/variable-font-tuple))))
270                               `(org-level-6 ((t (,@headline ,@sd/variable-font-tuple))))
271                               `(org-level-5 ((t (,@headline ,@sd/variable-font-tuple))))
272                               `(org-level-4 ((t (,@headline ,@sd/variable-font-tuple
273                                                             :height ,(round (* 1.1 base-height))))))
274                               `(org-level-3 ((t (,@headline ,@sd/variable-font-tuple
275                                                             :height ,(round (* 1.25 base-height))))))
276                               `(org-level-2 ((t (,@headline ,@sd/variable-font-tuple
277                                                             :height ,(round (* 1.5 base-height))))))
278                               `(org-level-1 ((t (,@headline ,@sd/variable-font-tuple
279                                                             :height ,(round (* 1.75 base-height))))))
280                               `(org-document-title ((t (,@headline ,@sd/variable-font-tuple :height 1.5 :underline nil)))))))
281
282
283 #+END_SRC
284
285 ** Rainbow-delimiter
286
287 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
288
289   (use-package rainbow-delimiters
290     :ensure t
291     :init
292     (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))
293
294 #+END_SRC
295
296 ** page-break-lines
297
298 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
299
300   (use-package page-break-lines
301     :ensure t
302     :config
303     (turn-on-page-break-lines-mode))
304
305 #+END_SRC
306
307 ** rainbow-mode
308
309 Enable rainbow mode in emacs lisp mode
310
311 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
312   (use-package rainbow-mode
313     :ensure t
314   ;  :init
315   ;  (add-hook emacs-lisp-mode-hook 'rainbow-mode)
316     )
317
318 #+END_SRC
319
320 * Mode-line
321
322 ** clean mode line
323
324 clean mode line, Refer to [[https://www.masteringemacs.org/article/hiding-replacing-modeline-strings][Marstering Emacs]]
325
326 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
327   (defvar mode-line-cleaner-alist
328     `((auto-complete-mode . " Î±")
329       (yas/minor-mode . " Ï…")
330       (paredit-mode . " Ï€")
331       (eldoc-mode . "")
332       (abbrev-mode . "")
333       (projectile-mode . "")
334       (ivy-mode . "")
335       (undo-tree-mode . "")
336       ;; default is WK
337       (which-key-mode . "")
338       ;; default is SP
339       (smartparens-mode . "")
340       ;; default is LR
341       (linum-relative-mode . "")
342       ;; default is ARev
343       (auto-revert-mode . "")
344       ;; default is Ind
345       (org-indent-mode . "")
346       ;; Major modes
347       (lisp-interaction-mode . "λ")
348       (hi-lock-mode . "")
349       (python-mode . "Py")
350       (emacs-lisp-mode . "EL")
351       (eshell-mode . "ε")
352       (nxhtml-mode . "nx"))
353     "Alist for `clean-mode-line'.
354
355   When you add a new element to the alist, keep in mind that you
356   must pass the correct minor/major mode symbol and a string you
357   want to use in the modeline *in lieu of* the original.")
358
359
360   (defun clean-mode-line ()
361     (interactive)
362     (loop for cleaner in mode-line-cleaner-alist
363           do (let* ((mode (car cleaner))
364                    (mode-str (cdr cleaner))
365                    (old-mode-str (cdr (assq mode minor-mode-alist))))
366                (when old-mode-str
367                    (setcar old-mode-str mode-str))
368                  ;; major mode
369                (when (eq mode major-mode)
370                  (setq mode-name mode-str)))))
371
372
373   (add-hook 'after-change-major-mode-hook 'clean-mode-line)
374 #+END_SRC
375
376 ** Powerline mode
377
378 Install powerline mode [[https://github.com/milkypostman/powerline][powerline]]
379
380 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
381   (use-package powerline
382     :ensure t
383     :config
384     ;; (powerline-center-theme)
385     )
386
387   ;; (use-package smart-mode-line
388   ;;   :ensure t)
389   ;; (use-package smart-mode-line-powerline-theme
390   ;;   :ensure t)
391 #+END_SRC
392
393 Revised powerline-center-theme
394
395 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
396   (defun sd/powerline-simpler-vc (s)
397     (if s
398         (replace-regexp-in-string "Git[:-]" "" s)
399       s))
400
401   (defun sd/powerline-center-theme_revised ()
402     "Setup a mode-line with major and minor modes centered."
403     (interactive)
404     (setq-default mode-line-format
405                   '("%e"
406                     (:eval
407                      (let* ((active (powerline-selected-window-active))
408                             (mode-line-buffer-id (if active 'mode-line-buffer-id 'mode-line-buffer-id-inactive))
409                             (mode-line (if active 'mode-line 'mode-line-inactive))
410                             (face1 (if active 'powerline-active1 'powerline-inactive1))
411                             (face2 (if active 'powerline-active2 'powerline-inactive2))
412                             (separator-left (intern (format "powerline-%s-%s"
413                                                             (powerline-current-separator)
414                                                             (car powerline-default-separator-dir))))
415                             (separator-right (intern (format "powerline-%s-%s"
416                                                              (powerline-current-separator)
417                                                              (cdr powerline-default-separator-dir))))
418                             (lhs (list (powerline-raw "%*" mode-line 'l)
419                                        ;; (powerline-buffer-size mode-line 'l)
420                                        (powerline-buffer-id mode-line-buffer-id 'l)
421                                        (powerline-raw " ")
422                                        (funcall separator-left mode-line face1)
423                                        (powerline-narrow face1 'l)
424                                        ;; (powerline-vc face1)
425                                        (sd/powerline-simpler-vc (powerline-vc face1))
426                                        ))
427                             (rhs (list (powerline-raw global-mode-string face1 'r)
428                                        (powerline-raw "%4l" face1 'r)
429                                        (powerline-raw ":" face1)
430                                        (powerline-raw "%3c" face1 'r)
431                                        (funcall separator-right face1 mode-line)
432                                        (powerline-raw " ")
433                                        (powerline-raw "%6p" mode-line 'r)
434                                        (powerline-hud face2 face1)))
435                             (center (list (powerline-raw " " face1)
436                                           (funcall separator-left face1 face2)
437                                           (when (and (boundp 'erc-track-minor-mode) erc-track-minor-mode)
438                                             (powerline-raw erc-modified-channels-object face2 'l))
439                                           (powerline-major-mode face2 'l)
440                                           (powerline-process face2)
441                                           (powerline-raw " :" face2)
442                                           (powerline-minor-modes face2 'l)
443                                           (powerline-raw " " face2)
444                                           (funcall separator-right face2 face1))))
445                        (concat (powerline-render lhs)
446                                (powerline-fill-center face1 (/ (powerline-width center) 2.0))
447                                (powerline-render center)
448                                (powerline-fill face1 (powerline-width rhs))
449                                (powerline-render rhs)))))))
450
451   (sd/powerline-center-theme_revised)
452 #+END_SRC
453
454 Fix the issue in mode line when showing triangle 
455
456 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
457   (setq ns-use-srgb-colorspace nil)
458 #+END_SRC
459
460 set height in mode line
461
462 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
463
464   (custom-set-variables
465    '(powerline-height 14)
466    '(powerline-text-scale-factor 0.8))
467   ;; 100/140
468   (set-face-attribute 'mode-line nil :height 100)
469
470 #+END_SRC
471
472 * Org-mode Settings
473
474 ** Org-mode Basic setting
475
476 Always indents header, and hide header leading starts so that no need type =#+STATUP: indent= 
477
478 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
479
480   (use-package org
481     :ensure t
482     :init
483     (setq org-startup-indented t)
484     (setq org-hide-leading-starts t)
485     (setq org-src-fontify-natively t)
486     (setq org-src-tab-acts-natively t)
487     (setq org-confirm-babel-evaluate nil)
488     (setq org-use-speed-commands t)
489     (setq org-completion-use-ido t))
490
491   (org-babel-do-load-languages
492    'org-babel-load-languages
493    '((python . t)
494      (C . t)
495      (perl . t)
496      (calc . t)
497      (latex . t)
498      (java . t)
499      (ruby . t)
500      (lisp . t)
501      (scheme . t)
502      (sh . t)
503      (sqlite . t)
504      (js . t)))
505
506   ;; use current window for org source buffer editting
507   (setq org-src-window-setup 'current-window )
508
509 #+END_SRC
510
511 ** Org-bullets
512
513 use [[https://github.com/sabof/org-bullets][org-bullets]] package to show utf-8 charactes
514
515 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
516   (use-package org-bullets
517     :ensure t
518     :init
519     (add-hook 'org-mode-hook
520               (lambda ()
521                 (org-bullets-mode t))))
522
523   (setq org-bullets-bullet-list '("⦿" "✪" "â—‰" "â—‹" "â–º" "â—†"))
524 #+END_SRC
525
526 ** Worf Mode
527
528 [[https://github.com/abo-abo/worf][worf]] mode is an extension of vi-like binding for org-mode. 
529 In =worf-mode=, it is mapping =[=, =]= as =worf-backward= and =worf-forward= in global, wich
530 cause we cannot input =[= and =]=, so here I unset this mappings. And redifined this two to
531 =M-[= and =M-]=. see this [[https://github.com/abo-abo/worf/issues/19#issuecomment-223756599][issue]]
532
533 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
534
535   (use-package worf
536     :ensure t
537     :commands worf-mode
538     :init (add-hook 'org-mode-hook 'worf-mode)
539     ;; :config
540     ;; (define-key worf-mode-map "[" nil)
541     ;; (define-key worf-mode-map "]" nil)
542     ;; (define-key worf-mode-map (kbd "M-[") 'worf-backward)
543     ;; (define-key worf-mode-map (kbd "M-]") 'worf-forward)
544     )
545
546 #+END_SRC
547
548 ** Get Things Done
549
550 Refer to [[http://doc.norang.ca/org-mode.html][Organize Your Life in Plain Text]]
551 *** basic setup
552
553 standard key binding
554
555 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
556   (global-set-key "\C-cl" 'org-store-link)
557   (global-set-key "\C-ca" 'org-agenda)
558   (global-set-key "\C-cb" 'org-iswitchb)
559 #+END_SRC
560
561 *** Plain List 
562
563 Replace the list bullet =-=, =+=,  with =•=, a litter change based [[https://github.com/howardabrams/dot-files/blob/master/emacs-org.org][here]]
564
565 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
566   ;; (use-package org-mode
567   ;;   :init
568   ;;   (font-lock-add-keywords 'org-mode
569   ;;    '(("^ *\\([-+]\\) "
570   ;;           (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "•")))))))
571 #+END_SRC
572  
573 *** Todo Keywords
574
575 refer to [[http://coldnew.github.io/coldnew-emacs/#orgheadline94][fancy todo states]], 
576
577 To track TODO state changes, the =!= is to insert a timetamp, =@= is to insert a note with
578 timestamp for the state change.
579
580 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
581     ;; (setq org-todo-keywords
582     ;;        '((sequence "☛ TODO(t)" "|" "✔ DONE(d)")
583     ;;          (sequence "âš‘ WAITING(w)" "|")
584     ;;          (sequence "|" "✘ CANCELLED(c)")))
585   ; (setq org-todo-keyword-faces
586   ;        (quote ("TODO" .  (:foreground "red" :weight bold))
587   ;               ("NEXT" .  (:foreground "blue" :weight bold))
588   ;               ("WAITING" . (:foreground "forest green" :weight bold))
589   ;               ("DONE" .  (:foreground "magenta" :weight bold))
590   ;               ("CANCELLED" . (:foreground "forest green" :weight bold))))
591
592
593   (setq org-todo-keywords
594         (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d!)")
595                 ;; (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING")
596                 (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" ))))
597
598   (setq org-todo-keyword-faces
599         (quote (("TODO" :foreground "red" :weight bold)
600                 ("NEXT" :foreground "blue" :weight bold)
601                 ("DONE" :foreground "forest green" :weight bold)
602                 ("WAITING" :foreground "orange" :weight bold)
603                 ("HOLD" :foreground "magenta" :weight bold)
604                 ("CANCELLED" :foreground "forest green" :weight bold)
605                 ;; ("MEETING" :foreground "forest green" :weight bold)
606                 ;; ("PHONE" :foreground "forest green" :weight bold)
607                 )))
608 #+END_SRC
609
610 Fast todo selections
611
612 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
613   (setq org-use-fast-todo-selection t)
614   (setq org-treat-S-cursor-todo-selection-as-state-change nil)
615 #+END_SRC
616
617 TODO state triggers and tags, [[http://doc.norang.ca/org-mode.html][Organize Your Life in Plain Text]]
618
619 - Moving a task to =CANCELLED=, adds a =CANCELLED= tag
620 - Moving a task to =WAITING=, adds a =WAITING= tag
621 - Moving a task to =HOLD=, add =HOLD= tags
622 - Moving a task to =DONE=, remove =WAITING=, =HOLD= tag
623 - Moving a task to =NEXT=, remove all waiting/hold/cancelled tags
624
625 This tags are used to filter tasks in agenda views
626 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
627   (setq org-todo-state-tags-triggers
628         (quote (("CANCELLED" ("CANCELLED" . t))
629                 ("WAITING" ("WAITING" . t))
630                 ("HOLD" ("WAITING") ("HOLD" . t))
631                 (done ("WAITING") ("HOLD"))
632                 ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
633                 ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
634                 ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))
635 #+END_SRC
636
637 Logging Stuff 
638 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
639   ;; log time when task done
640   ;; (setq org-log-done (quote time))
641   ;; save clocking into to LOGBOOK
642   (setq org-clock-into-drawer t)
643   ;; save state change notes and time stamp into LOGBOOK drawer
644   (setq org-log-into-drawer t)
645   (setq org-clock-into-drawer "CLOCK")
646 #+END_SRC
647
648 *** Tags
649 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
650   (setq org-tag-alist (quote ((:startgroup)
651                               ("@office" . ?e)
652                               ("@home" . ?h)
653                               (:endgroup)
654                               ("WAITING" . ?w)
655                               ("HOLD" . ?h)
656                               ("CANCELLED" . ?c))))
657
658   ;; Allow setting single tags without the menu
659   (setq org-fast-tag-selection-single-key (quote expert))
660 #+END_SRC
661
662 *** Capture - Refile - Archive
663
664 Capture lets you quickly store notes with little interruption of your work flow.
665
666 **** Capture Templates
667
668 When a new taks needs to be added, categorize it as 
669
670 All captured file which need next actions are stored in =refile.org=, 
671 - A new task / note (t) =refile.org=
672 - A work task in office =office.org=
673 - A jourenl =diary.org=
674 - A new habit (h) =refile.org=
675
676 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
677   (setq org-directory "~/org")
678   (setq org-default-notes-file "~/org/refile.org")
679   (setq sd/org-diary-file "~/org/diary.org")
680
681   (global-set-key (kbd "C-c c") 'org-capture)
682
683   (setq org-capture-templates
684         (quote (("t" "Todo" entry (file org-default-notes-file)
685                  "* TODO %?\n:LOGBOOK:\n- Added: %U\t\tAt: %a\n:END:")
686                 ("n" "Note" entry (file org-default-notes-file)
687                  "* %? :NOTE:\n:LOGBOOK:\n- Added: %U\t\tAt: %a\n:END:")
688                 ("j" "Journal" entry (file+datetree sd/org-diary-file)
689                  "* %?\n:LOGBOOK:\n:END:" :clock-in t :clock-resume t)
690                 ("h" "Habit" entry (file org-default-notes-file)
691                  "* NEXT %?\n:LOGBOOK:\n%a\nSCHEDULED: %(format-time-string \"%<<%Y-%m-%d %a .+1d/3d>>\")\n:END:\n:PROPERTIES:\n:STYLE: habit\n:REPEAT_TO_STATE: NEXT\n:END:\n "))))
692 #+END_SRC
693
694 **** Refiling Tasks
695
696 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
697   (setq org-refile-targets (quote (;; (nil :maxlevel . 9)
698                                    (org-agenda-files :maxlevel . 9))))
699
700   (setq org-refile-use-outline-path t)
701
702   (setq org-refile-allow-creating-parent-nodes (quote confirm))
703 #+END_SRC
704
705 *** Agenda Setup
706 Setting agenda files and the agenda view
707 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
708   (setq org-agenda-files (quote ("~/org/gtd.org"
709                                  "~/org/work.org")))
710
711   ;; only show today's tasks in agenda view
712   (setq org-agenda-span 'day)
713   ;; Use current windows for agenda view
714   (setq org-agenda-window-setup 'current-window)
715
716   ;; show all feature entries for repeating tasks,
717   ;; this is already setting by default
718   (setq org-agenda-repeating-timestamp-show-all t)
719
720   ;; Show all agenda dates - even if they are empty
721   (setq org-agenda-show-all-dates t)
722 #+END_SRC
723
724 ** Export PDF
725
726 Install MacTex-basic and some tex packages
727
728 #+BEGIN_SRC bash 
729
730   sudo tlmgr update --self
731
732   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
733
734 #+END_SRC
735
736 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
737   ;; ;; allow for export=>beamer by placing
738
739   ;; http://emacs-fu.blogspot.com/2011/04/nice-looking-pdfs-with-org-mode-and.html
740   ;; #+LaTeX_CLASS: beamer in org files
741   (unless (boundp 'org-export-latex-classes)
742     (setq org-export-latex-classes nil))
743   (add-to-list 'org-export-latex-classes
744     ;; beamer class, for presentations
745     '("beamer"
746        "\\documentclass[11pt]{beamer}\n
747         \\mode<{{{beamermode}}}>\n
748         \\usetheme{{{{beamertheme}}}}\n
749         \\usecolortheme{{{{beamercolortheme}}}}\n
750         \\beamertemplateballitem\n
751         \\setbeameroption{show notes}
752         \\usepackage[utf8]{inputenc}\n
753         \\usepackage[T1]{fontenc}\n
754         \\usepackage{hyperref}\n
755         \\usepackage{color}
756         \\usepackage{listings}
757         \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
758     frame=single,
759     basicstyle=\\small,
760     showspaces=false,showstringspaces=false,
761     showtabs=false,
762     keywordstyle=\\color{blue}\\bfseries,
763     commentstyle=\\color{red},
764     }\n
765         \\usepackage{verbatim}\n
766         \\institute{{{{beamerinstitute}}}}\n          
767          \\subject{{{{beamersubject}}}}\n"
768
769        ("\\section{%s}" . "\\section*{%s}")
770  
771        ("\\begin{frame}[fragile]\\frametitle{%s}"
772          "\\end{frame}"
773          "\\begin{frame}[fragile]\\frametitle{%s}"
774          "\\end{frame}")))
775
776     ;; letter class, for formal letters
777
778     (add-to-list 'org-export-latex-classes
779
780     '("letter"
781        "\\documentclass[11pt]{letter}\n
782         \\usepackage[utf8]{inputenc}\n
783         \\usepackage[T1]{fontenc}\n
784         \\usepackage{color}"
785  
786        ("\\section{%s}" . "\\section*{%s}")
787        ("\\subsection{%s}" . "\\subsection*{%s}")
788        ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
789        ("\\paragraph{%s}" . "\\paragraph*{%s}")
790        ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
791
792
793   (require 'ox-md)
794   (require 'ox-beamer)
795
796   (setq org-latex-pdf-process
797         '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
798           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
799           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
800
801   (setq TeX-parse-self t)
802
803   (setq TeX-PDF-mode t)
804   (add-hook 'LaTeX-mode-hook
805             (lambda ()
806               (LaTeX-math-mode)
807               (setq TeX-master t)))
808
809 #+END_SRC
810
811 ** others
812
813 extend org-mode's easy templates, refer to [[http://coldnew.github.io/coldnew-emacs/#orgheadline94][Extend org-modes' esay templates]]
814
815 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
816
817   (add-to-list 'org-structure-template-alist
818                '("E" "#+BEGIN_SRC emacs-lisp :tangle yes :results silent\n?\n#+END_SRC"))
819   (add-to-list 'org-structure-template-alist
820                '("S" "#+BEGIN_SRC sh\n?\n#+END_SRC"))
821   (add-to-list 'org-structure-template-alist
822                '("p" "#+BEGIN_SRC plantuml :file uml.png \n?\n#+END_SRC"))
823
824 #+END_SRC
825
826 * Magit
827
828 [[https://github.com/magit/magit][Magit]] is a very cool git interface on Emacs.
829
830 and Defined keys, using vi keybindings, Refer abo-abo's setting [[https://github.com/abo-abo/oremacs/blob/c5cafdcebc88afe9e73cc8bd40c49b70675509c7/modes/ora-nextmagit.el][here]]
831
832 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
833   (use-package magit
834     :ensure t
835     :commands magit-status magit-blame
836     :config
837     (dolist (map (list magit-status-mode-map
838                        magit-log-mode-map
839                        magit-diff-mode-map
840                        magit-staged-section-map))
841       (define-key map "j" 'magit-section-forward)
842       (define-key map "k" 'magit-section-backward)
843       (define-key map "n" nil)
844       (define-key map "p" nil)
845       (define-key map "v" 'recenter-top-bottom)
846       (define-key map "i" 'magit-section-toggle)))
847 #+END_SRC
848
849 * IDO & SMEX
850
851 ** IDO
852
853 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
854
855   (use-package ido
856     :ensure t
857     :init (setq ido-enable-flex-matching t
858                 ido-ignore-extensions t
859                 ido-use-virtual-buffers t
860                 ido-everywhere t)
861     :config
862     (ido-mode 1)
863     (ido-everywhere 1)
864     (add-to-list 'completion-ignored-extensions ".pyc"))
865
866   (icomplete-mode t)
867
868 #+END_SRC
869
870 ** FLX
871
872 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
873
874   (use-package flx-ido
875     :ensure t
876     :init (setq ido-enable-flex-matching t
877                 ido-use-faces nil)
878     :config (flx-ido-mode 1))
879
880 #+END_SRC
881
882 ** IDO-vertically
883
884 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
885
886   (use-package ido-vertical-mode
887     :ensure t
888     :init
889     (setq ido-vertical-define-keys 'C-n-C-p-up-and-down)
890     :config
891     (ido-vertical-mode 1))
892
893 #+END_SRC
894
895 ** SMEX
896
897 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
898
899   (use-package smex
900     :ensure t
901     :init (smex-initialize)
902     :bind
903     ("M-x" . smex)
904     ("M-X" . smex-major-mode-commands))
905
906 #+END_SRC
907
908 ** Ido-ubiquitous
909
910 Use [[https://github.com/DarwinAwardWinner/ido-ubiquitous][ido-ubiquitous]] for ido everywhere. It makes =describe-function= can also use ido
911
912 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
913
914   (use-package ido-ubiquitous
915     :ensure t
916     :init
917     (setq magit-completing-read-function 'magit-ido-completing-read)
918     (setq gnus-completing-read-function 'gnus-ido-completing-read)
919     :config
920     (ido-ubiquitous-mode 1))
921
922 #+END_SRC
923
924 ** Ido-exit-target
925
926 [[https://github.com/waymondo/ido-exit-target][ido-exit-target]] let you open file/buffer on =other-windows= when call =ido-switch-buffer=
927
928 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
929
930   (use-package ido-exit-target
931     :ensure t
932     :init
933     (define-key ido-common-completion-map (kbd "C-j") #'ido-exit-target-split-window-right)
934     (define-key ido-common-completion-map (kbd "C-l") #'ido-exit-target-split-window-below))
935
936 #+END_SRC
937
938 * Key bindings
939
940 ** Remove prefix =ESC=, refer [[http://emacs.stackexchange.com/questions/14755/how-to-remove-bindings-to-the-esc-prefix-key][here]]
941
942 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
943
944   ;; (define-key key-translation-map (kbd "ESC") (kbd "C-g"))
945
946 #+END_SRC
947
948 ** Esc on Minibuffer
949
950 Use =ESC= to exit minibuffer. Also I map =Super-h= the same as =C-g=
951
952 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
953
954   (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
955   (define-key minibuffer-local-map [escape]  'keyboard-escape-quit)
956   (define-key minibuffer-local-ns-map [escape]  'keyboard-escape-quit)
957   (define-key minibuffer-local-isearch-map [escape]  'keyboard-escape-quit)
958   (define-key minibuffer-local-completion-map [escape]  'keyboard-escape-quit)
959   (define-key minibuffer-local-must-match-map [escape]  'keyboard-escape-quit)
960   (define-key minibuffer-local-must-match-filename-map [escape]  'keyboard-escape-quit)
961   (define-key minibuffer-local-filename-completion-map [escape]  'keyboard-escape-quit)
962   (define-key minibuffer-local-filename-must-match-map [escape]  'keyboard-escape-quit)
963
964   ;; Also map s-h same as C-g
965   (define-key minibuffer-local-map (kbd "s-h") 'keyboard-escape-quit)
966
967 #+END_SRC
968
969 ** =Ctrl= key bindings
970
971 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
972   ;;
973
974   ;; C-h    help    
975   ;; C-j    newline and indent
976   ;; C-k    kill line
977   ;; C-l    recenter-top-bottom
978   ;; (global-set-key (kbd "C-;") 'ido-switch-buffer)
979   ;; C-;
980   ;; C-'   
981   ;; C-ret  
982
983   ;; C-n    next-line
984   ;; C-m
985   ;; C-,
986   ;; C-.
987   ;; C-/
988
989   ;; C-y
990   ;; C-u
991   ;; C-i
992   ;; C-o
993   ;; C-p
994   ;; C-[
995   ;; C-]
996   ;; C-\
997
998   ;; C-=
999   ;; C--
1000   ;; C-0
1001   ;; C-9
1002   ;; C-8
1003   ;; C-7
1004
1005   ;; C-Space
1006
1007
1008
1009
1010
1011
1012
1013 #+END_SRC
1014
1015 ** =Super= bindings for file, buffer and windows
1016
1017 Some global bindings on =Super=, on Mac, it is =Command=
1018
1019 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1020
1021   (global-set-key (kbd "s-h") 'keyboard-quit)
1022   (global-set-key (kbd "s-j") 'ido-switch-buffer)
1023   (global-set-key (kbd "s-k") 'ido-find-file)
1024   ;; s-k  -->  kill-this-buffer
1025   (global-set-key (kbd "s-l") (lambda ()
1026                                 (interactive)
1027                                 (if (> (length (window-list)) 1) 
1028                                     (delete-window)
1029                                   (message "Only one Windows now!"))))
1030   ;; s-l  -->  goto-line
1031   (global-set-key (kbd "s-;") 'swiper)
1032   ;; s-;  -->
1033   ;; s-'  -->  'next-multiframe-window
1034   (global-set-key (kbd "<s-return>") 'toggle-frame-fullscreen)
1035
1036   ;; (global-set-key (kbd "s-y") 'projectile-find-file)
1037   (global-set-key (kbd "s-f") 'projectile-find-file)
1038   ;; (global-set-key (kbd "s-[") 'persp-next)
1039   ;; (global-set-key (kbd "s-]") 'persp-prev)
1040
1041   (global-set-key (kbd "s-`") 'mode-line-other-buffer)
1042
1043   (global-set-key (kbd "s-n") 'persp-next)
1044   (global-set-key (kbd "s-p") 'persp-prev)
1045
1046
1047
1048   ;; someothers default mapping on super (command) key
1049   ;; s-s save-buffer
1050   ;; s-k kill-this-buffer
1051
1052
1053   ;; s-h  -->  ns-do-hide-emacs
1054   ;; s-j  -->  ido-switch-buffer  +
1055   ;; s-k  -->  kill-this-buffer
1056   ;; s-l  -->  goto-line
1057   ;; s-;  -->  undefined
1058   ;; s-'  -->  next-multiframe-window
1059   ;; s-ret --> toggle-frame-fullscreen +
1060
1061   ;; s-y  -->  ns-paste-secondary
1062   ;; s-u  -->  revert-buffer
1063   ;; s-i  -->  undefined - but used for iterm globally
1064   ;; s-o  -->  used for emacs globally
1065   ;; s-p  -->  projectile-persp-switch-project  +  
1066   ;; s-[  -->  next-buffer  +    
1067   ;; s-]  -->  previous-buffer +
1068
1069   ;; s-0  -->  undefined
1070   ;; s-9  -->  undefined
1071   ;; s-8  -->  undefined
1072   ;; s-7  -->  undefined
1073   ;; s-6  -->  undefined
1074   ;; s--  -->  center-line
1075   ;; s-=  -->  undefined
1076
1077   ;; s-n  -->  make-frame
1078   ;; s-m  -->  iconify-frame
1079   ;; s-b  -->  undefined
1080   ;; s-,  -->  customize
1081   ;; s-.  -->  undefined
1082   ;; s-/  -->  undefined
1083
1084   ;; s-g  -->  isearch-repeat-forward
1085   ;; s-f  -->  projectile-find-file   +
1086   ;; s-d  -->  isearch-repeat-background
1087   ;; s-s  -->  save-buffer
1088   ;; s-a  -->  make-whole-buffer
1089
1090   ;; s-b  -->  undefined
1091   ;; s-v  -->  yank
1092   ;; s-c  -->  ns-copy-including-secondary
1093
1094   ;; s-t  -->  ns-popup-font-panel
1095   ;; s-r  -->  undefined
1096   ;; s-e  -->  isearch-yanqk-kill
1097   ;; s-w  -->  delete-frame
1098   ;; s-q  -->  same-buffers-kill-emacs
1099
1100   ;; s-`  -->  other-frame
1101 #+END_SRC
1102
1103 ** =M-s= bindings for searching
1104
1105 I use the prefix =M-s= for searching in buffers
1106
1107 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1108
1109   (defun pl-make-keymap (key bindings)
1110     (setq keymap (make-sparse-keymap))
1111     (dolist (binding bindings)
1112       (define-key keymap (car binding) (cdr binding)))
1113     (global-set-key key keymap))
1114
1115   (define-key minibuffer-local-map "\M-s" nil)
1116
1117   (global-set-key (kbd "M-s s") 'isearch-forward-regexp)
1118   (define-key isearch-mode-map "\M-s" 'isearch-repeat-forward)
1119   (global-set-key (kbd "M-s r") 'isearch-backward-regexp)
1120   (define-key isearch-mode-map "\M-r" 'isearch-repeat-backward)
1121
1122   (global-set-key (kbd "s-/") 'isearch-forward-regexp)
1123   (define-key isearch-mode-map (kbd  "s-/") 'isearch-repeat-forward)
1124   (define-key isearch-mode-map (kbd  "C-n") 'isearch-repeat-forward)
1125
1126
1127   (set-face-background 'ido-first-match "white")
1128
1129   ;; M-s o  -->  occur
1130   ;; M-s s  -->  isearch-forward-regexp
1131   ;; M-s r  -->  isearch-backward-regexp
1132   ;; M-s w  -->  isearch-forward-word
1133   ;; M-s .  -->  isearch-forward-symbol-at-point
1134   ;; M-s _  -->  isearch-forward-symbol
1135
1136   ;; highlight bindings
1137   ;; M-s h .  -->  highlight-symbol-at-point
1138   ;; M-s h r  -->  highlight-regexp
1139   ;; M-s h u  -->  unhighlight-regexp
1140   ;; M-s h l  -->  highlight-lines-match-regexp
1141   ;; M-s h p  -->  highlight-phrase
1142   ;; M-s h f  -->  hi-lock-find-patterns
1143
1144   ;; 
1145   ;; (global-set-key (kbd "M-s M-r") 'isearch-backward-regexp)
1146   ;;
1147
1148   ;; M-c
1149   ;; M-r
1150   ;; M-t
1151   ;; M-u, 
1152 #+END_SRC
1153
1154 Occur search key bindings
1155
1156 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1157
1158   (define-key occur-mode-map (kbd "C-n")
1159     (lambda ()
1160       (interactive)
1161       (occur-next)
1162       (occur-mode-goto-occurrence-other-window)
1163       (recenter)
1164       (other-window 1)))
1165
1166   (define-key occur-mode-map (kbd "C-p")
1167     (lambda ()
1168       (interactive)
1169       (occur-prev)
1170       (occur-mode-goto-occurrence-other-window)
1171       (recenter)
1172       (other-window 1)))
1173
1174 #+END_SRC
1175
1176
1177 ** =M-o= as prefix key for windows
1178
1179 ** =M-g= as prefix key for launcher
1180
1181 ** others
1182
1183 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1184
1185
1186   ;; C-'    undefined
1187   ;; C-.    undefined
1188 #+END_SRC
1189
1190 * Eshell
1191
1192 Eshell alias
1193
1194 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1195
1196   (defalias 'e 'ido-find-file)
1197   (defalias 'ff 'ido-find-file)
1198   (defalias 'ee 'ido-find-file-other-window)
1199
1200 #+END_SRC
1201
1202 Quickly start eshll in split window below, refer [[http://www.howardism.org/Technical/Emacs/eshell-fun.html][eshell-here]]
1203
1204 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1205   ;; (defun eshell-x ()
1206   ;;   (insert "exit")
1207   ;;   (eshell-send-input)
1208   ;;   (delete-window))
1209
1210   ;; (defun sd/toggle-eshell ()
1211   ;;   "Open a eshell windows vertically"
1212   ;;   (interactive)
1213   ;;   (if 1
1214   ;;       (message "true")
1215   ;;     (progn
1216   ;;       (split-window-vertically (- (/ (window-total-height) 3)))
1217   ;;       (other-window 1)
1218   ;;       (switch-to-buffer eshell-buffer-name)
1219   ;;       (goto-char (point-max))
1220   ;;       (eshell-kill-input)
1221   ;;       (insert (format "cd %s" default-directory))
1222   ;;       (eshell-send-input)
1223   ;;       (goto-char (point-max))
1224   ;;       (insert (concat "ls"))
1225   ;;       (eshell-send-input))))
1226   ;; (defun eshell-here ()
1227   ;;   "Opens up a new shell in the directory associated with the
1228   ;; current buffer's file. The eshell is renamed to match that
1229   ;; directory to make multiple eshell windows easier."
1230   ;;   (interactive)
1231   ;;   (let* ((parent (if (buffer-file-name)
1232   ;;                      (file-name-directory (buffer-file-name))
1233   ;;                    default-directory))
1234   ;;          (height (/ (window-total-height) 3))
1235   ;;          (name   (car (last (split-string parent "/" t))))
1236   ;;          (eshell-name (concat "*eshell: " name "*")))
1237   ;;     (split-window-vertically (- height))
1238   ;;     (other-window 1)
1239   ;;     (if (get-buffer eshell-name)
1240   ;;         (progn
1241   ;;           (message "buffer exist")
1242   ;;           (switch-to-buffer eshell-name))
1243   ;;       (progn
1244   ;;         (eshell "new")
1245   ;;         (rename-buffer eshell-name)
1246
1247   ;;         (insert (concat "ls"))
1248   ;;         (eshell-send-input)))))
1249
1250   ;; (global-unset-key (kbd "M-`"))
1251   ;; (global-set-key (kbd "M-`") #'eshell-here)
1252 #+END_SRC
1253
1254 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1255   (defun sd/window-has-eshell ()
1256     "Check if current windows list has a eshell buffer, and return the window"
1257     (interactive)
1258     (let ((ret nil))
1259       (dolist (window (window-list))
1260         (let ((modename (with-current-buffer (window-buffer window)
1261                           major-mode)))
1262           (if (equal modename 'eshell-mode)
1263               (setq ret window))))
1264       ret))
1265
1266   (defun sd/toggle-eshell-here ()
1267     "Toggle a eshell buffer vertically"
1268     (interactive)
1269     (if (sd/window-has-eshell)
1270         (if (equal major-mode 'eshell-mode)
1271             (delete-window)
1272           (select-window (sd/window-has-eshell)))
1273       (progn
1274         (split-window-vertically (- (/ (window-total-height) 3)))
1275         (other-window 1)
1276         (unless (and (boundp 'eshell-buffer-name) (get-buffer eshell-buffer-name))
1277           (eshell))
1278         (switch-to-buffer eshell-buffer-name)
1279         (goto-char (point-max))
1280         (eshell-kill-input)
1281         (insert (format "cd %s" default-directory))
1282         (eshell-send-input))))
1283
1284   (global-unset-key (kbd "M-`"))
1285   (global-set-key (kbd "M-`") 'sd/toggle-eshell-here)
1286 #+END_SRC
1287
1288 * Misc Settings
1289
1290 ** [[https://github.com/abo-abo/hydra][Hydra]]
1291 *** hydra install
1292 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1293   (use-package hydra
1294     :ensure t)
1295   ;; disable new line in minibuffer when hint hydra
1296   (setq hydra-lv nil)
1297 #+END_SRC
1298
1299 *** Font Zoom
1300
1301 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1302     (defhydra sd/font-zoom (global-map "<f2>")
1303
1304     "zoom"
1305     ("g" text-scale-increase "in")
1306     ("l" text-scale-decrease "out"))
1307 #+END_SRC
1308
1309 *** Windmove Splitter
1310
1311 Refer [[https://github.com/abo-abo/hydra/blob/master/hydra-examples.el][hydra-example]], to enlarge or shrink the windows splitter
1312
1313 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1314
1315   (defun hydra-move-splitter-left (arg)
1316     "Move window splitter left."
1317     (interactive "p")
1318     (if (let ((windmove-wrap-around))
1319           (windmove-find-other-window 'right))
1320         (shrink-window-horizontally arg)
1321       (enlarge-window-horizontally arg)))
1322
1323   (defun hydra-move-splitter-right (arg)
1324     "Move window splitter right."
1325     (interactive "p")
1326     (if (let ((windmove-wrap-around))
1327           (windmove-find-other-window 'right))
1328         (enlarge-window-horizontally arg)
1329       (shrink-window-horizontally arg)))
1330
1331   (defun hydra-move-splitter-up (arg)
1332     "Move window splitter up."
1333     (interactive "p")
1334     (if (let ((windmove-wrap-around))
1335           (windmove-find-other-window 'up))
1336         (enlarge-window arg)
1337       (shrink-window arg)))
1338
1339   (defun hydra-move-splitter-down (arg)
1340     "Move window splitter down."
1341     (interactive "p")
1342     (if (let ((windmove-wrap-around))
1343           (windmove-find-other-window 'up))
1344         (shrink-window arg)
1345       (enlarge-window arg)))
1346
1347 #+END_SRC
1348
1349 *** hydra-window
1350
1351 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1352   (winner-mode 1)
1353
1354   (defhydra sd/hydra-window (:color red :columns nil)
1355     "window"
1356     ("h" windmove-left nil :exit t)
1357     ("j" windmove-down nil :exit t)
1358     ("k" windmove-up nil :exit t)
1359     ("l" windmove-right nil :exit t)
1360     ("H" hydra-move-splitter-left nil)
1361     ("J" hydra-move-splitter-down nil)
1362     ("K" hydra-move-splitter-up nil)
1363     ("L" hydra-move-splitter-right nil)
1364     ("v" (lambda ()
1365            (interactive)
1366            (split-window-right)
1367            (windmove-right))
1368      "vert" :exit t)
1369     ("x" (lambda ()
1370            (interactive)
1371            (split-window-below)
1372            (windmove-down))
1373      "horz" :exit t)
1374     ("o" delete-other-windows "one" :exit t)
1375     ("a" ace-window "ace")
1376     ("s" ace-swap-window "swap")
1377     ("d" ace-delete-window "ace-one" :exit t)
1378     ("i" ace-maximize-window "ace-one" :exit t)
1379     ("b" ido-switch-buffer "buf")
1380     ;; ("m" headlong-bookmark-jump "bmk")
1381     ("q" nil "cancel")
1382     ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
1383     ("r" (progn (winner-redo) (setq this-command 'winner-redo)) "redo")
1384     ("f" nil))
1385
1386   (global-unset-key (kbd "C-o"))
1387   (global-set-key (kbd "C-o") 'sd/hydra-window/body)
1388
1389   (defun triggle-windows-max-size ()
1390     (interactive)
1391     (if (> (length (window-list)) 1)
1392         (delete-other-windows)
1393       (winner-undo)))
1394
1395 #+END_SRC
1396
1397 ** Line Number
1398
1399 Enable linum mode on programming modes
1400
1401 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1402
1403   (add-hook 'prog-mode-hook 'linum-mode)
1404
1405 #+END_SRC
1406
1407 Fix the font size of line number
1408
1409 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1410
1411   (defun fix-linum-size ()
1412        (interactive)
1413        (set-face-attribute 'linum nil :height 110))
1414
1415   (add-hook 'linum-mode-hook 'fix-linum-size)
1416
1417 #+END_SRC
1418
1419 I like [[https://github.com/coldnew/linum-relative][linum-relative]], just like the =set relativenumber= on =vim=
1420
1421 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1422
1423   (use-package linum-relative
1424     :ensure t
1425     :config
1426     (defun linum-new-mode ()
1427       "If line numbers aren't displayed, then display them.
1428   Otherwise, toggle between absolute and relative numbers."
1429       (interactive)
1430       (if linum-mode
1431           (linum-relative-toggle)
1432         (linum-mode 1)))
1433
1434     :bind
1435     ("A-k" . linum-new-mode))
1436
1437   ;; auto enable linum-new-mode in programming modes
1438   (add-hook 'prog-mode-hook 'linum-relative-mode)
1439
1440 #+END_SRC
1441
1442 ** Save File Position
1443
1444 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1445
1446   (require 'saveplace)
1447   (setq-default save-place t)
1448   (setq save-place-forget-unreadable-files t)
1449   (setq save-place-skip-check-regexp "\\`/\\(?:cdrom\\|floppy\\|mnt\\|/[0-9]\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)")
1450
1451 #+END_SRC
1452
1453 ** Multi-term
1454
1455 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1456
1457   (use-package multi-term
1458     :ensure t)
1459
1460 #+END_SRC
1461
1462 ** ace-link
1463
1464 [[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
1465 Type =o= to go to the link
1466
1467 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1468
1469   (use-package ace-link
1470     :ensure t
1471     :init
1472     (ace-link-setup-default))
1473
1474 #+END_SRC
1475
1476 ** Emux
1477
1478 [[https://github.com/re5et/emux][emux]] is 
1479
1480 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1481
1482   (el-get-bundle re5et/emux)
1483
1484 #+END_SRC
1485
1486 ** Smart Parens
1487
1488 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1489
1490   (use-package smartparens
1491     :ensure t
1492     :config
1493     (progn
1494       (require 'smartparens-config)
1495       (add-hook 'prog-mode-hook 'smartparens-mode)))
1496
1497 #+END_SRC
1498
1499 ** Ace-Windows
1500
1501 [[https://github.com/abo-abo/ace-window][ace-window]] 
1502
1503 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1504
1505   (use-package ace-window
1506     :ensure t
1507     :defer t
1508   ;  :init
1509   ;  (global-set-key (kbd "M-o") 'ace-window)
1510     :config
1511     (setq aw-keys '(?a ?s ?d ?f ?j ?k ?l)))
1512
1513 #+END_SRC
1514
1515 ** Projectile
1516
1517 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1518
1519   (use-package projectile
1520     :ensure t
1521     :init
1522     (setq projectile-enable-caching t)
1523     :config
1524     (projectile-global-mode t))
1525
1526   (use-package persp-projectile
1527     :ensure t
1528     :config
1529     (persp-mode)
1530     :bind
1531     (:map projectile-mode-map
1532           ("s-t" . projectile-persp-switch-project)))
1533
1534   ;; projectile-find-file
1535   ;; projectile-switch-buffer
1536   ;; projectile-find-file-other-window
1537 #+END_SRC
1538
1539 ** Which key
1540
1541 [[https://github.com/justbur/emacs-which-key][which-key]] show the key bindings 
1542
1543 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1544
1545   (use-package which-key
1546     :ensure t
1547     :config
1548     (which-key-mode))
1549
1550 #+END_SRC
1551
1552 ** Emms
1553
1554 We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
1555
1556 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1557   (use-package emms
1558     :ensure t
1559     :init
1560     (setq emms-source-file-default-directory "~/Music/")
1561     :config
1562     (emms-standard)
1563     (emms-default-players)
1564     (define-emms-simple-player mplayer '(file url)
1565       (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
1566                     ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
1567                     "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
1568                     ".m4a" ".flv" ".ogv" ".pls"))
1569       "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
1570     (emms-history-load))
1571
1572 #+END_SRC
1573
1574 ** GnoGo
1575
1576 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
1577
1578 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1579
1580   (use-package gnugo
1581     :ensure t
1582     :defer t
1583     :init
1584     (require 'gnugo-imgen)
1585     (setq gnugo-xpms 'gnugo-imgen-create-xpms)
1586     (add-hook 'gnugo-start-game-hook '(lambda ()
1587                                         (gnugo-image-display-mode)
1588                                         (gnugo-grid-mode)))
1589       :config
1590     (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
1591
1592 #+END_SRC
1593
1594 ** undo-tree
1595
1596 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1597
1598   (use-package undo-tree
1599     :ensure t
1600     :config
1601     (global-undo-tree-mode 1))
1602
1603 #+END_SRC
1604
1605 ** swiper
1606
1607 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1608
1609   (use-package swiper
1610     :ensure t)
1611
1612
1613   (ivy-mode 1)
1614   (setq ivy-use-virtual-buffers t)
1615   ;; (global-set-key "\C-s" 'swiper)
1616   ;; (global-set-key (kbd "C-c C-r") 'ivy-resume)
1617   ;; (global-set-key (kbd "<f6>") 'ivy-resume)
1618   ;; ;; (global-set-key (kbd "M-x") 'counsel-M-x)
1619   ;; ;; (global-set-key (kbd "C-x C-f") 'counsel-find-file)
1620   ;; (global-set-key (kbd "<f1> f") 'counsel-describe-function)
1621   ;; (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
1622   ;; (global-set-key (kbd "<f1> l") 'counsel-load-library)
1623   ;; (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
1624   ;; (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
1625   ;; (global-set-key (kbd "C-c g") 'counsel-git)
1626   ;; (global-set-key (kbd "C-c j") 'counsel-git-grep)
1627   ;; (global-set-key (kbd "C-c k") 'counsel-ag)
1628   ;; (global-set-key (kbd "C-x l") 'counsel-locate)
1629   ;; (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
1630   ;; ;; (define-key read-expression-map (kbd "C-r") 'counsel-expression-history)
1631
1632 #+END_SRC
1633
1634 ** Tabbar
1635
1636 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1637
1638   ;; (use-package tabbar-ruler
1639   ;;   :ensure t
1640   ;;   :init
1641   ;;   (setq tabbar-ruler-global-tabbar t)
1642   ;;   (setq tabbar-ruler-global-ruler t)
1643   ;;   (setq tabbar-ruler-popu-menu t)
1644   ;;   (setq tabbar-ruler-popu-toolbar t)
1645   ;;   (setq tabbar-use-images t)
1646   ;;   :config
1647   ;;   (tabbar-ruler-group-by-projectile-project)
1648   ;;   (global-set-key (kbd "s-1") 'tabbar-forward-group)
1649   ;;   (global-set-key (kbd "s-2") 'tabbar-ruler-forward))
1650
1651 #+END_SRC
1652
1653 * Programming
1654
1655 ** Languages
1656
1657 *** Emacs Lisp
1658
1659 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1660
1661   (use-package color-identifiers-mode
1662     :ensure t
1663     :init
1664     (add-hook 'emacs-lisp-mode-hook 'color-identifiers-mode)
1665
1666     :diminish color-identifiers-mode)
1667
1668   (global-prettify-symbols-mode t)
1669
1670 #+END_SRC
1671
1672 **** Lispy Mode
1673
1674 In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
1675 bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
1676
1677 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1678
1679   (use-package lispy
1680     :ensure t
1681     :init
1682     (eval-after-load 'lispy
1683       '(progn
1684          (define-key lispy-mode-map (kbd "M-o") nil)))
1685     :config
1686     (add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1))))
1687
1688 #+END_SRC
1689
1690 *** Perl
1691
1692 [[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
1693
1694 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1695
1696   (defalias 'perl-mode 'cperl-mode)
1697
1698   ;(setq cperl-hairy t) ;; Turns on most of the CPerlMode options
1699   (setq cperl-auto-newline t)
1700   (setq cperl-highlight-variables-indiscriminately t)
1701   ;(setq cperl-indent-level 4)
1702   ;(setq cperl-continued-statement-offset 4)
1703   (setq cperl-close-paren-offset -4)
1704   (setq cperl-indent-parents-as-block t)
1705   (setq cperl-tab-always-indent t)
1706   ;(setq cperl-brace-offset  0)
1707
1708   (add-hook 'cperl-mode-hook
1709             '(lambda ()
1710                (cperl-set-style "C++")))
1711
1712   ;(require 'template)
1713   ;(template-initialize)
1714   ;(require 'perlnow)
1715
1716 #+END_SRC
1717
1718 - auto insert
1719 - run script 
1720
1721 Change the compile-command to set the default command run when call =compile=
1722 Mapping =s-r= (on Mac, it's =Command + R= to run the script. Here =current-prefix-arg= is set
1723 to call =compilation=  interactively.
1724
1725 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1726
1727   (defun my-perl-hook ()
1728     (progn
1729       (setq-local compilation-read-command nil)
1730       (set (make-local-variable 'compile-command)
1731            (concat "/usr/bin/perl "
1732                    (if buffer-file-name
1733                        (shell-quote-argument buffer-file-name))))
1734       (local-set-key (kbd "s-r")
1735                        (lambda ()
1736                          (interactive)
1737   ;                       (setq current-prefix-arg '(4)) ; C-u
1738                          (call-interactively 'compile)))))
1739
1740   (add-hook 'cperl-mode-hook 'my-perl-hook)
1741
1742
1743 #+END_SRC
1744
1745 *** C & C++
1746
1747 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1748
1749   (setq c-default-style "stroustrup"
1750         c-basic-offset 4)
1751
1752 #+END_SRC
1753
1754 ** Compile
1755
1756 Set the environments vairables in compilation mode
1757
1758 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1759
1760   (use-package compile
1761     :commands compile
1762     :config
1763     (setq compilation-environment (cons "LC_ALL=C" compilation-environment)))
1764
1765 #+END_SRC
1766
1767 ** Auto-Insert
1768
1769 Enable auto-insert mode
1770
1771 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1772
1773   (auto-insert-mode t)
1774   (setq auto-insert-query nil)
1775
1776 #+END_SRC
1777
1778 *** C++ Auto Insert
1779
1780 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1781
1782   (eval-after-load 'autoinsert
1783     '(define-auto-insert '("\\.cpp\\'" . "C++ skeleton")
1784        '(
1785          "Short description:"
1786          "/*"
1787          "\n * " (file-name-nondirectory (buffer-file-name))
1788          "\n */" > \n \n
1789          "#include <iostream>" \n
1790          "#include \""
1791          (file-name-sans-extension
1792           (file-name-nondirectory (buffer-file-name)))
1793          ".hpp\"" \n \n
1794          "using namespace std;" \n \n
1795          "int main ()"
1796          "\n{" \n 
1797          > _ \n
1798          "return 1;"
1799          "\n}" > \n
1800          )))
1801
1802   (eval-after-load 'autoinsert
1803     '(define-auto-insert '("\\.c\\'" . "C skeleton")
1804        '(
1805          "Short description:"
1806          "/*\n"
1807          " * " (file-name-nondirectory (buffer-file-name)) "\n"
1808          " */" > \n \n
1809          "#include <stdio.h>" \n
1810          "#include \""
1811          (file-name-sans-extension
1812           (file-name-nondirectory (buffer-file-name)))
1813          ".h\"" \n \n
1814          "int main ()\n"
1815          "{" \n
1816          > _ \n
1817          "return 1;\n"
1818          "}" > \n
1819          )))
1820        
1821 #+END_SRC
1822
1823 *** Perl Auto Insert
1824
1825 Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
1826
1827 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1828
1829   (eval-after-load 'autoinsert
1830     '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
1831        '(
1832          "Description: "
1833          "#!/usr/bin/perl -w" \n
1834          \n
1835          "use strict;" \n \n
1836          )))
1837
1838 #+END_SRC
1839
1840 ** Completion
1841
1842 company mode
1843
1844 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1845
1846   (use-package company
1847     :ensure t
1848     :diminish company-mode
1849     :init (setq company-idle-delay 0.1)
1850     :config
1851     (global-company-mode))
1852
1853 #+END_SRC
1854
1855 [[https://github.com/company-mode/company-statistics][company-statistics]]
1856
1857 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1858
1859   (use-package company-statistics
1860     :ensure t
1861     :config
1862     (company-statistics-mode))
1863
1864 #+END_SRC
1865
1866 * Todo 
1867
1868 - change M-o to trigger to delete other windows and restore previous config
1869