+** 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" windmove-left nil :exit t)
+ ("C-j" windmove-down nil :exit t)
+ ("C-k" windmove-up :exit t)
+ ("C-l" windmove-right 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