FG42/nix/modules/editor/lisp/fg42.el

167 lines
5.6 KiB
EmacsLisp

;;; FG42 --- The mighty editor for the emacsians -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2024 Sameer Rahmani <lxsameer@gnu.org>
;;
;; Author: Sameer Rahmani <lxsameer@gnu.org>
;; URL: https://devheroes.codes/FG42/FG42
;; Version: 4.0.0
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;; Code:
(require 'fg42/utils)
(eval-when-compile
(defvar package-archives)
(defvar use-package-ensure-function))
;; ============================================================================
;; Basic inital configuration for FG42's operations
;; ============================================================================
(defgroup fg42 nil
"Customize your FG42 instance via this group of configurations."
:group :editor)
(defvar fg42/disabled-features nil
"List of features to disable.")
(defvar fg42/after-init-hook nil
"The hook tha runs when FG42 finished running the user configuration.")
(defvar fg42/debug? nil
"It is t If FG42 is running in debug mode and nil otherwise.")
(defvar fg42/-gc-cons-threshold 16777216
"Value of GC threshold of FG42.")
(defvar fg42/initialized nil
"A variable that indicates whether FG42 is passed initialization.")
(defconst fg42/cache-dir (or (getenv "FG42_CACHE_DIR")
(path-join (or (getenv "XDG_CACHE_HOME") "~/.cache")
"fg42")))
(defconst fg42/config-dir (or (getenv "FG42_CONFIG_DIR")
(path-join (or (getenv "XDG_CONFIG_HOME") "~/.config")
"fg42")))
(defun defer-garbage-collection ()
"Disable garbage collection."
(setq gc-cons-threshold fg42/-gc-cons-threshold))
(defun restore-garbage-collection ()
"Restore garbage collection to it's default value."
(run-at-time
1 nil (lambda () (setq gc-cons-threshold most-positive-fixnum))))
(defun fg42/-startup-optimization ()
"Optimized FG42 startup."
;; by setting gc threshold to the largest number
;; Emacs can understand we are basically disabling it :).
(setq gc-cons-threshold most-positive-fixnum
gc-cons-percentage 0.6)
;; after initilization phase restore cons threshold to normal
(add-hook 'emacs-startup-hook
(lambda ()
(setq gc-cons-threshold fg42/-gc-cons-threshold ; 16mb
gc-cons-percentage 0.1)))
;; disable gc when we are in minibuffer using the same method we
;; use for initilization time
(add-hook 'minibuffer-setup-hook #'defer-garbage-collection)
;; just enable gc when exiting minibuffer
(add-hook 'minibuffer-exit-hook #'restore-garbage-collection)
;; we dont need Emacs to check every file type and look for file
;; handlers when we are initializing so we backup the original
;; value and set it to nil
(setq --file-name-handler-alist file-name-handler-alist)
(setq file-name-handler-alist nil)
;; after initialization we can restore that file-name-handler-alist
;; to original value.
(add-hook 'emacs-startup-hook
(lambda ()
(setq file-name-handler-alist
--file-name-handler-alist)))
;; initial mode for emacs can be fundamental mode we have nothing to lose
(setq initial-major-mode 'fundamental-mode))
(defun fg42/initialize ()
"Initialize FG42 after the Emacs window is rendered by loading the user init file."
;; (fg42/-startup-optimization)
(require 'fg42/editor)
(when (file-exists-p user-init-file)
(load user-init-file))
(fg42/setup-editor)
;; ;; If user didn't select a theme, load the default stuff
;; (when (not (featurep 'fg42/themes))
;; (require 'fg42/themes))
(add-hook 'emacs-startup-hook
(lambda ()
(run-hooks 'fg42/after-init-hook)
(run-hooks 'fg42/ui-hook)
(setq fg42/initialized t))))
(defun fg42/main ()
"The entry point of FG42."
(when (string= (getenv "FG42_DEBUG") "1")
(setq debug-on-error t
fg42/debug? t))
;; We don't use any Emacs package manager, so
;; prevent package.el to install anything at startup
(setq package-enable-at-startup nil
package-archives nil
use-package-ensure-function 'ignore
;; Setup the path for the custom.el file, We don't want
;; to touch user's Emacs configuration.
custom-file (path-join fg42/config-dir "custom.el")
;; Change the config dir to `~/.config/fg42/' by default
;; again, we don't want to temper with user's Emacs
user-emacs-directory fg42/config-dir
user-init-file
(or (getenv "FG42_CONFIG_FILE")
(path-join fg42/config-dir "fg42.el")))
;; Change the eln file cache to leave Emacs alone.
(startup-redirect-eln-cache
(or (getenv "FG42_ELN_DIR")
(path-join fg42/cache-dir "fg42.el")))
;; Load the customization file. In FG42 it is different than
;; the default `user-init-file'
(if (file-exists-p custom-file)
(load custom-file))
;; From point forward we can use normal Elisp stuff without
;; interfering with user's Emacs configuration
(fg42/initialize))
(fg42/main)
(provide 'fg42)
;;; fg42.el ends here