 ;;;; Ivy END
 
 ;;;; Navigation START
-(use-package isearch
-  :commands (isearch-forward isearch-backward)
-  :defer 5
-  :preface
-  (provide 'isearch)
-  :config
-  (use-package char-fold)
-
-  (vsetq search-default-mode #'char-fold-to-regexp)
-
-  ;; Prepopulate isearch with selectionn.
-  ;; From http://www.reddit.com/r/emacs/comments/2amn1v/isearch_selected_text
-  (defadvice isearch-mode (around isearch-mode-default-string
-                                  (forward &optional regexp op-fun recursive-edit word-p) activate)
-    "Enable isearch to start with current selection."
-    (if (and transient-mark-mode mark-active (not (eq (mark) (point))))
-        (progn
-          (isearch-update-ring (buffer-substring-no-properties (mark) (point)))
-          (deactivate-mark)
-          ad-do-it
-          (if (not forward)
-              (isearch-repeat-backward)
-            (goto-char (mark))
-            (isearch-repeat-forward)))
-      ad-do-it)))
+
+(add-hook 'emacs-startup-hook
+          (lambda ()
+            (use-package isearch
+              :commands (isearch-forward isearch-backward)
+              :defer 5
+              :preface
+              (provide 'isearch)
+              :config
+              (use-package char-fold)
+
+              (vsetq search-default-mode #'char-fold-to-regexp)
+
+              ;; Prepopulate isearch with selectionn.
+              ;; From http://www.reddit.com/r/emacs/comments/2amn1v/isearch_selected_text
+              (defadvice isearch-mode (around isearch-mode-default-string
+                                              (forward &optional regexp op-fun recursive-edit word-p) activate)
+                "Enable isearch to start with current selection."
+                (if (and transient-mark-mode mark-active (not (eq (mark) (point))))
+                    (progn
+                      (isearch-update-ring (buffer-substring-no-properties (mark) (point)))
+                      (deactivate-mark)
+                      ad-do-it
+                      (if (not forward)
+                          (isearch-repeat-backward)
+                        (goto-char (mark))
+                        (isearch-repeat-forward)))
+                  ad-do-it)))))
 
 ;; From http://endlessparentheses.com/emacs-narrow-or-widen-dwim.html
 (defun ar/narrow-or-widen-dwim (p)

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

(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


      ;; comint-magic-space needs to be whitelisted to ensure we receive company-begin events in eshell.
      (when (boundp 'company-begin-commands)
        (setq-local company-begin-commands
                    (append company-begin-commands (list 'comint-magic-space))))

      (bind-key "C-l" #'ar/eshell-cd-to-parent eshell-mode-map))
    :config
