;;; general.el --- General configuration settings.
;;; Commentary:
;;    General configuration for Emacs.
;;; Code:

(require 'nano-theme)
(require 'view)



;; Backups
(setq make-backup-files nil)
(setq vc-make-backup-files nil)
(setq kept-new-versions nil)
(setq kept-old-versions nil)
(setq delete-old-versions t)
(rassq-delete-all 'auto-save-mode auto-mode-alist)
(setq auto-save-default nil)
(setq auto-save-mode -1)
(setq backup-directory-alist '(("." . "~/.emacs.d/backups/")))
(setq backup-by-copying t)
(setq create-lockfiles nil)

;; UI
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(setq inhibit-startup-message t)
(setq-default truncate-lines t)

;; Bell
(setq visible-bell nil)
(setq ring-bell-function 'ignore)

;; Scratch
(setq initial-scratch-message nil)

;; Line
(column-number-mode 1)
(size-indication-mode 1)
(setq display-line-numbers 'relative)

;; Window splitting
(setq-default split-height-threshold
  (cond ((string= (system-name) "framework") 100)
        ;; default emacs value
        (t 80)))
(setq-default split-width-threshold
  (cond ((string= (system-name) "framework") 180)
        ;; default emacs value
        (t 160)))

;; Tabs
(setq-default tab-width 2)

;; Cursor
(blink-cursor-mode 1)

;; Theme & Font
(load-theme 'nano t)
(nano-light)

(defvar priime--font-config
  (cond ((string= (system-name) "framework") '("Roboto Mono Medium" "Roboto" 10 0.8))
        ((eq system-type 'gnu/linux)         '("Roboto Mono Medium" "Roboto" 10 0.8))
        ((eq system-type 'darwin)            '("Menlo" "Verdana" 12 1))
        (t                                   '("Roboto Mono" "Roboto" 10 0.8))))

(defvar priime-fixed-font    (-first-item priime--font-config))
(defvar priime-variable-font (-second-item priime--font-config))
(defvar priime-font-size     (-third-item priime--font-config))
(defvar priime-fixed-height  (-fourth-item priime--font-config))

(add-to-list 'default-frame-alist `(font . ,(format "%s-10" priime-fixed-font)))
(set-frame-font (format "%s %d" priime-fixed-font priime-font-size))

(custom-set-faces
 '(region                   ((t (:inherit nano-subtle :background "#EBE5F5"))))
 '(lazy-highlight           ((t (:inherit region))))
 `(variable-pitch           ((t (:inherit default :family ,priime-variable-font :height 125 :weight regular))))
 `(fixed-pitch              ((t (:family ,priime-fixed-font :height ,priime-fixed-height :inherit nil))))
 `(shr-text                 ((t (:inherit variable-pitch-text :family ,priime-variable-font))))
 '(line-number              ((t (:inherit default :foreground "#98A4AE"))))
 '(line-number-current-line ((t (:inherit default :foreground "#98A4AE")))))
(set-face-attribute 'italic nil
                    :family 'inherit
                    :slant 'italic
                    :weight 'medium
                    :foreground nano-light-foreground)

;; Use spaces instead of tabs
(setq-default indent-tabs-mode nil)

;; Column width
(setq fill-column 80)

;; Directional window moving
(windmove-default-keybindings)

;; Auto file refresh
(global-auto-revert-mode t)

;; Improve performance in files with long lines
(global-so-long-mode t)

;; Automatically follow symlinks
(setq vc-follow-symlinks t)

;; Undos
(setq undo-limit 200000)

;; Whitespace
(setq-default show-trailing-whitespace t)

;; Sentences
;; Emacs thinks that sentences ends with period and two spaces,
;; disable this.
(setq sentence-end-double-space nil)

;; Scrolling
(put 'scroll-left 'disabled nil)
(put 'scroll-right 'disabled nil)
(setq scroll-conservatively 101)

;; Keybindings
(keymap-global-set "<f6>"
                   (lambda ()
                     (interactive)
                     (split-window-right)
                     (windmove-right)))
(keymap-global-set "<f7>"
                   (lambda ()
                     (interactive)
                     (split-window-below)
                     (windmove-down)))
(keymap-global-set "<f8>"
                   (lambda ()
                     (interactive)
                     (split-window-right)
                     (windmove-right)
                     (if (projectile-project-p)
                         (projectile-run-vterm)
                       (vterm))
                     (balance-windows)))
(keymap-global-set "C-M-j" (lambda () (interactive) (scroll-up 1)))
(keymap-global-set "C-M-k" (lambda () (interactive) (scroll-down 1)))
(keymap-global-set "C-x <"
                   (lambda (arg)
                     (interactive "P")
                     (let ((amt (or arg 1)))
                       (scroll-right amt))))
(keymap-global-set "C-x >"
                   (lambda (arg)
                     (interactive "P")
                     (let ((amt (or arg 1)))
                       (scroll-left amt))))
(keymap-global-unset "C-x f")
(keymap-global-set "C-x f f" #'set-fill-column)
(keymap-global-set "C-x f i" #'display-fill-column-indicator-mode)
(keymap-global-set "C-x f a" #'auto-fill-mode)
(keymap-global-set "C-x f n" #'toggle-display-line-numbers)
(keymap-global-set "C-v"     #'View-scroll-half-page-forward)
(keymap-global-set "M-v"     #'View-scroll-half-page-backward)

(defun toggle-display-line-numbers ()
  "Toggle the display of line numbers."
  (interactive)
  (cond ((equal display-line-numbers t)
         (setq display-line-numbers 'relative))
        ((equal display-line-numbers 'relative)
         (setq display-line-numbers t))
        (t
         (progn
           (display-line-numbers-mode 1)
           (setq display-line-numbers t)))))

;; Garbage Collection
(setq gc-cons-threshold 50000000)

;; Confirmations
(fset 'yes-or-no-p 'y-or-n-p)

;; Disable suspend state
(keymap-global-unset "C-z")
(keymap-global-unset "C-x C-z")

;; emacs-nativecomp
(setq native-comp-async-report-warnings-errors nil)

;; From https://stackoverflow.com/questions/8309769/how-can-i-prevent-emacs-from-opening-new-window-for-compilation-output

;; Helper for compilation. Close the compilation window if
;; there was no error at all. (emacs wiki)
(defun compilation-exit-autoclose (status code msg)
  ;; If M-x compile exists with a 0
  (when (and (eq status 'exit) (zerop code))
    ;; then bury the *compilation* buffer, so that C-x b doesn't go there
    (bury-buffer)
    ;; and delete the *compilation* window
    (delete-window (get-buffer-window (get-buffer "*compilation*"))))
  ;; Always return the anticipated result of compilation-exit-message-function
  (cons msg code))
;; Specify my function (maybe I should have done a lambda function)
(setq compilation-exit-message-function 'compilation-exit-autoclose)

;; shell
(setq vterm-shell (or (executable-find "fish") shell-file-name))
(keymap-global-set "C-c v" #'vterm)

;; epa/gpg
(setq epa-file-encrypt-to '("lucas@priime.dev"))
(setq epa-file-select-keys 1)

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