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