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