Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/blenkernel/intern/text.c')
-rw-r--r--source/blender/blenkernel/intern/text.c110
1 files changed, 91 insertions, 19 deletions
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index 322b77e0462..3936c533a41 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -38,12 +38,12 @@
#include "MEM_guardedalloc.h"
+#include "BLI_utildefines.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_string_cursor_utf8.h"
#include "BLI_string_utf8.h"
#include "BLI_listbase.h"
-#include "BLI_utildefines.h"
#include "BLI_fileops.h"
#include "DNA_constraint_types.h"
@@ -171,9 +171,8 @@ void BKE_text_free(Text *text)
#endif
}
-Text *BKE_text_add(const char *name)
+Text *BKE_text_add(Main *bmain, const char *name)
{
- Main *bmain = G.main;
Text *ta;
TextLine *tmp;
@@ -363,9 +362,8 @@ int BKE_text_reload(Text *text)
return 1;
}
-Text *BKE_text_load(const char *file, const char *relpath)
+Text *BKE_text_load(Main *bmain, const char *file, const char *relpath)
{
- Main *bmain = G.main;
FILE *fp;
int i, llen, len;
unsigned char *buffer;
@@ -793,6 +791,29 @@ int txt_utf8_index_to_offset(const char *str, int index)
return offset;
}
+int txt_utf8_offset_to_column(const char *str, int offset)
+{
+ int column = 0, pos = 0;
+ while (pos < offset) {
+ column += BLI_str_utf8_char_width_safe(str + pos);
+ pos += BLI_str_utf8_size_safe(str + pos);
+ }
+ return column;
+}
+
+int txt_utf8_column_to_offset(const char *str, int column)
+{
+ int offset = 0, pos = 0, col;
+ while (pos < column) {
+ col = BLI_str_utf8_char_width_safe(str + offset);
+ if (pos + col > column)
+ break;
+ offset += BLI_str_utf8_size_safe(str + offset);
+ pos += col;
+ }
+ return offset;
+}
+
/* returns the real number of characters in string */
/* not the same as BLI_strlen_utf8, which returns length for wide characters */
static int txt_utf8_len(const char *src)
@@ -806,6 +827,17 @@ static int txt_utf8_len(const char *src)
return len;
}
+static int txt_utf8_width(const char *src)
+{
+ int col = 0;
+
+ for (; *src; src += BLI_str_utf8_size(src)) {
+ col += BLI_str_utf8_char_width(src);
+ }
+
+ return col;
+}
+
void txt_move_up(Text *text, short sel)
{
TextLine **linep;
@@ -817,10 +849,10 @@ void txt_move_up(Text *text, short sel)
if (!*linep) return;
if ((*linep)->prev) {
- int index = txt_utf8_offset_to_index((*linep)->line, *charp);
+ int column = txt_utf8_offset_to_column((*linep)->line, *charp);
*linep = (*linep)->prev;
- if (index > txt_utf8_len((*linep)->line)) *charp = (*linep)->len;
- else *charp = txt_utf8_index_to_offset((*linep)->line, index);
+ if (column > txt_utf8_width((*linep)->line)) *charp = (*linep)->len;
+ else *charp = txt_utf8_column_to_offset((*linep)->line, column);
}
else {
@@ -841,10 +873,10 @@ void txt_move_down(Text *text, short sel)
if (!*linep) return;
if ((*linep)->next) {
- int index = txt_utf8_offset_to_index((*linep)->line, *charp);
+ int column = txt_utf8_offset_to_column((*linep)->line, *charp);
*linep = (*linep)->next;
- if (index > txt_utf8_len((*linep)->line)) *charp = (*linep)->len;
- else *charp = txt_utf8_index_to_offset((*linep)->line, index);
+ if (column > txt_utf8_width((*linep)->line)) *charp = (*linep)->len;
+ else *charp = txt_utf8_column_to_offset((*linep)->line, column);
}
else {
txt_move_eol(text, sel);
@@ -932,13 +964,15 @@ void txt_move_right(Text *text, short sel)
tabsize++;
(*charp) = i;
}
- else (*charp) += BLI_str_utf8_size((*linep)->line + *charp);
+ else {
+ (*charp) += BLI_str_utf8_size((*linep)->line + *charp);
+ }
}
if (!sel) txt_pop_sel(text);
}
-void txt_jump_left(Text *text, short sel)
+void txt_jump_left(Text *text, bool sel, bool use_init_step)
{
TextLine **linep;
int *charp;
@@ -950,12 +984,12 @@ void txt_jump_left(Text *text, short sel)
BLI_str_cursor_step_utf8((*linep)->line, (*linep)->len,
charp, STRCUR_DIR_PREV,
- STRCUR_JUMP_DELIM);
+ STRCUR_JUMP_DELIM, use_init_step);
if (!sel) txt_pop_sel(text);
}
-void txt_jump_right(Text *text, short sel)
+void txt_jump_right(Text *text, bool sel, bool use_init_step)
{
TextLine **linep;
int *charp;
@@ -967,7 +1001,7 @@ void txt_jump_right(Text *text, short sel)
BLI_str_cursor_step_utf8((*linep)->line, (*linep)->len,
charp, STRCUR_DIR_NEXT,
- STRCUR_JUMP_DELIM);
+ STRCUR_JUMP_DELIM, use_init_step);
if (!sel) txt_pop_sel(text);
}
@@ -2404,7 +2438,7 @@ void txt_delete_char(Text *text)
void txt_delete_word(Text *text)
{
- txt_jump_right(text, 1);
+ txt_jump_right(text, true, true);
txt_delete_sel(text);
}
@@ -2453,7 +2487,7 @@ void txt_backspace_char(Text *text)
void txt_backspace_word(Text *text)
{
- txt_jump_left(text, 1);
+ txt_jump_left(text, true, true);
txt_delete_sel(text);
}
@@ -2562,6 +2596,7 @@ int txt_replace_char(Text *text, unsigned int add)
memcpy(text->curl->line + text->curc, ch, add_size);
text->curc += add_size;
+ text->curl->len += add_size - del_size;
txt_pop_sel(text);
txt_make_dirty(text);
@@ -2826,7 +2861,7 @@ void txt_move_lines(struct Text *text, const int direction)
}
}
-int setcurr_tab_spaces(Text *text, int space)
+int txt_setcurr_tab_spaces(Text *text, int space)
{
int i = 0;
int test = 0;
@@ -2930,9 +2965,46 @@ int text_check_identifier(const char ch)
return 0;
}
+int text_check_identifier_nodigit(const char ch)
+{
+ if (ch <= '9') return 0;
+ if (ch < 'A') return 0;
+ if (ch <= 'Z' || ch == '_') return 1;
+ if (ch < 'a') return 0;
+ if (ch <= 'z') return 1;
+ return 0;
+}
+
+#ifndef WITH_PYTHON
+int text_check_identifier_unicode(const unsigned int ch)
+{
+ return (ch < 255 && text_check_identifier((char)ch));
+}
+
+int text_check_identifier_nodigit_unicode(const unsigned int ch)
+{
+ return (ch < 255 && text_check_identifier_nodigit((char)ch));
+}
+#endif /* WITH_PYTHON */
+
int text_check_whitespace(const char ch)
{
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
return 1;
return 0;
}
+
+int text_find_identifier_start(const char *str, int i)
+{
+ if (UNLIKELY(i <= 0)) {
+ return 0;
+ }
+
+ while (i--) {
+ if (!text_check_identifier(str[i])) {
+ break;
+ }
+ }
+ i++;
+ return i;
+}