2626e492361f3ed7aefff0cc32b13408b8f11675
[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
12 Set system PATH and emacs exec path
13
14 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
15
16   (setenv "PATH" (concat (getenv "PATH")
17                          ":" "/usr/local/bin"
18                          ":" "/Library/TeX/texbin"))
19   (setq exec-path (append exec-path '("/usr/local/bin")))
20   (setq exec-path (append exec-path '("/Library/TeX/texbin/")))
21
22 #+END_SRC
23
24 Set the emacs load path
25
26 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
27   ;; (add-to-list 'load-path "~/.emacs.d/elisp")
28 #+END_SRC
29
30 ** Package Initialization
31
32 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
33
34   (require 'package)
35
36   (setq package-archives '(("mepla" . "http://melpa.milkbox.net/packages/")
37                            ("gnu" . "http://elpa.gnu.org/packages/")
38                            ("org" . "http://orgmode.org/elpa/")))
39
40   (package-initialize)
41
42 #+END_SRC       
43
44 ** General Setting
45 *** scroll bar, tool-bar and menu-bar
46 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
47   (scroll-bar-mode 0)
48   (tool-bar-mode 0)
49   (menu-bar-mode 1)
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 #+END_SRC
59
60 *** Custom file 
61 #+BEGIN_SRC emacs-lisp :tangle yes :results silent 
62   (setq custom-file "~/.emacs.d/custom.el")
63   (if (file-exists-p custom-file)
64       (load custom-file))
65 #+END_SRC
66
67 *** Switch the focus to help window when it appears
68 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
69   (setq help-window-select t)
70 #+END_SRC
71
72 *** Set default window size
73 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
74   (setq initial-frame-alist
75         '((width . 120)
76           (height . 50)))
77
78   ;; (setq-default indicate-empty-lines t)
79 #+END_SRC
80
81 *** Stop auto save
82 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
83   (setq auto-save-default nil)
84
85   ;; restore last session
86   ;; (desktop-save-mode t)
87 #+END_SRC
88
89 *** temp folder
90 Make a temp directory for all cache/history files
91 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
92   (defconst sd-temp-directory
93     (file-name-as-directory "~/.emacs.d/temp"))
94
95   (unless (file-exists-p sd-temp-directory)
96     (mkdir sd-temp-directory))
97 #+END_SRC
98
99 *** Save minibuffer history
100 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
101   (setq savehist-file (concat sd-temp-directory "history"))
102   (setq history-length 1000)
103   (setq savehist-additional-variables '(kill-ring search-ring regexp-search-ring))
104   (savehist-mode 1)
105
106   ;; set temp file path for recentf and auto-save
107   (setq recentf-save-file (concat sd-temp-directory "recentf"))
108   (setq auto-save-list-file-prefix (concat sd-temp-directory "auto-save-list/.saves-"))
109 #+END_SRC
110
111 * Package Management Tools
112 ** paradox
113 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
114   (use-package paradox
115     :ensure t)
116 #+END_SRC
117
118 ** Use-package
119 Using [[https://github.com/jwiegley/use-package][use-package]] to manage emacs packages
120 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
121   (unless (package-installed-p 'use-package)
122     (package-refresh-contents)
123     (package-install 'use-package))
124
125   (require 'use-package)
126 #+END_SRC
127
128 ** El-get
129 [[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. 
130 Check out [[http://tapoueh.org/emacs/el-get.html][el-get]].
131 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
132   (use-package el-get
133     :ensure t
134     :init
135     (add-to-list 'load-path "~/.emacs.d/el-get"))
136 #+END_SRC
137
138 * Color and Fonts Settings
139
140 ** highlight current line
141
142 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
143
144   (global-hl-line-mode)
145
146 #+END_SRC
147
148 ** Smart Comments
149
150 [[https://github.com/paldepind/smart-comment][smart-comments]]
151
152 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
153
154   (use-package smart-comment
155     :ensure t
156     :bind ("M-;" . smart-conmment))
157
158 #+END_SRC
159
160 ** Font Setting
161
162 syntax highlighting
163
164 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
165
166   (global-font-lock-mode 1)
167
168 #+END_SRC
169
170 [[https://github.com/i-tu/Hasklig][Hasklig]] and Source Code Pro, defined fonts family
171
172 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
173
174   (if window-system
175       (defvar sd/fixed-font-family
176         (cond ((x-list-fonts "Hasklig")         "Hasklig")
177               ((x-list-fonts "Source Code Pro") "Source Code Pro:weight:light")
178               ((x-list-fonts "Anonymous Pro")   "Anonymous Pro")
179               ((x-list-fonts "M+ 1mn")          "M+ 1mn"))
180         "The fixed width font based on what is installed, `nil' if not defined."))
181
182 #+END_SRC
183
184 Setting the fonts 
185
186 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
187
188   (if window-system
189       (when sd/fixed-font-family
190         (set-frame-font sd/fixed-font-family)
191         (set-face-attribute 'default nil :font sd/fixed-font-family :height 130)
192         (set-face-font 'default sd/fixed-font-family)))
193
194 #+END_SRC
195
196 ** Color Theme
197
198 Loading theme should be after all required loaded, refere [[https://github.com/jwiegley/use-package][:defer]] in =use-package=
199
200 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
201
202   (setq vc-follow-symlinks t)
203
204   (use-package color-theme
205     :ensure t
206     :init (require 'color-theme)
207     :config (use-package color-theme-sanityinc-tomorrow
208               :ensure t
209               :no-require t
210               :config
211               (load-theme 'sanityinc-tomorrow-bright t)))
212
213   ;(eval-after-load 'color-theme
214   ;  (load-theme 'sanityinc-tomorrow-bright t))
215
216 #+END_SRC
217
218 Change the Org-mode colors 
219
220 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
221
222   (defun org-src-color-blocks-light ()
223     "Colors the block headers and footers to make them stand out more for lighter themes"
224     (interactive)
225     (custom-set-faces
226      '(org-block-begin-line
227       ((t (:underline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
228      '(org-block-background
229        ((t (:background "#FFFFEA"))))
230      '(org-block
231        ((t (:background "#FFFFEA"))))
232      '(org-block-end-line
233        ((t (:overline "#A7A6AA" :foreground "#008ED1" :background "#EAEAFF"))))
234
235      '(mode-line-buffer-id ((t (:foreground "#005000" :bold t))))
236      '(which-func ((t (:foreground "#008000"))))))
237
238   (defun org-src-color-blocks-dark ()
239     "Colors the block headers and footers to make them stand out more for dark themes"
240     (interactive)
241     (custom-set-faces
242      '(org-block-begin-line
243        ((t (:foreground "#008ED1" :background "#002E41"))))
244      '(org-block-background
245        ((t (:background "#000000"))))
246      '(org-block
247        ((t (:background "#000000"))))
248      '(org-block-end-line
249        ((t (:foreground "#008ED1" :background "#002E41"))))
250
251      '(mode-line-buffer-id ((t (:foreground "black" :bold t))))
252      '(which-func ((t (:foreground "green"))))))
253
254   (org-src-color-blocks-dark)
255
256 #+END_SRC
257
258 improve color for org-mode
259 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
260   (deftheme ha/org-theme "Sub-theme to beautify org mode")
261
262   (if window-system
263       (defvar sd/variable-font-tuple
264         (cond ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
265               ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
266               ((x-list-fonts "Verdana")         '(:font "Verdana"))
267               ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
268               (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro.")))
269         "My variable width font available to org-mode files and whatnot."))
270
271   (defun sd/org-color ()
272     (let* ((sd/fixed-font-tuple (list :font sd/fixed-font-family))
273            (base-font-color     (face-foreground 'default nil 'default))
274            (background-color    (face-background 'default nil 'default))
275            (primary-color       (face-foreground 'mode-line nil))
276            (secondary-color     (face-background 'secondary-selection nil 'region))
277            (base-height         (face-attribute 'default :height))
278            (headline           `(:inherit default :weight bold :foreground ,base-font-color)))
279       (custom-theme-set-faces 'ha/org-theme
280                               `(org-agenda-structure ((t (:inherit default :height 2.0 :underline nil))))
281                               `(org-verbatim ((t (:inherit 'fixed-pitched :foreground "#aef"))))
282                               `(org-table ((t (:inherit 'fixed-pitched))))
283                               `(org-block ((t (:inherit 'fixed-pitched))))
284                               `(org-block-background ((t (:inherit 'fixed-pitched))))
285                               `(org-block-begin-line ((t (:inherit 'fixed-pitched))))
286                               `(org-block-end-line ((t (:inherit 'fixed-pitched))))
287                               `(org-level-8 ((t (,@headline ,@sd/variable-font-tuple))))
288                               `(org-level-7 ((t (,@headline ,@sd/variable-font-tuple))))
289                               `(org-level-6 ((t (,@headline ,@sd/variable-font-tuple))))
290                               `(org-level-5 ((t (,@headline ,@sd/variable-font-tuple))))
291                               `(org-level-4 ((t (,@headline ,@sd/variable-font-tuple
292                                                             :height ,(round (* 1.1 base-height))))))
293                               `(org-level-3 ((t (,@headline ,@sd/variable-font-tuple
294                                                             :height ,(round (* 1.25 base-height))))))
295                               `(org-level-2 ((t (,@headline ,@sd/variable-font-tuple
296                                                             :height ,(round (* 1.5 base-height))))))
297                               `(org-level-1 ((t (,@headline ,@sd/variable-font-tuple
298                                                             :height ,(round (* 1.75 base-height))))))
299                               `(org-document-title ((t (,@headline ,@sd/variable-font-tuple :height 1.5 :underline nil)))))))
300
301
302 #+END_SRC
303
304 ** Rainbow-delimiter
305
306 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
307
308   (use-package rainbow-delimiters
309     :ensure t
310     :init
311     (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))
312
313 #+END_SRC
314
315 ** page-break-lines
316
317 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
318
319   (use-package page-break-lines
320     :ensure t
321     :config
322     (turn-on-page-break-lines-mode))
323
324 #+END_SRC
325
326 ** rainbow-mode
327
328 Enable rainbow mode in emacs lisp mode
329
330 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
331   (use-package rainbow-mode
332     :ensure t
333   ;  :init
334   ;  (add-hook emacs-lisp-mode-hook 'rainbow-mode)
335     )
336
337 #+END_SRC
338
339 * Mode-line
340
341 ** clean mode line
342
343 clean mode line, Refer to [[https://www.masteringemacs.org/article/hiding-replacing-modeline-strings][Marstering Emacs]]
344
345 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
346   (defvar mode-line-cleaner-alist
347     `((auto-complete-mode . " Î±")
348       (yas/minor-mode . " Ï…")
349       (paredit-mode . " Ï€")
350       (eldoc-mode . "")
351       (abbrev-mode . "")
352       (projectile-mode . "")
353       (ivy-mode . "")
354       (undo-tree-mode . "")
355       ;; default is WK
356       (which-key-mode . "")
357       ;; default is SP
358       (smartparens-mode . "")
359       ;; default is LR
360       (linum-relative-mode . "")
361       ;; default is ARev
362       (auto-revert-mode . "")
363       ;; default is Ind
364       (org-indent-mode . "")
365       ;; default is  Fly
366       (flyspell-mode . "")
367       ;; Major modes
368       (lisp-interaction-mode . "λ")
369       (hi-lock-mode . "")
370       (python-mode . "Py")
371       (emacs-lisp-mode . "EL")
372       (eshell-mode . "ε")
373       (nxhtml-mode . "nx"))
374     "Alist for `clean-mode-line'.
375
376   When you add a new element to the alist, keep in mind that you
377   must pass the correct minor/major mode symbol and a string you
378   want to use in the modeline *in lieu of* the original.")
379
380
381   (defun clean-mode-line ()
382     (interactive)
383     (loop for cleaner in mode-line-cleaner-alist
384           do (let* ((mode (car cleaner))
385                    (mode-str (cdr cleaner))
386                    (old-mode-str (cdr (assq mode minor-mode-alist))))
387                (when old-mode-str
388                    (setcar old-mode-str mode-str))
389                  ;; major mode
390                (when (eq mode major-mode)
391                  (setq mode-name mode-str)))))
392
393
394   (add-hook 'after-change-major-mode-hook 'clean-mode-line)
395 #+END_SRC
396
397 ** Powerline mode
398
399 Install powerline mode [[https://github.com/milkypostman/powerline][powerline]]
400
401 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
402   (use-package powerline
403     :ensure t
404     :config
405     ;; (powerline-center-theme)
406     )
407
408   ;; (use-package smart-mode-line
409   ;;   :ensure t)
410   ;; (use-package smart-mode-line-powerline-theme
411   ;;   :ensure t)
412 #+END_SRC
413
414 Revised powerline-center-theme
415 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
416   (defun sd/powerline-simpler-vc (s)
417     (if s
418         (replace-regexp-in-string "Git[:-]" "" s)
419       s))
420
421   (defface sd/powerline-active1 '((t (:background "yellow" :foreground "black" :inherit mode-line)))
422     "My Powerline face 1 based on powerline-active1."
423     :group 'powerline)
424
425   (defface sd/buffer-modified-active1 '((t (:background "red" :foreground "black" :inherit mode-line)))
426     "My Powerline face 1 based on powerline-active1."
427     :group 'powerline)
428
429   (defface sd/buffer-view-active1 '((t (:background "green" :foreground "black" :inherit mode-line)))
430     "My Powerline face 1 based on powerline-active1."
431     :group 'powerline)
432
433   (defface sd/mode-line-buffer-id
434     '((t (:background "yellow" :foreground "black" :inherit mode-line-buffer-id)))
435     "My powerline mode-line face, based on mode-line-buffer-id"
436     :group 'powerline)
437
438   ;; Don't show buffer modified for scratch and eshell mode
439   (defun sd/buffer-show-modified ()
440     "Dot not show modified indicator for buffers"
441     (interactive)
442     (unless (or (string-match "*scratch*" (buffer-name))
443                 (equal major-mode 'eshell-mode))
444       t))
445
446   (defun sd/powerline-center-theme_revised ()
447     "Setup a mode-line with major and minor modes centered."
448     (interactive)
449     (setq-default mode-line-format
450                   '("%e"
451                     (:eval
452                      (let* ((active (powerline-selected-window-active))
453                             (mode-line-buffer-id (if active 'sd/mode-line-buffer-id 'mode-line-buffer-id-inactive))
454                             (mode-line (if active 'mode-line 'mode-line-inactive))
455                             (my-face1 (if active 'sd/powerline-active1 'powerline-inactive1))
456                             (my-face-buffer-modified (if (and (sd/buffer-show-modified) (buffer-modified-p)) 
457                                                          'sd/buffer-modified-active1
458                                                        (if buffer-read-only 'sd/buffer-view-active1
459                                                          my-face1)))
460                             (face1 (if active 'powerline-active1 'powerline-inactive1))
461                             (face2 (if active 'powerline-active2 'powerline-inactive2))
462                             (separator-left (intern (format "powerline-%s-%s"
463                                                             (powerline-current-separator)
464                                                             (car powerline-default-separator-dir))))
465                             (separator-right (intern (format "powerline-%s-%s"
466                                                              (powerline-current-separator)
467                                                              (cdr powerline-default-separator-dir))))
468                             (lhs (list (powerline-raw "%* " my-face-buffer-modified 'l)
469                                        ;; (powerline-buffer-size mode-line 'l)
470                                        (powerline-buffer-id mode-line-buffer-id 'l)
471                                        (powerline-raw " " my-face1)
472                                        (funcall separator-left my-face1 face1)
473                                        (powerline-narrow face1 'l)
474                                        ;; (powerline-vc face1)
475                                        (sd/powerline-simpler-vc (powerline-vc face1))))
476                             (rhs (list (powerline-raw global-mode-string face1 'r)
477                                        (powerline-raw "%4l" face1 'r)
478                                        (powerline-raw ":" face1)     
479                                        (powerline-raw "%3c" face1 'r)
480                                        (funcall separator-right face1 my-face1)
481                                        ;; (powerline-raw " " my-face1)
482                                        (powerline-raw (format-time-string " %I:%M %p  ") my-face1 'r)
483                                        ;; (powerline-raw "%6p" my-face1 'r)
484                                        ;; (powerline-hud my-face1 face1 )
485                                        ))
486                             (center (list (powerline-raw " " face1)
487                                           (funcall separator-left face1 face2)
488                                           (when (and (boundp 'erc-track-minor-mode) erc-track-minor-mode)
489                                             (powerline-raw erc-modified-channels-object face2 'l))
490                                           (powerline-major-mode face2 'l)
491                                           (powerline-process face2)
492                                           (powerline-raw " :" face2)
493                                           (powerline-minor-modes face2 'l)
494                                           (powerline-raw " " face2)
495                                           (funcall separator-right face2 face1))))
496                        (concat (powerline-render lhs)
497                                (powerline-fill-center face1 (/ (powerline-width center) 2.0))
498                                (powerline-render center)
499                                (powerline-fill face1 (powerline-width rhs))
500                                (powerline-render rhs)))))))
501
502   (sd/powerline-center-theme_revised)
503 #+END_SRC
504
505 Fix the issue in mode line when showing triangle 
506 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
507   (setq ns-use-srgb-colorspace nil)
508 #+END_SRC
509
510 set height in mode line
511 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
512   (set-variable 'powerline-height 14)
513   (set-variable 'powerline-text-scale-factor (/ (float 100) 140))
514   ;; (custom-set-variables
515   ;;  '(powerline-height 14)
516   ;;  '(powerline-text-scale-factor (/ (float 100) 140)))
517   ;; 100/140;0.8
518   (set-face-attribute 'mode-line nil :height 100)
519 #+END_SRC
520
521 * IDO & SMEX
522 ** IDO
523 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
524   (use-package ido
525     :ensure t
526     :init (setq ido-enable-flex-matching t
527                 ido-ignore-extensions t
528                 ido-use-virtual-buffers t
529                 ido-everywhere t)
530     (setq ido-save-directory-list-file (concat sd-temp-directory "ido.last"))
531     :config
532     (ido-mode 1)
533     (ido-everywhere 1)
534     (add-to-list 'completion-ignored-extensions ".pyc"))
535
536   (icomplete-mode t)
537 #+END_SRC
538
539 ** FLX
540 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
541   (use-package flx-ido
542     :ensure t
543     :init (setq ido-enable-flex-matching t
544                 ido-use-faces nil)
545     :config (flx-ido-mode 1))
546 #+END_SRC
547
548 ** IDO-vertically
549 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
550   (use-package ido-vertical-mode
551     :ensure t
552     :init
553     (setq ido-vertical-define-keys 'C-n-C-p-up-and-down)
554     :config
555     (ido-vertical-mode 1))
556 #+END_SRC
557
558 ** SMEX
559 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
560   (use-package smex
561     :ensure t
562     :init
563     (setq smex-save-file (concat sd-temp-directory "smex-items"))
564     (smex-initialize)
565     :bind
566     ("M-x" . smex)
567     ("M-X" . smex-major-mode-commands))
568 #+END_SRC
569
570 ** Ido-ubiquitous
571 Use [[https://github.com/DarwinAwardWinner/ido-ubiquitous][ido-ubiquitous]] for ido everywhere. It makes =describe-function= can also use ido
572 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
573   (use-package ido-ubiquitous
574     :ensure t
575     :init
576     (setq magit-completing-read-function 'magit-ido-completing-read)
577     (setq gnus-completing-read-function 'gnus-ido-completing-read)
578     :config
579     (ido-ubiquitous-mode 1))
580 #+END_SRC
581
582 ** Ido-exit-target
583 [[https://github.com/waymondo/ido-exit-target][ido-exit-target]] let you open file/buffer on =other-windows= when call =ido-switch-buffer=
584 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
585   (use-package ido-exit-target
586     :ensure t
587     :init
588     (mapcar (lambda (map)
589               (define-key map (kbd "C-j") #'ido-exit-target-split-window-right)
590               (define-key map (kbd "C-k") #'ido-exit-target-split-window-below))
591             (list ido-buffer-completion-map
592                   ;; ido-common-completion-map
593                   ido-file-completion-map
594                   ido-file-dir-completion-map)))
595 #+END_SRC
596
597 ** Counsel
598 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
599   (use-package counsel
600     :ensure t
601     :defer t
602     :init
603     (global-set-key (kbd "M-x") 'counsel-M-x)
604     (global-set-key (kbd "C-h f") 'counsel-describe-function)
605     (global-set-key (kbd "C-h v") 'counsel-describe-variable)
606     ;; (set-face-attribute 'ivy-current-match nil :background "Orange" :foreground "black")
607     (define-key read-expression-map (kbd "C-r") 'counsel-expression-history)
608     (global-set-key (kbd "C-c C-r") 'ivy-resume))
609
610
611
612   ;; (global-set-key "\C-s" 'swiper)
613   ;; (global-set-key (kbd "<f6>") 'ivy-resume)
614   ;; ;; (global-set-key (kbd "C-x C-f") 'counsel-find-file)
615   ;; (global-set-key (kbd "<f1> l") 'counsel-load-library)
616   ;; (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
617   ;; (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
618   ;; (global-set-key (kbd "C-c g") 'counsel-git)
619   ;; (global-set-key (kbd "C-c j") 'counsel-git-grep)
620   ;; (global-set-key (kbd "C-c k") 'counsel-ag)
621   ;; (global-set-key (kbd "C-x l") 'counsel-locate)
622   ;; (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
623
624   ;; (set-face-attribute
625   ;;  'ivy-current-match nil
626   ;;  :background "Orange"
627   ;;  :foreground "black")
628
629   ;; ivy-resume
630   ;; (define-key swiper-map (kbd "M-%") 'swiper-aa-replace)
631 #+END_SRC
632
633 ** helm
634 let helm windows split inside current window
635 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
636   (with-eval-after-load 'helm
637     (setq helm-split-window-in-side-p t))
638 #+END_SRC
639
640 * Org-mode Settings
641
642 ** Org-mode Basic setting
643
644 Always indents header, and hide header leading starts so that no need type =#+STATUP: indent= 
645
646 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
647   (use-package org
648     :ensure t
649     :init
650     (setq org-startup-indented t)
651     (setq org-hide-leading-starts t)
652     (setq org-src-fontify-natively t)
653     (setq org-src-tab-acts-natively t)
654     (setq org-confirm-babel-evaluate nil)
655     (setq org-use-speed-commands t)
656     (setq org-completion-use-ido t))
657
658   (org-babel-do-load-languages
659    'org-babel-load-languages
660    '((python . t)
661      (C . t)
662      (perl . t)
663      (calc . t)
664      (latex . t)
665      (java . t)
666      (ruby . t)
667      (lisp . t)
668      (scheme . t)
669      (sh . t)
670      (sqlite . t)
671      (js . t)
672      (gnuplot . t)
673      (ditaa . t)))
674
675   ;; use current window for org source buffer editting
676   (setq org-src-window-setup 'current-window )
677
678   (define-key org-mode-map (kbd "C-'") nil)
679   ;; C-M-i is mapped to imenu globally
680   (define-key org-mode-map (kbd "C-M-i") nil)
681
682   ;; set the ditta.jar path
683   (setq org-ditaa-jar-path "/usr/local/Cellar/ditaa/0.9/libexec/ditaa0_9.jar")
684   (unless 
685       (file-exists-p org-ditaa-jar-path)
686     (error "seudut: ditaa.jar not found at %s " org-ditaa-jar-path))
687 #+END_SRC
688
689 ** Org-bullets
690
691 use [[https://github.com/sabof/org-bullets][org-bullets]] package to show utf-8 charactes
692
693 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
694   (use-package org-bullets
695     :ensure t
696     :init
697     (add-hook 'org-mode-hook
698               (lambda ()
699                 (org-bullets-mode t))))
700
701   (setq org-bullets-bullet-list '("⦿" "✪" "â—‰" "â—‹" "â–º" "â—†"))
702
703   ;; increase font size when enter org-src-mode
704   (add-hook 'org-src-mode-hook (lambda () (text-scale-increase 2)))
705 #+END_SRC
706
707 ** Worf Mode
708
709 [[https://github.com/abo-abo/worf][worf]] mode is an extension of vi-like binding for org-mode. 
710 In =worf-mode=, it is mapping =[=, =]= as =worf-backward= and =worf-forward= in global, wich
711 cause we cannot input =[= and =]=, so here I unset this mappings. And redifined this two to
712 =M-[= and =M-]=. see this [[https://github.com/abo-abo/worf/issues/19#issuecomment-223756599][issue]]
713
714 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
715
716   (use-package worf
717     :ensure t
718     :commands worf-mode
719     :init (add-hook 'org-mode-hook 'worf-mode)
720     ;; :config
721     ;; (define-key worf-mode-map "[" nil)
722     ;; (define-key worf-mode-map "]" nil)
723     ;; (define-key worf-mode-map (kbd "M-[") 'worf-backward)
724     ;; (define-key worf-mode-map (kbd "M-]") 'worf-forward)
725     )
726
727 #+END_SRC
728
729 ** Get Things Done
730
731 Refer to [[http://doc.norang.ca/org-mode.html][Organize Your Life in Plain Text]]
732 *** basic setup
733
734 standard key binding
735
736 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
737   (global-set-key "\C-cl" 'org-store-link)
738   (global-set-key "\C-ca" 'org-agenda)
739   (global-set-key "\C-cb" 'org-iswitchb)
740 #+END_SRC
741
742 *** Plain List 
743
744 Replace the list bullet =-=, =+=,  with =•=, a litter change based [[https://github.com/howardabrams/dot-files/blob/master/emacs-org.org][here]]
745
746 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
747   ;; (use-package org-mode
748   ;;   :init
749   ;;   (font-lock-add-keywords 'org-mode
750   ;;    '(("^ *\\([-+]\\) "
751   ;;           (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "•")))))))
752 #+END_SRC
753  
754 *** Todo Keywords
755
756 refer to [[http://coldnew.github.io/coldnew-emacs/#orgheadline94][fancy todo states]], 
757
758 To track TODO state changes, the =!= is to insert a timetamp, =@= is to insert a note with
759 timestamp for the state change.
760
761 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
762     ;; (setq org-todo-keywords
763     ;;        '((sequence "☛ TODO(t)" "|" "✔ DONE(d)")
764     ;;          (sequence "âš‘ WAITING(w)" "|")
765     ;;          (sequence "|" "✘ CANCELLED(c)")))
766   ; (setq org-todo-keyword-faces
767   ;        (quote ("TODO" .  (:foreground "red" :weight bold))
768   ;               ("NEXT" .  (:foreground "blue" :weight bold))
769   ;               ("WAITING" . (:foreground "forest green" :weight bold))
770   ;               ("DONE" .  (:foreground "magenta" :weight bold))
771   ;               ("CANCELLED" . (:foreground "forest green" :weight bold))))
772
773
774   (setq org-todo-keywords
775         (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d!)")
776                 ;; (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING")
777                 (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" ))))
778
779   (setq org-todo-keyword-faces
780         (quote (("TODO" :foreground "red" :weight bold)
781                 ("NEXT" :foreground "blue" :weight bold)
782                 ("DONE" :foreground "forest green" :weight bold)
783                 ("WAITING" :foreground "orange" :weight bold)
784                 ("HOLD" :foreground "magenta" :weight bold)
785                 ("CANCELLED" :foreground "forest green" :weight bold)
786                 ;; ("MEETING" :foreground "forest green" :weight bold)
787                 ;; ("PHONE" :foreground "forest green" :weight bold)
788                 )))
789 #+END_SRC
790
791 Fast todo selections
792
793 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
794   (setq org-use-fast-todo-selection t)
795   (setq org-treat-S-cursor-todo-selection-as-state-change nil)
796 #+END_SRC
797
798 TODO state triggers and tags, [[http://doc.norang.ca/org-mode.html][Organize Your Life in Plain Text]]
799
800 - Moving a task to =CANCELLED=, adds a =CANCELLED= tag
801 - Moving a task to =WAITING=, adds a =WAITING= tag
802 - Moving a task to =HOLD=, add =HOLD= tags
803 - Moving a task to =DONE=, remove =WAITING=, =HOLD= tag
804 - Moving a task to =NEXT=, remove all waiting/hold/cancelled tags
805
806 This tags are used to filter tasks in agenda views
807 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
808   (setq org-todo-state-tags-triggers
809         (quote (("CANCELLED" ("CANCELLED" . t))
810                 ("WAITING" ("WAITING" . t))
811                 ("HOLD" ("WAITING") ("HOLD" . t))
812                 (done ("WAITING") ("HOLD"))
813                 ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
814                 ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
815                 ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))
816 #+END_SRC
817
818 Logging Stuff 
819 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
820   ;; log time when task done
821   ;; (setq org-log-done (quote time))
822   ;; save clocking into to LOGBOOK
823   (setq org-clock-into-drawer t)
824   ;; save state change notes and time stamp into LOGBOOK drawer
825   (setq org-log-into-drawer t)
826   (setq org-clock-into-drawer "CLOCK")
827 #+END_SRC
828
829 *** Tags
830 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
831   (setq org-tag-alist (quote ((:startgroup)
832                               ("@office" . ?e)
833                               ("@home" . ?h)
834                               (:endgroup)
835                               ("WAITING" . ?w)
836                               ("HOLD" . ?h)
837                               ("CANCELLED" . ?c))))
838
839   ;; Allow setting single tags without the menu
840   (setq org-fast-tag-selection-single-key (quote expert))
841 #+END_SRC
842
843 *** Capture - Refile - Archive
844
845 Capture lets you quickly store notes with little interruption of your work flow.
846
847 **** Capture Templates
848
849 When a new taks needs to be added, categorize it as 
850
851 All captured file which need next actions are stored in =refile.org=, 
852 - A new task / note (t) =refile.org=
853 - A work task in office =office.org=
854 - A jourenl =diary.org=
855 - A new habit (h) =refile.org=
856
857 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
858   (setq org-directory "~/org")
859   (setq org-default-notes-file "~/org/refile.org")
860   (setq sd/org-diary-file "~/org/diary.org")
861
862   (global-set-key (kbd "C-c c") 'org-capture)
863
864   (setq org-capture-templates
865         (quote (("t" "Todo" entry (file org-default-notes-file)
866                  "* TODO %?\n:LOGBOOK:\n- Added: %U\t\tAt: %a\n:END:")
867                 ("n" "Note" entry (file org-default-notes-file)
868                  "* %? :NOTE:\n:LOGBOOK:\n- Added: %U\t\tAt: %a\n:END:")
869                 ("j" "Journal" entry (file+datetree sd/org-diary-file)
870                  "* %?\n:LOGBOOK:\n:END:" :clock-in t :clock-resume t)
871                 ("h" "Habit" entry (file org-default-notes-file)
872                  "* 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 "))))
873 #+END_SRC
874
875 **** Refiling Tasks
876
877 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
878   (setq org-refile-targets (quote (;; (nil :maxlevel . 9)
879                                    (org-agenda-files :maxlevel . 9))))
880
881   (setq org-refile-use-outline-path t)
882
883   (setq org-refile-allow-creating-parent-nodes (quote confirm))
884 #+END_SRC
885
886 *** Agenda Setup
887 Setting agenda files and the agenda view
888 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
889   (setq org-agenda-files (quote ("~/org/gtd.org"
890                                  "~/org/work.org")))
891
892   ;; only show today's tasks in agenda view
893   (setq org-agenda-span 'day)
894   ;; Use current windows for agenda view
895   (setq org-agenda-window-setup 'current-window)
896
897   ;; show all feature entries for repeating tasks,
898   ;; this is already setting by default
899   (setq org-agenda-repeating-timestamp-show-all t)
900
901   ;; Show all agenda dates - even if they are empty
902   (setq org-agenda-show-all-dates t)
903 #+END_SRC
904
905 ** Export PDF
906
907 Install MacTex-basic and some tex packages
908
909 #+BEGIN_SRC bash 
910
911   sudo tlmgr update --self
912
913   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
914
915 #+END_SRC
916
917 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
918   ;; ;; allow for export=>beamer by placing
919
920   ;; http://emacs-fu.blogspot.com/2011/04/nice-looking-pdfs-with-org-mode-and.html
921   ;; #+LaTeX_CLASS: beamer in org files
922   (unless (boundp 'org-export-latex-classes)
923     (setq org-export-latex-classes nil))
924   (add-to-list 'org-export-latex-classes
925     ;; beamer class, for presentations
926     '("beamer"
927        "\\documentclass[11pt]{beamer}\n
928         \\mode<{{{beamermode}}}>\n
929         \\usetheme{{{{beamertheme}}}}\n
930         \\usecolortheme{{{{beamercolortheme}}}}\n
931         \\beamertemplateballitem\n
932         \\setbeameroption{show notes}
933         \\usepackage[utf8]{inputenc}\n
934         \\usepackage[T1]{fontenc}\n
935         \\usepackage{hyperref}\n
936         \\usepackage{color}
937         \\usepackage{listings}
938         \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
939     frame=single,
940     basicstyle=\\small,
941     showspaces=false,showstringspaces=false,
942     showtabs=false,
943     keywordstyle=\\color{blue}\\bfseries,
944     commentstyle=\\color{red},
945     }\n
946         \\usepackage{verbatim}\n
947         \\institute{{{{beamerinstitute}}}}\n          
948          \\subject{{{{beamersubject}}}}\n"
949
950        ("\\section{%s}" . "\\section*{%s}")
951  
952        ("\\begin{frame}[fragile]\\frametitle{%s}"
953          "\\end{frame}"
954          "\\begin{frame}[fragile]\\frametitle{%s}"
955          "\\end{frame}")))
956
957     ;; letter class, for formal letters
958
959     (add-to-list 'org-export-latex-classes
960
961     '("letter"
962        "\\documentclass[11pt]{letter}\n
963         \\usepackage[utf8]{inputenc}\n
964         \\usepackage[T1]{fontenc}\n
965         \\usepackage{color}"
966  
967        ("\\section{%s}" . "\\section*{%s}")
968        ("\\subsection{%s}" . "\\subsection*{%s}")
969        ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
970        ("\\paragraph{%s}" . "\\paragraph*{%s}")
971        ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
972
973
974   (require 'ox-md)
975   (require 'ox-beamer)
976
977   (setq org-latex-pdf-process
978         '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
979           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
980           "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
981
982   (setq TeX-parse-self t)
983
984   (setq TeX-PDF-mode t)
985   (add-hook 'LaTeX-mode-hook
986             (lambda ()
987               (LaTeX-math-mode)
988               (setq TeX-master t)))
989
990 #+END_SRC
991
992 ** others
993
994 extend org-mode's easy templates, refer to [[http://coldnew.github.io/coldnew-emacs/#orgheadline94][Extend org-modes' esay templates]]
995
996 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
997
998   (add-to-list 'org-structure-template-alist
999                '("E" "#+BEGIN_SRC emacs-lisp :tangle yes :results silent\n?\n#+END_SRC"))
1000   (add-to-list 'org-structure-template-alist
1001                '("S" "#+BEGIN_SRC sh\n?\n#+END_SRC"))
1002   (add-to-list 'org-structure-template-alist
1003                '("p" "#+BEGIN_SRC plantuml :file uml.png \n?\n#+END_SRC"))
1004
1005 #+END_SRC
1006
1007 * Magit
1008 [[https://github.com/magit/magit][Magit]] is a very cool git interface on Emacs.
1009 and Defined keys, using vi keybindings, Refer abo-abo's setting [[https://github.com/abo-abo/oremacs/blob/c5cafdcebc88afe9e73cc8bd40c49b70675509c7/modes/ora-nextmagit.el][here]]
1010 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1011   (use-package magit
1012     :ensure t
1013     :init
1014     ;; don't ask me to confirm the unsaved change 
1015     (setq magit-save-repository-buffers nil)
1016     :commands magit-status magit-blame
1017     :config
1018     (dolist (map (list magit-status-mode-map
1019                        magit-log-mode-map
1020                        magit-diff-mode-map
1021                        magit-staged-section-map))
1022       (define-key map "j" 'magit-section-forward)
1023       (define-key map "k" 'magit-section-backward)
1024       (define-key map "D" 'magit-discard)
1025       (define-key map "O" 'magit-discard-file)
1026       (define-key map "n" nil)
1027       (define-key map "p" nil)
1028       (define-key map "v" 'recenter-top-bottom)
1029       (define-key map "i" 'magit-section-toggle)))
1030 #+END_SRC
1031
1032 * Eshell
1033 ** Eshell alias
1034 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1035   (defalias 'e 'find-file)
1036   (defalias 'ff 'find-file)
1037   (defalias 'ee 'find-files)
1038 #+END_SRC
1039
1040 ** eshell temp directory
1041 set default eshell history folder
1042 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1043   (setq eshell-directory-name (concat  sd-temp-directory "eshell"))
1044 #+END_SRC
1045
1046 ** Eshell erase buffer
1047 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1048   (defun sd/eshell-clear-buffer ()
1049     "Clear eshell buffer"
1050     (interactive)
1051     (let ((inhibit-read-only t))
1052       (erase-buffer)
1053       (eshell-send-input)))
1054
1055    (add-hook 'eshell-mode-hook (lambda ()
1056                                 (local-set-key (kbd "C-l") 'sd/eshell-clear-buffer)))
1057 #+END_SRC
1058
1059 ** Toggle Eshell
1060 Toggle an eshell in split window below, refer [[http://www.howardism.org/Technical/Emacs/eshell-fun.html][eshell-here]]
1061 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1062   (defun sd/window-has-eshell ()
1063     "Check if current windows list has a eshell buffer, and return the window"
1064     (interactive)
1065     (let ((ret nil))
1066       (walk-windows (lambda (window)
1067                       (if (equal (with-current-buffer (window-buffer window) major-mode)
1068                                  'eshell-mode)
1069                           (setq ret window)))
1070                     nil nil)
1071       ret))
1072
1073   (defun sd/toggle-eshell-here ()
1074     "Toggle a eshell buffer vertically"
1075     (interactive)
1076     (if (sd/window-has-eshell)
1077         (if (equal major-mode 'eshell-mode)
1078             (progn
1079               (if (equal (length (window-list)) 1)
1080                   (mode-line-other-buffer)
1081                 (delete-window)))
1082           (select-window (sd/window-has-eshell)))
1083       (progn
1084         (let ((dir default-directory))
1085         
1086           (split-window-vertically (- (/ (window-total-height) 3)))
1087           (other-window 1)
1088           (unless (and (boundp 'eshell-buffer-name) (get-buffer eshell-buffer-name))
1089             (eshell))
1090           (switch-to-buffer eshell-buffer-name)
1091           (goto-char (point-max))
1092           (eshell-kill-input)
1093           (insert (format "cd %s" dir))
1094           (eshell-send-input)))))
1095
1096   ;; (global-unset-key (kbd "M-`"))
1097   (global-set-key (kbd "s-e") 'sd/toggle-eshell-here)
1098 #+END_SRC
1099
1100 ** TODO smart display
1101 * Misc Settings
1102
1103 ** [[https://github.com/abo-abo/hydra][Hydra]]
1104 *** hydra install
1105 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1106   (use-package hydra
1107     :ensure t)
1108   ;; disable new line in minibuffer when hint hydra
1109   (setq hydra-lv nil)
1110 #+END_SRC
1111
1112 *** Windmove Splitter
1113
1114 Refer [[https://github.com/abo-abo/hydra/blob/master/hydra-examples.el][hydra-example]], to enlarge or shrink the windows splitter
1115
1116 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1117
1118   (defun hydra-move-splitter-left (arg)
1119     "Move window splitter left."
1120     (interactive "p")
1121     (if (let ((windmove-wrap-around))
1122           (windmove-find-other-window 'right))
1123         (shrink-window-horizontally arg)
1124       (enlarge-window-horizontally arg)))
1125
1126   (defun hydra-move-splitter-right (arg)
1127     "Move window splitter right."
1128     (interactive "p")
1129     (if (let ((windmove-wrap-around))
1130           (windmove-find-other-window 'right))
1131         (enlarge-window-horizontally arg)
1132       (shrink-window-horizontally arg)))
1133
1134   (defun hydra-move-splitter-up (arg)
1135     "Move window splitter up."
1136     (interactive "p")
1137     (if (let ((windmove-wrap-around))
1138           (windmove-find-other-window 'up))
1139         (enlarge-window arg)
1140       (shrink-window arg)))
1141
1142   (defun hydra-move-splitter-down (arg)
1143     "Move window splitter down."
1144     (interactive "p")
1145     (if (let ((windmove-wrap-around))
1146           (windmove-find-other-window 'up))
1147         (shrink-window arg)
1148       (enlarge-window arg)))
1149
1150 #+END_SRC
1151
1152 *** hydra misc
1153 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1154   (defhydra sd/hydra-misc (:color red :columns nil)
1155     "Misc"
1156     ("e" eshell "eshell" :exit t)
1157     ("p" (lambda ()
1158            (interactive)
1159            (if (not (eq nil (get-buffer "*Packages*")))
1160                (switch-to-buffer "*Packages*")
1161              (package-list-packages)))
1162      "list-package" :exit t)
1163     ("g" magit-status "git-status" :exit t)
1164     ("'" mode-line-other-buffer "last buffer" :exit t)
1165     ("C-'" mode-line-other-buffer "last buffer" :exit t)
1166     ("m" man "man" :exit t)
1167     ("d" dired-jump "dired" :exit t)
1168     ("b" ibuffer "ibuffer" :exit t)
1169     ("q" nil "quit")
1170     ("f" nil "quit"))
1171
1172   (global-set-key (kbd "C-'") 'sd/hydra-misc/body)
1173 #+END_SRC
1174
1175 *** hydra launcher
1176 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1177   (defhydra sd/hydra-launcher (:color blue :columns 2)
1178     "Launch"
1179     ("e" emms "emms" :exit t)
1180     ("q" nil "cancel"))
1181 #+END_SRC
1182
1183 ** Line Number
1184
1185 Enable linum mode on programming modes
1186
1187 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1188   (add-hook 'prog-mode-hook 'linum-mode)
1189   ;; (add-hook 'prog-mode-hook (lambda ()
1190   ;;                             (setq-default indicate-empty-lines t)))
1191 #+END_SRC
1192
1193 Fix the font size of line number
1194
1195 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1196
1197   (defun fix-linum-size ()
1198        (interactive)
1199        (set-face-attribute 'linum nil :height 110))
1200
1201   (add-hook 'linum-mode-hook 'fix-linum-size)
1202
1203 #+END_SRC
1204
1205 I like [[https://github.com/coldnew/linum-relative][linum-relative]], just like the =set relativenumber= on =vim=
1206
1207 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1208
1209   (use-package linum-relative
1210     :ensure t
1211     :config
1212     (defun linum-new-mode ()
1213       "If line numbers aren't displayed, then display them.
1214   Otherwise, toggle between absolute and relative numbers."
1215       (interactive)
1216       (if linum-mode
1217           (linum-relative-toggle)
1218         (linum-mode 1)))
1219
1220     :bind
1221     ("A-k" . linum-new-mode))
1222
1223   ;; auto enable linum-new-mode in programming modes
1224   (add-hook 'prog-mode-hook 'linum-relative-mode)
1225
1226 #+END_SRC
1227
1228 ** Save File Position
1229
1230 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1231
1232   (require 'saveplace)
1233   (setq-default save-place t)
1234   (setq save-place-forget-unreadable-files t)
1235   (setq save-place-skip-check-regexp "\\`/\\(?:cdrom\\|floppy\\|mnt\\|/[0-9]\\|\\(?:[^@/:]*@\\)?[^@/:]*[^@/:.]:\\)")
1236
1237 #+END_SRC
1238
1239 ** Multi-term
1240
1241 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1242
1243   (use-package multi-term
1244     :ensure t)
1245
1246 #+END_SRC
1247
1248 ** ace-link
1249
1250 [[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
1251 Type =o= to go to the link
1252
1253 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1254
1255   (use-package ace-link
1256     :ensure t
1257     :init
1258     (ace-link-setup-default))
1259
1260 #+END_SRC
1261
1262 ** Emux
1263
1264 [[https://github.com/re5et/emux][emux]] is 
1265
1266 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1267
1268   (el-get-bundle re5et/emux)
1269
1270 #+END_SRC
1271
1272 ** Smart Parens
1273
1274 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1275
1276   (use-package smartparens
1277     :ensure t
1278     :config
1279     (progn
1280       (require 'smartparens-config)
1281       (add-hook 'prog-mode-hook 'smartparens-mode)))
1282
1283 #+END_SRC
1284
1285 ** Ace-Windows
1286
1287 [[https://github.com/abo-abo/ace-window][ace-window]] 
1288
1289 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1290
1291   (use-package ace-window
1292     :ensure t
1293     :defer t
1294   ;  :init
1295   ;  (global-set-key (kbd "M-o") 'ace-window)
1296     :config
1297     (setq aw-keys '(?a ?s ?d ?f ?j ?k ?l)))
1298
1299 #+END_SRC
1300
1301 ** Which key
1302
1303 [[https://github.com/justbur/emacs-which-key][which-key]] show the key bindings 
1304
1305 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1306
1307   (use-package which-key
1308     :ensure t
1309     :config
1310     (which-key-mode))
1311
1312 #+END_SRC
1313
1314 ** Tabbar
1315
1316 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1317
1318   ;; (use-package tabbar-ruler
1319   ;;   :ensure t
1320   ;;   :init
1321   ;;   (setq tabbar-ruler-global-tabbar t)
1322   ;;   (setq tabbar-ruler-global-ruler t)
1323   ;;   (setq tabbar-ruler-popu-menu t)
1324   ;;   (setq tabbar-ruler-popu-toolbar t)
1325   ;;   (setq tabbar-use-images t)
1326   ;;   :config
1327   ;;   (tabbar-ruler-group-by-projectile-project)
1328   ;;   (global-set-key (kbd "s-1") 'tabbar-forward-group)
1329   ;;   (global-set-key (kbd "s-2") 'tabbar-ruler-forward))
1330
1331 #+END_SRC
1332
1333 ** View only for some directory
1334 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]]
1335 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1336   (dir-locals-set-class-variables
1337    'emacs
1338    '((nil . ((buffer-read-only . t)
1339              (show-trailing-whitespace . nil)
1340              (tab-width . 8)
1341              (eval . (whitespace-mode -1))))))
1342
1343   ;; (dir-locals-set-directory-class (expand-file-name "/usr/local/share/emacs") 'emacs)
1344   (dir-locals-set-directory-class "/usr/local/Cellar/emacs" 'emacs)
1345   ;; (dir-locals-set-directory-class "~/.emacs.d/elpa" 'emacs)
1346   (dir-locals-set-directory-class "~/dotfiles/emacs.d/elpa" 'emacs)
1347   (dir-locals-set-directory-class "~/dotfiles/emacs.d/el-get" 'emacs)
1348 #+END_SRC
1349
1350 ** Info plus
1351 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1352   (el-get-bundle info+
1353     :url "https://raw.githubusercontent.com/emacsmirror/emacswiki.org/master/info+.el"
1354     (require 'info+))
1355 #+END_SRC
1356
1357 ** TODO bookmark
1358
1359 ** TODO Calendar
1360 ** advice info
1361 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1362   (defun sd/info-mode ()
1363     (interactive)
1364     (unless (equal major-mode 'Info-mode)
1365       (unless (> (length (window-list)) 1)
1366         (split-window-right))
1367       (other-window 1)
1368       ;; (info)
1369       ))
1370
1371   ;; (global-set-key (kbd "C-h i") 'sd/info-mode)
1372
1373   ;; open Info buffer in other window instead of current window
1374   (defadvice info (before my-info (&optional file buf) activate)
1375     (sd/info-mode))
1376
1377   (defadvice Info-exit (after my-info-exit activate)
1378     (sd/delete-current-window))
1379 #+END_SRC
1380
1381 ** TODO Man mode
1382 Color for Man-mode
1383
1384 ** TODO swiper to occur
1385
1386 ** TODO UTF8
1387 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1388   ;; (set-language-environment "UTF-8")
1389   ;; (set-default-coding-systems 'utf-8)
1390 #+END_SRC
1391
1392 ** Demo It
1393 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1394   ;; (el-get-bundle howardabrams/demo-it)
1395
1396   (use-package org-tree-slide
1397     :ensure t)
1398
1399   ;; (use-package yasnippet
1400   ;;   :ensure t)
1401 #+END_SRC
1402
1403 ** Presentation
1404 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1405   (use-package org-tree-slide
1406     :ensure
1407     :config
1408     ;; (define-key org-mode-map "\C-ccp" 'org-tree-slide-mode)
1409     (define-key org-tree-slide-mode-map (kbd "<ESC>") 'org-tree-slide-content)
1410     (define-key org-tree-slide-mode-map (kbd "<SPACE>") 'org-tree-slide-move-next-tree)
1411     (define-key org-tree-slide-mode-map [escape] 'org-tree-slide-move-previous-tree))
1412 #+END_SRC
1413
1414 * dired
1415 ** Dired config
1416 =C-o= is defined as a global key for window operation, here unset it in dired mode
1417 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1418   (use-package dired
1419     :config
1420     (require 'dired-x)
1421     (setq dired-omit-mode t)
1422     (setq dired-omit-files (concat dired-omit-files "\\|^\\..+$"))
1423     (add-hook 'dired-mode-hook (lambda ()
1424                                  (define-key dired-mode-map (kbd "C-o") nil)
1425                                  (define-key dired-mode-map (kbd "H") 'dired-omit-mode)
1426                                  (define-key dired-mode-map (kbd "DEL") (lambda () (interactive) (find-alternate-file "..")))
1427                                  (dired-omit-mode))))
1428 #+END_SRC
1429
1430 ** Dired+
1431 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1432   (use-package dired+
1433     :ensure t
1434     :init
1435     (setq diredp-hide-details-initially-flag nil)
1436     :config
1437     (define-key dired-mode-map (kbd "j") 'diredp-next-line)
1438     (define-key dired-mode-map (kbd "k") 'diredp-previous-line)
1439     (define-key dired-mode-map (kbd "g") 'dired-goto-file))
1440 #+END_SRC
1441
1442 ** dired-hacks
1443 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1444   (use-package dired-hacks-utils
1445     :ensure t)
1446 #+END_SRC
1447
1448 * ibuffer
1449 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1450   (global-set-key (kbd "s-b") 'ibuffer)
1451
1452   (with-eval-after-load 'ibuffer
1453     (define-key ibuffer-mode-map (kbd "C-o") nil)
1454     (define-key ibuffer-mode-map (kbd "j") 'ibuffer-forward-line)
1455     (define-key ibuffer-mode-map (kbd "k") 'ibuffer-backward-line)
1456     (define-key ibuffer-mode-map (kbd "r") 'ibuffer-update)
1457     (define-key ibuffer-mode-map (kbd "g") 'ibuffer-jump-to-buffer))
1458 #+END_SRC
1459
1460 * Completion
1461 company mode and company-statistics
1462 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1463   (use-package company
1464     :ensure t
1465     :diminish company-mode
1466     :init (setq company-idle-delay 0.1)
1467     :config
1468     (global-company-mode))
1469
1470   (use-package company-statistics
1471     :ensure t
1472     :config
1473     (company-statistics-mode))
1474 #+END_SRC
1475
1476 * Programming Language
1477 ** Emacs Lisp
1478 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1479   (use-package color-identifiers-mode
1480     :ensure t
1481     :init
1482     (add-hook 'emacs-lisp-mode-hook 'color-identifiers-mode)
1483
1484     :diminish color-identifiers-mode)
1485
1486   (global-prettify-symbols-mode t)
1487 #+END_SRC
1488
1489 In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
1490 bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
1491 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1492   (use-package lispy
1493     :ensure t
1494     :init
1495     (eval-after-load "lispy"
1496       `(progn
1497          (define-key lispy-mode-map (kbd "M-o") nil)))
1498     :config
1499     (add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1))))
1500 #+END_SRC
1501
1502 ** Perl
1503 *** CPerl mode
1504 [[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
1505 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1506   (defalias 'perl-mode 'cperl-mode)
1507
1508   ;; (setq cperl-hairy t)
1509   ;; Turns on most of the CPerlMode options
1510   (setq cperl-auto-newline t)
1511   (setq cperl-highlight-variables-indiscriminately t)
1512   ;(setq cperl-indent-level 4)
1513   ;(setq cperl-continued-statement-offset 4)
1514   (setq cperl-close-paren-offset -4)
1515   (setq cperl-indent-parents-as-block t)
1516   (setq cperl-tab-always-indent t)
1517   ;(setq cperl-brace-offset  0)
1518
1519   (add-hook 'cperl-mode-hook
1520             '(lambda ()
1521                (cperl-set-style "C++")))
1522
1523   (defalias 'perldoc 'cperl-perldoc)
1524 #+END_SRC
1525
1526 *** Perl template
1527 Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
1528 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1529   (eval-after-load 'autoinsert
1530     '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
1531        '(
1532          "Empty"
1533          "#!/usr/bin/perl -w" \n
1534          \n
1535          "use strict;" >  \n \n
1536          > _
1537          )))
1538 #+END_SRC
1539
1540 *** Perl Keywords
1541 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1542   (font-lock-add-keywords 'cperl-mode
1543                           '(("\\(say\\)" . cperl-nonoverridable-face)
1544                             ("\\([0-9.]\\)*" . font-lock-constant-face)
1545                             ("\".*\\(\\\n\\).*\"" . font-lock-constant-face)
1546                             ("\n" . font-lock-constant-face)
1547                             ("\\(^#!.*\\)$" .  cperl-nonoverridable-face)))
1548
1549     ;; (font-lock-add-keywords 'Man-mode
1550     ;;                         '(("\\(NAME\\)" . font-lock-function-name-face)))
1551
1552 #+END_SRC
1553
1554 *** Run Perl
1555 Change the compile-command to set the default command run when call =compile=
1556 Mapping =s-r= (on Mac, it's =Command + R= to run the script. Here =current-prefix-arg= is set
1557 to call =compilation=  interactively.
1558 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1559   (defun my-perl-hook ()
1560     (progn
1561       (setq-local compilation-read-command nil)
1562       (set (make-local-variable 'compile-command)
1563            (concat "/usr/bin/perl "
1564                    (if buffer-file-name
1565                        (shell-quote-argument buffer-file-name))))
1566       (local-set-key (kbd "s-r")
1567                      (lambda ()
1568                        (interactive)
1569                                           ;                       (setq current-prefix-arg '(4)) ; C-u
1570                        (call-interactively 'compile)))))
1571
1572   (add-hook 'cperl-mode-hook 'my-perl-hook)
1573 #+END_SRC
1574
1575 ** C & C++
1576 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1577   (setq c-default-style "stroustrup"
1578         c-basic-offset 4)
1579 #+END_SRC
1580
1581 * Compile
1582 Set the environments vairables in compilation mode
1583 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1584   (use-package compile
1585     :commands compile
1586     :config
1587     (setq compilation-environment (cons "LC_ALL=C" compilation-environment))
1588     (setq compilation-auto-jump-to-first-error t)
1589     (setq compilation-auto-jump-to-next t)
1590     (setq compilation-scroll-output 'first-error))
1591
1592   ;; super-r to compile
1593   (with-eval-after-load "compile"
1594     (define-key compilation-mode-map (kbd "C-o") nil)
1595     (define-key compilation-mode-map (kbd "n") 'compilation-next-error)
1596     (define-key compilation-mode-map (kbd "p") 'compilation-previous-error)
1597     (define-key compilation-mode-map (kbd "r") #'recompile))
1598 #+END_SRC
1599
1600 * Auto-Insert
1601 ** Enable auto-insert mode
1602 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1603   (auto-insert-mode t)
1604   (setq auto-insert-query nil)
1605 #+END_SRC
1606
1607 ** C++ Auto Insert
1608 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1609   (eval-after-load 'autoinsert
1610     '(define-auto-insert '("\\.cpp\\'" . "C++ skeleton")
1611        '(
1612          "Short description:"
1613          "/*"
1614          "\n * " (file-name-nondirectory (buffer-file-name))
1615          "\n */" > \n \n
1616          "#include <iostream>" \n
1617          "#include \""
1618          (file-name-sans-extension
1619           (file-name-nondirectory (buffer-file-name)))
1620          ".hpp\"" \n \n
1621          "using namespace std;" \n \n
1622          "int main ()"
1623          "\n{" \n 
1624          > _ \n
1625          "return 1;"
1626          "\n}" > \n
1627          )))
1628
1629   (eval-after-load 'autoinsert
1630     '(define-auto-insert '("\\.c\\'" . "C skeleton")
1631        '(
1632          "Short description:"
1633          "/*\n"
1634          " * " (file-name-nondirectory (buffer-file-name)) "\n"
1635          " */" > \n \n
1636          "#include <stdio.h>" \n
1637          "#include \""
1638          (file-name-sans-extension
1639           (file-name-nondirectory (buffer-file-name)))
1640          ".h\"" \n \n
1641          "int main ()\n"
1642          "{" \n
1643          > _ \n
1644          "return 1;\n"
1645          "}" > \n
1646          )))
1647 #+END_SRC
1648
1649 ** Python template
1650 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1651   (eval-after-load 'autoinsert
1652     '(define-auto-insert '("\\.\\(py\\)\\'" . "Python skeleton")
1653        '(
1654          "Empty"
1655          "#import os,sys" \n
1656          \n \n
1657          )))
1658 #+END_SRC
1659
1660 ** Elisp 
1661 Emacs lisp auto-insert, based on the default module in =autoinsert.el=, but replace =completing-read= as 
1662 =completing-read-ido-ubiquitous= to fix the edge case of that =ido= cannot handle.
1663 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1664   (eval-after-load 'autoinsert
1665     '(define-auto-insert '("\\.el\\'" . "my Emacs Lisp header")
1666        '(
1667          "Short description: "
1668          ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
1669          (make-string (max 2 (- 80 (current-column) 27)) ?\s)
1670          "-*- lexical-binding: t; -*-" '(setq lexical-binding t)
1671          "\n
1672   ;; Copyright (C) " (format-time-string "%Y") "  "
1673          (getenv "ORGANIZATION") | (progn user-full-name) "
1674
1675   ;; Author: " (user-full-name)
1676          '(if (search-backward "&" (line-beginning-position) t)
1677               (replace-match (capitalize (user-login-name)) t t))
1678          '(end-of-line 1) " <" (progn user-mail-address) ">
1679   ;; Keywords: "
1680          '(require 'finder)
1681          ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
1682          '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
1683                            finder-known-keywords)
1684                 v2 (mapconcat (lambda (x) (format "%12s:  %s" (car x) (cdr x)))
1685                               finder-known-keywords
1686                               "\n"))
1687          ((let ((minibuffer-help-form v2))
1688             (completing-read-ido-ubiquitous "Keyword, C-h: " v1 nil t))
1689           str ", ") & -2 "
1690
1691   \;; This program is free software; you can redistribute it and/or modify
1692   \;; it under the terms of the GNU General Public License as published by
1693   \;; the Free Software Foundation, either version 3 of the License, or
1694   \;; (at your option) any later version.
1695
1696   \;; This program is distributed in the hope that it will be useful,
1697   \;; but WITHOUT ANY WARRANTY; without even the implied warranty of
1698   \;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1699   \;; GNU General Public License for more details.
1700
1701   \;; You should have received a copy of the GNU General Public License
1702   \;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
1703
1704   \;;; Commentary:
1705
1706   \;; " _ "
1707
1708   \;;; Code:
1709
1710
1711   \(provide '"
1712          (file-name-base)
1713          ")
1714   \;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")))
1715 #+END_SRC
1716
1717 ** Org file template
1718 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1719   (eval-after-load 'autoinsert
1720     '(define-auto-insert '("\\.\\(org\\)\\'" . "Org-mode skeleton")
1721        '(
1722          "title: "
1723          "#+TITLE: " str (make-string 30 ?\s) > \n
1724          "#+AUTHOR: Peng Li\n"
1725          "#+EMAIL: seudut@gmail.com\n"
1726          "#+DATE: " (shell-command-to-string "echo -n $(date +%Y-%m-%d)") > \n
1727          > \n
1728          > _)))
1729 #+END_SRC
1730
1731 * Markdown mode
1732 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1733   (use-package markdown-mode
1734     :ensure t
1735     :commands (markdown-mode gfm-mode)
1736     :mode (("README\\.md\\'" . gfm-mode)
1737            ("\\.md\\'" . markdown-mode)
1738            ("\\.markdown\\'" . markdown-mode))
1739     :init (setq markdown-command "multimarkdown"))
1740 #+END_SRC
1741
1742 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1743   (use-package markdown-preview-eww
1744     :ensure t)
1745 #+END_SRC
1746
1747 * Gnus
1748 ** Gmail setting 
1749 Refer [[https://www.emacswiki.org/emacs/GnusGmail][GnusGmail]]
1750 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1751   (setq user-mail-address "seudut@gmail.com"
1752         user-full-name "Peng Li")
1753
1754   (setq gnus-select-method
1755         '(nnimap "gmail"
1756                  (nnimap-address "imap.gmail.com")
1757                  (nnimap-server-port "imaps")
1758                  (nnimap-stream ssl)))
1759
1760   (setq smtpmail-smtp-service 587
1761         gnus-ignored-newsgroups "^to\\.\\|^[0-9. ]+\\( \\|$\\)\\|^[\"]\"[#'()]")
1762
1763   ;; Use gmail sending mail
1764   (setq message-send-mail-function 'smtpmail-send-it
1765         smtpmail-starttls-credentials '(("smtp.gmail.com" 587 nil nil))
1766         smtpmail-auth-credentials '(("smtp.gmail.com" 587 "seudut@gmail.com" nil))
1767         smtpmail-default-smtp-server "smtp.gmail.com"
1768         smtpmail-smtp-server "smtp.gmail.com"
1769         smtpmail-smtp-service 587
1770         starttls-use-gnutls t)
1771 #+END_SRC
1772
1773 And put the following in =~/.authinfo= file, replacing =<USE>= with your email address
1774 and =<PASSWORD>= with the password
1775 #+BEGIN_EXAMPLE
1776   machine imap.gmail.com login <USER> password <PASSWORD> port imaps
1777   machine smtp.gmail.com login <USER> password <PASSWORD> port 587
1778 #+END_EXAMPLE
1779
1780 Then Run =M-x gnus=
1781
1782 ** Group buffer
1783 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1784   (use-package gnus
1785     :init
1786     (setq gnus-permanently-visible-groups "\.*")
1787     :config
1788     (cond (window-system
1789            (setq custom-background-mode 'light)
1790            (defface my-group-face-1
1791              '((t (:foreground "Red" :bold t))) "First group face")
1792            (defface my-group-face-2
1793              '((t (:foreground "DarkSeaGreen4" :bold t)))
1794              "Second group face")
1795            (defface my-group-face-3
1796              '((t (:foreground "Green4" :bold t))) "Third group face")
1797            (defface my-group-face-4
1798              '((t (:foreground "SteelBlue" :bold t))) "Fourth group face")
1799            (defface my-group-face-5
1800              '((t (:foreground "Blue" :bold t))) "Fifth group face")))
1801     (setq gnus-group-highlight
1802           '(((> unread 200) . my-group-face-1)
1803             ((and (< level 3) (zerop unread)) . my-group-face-2)
1804             ((< level 3) . my-group-face-3)
1805             ((zerop unread) . my-group-face-4)
1806             (t . my-group-face-5))))
1807
1808
1809   ;; key-
1810   (add-hook 'gnus-group-mode-hook (lambda ()
1811                                     (define-key gnus-group-mode-map "k" 'gnus-group-prev-group)
1812                                     (define-key gnus-group-mode-map "j" 'gnus-group-next-group)
1813                                     (define-key gnus-group-mode-map "g" 'gnus-group-jump-to-group)
1814                                     (define-key gnus-group-mode-map "v" (lambda () (interactive) (gnus-group-select-group t)))))
1815 #+END_SRC
1816
1817 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1818   (setq gnus-fetch-old-headers 't)
1819
1820
1821
1822   (setq gnus-extract-address-components
1823         'mail-extract-address-components)
1824   ;; summary buffer 
1825   (setq gnus-summary-line-format "%U%R%z%I%(%[%-20,20f%]%)  %s%-80=   %11&user-date;\n")
1826   (setq gnus-user-date-format-alist '(((gnus-seconds-today) . "%H:%M")
1827                                       ((+ 86400 (gnus-seconds-today)) . "%a %H:%M")
1828                                       (604800 . "%a, %b %-d")
1829                                       (15778476 . "%b %-d")
1830                                       (t . "%Y-%m-%d")))
1831
1832   (setq gnus-thread-sort-functions '((not gnus-thread-sort-by-number)))
1833   (setq gnus-unread-mark ?\.)
1834   (setq gnus-use-correct-string-widths t)
1835
1836   ;; thread
1837   (setq gnus-thread-hide-subtree t)
1838
1839   ;; (with-eval-after-load 'gnus-summary-mode
1840   ;;   (define-key gnus-summary-mode-map (kbd "C-o") 'sd/hydra-window/body))
1841
1842   (add-hook 'gnus-summary-mode-hook (lambda ()
1843                                       (define-key gnus-summary-mode-map (kbd "C-o") nil)))
1844
1845
1846 #+END_SRC
1847
1848 ** Windows layout
1849 See [[https://www.emacswiki.org/emacs/GnusWindowLayout][GnusWindowLayout]]
1850 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1851   (gnus-add-configuration
1852    '(summary
1853      (horizontal 1.0
1854                  (vertical 35
1855                            (group 1.0))
1856                  (vertical 1.0
1857                            (summary 1.0 poine)))))
1858
1859   (gnus-add-configuration
1860    '(article
1861      (horizontal 1.0
1862                  (vertical 35
1863                            (group 1.0))
1864                  (vertical 1.0
1865                            (summary 0.50 point)
1866                            (article 1.0)))))
1867
1868   (with-eval-after-load 'gnus-group-mode
1869     (gnus-group-select-group "INBOX"))
1870   ;; (add-hook 'gnus-group-mode-map (lambda ()
1871   ;;                               (gnus-group-select-group "INBOX")))
1872 #+END_SRC
1873
1874 * Gnu Plot
1875 To fix some issue that =toolbar-make-button-list= is void, see the [[https://github.com/bruceravel/gnuplot-mode/issues/31][issue]], here I set some variable as =nil=
1876 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1877   (use-package gnuplot
1878     :ensure
1879     :init
1880     (setq gnuplot-help-xpm nil)
1881     (setq gnuplot-line-xpm nil)
1882     (setq gnuplot-region-xpm nil)
1883     (setq gnuplot-buffer-xpm nil)
1884     (setq gnuplot-doc-xpm nil))
1885 #+END_SRC
1886
1887 Use =gnuplot= on =Org-mode= file, see [[http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-gnuplot.html][ob-doc-gnuplot]]
1888 #+BEGIN_SRC gnuplot :exports code :file ./temp/file.png
1889   reset
1890
1891   set title "Putting it All Together"
1892
1893   set xlabel "X"
1894   set xrange [-8:8]
1895   set xtics -8,2,8
1896
1897
1898   set ylabel "Y"
1899   set yrange [-20:70]
1900   set ytics -20,10,70
1901
1902   f(x) = x**2
1903   g(x) = x**3
1904   h(x) = 10*sqrt(abs(x))
1905
1906   plot f(x) w lp lw 1, g(x) w p lw 2, h(x) w l lw 3
1907 #+END_SRC
1908
1909 #+RESULTS:
1910 [[file:./temp/file.png]]
1911 * Ediff
1912 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1913   (with-eval-after-load 'ediff
1914     (setq ediff-split-window-function 'split-window-horizontally)
1915     (setq ediff-window-setup-function 'ediff-setup-windows-plain)
1916     (add-hook 'ediff-startup-hook 'ediff-toggle-wide-display)
1917     (add-hook 'ediff-cleanup-hook 'ediff-toggle-wide-display)
1918     (add-hook 'ediff-suspend-hook 'ediff-toggle-wide-display))
1919 #+END_SRC
1920
1921 * Entertainment
1922 ** GnoGo
1923 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
1924 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1925   (use-package gnugo
1926     :ensure t
1927     :defer t
1928     :init
1929     (require 'gnugo-imgen)
1930     (setq gnugo-xpms 'gnugo-imgen-create-xpms)
1931     (add-hook 'gnugo-start-game-hook '(lambda ()
1932                                         (gnugo-image-display-mode)
1933                                         (gnugo-grid-mode)))
1934     :config
1935     (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
1936 #+END_SRC
1937
1938 ** Emms
1939 We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
1940 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1941   (use-package emms
1942     :ensure t
1943     :init
1944     (setq emms-directory (concat sd-temp-directory "emms"))
1945     (setq emms-source-file-default-directory "~/Music/")
1946     :config
1947     (emms-standard)
1948     (emms-default-players)
1949     (define-emms-simple-player mplayer '(file url)
1950       (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
1951                     ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
1952                     "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
1953                     ".m4a" ".flv" ".ogv" ".pls"))
1954       "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
1955     (emms-history-load))
1956 #+END_SRC
1957
1958 * Dictionary
1959 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1960   (use-package bing-dict
1961     :ensure t
1962     :init
1963     (global-set-key (kbd "s-d") 'bing-dict-brief)
1964     :commands (bing-dict-brief))
1965 #+END_SRC
1966
1967 * Key Bindings
1968 Here are some global key bindings for basic editting
1969 ** Esc in minibuffer
1970 Use =ESC= to exit minibuffer. Also I map =Super-h= the same as =C-g=
1971 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1972   (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
1973   (define-key minibuffer-local-map [escape]  'keyboard-escape-quit)
1974   (define-key minibuffer-local-ns-map [escape]  'keyboard-escape-quit)
1975   (define-key minibuffer-local-isearch-map [escape]  'keyboard-escape-quit)
1976   (define-key minibuffer-local-completion-map [escape]  'keyboard-escape-quit)
1977   (define-key minibuffer-local-must-match-map [escape]  'keyboard-escape-quit)
1978   (define-key minibuffer-local-must-match-filename-map [escape]  'keyboard-escape-quit)
1979   (define-key minibuffer-local-filename-completion-map [escape]  'keyboard-escape-quit)
1980   (define-key minibuffer-local-filename-must-match-map [escape]  'keyboard-escape-quit)
1981
1982   ;; Also map s-h same as C-g
1983   (define-key minibuffer-local-map (kbd "s-h") 'keyboard-escape-quit)
1984 #+END_SRC
1985
1986 ** Project operations - =super=
1987 *** Projectile
1988 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
1989   (use-package projectile
1990     :ensure t
1991     :init
1992     (setq projectile-enable-caching t)
1993     (setq projectile-cache-file (concat sd-temp-directory "projectile.cache"))
1994     :config
1995     (add-to-list 'projectile-globally-ignored-files "GTAGS")
1996     (projectile-global-mode t))
1997
1998   (use-package persp-projectile
1999     :ensure t
2000     :config
2001     (persp-mode)
2002     :bind
2003     (:map projectile-mode-map
2004           ("s-t" . projectile-persp-switch-project)))
2005
2006   ;; projectile-find-file
2007   ;; projectile-switch-buffer
2008   ;; projectile-find-file-other-window
2009 #+END_SRC
2010
2011 *** project config =super= keybindings
2012 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2013   ;; (global-set-key (kbd "s-h") 'keyboard-quit)
2014   ;; (global-set-key (kbd "s-j") 'ido-switch-buffer)
2015   ;; (global-set-key (kbd "s-k") 'ido-find-file)
2016   ;; (global-set-key (kbd "s-l") 'sd/delete-current-window)
2017   ;; s-l  -->  goto-line
2018   ;; (global-set-key (kbd "s-/") 'swiper)
2019   ;; s-;  -->
2020   ;; s-'  -->  'next-multiframe-window
2021   (global-set-key (kbd "<s-return>") 'toggle-frame-fullscreen)
2022
2023   (global-set-key (kbd "s-f") 'projectile-find-file)
2024   (global-set-key (kbd "s-`") 'mode-line-other-buffer)
2025
2026   (global-set-key (kbd "s-n") 'persp-next)
2027   (global-set-key (kbd "s-p") 'persp-prev)
2028
2029   (global-set-key (kbd "s-=") 'text-scale-increase)
2030   (global-set-key (kbd "s--") 'text-scale-decrease)
2031
2032   ;; (global-set-key (kbd "s-u") 'undo-tree-visualize)
2033
2034
2035   ;; someothers default mapping on super (command) key
2036   ;; s-s save-buffer
2037   ;; s-k kill-this-buffer
2038
2039
2040   ;; s-h  -->  ns-do-hide-emacs
2041   ;; s-j  -->  ido-switch-buffer  +
2042   ;; s-k  -->  kill-this-buffer
2043   ;; s-l  -->  goto-line
2044   ;; s-;  -->  undefined
2045   ;; s-'  -->  next-multiframe-window
2046   ;; s-ret --> toggle-frame-fullscreen +
2047
2048   ;; s-y  -->  ns-paste-secondary
2049   ;; s-u  -->  revert-buffer
2050   ;; s-i  -->  undefined - but used for iterm globally
2051   ;; s-o  -->  used for emacs globally
2052   ;; s-p  -->  projectile-persp-switch-project  +  
2053   ;; s-[  -->  next-buffer  +    
2054   ;; s-]  -->  previous-buffer +
2055
2056   ;; s-0  -->  undefined
2057   ;; s-9  -->  undefined
2058   ;; s-8  -->  undefined
2059   ;; s-7  -->  undefined
2060   ;; s-6  -->  undefined
2061   ;; s--  -->  center-line
2062   ;; s-=  -->  undefined
2063
2064   ;; s-n  -->  make-frame
2065   ;; s-m  -->  iconify-frame
2066   ;; s-b  -->  undefined
2067   ;; s-,  -->  customize
2068   ;; s-.  -->  undefined
2069   ;; s-/  -->  undefined
2070
2071   ;; s-g  -->  isearch-repeat-forward
2072   ;; s-f  -->  projectile-find-file   +
2073   ;; s-d  -->  isearch-repeat-background
2074   ;; s-s  -->  save-buffer
2075   ;; s-a  -->  make-whole-buffer
2076
2077   ;; s-b  -->  undefined
2078   ;; s-v  -->  yank
2079   ;; s-c  -->  ns-copy-including-secondary
2080
2081   ;; s-t  -->  ns-popup-font-panel
2082   ;; s-r  -->  undefined
2083   ;; s-e  -->  isearch-yanqk-kill
2084   ;; s-w  -->  delete-frame
2085   ;; s-q  -->  same-buffers-kill-emacs
2086
2087   ;; s-`  -->  other-frame
2088 #+END_SRC
2089
2090 ** Windown & Buffer - =C-o=
2091 Defind a =hydra= function for windows, buffer & bookmark operations. And map it to =C-o= globally.
2092 Most use =C-o C-o= to switch buffers; =C-o x, v= to split window; =C-o o= to delete other windows
2093 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2094   (winner-mode 1)
2095
2096   (defun sd/delete-current-window ()
2097     (interactive)
2098     (if (> (length (window-list)) 1)
2099         (delete-window)
2100       (message "Only one Windows now!")))
2101
2102   (defun sd/toggle-max-windows ()
2103     "Set maximize current if there are multiple windows, if only
2104   one window, window undo"
2105     (interactive)
2106     (if (equal  (length (window-list)) 1)
2107         (winner-undo)
2108       (delete-other-windows)))
2109
2110   (defhydra sd/hydra-window (:color red :columns nil)
2111     "Window"
2112     ;; windows split
2113     ("h" windmove-left nil :exit t)
2114     ("j" windmove-down nil :exit t)
2115     ("k" windmove-up nil :exit t)
2116     ("l" windmove-right nil :exit t)
2117     ("H" hydra-move-splitter-left nil)
2118     ("J" hydra-move-splitter-down nil)
2119     ("K" hydra-move-splitter-up nil)
2120     ("L" hydra-move-splitter-right nil)
2121     ("v" (lambda ()
2122            (interactive)
2123            (split-window-right)
2124            (windmove-right))
2125      "vert" :exit t)
2126     ("x" (lambda ()
2127            (interactive)
2128            (split-window-below)
2129            (windmove-down))
2130      "horz" :exit t)
2131
2132     ;; buffer / windows switch
2133     ("o" sd/toggle-max-windows "one" :exit t)
2134     ("C-k" sd/delete-current-window "del" :exit t)
2135     ("'" other-window "other" :exit t)
2136     ;; ("a" ace-window "ace")
2137     ("s" ace-swap-window "swap")
2138     ;; ("i" ace-maximize-window "ace-one" :exit t)
2139
2140     ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
2141     ("r" (progn (winner-redo) (setq this-command 'winner-redo)) "redo")
2142
2143     ;; ibuffer, dired, eshell, bookmarks
2144     ;; ("d" ace-delete-window "ace-one" :exit t)
2145     ("C-o" ido-switch-buffer nil :exit t)
2146     ("d" dired-jump nil :exit t)
2147     ("b" ibuffer nil n:exit t)
2148     ("e" eshell nil :exit t)
2149     ("m" bookmark-jump-other-window nil :exit t)
2150     ("M" bookmark-set nil :exit t)
2151     ("g" magit-status nil :exit t)
2152     ("p" paradox-list-packages nil :exit t)
2153
2154     ;; quit
2155     ("q" nil "cancel")
2156     ("<ESC>" nil)
2157     ("C-h" nil nil :exit t)
2158     ("C-j" nil nil :exit t)
2159     ;; ("C-k" nil :exit t)
2160     ("C-l" nil nil :exit t)
2161     ("C-;" nil nil :exit t)
2162     ("n" nil nil :exit t)
2163     ("[" nil nil :exit t)
2164     ("]" nil nil :exit t)
2165     ("f" nil))
2166
2167   (global-unset-key (kbd "C-o"))
2168   (global-set-key (kbd "C-o") 'sd/hydra-window/body)
2169 #+END_SRC
2170
2171 ** Motion
2172 - =C-M-=
2173 [[https://www.masteringemacs.org/article/effective-editing-movement][effective-editing-movement]]
2174 *** Command Arguments, numeric argumens
2175 =C-u 4= same as =C-4=, =M-4=
2176 *** Basic movement
2177 moving by line / word / 
2178 =C-f=, =C-b=, =C-p=, =C-n=, =M-f=, =M-b=
2179 =C-a=, =C-e=
2180 =M-m= (move first non-whitespace on this line) 
2181 =M-}=, =M-{=, Move forward end of paragraph
2182 =M-a=, =M-e=,  beginning / end of sentence
2183 =C-M-a=, =C-M-e=, move begining of defun
2184 =C-x ]=, =C-x [=, forward/backward one page
2185 =C-v=, =M-v=, =C-M-v=, =C-M-S-v= scroll down/up
2186 =M-<=, =M->=, beginning/end of buffer
2187 =M-r=, Repositiong point
2188
2189 *** Moving by S-expression / List
2190 *** Marks
2191 =C-<SPC>= set marks toggle the region
2192 =C-u C-<SPC>= Jump to the mark, repeated calls go further back the mark ring
2193 =C-x C-x= Exchanges the point and mark.
2194
2195 Stolen [[https://www.masteringemacs.org/article/fixing-mark-commands-transient-mark-mode][fixing-mark-commands-transient-mark-mode]]
2196 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2197   (defun push-mark-no-activate ()
2198     "Pushes `point' to `mark-ring' and does not activate the region
2199      Equivalent to \\[set-mark-command] when \\[transient-mark-mode] is disabled"
2200     (interactive)
2201     (push-mark (point) t nil)
2202     (message "Pushed mark to ring"))
2203
2204   ;; (global-set-key (kbd "C-`") 'push-mark-no-activate)
2205
2206   (defun jump-to-mark ()
2207     "Jumps to the local mark, respecting the `mark-ring' order.
2208     This is the same as using \\[set-mark-command] with the prefix argument."
2209     (interactive)
2210     (set-mark-command 1))
2211
2212   ;; (global-set-key (kbd "M-`") 'jump-to-mark)
2213
2214   (defun exchange-point-and-mark-no-activate ()
2215     "Identical to \\[exchange-point-and-mark] but will not activate the region."
2216     (interactive)
2217     (exchange-point-and-mark)
2218     (deactivate-mark nil))
2219
2220   ;; (define-key global-map [remap exchange-point-and-mark] 'exchange-point-and-mark-no-activate)
2221 #+END_SRC
2222
2223 Show the mark ring using =helm-mark-ring=, also mapping =M-`= to quit minibuffer. so that =M-`= can 
2224 toggle the mark ring. the best way is add a new action and mapping to =helm-source-mark-ring=,  but 
2225 since there is no map such as =helm-mark-ring=map=, so I cannot binding a key to the quit action.
2226 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2227   (setq mark-ring-max 50)
2228   (global-set-key (kbd "M-`") #'helm-mark-ring)
2229
2230   (define-key minibuffer-local-map (kbd "M-`") 'keyboard-escape-quit)
2231 #+END_SRC
2232
2233 =M-h= marks the next paragraph
2234 =C-x h= marks the whole buffer
2235 =C-M-h= marks the next defun
2236 =C-x C-p= marks the next page
2237 *** Registers
2238 Registers can save text, position, rectangles, file and configuration and other things.
2239 Here for movement, we can use register to save/jump position
2240 =C-x r SPC= store point in register
2241 =C-x r j= jump to register
2242 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2243   (use-package list-register
2244     :ensure t)
2245 #+END_SRC
2246
2247 *** Bookmarks
2248 As I would like use bookmakr for different buffer/files. to help to swith
2249 different buffer/file quickly. this setting is in Windows/buffer node
2250 =C-x r m= set a bookmarks
2251 =C-x r l= list bookmarks
2252 =C-x r b= jump to bookmarks
2253
2254 *** Search
2255 Search, replace and hightlight will in later paragraph
2256 *** =Avy= for easy motion
2257 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2258   (use-package avy
2259     :ensure t
2260     :config
2261     (avy-setup-default))
2262
2263   (global-set-key (kbd "C-M-j") 'avy-goto-line-below)
2264   (global-set-key (kbd "C-M-n") 'avy-goto-line-below)
2265   (global-set-key (kbd "C-M-k") 'avy-goto-line-above)
2266   (global-set-key (kbd "C-M-p") 'avy-goto-line-above)
2267
2268   (global-set-key (kbd "C-M-f") 'avy-goto-word-1-below)
2269   (global-set-key (kbd "C-M-b") 'avy-goto-word-1-above)
2270
2271   ;; (global-set-key (kbd "M-g e") 'avy-goto-word-0)
2272   (global-set-key (kbd "C-M-w") 'avy-goto-char-timer)
2273   (global-set-key (kbd "C-M-l") 'avy-goto-char-in-line)
2274
2275   ;; ;; will delete above 
2276   ;; (global-set-key (kbd "M-g j") 'avy-goto-line-below)
2277   ;; (global-set-key (kbd "M-g k") 'avy-goto-line-above)
2278   ;; (global-set-key (kbd "M-g w") 'avy-goto-word-1-below)
2279   ;; (global-set-key (kbd "M-g b") 'avy-goto-word-1-above)
2280   ;; (global-set-key (kbd "M-g e") 'avy-goto-word-0)
2281   ;; (global-set-key (kbd "M-g f") 'avy-goto-char-timer)
2282   ;; (global-set-key (kbd "M-g c") 'avy-goto-char-in-line)
2283 #+END_SRC
2284
2285 *** =Imenu= goto tag
2286 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2287   ;; (global-set-key (kbd "M-i") #'counsel-imenu)
2288   (global-set-key (kbd "M-i") #'imenu)
2289
2290   ;; (global-set-key (kbd "M-l") 'goto-line)
2291 #+END_SRC
2292
2293 *** Go-to line
2294 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2295   (global-set-key (kbd "M-l") 'goto-line)
2296 #+END_SRC
2297
2298 ** Edit
2299 *** basic editting
2300 - cut, yank, =C-w=, =C-y=
2301 - save, revert
2302 - undo, redo - undo-tree
2303 - select, expand-region
2304 - spell check, flyspell
2305
2306 *** Kill ring
2307 =helm-show-kill-ring=
2308 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2309   (setq kill-ring-max 100)                ; default is 60p
2310   (global-set-key (kbd "M-y") #'helm-show-kill-ring)
2311 #+END_SRC
2312
2313 *** undo-tree
2314 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2315   (use-package undo-tree
2316     :ensure t
2317     :config
2318     (define-key undo-tree-visualizer-mode-map "j" 'undo-tree-visualize-redo)
2319     (define-key undo-tree-visualizer-mode-map "k" 'undo-tree-visualize-undo)
2320     (define-key undo-tree-visualizer-mode-map "h" 'undo-tree-visualize-switch-branch-left)
2321     (define-key undo-tree-visualizer-mode-map "l" 'undo-tree-visualize-switch-branch-right)
2322     (global-undo-tree-mode 1))
2323
2324   (global-set-key (kbd "s-u") 'undo-tree-visualize)
2325 #+END_SRC
2326
2327 *** flyspell
2328 Stolen from [[https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-spelling.el][here]], hunspell will search dictionary in =DICPATH=
2329 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2330   (setenv "DICPATH" "/usr/local/share/hunspell")
2331
2332   (when (executable-find "hunspell")
2333     (setq-default ispell-program-name "hunspell")
2334     (setq ispell-really-hunspell t))
2335
2336   ;; (defun text-mode-hook-setup ()
2337   ;;   ;; Turn off RUN-TOGETHER option when spell check text-mode
2338   ;;   (setq-local ispell-extra-args (flyspell-detect-ispell-args)))
2339   ;; (add-hook 'text-mode-hook 'text-mode-hook-setup)
2340   ;; (add-hook 'text-mode-hook 'flyspell-mode)
2341
2342   ;; enable flyspell check on comments and strings in progmamming modes
2343   ;; (add-hook 'prog-mode-hook 'flyspell-prog-mode)
2344
2345   ;; I don't use the default mappings
2346   (with-eval-after-load 'flyspell
2347     (define-key flyspell-mode-map (kbd "C-;") nil)
2348     (define-key flyspell-mode-map (kbd "C-,") nil)
2349     (define-key flyspell-mode-map (kbd "C-.") nil))
2350 #+END_SRC
2351
2352 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]]
2353 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2354   ;; NO spell check for embedded snippets
2355   (defadvice org-mode-flyspell-verify (after org-mode-flyspell-verify-hack activate)
2356     (let ((rlt ad-return-value)
2357           (begin-regexp "^[ \t]*#\\+begin_\\(src\\|html\\|latex\\)")
2358           (end-regexp "^[ \t]*#\\+end_\\(src\\|html\\|latex\\)")
2359           old-flag
2360           b e)
2361       (when ad-return-value
2362         (save-excursion
2363           (setq old-flag case-fold-search)
2364           (setq case-fold-search t)
2365           (setq b (re-search-backward begin-regexp nil t))
2366           (if b (setq e (re-search-forward end-regexp nil t)))
2367           (setq case-fold-search old-flag))
2368         (if (and b e (< (point) e)) (setq rlt nil)))
2369       (setq ad-return-value rlt)))
2370 #+END_SRC
2371
2372 ** Search & Replace / hightlight =M-s=
2373 *** isearch
2374 =C-s=, =C-r=, 
2375 =C-w= add word at point to search string, 
2376 =M-%= query replace
2377 =C-M-y= add character at point to search string
2378 =M-s C-e= add reset of line at point
2379 =C-y= yank from clipboard to search string
2380 =M-n=, =M-p=, history
2381 =C-M-i= complete search string
2382 set the isearch history size, the default is only =16=
2383 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2384   (setq history-length 5000)
2385   (setq regexp-search-ring-max 1000)
2386   (setq search-ring-max 1000)
2387
2388   ;; when search a word or a symbol , also add the word into regexp-search-ring
2389   (defadvice isearch-update-ring (after sd/isearch-update-ring (string &optional regexp) activate)
2390     "Add search-ring to regexp-search-ring"
2391     (unless regexp
2392       (add-to-history 'regexp-search-ring string regexp-search-ring-max)))
2393 #+END_SRC
2394
2395 *** =M-s= prefix
2396 use the prefix =M-s= for searching in buffers
2397 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2398   (defun sd/make-keymap (key bindings)
2399     (setq keymap (make-sparse-keymap))
2400     (dolist (binding bindings)
2401       (define-key keymap (car binding) (cdr binding)))
2402     (global-set-key key keymap))
2403
2404   ;; (sd/make-keymap "\M-s"
2405   ;;                 '(("w" . save-buffer)
2406   ;;                   ;; ("\M-w" . save-buffer)
2407   ;;                   ("e" . revert-buffer)
2408   ;;                   ("s" . isearch-forward-regexp)
2409   ;;                   ("\M-s" . isearch-forward-regexp)
2410   ;;                   ("r" . isearch-backward-regexp)
2411   ;;                   ("." . isearch-forward-symbol-at-point)
2412   ;;                   ("o" . occur)
2413   ;;                   ;; ("h" . highlight-symbol-at-point)
2414   ;;                   ("h" . highlight-symbol)
2415   ;;                   ("m" . highlight-regexp)
2416   ;;                   ("l" . highlight-lines-matching-regexp)
2417   ;;                   ("M" . unhighlight-regexp)
2418   ;;                   ("f" . keyboard-quit)
2419   ;;                   ("q" . keyboard-quit)))
2420 #+END_SRC
2421
2422 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2423   (use-package highlight-symbol
2424     :ensure t)
2425
2426   (defhydra sd/search-replace (:color red :columns nil)
2427     "Search"
2428     ("w" save-buffer "save" :exit t)
2429     ("e" revert-buffer "revert" :exit t)
2430     ("u" undo-tree-visualize "undo" :exit t)
2431     ("s" isearch-forward-regexp "s-search" :exit t)
2432     ("M-s" isearch-forward-regexp "s-search" :exit t)
2433     ("r" isearch-backward-regexp "r-search" :exit t)
2434     ("." isearch-forward-symbol-at-point "search point" :exit t)
2435     ("/" swiper "swiper" :exit t)
2436     ("o" occur "occur" :exit t)
2437     ("h" highlight-symbol "higlight" :exit t)
2438     ("l" highlight-lines-matching-regexp "higlight line" :exit t)
2439     ("m" highlight-regexp "higlight" :exit t)
2440     ("M" unhighlight-regexp "unhiglight" :exit t)
2441     ("q" nil "quit")
2442     ("f" nil))
2443
2444   (global-unset-key (kbd "M-s"))
2445   (global-set-key (kbd "M-s") 'sd/search-replace/body)
2446
2447
2448   ;; search and replace and highlight
2449   (define-key isearch-mode-map (kbd "M-s") 'isearch-repeat-forward)
2450   (define-key isearch-mode-map (kbd "M-r") 'isearch-repeat-backward)
2451   (global-set-key (kbd "s-[") 'highlight-symbol-next)
2452   (global-set-key (kbd "s-]") 'highlight-symbol-prev)
2453   (global-set-key (kbd "s-\\") 'highlight-symbol-query-replace)
2454 #+END_SRC
2455
2456 *** Occur
2457 Occur search key bindings
2458 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2459   (defun sd/occur-keys ()
2460     "My key bindings in occur-mode"
2461     (interactive)
2462     (switch-to-buffer-other-window "*Occur*")
2463     (define-key occur-mode-map (kbd "C-o") nil)
2464     (define-key occur-mode-map (kbd "C-n") (lambda ()
2465                                              (interactive)
2466                                              (occur-next)
2467                                              (occur-mode-goto-occurrence-other-window)
2468                                              (recenter)
2469                                              (other-window 1)))
2470     (define-key occur-mode-map (kbd "C-p") (lambda ()
2471                                              (interactive)
2472                                              (occur-prev)
2473                                              (occur-mode-goto-occurrence-other-window)
2474                                              (recenter)
2475                                              (other-window 1))))
2476
2477   (add-hook 'occur-hook #'sd/occur-keys)
2478
2479   (use-package color-moccur
2480     :ensure t
2481     :commands (isearch-moccur isearch-all)
2482     :init
2483     (setq isearch-lazy-highlight t)
2484     :config
2485     (use-package moccur-edit))
2486 #+END_SRC
2487
2488 *** Swiper
2489 stolen from [[https://github.com/mariolong/emacs.d/blob/f6a061594ef1b5d1f4750e9dad9dc97d6e122840/emacs-init.org][here]]
2490 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2491   (use-package swiper
2492     :ensure t
2493     :init
2494     (setq ivy-use-virtual-buffers t)
2495     (set-face-attribute 'ivy-current-match nil :background "Orange" :foreground "black")
2496     :config
2497     (ivy-mode)
2498     (global-set-key (kbd "s-/") 'swiper)
2499     (define-key swiper-map (kbd "M-r") 'swiper-query-replace)
2500     (define-key swiper-map (kbd "C-.") (lambda ()
2501                                          (interactive)
2502                                          (insert (format "%s" (with-ivy-window (thing-at-point 'word))))))
2503     (define-key swiper-map (kbd "M-.") (lambda ()
2504                                          (interactive)
2505                                          (insert (format "%s" (with-ivy-window (thing-at-point 'symbol)))))))
2506 #+END_SRC
2507
2508 ** Expand region map
2509 *** Install =expand-region=
2510 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2511   (use-package expand-region
2512     :ensure t
2513     :config
2514     ;; (global-set-key (kbd "C-=") 'er/expand-region)
2515     )
2516 #+END_SRC
2517
2518 *** Add a =hydra= map for =expand-region= operations
2519 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2520   (defun sd/mark-line ()
2521     "Mark current line without whitespace beginning"
2522     (interactive)
2523     (back-to-indentation)
2524     (set-mark (line-end-position)))
2525
2526   (defhydra sd/expand-selected (:color pink :columns nil
2527                                        :post (deactivate-mark))
2528     "Selected"
2529     ;; select
2530     ;; ("e"  er/expand-region "+")
2531     ("SPC"  er/expand-region "+")
2532     ;; ("c"  er/contract-region "-")
2533     ("S-SPC"  er/contract-region "-")
2534     ("r"  (lambda ()
2535             (interactive)
2536             (er/contract-region 0))
2537      "reset")
2538
2539     ("i'" er/mark-inside-quotes "in")
2540     ("i\"" er/mark-inside-quotes nil)
2541     ("o'" er/mark-outside-quotes "out")
2542     ("o\"" er/mark-outside-quotes nil)
2543
2544     ("i{" er/mark-inside-pairs nil)
2545     ("i(" er/mark-inside-pairs nil)
2546     ("o{" er/mark-inside-pairs nil)
2547     ("o(" er/mark-inside-pairs nil)
2548
2549     ("p"  er/mark-paragraph "paragraph")
2550
2551     ("l"  sd/mark-line "line")
2552     ("u"  er/mark-url "url")
2553     ("f"  er/mark-defun "fun")
2554     ("n"  er/mark-next-accessor "next")
2555
2556     ("x" exchange-point-and-mark "exchange")
2557
2558     ;; Move
2559     ;; ("h" backward-char nil)
2560     ;; ("j" next-line nil)
2561     ;; ("k" previous-line nil)
2562     ;; ("l" foreward-char nil)
2563
2564     ;; Search
2565     ;; higlight
2566
2567     ;; exit
2568     ("d" kill-region "delete" :exit t)
2569
2570     ("y" kill-ring-save "yank" :exit t)
2571     ("M-SPC" nil "quit" :exit t)
2572     ("C-SPC" nil "quit" :exit t)
2573     ("q" nil "quit" :exit t))
2574
2575   (global-set-key (kbd "C-SPC") (lambda ()
2576                                   (interactive)
2577                                   (set-mark-command nil)
2578                                   (er/mark-word)
2579                                   (sd/expand-selected/body)))
2580 #+END_SRC
2581
2582 *** TODO make expand-region hydra work with lispy selected
2583
2584 * TODO Convert ASCII to key
2585 ** map =function-key-map= [[http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm][ascii_ctrl]]
2586 new file =C-x C-f C-f=
2587
2588 ** write color syntax for =Man-mode=
2589
2590 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2591   (font-lock-add-keywords 'perl-mode '(("\\(|\\w+?\\(,\\w+?\\)?|\\)" 1 'py-builtins-face)))
2592 #+END_SRC
2593
2594 * TODO jump last change point
2595 * TODO emms mode-line
2596
2597 * =C-u C-h a= search funtions 
2598 =apropos-command= 
2599
2600 * key
2601 - passion
2602 - vision
2603 - mission
2604
2605 * M prefix
2606 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
2607
2608   ;; M-h - mark-paragraph
2609   ;; M-j - new line indent
2610   ;; M-k - kill-sentence
2611   ;; M-l - downcase words     ====== goto-line
2612   ;; M-; - comments
2613   ;; M-'- abbrev-prefix-mark  =======
2614   ;; M-RET none                ======
2615
2616   ;; M-y - yank
2617   ;; M-u - upcase word
2618   ;; M-i -                 ====== imenu
2619   ;; M-o             ====== prefix
2620   ;; M-p
2621   ;; M-[                   =====
2622   ;; M-]              ========
2623   ;; M-\   deleter horizontal space =====
2624
2625   ;; M-n 
2626   ;; M-m ---- indent
2627   ;; M-, --- mark stack====== =
2628   ;; M-. lisp-goto-symboo, find definitation
2629
2630   ;; M-a 
2631   ;; M-s ===== 
2632   ;; M-d
2633   ;; M-f
2634   ;; M-g - prefix
2635
2636   ;; M-q - lisp fill, fill paragraph
2637   ;; M-w 
2638   ;; M-e
2639   ;; M-r  - rePositioning 
2640   ;; M-t transpose word 
2641
2642   ;; M-z zap-to-char
2643   ;; M-x
2644   ;; M-c  capitalize-word
2645   ;; M-v 
2646   ;; M-b 
2647 #+END_SRC
2648