Developing a custom field/variable

I’m interesting in developing a custom object/field/variable thingy for LibreOffice writer and need pointing in the right direction.

Goal:
I’d like to be able to insert a mark within the document to display information about that mark’s position within the document. When creating a novel/play/script the positions of events within the story are important.

Questions:
The terminology is still unclear to me. Searching for something you can’t name is difficult :wink:
Is the best way to accomplish this a “field”, a “variable”, “extension”, or something else?

  • It should be optionally unprintable when the document is printed/exported.
  • A tool tip or text should show position with the file
  • I’d like the user to be able to name the mark and optionally use it as a bookmark or hyperlink

I’m comfortable developing in C++, java, python, or whatever but have no office development experience.
Where do I start?

How should the “information about that mark’s position within the document” be given. Do you aim at Position.X, Position.Y relative to… or …?
Your turn.

I envisioned something like an annotated separator. Probably like:
“---- First plot point. Now at 33% ( 1234 words of 3702 total ) ----”

I assume not terribly difficult to code. I’m just not sure what the best starting point.

See LibreOffice: XWordCursor Interface Reference
I don’t like the idea. Text documents have different types of content, and not only the bodytext can contain words. There are tables, shapes, frames,
In addition it’s not culture-independent what needs to be counted as a a word.
Probably someone else likes this kind of task better.

Thanks for the link to the API. That’s will be helpful once I get down among the trees. I’m still not sure which part of the forest to go to yet. What type of object would I call the API from?

Please when cross posting let each site know - also posted here → Developing custom field/variable for LibreOffice

[edit datetime=“about 2020-12-21 11:35”]
Missed to mention that the demo uses the REGEX() function only available in LibreOffice V6.2 or higher.
I didn’t test, but assume the usage of XWordCursor would be less efficient.
But then, I don’t know if there is a restricion concerning the length of the string passed to REGEX for parsing.
[/edit]

There isn’t a “formula” for the purpose. You need to create the content yourself.
I made a demo. It’s not perfect, but works for me satisfatorily.
demoWCannotations.odt

Place the insertion cursor somewhere without selecting any text, and run the macro ‘createInsertWCannotation’.
Repeat this as often as you want.
Change the text by inserting / deleting a few words here and there.
Run the macro 'updateWCannotations.

You see?

The way I count the words may not give exactly the same results as built-in routines. The difference should be negligible.

The word count need not be perfectly exact. Thanks for all the effort!