aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2017-05-21 22:58:23 +0200
committerThomas White <taw@bitwiz.org.uk>2017-05-21 22:58:23 +0200
commit272e297df33bbfeaca3daebbbb1b074d7d8f036b (patch)
tree1bef01b868df5cf29804abcab8b2f31260ab319d /src
parentc6c1a2e6b3f4bca8e833d3c519a78bcf2f41d5c3 (diff)
Fix character offsets
Diffstat (limited to 'src')
-rw-r--r--src/frame.c25
-rw-r--r--src/frame.h4
-rw-r--r--src/sc_editor.c1
3 files changed, 23 insertions, 7 deletions
diff --git a/src/frame.c b/src/frame.c
index d69f0fb..cf4ee4d 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -39,8 +39,8 @@ struct text_run
{
SCBlock *scblock;
SCBlock *macro_real_block;
- size_t scblock_offs_bytes;
- size_t para_offs_bytes;
+ size_t scblock_offs_bytes; /* Offset from start of SCBlock */
+ size_t para_offs_bytes; /* Offset from start of paragraph */
size_t len_bytes;
PangoFontDescription *fontdesc;
double col[4];
@@ -825,6 +825,7 @@ static int which_run(Paragraph *para, size_t offs)
size_t pos_trail_to_offset(Paragraph *para, size_t offs, int trail)
{
glong char_offs;
+ size_t run_offs;
const char *run_text;
struct text_run *run;
int nrun;
@@ -838,12 +839,24 @@ size_t pos_trail_to_offset(Paragraph *para, size_t offs, int trail)
return 0;
}
+ /* Get the text for the run */
run_text = sc_block_contents(run->scblock) + run->scblock_offs_bytes;
- char_offs = g_utf8_pointer_to_offset(run_text, run_text+offs);
+
+ /* Turn the paragraph offset into a run offset */
+ run_offs = offs - run->para_offs_bytes;
+
+ char_offs = g_utf8_pointer_to_offset(run_text, run_text+run_offs);
char_offs += trail;
+ if ( char_offs > g_utf8_strlen(run_text, -1) ) {
+ printf("Offset outside string! '%s'\n"
+ "char_offs %li offs %li len %li\n",
+ run_text, (long int)char_offs, (long int)offs,
+ (long int)g_utf8_strlen(run_text, -1));
+ }
+
ptr = g_utf8_offset_to_pointer(run_text, char_offs);
- return ptr - run_text;
+ return ptr - run_text + run->para_offs_bytes;
}
@@ -896,6 +909,8 @@ void delete_text_from_frame(struct frame *fr, struct edit_pos p1, struct edit_po
{
int i;
+ sort_positions(&p1, &p2);
+
for ( i=p1.para; i<=p2.para; i++ ) {
size_t start;
@@ -916,7 +931,7 @@ void delete_text_from_frame(struct frame *fr, struct edit_pos p1, struct edit_po
}
if ( (start == 0) && (finis == -1) ) {
- delete_paragraph(fr, para);
+ delete_paragraph(fr, i);
} else {
delete_text_in_paragraph(para, start, finis);
wrap_paragraph(para, NULL, wrapw, 0, 0);
diff --git a/src/frame.h b/src/frame.h
index 6302edc..0386a51 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -61,8 +61,8 @@ typedef struct _paragraph Paragraph;
struct edit_pos
{
- int para;
- size_t pos;
+ int para; /* Paragraph number */
+ size_t pos; /* Byte position within paragraph */
int trail;
};
diff --git a/src/sc_editor.c b/src/sc_editor.c
index 32974fb..aabb84a 100644
--- a/src/sc_editor.c
+++ b/src/sc_editor.c
@@ -724,6 +724,7 @@ static void do_backspace(struct frame *fr, SCEditor *e)
delete_text_from_frame(e->cursor_frame, e->sel_start, e->sel_end, wrapw);
/* Cursor goes at start of deletion */
+ sort_positions(&e->sel_start, &e->sel_end);
e->cursor_para = e->sel_start.para;
e->cursor_pos = e->sel_start.pos;
e->cursor_trail = e->sel_start.trail;