;; 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 'all-the-icons)
(straight-use-package 'neotree)
(straight-use-package 'rust-mode)
(straight-use-package 'markdown-mode)
(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 'rainbow-delimiters)
(straight-use-package 'focus)
(straight-use-package 'solarized-theme)
(straight-use-package 'windresize)
(straight-use-package 'yasnippet)
(straight-use-package
'(janet-mode :type git :host github
:repo "ALSchwalm/janet-mode"))
(straight-use-package
'(puppet-mode :type git :host github
:repo "voxpupuli/puppet-mode"))
(straight-use-package
'(pico8-mode :type git :host github
:repo "Kaali/pico8-mode"))
(straight-use-package
'(fzf :type git :host github
:repo "bling/fzf.el"))

;; EasyPG settings
(setq epa-pinentry-mode 'loopback)

;; add hook for searching in zeal
;; https://zealdocs.org/
(straight-use-package
'(zeal-at-point :type git :host github
:repo "jinzhu/zeal-at-point"))

(global-set-key (kbd "C-c v") 'zeal-at-point)

;; newlisp mode
;; (straight-use-package
;; '(newlisp-mode
;; :type git :host github
;; :repo "may/newlisp-mode"))
;; ;; Straight weirdly doesn't resolve this correctly
;; (load-file "~/.emacs.d/straight/repos/newlisp-mode/newlisp.el")

;; Mastodon
;; (straight-use-package
;; '(mastodon
;; :type git :host nil
;; :repo "https://codeberg.org/martianh/mastodon.el"))

;; Gopher browser
(straight-use-package 'elpher)
(straight-use-package 'xterm-color)
(defun display-ansi-colors ()
(interactive)
(let ((inhibit-read-only t))
(ansi-color-apply-on-region (point-min) (point-max))))

;;; Terminal in emacs via vterm lib
(straight-use-package
'(vterm :type git :host github
:repo "akermu/emacs-libvterm"))

;;; Vterm config
(setq vterm-buffer-name-string "%s")
(add-hook 'vterm-mode-hook
(lambda ()
(set (make-local-variable 'buffer-face-mode-face) 'fixed-pitch)
(buffer-face-mode t)))

(global-set-key (kbd "C-c <escape>") 'vterm-send-escape) ;
(global-set-key (kbd "C-c C-u") 'vterm-send-C-u) ; Readline line
(global-set-key (kbd "C-c C-w") 'vterm-send-C-w) ; Readline word
(global-set-key (kbd "C-c C-d") 'vterm-send-C-d) ; Exit
(global-set-key (kbd "C-c C-s") 'vterm-send-C-r) ; Reverse search
(global-set-key (kbd "C-b") 'vterm-send-C-b) ; Tmux
(global-set-key (kbd "C-c C-a") 'vterm-send-C-a) ; Screen prefix

;;; Syntax highlights for uxn-tal
;; (straight-use-package
;; '(tal-mode :type git :host github
;; :repo "rafapaezbas/uxntal-mode"))

;; Haskell Mode
(straight-use-package 'haskell-mode)

;;; Cursor highlighter
(straight-use-package
'(beacon :type git :host github
:repo "Malabarba/beacon"))
(beacon-mode)
(global-set-key (kbd "C-c B") 'beacon-mode)

;; Racket config
;; (straight-use-package 'racket-mode)
;; Using DrRacket for now

;; Org to reveal.js presentations
;; Docs: https://github.com/yjwen/org-reveal#configuration
(straight-use-package 'org-reveal)
(require 'ox-reveal)
(setq org-reveal-root "file:///home/swatson/Repos/reveal.js")

;; Vimperator like hints for
;; file navigationaki2o/emacs-pophint
(straight-use-package
'(pophint :type git :host github
:repo "aki2o/emacs-pophint"))

(global-set-key (kbd "C-c f") 'pophint:do-flexibly)

;;; Code folding
(add-hook 'prog-mode-hook 'hs-minor-mode)

(global-set-key (kbd "C-c a") 'hs-toggle-hiding)

;;; Error checking
(global-set-key (kbd "C-c 2 e") 'flymake-mode)

;; System monitor
;; (straight-use-package
;; '(symon :type git :host github
;; :repo "zk-phi/symon"))
;; For whatever reason, using straight to build this
;; package results in bugs on Linux (not OSX). I'm assuming
;; because straight is building from git and there is a regression.
;; As such -- MELPA config
;; This config is likely redudant and I can use the straight
;; MELPA interface, but it's working and I'm too lazy to change it
;; 2022/03/03 - Using straight MEPLA interface also surfaces this bug.
;; Below seems to be only reliable cross platform way to include this package
;; (require 'package)
;; (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
;; (package-install 'symon)
;; ;;(symon-mode 1)

(straight-use-package
'(telephone-line :type git :host github
:repo "dbordak/telephone-line"))
(telephone-line-mode 1)

;;; Indent highlighting
(straight-use-package
'(highlight-indent-guides
:type git :host github
:repo "DarthFennec/highlight-indent-guides"))
(setq highlight-indent-guides-method 'character)
(add-hook 'prog-mode-hook 'highlight-indent-guides-mode)

;; Org mode
;; Get src block tabs to work how I expect
(setq org-src-tab-acts-natively t)
(setq org-src-preserve-indentation nil
org-edit-src-content-indentation 0)

;; tramp
(setq tramp-default-method "ssh")

;; Ivy
;; (straight-use-package 'ivy)
;; (ivy-mode)
;; (setq ivy-use-virtual-buffers t)
;; (setq enable-recursive-minibuffers t)

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

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

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

;; Common Lisp
(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))

;; Guile
(straight-use-package 'geiser)
(straight-use-package 'geiser-guile)
(setq geiser-active-implementations '(guile))

(require 'markdown-mode)

(require 'neotree)
(global-set-key (kbd "C-c n") 'neotree-toggle)

(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 40)
(setq sublimity-map-fraction 0.3)
(setq sublimity-map-text-scale -5)
(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
;;; If scrolling performance is bad, remember the profiler:
;;; https://christiantietze.de/posts/2020/12/emacs-scroll-performance-projectile/
(setq mouse-wheel-progressive-speed t)

;; whitespace config
;; make whitespace-mode use just basic coloring
(setq whitespace-style '(face tabs))
(setq tab-face (make-face 'tab-face))
(set-face-background 'tab-face "gray")
(setq whitespace-tab 'tab-face)

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

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

;; General is typically used for mode specific
;; rebinds and keymaps, while 'global-set-key
;; is still used for global keymaps for the sake of
;; clarity
(straight-use-package 'general)
;; Keybinds/macros
(global-set-key (kbd "C-c C-q") #'mu-magit-kill-buffers)
(global-set-key (kbd "C-c ;") 'comment-line)
(global-set-key (kbd "C-c b") 'fzf-switch-buffer)
(global-set-key (kbd "C-c z") '(lambda ()
(interactive)
(switch-to-buffer "*scratch*")
(fzf-find-file)))
(global-set-key (kbd "C-c e") '(lambda ()
(interactive)
(diff-hl-mode)
(diff-hl-flydiff-mode)))
(global-set-key (kbd "C-c d") 'slime-describe-symbol)
(global-set-key (kbd "C-c M-r") 'slime-restart-inferior-lisp)
(global-set-key (kbd "C-c D") 'dimmer-mode)
(global-set-key (kbd "C-c S") 'sublimity-mode)
(global-set-key (kbd "C-c s") 'flyspell-correct-word-before-point)
(global-set-key (kbd "C-c F") 'focus-mode)
(global-set-key (kbd "C-c r") '(lambda ()
(interactive)
(load-file (file-truename "~/.emacs"))))
(global-set-key (kbd "C-c C-e") '(lambda ()
(interactive)
(find-file (file-truename "~/.emacs"))
(with-current-buffer (buffer-name)
(emacs-lisp-mode))))
(global-set-key (kbd "C-c l") 'display-line-numbers-mode)
(global-set-key (kbd "C-c w") 'window-swap-states)
(global-set-key (kbd "C-c C-W") 'windresize)
(global-set-key (kbd "C-c W") 'whitespace-mode)
(global-set-key (kbd "C-c t") 'sol-dk)
(global-set-key (kbd "C-c y") 'sol-l)
(global-set-key (kbd "C-c g") 'magit)
(global-set-key (kbd "C-c C-.") '(lambda ()
(interactive)
(switch-to-buffer "*Messages*")))
(global-set-key (kbd "C-c C-f") 'find-file-at-point)
(global-set-key (kbd "C-c m p") '(lambda ()
(interactive)
(shell-command "mpc toggle")))
(global-set-key (kbd "C-c n") 'neotree-toggle)

;;; 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-post-buffer)
(global-set-key (kbd "C-c C-o") 'rclone-get-org)

;; Small helper functions
(load "~/Repos/dotfiles/emacs/elisp/helpers.el")
(global-set-key (kbd "C-c p") 'display-full-buffer-path)
(global-set-key (kbd "C-c C-P") 'post-region-to-pb)
(global-set-key (kbd "C-c 2 p") 'display-pb)

;; OpenAI script
(load "~/Repos/dotfiles/emacs/elisp/openai.el")
(global-set-key (kbd "C-c q") 'ask-gpt)

;; Disable bars
;; (menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)

;; Start in server mode
(server-start)

;; 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)
;; (global-set-key (kbd "C-c n") 'toggle-frame-fullscreen)
;; Graphic Display
(if (display-graphic-p)
(progn
(load-theme 'solarized-dark t)))))

;; Load for GUI Linux
(if (eq system-type 'gnu/linux)
(if (display-graphic-p)
(progn
;; normal home/end behavior in GUI
(global-set-key (kbd "<home>") 'beginning-of-line)
(global-set-key (kbd "<end>") 'end-of-line)
(load (expand-file-name "~/quicklisp/slime-helper.el"))
(setq inferior-lisp-program (locate-file "sbcl" exec-path exec-suffixes 1))
;; https://emacs.stackexchange.com/questions/332/how-can-i-find-the-path-to-an-executable-with-emacs-lisp
(setq yas-snippet-dirs
'("~/Repos/dotfiles/emacs/yasnippets/"))
(yas-global-mode 1)

(if (file-exists-p "~/.enable_exwm")
(progn
(straight-use-package 'exwm)
(require 'exwm)
(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)
(progn
(load "~/Repos/dotfiles/emacs/linux-load/customs.el")
(xterm-mouse-mode t)
(menu-bar-mode -1)
(setq vc-follow-symlinks t) ;;; Typically in the full GUI I want to know it's a symlink
(global-set-key (kbd "<home>") 'beginning-of-line)
(global-set-key (kbd "<end>") 'end-of-line)
(global-set-key (kbd "C-c c c") (lambda ()
(interactive)
(shell-command-on-region (point) (mark) "xclip -selection clipboard")))
(load (expand-file-name "~/quicklisp/slime-helper.el"))
(setq inferior-lisp-program (locate-file "sbcl" exec-path exec-suffixes 1))))