;;; PACKAGES
;; Bootstrap Straight Package Manager
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

;; packages to install and load
(straight-use-package 'auto-complete)
(straight-use-package '(nano-theme :type git :host github
                                   :repo "rougier/nano-theme"))
(straight-use-package
 '(slime-company :type git :host github :repo "anwyn/slime-company"))
(straight-use-package 'sublimity)
(straight-use-package 'dashboard)
(straight-use-package 'all-the-icons)
(straight-use-package 'neotree)
(straight-use-package 'rust-mode)
(straight-use-package 'powerline)
(straight-use-package 'markdown-mode)
(straight-use-package 'moe-theme)
(straight-use-package 'ample-theme)
(straight-use-package 'slime)
(straight-use-package 'ac-slime)
(straight-use-package 'evil)
(straight-use-package 'dimmer)
(straight-use-package 'magit)
(straight-use-package 'diff-hl)
(straight-use-package 'powerline-evil)
(straight-use-package 'rainbow-delimiters)
(straight-use-package 'focus)
(straight-use-package 'solarized-theme)
(straight-use-package 'windresize)
(straight-use-package 'yasnippet)

(require 'yasnippet)

;; Configure focus
(require 'focus)
(add-to-list 'focus-mode-to-thing '(lisp-mode . paragraph))

;; rainbow delims
(require 'rainbow-delimiters)
(add-hook 'prog-mode-hook #'rainbow-delimiters-mode)

;; Git stuff
(require 'magit)
(require 'diff-hl)
;; Just enable diff-hl per buffer,
;; it seems expensive 
;; (global-diff-hl-mode)
(add-hook 'magit-pre-refresh-hook 'diff-hl-magit-pre-refresh)
(add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh)
(diff-hl-margin-mode t)

(require 'dimmer)
(setq dimmer-fraction 0.4)

;; always enable ido mode
(require 'ido)
(ido-mode t)

(require 'evil)
(evil-mode 1)

;; Enable tab behavior like vi
(straight-use-package '(evil-tabs :type git :host github
                                  :repo "krisajenkins/evil-tabs"))
(require 'evil-tabs)
(global-evil-tabs-mode t)

(require 'auto-complete)
(global-auto-complete-mode t)

(require 'slime)
(require 'ac-slime)
(slime-setup '(slime-fancy ac-slime))
(add-hook 'slime-mode-hook 'set-up-slime-ac)
(add-hook 'slime-repl-mode-hook 'set-up-slime-ac)
(eval-after-load "auto-complete"
  '(add-to-list 'ac-modes 'slime-repl-mode))

(require 'markdown-mode)

(require 'powerline)
;;(powerline-default-theme)
;; Better powerline support for evil mode
(require 'powerline-evil)
(powerline-evil-vim-theme)

(require 'neotree)
(global-set-key [f8] 'neotree-toggle)

(require 'dashboard)
(dashboard-setup-startup-hook)

(require 'all-the-icons)

(require 'sublimity)
(require 'sublimity-scroll)
(require 'sublimity-map) ;; experimental
(require 'sublimity-attractive)
(setq sublimity-scroll-weight 10
      sublimity-scroll-drift-length 5)
(setq sublimity-map-size 30)
(setq sublimity-map-fraction 0.3)
(setq sublimity-map-text-scale -9)
(sublimity-map-set-delay 0.1)
(setq sublimity-attractive-centering-width nil)

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

;;; Generic options for all OSs
; Smooth mouse scroll a bit
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1) ((control) . nil)))
(setq mouse-wheel-progressive-speed nil)

;; normal home/end behavior
(global-set-key (kbd "<home>") 'beginning-of-line)
(global-set-key (kbd "<end>") 'end-of-line)

;; Custom functions
(defun reload-dot-emacs ()
  (interactive)
  (load-file (file-truename "~/.emacs")))

(defun sol-dk ()
  (interactive)
  (load-theme 'solarized-dark))

(defun sol-l ()
  (interactive)
  (load-theme 'solarized-light))

;; Keybinds/macros
(global-set-key (kbd "C-c e") 'diff-hl-mode)
(global-set-key (kbd "C-c d") 'dimmer-mode)
(global-set-key (kbd "C-c s") 'sublimity-mode)
(global-set-key (kbd "C-c f") 'focus-mode)
(global-set-key (kbd "C-c r") 'reload-dot-emacs)
(global-set-key (kbd "C-c l") 'display-line-numbers-mode)
(global-set-key (kbd "C-c w") 'windresize)
(global-set-key (kbd "C-c t") 'sol-dk)
(global-set-key (kbd "C-c g") 'sol-l)
(global-set-key (kbd "C-c y") 'evil-mode) ; Quick toggle evil-mode for slime err
(global-set-key (kbd "C-c h") 'tramp-cleanup-connection)
(global-set-key (kbd "C-c C-.") '(lambda ()
				  (interactive)
				  (switch-to-buffer "*Messages*")))

;;; Can probably be replaced with
;;; a yasnippet
(fset 'date-to-point
   (kmacro-lambda-form [?\C-u ?\M-! ?d ?a ?t ?e return] 0 "%d"))

;;; Erc config
(setq erc-nick "speskk")
(setq erc-hide-list '("JOIN" "PART" "QUIT"))
(if (file-exists-p "~/.emacs.d/.erc-auth.el")
    (progn
      (load "~/.emacs.d/.erc-auth.el")
      (defun connect-to-libera-erc () (interactive)
	(erc :server "irc.libera.chat" :port "6667"
             :nick "speskk" :password *libera-chat*))))

;; Allow M-up / M-down behavior to switch windows
(windmove-default-keybindings 'meta)

(display-time-mode t)

;; Perl Config
(setq
 perl-indent-parens-as-block 0
 perl-indent-level 8)

;; Set to make mouse focus window
(setq mouse-autoselect-window t
      focus-follows-mouse t)

;; Custom eshell prompt
(setq eshell-prompt-function
 (lambda nil
   " λ > "))
(setq eshell-prompt-regexp "^ λ > ")

(set-language-environment "UTF-8")

;; Simple rclone cli wrapper
(load "~/Repos/dotfiles/emacs/elisp/rclone.el")
(global-set-key (kbd "C-c o") 'rclone-copy-buffer)


;; Load for macOS
;;; Slime/etc
(if (eq system-type 'darwin)
    (progn
      ;; Configure yasnippet
      (setq yas-snippet-dirs
	    '("~/Repos/dotfiles/emacs/yasnippets/"))
      (yas-global-mode 1)
      
      (setq inferior-lisp-program "/opt/homebrew/bin/sbcl")
      (load (expand-file-name "~/.local/opt/quicklisp/slime-helper.el"))
      ;; Font/themes/etc
      (load "/Users/swatson/Repos/dotfiles/emacs/mac-load/customs.el")

      ;; Easily set cua mode on/off so that I don't have to
      ;; use shift+insert for paste on mac
      (global-set-key (kbd "C-c c") 'cua-mode)
      ;; Graphic Display
      (if (display-graphic-p)
	  (progn
	    (load-theme 'solarized-dark t)))))

;; Load for Windows10
(if (eq system-type 'windows-nt)
    (progn
      ;; Configure yasnippet
      (setq yas-snippet-dirs
	    '("C:/Users/watsonsi/Documents/Github/dotfiles/emacs/yasnippets/"))
      (yas-global-mode 1)

      (load "C:/Users/watsonsi/Documents/GitHub/dotfiles/emacs/windows-load/customs.el")
      (setq visible-bell 1)
      (load-theme 'solarized-light t)))

;; Load for GUI Linux
(if (eq system-type 'gnu/linux)
    (if (display-graphic-p)
	(progn
	  (setq yas-snippet-dirs
	    '("~/Repos/dotfiles/emacs/yasnippets/"))
	  (yas-global-mode 1)

	  (straight-use-package 'exwm)
	  (require 'exwm)

	  ;(require 'exwm-systemtray)
	  ;(exwm-systemtray-enable)

	  (load "~/Repos/dotfiles/emacs/linux-load/exwm-config.el")
	  
	  (require 'exwm-config-spw)
	  (exwm-config-spw)

	  (setq visible-bell 1)
	  (load "~/Repos/dotfiles/emacs/linux-load/customs.el")
	  (load-theme 'solarized-light t))))
      
;; Load for Term
(unless (display-graphic-p)
  (xterm-mouse-mode t))