;;; early-init.el --- Early initialization file for Emacs  -*- lexical-binding: t; -*-

;;; Commentary:
;; Emacs 27.1 introduced early-init.el, which is run before init.el, before
;; package and UI initialization happens, and before site files are loaded.

;;; Code:
;; A big contributor to startup times is garbage collection.  We up the gc
;; threshold to temporarily prevent it from running, then reset it later by
;; enabling `gcmh-mode'.  Not resetting it will cause stuttering/freezes.
(setq gc-cons-threshold (* 50 1000 1000))          ;; Make startup faster by reducing the frequency of garbage collection. The default is 800 kilobytes. Measured in bytes. Will be decreased again at the end.

;; make UTF-8 the default coding system:
(set-language-environment "UTF-8")
;; `set-language-enviornment' sets `default-input-method', which is unwanted
(setq default-input-method nil)

(defvar config-dir (file-name-directory (file-truename load-file-name)))
(setq user-emacs-directory "~/.config/emacs/")  ;; for cache etc.
(defconst user-cache-directory
  (file-name-as-directory (concat user-emacs-directory ".cache"))
  "My Emacs storage area for persistent files.")
;; create the `user-cache-directory' if not exists
(make-directory user-cache-directory t)

;; path where settings files are kept
(add-to-list 'load-path (concat config-dir "settings"))
;; personal elisp lib dir, for manually installed packages
(add-to-list 'load-path (concat config-dir "lisp"))
;; add all directories from config-dir/lisp into load-path
(let ((default-directory (concat config-dir "lisp")))
  (normal-top-level-add-subdirs-to-load-path))

;;
;; FRAME
;;
;; (add-to-list 'default-frame-alist '(fullscreen . maximized))       ;; restore saved geometry, see gui-settings.el my-frame-geometry-
(add-to-list 'default-frame-alist '(tool-bar-position . left))
;; (add-to-list 'default-frame-alist '(horizontal-scroll-bars . t))   ;; see gui-settings

;; transparency
(set-frame-parameter (selected-frame) 'alpha '(95 . 95))              ;; VALUE: '(<active> . <inactive>) / <both>
(add-to-list 'default-frame-alist '(alpha . (95 . 95)))

;; Custom functions/hooks for persisting/loading frame geometry upon save/load
(defvar my-frame-geometry-file (concat user-cache-directory "frame-geometry.el"))
(defun my-frame-geometry-save ()
  "Gets the current frame's geometry and save it to `my-frame-geometry-file'."
  (let ((frameg-font (frame-parameter (selected-frame) 'font))
        (frameg-left (frame-parameter (selected-frame) 'left))
        (frameg-top (frame-parameter (selected-frame) 'top))
        (frameg-width (frame-parameter (selected-frame) 'width))
        (frameg-height (frame-parameter (selected-frame) 'height))
        (frameg-file my-frame-geometry-file))
    (with-temp-buffer
      ;; Turn off backup for this file
      (make-local-variable 'make-backup-files)
      (setq make-backup-files nil)
      (insert
       ";;; This file stores the previous emacs frame's geometry.\n"
       ";;; Last generated " (current-time-string) ".\n"
       "(setq initial-frame-alist\n"
       ;; " '((font . \"" frameg-font "\")\n"
       " '("
       (format " (top . %d)\n" (max frameg-top 0))
       (format " (left . %d)\n" (max frameg-left 0))
       (format " (width . %d)\n" (max frameg-width 0))
       (format " (height . %d)))\n" (max frameg-height 0)))
      (when (file-writable-p frameg-file)
        (write-file frameg-file)))))
(defun my-frame-geometry-load ()
  "Load `my-frame-geometry-file' which should load the previous frame's geometry."
  (let ((frameg-file my-frame-geometry-file))
    (when (file-readable-p frameg-file)
      (load-file frameg-file))))
;; Special work to do ONLY when there is a window system being used
;; (if (display-graphic-p)
;;     (progn
;;       (add-hook 'kill-emacs-hook 'my-frame-geometry-save)))
(my-frame-geometry-load)
(add-hook 'window-setup-hook
          (lambda ()
            (if (display-graphic-p)
                (add-hook 'kill-emacs-hook 'my-frame-geometry-save))))

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