Clean up emacs init

Signed-off-by: magic_rb <magic_rb@redalder.org>
This commit is contained in:
magic_rb 2024-10-27 11:50:59 +01:00
parent f1658a7a56
commit 4f49a7092a
No known key found for this signature in database
GPG key ID: 08D5287CC5DDCA0E

View file

@ -1,8 +1,7 @@
; SPDX-FileCopyrightText: 2022 Richard Brežák <richard@brezak.sk>
;
; SPDX-License-Identifier: LGPL-3.0-or-later
;;; package --- Summary
;; init.el --- Initialization -*- lexical-binding: t -*-
;; SPDX-FileCopyrightText: 2022 Richard Brežák <richard@brezak.sk>
;;
;; SPDX-License-Identifier: LGPL-3.0-or-later
;;; Commentary:
@ -59,45 +58,6 @@
(use-package org
:straight t)
(defun magic_rb/org-transclusion-babel-load (&optional narrowed)
"Call `org-babel-load-file` on all transcludes in the current file."
(interactive "P")
(save-restriction
(let ((marker (move-marker (make-marker) (point)))
(load-link (lambda ()
(let* ((keyword-plist (org-transclusion-keyword-string-to-plist))
(link (org-transclusion-wrap-path-to-link
(plist-get keyword-plist :link)))
(link-raw (org-element-property :raw-link link))
(link-type (org-element-property :type link)))
(when (string-equal link-type "id")
(message "RB Loading: %s" (org-id-find-id-file link-raw))
(org-babel-load-file (org-id-find-id-file link-raw)))))))
(unless narrowed (widen))
(goto-char (point-min))
;; Handle inactive transclusions
(let ((regexp "^[ \t]*#\\+TRANSCLUDE:"))
(while (re-search-forward regexp nil t)
;; Don't transclude if within a transclusion to avoid infinite
;; recursion
(unless (or (org-transclusion-within-transclusion-p)
(plist-get (org-transclusion-keyword-string-to-plist)
:disable-auto))
(funcall load-link))))
;; Handle active transclusions
(while (setq match (text-property-search-forward 'org-transclusion-type))
(goto-char (prop-match-beginning match))
(org-transclusion-remove)
(funcall load-link)
(org-transclusion-add))
(goto-char marker)
(move-marker marker nil) ; point nowhere for GC
t)))
(require 'cl-lib)
(use-package org-roam
@ -120,6 +80,67 @@
: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"))
@ -132,7 +153,7 @@
(delete-file file)))
(mapc #'magic_rb/delete-file-maybe magic_rb/org-el-init-files)
(mapc #'org-babel-load-file magic_rb/org-init-files)
(mapc #'magic_rb/load-org-file magic_rb/org-init-files)
(provide '.emacs)