;;; 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 visible-bell            nil)
(setq truncate-lines          t)

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

;; Line
(add-hook 'prog-mode-hook #'display-line-numbers-mode)
(hl-line-mode 1)
(line-number-mode 1)
(column-number-mode 1)
(size-indication-mode 1)

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

;; Cursor
(blink-cursor-mode 1)

;; Theme
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'nano t)
(nano-light)


;; Font
(add-to-list 'default-frame-alist '(font . "JetBrains Mono SemiBold-10"))
(defvar font-size 10)
(defvar font-family "JetBrains Mono SemiBold")
(set-frame-font (format "%s %d" font-family font-size))
(custom-set-faces
 '(shr-text ((t (:inherit variable-pitch-text :family "Noto Sans Light")))))
(set-face-attribute 'italic nil
                    :family "Roboto Mono"
                    :slant 'italic
                    :weight 'bold
                    :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)

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

;; Keybindings
(global-set-key (kbd "C-M-j") (lambda () (interactive) (scroll-up 1)))
(global-set-key (kbd "C-M-k") (lambda () (interactive) (scroll-down 1)))
(global-unset-key (kbd "C-x f"))
(global-set-key (kbd "C-x f f") #'set-fill-column)
(global-set-key (kbd "C-x f i") #'display-fill-column-indicator-mode)
(global-set-key (kbd "C-x f a") #'auto-fill-mode)
(global-set-key (kbd "C-x f n") #'toggle-display-line-numbers)
(global-set-key (kbd "C-v")     #'View-scroll-half-page-forward)
(global-set-key (kbd "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))
        (progn
          (display-line-numbers-mode 1)
          (setq display-line-numbers nil))))

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

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

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

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

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