{ config, pkgs, ... }:

let
  emacsWithPackages = (pkgs.emacsPackagesFor pkgs.emacs).emacsWithPackages;
in {
  programs.emacs = {
    enable = true;
    extraConfig = ''
(load-theme 'catppuccin :no-confirm)
(global-display-line-numbers-mode 1)
(setq-default truncate-lines t)
(telephone-line-mode 1)
(xterm-mouse-mode 1)

(setq evil-want-C-u-scroll t)

(menu-bar--display-line-numbers-mode-relative)
(global-display-line-numbers-mode 1)

(electric-pair-mode)

;;(menu-bar-mode -1)

(require 'yasnippet)
(yas-global-mode 1)

(require 'which-key)
(which-key-mode)

(require 'evil-god-state)
(evil-define-key 'normal global-map "," 'evil-execute-in-god-state)

(which-key-enable-god-mode-support)

(require 'nix-mode)
(add-to-list 'auto-mode-alist '("\\.nix\\'" . nix-mode))

(use-package treemacs
  :ensure t
  :defer t
  :init
  (with-eval-after-load 'winum
    (define-key winum-keymap (kbd "M-0") #'treemacs-select-window))
  :config
  (progn
    (setq treemacs-collapse-dirs                   (if treemacs-python-executable 3 0)
          treemacs-deferred-git-apply-delay        0.5
          treemacs-directory-name-transformer      #'identity
          treemacs-display-in-side-window          t
          treemacs-eldoc-display                   'simple
          treemacs-file-event-delay                2000
          treemacs-file-extension-regex            treemacs-last-period-regex-value
          treemacs-file-follow-delay               0.2
          treemacs-file-name-transformer           #'identity
          treemacs-follow-after-init               t
          treemacs-expand-after-init               t
          treemacs-find-workspace-method           'find-for-file-or-pick-first
          treemacs-git-command-pipe                ""
          treemacs-goto-tag-strategy               'refetch-index
          treemacs-header-scroll-indicators        '(nil . "^^^^^^")
          treemacs-hide-dot-git-directory          t
          treemacs-indentation                     2
          treemacs-indentation-string              " "
          treemacs-is-never-other-window           nil
          treemacs-max-git-entries                 5000
          treemacs-missing-project-action          'ask
          treemacs-move-files-by-mouse-dragging    t
          treemacs-move-forward-on-expand          nil
          treemacs-no-png-images                   nil
          treemacs-no-delete-other-windows         t
          treemacs-project-follow-cleanup          nil
          treemacs-persist-file                    (expand-file-name ".cache/treemacs-persist" user-emacs-directory)
          treemacs-position                        'left
          treemacs-read-string-input               'from-child-frame
          treemacs-recenter-distance               0.1
          treemacs-recenter-after-file-follow      nil
          treemacs-recenter-after-tag-follow       nil
          treemacs-recenter-after-project-jump     'always
          treemacs-recenter-after-project-expand   'on-distance
          treemacs-litter-directories              '("/node_modules" "/.venv" "/.cask")
          treemacs-project-follow-into-home        nil
          treemacs-show-cursor                     nil
          treemacs-show-hidden-files               t
          treemacs-silent-filewatch                nil
          treemacs-silent-refresh                  nil
          treemacs-sorting                         'alphabetic-asc
          treemacs-select-when-already-in-treemacs 'move-back
          treemacs-space-between-root-nodes        t
          treemacs-tag-follow-cleanup              t
          treemacs-tag-follow-delay                1.5
          treemacs-text-scale                      nil
          treemacs-user-mode-line-format           nil
          treemacs-user-header-line-format         nil
          treemacs-wide-toggle-width               70
          treemacs-width                           35
          treemacs-width-increment                 1
          treemacs-width-is-initially-locked       t
          treemacs-workspace-switch-cleanup        nil)

    ;; The default width and height of the icons is 22 pixels. If you are
    ;; using a Hi-DPI display, uncomment this to double the icon size.
    ;;(treemacs-resize-icons 44)

    (treemacs-follow-mode t)
    (treemacs-filewatch-mode t)
    (treemacs-fringe-indicator-mode 'always)
    (when treemacs-python-executable
      (treemacs-git-commit-diff-mode t))

    (pcase (cons (not (null (executable-find "git")))
                 (not (null treemacs-python-executable)))
      (`(t . t)
       (treemacs-git-mode 'deferred))
      (`(t . _)
       (treemacs-git-mode 'simple)))

    (treemacs-hide-gitignored-files-mode nil))
  :bind
  (:map global-map
        ("M-0"       . treemacs-select-window)
        ("C-x t 1"   . treemacs-delete-other-windows)
        ("C-x t t"   . treemacs)
        ("C-x t d"   . treemacs-select-directory)
        ("C-x t B"   . treemacs-bookmark)
        ("C-x t C-t" . treemacs-find-file)
        ("C-x t M-t" . treemacs-find-tag)))

(use-package treemacs-evil
  :after (treemacs evil)
  :ensure t)

(use-package treemacs-projectile
  :after (treemacs projectile)
  :ensure t)

(use-package treemacs-icons-dired
  :hook (dired-mode . treemacs-icons-dired-enable-once)
  :ensure t)

(use-package treemacs-magit
  :after (treemacs magit)
  :ensure t)

(use-package treemacs-persp ;;treemacs-perspective if you use perspective.el vs. persp-mode
  :after (treemacs persp-mode) ;;or perspective vs. persp-mode
  :ensure t
  :config (treemacs-set-scope-type 'Perspectives))

(use-package treemacs-tab-bar ;;treemacs-tab-bar if you use tab-bar-mode
  :after (treemacs)
  :ensure t
  :config (treemacs-set-scope-type 'Tabs))

(treemacs-start-on-boot)
(treemacs-project-follow-mode)

(setq evil-want-keybinding nil)
(require 'evil)
(when (require 'evil-collection nil t)
  (evil-collection-init))

(require 'evil)

(evil-mode 1)

(defun my-evil-jump-out-or-indent ()
  "Springe aus Klammern oder Anführungszeichen heraus oder führe eine Einrückung durch."
  (interactive)
  (let ((pos (point)))
    (cond
     ;; Wenn der Cursor auf einem Escape-Zeichen ist, gehe zurück
     ((looking-at-p "[\\]") (backward-char 1))
     ;; Wenn der Cursor auf einem öffnenden Zeichen ist, gehe vorwärts
     ((looking-at-p "[\"'()\\[\\{]") (forward-char 1))
     ;; Wenn der Cursor hinter einem schließenden Zeichen ist, gehe zurück
     ((looking-back "[\"'()\\[\\{]" (line-beginning-position)) (backward-char 1))
     ;; Wenn der Cursor nicht in Klammern oder Anführungszeichen ist, führe eine Einrückung durch
     (t (indent-for-tab-command)))))

(define-key evil-insert-state-map (kbd "TAB") 'my-evil-jump-out-or-indent)

(with-eval-after-load 'flycheck
  (add-hook 'flycheck-mode-hook #'flycheck-inline-mode))

(global-flycheck-mode 1)

(evil-define-key 'god global-map [escape] 'evil-god-state-bail)

(require 'smex)
(smex-initialize)

  (global-set-key (kbd "M-x") 'smex)
  (global-set-key (kbd "M-X") 'smex-major-mode-commands)
  ;; This is your old M-x.
  (global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)


(add-hook 'prog-mode-hook #'rainbow-delimiters-mode)

(setq gc-cons-threshold (* 100 1024 1024)
      read-process-output-max (* 1024 1024))

;;Exit insert mode by pressing j and then k quickly
(setq key-chord-two-keys-delay 0.5)
(key-chord-define evil-insert-state-map "jk" 'evil-normal-state)
(key-chord-mode 1)


	(setq company-idle-delay
      (lambda () (if (company-in-string-or-comment) nil 0.1)))

(setq company-minimum-prefix-length 1)
(add-hook 'after-init-hook 'global-company-mode)

(custom-set-faces
 '(company-tooltip
   ((t (:background "black" :foreground "thistle1"))))
 '(company-tooltip-selection
   ((t (:background "bdarkslateblue" :foreground "thistle1"))))
)

(with-eval-after-load 'evil
  (define-key evil-normal-state-map (kbd "C-h") 'windmove-left)
  (define-key evil-normal-state-map (kbd "C-j") 'windmove-down)
  (define-key evil-normal-state-map (kbd "C-k") 'windmove-up)
  (define-key evil-normal-state-map (kbd "C-l") 'windmove-right)

  (define-key evil-motion-state-map (kbd "C-h") 'windmove-left)
  (define-key evil-motion-state-map (kbd "C-j") 'windmove-down)
  (define-key evil-motion-state-map (kbd "C-k") 'windmove-up)
  (define-key evil-motion-state-map (kbd "C-l") 'windmove-right))


(setq lsp-keymap-prefix "C-c l")

    '';
    package = emacsWithPackages (epkgs: (with epkgs.melpaStablePackages; [
    catppuccin-theme
      ])
      ++ (with epkgs.melpaPackages; [
	evil
	nix-mode
  lsp-treemacs
  treemacs-evil
  treemacs-tab-bar
  flycheck
  flycheck-inline
  lsp-mode
  company
  rainbow-delimiters
  undo-fu
  key-chord
  telephone-line
  smex
  ranger
  evil-god-state
  which-key
  magit
  format-all
      ])
      ++ (with epkgs.elpaPackages; [
  undo-tree
      ])
      ++ [
        epkgs.yasnippet
#        epkgs.yasnippet-snippets

      ]);
    };

}


