dotfiles/home-manager/modules/emacs/.emacs
magic_rb 4f49a7092a
Clean up emacs init
Signed-off-by: magic_rb <magic_rb@redalder.org>
2024-10-27 11:50:59 +01:00

161 lines
5.1 KiB
Plaintext
Executable file

;; init.el --- Initialization -*- lexical-binding: t -*-
;; SPDX-FileCopyrightText: 2022 Richard Brežák <richard@brezak.sk>
;;
;; SPDX-License-Identifier: LGPL-3.0-or-later
;;; Commentary:
;;; Code:
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(auth-source-save-behavior nil)
'(custom-enabled-themes '())
'(custom-safe-themes
'("2f1518e906a8b60fac943d02ad415f1d8b3933a5a7f75e307e6e9a26ef5bf570" default))
'(package-selected-packages
'()))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(setq backup-directory-alist `(("." . "~/.emacs.d/saves")))
(setq vc-follow-symlinks t)
(add-to-list 'load-path "~/.emacs.d/lisp/")
(add-to-list 'load-path "~/.emacs.d/lisp/org-task-dump")
(add-to-list 'load-path "~/.emacs.d/vterm-module/lib")
(add-to-list 'exec-path (concat (expand-file-name "~") "/.emacs.d/profile/bin"))
(setenv "PATH" (concat (expand-file-name "~") "/.emacs.d/profile/bin:" (getenv "PATH")))
(straight-use-package 'use-package)
(straight-thaw-versions)
;; load general early for now
(use-package general
:straight t)
(use-package org
:straight t)
(require 'cl-lib)
(use-package org-roam
:straight t
:demand t
:init
(setq org-roam-v2-ack t
org-roam-directory "~/roam"))
(defvar magic_rb/org-init-files
(cl-concatenate
'list
(directory-files "~/.emacs.d/org" t "\\.org$")
(seq-map #'car
(org-roam-db-query
[:select [nodes:file]
:from tags
:left-join nodes
:on (= tags:node-id nodes:id)
:where (like tag (quote "%\"emacs-load\""))])))
"List of org files, which should be tangled and loaded.")
(defvar magic_rb/org-el-cache-dir
(concat user-emacs-directory ".cache/org-el/")
"Directory to cache org babel el builds.")
(defun magic_rb/hash-file (hash file-path)
"Hash a file at FILE-PATH using HASH algorithm."
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally file-path)
(secure-hash hash (current-buffer))))
(defun magic_rb/hash-string (hash str)
"Hash STR using HASH algorithm."
(with-temp-buffer
(set-buffer-multibyte nil)
(insert str)
(secure-hash hash (current-buffer))))
(defun magic_rb/org-el-cache-path-hash (file-path)
"Calculate the path hash based on FILE-PATH."
(magic_rb/hash-string 'sha256 (file-name-nondirectory file-path)))
(defun magic_rb/org-el-cache-file (file-path)
"Get the path to the cache file for a FILE-PATH."
(concat magic_rb/org-el-cache-dir (magic_rb/org-el-cache-path-hash file-path)))
(defun magic_rb/load-org-file (file-path)
"Load a Org file at FILE-PATH optionally cached if it didn't change."
(if (magic_rb/org-file-changed-p file-path)
(progn
(org-babel-load-file file-path)
(magic_rb/cache-org-file file-path))
(load-file (file-name-with-extension file-path ".el"))))
(defun magic_rb/cache-org-file (file-path)
"Cache a file at FILE-PATH as tangled."
(let* ((path-hash (magic_rb/org-el-cache-file file-path))
(file-hash (magic_rb/hash-file 'sha256 file-path)))
(make-directory (file-name-directory path-hash) t)
(with-temp-file path-hash
(insert file-hash))))
(defun magic_rb/org-file-changed-p (file-path)
"Chech if a particular file at FILE-PATH has changed.
Paths with the same non-directory component are considered to be the
same file."
(if (not (file-exists-p file-path))
(error "File should exist"))
(let* ((path-hash (magic_rb/org-el-cache-file file-path))
(file-hash (magic_rb/hash-file 'sha256 file-path))
(stored-file-hash
(if (file-exists-p path-hash)
(with-temp-buffer
(insert-file-contents path-hash)
(buffer-string))
nil)))
(or
(not (file-exists-p (file-name-with-extension file-path ".el")))
(not stored-file-hash)
(string-equal file-hash stored-file-hash))))
(defvar magic_rb/org-el-init-files
(cl-map 'list
(lambda (file) (concat (file-name-sans-extension file) ".el"))
magic_rb/org-init-files)
"List of generated elisp files from magic_rb/org-init-files.")
(defun magic_rb/delete-file-maybe (file)
"If FILE exists, delete it."
(when (file-exists-p file)
(delete-file file)))
(mapc #'magic_rb/delete-file-maybe magic_rb/org-el-init-files)
(mapc #'magic_rb/load-org-file magic_rb/org-init-files)
(provide '.emacs)
;;; .emacs ends here