Blender V5.3
text.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib> /* abort */
10#include <cstring> /* strstr */
11#include <cwctype>
12#include <optional>
13#include <sys/stat.h>
14#include <sys/types.h>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_fileops.hh"
19#include "BLI_listbase.hh"
20#include "BLI_path_utils.hh"
21#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24#include "BLI_utildefines.hh"
25
26#include "BLT_translation.hh"
27
28#include "DNA_text_types.h"
29#include "DNA_userdef_types.h"
30
31#include "BKE_bpath.hh"
32#include "BKE_idtype.hh"
33#include "BKE_lib_id.hh"
34#include "BKE_library.hh"
35#include "BKE_main.hh"
36#include "BKE_text.h"
37
38#include "BLO_read_write.hh"
39
40#ifdef WITH_PYTHON
41# include "BPY_extern.hh"
42#endif
43
44namespace blender {
45
46/* -------------------------------------------------------------------- */
49
50static void txt_pop_first(Text *text);
51static void txt_pop_last(Text *text);
52static void txt_delete_line(Text *text, TextLine *line);
53static void txt_delete_sel(Text *text);
54static void txt_make_dirty(Text *text);
55
57
59
60/* -------------------------------------------------------------------- */
63
64static void text_init_data(ID *id)
65{
66 Text *text = id_cast<Text *>(id);
67
69
70 text->filepath = nullptr;
71
72 text->flags = TXT_ISDIRTY | TXT_ISMEM;
73 if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0) {
74 text->flags |= TXT_TABSTOSPACES;
75 }
76
77 text->lines.clear_no_delete();
78
80 tmp->line = MEM_new_array_uninitialized<char>(1, "textline_string");
81 tmp->format = nullptr;
82
83 tmp->line[0] = 0;
84 tmp->len = 0;
85
86 tmp->next = nullptr;
87 tmp->prev = nullptr;
88
89 BLI_addhead(&text->lines, tmp);
90
91 text->curl = text->lines.first();
92 text->curc = 0;
93 text->sell = text->lines.first();
94 text->selc = 0;
95}
96
107static void text_copy_data(Main * /*bmain*/,
108 std::optional<Library *> /*owner_library*/,
109 ID *id_dst,
110 const ID *id_src,
111 const int /*flag*/)
112{
113 Text *text_dst = id_cast<Text *>(id_dst);
114 const Text *text_src = id_cast<Text *>(const_cast<ID *>(id_src));
115
116 /* File name can be nullptr. */
117 if (text_src->filepath) {
118 text_dst->filepath = BLI_strdup(text_src->filepath);
119 }
120
121 text_dst->flags |= TXT_ISDIRTY;
122
123 text_dst->lines.clear_no_delete();
124 text_dst->curl = text_dst->sell = nullptr;
125 text_dst->compiled = nullptr;
126
127 /* Walk down, reconstructing. */
128 for (TextLine &line_src : text_src->lines) {
129 TextLine *line_dst = txt_line_malloc();
130
131 line_dst->line = BLI_strdupn(line_src.line, line_src.len);
132 line_dst->len = line_src.len;
133 line_dst->format = nullptr;
134
135 BLI_addtail(&text_dst->lines, line_dst);
136 }
137
138 text_dst->curl = text_dst->sell = text_dst->lines.first();
139 text_dst->curc = text_dst->selc = 0;
140}
141
143static void text_free_data(ID *id)
144{
145 /* No animation-data here. */
146 Text *text = id_cast<Text *>(id);
147
149
150 MEM_SAFE_DELETE(text->filepath);
151#ifdef WITH_PYTHON
152 BPY_text_free_code(text);
153#endif
154}
155
156static void text_foreach_path(ID *id, BPathForeachPathData *bpath_data)
157{
158 Text *text = id_cast<Text *>(id);
159
160 if (text->filepath != nullptr && text->filepath[0] != '\0') {
161 BKE_bpath_foreach_path_allocated_process(bpath_data, &text->filepath);
162 }
163}
164
165static void text_blend_write(BlendWriter *writer, ID *id, const void *id_address)
166{
167 Text *text = id_cast<Text *>(id);
168
169 /* NOTE: we are clearing local temp data here, *not* the flag in the actual 'real' ID. */
170 if ((text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) {
171 text->flags &= ~TXT_ISEXT;
172 }
173
174 /* Clean up, important in undo case to reduce false detection of changed data-blocks. */
175 text->compiled = nullptr;
176
177 /* write LibData */
178 writer->write_id_struct(id_address, text);
179 BKE_id_blend_write(writer, &text->id);
180
181 if (text->filepath) {
182 writer->write_string(text->filepath);
183 }
184
185 if (!(text->flags & TXT_ISEXT)) {
186 /* Now write the text data, in two steps for optimization in the read-function. */
187 for (TextLine &tmp : text->lines) {
188 writer->write_struct(&tmp);
189 }
190
191 for (TextLine &tmp : text->lines) {
192 writer->write_string(tmp.line);
193 }
194 }
195}
196
197static void text_blend_read_data(BlendDataReader *reader, ID *id)
198{
199 Text *text = id_cast<Text *>(id);
200 BLO_read_string(reader, &text->filepath);
201
202 text->compiled = nullptr;
203
204#if 0
205 if (text->flags & TXT_ISEXT) {
206 BKE_text_reload(text);
207 }
208/* else { */
209#endif
210
211 BLO_read_struct_list(reader, TextLine, &text->lines);
212
213 BLO_read_struct(reader, TextLine, &text->curl);
214 BLO_read_struct(reader, TextLine, &text->sell);
215
216 for (TextLine &ln : text->lines) {
217 BLO_read_string(reader, &ln.line);
218 ln.format = nullptr;
219
220 const int actual_len = ln.line ? int(strlen(ln.line)) : 0;
221 if (ln.len != actual_len) {
222 printf("Error loading text, line lengths differ\n");
223 ln.len = actual_len;
224 }
225 }
226
227 text->flags = (text->flags) & ~TXT_ISEXT;
228}
229
231 .id_code = Text::id_type,
232 .id_filter = FILTER_ID_TXT,
233 .dependencies_id_types = 0,
234 .main_listbase_index = INDEX_ID_TXT,
235 .struct_size = sizeof(Text),
236 .name = "Text",
237 .name_plural = N_("texts"),
238 .translation_context = BLT_I18NCONTEXT_ID_TEXT,
240 .asset_type_info = nullptr,
241
242 .init_data = text_init_data,
243 .copy_data = text_copy_data,
244 .free_data = text_free_data,
245 .make_local = nullptr,
246 .foreach_id = nullptr,
247 .foreach_cache = nullptr,
248 .foreach_path = text_foreach_path,
249 .foreach_working_space_color = nullptr,
250 .foreach_asset_weak_reference = nullptr,
251 .owner_pointer_get = nullptr,
252
253 .blend_write = text_blend_write,
254 .blend_read_data = text_blend_read_data,
255 .blend_read_after_liblink = nullptr,
256
257 .blend_read_undo_preserve = nullptr,
258
259 .lib_override_apply_post = nullptr,
260};
261
263
264/* -------------------------------------------------------------------- */
267
269{
270 for (TextLine *tmp = text->lines.first(), *tmp_next; tmp; tmp = tmp_next) {
271 tmp_next = tmp->next;
272 MEM_delete(tmp->line);
273 if (tmp->format) {
274 MEM_delete(tmp->format);
275 }
276 MEM_delete(tmp);
277 }
278
279 text->lines.clear_no_delete();
280
281 text->curl = text->sell = nullptr;
282}
283
284Text *BKE_text_add(Main *bmain, const char *name)
285{
286 Text *ta;
287
288 ta = BKE_id_new<Text>(bmain, name);
289 /* Texts have no users by default... Set the fake user flag to ensure that this text block
290 * doesn't get deleted by default when cleaning up data blocks. */
291 id_us_min(&ta->id);
292 id_fake_user_set(&ta->id);
293
294 return ta;
295}
296
298{
299 ptrdiff_t bad_char, i = 0;
300 const ptrdiff_t length = ptrdiff_t(strlen(*str));
301 int added = 0;
302
303 while ((*str)[i]) {
304 if ((bad_char = BLI_str_utf8_invalid_byte(*str + i, length - i)) == -1) {
305 break;
306 }
307
308 added++;
309 i += bad_char + 1;
310 }
311
312 if (added != 0) {
313 char *newstr = MEM_new_array_uninitialized<char>(size_t(length) + size_t(added) + 1,
314 "text_line");
315 ptrdiff_t mi = 0;
316 i = 0;
317
318 while ((*str)[i]) {
319 if ((bad_char = BLI_str_utf8_invalid_byte((*str) + i, length - i)) == -1) {
320 memcpy(newstr + mi, (*str) + i, length - i + 1);
321 break;
322 }
323
324 memcpy(newstr + mi, (*str) + i, bad_char);
325
326 const int mofs = mi + bad_char;
327 BLI_str_utf8_from_unicode((*str)[i + bad_char], newstr + mofs, (length + added) - mofs);
328 i += bad_char + 1;
329 mi += bad_char + 2;
330 }
331 newstr[length + added] = '\0';
332 MEM_delete(*str);
333 *str = newstr;
334 }
335
336 return added;
337}
338
343{
344 int i;
345
346 for (i = 0; i < tl->len; i++) {
347 if (tl->line[i] < ' ' && tl->line[i] != '\t') {
348 memmove(tl->line + i, tl->line + i + 1, tl->len - i);
349 tl->len--;
350 i--;
351 }
352 }
354}
355
360static void text_from_buf(Text *text, const uchar *buffer, const int len)
361{
362 int i, llen, lines_count;
363
364 BLI_assert(text->lines.is_empty());
365
366 llen = 0;
367 lines_count = 0;
368 for (i = 0; i < len; i++) {
369 if (buffer[i] == '\n') {
370 TextLine *tmp = txt_line_malloc();
371 tmp->line = MEM_new_array_uninitialized<char>(size_t(llen) + 1, "textline_string");
372 tmp->format = nullptr;
373
374 if (llen) {
375 memcpy(tmp->line, &buffer[i - llen], llen);
376 }
377 tmp->line[llen] = 0;
378 tmp->len = llen;
379
380 cleanup_textline(tmp);
381
382 BLI_addtail(&text->lines, tmp);
383 lines_count += 1;
384
385 llen = 0;
386 continue;
387 }
388 llen++;
389 }
390
391 /* create new line in cases:
392 * - rest of line (if last line in file hasn't got \n terminator).
393 * in this case content of such line would be used to fill text line buffer
394 * - file is empty. in this case new line is needed to start editing from.
395 * - last character in buffer is \n. in this case new line is needed to
396 * deal with newline at end of file. (see #28087) (sergey) */
397 if (llen != 0 || lines_count == 0 || buffer[len - 1] == '\n') {
398 TextLine *tmp = txt_line_malloc();
399 tmp->line = MEM_new_array_uninitialized<char>(size_t(llen) + 1, "textline_string");
400 tmp->format = nullptr;
401
402 if (llen) {
403 memcpy(tmp->line, &buffer[i - llen], llen);
404 }
405
406 tmp->line[llen] = 0;
407 tmp->len = llen;
408
409 cleanup_textline(tmp);
410
411 BLI_addtail(&text->lines, tmp);
412 // lines_count += 1; /* UNUSED. */
413 }
414
415 text->curl = text->sell = text->lines.first();
416 text->curc = text->selc = 0;
417}
418
420{
421 char *buffer;
422 size_t buffer_len;
423 char filepath_abs[FILE_MAX];
424 BLI_stat_t st;
425
426 if (!text->filepath) {
427 return false;
428 }
429
430 STRNCPY(filepath_abs, text->filepath);
431 BLI_path_abs(filepath_abs, ID_BLEND_PATH_FROM_GLOBAL(&text->id));
432
433 buffer = BLI_file_read_text_as_mem(filepath_abs, 0, &buffer_len);
434 if (buffer == nullptr) {
435 return false;
436 }
437
438 /* free memory: */
440 txt_make_dirty(text);
441
442 /* clear undo buffer */
443 if (BLI_stat(filepath_abs, &st) != -1) {
444 text->mtime = st.st_mtime;
445 }
446 else {
447 text->mtime = 0;
448 }
449
450 text_from_buf(text, reinterpret_cast<uchar *>(buffer), buffer_len);
451
452 MEM_delete(buffer);
453 return true;
454}
455
457 const char *filepath,
458 const char *relbase,
459 const bool is_internal)
460{
461 char *buffer;
462 size_t buffer_len;
463 Text *ta;
464 char filepath_abs[FILE_MAX];
465 BLI_stat_t st;
466
467 STRNCPY(filepath_abs, filepath);
468 BLI_path_abs(filepath_abs, relbase);
469
470 buffer = BLI_file_read_text_as_mem(filepath_abs, 0, &buffer_len);
471 if (buffer == nullptr) {
472 return nullptr;
473 }
474
475 ta = static_cast<Text *>(BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(filepath_abs), 0));
476 id_us_min(&ta->id);
477 id_fake_user_set(&ta->id);
478
479 ta->lines.clear_no_delete();
480 ta->curl = ta->sell = nullptr;
481
482 if ((U.flag & USER_TXT_TABSTOSPACES_DISABLE) == 0) {
483 ta->flags = TXT_TABSTOSPACES;
484 }
485
486 if (is_internal == false) {
487 const size_t filepath_len = strlen(filepath);
488 ta->filepath = MEM_new_array_uninitialized<char>(filepath_len + 1, "text_name");
489 memcpy(ta->filepath, filepath, filepath_len + 1);
490 }
491 else {
492 ta->flags |= TXT_ISMEM | TXT_ISDIRTY;
493 }
494
495 /* clear undo buffer */
496 if (BLI_stat(filepath_abs, &st) != -1) {
497 ta->mtime = st.st_mtime;
498 }
499 else {
500 ta->mtime = 0;
501 }
502
503 text_from_buf(ta, reinterpret_cast<uchar *>(buffer), buffer_len);
504
505 MEM_delete(buffer);
506
507 return ta;
508}
509
510Text *BKE_text_load(Main *bmain, const char *filepath, const char *relbase)
511{
512 return BKE_text_load_ex(bmain, filepath, relbase, false);
513}
514
515void BKE_text_clear(Text *text) /* called directly from rna */
516{
517 txt_sel_all(text);
518 txt_delete_sel(text);
519 txt_make_dirty(text);
520}
521
522void BKE_text_write(Text *text, const char *str, int str_len) /* called directly from rna */
523{
524 txt_insert_buf(text, str, str_len);
525 txt_move_eof(text, false);
526 txt_make_dirty(text);
527}
528
530{
531 BLI_stat_t st;
532 int result;
533 char filepath[FILE_MAX];
534
535 if (!text->filepath) {
536 return 0;
537 }
538
539 STRNCPY(filepath, text->filepath);
540 BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&text->id));
541
542 if (!BLI_exists(filepath)) {
543 return 2;
544 }
545
546 result = BLI_stat(filepath, &st);
547
548 if (result == -1) {
549 return -1;
550 }
551
552 if ((st.st_mode & S_IFMT) != S_IFREG) {
553 return -1;
554 }
555
556 if (st.st_mtime > text->mtime) {
557 return 1;
558 }
559
560 return 0;
561}
562
564{
565 BLI_stat_t st;
566 int result;
567 char filepath[FILE_MAX];
568
569 if (!text->filepath) {
570 return;
571 }
572
573 STRNCPY(filepath, text->filepath);
574 BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&text->id));
575
576 if (!BLI_exists(filepath)) {
577 return;
578 }
579
580 result = BLI_stat(filepath, &st);
581
582 if (result == -1 || (st.st_mode & S_IFMT) != S_IFREG) {
583 return;
584 }
585
586 text->mtime = st.st_mtime;
587}
588
590
591/* -------------------------------------------------------------------- */
594
596{
597 TextLine *l = MEM_new<TextLine>("textline");
598 /* Quiet VALGRIND warning, may avoid unintended differences with MEMFILE undo as well. */
599 memset(l->_pad0, 0, sizeof(l->_pad0));
600 return l;
601}
602
603static void make_new_line(TextLine *line, char *newline)
604{
605 if (line->line) {
606 MEM_delete(line->line);
607 }
608 if (line->format) {
609 MEM_delete(line->format);
610 }
611
612 line->line = newline;
613 line->len = strlen(newline);
614 line->format = nullptr;
615}
616
617static TextLine *txt_new_linen(const char *str, int str_len)
618{
619 TextLine *tmp = txt_line_malloc();
620 tmp->line = MEM_new_array_uninitialized<char>(size_t(str_len) + 1, "textline_string");
621 tmp->format = nullptr;
622
623 memcpy(tmp->line, str, str_len);
624 tmp->line[str_len] = '\0';
625 tmp->len = str_len;
626 tmp->next = tmp->prev = nullptr;
627
628 BLI_assert(strlen(tmp->line) == str_len);
629
630 return tmp;
631}
632
633static TextLine *txt_new_line(const char *str)
634{
635 return txt_new_linen(str, strlen(str));
636}
637
639{
640 TextLine **top, **bot;
641
642 if (!text->lines.first()) {
643 if (text->lines.last()) {
644 text->lines.first_ = text->lines.last();
645 }
646 else {
647 text->lines.first_ = text->lines.last_ = txt_new_line("");
648 }
649 }
650
651 if (!text->lines.last()) {
652 text->lines.last_ = text->lines.first();
653 }
654
655 top = reinterpret_cast<TextLine **>(&text->lines.first_);
656 bot = reinterpret_cast<TextLine **>(&text->lines.last_);
657
658 while ((*top)->prev) {
659 *top = (*top)->prev;
660 }
661 while ((*bot)->next) {
662 *bot = (*bot)->next;
663 }
664
665 if (!text->curl) {
666 if (text->sell) {
667 text->curl = text->sell;
668 }
669 else {
670 text->curl = text->lines.first();
671 }
672 text->curc = 0;
673 }
674
675 if (!text->sell) {
676 text->sell = text->curl;
677 text->selc = 0;
678 }
679}
680
681int txt_get_span(const TextLine *from, const TextLine *to)
682{
683 int ret = 0;
684 const TextLine *tmp = from;
685
686 if (!to || !from) {
687 return 0;
688 }
689 if (from == to) {
690 return 0;
691 }
692
693 /* Look forwards */
694 while (tmp) {
695 if (tmp == to) {
696 return ret;
697 }
698 ret++;
699 tmp = tmp->next;
700 }
701
702 /* Look backwards */
703 if (!tmp) {
704 tmp = from;
705 ret = 0;
706 while (tmp) {
707 if (tmp == to) {
708 break;
709 }
710 ret--;
711 tmp = tmp->prev;
712 }
713 if (!tmp) {
714 ret = 0;
715 }
716 }
717
718 return ret;
719}
720
721static void txt_make_dirty(Text *text)
722{
723 text->flags |= TXT_ISDIRTY;
724#ifdef WITH_PYTHON
725 if (text->compiled) {
726 BPY_text_free_code(text);
727 }
728#endif
729}
730
732
733/* -------------------------------------------------------------------- */
736
737static void txt_curs_cur(Text *text, TextLine ***linep, int **charp)
738{
739 *linep = &text->curl;
740 *charp = &text->curc;
741}
742
743static void txt_curs_sel(Text *text, TextLine ***linep, int **charp)
744{
745 *linep = &text->sell;
746 *charp = &text->selc;
747}
748
750{
751 return (text->selc == 0);
752}
753
755{
756 return (text->selc == text->sell->len);
757}
758
760
761/* -------------------------------------------------------------------- */
768
769void txt_move_up(Text *text, const bool sel)
770{
771 TextLine **linep;
772 int *charp;
773
774 if (sel) {
775 txt_curs_sel(text, &linep, &charp);
776 }
777 else {
778 txt_pop_first(text);
779 txt_curs_cur(text, &linep, &charp);
780 }
781 if (!*linep) {
782 return;
783 }
784
785 if ((*linep)->prev) {
787 (*linep)->line, (*linep)->len, *charp, TXT_TABSIZE);
788 *linep = (*linep)->prev;
790 (*linep)->line, (*linep)->len, column, TXT_TABSIZE);
791 }
792 else {
793 txt_move_bol(text, sel);
794 }
795
796 if (!sel) {
797 txt_pop_sel(text);
798 }
799}
800
801void txt_move_down(Text *text, const bool sel)
802{
803 TextLine **linep;
804 int *charp;
805
806 if (sel) {
807 txt_curs_sel(text, &linep, &charp);
808 }
809 else {
810 txt_pop_last(text);
811 txt_curs_cur(text, &linep, &charp);
812 }
813 if (!*linep) {
814 return;
815 }
816
817 if ((*linep)->next) {
819 (*linep)->line, (*linep)->len, *charp, TXT_TABSIZE);
820 *linep = (*linep)->next;
822 (*linep)->line, (*linep)->len, column, TXT_TABSIZE);
823 }
824 else {
825 txt_move_eol(text, sel);
826 }
827
828 if (!sel) {
829 txt_pop_sel(text);
830 }
831}
832
833int txt_calc_tab_left(const TextLine *tl, int ch)
834{
835 /* do nice left only if there are only spaces */
836
837 int tabsize = (ch < TXT_TABSIZE) ? ch : TXT_TABSIZE;
838
839 for (int i = 0; i < ch; i++) {
840 if (tl->line[i] != ' ') {
841 tabsize = 0;
842 break;
843 }
844 }
845
846 /* if in the middle of the space-tab */
847 if (tabsize && ch % TXT_TABSIZE != 0) {
848 tabsize = (ch % TXT_TABSIZE);
849 }
850 return tabsize;
851}
852
853int txt_calc_tab_right(const TextLine *tl, int ch)
854{
855 if (tl->line[ch] == ' ') {
856 int i;
857 for (i = 0; i < ch; i++) {
858 if (tl->line[i] != ' ') {
859 return 0;
860 }
861 }
862
863 int tabsize = (ch) % TXT_TABSIZE + 1;
864 for (i = ch + 1; tl->line[i] == ' ' && tabsize < TXT_TABSIZE; i++) {
865 tabsize++;
866 }
867
868 return i - ch;
869 }
870
871 return 0;
872}
873
874void txt_move_left(Text *text, const bool sel)
875{
876 TextLine **linep;
877 int *charp;
878 int tabsize = 0;
879
880 if (sel) {
881 txt_curs_sel(text, &linep, &charp);
882 }
883 else {
884 txt_pop_first(text);
885 txt_curs_cur(text, &linep, &charp);
886 }
887 if (!*linep) {
888 return;
889 }
890
891 if (*charp == 0) {
892 if ((*linep)->prev) {
893 txt_move_up(text, sel);
894 *charp = (*linep)->len;
895 }
896 }
897 else {
898 /* do nice left only if there are only spaces */
899 /* #TXT_TABSIZE hard-coded in DNA_text_types.h */
900 if (text->flags & TXT_TABSTOSPACES) {
901 tabsize = txt_calc_tab_left(*linep, *charp);
902 }
903
904 if (tabsize) {
905 (*charp) -= tabsize;
906 }
907 else {
908 BLI_str_cursor_step_prev_utf8((*linep)->line, (*linep)->len, charp);
909 }
910 }
911
912 if (!sel) {
913 txt_pop_sel(text);
914 }
915}
916
917void txt_move_right(Text *text, const bool sel)
918{
919 TextLine **linep;
920 int *charp;
921
922 if (sel) {
923 txt_curs_sel(text, &linep, &charp);
924 }
925 else {
926 txt_pop_last(text);
927 txt_curs_cur(text, &linep, &charp);
928 }
929 if (!*linep) {
930 return;
931 }
932
933 if (*charp == (*linep)->len) {
934 if ((*linep)->next) {
935 txt_move_down(text, sel);
936 *charp = 0;
937 }
938 }
939 else {
940 /* do nice right only if there are only spaces */
941 /* spaces hardcoded in DNA_text_types.h */
942 int tabsize = 0;
943
944 if (text->flags & TXT_TABSTOSPACES) {
945 tabsize = txt_calc_tab_right(*linep, *charp);
946 }
947
948 if (tabsize) {
949 (*charp) += tabsize;
950 }
951 else {
952 BLI_str_cursor_step_next_utf8((*linep)->line, (*linep)->len, charp);
953 }
954 }
955
956 if (!sel) {
957 txt_pop_sel(text);
958 }
959}
960
961void txt_jump_left(Text *text, const bool sel, const bool use_init_step)
962{
963 TextLine **linep;
964 int *charp;
965
966 if (sel) {
967 txt_curs_sel(text, &linep, &charp);
968 }
969 else {
970 txt_pop_first(text);
971 txt_curs_cur(text, &linep, &charp);
972 }
973 if (!*linep) {
974 return;
975 }
976
978 (*linep)->line, (*linep)->len, charp, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, use_init_step);
979
980 if (!sel) {
981 txt_pop_sel(text);
982 }
983}
984
985void txt_jump_right(Text *text, const bool sel, const bool use_init_step)
986{
987 TextLine **linep;
988 int *charp;
989
990 if (sel) {
991 txt_curs_sel(text, &linep, &charp);
992 }
993 else {
994 txt_pop_last(text);
995 txt_curs_cur(text, &linep, &charp);
996 }
997 if (!*linep) {
998 return;
999 }
1000
1002 (*linep)->line, (*linep)->len, charp, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, use_init_step);
1003
1004 if (!sel) {
1005 txt_pop_sel(text);
1006 }
1007}
1008
1009void txt_move_bol(Text *text, const bool sel)
1010{
1011 TextLine **linep;
1012 int *charp;
1013
1014 if (sel) {
1015 txt_curs_sel(text, &linep, &charp);
1016 }
1017 else {
1018 txt_curs_cur(text, &linep, &charp);
1019 }
1020 if (!*linep) {
1021 return;
1022 }
1023
1024 *charp = 0;
1025
1026 if (!sel) {
1027 txt_pop_sel(text);
1028 }
1029}
1030
1031void txt_move_eol(Text *text, const bool sel)
1032{
1033 TextLine **linep;
1034 int *charp;
1035
1036 if (sel) {
1037 txt_curs_sel(text, &linep, &charp);
1038 }
1039 else {
1040 txt_curs_cur(text, &linep, &charp);
1041 }
1042 if (!*linep) {
1043 return;
1044 }
1045
1046 *charp = (*linep)->len;
1047
1048 if (!sel) {
1049 txt_pop_sel(text);
1050 }
1051}
1052
1053void txt_move_bof(Text *text, const bool sel)
1054{
1055 TextLine **linep;
1056 int *charp;
1057
1058 if (sel) {
1059 txt_curs_sel(text, &linep, &charp);
1060 }
1061 else {
1062 txt_curs_cur(text, &linep, &charp);
1063 }
1064 if (!*linep) {
1065 return;
1066 }
1067
1068 *linep = text->lines.first();
1069 *charp = 0;
1070
1071 if (!sel) {
1072 txt_pop_sel(text);
1073 }
1074}
1075
1076void txt_move_eof(Text *text, const bool sel)
1077{
1078 TextLine **linep;
1079 int *charp;
1080
1081 if (sel) {
1082 txt_curs_sel(text, &linep, &charp);
1083 }
1084 else {
1085 txt_curs_cur(text, &linep, &charp);
1086 }
1087 if (!*linep) {
1088 return;
1089 }
1090
1091 *linep = text->lines.last();
1092 *charp = (*linep)->len;
1093
1094 if (!sel) {
1095 txt_pop_sel(text);
1096 }
1097}
1098
1099void txt_move_toline(Text *text, uint line, const bool sel)
1100{
1101 txt_move_to(text, line, 0, sel);
1102}
1103
1104void txt_move_to(Text *text, uint line, uint ch, const bool sel)
1105{
1106 TextLine **linep;
1107 int *charp;
1108 uint i;
1109
1110 if (sel) {
1111 txt_curs_sel(text, &linep, &charp);
1112 }
1113 else {
1114 txt_curs_cur(text, &linep, &charp);
1115 }
1116 if (!*linep) {
1117 return;
1118 }
1119
1120 *linep = text->lines.first();
1121 for (i = 0; i < line; i++) {
1122 if ((*linep)->next) {
1123 *linep = (*linep)->next;
1124 }
1125 else {
1126 break;
1127 }
1128 }
1129 if (ch > uint((*linep)->len)) {
1130 ch = uint((*linep)->len);
1131 }
1132 *charp = ch;
1133
1134 if (!sel) {
1135 txt_pop_sel(text);
1136 }
1137}
1138
1140
1141/* -------------------------------------------------------------------- */
1144
1145static void txt_curs_swap(Text *text)
1146{
1147 TextLine *tmpl;
1148 int tmpc;
1149
1150 tmpl = text->curl;
1151 text->curl = text->sell;
1152 text->sell = tmpl;
1153
1154 tmpc = text->curc;
1155 text->curc = text->selc;
1156 text->selc = tmpc;
1157}
1158
1159static void txt_pop_first(Text *text)
1160{
1161 if (txt_get_span(text->curl, text->sell) < 0 ||
1162 (text->curl == text->sell && text->curc > text->selc))
1163 {
1164 txt_curs_swap(text);
1165 }
1166
1167 txt_pop_sel(text);
1168}
1169
1170static void txt_pop_last(Text *text)
1171{
1172 if (txt_get_span(text->curl, text->sell) > 0 ||
1173 (text->curl == text->sell && text->curc < text->selc))
1174 {
1175 txt_curs_swap(text);
1176 }
1177
1178 txt_pop_sel(text);
1179}
1180
1182{
1183 text->sell = text->curl;
1184 text->selc = text->curc;
1185}
1186
1187void txt_order_cursors(Text *text, const bool reverse)
1188{
1189 if (!text->curl) {
1190 return;
1191 }
1192 if (!text->sell) {
1193 return;
1194 }
1195
1196 /* Flip so text->curl is before/after text->sell */
1197 if (reverse == false) {
1198 if ((txt_get_span(text->curl, text->sell) < 0) ||
1199 (text->curl == text->sell && text->curc > text->selc))
1200 {
1201 txt_curs_swap(text);
1202 }
1203 }
1204 else {
1205 if ((txt_get_span(text->curl, text->sell) > 0) ||
1206 (text->curl == text->sell && text->curc < text->selc))
1207 {
1208 txt_curs_swap(text);
1209 }
1210 }
1211}
1212
1213bool txt_has_sel(const Text *text)
1214{
1215 return ((text->curl != text->sell) || (text->curc != text->selc));
1216}
1217
1218static void txt_delete_sel(Text *text)
1219{
1220 TextLine *tmpl;
1221 char *buf;
1222
1223 if (!text->curl) {
1224 return;
1225 }
1226 if (!text->sell) {
1227 return;
1228 }
1229
1230 if (!txt_has_sel(text)) {
1231 return;
1232 }
1233
1234 txt_order_cursors(text, false);
1235
1237 size_t(text->curc) + (size_t(text->sell->len) - size_t(text->selc)) + 1, "textline_string");
1238
1239 memcpy(buf, text->curl->line, text->curc);
1240 memcpy(buf + text->curc, text->sell->line + text->selc, text->sell->len - text->selc);
1241 buf[text->curc + (text->sell->len - text->selc)] = 0;
1242
1243 make_new_line(text->curl, buf);
1244
1245 tmpl = text->sell;
1246 while (tmpl != text->curl) {
1247 tmpl = tmpl->prev;
1248 if (!tmpl) {
1249 break;
1250 }
1251
1252 txt_delete_line(text, tmpl->next);
1253 }
1254
1255 text->sell = text->curl;
1256 text->selc = text->curc;
1257}
1258
1260{
1261 text->curl = text->lines.first();
1262 text->curc = 0;
1263
1264 text->sell = text->lines.last();
1265 text->selc = text->sell->len;
1266}
1267
1269{
1270 if (text->sell) {
1271 text->curl = text->sell;
1272 text->curc = text->selc;
1273 }
1274}
1275
1277{
1278 if (!text->curl) {
1279 return;
1280 }
1281
1282 text->curc = 0;
1283 text->sell = text->curl;
1284 text->selc = text->sell->len;
1285}
1286
1287void txt_sel_set(Text *text, int startl, int startc, int endl, int endc)
1288{
1289 TextLine *froml, *tol;
1290
1291 /* Support negative indices. */
1292 if (startl < 0 || endl < 0) {
1293 int end = text->lines.count() - 1;
1294 if (startl < 0) {
1295 startl = end + startl + 1;
1296 }
1297 if (endl < 0) {
1298 endl = end + endl + 1;
1299 }
1300 }
1301 CLAMP_MIN(startl, 0);
1302 CLAMP_MIN(endl, 0);
1303
1304 froml = static_cast<TextLine *>(BLI_findlink(&text->lines, startl));
1305 if (froml == nullptr) {
1306 froml = text->lines.last();
1307 }
1308 if (startl == endl) {
1309 tol = froml;
1310 }
1311 else {
1312 tol = static_cast<TextLine *>(BLI_findlink(&text->lines, endl));
1313 if (tol == nullptr) {
1314 tol = text->lines.last();
1315 }
1316 }
1317
1318 /* Support negative indices. */
1319 if (startc < 0) {
1320 const int fromllen = BLI_strlen_utf8(froml->line);
1321 startc = std::max(0, fromllen + startc + 1);
1322 }
1323 if (endc < 0) {
1324 const int tollen = BLI_strlen_utf8(tol->line);
1325 endc = std::max(0, tollen + endc + 1);
1326 }
1327
1328 text->curl = froml;
1329 text->curc = BLI_str_utf8_offset_from_index(froml->line, froml->len, startc);
1330 text->sell = tol;
1331 text->selc = BLI_str_utf8_offset_from_index(tol->line, tol->len, endc);
1332}
1333
1335
1336/* -------------------------------------------------------------------- */
1348
1349char *txt_to_buf_for_undo(Text *text, size_t *r_buf_len)
1350{
1351 int buf_len = 0;
1352 for (const TextLine &l : text->lines) {
1353 buf_len += l.len + 1;
1354 }
1355 char *buf = MEM_new_array_uninitialized<char>(size_t(buf_len), __func__);
1356 char *buf_step = buf;
1357 for (const TextLine &l : text->lines) {
1358 memcpy(buf_step, l.line, l.len);
1359 buf_step += l.len;
1360 *buf_step++ = '\n';
1361 }
1362 *r_buf_len = buf_len;
1363 return buf;
1364}
1365
1366void txt_from_buf_for_undo(Text *text, const char *buf, size_t buf_len)
1367{
1368 const char *buf_end = buf + buf_len;
1369 const char *buf_step = buf;
1370
1371 /* First re-use existing lines.
1372 * Good for undo since it means in practice many operations re-use all
1373 * except for the modified line. */
1374 TextLine *l_src = text->lines.first();
1375 text->lines.clear_no_delete();
1376 while (buf_step != buf_end && l_src) {
1377 /* New lines are ensured by #txt_to_buf_for_undo. */
1378 const char *buf_step_next = strchr(buf_step, '\n');
1379 const int len = buf_step_next - buf_step;
1380
1381 TextLine *l = l_src;
1382 l_src = l_src->next;
1383 if (l->len != len) {
1384 l->line = static_cast<char *>(MEM_realloc_uninitialized(l->line, len + 1));
1385 l->len = len;
1386 }
1387 MEM_SAFE_DELETE(l->format);
1388
1389 memcpy(l->line, buf_step, len);
1390 l->line[len] = '\0';
1391 BLI_addtail(&text->lines, l);
1392 buf_step = buf_step_next + 1;
1393 }
1394
1395 /* If we have extra lines. */
1396 while (l_src != nullptr) {
1397 TextLine *l_src_next = l_src->next;
1398 MEM_delete(l_src->line);
1399 if (l_src->format) {
1400 MEM_delete(l_src->format);
1401 }
1402 MEM_delete(l_src);
1403 l_src = l_src_next;
1404 }
1405
1406 while (buf_step != buf_end) {
1407 /* New lines are ensured by #txt_to_buf_for_undo. */
1408 const char *buf_step_next = strchr(buf_step, '\n');
1409 const int len = buf_step_next - buf_step;
1410
1412 l->line = MEM_new_array_uninitialized<char>(size_t(len) + 1, "textline_string");
1413 l->len = len;
1414 l->format = nullptr;
1415
1416 memcpy(l->line, buf_step, len);
1417 l->line[len] = '\0';
1418 BLI_addtail(&text->lines, l);
1419 buf_step = buf_step_next + 1;
1420 }
1421
1422 text->curl = text->sell = text->lines.first();
1423 text->curc = text->selc = 0;
1424
1425 txt_make_dirty(text);
1426}
1427
1429
1430/* -------------------------------------------------------------------- */
1433
1434char *txt_to_buf(Text *text, size_t *r_buf_strlen)
1435{
1436 const bool has_data = !text->lines.is_empty();
1437 /* Identical to #txt_to_buf_for_undo except that the string is nil terminated. */
1438 size_t buf_len = 0;
1439 for (const TextLine &l : text->lines) {
1440 buf_len += l.len + 1;
1441 }
1442 if (has_data) {
1443 buf_len -= 1;
1444 }
1445 char *buf = MEM_new_array_uninitialized<char>(buf_len + 1, __func__);
1446 char *buf_step = buf;
1447 for (const TextLine &l : text->lines) {
1448 memcpy(buf_step, l.line, l.len);
1449 buf_step += l.len;
1450 *buf_step++ = '\n';
1451 }
1452 /* Remove the trailing new-line so a round-trip doesn't add a newline:
1453 * Python for example `text.from_string(text.as_string())`. */
1454 if (has_data) {
1455 buf_step--;
1456 }
1457 *buf_step = '\0';
1458 *r_buf_strlen = buf_len;
1459 return buf;
1460}
1461
1462char *txt_sel_to_buf(const Text *text, size_t *r_buf_strlen)
1463{
1464 char *buf;
1465 size_t length = 0;
1466 TextLine *tmp, *linef, *linel;
1467 int charf, charl;
1468
1469 if (r_buf_strlen) {
1470 *r_buf_strlen = 0;
1471 }
1472
1473 if (!text->curl) {
1474 return nullptr;
1475 }
1476 if (!text->sell) {
1477 return nullptr;
1478 }
1479
1480 if (text->curl == text->sell) {
1481 linef = linel = text->curl;
1482
1483 if (text->curc < text->selc) {
1484 charf = text->curc;
1485 charl = text->selc;
1486 }
1487 else {
1488 charf = text->selc;
1489 charl = text->curc;
1490 }
1491 }
1492 else if (txt_get_span(text->curl, text->sell) < 0) {
1493 linef = text->sell;
1494 linel = text->curl;
1495
1496 charf = text->selc;
1497 charl = text->curc;
1498 }
1499 else {
1500 linef = text->curl;
1501 linel = text->sell;
1502
1503 charf = text->curc;
1504 charl = text->selc;
1505 }
1506
1507 if (linef == linel) {
1508 length = charl - charf;
1509 buf = MEM_new_array_uninitialized<char>(length + 1, "sel buffer");
1510 memcpy(buf, linef->line + charf, length);
1511 buf[length] = '\0';
1512 }
1513 else {
1514 /* Add 1 for the '\n' */
1515 length = (linef->len - charf) + charl + 1;
1516
1517 for (tmp = linef->next; tmp && tmp != linel; tmp = tmp->next) {
1518 length += tmp->len + 1;
1519 }
1520
1521 buf = MEM_new_array_uninitialized<char>(length + 1, "sel buffer");
1522
1523 memcpy(buf, linef->line + charf, linef->len - charf);
1524 length = linef->len - charf;
1525 buf[length++] = '\n';
1526
1527 for (tmp = linef->next; tmp && tmp != linel; tmp = tmp->next) {
1528 memcpy(buf + length, tmp->line, tmp->len);
1529 length += tmp->len;
1530 buf[length++] = '\n';
1531 }
1532
1533 memcpy(buf + length, linel->line, charl);
1534 length += charl;
1535 buf[length] = '\0';
1536 }
1537
1538 if (r_buf_strlen) {
1539 *r_buf_strlen = length;
1540 }
1541
1542 return buf;
1543}
1544
1545void txt_insert_buf(Text *text, const char *in_buffer, int in_buffer_len)
1546{
1547 BLI_assert(in_buffer_len == strlen(in_buffer));
1548
1549 int l = 0;
1550 size_t i = 0, j;
1551 TextLine *add;
1552 char *buffer;
1553
1554 txt_delete_sel(text);
1555
1556 buffer = BLI_strdupn(in_buffer, in_buffer_len);
1557 in_buffer_len += txt_extended_ascii_as_utf8(&buffer);
1558
1559 /* Read the first line (or as close as possible). */
1560 while (buffer[i] && buffer[i] != '\n') {
1561 txt_add_raw_char(text, BLI_str_utf8_as_unicode_step_safe(buffer, in_buffer_len, &i));
1562 }
1563
1564 if (buffer[i] == '\n') {
1565 txt_split_curline(text);
1566 i++;
1567
1568 while (i < in_buffer_len) {
1569 l = 0;
1570
1571 while (buffer[i] && buffer[i] != '\n') {
1572 i++;
1573 l++;
1574 }
1575
1576 if (buffer[i] == '\n') {
1577 add = txt_new_linen(buffer + (i - l), l);
1578 BLI_insertlinkbefore(&text->lines, text->curl, add);
1579 i++;
1580 }
1581 else {
1582 for (j = i - l; j < i && j < in_buffer_len;) {
1583 txt_add_raw_char(text, BLI_str_utf8_as_unicode_step_safe(buffer, in_buffer_len, &j));
1584 }
1585 break;
1586 }
1587 }
1588 }
1589
1590 MEM_delete(buffer);
1591}
1592
1594
1595/* -------------------------------------------------------------------- */
1598
1599bool txt_find_string(Text *text, const char *findstr, int wrap, int match_case)
1600{
1601 TextLine *tl, *startl;
1602 const char *s = nullptr;
1603
1604 if (!text->curl || !text->sell) {
1605 return false;
1606 }
1607
1608 txt_order_cursors(text, false);
1609
1610 tl = startl = text->sell;
1611
1612 if (match_case) {
1613 s = strstr(&tl->line[text->selc], findstr);
1614 }
1615 else {
1616 s = BLI_strcasestr(&tl->line[text->selc], findstr);
1617 }
1618 while (!s) {
1619 tl = tl->next;
1620 if (!tl) {
1621 if (wrap) {
1622 tl = text->lines.first();
1623 }
1624 else {
1625 break;
1626 }
1627 }
1628
1629 if (match_case) {
1630 s = strstr(tl->line, findstr);
1631 }
1632 else {
1633 s = BLI_strcasestr(tl->line, findstr);
1634 }
1635 if (tl == startl) {
1636 break;
1637 }
1638 }
1639
1640 if (s) {
1641 int newl = txt_get_span(text->lines.first(), tl);
1642 int newc = int(s - tl->line);
1643 txt_move_to(text, newl, newc, false);
1644 txt_move_to(text, newl, newc + strlen(findstr), true);
1645 return true;
1646 }
1647
1648 return false;
1649}
1650
1652
1653/* -------------------------------------------------------------------- */
1656
1658{
1659 TextLine *ins;
1660 char *left, *right;
1661
1662 if (!text->curl) {
1663 return;
1664 }
1665
1666 txt_delete_sel(text);
1667
1668 /* Make the two half strings */
1669
1670 left = MEM_new_array_uninitialized<char>(size_t(text->curc) + 1, "textline_string");
1671 if (text->curc) {
1672 memcpy(left, text->curl->line, text->curc);
1673 }
1674 left[text->curc] = 0;
1675
1676 right = MEM_new_array_uninitialized<char>(size_t(text->curl->len) - size_t(text->curc) + 1,
1677 "textline_string");
1678 memcpy(right, text->curl->line + text->curc, text->curl->len - text->curc + 1);
1679
1680 MEM_delete(text->curl->line);
1681 if (text->curl->format) {
1682 MEM_delete(text->curl->format);
1683 }
1684
1685 /* Make the new TextLine */
1686
1687 ins = txt_line_malloc();
1688 ins->line = left;
1689 ins->format = nullptr;
1690 ins->len = text->curc;
1691
1692 text->curl->line = right;
1693 text->curl->format = nullptr;
1694 text->curl->len = text->curl->len - text->curc;
1695
1696 BLI_insertlinkbefore(&text->lines, text->curl, ins);
1697
1698 text->curc = 0;
1699
1700 txt_make_dirty(text);
1701 txt_clean_text(text);
1702
1703 txt_pop_sel(text);
1704}
1705
1706static void txt_delete_line(Text *text, TextLine *line)
1707{
1708 if (!text->curl) {
1709 return;
1710 }
1711
1712 BLI_remlink(&text->lines, line);
1713
1714 if (line->line) {
1715 MEM_delete(line->line);
1716 }
1717 if (line->format) {
1718 MEM_delete(line->format);
1719 }
1720
1721 MEM_delete(line);
1722
1723 txt_make_dirty(text);
1724 txt_clean_text(text);
1725}
1726
1727static void txt_combine_lines(Text *text, TextLine *linea, TextLine *lineb)
1728{
1729 char *tmp, *s;
1730
1731 if (!linea || !lineb) {
1732 return;
1733 }
1734
1735 tmp = MEM_new_array_uninitialized<char>(size_t(linea->len) + size_t(lineb->len) + 1,
1736 "textline_string");
1737
1738 s = tmp;
1739 memcpy(s, linea->line, linea->len);
1740 s += linea->len;
1741 memcpy(s, lineb->line, lineb->len);
1742 s += lineb->len;
1743 *s = '\0';
1744 (void)s;
1745
1746 make_new_line(linea, tmp);
1747
1748 txt_delete_line(text, lineb);
1749
1750 txt_make_dirty(text);
1751 txt_clean_text(text);
1752}
1753
1755{
1756 TextLine *textline;
1757
1758 if (!text->curl) {
1759 return;
1760 }
1761
1762 if (text->curl == text->sell) {
1763 textline = txt_new_line(text->curl->line);
1764 BLI_insertlinkafter(&text->lines, text->curl, textline);
1765
1766 txt_make_dirty(text);
1767 txt_clean_text(text);
1768 }
1769}
1770
1772{
1773 if (!text->curl) {
1774 return;
1775 }
1776
1777 if (txt_has_sel(text)) { /* deleting a selection */
1778 txt_delete_sel(text);
1779 txt_make_dirty(text);
1780 return;
1781 }
1782 if (text->curc == text->curl->len) { /* Appending two lines */
1783 if (text->curl->next) {
1784 txt_combine_lines(text, text->curl, text->curl->next);
1785 txt_pop_sel(text);
1786 }
1787 else {
1788 return;
1789 }
1790 }
1791 else { /* Just deleting a char */
1792 int pos = text->curc;
1793 BLI_str_cursor_step_next_utf8(text->curl->line, text->curl->len, &pos);
1794 size_t c_len = pos - text->curc;
1795
1796 memmove(text->curl->line + text->curc,
1797 text->curl->line + text->curc + c_len,
1798 text->curl->len - text->curc - c_len + 1);
1799
1800 text->curl->len -= c_len;
1801
1802 txt_pop_sel(text);
1803 }
1804
1805 txt_make_dirty(text);
1806 txt_clean_text(text);
1807}
1808
1810{
1811 txt_jump_right(text, true, true);
1812 txt_delete_sel(text);
1813 txt_make_dirty(text);
1814}
1815
1817{
1818 if (!text->curl) {
1819 return;
1820 }
1821
1822 if (txt_has_sel(text)) { /* deleting a selection */
1823 txt_delete_sel(text);
1824 txt_make_dirty(text);
1825 return;
1826 }
1827 if (text->curc == 0) { /* Appending two lines */
1828 if (!text->curl->prev) {
1829 return;
1830 }
1831
1832 text->curl = text->curl->prev;
1833 text->curc = text->curl->len;
1834
1835 txt_combine_lines(text, text->curl, text->curl->next);
1836 txt_pop_sel(text);
1837 }
1838 else { /* Just backspacing a char */
1839 int pos = text->curc;
1840 BLI_str_cursor_step_prev_utf8(text->curl->line, text->curl->len, &pos);
1841 size_t c_len = text->curc - pos;
1842
1843 /* source and destination overlap, don't use memcpy() */
1844 memmove(text->curl->line + text->curc - c_len,
1845 text->curl->line + text->curc,
1846 text->curl->len - text->curc + 1);
1847
1848 text->curl->len -= c_len;
1849 text->curc -= c_len;
1850
1851 txt_pop_sel(text);
1852 }
1853
1854 txt_make_dirty(text);
1855 txt_clean_text(text);
1856}
1857
1859{
1860 txt_jump_left(text, true, true);
1861 txt_delete_sel(text);
1862 txt_make_dirty(text);
1863}
1864
1865/* Max spaces to replace a tab with, currently hardcoded to TXT_TABSIZE = 4.
1866 * Used by txt_convert_tab_to_spaces, indent and unindent.
1867 * Remember to change this string according to max tab size */
1868static char tab_to_spaces[] = " ";
1869
1871{
1872 /* sb aims to pad adjust the tab-width needed so that the right number of spaces
1873 * is added so that the indentation of the line is the right width (i.e. aligned
1874 * to multiples of TXT_TABSIZE)
1875 */
1876 const char *sb = &tab_to_spaces[text->curc % TXT_TABSIZE];
1877 txt_insert_buf(text, sb, strlen(sb));
1878}
1879
1880static bool txt_add_char_intern(Text *text, uint add, bool replace_tabs)
1881{
1882 char *tmp, ch[BLI_UTF8_MAX];
1883 size_t add_len;
1884
1885 if (!text->curl) {
1886 return false;
1887 }
1888
1889 if (add == '\n') {
1890 txt_split_curline(text);
1891 return true;
1892 }
1893
1894 /* insert spaces rather than tabs */
1895 if (add == '\t' && replace_tabs) {
1897 return true;
1898 }
1899
1900 txt_delete_sel(text);
1901
1902 add_len = BLI_str_utf8_from_unicode(add, ch, sizeof(ch));
1903
1904 tmp = MEM_new_array_uninitialized<char>(size_t(text->curl->len) + add_len + 1,
1905 "textline_string");
1906
1907 memcpy(tmp, text->curl->line, text->curc);
1908 memcpy(tmp + text->curc, ch, add_len);
1909 memcpy(
1910 tmp + text->curc + add_len, text->curl->line + text->curc, text->curl->len - text->curc + 1);
1911
1912 make_new_line(text->curl, tmp);
1913
1914 text->curc += add_len;
1915
1916 txt_pop_sel(text);
1917
1918 txt_make_dirty(text);
1919 txt_clean_text(text);
1920
1921 return true;
1922}
1923
1924bool txt_add_char(Text *text, uint add)
1925{
1926 return txt_add_char_intern(text, add, (text->flags & TXT_TABSTOSPACES) != 0);
1927}
1928
1930{
1931 return txt_add_char_intern(text, add, false);
1932}
1933
1935{
1936 txt_delete_sel(text);
1937 txt_make_dirty(text);
1938}
1939
1941{
1942 uint del;
1943 size_t del_size = 0, add_size;
1944 char ch[BLI_UTF8_MAX];
1945
1946 if (!text->curl) {
1947 return false;
1948 }
1949
1950 /* If text is selected or we're at the end of the line just use txt_add_char */
1951 if (text->curc == text->curl->len || txt_has_sel(text) || add == '\n') {
1952 return txt_add_char(text, add);
1953 }
1954
1955 del_size = text->curc;
1956 del = BLI_str_utf8_as_unicode_step_safe(text->curl->line, text->curl->len, &del_size);
1957 del_size -= text->curc;
1958 UNUSED_VARS(del);
1959 add_size = BLI_str_utf8_from_unicode(add, ch, sizeof(ch));
1960
1961 if (add_size > del_size) {
1963 size_t(text->curl->len) + add_size - del_size + 1, "textline_string");
1964 memcpy(tmp, text->curl->line, text->curc);
1965 memcpy(tmp + text->curc + add_size,
1966 text->curl->line + text->curc + del_size,
1967 text->curl->len - text->curc - del_size + 1);
1968 MEM_delete(text->curl->line);
1969 text->curl->line = tmp;
1970 }
1971 else if (add_size < del_size) {
1972 char *tmp = text->curl->line;
1973 memmove(tmp + text->curc + add_size,
1974 tmp + text->curc + del_size,
1975 text->curl->len - text->curc - del_size + 1);
1976 }
1977
1978 memcpy(text->curl->line + text->curc, ch, add_size);
1979 text->curc += add_size;
1980 text->curl->len += add_size - del_size;
1981
1982 txt_pop_sel(text);
1983 txt_make_dirty(text);
1984 txt_clean_text(text);
1985 return true;
1986}
1987
1993static void txt_select_prefix(Text *text, const char *add, bool skip_blank_lines)
1994{
1995 int len, num, curc_old, selc_old;
1996 char *tmp;
1997
1998 const int indentlen = strlen(add);
1999
2000 BLI_assert(!ELEM(nullptr, text->curl, text->sell));
2001
2002 curc_old = text->curc;
2003 selc_old = text->selc;
2004
2005 num = 0;
2006 while (true) {
2007
2008 /* don't indent blank lines */
2009 if ((text->curl->len != 0) || (skip_blank_lines == 0)) {
2010 tmp = MEM_new_array_uninitialized<char>(size_t(text->curl->len) + size_t(indentlen) + 1,
2011 "textline_string");
2012
2013 text->curc = 0;
2014 if (text->curc) {
2015 memcpy(tmp, text->curl->line, text->curc); /* XXX never true, check prev line */
2016 }
2017 memcpy(tmp + text->curc, add, indentlen);
2018
2019 len = text->curl->len - text->curc;
2020 if (len > 0) {
2021 memcpy(tmp + text->curc + indentlen, text->curl->line + text->curc, len);
2022 }
2023 tmp[text->curl->len + indentlen] = 0;
2024
2025 make_new_line(text->curl, tmp);
2026
2027 text->curc += indentlen;
2028
2029 txt_make_dirty(text);
2030 txt_clean_text(text);
2031 }
2032
2033 if (text->curl == text->sell) {
2034 if (text->curl->len != 0) {
2035 text->selc += indentlen;
2036 }
2037 break;
2038 }
2039
2040 text->curl = text->curl->next;
2041 num++;
2042 }
2043
2044 while (num > 0) {
2045 text->curl = text->curl->prev;
2046 num--;
2047 }
2048
2049 /* Keep the cursor left aligned if we don't have a selection. */
2050 if (curc_old == 0 && !(text->curl == text->sell && curc_old == selc_old)) {
2051 if (text->curl == text->sell) {
2052 if (text->curc == text->selc) {
2053 text->selc = 0;
2054 }
2055 }
2056 text->curc = 0;
2057 }
2058 else {
2059 if (text->curl->len != 0) {
2060 text->curc = curc_old + indentlen;
2061 }
2062 }
2063}
2064
2073static bool txt_select_unprefix(Text *text, const char *remove, const bool require_all)
2074{
2075 int num = 0;
2076 const int indentlen = strlen(remove);
2077 bool unindented_first = false;
2078 bool changed_any = false;
2079
2080 BLI_assert(!ELEM(nullptr, text->curl, text->sell));
2081
2082 if (require_all) {
2083 /* Check all non-empty lines use this 'remove',
2084 * so the operation is applied equally or not at all. */
2085 TextLine *l = text->curl;
2086 while (true) {
2087 if (STREQLEN(l->line, remove, indentlen)) {
2088 /* pass */
2089 }
2090 else {
2091 /* Blank lines or whitespace can be skipped. */
2092 for (int i = 0; i < l->len; i++) {
2093 if (!ELEM(l->line[i], '\t', ' ')) {
2094 return false;
2095 }
2096 }
2097 }
2098 if (l == text->sell) {
2099 break;
2100 }
2101 l = l->next;
2102 }
2103 }
2104
2105 while (true) {
2106 bool changed = false;
2107 if (STREQLEN(text->curl->line, remove, indentlen)) {
2108 if (num == 0) {
2109 unindented_first = true;
2110 }
2111 text->curl->len -= indentlen;
2112 memmove(text->curl->line, text->curl->line + indentlen, text->curl->len + 1);
2113 changed = true;
2114 changed_any = true;
2115 }
2116
2117 txt_make_dirty(text);
2118 txt_clean_text(text);
2119
2120 if (text->curl == text->sell) {
2121 if (changed) {
2122 text->selc = std::max(text->selc - indentlen, 0);
2123 }
2124 break;
2125 }
2126
2127 text->curl = text->curl->next;
2128 num++;
2129 }
2130
2131 if (unindented_first) {
2132 text->curc = std::max(text->curc - indentlen, 0);
2133 }
2134
2135 while (num > 0) {
2136 text->curl = text->curl->prev;
2137 num--;
2138 }
2139
2140 /* caller must handle undo */
2141 return changed_any;
2142}
2143
2144void txt_comment(Text *text, const char *prefix)
2145{
2146 if (ELEM(nullptr, text->curl, text->sell)) {
2147 return;
2148 }
2149
2150 const bool skip_blank_lines = txt_has_sel(text);
2151 txt_select_prefix(text, prefix, skip_blank_lines);
2152}
2153
2154bool txt_uncomment(Text *text, const char *prefix)
2155{
2156 if (ELEM(nullptr, text->curl, text->sell)) {
2157 return false;
2158 }
2159
2160 return txt_select_unprefix(text, prefix, true);
2161}
2162
2163void txt_indent(Text *text)
2164{
2165 const char *prefix = (text->flags & TXT_TABSTOSPACES) ? tab_to_spaces : "\t";
2166
2167 if (ELEM(nullptr, text->curl, text->sell)) {
2168 return;
2169 }
2170
2171 txt_select_prefix(text, prefix, true);
2172}
2173
2175{
2176 const char *prefix = (text->flags & TXT_TABSTOSPACES) ? tab_to_spaces : "\t";
2177
2178 if (ELEM(nullptr, text->curl, text->sell)) {
2179 return false;
2180 }
2181
2182 return txt_select_unprefix(text, prefix, false);
2183}
2184
2185void txt_move_lines(Text *text, const int direction)
2186{
2187 TextLine *line_other;
2188
2190
2191 if (!text->curl || !text->sell) {
2192 return;
2193 }
2194
2195 txt_order_cursors(text, false);
2196
2197 line_other = (direction == TXT_MOVE_LINE_DOWN) ? text->sell->next : text->curl->prev;
2198
2199 if (!line_other) {
2200 return;
2201 }
2202
2203 BLI_remlink(&text->lines, line_other);
2204
2205 if (direction == TXT_MOVE_LINE_DOWN) {
2206 BLI_insertlinkbefore(&text->lines, text->curl, line_other);
2207 }
2208 else {
2209 BLI_insertlinkafter(&text->lines, text->sell, line_other);
2210 }
2211
2212 txt_make_dirty(text);
2213 txt_clean_text(text);
2214}
2215
2216int txt_setcurr_tab_spaces(Text *text, int space)
2217{
2218 int i = 0;
2219 int test = 0;
2220 const char *word = ":";
2221 const char *comm = "#";
2222 const char indent = (text->flags & TXT_TABSTOSPACES) ? ' ' : '\t';
2223 static const char *back_words[] = {"return", "break", "continue", "pass", "yield", nullptr};
2224
2225 if (!text->curl) {
2226 return 0;
2227 }
2228
2229 while (text->curl->line[i] == indent) {
2230 /* We only count those tabs/spaces that are before any text or before the `curs`. */
2231 if (i == text->curc) {
2232 return i;
2233 }
2234
2235 i++;
2236 }
2237 if (strstr(text->curl->line, word)) {
2238 /* if we find a ':' on this line, then add a tab but not if it is:
2239 * 1) in a comment
2240 * 2) within an identifier
2241 * 3) after the cursor (text->curc), i.e. when creating space before a function def #25414.
2242 */
2243 int a;
2244 bool is_indent = false;
2245 for (a = 0; (a < text->curc) && (text->curl->line[a] != '\0'); a++) {
2246 char ch = text->curl->line[a];
2247 if (ch == '#') {
2248 break;
2249 }
2250 if (ch == ':') {
2251 is_indent = true;
2252 }
2253 else if (!ELEM(ch, ' ', '\t')) {
2254 is_indent = false;
2255 }
2256 }
2257 if (is_indent) {
2258 i += space;
2259 }
2260 }
2261
2262 for (test = 0; back_words[test]; test++) {
2263 /* if there are these key words then remove a tab because we are done with the block */
2264 if (strstr(text->curl->line, back_words[test]) && i > 0) {
2265 if (strcspn(text->curl->line, back_words[test]) < strcspn(text->curl->line, comm)) {
2266 i -= space;
2267 }
2268 }
2269 }
2270 return i;
2271}
2272
2274
2275/* -------------------------------------------------------------------- */
2278
2279int text_check_bracket(const char ch)
2280{
2281 int a;
2282 const char opens[] = "([{";
2283 const char close[] = ")]}";
2284
2285 for (a = 0; a < (sizeof(opens) - 1); a++) {
2286 if (ch == opens[a]) {
2287 return a + 1;
2288 }
2289 if (ch == close[a]) {
2290 return -(a + 1);
2291 }
2292 }
2293 return 0;
2294}
2295
2296bool text_check_delim(const char ch)
2297{
2298 /* TODO: have a function for operators:
2299 * http://docs.python.org/py3k/reference/lexical_analysis.html#operators */
2300
2301 int a;
2302 char delims[] = "():\"\' ~!%^&*-+=[]{};/<>|.#\t,@";
2303
2304 for (a = 0; a < (sizeof(delims) - 1); a++) {
2305 if (ch == delims[a]) {
2306 return true;
2307 }
2308 }
2309 return false;
2310}
2311
2312bool text_check_digit(const char ch)
2313{
2314 if (ch < '0') {
2315 return false;
2316 }
2317 if (ch <= '9') {
2318 return true;
2319 }
2320 return false;
2321}
2322
2323bool text_check_identifier(const char ch)
2324{
2325 if (ch < '0') {
2326 return false;
2327 }
2328 if (ch <= '9') {
2329 return true;
2330 }
2331 if (ch < 'A') {
2332 return false;
2333 }
2334 if (ch <= 'Z' || ch == '_') {
2335 return true;
2336 }
2337 if (ch < 'a') {
2338 return false;
2339 }
2340 if (ch <= 'z') {
2341 return true;
2342 }
2343 return false;
2344}
2345
2347{
2348 if (ch <= '9') {
2349 return false;
2350 }
2351 if (ch < 'A') {
2352 return false;
2353 }
2354 if (ch <= 'Z' || ch == '_') {
2355 return true;
2356 }
2357 if (ch < 'a') {
2358 return false;
2359 }
2360 if (ch <= 'z') {
2361 return true;
2362 }
2363 return false;
2364}
2365
2366#ifndef WITH_PYTHON
2368{
2369 return (ch < 255 && text_check_identifier(uint(ch)));
2370}
2371
2373{
2374 return (ch < 255 && text_check_identifier_nodigit(char(ch)));
2375}
2376#endif /* !WITH_PYTHON */
2377
2378bool text_check_whitespace(const char ch)
2379{
2380 if (ELEM(ch, ' ', '\t', '\r', '\n')) {
2381 return true;
2382 }
2383 return false;
2384}
2385
2386int text_find_identifier_start(const char *str, int i)
2387{
2388 if (i <= 0) [[unlikely]] {
2389 return 0;
2390 }
2391
2392 while (i--) {
2393 if (!text_check_identifier(str[i])) {
2394 break;
2395 }
2396 }
2397 i++;
2398 return i;
2399}
2400
2402
2403} // namespace blender
File and directory operations.
#define FILE_MAX
#define BLI_UTF8_MAX
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_TEXT
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
#define FILTER_ID_TXT
Definition DNA_ID.h:1223
#define TXT_TABSIZE
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
#define STREQLEN(a, b, n)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define CLAMP_MIN(a, b)
#define MEM_realloc_uninitialized(vmemh, len)
#define str(s)
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BLI_INLINE Texture * wrap(MTLTexture *texture)
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct Text * BKE_text_add(struct Main *bmain, const char *name)
Definition text.cc:284
bool text_check_identifier(char ch)
Definition text.cc:2323
bool text_check_digit(char ch)
Definition text.cc:2312
int txt_get_span(const struct TextLine *from, const struct TextLine *to)
char * STRNCPY(char(&dst)[N], const char *src)
static TextLine * txt_new_line(const char *str)
Definition text.cc:633
int txt_calc_tab_left(const struct TextLine *tl, int ch)
size_t BLI_str_utf8_from_unicode(unsigned int c, char *dst, size_t dst_maxncpy) ATTR_NONNULL(2)
static void txt_select_prefix(Text *text, const char *add, bool skip_blank_lines)
Definition text.cc:1993
void txt_jump_left(struct Text *text, bool sel, bool use_init_step)
Definition text.cc:961
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
void txt_insert_buf(struct Text *text, const char *in_buffer, int in_buffer_len) ATTR_NONNULL(1
Definition text.cc:1545
void txt_indent(struct Text *text)
Definition text.cc:2163
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BKE_bpath_foreach_path_allocated_process(BPathForeachPathData *bpath_data, char **path)
Definition bpath.cc:266
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static bool txt_add_char_intern(Text *text, uint add, bool replace_tabs)
Definition text.cc:1880
bool BLI_str_cursor_step_next_utf8(const char *str, int str_maxlen, int *pos)
int txt_extended_ascii_as_utf8(char **str)
Definition text.cc:297
static void txt_make_dirty(Text *text)
Definition text.cc:721
void txt_move_eof(struct Text *text, bool sel)
Definition text.cc:1076
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
static TextLine * txt_new_linen(const char *str, int str_len)
Definition text.cc:617
static void txt_convert_tab_to_spaces(Text *text)
Definition text.cc:1870
bool BKE_text_reload(struct Text *text)
Definition text.cc:419
void txt_backspace_char(struct Text *text)
Definition text.cc:1816
bool text_check_identifier_nodigit(char ch)
Definition text.cc:2346
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void txt_delete_line(Text *text, TextLine *line)
Definition text.cc:1706
void txt_move_bof(struct Text *text, bool sel)
Definition text.cc:1053
bool txt_has_sel(const struct Text *text)
void BPY_text_free_code(Text *text)
unsigned int BLI_str_utf8_as_unicode_step_safe(const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
char * txt_to_buf_for_undo(struct Text *text, size_t *r_buf_len) ATTR_NONNULL(1
Definition text.cc:1349
int text_find_identifier_start(const char *str, int i)
Definition text.cc:2386
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
int char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void txt_sel_set(struct Text *text, int startl, int startc, int endl, int endc)
Definition text.cc:1287
static void txt_curs_cur(Text *text, TextLine ***linep, int **charp)
Definition text.cc:737
void txt_sel_clear(struct Text *text)
Definition text.cc:1268
void txt_duplicate_line(struct Text *text)
Definition text.cc:1754
bool txt_add_char(struct Text *text, unsigned int add)
Definition text.cc:1924
void txt_comment(struct Text *text, const char *prefix)
Definition text.cc:2144
void txt_move_toline(struct Text *text, unsigned int line, bool sel)
Definition text.cc:1099
static TextLine * txt_line_malloc() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition text.cc:595
static void text_init_data(ID *id)
Definition text.cc:64
bool text_check_delim(char ch)
Definition text.cc:2296
bool txt_cursor_is_line_start(const struct Text *text)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
bool txt_unindent(struct Text *text)
Definition text.cc:2174
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
bool txt_find_string(struct Text *text, const char *findstr, int wrap, int match_case)
Definition text.cc:1599
static void txt_pop_first(Text *text)
Definition text.cc:1159
@ TXT_MOVE_LINE_UP
Definition BKE_text.h:144
@ TXT_MOVE_LINE_DOWN
Definition BKE_text.h:145
int text_check_identifier_nodigit_unicode(unsigned int ch)
Definition text.cc:2372
struct Text * BKE_text_load_ex(struct Main *bmain, const char *filepath, const char *relbase, bool is_internal) ATTR_NONNULL(1
Definition text.cc:456
void txt_delete_word(struct Text *text)
Definition text.cc:1809
unsigned char uchar
Definition BKE_text.h:58
int txt_calc_tab_right(const struct TextLine *tl, int ch)
IDTypeInfo IDType_ID_TXT
Definition text.cc:230
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
int txt_setcurr_tab_spaces(struct Text *text, int space)
Definition text.cc:2216
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
bool txt_cursor_is_line_end(const struct Text *text)
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void txt_pop_sel(struct Text *text)
Definition text.cc:1181
void txt_sel_all(struct Text *text)
Definition text.cc:1259
void txt_move_lines(struct Text *text, int direction)
Definition text.cc:2185
void txt_clean_text(struct Text *text)
Definition text.cc:638
void void txt_split_curline(struct Text *text)
Definition text.cc:1657
static void text_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition text.cc:165
void txt_from_buf_for_undo(struct Text *text, const char *buf, size_t buf_len) ATTR_NONNULL(1
Definition text.cc:1366
void txt_delete_selected(struct Text *text)
Definition text.cc:1934
bool BLI_str_cursor_step_prev_utf8(const char *str, int str_maxlen, int *pos)
int BLI_str_utf8_offset_to_column_with_tabs(const char *str, size_t str_len, int offset_target, int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
ATTR_WARN_UNUSED_RESULT const size_t num
bool text_check_whitespace(char ch)
Definition text.cc:2378
int text_check_identifier_unicode(unsigned int ch)
Definition text.cc:2367
void txt_move_left(struct Text *text, bool sel)
Definition text.cc:874
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
void txt_move_down(struct Text *text, bool sel)
Definition text.cc:801
char * txt_sel_to_buf(const struct Text *text, size_t *r_buf_strlen)
bool txt_uncomment(struct Text *text, const char *prefix)
Definition text.cc:2154
static void text_foreach_path(ID *id, BPathForeachPathData *bpath_data)
Definition text.cc:156
void int BKE_text_file_modified_check(const struct Text *text)
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void txt_move_right(struct Text *text, bool sel)
Definition text.cc:917
int text_check_bracket(char ch)
Definition text.cc:2279
static bool txt_select_unprefix(Text *text, const char *remove, const bool require_all)
Definition text.cc:2073
void txt_sel_line(struct Text *text)
Definition text.cc:1276
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void make_new_line(TextLine *line, char *newline)
Definition text.cc:603
void txt_move_bol(struct Text *text, bool sel)
Definition text.cc:1009
void txt_move_up(struct Text *text, bool sel)
Definition text.cc:769
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ INDEX_ID_TXT
Definition DNA_ID.h:1303
void BLI_str_cursor_step_utf8(const char *str, int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step)
@ USER_TXT_TABSTOSPACES_DISABLE
static void text_blend_read_data(BlendDataReader *reader, ID *id)
Definition text.cc:197
static void cleanup_textline(TextLine *tl)
Definition text.cc:342
static void txt_combine_lines(Text *text, TextLine *linea, TextLine *lineb)
Definition text.cc:1727
char * txt_to_buf(struct Text *text, size_t *r_buf_strlen) ATTR_NONNULL(1
Definition text.cc:1434
struct Text struct Text * BKE_text_load(struct Main *bmain, const char *filepath, const char *relbase) ATTR_NONNULL(1
Definition text.cc:510
void BKE_text_write(struct Text *text, const char *str, int str_len) ATTR_NONNULL(1
Definition text.cc:522
void txt_jump_right(struct Text *text, bool sel, bool use_init_step)
Definition text.cc:985
bool ATTR_WARN_UNUSED_RESULT
static void txt_curs_swap(Text *text)
Definition text.cc:1145
static void txt_pop_last(Text *text)
Definition text.cc:1170
struct stat BLI_stat_t
void txt_delete_char(struct Text *text)
Definition text.cc:1771
static void txt_curs_sel(Text *text, TextLine ***linep, int **charp)
Definition text.cc:743
void BKE_text_free_lines(struct Text *text)
Definition text.cc:268
static void text_from_buf(Text *text, const uchar *buffer, const int len)
Definition text.cc:360
void txt_move_eol(struct Text *text, bool sel)
Definition text.cc:1031
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void BKE_text_file_modified_ignore(struct Text *text)
Definition text.cc:563
void txt_order_cursors(struct Text *text, bool reverse)
Definition text.cc:1187
static char tab_to_spaces[]
Definition text.cc:1868
char ATTR_MALLOC
Definition BLI_string.hh:79
void id_us_min(ID *id)
Definition lib_id.cc:441
void txt_backspace_word(struct Text *text)
Definition text.cc:1858
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, bool sel)
Definition text.cc:1104
static void txt_delete_sel(Text *text)
Definition text.cc:1218
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
int BLI_str_utf8_offset_from_column_with_tabs(const char *str, size_t str_len, int column_target, int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool txt_replace_char(struct Text *text, unsigned int add)
Definition text.cc:1940
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
static void text_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
Definition text.cc:107
static void text_free_data(ID *id)
Definition text.cc:143
struct Text struct Text void BKE_text_clear(struct Text *text) ATTR_NONNULL(1)
Definition text.cc:515
bool txt_add_raw_char(struct Text *text, unsigned int add)
Definition text.cc:1929
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void write_string(const char *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
struct TextLine * prev
struct TextLine * next
#define N_(msgid)