Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/text.c
| Show First 20 Lines • Show All 1,890 Lines • ▼ Show 20 Lines | else { | ||||
| txt_undo_store_uint32(text->undo_buf, &text->undo_pos, c); | txt_undo_store_uint32(text->undo_buf, &text->undo_pos, c); | ||||
| text->undo_buf[text->undo_pos] = op_start + 3; | text->undo_buf[text->undo_pos] = op_start + 3; | ||||
| } | } | ||||
| text->undo_buf[text->undo_pos + 1] = 0; | text->undo_buf[text->undo_pos + 1] = 0; | ||||
| } | } | ||||
| // extends Link | |||||
| typedef struct LinkInt { | |||||
| struct LinkInt* next; | |||||
| struct LinkInt* prev; | |||||
| int content; | |||||
| } LinkInt; | |||||
| // unindentLines points to a ListBase composed of LinkInt elements, listing the numbers | |||||
| // of the lines that should not be indented back. | |||||
| static void txt_undo_add_unindent_op(Text *text, ListBase* unindentLines) { | |||||
| int count = BLI_listbase_count(unindentLines); | |||||
| // OP byte + UInt32 count + counted UInt32 line numbers + UInt32 count + | |||||
| // + 12-bytes selection + OP byte | |||||
| if (!max_undo_test(text, 1 + 4 + count*4 + 4 + 12 + 1)) { | |||||
| return; | |||||
| } | |||||
| // Opening buffer sequence with OP | |||||
| text->undo_pos++; | |||||
| text->undo_buf[text->undo_pos] = UNDO_UNINDENT; | |||||
| text->undo_pos++; | |||||
| // Adding number of line numbers to read | |||||
| txt_undo_store_uint32(text->undo_buf, &text->undo_pos, BLI_listbase_count(unindentLines)); | |||||
| // Adding linenumbers of lines that shall not be indented if undoing | |||||
| LinkInt* iData; | |||||
| for (iData = (LinkInt*)unindentLines->first; iData; iData = iData->next) { | |||||
| txt_undo_store_uint32(text->undo_buf, &text->undo_pos, iData->content); | |||||
| printf("%d\n", iData->content); | |||||
| } | |||||
| // Adding number of line numbers to read again | |||||
| txt_undo_store_uint32(text->undo_buf, &text->undo_pos, BLI_listbase_count(unindentLines)); | |||||
| // Adding current selection | |||||
| txt_undo_store_cursors(text); | |||||
| // Closing with OP (same as above) | |||||
| text->undo_buf[text->undo_pos] = UNDO_UNINDENT; | |||||
| // Marking as last undo operation | |||||
| text->undo_buf[text->undo_pos + 1] = 0; | |||||
| } | |||||
| static unsigned short txt_undo_read_uint16(const char *undo_buf, int *undo_pos) | static unsigned short txt_undo_read_uint16(const char *undo_buf, int *undo_pos) | ||||
| { | { | ||||
| unsigned short val; | unsigned short val; | ||||
| val = undo_buf[*undo_pos]; (*undo_pos)--; | val = undo_buf[*undo_pos]; (*undo_pos)--; | ||||
| val = (val << 8) + undo_buf[*undo_pos]; (*undo_pos)--; | val = (val << 8) + undo_buf[*undo_pos]; (*undo_pos)--; | ||||
| return val; | return val; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 278 Lines • ▼ Show 20 Lines | case UNDO_IBLOCK: | ||||
| text->flags = prev_flags; | text->flags = prev_flags; | ||||
| } | } | ||||
| txt_delete_selected(text); | txt_delete_selected(text); | ||||
| text->undo_pos--; | text->undo_pos--; | ||||
| break; | break; | ||||
| case UNDO_INDENT: | case UNDO_INDENT: | ||||
| case UNDO_UNINDENT: | |||||
| case UNDO_COMMENT: | case UNDO_COMMENT: | ||||
| case UNDO_UNCOMMENT: | case UNDO_UNCOMMENT: | ||||
| case UNDO_DUPLICATE: | case UNDO_DUPLICATE: | ||||
| case UNDO_MOVE_LINES_UP: | case UNDO_MOVE_LINES_UP: | ||||
| case UNDO_MOVE_LINES_DOWN: | case UNDO_MOVE_LINES_DOWN: | ||||
| /* get and restore the cursors */ | /* get and restore the cursors */ | ||||
| txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc); | txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc); | ||||
| txt_move_to(text, curln, curc, 0); | txt_move_to(text, curln, curc, 0); | ||||
| txt_move_to(text, selln, selc, 1); | txt_move_to(text, selln, selc, 1); | ||||
| if (op == UNDO_INDENT) { | if (op == UNDO_INDENT) { | ||||
| txt_unindent(text); | txt_unindent(text); | ||||
| } | } | ||||
| else if (op == UNDO_UNINDENT) { | |||||
| txt_indent(text); | |||||
| } | |||||
| else if (op == UNDO_COMMENT) { | else if (op == UNDO_COMMENT) { | ||||
| txt_uncomment(text); | txt_uncomment(text); | ||||
| } | } | ||||
| else if (op == UNDO_UNCOMMENT) { | else if (op == UNDO_UNCOMMENT) { | ||||
| txt_comment(text); | txt_comment(text); | ||||
| } | } | ||||
| else if (op == UNDO_DUPLICATE) { | else if (op == UNDO_DUPLICATE) { | ||||
| txt_delete_line(text, text->curl->next); | txt_delete_line(text, text->curl->next); | ||||
| } | } | ||||
| else if (op == UNDO_MOVE_LINES_UP) { | else if (op == UNDO_MOVE_LINES_UP) { | ||||
| txt_move_lines(text, TXT_MOVE_LINE_DOWN); | txt_move_lines(text, TXT_MOVE_LINE_DOWN); | ||||
| } | } | ||||
| else if (op == UNDO_MOVE_LINES_DOWN) { | else if (op == UNDO_MOVE_LINES_DOWN) { | ||||
| txt_move_lines(text, TXT_MOVE_LINE_UP); | txt_move_lines(text, TXT_MOVE_LINE_UP); | ||||
| } | } | ||||
| text->undo_pos--; | text->undo_pos--; | ||||
| break; | break; | ||||
| case UNDO_UNINDENT: | |||||
| // Get and restore the cursors | |||||
| txt_undo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc); | |||||
| txt_move_to(text, curln, curc, 0); | |||||
| txt_move_to(text, selln, selc, 1); | |||||
| // Un-unindent | |||||
| txt_indent(text); | |||||
| // Get the count | |||||
| int count = txt_undo_read_uint32(text->undo_buf, &text->undo_pos); | |||||
| // Iterate! | |||||
| txt_pop_sel(text); | |||||
| int i; | |||||
| for (i=0; i<count; i++) { | |||||
| txt_move_to(text, txt_undo_read_uint32(text->undo_buf, &text->undo_pos), 0, 0); | |||||
| // Un-un-unindent | |||||
| txt_unindent(text); | |||||
| } | |||||
| // 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); | |||||
| // Jump over closing OP byte | |||||
| text->undo_pos--; | |||||
| break; | |||||
| default: | default: | ||||
| //XXX error("Undo buffer error - resetting"); | //XXX error("Undo buffer error - resetting"); | ||||
| text->undo_pos = -1; | text->undo_pos = -1; | ||||
| break; | break; | ||||
| } | } | ||||
| undoing = 0; | undoing = 0; | ||||
| ▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | case UNDO_IBLOCK: | ||||
| MEM_freeN(buf); | MEM_freeN(buf); | ||||
| /* skip over the length that was stored again */ | /* skip over the length that was stored again */ | ||||
| text->undo_pos += 4; | text->undo_pos += 4; | ||||
| break; | break; | ||||
| case UNDO_INDENT: | case UNDO_INDENT: | ||||
| case UNDO_UNINDENT: | |||||
| case UNDO_COMMENT: | case UNDO_COMMENT: | ||||
| case UNDO_UNCOMMENT: | case UNDO_UNCOMMENT: | ||||
| case UNDO_DUPLICATE: | case UNDO_DUPLICATE: | ||||
| case UNDO_MOVE_LINES_UP: | case UNDO_MOVE_LINES_UP: | ||||
| case UNDO_MOVE_LINES_DOWN: | case UNDO_MOVE_LINES_DOWN: | ||||
| text->undo_pos++; | text->undo_pos++; | ||||
| /* get and restore the cursors */ | /* get and restore the cursors */ | ||||
| txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc); | txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc); | ||||
| txt_move_to(text, curln, curc, 0); | txt_move_to(text, curln, curc, 0); | ||||
| txt_move_to(text, selln, selc, 1); | txt_move_to(text, selln, selc, 1); | ||||
| if (op == UNDO_INDENT) { | if (op == UNDO_INDENT) { | ||||
| txt_indent(text); | txt_indent(text); | ||||
| } | } | ||||
| else if (op == UNDO_UNINDENT) { | |||||
| txt_unindent(text); | |||||
| } | |||||
| else if (op == UNDO_COMMENT) { | else if (op == UNDO_COMMENT) { | ||||
| txt_comment(text); | txt_comment(text); | ||||
| } | } | ||||
| else if (op == UNDO_UNCOMMENT) { | else if (op == UNDO_UNCOMMENT) { | ||||
| txt_uncomment(text); | txt_uncomment(text); | ||||
| } | } | ||||
| else if (op == UNDO_DUPLICATE) { | else if (op == UNDO_DUPLICATE) { | ||||
| txt_duplicate_line(text); | txt_duplicate_line(text); | ||||
| Show All 13 Lines | case UNDO_MOVE_LINES_DOWN: | ||||
| txt_move_lines(text, TXT_MOVE_LINE_DOWN); | txt_move_lines(text, TXT_MOVE_LINE_DOWN); | ||||
| } | } | ||||
| /* re-restore the cursors since they got moved when redoing */ | /* re-restore the cursors since they got moved when redoing */ | ||||
| txt_move_to(text, curln, curc, 0); | txt_move_to(text, curln, curc, 0); | ||||
| txt_move_to(text, selln, selc, 1); | txt_move_to(text, selln, selc, 1); | ||||
| break; | break; | ||||
| case UNDO_UNINDENT: | |||||
| text->undo_pos++; | |||||
| // Scan all the stuff described in txt_undo_add_unindent_op | |||||
| int count = txt_redo_read_uint32(text->undo_buf, &text->undo_pos); | |||||
| int i; | |||||
| for (i=0; i<count; i++) { | |||||
| txt_redo_read_uint32(text->undo_buf, &text->undo_pos); | |||||
| } | |||||
| // Count again | |||||
| txt_redo_read_uint32(text->undo_buf, &text->undo_pos); | |||||
| // Get the selection and re-unindent | |||||
| txt_redo_read_cursors(text->undo_buf, &text->undo_pos, &curln, &curc, &selln, &selc); | |||||
| txt_move_to(text, curln, curc, 0); | |||||
| txt_move_to(text, selln, selc, 1); | |||||
| txt_unindent(text); | |||||
| break; | |||||
| default: | default: | ||||
| //XXX error("Undo buffer error - resetting"); | //XXX error("Undo buffer error - resetting"); | ||||
| text->undo_pos = -1; | text->undo_pos = -1; | ||||
| break; | break; | ||||
| } | } | ||||
| undoing = 0; | undoing = 0; | ||||
| ▲ Show 20 Lines • Show All 384 Lines • ▼ Show 20 Lines | |||||
| void txt_unindent(Text *text) | void txt_unindent(Text *text) | ||||
| { | { | ||||
| int num = 0; | int num = 0; | ||||
| const char *remove = "\t"; | const char *remove = "\t"; | ||||
| int indentlen = 1; | int indentlen = 1; | ||||
| bool unindented_first = false; | bool unindented_first = false; | ||||
| //List of lines that are already at indent level 0, to store them later into the undo buffer | |||||
| ListBase unindentLines = {NULL, NULL}; | |||||
| /* hardcoded: TXT_TABSIZE = 4 spaces: */ | /* hardcoded: TXT_TABSIZE = 4 spaces: */ | ||||
| int spaceslen = TXT_TABSIZE; | int spaceslen = TXT_TABSIZE; | ||||
| if (ELEM(NULL, text->curl, text->sell)) { | if (ELEM(NULL, text->curl, text->sell)) { | ||||
| return; | return; | ||||
| } | } | ||||
| /* insert spaces rather than tabs */ | /* insert spaces rather than tabs */ | ||||
| if (text->flags & TXT_TABSTOSPACES) { | if (text->flags & TXT_TABSTOSPACES) { | ||||
| remove = tab_to_spaces; | remove = tab_to_spaces; | ||||
| indentlen = spaceslen; | indentlen = spaceslen; | ||||
| } | } | ||||
| while (true) { | while (true) { | ||||
| bool changed = false; | bool changed = false; | ||||
| if (STREQLEN(text->curl->line, remove, indentlen)) { | if (STREQLEN(text->curl->line, remove, indentlen)) { | ||||
| if (num == 0) | if (num == 0) | ||||
| unindented_first = true; | unindented_first = true; | ||||
| text->curl->len -= indentlen; | text->curl->len -= indentlen; | ||||
| memmove(text->curl->line, text->curl->line + indentlen, text->curl->len + 1); | memmove(text->curl->line, text->curl->line + indentlen, text->curl->len + 1); | ||||
| changed = true; | changed = true; | ||||
| } | } | ||||
| else { | |||||
| if (!undoing) { | |||||
| // Create list element for 0 indent line | |||||
| LinkInt* lineLink = (LinkInt*)MEM_mallocN(sizeof(LinkInt), "lineLink"); | |||||
| lineLink->content = txt_get_span(text->lines.first, text->curl); | |||||
| BLI_addtail(&unindentLines, lineLink); | |||||
| } | |||||
| } | |||||
| txt_make_dirty(text); | txt_make_dirty(text); | ||||
| txt_clean_text(text); | txt_clean_text(text); | ||||
| if (text->curl == text->sell) { | if (text->curl == text->sell) { | ||||
| if (changed) | if (changed) | ||||
| text->selc = MAX2(text->selc - indentlen, 0); | text->selc = MAX2(text->selc - indentlen, 0); | ||||
| break; | break; | ||||
| Show All 9 Lines | if (unindented_first) | ||||
| text->curc = MAX2(text->curc - indentlen, 0); | text->curc = MAX2(text->curc - indentlen, 0); | ||||
| while (num > 0) { | while (num > 0) { | ||||
| text->curl = text->curl->prev; | text->curl = text->curl->prev; | ||||
| num--; | num--; | ||||
| } | } | ||||
| if (!undoing) { | if (!undoing) { | ||||
| txt_undo_add_op(text, UNDO_UNINDENT); | txt_undo_add_unindent_op(text, &unindentLines); | ||||
| } | } | ||||
| BLI_freelistN(&unindentLines); | |||||
| } | } | ||||
| void txt_comment(Text *text) | void txt_comment(Text *text) | ||||
| { | { | ||||
| int len, num; | int len, num; | ||||
| char *tmp; | char *tmp; | ||||
| char add = '#'; | char add = '#'; | ||||
| ▲ Show 20 Lines • Show All 266 Lines • Show Last 20 Lines | |||||