Expand only one of the included files

Hello,

I have a main text file in which I have included several other files (tex files for example) using the make-include command.
I would like to be able to expand only one of these included files , so that I can access and edit its content directly in TeXmacs, without expanding all the other inclusions in the main document.

I know about the buffer-expand-includes command, but it expands all included files at once.
What I am looking for is a function that can expand only a single included file — specifically, the one that appears just above the cursor at the moment of execution.

More precisely, this function should:

  • locate in the document tree the first (include ...) node above the cursor;
  • replace this node with the actual content of the included file;
  • move the cursor to the end of the newly inserted content.

I have tried to adapt the buffer-expand-includes function and the definitions found in
progs/generic/document-part.scm , but I haven’t succeeded so far.

Perhaps such a function already exists?
Thank you very much for any help or suggestions you might provide.

A modification of the function using focus-tree instead of buffer-tree seems to work

(tm-define (buffer-expand-includes)
  (with t (focus-tree)
    (tree-assign! t (expand-includes (buffer-tree) (buffer-master)))))

with-innermost might also be useful.

I’ve tested your function using a keyboard shortcut, but it doesn’t always work correctly.

During the first use within a file, if I place the cursor just after an inclusion tag, the corresponding file is properly expanded.

However, when I try to use the shortcut again on other included files within the same document, the behavior becomes inconsistent: sometimes nothing happens, or the wrong inclusion is expanded.

Moreover, even when the function works, the cursor does not return to its original position — it usually ends up at the end of the main TeXmacs file, which makes the operation less convenient.

I wonder if the function should be rewritten using something like with-innermost , or a similar mechanism, in order to more precisely target the inclusion closest to the cursor before expanding it.

Unfortunately, I don’t feel confident enough to implement this myself.
Thank you very much for your help.

It seems I missed a few things. This might work better.

(tm-define (focus-expand-includes)
  (with t (focus-tree)
    (tree-assign! t (cons 'document (expand-includes-one t (buffer-master))))))