From fc8433445a7abf4aca56054a9d78cc5ee6b1db52 Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 21 Apr 2021 10:28:26 +0200 Subject: [PATCH] added tangle function --- config.org | 67 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/config.org b/config.org index e3cba56..5c9a215 100644 --- a/config.org +++ b/config.org @@ -54,16 +54,63 @@ When pulling the repository the first time, an initial init.el needs to be setup This function updates init.el whenever changes in init.org are made. The update will be active after saving. #+BEGIN_SRC emacs-lisp -(defun me/tangle-init () - "If the current buffer is 'init.org', the code blocks are tangled, and the tangled file is compiled." - (when (equal (buffer-file-name) - (expand-file-name (concat user-emacs-directory "init.org"))) - ;; avoid running hooks - (let ((prog-mode-hook nil)) - (org-babel-tangle) - (byte-compile-file (concat user-emacs-directory "init.el")) - (load-file user-init-file)))) -(add-hook 'after-save-hook 'me/tangle-init) +;(defun me/tangle-init () +; "If the current buffer is 'init.org', the code blocks are tangled, and the tangled file is compiled." +; (when (equal (buffer-file-name) +; (expand-file-name (concat user-emacs-directory "init.org"))) +; ;; avoid running hooks +; (let ((prog-mode-hook nil)) +; (org-babel-tangle) +; (byte-compile-file (concat user-emacs-directory "init.el")) +; (load-file user-init-file)))) +;(add-hook 'after-save-hook 'me/tangle-init) + +(defun me/tangle-config () + "Export code blocks from the literate config file +asynchronously." + (interactive) + ;; prevent emacs from killing until tangle-process finished + (add-to-list 'kill-emacs-query-functions + (lambda () + (or (not (process-live-p (get-process "tangle-process"))) + (y-or-n-p "\"me/tangle-config\" is running; kill it? ")))) + ;; tangle config asynchronously + (me/async-process + (format "emacs %s --batch --eval '(org-babel-tangle nil \"%s\")'" config-org config-el) + "tangle-process")) + +(add-hook 'org-mode-hook + (lambda () + (if (equal (buffer-file-name) config-org) + (me/add-local-hook 'after-save-hook 'me/tangle-config)))) + +(defun me/add-local-hook (hook function) + "Add buffer-local hook." + (add-hook hook function :local t)) + +(defun me/async-process (command &optional name filter) + "Start an async process by running the COMMAND string with bash. Return the +process object for it. + +NAME is name for the process. Default is \"async-process\". + +FILTER is function that runs after the process is finished, its args should be +\"(process output)\". Default is just messages the output." + (make-process + :command `("bash" "-c" ,command) + :name (if name name + "async-process") + :filter (if filter filter + (lambda (process output) (message (s-trim output)))))) + +;; Examples: +;; +;; (me/async-process "ls") +;; +;; (me/async-process "ls" "my ls process" +;; (lambda (process output) (message "Output:\n\n%s" output))) +;; +;; (me/async-process "unknown command") #+END_SRC A small function to measure start up time.