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