Undo: split text undo steps out of the data-block

This moves undo storage into a separate struct which is passed
in from the undo system.
This commit is contained in:
Campbell Barton
2018-04-05 15:22:33 +02:00
parent 79c3c56894
commit ad1ea9a427
11 changed files with 384 additions and 360 deletions

View File

@@ -40,6 +40,7 @@ extern "C" {
struct Main;
struct Text;
struct TextLine;
struct TextUndoBuf;
void BKE_text_free_lines (struct Text *text);
void BKE_text_free (struct Text *text);
@@ -55,8 +56,8 @@ struct Text *BKE_text_load (struct Main *bmain, const char *file, const char
void BKE_text_copy_data(struct Main *bmain, struct Text *ta_dst, const struct Text *ta_src, const int flag);
struct Text *BKE_text_copy (struct Main *bmain, const struct Text *ta);
void BKE_text_make_local (struct Main *bmain, struct Text *text, const bool lib_local);
void BKE_text_clear (struct Text *text);
void BKE_text_write (struct Text *text, const char *str);
void BKE_text_clear (struct Text *text, struct TextUndoBuf *utxt);
void BKE_text_write (struct Text *text, struct TextUndoBuf *utxt, const char *str);
int BKE_text_file_modified_check(struct Text *text);
void BKE_text_file_modified_ignore(struct Text *text);
@@ -83,29 +84,29 @@ void txt_move_eol (struct Text *text, const bool sel);
void txt_move_toline (struct Text *text, unsigned int line, const bool sel);
void txt_move_to (struct Text *text, unsigned int line, unsigned int ch, const bool sel);
void txt_pop_sel (struct Text *text);
void txt_delete_char (struct Text *text);
void txt_delete_word (struct Text *text);
void txt_delete_selected (struct Text *text);
void txt_delete_char (struct Text *text, struct TextUndoBuf *utxt);
void txt_delete_word (struct Text *text, struct TextUndoBuf *utxt);
void txt_delete_selected (struct Text *text, struct TextUndoBuf *utxt);
void txt_sel_all (struct Text *text);
void txt_sel_clear (struct Text *text);
void txt_sel_line (struct Text *text);
char *txt_sel_to_buf (struct Text *text);
void txt_insert_buf (struct Text *text, const char *in_buffer);
void txt_undo_add_op (struct Text *text, int op);
void txt_do_undo (struct Text *text);
void txt_do_redo (struct Text *text);
void txt_split_curline (struct Text *text);
void txt_backspace_char (struct Text *text);
void txt_backspace_word (struct Text *text);
bool txt_add_char (struct Text *text, unsigned int add);
bool txt_add_raw_char (struct Text *text, unsigned int add);
bool txt_replace_char (struct Text *text, unsigned int add);
void txt_unindent (struct Text *text);
void txt_comment (struct Text *text);
void txt_indent (struct Text *text);
void txt_uncomment (struct Text *text);
void txt_move_lines (struct Text *text, const int direction);
void txt_duplicate_line (struct Text *text);
void txt_insert_buf (struct Text *text, struct TextUndoBuf *utxt, const char *in_buffer);
void txt_undo_add_op (struct Text *text, struct TextUndoBuf *utxt, int op);
void txt_do_undo (struct Text *text, struct TextUndoBuf *utxt);
void txt_do_redo (struct Text *text, struct TextUndoBuf *utxt);
void txt_split_curline (struct Text *text, struct TextUndoBuf *utxt);
void txt_backspace_char (struct Text *text, struct TextUndoBuf *utxt);
void txt_backspace_word (struct Text *text, struct TextUndoBuf *utxt);
bool txt_add_char (struct Text *text, struct TextUndoBuf *utxt, unsigned int add);
bool txt_add_raw_char (struct Text *text, struct TextUndoBuf *utxt, unsigned int add);
bool txt_replace_char (struct Text *text, struct TextUndoBuf *utxt, unsigned int add);
void txt_unindent (struct Text *text, struct TextUndoBuf *utxt);
void txt_comment (struct Text *text, struct TextUndoBuf *utxt);
void txt_indent (struct Text *text, struct TextUndoBuf *utxt);
void txt_uncomment (struct Text *text, struct TextUndoBuf *utxt);
void txt_move_lines (struct Text *text, struct TextUndoBuf *utxt, const int direction);
void txt_duplicate_line (struct Text *text, struct TextUndoBuf *utxt);
int txt_setcurr_tab_spaces(struct Text *text, int space);
bool txt_cursor_is_line_start(struct Text *text);
bool txt_cursor_is_line_end(struct Text *text);
@@ -135,6 +136,11 @@ enum {
TXT_MOVE_LINE_DOWN = 1
};
typedef struct TextUndoBuf {
char *buf;
int pos, len;
} TextUndoBuf;
#ifdef __cplusplus
}
#endif

View File

@@ -168,9 +168,9 @@ enum {
static void txt_pop_first(Text *text);
static void txt_pop_last(Text *text);
static void txt_undo_add_blockop(Text *text, int op, const char *buf);
static void txt_undo_add_blockop(Text *text, TextUndoBuf *utxt, int op, const char *buf);
static void txt_delete_line(Text *text, TextLine *line);
static void txt_delete_sel(Text *text);
static void txt_delete_sel(Text *text, TextUndoBuf *utxt);
static void txt_make_dirty(Text *text);
/***/
@@ -188,13 +188,6 @@ int txt_get_undostate(void)
return undoing;
}
static void init_undo_text(Text *text)
{
text->undo_pos = -1;
text->undo_len = 0;
text->undo_buf = NULL;
}
/**
* \note caller must handle `undo_buf` and `compiled` members.
*/
@@ -222,7 +215,6 @@ void BKE_text_free(Text *text)
BKE_text_free_lines(text);
MEM_SAFE_FREE(text->name);
MEM_SAFE_FREE(text->undo_buf);
#ifdef WITH_PYTHON
BPY_text_free_code(text);
#endif
@@ -236,8 +228,6 @@ void BKE_text_init(Text *ta)
ta->name = NULL;
init_undo_text(ta);
ta->nlines = 1;
ta->flags = TXT_ISDIRTY | TXT_ISMEM;
if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0)
@@ -419,10 +409,6 @@ bool BKE_text_reload(Text *text)
txt_make_dirty(text);
/* clear undo buffer */
MEM_freeN(text->undo_buf);
init_undo_text(text);
if (BLI_stat(filepath_abs, &st) != -1) {
text->mtime = st.st_mtime;
}
@@ -471,8 +457,6 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const
}
/* clear undo buffer */
init_undo_text(ta);
if (BLI_stat(filepath_abs, &st) != -1) {
ta->mtime = st.st_mtime;
}
@@ -526,8 +510,6 @@ void BKE_text_copy_data(Main *UNUSED(bmain), Text *ta_dst, const Text *ta_src, c
ta_dst->curl = ta_dst->sell = ta_dst->lines.first;
ta_dst->curc = ta_dst->selc = 0;
init_undo_text(ta_dst);
}
Text *BKE_text_copy(Main *bmain, const Text *ta)
@@ -542,25 +524,29 @@ void BKE_text_make_local(Main *bmain, Text *text, const bool lib_local)
BKE_id_make_local_generic(bmain, &text->id, true, lib_local);
}
void BKE_text_clear(Text *text) /* called directly from rna */
void BKE_text_clear(Text *text, TextUndoBuf *utxt) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate();
txt_set_undostate(1);
if (utxt) {
oldstate = txt_get_undostate();
}
txt_set_undostate(utxt != NULL);
txt_sel_all(text);
txt_delete_sel(text);
txt_delete_sel(text, utxt);
txt_set_undostate(oldstate);
txt_make_dirty(text);
}
void BKE_text_write(Text *text, const char *str) /* called directly from rna */
void BKE_text_write(Text *text, TextUndoBuf *utxt, const char *str) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate();
txt_insert_buf(text, str);
txt_insert_buf(text, utxt, str);
txt_move_eof(text, 0);
txt_set_undostate(oldstate);
@@ -1153,7 +1139,7 @@ bool txt_has_sel(Text *text)
return ((text->curl != text->sell) || (text->curc != text->selc));
}
static void txt_delete_sel(Text *text)
static void txt_delete_sel(Text *text, TextUndoBuf *utxt)
{
TextLine *tmpl;
char *buf;
@@ -1167,7 +1153,7 @@ static void txt_delete_sel(Text *text)
if (!undoing) {
buf = txt_sel_to_buf(text);
txt_undo_add_blockop(text, UNDO_DBLOCK, buf);
txt_undo_add_blockop(text, utxt, UNDO_DBLOCK, buf);
MEM_freeN(buf);
}
@@ -1408,7 +1394,7 @@ char *txt_sel_to_buf(Text *text)
return buf;
}
void txt_insert_buf(Text *text, const char *in_buffer)
void txt_insert_buf(Text *text, TextUndoBuf *utxt, const char *in_buffer)
{
int l = 0, u, len;
size_t i = 0, j;
@@ -1417,22 +1403,22 @@ void txt_insert_buf(Text *text, const char *in_buffer)
if (!in_buffer) return;
txt_delete_sel(text);
txt_delete_sel(text, utxt);
len = strlen(in_buffer);
buffer = BLI_strdupn(in_buffer, len);
len += txt_extended_ascii_as_utf8(&buffer);
if (!undoing) txt_undo_add_blockop(text, UNDO_IBLOCK, buffer);
if (!undoing) txt_undo_add_blockop(text, utxt, UNDO_IBLOCK, buffer);
u = undoing;
undoing = 1;
/* Read the first line (or as close as possible */
while (buffer[i] && buffer[i] != '\n')
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, &i));
txt_add_raw_char(text, utxt, BLI_str_utf8_as_unicode_step(buffer, &i));
if (buffer[i] == '\n') txt_split_curline(text);
if (buffer[i] == '\n') txt_split_curline(text, utxt);
else { undoing = u; MEM_freeN(buffer); return; }
i++;
@@ -1450,7 +1436,7 @@ void txt_insert_buf(Text *text, const char *in_buffer)
}
else {
for (j = i - l; j < i && j < len; )
txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, &j));
txt_add_raw_char(text, utxt, BLI_str_utf8_as_unicode_step(buffer, &j));
break;
}
}
@@ -1464,7 +1450,7 @@ void txt_insert_buf(Text *text, const char *in_buffer)
/* Undo functions */
/******************/
static bool max_undo_test(Text *text, int x)
static bool max_undo_test(TextUndoBuf *utxt, int x)
{
/* Normally over-allocating is preferred,
* however in this case the buffer is small enough and re-allocation
@@ -1474,25 +1460,24 @@ static bool max_undo_test(Text *text, int x)
*/
/* Add one for the null terminator. */
text->undo_len = text->undo_pos + x + 1;
if (text->undo_len > TXT_MAX_UNDO) {
utxt->len = utxt->pos + x + 1;
if (utxt->len > TXT_MAX_UNDO) {
/* XXX error("Undo limit reached, buffer cleared\n"); */
MEM_freeN(text->undo_buf);
init_undo_text(text);
MEM_freeN(utxt->buf);
return false;
}
else {
/* Small reallocations on each undo step is fine. */
text->undo_buf = MEM_recallocN(text->undo_buf, text->undo_len);
utxt->buf = MEM_recallocN(utxt->buf, utxt->len);
}
return true;
}
static void txt_undo_end(Text *text)
static void txt_undo_end(Text *UNUSED(text), TextUndoBuf *utxt)
{
int undo_pos_end = text->undo_pos + 1;
BLI_assert(undo_pos_end + 1 == text->undo_len);
text->undo_buf[undo_pos_end] = '\0';
int undo_pos_end = utxt->pos + 1;
BLI_assert(undo_pos_end + 1 == utxt->len);
utxt->buf[undo_pos_end] = '\0';
}
/* Call once undo is done. */
@@ -1501,11 +1486,11 @@ static void txt_undo_end(Text *text)
#endif
#if 0 /* UNUSED */
static void dump_buffer(Text *text)
static void dump_buffer(TextUndoBuf *utxt)
{
int i = 0;
while (i++ < text->undo_pos) printf("%d: %d %c\n", i, text->undo_buf[i], text->undo_buf[i]);
while (i++ < utxt->undo_pos) printf("%d: %d %c\n", i, utxt->buf[i], utxt->buf[i]);
}
/* Note: this function is outdated and must be updated if needed for future use */
@@ -1520,10 +1505,10 @@ void txt_print_undo(Text *text)
printf("---< Undo Buffer >---\n");
printf("UndoPosition is %d\n", text->undo_pos);
printf("UndoPosition is %d\n", utxt->pos);
while (i <= text->undo_pos) {
op = text->undo_buf[i];
while (i <= utxt->pos) {
op = utxt->buf[i];
if (op == UNDO_INSERT_1) {
ops = "Insert ascii ";
@@ -1589,15 +1574,15 @@ void txt_print_undo(Text *text)
printf(" - Char is ");
switch (op) {
case UNDO_INSERT_1: case UNDO_BS_1: case UNDO_DEL_1:
printf("%c", text->undo_buf[i]);
printf("%c", utxt->buf[i]);
i++;
break;
case UNDO_INSERT_2: case UNDO_BS_2: case UNDO_DEL_2:
printf("%c%c", text->undo_buf[i], text->undo_buf[i + 1]);
printf("%c%c", utxt->buf[i], utxt->buf[i + 1]);
i += 2;
break;
case UNDO_INSERT_3: case UNDO_BS_3: case UNDO_DEL_3:
printf("%c%c%c", text->undo_buf[i], text->undo_buf[i + 1], text->undo_buf[i + 2]);
printf("%c%c%c", utxt->buf[i], utxt->buf[i + 1], utxt->buf[i + 2]);
i += 3;
break;
case UNDO_INSERT_4: case UNDO_BS_4: case UNDO_DEL_4:
@@ -1605,10 +1590,10 @@ void txt_print_undo(Text *text)
unsigned int uc;
char c[BLI_UTF8_MAX + 1];
size_t c_len;
uc = text->undo_buf[i]; i++;
uc = uc + (text->undo_buf[i] << 8); i++;
uc = uc + (text->undo_buf[i] << 16); i++;
uc = uc + (text->undo_buf[i] << 24); i++;
uc = utxt->buf[i]; i++;
uc = uc + (utxt->buf[i] << 8); i++;
uc = uc + (utxt->buf[i] << 16); i++;
uc = uc + (utxt->buf[i] << 24); i++;
c_len = BLI_str_utf8_from_unicode(uc, c);
c[c_len] = '\0';
puts(c);
@@ -1619,44 +1604,44 @@ void txt_print_undo(Text *text)
else if (op == UNDO_DBLOCK || op == UNDO_IBLOCK) {
i++;
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
linep = utxt->buf[i]; i++;
linep = linep + (utxt->buf[i] << 8); i++;
linep = linep + (utxt->buf[i] << 16); i++;
linep = linep + (utxt->buf[i] << 24); i++;
printf(" (length %d) <", linep);
while (linep > 0) {
putchar(text->undo_buf[i]);
putchar(utxt->buf[i]);
linep--; i++;
}
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
linep = utxt->buf[i]; i++;
linep = linep + (utxt->buf[i] << 8); i++;
linep = linep + (utxt->buf[i] << 16); i++;
linep = linep + (utxt->buf[i] << 24); i++;
printf("> (%d)", linep);
}
else if (op == UNDO_INDENT || op == UNDO_UNINDENT) {
i++;
charp = text->undo_buf[i]; i++;
charp = charp + (text->undo_buf[i] << 8); i++;
charp = utxt->buf[i]; i++;
charp = charp + (utxt->buf[i] << 8); i++;
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
linep = utxt->buf[i]; i++;
linep = linep + (utxt->buf[i] << 8); i++;
linep = linep + (utxt->buf[i] << 16); i++;
linep = linep + (utxt->buf[i] << 24); i++;
printf("to <%d, %d> ", linep, charp);
charp = text->undo_buf[i]; i++;
charp = charp + (text->undo_buf[i] << 8); i++;
charp = utxt->buf[i]; i++;
charp = charp + (utxt->buf[i] << 8); i++;
linep = text->undo_buf[i]; i++;
linep = linep + (text->undo_buf[i] << 8); i++;
linep = linep + (text->undo_buf[i] << 16); i++;
linep = linep + (text->undo_buf[i] << 24); i++;
linep = utxt->buf[i]; i++;
linep = linep + (utxt->buf[i] << 8); i++;
linep = linep + (utxt->buf[i] << 16); i++;
linep = linep + (utxt->buf[i] << 24); i++;
printf("from <%d, %d>", linep, charp);
}
@@ -1688,113 +1673,113 @@ static void txt_undo_store_uint32(char *undo_buf, int *undo_pos, unsigned int va
}
/* store the cur cursor to the undo buffer (6 bytes)*/
static void txt_undo_store_cur(Text *text)
static void txt_undo_store_cur(Text *text, TextUndoBuf *utxt)
{
txt_undo_store_uint16(text->undo_buf, &text->undo_pos, text->curc);
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, txt_get_span(text->lines.first, text->curl));
txt_undo_store_uint16(utxt->buf, &utxt->pos, text->curc);
txt_undo_store_uint32(utxt->buf, &utxt->pos, txt_get_span(text->lines.first, text->curl));
}
/* store the sel cursor to the undo buffer (6 bytes) */
static void txt_undo_store_sel(Text *text)
static void txt_undo_store_sel(Text *text, TextUndoBuf *utxt)
{
txt_undo_store_uint16(text->undo_buf, &text->undo_pos, text->selc);
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, txt_get_span(text->lines.first, text->sell));
txt_undo_store_uint16(utxt->buf, &utxt->pos, text->selc);
txt_undo_store_uint32(utxt->buf, &utxt->pos, txt_get_span(text->lines.first, text->sell));
}
/* store both cursors to the undo buffer (12 bytes) */
static void txt_undo_store_cursors(Text *text)
static void txt_undo_store_cursors(Text *text, TextUndoBuf *utxt)
{
txt_undo_store_cur(text);
txt_undo_store_sel(text);
txt_undo_store_cur(text, utxt);
txt_undo_store_sel(text, utxt);
}
/* store an operator along with a block of data */
static void txt_undo_add_blockop(Text *text, int op, const char *buf)
static void txt_undo_add_blockop(Text *text, TextUndoBuf *utxt, int op, const char *buf)
{
unsigned int length = strlen(buf);
if (!max_undo_test(text, 2 + 12 + 4 + length + 4 + 1)) {
if (!max_undo_test(utxt, 2 + 12 + 4 + length + 4 + 1)) {
return;
}
/* 2 bytes */
text->undo_pos++;
text->undo_buf[text->undo_pos] = op;
text->undo_pos++;
utxt->pos++;
utxt->buf[utxt->pos] = op;
utxt->pos++;
/* 12 bytes */
txt_undo_store_cursors(text);
txt_undo_store_cursors(text, utxt);
/* 4 bytes */
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, length);
txt_undo_store_uint32(utxt->buf, &utxt->pos, length);
/* 'length' bytes */
strncpy(text->undo_buf + text->undo_pos, buf, length);
text->undo_pos += length;
strncpy(utxt->buf + utxt->pos, buf, length);
utxt->pos += length;
/* 4 bytes */
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, length);
txt_undo_store_uint32(utxt->buf, &utxt->pos, length);
/* 1 byte */
text->undo_buf[text->undo_pos] = op;
utxt->buf[utxt->pos] = op;
txt_undo_end(text);
txt_undo_end(text, utxt);
}
/* store a regular operator */
void txt_undo_add_op(Text *text, int op)
void txt_undo_add_op(Text *text, TextUndoBuf *utxt, int op)
{
if (!max_undo_test(text, 2 + 12 + 1)) {
if (!max_undo_test(utxt, 2 + 12 + 1)) {
return;
}
/* 2 bytes */
text->undo_pos++;
text->undo_buf[text->undo_pos] = op;
text->undo_pos++;
utxt->pos++;
utxt->buf[utxt->pos] = op;
utxt->pos++;
/* 12 bytes */
txt_undo_store_cursors(text);
txt_undo_store_cursors(text, utxt);
/* 1 byte */
text->undo_buf[text->undo_pos] = op;
utxt->buf[utxt->pos] = op;
txt_undo_end(text);
txt_undo_end(text, utxt);
}
/* store an operator for a single character */
static void txt_undo_add_charop(Text *text, int op_start, unsigned int c)
static void txt_undo_add_charop(Text *text, TextUndoBuf *utxt, int op_start, unsigned int c)
{
char utf8[BLI_UTF8_MAX];
size_t i, utf8_size = BLI_str_utf8_from_unicode(c, utf8);
if (utf8_size < 4 && 0) {
if (!max_undo_test(text, 2 + 6 + utf8_size + 1)) {
if (!max_undo_test(utxt, 2 + 6 + utf8_size + 1)) {
return;
}
/* 2 bytes */
text->undo_pos++;
text->undo_buf[text->undo_pos] = op_start + utf8_size - 1;
text->undo_pos++;
utxt->pos++;
utxt->buf[utxt->pos] = op_start + utf8_size - 1;
utxt->pos++;
/* 6 bytes */
txt_undo_store_cur(text);
txt_undo_store_cur(text, utxt);
/* 'utf8_size' bytes */
for (i = 0; i < utf8_size; i++) {
text->undo_buf[text->undo_pos] = utf8[i];
text->undo_pos++;
utxt->buf[utxt->pos] = utf8[i];
utxt->pos++;
}
/* 1 byte */
text->undo_buf[text->undo_pos] = op_start + utf8_size - 1;
utxt->buf[utxt->pos] = op_start + utf8_size - 1;
}
else {
if (!max_undo_test(text, 2 + 6 + 4 + 1)) {
if (!max_undo_test(utxt, 2 + 6 + 4 + 1)) {
return;
}
/* 2 bytes */
text->undo_pos++;
text->undo_buf[text->undo_pos] = op_start + 3;
text->undo_pos++;
utxt->pos++;
utxt->buf[utxt->pos] = op_start + 3;
utxt->pos++;
/* 6 bytes */
txt_undo_store_cur(text);
txt_undo_store_cur(text, utxt);
/* 4 bytes */
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, c);
txt_undo_store_uint32(utxt->buf, &utxt->pos, c);
/* 1 byte */
text->undo_buf[text->undo_pos] = op_start + 3;
utxt->buf[utxt->pos] = op_start + 3;
}
txt_undo_end(text);
txt_undo_end(text, utxt);
}
/* extends Link */
@@ -1808,7 +1793,7 @@ struct LinkInt {
* of the lines that should not be indented back.
*/
static void txt_undo_add_unprefix_op(
Text *text, char undo_op,
Text *text, TextUndoBuf *utxt, char undo_op,
const ListBase *line_index_mask, const int line_index_mask_len)
{
struct LinkInt *idata;
@@ -1816,35 +1801,35 @@ static void txt_undo_add_unprefix_op(
BLI_assert(BLI_listbase_count(line_index_mask) == line_index_mask_len);
/* OP byte + UInt32 count + counted UInt32 line numbers + UInt32 count + 12-bytes selection + OP byte */
if (!max_undo_test(text, 2 + 4 + (line_index_mask_len * 4) + 4 + 12 + 1)) {
if (!max_undo_test(utxt, 2 + 4 + (line_index_mask_len * 4) + 4 + 12 + 1)) {
return;
}
/* 2 bytes */
text->undo_pos++;
text->undo_buf[text->undo_pos] = undo_op;
text->undo_pos++;
utxt->pos++;
utxt->buf[utxt->pos] = undo_op;
utxt->pos++;
/* Adding number of line numbers to read
* 4 bytes */
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, line_index_mask_len);
txt_undo_store_uint32(utxt->buf, &utxt->pos, line_index_mask_len);
/* Adding linenumbers of lines that shall not be indented if undoing.
* 'line_index_mask_len * 4' bytes */
for (idata = line_index_mask->first; idata; idata = idata->next) {
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, idata->value);
txt_undo_store_uint32(utxt->buf, &utxt->pos, idata->value);
}
/* Adding number of line numbers to read again.
* 4 bytes */
txt_undo_store_uint32(text->undo_buf, &text->undo_pos, line_index_mask_len);
txt_undo_store_uint32(utxt->buf, &utxt->pos, line_index_mask_len);
/* Adding current selection.
* 12 bytes */
txt_undo_store_cursors(text);
txt_undo_store_cursors(text, utxt);
/* Closing with OP (same as above).
* 1 byte */
text->undo_buf[text->undo_pos] = undo_op;
utxt->buf[utxt->pos] = undo_op;
/* Marking as last undo operation */
txt_undo_end(text);
txt_undo_end(text, utxt);
}
static unsigned short txt_undo_read_uint16(const char *undo_buf, int *undo_pos)
@@ -1999,9 +1984,9 @@ static unsigned int txt_redo_read_unicode(const char *undo_buf, int *undo_pos, s
return unicode;
}
void txt_do_undo(Text *text)
void txt_do_undo(Text *text, TextUndoBuf *utxt)
{
int op = text->undo_buf[text->undo_pos];
int op = utxt->buf[utxt->pos];
int prev_flags;
unsigned int linep;
unsigned int uni_char;
@@ -2010,11 +1995,11 @@ void txt_do_undo(Text *text)
unsigned short charp;
char *buf;
if (text->undo_pos < 0) {
if (utxt->pos < 0) {
return;
}
text->undo_pos--;
utxt->pos--;
undoing = 1;
@@ -2023,16 +2008,16 @@ void txt_do_undo(Text *text)
case UNDO_INSERT_2:
case UNDO_INSERT_3:
case UNDO_INSERT_4:
text->undo_pos -= op - UNDO_INSERT_1 + 1;
utxt->pos -= op - UNDO_INSERT_1 + 1;
/* get and restore the cursors */
txt_undo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_undo_read_cur(utxt->buf, &utxt->pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
txt_delete_char(text);
txt_delete_char(text, utxt);
text->undo_pos--;
utxt->pos--;
break;
case UNDO_BS_1:
@@ -2040,16 +2025,16 @@ void txt_do_undo(Text *text)
case UNDO_BS_3:
case UNDO_BS_4:
charp = op - UNDO_BS_1 + 1;
uni_char = txt_undo_read_unicode(text->undo_buf, &text->undo_pos, charp);
uni_char = txt_undo_read_unicode(utxt->buf, &utxt->pos, charp);
/* get and restore the cursors */
txt_undo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_undo_read_cur(utxt->buf, &utxt->pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
txt_add_char(text, uni_char);
txt_add_char(text, utxt, uni_char);
text->undo_pos--;
utxt->pos--;
break;
case UNDO_DEL_1:
@@ -2057,50 +2042,50 @@ void txt_do_undo(Text *text)
case UNDO_DEL_3:
case UNDO_DEL_4:
charp = op - UNDO_DEL_1 + 1;
uni_char = txt_undo_read_unicode(text->undo_buf, &text->undo_pos, charp);
uni_char = txt_undo_read_unicode(utxt->buf, &utxt->pos, charp);
/* get and restore the cursors */
txt_undo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_undo_read_cur(utxt->buf, &utxt->pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
txt_add_char(text, uni_char);
txt_add_char(text, utxt, uni_char);
txt_move_left(text, 0);
text->undo_pos--;
utxt->pos--;
break;
case UNDO_DBLOCK:
{
int i;
/* length of the string in the buffer */
linep = txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
linep = txt_undo_read_uint32(utxt->buf, &utxt->pos);
buf = MEM_mallocN(linep + 1, "dblock buffer");
for (i = 0; i < linep; i++) {
buf[(linep - 1) - i] = text->undo_buf[text->undo_pos];
text->undo_pos--;
buf[(linep - 1) - i] = utxt->buf[utxt->pos];
utxt->pos--;
}
buf[i] = 0;
/* skip over the length that was stored again */
text->undo_pos -= 4;
utxt->pos -= 4;
/* Get the cursor positions */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_undo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
/* move cur to location that needs buff inserted */
txt_move_to(text, curln, curc, 0);
txt_insert_buf(text, buf);
txt_insert_buf(text, utxt, buf);
MEM_freeN(buf);
/* restore the cursors */
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
text->undo_pos--;
utxt->pos--;
break;
}
@@ -2108,23 +2093,23 @@ void txt_do_undo(Text *text)
{
int i;
/* length of the string in the buffer */
linep = txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
linep = txt_undo_read_uint32(utxt->buf, &utxt->pos);
/* txt_backspace_char removes utf8-characters, not bytes */
buf = MEM_mallocN(linep + 1, "iblock buffer");
for (i = 0; i < linep; i++) {
buf[(linep - 1) - i] = text->undo_buf[text->undo_pos];
text->undo_pos--;
buf[(linep - 1) - i] = utxt->buf[utxt->pos];
utxt->pos--;
}
buf[i] = 0;
linep = BLI_strlen_utf8(buf);
MEM_freeN(buf);
/* skip over the length that was stored again */
text->undo_pos -= 4;
utxt->pos -= 4;
/* get and restore the cursors */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_undo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
@@ -2140,9 +2125,9 @@ void txt_do_undo(Text *text)
text->flags = prev_flags;
}
txt_delete_selected(text);
txt_delete_selected(text, utxt);
text->undo_pos--;
utxt->pos--;
break;
}
case UNDO_INDENT:
@@ -2151,37 +2136,37 @@ void txt_do_undo(Text *text)
case UNDO_MOVE_LINES_UP:
case UNDO_MOVE_LINES_DOWN:
/* get and restore the cursors */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_undo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
if (op == UNDO_INDENT) {
txt_unindent(text);
txt_unindent(text, utxt);
}
else if (op == UNDO_COMMENT) {
txt_uncomment(text);
txt_uncomment(text, utxt);
}
else if (op == UNDO_DUPLICATE) {
txt_delete_line(text, text->curl->next);
}
else if (op == UNDO_MOVE_LINES_UP) {
txt_move_lines(text, TXT_MOVE_LINE_DOWN);
txt_move_lines(text, utxt, TXT_MOVE_LINE_DOWN);
}
else if (op == UNDO_MOVE_LINES_DOWN) {
txt_move_lines(text, TXT_MOVE_LINE_UP);
txt_move_lines(text, utxt, TXT_MOVE_LINE_UP);
}
text->undo_pos--;
utxt->pos--;
break;
case UNDO_UNINDENT:
case UNDO_UNCOMMENT:
{
void (*txt_prefix_fn)(Text *);
void (*txt_unprefix_fn)(Text *);
void (*txt_prefix_fn)(Text *, TextUndoBuf *);
void (*txt_unprefix_fn)(Text *, TextUndoBuf *);
int count;
int i;
/* Get and restore the cursors */
txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_undo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
@@ -2195,30 +2180,30 @@ void txt_do_undo(Text *text)
txt_unprefix_fn = txt_uncomment;
}
txt_prefix_fn(text);
txt_prefix_fn(text, utxt);
/* Get the count */
count = txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
count = txt_undo_read_uint32(utxt->buf, &utxt->pos);
/* Iterate! */
txt_pop_sel(text);
for (i = 0; i < count; i++) {
txt_move_to(text, txt_undo_read_uint32(text->undo_buf, &text->undo_pos), 0, 0);
txt_move_to(text, txt_undo_read_uint32(utxt->buf, &utxt->pos), 0, 0);
/* Un-un-unindent/comment */
txt_unprefix_fn(text);
txt_unprefix_fn(text, utxt);
}
/* Restore selection */
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
/* Jumo over count */
txt_undo_read_uint32(text->undo_buf, &text->undo_pos);
txt_undo_read_uint32(utxt->buf, &utxt->pos);
/* Jump over closing OP byte */
text->undo_pos--;
utxt->pos--;
break;
}
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos = -1;
utxt->pos = -1;
break;
}
@@ -2226,7 +2211,7 @@ void txt_do_undo(Text *text)
undoing = 0;
}
void txt_do_redo(Text *text)
void txt_do_redo(Text *text, TextUndoBuf *utxt)
{
char op;
char *buf;
@@ -2236,11 +2221,11 @@ void txt_do_redo(Text *text)
unsigned int curln, selln;
unsigned short curc, selc;
text->undo_pos++;
op = text->undo_buf[text->undo_pos];
utxt->pos++;
op = utxt->buf[utxt->pos];
if (!op) {
text->undo_pos--;
utxt->pos--;
return;
}
@@ -2251,35 +2236,35 @@ void txt_do_redo(Text *text)
case UNDO_INSERT_2:
case UNDO_INSERT_3:
case UNDO_INSERT_4:
text->undo_pos++;
utxt->pos++;
/* get and restore the cursors */
txt_redo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_redo_read_cur(utxt->buf, &utxt->pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
charp = op - UNDO_INSERT_1 + 1;
uni_uchar = txt_redo_read_unicode(text->undo_buf, &text->undo_pos, charp);
uni_uchar = txt_redo_read_unicode(utxt->buf, &utxt->pos, charp);
txt_add_char(text, uni_uchar);
txt_add_char(text, utxt, uni_uchar);
break;
case UNDO_BS_1:
case UNDO_BS_2:
case UNDO_BS_3:
case UNDO_BS_4:
text->undo_pos++;
utxt->pos++;
/* get and restore the cursors */
txt_redo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_redo_read_cur(utxt->buf, &utxt->pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
text->undo_pos += op - UNDO_BS_1 + 1;
utxt->pos += op - UNDO_BS_1 + 1;
/* move right so we backspace the correct char */
txt_move_right(text, 0);
txt_backspace_char(text);
txt_backspace_char(text, utxt);
break;
@@ -2287,60 +2272,60 @@ void txt_do_redo(Text *text)
case UNDO_DEL_2:
case UNDO_DEL_3:
case UNDO_DEL_4:
text->undo_pos++;
utxt->pos++;
/* get and restore the cursors */
txt_redo_read_cur(text->undo_buf, &text->undo_pos, &curln, &curc);
txt_redo_read_cur(utxt->buf, &utxt->pos, &curln, &curc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
text->undo_pos += op - UNDO_DEL_1 + 1;
utxt->pos += op - UNDO_DEL_1 + 1;
txt_delete_char(text);
txt_delete_char(text, utxt);
break;
case UNDO_DBLOCK:
text->undo_pos++;
utxt->pos++;
/* get and restore the cursors */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_redo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
/* length of the block */
linep = txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
linep = txt_redo_read_uint32(utxt->buf, &utxt->pos);
text->undo_pos += linep;
utxt->pos += linep;
/* skip over the length that was stored again */
text->undo_pos += 4;
utxt->pos += 4;
txt_delete_sel(text);
txt_delete_sel(text, utxt);
break;
case UNDO_IBLOCK:
text->undo_pos++;
utxt->pos++;
/* get and restore the cursors */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_redo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, curln, curc, 1);
/* length of the block */
linep = txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
linep = txt_redo_read_uint32(utxt->buf, &utxt->pos);
buf = MEM_mallocN(linep + 1, "iblock buffer");
memcpy(buf, &text->undo_buf[text->undo_pos], linep);
text->undo_pos += linep;
memcpy(buf, &utxt->buf[utxt->pos], linep);
utxt->pos += linep;
buf[linep] = 0;
txt_insert_buf(text, buf);
txt_insert_buf(text, utxt, buf);
MEM_freeN(buf);
/* skip over the length that was stored again */
text->undo_pos += 4;
utxt->pos += 4;
break;
@@ -2350,38 +2335,38 @@ void txt_do_redo(Text *text)
case UNDO_DUPLICATE:
case UNDO_MOVE_LINES_UP:
case UNDO_MOVE_LINES_DOWN:
text->undo_pos++;
utxt->pos++;
/* get and restore the cursors */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_redo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
if (op == UNDO_INDENT) {
txt_indent(text);
txt_indent(text, utxt);
}
else if (op == UNDO_COMMENT) {
txt_comment(text);
txt_comment(text, utxt);
}
else if (op == UNDO_UNCOMMENT) {
txt_uncomment(text);
txt_uncomment(text, utxt);
}
else if (op == UNDO_DUPLICATE) {
txt_duplicate_line(text);
txt_duplicate_line(text, utxt);
}
else if (op == UNDO_MOVE_LINES_UP) {
/* offset the cursor by + 1 */
txt_move_to(text, curln + 1, curc, 0);
txt_move_to(text, selln + 1, selc, 1);
txt_move_lines(text, TXT_MOVE_LINE_UP);
txt_move_lines(text, utxt, TXT_MOVE_LINE_UP);
}
else if (op == UNDO_MOVE_LINES_DOWN) {
/* offset the cursor by - 1 */
txt_move_to(text, curln - 1, curc, 0);
txt_move_to(text, selln - 1, selc, 1);
txt_move_lines(text, TXT_MOVE_LINE_DOWN);
txt_move_lines(text, utxt, TXT_MOVE_LINE_DOWN);
}
/* re-restore the cursors since they got moved when redoing */
@@ -2394,24 +2379,24 @@ void txt_do_redo(Text *text)
int count;
int i;
text->undo_pos++;
utxt->pos++;
/* Scan all the stuff described in txt_undo_add_unindent_op */
count = txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
count = txt_redo_read_uint32(utxt->buf, &utxt->pos);
for (i = 0; i < count; i++) {
txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
txt_redo_read_uint32(utxt->buf, &utxt->pos);
}
/* Count again */
txt_redo_read_uint32(text->undo_buf, &text->undo_pos);
txt_redo_read_uint32(utxt->buf, &utxt->pos);
/* Get the selection and re-unindent */
txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc);
txt_redo_read_cursors(utxt->buf, &utxt->pos, &curln, &curc, &selln, &selc);
txt_move_to(text, curln, curc, 0);
txt_move_to(text, selln, selc, 1);
txt_unindent(text);
txt_unindent(text, utxt);
break;
}
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos = -1;
utxt->pos = -1;
break;
}
@@ -2423,16 +2408,16 @@ void txt_do_redo(Text *text)
/* Line editing functions */
/**************************/
void txt_split_curline(Text *text)
void txt_split_curline(Text *text, TextUndoBuf *utxt)
{
TextLine *ins;
char *left, *right;
if (!text->curl) return;
txt_delete_sel(text);
txt_delete_sel(text, utxt);
if (!undoing) txt_undo_add_charop(text, UNDO_INSERT_1, '\n');
if (!undoing) txt_undo_add_charop(text, utxt, UNDO_INSERT_1, '\n');
/* Make the two half strings */
@@ -2504,7 +2489,7 @@ static void txt_combine_lines(Text *text, TextLine *linea, TextLine *lineb)
txt_clean_text(text);
}
void txt_duplicate_line(Text *text)
void txt_duplicate_line(Text *text, TextUndoBuf *utxt)
{
TextLine *textline;
@@ -2517,18 +2502,18 @@ void txt_duplicate_line(Text *text)
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) txt_undo_add_op(text, UNDO_DUPLICATE);
if (!undoing) txt_undo_add_op(text, utxt, UNDO_DUPLICATE);
}
}
void txt_delete_char(Text *text)
void txt_delete_char(Text *text, TextUndoBuf *utxt)
{
unsigned int c = '\n';
if (!text->curl) return;
if (txt_has_sel(text)) { /* deleting a selection */
txt_delete_sel(text);
txt_delete_sel(text, utxt);
txt_make_dirty(text);
return;
}
@@ -2554,24 +2539,24 @@ void txt_delete_char(Text *text)
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) txt_undo_add_charop(text, UNDO_DEL_1, c);
if (!undoing) txt_undo_add_charop(text, utxt, UNDO_DEL_1, c);
}
void txt_delete_word(Text *text)
void txt_delete_word(Text *text, TextUndoBuf *utxt)
{
txt_jump_right(text, true, true);
txt_delete_sel(text);
txt_delete_sel(text, utxt);
txt_make_dirty(text);
}
void txt_backspace_char(Text *text)
void txt_backspace_char(Text *text, TextUndoBuf *utxt)
{
unsigned int c = '\n';
if (!text->curl) return;
if (txt_has_sel(text)) { /* deleting a selection */
txt_delete_sel(text);
txt_delete_sel(text, utxt);
txt_make_dirty(text);
return;
}
@@ -2603,13 +2588,13 @@ void txt_backspace_char(Text *text)
txt_make_dirty(text);
txt_clean_text(text);
if (!undoing) txt_undo_add_charop(text, UNDO_BS_1, c);
if (!undoing) txt_undo_add_charop(text, utxt, UNDO_BS_1, c);
}
void txt_backspace_word(Text *text)
void txt_backspace_word(Text *text, TextUndoBuf *utxt)
{
txt_jump_left(text, true, true);
txt_delete_sel(text);
txt_delete_sel(text, utxt);
txt_make_dirty(text);
}
@@ -2618,17 +2603,17 @@ void txt_backspace_word(Text *text)
* Remember to change this string according to max tab size */
static char tab_to_spaces[] = " ";
static void txt_convert_tab_to_spaces(Text *text)
static void txt_convert_tab_to_spaces(Text *text, TextUndoBuf *utxt)
{
/* sb aims to pad adjust the tab-width needed so that the right number of spaces
* is added so that the indention of the line is the right width (i.e. aligned
* to multiples of TXT_TABSIZE)
*/
const char *sb = &tab_to_spaces[text->curc % TXT_TABSIZE];
txt_insert_buf(text, sb);
txt_insert_buf(text, utxt, sb);
}
static bool txt_add_char_intern(Text *text, unsigned int add, bool replace_tabs)
static bool txt_add_char_intern(Text *text, TextUndoBuf *utxt, unsigned int add, bool replace_tabs)
{
char *tmp, ch[BLI_UTF8_MAX];
size_t add_len;
@@ -2636,19 +2621,19 @@ static bool txt_add_char_intern(Text *text, unsigned int add, bool replace_tabs)
if (!text->curl) return 0;
if (add == '\n') {
txt_split_curline(text);
txt_split_curline(text, utxt);
return true;
}
/* insert spaces rather than tabs */
if (add == '\t' && replace_tabs) {
txt_convert_tab_to_spaces(text);
txt_convert_tab_to_spaces(text, utxt);
return true;
}
txt_delete_sel(text);
txt_delete_sel(text, utxt);
if (!undoing) txt_undo_add_charop(text, UNDO_INSERT_1, add);
if (!undoing) txt_undo_add_charop(text, utxt, UNDO_INSERT_1, add);
add_len = BLI_str_utf8_from_unicode(add, ch);
@@ -2670,23 +2655,23 @@ static bool txt_add_char_intern(Text *text, unsigned int add, bool replace_tabs)
return 1;
}
bool txt_add_char(Text *text, unsigned int add)
bool txt_add_char(Text *text, TextUndoBuf *utxt, unsigned int add)
{
return txt_add_char_intern(text, add, (text->flags & TXT_TABSTOSPACES) != 0);
return txt_add_char_intern(text, utxt, add, (text->flags & TXT_TABSTOSPACES) != 0);
}
bool txt_add_raw_char(Text *text, unsigned int add)
bool txt_add_raw_char(Text *text, TextUndoBuf *utxt, unsigned int add)
{
return txt_add_char_intern(text, add, 0);
return txt_add_char_intern(text, utxt, add, 0);
}
void txt_delete_selected(Text *text)
void txt_delete_selected(Text *text, TextUndoBuf *utxt)
{
txt_delete_sel(text);
txt_delete_sel(text, utxt);
txt_make_dirty(text);
}
bool txt_replace_char(Text *text, unsigned int add)
bool txt_replace_char(Text *text, TextUndoBuf *utxt, unsigned int add)
{
unsigned int del;
size_t del_size = 0, add_size;
@@ -2696,7 +2681,7 @@ bool txt_replace_char(Text *text, unsigned int add)
/* If text is selected or we're at the end of the line just use txt_add_char */
if (text->curc == text->curl->len || txt_has_sel(text) || add == '\n') {
return txt_add_char(text, add);
return txt_add_char(text, utxt, add);
}
del = BLI_str_utf8_as_unicode_and_size(text->curl->line + text->curc, &del_size);
@@ -2724,10 +2709,10 @@ bool txt_replace_char(Text *text, unsigned int add)
/* Should probably create a new op for this */
if (!undoing) {
txt_undo_add_charop(text, UNDO_INSERT_1, add);
txt_undo_add_charop(text, utxt, UNDO_INSERT_1, add);
text->curc -= add_size;
txt_pop_sel(text);
txt_undo_add_charop(text, UNDO_DEL_1, del);
txt_undo_add_charop(text, utxt, UNDO_DEL_1, del);
text->curc += add_size;
txt_pop_sel(text);
}
@@ -2867,7 +2852,7 @@ static void txt_select_unprefix(
/* caller must handle undo */
}
void txt_comment(Text *text)
void txt_comment(Text *text, TextUndoBuf *utxt)
{
const char *prefix = "#";
@@ -2878,11 +2863,11 @@ void txt_comment(Text *text)
txt_select_prefix(text, prefix);
if (!undoing) {
txt_undo_add_op(text, UNDO_COMMENT);
txt_undo_add_op(text, utxt, UNDO_COMMENT);
}
}
void txt_uncomment(Text *text)
void txt_uncomment(Text *text, TextUndoBuf *utxt)
{
const char *prefix = "#";
ListBase line_index_mask;
@@ -2895,13 +2880,13 @@ void txt_uncomment(Text *text)
txt_select_unprefix(text, prefix, &line_index_mask, &line_index_mask_len);
if (!undoing) {
txt_undo_add_unprefix_op(text, UNDO_UNCOMMENT, &line_index_mask, line_index_mask_len);
txt_undo_add_unprefix_op(text, utxt, UNDO_UNCOMMENT, &line_index_mask, line_index_mask_len);
}
BLI_freelistN(&line_index_mask);
}
void txt_indent(Text *text)
void txt_indent(Text *text, TextUndoBuf *utxt)
{
const char *prefix = (text->flags & TXT_TABSTOSPACES) ? tab_to_spaces : "\t";
@@ -2912,11 +2897,11 @@ void txt_indent(Text *text)
txt_select_prefix(text, prefix);
if (!undoing) {
txt_undo_add_op(text, UNDO_INDENT);
txt_undo_add_op(text, utxt, UNDO_INDENT);
}
}
void txt_unindent(Text *text)
void txt_unindent(Text *text, TextUndoBuf *utxt)
{
const char *prefix = (text->flags & TXT_TABSTOSPACES) ? tab_to_spaces : "\t";
ListBase line_index_mask;
@@ -2929,13 +2914,13 @@ void txt_unindent(Text *text)
txt_select_unprefix(text, prefix, &line_index_mask, &line_index_mask_len);
if (!undoing) {
txt_undo_add_unprefix_op(text, UNDO_UNINDENT, &line_index_mask, line_index_mask_len);
txt_undo_add_unprefix_op(text, utxt, UNDO_UNINDENT, &line_index_mask, line_index_mask_len);
}
BLI_freelistN(&line_index_mask);
}
void txt_move_lines(struct Text *text, const int direction)
void txt_move_lines(struct Text *text, TextUndoBuf *utxt, const int direction)
{
TextLine *line_other;
@@ -2962,7 +2947,7 @@ void txt_move_lines(struct Text *text, const int direction)
txt_clean_text(text);
if (!undoing) {
txt_undo_add_op(text, (direction == TXT_MOVE_LINE_DOWN) ? UNDO_MOVE_LINES_DOWN : UNDO_MOVE_LINES_UP);
txt_undo_add_op(text, utxt, (direction == TXT_MOVE_LINE_DOWN) ? UNDO_MOVE_LINES_DOWN : UNDO_MOVE_LINES_UP);
}
}

View File

@@ -3712,15 +3712,11 @@ static void lib_link_text(FileData *fd, Main *main)
static void direct_link_text(FileData *fd, Text *text)
{
TextLine *ln;
text->name = newdataadr(fd, text->name);
text->undo_pos = -1;
text->undo_len = TXT_INIT_UNDO;
text->undo_buf = MEM_mallocN(text->undo_len, "undo buf");
text->compiled = NULL;
#if 0
if (text->flags & TXT_ISEXT) {
BKE_text_reload(text);

View File

@@ -33,10 +33,13 @@
struct SpaceText;
struct ARegion;
struct UndoType;
struct TextUndoBuf;
bool ED_text_region_location_from_cursor(struct SpaceText *st, struct ARegion *ar, const int cursor_co[2], int r_pixel_co[2]);
/* text_undo.c */
void ED_text_undosys_type(struct UndoType *ut);
struct TextUndoBuf *ED_text_undo_push_init(struct bContext *C);
#endif /* __ED_TEXT_H__ */

View File

@@ -611,7 +611,8 @@ static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
str = BKE_reports_string(reports, (G.debug & G_DEBUG) ? RPT_DEBUG : RPT_INFO);
if (str) {
BKE_text_write(txt, str);
TextUndoBuf *utxt = NULL; // FIXME
BKE_text_write(txt, utxt, str);
MEM_freeN(str);
return OPERATOR_FINISHED;

View File

@@ -241,7 +241,7 @@ static void get_suggest_prefix(Text *text, int offset)
texttool_suggest_prefix(line + i, len);
}
static void confirm_suggestion(Text *text)
static void confirm_suggestion(Text *text, TextUndoBuf *utxt)
{
SuggItem *sel;
int i, over = 0;
@@ -260,7 +260,7 @@ static void confirm_suggestion(Text *text)
// for (i = 0; i < skipleft; i++)
// txt_move_left(text, 0);
BLI_assert(memcmp(sel->name, &line[i], over) == 0);
txt_insert_buf(text, sel->name + over);
txt_insert_buf(text, utxt, sel->name + over);
// for (i = 0; i < skipleft; i++)
// txt_move_right(text, 0);
@@ -284,7 +284,8 @@ static int text_autocomplete_invoke(bContext *C, wmOperator *op, const wmEvent *
ED_area_tag_redraw(CTX_wm_area(C));
if (texttool_suggest_first() == texttool_suggest_last()) {
confirm_suggestion(st->text);
TextUndoBuf *utxt = NULL; // FIXME
confirm_suggestion(st->text, utxt);
text_update_line_edited(st->text->curl);
text_autocomplete_free(C, op);
return OPERATOR_FINISHED;
@@ -314,6 +315,8 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
(void)text;
TextUndoBuf *utxt = NULL; // FIXME
if (st->doplugins && texttool_text_is_active(st->text)) {
if (texttool_suggest_first()) tools |= TOOL_SUGG_LIST;
if (texttool_docs_get()) tools |= TOOL_DOCUMENT;
@@ -340,7 +343,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
case MIDDLEMOUSE:
if (event->val == KM_PRESS) {
if (text_do_suggest_select(st, ar)) {
confirm_suggestion(st->text);
confirm_suggestion(st->text, utxt);
text_update_line_edited(st->text->curl);
swallow = 1;
}
@@ -375,7 +378,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
case PADENTER:
if (event->val == KM_PRESS) {
if (tools & TOOL_SUGG_LIST) {
confirm_suggestion(st->text);
confirm_suggestion(st->text, utxt);
text_update_line_edited(st->text->curl);
swallow = 1;
draw = 1;

View File

@@ -731,7 +731,8 @@ static int text_paste_exec(bContext *C, wmOperator *op)
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
txt_insert_buf(text, buf);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_insert_buf(text, utxt, buf);
text_update_edited(text);
MEM_freeN(buf);
@@ -769,9 +770,11 @@ void TEXT_OT_paste(wmOperatorType *ot)
static int text_duplicate_line_exec(bContext *C, wmOperator *UNUSED(op))
{
Text *text = CTX_data_edit_text(C);
txt_duplicate_line(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_duplicate_line(text, utxt);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
/* run the script while editing, evil but useful */
@@ -844,7 +847,9 @@ static int text_cut_exec(bContext *C, wmOperator *UNUSED(op))
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
txt_copy_clipboard(text);
txt_delete_selected(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_delete_selected(text, utxt);
text_update_cursor_moved(C);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@@ -879,12 +884,15 @@ static int text_indent_exec(bContext *C, wmOperator *UNUSED(op))
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
if (txt_has_sel(text)) {
txt_order_cursors(text, false);
txt_indent(text);
txt_indent(text, utxt);
}
else {
txt_add_char(text, utxt, '\t');
}
else
txt_add_char(text, '\t');
text_update_edited(text);
@@ -917,8 +925,10 @@ static int text_unindent_exec(bContext *C, wmOperator *UNUSED(op))
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_order_cursors(text, false);
txt_unindent(text);
txt_unindent(text, utxt);
text_update_edited(text);
@@ -956,14 +966,15 @@ static int text_line_break_exec(bContext *C, wmOperator *UNUSED(op))
// double check tabs/spaces before splitting the line
curts = txt_setcurr_tab_spaces(text, space);
txt_split_curline(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_split_curline(text, utxt);
for (a = 0; a < curts; a++) {
if (text->flags & TXT_TABSTOSPACES) {
txt_add_char(text, ' ');
txt_add_char(text, utxt, ' ');
}
else {
txt_add_char(text, '\t');
txt_add_char(text, utxt, '\t');
}
}
@@ -1003,8 +1014,10 @@ static int text_comment_exec(bContext *C, wmOperator *UNUSED(op))
if (txt_has_sel(text)) {
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_order_cursors(text, false);
txt_comment(text);
txt_comment(text, utxt);
text_update_edited(text);
text_update_cursor_moved(C);
@@ -1039,8 +1052,10 @@ static int text_uncomment_exec(bContext *C, wmOperator *UNUSED(op))
if (txt_has_sel(text)) {
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_order_cursors(text, false);
txt_uncomment(text);
txt_uncomment(text, utxt);
text_update_edited(text);
text_update_cursor_moved(C);
@@ -1292,8 +1307,10 @@ static int move_lines_exec(bContext *C, wmOperator *op)
{
Text *text = CTX_data_edit_text(C);
const int direction = RNA_enum_get(op->ptr, "direction");
txt_move_lines(text, direction);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_move_lines(text, utxt, direction);
text_update_cursor_moved(C);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@@ -1966,6 +1983,7 @@ static int text_delete_exec(bContext *C, wmOperator *op)
Text *text = CTX_data_edit_text(C);
int type = RNA_enum_get(op->ptr, "type");
text_drawcache_tag_update(st, 0);
/* behavior could be changed here,
@@ -1975,11 +1993,13 @@ static int text_delete_exec(bContext *C, wmOperator *op)
else if (type == DEL_NEXT_WORD) type = DEL_NEXT_CHAR;
}
TextUndoBuf *utxt = ED_text_undo_push_init(C);
if (type == DEL_PREV_WORD) {
if (txt_cursor_is_line_start(text)) {
txt_backspace_char(text);
txt_backspace_char(text, utxt);
}
txt_backspace_word(text);
txt_backspace_word(text, utxt);
}
else if (type == DEL_PREV_CHAR) {
@@ -1995,13 +2015,13 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
txt_backspace_char(text);
txt_backspace_char(text, utxt);
}
else if (type == DEL_NEXT_WORD) {
if (txt_cursor_is_line_end(text)) {
txt_delete_char(text);
txt_delete_char(text, utxt);
}
txt_delete_word(text);
txt_delete_word(text, utxt);
}
else if (type == DEL_NEXT_CHAR) {
@@ -2017,7 +2037,7 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
txt_delete_char(text);
txt_delete_char(text, utxt);
}
text_update_line_edited(text->curl);
@@ -2870,16 +2890,18 @@ static int text_insert_exec(bContext *C, wmOperator *op)
str = RNA_string_get_alloc(op->ptr, "text", NULL, 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
if (st && st->overwrite) {
while (str[i]) {
code = BLI_str_utf8_as_unicode_step(str, &i);
done |= txt_replace_char(text, code);
done |= txt_replace_char(text, utxt, code);
}
}
else {
while (str[i]) {
code = BLI_str_utf8_as_unicode_step(str, &i);
done |= txt_add_char(text, code);
done |= txt_add_char(text, utxt, code);
}
}
@@ -2988,7 +3010,8 @@ static int text_find_and_replace(bContext *C, wmOperator *op, short mode)
if (found) {
if (mode == TEXT_REPLACE) {
txt_insert_buf(text, st->replacestr);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_insert_buf(text, utxt, st->replacestr);
if (text->curl && text->curl->format) {
MEM_freeN(text->curl->format);
text->curl->format = NULL;

View File

@@ -48,6 +48,7 @@
#include "ED_text.h"
#include "ED_curve.h"
#include "ED_screen.h"
#include "ED_undo.h"
#include "UI_interface.h"
#include "UI_resources.h"
@@ -63,11 +64,6 @@
/* -------------------------------------------------------------------- */
/** \name Implements ED Undo System
* \{ */
typedef struct TextUndoBuf {
char *buf;
int len;
int pos;
} TextUndoBuf;
typedef struct TextUndoStep {
UndoStep step;
@@ -87,29 +83,33 @@ static bool text_undosys_poll(bContext *C)
return true;
}
static bool text_undosys_step_encode(struct bContext *C, UndoStep *us_p)
static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
{
TextUndoStep *us = (TextUndoStep *)us_p;
BLI_assert(BLI_array_is_zeroed(&us->data, 1));
UNUSED_VARS(C);
/* XXX, use to set the undo type only. */
us->data.buf = NULL;
us->data.len = 0;
us->data.pos = -1;
}
static bool text_undosys_step_encode(struct bContext *C, UndoStep *us_p)
{
TextUndoStep *us = (TextUndoStep *)us_p;
Text *text = CTX_data_edit_text(C);
/* No undo data was generated. Hint, use global undo here. */
if (text->undo_pos == -1) {
if ((us->data.pos == -1) || (us->data.buf == NULL)) {
return false;
}
us->text_ref.ptr = text;
us->data.buf = text->undo_buf;
us->data.pos = text->undo_pos;
us->data.len = text->undo_len;
text->undo_buf = NULL;
text->undo_len = 0;
text->undo_pos = -1;
us->step.data_size = text->undo_len;
us->step.data_size = us->data.len;
return true;
}
@@ -119,20 +119,15 @@ static void text_undosys_step_decode(struct bContext *C, UndoStep *us_p, int dir
TextUndoStep *us = (TextUndoStep *)us_p;
Text *text = us->text_ref.ptr;
/* TODO(campbell): undo_system: move undo out of Text data block. */
text->undo_buf = us->data.buf;
text->undo_len = us->data.len;
if (dir < 0) {
text->undo_pos = us->data.pos;
txt_do_undo(text);
TextUndoBuf data = us->data;
txt_do_undo(text, &data);
}
else {
text->undo_pos = -1;
txt_do_redo(text);
TextUndoBuf data = us->data;
data.pos = -1;
txt_do_redo(text, &data);
}
text->undo_buf = NULL;
text->undo_len = 0;
text->undo_pos = -1;
text_update_edited(text);
text_update_cursor_moved(C);
@@ -159,6 +154,7 @@ void ED_text_undosys_type(UndoType *ut)
{
ut->name = "Text";
ut->poll = text_undosys_poll;
ut->step_encode_init = text_undosys_step_encode_init;
ut->step_encode = text_undosys_step_encode;
ut->step_decode = text_undosys_step_decode;
ut->step_free = text_undosys_step_free;
@@ -166,9 +162,24 @@ void ED_text_undosys_type(UndoType *ut)
ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref;
ut->mode = BKE_UNDOTYPE_MODE_ACCUMULATE;
ut->use_context = true;
ut->use_context = false;
ut->step_size = sizeof(TextUndoStep);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
/* Use operator system to finish the undo step. */
TextUndoBuf *ED_text_undo_push_init(bContext *C)
{
UndoStack *ustack = ED_undo_stack_get();
UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT);
TextUndoStep *us = (TextUndoStep *)us_p;
return &us->data;
}
/** \} */

View File

@@ -62,7 +62,7 @@ void ED_undosys_type_init(void)
BKE_UNDOSYS_TYPE_PAINTCURVE = BKE_undosys_type_append(ED_paintcurve_undosys_type);
/* Text editor */
BKE_undosys_type_append(ED_text_undosys_type);
BKE_UNDOSYS_TYPE_TEXT = BKE_undosys_type_append(ED_text_undosys_type);
/* Keep global undo last (as a fallback). */
BKE_UNDOSYS_TYPE_MEMFILE = BKE_undosys_type_append(ED_memfile_undosys_type);

View File

@@ -59,10 +59,6 @@ typedef struct Text {
TextLine *curl, *sell;
int curc, selc;
char *undo_buf;
void *pad;
int undo_pos, undo_len;
double mtime;
} Text;

View File

@@ -40,13 +40,13 @@
static void rna_Text_clear(Text *text)
{
BKE_text_clear(text);
BKE_text_clear(text, NULL);
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);
}
static void rna_Text_write(Text *text, const char *str)
{
BKE_text_write(text, str);
BKE_text_write(text, NULL, str);
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);
}