 
 ;; Embark stuff
 (use-package embark
+  :defer 1
   :straight t
   :bind (("C-." . embark-act)
          ("C-'" . embark-dwim)

  (setq prefix-help-command #'embark-prefix-help-command))
(use-package embark-consult
  :straight t
  :after (embark consult):demand
  t
  :hook (embark-collect-mode . consult-preview-at-point-mode))


(use-package vterm
  :straight t
  :defer t
  :commands (vterm meain/shell-toggle):init
  (global-set-key (kbd "M-;")
                  'meain/shell-toggle)
  :config (progn
            (evil-set-initial-state 'vterm-mode 'insert)
            (setq vterm-max-scrollback 100000)
            (setq vterm-kill-buffer-on-exit t)
            (define-key vterm-mode-map (kbd "M-c") 'meain/shell-new)
            (define-key vterm-mode-map (kbd "M-m") 'meain/shell-other)
            (define-key vterm-mode-map (kbd "M-w") 'delete-window)
            (define-key vterm-mode-map (kbd "M-u") 'universal-argument)
            (define-key vterm-mode-map (kbd "M-l") 'meain/move-swap-right)
            (define-key vterm-mode-map (kbd "M-h") 'meain/move-swap-left)
            (define-key vterm-mode-map (kbd "M-k") 'meain/move-swap-up)
            (define-key vterm-mode-map (kbd "M-j") 'meain/move-swap-down)
            (define-key vterm-mode-map (kbd "M-H") 'shrink-window-horizontally)
            (define-key vterm-mode-map (kbd "M-L") 'enlarge-window-horizontally)
            (define-key vterm-mode-map (kbd "M-K") 'shrink-window)
            (define-key vterm-mode-map (kbd "M-J") 'enlarge-window)
            ;; (define-key vterm-mode-map (kbd "M-f l") 'ace-link)
            (define-key vterm-mode-map (kbd "M-b") (lambda (&optional open-term)
                                                     (interactive "P")
                                                     (split-window-below)
                                                     (windmove-down)
                                                     (when open-term
                                                       (vterm t))))
            (define-key vterm-mode-map (kbd "M-v") (lambda (&optional open-term)
                                                     (interactive "P")
                                                     (split-window-right)
                                                     (windmove-right)
                                                     (when open-term
                                                       (vterm t))))
            (add-to-list 'display-buffer-alist
                         '((lambda (bufname _)
                             (s-starts-with-p "*popup-shell" bufname))
                           (display-buffer-reuse-window display-buffer-at-bottom)
                           (reusable-frames . visible)
                           (window-height . 0.3)))
            (defun meain/shell-name ()
              "Get the name of the shell based on project info."
              (format "*popup-shell-%s*"
                      (if (projectile-project-p)
                          (projectile-project-name)
                        "-")))
            (defun meain/shell-toggle (&optional rerun-previous)
              "Create/toggle shell for current project."
              (interactive "P")
              (let ((shell-buffers (remove-if-not (lambda (x)
                                                    (s-starts-with-p (meain/shell-name)
                                                                     (buffer-name x)))
                                                  (buffer-list))))
                (cond
                 ((s-starts-with-p (meain/shell-name)
                                   (buffer-name (current-buffer)))
                  (progn
                    (if rerun-previous
                        (progn
                          (vterm-clear)
                          (vterm-clear-scrollback))
                      (delete-window))))
                 ((equal (length shell-buffers) 0)
                  (meain/shell-new t))
                 (t (progn
                      (pop-to-buffer (car shell-buffers))
                      (if rerun-previous
                          (progn
                            (vterm-clear)
                            (vterm-clear-scrollback)
                            (vterm-send-up)
                            (vterm-send-return))))))))
            (defun meain/shell-new (&optional always-create)
              "Create a new shell for the current project."
              (interactive)
              (setq default-directory (cond
                                       ((projectile-project-p)
                                        (projectile-project-root))
                                       (t "~/")))
              (if (or always-create
                      (s-starts-with-p "*popup-shell"
                                       (buffer-name)))
                  (progn
                    (if (s-starts-with-p "*popup-shell"
                                         (buffer-name))
                        (delete-window))
                    (vterm (meain/shell-name)))
                (call-interactively 'switch-to-buffer)))
            (defun meain/shell-other (&optional alternate)
              "Switch to previous shell in current project. Use ALTERNATE to get a list of shell in current project."
              (interactive "P")
              (let ((shell-buffers (remove-if-not (lambda (x)
                                                    (s-starts-with-p (meain/shell-name)
                                                                     (buffer-name x)))
                                                  (buffer-list))))
                (cond
                 ((equal (length shell-buffers) 0)
                  (message "No shells bruh!"))
                 ((equal (length shell-buffers) 1)
                  (message "Only one shell"))
                 (alternate (switch-to-buffer (completing-read "Choose shell: "
                                                               (mapcar (lambda (x)
                                                                         (buffer-name x))
                                                                       shell-buffers))))
                 (t (switch-to-buffer (car (cdr shell-buffers)))))))
            (defun meain/run-in-vterm-kill (process event)
              "A process sentinel. Kills PROCESS's buffer if it is live."
              (let ((b (process-buffer process)))
                (and (buffer-live-p b)
                     (kill-buffer b)
                     (delete-window))))
            (defun meain/run-in-vterm (command)
              "Execute string COMMAND in a new vterm and kill the shell once done.  Useful for interactive items."
              (interactive (list (let* ((f (cond
                                            (buffer-file-name)
                                            ((eq major-mode 'dired-mode)
                                             (dired-get-filename nil t))))
                                        (filename (concat " "
                                                          (shell-quote-argument (and f
                                                                                     (file-relative-name f))))))
                                   (read-shell-command "Terminal command: "
                                                       (cons filename 0)
                                                       (cons 'shell-command-history 1)
                                                       (list filename)))))
              (with-current-buffer (vterm (concat "*popup-shell-" command "*"))
                (set-process-sentinel vterm--process #'meain/run-in-vterm-kill)
                (vterm-send-string (concatenate 'string command ";exit 0"))
                (vterm-send-return)))
            (defun meain/clear-and-exec ()
              (interactive)
              (vterm-clear)
              (vterm-clear-scrollback)
              (vterm-send-return))
            (define-key vterm-mode-map [(S-return)] 'meain/clear-and-exec)
            (defun meain/vterm--kill-vterm-buffer-and-window (process event)
              "Kill buffer and window on vterm PROCESS termination.  EVENT is the close event."
              (when (not (process-live-p process))
                (let ((buf (process-buffer process)))
                  (when (buffer-live-p buf)
                    (with-current-buffer buf
                      (kill-buffer)
                      (ignore-errors (delete-window))
                      (message "VTerm closed."))))))
            (add-hook 'vterm-mode-hook
                      (lambda ()
                        (set-process-sentinel (get-buffer-process (buffer-name))
                                              #'meain/vterm--kill-vterm-buffer-and-window)))))

;; ranger in emacs
(use-package ranger
