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