(package-initialize)
(use-package use-package
  :custom
  ;; Unless otherwise specified, always install packages if they are absent.
  (use-package-always-ensure t)
  ;; Allow updating built-in packages like eglot
  ;; For some reason, built-in packages are still not updating so I'm just going to comment this out.
  ;; (package-install-upgrade-built-in t)
  ;; Natively compile packages
  (package-native-compile t)
  :config
  (add-to-list 'package-archives
               '("melpa" . "https://melpa.org/packages/")
               )
  )


(use-package auto-package-update
  :ensure t
  :custom
  (auto-package-update-interval 14)
  (auto-package-update-delete-old-versions t)
  :config
  (auto-package-update-maybe)
  )

(defun assert-directory (p)
  (unless (file-exists-p p) (make-directory p t))
  p
  )

(defconst private-dir  (expand-file-name "private" user-emacs-directory))
(defconst temp-dir (format "%s/cache" private-dir)
  "Hostname-based elisp temp directories")
(assert-directory (concat temp-dir "/auto-save-list/"))
(setq autoload-directory (concat user-emacs-directory (file-name-as-directory "elisp") (file-name-as-directory "autoload")))
(add-to-list 'load-path (assert-directory autoload-directory))

(use-package emacs
  :ensure nil
  :bind
  (("C-z" . nil)
   ("C-x C-z" . nil)
   ("RET" . newline-and-indent)
   )
  :custom
  ;; Replace highlighted text if you start typing.
  (delete-selection-mode 1)

  (history-length 300)

  ;; Enable auto-revert for buffers like dired
  (global-auto-revert-non-file-buffers t)

  ;; If the underlying file changes, reload it automatically. This is useful for moving around in git without confusing language servers.
  (auto-revert-avoid-polling t)
  (auto-revert-interval 5)
  (auto-revert-check-vc-info t)
  (global-auto-revert-mode t)

  ;; Disable backup files and lockfiles
  (create-lockfiles nil)
  (make-backup-files nil)
  (backup-inhibited t)

  ;; Do not auto-save files
  (auto-save-default nil)

  (pixel-scroll-precision-mode t)
  (pixel-scroll-precision-use-momentum nil)

  :config
  (setq enable-recursive-minibuffers t)

  ;; Filter the M-x list base on the current mode
  (setq read-extended-command-predicate #'command-completion-default-include-p)

  ;; Enable triggering completion with the tab key.
  (setq tab-always-indent 'complete)

  )

(setq-default
 ;; Point custom-file at /dev/null so emacs does not write any settings to my dotfiles.
 ;; custom-file "/dev/null"
 ;;
 ;; list-package breaks on newer versions of emacs if custom-file is set to /dev/null
 custom-file (expand-file-name "custom.el" user-emacs-directory)
 ;; Don't pop up a small window at the bottom of emacs at launch.
 inhibit-startup-screen t
 inhibit-startup-message t
 ;; Don't show the list of buffers when opening many files.
 inhibit-startup-buffer-menu t
 ;; Give the scratch buffer a clean slate.
 initial-major-mode 'fundamental-mode
 initial-scratch-message nil
 ;; Send prompts to mini-buffer not the GUI
 use-dialog-box nil
 ;; End files with line break
 require-final-newline t
 ;; Use spaces, not tabs
 indent-tabs-mode nil
 ;; Use a better frame title
 frame-title-format '("" invocation-name ": "(:eval (if (buffer-file-name)
                                                        (abbreviate-file-name (buffer-file-name))
                                                      "%b")))
 ;; Use 'y' or 'n' instead of 'yes' or 'no'
 use-short-answers t
 ;; Confirm when opening a file that does not exist
 confirm-nonexistent-file-or-buffer t
 ;; Do not require double space to end a sentence.
 sentence-end-double-space nil
 ;; Show trailing whitespace
 show-trailing-whitespace t
 ;; Remove the line when killing it with ctrl-k
 kill-whole-line t

 ;; Show the current project in the mode line
 project-mode-line t
 )

;; Typed text replaces selection
(delete-selection-mode)


;; Delete trailing whitespace before save
(add-hook 'before-save-hook 'delete-trailing-whitespace)

;;;;; Performance
;; Run garbage collect when emacs is idle
(run-with-idle-timer 5 t (lambda () (garbage-collect)))
(add-function :after after-focus-change-function
              (lambda ()
                (unless (frame-focus-state)
                  (garbage-collect))))

(provide 'base)
