Sorry for the delay. I attached a better fix but it still has an issue.

The patch replaces `end' (that is a point in the tangled file) with
the end point of the block in the org file, like you suggested. It is
an improvement. The condition is the same: do not move past the end.
The issue is that the offset is still from the tangled file, which has
unescaped code.

A complete fix would need to calculate the offset using escaped
characters. org-edit-special and org-edit-src-exit preserve the point
in the special buffer, so one could use these to find the true
target-char (a variable defined in the function), but these functions
are too heavy (load modes). Or one could make a
org-src-forward-unescaped-char. What do you think?


On Sat, 7 Mar 2026 at 18:07, Ihor Radchenko <[email protected]> wrote:
>
> George Moutsopoulos <[email protected]> writes:
>
> > On closer inspection, there is a larger error that (point) in the code
> > is the point in the org-mode buffer whereas mid, end and body-start
> > were calculated as positions inside the code block in the tangled
> > buffer. E.g. in my case the tangled buffer is stripped of titles,
> > headings, results etc.
> >
> > The following works in my MWE but not sure if it will throw for
> > whatever reason. I send you a patch with
> >
> > (let* ((el (org-element-at-point))
> >      (org-block-size (length (org-element-property :value el)))
> >              (offset (min org-block-size (- mid body-start)))
> >              )
> > (forward-char offset))
>
> That's indeed better.
> The only problem is  that length of :value is not necessarily the same
> as actual code in Org buffer. The :value is unescaped by
> org-unescape-code-in-string and may have a different length.
>
> A more reliable way is
>
> (org-with-point-at (org-element-end element)
>   (skip-chars-backward " \t\n\r")
>   (forward-line 0)
>   (point))
>
> > Subject: [PATCH] fixes offset in org-babel-tangle-jump-to-org
>
> While you are on it, could you also format the commit message as
> described in
> https://orgmode.org/worg/org-contribute.html#commit-messages and add a
> TINYCHANGE cookie since you do not seem to have the copyright assignment?
>
>
> --
> Ihor Radchenko // yantar92,
> Org mode maintainer,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
From ca91c08ef52a5bc4e4822ee6c04347f56e4669ac Mon Sep 17 00:00:00 2001
From: George Moutsopoulos <[email protected]>
Date: Wed, 25 Mar 2026 12:36:25 +0000
Subject: [PATCH] lisp/ob-tangle.el: fix offset of jump

* ob-tangle.el (org-babel-tangle-jump-to-org): do not forward by
offset more than the source code block.

When jumping to org file, in trying to preserve location of point
within source code, the variables used (mid, body-start, end) are
points from the tangled file. The condition to not move beyond the end
of the code block in the org file should not use `end' but the actual
end point of the block in the org file.

This bug fix will make sure we do not end up outside the source
code block in the org file. It still uses as offset the tangled code
offset, which will not consider escaped characters.

Reported-by: "George Moutsopoulos" <[email protected]>
Link: https://list.orgmode.org/orgmode/cabu1ay8ofcs62zlin9calzxvvhepihfyzm0jyn6a7hgf3uc...@mail.gmail.com

TINYCHANGE
---
 lisp/ob-tangle.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 6e4ae07da..e8ecf15a3 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -755,8 +755,12 @@ of the current buffer."
       (forward-line 1)
       ;; Try to preserve location of point within the source code in
       ;; tangled code file.
-      (let ((offset (- mid body-start)))
-	(when (> end (+ offset (point)))
+      (let ((offset (- mid body-start))
+            (block-ends-here (org-with-point-at (org-element-end (org-element-at-point))
+                               (skip-chars-backward " \t\n\r")
+                               (forward-line 0)
+                               (point))))
+        (when (> block-ends-here (+ offset (point)))
 	  (forward-char offset)))
       (setq target-char (point)))
     (org-src-switch-to-buffer target-buffer t)
-- 
2.43.0

Reply via email to