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