;;-*-coding: iso-latin-1-*-
;; Disable GC for the duration of init scripts
(let ((gc-cons-threshold most-positive-fixnum))
(global-auto-revert-mode t) 
;;(w32-register-hot-key [C-]) --Windows!
(defconst user-init-dir
  (cond ((boundp 'user-emacs-directory)
         user-emacs-directory)
        ((boundp 'user-init-directory)
         user-init-directory)
        (t "~/.emacs.d/")))

(defun load-user-file (file)
  (interactive "f")
  "Load a file in current user's configuration directory"
  (load-file (expand-file-name file user-init-dir)))
;; Lmao do the loading here

;; Eval buffer
(global-set-key (kbd "<f5>") #'eval-buffer)

;;; PACKAGE BOOTSTRAPPING ;;;

(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))

(package-initialize)

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

(eval-when-compile
  (require 'use-package))

(use-package ssh-agency
  :ensure t)
C
(use-package benchmark-init
  :ensure t
  :config
  ;; To disable collection of benchmark data after init is done.
  (add-hook 'after-init-hook 'benchmark-init/deactivate))

(use-package tangotango-theme
         :ensure t
         :config (load-theme 'tangotango t))
		 
(use-package magit
         :ensure t)
		 ;;:init (setq magit-git-executable "C:/capybara/git/bin/git.exe"))

(use-package centered-cursor-mode
         :ensure t)

(use-package projectile
  :ensure t
  :config (projectile-global-mode))

(use-package helm-projectile
  :ensure t)

(use-package dokuwiki
  :ensure t
  :config
  (setq dokuwiki-xml-rpc-url "https://wiki.roboces.dev/lib/exe/xmlrpc.php")
  (setq dokuwiki-login-user-name "superuser"))

(use-package dokuwiki-mode
  :ensure t)

(use-package projectile-ripgrep
  :ensure t)

(use-package crdt
  :ensure t)

(use-package lua-mode
  :ensure t
  :config
  (autoload 'lua-mode "lua-mode" "Lua editing mode." t)
  (add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode))
  (add-to-list 'interpreter-mode-alist '("lua" . lua-mode)))

(use-package super-save
  :ensure t
  :config
  (super-save-mode +1)
  (setq super-save-auto-save-when-idle t))

(use-package sudo-edit
  :ensure t)

(use-package hl-todo
  :ensure t
  :config
  ;; Enable hl-todo mode globally
  (global-hl-todo-mode)
  ;; Define custom faces for the mode
  (defface hl-todo-custom-face
    '((t :weight ultra-bold :underline t :foreground "#db383d" : :inherit (hl-todo)))
    "Custom keyface for the TODO keyword."
    :group 'hl-todo)
  (setq hl-todo-keyword-faces
        '(("HOLD" . "#d0bf8f")
          ("TODO" . hl-todo-custom-face)
		  ("todo" . hl-todo-custom-face)
          ("NEXT" . "#dca3a3")
          ("THEM" . "#dc8cc3")
          ("PROG" . "#7cb8bb")
          ("OKAY" . "#7cb8bb")
          ("DONT" . "#5f7f5f")
          ("FAIL" . "#8c5353")
          ("DONE" . "#afd8af")
          ("NOTE"   . "#d0bf8f")
          ("KLUDGE" . "#d0bf8f")
          ("HACK"   . "#d0bf8f")
          ("TEMP"   . "#d0bf8f")
          ("FIXME"  . "#cc9393")
          ("XXX+"   . "#cc9393"))))

(use-package magit-todos
  :ensure t
  :after magit
  :commands (magit-todos-mode)
  :config
  (setq magit-todos-nice nil)
  ;;(setq (magit-todos-git-grep-extra-args (list "-n")))
  (setq magit-todos-scanner 'magit-todos--scan-with-rg)
  ;;(setq magit-todos-depth 100)
  (magit-todos-mode 1))

(use-package markdown-mode
  :ensure t
  :mode ("README\\.md\\'" . gfm-mode)
  :init (setq markdown-command "multimarkdown"))

(custom-set-variables
  '(markdown-command "/usr/bin/pandoc"))

(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)

;;; USER CONFIG ;;;

;;Remapping treemacs
(define-key treemacs-mode-map (kbd "C-j") nil)
(define-key treemacs-mode-map (kbd "C-k") nil)
(define-key treemacs-mode-map (kbd "C-;") 'treemacs-RET-action)
(define-key treemacs-mode-map (kbd "M-p") nil)

;;Using helm as minibuffer autocompletion manager
(setf projectile-completion-system 'helm)
(setf projectile-indexing-method 'alien)

;;Allowing per project compilation command
(setq projectile-per-project-compilation-buffer t)

;; Windows: Let git's UI ask for passphrase
;;(setenv "SSH_ASKPASS" "git-gui--askpass") --only this one, iirc!
;;(require 'exec-path
;;(exec-path-from-shell
;;l-copy-env "SSH_AGENT_PID")
;;(exec-path-from-shell-copy-env "SSH_AUTH_SOCK")

;; Display line numbers ;;in prog files
;;(add-hook 'prog-mode-hook 'display-line-numbers-mode)
(add-hook 'after-init-hook 'global-display-line-numbers-mode t)

;; Disable backup files
(setq make-backup-files nil)

;; Disable window cruft
(menu-bar-mode -1)

;; Disable toolbar
(tool-bar-mode -1)

;; Disable scrollbar
(scroll-bar-mode -1)

;; Use better buffer list
(global-set-key (kbd "C-x C-b") 'ibuffer)

;; Allow remember risky variable usage
(advice-add 'risky-local-variable-p :override #'ignore)

;; Skip windows when switching
;; (defvar ignore-windows-containing-buffers-matching-res '("*Treemacs*")
;;   "List of regular expressions specifying windows to skip (if window contains buffer that matches, skip)")

;; (defadvice other-window (before other-window-ignore-windows-containing activate)
;;   "skip over windows containing buffers which match regular expressions in 'ignore-windows-containing-buffers-matching-res"
;;   (if (and (= 1 (ad-get-arg 0)) (interactive-p))
;;       (let* ((win (next-window))
;;              (bname (buffer-name (window-buffer win))))
;;         (when (some 'identity (mapcar '(lambda (re)
;;                                          (string-match re bname))
;;                                       ignore-windows-containing-buffers-matching-res))
;;           (ad-set-arg 0 2)))))

;; Disable splash screen and startup message
(setq inhibit-startup-message t) 
(setq initial-scratch-message nil)

;; Use regex search toolsx
(global-set-key (kbd "C-s") 'isearch-forward-regexp) 
(global-set-key (kbd "C-r") 'isearch-backward-regexp)

;; On-home-row pointxx movement
(global-set-key (kbd "C-k") 'next-line)
(global-set-key (kbd "C-l") 'previous-line)
(global-set-key (kbd "C-;") 'forward-char)
(global-set-key (kbd "C-ñ") 'forward-char)
(global-set-key (kbd "C-j") 'backward-char)

;; Move forward word by word
(global-set-key (kbd "C-f") 'forward-word)
(global-set-key (kbd "C-b") 'backward-word)

;; Map delete/backspace 
(global-set-key (kbd "C-d") (lambda () (interactive) (backward-delete-char-untabify 1)))
(global-set-key (kbd "C-S-d") (lambda () (interactive) (delete-char 1)))

;; Map inverse transpose
(global-set-key (kbd "C-S-t") (lambda () (interactive) (transpose-chars -1)))

;; Map RET
(global-set-key (kbd "C-i") (lambda () (interactive) (newline)))

;; TODO: Remap up/downcase to region instead of word - not working
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)
(global-set-key (kbd "M-S-l") (lambda () (interactive) (upcase-region)))
(global-set-key (kbd "M-l") (lambda () (interactive) (downcase-region)))

;; Map scroll line by line
(global-set-key (kbd "M-i") (lambda () (interactive) (scroll-up   1)) )
(global-set-key (kbd "M-p") (lambda () (interactive) (scroll-down 1)) )

;; Windmove
(global-set-key (kbd "M-u")  'windmove-left)
(global-set-key (kbd "M-i")  'windmove-down)
(global-set-key (kbd "M-o")  'windmove-up)
(global-set-key (kbd "M-p")  'windmove-right)

;; Forcefully insert column tab
;;(global-set-key (kbd "S-<iso-lefttab>") 'tab-to-tab-stop)

(global-set-key (kbd "C-x C-u") (lambda () (interactive) (other-window -1)))
(global-set-key (kbd "C-x C-o") (lambda () (interactive) (other-window 1)))
(global-set-key (kbd "C-x u") 'dired-jump)
(global-set-key (kbd "C-x o") 'delete-blank-lines)

;; On-home-row cycling through buffers
(global-set-key (kbd "C-x C-j") 'previous-buffer)
(global-set-key (kbd "C-x C-ñ") 'next-buffer)
(global-set-key (kbd "C-x C-;") 'next-buffer)

;; x(global-set-key (kbd "C-x C-´") 'enlarge-window)
;;(global-set-key (kbd "C-x C-'") 'enlarge-window)
(global-set-key (kbd "C-c C-'") (lambda () (interactive) (enlarge-window 10)))
(global-set-key (kbd "C-c C-/") (lambda () (interactive) (enlarge-window -10)))

(global-set-key (kbd "C-z") 'kill-whole-line)
;;(global-set-key (kbd "C-x C-o") (lambda () (interactive) (other-window 1)))
;;(global-set-key (kbd "C-x u") 'dired-jump)
;;(global-set-key (kbd "C-x o") 'delete-blank-lines)

(global-set-key (kbd "C-n") 'undo)
(define-key input-decode-map [?\C-m] [C-m])
(global-set-key (kbd "<C-m>") 'undo-redo)

;;zap-to-char M-z, toggle-input-method C-\, transpose-chars C-t
(global-set-key (kbd "M-b") 'backward-char)
(global-set-key (kbd "M-f") 'forward-char)

(global-set-key (kbd "C-c C-SPC") 'string-insert-rectangle)
(global-set-key (kbd "C-x C-SPC") 'rectangle-mark-mode)
(global-set-key (kbd "C-x SPC") 'pop-global-mark)

(desktop-save-mode 1)
(setq desktop-restore-eager nil)
;;(setf desktop-restore-eager 5)
(delete-selection-mode 1)

;; C++
(add-hook 'c++-mode-hook (lambda () 
					 (setq-local comment-start "/*")
					 (setq-local comment-end "*/")
					 (setq-local comment-style 'extra-line)))

(global-set-key [f6] 'comment-region)
(global-set-key [f9] (lambda () (interactive) (find-file user-init-file)) )
(global-set-key [f8] 'projectile-find-file)
(global-set-key [f7] 'projectile-compile-project)

;; oddly specific buffer movement

(global-set-key (kbd "C-h u") 'info-apropos)
(global-set-key (kbd "C-,") (lambda () (interactive) (move-to-window-line -1)) ) 							  	
(global-set-key (kbd "M-,") (lambda () (interactive) (move-to-window-line 0)) ) 
(global-set-key (kbd "C-.") 'end-of-buffer)
(global-set-key (kbd "M-.") 'beginning-of-buffer)

;; info-apropos mapping
(global-set-key (kbd "C-h u") 'info-apropos)

;;frame size control
(global-set-key (kbd "C-x C-c") 'toggle-frame-maximized)

;; (global-set-key (kbd "C-x C-<dead-acute>") 'enlarge-window)
;; Remapping save commit in Magit
(define-key with-editor-mode-map (kbd "C-c C-c") nil)

;;Tell grep-find and ripgrep-regexp to follow symlinks
(grep-apply-setting
   'grep-find-command
   '("find -L .  -type f -exec grep --color=auto -nH --null -e  \{\} +" . 58))

;; 
(define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file)
(define-key dired-mode-map (kbd "a") 'dired-find-file)
(setq global-auto-revert-non-file-buffers t)
(add-hook 'dired-mode-hook  (lambda () (setq-local auto-revert-verbose nil)) )
(add-hook 'ibuffer-mode-hook (lambda () (ibuffer-auto-mode t)))

(setq compilation-scroll-output t)

;;Don't ask for following symlinks (nil = treat as from where we're accessing)
(setq vc-follow-symlinks nil)

;; Copy config
(defun copy-region-or-word ()
  "If mark is active, copy region. Otherwise, copy word currently on cursor"
  (interactive)
  (if mark-active
      (if (bound-and-true-p rectangle-mark-mode)
          (progn (copy-rectangle-as-kill (region-beginning) (region-end))
                 (kill-new (mapconcat 'identity (extract-rectangle (region-beginning) (region-end)) "\n")))
          (kill-ring-save (region-beginning) (region-end)))
      (let ((region (bounds-of-thing-at-point 'symbol)))
        (copy-region-as-kill (car region) (cdr region)))))

;; Tabulation and Tree-sitter config
(add-to-list 'auto-mode-alist '("\\.cpp\\'" . c++-mode))

;; (setq major-mode-remap-alist
;; 	  '(
;; ;;   (c-mode . c-ts-mode)
;;    (bash-mode . bash-ts-mode)
;; ;;   (cpp-mode . c++-ts-mode)
;;    (asm-mode . asm-ts-mode)
;;    (cmake-mode . cmake-ts-mode)
;;    (c-sharp-mode . c-sharp-mode)
;; ;;   (css-mode . css-ts-mode)
;;    (bash-mode . bash-ts-mode)
;;    (dockerfile-mode . dockerfile-ts-mode)
;;    (elisp-mode . elisp-ts-mode)
;;    (elm-mode . elm-ts-mode)
;;    (gitcommit-mode . gitcommit-ts-mode)
;;    (gitignore-mode . gitignore-ts-mode)
;;    (git-rebase-mode . git-rebase-ts-mode)
;;    (json-mode . json-ts-mode)
;;    (latex-mode . latex-ts-mode)
;;    (lua-mode . lua-ts-mode)
;;    (make-mode . make-ts-mode)
;;    (sql-mode . sql-ts-mode)
;;    (csv-mode . csv-ts-mode)))

;; (customize-set-variable 'treesit-font-lock-level 2)

;; Projectile custom project types
 ;; .NET C# or F# projects
(projectile-register-project-type 'xdg-desktop-portal-hyprland '("hyprland.portal")
                                  ;;:project-file '("?*.csproj" "?*.fsproj")
                                  :compile "ls"
                                  :run "dotnet run"
                                  :test "dotnet test")

(setq-default indent-tabs-mode t)
(setq-default tab-width 4) ; Assuming you want your tabs to be four spaces wide
(global-set-key (kbd "C-/") 'tab-to-tab-stop)	
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'c-ts-mode-indent-offset 'tab-width)
(defvaralias 'c++-ts-mode-indent-offset 'tab-width)

;;Close compilation buffer
(global-set-key [f10] (lambda () (interactive) (kill-buffer "*compilation*")))

;;Common lisp
(load (expand-file-name "~/.quicklisp/slime-helper.el"))
(setq inferior-lisp-program "sbcl")

;; Change *scratch* to org-mode + fixes for extreme notetaking action
(setq initial-major-mode 'org-mode)
(setq org-startup-truncated nil)
(with-eval-after-load "org" (define-key org-mode-map (kbd "C-j") nil))
;;(define-key org-mode-map (kbd "C-j") nil)
;; 
(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.
 '(package-selected-packages
   '(projectile-ripgrep helm-projectile projectile ssh-agency tangotango-theme)))
(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.
 )
(put 'dired-find-alternate-file 'disabled nil)
)		
(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.
 '(markdown-command "/usr/bin/pandoc")
 '(package-selected-packages
   '(benchmark-init centered-cursor-mode crdt dokuwiki dokuwiki-mode
					helm-projectile lua-mode magit-todos markdown-mode
					projectile-ripgrep ssh-agency sudo-edit super-save
					tangotango-theme treemacs-icons-dired
					treemacs-magit treemacs-persp treemacs-projectile
					treemacs-tab-bar))
 '(ripgrep-arguments '("--follow"))
 '(safe-local-variable-values
   '((projectile-project-compilation-cmd . "latexmk")
	 (compilation-read-command) (compilation-read-command . 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.
 )

(load "~/.emacs.d/roleplay.el")


