;;; init.el --- Coast's ~/.emacs.d/init.el
;;
;;; Commentary:
;; This is my GNU/Emacs configuration --
;; It has:
;; - (TEMPORARY) Evil mode; as I've gotten used to Vim binds when I was away from GNU/Emacs for a bit.
;; - Automatic syntax-highlighting.
;; - Has a minimap.
;; - Bad theme; it's fine on my eyes, though -- I like the 'solarized' theme >:3
;; - It's also gay.
;;
;;; Code:
;;
(require 'package)
(setq package-archives
      '(("gnu" . "https://elpa.gnu.org/packages/")
        ("melpa" . "https://melpa.org/packages/")))
(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))
(require 'use-package)

;;; Message:
;; "run" command (M-x / evil)
(defun coast/run-current-file ()
  (interactive)
  (let ((file (buffer-file-name)))
    (cond
     ((not file)
      (message "No file to run!"))
     ((string-match "\\.py\\'" file)
      (compile (format "python3 %s" file)))
     ((string-match "\\.c\\'" file)
      (let* ((out (concat (file-name-sans-extension file) ".out")))
        (compile (format "gcc %s -o %s && %s" file out out))))
     ((string-match "\\.sh\\'" file)
      (compile (format "bash %s" file)))
     ((string-match "\\.rs\\'" file)
      (let* ((out (file-name-sans-extension file)))
        (compile (format "rustc %s && %s" file out))))
     ((string-match "\\.lua\\'" file)
      (compile (format "lua %s" file)))
     ((string-match "\\.js\\'" file)
      (compile (format "node %s" file)))
     ((coast/file-has-shebang-p file)
      (compile (format "%s" file)))
     (t (message "Not sure how to run this.")))))

(defun coast/file-has-shebang-p (file)
  (when (and file (file-readable-p file))
    (with-temp-buffer
      (insert-file-contents-literally file nil 0 128)
      (goto-char (point-min))
      (looking-at "^#!"))))

(defalias 'run #'coast/run-current-file)

(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(global-display-line-numbers-mode 1)
(global-hl-line-mode 1)
(electric-pair-mode 1)

(setq-default electric-pair-pairs
              '((?\" . ?\")
                (?\{ . ?\})
                (?\( . ?\))
                (?\[ . ?\])
                (?\< . ?\>)))

(setq-default electric-pair-text-pairs electric-pair-pairs)

(set-frame-parameter (selected-frame) 'alpha '(95 . 95))
(add-to-list 'default-frame-alist '(alpha . (95 . 95)))

(set-face-attribute 'default nil :family "Inconsolata" :height 180)

(setq backup-directory-alist `((".*" . "~/.local/tmp/emacsbackup/")))
(setq make-backup-files t)
(setq backup-by-copying t)

(use-package solarized-theme :ensure t)
(load-theme 'solarized-selenized-dark t)

(use-package all-the-icons :ensure t :if (display-graphic-p))
(use-package rainbow-mode :ensure t :hook (prog-mode . rainbow-mode))
(use-package elcord :ensure t :config (elcord-mode 1))

(use-package neotree :ensure t :bind ("<f9>" . neotree-toggle))

(use-package vertico :ensure t :config (vertico-mode 1))
(use-package marginalia :ensure t :hook (vertico-mode . marginalia-mode))
(use-package consult
  :ensure t
  :bind (("C-x b" . consult-buffer)
         ("C-s" . consult-line)))

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

(use-package company :ensure t :hook (prog-mode . company-mode))
(use-package yasnippet :ensure t :hook (prog-mode . yas-minor-mode))
(use-package flycheck :ensure t :hook (prog-mode . flycheck-mode))

(global-set-key (kbd "C-c t") 'ansi-term)

(use-package web-mode
  :ensure t
  :mode "\\.html?\\'"
  :config
  (setq web-mode-enable-auto-pairing t
        web-mode-enable-auto-closing t
        web-mode-enable-auto-expanding t))

(use-package emmet-mode
  :ensure t
  :hook ((web-mode html-mode css-mode) . emmet-mode)
  :config
  (setq emmet-expand-jsx-className? t))

(use-package python :mode "\\.py\\'")
(use-package sh-script :mode "\\.sh\\'")
(use-package cc-mode)
(use-package markdown-mode :ensure t :mode "\\.md\\'")
(use-package yaml-mode :ensure t :mode "\\.ya?ml\\'")
(use-package macrostep :ensure t)

;;; Message:
;; Language-setup.
(use-package python
  :mode ("\\.py\\'" . python-mode)
  :interpreter ("python" . python-mode))

(use-package rust-mode
  :ensure t
  :mode ("\\.rs\\'" . rust-mode))

(use-package sh-script
  :mode (("\\.sh\\'" . sh-mode)
         ("\\.bash\\'" . sh-mode)
         ("\\.zsh\\'" . sh-mode))
  :interpreter (("bash" . sh-mode)
                ("sh" . sh-mode)
                ("zsh" . sh-mode)))

(use-package lua-mode
  :ensure t
  :mode ("\\.lua\\'" . lua-mode))

(use-package cc-mode
  :mode (("\\.c\\'" . c-mode)
         ("\\.h\\'" . c-mode)
         ("\\.cpp\\'" . c++-mode)
         ("\\.hpp\\'" . c++-mode))
  :interpreter (("c" . c-mode)
                ("cpp" . c++-mode)))

(use-package markdown-mode
  :ensure t
  :mode ("\\.md\\'" . markdown-mode))

(use-package yaml-mode
  :ensure t
  :mode ("\\.ya?ml\\'" . yaml-mode))

;;; Message
;; -- Set window title --
(setq frame-title-format '("%b —— GNU/Emacs"))

;;; Message:
;; -- The below may have been temporarily added --

(use-package doom-modeline
  :ensure t
  :init
  (setq doom-modeline-height 25
        doom-modeline-bar-width 3
        doom-modeline-buffer-file-name-style 'truncate-with-project
        doom-modeline-icon t
        doom-modeline-major-mode-icon t
        doom-modeline-enable-word-count t
        doom-modeline-vcs-max-length 12
        doom-modeline-minor-modes nil)
  :config
  (doom-modeline-mode 1))

(setq evil-want-keybinding nil)

(use-package evil
  :ensure t
  :init (setq evil-want-integration t
              evil-want-C-u-scroll t)
  :config (evil-mode 1))

(use-package evil-collection
  :after evil
  :ensure t
  :config (evil-collection-init))

(use-package evil-leader
  :ensure t
  :config
  (global-evil-leader-mode)
  (evil-leader/set-leader "<SPC>")
  (evil-leader/set-key
    "f" 'find-file
    "b" 'switch-to-buffer
    "k" 'kill-buffer
    "t" 'ansi-term
    "e" 'eval-buffer
    "r" 'coast/run-current-file))

(use-package evil-surround :ensure t :config (global-evil-surround-mode 1))
(use-package evil-commentary :ensure t :config (evil-commentary-mode 1))

(use-package corfu
  :ensure t
  :custom
  (corfu-auto t)
  (corfu-cycle t)
  (corfu-quit-no-match nil)
  :init
  (global-corfu-mode))

(use-package cape
  :ensure t)

(defun my/evil-ex-corfu-setup ()
  (setq-local completion-at-point-functions
              (list (cape-super-capf #'completion-at-point))))

(add-hook 'evil-ex-completion-hook #'my/evil-ex-corfu-setup)

(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.
 '(warning-suppress-log-types '((use-package))))
(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.
 )

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