emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/org 17096b3f4c 2/3: Refactor `org-src--contents-for-wri


From: ELPA Syncer
Subject: [elpa] externals/org 17096b3f4c 2/3: Refactor `org-src--contents-for-write-back'
Date: Thu, 13 Jul 2023 12:58:41 -0400 (EDT)

branch: externals/org
commit 17096b3f4c6e41d7d1ad82e1d1834d7d29365bb6
Author: Ihor Radchenko <yantar92@posteo.net>
Commit: Ihor Radchenko <yantar92@posteo.net>

    Refactor `org-src--contents-for-write-back'
    
    * lisp/org-src.el (org-src--contents-for-write-back-1): New function
    that is passed indentation parameters and contents explicitly, without
    relying on buffer-local variables in current buffer.  The function
    extracts variable-independent code and removes unused MARKER.
    (org-src--contents-for-write-back): Use the new function, passing the
    parameters according to buffer-local values in src edit buffer.
---
 lisp/org-src.el | 87 +++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 53 insertions(+), 34 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index ade847e8a7..866ff2dbff 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -469,43 +469,42 @@ When NODE is not passed, assume element at point."
          (or (org-element-property :preserve-indent node)
             org-src-preserve-indentation))))
 
-(defun org-src--contents-for-write-back (write-back-buf)
-  "Populate WRITE-BACK-BUF with contents in the appropriate format.
-Assume point is in the corresponding edit buffer."
-  (let ((indentation-offset
-        (if org-src--preserve-indentation 0
-          (+ (or org-src--block-indentation 0)
-             (if (memq org-src--source-type '(example-block src-block))
-                 org-src--content-indentation
-               0))))
-       (use-tabs? (and (> org-src--tab-width 0) t))
-        (preserve-fl (eq org-src--source-type 'latex-fragment))
-       (source-tab-width org-src--tab-width)
-       (contents (org-with-wide-buffer
-                   (let ((eol (line-end-position)))
-                     (list (buffer-substring (point-min) eol)
-                           (buffer-substring eol (point-max))))))
-       (write-back org-src--allow-write-back)
-        marker indent-str)
-    ;; Compute the exact sequence of tabs and spaces used to indent up
-    ;; to `indentation-offset' in the Org buffer.
-    (setq indent-str
-          (with-temp-buffer
-            ;; Reproduce indentation parameters from org buffer.
-            (setq indent-tabs-mode use-tabs?)
-            (when (> source-tab-width 0) (setq tab-width source-tab-width))
-            (indent-to indentation-offset)
-            (buffer-string)))
+(defun org-src--contents-for-write-back-1
+    ( write-back-buf contents
+      &optional indentation-offset preserve-fl source-tab-width write-back)
+  "Populate WRITE-BACK-BUF with CONTENTS in the appropriate format.
+
+INDENTATION-OFFSET, when non-nil is additional indentation to be applied
+to all the lines.  PRESERVE-FL means that first line should not be
+indented (useful for inline blocks contents that belong to paragraph).
+The original indentation, if any, is not altered.
+
+TAB-WIDTH is `tab-width' to be used when indenting.  The value of 0
+means that tabs should not be used.
+
+WRITE-BACK, when non-nil, is a function to be called with point at
+WRITE-BACK-BUF after inserting the original contents, but before
+applying extra indentation."
+  (let ((use-tabs? (and (> source-tab-width 0) t))
+        indent-str)
     (with-current-buffer write-back-buf
       ;; Apply WRITE-BACK function on edit buffer contents.
-      (insert (org-no-properties (car contents)))
-      (setq marker (point-marker))
-      (insert (org-no-properties (car (cdr contents))))
+      (insert (org-no-properties contents))
       (goto-char (point-min))
       (when (functionp write-back) (save-excursion (funcall write-back)))
       ;; Add INDENTATION-OFFSET to every line in buffer,
       ;; unless indentation is meant to be preserved.
-      (when (> indentation-offset 0)
+      (when (and indentation-offset (> indentation-offset 0))
+        ;; The exact sequence of tabs and spaces used to indent
+        ;; up to `indentation-offset' in the Org buffer.
+        (setq indent-str
+              (with-temp-buffer
+                ;; Reproduce indentation parameters.
+                (setq indent-tabs-mode use-tabs?)
+                (when (> source-tab-width 0)
+                  (setq tab-width source-tab-width))
+                (indent-to indentation-offset)
+                (buffer-string)))
         ;; LaTeX-fragments are inline. Do not add indentation to their
         ;; first line.
         (when preserve-fl (forward-line))
@@ -513,9 +512,29 @@ Assume point is in the corresponding edit buffer."
           ;; Keep empty src lines empty, even when src block is
           ;; indented on Org side.
           ;; See 
https://list.orgmode.org/725763.1632663635@apollo2.minshall.org/T/
-          (when (not (eolp)) (insert indent-str)) ; not an empty line
-         (forward-line)))
-      (set-marker marker nil))))
+          (when (not (eolp)) ; not an empty line
+            (insert indent-str))
+         (forward-line))))))
+
+(defun org-src--contents-for-write-back (write-back-buf)
+  "Populate WRITE-BACK-BUF with contents in the appropriate format.
+Assume point is in the corresponding edit buffer."
+  (org-src--contents-for-write-back-1
+   write-back-buf
+   ;; CONTENTS
+   (org-with-wide-buffer (buffer-string))
+   ;; INDENTATION
+   (if org-src--preserve-indentation 0
+     (+ (or org-src--block-indentation 0)
+       (if (memq org-src--source-type '(example-block src-block))
+           org-src--content-indentation
+         0)))
+   ;; PRESERVE-FL
+   (eq org-src--source-type 'latex-fragment)
+   ;; TAB-WIDTH
+   org-src--tab-width
+   ;; WRITE-BACK
+   org-src--allow-write-back))
 
 (defun org-src--edit-element
     (datum name &optional initialize write-back contents remote)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]