int64_t word_wrap(buffer * const b) { const int64_t len = b->cur_line_desc->line_len; char * const line = b->cur_line_desc->line; int64_t pos, first_pos; if (!(pos = b->cur_pos)) return ERROR; /* Find the first possible position we could break a line on. */ first_pos = 0; /* Skip leading white space */ while (first_pos < len && ne_isspace(get_char(&line[first_pos], b->encoding), b->encoding)) first_pos = next_pos(line, first_pos, b->encoding); /* Skip non_space after leading white space */ while (first_pos < len && !ne_isspace(get_char(&line[first_pos], b->encoding), b->encoding)) first_pos = next_pos(line, first_pos, b->encoding); /* Now we know that the line shouldn't be broken before &line[first_pos]. */ /* Search left from the current position to find a candidate space to break the line on.*/ while((pos = prev_pos(line, pos, b->encoding)) && !ne_isspace(get_char(&line[pos], b->encoding), b->encoding)); if (! pos || pos < first_pos) return ERROR; start_undo_chain(b); const int64_t result = b->cur_pos - pos - 1; if (pos < b->cur_pos) b->cur_pos = -1; delete_one_char(b, b->cur_line_desc, b->cur_line, pos); insert_one_line(b, b->cur_line_desc, b->cur_line, pos); end_undo_chain(b); return result; }
int word_wrap2(buffer * const b) { static char avcmd[16]; if (b->cur_pos > b->cur_line_desc->line_len) return OK; bool non_blank_added = false; int avshift; char * line = b->cur_line_desc->line; int64_t pos, original_line; /* If the char to our left is a space, we need to insert a non-space to attach our WORDWRAP_BOOKMARK to because spaces at the split point get removed, which effectively leaves our bookmark on the current line. */ delay_update(); pos = prev_pos(line, b->cur_pos, b->encoding); if (pos >= 0 && (non_blank_added = ne_isspace(get_char(&line[pos], b->encoding), b->encoding))) { start_undo_chain(b); insert_one_char(b, b->cur_line_desc, b->cur_line, b->cur_pos, 'X'); line = b->cur_line_desc->line; goto_pos(b, next_pos(line, b->cur_pos, b->encoding)); } b->bookmark[WORDWRAP_BOOKMARK].pos = b->cur_pos; b->bookmark[WORDWRAP_BOOKMARK].line = original_line = b->cur_line; b->bookmark[WORDWRAP_BOOKMARK].cur_y = b->cur_y; b->bookmark_mask |= (1 << WORDWRAP_BOOKMARK); paragraph(b); goto_line_pos(b, b->bookmark[WORDWRAP_BOOKMARK].line, b->bookmark[WORDWRAP_BOOKMARK].pos); line = b->cur_line_desc->line; b->bookmark[WORDWRAP_BOOKMARK].cur_y += b->bookmark[WORDWRAP_BOOKMARK].line - original_line; if (avshift = b->cur_y - b->bookmark[WORDWRAP_BOOKMARK].cur_y) { snprintf(avcmd, 16, "%c%d", avshift > 0 ? 'T' :'B', avshift > 0 ? avshift : -avshift); adjust_view(b, avcmd); } b->bookmark_mask &= ~(1 << WORDWRAP_BOOKMARK); if (non_blank_added) { goto_pos(b, prev_pos(b->cur_line_desc->line, b->cur_pos, b->encoding)); delete_one_char(b, b->cur_line_desc, b->cur_line, b->cur_pos); end_undo_chain(b); } return stop ? STOPPED : OK; }
static void delete_char(InputItem *sym) { if (delete_one_char(sym)) update_inputsymbol(sym); }