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