branch: externals/org-transclusion
commit 962f116c4babc37934211282020b3231f474854b
Author: gggion <[email protected]>
Commit: gggion <[email protected]>

    refactor: ensure org-indent properties exist before adding fringes
    
    Modify org-transclusion-indent-mode extension to explicitly refresh
    org-indent properties in transcluded region before main package adds
    fringes. This prevents race condition where fringes might be added
    before org-indent's after-change hook runs.
    
    * org-transclusion-indent-mode.el
    (org-transclusion-indent--add-properties-and-fringes): New function.
    Calls org-indent-add-properties to ensure line-prefix and wrap-prefix
    exist, then re-applies fringes that may have been overwritten.
    Registered with org-transclusion-after-add-functions.
    
    * org-transclusion-indent-mode.el
    (org-transclusion-indent--refresh-source-region): Keep existing
    function for after-remove cleanup.
    
    The main package adds fringes in org-transclusion-content-insert, but if
    org-indent-mode's after-change hook hasn't run yet, those fringes are
    added to non-existent properties. Now it ensures org-indent runs first,
    then re-applies fringes to the correct properties.
---
 org-transclusion-indent-mode.el | 86 ++++++++++++++---------------------------
 1 file changed, 30 insertions(+), 56 deletions(-)

diff --git a/org-transclusion-indent-mode.el b/org-transclusion-indent-mode.el
index 2b3ba26a48..ccf2a099d7 100644
--- a/org-transclusion-indent-mode.el
+++ b/org-transclusion-indent-mode.el
@@ -22,70 +22,44 @@
 ;;; Commentary:
 ;;  This file is part of Org-transclusion
 ;;  URL: https://github.com/nobiot/org-transclusion
+;;  
+;;  This extension ensures org-indent-mode properties are correctly
+;;  applied to transcluded content and refreshed after transclusion
+;;  removal.
 
 ;;; Code:
 
 (require 'org-indent)
-(declare-function org-transclusion-within-transclusion-p
-                  "org-transclusion")
+(declare-function org-transclusion-add-fringe-to-region "org-transclusion")
 
-(add-hook 'org-transclusion-after-add-functions
-          #'org-translusion-indent-add-properties)
+(defun org-transclusion-indent--add-properties-and-fringes (beg end)
+  "Ensure org-indent properties exist in transcluded region, then re-add 
fringes.
+BEG and END are the transcluded region bounds.
 
-(defun org-translusion-indent-add-properties (beg end)
-  "BEG END."
+The main package adds fringes during content insertion, but org-indent's
+after-change hook may not have run yet. This function ensures org-indent
+properties are set, then re-applies fringes that may have been overwritten."
   (when org-indent-mode
-    (advice-add #'org-indent-set-line-properties
-                :override
-                #'org-transclusion-indent-set-line-properties-ad)
+    ;; Ensure org-indent properties exist
     (org-indent-add-properties beg end)
-    (advice-remove #'org-indent-set-line-properties
-                   #'org-transclusion-indent-set-line-properties-ad)))
-
-(defun org-transclusion-indent-set-line-properties-ad (level indentation 
&optional heading)
-  "Set prefix properties on current line an move to next one.
-
-LEVEL is the current level of heading.  INDENTATION is the
-expected indentation when wrapping line.
-
-When optional argument HEADING is non-nil, assume line is at
-a heading.  Moreover, if it is `inlinetask', the first star will
-have `org-warning' face."
-
-  (let* ((line (aref (pcase heading
-                       (`nil org-indent--text-line-prefixes)
-                       (`inlinetask org-indent--inlinetask-line-prefixes)
-                       (_ org-indent--heading-line-prefixes))
-                     level))
-         (wrap
-          (org-add-props
-              (concat line
-                      (if heading (concat (make-string level ?*) " ")
-                        (make-string indentation ?\s)))
-              nil 'face 'org-indent)))
-
-    ;; Org-transclusion's addition begin
-    (when (org-transclusion-within-transclusion-p)
-      (setq line
-            (concat line
-                    (propertize
-                     "x"
-                     'display
-                     '(left-fringe org-transclusion-fringe-bitmap
-                                   org-transclusion-fringe))))
-      (setq wrap
-            (concat line
-                    (propertize
-                     "x"
-                     'display
-                     '(left-fringe org-transclusion-fringe-bitmap
-                                   org-transclusion-fringe)))))
-    ;; Org-transclusion's addition end
-
-    ;; Add properties down to the next line to indent empty lines.
-    (add-text-properties (line-beginning-position) (line-beginning-position 2)
-                         `(line-prefix ,line wrap-prefix ,wrap)))
-  (forward-line))
+    ;; Re-apply fringes (org-indent-add-properties may have overwritten them)
+    (org-transclusion-add-fringe-to-region
+     (current-buffer) beg end 'org-transclusion-fringe)))
+
+(defun org-transclusion-indent--refresh-source-region (src-buf src-beg src-end)
+  "Refresh org-indent properties in source region after transclusion removal.
+SRC-BUF is the source buffer, SRC-BEG and SRC-END are the region bounds.
+This ensures visual indentation updates immediately when org-indent-mode
+is active."
+  (when (buffer-local-value 'org-indent-mode src-buf)
+    (with-current-buffer src-buf
+      (org-indent-add-properties src-beg src-end))))
+
+;; Register hooks when extension loads
+(add-hook 'org-transclusion-after-add-functions
+          #'org-transclusion-indent--add-properties-and-fringes)
+(add-hook 'org-transclusion-after-remove-functions
+          #'org-transclusion-indent--refresh-source-region)
 
 (provide 'org-transclusion-indent-mode)
 

Reply via email to