Blender V5.3
text_undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cerrno>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_text_types.h"
15
16#include "BLI_array_store.hh"
17#include "BLI_array_utils_c.hh" /* For `BLI_array_is_zeroed`. */
18
19#include "BKE_context.hh"
20#include "BKE_main.hh"
21#include "BKE_text.h"
22#include "BKE_undo_system.hh"
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
27#include "ED_screen.hh"
28#include "ED_text.hh"
29#include "ED_undo.hh"
30
31#include "text_intern.hh"
32
33namespace blender {
34
35/* -------------------------------------------------------------------- */
38
39#define ARRAY_CHUNK_SIZE 128
40
50
51static void text_state_encode(TextState *state, Text *text, BArrayStore *buffer_store)
52{
53 size_t buf_len = 0;
54 uchar *buf = reinterpret_cast<uchar *>(txt_to_buf_for_undo(text, &buf_len));
55 state->buf_array_state = BLI_array_store_state_add(buffer_store, buf, buf_len, nullptr);
56 MEM_delete(buf);
57
58 state->cursor_line = txt_get_span(text->lines.first(), text->curl);
59 state->cursor_column = text->curc;
60
61 if (txt_has_sel(text)) {
62 state->cursor_line_select = (text->curl == text->sell) ?
63 state->cursor_line :
64 txt_get_span(text->lines.first(), text->sell);
65 state->cursor_column_select = text->selc;
66 }
67 else {
68 state->cursor_line_select = state->cursor_line;
69 state->cursor_column_select = state->cursor_column;
70 }
71}
72
74{
75 size_t buf_len;
76 {
77 const uchar *buf = static_cast<const uchar *>(
78 BLI_array_store_state_data_get_alloc(state->buf_array_state, &buf_len));
79 txt_from_buf_for_undo(text, reinterpret_cast<const char *>(buf), buf_len);
80 MEM_delete(buf);
81 }
82
83 const bool has_select = ((state->cursor_line != state->cursor_line_select) ||
84 (state->cursor_column != state->cursor_column_select));
85 if (has_select) {
86 txt_move_to(text, state->cursor_line_select, state->cursor_column_select, false);
87 }
88 txt_move_to(text, state->cursor_line, state->cursor_column, has_select);
89}
90
92
93/* -------------------------------------------------------------------- */
96
99 UndoRefID_Text text_ref;
105};
106
107static struct {
109 int users;
110} g_text_buffers = {nullptr};
111
113{
115 if (g_text_buffers.buffer_store == nullptr) {
117 }
118 g_text_buffers.users += 1;
119 const size_t total_size_prev = BLI_array_store_calc_size_compacted_get(
120 g_text_buffers.buffer_store);
121
122 text_state_encode(state, text, g_text_buffers.buffer_store);
123
124 return BLI_array_store_calc_size_compacted_get(g_text_buffers.buffer_store) - total_size_prev;
125}
126
127static bool text_undosys_poll(bContext * /*C*/)
128{
129 /* Only use when operators initialized. */
130 UndoStack *ustack = ED_undo_stack_get();
131 return (ustack->step_init && (ustack->step_init->type == BKE_UNDOSYS_TYPE_TEXT));
132}
133
135{
136 TextUndoStep *us = reinterpret_cast<TextUndoStep *>(us_p);
138
139 Text *text = CTX_data_edit_text(C);
140
141 /* Avoid writing the initial state where possible,
142 * failing to do this won't cause bugs, it's just inefficient. */
143 bool write_init = true;
144 UndoStack *ustack = ED_undo_stack_get();
145 if (ustack->step_active) {
146 if (ustack->step_active->type == BKE_UNDOSYS_TYPE_TEXT) {
147 TextUndoStep *us_active = reinterpret_cast<TextUndoStep *>(ustack->step_active);
148 if (STREQ(text->id.name, us_active->text_ref.name)) {
149 write_init = false;
150 }
151 }
152 }
153
154 if (write_init) {
156 }
157 us->text_ref.ptr = text;
158}
159
160static bool text_undosys_step_encode(bContext *C, Main * /*bmain*/, UndoStep *us_p)
161{
162 TextUndoStep *us = reinterpret_cast<TextUndoStep *>(us_p);
163
164 Text *text = us->text_ref.ptr;
167
169
170 us_p->is_applied = true;
171
172 return true;
173}
174
176 bContext *C, Main * /*bmain*/, UndoStep *us_p, const eUndoStepDir dir, bool is_final)
177{
178 BLI_assert(dir != STEP_INVALID);
179
180 TextUndoStep *us = reinterpret_cast<TextUndoStep *>(us_p);
181 Text *text = us->text_ref.ptr;
182
184 if ((us->states[0].buf_array_state != nullptr) && (dir == STEP_UNDO) && !is_final) {
185 state = &us->states[0];
186 }
187 else {
188 state = &us->states[1];
189 }
190
192
194 if (st) {
195 /* Not essential, always show text being undo where possible. */
196 st->text = text;
197 }
201}
202
204{
205 TextUndoStep *us = reinterpret_cast<TextUndoStep *>(us_p);
206
207 for (int i = 0; i < ARRAY_SIZE(us->states); i++) {
208 TextState *state = &us->states[i];
209 if (state->buf_array_state) {
210 BLI_array_store_state_remove(g_text_buffers.buffer_store, state->buf_array_state);
211 g_text_buffers.users -= 1;
212 if (g_text_buffers.users == 0) {
214 g_text_buffers.buffer_store = nullptr;
215 }
216 }
217 }
218}
219
221 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
222 void *user_data)
223{
224 TextUndoStep *us = reinterpret_cast<TextUndoStep *>(us_p);
225 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->text_ref)));
226}
227
243
245
246/* -------------------------------------------------------------------- */
249
251{
252 UndoStack *ustack = ED_undo_stack_get();
253 Main *bmain = CTX_data_main(C);
254 wmWindowManager *wm = bmain->wm.first();
255 if (wm->op_undo_depth <= 1) {
257 ustack, C, nullptr, BKE_UNDOSYS_TYPE_TEXT);
258 return us_p;
259 }
260 return nullptr;
261}
262
264
265} // namespace blender
Efficient in-memory storage of multiple similar arrays.
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NA_EDITED
Definition WM_types.hh:634
#define NC_TEXT
Definition WM_types.hh:399
#define UNUSED_VARS_NDEBUG(...)
#define STREQ(a, b)
#define ARRAY_CHUNK_SIZE
const int state
BMesh Iterators.
static void text_undosys_step_decode(bContext *C, Main *, UndoStep *us_p, const eUndoStepDir dir, bool is_final)
Definition text_undo.cc:175
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int txt_get_span(const struct TextLine *from, const struct TextLine *to)
static struct blender::@372137351376374056245260017074100203077155045231 g_text_buffers
void BLI_array_store_state_remove(BArrayStore *bs, BArrayState *state)
void space_text_drawcache_tag_update(SpaceText *st, const bool full)
Definition text_draw.cc:757
UndoStack * ED_undo_stack_get()
Definition ed_undo.cc:690
@ UNDOTYPE_FLAG_DECODE_ACTIVE_STEP
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
BArrayState * BLI_array_store_state_add(BArrayStore *bs, const void *data, size_t data_len, const BArrayState *state_reference)
void space_text_update_cursor_moved(bContext *C)
void ED_text_undosys_type(UndoType *ut)
Definition text_undo.cc:228
static void text_state_encode(TextState *state, Text *text, BArrayStore *buffer_store)
Definition text_undo.cc:51
bool txt_has_sel(const struct Text *text)
static void text_state_decode(TextState *state, Text *text)
Definition text_undo.cc:73
Text * CTX_data_edit_text(const bContext *C)
char * txt_to_buf_for_undo(struct Text *text, size_t *r_buf_len) ATTR_NONNULL(1
Definition text.cc:1349
void * BLI_array_store_state_data_get_alloc(const BArrayState *state, size_t *r_data_len)
SpaceText * CTX_wm_space_text(const bContext *C)
unsigned char uchar
Definition BKE_text.h:58
static bool text_undosys_poll(bContext *)
Definition text_undo.cc:127
static void text_undosys_step_encode_init(bContext *C, UndoStep *us_p)
Definition text_undo.cc:134
const UndoType * BKE_UNDOSYS_TYPE_TEXT
BArrayStore * BLI_array_store_create(unsigned int stride, unsigned int chunk_count)
void txt_from_buf_for_undo(struct Text *text, const char *buf, size_t buf_len) ATTR_NONNULL(1
Definition text.cc:1366
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static size_t text_undosys_step_encode_to_state(TextState *state, Text *text)
Definition text_undo.cc:112
static void text_undosys_step_free(UndoStep *us_p)
Definition text_undo.cc:203
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
size_t BLI_array_store_calc_size_compacted_get(const BArrayStore *bs)
Main * CTX_data_main(const bContext *C)
UndoStep * ED_text_undo_push_init(bContext *C)
Definition text_undo.cc:250
static bool text_undosys_step_encode(bContext *C, Main *, UndoStep *us_p)
Definition text_undo.cc:160
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
static void text_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
Definition text_undo.cc:220
void BLI_array_store_destroy(BArrayStore *bs)
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, bool sel)
Definition text.cc:1104
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
BArrayState * buf_array_state
Definition text_undo.cc:45
UndoRefID_Text text_ref
Definition text_undo.cc:99
TextState states[2]
Definition text_undo.cc:104
const UndoType * type
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier
void(*) step_encode_init(bContext *C, UndoStep *us)