 
 ;;;; Hydra START
 
-(add-hook 'emacs-startup-hook
-          (lambda ()
-            (use-package hydra
-              :ensure t
-              :defer 5
-              :config
-              (vsetq hydra-is-helpful t)
-              (defhydra hydra-search (:color blue)
-                "search"
-                ("d" ar/helm-ag "search directory")
-                ("f" ar/find-dired-current-dir "find file")
-                ("a" ar/find-all-dired-current-dir "find all files")
-                ("i" ar/helm-ag-insert "insert match")
-                ("q" nil "quit"))
-              (defhydra hydra-quick-insert (:color blue)
-                "
+(add-hook! 'emacs-startup-hook
+           (use-package hydra
+             :ensure t
+             :defer 5
+             :config
+             (vsetq hydra-is-helpful t)
+             (defhydra hydra-search (:color blue)
+               "search"
+               ("d" ar/helm-ag "search directory")
+               ("f" ar/find-dired-current-dir "find file")
+               ("a" ar/find-all-dired-current-dir "find all files")
+               ("i" ar/helm-ag-insert "insert match")
+               ("q" nil "quit"))
+             (defhydra hydra-quick-insert (:color blue)
+               "
 Quick insert: _w_eb bookmark _b_acklog bookmark
               _t_odo _d_one
 "
-                ("w" ar/helm-org-add-bookmark nil)
-                ("b" ar/helm-org-add-backlog-link nil)
-                ("t" ar/org-add-todo nil)
-                ("d" ar/org-add-done nil)
-                ("q" nil nil :color blue))
-
-              (defun ar/hydra-open-dwim ()
-                "Choose \"open\" hydra based on current mode."
-                (interactive)
-                (cond ((derived-mode-p 'c-mode) (hydra-open-c-mode/body))
-                      ((derived-mode-p 'prog-mode) (hydra-open-prog-mode/body))
-                      (t (hydra-open/body))))
-
-              (defhydra hydra-open-c-mode (:color blue)
-                "open"
-                ("o" ff-find-other-file "other")
-                ("e" ar/platform-open-in-external-app "externally")
-                ("u" ar/platform-open-file-at-point "url at point")
-                ("b" ar/file-open-closest-build-file "build file")
-                ("q" nil "cancel"))
-
-              (defhydra hydra-open (:color blue)
-                "
+               ("w" ar/helm-org-add-bookmark nil)
+               ("b" ar/helm-org-add-backlog-link nil)
+               ("t" ar/org-add-todo nil)
+               ("d" ar/org-add-done nil)
+               ("q" nil nil :color blue))
+
+             (defun ar/hydra-open-dwim ()
+               "Choose \"open\" hydra based on current mode."
+               (interactive)
+               (cond ((derived-mode-p 'c-mode) (hydra-open-c-mode/body))
+                     ((derived-mode-p 'prog-mode) (hydra-open-prog-mode/body))
+                     (t (hydra-open/body))))
+
+             (defhydra hydra-open-c-mode (:color blue)
+               "open"
+               ("o" ff-find-other-file "other")
+               ("e" ar/platform-open-in-external-app "externally")
+               ("u" ar/platform-open-file-at-point "url at point")
+               ("b" ar/file-open-closest-build-file "build file")
+               ("q" nil "cancel"))
+
+             (defhydra hydra-open (:color blue)
+               "
 Open: _p_oint _e_xternally
 "
-                ("e" ar/platform-open-in-external-app nil)
-                ("p" ar/platform-open-file-at-point nil)
-                ("q" nil "cancel"))
-
-              (defhydra hydra-open-prog-mode (:color blue)
-                "open"
-                ("o" ff-find-other-file "other")
-                ("e" ar/platform-open-in-external-app "externally")
-                ("u" ar/platform-open-file-at-point "url at point")
-                ("b" ar/file-open-closest-build-file "build file")
-                ("q" nil "cancel"))
-
-              (defhydra hydra-git-gutter (:pre (git-gutter-mode +1))
-                "
+               ("e" ar/platform-open-in-external-app nil)
+               ("p" ar/platform-open-file-at-point nil)
+               ("q" nil "cancel"))
+
+             (defhydra hydra-open-prog-mode (:color blue)
+               "open"
+               ("o" ff-find-other-file "other")
+               ("e" ar/platform-open-in-external-app "externally")
+               ("u" ar/platform-open-file-at-point "url at point")
+               ("b" ar/file-open-closest-build-file "build file")
+               ("q" nil "cancel"))
+
+             (defhydra hydra-git-gutter (:pre (git-gutter-mode +1))
+               "
 Git: _n_ext     _s_tage  _d_iff
      _p_revious _k_ill _q_uit
 "
-                ("n" git-gutter:next-hunk nil)
-                ("p" git-gutter:previous-hunk nil)
-                ("s" git-gutter:stage-hunk nil)
-                ("k" (lambda ()
-                       (interactive)
-                       (git-gutter:revert-hunk)
-                       (call-interactively #'git-gutter:next-hunk)) nil)
-                ("d" git-gutter:popup-hunk nil)
-                ("q" nil nil :color blue))
-              :bind (("C-c s" . hydra-search/body)
-                     ("C-c x" . hydra-quick-insert/body)
-                     ("C-c o" . ar/hydra-open-dwim)
-                     ("C-c g" . hydra-git-gutter/body)))))
+               ("n" git-gutter:next-hunk nil)
+               ("p" git-gutter:previous-hunk nil)
+               ("s" git-gutter:stage-hunk nil)
+               ("k" (lambda ()
+                      (interactive)
+                      (git-gutter:revert-hunk)
+                      (call-interactively #'git-gutter:next-hunk)) nil)
+               ("d" git-gutter:popup-hunk nil)
+               ("q" nil nil :color blue))
+             :bind (("C-c s" . hydra-search/body)
+                    ("C-c x" . hydra-quick-insert/body)
+                    ("C-c o" . ar/hydra-open-dwim)
+                    ("C-c g" . hydra-git-gutter/body))))
+
 ;;;; Hydra END
 
 ;;;; Buffers START

      (insert (replace-regexp-in-string "^[^ ]*:" "" candidate))
      (indent-for-tab-command))
    (let ((helm-source-do-ag (helm-build-async-source "Silver Searcher inserter"
                                                      :init 'helm-ag--do-ag-set-command
                                                      :candidates-process 'helm-ag--do-ag-candidate-process
                                                      :action 'ar/insert-candidate
                                                      :nohighlight t
                                                      :requires-pattern 3
                                                      :candidate-number-limit 9999
                                                      :keymap helm-do-ag-map)))
      (call-interactively #'ar/helm-ag)))

  (defun ar/helm-ag (arg)

  :bind (("M-<up>" . drag-stuff-up)
         ("M-<down>" . drag-stuff-down)))

(add-hook! 'emacs-startup-hook
           ;; Remember history of things across launches (ie. kill ring).
           ;; From https://www.wisdomandwonder.com/wp-content/uploads/2014/03/C3F.html
           (use-package savehist
             :defer 2
             :config
             (vsetq savehist-file "~/.emacs.d/savehist")
             (vsetq savehist-save-minibuffer-history t)
             (vsetq history-length 1000)
             (vsetq savehist-additional-variables
                    '(kill-ring
                      search-ring
                      regexp-search-ring))
             (savehist-mode +1))
           (use-package whitespace
             :defer 5
             ;; Automatically remove whitespace on saving.
             :hook ((before-save . whitespace-cleanup)
                    (prog-mode . whitespace-mode))
             :config
             ;; When nil, fill-column is used instead.
             (vsetq whitespace-line-column nil)
             ;; Highlight empty lines, TABs, blanks at beginning/end, lines
             ;; longer than fill-column, and trailing blanks.
             (vsetq whitespace-style '(face empty tabs lines-tail trailing))
             (vsetq show-trailing-whitespace t)
             (set-face-attribute 'whitespace-line nil
                                 :foreground "DarkOrange1"
                                 :background "default"))
           (use-package smartparens
             :ensure t
             :defer 0.01
             ;; Add to minibuffer also.
             :hook ((minibuffer-setup . smartparens-mode)
                    (prog-mode . smartparens-strict-mode)
                    (eshell-mode . smartparens-strict-mode))
             :config
             (require 'smartparens-config)
             (require 'smartparens-html)
             (require 'smartparens-python)

             (defun ar/kill-region-advice-fun (orig-fun &rest r)
               "Advice function around `kill-region' (ORIG-FUN and R)."
               (if (or (null (nth 2 r)) ;; Consider kill-line (C-k).
                       mark-active)
                   (apply orig-fun r)
                 ;; Kill entire line.
                 (let ((last-command (lambda ())) ;; Override last command to avoid appending to kill ring.
                       (offset (- (point)
                                  (line-beginning-position))))
                   (apply orig-fun (list (line-beginning-position)
                                         (line-end-position)
                                         nil))
                   (delete-char 1)
                   (forward-char (min offset
                                      (- (line-end-position)
                                         (line-beginning-position)))))))

             (advice-add 'kill-region
                         :around
                         'ar/kill-region-advice-fun)

             ;; I prefer keeping C-w to DWIM kill, provided by
             ;; `ar/kill-region-advice-fun'. Removing remap.
             ;;   (define-key smartparens-strict-mode-map [remap kill-region] nil)

             (defun ar/smartparens-wrap-square-bracket (arg)
               "[] equivalent of `paredit-wrap-round'."
               (interactive "P")
               (save-excursion
                 (unless (sp-point-in-symbol)
                   (backward-char))
                 (sp-wrap-with-pair "["))
               (insert " "))

             :bind (:map smartparens-strict-mode-map
                         ([remap kill-region] . kill-region)
                         ("C-c <right>" . sp-forward-slurp-sexp)
                         ("C-c <left>" . sp-forward-barf-sexp)
                         ("M-[" . sp-rewrap-sexp)
                         :map smartparens-mode-map
                         ("M-]" . ar/smartparens-wrap-square-bracket))))


(use-package region-bindings-mode
