(package-initialize)
(use-package use-package)

(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/")
             )

(use-package auto-package-update
   :ensure t
   :config
   (setq auto-package-update-delete-old-versions t
         auto-package-update-interval 14)
   (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))

(setq-default
 ;; Disable backup files and lockfiles
 make-backup-files nil
 auto-save-default nil
 create-lockfiles nil
 ;; Unless otherwise specified, always install packages if they are absent.
 use-package-always-ensure t
 ;; Point custom-file at /dev/null so emacs does not write any settings to my dotfiles.
 custom-file "/dev/null"
 ;; 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
 ;; Natively compile packages
 package-native-compile 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
 )

;; (setq-default fringes-outside-margins t)

;; Per-pixel scrolling instead of per-line
(pixel-scroll-precision-mode)

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


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

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

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