+* Mu4e
+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]]
+
+** OfflineImap - download all mails from IMAP into local directory, and keep in sync
+#+BEGIN_SRC sh :results output replace
+ # offline-imap
+ brew install offline-imap
+
+ cp /usr/local/etc/offlineimap.conf ~/.offlineimapr
+
+ #For the =offlineimap= config on mac, using =sslcacertfile= instead of =cert_fingerpring=. On Mac
+ sslcacertfile = /usr/local/etc/openssl/cert.pem
+#+END_SRC
+
+#+BEGIN_SRC conf
+ [general]
+ ui=TTYUI
+ accounts = Gmail
+ autorefresh = 5
+
+ [Account Gmail]
+ localrepository = Gmail-Local
+ remoterepository = Gmail-Remote
+
+ [Repository Gmail-Local]
+ type = Maildir
+ localfolders = ~/.Mail/seudut@gmail.com
+
+ [Repository Gmail-Remote]
+ type = Gmail
+ remotehost = imap.gmail.com
+ remoteuser = seudut@gmail.com
+ remotepass = xxxxxxxx
+ realdelete = no
+ ssl = yes
+ #cert_fingerprint = <insert gmail server fingerprint here>
+ sslcacertfile = /usr/local/etc/openssl/cert.pem
+ maxconnections = 1
+ folderfilter = lambda folder: folder not in ['[Gmail]/Trash',
+ '[Gmail]/Spam',
+ '[Gmail]/All Mail',
+ ]
+#+END_SRC
+
+Then, run =offlineimap= to sync the mail
+
+** Mu - fast search, view mails and extract attachments.
+#+BEGIN_SRC sh
+ EMACS=/usr/local/bin/emacs brew install mu --with-emacs
+#+END_SRC
+
+Then, run =mu index --maildir=~/.Mail=
+
+** Mu4e - Emacs frontend of Mu
+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]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (require 'mu4e)
+ (setq mu4e-maildir "~/.Mail")
+ (setq mu4e-drafts-folder "/[Gmail].Drafts")
+ (setq mu4e-sent-folder "/[Gmail].Sent Mail")
+ ;; don't save message to Sent Messages, Gmail/IMAP takes care of this
+ (setq mu4e-sent-messages-behavior 'delete)
+ ;; allow for updating mail using 'U' in the main view:
+ (setq mu4e-get-mail-command "offlineimap")
+
+ ;; shortcuts
+ (setq mu4e-maildir-shortcuts
+ '( ("/INBOX" . ?i)
+ ("/[Gmail].Sent Mail" . ?s)))
+
+ ;; something about ourselves
+ (setq
+ user-mail-address "seudut@gmail.com"
+ user-full-name "Peng Li"
+ mu4e-compose-signature
+ (concat
+ "Thanks,\n"
+ "Peng\n"))
+
+ ;; show images
+ (setq mu4e-show-images t)
+
+ ;; use imagemagick, if available
+ (when (fboundp 'imagemagick-register-types)
+ (imagemagick-register-types))
+
+ ;; convert html emails properly
+ ;; Possible options:
+ ;; - html2text -utf8 -width 72
+ ;; - textutil -stdin -format html -convert txt -stdout
+ ;; - html2markdown | grep -v ' _place_holder;' (Requires html2text pypi)
+ ;; - w3m -dump -cols 80 -T text/html
+ ;; - view in browser (provided below)
+ (setq mu4e-html2text-command "textutil -stdin -format html -convert txt -stdout")
+
+ ;; spell check
+ (add-hook 'mu4e-compose-mode-hook
+ (defun my-do-compose-stuff ()
+ "My settings for message composition."
+ (set-fill-column 72)
+ (flyspell-mode)))
+
+ ;; add option to view html message in a browser
+ ;; `aV` in view to activate
+ (add-to-list 'mu4e-view-actions
+ '("ViewInBrowser" . mu4e-action-view-in-browser) t)
+
+ ;; fetch mail every 10 mins
+ (setq mu4e-update-interval 600)
+
+ ;; mu4e view
+ (setq-default mu4e-headers-fields '((:flags . 6)
+ (:from-or-to . 22)
+ (:mailing-list . 20)
+ (:thread-subject . 70)
+ (:human-date . 16)))
+#+END_SRC
+
+** Smtp - send mail
+- =gnutls=, depends on =gnutls=, first confirm this is installed, otherwise, =brew install gnutls=
+- =~/.authinfo=
+#+BEGIN_SRC fundamental
+ machine smtp.gmail.com login <gmail username> password <gmail password>
+#+END_SRC
+- OPTIONAL, encrypt the =~/.authinfo= file
+#+BEGIN_SRC sh :results output replace
+ gpg --output ~/.authinfo.gpg --symmetric ~/.authinfo
+#+END_SRC
+
+* Ediff
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (with-eval-after-load 'ediff
+ (setq ediff-split-window-function 'split-window-horizontally)
+ (setq ediff-window-setup-function 'ediff-setup-windows-plain)
+ (add-hook 'ediff-startup-hook 'ediff-toggle-wide-display)
+ (add-hook 'ediff-cleanup-hook 'ediff-toggle-wide-display)
+ (add-hook 'ediff-suspend-hook 'ediff-toggle-wide-display))
+#+END_SRC
+
+* Entertainment
+** GnuGo
+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
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package gnugo
+ :ensure t
+ :defer t
+ :init
+ (require 'gnugo-imgen)
+ (setq gnugo-xpms 'gnugo-imgen-create-xpms)
+ (add-hook 'gnugo-start-game-hook '(lambda ()
+ (gnugo-image-display-mode)
+ (gnugo-grid-mode)))
+ :config
+ (add-to-list 'gnugo-option-history (format "--boardsize 19 --color black --level 1")))
+#+END_SRC
+
+** Emms
+We can use [[https://www.gnu.org/software/emms/quickstart.html][Emms]] for multimedia in Emacs
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package emms
+ :ensure t
+ :init
+ (setq emms-directory (concat sd-temp-directory "emms"))
+ (setq emms-source-file-default-directory "~/Music/")
+ :config
+ (emms-standard)
+ (emms-default-players)
+ (define-emms-simple-player mplayer '(file url)
+ (regexp-opt '(".ogg" ".mp3" ".mgp" ".wav" ".wmv" ".wma" ".ape"
+ ".mov" ".avi" ".ogm" ".asf" ".mkv" ".divx" ".mpeg"
+ "http://" "mms://" ".rm" ".rmvb" ".mp4" ".flac" ".vob"
+ ".m4a" ".flv" ".ogv" ".pls"))
+ "mplayer" "-slave" "-quiet" "-really-quiet" "-fullscreen")
+ (emms-history-load))
+#+END_SRC
+
+* Dictionary
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package bing-dict
+ :ensure t
+ :init
+ (global-set-key (kbd "s-d") 'bing-dict-brief)
+ :commands (bing-dict-brief))
+#+END_SRC
+
+* Key Bindings
+Here are some global key bindings for basic editting
+** Esc in minibuffer
+Use =ESC= to exit minibuffer. Also I map =Super-h= the same as =C-g=
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-ns-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-isearch-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-completion-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-must-match-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-must-match-filename-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-filename-completion-map [escape] 'keyboard-escape-quit)
+ (define-key minibuffer-local-filename-must-match-map [escape] 'keyboard-escape-quit)
+
+ ;; Also map s-h same as C-g
+ (define-key minibuffer-local-map (kbd "s-h") 'keyboard-escape-quit)
+#+END_SRC
+
+** Project operations - =super=
+*** Projectile
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package projectile
+ :ensure t
+ :init
+ (setq projectile-enable-caching t)
+ (setq projectile-switch-project-action (lambda ()
+ (projectile-dired)
+ (sd/project-switch-action)))
+ (setq projectile-cache-file (concat sd-temp-directory "projectile.cache"))
+ :config
+ (add-to-list 'projectile-globally-ignored-files "GTAGS")
+ (projectile-global-mode t))
+
+ (use-package persp-projectile
+ :ensure t
+ :config
+ (persp-mode)
+ :bind
+ (:map projectile-mode-map
+ ("s-t" . projectile-persp-switch-project)))
+
+ ;; (defun sd/change-default-directory (buffer dir)
+ ;; "change defafult directory of buffer to dir"
+ ;; (with-current-buffer buffer
+ ;; (cd dir)))
+
+ ;; change default-directory of scratch buffer to projectile-project-root
+ (defun sd/project-switch-action ()
+ "Change default-directory of scratch buffer to current projectile-project-root directory"
+ (interactive)
+ (dolist (buffer (buffer-list))
+ (if (string-match (concat "scratch.*" (projectile-project-name))
+ (buffer-name buffer))
+ (let ((root (projectile-project-root)))
+ (with-current-buffer buffer
+ (cd root)))
+ ;; (sd/change-default-directory buffer (projectile-project-root))
+ )))
+#+END_SRC
+
+*** project config =super= keybindings
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ ;; (global-set-key (kbd "s-h") 'keyboard-quit)
+ ;; (global-set-key (kbd "s-j") 'ido-switch-buffer)
+ ;; (global-set-key (kbd "s-k") 'ido-find-file)
+ ;; (global-set-key (kbd "s-l") 'sd/delete-current-window)
+ ;; s-l --> goto-line
+ ;; (global-set-key (kbd "s-/") 'swiper)
+ ;; s-; -->
+ ;; s-' --> 'next-multiframe-window
+ (global-set-key (kbd "<s-return>") 'toggle-frame-fullscreen)
+
+ (global-set-key (kbd "s-f") 'projectile-find-file)
+ (global-set-key (kbd "s-`") 'mode-line-other-buffer)
+
+ (global-set-key (kbd "s-n") 'persp-next)
+ (global-set-key (kbd "s-p") 'persp-prev)
+ (global-set-key (kbd "s-;") 'persp-switch-last)
+
+ (global-set-key (kbd "s-=") 'text-scale-increase)
+ (global-set-key (kbd "s--") 'text-scale-decrease)
+
+ ;; (global-set-key (kbd "s-u") 'undo-tree-visualize)
+
+
+ ;; someothers default mapping on super (command) key
+ ;; s-s save-buffer
+ ;; s-k kill-this-buffer
+
+
+ ;; s-h --> ns-do-hide-emacs
+ ;; s-j --> ido-switch-buffer +
+ ;; s-k --> kill-this-buffer
+ ;; s-l --> goto-line
+ ;; s-; --> undefined
+ ;; s-' --> next-multiframe-window
+ ;; s-ret --> toggle-frame-fullscreen +
+
+ ;; s-y --> ns-paste-secondary
+ ;; s-u --> revert-buffer
+ ;; s-i --> undefined - but used for iterm globally
+ ;; s-o --> used for emacs globally
+ ;; s-p --> projectile-persp-switch-project +
+ ;; s-[ --> next-buffer +
+ ;; s-] --> previous-buffer +
+
+ ;; s-0 --> undefined
+ ;; s-9 --> undefined
+ ;; s-8 --> undefined
+ ;; s-7 --> undefined
+ ;; s-6 --> undefined
+ ;; s-- --> center-line
+ ;; s-= --> undefined
+
+ ;; s-n --> make-frame
+ ;; s-m --> iconify-frame
+ ;; s-b --> undefined
+ ;; s-, --> customize
+ ;; s-. --> undefined
+ ;; s-/ --> undefined
+
+ ;; s-g --> isearch-repeat-forward
+ ;; s-f --> projectile-find-file +
+ ;; s-d --> isearch-repeat-background
+ ;; s-s --> save-buffer
+ ;; s-a --> make-whole-buffer
+
+ ;; s-b --> undefined
+ ;; s-v --> yank
+ ;; s-c --> ns-copy-including-secondary
+
+ ;; s-t --> ns-popup-font-panel
+ ;; s-r --> undefined
+ ;; s-e --> isearch-yanqk-kill
+ ;; s-w --> delete-frame
+ ;; s-q --> same-buffers-kill-emacs
+
+ ;; s-` --> other-frame
+#+END_SRC
+
+** Windown & Buffer - =C-o=
+Defind a =hydra= function for windows, buffer & bookmark operations. And map it to =C-o= globally.
+Most use =C-o C-o= to switch buffers; =C-o x, v= to split window; =C-o o= to delete other windows
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (winner-mode 1)
+
+ (defun sd/delete-current-window ()
+ (interactive)
+ (if (> (length (window-list)) 1)
+ (delete-window)
+ (message "Only one Windows now!")))
+
+ (defun sd/toggle-max-windows ()
+ "Set maximize current if there are multiple windows, if only
+ one window, window undo"
+ (interactive)
+ (if (equal (length (window-list)) 1)
+ (winner-undo)
+ (delete-other-windows)))
+
+ (defhydra sd/hydra-window (:color red :columns nil)
+ "Window"
+ ;; windows switch
+ ("h" windmove-left nil :exit t)
+ ("j" windmove-down nil :exit t)
+ ("k" windmove-up nil :exit t)
+ ("l" windmove-right nil :exit t)
+ ("C-o" other-window nil :exit t)
+ ;; window resize
+ ("H" hydra-move-splitter-left nil)
+ ("J" hydra-move-splitter-down nil)
+ ("K" hydra-move-splitter-up nil)
+ ("L" hydra-move-splitter-right nil)
+ ;; windows split
+ ("v" (lambda ()
+ (interactive)
+ (split-window-right)
+ (windmove-right))
+ "vert" :exit t)
+ ("x" (lambda ()
+ (interactive)
+ (split-window-below)
+ (windmove-down))
+ "horz" :exit t)
+ ;; buffer / windows switch
+ ("o" sd/toggle-max-windows "one" :exit t)
+ ("C-k" sd/delete-current-window "del" :exit t)
+ ("C-d" (lambda ()
+ (interactive)
+ (kill-buffer)
+ (sd/delete-current-window))
+ "kill" :exit t)
+
+ ;; ace-window
+ ;; ("'" other-window "other" :exit t)
+ ;; ("a" ace-window "ace")
+ ("s" ace-swap-window "swap")
+ ("D" ace-delete-window "ace-one" :exit t)
+ ;; ("i" ace-maximize-window "ace-one" :exit t)
+ ;; Windows undo - redo
+ ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
+ ("r" (progn (winner-redo) (setq this-command 'winner-redo)) "redo")
+
+ ;; ibuffer, dired, eshell, bookmarks
+ ;; ("C-i" other-window nil :exit t)
+ ("C-b" ido-switch-buffer nil :exit t)
+ ("C-f" projectile-find-file nil :exit t)
+ ("C-p" persp-switch :exit t)
+
+ ;; other special buffers
+ ("d" sd/project-or-dired-jump nil :exit t)
+ ("b" ibuffer nil :exit t)
+ ("t" multi-term nil :exit t)
+ ("e" sd/toggle-project-eshell nil :exit t)
+ ("m" bookmark-jump-other-window nil :exit t)
+ ("M" bookmark-set nil :exit t)
+ ("g" magit-status nil :exit t)
+ ;; ("p" paradox-list-packages nil :exit t)
+
+ ;; quit
+ ("q" nil "cancel")
+ ("<ESC>" nil)
+ ("C-h" nil nil :exit t)
+ ("C-j" nil nil :exit t)
+ ;; ("C-k" nil :exit t)
+ ("C-l" nil nil :exit t)
+ ("C-;" nil nil :exit t)
+ ("n" nil nil :exit t)
+ ("[" nil nil :exit t)
+ ("]" nil nil :exit t)
+ ("f" nil))
+
+ (global-unset-key (kbd "C-o"))
+ (global-set-key (kbd "C-o") 'sd/hydra-window/body)
+
+ (defun sd/project-or-dired-jump ()
+ "If under project, jump to the root directory, otherwise
+ jump to dired of current file"
+ (interactive)
+ (if (projectile-project-p)
+ (projectile-dired)
+ (dired-jump)))
+#+END_SRC
+
+** Motion
+- =C-M-=
+[[https://www.masteringemacs.org/article/effective-editing-movement][effective-editing-movement]]
+*** Command Arguments, numeric argumens
+=C-u 4= same as =C-4=, =M-4=
+*** Basic movement
+moving by line / word /
+=C-f=, =C-b=, =C-p=, =C-n=, =M-f=, =M-b=
+=C-a=, =C-e=
+=M-m= (move first non-whitespace on this line)
+=M-}=, =M-{=, Move forward end of paragraph
+=M-a=, =M-e=, beginning / end of sentence
+=C-M-a=, =C-M-e=, move begining of defun
+=C-x ]=, =C-x [=, forward/backward one page
+=C-v=, =M-v=, =C-M-v=, =C-M-S-v= scroll down/up
+=M-<=, =M->=, beginning/end of buffer
+=M-r=, Repositiong point
+
+*** Moving by S-expression / List
+*** Marks
+=C-<SPC>= set marks toggle the region
+=C-u C-<SPC>= Jump to the mark, repeated calls go further back the mark ring
+=C-x C-x= Exchanges the point and mark.
+
+Stolen [[https://www.masteringemacs.org/article/fixing-mark-commands-transient-mark-mode][fixing-mark-commands-transient-mark-mode]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defun push-mark-no-activate ()
+ "Pushes `point' to `mark-ring' and does not activate the region
+ Equivalent to \\[set-mark-command] when \\[transient-mark-mode] is disabled"
+ (interactive)
+ (push-mark (point) t nil)
+ (message "Pushed mark to ring"))
+
+ ;; (global-set-key (kbd "C-`") 'push-mark-no-activate)
+
+ (defun jump-to-mark ()
+ "Jumps to the local mark, respecting the `mark-ring' order.
+ This is the same as using \\[set-mark-command] with the prefix argument."
+ (interactive)
+ (set-mark-command 1))
+
+ ;; (global-set-key (kbd "M-`") 'jump-to-mark)
+
+ (defun exchange-point-and-mark-no-activate ()
+ "Identical to \\[exchange-point-and-mark] but will not activate the region."
+ (interactive)
+ (exchange-point-and-mark)
+ (deactivate-mark nil))
+
+ ;; (define-key global-map [remap exchange-point-and-mark] 'exchange-point-and-mark-no-activate)
+#+END_SRC
+
+Show the mark ring using =helm-mark-ring=, also mapping =M-`= to quit minibuffer. so that =M-`= can
+toggle the mark ring. the best way is add a new action and mapping to =helm-source-mark-ring=, but
+since there is no map such as =helm-mark-ring=map=, so I cannot binding a key to the quit action.
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (setq mark-ring-max 50)
+
+ (use-package helm
+ :ensure t
+ :init
+ (global-set-key (kbd "M-`") #'helm-mark-ring))
+
+ (define-key minibuffer-local-map (kbd "M-`") 'keyboard-escape-quit)
+#+END_SRC
+
+=M-h= marks the next paragraph
+=C-x h= marks the whole buffer
+=C-M-h= marks the next defun
+=C-x C-p= marks the next page
+*** Registers
+Registers can save text, position, rectangles, file and configuration and other things.
+Here for movement, we can use register to save/jump position
+=C-x r SPC= store point in register
+=C-x r j= jump to register
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package list-register
+ :ensure t)
+#+END_SRC
+
+*** Bookmarks
+As I would like use bookmakr for different buffer/files. to help to swith
+different buffer/file quickly. this setting is in Windows/buffer node
+=C-x r m= set a bookmarks
+=C-x r l= list bookmarks
+=C-x r b= jump to bookmarks
+
+*** Search
+Search, replace and hightlight will in later paragraph
+*** =Avy= for easy motion
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package avy
+ :ensure t
+ :config
+ (avy-setup-default))
+
+ (global-set-key (kbd "C-M-j") 'avy-goto-line-below)
+ (global-set-key (kbd "C-M-n") 'avy-goto-line-below)
+ (global-set-key (kbd "C-M-k") 'avy-goto-line-above)
+ (global-set-key (kbd "C-M-p") 'avy-goto-line-above)
+
+ (global-set-key (kbd "C-M-f") 'avy-goto-word-1-below)
+ (global-set-key (kbd "C-M-b") 'avy-goto-word-1-above)
+
+ ;; (global-set-key (kbd "M-g e") 'avy-goto-word-0)
+ (global-set-key (kbd "C-M-w") 'avy-goto-char-timer)
+ (global-set-key (kbd "C-M-l") 'avy-goto-char-in-line)
+
+ ;; ;; will delete above
+ ;; (global-set-key (kbd "M-g j") 'avy-goto-line-below)
+ ;; (global-set-key (kbd "M-g k") 'avy-goto-line-above)
+ ;; (global-set-key (kbd "M-g w") 'avy-goto-word-1-below)
+ ;; (global-set-key (kbd "M-g b") 'avy-goto-word-1-above)
+ ;; (global-set-key (kbd "M-g e") 'avy-goto-word-0)
+ ;; (global-set-key (kbd "M-g f") 'avy-goto-char-timer)
+ ;; (global-set-key (kbd "M-g c") 'avy-goto-char-in-line)
+#+END_SRC
+
+*** =Imenu= goto tag
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (global-set-key (kbd "M-i") #'counsel-imenu)
+ ;; (global-set-key (kbd "M-i") #'imenu)
+
+ ;; define M-[ as C-M-a
+ ;; http://ergoemacs.org/emacs/emacs_key-translation-map.html
+ (define-key key-translation-map (kbd "M-[") (kbd "C-M-a"))
+ (define-key key-translation-map (kbd "M-]") (kbd "C-M-e"))
+#+END_SRC
+
+*** Go-to line
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (global-set-key (kbd "M-l") 'goto-line)
+#+END_SRC
+
+** Edit
+*** basic editting
+- cut, yank, =C-w=, =C-y=
+- save, revert
+- undo, redo - undo-tree
+- select, expand-region
+- spell check, flyspell
+
+*** Kill ring
+=helm-show-kill-ring=
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (setq kill-ring-max 100) ; default is 60p
+
+ (use-package helm
+ :ensure t
+ :init
+ (global-set-key (kbd "M-y") #'helm-show-kill-ring))
+#+END_SRC
+
+*** undo-tree
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package undo-tree
+ :ensure t
+ :config
+ (define-key undo-tree-visualizer-mode-map "j" 'undo-tree-visualize-redo)
+ (define-key undo-tree-visualizer-mode-map "k" 'undo-tree-visualize-undo)
+ (define-key undo-tree-visualizer-mode-map "h" 'undo-tree-visualize-switch-branch-left)
+ (define-key undo-tree-visualizer-mode-map "l" 'undo-tree-visualize-switch-branch-right)
+ (global-undo-tree-mode 1))
+
+ (global-set-key (kbd "s-u") 'undo-tree-visualize)
+#+END_SRC
+
+*** flyspell
+Stolen from [[https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-spelling.el][here]], hunspell will search dictionary in =DICPATH=
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (setenv "DICPATH" "/usr/local/share/hunspell")
+
+ (when (executable-find "hunspell")
+ (setq-default ispell-program-name "hunspell")
+ (setq ispell-really-hunspell t))
+
+ ;; (defun text-mode-hook-setup ()
+ ;; ;; Turn off RUN-TOGETHER option when spell check text-mode
+ ;; (setq-local ispell-extra-args (flyspell-detect-ispell-args)))
+ ;; (add-hook 'text-mode-hook 'text-mode-hook-setup)
+ ;; (add-hook 'text-mode-hook 'flyspell-mode)
+
+ ;; enable flyspell check on comments and strings in progmamming modes
+ ;; (add-hook 'prog-mode-hook 'flyspell-prog-mode)
+
+ ;; I don't use the default mappings
+ (with-eval-after-load 'flyspell
+ (define-key flyspell-mode-map (kbd "C-;") nil)
+ (define-key flyspell-mode-map (kbd "C-,") nil)
+ (define-key flyspell-mode-map (kbd "C-.") nil))
+#+END_SRC
+
+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]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ ;; NO spell check for embedded snippets
+ (defadvice org-mode-flyspell-verify (after org-mode-flyspell-verify-hack activate)
+ (let ((rlt ad-return-value)
+ (begin-regexp "^[ \t]*#\\+begin_\\(src\\|html\\|latex\\)")
+ (end-regexp "^[ \t]*#\\+end_\\(src\\|html\\|latex\\)")
+ old-flag
+ b e)
+ (when ad-return-value
+ (save-excursion
+ (setq old-flag case-fold-search)
+ (setq case-fold-search t)
+ (setq b (re-search-backward begin-regexp nil t))
+ (if b (setq e (re-search-forward end-regexp nil t)))
+ (setq case-fold-search old-flag))
+ (if (and b e (< (point) e)) (setq rlt nil)))
+ (setq ad-return-value rlt)))
+#+END_SRC
+
+** Search & Replace / hightlight =M-s=
+*** isearch
+=C-s=, =C-r=,
+=C-w= add word at point to search string,
+=M-%= query replace
+=C-M-y= add character at point to search string
+=M-s C-e= add reset of line at point
+=C-y= yank from clipboard to search string
+=M-n=, =M-p=, history
+=C-M-i= complete search string
+set the isearch history size, the default is only =16=
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (setq history-length 5000)
+ (setq regexp-search-ring-max 1000)
+ (setq search-ring-max 1000)
+
+ ;; when search a word or a symbol , also add the word into regexp-search-ring
+ (defadvice isearch-update-ring (after sd/isearch-update-ring (string &optional regexp) activate)
+ "Add search-ring to regexp-search-ring"
+ (unless regexp
+ (add-to-history 'regexp-search-ring string regexp-search-ring-max)))
+#+END_SRC
+
+*** =M-s= prefix
+use the prefix =M-s= for searching in buffers
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defun sd/make-keymap (key bindings)
+ (setq keymap (make-sparse-keymap))
+ (dolist (binding bindings)
+ (define-key keymap (car binding) (cdr binding)))
+ (global-set-key key keymap))
+
+ ;; (sd/make-keymap "\M-s"
+ ;; '(("w" . save-buffer)
+ ;; ;; ("\M-w" . save-buffer)
+ ;; ("e" . revert-buffer)
+ ;; ("s" . isearch-forward-regexp)
+ ;; ("\M-s" . isearch-forward-regexp)
+ ;; ("r" . isearch-backward-regexp)
+ ;; ("." . isearch-forward-symbol-at-point)
+ ;; ("o" . occur)
+ ;; ;; ("h" . highlight-symbol-at-point)
+ ;; ("h" . highlight-symbol)
+ ;; ("m" . highlight-regexp)
+ ;; ("l" . highlight-lines-matching-regexp)
+ ;; ("M" . unhighlight-regexp)
+ ;; ("f" . keyboard-quit)
+ ;; ("q" . keyboard-quit)))
+#+END_SRC
+
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package highlight-symbol
+ :ensure t)
+
+ (defhydra sd/search-replace (:color red :columns nil)
+ "Search"
+ ("w" save-buffer "save" :exit t)
+ ("e" revert-buffer "revert" :exit t)
+ ("u" undo-tree-visualize "undo" :exit t)
+ ("s" isearch-forward-regexp "s-search" :exit t)
+ ("M-s" isearch-forward-regexp "s-search" :exit t)
+ ("r" isearch-backward-regexp "r-search" :exit t)
+ ("." isearch-forward-symbol-at-point "search point" :exit t)
+ ("/" swiper "swiper" :exit t)
+ ("o" occur "occur" :exit t)
+ ("h" highlight-symbol "higlight" :exit t)
+ ("l" highlight-lines-matching-regexp "higlight line" :exit t)
+ ("m" highlight-regexp "higlight" :exit t)
+ ("M" unhighlight-regexp "unhiglight" :exit t)
+ ("q" nil "quit")
+ ("f" nil))
+
+ (global-unset-key (kbd "M-s"))
+ (global-set-key (kbd "M-s") 'sd/search-replace/body)
+
+
+ ;; search and replace and highlight
+ (define-key isearch-mode-map (kbd "M-s") 'isearch-repeat-forward)
+ (define-key isearch-mode-map (kbd "M-r") 'isearch-repeat-backward)
+ (global-set-key (kbd "s-[") 'highlight-symbol-next)
+ (global-set-key (kbd "s-]") 'highlight-symbol-prev)
+ (global-set-key (kbd "s-\\") 'highlight-symbol-query-replace)
+#+END_SRC
+
+*** Occur
+Occur search key bindings
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defun sd/occur-keys ()
+ "My key bindings in occur-mode"
+ (interactive)
+ (switch-to-buffer-other-window "*Occur*")
+ (define-key occur-mode-map (kbd "C-o") nil)
+ (define-key occur-mode-map (kbd "C-n") (lambda ()
+ (interactive)
+ (occur-next)
+ (occur-mode-goto-occurrence-other-window)
+ (recenter)
+ (other-window 1)))
+ (define-key occur-mode-map (kbd "C-p") (lambda ()
+ (interactive)
+ (occur-prev)
+ (occur-mode-goto-occurrence-other-window)
+ (recenter)
+ (other-window 1))))
+
+ (add-hook 'occur-hook #'sd/occur-keys)
+
+ (use-package color-moccur
+ :ensure t
+ :commands (isearch-moccur isearch-all)
+ :init
+ (setq isearch-lazy-highlight t)
+ :config
+ (use-package moccur-edit))
+#+END_SRC
+
+*** Swiper
+stolen from [[https://github.com/mariolong/emacs.d/blob/f6a061594ef1b5d1f4750e9dad9dc97d6e122840/emacs-init.org][here]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package swiper
+ :ensure t
+ :init
+ (setq ivy-use-virtual-buffers t)
+ (set-face-attribute 'ivy-current-match nil :background "Orange" :foreground "black")
+ :config
+ (ivy-mode)
+ (global-set-key (kbd "s-/") 'swiper)
+ (define-key swiper-map (kbd "M-r") 'swiper-query-replace)
+ (define-key swiper-map (kbd "C-.") (lambda ()
+ (interactive)
+ (insert (format "%s" (with-ivy-window (thing-at-point 'word))))))
+ (define-key swiper-map (kbd "M-.") (lambda ()
+ (interactive)
+ (insert (format "%s" (with-ivy-window (thing-at-point 'symbol)))))))
+#+END_SRC
+
+** Expand region map
+*** Install =expand-region=
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package expand-region
+ :ensure t
+ :config
+ ;; (global-set-key (kbd "C-=") 'er/expand-region)
+ )
+#+END_SRC
+
+*** Add a =hydra= map for =expand-region= operations
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defun sd/mark-line ()
+ "Mark current line without whitespace beginning"
+ (interactive)
+ (back-to-indentation)
+ (set-mark (line-end-position)))
+
+ (defhydra sd/expand-selected (:color red :columns nil
+ :post (deactivate-mark)
+ )
+ "Selected"
+ ;; select
+ ;; ("e" er/expand-region "+")
+ ("SPC" er/expand-region "+")
+ ;; ("c" er/contract-region "-")
+ ("S-SPC" er/contract-region "-")
+ ("r" (lambda ()
+ (interactive)
+ (er/contract-region 0))
+ "reset")
+
+ ("i'" er/mark-inside-quotes "in")
+ ("i\"" er/mark-inside-quotes nil)
+ ("o'" er/mark-outside-quotes "out")
+ ("o\"" er/mark-outside-quotes nil)
+
+ ("i{" er/mark-inside-pairs nil)
+ ("i(" er/mark-inside-pairs nil)
+ ("o{" er/mark-inside-pairs nil)
+ ("o(" er/mark-inside-pairs nil)
+
+ ("p" er/mark-paragraph "paragraph")
+
+ ("l" sd/mark-line "line")
+ ("u" er/mark-url "url")
+ ("f" er/mark-defun "fun")
+ ("n" er/mark-next-accessor "next")
+
+ ("x" exchange-point-and-mark "exchange")
+
+ ;; Search
+ ;; higlight
+
+ ;; exit
+ ("d" kill-region "delete" :exit t)
+
+ ("y" kill-ring-save "yank" :exit t)
+ ("M-SPC" nil "quit" :exit t)
+ ;; ("C-SPC" "quit" :exit t)
+ ("q" deactivate-mark "quit" :exit t))
+
+ (global-set-key (kbd "M-SPC") (lambda ()
+ (interactive)
+ (set-mark-command nil)
+ ;; (er/expand-region 1)
+ (er/mark-word)
+ (sd/expand-selected/body)))
+#+END_SRC
+
+*** TODO make expand-region hydra work with lispy selected
+** =C-w= delete backward word
+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]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defun sd/kill-region-or-backward-kill-word ()
+ (interactive)
+ (if (region-active-p)
+ (kill-region (point) (mark))
+ (backward-kill-word 1)))
+
+ (global-set-key (kbd "C-w") 'sd/kill-region-or-backward-kill-word)
+#+END_SRC
+