#+TITLE: Core Emacs Configuration
#+PROPERTY: header-args :tangle ~/.emacs.d/config.el

* Front matter

#+begin_src elisp :tangle ~/.emacs
  ;;; .emacs --- Global settings
  ;;; Commentary:
  ;;; Code:

  ;; -*- lexical-binding: t; -*-
#+end_src

#+begin_src elisp
  ;;; config.el --- Summary
  ;;; Commentary:
  ;;; Code:

  ;; -*- lexical-binding: t; -*-
#+end_src

#+begin_src elisp :tangle ~/.emacs.d/custom.el
  ;;; custom.el --- Summary
  ;;; Commentary:
  ;;; Code:

  ;; -*- lexical-binding: t; -*-
#+end_src

* Garbage collector

Increase threshold to 500 MB to ease startup
#+begin_src elisp :tangle ~/.emacs
  (setq gc-cons-threshold (* 500 1024 1024))
#+end_src

Decrease threshold to 5 MB after init
#+begin_src elisp :tangle ~/.emacs
  (add-hook 'after-init-hook (lambda () (setq gc-cons-threshold (* 5 1024 1024))))
#+end_src

* Straight.el and use-package

Bootstrap Straight.el and install use-package
#+begin_src elisp :tangle ~/.emacs
  (setq straight-repository-branch "develop") ;; Using develop branch temporarily to fix the org-roam-dailies issue.

   (eval-and-compile
     (defvar bootstrap-version)
     (let ((bootstrap-file
            (expand-file-name "straight/repos/straight.el/bootstrap.el"
               		   (or (bound-and-true-p straight-base-dir)
               		       user-emacs-directory)))
           (bootstrap-version 7))
       (unless (file-exists-p bootstrap-file)
         (with-current-buffer
             (url-retrieve-synchronously  "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el" 'silent 'inhibit-cookies)
           (goto-char (point-max))
           (eval-print-last-sexp)))
       (load bootstrap-file nil 'nomessage))
     
     (straight-use-package 'use-package)
     )
#+end_src

Integrate use-package and straight
#+begin_src elisp :tangle ~/.emacs
  (setq straight-use-package-by-default t)
#+end_src

Make sure Org is installed (straight.el)
#+begin_src elisp :tangle ~/.emacs
  (unless (file-directory-p "~/.emacs.d/straight/versions") (make-directory (concat user-emacs-directory "straight/versions")))
#+end_src

#+begin_src elisp :tangle ~/.emacs
  (use-package org)
#+end_src

A use-package declaration for simplifying your .emacs
#+begin_src elisp
  (require 'use-package)
#+end_src

* Custom file

#+begin_src elisp
  (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
  (when (file-exists-p custom-file) (load custom-file))
#+end_src

* System information

#+begin_src elisp  :tangle ~/.emacs.d/custom.el
  (defvar my-laptop-p (equal (system-name) "lilitop"))
  (defvar my-server-p (and (equal (system-name) "localhost") (equal user-login-name "root")))
  (defvar my-phone-p (not (null (getenv "ANDROID_ROOT")))
    "If non-nil, GNU Emacs is running on Termux.")
  (when my-phone-p (defvar gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3"))
  (global-auto-revert-mode)  ; simplifies syncing
#+end_src

* Persistent history

#+begin_src elisp
  (savehist-mode)
#+end_src

* Backup and versioning

#+begin_src emacs-lisp
  (use-package magit
    :ensure t
    )
#+end_src

* Personal information

#+begin_src elisp :tangle ~/.emacs.d/custom.el
  (setq user-full-name "Amr Gharbeia")
  (defvar email-address "amr@gharbeia.net")
  (defvar calendar-latitude 39.0)
  (defvar calendar-longitude -77.1)
  (defvar calendar-location-name "Washington, DC")
  (defvar calendar-time-zone -300)
  (defvar calendar-standard-time-zone-name "EST")
  (defvar calendar-daylight-time-zone-name "EDT")
#+end_src

* Saving Emacs Sessions

Close frame when done
#+begin_src elisp
  (add-hook 'server-done-hook (lambda () (delete-frame)))
#+end_src

Save desktop session
#+begin_src elisp
  (desktop-save-mode t)
#+end_src

* Security

#+begin_src elisp :tangle no
  (use-package password-store)
#+end_src

#+begin_src elisp
  (use-package auth-source
    :config (auth-source-pass-enable)
    )
#+end_src

* End matter

#+begin_src elisp :tangle ~/.emacs
  (provide '.emacs)
  ;;; .emacs ends here
#+end_src

#+begin_src elisp
  (provide 'config)
  ;;; config.el ends here
#+end_src

#+begin_src elisp :tangle ~/.emacs.d/custom.el
  (provide 'custom)
  ;;; custom.el ends here
#+end_src
