(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(initial-frame-alist '((fullscreen . maximized)))
 '(package-selected-packages
   '(zig-mode treesit ef-themes dockerfile-mode dap-mode julia-ts-mode lsp-julia julia-mode marginalia vertico tide which-key use-package pyenv-mode flycheck exec-path-from-shell elpy doom-themes doom-modeline all-the-icons)))

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))

(use-package which-key
  :ensure t
  :config (which-key-mode))

(use-package vertico
  :ensure t
  :config
  (add-hook 'rfn-eshadow-update-overlay-hook #'vertico-directory-tidy))

(use-package marginalia
  :ensure t)

(require 'treesit)

(use-package zig-mode
  :ensure t)

(use-package dired
  :config
  (put 'dired-find-alternate-file 'disabled nil)
  (add-hook 'dired-mode-hook
          (lambda ()
            (define-key dired-mode-map (kbd "RET")
              (lambda () (interactive) (find-alternate-file (dired-get-file-for-visit))))))
  (setq delete-by-moving-to-trash t)
  (setq dired-dwim-target t))

(vertico-mode 1)

(marginalia-mode 1)

(tool-bar-mode -1)

(scroll-bar-mode -1)

(set-face-attribute 'default nil :font "Hack Nerd Font Mono" :height 110)
;;(set-face-attribute 'default nil :font "VictorMono Nerd Font Mono" :height 110)

(set-default-coding-systems 'utf-8)

(setq ring-bell-function 'ignore)

(setq inhibit-startup-message t)

(setq backup-directory-alist '(("." . "~/.saves")))

(setq auto-save-default nil)

(setq-default indent-tabs-mode nil)

(setq show-paren-delay 0)

(setq native-comp-async-report-warnings-errors 'silent)

(delete-selection-mode 1)

(show-paren-mode 1)

(add-hook 'before-save-hook 'whitespace-cleanup)

(setq-default frame-title-format
              '(:eval
                (format "%s"
                        (cond
                         (buffer-file-truename buffer-file-truename )
                         (dired-directory (concat "[ " dired-directory " ]"))
                         (t "GNU Emacs 👾")))))

(use-package elpy
             :ensure t)

(use-package flycheck
            :ensure t)

(use-package exec-path-from-shell
             :ensure t)

(elpy-enable)

(exec-path-from-shell-copy-env "PATH")

;; black or 'standard' (flake8 ?) code formatter:
;; (add-hook 'elpy-mode-hook (lambda ()
;;                             (add-hook 'before-save-hook
;;                                       'elpy-format-code nil t)))

(add-hook 'elpy-mode-hook (lambda ()
                            (add-hook 'before-save-hook
                                      'elpy-black-fix-code nil t)))

(add-hook 'python-mode-hook
          (lambda () (setq gud-pdb-command-name "python3 -m pdb")))

(use-package pyenv-mode
             :ensure t)

(pyenv-mode)

(use-package yasnippet
  :config
  (setq yas-snippet-dirs '("~/.emacs.d/snippets"))
  (yas-reload-all)
  (add-hook 'prog-mode-hook 'yas-minor-mode))


;; my keybindings

(defun revert-buffer-no-confirm ()
    (interactive)
    (revert-buffer :ignore-auto :noconfirm))

(defun kill-buffer-no-confirm ()
    (interactive)
    (kill-buffer nil))

(defun list-buffers-please ()
  (interactive)
  (list-buffers)
  (other-window 1)
  (delete-other-windows))

(defun make-json-great-again ()
  (interactive)
  (when (string-match-p ".json\\'" (buffer-name))
    (mark-whole-buffer)
    (json-pretty-print (region-beginning) (region-end))))

(global-unset-key (kbd "C-z"))
(global-unset-key (kbd "C-v"))
(global-unset-key (kbd "C-f"))
(global-unset-key (kbd "C-b"))
(global-unset-key (kbd "C-n"))
(global-unset-key (kbd "C-l"))
(global-unset-key (kbd "C-p"))
(global-unset-key (kbd "M-v"))

(global-set-key (kbd "<f1>") 'find-file)
(global-set-key (kbd "<f5>") 'revert-buffer-no-confirm)
(global-set-key (kbd "<f12>") 'kill-buffer-no-confirm)
(global-set-key (kbd "C-b") 'list-buffers-please)
(global-set-key (kbd "C-n") 'dired-create-empty-file)

(add-hook 'elpy-mode-hook
          (lambda () (local-set-key [f2] 'elpy-refactor-rename)))

(add-hook 'elpy-mode-hook
          (lambda () (local-set-key [f7] 'elpy-goto-definition)))

(add-hook 'typescript-mode-hook
          (lambda () (local-set-key [f2] 'tide-rename-symbol)))

(add-hook 'typescript-mode-hook
          (lambda () (local-set-key [f7] 'tide-references)))

(add-hook 'hs-minor-mode-hook
          (lambda () (local-set-key (kbd "C-<escape>") 'hs-toggle-hiding)))

(add-hook 'mhtml-mode-hook
          (lambda () (local-set-key (kbd "C-f") 'prettier-js)))

(add-hook 'typescript-ts-mode-hook
          (lambda () (local-set-key (kbd "C-f") 'prettier-js)))

(add-hook 'js-mode-hook
          (lambda () (local-set-key (kbd "C-f") 'make-json-great-again)))

(add-hook 'lsp-mode-hook
          (lambda () (local-set-key (kbd "C-f") 'lsp-format-buffer)))

;; (add-hook 'prog-mode-hook
;;           (lambda () (local-set-key (kbd "C-;") 'comment-or-uncomment-region)))

;; (define-key map (kbd "M-.") 'elpy-goto-assignment
;; (define-key map (kbd "C-x 4 M-.") 'elpy-goto-assignment-other-window)

;; stuff for folding html
(defun mhtml-forward (arg)
  (interactive "P")
  (pcase (get-text-property (point) 'mhtml-submode)
    ('nil (sgml-skip-tag-forward 1))
    (submode (forward-sexp))))

(add-to-list 'hs-special-modes-alist
             '(mhtml-mode
               "{\\|<[^/>]+?"
               "}\\|</[^/>]*[^/]>"
               "<!--"
               mhtml-forward
               nil))

(add-hook 'prog-mode-hook 'hs-minor-mode)

(require 'display-line-numbers)

(defcustom display-line-numbers-exempt-modes
  '(gud-mode inferior-python-mode dired-mode vterm-mode eshell-mode shell-mode term-mode ansi-tern-mode treemacs-mode)
  "Major modes on which to disable line numbers"
  :group 'display-line-numbers
  :type 'list
  :version "green")

(defun display-line-numbers--turn-on ()
  "Turn on line numbers except for certain modes.
Exempt major modes are defined in `display-line-numbers-exempt-modes'."
  (unless (or (minibufferp)
              (member major-mode display-line-numbers-exempt-modes))
    (display-line-numbers-mode)))

;; (global-display-line-numbers-mode)

;; theming starts here:

(use-package modus-themes
  :ensure t)

(modus-themes-select 'modus-vivendi-tinted)
(setq modus-themes-to-toggle '(modus-operandi-tinted modus-vivendi-tinted))

(use-package ef-themes
  :ensure t)

(setq ef-themes-to-toggle '(ef-day ef-autumn))

(setq ef-themes-headings ; read the manual's entry or the doc string
'((0 . (variable-pitch light 1.9))
(1 . (variable-pitch light 1.8))
(2 . (variable-pitch regular 1.7))
(3 . (variable-pitch regular 1.6))
(4 . (variable-pitch regular 1.5))
(5 . (variable-pitch 1.4)) ; absence of weight means `bold'
(6 . (variable-pitch 1.3))
(7 . (variable-pitch 1.2))
(t . (variable-pitch 1.1))))

(setq ef-themes-mixed-fonts t
ef-themes-variable-pitch-ui t)

;; (mapc #'disable-theme custom-enabled-themes)

;; (ef-themes-select 'ef-maris-light)

;; theming stops here

(use-package magit
  :ensure t)

(use-package doom-modeline
  :ensure t
  :hook (after-init . doom-modeline-mode))

;; M-x nerd-icons-install-fonts
(use-package nerd-icons
  :ensure t)

;; prettier setup goes here
(use-package prettier-js
  :ensure t)

;; __________________________________________________

(use-package orderless
  :ensure t
  :custom
  (completion-styles '(orderless basic))
  (completion-category-overrides '((file (styles basic partial-completion)))))

(use-package json-mode :ensure t)

;; __________________________________________________
;; typescript setup starts here
(defun setup-tide-mode ()
  (interactive)
  (tide-setup)
  (flycheck-mode +1)
  (setq flycheck-check-syntax-automatically '(save mode-enabled))
  (eldoc-mode +1)
  (tide-hl-identifier-mode +1))

(use-package tide
  :ensure t
  :after (flycheck)
  :hook ((typescript-ts-mode . setup-tide-mode)
         (tsx-ts-mode . setup-tide-mode)
         (typescript-ts-mode . tide-hl-identifier-mode)
         (before-save . tide-format-before-save)))

(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode))
(add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))

;;----------------------------------------
;; dotnet starts here
(use-package lsp-mode
  :commands (lsp lsp-deferred))

(use-package dap-mode
  :ensure t
  ;;:custom (lsp-enable-dap-auto-configure nil)
  :commands dap-debug)

;;(setq dap-auto-configure-features '(sessions locals controls tooltip))

(defun setup-lsp-keys ()
  (interactive)
  (local-set-key [f7] 'lsp-find-implementation)
  (local-set-key [f2] 'lsp-rename))

(defun setup-dotnet ()
  (interactive)
  (exec-path-from-shell-copy-env "DOTNET_ROOT")
  (lsp-deferred))

(add-hook 'csharp-mode-hook
          (lambda () (setup-dotnet)))

(add-hook 'lsp-mode-hook
          (lambda () (setup-lsp-keys)))

(require 'dap-netcore)

(use-package dockerfile-mode
  :ensure t)


(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
