add script of wifif config for raspberry pi
[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 ** Setting loading Path
11 Set system PATH and emacs exec path
12 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
13   (setenv "PATH" (concat (getenv "PATH")
14                          ":" "/usr/local/bin"
15                          ":" "/Library/TeX/texbin"))
16   (setq exec-path (append exec-path '("/usr/local/bin")))
17   (setq exec-path (append exec-path '("/Library/TeX/texbin/")))
18 #+END_SRC
19
20 ** Package Initialization
21 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
22   (require 'package)
23
24   (setq package-archives '(("mepla" . "http://melpa.milkbox.net/packages/")
25                            ("gnu" . "http://elpa.gnu.org/packages/")
26                            ("org" . "http://orgmode.org/elpa/")))
27
28   (package-initialize)
29 #+END_SRC       
30
31 ** My constant
32 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
33   (defconst my-name "Peng Li")
34   (defconst my-email "seudut@gmail.com")
35   (defconst MY-EMACS-DIR (concat (getenv "HOME") "/.emacs.d"))
36   (defconst MY-EMACS-TMP (concat MY-EMACS-DIR "/temp"))
37   (unless (file-exists-p MY-EMACS-TMP)
38     (mkdir MY-EMACS-TMP))
39 #+END_SRC
40
41 ** General Setting
42 *** scroll bar, tool-bar and menu-bar
43 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
44   (if (display-graphic-p)
45       (progn
46         (scroll-bar-mode 0)
47         (tool-bar-mode 0)
48         (menu-bar-mode 1))
49     (menu-bar-mode 0))
50
51   ;; (setq debug-on-error t)
52   (setq inhibit-startup-message t)
53
54   (defalias 'yes-or-no-p 'y-or-n-p)
55   (show-paren-mode 1)
56   ;; don't backupf
57   (setq make-backup-files nil)
58
59   ;;supress the redefined warning at startup
60   (setq ad-redefinition-action 'accept)
61
62   (setq scroll-step 1)
63   (setq scroll-margin 5)
64
65   (when (display-graphic-p)
66     (fringe-mode '(0 . 0)))
67
68   (blink-cursor-mode 0)
69   ;; (setq-default indicate-empty-lines t)
70   ;; (setq-default indicate-abuffer-boundaries 'right)
71 #+END_SRC
72
73 *** Custom file 
74 #+BEGIN_SRC emacs-lisp :tangle yes :results silent 
75   (setq custom-file "~/.emacs.d/custom.el")
76   (if (file-exists-p custom-file)
77       (load custom-file))
78 #+END_SRC
79
80 *** Switch the focus to help window when it appears
81 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
82   (setq help-window-select t)
83 #+END_SRC
84
85 *** Set default window size
86 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
87   (setq initial-frame-alist
88         '((width . 120)
89           (height . 50)))
90
91   ;; (setq-default indicate-empty-lines t)
92 #+END_SRC
93
94 *** Stop auto save
95 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
96   (setq auto-save-default nil)
97
98   ;; restore last session
99   ;; (desktop-save-mode t)
100 #+END_SRC
101
102 *** temp folder
103 Make a temp directory for all cache/history files
104 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
105   (defconst sd-temp-directory
106     (file-name-as-directory "~/.emacs.d/temp"))
107
108   (unless (file-exists-p sd-temp-directory)
109     (mkdir sd-temp-directory))
110 #+END_SRC
111
112 *** Save minibuffer history
113 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
114   (setq savehist-file (concat sd-temp-directory "history"))
115   (setq history-length 1000)
116   (setq savehist-additional-variables '(kill-ring search-ring regexp-search-ring))
117   (savehist-mode 1)
118
119   ;; set temp file path for recentf and auto-save
120   (setq recentf-save-file (concat sd-temp-directory "recentf"))
121   (setq recentf-max-saved-items 1000)
122   (setq auto-save-list-file-prefix (concat sd-temp-directory "auto-save-list/.saves-"))
123 #+END_SRC
124
125 *** Max file size
126 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
127   (setq large-file-warning-threshold nil)
128 #+END_SRC
129
130 *** Xterm mode
131 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
132   (unless (display-graphic-p)
133     (xterm-mouse-mode))
134 #+END_SRC
135
136 *** others
137 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
138   (setq vc-follow-symlinks t)
139 #+END_SRC
140 * Package Management Tools
141 ** Use-package
142 Using [[https://github.com/jwiegley/use-package][use-package]] to manage emacs packages
143 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
144   (unless (package-installed-p 'use-package)
145     (package-refresh-contents)
146     (package-install 'use-package))
147
148   (require 'use-package)
149 #+END_SRC
150
151 ** El-get
152 [[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. 
153 Check out [[http://tapoueh.org/emacs/el-get.html][el-get]].
154 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
155   (use-package el-get
156     :ensure t
157     :init
158     (add-to-list 'load-path "~/.emacs.d/el-get"))
159 #+END_SRC
160
161 ** paradox
162 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
163   (use-package paradox
164     :ensure t)
165 #+END_SRC
166
167 * Mode-line
168 ** clean mode line
169 clean mode line, Refer to [[https://www.masteringemacs.org/article/hiding-replacing-modeline-strings][Marstering Emacs]], some greek character see [[http://xahlee.info/math/math_unicode_greek.html][math_unicode_greek]]
170 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
171   (defvar mode-line-cleaner-alist
172     `((auto-complete-mode . " Ξ±")
173       (paredit-mode . " Ο€")
174       (eldoc-mode . "")
175       (abbrev-mode . "")
176       (projectile-mode . "")
177       (ivy-mode . "")
178       (undo-tree-mode . "")
179       ;; default is WK
180       (which-key-mode . "")
181       ;; default is SP
182       (smartparens-mode . "")
183       ;; default is LR
184       (linum-relative-mode . "")
185       ;; default is ARev
186       (auto-revert-mode . "")
187       ;; default is Ind
188       (org-indent-mode . "")
189       ;; default is  Fly
190       (flyspell-mode . "")
191       (irony-mode . "")
192       (page-break-lines-mode . "")
193       (yas-minor-mode . "y")
194       ;; default jj
195       (evil-escape-mode . "")
196       (auto-fill-mode . "")
197       ;; Major modes
198       (lisp-interaction-mode . "Ξ»")
199       (hi-lock-mode . "")
200       (python-mode . "Py")
201       (emacs-lisp-mode . "EL")
202       ;; (eshell-mode . "πž”")
203       ;; (dired-mode . "πž“")
204       ;; (ibuffer-mode . "πž‘")
205       ;; (org-mode . "𝞞")
206       (nxhtml-mode . "nx"))
207     "Alist for `clean-mode-line'.
208
209   When you add a new element to the alist, keep in mind that you
210   must pass the correct minor/major mode symbol and a string you
211   want to use in the modeline *in lieu of* the original.")
212
213
214   (defun clean-mode-line ()
215     (interactive)
216     (loop for cleaner in mode-line-cleaner-alist
217           do (let* ((mode (car cleaner))
218                    (mode-str (cdr cleaner))
219                    (old-mode-str (cdr (assq mode minor-mode-alist))))
220                (when old-mode-str
221                    (setcar old-mode-str mode-str))
222                  ;; major mode
223                (when (eq mode major-mode)
224                  (setq mode-name mode-str)))))
225
226
227   (add-hook 'after-change-major-mode-hook 'clean-mode-line)
228 #+END_SRC
229
230 ** Powerline mode
231 Install powerline mode [[https://github.com/milkypostman/powerline][powerline]]
232 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
233   (use-package powerline
234     :ensure t
235     :config)
236 #+END_SRC
237
238 Revised powerline-center-theme
239 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
240   (defun sd/powerline-simpler-vc (s)
241     (if s
242         (replace-regexp-in-string "Git[:-]" "" s)
243       s))
244
245   (defface sd/powerline-active1 '((t (:background "yellow" :foreground "black" :inherit mode-line)))
246     "My Powerline face 1 based on powerline-active1."
247     :group 'powerline)
248
249   (defface sd/buffer-modified-active1 '((t (:background "red" :foreground "black" :inherit mode-line)))
250     "My Powerline face 1 based on powerline-active1."
251     :group 'powerline)
252
253   (defface sd/buffer-view-active1 '((t (:background "green" :foreground "black" :inherit mode-line)))
254     "My Powerline face 1 based on powerline-active1."
255     :group 'powerline)
256
257   (defface sd/mode-line-buffer-id
258     '((t (:background "yellow" :foreground "black" :inherit mode-line-buffer-id)))
259     "My powerline mode-line face, based on mode-line-buffer-id"
260     :group 'powerline)
261
262   ;; Don't show buffer modified for scratch and eshell mode
263   (defun sd/buffer-is-eshel-or-scratch ()
264     "Dot not show modified indicator for buffers"
265     (interactive)
266     (unless (or (string-match "*scratch*" (buffer-name))
267                 (equal major-mode 'eshell-mode))
268       t))
269
270   (defun sd/powerline-center-theme_revised ()
271     "Setup a mode-line with major and minor modes centered."
272     (interactive)
273     (setq-default mode-line-format
274                   '("%e"
275                     (:eval
276                      (let* ((active (powerline-selected-window-active))
277                             (mode-line-buffer-id (if active 'sd/mode-line-buffer-id 'powerline-inactive1))
278                             (mode-line (if active 'mode-line 'mode-line-inactive))
279                             (my-face1 (if active 'sd/powerline-active1 'powerline-inactive1))
280                             (my-face-buffer-modified (if (and (sd/buffer-is-eshel-or-scratch) (buffer-modified-p) (not buffer-read-only)) 
281                                                          'sd/buffer-modified-active1
282                                                        (if buffer-read-only 'sd/buffer-view-active1
283                                                          my-face1)))
284                             (face1 (if active 'powerline-active1 'powerline-inactive1))
285                             (face2 (if active 'powerline-active2 'powerline-inactive2))
286                             (separator-left (intern (format "powerline-%s-%s"
287                                                             (powerline-current-separator)
288                                                             (car powerline-default-separator-dir))))
289                             (separator-right (intern (format "powerline-%s-%s"
290                                                              (powerline-current-separator)
291                                                              (cdr powerline-default-separator-dir))))
292                             (lhs (list (powerline-raw "%* " my-face-buffer-modified 'l)
293                                        (powerline-buffer-id mode-line-buffer-id 'l)
294                                        (powerline-raw " " my-face1)
295                                        (funcall separator-left my-face1 face1)
296                                        (powerline-narrow face1 'l)
297                                        (sd/powerline-simpler-vc (powerline-vc face1))))
298                             (rhs (list (powerline-raw global-mode-string face1 'r)
299                                        (powerline-raw "%4l" face1 'r)
300                                        (powerline-raw ":" face1)     
301                                        (powerline-raw "%3c" face1 'r)
302                                        (funcall separator-right face1 my-face1)
303                                        (powerline-raw (format-time-string " %I:%M %p  ") my-face1 'r)))
304                             (center (list (powerline-raw " " face1)
305                                           (funcall separator-left face1 face2)
306                                           (powerline-raw (when  (and (boundp 'evil-mode)  evil-mode) evil-mode-line-tag) face2)
307                                           (when (and (boundp 'erc-track-minor-mode) erc-track-minor-mode)
308                                             (powerline-raw erc-modified-channels-object face2 'l))
309                                           (powerline-major-mode face2 'l)
310                                           (powerline-process face2)
311                                           (powerline-raw " :" face2)
312                                           (powerline-minor-modes face2 'l)
313                                           (powerline-raw " " face2)
314                                           (funcall separator-right face2 face1))))
315                        (concat (powerline-render lhs)
316                                (powerline-fill-center face1 (/ (powerline-width center) 2.0))
317                                (powerline-render center)
318                                (powerline-fill face1 (powerline-width rhs))
319                                (powerline-render rhs)))))))
320
321   (setq ns-use-srgb-colorspace nil)       ;; Fix the issue in mode-line when showing triangle
322   (sd/powerline-center-theme_revised)
323 #+END_SRC
324
325 * Color and Theme
326 ** highlight setting
327
328 - hightligh current line
329 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
330     ;; (global-hl-line-mode)
331     ;; don't want high light current line in eshell/term mode
332     (add-hook 'prog-mode-hook 'hl-line-mode)
333     (add-hook 'text-mode-hook 'hl-line-mode)
334     (add-hook 'dired-mode-hook 'hl-line-mode)
335
336     ;; only highlight selected window
337     (setq hl-line-sticky-flag nil)
338
339     ;; (setq-default fill-column 120)
340     ;; (add-hook 'text-mode-hook 'turn-on-auto-fill)
341 #+END_SRC
342
343 - highlight searching text
344 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
345   (set-face-attribute 'lazy-highlight nil :background "yellow" :foreground "black" :weight 'bold)
346 #+END_SRC
347
348 ** color theme
349 Loading theme should be after all required loaded, refere [[https://github.com/jwiegley/use-package][:defer]] in =use-package=
350
351 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
352   (use-package molokai-theme
353     :ensure t)
354
355   (use-package color-theme
356     :ensure t
357     :init (require 'color-theme)
358     :config (use-package color-theme-sanityinc-tomorrow
359               :ensure t
360               :no-require t
361               :config
362               (load-theme 'molokai t)))
363 #+END_SRC
364
365 Change the Org-mode colors 
366
367 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
368   ;; (defun org-src-color-blocks-light ()
369   ;;   "Colors the block headers and footers to make them stand out more for lighter themes"
370   ;;   (interactive)
371   ;;   (custom-set-faces
372   ;;    '(org-block-begin-line
373   ;;      ((t (:underline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
374   ;;    '(org-block-background
375   ;;      ((t (:background "#FFFFEA"))))
376   ;;    '(org-block
377   ;;      ((t (:background "#FFFFEA"))))
378   ;;    '(org-block-end-line
379   ;;      ((t (:overline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
380
381   ;;    '(mode-line-buffer-id ((t (:foreground "#005000" :bold t))))
382   ;;    '(which-func ((t (:foreground "#008000"))))))
383
384   ;; (defun org-src-color-blocks-dark ()
385   ;;   "Colors the block headers and footers to make them stand out more for dark themes"
386   ;;   (interactive)
387   ;;   (custom-set-faces
388   ;;    '(org-block-begin-line
389   ;;      ((t (:foreground "#008ED1" :background "#002E41"))))
390   ;;    '(org-block-background
391   ;;      ((t (:background "#000000"))))
392   ;;    '(org-block
393   ;;      ((t (:background "#000000"))))
394   ;;    '(org-block-end-line
395   ;;      ((t (:foreground "#008ED1" :background "#002E41"))))
396
397   ;;    '(mode-line-buffer-id ((t (:foreground "black" :bold t))))
398   ;;    '(which-func ((t (:foreground "green"))))))
399
400   ;; (org-src-color-blocks-dark)
401
402 #+END_SRC
403
404 improve color for org-mode
405 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
406   ;; (deftheme ha/org-theme "Sub-theme to beautify org mode")
407
408   ;; (if window-system
409   ;;     (defvar sd/variable-font-tuple
410   ;;       (cond ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
411   ;;             ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
412   ;;             ((x-list-fonts "Verdana")         '(:font "Verdana"))
413   ;;             ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
414   ;;             (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro.")))
415   ;;       "My variable width font available to org-mode files and whatnot."))
416
417   ;; (defun sd/org-color ()
418   ;;   (let* ((sd/fixed-font-tuple (list :font sd/fixed-font-family))
419   ;;          (base-font-color     (face-foreground 'default nil 'default))
420   ;;          (background-color    (face-background 'default nil 'default))
421   ;;          (primary-color       (face-foreground 'mode-line nil))
422   ;;          (secondary-color     (face-background 'secondary-selection nil 'region))
423   ;;          (base-height         (face-attribute 'default :height))
424   ;;          (headline           `(:inherit default :weight bold :foreground ,base-font-color)))
425   ;;     (custom-theme-set-faces 'ha/org-theme
426   ;;                             `(org-agenda-structure ((t (:inherit default :height 2.0 :underline nil))))
427   ;;                             `(org-verbatim ((t (:inherit 'fixed-pitched :foreground "#aef"))))
428   ;;                             `(org-table ((t (:inherit 'fixed-pitched))))
429   ;;                             `(org-block ((t (:inherit 'fixed-pitched))))
430   ;;                             `(org-block-background ((t (:inherit 'fixed-pitched))))
431   ;;                             `(org-block-begin-line ((t (:inherit 'fixed-pitched))))
432   ;;                             `(org-block-end-line ((t (:inherit 'fixed-pitched))))
433   ;;                             `(org-level-8 ((t (,@headline ,@sd/variable-font-tuple))))
434   ;;                             `(org-level-7 ((t (,@headline ,@sd/variable-font-tuple))))
435   ;;                             `(org-level-6 ((t (,@headline ,@sd/variable-font-tuple))))
436   ;;                             `(org-level-5 ((t (,@headline ,@sd/variable-font-tuple))))
437   ;;                             `(org-level-4 ((t (,@headline ,@sd/variable-font-tuple
438   ;;                                                           :height ,(round (* 1.1 base-height))))))
439   ;;                             `(org-level-3 ((t (,@headline ,@sd/variable-font-tuple
440   ;;                                                           :height ,(round (* 1.25 base-height))))))
441   ;;                             `(org-level-2 ((t (,@headline ,@sd/variable-font-tuple
442   ;;                                                           :height ,(round (* 1.5 base-height))))))
443   ;;                             `(org-level-1 ((t (,@headline ,@sd/variable-font-tuple
444   ;;                                                           :height ,(round (* 1.75 base-height))))))
445   ;;                             `(org-document-title ((t (,@headline ,@sd/variable-font-tuple :height 1.5 :underline nil)))))))
446
447
448 #+END_SRC
449
450
451
452 ** Rainbow-delimiter
453 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
454   (use-package rainbow-delimiters
455     :ensure t
456     :init
457     (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))
458 #+END_SRC
459
460 ** page-break-lines
461 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
462   (use-package page-break-lines
463     :ensure t
464     :config
465     (global-page-break-lines-mode))
466 #+END_SRC
467
468 ** rainbow-mode
469
470 Enable rainbow mode in emacs lisp mode
471
472 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
473   (use-package rainbow-mode
474     :ensure t
475   ;  :init
476   ;  (add-hook emacs-lisp-mode-hook 'rainbow-mode)
477     )
478
479 #+END_SRC
480
481 ** cusor color
482 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
483   (if (display-graphic-p)
484       (set-cursor-color 'red))
485 #+END_SRC
486
487 * Font Settingh
488 ** font lock
489 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
490   (global-font-lock-mode 1)
491 #+END_SRC
492
493 ** fonts family
494 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
495   (defvar my-font-family
496     (cond ((x-list-fonts "Source Code Pro") "Source Code Pro"))
497     (cond ((x-list-fonts "HanziPen SC") "HanziPen SC"))
498     ;; (cond ((x-list-fonts "Source Code Pro") "Source Code Pro:weight=light"))
499     )
500
501   ;; (when (display-graphic-p)
502   ;;   (set-frame-font my-font-family)
503   ;;   (set-face-attribute 'default nil :font my-font-family :height 120)
504   ;;   (set-face-font 'default my-font-family))
505 #+END_SRC
506
507 ** Chinese fonts 
508
509 Fix the font alignment issue when both Chinese and English hybird in org-mode table. Refer [fn:1]
510 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
511     ;; (defvar emacs-english-font "Source Code Pro:weight=light" "The font name of English.")
512     (defvar emacs-english-font "Source Code Pro" "The font name of English.")
513
514     ;; (defvar emacs-cjk-font "STHeiti" "The font name for CJK.")
515     (defvar emacs-cjk-font "HanziPen SC" "The font name for CJK.")
516
517     (defvar emacs-font-size-pair-list
518       '((5 . 6) (10 . 12)
519         (11 . 14) (12 . 14)
520         (13 . 16) (14 . 16) (15 . 18) (16 . 20) (17 . 20)
521         (18 . 22) (19 . 22) (20 . 24) (21 . 26)
522         (24 . 28) (26 . 32) (28 . 34)
523         (30 . 36) (34 . 40) (36 . 44))
524       "This list is used to store matching (englis . chinese) font-size.")
525       ;; (defvar emacs-font-size-pair '(13 . 16) "Default font size pair for (english . chinese)")
526
527       ;; (defvar emacs-font-size-pair '(13 . 16) "Default font size pair for (english . chinese)")
528
529     (defvar emacs-font-size-pair (nth 3 emacs-font-size-pair-list) "Default font size pair for (english . chinese)")
530
531     (defun font-exist-p (fontname)
532       "Test if this font is exist or not."
533       (if (or (not fontname) (string= fontname ""))
534           nil
535         (if (not (x-list-fonts fontname)) nil t)))
536
537     (defun set-font (english chinese size-pair)
538       "Setup emacs English and Chinese font on x window-system."
539       (if (font-exist-p english)
540         (set-frame-font english (format "%s:pixelsize=%d" english (car size-pair)) t)
541         ;; (set-face-attribute 'default nil :font english :height (* 10 (car size-pair)))
542         
543         ;; (set-frame-font (format "%s:pixelsize=%d" english (car size-pair)) t)
544         )
545       (if (font-exist-p chinese)
546           (dolist (charset '(han cjk-misc) ;; '(kana han symbol cjk-misc bopomofo)
547                            )
548             (set-fontset-font (frame-parameter nil 'font) charset
549                               (font-spec :family chinese :size (cdr size-pair))))))
550
551     (defun emacs-step-font-size (step)
552       "Increase/Decrease emacs's font size."
553       (let ((scale-steps emacs-font-size-pair-list))
554         (if (< step 0) (setq scale-steps (reverse scale-steps)))
555         (setq emacs-font-size-pair
556               (or (cadr (member emacs-font-size-pair scale-steps))
557                   emacs-font-size-pair))
558         (when emacs-font-size-pair
559           (message "emacs font size set to %.1f" (car emacs-font-size-pair))
560           (set-font emacs-english-font emacs-cjk-font emacs-font-size-pair))))
561
562     (defun increase-emacs-font-size ()
563       "Decrease emacs's font-size acording emacs-font-size-pair-list."
564       (interactive) (emacs-step-font-size 1))
565
566     (defun decrease-emacs-font-size ()
567       "Increase emacs's font-size acording emacs-font-size-pair-list."
568       (interactive) (emacs-step-font-size -1))
569
570   ;  (set-font emacs-english-font emacs-cjk-font '(12 . 16))
571     ;; (set-font emacs-english-font emacs-cjk-font emacs-font-size-pair)
572 #+END_SRC
573
574 ** Org Mode font
575 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
576   (when (display-graphic-p)
577     (setq my-ch-font "STkaiti")
578     ;; (setq face-font-rescale-alist `(("HanziPen SC" . ,(/ 16.0 13))))
579     (setq face-font-rescale-alist `((,my-ch-font . ,(/ 16.0 13))))
580     ;; (set-face-attribute 'default nil :font "Source Code Pro-13")
581     (set-face-attribute 'default nil :font "Source Code Pro:weight=light" :height 130)
582     ;; (set-fontset-font t 'han      (font-spec :family "STkaiti"))
583     ;; (set-fontset-font t 'cjk-misc (font-spec :family "STkaiti"))
584     (dolist (charset '(han cjk-misc))
585       (set-fontset-font (frame-parameter nil 'font) charset
586                         ;; (font-spec :family "HanziPen SC")
587                         (font-spec :family my-ch-font))))
588 #+END_SRC
589
590 * IDO & SMEX
591 ** IDO
592 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
593   (use-package ido
594     :ensure t
595     :init (setq ido-enable-flex-matching t
596                 ido-ignore-extensions t
597                 ido-use-virtual-buffers t
598                 ido-use-faces nil
599                 ido-everywhere t)
600     (setq ido-save-directory-list-file (concat sd-temp-directory "ido.last"))
601     :config
602     (ido-mode 1)
603     (ido-everywhere 1)
604     (add-to-list 'completion-ignored-extensions ".pyc")
605
606
607     (define-key ido-buffer-completion-map (kbd "C-w") #'ido-delete-backward-word-updir)
608     (define-key ido-file-completion-map (kbd "C-w") #'ido-delete-backward-word-updir)
609     (define-key ido-file-dir-completion-map (kbd "C-w") #'ido-delete-backward-updir)
610     ;; (define-key ido-file-dir-completion-map (kbd "C-i") #'ido-copy-current-word)
611     )
612
613   (icomplete-mode t)
614 #+END_SRC
615
616 ** FLX
617 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
618   (use-package flx-ido
619     :ensure t
620     :config (flx-ido-mode nil))
621 #+END_SRC
622
623 ** IDO-vertically
624 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
625   (use-package ido-vertical-mode
626     :ensure t
627     :init
628     (setq ido-vertical-define-keys 'C-n-C-p-up-and-down)
629     :config
630     (ido-vertical-mode 1))
631 #+END_SRC
632
633 ** SMEX
634 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
635   (use-package smex
636     :ensure t
637     :init
638     (setq smex-save-file (concat sd-temp-directory "smex-items"))
639     (smex-initialize)
640     :bind
641     ("M-x" . smex)
642     ("M-X" . smex-major-mode-commands))
643 #+END_SRC
644
645 ** Ido-ubiquitous
646 Use [[https://github.com/DarwinAwardWinner/ido-ubiquitous][ido-ubiquitous]] for ido everywhere. It makes =describe-function= can also use ido
647 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
648   (use-package ido-ubiquitous
649     :ensure t
650     :init
651     (setq magit-completing-read-function 'magit-ido-completing-read)
652     (setq gnus-completing-read-function 'gnus-ido-completing-read)
653     :config
654     (ido-ubiquitous-mode 1))
655 #+END_SRC
656
657 ** Ido-exit-target
658 [[https://github.com/waymondo/ido-exit-target][ido-exit-target]] let you open file/buffer on =other-windows= when call =ido-switch-buffer=
659 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
660   (use-package ido-exit-target
661     :ensure t
662     :init
663     (mapcar #'(lambda (map)
664               (define-key map (kbd "C-j") #'ido-exit-target-other-window)
665               (define-key map (kbd "C-k") #'ido-exit-target-split-window-below))
666             (list ido-buffer-completion-map
667                   ;; ido-common-completion-map
668                   ido-file-completion-map
669                   ido-file-dir-completion-map)))
670 #+END_SRC
671
672 ** helm
673 let helm windows split inside current window
674 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
675   (with-eval-after-load 'helm
676     (setq helm-split-window-in-side-p t))
677 #+END_SRC
678
679 * Projectile
680 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
681   (use-package projectile
682     :ensure t
683     :init
684     (setq projectile-enable-caching t)
685     (setq projectile-switch-project-action 'projectile-dired)
686     (setq projectile-cache-file (concat sd-temp-directory "projectile.cache"))
687     (setq projectile-completion-system 'ivy)
688     :config
689     (add-to-list 'projectile-globally-ignored-files "GTAGS")
690     (projectile-global-mode t))
691
692   ;; https://emacs.stackexchange.com/questions/16497/how-to-exclude-files-from-projectile
693   (if (executable-find "rg")
694       (progn
695         (defconst modi/rg-arguments
696           `("--line-number"               ; line numbers
697             "--smart-case"
698             "--follow"                    ; follow symlinks
699             "--mmap")                     ; apply memory map optimization when possible
700           "Default rg arguments used in the functions in `projectile' package.")
701
702         (defun modi/advice-projectile-use-rg ()
703           "Always use `rg' for getting a list of all files in the project."
704           (mapconcat 'identity
705                      (append '("\\rg")    ; used unaliased version of `rg': \rg
706                              modi/rg-arguments
707                              '("--null"   ; output null separated results,
708                                "--files")) ; get file names matching the regex '' (all files)
709                      " "))
710
711         (advice-add 'projectile-get-ext-command :override #'modi/advice-projectile-use-rg))
712     (message "rg is not found"))
713 #+END_SRC
714
715 * Swiper & Ivy & Counsel
716 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
717   (use-package counsel
718     :ensure t
719     :defer t
720     :init
721     (use-package ivy :ensure t)
722     (ivy-mode 1)
723     (setq ivy-use-virtual-buffers nil
724           ivy-wrap t
725           ivy-count-format "%d/%d ")
726     (set-face-attribute 'ivy-current-match nil :background "Orange" :foreground "black")
727     (global-set-key (kbd "M-x") 'counsel-M-x)
728     ;; (global-set-key (kbd "C-h f") 'counsel-describe-function)
729     ;; (global-set-key (kbd "C-h v") 'counsel-describe-variable)
730     (global-set-key (kbd "C-x C-f") 'counsel-find-file)
731     (define-key read-expression-map (kbd "C-r") 'counsel-expression-history)
732     (global-set-key (kbd "C-c C-r") 'ivy-resume))
733 #+END_SRC
734
735 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
736   (ivy-set-actions
737    t
738    '(("q" (lambda (x) (ivy-quit-and-run (message "=="))) "exit")))
739
740   (ivy-set-actions
741    'projectile-switch-to-buffer
742    '(("j" ivy--switch-buffer-other-window-action "other window" )))
743
744   (defun sd/projectile-find-file-other-window-action (file)
745     (message "=========")
746     (message (projectile-expand-root file))
747     (find-file-other-window (projectile-expand-root file)))
748
749   (ivy-set-actions
750    'projectile-find-file
751    '(("j" sd/projectile-find-file-other-window-action "other-window")))
752
753   (defun sd/swith-to-buffer ()
754     "switch to buffer"
755     (interactive)
756     (if (projectile-project-p)
757         (projectile-switch-to-buffer)
758       (ivy-switch-buffer)))
759
760   (ivy-set-actions
761    'sd/swith-to-buffer
762    '(("j" ivy--switch-buffer-other-window-action "other window")))
763
764   (defun sd/exit-ivy-and-swith-to-buffer ()
765     "exit ivy complete, and call swith to buffer"
766     (interactive)
767     (ivy-quit-and-run
768      (ivy-switch-buffer)))
769
770   (defun my/ivy-read-action (key)
771     (let ((actions (ivy-state-action ivy-last)))
772       (if (null (ivy--actionp actions))
773           t
774         (let* ((action-idx (cl-position-if
775                             (lambda (x) (equal (car x) key))
776                             (cdr actions))))
777           (cond ((member key '("\e" "\a"))
778                  nil)
779                 ((null action-idx)
780                  (message "%s is not bound" key)
781                  nil)
782                 (t
783                  (message "")
784                  (setcar actions (1+ action-idx))
785                  (ivy-set-action actions)))))))
786
787   (with-eval-after-load "ivy"
788     (define-key ivy-minibuffer-map (kbd "C-o") 'ivy-dispatching-done)
789     (define-key ivy-minibuffer-map (kbd "C-k") (lambda () (interactive)
790                                                  (my/ivy-read-action "j")
791                                                  (ivy-done)))
792     (define-key ivy-minibuffer-map (kbd "M-o") #'sd/exit-ivy-and-swith-to-buffer))
793 #+END_SRC
794
795 stolen from [[https://github.com/mariolong/emacs.d/blob/f6a061594ef1b5d1f4750e9dad9dc97d6e122840/emacs-init.org][here]]
796 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
797   (use-package swiper
798     :ensure t
799     ;; :init
800     :config
801     ;; (ivy-mode)
802     (global-set-key (kbd "s-/") 'swiper)
803     (define-key swiper-map (kbd "M-r") 'swiper-query-replace)
804     (define-key swiper-map (kbd "C-.") (lambda ()
805                                          (interactive)
806                                          (insert (format "%s" (with-ivy-window (thing-at-point 'word))))))
807     (define-key swiper-map (kbd "M-.") (lambda ()
808                                          (interactive)
809                                          (insert (format "%s" (with-ivy-window (thing-at-point 'symbol)))))))
810
811 #+END_SRC
812 * Org-mode Settings
813 ** Org-mode Basic setting
814 Always indents header, and hide header leading starts so that no need type =#+STATUP: indent= 
815 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
816   (use-package org
817     :ensure t
818     :init
819     (setq org-startup-indented t)
820     (setq org-hide-leading-starts t)
821     (setq org-src-fontify-natively t)
822     (setq org-src-tab-acts-natively t)
823     (setq org-confirm-babel-evaluate nil)
824     (setq org-use-speed-commands t)
825     (setq org-completion-use-ido t)
826     (setq org-startup-with-inline-images t)
827     ;; latex preview
828     ;; (setq org-startup-with-latex-preview t)
829     ;; (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.2))
830     (require 'org-habit)
831     (add-to-list 'org-modules 'org-habit)
832     (setq org-habit-graph-column 50)
833     (setq org-hide-emphasis-markers t)
834     (setq org-html-validation-link nil)
835     ;; open link when return clicked
836     (setq org-return-follows-link t)
837     ;; open the ppt file by external open instead of emacs
838     (add-to-list 'org-file-apps '("\\.pptx" . "open %s"))
839     (add-to-list 'org-file-apps '("\\.doc" . "open %s"))
840     (setq org-image-actual-width nil))
841 #+END_SRC
842
843 ** Org theme
844 *** Fix the default theme - molokai
845 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
846   (with-eval-after-load "org"
847     (set-face-attribute 'org-block-begin-line nil :background nil :foreground nil :inherit 'shadow :slant 'italic)
848     (set-face-attribute 'org-block-end-line nil :background nil :foreground nil :inherit 'shadow :slant 'italic)
849     ;; (set-face-attribute 'org-table nil :family "Ubuntu Mono" :height (face-attribute 'default :height))
850     )
851 #+END_SRC
852 *** variable-pitch-mode and fixed-pitch-mode
853 [[https://yoo2080.wordpress.com/2013/05/30/monospace-font-in-tables-and-source-code-blocks-in-org-mode-proportional-font-in-other-parts/][monospace font in tables and source code blocks in org-mode, proportional font in other parts]]
854 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
855   ;(when (display-graphic-p)
856   ;  (unless (find-font (font-spec :name "Ubuntu Mono"))
857   ;    (warn "Font not found Ubuntu Mono"))
858   ;  (unless (find-font (font-spec :name "Source Code Pro"))
859   ;    (warn "Font not found Source Code Pro"))
860   ;  (unless (find-font (font-spec :name "Source Sans Pro"))
861   ;    (warn "Font not found Source Sans Pro")))
862
863   ;; (set-face-attribute 'variable-pitch nil :font "Source Sans Pro" :height 160)
864   ;; (set-face-attribute 'fixed-pitch nil :font "Source Code Pro" :height (face-attribute 'default :height))
865
866
867   ;; Install Ubuntu Mono fonts and apply it in org-table to align Chinese fonts
868   ;; (with-eval-after-load "org"
869   ;;   (mapc (lambda (face)
870   ;;           (set-face-attribute face nil :inherit 'fixed-pitch))
871   ;;         (list
872   ;;          'org-code
873   ;;          'org-block
874   ;;          'org-block-background
875   ;;          'org-block-begin-line
876   ;;          'org-block-end-line))
877   ;;   (set-face-attribute 'org-table nil :family "Ubuntu Mono" :height 100)
878   ;;   (set-face-attribute 'org-formula nil :family "Ubuntu Mono" :height 100)
879   ;;   ;; org-special-keyword inherited from font-lock-keywork originally; as org is changed to variable-pitch, it cause
880   ;;   ;; the font in special-keywords are not monospace
881   ;;   (set-face-attribute 'org-special-keyword nil :inherit '(font-lock-keyword-face fixed-pitch))
882   ;;   ;; same as above 
883   ;;   (set-face-attribute 'org-verbatim nil :inherit '(shadow fixed-pitch))
884   ;;   (set-face-attribute 'org-block-begin-line nil :inherit 'shadow :slant 'italic :foreground nil)
885   ;;   (set-face-attribute 'org-block-end-line nil :inherit 'shadow :slant 'italic :foreground nil)
886     
887   ;;   ;; fix indent broken by variable-pitch-mode
888   ;;   ;; http://emacs.stackexchange.com/questions/26864/variable-pitch-face-breaking-indentation-in-org-mode
889   ;;   (require 'org-indent)
890   ;;   (set-face-attribute 'org-indent nil :inherit '(org-hide fixed-pitch)))
891
892   ;; (with-eval-after-load "org"
893   ;;   (let ((default-font-size (- (face-attribute 'default :height) (if (display-graphic-p) 20 0))))
894   ;;     (set-face-attribute 'org-code nil :height default-font-size)
895   ;;     (set-face-attribute 'org-block nil :height default-font-size)
896   ;;     ;; (set-face-attribute 'org-block-begin-line nil :background nil :foreground nil :inherit 'shadow :slant 'italic :height default-font-size)
897   ;;     ;; (set-face-attribute 'org-block-end-line nil :background nil :foreground nil :inherit 'shadow :slant 'italic :height default-font-size)
898   ;;     (when (find-font (font-spec :name "Ubuntu Mono"))
899   ;;       (set-face-attribute 'org-table nil :family "Ubutu Mono" :height default-font-size)
900   ;;       (set-face-attribute 'org-formula nil :family "Ubutu Mono" :height default-font-size))))
901
902   ;; (add-hook 'org-mode-hook
903   ;;           (lambda ()
904   ;;             (let ((default-font-size (- (face-attribute 'default :height) (if (display-graphic-p) 20 0))))
905   ;;               (set-face-attribute 'org-code nil :height default-font-size)
906   ;;               (set-face-attribute 'org-block nil :height default-font-size)
907   ;;               ;; (set-face-attribute 'org-block-begin-line nil :background nil :foreground nil :inherit 'shadow :slant 'italic :height default-font-size)
908   ;;               ;; (set-face-attribute 'org-block-end-line nil :background nil :foreground nil :inherit 'shadow :slant 'italic :height default-font-size)
909   ;;               (when (find-font (font-spec :name "Ubuntu Mono"))
910   ;;                 (set-face-attribute 'org-table nil :family "Ubutu Mono" :height default-font-size)
911   ;;                 (set-face-attribute 'org-formula nil :family "Ubutu Mono" :height default-font-size)))))
912 #+END_SRC
913
914 Also correct the face of  =org-meta-line= in =org-table= 
915 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
916   ;; (with-eval-after-load "org"
917   ;;   (set-face-attribute 'org-meta-line nil :font "Source Code Pro" :height 120 :slant 'italic :inherit 'font-lock-comment-face))
918 #+END_SRC
919 *** 
920
921 *** Org-head face
922 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
923   ;; (with-eval-after-load "org"
924   ;;   (let* ((base-height (face-attribute 'variable-pitch :height))
925   ;;          (base-font-color (face-foreground 'default nil  'default)))
926   ;;     (set-face-attribute 'org-document-title nil :weight 'bold :height (+ 60 base-height))
927   ;;     (set-face-attribute 'org-level-1 nil :weight 'bold :height (+ 40 base-height))
928   ;;     (set-face-attribute 'org-level-2 nil :weight 'bold :height (+ 30 base-height))
929   ;;     (set-face-attribute 'org-level-3 nil :weight 'bold :height (+ 20 base-height))
930   ;;     (set-face-attribute 'org-level-4 nil :weight 'bold :height (+ 10 base-height))
931   ;;     (set-face-attribute 'org-level-5 nil :weight 'bold)
932   ;;     (set-face-attribute 'org-level-6 nil :weight 'bold)
933   ;;     (set-face-attribute 'org-level-7 nil :weight 'bold)
934   ;;     (set-face-attribute 'org-level-8 nil :weight 'bold)))
935 #+END_SRC
936
937
938 ** Org babel
939 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
940   (el-get-bundle hasu/emacs-ob-racket
941     :features ob-racket)
942
943   ;; Lua support
944 ;;  (use-package ob-lua
945 ;;    :ensure t)
946
947   ;; use current window for org source buffer editting
948
949   ;; (setq org-src-window-setup 'current-window)
950   (define-key org-mode-map (kbd "C-'") nil)
951   ;; C-M-i is mapped to imenu globally
952   (define-key org-mode-map (kbd "C-M-i") nil)
953   ;; set the ditta.jar path
954
955   (setq org-ditaa-jar-path "/usr/local/Cellar/ditaa/0.9/libexec/ditaa0_9.jar")
956   (unless 
957       (file-exists-p org-ditaa-jar-path)
958     (message "seudut: ditaa.jar not found at %s " org-ditaa-jar-path))
959
960   (org-babel-do-load-languages 'org-babel-load-languages
961                                '((python . t)
962                                  (C . t)
963                                  (perl . t)
964                                  (calc . t)
965                                  (latex . t)
966                                  (java . t)
967                                  (ruby . t)
968 ;                                 (lua . t)
969                                  (lisp . t)
970                                  (scheme . t)
971                                  (racket . t)
972                                  (sh . t)
973                                  (sqlite . t)
974                                  (js . t)
975                                  (gnuplot . t)
976                                  (ditaa . t)
977                                  (plantuml . t)))
978
979
980   ;; toggle image preview 
981   (add-hook 'org-babel-after-execute-hook 'sd/display-inline-images 'append)
982
983   (defun sd/display-inline-images ()
984     (condition-case nil
985         (org-display-inline-images)
986       (error nil)))
987
988   ;; ditaa artist mode
989   (with-eval-after-load "artist"
990     (define-key artist-mode-map [down-mouse-3] 'artist-mouse-choose-operation))
991 #+END_SRC
992 *** plantUML
993 [[http://eschulte.github.io/babel-dev/DONE-integrate-plantuml-support.html][DONE-integrate-plantuml-support]]
994 #+BEGIN_SRC sh
995   ## support plantuml
996   brew install plantuml
997 #+END_SRC
998
999 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1000   (use-package plantuml-mode
1001     :ensure t)
1002
1003   (setq org-plantuml-jar-path
1004         (expand-file-name "/usr/local/Cellar/plantuml/8041/plantuml.8041.jar"))
1005
1006   (set-variable 'plantuml-jar-path
1007                 (expand-file-name "/usr/local/Cellar/plantuml/8041/plantuml.8041.jar"))
1008 #+END_SRC
1009
1010 ** Org-bullets
1011 use [[https://github.com/sabof/org-bullets][org-bullets]] package to show utf-8 charactes
1012 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1013   (use-package org-bullets
1014     :ensure t
1015     :init
1016     (add-hook 'org-mode-hook (apply-partially #'org-bullets-mode t)))
1017
1018   (setq org-bullets-bullet-list '("β¦Ώ" "βœͺ" "β—‰" "β—‹" "β–Ί" "β—†"))
1019
1020   ;; increase font size when enter org-src-mode
1021   ;; (add-hook 'org-src-mode-hook (lambda () (text-scale-increase 2)))
1022
1023   ;; define a face for org-bullets
1024   (defface org-bullet-face
1025       '((t (:foreground "green yellow")))
1026   "Face used for the org-bullets.")
1027   (setq org-bullets-face-name (quote  org-bullet-face))
1028   ;; (set-face-attribute 'org-bullet-face t :foreground "burlywood" :weight 'normal :height 1.6)
1029 #+END_SRC
1030
1031 ** Worf Mode
1032 [[https://github.com/abo-abo/worf][worf]] mode is an extension of vi-like binding for org-mode. 
1033 In =worf-mode=, it is mapping =[=, =]= as =worf-backward= and =worf-forward= in global, wich
1034 cause we cannot input =[= and =]=, so here I unset this mappings. And redifined this two to
1035 =M-[= and =M-]=. see this [[https://github.com/abo-abo/worf/issues/19#issuecomment-223756599][issue]]
1036 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1037   (use-package worf
1038     :ensure t
1039     :commands worf-mode
1040     :init
1041     (require 'ivy)
1042     (add-hook 'org-mode-hook 'worf-mode))
1043 #+END_SRC
1044
1045 ** Get Things Done
1046 Refer to [[http://doc.norang.ca/org-mode.html][Organize Your Life in Plain Text]]
1047 *** basic setup
1048 standard key binding
1049 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1050   (global-set-key "\C-cl" 'org-store-link)
1051   (global-set-key "\C-ca" 'org-agenda)
1052   (global-set-key "\C-cb" 'org-iswitchb)
1053 #+END_SRC
1054
1055 *** Plain List 
1056 Replace the list bullet =-=, =+=,  with =β€’=, a litter change based [[https://github.com/howardabrams/dot-files/blob/master/emacs-org.org][here]]
1057 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1058   ;; (use-package org-mode
1059   ;;   :init
1060   ;;   (font-lock-add-keywords 'org-mode
1061   ;;    '(("^ *\\([-+]\\) "
1062   ;;           (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "β€’")))))))
1063 #+END_SRC
1064  
1065 *** Todo Keywords
1066 refer to [[http://coldnew.github.io/coldnew-emacs/#orgheadline94][fancy todo states]], 
1067 To track TODO state changes, the =!= is to insert a timetamp, =@= is to insert a note with
1068 timestamp for the state change.
1069 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1070     ;; (setq org-todo-keywords
1071     ;;        '((sequence "β˜› TODO(t)" "|" "βœ” DONE(d)")
1072     ;;          (sequence "βš‘ WAITING(w)" "|")
1073     ;;          (sequence "|" "✘ CANCELLED(c)")))
1074   ; (setq org-todo-keyword-faces
1075   ;        (quote ("TODO" .  (:foreground "red" :weight bold))
1076   ;               ("NEXT" .  (:foreground "blue" :weight bold))
1077   ;               ("WAITING" . (:foreground "forest green" :weight bold))
1078   ;               ("DONE" .  (:foreground "magenta" :weight bold))
1079   ;               ("CANCELLED" . (:foreground "forest green" :weight bold))))
1080
1081
1082   (setq org-todo-keywords
1083         (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d!)")
1084                 ;; (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING")
1085                 (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" ))))
1086
1087   (setq org-todo-keyword-faces
1088         (quote (("TODO" :foreground "red" :weight bold)
1089                 ("NEXT" :foreground "blue" :weight bold)
1090                 ("DONE" :foreground "forest green" :weight bold)
1091                 ("WAITING" :foreground "orange" :weight bold)
1092                 ("HOLD" :foreground "magenta" :weight bold)
1093                 ("CANCELLED" :foreground "forest green" :weight bold)
1094                 ;; ("MEETING" :foreground "forest green" :weight bold)
1095                 ;; ("PHONE" :foreground "forest green" :weight bold)
1096                 )))
1097 #+END_SRC
1098
1099 Fast todo selections
1100
1101 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1102   (setq org-use-fast-todo-selection t)
1103   (setq org-treat-S-cursor-todo-selection-as-state-change nil)
1104 #+END_SRC
1105
1106 TODO state triggers and tags, [[http://doc.norang.ca/org-mode.html][Organize Your Life in Plain Text]]
1107
1108 - Moving a task to =CANCELLED=, adds a =CANCELLED= tag
1109 - Moving a task to =WAITING=, adds a =WAITING= tag
1110 - Moving a task to =HOLD=, add =HOLD= tags
1111 - Moving a task to =DONE=, remove =WAITING=, =HOLD= tag
1112 - Moving a task to =NEXT=, remove all waiting/hold/cancelled tags
1113
1114 This tags are used to filter tasks in agenda views
1115 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1116   (setq org-todo-state-tags-triggers
1117         (quote (("CANCELLED" ("CANCELLED" . t))
1118                 ("WAITING" ("WAITING" . t))
1119                 ("HOLD" ("WAITING") ("HOLD" . t))
1120                 (done ("WAITING") ("HOLD"))
1121                 ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
1122                 ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
1123                 ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))
1124 #+END_SRC
1125
1126 Logging Stuff 
1127 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1128   ;; log time when task done
1129   ;; (setq org-log-done (quote time))
1130   ;; save clocking into to LOGBOOK
1131   (setq org-clock-into-drawer t)
1132   ;; save state change notes and time stamp into LOGBOOK drawer
1133   (setq org-log-into-drawer t)
1134   (setq org-clock-into-drawer "CLOCK")
1135 #+END_SRC
1136
1137 *** Tags
1138 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1139   (setq org-tag-alist (quote ((:startgroup)
1140                               ("@office" . ?e)
1141                               ("@home" . ?h)
1142                               (:endgroup)
1143                               ("WAITING" . ?w)
1144                               ("HOLD" . ?h)
1145                               ("CANCELLED" . ?c))))
1146
1147   ;; Allow setting single tags without the menu
1148   (setq org-fast-tag-selection-single-key (quote expert))
1149 #+END_SRC
1150
1151 *** Capture - Refile - Archive
1152
1153 Capture lets you quickly store notes with little interruption of your work flow.
1154
1155 **** Capture Templates
1156
1157 When a new taks needs to be added, categorize it as 
1158
1159 All captured file which need next actions are stored in =refile.org=, 
1160 - A new task / note (t) =refile.org=
1161 - A work task in office =office.org=
1162 - A jourenl =diary.org=
1163 - A new habit (h) =refile.org=
1164
1165 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1166   (setq org-directory "~/org")
1167   (setq org-default-notes-file "~/org/refile.org")
1168   (setq sd/org-diary-file "~/org/diary.org")
1169
1170   (global-set-key (kbd "C-c c") 'org-capture)
1171
1172   (setq org-capture-templates
1173         (quote (("t" "Todo" entry (file org-default-notes-file)
1174                  "* TODO %?\n:LOGBOOK:\n- Added: %U\t\tAt: %a\n:END:")
1175                 ("n" "Note" entry (file org-default-notes-file)
1176                  "* %? :NOTE:\n:LOGBOOK:\n- Added: %U\t\tAt: %a\n:END:")
1177                 ("j" "Journal" entry (file+datetree sd/org-diary-file)
1178                  "* %?\n:LOGBOOK:\n:END:" :clock-in t :clock-resume t)
1179                 ("h" "Habit" entry (file org-default-notes-file)
1180                  "* 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 "))))
1181 #+END_SRC
1182
1183 **** Refiling Tasks
1184
1185 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1186   (setq org-refile-targets (quote (;; (nil :maxlevel . 9)
1187                                    (org-agenda-files :maxlevel . 9))))
1188
1189   (setq org-refile-use-outline-path t)
1190
1191   (setq org-refile-allow-creating-parent-nodes (quote confirm))
1192 #+END_SRC
1193
1194 *** Agenda Setup
1195 Setting agenda files and the agenda view
1196 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1197   (setq org-agenda-files (quote ("~/org/gtd.org")))
1198
1199   ;; only show today's tasks in agenda view
1200   ;; (setq org-agenda-span 'day)
1201   ;; Use current windows for agenda view
1202   ;; (setq org-agenda-window-setup 'current-window)
1203
1204   ;; show all feature entries for repeating tasks,
1205   ;; this is already setting by default
1206   (setq org-agenda-repeating-timestamp-show-all t)
1207
1208   ;; Show all agenda dates - even if they are empty
1209   (setq org-agenda-show-all-dates t)
1210 #+END_SRC
1211
1212 ** Org mobile
1213 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1214   (with-eval-after-load "org"
1215     (require 'org-mobile)
1216     ;; Set to the location of your Org files on your local system
1217     ;; (setq org-directory "~/org")
1218     ;; Set to the name of the file where new notes will be stored
1219     (setq org-mobile-inbox-for-pull "~/org/flagged.org")
1220     ;; Set to <your Dropbox root directory>/MobileOrg.
1221     (setq org-mobile-directory "~/Dropbox/Apps/MobileOrg"))
1222 #+END_SRC
1223
1224 ** Export PDF
1225 Install MacTex-basic [[http://www.tug.org/mactex/morepackages.html][MacTex-basic]]  and some tex packages
1226 #+BEGIN_SRC sh 
1227   wget http://tug.org/cgi-bin/mactex-download/BasicTeX.pkg
1228
1229   sudo tlmgr update --self
1230
1231   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
1232 #+END_SRC
1233
1234 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1235   ;; ;; allow for export=>beamer by placing
1236
1237   ;; http://emacs-fu.blogspot.com/2011/04/nice-looking-pdfs-with-org-mode-and.html
1238   ;; #+LaTeX_CLASS: beamer in org files
1239   (unless (boundp 'org-export-latex-classes)
1240     (setq org-export-latex-classes nil))
1241   (add-to-list 'org-export-latex-classes
1242     ;; beamer class, for presentations
1243     '("beamer"
1244        "\\documentclass[11pt]{beamer}\n
1245         \\mode<{{{beamermode}}}>\n
1246         \\usetheme{{{{beamertheme}}}}\n
1247         \\usecolortheme{{{{beamercolortheme}}}}\n
1248         \\beamertemplateballitem\n
1249         \\setbeameroption{show notes}
1250         \\usepackage[utf8]{inputenc}\n
1251         \\usepackage[T1]{fontenc}\n
1252         \\usepackage{hyperref}\n
1253         \\usepackage{color}
1254         \\usepackage{listings}
1255         \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
1256     frame=single,
1257     basicstyle=\\small,
1258     showspaces=false,showstringspaces=false,
1259     showtabs=false,
1260     keywordstyle=\\color{blue}\\bfseries,
1261     commentstyle=\\color{red},
1262     }\n
1263         \\usepackage{verbatim}\n
1264         \\institute{{{{beamerinstitute}}}}\n          
1265          \\subject{{{{beamersubject}}}}\n"
1266
1267        ("\\section{%s}" . "\\section*{%s}")
1268  
1269        ("\\begin{frame}[fragile]\\frametitle{%s}"
1270          "\\end{frame}"
1271          "\\begin{frame}[fragile]\\frametitle{%s}"
1272          "\\end{frame}")))
1273
1274     ;; letter class, for formal letters
1275
1276     (add-to-list 'org-export-latex-classes
1277
1278     '("letter"
1279        "\\documentclass[11pt]{letter}\n
1280         \\usepackage[utf8]{inputenc}\n
1281         \\usepackage[T1]{fontenc}\n
1282         \\usepackage{color}"
1283  
1284        ("\\section{%s}" . "\\section*{%s}")
1285        ("\\subsection{%s}" . "\\subsection*{%s}")
1286        ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
1287        ("\\paragraph{%s}" . "\\paragraph*{%s}")
1288        ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
1289
1290
1291   (require 'ox-md)
1292   (require 'ox-beamer)
1293
1294   (setq org-latex-pdf-process
1295         '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
1296           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
1297           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
1298
1299   (setq TeX-parse-self t)
1300
1301   (setq TeX-PDF-mode t)
1302   (add-hook 'LaTeX-mode-hook
1303             (lambda ()
1304               (LaTeX-math-mode)
1305               (setq TeX-master t)))
1306
1307 #+END_SRC
1308
1309 ** Export Html
1310 Color higlight the source code block in exported html, [[http://stackoverflow.com/questions/24082430/org-mode-no-syntax-highlighting-in-exported-html-page][org-mode-no-syntax-highlighting-in-exported-html-page]]
1311 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1312   (use-package htmlize
1313     :ensure t)
1314 #+END_SRC
1315
1316 ** Org structure template
1317 extend org-mode's easy templates, refer to [[http://coldnew.github.io/coldnew-emacs/#orgheadline94][Extend org-modes' esay templates]]
1318 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1319   ;; β€˜s’     β€˜#+BEGIN_SRC ... #+END_SRC’
1320   ;; β€˜e’     β€˜#+BEGIN_EXAMPLE ... #+END_EXAMPLE’
1321   ;; β€˜q’     β€˜#+BEGIN_QUOTE ... #+END_QUOTE’
1322   ;; β€˜v’     β€˜#+BEGIN_VERSE ... #+END_VERSE’
1323   ;; β€˜c’     β€˜#+BEGIN_CENTER ... #+END_CENTER’
1324   ;; β€˜l’     β€˜#+BEGIN_LaTeX ... #+END_LaTeX’
1325   ;; β€˜L’     β€˜#+LaTeX:’
1326   ;; β€˜h’     β€˜#+BEGIN_HTML ... #+END_HTML’
1327   ;; β€˜H’     β€˜#+HTML:’
1328   ;; β€˜a’     β€˜#+BEGIN_ASCII ... #+END_ASCII’
1329   ;; β€˜A’     β€˜#+ASCII:’
1330   ;; β€˜i’     β€˜#+INDEX:’ line
1331   ;; β€˜I’     β€˜#+INCLUDE:’ line
1332
1333
1334   (add-to-list 'org-structure-template-alist
1335                '("E" "#+BEGIN_SRC emacs-lisp :tangle yes :results silent\n?\n#+END_SRC"))
1336   (add-to-list 'org-structure-template-alist
1337                '("R" "#+BEGIN_SRC racket :tangle no :results output replace\n?\n#+END_SRC"))
1338   (add-to-list 'org-structure-template-alist
1339                '("S" "#+BEGIN_SRC sh :results output replace\n?\n#+END_SRC"))
1340   (add-to-list 'org-structure-template-alist
1341                '("p" "#+BEGIN_SRC plantuml :file uml.png \n?\n#+END_SRC"))
1342   (add-to-list 'org-structure-template-alist
1343                '("P" "#+BEGIN_SRC perl \n?\n#+END_SRC"))
1344   (add-to-list 'org-structure-template-alist
1345                '("f" "#+BEGIN_SRC fundamental :tangle ?\n\n#+END_SRC"))
1346   (add-to-list 'org-structure-template-alist
1347                '("C" "#+BEGIN_SRC c :tangle ?\n\n#+END_SRC"))
1348   (add-to-list 'org-structure-template-alist
1349                '("m" "\\begin{equation}\n?\n\\end{equation}"))
1350   (add-to-list 'org-structure-template-alist
1351                '("b" "#+STARTUP: showall
1352   ,#+STARTUP: inlineimages
1353   ,#+OPTIONS: toc:nil\n"))
1354   (add-to-list 'org-structure-template-alist
1355                '("d" "#+BEGIN_SRC ditaa :file ?  :cmdline -r -s 0.8 :cache yes \n\n#+END_SRC"))
1356   (add-to-list 'org-structure-template-alist
1357                '("u" "#+BEGIN_SRC plantuml :file ? \n\n#+END_SRC"))
1358
1359 #+END_SRC
1360
1361 ** Org Blog
1362 Fetch dependencies file, which is not in this repository.
1363 #+BEGIN_SRC perl :results silent :tangle yes
1364   print `curl https://raw.githubusercontent.com/seudut/blog/master/my-publish.el -o ./elisp/my-publish.el`;
1365 #+END_SRC
1366
1367 Load 
1368 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1369   (add-to-list 'load-path "~/.emacs.d/elisp")
1370
1371   (when (file-exists-p "~/.emacs.d/elisp/my-publish.el")
1372     (require 'my-publish)
1373     (blog-setup-project-alist "~/Private/blog/"))
1374 #+END_SRC
1375
1376
1377 Refer to [[http://orgmode.org/worg/org-tutorials/org-publish-html-tutorial.html][org-publish-html-tutorial]], and [[https://ogbe.net/blog/blogging_with_org.html][blogging_with_org]]
1378
1379 * Magit
1380 [[https://github.com/magit/magit][Magit]] is a very cool git interface on Emacs.
1381 and Defined keys, using vi keybindings, Refer abo-abo's setting [[https://github.com/abo-abo/oremacs/blob/c5cafdcebc88afe9e73cc8bd40c49b70675509c7/modes/ora-nextmagit.el][here]]
1382 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1383   (use-package magit
1384     :ensure t
1385     :init
1386     ;; don't ask me to confirm the unsaved change 
1387     (setq magit-save-repository-buffers nil)
1388     ;; default is 50
1389     (setq git-commit-summary-max-length 100)
1390     :commands magit-status magit-blame
1391     :config
1392     (dolist (map (list magit-status-mode-map
1393                        magit-log-mode-map
1394                        magit-diff-mode-map
1395                        magit-staged-section-map))
1396       (define-key map "j" 'magit-section-forward)
1397       (define-key map "k" 'magit-section-backward)
1398       (define-key map "D" 'magit-discard)
1399       (define-key map "O" 'magit-discard-file)
1400       (define-key map "n" nil)
1401       (define-key map "p" nil)
1402       (define-key map "v" 'recenter-top-bottom)
1403       (define-key map "i" 'magit-section-toggle))
1404     (define-key magit-mode-map "q" (lambda () (interactive)
1405                                      (magit-mode-bury-buffer t))))
1406 #+END_SRC
1407
1408 * Eshell
1409 ** Eshell alias
1410 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1411   (defalias 'e 'find-file)
1412   (defalias 'ff 'find-file-other-window)
1413   (defalias 'ee 'find-files)
1414 #+END_SRC
1415
1416 ** eshell temp directory
1417 set default eshell history folder
1418 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1419   (setq eshell-directory-name (concat  sd-temp-directory "eshell"))
1420 #+END_SRC
1421
1422 ** Eshell erase buffer
1423 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1424   (require 'utilities)
1425
1426   (defun sd/eshell-hook ()
1427     (interactive)
1428     (define-key eshell-mode-map (kbd "C-j") #'eshell-send-input)
1429     (define-key eshell-mode-map (kbd "C-l") #'sd/eshell-clear-buffer))
1430
1431   (add-hook 'eshell-mode-hook #'sd/eshell-hook)
1432 #+END_SRC
1433
1434 ** Toggle Eshell
1435 Toggle an eshell in split window below, refer [[http://www.howardism.org/Technical/Emacs/eshell-fun.html][eshell-here]]
1436 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1437   (defun sd/window-has-eshell ()
1438     "Check if current windows list has a eshell buffer, and return the window"
1439     (interactive)
1440     (let ((ret nil))
1441       (walk-windows (lambda (window)
1442                       (if (equal (with-current-buffer (window-buffer window) major-mode)
1443                                  'eshell-mode)
1444                           (setq ret window)))
1445                     nil nil)
1446       ret))
1447
1448   (defun sd/toggle-project-eshell ()
1449     "Toggle a eshell buffer vertically"
1450     (interactive)
1451     (if (sd/window-has-eshell)
1452         (if (equal major-mode 'eshell-mode)
1453             (progn
1454               (if (equal (length (window-list)) 1)
1455                   (mode-line-other-buffer)
1456                 (delete-window)))
1457           (select-window (sd/window-has-eshell)))
1458       (progn
1459         (split-window-vertically (- (/ (window-total-height) 3)))
1460         (other-window 1)
1461         (if (projectile-project-p)
1462             (projectile-run-eshell)
1463           (eshell)))))
1464
1465   (global-set-key (kbd "s-e") 'sd/toggle-project-eshell)
1466 #+END_SRC
1467
1468 ** exec-path-from-shell
1469 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1470   (use-package exec-path-from-shell
1471     :ensure t
1472     :init
1473     (setq exec-path-from-shell-check-startup-files nil)
1474     :config
1475     (exec-path-from-shell-initialize))
1476 #+END_SRC
1477
1478 * Misc Settings
1479 ** [[https://github.com/abo-abo/hydra][Hydra]]
1480 *** hydra install
1481 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1482   (use-package hydra
1483     :ensure t
1484     ;; disable new line in minibuffer when hint hydra
1485     :init
1486     (setq hydra-lv nil))
1487 #+END_SRC
1488
1489 *** Windmove Splitter
1490
1491 Refer [[https://github.com/abo-abo/hydra/blob/master/hydra-examples.el][hydra-example]], to enlarge or shrink the windows splitter
1492
1493 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1494
1495   (defun hydra-move-splitter-left (arg)
1496     "Move window splitter left."
1497     (interactive "p")
1498     (if (let ((windmove-wrap-around))
1499           (windmove-find-other-window 'right))
1500         (shrink-window-horizontally arg)
1501       (enlarge-window-horizontally arg)))
1502
1503   (defun hydra-move-splitter-right (arg)
1504     "Move window splitter right."
1505     (interactive "p")
1506     (if (let ((windmove-wrap-around))
1507           (windmove-find-other-window 'right))
1508         (enlarge-window-horizontally arg)
1509       (shrink-window-horizontally arg)))
1510
1511   (defun hydra-move-splitter-up (arg)
1512     "Move window splitter up."
1513     (interactive "p")
1514     (if (let ((windmove-wrap-around))
1515           (windmove-find-other-window 'up))
1516         (enlarge-window arg)
1517       (shrink-window arg)))
1518
1519   (defun hydra-move-splitter-down (arg)
1520     "Move window splitter down."
1521     (interactive "p")
1522     (if (let ((windmove-wrap-around))
1523           (windmove-find-other-window 'up))
1524         (shrink-window arg)
1525       (enlarge-window arg)))
1526
1527 #+END_SRC
1528
1529 *** hydra misc
1530 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1531   (defhydra sd/hydra-misc (:color red :columns nil)
1532     "Misc"
1533     ("e" eshell "eshell" :exit t)
1534     ("p" (lambda ()
1535            (interactive)
1536            (if (not (eq nil (get-buffer "*Packages*")))
1537                (switch-to-buffer "*Packages*")
1538              (package-list-packages)))
1539      "list-package" :exit t)
1540     ("g" magit-status "git-status" :exit t)
1541     ("'" mode-line-other-buffer "last buffer" :exit t)
1542     ("C-'" mode-line-other-buffer "last buffer" :exit t)
1543     ("m" man "man" :exit t)
1544     ("d" dired-jump "dired" :exit t)
1545     ("b" ibuffer "ibuffer" :exit t)
1546     ("q" nil "quit")
1547     ("f" nil "quit"))
1548
1549   ;; (global-set-key (kbd "C-'") 'sd/hydra-misc/body)
1550
1551   (defun sd/exchange-win-layout ()
1552     "Change the windos layout."
1553     (interactive)
1554     (when (equal (length (window-list)) 2)
1555       (let ((current-layout (if (or (window-in-direction 'right) (window-in-direction 'left))
1556                                 'v
1557                               'h))
1558             (other-buf (window-buffer (or (window-in-direction 'right)
1559                                           (window-in-direction 'left)
1560                                           (window-in-direction 'below)
1561                                           (window-in-direction 'above)))))
1562         (delete-other-windows)
1563         (if (eq current-layout 'v)
1564             (split-window-below)
1565           (split-window-right))
1566         (other-window 1)
1567         (switch-to-buffer other-buf))))
1568
1569   (defhydra sd/hydra-window-layout (:color red :colums nil)
1570     "Window Layout"
1571     ("m" sd/toggle-max-windows "Max-win" :exit t)
1572     ("s" ace-swap-window "Swap" :exit t)
1573     ("d" ace-delete-window "Delete"  :exit t)
1574     ("x" sd/exchange-win-layout "eXchange"  :exit t)
1575     ("u" winner-undo "window-Undo"  :exit t)
1576     ("r" winner-redo "window-Redo"  :exit t)
1577     ("C-h" (lambda () (interactive) (evil-window-increase-width 3)) "<<")
1578     ("C-l" (lambda () (interactive) (evil-window-decrease-width 3)) ">>")
1579     ("C-k" (lambda () (interactive) (evil-window-increase-height 3)) "^")
1580     ("C-j" (lambda () (interactive) (evil-window-decrease-height 3)) "v")
1581     ("=" balance-windows "=" :exit t)
1582     ("q" nil "quit"))
1583
1584   (defhydra sd/hydra-gtags (:color red :colums nil)
1585     "ggtags - global"
1586     ("d" sd/ggtags-find-definition "Definition" :exit t)
1587     ("r" ggtags-find-reference "Reference" :exit t)
1588     ("s" sd/ggtags-find-symbol "Symbol" :exit t))
1589
1590   (defhydra sd/hydra-projectile (:color red :column nil)
1591     "Projectile: "
1592     ("s" projectile-switch-open-project "Switch" :exit t)
1593     ("f" projectile-find-file "File" :exit t)
1594     ("b" projectile-switch-to-buffer "Buffer" :exit t)
1595     ("p" projectile-switch-project "Project" :exit t))
1596 #+END_SRC
1597
1598 *** hydra launcher
1599 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1600   (defhydra sd/hydra-launcher (:color blue :columns 2)
1601     "Launch"
1602     ("e" emms "emms" :exit t)
1603     ("q" nil "cancel"))
1604 #+END_SRC
1605
1606 ** Line Number
1607 Enable linum mode on programming modes
1608 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1609   (add-hook 'prog-mode-hook 'linum-mode)
1610 #+END_SRC
1611
1612 Fix the font size of line number
1613 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1614   (defun fix-linum-size ()
1615     (interactive)
1616     (set-face-attribute 'linum nil :height 110))
1617
1618   (add-hook 'linum-mode-hook 'fix-linum-size)
1619 #+END_SRC
1620
1621 I like [[https://github.com/coldnew/linum-relative][linum-relative]], just like the =set relativenumber= on =vim=
1622 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1623   (use-package linum-relative
1624     :ensure t
1625     :init
1626     (setq linum-relative-current-symbol "")
1627     :config
1628     (defun linum-new-mode ()
1629       "If line numbers aren't displayed, then display them.
1630   Otherwise, toggle between absolute and relative numbers."
1631       (interactive)
1632       (if linum-mode
1633           (linum-relative-toggle)
1634         (linum-mode 1)))
1635
1636     :bind
1637     ("A-k" . linum-new-mode))
1638
1639   ;; auto enable linum-new-mode in programming modes
1640   (add-hook 'prog-mode-hook 'linum-relative-mode)
1641 #+END_SRC
1642
1643 ** Save File Position
1644 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1645   (require 'saveplace)
1646   (setq save-place-file "~/.emacs.d/temp/saveplace")
1647   (setq-default save-place t)
1648   (setq save-place-forget-unreadable-files t)
1649   (setq save-place-skip-check-regexp "\\`/\\(?:cdrom\\|floppy\\|mnt\\|/[0-9]\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)")
1650 #+END_SRC
1651
1652 ** Multi-term
1653 define =multi-term= mapping to disable some mapping which is used globally.
1654 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1655   (use-package multi-term
1656     :ensure t)
1657
1658   (defun sd/term-mode-mapping ()
1659     (mapcar #'(lambda (map)
1660               (define-key map (kbd "C-o") nil)
1661               (define-key map (kbd "C-g") nil))
1662             (list term-mode-map
1663                   term-raw-map)))
1664
1665   (with-eval-after-load 'multi-term
1666     (sd/term-mode-mapping))
1667 #+END_SRC
1668
1669 ** ace-link
1670 [[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
1671 Type =o= to go to the link
1672 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1673   (use-package ace-link
1674     :ensure t
1675     :init
1676     (ace-link-setup-default))
1677 #+END_SRC
1678
1679 ** Ace-Windows
1680 [[https://github.com/abo-abo/ace-window][ace-window]] 
1681 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1682   (use-package ace-window
1683     :ensure t
1684     :defer t
1685                                           ;  :init
1686                                           ;  (global-set-key (kbd "M-o") 'ace-window)
1687     :config
1688     (setq aw-keys '(?a ?s ?d ?f ?j ?k ?l)))
1689 #+END_SRC
1690
1691 ** View only for some directory
1692 When see function by =C-h f=, and visit the source code, I would like the buffer is read only. See [[http://emacs.stackexchange.com/questions/3676/how-to-enter-view-only-mode-when-browsing-emacs-source-code-from-help/3681#3681][here]]
1693 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1694   (dir-locals-set-class-variables
1695    'emacs
1696    '((nil . ((buffer-read-only . t)
1697              (show-trailing-whitespace . nil)
1698              (tab-width . 8)
1699              (eval . (whitespace-mode -1))
1700              ;; (eval . (when buffer-file-name
1701              ;;           (setq-local view-no-disable-on-exit t)
1702              ;;           (view-mode-enter)))
1703              (projectile-enable-caching . nil)
1704              ))))
1705
1706   ;; (dir-locals-set-directory-class (expand-file-name "/usr/local/share/emacs") 'emacs)
1707   (dir-locals-set-directory-class "/usr/local/Cellar/emacs" 'emacs)
1708   ;; (dir-locals-set-directory-class "~/.emacs.d/elpa" 'emacs)
1709   (dir-locals-set-directory-class "~/dotfiles/emacs.d/elpa" 'emacs)
1710   (dir-locals-set-directory-class "~/dotfiles/emacs.d/el-get" 'emacs)
1711
1712   ;; temp-mode.el
1713   ;; Temporary minor mode
1714   ;; Main use is to enable it only in specific buffers to achieve the goal of
1715   ;; buffer-specific keymaps
1716
1717   ;; (defvar sd/temp-mode-map (make-sparse-keymap)
1718   ;;   "Keymap while temp-mode is active.")
1719
1720   ;; ;;;###autoload
1721   ;; (define-minor-mode sd/temp-mode
1722   ;;   "A temporary minor mode to be activated only specific to a buffer."
1723   ;;   nil
1724   ;;   :lighter " Temp"
1725   ;;   sd/temp-mode-map)
1726
1727   ;; (defun sd/temp-hook ()
1728   ;;   (if sd/temp-mode
1729   ;;       (progn
1730   ;;      (define-key sd/temp-mode-map (kbd "q") 'quit-window))))
1731
1732   ;; (add-hook 'lispy-mode-hook (lambda ()
1733   ;;                           (sd/temp-hook)))
1734 #+END_SRC
1735
1736 ** Info plus
1737 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1738   (el-get-bundle info+
1739     :url "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/info+.el"
1740     ;; (require 'info+)
1741     )
1742
1743   (with-eval-after-load 'info
1744     (require 'info+))
1745 #+END_SRC
1746
1747 ** advice info
1748 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1749   (defun sd/info-mode ()
1750     (interactive)
1751     (unless (equal major-mode 'Info-mode)
1752       (unless (> (length (window-list)) 1)
1753         (split-window-right))
1754       (other-window 1)))
1755
1756   ;; open Info buffer in other window instead of current window
1757   (defadvice info (before my-info (&optional file buf) activate)
1758     (sd/info-mode))
1759
1760   (defadvice Info-exit (after my-info-exit activate)
1761     (sd/delete-current-window))
1762 #+END_SRC
1763
1764 ** Presentation
1765 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1766   (use-package org-tree-slide
1767     :ensure
1768     :config
1769     ;; (define-key org-mode-map "\C-ccp" 'org-tree-slide-mode)
1770     (define-key org-tree-slide-mode-map (kbd "<ESC>") 'org-tree-slide-content)
1771     (define-key org-tree-slide-mode-map (kbd "<SPACE>") 'org-tree-slide-move-next-tree)
1772     (define-key org-tree-slide-mode-map [escape] 'org-tree-slide-move-previous-tree))
1773 #+END_SRC
1774
1775 ** pdf-tools
1776 #+BEGIN_SRC sh
1777   #brew install poppler
1778 #+END_SRC
1779
1780 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1781   ;(use-package pdf-tools
1782   ;  :ensure t
1783   ;  :init
1784   ;  ;; run to complete the installation
1785   ;  (pdf-tools-install)
1786   ;  :config
1787   ;  (add-to-list 'auto-mode-alist '("\.pdf$" . pdf-view-mode))
1788   ;  (add-hook 'pdf-outline-buffer-mode-hook #'sd/pdf-outline-map))
1789
1790   ;(defun sd/pdf-outline-map ()
1791   ;  "My keybindings in pdf-outline-map"
1792   ;  (interactive)
1793   ;  (define-key pdf-outline-buffer-mode-map (kbd "C-o") nil)
1794   ;  (define-key pdf-outline-buffer-mode-map (kbd "i") 'outline-toggle-children)
1795   ;  (define-key pdf-outline-buffer-mode-map (kbd "j") 'next-line)
1796    ; (define-key pdf-outline-buffer-mode-map (kbd "k") 'previous-line))
1797 #+END_SRC
1798
1799 ** help-mode
1800 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1801   (defun sd/help-mode-hook ()
1802     "Mapping for help mode"
1803     (define-key help-mode-map "j" 'next-line)
1804     (define-key help-mode-map "k" 'previous-line)
1805     (define-key help-mode-map "h" 'forward-char)
1806     (define-key help-mode-map "l" 'forward-char)
1807     (define-key help-mode-map "H" 'describe-mode)
1808     (define-key help-mode-map "v" 'recenter-top-bottom)
1809     (define-key help-mode-map "i" 'forward-button)
1810     (define-key help-mode-map "I" 'backward-button)
1811     (define-key help-mode-map "o" 'ace-link-help))
1812
1813   (add-hook 'help-mode-hook 'sd/help-mode-hook)
1814 #+END_SRC
1815
1816 Wiki [[http://stackoverflow.com/questions/3480173/show-keys-in-emacs-keymap-value][show-keys-in-emacs-keymap-value]], Helpplus mode can show keymap as human-readable, use ~describe-keymap~ or =C-h M-k=
1817
1818 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1819   (el-get-bundle help-macro+
1820     :url "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/help-macro+.el"
1821     :features help-macro+)
1822   (el-get-bundle help+
1823     :url "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/help+.el"
1824     :features help+)
1825   (el-get-bundle help-fns+
1826     :url "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/help-fns+.el"
1827     :features help-fns+)
1828   (el-get-bundle help-mode+
1829     :url "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/help-mode+.el"
1830     :features help-mode+)
1831 #+END_SRC
1832
1833 ** Ag
1834 install =ag=, =the-silver-searcher= by homebrew on mac
1835 #+BEGIN_SRC sh
1836 brew install the-silver-searcher
1837 #+END_SRC
1838
1839 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1840   (use-package ag
1841     :ensure t)
1842 #+END_SRC
1843
1844 ** Local Variable hooks
1845 [[https://www.emacswiki.org/emacs/LocalVariables][LocalVariables]], use =hack-local-variables-hook=, run a hook to set local variable in mode hook
1846 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1847   ;; make Emacs run a new "local variables hook" for each major mode
1848   (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
1849
1850   (defun run-local-vars-mode-hook ()
1851     "Run a hook for the major-mode after the local variables have been processed."
1852     (run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))
1853
1854   ;;   (add-hook 'c++-mode-local-vars-hook #'sd/c++-mode-local-vars)
1855 #+END_SRC
1856
1857 ** Table
1858 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1859   (add-hook 'text-mode-hook 'table-recognize)
1860 #+END_SRC
1861
1862 ** url-download
1863 To download file in =elisp=, best is =url-copy-file=, here refer [[http://stackoverflow.com/questions/4448055/download-a-file-with-emacs-lisp][download-a-file-with-emacs-lisp]] using =url-retrieve-synchronously= wrapping
1864 as a http download client tool
1865 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1866   (defun sd/download-file (&optional url download-dir download-name)
1867     (interactive)
1868     (let ((url (or url
1869                    (read-string "Enter download URL: ")))
1870           (download-dir (read-directory-name "Save to (~/Downloads): " "~/Downloads" "~/Downloads" 'confirm' nil)))
1871       (let ((download-buffer (url-retrieve-synchronously url)))
1872         (save-excursion
1873           (set-buffer download-buffer)
1874           ;; we may have to trim the http response
1875           (goto-char (point-min))
1876           (re-search-forward "^$" nil 'move)
1877           (forward-char)
1878           (delete-region (point-min) (point))
1879           (write-file (concat (or download-dir
1880                                   "~/Downloads/")
1881                               (or download-name
1882                                   (car (last (split-string url "/" t))))))))))
1883 #+END_SRC
1884
1885 * Dired
1886 ** Dired basic
1887 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1888   (setq dired-dwim-target t)
1889
1890   (use-package dired-details
1891     :ensure t
1892     :config
1893     (setq-default dired-details-hidden-string "--- ")
1894     (dired-details-install))
1895 #+END_SRC
1896
1897 ** Dired functions
1898 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1899   (defun sd/dired-next-line (count)
1900     "Move to next line, and always focus on the file name."
1901     (interactive "p")
1902     (dired-next-line count)
1903     (dired-move-to-filename))
1904
1905   (defun sd/dired-previous-line (count)
1906     "Move to previous line, and always focus on the file name."
1907     (interactive "p")
1908     (dired-previous-line count)
1909     (dired-move-to-filename))
1910
1911   (defun sd/dired-up-directory ()
1912     "Go to up directory"
1913     (interactive)
1914     (let ((old (current-buffer)))
1915       (dired-up-directory)
1916       (kill-buffer old)))
1917 #+END_SRC
1918
1919 ** Dired bindings
1920 =C-o= is defined as a global key for window operation, here unset it in dired mode
1921 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1922   (defun sd/dired-key-map ()
1923     "My keybindings for dired"
1924     (interactive)
1925     ;; these two prefix are used globally
1926     (define-key dired-mode-map (kbd "C-o") nil)
1927     (define-key dired-mode-map (kbd "M-s") nil)
1928     ;; toggle hidden files
1929     (define-key dired-mode-map (kbd "H") 'dired-omit-mode)
1930     ;; scroll 
1931     (define-key dired-mode-map (kbd "SPC") 'scroll-up-command)
1932     (define-key dired-mode-map (kbd "DEL") 'scroll-down-command)
1933     (define-key dired-mode-map (kbd "j") 'diredp-next-line)
1934     (define-key dired-mode-map (kbd "k") 'diredp-previous-line)
1935     (define-key dired-mode-map (kbd "g") 'dired-goto-file)
1936     ;; (define-key dired-mode-map (kbd "S-SPC") 'scroll-down-command)
1937     ;; jump to fil/dirs
1938     (define-key dired-mode-map (kbd "f") 'dired-isearch-filenames)
1939     ;; subdir
1940     ;; i dired-maybe-insert-subdir
1941     ;; o dired-find-file-other-window (switch to other window)
1942     ;; O dired-display-file
1943     (define-key dired-mode-map (kbd "G") 'ido-dired)
1944     (define-key dired-mode-map (kbd "c") 'sd/dired-new-file)
1945     (define-key dired-mode-map (kbd "h") 'dired-summary)
1946     (define-key dired-mode-map (kbd "r") 'revert-buffer)
1947     (define-key dired-mode-map (kbd "l") 'dired-display-file)
1948     (define-key dired-mode-map [C-backspace] 'dired-up-directory)
1949     (define-key dired-mode-map (kbd "?") 'describe-mode)
1950     (define-key dired-mode-map (kbd "z") #'sd/dired-get-size)
1951     (define-key dired-mode-map (kbd "C-d") 'dired-kill-subdir)
1952     (define-key dired-mode-map (kbd "M-d") 'dired-kill-subdir)
1953     (define-key dired-mode-map (kbd "J") 'diredp-next-subdir)
1954     (define-key dired-mode-map (kbd "TAB") 'diredp-next-subdir)
1955     (define-key dired-mode-map (kbd "K") 'diredp-prev-subdir)
1956     (define-key dired-mode-map (kbd "O") 'dired-display-file)
1957     (define-key dired-mode-map (kbd "I") 'other-window)
1958     (define-key dired-mode-map (kbd "o") 'other-window)) 
1959
1960   (use-package dired
1961     :config
1962     (require 'dired-x)
1963     ;; also load dired+
1964     (use-package dired+
1965       :ensure t
1966       :init (setq diredp-hide-details-initially-flag nil))
1967     
1968     (setq dired-omit-mode t)
1969     (setq dired-omit-files (concat dired-omit-files "\\|^\\..+$"))
1970     (add-hook 'dired-mode-hook (lambda ()
1971                                  (sd/dired-key-map)
1972                                  (dired-omit-mode))))
1973
1974   (defadvice dired-summary (around sd/dired-summary activate)
1975     "Revisied dired summary."
1976     (interactive)
1977     (dired-why)
1978     (message
1979      "Ξ”: d-delete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, c-create, +new dir, r-evert, /-filter, v-iew, l-ist, z-Size, h-summary, ?-help"))
1980
1981   (defun sd/dired-high-level-dir ()
1982     "Go to higher level directory"
1983     (interactive)
1984     (find-alternate-file ".."))
1985 #+END_SRC
1986
1987 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1988   (defun sd/dired-new-file-and-open ()
1989     "Create a new file in dired mode"
1990     (interactive)
1991     (call-interactively 'find-file))
1992
1993   (defun sd/dired-new-file (file)
1994     "Create a new file called FILE.
1995   If FILE already exists, signal an error."
1996     (interactive
1997      (list (read-file-name "Create file: " (dired-current-directory))))
1998     (let* ((expanded (expand-file-name file)))
1999       (if (file-exists-p expanded)
2000           (error "Cannot create file %s: file exists" expanded))
2001       (write-region "" nil expanded t)
2002       (when expanded
2003         (dired-add-file expanded)
2004         (dired-move-to-filename))))
2005
2006   ;; copied from abo-abo's config
2007   (defun sd/dired-get-size ()
2008     (interactive)
2009     (let ((files (dired-get-marked-files)))
2010       (with-temp-buffer
2011         (apply 'call-process "/usr/bin/du" nil t nil "-sch" files)
2012         (message
2013          "Size of all marked files: %s"
2014          (progn
2015            (re-search-backward "\\(^[ 0-9.,]+[A-Za-z]+\\).*total$")
2016            (match-string 1))))))
2017 #+END_SRC
2018
2019 ** disable ido when dired new file
2020 When create a new directory, I want to disalbe =ido= completion. see [[http://stackoverflow.com/questions/7479565/emacs-ido-mode-and-creating-new-files-in-directories-it-keeps-changing-the-dire][here]]. Thhis code snippets copied
2021 from [[https://emacs.stackexchange.com/questions/13713/how-to-disable-ido-in-dired-create-directory/13795#13795?newreg%3Ddb17c20f7af3490fb11cf15f1d888e9e][How to disable IDO in β€˜dired-create-directory’]]
2022 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2023   (defun mk-anti-ido-advice (func &rest args)
2024     "Temporarily disable IDO and call function FUNC with arguments ARGS."
2025     (interactive)
2026     (let ((read-file-name-function #'read-file-name-default)
2027           (completing-read-function #'completing-read-default))
2028       (if (called-interactively-p 'any)
2029           (call-interactively func)
2030         (apply func args))))
2031
2032   (defun mk-disable-ido (command)
2033     "Disable IDO when command COMMAND is called."
2034     (advice-add command :around #'mk-anti-ido-advice))
2035
2036   (defun mk-anti-ido-no-completing-advice (func &rest args)
2037     "Temporarily disable IDO and call function FUNC with arguments ARGS."
2038     (interactive)
2039     (let ((read-file-name-function #'read-file-name-default)
2040           ;; (completing-read-function #'completing-read-default)
2041           )
2042       (if (called-interactively-p 'any)
2043           (call-interactively func)
2044         (apply func args))))
2045
2046   (defun mk-disable-ido-no-completing (command)
2047     "Disable IDO when command COMMAND is called."
2048     (advice-add command :around #'mk-anti-ido-no-completing-advice))
2049 #+END_SRC
2050
2051 Disalble =ido= when new a directory or file in =dired= mode
2052 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2053   ;; call the function which you want to disable ido
2054   (mk-disable-ido 'dired-create-directory)
2055   (mk-disable-ido 'sd/dired-new-file-and-open)
2056   (mk-disable-ido 'sd/dired-new-file)
2057   (mk-disable-ido-no-completing 'dired-goto-file)
2058 #+END_SRC
2059
2060 ** Dired open with
2061 =!= =dired-do-shell-command=
2062 =&= =dired-do-async-shell-command=
2063 here on Mac, just use "open" commands to pen =.pdf=,  =.html= and image files
2064 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2065   (setq dired-guess-shell-alist-user
2066         '(("\\.pdf\\'" "open" "okular")
2067           ("\\.\\(?:djvu\\|eps\\)\\'" "evince")
2068           ("\\.\\(?:jpg\\|jpeg\\|png\\|gif\\|xpm\\)\\'" "open")
2069           ("\\.\\(?:xcf\\)\\'" "gimp")
2070           ("\\.csv\\'" "libreoffice")
2071           ("\\.tex\\'" "pdflatex" "latex")
2072           ("\\.\\(?:mp4\\|mkv\\|avi\\|rmvb\\|flv\\|ogv\\)\\(?:\\.part\\)?\\'" "mplayer")
2073           ("\\.\\(?:mp3\\|flac\\|wav\\)\\'" "mplayer")
2074           ("\\.html?\\'" "open")
2075           ("\\.dmg\\'" "open")
2076           ("\\.cue?\\'" "audacious")))
2077
2078
2079   (defun sd/dired-start-process (cmd &optional file-list)
2080     (interactive
2081      (let ((files (dired-get-marked-files t current-prefix-arg)))
2082        (list
2083         (dired-read-shell-command "& on %s: " current-prefix-arg files)
2084         files)))
2085     
2086     ;; (let (list-switch))
2087     (start-process
2088      cmd nil shell-file-name
2089      shell-command-switch
2090      (format
2091       "nohup 1>/dev/null 2>/dev/null %s \"%s\""
2092       cmd
2093       ;; (if (and (> (length file-list) 1)
2094       ;;          (setq list-switch
2095       ;;                (cadr (assoc cmd ora-dired-filelist-cmd))))
2096       ;;     (format "%s %s" cmd list-switch)
2097       ;;   cmd)
2098       (mapconcat #'expand-file-name file-list "\" \""))))
2099
2100
2101
2102 #+END_SRC
2103
2104 ** dired-hacks
2105 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2106   (use-package dired-hacks-utils
2107     :ensure t
2108     :defer t)
2109 #+END_SRC
2110
2111 ** dired-narrow
2112 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2113   ;;narrow dired to match filter
2114   (use-package dired-narrow
2115     :ensure t
2116     :commands (dired-narrow)
2117     :bind (:map dired-mode-map
2118                 ("/" . dired-narrow)))
2119 #+END_SRC
2120
2121 * Ibuffer
2122 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2123   (global-set-key (kbd "s-b") 'ibuffer)
2124
2125   (with-eval-after-load 'ibuffer
2126     (define-key ibuffer-mode-map (kbd "C-o") nil)
2127     (define-key ibuffer-mode-map (kbd "j") 'ibuffer-forward-line)
2128     (define-key ibuffer-mode-map (kbd "k") 'ibuffer-backward-line)
2129     (define-key ibuffer-mode-map (kbd "r") 'ibuffer-update)
2130     (define-key ibuffer-mode-map (kbd "g") 'ibuffer-jump-to-buffer)
2131     (define-key ibuffer-mode-map (kbd "h") 'sd/ibuffer-summary))
2132
2133   (defun sd/ibuffer-summary ()
2134     "Show summary of keybindings in ibuffer mode"
2135     (interactive)
2136     (message
2137      "Ξ’: m|u - (un)mark, /-filter, //-remove filter, t, RET, g, k, S, D, Q; q to quit; h for help"))
2138 #+END_SRC
2139
2140 * Completion
2141 ** company mode and company-statistics
2142 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2143   (use-package company
2144     :ensure t
2145     :diminish company-mode
2146     :init (setq company-idle-delay 0.1)
2147     (setq company-selection-wrap-around t)
2148     :config
2149     (define-key company-active-map (kbd "M-n") nil)
2150     (define-key company-active-map (kbd "M-p") nil)
2151     (define-key company-active-map (kbd "SPC") #'sd/company-stop-input-space)
2152     (define-key company-active-map (kbd "C-n") #'company-select-next)
2153     (define-key company-active-map (kbd "C-p") #'company-select-previous)
2154     ;; should map both (kbd "TAB") and [tab],https://github.com/company-mode/company-mode/issues/75
2155     (define-key company-active-map (kbd "TAB") #'company-complete-selection)
2156     (define-key company-active-map [tab] #'company-complete-selection)
2157     (define-key company-active-map (kbd "C-w") nil)
2158     (define-key company-active-map (kbd "C-h") nil)
2159     (global-company-mode)
2160     ;; magig-commit is text-modeh
2161     (setq company-global-modes '(not org-mode magit-status-mode text-mode eshell-mode gfm-mode markdown-mode)))
2162
2163   (use-package company-statistics
2164     :ensure t
2165     :init
2166     (setq company-statistics-file (concat MY-EMACS-TMP "/company-statistics-cache.el"))
2167     :config
2168     (company-statistics-mode))
2169
2170   (defun sd/company-stop-input-space ()
2171     "Stop completing and input a space,a workaround of a semantic issue `https://github.com/company-mode/company-mode/issues/614'"
2172     (interactive)
2173     (company-abort)
2174     (insert " "))
2175 #+END_SRC
2176
2177 ** YASnippet
2178 *** yasnippet
2179 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2180   (use-package yasnippet
2181     :ensure t
2182     :defer t
2183     :init
2184     (add-hook 'prog-mode-hook #'yas-minor-mode)
2185     :config
2186     (yas-reload-all))
2187 #+END_SRC
2188
2189 ** YouCompleteMe
2190 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2191   (use-package ycmd
2192     :ensure t
2193     :init
2194     (use-package company-ycmd
2195       :ensure t
2196       :init
2197       (setq company-ycmd-enable-fuzzy-matching nil)
2198       :config
2199       (company-ycmd-setup))
2200     (setq ycmd-force-semantic-completion t)
2201     (add-hook 'c++-mode-hook 'ycmd-mode)
2202     (add-hook 'c-mode-hook 'ycmd-mode)
2203     (set-variable 'ycmd-server-command '("python" "/Users/peng/project/ycmd/ycmd"))
2204     (set-variable 'ycmd-global-config "/Users/peng/project/ycmd/cpp/ycm/.ycm_extra_conf.py"))
2205 #+END_SRC
2206
2207
2208 ** company and yasnippet
2209 Add yasnippet as the company candidates
2210 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2211   ;Add yasnippet support for all company backends
2212   ;https://github.com/syl20bnr/spacemacs/pull/179
2213   (defvar company-mode/enable-yas t
2214     "Enable yasnippet for all backends.")
2215
2216   (defun company-mode/backend-with-yas (backend)
2217     (if (or (not company-mode/enable-yas) (and (listp backend) (member 'company-yasnippet backend)))
2218         backend
2219       (append (if (consp backend) backend (list backend))
2220               '(:with company-yasnippet))))
2221
2222   (setq company-backends (mapcar #'company-mode/backend-with-yas company-backends))
2223 #+END_SRC
2224
2225 Refer, [[http://emacs.stackexchange.com/questions/7908/how-to-make-yasnippet-and-company-work-nicer][how-to-make-yasnippet-and-company-work-nicer]]
2226 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2227   (defun check-expansion ()
2228     (save-excursion
2229       (if (looking-at "\\_>") t
2230         (backward-char 1)
2231         (if (looking-at "\\.") t
2232           (backward-char 1)
2233           (if (looking-at "->") t nil)))))
2234
2235   (defun do-yas-expand ()
2236     (let ((yas/fallback-behavior 'return-nil))
2237       (yas/expand)))
2238
2239   (defun tab-indent-or-complete ()
2240     (interactive)
2241     (cond
2242      ((minibufferp)
2243       (minibuffer-complete))
2244      (t
2245       (indent-for-tab-command)
2246       (if (or (not yas/minor-mode)
2247               (null (do-yas-expand)))
2248           (if (check-expansion)
2249               (progn
2250                 (company-manual-begin)
2251                 (if (null company-candidates)
2252                     (progn
2253                       (company-abort)
2254                       (indent-for-tab-command)))))))))
2255
2256   (defun tab-complete-or-next-field ()
2257     (interactive)
2258     (if (or (not yas/minor-mode)
2259             (null (do-yas-expand)))
2260         (if company-candidates
2261             (company-complete-selection)
2262           (if (check-expansion)
2263               (progn
2264                 (company-manual-begin)
2265                 (if (null company-candidates)
2266                     (progn
2267                       (company-abort)
2268                       (yas-next-field))))
2269             (yas-next-field)))))
2270
2271   (defun expand-snippet-or-complete-selection ()
2272     (interactive)
2273     (if (or (not yas/minor-mode)
2274             (null (do-yas-expand))
2275             (company-abort))
2276         (company-complete-selection)))
2277
2278   (defun abort-company-or-yas ()
2279     (interactive)
2280     (if (null company-candidates)
2281         (yas-abort-snippet)
2282       (company-abort)))
2283
2284   '
2285   ;; (require 'company)
2286   ;; (require 'yasnippet)
2287
2288
2289   ;; (global-set-key [tab] 'tab-indent-or-complete)
2290   ;; (global-set-key (kbd "TAB") 'tab-indent-or-complete)
2291   ;; (global-set-key [(control return)] 'company-complete-common)
2292
2293   ;; (define-key company-active-map [tab] 'expand-snippet-or-complete-selection)
2294   ;; (define-key company-active-map (kbd "TAB") 'expand-snippet-or-complete-selection)
2295
2296   ;; (define-key yas-minor-mode-map [tab] nil)
2297   ;; (define-key yas-minor-mode-map (kbd "TAB") nil)
2298
2299   ;; (define-key yas-keymap [tab] 'tab-complete-or-next-field)
2300   ;; (define-key yas-keymap (kbd "TAB") 'tab-complete-or-next-field)
2301   ;; (define-key yas-keymap [(control tab)] 'yas-next-field)
2302   ;; (define-key yas-keymap (kbd "C-g") 'abort-company-or-yas)
2303 #+END_SRC
2304
2305 * Libs
2306 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2307   (use-package s
2308     :ensure t)
2309 #+END_SRC
2310
2311 * Programming Language
2312 ** Color identiifiers mode
2313 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2314   (use-package color-identifiers-mode
2315     :ensure t
2316     :init
2317     (dolist (mode '(emacs-lisp-mode-hook
2318                     c-mode-hook
2319                     c++-mode-hook))
2320       (add-hook mode #'color-identifiers-mode))
2321     :diminish color-identifiers-mode)
2322
2323   (global-prettify-symbols-mode t)
2324 #+END_SRC
2325 ** Font lock face
2326
2327 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2328   (set-face-attribute 'font-lock-keyword-face nil :foreground "#F92672"  :weight 'bold)
2329   (set-face-attribute 'font-lock-builtin-face nil :weight 'bold)
2330   (set-face-attribute 'font-lock-function-name-face nil :foreground "#A6E22E" :weight 'normal :slant 'normal)
2331   (set-face-attribute 'font-lock-variable-name-face nil :foreground "#00FF00")
2332
2333   (dolist (mode '(c-mode
2334                   c++-mode))
2335     (font-lock-add-keywords mode
2336                             ;; highlight %s \n
2337                             '(("\\([%\\][a-zA-Z]\\)" (1  font-lock-keyword-face prepend))
2338                               ("[^[:alpha:]]\\([[:digit:]]*\\)[^[:alpha:]]" (1 font-lock-constant-face append))
2339                               ;; hex number
2340                               ("[^[:alpha:]]\\(0[x\\|X][0-9a-fA-F]*\\)[^[:alpha:]]" (1 font-lock-constant-face append))
2341                               ;; hightlight the function call
2342                               ("\\s\"?\\(\\(\\sw\\|\\s_\\)+\\(<-\\)?\\)\\s\"?*\\s-*(" (1 font-lock-function-name-face))) t))
2343
2344   (font-lock-add-keywords 'c++-mode
2345                           '(
2346                             ("[\s\\|::]\\(cin\\|cout\\|endl\\)[^[:alpha:][:digit:]]" (1 font-lock-constant-face append))
2347                             ) t)
2348 #+END_SRC
2349
2350 ** Emacs Lisp
2351 In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
2352 bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
2353 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2354   (use-package lispy
2355     :ensure t
2356     :init
2357     (setq lispy-delete-backward-recenter 0)
2358     (with-eval-after-load "lispy"
2359       (define-key lispy-mode-map (kbd "M-o") nil)
2360       (define-key lispy-mode-map (kbd "g") 'special-lispy-goto-local)
2361       (define-key lispy-mode-map (kbd "G") 'special-lispy-goto)
2362       (define-key lispy-mode-map (kbd "M-m") 'back-to-indentation))
2363     :config
2364     (add-hook 'emacs-lisp-mode-hook (apply-partially #'lispy-mode 1)))
2365 #+END_SRC
2366
2367 ** Perl
2368 *** CPerl mode
2369 [[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
2370 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2371   (defalias 'perl-mode 'cperl-mode)
2372
2373   ;; (setq cperl-hairy t)
2374   ;; Turns on most of the CPerlMode options
2375   (setq cperl-auto-newline t)
2376   (setq cperl-highlight-variables-indiscriminately t)
2377   ;(setq cperl-indent-level 4)
2378   ;(setq cperl-continued-statement-offset 4)
2379   (setq cperl-close-paren-offset -4)
2380   (setq cperl-indent-parents-as-block t)
2381   (setq cperl-tab-always-indent t)
2382   ;(setq cperl-brace-offset  0)
2383
2384   (add-hook 'cperl-mode-hook (apply-partially #'cperl-set-style "C++"))
2385
2386   (defalias 'perldoc 'cperl-perldoc)
2387 #+END_SRC
2388
2389 *** Perl template
2390 Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
2391 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2392   (eval-after-load 'autoinsert
2393     '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
2394        '(
2395          "Empty"
2396          "#!/usr/bin/perl -w" \n
2397          \n
2398          "use strict;" >  \n \n
2399          > _
2400          )))
2401 #+END_SRC
2402
2403 *** Perl Keywords
2404 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2405   (font-lock-add-keywords 'cperl-mode
2406                           '(("\\(say\\)" . cperl-nonoverridable-face)
2407                             ("\\([0-9.]\\)*" . font-lock-constant-face)
2408                             ("\".*\\(\\\n\\).*\"" 1 font-lock-constant-face prepend)
2409                             ("\n" 0 font-lock-constant-face prepend)
2410                             ;; ("[%\\][[:alpha:]]" . font-lock-constant-face)
2411                             ("\\(^#!.*\\)$" .  cperl-nonoverridable-face)))
2412 #+END_SRC
2413
2414 ** C & C++
2415 C/C++ ide tools
2416 1. completion (file name, function name, variable name)
2417 2. template yasnippet (keywords, if, function)
2418 3. tags jump
2419 *** c/c++ style
2420 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2421   ;; (setq c-default-style "stroustrup"
2422   ;;       c-basic-offset 4)
2423
2424   ;; ;; "C-M-j" is my global binding for avy goto line below
2425   ;; ;; disable it in c mode
2426   ;; (mapcar #'(lambda (map)
2427   ;;            (define-key map (kbd "C-M-j") nil))
2428   ;;         (list c-mode-map
2429   ;;               c++-mode-map
2430   ;;               objc-mode-map))
2431
2432   ;; objective c
2433   (add-to-list 'auto-mode-alist '("\\.mm\\'" . objc-mode))
2434
2435   (setq-default c-hungry-delete-key t)
2436
2437
2438   ;; Looks `c-hanging-braces-alist' doesn't work as expected when  `electric-pair-mode' is enabled.
2439   (electric-pair-mode 1)
2440
2441   (defconst sd/c-style
2442     '("stroustrup"
2443       (c-tab-always-indent . t)
2444       (c-basic-offset . 4)
2445       (c-hanging-braces-alist . ((substatement-open before)
2446                                  (statement-block-intro before)))
2447       (c-hanging-colons-alist . ())
2448       (c-echo-syntactic-information-p . nil)
2449       (c-hanging-semi&comma-criteria . ()))
2450     "My C programming style")
2451
2452   (c-add-style "sd-c-style" sd/c-style)
2453
2454   (setq c-default-style "sd-c-style")
2455
2456   (add-hook 'c-mode-hook '(lambda ()
2457                             (c-toggle-auto-newline 1)
2458                             (c-set-style "sd-c-style")))
2459
2460   ;; (advice-add 'c-electric-brace :after (lambda (arg) "Add new line after insert {}" (newline-and-indent)))
2461 #+END_SRC
2462 *** irony
2463 **** install irony server
2464 Install clang, on mac, it has =libclang.dylib=, but no develop headers. Install by =brew=
2465 #+BEGIN_SRC sh
2466   brew install llvm --with-clang
2467 #+END_SRC
2468
2469 then install irony searver, and =LIBCLANG_LIBRARY= and =LIBCLANG_INCLUDE_DIR= accordingly
2470 #+BEGIN_SRC emacs-lisp :tangle no :results silent
2471   (irony-install-server)
2472 #+END_SRC
2473
2474 #+BEGIN_SRC sh
2475   cmake -DLIBCLANG_LIBRARY\=/usr/local/Cellar/llvm/4.0.1/lib/libclang.dylib \
2476         -DLIBCLANG_INCLUDE_DIR=/usr/local/Cellar/llvm/4.0.1/include \
2477         -DCMAKE_INSTALL_PREFIX\=/Users/peng/.emacs.d/irony/ \
2478         /Users/peng/.emacs.d/elpa/irony-20160713.1245/server && cmake --build . --use-stderr --config Release --target install 
2479 #+END_SRC
2480
2481 **** irony config
2482 irony-mode-hook, copied from [[https://github.com/Sarcasm/irony-mode][irony-mode]] github
2483 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2484   ;; (use-package irony
2485   ;;   :ensure t
2486   ;;   :init
2487   ;;   (add-hook 'c++-mode-hook 'irony-mode)
2488   ;;   (add-hook 'c-mode-hook 'irony-mode)
2489   ;;   (add-hook 'objc-mode-hook 'irony-mode))
2490
2491   ;; replace the `completion-at-point' and `complete-symbol' bindings in
2492   ;; irony-mode's buffers by irony-mode's function
2493
2494   (defun my-irony-mode-hook ()
2495     (define-key irony-mode-map [remap completion-at-point]
2496       'irony-completion-at-point-async)
2497     (define-key irony-mode-map [remap complete-symbol]
2498       'irony-completion-at-point-async))
2499
2500   ;; (add-hook 'irony-mode-hook 'my-irony-mode-hook)
2501   ;; (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
2502
2503   ;; (add-hook 'c++-mode-local-vars-hook #'sd/c++-mode-local-vars)
2504
2505   ;; add C++ completions, because by default c++ file can not complete
2506   ;; c++ std functions, another method is create .dir-local.el file, for p
2507   ;; for project see irony
2508   (defun sd/c++-mode-local-vars ()
2509     (setq irony--compile-options
2510         '("-std=c++11"
2511           "-stdlib=libc++"
2512           "-I/usr/include/c++/4.2.1")))
2513 #+END_SRC
2514
2515 irony-company
2516 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2517   ;; (use-package company-irony
2518   ;;   :ensure t)
2519
2520   ;; (use-package flycheck-irony
2521   ;;   :ensure t)
2522
2523   ;; (use-package company-c-headers
2524   ;;   :ensure t
2525   ;;   :config
2526   ;;   (add-to-list 'company-c-headers-path-system "/usr/include/c++/4.2.1/")
2527   ;;   (add-to-list 'company-c-headers-path-system "/usr/local/include/"))
2528
2529   ;; (with-eval-after-load 'company
2530   ;;   (add-to-list 'company-backends 'company-irony)
2531   ;;   (add-to-list 'company-backends 'company-c-headers))
2532
2533   ;; (with-eval-after-load 'company
2534   ;;   (push  '(company-irony :with company-yasnippet) company-backends)
2535   ;;   (push  '(company-c-headers :with company-yasnippet) company-backends))
2536
2537   ;; (with-eval-after-load 'flycheck
2538   ;;   (add-hook 'flycheck-mode-hook #'flycheck-irony-setup))
2539 #+END_SRC
2540
2541 *** flycheck
2542 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2543   (use-package flycheck
2544     :ensure t)
2545 #+END_SRC
2546
2547 *** gtags
2548 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2549   (use-package ggtags
2550     :ensure t
2551     :config
2552     (define-key ggtags-mode-map (kbd "M-g d") 'ggtags-find-definition)
2553     (define-key ggtags-mode-map (kbd "M-g r") 'ggtags-find-reference)
2554     (define-key ggtags-mode-map (kbd "M-g r") 'ggtags-find-reference)
2555     (define-key ggtags-mode-map (kbd "C-c g s") 'ggtags-find-other-symbol)
2556     (define-key ggtags-mode-map (kbd "C-c g h") 'ggtags-view-tag-history)
2557     (define-key ggtags-mode-map (kbd "C-c g r") 'ggtags-find-reference)
2558     (define-key ggtags-mode-map (kbd "C-c g f") 'ggtags-find-file)
2559     (define-key ggtags-mode-map (kbd "C-c g c") 'ggtags-create-tags)
2560     (define-key ggtags-mode-map (kbd "C-c g u") 'ggtags-update-tags))
2561
2562   (add-hook 'c-mode-common-hook
2563             (lambda ()
2564               (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
2565                 (ggtags-mode 1))))
2566
2567   (defun sd/ggtags-find-definition ()
2568     "Find the definition with current name completing."
2569     (interactive)
2570     (setq current-prefix-arg '(4))
2571     (call-interactively 'ggtags-find-definition))
2572
2573   (defun sd/ggtags-find-symbol ()
2574     "Find symbol"
2575     (interactive)
2576     (setq current-prefix-arg '(4))
2577     (call-interactively 'ggtags-find-other-symbol))
2578
2579   (require 'cc-mode)
2580   (require 'semantic)
2581   (setq semanticdb-default-save-directory (concat MY-EMACS-TMP "/semantic/"))
2582   (unless (file-exists-p semanticdb-default-save-directory)
2583     (make-directory semanticdb-default-save-directory))
2584
2585   (global-semanticdb-minor-mode 1)
2586   (global-semantic-idle-scheduler-mode 1)
2587
2588   (semantic-mode 1)
2589 #+END_SRC
2590
2591 *** google C style
2592 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2593   ;; (use-package google-c-style
2594   ;;   :ensure t
2595   ;;   :config
2596   ;;   (add-hook 'c-mode-hook 'google-set-c-style)
2597   ;;   (add-hook 'c++-mode-hook 'google-set-c-style))
2598 #+END_SRC
2599
2600 *** Compile and Run the C file
2601 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2602   (defun my-cpp-hook ()
2603     (let* ((current-file-name)
2604            (out-file-name))
2605       (when buffer-file-name
2606         (setq current-file-name (shell-quote-argument buffer-file-name))
2607         (setq out-file-name (shell-quote-argument (concat (file-name-sans-extension buffer-file-name) ".out"))))
2608       (setq-local compilation-read-command t)
2609       (set (make-local-variable 'compile-command)
2610            (concat "g++ -Wall -std=c++11 "
2611                    current-file-name
2612                    " -o "
2613                    out-file-name
2614                    " && "
2615                    out-file-name
2616                    ))
2617       (local-set-key (kbd "s-r") 'compile)))
2618
2619   (add-hook 'c-mode-hook 'my-cpp-hook)
2620   (add-hook 'c++-mode-hook 'my-cpp-hook)
2621 #+END_SRC
2622
2623 ** Lua
2624 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2625   (use-package lua-mode
2626     :ensure t)
2627 #+END_SRC
2628
2629 ** Scheme
2630 Install =guile=, =guile= is an implementation of =Scheme= programming language.
2631 #+BEGIN_SRC sh
2632   brew install guile
2633 #+END_SRC
2634
2635 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2636   (setq geiser-scheme-implementation 'guile)
2637 #+END_SRC
2638
2639 #+BEGIN_SRC scheme
2640   (define a "3")
2641   a
2642 #+END_SRC
2643
2644 #+RESULTS:
2645 : 3
2646
2647 ** Racket
2648 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2649   (use-package racket-mode
2650     :ensure t
2651     :config
2652     (define-key racket-mode-map (kbd "s-r") 'racket-run)
2653     (add-to-list 'racket-mode-hook (lambda () (lispy-mode 1))))
2654
2655   ;; set racket path
2656   (setenv "PATH" (concat (getenv "PATH")
2657                          ":" "/Applications/Racket v6.10.1/bin"))
2658   (setenv "MANPATH" (concat (getenv "MANPATH")
2659                             ":" "/Applications/Racket v6.10.1/man"))
2660   (setq exec-path (append exec-path '("/Applications/Racket v6.10.1/bin")))
2661
2662   (add-to-list 'auto-mode-alist '("\\.rkt\\'" . racket-mode))
2663 #+END_SRC
2664 * Compile
2665 Set the environments vairables in compilation mode
2666 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2667   (use-package compile
2668     :commands compile
2669     :config
2670     (setq compilation-environment (cons "LC_ALL=C" compilation-environment))
2671     (setq compilation-auto-jump-to-first-error t)
2672     (setq compilation-auto-jump-to-next t)
2673     (setq compilation-scroll-output 'first-error)
2674     ;; this will save all the modified buffers before compile
2675     (setq compilation-ask-about-save nil)
2676     (setq compilation-window-height (/ (window-total-height) 3)))
2677
2678   ;; super-r to compile
2679   (with-eval-after-load "compile"
2680     (define-key compilation-mode-map (kbd "C-o") nil)
2681     (define-key compilation-mode-map (kbd "n") 'compilation-next-error)
2682     (define-key compilation-mode-map (kbd "p") 'compilation-previous-error)
2683     (define-key compilation-mode-map (kbd "q") (lambda () (interactive) (quit-window t)))
2684     (define-key compilation-mode-map (kbd "r") #'recompile))
2685
2686   ;; here note dynamic binding the value of vv, otherwise it will resport error when run the hook.
2687   ;; https://emacs.stackexchange.com/questions/10394/scope-in-lambda
2688   (dolist (vv '(
2689                 (cperl-mode-hook . "perl")
2690                 (lua-mode-hook . "lua")
2691                 (python-mode-hook . "python")
2692                 (shell-mode-hook . "sh")))
2693     (add-hook (car vv) `(lambda ()
2694                           (unless (or (file-exists-p "makefile")
2695                                       (file-exists-p "Makefile"))
2696                             (set (make-local-variable 'compile-command)
2697                                  (concat (cdr ',vv)
2698                                          " "
2699                                          (if buffer-file-name
2700                                              (shell-quote-argument buffer-file-name))))))))
2701
2702   (global-set-key (kbd "s-r") 'compile)
2703 #+END_SRC
2704
2705 * Auto-Insert
2706 ** Enable auto-insert mode
2707 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2708   (auto-insert-mode t)
2709   (setq auto-insert-query nil)
2710 #+END_SRC
2711
2712 ** C++ Auto Insert
2713 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2714   (eval-after-load 'autoinsert
2715     '(define-auto-insert '("\\.cpp\\|.cc\\'" . "C++ skeleton")
2716        '(
2717          "Short description:"
2718          "/*"
2719          "\n * " (file-name-nondirectory (buffer-file-name))
2720          "\n */" > \n \n
2721          "#include <iostream>" \n
2722          "//#include \""
2723          (file-name-sans-extension
2724           (file-name-nondirectory (buffer-file-name)))
2725          ".hpp\"" \n \n
2726          "using namespace std;" \n \n
2727          "int main (int argc, char *argv[])"
2728          "\n{" \n 
2729          > _ \n
2730          "return 0;"
2731          "\n}" > \n
2732          )))
2733
2734   (eval-after-load 'autoinsert
2735     '(define-auto-insert '("\\.c\\'" . "C skeleton")
2736        '(
2737          "Short description:"
2738          "/*\n"
2739          " * " (file-name-nondirectory (buffer-file-name)) "\n"
2740          " */" > \n \n
2741          "#include <stdio.h>" \n
2742          "//#include \""
2743          (file-name-sans-extension
2744           (file-name-nondirectory (buffer-file-name)))
2745          ".h\"" \n \n
2746          "int main (int argc, char *argv[])\n"
2747          "{" \n
2748          > _ \n
2749          "return 0;\n"
2750          "}" > \n
2751          )))
2752
2753   (eval-after-load 'autoinsert
2754     '(define-auto-insert '("\\.h\\|.hpp\\'" . "c/c++ header")
2755        '((s-upcase (s-snake-case (file-name-nondirectory buffer-file-name)))
2756          "#ifndef " str n "#define " str "\n\n" _ "\n\n#endif  // " str)))
2757 #+END_SRC
2758
2759 ** Python template
2760 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2761   (eval-after-load 'autoinsert
2762     '(define-auto-insert '("\\.\\(py\\)\\'" . "Python skeleton")
2763        '(
2764          "Empty"
2765          "#import os,sys" \n
2766          \n \n
2767          )))
2768 #+END_SRC
2769
2770 ** Elisp 
2771 Emacs lisp auto-insert, based on the default module in =autoinsert.el=, but replace =completing-read= as 
2772 =completing-read-ido-ubiquitous= to fix the edge case of that =ido= cannot handle.
2773 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2774   (eval-after-load 'autoinsert
2775     '(define-auto-insert '("\\.el\\'" . "my Emacs Lisp header")
2776        '(
2777          "Short description: "
2778          ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
2779          (make-string (max 2 (- 80 (current-column) 27)) ?\s)
2780          "-*- lexical-binding: t; -*-" '(setq lexical-binding t)
2781          "\n
2782   ;; Copyright (C) " (format-time-string "%Y") "  "
2783          (getenv "ORGANIZATION") | (progn user-full-name) "
2784
2785   ;; Author: " (user-full-name)
2786          '(if (search-backward "&" (line-beginning-position) t)
2787               (replace-match (capitalize (user-login-name)) t t))
2788          '(end-of-line 1) " <" (progn user-mail-address) ">
2789   ;; Keywords: "
2790          '(require 'finder)
2791          ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
2792          '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
2793                            finder-known-keywords)
2794                 v2 (mapconcat (lambda (x) (format "%12s:  %s" (car x) (cdr x)))
2795                               finder-known-keywords
2796                               "\n"))
2797          ((let ((minibuffer-help-form v2))
2798             (completing-read-ido-ubiquitous "Keyword, C-h: " v1 nil t))
2799           str ", ") & -2 "
2800
2801   \;; This program is free software; you can redistribute it and/or modify
2802   \;; it under the terms of the GNU General Public License as published by
2803   \;; the Free Software Foundation, either version 3 of the License, or
2804   \;; (at your option) any later version.
2805
2806   \;; This program is distributed in the hope that it will be useful,
2807   \;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2808   \;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2809   \;; GNU General Public License for more details.
2810
2811   \;; You should have received a copy of the GNU General Public License
2812   \;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
2813
2814   \;;; Commentary:
2815
2816   \;; " _ "
2817
2818   \;;; Code:
2819
2820
2821   \(provide '"
2822          (file-name-base)
2823          ")
2824   \;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")))
2825 #+END_SRC
2826
2827 ** Org file template
2828 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2829   ;; (eval-after-load 'autoinsert
2830   ;;   '(define-auto-insert '("\\.\\(org\\)\\'" . "Org-mode skeleton")
2831   ;;      '(
2832   ;;        "title: "
2833   ;;        "#+TITLE: " str (make-string 30 ?\s) > \n
2834   ;;        "#+AUTHOR: Peng Li\n"
2835   ;;        "#+EMAIL: seudut@gmail.com\n"
2836   ;;        "#+DATE: " (shell-command-to-string "echo -n $(date +%Y-%m-%d)") > \n
2837   ;;        > \n
2838   ;;        > _)))
2839 #+END_SRC
2840
2841 * Markdown mode
2842 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2843   (use-package markdown-mode
2844     :ensure t
2845     :commands (markdown-mode gfm-mode)
2846     :mode (("README\\.md\\'" . gfm-mode)
2847            ("\\.md\\'" . markdown-mode)
2848            ("\\.markdown\\'" . markdown-mode))
2849     :init (setq markdown-command "multimarkdown"))
2850
2851   ;; (add-hook 'gfm-mode-hook (lambda ()
2852   ;;                            (set-face-attribute 'markdown-inline-code-face nil :inherit 'fixed-pitch)
2853   ;;                            (set-face-attribute 'markdown-pre-face nil :inherit 'fixed-pitch)))
2854   ;; (with-eval-after-load "gfm-mode"
2855   ;;   (set-face-attribute 'markdown-inline-code-face nil :inherit 'fixed-pitch)
2856   ;;   (set-face-attribute 'markdown-pre-face nil :inherit 'fixed-pitch))
2857 #+END_SRC
2858
2859 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2860   (use-package markdown-preview-eww
2861     :ensure t)
2862 #+END_SRC
2863
2864 * Iedit
2865 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2866   (use-package iedit
2867     :ensure t
2868     :init
2869     (global-set-key (kbd "C-;") #'iedit-mode))
2870 #+END_SRC
2871 * Gnus
2872 ** Gmail setting 
2873 Refer [[https://www.emacswiki.org/emacs/GnusGmail][GnusGmail]]
2874 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2875   (setq user-mail-address "seudut@gmail.com"
2876         user-full-name "Peng Li")
2877
2878   (setq gnus-select-method
2879         '(nnimap "gmail"
2880                  (nnimap-address "imap.gmail.com")
2881                  (nnimap-server-port "imaps")
2882                  (nnimap-stream ssl)))
2883
2884   (setq smtpmail-smtp-service 587
2885         gnus-ignored-newsgroups "^to\\.\\|^[0-9. ]+\\( \\|$\\)\\|^[\"]\"[#'()]")
2886
2887   ;; Use gmail sending mail
2888   (setq message-send-mail-function 'smtpmail-send-it
2889         smtpmail-starttls-credentials '(("smtp.gmail.com" 587 nil nil))
2890         smtpmail-auth-credentials '(("smtp.gmail.com" 587 "seudut@gmail.com" nil))
2891         smtpmail-default-smtp-server "smtp.gmail.com"
2892         smtpmail-smtp-server "smtp.gmail.com"
2893         smtpmail-smtp-service 587
2894         starttls-use-gnutls t)
2895 #+END_SRC
2896
2897 And put the following in =~/.authinfo= file, replacing =<USE>= with your email address
2898 and =<PASSWORD>= with the password
2899 #+BEGIN_EXAMPLE
2900   machine imap.gmail.com login <USER> password <PASSWORD> port imaps
2901   machine smtp.gmail.com login <USER> password <PASSWORD> port 587
2902 #+END_EXAMPLE
2903
2904 Then Run =M-x gnus=
2905
2906 ** Group buffer
2907 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2908   ;; (use-package gnus
2909   ;;   :init
2910   ;;   (setq gnus-permanently-visible-groups "\.*")
2911   ;;   :config
2912   ;;   (cond (window-system
2913   ;;          (setq custom-background-mode 'light)
2914   ;;          (defface my-group-face-1
2915   ;;            '((t (:foreground "Red" :bold t))) "First group face")
2916   ;;          (defface my-group-face-2
2917   ;;            '((t (:foreground "DarkSeaGreen4" :bold t)))
2918   ;;            "Second group face")
2919   ;;          (defface my-group-face-3
2920   ;;            '((t (:foreground "Green4" :bold t))) "Third group face")
2921   ;;          (defface my-group-face-4
2922   ;;            '((t (:foreground "SteelBlue" :bold t))) "Fourth group face")
2923   ;;          (defface my-group-face-5
2924   ;;            '((t (:foreground "Blue" :bold t))) "Fifth group face")))
2925   ;;   (setq gnus-group-highlight
2926   ;;         '(((> unread 200) . my-group-face-1)
2927   ;;           ((and (< level 3) (zerop unread)) . my-group-face-2)
2928   ;;           ((< level 3) . my-group-face-3)
2929   ;;           ((zerop unread) . my-group-face-4)
2930   ;;           (t . my-group-face-5))))
2931
2932
2933   ;; ;; key-
2934   ;; (add-hook 'gnus-group-mode-hook (lambda ()
2935   ;;                                   (define-key gnus-group-mode-map "k" 'gnus-group-prev-group)
2936   ;;                                   (define-key gnus-group-mode-map "j" 'gnus-group-next-group)
2937   ;;                                   (define-key gnus-group-mode-map "g" 'gnus-group-jump-to-group)
2938   ;;                                   (define-key gnus-group-mode-map "v" (lambda () (interactive) (gnus-group-select-group t)))))
2939 #+END_SRC
2940
2941 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2942   (setq gnus-fetch-old-headers 't)
2943
2944
2945
2946   (setq gnus-extract-address-components
2947         'mail-extract-address-components)
2948   ;; summary buffer 
2949   (setq gnus-summary-line-format "%U%R%z%I%(%[%-20,20f%]%)  %s%-80=   %11&user-date;\n")
2950   (setq gnus-user-date-format-alist '(((gnus-seconds-today) . "%H:%M")
2951                                       ((+ 86400 (gnus-seconds-today)) . "%a %H:%M")
2952                                       (604800 . "%a, %b %-d")
2953                                       (15778476 . "%b %-d")
2954                                       (t . "%Y-%m-%d")))
2955
2956   (setq gnus-thread-sort-functions '((not gnus-thread-sort-by-number)))
2957   (setq gnus-unread-mark ?\.)
2958   (setq gnus-use-correct-string-widths t)
2959
2960   ;; thread
2961   (setq gnus-thread-hide-subtree t)
2962
2963   ;; (with-eval-after-load 'gnus-summary-mode
2964   ;;   (define-key gnus-summary-mode-map (kbd "C-o") 'sd/hydra-window/body))
2965
2966   ;; (add-hook 'gnus-summary-mode-hook (lambda ()
2967   ;;                                     (define-key gnus-summary-mode-map (kbd "C-o") nil)))
2968   ;(add-hook 'gnus-summary-mode-hook (apply-partially #'define-key gnus-summary-mode-map (kbd "C-o") nil))
2969
2970
2971 #+END_SRC
2972
2973 ** Windows layout
2974 See [[https://www.emacswiki.org/emacs/GnusWindowLayout][GnusWindowLayout]]
2975 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2976   (gnus-add-configuration
2977    '(summary
2978      (horizontal 1.0
2979                  (vertical 35
2980                            (group 1.0))
2981                  (vertical 1.0
2982                            (summary 1.0 poine)))))
2983
2984   (gnus-add-configuration
2985    '(article
2986      (horizontal 1.0
2987                  (vertical 35
2988                            (group 1.0))
2989                  (vertical 1.0
2990                            (summary 0.50 point)
2991                            (article 1.0)))))
2992
2993   (with-eval-after-load 'gnus-group-mode
2994     (gnus-group-select-group "INBOX"))
2995   ;; (add-hook 'gnus-group-mode-map (lambda ()
2996   ;;                               (gnus-group-select-group "INBOX")))
2997 #+END_SRC
2998
2999 * Mu4e
3000 Refer [[http://www.kirang.in/2014/11/13/emacs-as-email-client-with-offlineimap-and-mu4e-on-osx][emacs-as-email-client-with-offlineimap-and-mu4e-on-osx]]
3001
3002 ** OfflineImap - download all mails from IMAP into local directory, and keep in sync
3003 #+BEGIN_SRC sh :results output replace
3004   # offline-imap
3005   brew install offline-imap
3006
3007   cp /usr/local/etc/offlineimap.conf ~/.offlineimapr
3008
3009   #For the =offlineimap= config on mac, using =sslcacertfile= instead of =cert_fingerpring=. On Mac
3010   sslcacertfile = /usr/local/etc/openssl/cert.pem 
3011 #+END_SRC
3012
3013 #+BEGIN_SRC conf 
3014   [general]
3015   ui=TTYUI
3016   accounts = Gmail
3017   autorefresh = 5
3018
3019   [Account Gmail]
3020   localrepository = Gmail-Local
3021   remoterepository = Gmail-Remote
3022
3023   [Repository Gmail-Local]
3024   type = Maildir
3025   localfolders = ~/.Mail/seudut@gmail.com
3026
3027   [Repository Gmail-Remote]
3028   type = Gmail
3029   remotehost = imap.gmail.com
3030   remoteuser = seudut@gmail.com
3031   remotepass = xxxxxxxx
3032   realdelete = no
3033   ssl = yes
3034   #cert_fingerprint = <insert gmail server fingerprint here>
3035   sslcacertfile = /usr/local/etc/openssl/cert.pem
3036   maxconnections = 1
3037   folderfilter = lambda folder: folder not in ['[Gmail]/Trash',
3038                                                '[Gmail]/Spam',
3039                                                '[Gmail]/All Mail',
3040                                                ]
3041 #+END_SRC
3042
3043 Then, run =offlineimap= to sync the mail
3044
3045 ** Mu - fast search, view mails and extract attachments.
3046 #+BEGIN_SRC sh
3047   EMACS=/usr/local/bin/emacs brew install mu --with-emacs
3048 #+END_SRC
3049
3050 Then, run =mu index --maildir=~/.Mail=
3051
3052 ** Mu4e - Emacs frontend of Mu
3053 config from [[http://www.kirang.in/2014/11/13/emacs-as-email-client-with-offlineimap-and-mu4e-on-osx/][emacs-as-email-client-with-offlineimap-and-mu4e-on-osx]]
3054 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3055   (if (require 'mu4e nil 'noerror)
3056       (progn
3057         (setq mu4e-maildir "~/.Mail")
3058         (setq mu4e-drafts-folder "/[Gmail].Drafts")
3059         (setq mu4e-sent-folder   "/[Gmail].Sent Mail")
3060         ;; don't save message to Sent Messages, Gmail/IMAP takes care of this
3061         (setq mu4e-sent-messages-behavior 'delete)
3062         ;; allow for updating mail using 'U' in the main view:
3063         (setq mu4e-get-mail-command "offlineimap")
3064
3065         ;; shortcuts
3066         (setq mu4e-maildir-shortcuts
3067               '( ("/INBOX"               . ?i)
3068                  ("/[Gmail].Sent Mail"   . ?s)))
3069
3070         ;; something about ourselves
3071         (setq
3072          user-mail-address "seudut@gmail.com"
3073          user-full-name  "Peng Li"
3074          mu4e-compose-signature
3075          (concat
3076           "Thanks,\n"
3077           "Peng\n"))
3078
3079         ;; show images
3080         (setq mu4e-show-images t)
3081
3082         ;; use imagemagick, if available
3083         (when (fboundp 'imagemagick-register-types)
3084           (imagemagick-register-types))
3085
3086         ;; convert html emails properly
3087         ;; Possible options:
3088         ;;   - html2text -utf8 -width 72
3089         ;;   - textutil -stdin -format html -convert txt -stdout
3090         ;;   - html2markdown | grep -v '&nbsp_place_holder;' (Requires html2text pypi)
3091         ;;   - w3m -dump -cols 80 -T text/html
3092         ;;   - view in browser (provided below)
3093         (setq mu4e-html2text-command "textutil -stdin -format html -convert txt -stdout")
3094
3095         ;; spell check
3096         (add-hook 'mu4e-compose-mode-hook
3097                   (defun my-do-compose-stuff ()
3098                     "My settings for message composition."
3099                     (set-fill-column 72)
3100                     (flyspell-mode)))
3101
3102         ;; add option to view html message in a browser
3103         ;; `aV` in view to activate
3104         (add-to-list 'mu4e-view-actions
3105                      '("ViewInBrowser" . mu4e-action-view-in-browser) t)
3106
3107         ;; fetch mail every 10 mins
3108         (setq mu4e-update-interval 600)
3109
3110         ;; mu4e view
3111         (setq-default mu4e-headers-fields '((:flags . 6)
3112                                             (:from-or-to . 22)
3113                                             (:mailing-list . 20)
3114                                             (:thread-subject . 70)
3115                                             (:human-date . 16))))
3116     (message "seudut:mu4e not installed, it won't work."))
3117 #+END_SRC
3118
3119 ** Smtp - send mail
3120 - =gnutls=, depends on =gnutls=, first confirm this is installed, otherwise, =brew install gnutls=
3121 - =~/.authinfo=
3122 #+BEGIN_SRC fundamental 
3123   machine smtp.gmail.com login <gmail username> password <gmail password>
3124 #+END_SRC
3125 - OPTIONAL, encrypt the =~/.authinfo= file
3126 #+BEGIN_SRC sh :results output replace
3127   gpg --output ~/.authinfo.gpg --symmetric ~/.authinfo
3128 #+END_SRC
3129
3130 * Ediff
3131 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3132   (with-eval-after-load 'ediff
3133     (setq ediff-split-window-function 'split-window-horizontally)
3134     (setq ediff-window-setup-function 'ediff-setup-windows-plain)
3135     (add-hook 'ediff-startup-hook 'ediff-toggle-wide-display)
3136     (add-hook 'ediff-cleanup-hook 'ediff-toggle-wide-display)
3137     (add-hook 'ediff-suspend-hook 'ediff-toggle-wide-display))
3138 #+END_SRC
3139
3140 * Modes
3141 ** Yaml-mode
3142 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3143   (use-package yaml-mode
3144     :ensure t
3145     :init
3146     (add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode)))
3147 #+END_SRC
3148
3149 * Entertainment
3150 ** GnuGo
3151 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
3152 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3153   (use-package gnugo
3154     :ensure t
3155     :defer t
3156     :init
3157     (require 'gnugo-imgen)
3158     (setq gnugo-xpms 'gnugo-imgen-create-xpms)
3159     (add-hook 'gnugo-start-game-hook '(lambda ()
3160                                         (gnugo-image-display-mode)
3161                                         (gnugo-grid-mode)))
3162     :config
3163     (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
3164 #+END_SRC
3165
3166 ** Emms
3167 We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
3168 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3169   (use-package emms
3170     :ensure t
3171     :init
3172     (setq emms-directory (concat sd-temp-directory "emms"))
3173     (setq emms-source-file-default-directory "~/Music/")
3174     :config
3175     (emms-standard)
3176     (emms-default-players)
3177     (define-emms-simple-player mplayer '(file url)
3178       (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
3179                     ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
3180                     "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
3181                     ".m4a" ".flv" ".ogv" ".pls"))
3182       "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
3183     (emms-history-load))
3184 #+END_SRC
3185
3186 * Dictionary
3187 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3188   (use-package bing-dict
3189     :ensure t
3190     :init
3191     (global-set-key (kbd "s-d") 'bing-dict-brief)
3192     :commands (bing-dict-brief))
3193 #+END_SRC
3194
3195 * Project operations - =super=
3196
3197 ** Windown & Buffer - =C-o=
3198 Defind a =hydra= function for windows, buffer & bookmark operations. And map it to =C-o= globally.
3199 Most use =C-o C-o= to switch buffers; =C-o x, v= to split window; =C-o o= to delete other windows
3200 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3201   (winner-mode 1)
3202
3203   (require 'utilities)
3204   ;; (defhydra sd/hydra-window (:color red :columns nil)
3205   ;;   "C-o"
3206   ;;   ;; windows switch
3207   ;;   ("h" windmove-left nil :exit t)
3208   ;;   ("j" windmove-down nil :exit t)
3209   ;;   ("k" windmove-up nil :exit t)
3210   ;;   ("l" windmove-right nil :exit t)
3211   ;;   ("C-o" other-window nil :exit t)
3212   ;;   ;; window resize
3213   ;;   ("H" hydra-move-splitter-left nil)
3214   ;;   ("J" hydra-move-splitter-down nil)
3215   ;;   ("K" hydra-move-splitter-up nil)
3216   ;;   ("L" hydra-move-splitter-right nil)
3217   ;;   ;; windows split
3218   ;;   ("v" (lambda ()
3219   ;;          (interactive)
3220   ;;          (split-window-right)
3221   ;;          (windmove-right))
3222   ;;    nil :exit t)
3223   ;;   ("x" (lambda ()
3224   ;;          (interactive)
3225   ;;          (split-window-below)
3226   ;;          (windmove-down))
3227   ;;    nil :exit t)
3228   ;;   ;; buffer / windows switch
3229   ;;   ("o" sd/toggle-max-windows nil :exit t)
3230   ;;   ("C-k" sd/delete-current-window nil :exit t)
3231   ;;   ("C-d" (lambda ()
3232   ;;            (interactive)
3233   ;;            (kill-buffer)
3234   ;;            (sd/delete-current-window))
3235   ;;    nil :exit t)
3236
3237   ;;   ;; ace-window
3238   ;;   ;; ("'" other-window "other" :exit t)
3239   ;;   ;; ("a" ace-window "ace")
3240   ;;   ("s" ace-swap-window nil)
3241   ;;   ("D" ace-delete-window nil :exit t)
3242   ;;   ;; ("i" ace-maximize-window "ace-one" :exit t)
3243   ;;   ;; Windows undo - redo
3244   ;;   ("u" (progn (winner-undo) (setq this-command 'winner-undo)) nil)
3245   ;;   ("r" (progn (winner-redo) (setq this-command 'winner-redo)) nil)
3246
3247   ;;   ;; ibuffer, dired, eshell, bookmarks
3248   ;;   ;; ("C-i" other-window nil :exit t)
3249   ;;   ("C-b" ido-switch-buffer nil :exit t)
3250   ;;   ("C-f" projectile-find-file nil :exit t)
3251   ;;   ("C-r" ivy-recentf nil :exit t)
3252   ;;   ;; ("C-p" persp-switch nil :exit t)
3253   ;;   ;; ("C-t" projectile-persp-switch-project nil :exit t)
3254
3255   ;;   ;; other special buffers
3256   ;;   ("d" sd/project-or-dired-jump nil :exit t)
3257   ;;   ("b" ibuffer nil :exit t)
3258   ;;   ("t" multi-term nil :exit t)
3259   ;;   ("e" sd/toggle-project-eshell nil :exit t)
3260   ;;   ("m" bookmark-jump-other-window nil :exit t)
3261   ;;   ("M" bookmark-set nil :exit t)
3262   ;;   ("g" magit-status nil :exit t)
3263   ;;   ;; ("p" paradox-list-packages nil :exit t)
3264
3265   ;;   ;; quit
3266   ;;   ("q" nil nil)
3267   ;;   ("<ESC>" nil nil)
3268   ;;   ("C-h" windmove-left nil :exit t)
3269   ;;   ("C-j" windmove-down nil :exit t)
3270   ;;   ("C-k" windmove-up nil :exit t)
3271   ;;   ("C-l" windmove-right nil :exit t)
3272   ;;   ("C-;" nil nil :exit t)
3273   ;;   ("n" nil nil :exit t)
3274   ;;   ("[" nil nil :exit t)
3275   ;;   ("]" nil nil :exit t)
3276   ;;   ("f" nil nil))
3277
3278   ;; (global-unset-key (kbd "C-o"))
3279   ;; (global-set-key (kbd "C-o") 'sd/hydra-window/body)
3280
3281   ;; (defun sd/project-or-dired-jump ()
3282   ;;   "If under project, jump to the root directory, otherwise
3283   ;; jump to dired of current file"
3284   ;;   (interactive)
3285   ;;   (if (projectile-project-p)
3286   ;;       (projectile-dired)
3287   ;;     (dired-jump)))
3288 #+END_SRC
3289
3290 Kill the help window and buffer when quit.
3291 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3292   (with-eval-after-load "help-mode"
3293     (define-key help-mode-map (kbd "q") (lambda ()
3294                                           (interactive)
3295                                           (quit-window t))))
3296
3297   (with-eval-after-load 'man
3298     (set-face-attribute 'Man-overstrike nil :inherit 'bold :foreground "orange red")
3299     (set-face-attribute 'Man-underline nil :inherit 'underline :foreground "forest green")
3300     (define-key Man-mode-map (kbd "q") (lambda ()
3301                                          (interactive)
3302                                          (Man-kill))))
3303
3304
3305   ;; (advice-add 'man :after (lambda (man-args) (other-window 1)))
3306   (advice-add 'man :after (apply-partially 'other-window 1))
3307
3308
3309   (require 'shell-command-output-mode)
3310
3311   (defun my/shell-command-after (command &optional output-buffer error-buffer)
3312     (let* ((buffer (get-buffer "*Shell Command Output*"))
3313            (window (get-buffer-window buffer)))
3314       (if buffer (with-current-buffer buffer
3315                    (shell-command-output-mode)))
3316       (if window
3317           (select-window window))))
3318
3319   (advice-add 'shell-command :after 'my/shell-command-after)
3320 #+END_SRC
3321
3322 ** Motion
3323
3324 *** Marks
3325 =C-<SPC>= set marks toggle the region
3326 =C-u C-<SPC>= Jump to the mark, repeated calls go further back the mark ring
3327 =C-x C-x= Exchanges the point and mark.
3328
3329 Stolen [[https://www.masteringemacs.org/article/fixing-mark-commands-transient-mark-mode][fixing-mark-commands-transient-mark-mode]]
3330 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3331   (defun push-mark-no-activate ()
3332     "Pushes `point' to `mark-ring' and does not activate the region
3333      Equivalent to \\[set-mark-command] when \\[transient-mark-mode] is disabled"
3334     (interactive)
3335     (push-mark (point) t nil)
3336     (message "Pushed mark to ring"))
3337
3338   ;; (global-set-key (kbd "C-`") 'push-mark-no-activate)
3339
3340   (defun jump-to-mark ()
3341     "Jumps to the local mark, respecting the `mark-ring' order.
3342     This is the same as using \\[set-mark-command] with the prefix argument."
3343     (interactive)
3344     (set-mark-command 1))
3345
3346   ;; (global-set-key (kbd "M-`") 'jump-to-mark)
3347
3348   (defun exchange-point-and-mark-no-activate ()
3349     "Identical to \\[exchange-point-and-mark] but will not activate the region."
3350     (interactive)
3351     (exchange-point-and-mark)
3352     (deactivate-mark nil))
3353
3354   ;; (define-key global-map [remap exchange-point-and-mark] 'exchange-point-and-mark-no-activate)
3355 #+END_SRC
3356
3357 Show the mark ring using =helm-mark-ring=, also mapping =M-`= to quit minibuffer. so that =M-`= can 
3358 toggle the mark ring. the best way is add a new action and mapping to =helm-source-mark-ring=,  but 
3359 since there is no map such as =helm-mark-ring=map=, so I cannot binding a key to the quit action.
3360 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3361   (setq mark-ring-max 50)
3362
3363   (use-package helm
3364     :ensure t
3365     :init
3366     (global-set-key (kbd "M-`") #'helm-mark-ring))
3367
3368   (define-key minibuffer-local-map (kbd "M-`") 'keyboard-escape-quit)
3369 #+END_SRC
3370
3371 =M-h= marks the next paragraph
3372 =C-x h= marks the whole buffer
3373 =C-M-h= marks the next defun
3374 =C-x C-p= marks the next page
3375 *** Registers
3376 Registers can save text, position, rectangles, file and configuration and other things.
3377 Here for movement, we can use register to save/jump position
3378 =C-x r SPC= store point in register
3379 =C-x r j= jump to register
3380 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3381   (use-package list-register
3382     :ensure t)
3383 #+END_SRC
3384
3385 *** =Avy= for easy motion
3386 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3387   (use-package avy
3388     :ensure t
3389     :config
3390     (avy-setup-default))
3391
3392   ;; (global-set-key (kbd "C-M-j") 'avy-goto-line-below)
3393   ;; (global-set-key (kbd "C-M-n") 'avy-goto-line-below)
3394   ;; (global-set-key (kbd "C-M-k") 'avy-goto-line-above)
3395   ;; (global-set-key (kbd "C-M-p") 'avy-goto-line-above)
3396
3397   ;; (global-set-key (kbd "C-M-f") 'avy-goto-word-1-below)
3398   ;; (global-set-key (kbd "C-M-b") 'avy-goto-word-1-above)
3399
3400   ;; (global-set-key (kbd "M-g e") 'avy-goto-word-0)
3401   ;; (global-set-key (kbd "C-M-w") 'avy-goto-char-timer)
3402   ;; (global-set-key (kbd "C-M-l") 'avy-goto-char-in-line)
3403
3404   ;; ;; will delete above 
3405   ;; (global-set-key (kbd "M-g j") 'avy-goto-line-below)
3406   ;; (global-set-key (kbd "M-g k") 'avy-goto-line-above)
3407   ;; (global-set-key (kbd "M-g w") 'avy-goto-word-1-below)
3408   ;; (global-set-key (kbd "M-g b") 'avy-goto-word-1-above)
3409   ;; (global-set-key (kbd "M-g e") 'avy-goto-word-0)
3410   ;; (global-set-key (kbd "M-g f") 'avy-goto-char-timer)
3411   ;; (global-set-key (kbd "M-g c") 'avy-goto-char-in-line)
3412   (global-set-key (kbd "M-g c") 'avy-goto-char)
3413   (global-set-key (kbd "M-g b") 'avy-goto-word-0-above)
3414   (global-set-key (kbd "M-g w") 'avy-goto-word-0-below)
3415 #+END_SRC
3416
3417 *** =Imenu= goto tag
3418 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3419   (global-set-key (kbd "M-i") #'counsel-imenu)
3420   ;; (global-set-key (kbd "M-i") #'imenu)
3421
3422   ;; define M-[ as C-M-a
3423   ;; http://ergoemacs.org/emacs/emacs_key-translation-map.html
3424   (define-key key-translation-map (kbd "M-[") (kbd "C-M-a"))
3425   (define-key key-translation-map (kbd "M-]") (kbd "C-M-e"))
3426 #+END_SRC
3427
3428 *** Go-to line
3429 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3430   (global-set-key (kbd "M-l") 'goto-line)
3431 #+END_SRC
3432
3433 ** Edit
3434
3435 *** Kill ring
3436 =helm-show-kill-ring=
3437 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3438   (setq kill-ring-max 100)                ; default is 60p
3439
3440   (use-package helm
3441     :ensure t
3442     :init
3443     (global-set-key (kbd "M-y") #'helm-show-kill-ring))
3444 #+END_SRC
3445
3446 *** undo-tree
3447 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3448   (use-package undo-tree
3449     :ensure t
3450     :config
3451     (define-key undo-tree-visualizer-mode-map "j" 'undo-tree-visualize-redo)
3452     (define-key undo-tree-visualizer-mode-map "k" 'undo-tree-visualize-undo)
3453     (define-key undo-tree-visualizer-mode-map "h" 'undo-tree-visualize-switch-branch-left)
3454     (define-key undo-tree-visualizer-mode-map "l" 'undo-tree-visualize-switch-branch-right)
3455     (global-undo-tree-mode 1))
3456
3457   (global-set-key (kbd "s-u") 'undo-tree-visualize)
3458 #+END_SRC
3459
3460 *** flyspell
3461 Stolen from [[https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-spelling.el][here]], hunspell will search dictionary in =DICPATH=
3462 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3463   (setenv "DICPATH" "/usr/local/share/hunspell")
3464
3465   (when (executable-find "hunspell")
3466     (setq-default ispell-program-name "hunspell")
3467     (setq ispell-really-hunspell t))
3468
3469   ;; (defun text-mode-hook-setup ()
3470   ;;   ;; Turn off RUN-TOGETHER option when spell check text-mode
3471   ;;   (setq-local ispell-extra-args (flyspell-detect-ispell-args)))
3472   ;; (add-hook 'text-mode-hook 'text-mode-hook-setup)
3473   ;; (add-hook 'text-mode-hook 'flyspell-mode)
3474
3475   ;; enable flyspell check on comments and strings in progmamming modes
3476   ;; (add-hook 'prog-mode-hook 'flyspell-prog-mode)
3477
3478   ;; I don't use the default mappings
3479   (with-eval-after-load 'flyspell
3480     (define-key flyspell-mode-map (kbd "C-;") nil)
3481     (define-key flyspell-mode-map (kbd "C-,") nil)
3482     (define-key flyspell-mode-map (kbd "C-.") nil))
3483 #+END_SRC
3484
3485 Make flyspell enabled for org-mode, see [[http://emacs.stackexchange.com/questions/9333/how-does-one-use-flyspell-in-org-buffers-without-flyspell-triggering-on-tangled][here]]
3486 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3487   ;; NO spell check for embedded snippets
3488   (defadvice org-mode-flyspell-verify (after org-mode-flyspell-verify-hack activate)
3489     (let ((rlt ad-return-value)
3490           (begin-regexp "^[ \t]*#\\+begin_\\(src\\|html\\|latex\\)")
3491           (end-regexp "^[ \t]*#\\+end_\\(src\\|html\\|latex\\)")
3492           old-flag
3493           b e)
3494       (when ad-return-value
3495         (save-excursion
3496           (setq old-flag case-fold-search)
3497           (setq case-fold-search t)
3498           (setq b (re-search-backward begin-regexp nil t))
3499           (if b (setq e (re-search-forward end-regexp nil t)))
3500           (setq case-fold-search old-flag))
3501         (if (and b e (< (point) e)) (setq rlt nil)))
3502       (setq ad-return-value rlt)))
3503 #+END_SRC
3504
3505 ** Search & Replace / hightlight =M-s=
3506 *** isearch
3507 =C-s=, =C-r=, 
3508 =C-w= add word at point to search string, 
3509 =M-%= query replace
3510 =C-M-y= add character at point to search string
3511 =M-s C-e= add reset of line at point
3512 =C-y= yank from clipboard to search string
3513 =M-n=, =M-p=, history
3514 =C-M-i= complete search string
3515 set the isearch history size, the default is only =16=
3516 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3517   (setq history-length 5000)
3518   (setq regexp-search-ring-max 1000)
3519   (setq search-ring-max 1000)
3520
3521   ;; when search a word or a symbol , also add the word into regexp-search-ring
3522   (defadvice isearch-update-ring (after sd/isearch-update-ring (string &optional regexp) activate)
3523     "Add search-ring to regexp-search-ring"
3524     (unless regexp
3525       (add-to-history 'regexp-search-ring string regexp-search-ring-max)))
3526 #+END_SRC
3527
3528 *** =M-s= prefix
3529 use the prefix =M-s= for searching in buffers
3530 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3531   (defun sd/make-keymap (key bindings)
3532     (setq keymap (make-sparse-keymap))
3533     (dolist (binding bindings)
3534       (define-key keymap (car binding) (cdr binding)))
3535     (global-set-key key keymap))
3536
3537   ;; (sd/make-keymap "\M-s"
3538   ;;                 '(("w" . save-buffer)
3539   ;;                   ;; ("\M-w" . save-buffer)
3540   ;;                   ("e" . revert-buffer)
3541   ;;                   ("s" . isearch-forward-regexp)
3542   ;;                   ("\M-s" . isearch-forward-regexp)
3543   ;;                   ("r" . isearch-backward-regexp)
3544   ;;                   ("." . isearch-forward-symbol-at-point)
3545   ;;                   ("o" . occur)
3546   ;;                   ;; ("h" . highlight-symbol-at-point)
3547   ;;                   ("h" . highlight-symbol)
3548   ;;                   ("m" . highlight-regexp)
3549   ;;                   ("l" . highlight-lines-matching-regexp)
3550   ;;                   ("M" . unhighlight-regexp)
3551   ;;                   ("f" . keyboard-quit)
3552   ;;                   ("q" . keyboard-quit)))
3553 #+END_SRC
3554
3555 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3556   (use-package highlight-symbol
3557     :ensure t)
3558
3559   (defhydra sd/search-replace (:color red :columns nil)
3560     "Search"
3561     ("w" save-buffer "save" :exit t)
3562     ("e" revert-buffer "revert" :exit t)
3563     ("u" undo-tree-visualize "undo" :exit t)
3564     ("s" isearch-forward-regexp "s-search" :exit t)
3565     ("M-s" isearch-forward-regexp "s-search" :exit t)
3566     ("r" isearch-backward-regexp "r-search" :exit t)
3567     ("." isearch-forward-symbol-at-point "search point" :exit t)
3568     ("/" swiper "swiper" :exit t)
3569     ("o" occur "occur" :exit t)
3570     ("h" highlight-symbol "higlight" :exit t)
3571     ("l" highlight-lines-matching-regexp "higlight line" :exit t)
3572     ("m" highlight-regexp "higlight" :exit t)
3573     ("M" unhighlight-regexp "unhiglight" :exit t)
3574     ("q" nil "quit")
3575     ("f" nil))
3576
3577   (global-unset-key (kbd "M-s"))
3578   (global-set-key (kbd "M-s") 'sd/search-replace/body)
3579
3580
3581   ;; search and replace and highlight
3582   (define-key isearch-mode-map (kbd "M-s") 'isearch-repeat-forward)
3583   (define-key isearch-mode-map (kbd "M-r") 'isearch-repeat-backward)
3584   (global-set-key (kbd "s-[") 'highlight-symbol-next)
3585   (global-set-key (kbd "s-]") 'highlight-symbol-prev)
3586   (global-set-key (kbd "s-\\") 'highlight-symbol-query-replace)
3587 #+END_SRC
3588
3589 *** Occur
3590 Occur search key bindings
3591 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3592   (defun sd/occur-keys ()
3593     "My key bindings in occur-mode"
3594     (interactive)
3595     (switch-to-buffer-other-window "*Occur*")
3596     (define-key occur-mode-map (kbd "C-o") nil)
3597     (define-key occur-mode-map (kbd "C-n") (lambda ()
3598                                              (interactive)
3599                                              (occur-next)
3600                                              (occur-mode-goto-occurrence-other-window)
3601                                              (recenter)
3602                                              (other-window 1)))
3603     (define-key occur-mode-map (kbd "C-p") (lambda ()
3604                                              (interactive)
3605                                              (occur-prev)
3606                                              (occur-mode-goto-occurrence-other-window)
3607                                              (recenter)
3608                                              (other-window 1))))
3609
3610   (add-hook 'occur-hook #'sd/occur-keys)
3611
3612   (use-package color-moccur
3613     :ensure t
3614     :commands (isearch-moccur isearch-all)
3615     :init
3616     (setq isearch-lazy-highlight t)
3617     :config
3618     (use-package moccur-edit))
3619 #+END_SRC
3620
3621
3622 ** Expand region map
3623 *** Install =expand-region=
3624 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3625   (use-package expand-region
3626     :ensure t
3627     :config
3628     ;; (global-set-key (kbd "C-=") 'er/expand-region)
3629     )
3630 #+END_SRC
3631
3632 *** Add a =hydra= map for =expand-region= operations
3633 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3634   (defun sd/mark-line ()
3635     "Mark current line without whitespace beginning"
3636     (interactive)
3637     (back-to-indentation)
3638     (set-mark (line-end-position)))
3639
3640   (defhydra sd/expand-selected (:color red :columns nil
3641                                        :post (deactivate-mark)
3642                                        )
3643     "Selected"
3644     ;; select
3645     ;; ("e"  er/expand-region "+")
3646     ("SPC" er/expand-region "+")
3647     ;; ("c"  er/contract-region "-")
3648     ("S-SPC" er/contract-region "-")
3649     ("r" (lambda ()
3650            (interactive)
3651            (er/contract-region 0))
3652      "reset")
3653
3654     ("i'" er/mark-inside-quotes "in")
3655     ("i\"" er/mark-inside-quotes nil)
3656     ("o'" er/mark-outside-quotes "out")
3657     ("o\"" er/mark-outside-quotes nil)
3658
3659     ("i{" er/mark-inside-pairs nil)
3660     ("i(" er/mark-inside-pairs nil)
3661     ("o{" er/mark-inside-pairs nil)
3662     ("o(" er/mark-inside-pairs nil)
3663
3664     ("p" er/mark-paragraph "paragraph")
3665
3666     ("l" sd/mark-line "line")
3667     ("u" er/mark-url "url")
3668     ("f" er/mark-defun "fun")
3669     ("n" er/mark-next-accessor "next")
3670
3671     ("x" exchange-point-and-mark "exchange")
3672
3673     ;; Search
3674     ;; higlight
3675
3676     ;; exit
3677     ("d" kill-region "delete" :exit t)
3678
3679     ("y" kill-ring-save "yank" :exit t)
3680     ("M-SPC" nil "quit" :exit t)
3681     ;; ("C-SPC" "quit" :exit t)
3682     ("q" deactivate-mark "quit" :exit t))
3683
3684   (global-set-key (kbd "M-SPC") (lambda ()
3685                                   (interactive)
3686                                   (set-mark-command nil)
3687                                   ;; (er/expand-region 1)
3688                                   (er/mark-word)
3689                                   (sd/expand-selected/body)))
3690 #+END_SRC
3691
3692 *** TODO make expand-region hydra work with lispy selected
3693
3694 * Key Bindings
3695 Here are some global key bindings for basic editting
3696 ** Global key bingding
3697 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3698   (global-set-key (kbd "C-h") 'delete-backward-char)
3699   (global-set-key (kbd "s-m") 'man)
3700   (global-set-key (kbd "<s-return>") 'toggle-frame-fullscreen)
3701   ;; project operation
3702   (global-set-key (kbd "s-p") 'projectile-switch-open-project)
3703   (global-set-key (kbd "s-f") 'projectile-find-file)
3704
3705   ;; (global-set-key (kbd "s-=") 'increase-emacs-font-size)
3706   ;; (global-set-key (kbd "s--") 'decrease-emacs-font-size)
3707   (global-set-key (kbd "s-=") 'text-scale-increase)
3708   (global-set-key (kbd "s--") 'text-scale-decrease)
3709   (global-unset-key (kbd "s-n"))
3710 #+END_SRC
3711
3712 Refer [[https://github.com/fnwiya/dotfiles/blob/c9ca79f1b22c919d9f4c3a0f944ba8281255a594/setup/.emacs.d/loader-init/_90-kill-region-or-backward-kill-word.el][kill-region-or-backward-kill-word]]
3713
3714 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3715   (require 'utilities)
3716   (global-set-key (kbd "C-w") 'sd/kill-region-or-backward-kill-word)
3717 #+END_SRC
3718
3719
3720 ** Esc in minibuffer
3721 Use =ESC= to exit minibuffer. Also I map =Super-h= the same as =C-g=
3722 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3723   (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
3724   (define-key minibuffer-local-map [escape]  'keyboard-escape-quit)
3725   (define-key minibuffer-local-ns-map [escape]  'keyboard-escape-quit)
3726   (define-key minibuffer-local-isearch-map [escape]  'keyboard-escape-quit)
3727   (define-key minibuffer-local-completion-map [escape]  'keyboard-escape-quit)
3728   (define-key minibuffer-local-must-match-map [escape]  'keyboard-escape-quit)
3729   (define-key minibuffer-local-must-match-filename-map [escape]  'keyboard-escape-quit)
3730   (define-key minibuffer-local-filename-completion-map [escape]  'keyboard-escape-quit)
3731   (define-key minibuffer-local-filename-must-match-map [escape]  'keyboard-escape-quit)
3732
3733   ;; Also map s-h same as C-g
3734   (define-key minibuffer-local-map (kbd "s-h") 'keyboard-escape-quit)
3735 #+END_SRC
3736
3737 * Developing
3738 ** perspeen
3739 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3740   ;; (use-package perspeen
3741   ;;   :ensure t
3742   ;;   :init
3743   ;;   (setq perspeen-use-tab nil)
3744   ;;   :config
3745   ;;   (perspeen-mode))
3746
3747   (el-get-bundle seudut/perspeen
3748     :features perspeen
3749     (setq perspeen-use-tab nil)
3750     ;; (perspeen-mode)
3751     )
3752
3753   ;; super-i to switch to ith workspace
3754
3755   (defmacro sd/define-keys (map key func &rest args)
3756     "A macro to define multi keys "
3757     `(define-key ,map ,key (lambda () (interactive) (,func ,@args))))
3758
3759
3760   (with-eval-after-load "perspeen"
3761     (dotimes (ii 9)
3762       (sd/define-keys perspeen-mode-map (kbd (concat "s-" (number-to-string (+ ii 1))))
3763                       perspeen-goto-ws (+ ii 1)))
3764     (define-key perspeen-mode-map (kbd "s-c") 'perspeen-create-ws)
3765     (define-key perspeen-mode-map (kbd "s-n") 'perspeen-next-ws)
3766     (define-key perspeen-mode-map (kbd "s-p") 'perspeen-previous-ws)
3767     (define-key perspeen-mode-map (kbd "s-'") 'perspeen-last-ws)
3768     (define-key perspeen-mode-map (kbd "s-t") 'perspeen-tab-create-tab)
3769     (define-key perspeen-mode-map (kbd "s-t") 'perspeen-tab-create-tab))
3770 #+END_SRC
3771 * Evil Mode
3772 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
3773   (org-babel-load-file "~/.emacs.d/emacs-evil.org")
3774 #+END_SRC
3775
3776 * Footnotes
3777
3778 [fn:1] [[http://coldnew.github.io/blog/2013/11-16_d2f3a/][解決 org-mode θ‘¨ζ Όε…§δΈ­θ‹±ζ–‡ε°ι½Šηš„ε•ι‘Œ]] 
3779
3780
3781
3782
3783