fixed autotangle over tramp. The hacky barbarian way

This commit is contained in:
Ionut Adrian Ciolan 2025-06-17 18:37:22 +03:00
parent 42adda3899
commit aa8109cd5f

View File

@ -1,14 +1,37 @@
;;; iadrian-iadrian-org-auto-tangle.el --- Automatically and Asynchronously tangles org files on save -*- lexical-binding: t; -*-
;; Original Author: Yilkal Argaw <yilkalargawworkneh@gmail.com>
;; Original License: https://github.com/yilkalargaw/org-auto-tangle/blob/master/License.org
;; Maintainer: Ionut Adrian Ciolan <iadrian.ciolan@gmail.com>
;; URL: https://github.com/yilkalargaw/iadrian-org-auto-tangle
;; Version: 0.6.1
;; Keywords: outlines
;; Package-Requires: ((emacs "24.1") (async "1.9.3"))
;; This file is not part of GNU Emacs
;; Copyright (c) 2021, Yilkal Argaw
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; 1. Redistributions of source code must retain the above copyright notice, this
;; list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright notice,
;; this list of conditions and the following disclaimer in the documentation
;; and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;; Commentary:
;; It is common to want to tangle org files everytime you save your changes,
@ -21,6 +44,9 @@
;; - Add #+auto_tangle: t to your tangled org file
;; - Make changes to the emacs file and save your changes
;;; Code:
(require 'async)
(require 'cl-lib)
(require 'org)
@ -120,9 +146,15 @@ Assume buffer is in Org mode. Narrowing, if any, is ignored."
(let ((org-export-options-alist iadrian-org-auto-tangle-options-alist))
(org-export--parse-option-keyword auto-tangle)))
;; Fix async tangling failing over TRAMP
(defun iadrian-org-auto-tangle-async (file)
"Invoke `org-babel-tangle-file' asynchronously on FILE."
(message "Tangling %s..." (buffer-file-name))
"Tangle FILE asynchronously unless it's a TRAMP path (then fallback to sync)."
(if (file-remote-p file)
;; Use current buffer over TRAMP. Slower but safe
(with-current-buffer (get-file-buffer file)
(org-babel-tangle))
;; Normal async tangle locally
(message "[iadrian-org-auto-tangle] Tangling %s asynchronously..." (buffer-file-name))
(async-start
(let* ((buf-vars (plist-get (iadrian-org-auto-tangle--get-inbuffer-options)
:with-vars))
@ -138,17 +170,49 @@ Assume buffer is in Org mode. Narrowing, if any, is ignored."
org-babel-post-tangle-hook)
with-vars)))
(evaluate (not (member file iadrian-org-auto-tangle-babel-safelist))))
(lambda ()
`(lambda ()
(require 'org)
(let ((start-time (current-time))
(non-essential t)
(org-confirm-babel-evaluate evaluate))
(cl-progv (mapcar #'car preserved) (mapcar #'cdr preserved)
(org-babel-tangle-file file))
(org-confirm-babel-evaluate ,evaluate))
(cl-progv ',(mapcar #'car preserved) ',(mapcar #'cdr preserved)
(org-babel-tangle-file ,file))
(format "%.2f" (float-time (time-since start-time))))))
(let ((message-string (format "Tangling %S completed after" file)))
(lambda (tangle-time)
(message "%s %s seconds" message-string tangle-time)))))
(message "%s %s seconds" message-string tangle-time))))))
;(defun iadrian-org-auto-tangle-async (file)
; "Invoke `org-babel-tangle-file' asynchronously on FILE."
; (message "Tangling %s..." (buffer-file-name))
; (async-start
; (let* ((buf-vars (plist-get (iadrian-org-auto-tangle--get-inbuffer-options)
; :with-vars))
; (with-vars (if buf-vars
; (mapcar #'intern
; (org-uniquify (org-split-string
; (symbol-name buf-vars) ":")))
; iadrian-org-auto-tangle-with-vars))
; (preserved (mapcar (lambda (v)
; (cons v (symbol-value v)))
; (append '(org-src-preserve-indentation
; org-babel-pre-tangle-hook
; org-babel-post-tangle-hook)
; with-vars)))
; (evaluate (not (member file iadrian-org-auto-tangle-babel-safelist))))
; (lambda ()
; (require 'org)
; (let ((start-time (current-time))
; (non-essential t)
; (org-confirm-babel-evaluate evaluate))
; (cl-progv (mapcar #'car preserved) (mapcar #'cdr preserved)
; (org-babel-tangle-file file))
; (format "%.2f" (float-time (time-since start-time))))))
; (let ((message-string (format "Tangling %S completed after" file)))
; (lambda (tangle-time)
; (message "%s %s seconds" message-string tangle-time)))))
(defun iadrian-org-auto-tangle-tangle-if-needed ()
"Call iadrian-org-auto-tangle-async if needed.