Ruijie Yu <rui...@netyu.xyz>  writes:

> Great idea!  Some inline comments below.
>
> You only need to say #'expand-file-name instead of the quoted lambda.
> Also, you need to set the variable, otherwise the variable
> `org-babel-tangle-sync-files' is undefined.
>
> What I have in mind is this:
>    :set (lambda (var val) (set var (mapcar #'expand-file-name val)))
>
> Here you don't have to use `progn' because it is implied from 
> `save-excursion'.
>

Thanks! I've made your changes, and I've also incorporated John
Wiegley's comments about using "import" instead of "pull" as a
tangle-sync action word (small diff attached).

I've also written up my changes in the ~etc/ORG-NEWS~ and
targeted my custom variable for the 9.7 release (diff attached).

> Is there possibility to have a local minor mode (without introducing too
> much code changes)?

I initially tried it this way, but the problem is that an org
source block buffer might be in sync-mode, but it's
corresponding tangle file might not be, making any changes
asymmetric.

Another issue is in order to see the changes in the tangled file,
the tangle buffer needs to be reverted (with user prompt) which
then switches off the sync-mode for that buffer on reload.

One way around this (and it's something I implemented 3 years ago in
my messy org-tanglesync[0] MELPA code) is to set an explicit list of
"sync files", and then for Emacs to parse every =:tangle= header in a
given file when loaded (via =org-src-mode-hook=) to create an alist of
config files and their associated tangled files[1], such as
=((file1.conf . (tanglefile1.txt tanglefile2.txt etc)))=. Then, for
example, when ~tanglefile1.txt~ is loaded, Emacs knows that it should
load the sync-mode too.

This approach works reasonably well when the "sync files" list is
mandatory, but it's also prone to errors if a sync file is edited and
the alist of config files isn't updated, and the user would also lose
the flexibility of having ~ob-tangle-sync~ function everywhere.

I think a global minor mode is really elegant in this regard and I
wish I knew about it 3 years ago!

Best,
Mehmet


0: https://gitlab.com/mtekman/org-tanglesync.el
1: 
https://gitlab.com/mtekman/org-tanglesync.el/-/blob/master/org-tanglesync.el#L400-L410
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 03894f128..29f60c755 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -207,12 +207,35 @@ selection.
 TODO state, priority, tags, statistics cookies, and COMMENT keywords
 are allowed in the tree structure.
 
-*** Asynchronous code evaluatation in ~ob-shell~
+*** Asynchronous code evaluation in ~ob-shell~
 
 Running shell blocks with the ~:session~ header freezes Emacs until
 execution completes.  The new ~:async~ header allows users to continue
 editing with Emacs while a ~:session~ block executes.
 
+*** Automatic sync of source blocks and tangled blocks in ~ob-tangle-sync~
+
+Invoking minor mode =org-babel-tangle-sync-mode= synchronizes contents
+between a currently visited tangled file its org-mode source block
+(and vice versa) via the =after-save-hook=.
+
+Desired tangling actions can be assymetric depending on whether the
+org-mode source block header argument =:tangle-sync <action>= has an
+action of:
+
+- =skip= :: do nothing, just save the buffer, even if the sync mode is
+            active
+
+- =import= :: only pull changes from the tangled block into the
+              org-mode source block (even when visited from either)
+
+- =export= :: only pull changes from the org-mode source block into
+              the tangled block (even when visited from either)
+
+- =both= (or nil) :: freely sync changes of current buffer to
+                     associated source or target
+
+
 ** Miscellaneous
 *** Blank lines after removed objects are not retained during export
 
diff --git a/lisp/ob-tangle-sync.el b/lisp/ob-tangle-sync.el
index 61c23f647..cfa6abdd2 100644
--- a/lisp/ob-tangle-sync.el
+++ b/lisp/ob-tangle-sync.el
@@ -44,7 +44,6 @@
 (define-minor-mode org-babel-tangle-sync-mode
   "Global minor mode that synchronizes tangled files after every save."
   :global t
-  :interactive t
   :lighter " o-ts"
   (if org-babel-tangle-sync-mode
       (add-hook 'after-save-hook 'org-babel-tangle-sync-synchronize nil t)
@@ -57,8 +56,8 @@ here are subject to the org-babel-tangle-sync treatment.  If nil,
 then all org files with tangle headers are considered."
   :group 'org-babel-tangle-sync
   :type 'list
-  :package-version '(Org . "9.6.5")
-  :set (lambda (_var val) (mapcar #'(lambda (x) (expand-file-name x)) val)))
+  :package-version '(Org . "9.7")
+  :set (lambda (var val) (set var (mapcar #'expand-file-name val))))
 
 
 (defun org-babel-tangle-sync--babel-tangle-jump (link block-name)
@@ -95,15 +94,15 @@ If the cursor is either within the source file or in destination
 tangled file, perform a desired tangling action.  The tangling
 action by default is to detangle the tangled files' changes back
 to its source block, or to tangle the source block to its tangled
-file.  Actions are one of `skip' (no action), `pull' (detangle
+file.  Actions are one of `skip' (no action), `import' (detangle
 only), `export' (tangle only), and `both' (default, synchronize
 in both directions).  All `org-mode' source blocks and all tangled
 files with comments are considered valid targets, unless
 specified otherwise by `org-babel-tangle-sync-files'."
   (interactive)
   (let* ((link (save-excursion
-                 (progn (re-search-backward org-link-bracket-re nil t)
-		        (match-string-no-properties 0))))
+                 (re-search-backward org-link-bracket-re nil t)
+		         (match-string-no-properties 0)))
          (block-name (match-string 2))
          (orgfile-p (string= major-mode "org-mode"))
          (tangled-file-p (and link (not orgfile-p))))
@@ -142,12 +141,12 @@ specified otherwise by `org-babel-tangle-sync-files'."
                        (org-babel-detangle)
                        (message "Synced to %s" source-file))))))
 
-      ;; Source Block → Tangled File (or Source Block ← Tangled File (via "pull"))
+      ;; Source Block → Tangled File (or Source Block ← Tangled File (via "import"))
       (when orgfile-p
         ;; Tangle action of Source file on Block if:
         ;; - member of sync file list (or list is empty)
         ;; Actions
-        ;; - pull (Source Block ← File)
+        ;; - import (Source Block ← File)
         ;; - skip (nothing)
         ;; - export, both, nil (Source Block → File)
         (if (or (null org-babel-tangle-sync-files)
@@ -157,7 +156,7 @@ specified otherwise by `org-babel-tangle-sync-files'."
                    (tangle-file (cdr (assq :tangle src-headers)))
                    (tangle-action (alist-get :tangle-sync src-headers)))
               (when tangle-file
-                (cond ((string= tangle-action "pull") (save-excursion
+                (cond ((string= tangle-action "import") (save-excursion
                                                         (org-babel-detangle tangle-file)))
                       ((string= tangle-action "skip") nil)
                       (t (let ((current-prefix-arg '(16)))

Reply via email to