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