A
A
aldexnotproger2020-05-19 20:12:41
C++ / C#
aldexnotproger, 2020-05-19 20:12:41

How to navigate the Gap buffer?

Hello.
I want to write a console text editor in ncurses and C.
I want to use the Gap buffer as a data structure for storing text, and the question arises: how to navigate in it. I mean, when moving the cursor around the screen, I have to calculate the line and column, and the cursor should not go beyond the line. It's not clear to me how to do this, maybe it's better to look for another data structure?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Dubrovin, 2020-05-20
@aldexnotproger

You can do it with a gap buffer, store information about the cursor position separately when you change the cursor position, change the gap position, in general, it is designed for this.
I don't know how it is customary to implement in real text editors, I would make a structure similar to btree without a key
https://en.wikipedia.org/wiki/B-%D0%B4%D0%B5%D1%80...
in each node, store either a pointer to a text fragment + its length, or a sign of the end of a paragraph/special character, or a link to another node. Store each fragment and line as a separate node.
In fact, this is the ability to have an unlimited number of gaps in arbitrary places without the need to move them. As a bonus, this structure makes it easy to navigate, undo/redo, as each entry is stored in a separate node copy, paste and everything else that is usually required. Maintaining formatted text is also very easy, just add formatting tags as another type of node.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question