General XMetaL Discussion
MrPaul March 22, 2013 at 7:50 pm
Getting Range locationMarch 22, 2013 at 7:50 pmParticipants 0Replies 1Last Activity 9 years, 6 months ago
Is there a way to get the ActiveDocument.Range location, either by line number & character position or byte location in the file or even by xpath? Some kind of marker to keep track of where it is?
Using XMAX v6.
Thanks.Derek Read March 25, 2013 at 6:37 am
Reply to: Getting Range locationMarch 25, 2013 at 6:37 am
Can I assume you are you trying to persist this information after closing the document so that you can restore the Range at document open?
If you can build an XPath from the current position then it could be used to get back to the same element. There is no API that will give you an XPath based on the current element automatically (that I can think of) but the logic should be possible to code by just walking up the node tree from the current element to the root (you might use ContainerNode or similar). If you need to get back to the same position inside a text node (inside an element) you would likely need to find that based on some string manipulation. Creating a second Range that starts at the beginning of the text node and ends at the current Range you could then use Range.Text and then find that strings length would give you the position. Then to get back to that spot you could use Range.MoveRight().
Keep in mind that if the file has changed between closing and reopening (ie: something other than XMAX modifies the file) that strategy would probably fail.
An alternative might be to insert a PI with unique target and/or data when the document is saved and then find it when the document is reopened (and remove it at that time). This might be more robust. Provided the PI is not removed, the document could be changed in any fashion and you could still restore your Range to the position of the PI. Any amount of text or any number of elements could be modified before the PI in this case.
I believe there may be more advanced APIs available in newer versions but these are currently for internal use only (and as a result they are undocumented). I believe the cross-file features (Spell Check in Files and Find in Files) in XMetaL Author take advantage of these to implement some of their functionality.
- You must be logged in to reply to this topic.