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