;;; init.el -- willemm's Emacs configuration.
;;
;;; Commentary:
;;
;; My Emacs configuration, uses nix-community's emacs-overlay for package installation.
;; Originally generated by home-manager, taken apart and reassembled by me.
;;
;;; Code:

;; Make sure nix installed binaries are in the PATH
(setenv "PATH" (concat "/Users/willem/.nix-profile/bin:" (getenv "PATH")))
(add-to-list 'exec-path "/Users/willem/.nix-profile/bin")

(setq inhibit-default-init t)

;; Typically, I only want spaces when pressing the TAB key. I also
;; want 4 of them.
(setq-default indent-tabs-mode nil
              tab-width 4
              c-basic-offset 4)

;; Increase emacs data read limit (default too low for LSP)
(setq read-process-output-max (* 1024 1024))

;; Stop creating backup and autosave files.
(setq make-backup-files nil
      auto-save-default nil)

;; Always show line and column number in the mode line.
(line-number-mode)
(column-number-mode)

;; Soft wrap lines
(visual-line-mode)

;; Use one space to end sentences.
(setq sentence-end-double-space nil)

;; Enable highlighting of current line.
(global-hl-line-mode 1)

;; Make it easier to enter tildes and backticks
(global-set-key (kbd "M-n") "~")
(global-set-key (kbd "M-N") "`")

(defun with-buffer-name-prompt-and-make-subdirs ()
  "Offer to create parent directory when finding file in a non-existent directory."
  (let ((parent-directory (file-name-directory buffer-file-name)))
    (when (and (not (file-exists-p parent-directory))
               (y-or-n-p (format "Directory `%s' does not exist! Create it? " parent-directory)))
      (make-directory parent-directory t))))
(add-to-list 'find-file-not-found-functions #'with-buffer-name-prompt-and-make-subdirs)

;; Don't warn when cannot guess python indent level
(setq-default python-indent-guess-indent-offset-verbose nil)

;; Disable scroll + C to zoom
(global-unset-key (kbd "C-<wheel-down>"))
(global-unset-key (kbd "C-<wheel-up>"))

(require 'all-the-icons)
(require 'all-the-icons-dired)
(require 'company)
(require 'counsel)
(require 'edit-indirect)
(require 'editorconfig)
(require 'eglot)
(require 'format-all)
(require 'ivy)
(require 'magit)
(require 'meow)
(require 'nix-mode)
(require 'nix-update)
(require 'org)
(require 'org-modern)
(require 'polymode)
(require 'rustic)
(require 'rust-ts-mode)
(require 'swiper)
(require 'yasnippet)

(let ((org-config (expand-file-name "~/.emacs.d/org-tex-cfg.el")))
 (when (file-exists-p org-config)
   (load-file org-config))
)

(setq org-directory (expand-file-name "~/Documents/org"))

(add-hook 'dired-mode-hook 'all-the-icons-dired-mode)

(setq company-format-margin-function 'company-text-icons-margin)
(setq company-text-icons-add-background t)

(add-hook 'after-init-hook 'global-company-mode)

;; Configure Ivy/Swiper
(global-set-key "\C-s" 'swiper)
(global-set-key (kbd "C-c C-r") 'ivy-resume)
(global-set-key (kbd "<f6>") 'ivy-resume)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "<f1> f") 'counsel-describe-function)
(global-set-key (kbd "<f1> v") 'counsel-describe-variable)
(global-set-key (kbd "<f1> o") 'counsel-describe-symbol)
(global-set-key (kbd "<f1> l") 'counsel-find-library)
(global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
(global-set-key (kbd "<f2> u") 'counsel-unicode-char)
(global-set-key (kbd "C-c g") 'counsel-git)
(global-set-key (kbd "C-c j") 'counsel-git-grep)
(global-set-key (kbd "C-c k") 'counsel-ag)
(global-set-key (kbd "C-x l") 'counsel-locate)
(global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
(define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history)

(editorconfig-mode 1)

(global-set-key (kbd "C-c C-y") 'format-all-buffer)

(add-to-list 'format-all-default-formatters '("Nix" alejandra))
(setq-default format-all-formatters format-all-default-formatters)

(global-set-key (kbd "C-c C-r") 'ivy-resume)
(add-hook 'after-init-hook 'ivy-mode)

(setq ivy-use-virtual-buffers t)
(setq ivy-use-selectable-prompt t)
(setq enable-recursive-minibuffers t)

;; ivy-bibtex requires ivy's `ivy--regex-ignore-order` regex builder, which
;; ignores the order of regexp tokens when searching for matching candidates.
(setq ivy-re-builders-alist
      '((ivy-bibtex . ivy--regex-ignore-order)
        (t . ivy--regex-plus)))

(defun meow-setup ()
  "Initialize meow-edit for colemak."
  (setq meow-cheatsheet-layout meow-cheatsheet-layout-colemak)
  (meow-motion-overwrite-define-key
   ;; Use e to move up, n to move down.
   ;; Since special modes usually use n to move down, we only overwrite e here.
   '("e" . meow-prev)
   '("<escape>" . ignore))
  (meow-leader-define-key
   '("?" . meow-cheatsheet)
   ;; To execute the originally e in MOTION state, use SPC e.
   '("e" . "H-e")
   '("1" . meow-digit-argument)
   '("2" . meow-digit-argument)
   '("3" . meow-digit-argument)
   '("4" . meow-digit-argument)
   '("5" . meow-digit-argument)
   '("6" . meow-digit-argument)
   '("7" . meow-digit-argument)
   '("8" . meow-digit-argument)
   '("9" . meow-digit-argument)
   '("0" . meow-digit-argument))
  (meow-normal-define-key
   '("0" . meow-expand-0)
   '("1" . meow-expand-1)
   '("2" . meow-expand-2)
   '("3" . meow-expand-3)
   '("4" . meow-expand-4)
   '("5" . meow-expand-5)
   '("6" . meow-expand-6)
   '("7" . meow-expand-7)
   '("8" . meow-expand-8)
   '("9" . meow-expand-9)
   '("-" . negative-argument)
   '(";" . meow-reverse)
   '("," . meow-inner-of-thing)
   '("." . meow-bounds-of-thing)
   '("[" . meow-beginning-of-thing)
   '("]" . meow-end-of-thing)
   '("/" . meow-visit)
   '("a" . meow-append)
   '("A" . meow-open-below)
   '("b" . meow-back-word)
   '("B" . meow-back-symbol)
   '("c" . meow-change)
   '("d" . meow-delete)
   '("e" . meow-prev)
   '("E" . meow-prev-expand)
   '("f" . meow-find)
   '("g" . meow-cancel-selection)
   '("G" . meow-grab)
   '("h" . meow-left)
   '("H" . meow-left-expand)
   '("i" . meow-right)
   '("I" . meow-right-expand)
   '("j" . meow-join)
   '("k" . meow-kill)
   '("l" . meow-line)
   '("L" . meow-goto-line)
   '("m" . meow-mark-word)
   '("M" . meow-mark-symbol)
   '("n" . meow-next)
   '("N" . meow-next-expand)
   '("o" . meow-block)
   '("O" . meow-to-block)
   '("p" . meow-yank)
   '("q" . meow-quit)
   '("r" . meow-replace)
   '("s" . meow-insert)
   '("S" . meow-open-above)
   '("t" . meow-till)
   '("u" . meow-undo)
   '("U" . meow-undo-in-selection)
   '("v" . meow-search)
   '("w" . meow-next-word)
   '("W" . meow-next-symbol)
   '("x" . meow-delete)
   '("X" . meow-backward-delete)
   '("y" . meow-save)
   '("z" . meow-pop-selection)
   '("'" . repeat)
   '("<escape>" . ignore)))

(meow-setup)
(meow-global-mode 1)


;; Polymode config (add support for nix files with commented strings)

(define-hostmode poly-nix-hostmode :mode 'nix-mode)

(define-auto-innermode poly-any-expr-nix-innermode
  :head-matcher (rx (* blank) "/*" (* blank) bow (+ (or word punct)) eow (* blank) "*/" (* blank) "''\n")
  :mode-matcher (cons (rx "/*" (* blank) (submatch bow (+ (or word punct)) eow) (* blank) "*/") 1)
  :tail-matcher (rx bol (* blank) "'';" (* blank) eol)
  :head-mode 'host
  :tail-mode 'host
  :fallback-mode 'text-mode)

(define-polymode poly-nix-mode
  :hostmode 'poly-nix-hostmode
  :innermodes '(poly-any-expr-nix-innermode))

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

;; Automatically update hash for nix fetch functions
(define-key nix-mode-map (kbd "C-c C-u") 'nix-update-fetch)

(define-key eglot-mode-map (kbd "C-c C-y") 'eglot-format-buffer)
(define-key eglot-mode-map (kbd "C-c C-a") 'eglot-code-actions)
(setq eglot-extend-to-xref t)
(setq rustic-lsp-client 'eglot)

(setq rustic-treesitter-derive t)

(setq auto-mode-alist (rassq-delete-all 'rust-mode auto-mode-alist))
(setq auto-mode-alist (rassq-delete-all 'rust-ts-mode auto-mode-alist))
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rustic-mode))

(defvar separedit-preserve-string-indentation t)
(global-set-key (kbd "C-c '") 'separedit)
(add-hook 'separedit-buffer-creation-hook 'normal-mode)

(load-theme 'solarized-gruvbox-dark t)

(global-set-key (kbd "C-s") 'swiper)

(setq yas-snippet-dirs (list (expand-file-name "snippets" org-directory)))

;; Accept 'y' and 'n' rather than 'yes' and 'no'.
(defalias 'yes-or-no-p 'y-or-n-p)

(provide 'init)
;;; init.el ends here
