Blender  V2.93
text_undo.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <errno.h>
22 #include <string.h>
23 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_text_types.h"
27 
28 #include "BLI_array_store.h"
29 #include "BLI_array_utils.h"
30 
31 #include "BLT_translation.h"
32 
33 #include "PIL_time.h"
34 
35 #include "BKE_context.h"
36 #include "BKE_main.h"
37 #include "BKE_report.h"
38 #include "BKE_text.h"
39 #include "BKE_undo_system.h"
40 
41 #include "WM_api.h"
42 #include "WM_types.h"
43 
44 #include "ED_curve.h"
45 #include "ED_screen.h"
46 #include "ED_text.h"
47 #include "ED_undo.h"
48 
49 #include "UI_interface.h"
50 #include "UI_resources.h"
51 
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54 
55 #include "text_format.h"
56 #include "text_intern.h"
57 
58 /* -------------------------------------------------------------------- */
62 #define ARRAY_CHUNK_SIZE 128
63 
67 typedef struct TextState {
69 
73 
75 {
76  int buf_len = 0;
77  uchar *buf = (uchar *)txt_to_buf_for_undo(text, &buf_len);
78  state->buf_array_state = BLI_array_store_state_add(buffer_store, buf, buf_len, NULL);
79  MEM_freeN(buf);
80 
81  state->cursor_line = txt_get_span(text->lines.first, text->curl);
82  state->cursor_column = text->curc;
83 
84  if (txt_has_sel(text)) {
85  state->cursor_line_select = (text->curl == text->sell) ?
86  state->cursor_line :
87  txt_get_span(text->lines.first, text->sell);
88  state->cursor_column_select = text->selc;
89  }
90  else {
91  state->cursor_line_select = state->cursor_line;
92  state->cursor_column_select = state->cursor_column;
93  }
94 }
95 
96 static void text_state_decode(TextState *state, Text *text)
97 {
98  size_t buf_len;
99  {
100  const uchar *buf = BLI_array_store_state_data_get_alloc(state->buf_array_state, &buf_len);
101  txt_from_buf_for_undo(text, (const char *)buf, buf_len);
102  MEM_freeN((void *)buf);
103  }
104 
105  const bool has_select = ((state->cursor_line != state->cursor_line_select) ||
106  (state->cursor_column != state->cursor_column_select));
107  if (has_select) {
108  txt_move_to(text, state->cursor_line_select, state->cursor_column_select, false);
109  }
110  txt_move_to(text, state->cursor_line, state->cursor_column, has_select);
111 }
112 
115 /* -------------------------------------------------------------------- */
119 typedef struct TextUndoStep {
121  UndoRefID_Text text_ref;
128 
129 static struct {
131  int users;
133 
135 {
137  if (g_text_buffers.buffer_store == NULL) {
139  }
140  g_text_buffers.users += 1;
141  const size_t total_size_prev = BLI_array_store_calc_size_compacted_get(
142  g_text_buffers.buffer_store);
143 
144  text_state_encode(state, text, g_text_buffers.buffer_store);
145 
146  return BLI_array_store_calc_size_compacted_get(g_text_buffers.buffer_store) - total_size_prev;
147 }
148 
150 {
151  /* Only use when operators initialized. */
152  UndoStack *ustack = ED_undo_stack_get();
153  return (ustack->step_init && (ustack->step_init->type == BKE_UNDOSYS_TYPE_TEXT));
154 }
155 
157 {
158  TextUndoStep *us = (TextUndoStep *)us_p;
160 
161  Text *text = CTX_data_edit_text(C);
162 
163  /* Avoid writing the initial state where possible,
164  * failing to do this won't cause bugs, it's just inefficient. */
165  bool write_init = true;
166  UndoStack *ustack = ED_undo_stack_get();
167  if (ustack->step_active) {
168  if (ustack->step_active->type == BKE_UNDOSYS_TYPE_TEXT) {
169  TextUndoStep *us_active = (TextUndoStep *)ustack->step_active;
170  if (STREQ(text->id.name, us_active->text_ref.name)) {
171  write_init = false;
172  }
173  }
174  }
175 
176  if (write_init) {
178  }
179  us->text_ref.ptr = text;
180 }
181 
182 static bool text_undosys_step_encode(struct bContext *C,
183  struct Main *UNUSED(bmain),
184  UndoStep *us_p)
185 {
186  TextUndoStep *us = (TextUndoStep *)us_p;
187 
188  Text *text = us->text_ref.ptr;
189  BLI_assert(text == CTX_data_edit_text(C));
191 
193 
194  us_p->is_applied = true;
195 
196  return true;
197 }
198 
199 static void text_undosys_step_decode(struct bContext *C,
200  struct Main *UNUSED(bmain),
201  UndoStep *us_p,
202  const eUndoStepDir dir,
203  bool is_final)
204 {
205  BLI_assert(dir != STEP_INVALID);
206 
207  TextUndoStep *us = (TextUndoStep *)us_p;
208  Text *text = us->text_ref.ptr;
209 
210  TextState *state;
211  if ((us->states[0].buf_array_state != NULL) && (dir == STEP_UNDO) && !is_final) {
212  state = &us->states[0];
213  }
214  else {
215  state = &us->states[1];
216  }
217 
218  text_state_decode(state, text);
219 
221  if (st) {
222  /* Not essential, always show text being undo where possible. */
223  st->text = text;
224  }
228 }
229 
231 {
232  TextUndoStep *us = (TextUndoStep *)us_p;
233 
234  for (int i = 0; i < ARRAY_SIZE(us->states); i++) {
235  TextState *state = &us->states[i];
236  if (state->buf_array_state) {
237  BLI_array_store_state_remove(g_text_buffers.buffer_store, state->buf_array_state);
238  g_text_buffers.users -= 1;
239  if (g_text_buffers.users == 0) {
241  g_text_buffers.buffer_store = NULL;
242  }
243  }
244  }
245 }
246 
248  UndoTypeForEachIDRefFn foreach_ID_ref_fn,
249  void *user_data)
250 {
251  TextUndoStep *us = (TextUndoStep *)us_p;
252  foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->text_ref));
253 }
254 
255 /* Export for ED_undo_sys. */
256 
258 {
259  ut->name = "Text";
260  ut->poll = text_undosys_poll;
265 
267 
269 
270  ut->step_size = sizeof(TextUndoStep);
271 }
272 
275 /* -------------------------------------------------------------------- */
279 /* Use operator system to finish the undo step. */
281 {
282  UndoStack *ustack = ED_undo_stack_get();
283  Main *bmain = CTX_data_main(C);
284  wmWindowManager *wm = bmain->wm.first;
285  if (wm->op_undo_depth <= 1) {
287  return us_p;
288  }
289  return NULL;
290 }
291 
struct SpaceText * CTX_wm_space_text(const bContext *C)
Definition: context.c:782
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
struct Text * CTX_data_edit_text(const bContext *C)
Definition: context.c:1306
int txt_get_span(struct TextLine *from, struct TextLine *to)
Definition: text.c:716
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, const bool sel)
Definition: text.c:1137
char * txt_to_buf_for_undo(struct Text *text, int *r_buf_len)
Definition: text.c:1390
bool txt_has_sel(struct Text *text)
Definition: text.c:1242
void txt_from_buf_for_undo(struct Text *text, const char *buf, int buf_len)
Definition: text.c:1410
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, struct bContext *C, const char *name, const UndoType *ut)
Definition: undo_system.c:458
eUndoStepDir
@ STEP_INVALID
@ STEP_UNDO
const UndoType * BKE_UNDOSYS_TYPE_TEXT
Definition: undo_system.c:105
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
void(* UndoTypeForEachIDRefFn)(void *user_data, struct UndoRefID *id_ref)
Efficient in-memory storage of multiple similar arrays.
void * BLI_array_store_state_data_get_alloc(BArrayState *state, size_t *r_data_len)
Definition: array_store.c:1654
void BLI_array_store_state_remove(BArrayStore *bs, BArrayState *state)
Definition: array_store.c:1609
BArrayStore * BLI_array_store_create(unsigned int stride, unsigned int chunk_count)
Definition: array_store.c:1422
BArrayState * BLI_array_store_state_add(BArrayStore *bs, const void *data, const size_t data_len, const BArrayState *state_reference)
Definition: array_store.c:1556
void BLI_array_store_destroy(BArrayStore *bs)
Definition: array_store.c:1478
size_t BLI_array_store_calc_size_compacted_get(const BArrayStore *bs)
Definition: array_store.c:1525
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define BLI_assert(a)
Definition: BLI_assert.h:58
unsigned char uchar
Definition: BLI_sys_types.h:86
#define ARRAY_SIZE(arr)
#define UNUSED_VARS_NDEBUG(...)
#define UNUSED(x)
#define STREQ(a, b)
struct UndoStack * ED_undo_stack_get(void)
Definition: ed_undo.c:501
Read Guarded memory(de)allocation.
Platform independent time functions.
#define C
Definition: RandGen.cpp:39
#define NA_EDITED
Definition: WM_types.h:462
#define NC_TEXT
Definition: WM_types.h:287
void * user_data
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
static ulong state[N]
char name[66]
Definition: DNA_ID.h:283
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase wm
Definition: BKE_main.h:175
struct Text * text
int cursor_line
Definition: text_undo.c:70
int cursor_column
Definition: text_undo.c:71
BArrayState * buf_array_state
Definition: text_undo.c:68
int cursor_line_select
Definition: text_undo.c:70
int cursor_column_select
Definition: text_undo.c:71
TextState states[2]
Definition: text_undo.c:126
UndoRefID_Text text_ref
Definition: text_undo.c:121
UndoStep step
Definition: text_undo.c:120
ListBase lines
TextLine * curl
int selc
TextLine * sell
int curc
struct UndoStep * step_active
struct UndoStep * step_init
const struct UndoType * type
bool is_applied
size_t data_size
size_t step_size
void(* step_decode)(struct bContext *C, struct Main *bmain, UndoStep *us, const eUndoStepDir dir, bool is_final)
bool(* step_encode)(struct bContext *C, struct Main *bmain, UndoStep *us)
void(* step_encode_init)(struct bContext *C, UndoStep *us)
void(* step_foreach_ID_ref)(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
const char * name
void(* step_free)(UndoStep *us)
bool(* poll)(struct bContext *C)
void text_update_cursor_moved(struct bContext *C)
void text_drawcache_tag_update(struct SpaceText *st, int full)
static struct @534 g_text_buffers
static void text_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
Definition: text_undo.c:247
static void text_state_encode(TextState *state, Text *text, BArrayStore *buffer_store)
Definition: text_undo.c:74
static bool text_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
Definition: text_undo.c:182
void ED_text_undosys_type(UndoType *ut)
Definition: text_undo.c:257
#define ARRAY_CHUNK_SIZE
Definition: text_undo.c:62
static bool text_undosys_poll(bContext *UNUSED(C))
Definition: text_undo.c:149
static size_t text_undosys_step_encode_to_state(TextState *state, Text *text)
Definition: text_undo.c:134
static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
Definition: text_undo.c:156
static void text_undosys_step_free(UndoStep *us_p)
Definition: text_undo.c:230
struct TextUndoStep TextUndoStep
static void text_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, const eUndoStepDir dir, bool is_final)
Definition: text_undo.c:199
UndoStep * ED_text_undo_push_init(bContext *C)
Definition: text_undo.c:280
int users
Definition: text_undo.c:131
BArrayStore * buffer_store
Definition: text_undo.c:130
static void text_state_decode(TextState *state, Text *text)
Definition: text_undo.c:96
struct TextState TextState
void WM_event_add_notifier(const bContext *C, uint type, void *reference)