mercredi 23 juin 2021

How does one implement soft wrapping of long lines in editor?

I'm in a need of implementing soft (visual) breaking of long lines in my ad-hoc text editor. I'm having problems with deciding upon:

  • How should (and if it should) the information about (visual-only-) break points of each long line be stored?
  • How should (and if…) the information about cursor position within the logical long line be stored?
  • When scrolling, from where to get information that first line on screen is actually eg. a 2nd part of a long line already partially scrolled outside the viewport?

Basically I'm looking for something possibly close to a design pattern and/or someone's experienced advice.

The current buffer structure is as follows:

typedef struct 
{
    off_t curs1;                /* pos. of the cursor from beginning of file. */
    off_t curs2;                /* pos. from end of file */
    GPtrArray *b1;              /* all data up to curs1 */
    GPtrArray *b2;              /* all data from end of file down to curs2 */
} edit_buf_t;

The pointer arrays hold hunks of 65535 bytes, so, for example, in order to get a byte before curs1, one could:

/* Size of the buffer in bits and max value */
#define EDIT_BF_SZ_BITS 16
#define EDIT_BF_SZ (((off_t) 1) << EDIT_BF_SZ_BITS)

/* Buffer size mask of all bits set */
#define EDIT_BF_SZ_MASK (EDIT_BF_SZ - 1)

b = g_ptr_array_index (buf->b1, byte_index >> EDIT_BF_SZ_BITS);
return (char *) b + (byte_index & EDIT_BF_SZ_MASK);

Aucun commentaire:

Enregistrer un commentaire