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