Hi Ihor,

Good point, thanks. I moved the helper to org-macs.el and reused it in
the matching places in org-table.el. I also added a test.

What do you think about the name? I used `org-ensure-and-move-marker' to
indicate that it creates a marker when passed nil, but I am wondering
whether the simpler org-move-marker would be preferable.

Best,
-- 
Slawomir Grochowski
>From 7c9f8d34b21d3795e97bcbb40f875352386acb33 Mon Sep 17 00:00:00 2001
From: Slawomir Grochowski <[email protected]>
Date: Sat, 13 Jun 2026 08:20:30 +0200
Subject: [PATCH] ; org: Move marker helper to org-macs

* lisp/org-macs.el (org-ensure-and-move-marker): New function.
* lisp/org-colview.el (org-columns--ensure-and-move-marker): Remove.
(org-columns-goto-top-level, org-columns--prepare-rows)
(org-agenda-columns): Use `org-ensure-and-move-marker'.
* lisp/org-table.el (org-table-recalculate, org-table-analyze): Use it.
* testing/lisp/test-org-macs.el (test-org/ensure-and-move-marker): Test it.

Refactoring: Move Function, Replace Inline Code with Function Call.

No behavior change.
---
 lisp/org-colview.el           | 12 +++---------
 lisp/org-macs.el              |  6 ++++++
 lisp/org-table.el             | 12 +++++-------
 testing/lisp/test-org-macs.el | 11 +++++++++++
 4 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/lisp/org-colview.el b/lisp/org-colview.el
index 92556d0c7..3cfb12e8f 100644
--- a/lisp/org-colview.el
+++ b/lisp/org-colview.el
@@ -978,18 +978,12 @@ back to the next source, ultimately to
     (org-columns-compile-format selected-columns-format)
     selected-columns-format))
 
-(defun org-columns--ensure-and-move-marker (marker &optional position)
-  "Return MARKER moved to POSITION in the current buffer.
-When MARKER is nil, create a new marker first.  POSITION defaults
-to point."
-  (move-marker (or marker (make-marker)) (or position (point))))
-
 (defun org-columns-goto-top-level ()
   "Move to the beginning of the column view area.
 Also sets `org-columns-top-level-marker' to the new position."
   (goto-char
    (setq org-columns-top-level-marker
-	 (org-columns--ensure-and-move-marker
+	 (org-ensure-and-move-marker
 	  org-columns-top-level-marker
 	  (cond ((org-before-first-heading-p) (point-min))
 		((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from)
@@ -1013,7 +1007,7 @@ COLUMNS-FORMAT is non-nil, use it instead of the format selected from
 the buffer."
   (when global (goto-char (point-min)))
   (setq org-columns-begin-marker
-	(org-columns--ensure-and-move-marker org-columns-begin-marker))
+	(org-ensure-and-move-marker org-columns-begin-marker))
   (org-columns-goto-top-level)
   (org-columns-get-format columns-format)
   (unless org-columns-inhibit-recalculation (org-columns-compute-all))
@@ -1960,7 +1954,7 @@ definition."
   (interactive nil org-agenda-mode)
   (org-columns-remove-overlays)
   (setq org-columns-begin-marker
-	(org-columns--ensure-and-move-marker org-columns-begin-marker))
+	(org-ensure-and-move-marker org-columns-begin-marker))
   (let* ((org-columns--time (float-time))
 	 (org-done-keywords org-done-keywords-for-agenda)
 	 (columns-format
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 2e482bb14..6705b95d1 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -743,6 +743,12 @@ Optional argument REGEXP selects variables to clone."
 
 ;;; Miscellaneous
 
+(defun org-ensure-and-move-marker (marker &optional position)
+  "Move MARKER to POSITION in the current buffer and return it.
+When MARKER is nil, create a new marker first.  POSITION defaults
+to point."
+  (move-marker (or marker (make-marker)) (or position (point))))
+
 (defsubst org-call-with-arg (command arg)
   "Call COMMAND interactively, but pretend prefix arg was ARG."
   (let ((current-prefix-arg arg)) (call-interactively command)))
diff --git a/lisp/org-table.el b/lisp/org-table.el
index c0e2c140c..a5a9cb5d5 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -3052,10 +3052,9 @@ existing formula for column %s"
 		          (org-table-message-once-per-second
 		           log-last-time
 		           "Re-applying formulas to full table...(line %d)" cnt)))
-	          (if (markerp org-last-recalc-line)
-		      (move-marker org-last-recalc-line (line-beginning-position))
-		    (setq org-last-recalc-line
-		          (copy-marker (line-beginning-position))))
+	          (setq org-last-recalc-line
+		        (org-ensure-and-move-marker
+			 org-last-recalc-line (line-beginning-position)))
 	          (dolist (entry eqlcol)
 		    (goto-char org-last-recalc-line)
 		    (org-table-goto-column
@@ -4954,9 +4953,8 @@ This function sets up the following dynamically scoped variables:
 			(push (list field line col)
 			      org-table-named-field-locations))))))))))
       ;; Reuse existing markers when possible.
-      (if (markerp org-table-current-begin-pos)
-	  (move-marker org-table-current-begin-pos (point))
-	(setq org-table-current-begin-pos (point-marker)))
+      (setq org-table-current-begin-pos
+	    (org-ensure-and-move-marker org-table-current-begin-pos))
       ;; Analyze the line types.
       (let ((l 0) hlines dlines types)
 	(while (looking-at "[ \t]*|\\(-\\)?")
diff --git a/testing/lisp/test-org-macs.el b/testing/lisp/test-org-macs.el
index 015668fc8..db1f9921f 100644
--- a/testing/lisp/test-org-macs.el
+++ b/testing/lisp/test-org-macs.el
@@ -154,6 +154,17 @@
 
 ;;; Buffers
 
+(ert-deftest test-org/ensure-and-move-marker ()
+  "Test `org-ensure-and-move-marker' specifications."
+  (org-test-with-temp-text "A<point>B"
+    (let ((marker (org-ensure-and-move-marker nil)))
+      (should (markerp marker))
+      (should (eq (current-buffer) (marker-buffer marker)))
+      (should (= (point) marker))
+      (goto-char (point-min))
+      (should (eq marker (org-ensure-and-move-marker marker (point-max))))
+      (should (= (point-max) marker)))))
+
 (ert-deftest test-org-base-buffer-file-name ()
   "Test `org-base-buffer-file-name'."
   ;; Test direct buffer resolution
-- 
2.39.5

Reply via email to