Blender V5.0
text.cc File Reference
#include <cstdlib>
#include <cstring>
#include <cwctype>
#include <optional>
#include <sys/stat.h>
#include <sys/types.h>
#include "MEM_guardedalloc.h"
#include "BLI_fileops.h"
#include "BLI_listbase.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "BLI_string_cursor_utf8.h"
#include "BLI_string_utf8.h"
#include "BLI_utildefines.h"
#include "BLT_translation.hh"
#include "DNA_text_types.h"
#include "DNA_userdef_types.h"
#include "BKE_bpath.hh"
#include "BKE_idtype.hh"
#include "BKE_lib_id.hh"
#include "BKE_library.hh"
#include "BKE_main.hh"
#include "BKE_text.h"
#include "BLO_read_write.hh"

Go to the source code of this file.

Functions

Prototypes
static void txt_pop_first (Text *text)
static void txt_pop_last (Text *text)
static void txt_delete_line (Text *text, TextLine *line)
static void txt_delete_sel (Text *text)
static void txt_make_dirty (Text *text)
static TextLinetxt_line_malloc () ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Text Add, Free, Validation
void BKE_text_free_lines (Text *text)
TextBKE_text_add (Main *bmain, const char *name)
int txt_extended_ascii_as_utf8 (char **str)
static void cleanup_textline (TextLine *tl)
static void text_from_buf (Text *text, const uchar *buffer, const int len)
bool BKE_text_reload (Text *text)
TextBKE_text_load_ex (Main *bmain, const char *filepath, const char *relbase, const bool is_internal)
TextBKE_text_load (Main *bmain, const char *filepath, const char *relbase)
void BKE_text_clear (Text *text)
void BKE_text_write (Text *text, const char *str, int str_len)
int BKE_text_file_modified_check (const Text *text)
void BKE_text_file_modified_ignore (Text *text)
Editing Utility Functions
static void make_new_line (TextLine *line, char *newline)
static TextLinetxt_new_linen (const char *str, int str_len)
static TextLinetxt_new_line (const char *str)
void txt_clean_text (Text *text)
int txt_get_span (const TextLine *from, const TextLine *to)
Cursor Utility Functions
static void txt_curs_cur (Text *text, TextLine ***linep, int **charp)
static void txt_curs_sel (Text *text, TextLine ***linep, int **charp)
bool txt_cursor_is_line_start (const Text *text)
bool txt_cursor_is_line_end (const Text *text)
Cursor Movement Functions
Note
If the user moves the cursor the space containing that cursor should be popped See txt_pop_first, txt_pop_last Other space-types retain their own top location.
void txt_move_up (Text *text, const bool sel)
void txt_move_down (Text *text, const bool sel)
int txt_calc_tab_left (const TextLine *tl, int ch)
int txt_calc_tab_right (const TextLine *tl, int ch)
void txt_move_left (Text *text, const bool sel)
void txt_move_right (Text *text, const bool sel)
void txt_jump_left (Text *text, const bool sel, const bool use_init_step)
void txt_jump_right (Text *text, const bool sel, const bool use_init_step)
void txt_move_bol (Text *text, const bool sel)
void txt_move_eol (Text *text, const bool sel)
void txt_move_bof (Text *text, const bool sel)
void txt_move_eof (Text *text, const bool sel)
void txt_move_toline (Text *text, uint line, const bool sel)
void txt_move_to (Text *text, uint line, uint ch, const bool sel)
Text Selection Functions
static void txt_curs_swap (Text *text)
void txt_pop_sel (Text *text)
void txt_order_cursors (Text *text, const bool reverse)
bool txt_has_sel (const Text *text)
void txt_sel_all (Text *text)
void txt_sel_clear (Text *text)
void txt_sel_line (Text *text)
void txt_sel_set (Text *text, int startl, int startc, int endl, int endc)
Buffer Conversion for Undo/Redo

Buffer conversion functions that rely on the buffer already being validated.

The only requirement for these functions is that they're reverse-able, the undo logic doesn't inspect their content.

Currently buffers:

  • Always ends with a new-line.
  • Are not null terminated.
char * txt_to_buf_for_undo (Text *text, size_t *r_buf_len)
void txt_from_buf_for_undo (Text *text, const char *buf, size_t buf_len)
Cut and Paste Functions
char * txt_to_buf (Text *text, size_t *r_buf_strlen)
char * txt_sel_to_buf (const Text *text, size_t *r_buf_strlen)
void txt_insert_buf (Text *text, const char *in_buffer, int in_buffer_len)
Find String in Text
bool txt_find_string (Text *text, const char *findstr, int wrap, int match_case)
Character Queries
int text_check_bracket (const char ch)
bool text_check_delim (const char ch)
bool text_check_digit (const char ch)
bool text_check_identifier (const char ch)
bool text_check_identifier_nodigit (const char ch)
int text_check_identifier_unicode (const uint ch)
int text_check_identifier_nodigit_unicode (const uint ch)
bool text_check_whitespace (const char ch)
int text_find_identifier_start (const char *str, int i)

Text Data-Block

IDTypeInfo IDType_ID_TXT
static void text_init_data (ID *id)
static void text_copy_data (Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
static void text_free_data (ID *id)
static void text_foreach_path (ID *id, BPathForeachPathData *bpath_data)
static void text_blend_write (BlendWriter *writer, ID *id, const void *id_address)
static void text_blend_read_data (BlendDataReader *reader, ID *id)

Line Editing Functions

static char tab_to_spaces [] = " "
void txt_split_curline (Text *text)
static void txt_combine_lines (Text *text, TextLine *linea, TextLine *lineb)
void txt_duplicate_line (Text *text)
void txt_delete_char (Text *text)
void txt_delete_word (Text *text)
void txt_backspace_char (Text *text)
void txt_backspace_word (Text *text)
static void txt_convert_tab_to_spaces (Text *text)
static bool txt_add_char_intern (Text *text, uint add, bool replace_tabs)
bool txt_add_char (Text *text, uint add)
bool txt_add_raw_char (Text *text, uint add)
void txt_delete_selected (Text *text)
bool txt_replace_char (Text *text, uint add)
static void txt_select_prefix (Text *text, const char *add, bool skip_blank_lines)
static bool txt_select_unprefix (Text *text, const char *remove, const bool require_all)
void txt_comment (Text *text, const char *prefix)
bool txt_uncomment (Text *text, const char *prefix)
void txt_indent (Text *text)
bool txt_unindent (Text *text)
void txt_move_lines (Text *text, const int direction)
int txt_setcurr_tab_spaces (Text *text, int space)

Function Documentation

◆ BKE_text_add()

Text * BKE_text_add ( Main * bmain,
const char * name )

Definition at line 281 of file text.cc.

References BKE_id_new(), Text::id, id_fake_user_set(), id_us_min(), and name.

Referenced by text_new_exec().

◆ BKE_text_clear()

void BKE_text_clear ( Text * text)

Definition at line 511 of file text.cc.

References txt_delete_sel(), txt_make_dirty(), and txt_sel_all().

◆ BKE_text_file_modified_check()

int BKE_text_file_modified_check ( const Text * text)

◆ BKE_text_file_modified_ignore()

void BKE_text_file_modified_ignore ( Text * text)

◆ BKE_text_free_lines()

void BKE_text_free_lines ( struct Text * text)
Note
caller must handle compiled member.

Definition at line 264 of file text.cc.

References BLI_listbase_clear(), Text::curl, ListBase::first, Text::lines, MEM_freeN(), and Text::sell.

Referenced by BKE_text_reload(), and text_free_data().

◆ BKE_text_load()

Text * BKE_text_load ( Main * bmain,
const char * filepath,
const char * relbase )

Definition at line 506 of file text.cc.

References BKE_text_load_ex().

◆ BKE_text_load_ex()

◆ BKE_text_reload()

◆ BKE_text_write()

void BKE_text_write ( Text * text,
const char * str,
int str_len )

Definition at line 518 of file text.cc.

References str, txt_insert_buf(), txt_make_dirty(), and txt_move_eof().

◆ cleanup_textline()

void cleanup_textline ( TextLine * tl)
static

Removes any control characters from a text-line and fixes invalid UTF8 sequences.

Definition at line 338 of file text.cc.

References i, TextLine::len, TextLine::line, and txt_extended_ascii_as_utf8().

Referenced by text_from_buf().

◆ make_new_line()

void make_new_line ( TextLine * line,
char * newline )
static

◆ text_blend_read_data()

◆ text_blend_write()

void text_blend_write ( BlendWriter * writer,
ID * id,
const void * id_address )
static

◆ text_check_bracket()

int text_check_bracket ( char ch)

Utility functions, could be moved somewhere more generic but are python/text related.

Definition at line 2273 of file text.cc.

Referenced by draw_brackets(), and txt_calc_tab_right().

◆ text_check_delim()

◆ text_check_digit()

◆ text_check_identifier()

◆ text_check_identifier_nodigit()

bool text_check_identifier_nodigit ( const char ch)

◆ text_check_identifier_nodigit_unicode()

int text_check_identifier_nodigit_unicode ( const uint ch)

Definition at line 2366 of file text.cc.

◆ text_check_identifier_unicode()

int text_check_identifier_unicode ( const uint ch)

Definition at line 2361 of file text.cc.

◆ text_check_whitespace()

bool text_check_whitespace ( const char ch)

◆ text_copy_data()

void text_copy_data ( Main * ,
std::optional< Library * > ,
ID * id_dst,
const ID * id_src,
const int  )
static

Only copy internal data of Text ID from source to already allocated/initialized destination. You probably never want to use that directly, use BKE_id_copy or BKE_id_copy_ex for typical needs.

WARNING! This function will not handle ID user count!

Parameters
flagCopying options (see BKE_lib_id.hh's LIB_ID_COPY_... flags for more).

Definition at line 105 of file text.cc.

References BLI_addtail(), BLI_listbase_clear(), BLI_strdup(), BLI_strdupn(), Text::compiled, Text::curc, Text::curl, Text::filepath, ListBase::first, Text::flags, TextLine::format, TextLine::len, TextLine::line, Text::lines, LISTBASE_FOREACH, Text::selc, Text::sell, TXT_ISDIRTY, and txt_line_malloc().

◆ text_find_identifier_start()

int text_find_identifier_start ( const char * str,
int i )

◆ text_foreach_path()

void text_foreach_path ( ID * id,
BPathForeachPathData * bpath_data )
static

Definition at line 154 of file text.cc.

References BKE_bpath_foreach_path_allocated_process(), and Text::filepath.

◆ text_free_data()

void text_free_data ( ID * id)
static

Free (or release) any data used by this text (does not free the text itself).

Definition at line 141 of file text.cc.

References BKE_text_free_lines(), BPY_text_free_code(), Text::filepath, and MEM_SAFE_FREE.

◆ text_from_buf()

void text_from_buf ( Text * text,
const uchar * buffer,
const int len )
static

used for load and reload (unlike txt_insert_buf) assumes all fields are empty

Definition at line 356 of file text.cc.

References BLI_addtail(), BLI_assert, BLI_listbase_is_empty(), cleanup_textline(), Text::curc, Text::curl, ListBase::first, TextLine::format, i, len, TextLine::len, TextLine::line, Text::lines, MEM_malloc_arrayN(), Text::selc, Text::sell, and txt_line_malloc().

Referenced by BKE_text_load_ex(), and BKE_text_reload().

◆ text_init_data()

◆ txt_add_char()

◆ txt_add_char_intern()

◆ txt_add_raw_char()

bool txt_add_raw_char ( Text * text,
uint add )

Definition at line 1923 of file text.cc.

References add(), and txt_add_char_intern().

Referenced by txt_insert_buf(), and txt_insert_buf().

◆ txt_backspace_char()

◆ txt_backspace_word()

void txt_backspace_word ( Text * text)

Definition at line 1853 of file text.cc.

References txt_delete_sel(), txt_jump_left(), and txt_make_dirty().

Referenced by text_delete_exec(), and txt_insert_buf().

◆ txt_calc_tab_left()

int txt_calc_tab_left ( const TextLine * tl,
int ch )

Definition at line 829 of file text.cc.

References i, TextLine::line, and TXT_TABSIZE.

Referenced by txt_move_left().

◆ txt_calc_tab_right()

int txt_calc_tab_right ( const TextLine * tl,
int ch )

Definition at line 849 of file text.cc.

References i, TextLine::line, and TXT_TABSIZE.

Referenced by txt_move_right().

◆ txt_clean_text()

◆ txt_combine_lines()

void txt_combine_lines ( Text * text,
TextLine * linea,
TextLine * lineb )
static

◆ txt_comment()

void txt_comment ( Text * text,
const char * prefix )

Definition at line 2138 of file text.cc.

References Text::curl, ELEM, Text::sell, txt_has_sel(), and txt_select_prefix().

Referenced by text_comment_exec(), and txt_insert_buf().

◆ txt_convert_tab_to_spaces()

void txt_convert_tab_to_spaces ( Text * text)
static

Definition at line 1865 of file text.cc.

References Text::curc, tab_to_spaces, txt_insert_buf(), and TXT_TABSIZE.

Referenced by txt_add_char_intern().

◆ txt_curs_cur()

void txt_curs_cur ( Text * text,
TextLine *** linep,
int ** charp )
static

◆ txt_curs_sel()

void txt_curs_sel ( Text * text,
TextLine *** linep,
int ** charp )
static

◆ txt_curs_swap()

void txt_curs_swap ( Text * text)
static

Definition at line 1141 of file text.cc.

References Text::curc, Text::curl, Text::selc, and Text::sell.

Referenced by txt_order_cursors(), txt_pop_first(), and txt_pop_last().

◆ txt_cursor_is_line_end()

bool txt_cursor_is_line_end ( const Text * text)

Definition at line 750 of file text.cc.

References TextLine::len, Text::selc, and Text::sell.

◆ txt_cursor_is_line_start()

bool txt_cursor_is_line_start ( const Text * text)

Definition at line 745 of file text.cc.

References Text::selc.

◆ txt_delete_char()

◆ txt_delete_line()

void txt_delete_line ( Text * text,
TextLine * line )
static

◆ txt_delete_sel()

◆ txt_delete_selected()

void txt_delete_selected ( Text * text)

Definition at line 1928 of file text.cc.

References txt_delete_sel(), and txt_make_dirty().

Referenced by text_cut_exec(), and txt_get_span().

◆ txt_delete_word()

void txt_delete_word ( Text * text)

Definition at line 1804 of file text.cc.

References txt_delete_sel(), txt_jump_right(), and txt_make_dirty().

Referenced by text_delete_exec(), and txt_get_span().

◆ txt_duplicate_line()

void txt_duplicate_line ( Text * text)

◆ txt_extended_ascii_as_utf8()

int txt_extended_ascii_as_utf8 ( char ** str)

Use to a valid UTF8 sequences. this function replaces extended ASCII characters.

Definition at line 294 of file text.cc.

References BLI_str_utf8_from_unicode(), BLI_str_utf8_invalid_byte(), i, length(), MEM_freeN(), MEM_malloc_arrayN(), and str.

Referenced by blo_do_versions_260(), cleanup_textline(), and txt_insert_buf().

◆ txt_find_string()

bool txt_find_string ( Text * text,
const char * findstr,
int wrap,
int match_case )

◆ txt_from_buf_for_undo()

◆ txt_get_span()

int txt_get_span ( const TextLine * from,
const TextLine * to )

◆ txt_has_sel()

bool txt_has_sel ( const Text * text)

Definition at line 1209 of file text.cc.

References Text::curc, Text::curl, Text::selc, and Text::sell.

Referenced by txt_backspace_char(), txt_comment(), txt_delete_char(), and txt_delete_sel().

◆ txt_indent()

void txt_indent ( Text * text)

◆ txt_insert_buf()

◆ txt_jump_left()

void txt_jump_left ( Text * text,
const bool sel,
const bool use_init_step )

◆ txt_jump_right()

void txt_jump_right ( Text * text,
const bool sel,
const bool use_init_step )

◆ txt_line_malloc()

TextLine * txt_line_malloc ( )
static

◆ txt_make_dirty()

◆ txt_move_bof()

void txt_move_bof ( Text * text,
const bool sel )

Definition at line 1049 of file text.cc.

References ListBase::first, linep, Text::lines, txt_curs_cur(), txt_curs_sel(), and txt_pop_sel().

Referenced by text_move_cursor(), and txt_get_span().

◆ txt_move_bol()

void txt_move_bol ( Text * text,
const bool sel )

Definition at line 1005 of file text.cc.

References linep, txt_curs_cur(), txt_curs_sel(), and txt_pop_sel().

Referenced by text_move_cursor(), txt_get_span(), and txt_move_up().

◆ txt_move_down()

◆ txt_move_eof()

void txt_move_eof ( Text * text,
const bool sel )

◆ txt_move_eol()

void txt_move_eol ( Text * text,
const bool sel )

Definition at line 1027 of file text.cc.

References linep, txt_curs_cur(), txt_curs_sel(), and txt_pop_sel().

Referenced by text_move_cursor(), txt_get_span(), and txt_move_down().

◆ txt_move_left()

◆ txt_move_lines()

◆ txt_move_right()

◆ txt_move_to()

void txt_move_to ( struct Text * text,
unsigned int line,
unsigned int ch,
bool sel )

◆ txt_move_toline()

void txt_move_toline ( Text * text,
uint line,
const bool sel )

◆ txt_move_up()

◆ txt_new_line()

TextLine * txt_new_line ( const char * str)
static

Definition at line 629 of file text.cc.

References str, and txt_new_linen().

Referenced by txt_clean_text(), and txt_duplicate_line().

◆ txt_new_linen()

TextLine * txt_new_linen ( const char * str,
int str_len )
static

◆ txt_order_cursors()

◆ txt_pop_first()

void txt_pop_first ( Text * text)
static

◆ txt_pop_last()

void txt_pop_last ( Text * text)
static

◆ txt_pop_sel()

◆ txt_replace_char()

◆ txt_sel_all()

void txt_sel_all ( Text * text)

◆ txt_sel_clear()

void txt_sel_clear ( struct Text * text)

Reverse of txt_pop_sel Clears the selection and ensures the cursor is located at the selection (where the cursor is visually while editing).

Definition at line 1264 of file text.cc.

References Text::curc, Text::curl, Text::selc, and Text::sell.

Referenced by text_move_cursor(), and txt_get_span().

◆ txt_sel_line()

void txt_sel_line ( Text * text)

Definition at line 1272 of file text.cc.

References Text::curc, Text::curl, TextLine::len, Text::selc, and Text::sell.

Referenced by text_select_line_exec(), and txt_get_span().

◆ txt_sel_set()

◆ txt_sel_to_buf()

char * txt_sel_to_buf ( const Text * text,
size_t * r_buf_strlen )

◆ txt_select_prefix()

void txt_select_prefix ( Text * text,
const char * add,
bool skip_blank_lines )
static

Generic prefix operation, use for comment & indent.

Note
caller must handle undo.

Definition at line 1987 of file text.cc.

References add(), BLI_assert, Text::curc, Text::curl, ELEM, len, TextLine::len, TextLine::line, make_new_line(), MEM_malloc_arrayN(), TextLine::next, num, TextLine::prev, Text::selc, Text::sell, txt_clean_text(), and txt_make_dirty().

Referenced by txt_comment(), and txt_indent().

◆ txt_select_unprefix()

bool txt_select_unprefix ( Text * text,
const char * remove,
const bool require_all )
static

Generic un-prefix operation, use for comment & indent.

Parameters
require_allWhen true, all non-empty lines must have this prefix. Needed for comments where we might want to un-comment a block which contains some comments.
Note
caller must handle undo.

Definition at line 2067 of file text.cc.

References BLI_assert, Text::curc, Text::curl, ELEM, i, l, TextLine::len, TextLine::line, TextLine::next, num, TextLine::prev, Text::selc, Text::sell, STREQLEN, txt_clean_text(), and txt_make_dirty().

Referenced by txt_uncomment(), and txt_unindent().

◆ txt_setcurr_tab_spaces()

int txt_setcurr_tab_spaces ( Text * text,
int space )

Definition at line 2210 of file text.cc.

References Text::curc, Text::curl, ELEM, Text::flags, i, TextLine::line, and TXT_TABSTOSPACES.

Referenced by text_line_break_exec(), and txt_insert_buf().

◆ txt_split_curline()

◆ txt_to_buf()

char * txt_to_buf ( Text * text,
size_t * r_buf_strlen )

Definition at line 1430 of file text.cc.

References BLI_listbase_is_empty(), l, Text::lines, LISTBASE_FOREACH, and MEM_malloc_arrayN().

◆ txt_to_buf_for_undo()

char * txt_to_buf_for_undo ( Text * text,
size_t * r_buf_len )

Definition at line 1345 of file text.cc.

References l, Text::lines, LISTBASE_FOREACH, and MEM_malloc_arrayN().

◆ txt_uncomment()

bool txt_uncomment ( Text * text,
const char * prefix )

Definition at line 2148 of file text.cc.

References Text::curl, ELEM, Text::sell, and txt_select_unprefix().

Referenced by text_comment_exec(), and txt_insert_buf().

◆ txt_unindent()

bool txt_unindent ( Text * text)

Variable Documentation

◆ IDType_ID_TXT

IDTypeInfo IDType_ID_TXT
Initial value:
= {
Text::id_type,
0,
sizeof(Text),
"Text",
N_("texts"),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
}
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:47
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:49
#define BLT_I18NCONTEXT_ID_TEXT
@ INDEX_ID_TXT
Definition DNA_ID.h:1301
#define FILTER_ID_TXT
Definition DNA_ID.h:1221
static void text_foreach_path(ID *id, BPathForeachPathData *bpath_data)
Definition text.cc:154
static void text_free_data(ID *id)
Definition text.cc:141
static void text_blend_read_data(BlendDataReader *reader, ID *id)
Definition text.cc:195
static void text_init_data(ID *id)
Definition text.cc:62
static void text_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition text.cc:163
static void text_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
Definition text.cc:105
#define N_(msgid)

Definition at line 227 of file text.cc.

◆ tab_to_spaces

char tab_to_spaces[] = " "
static

Definition at line 1863 of file text.cc.

Referenced by txt_convert_tab_to_spaces(), txt_indent(), and txt_unindent().