diff options
Diffstat (limited to 'source/blender/editors/space_text')
-rw-r--r-- | source/blender/editors/space_text/CMakeLists.txt | 66 | ||||
-rw-r--r-- | source/blender/editors/space_text/space_text.c | 617 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_autocomplete.c | 995 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_draw.c | 2824 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format.c | 240 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format.h | 110 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format_lua.c | 514 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format_osl.c | 566 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format_pov.c | 1693 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format_pov_ini.c | 894 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_format_py.c | 528 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_header.c | 224 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_intern.h | 57 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_ops.c | 4689 | ||||
-rw-r--r-- | source/blender/editors/space_text/text_undo.c | 213 |
15 files changed, 7286 insertions, 6944 deletions
diff --git a/source/blender/editors/space_text/CMakeLists.txt b/source/blender/editors/space_text/CMakeLists.txt index f4054a0dd0e..f5db86ac4c2 100644 --- a/source/blender/editors/space_text/CMakeLists.txt +++ b/source/blender/editors/space_text/CMakeLists.txt @@ -16,57 +16,57 @@ # ***** END GPL LICENSE BLOCK ***** set(INC - ../include - ../../blenfont - ../../blenkernel - ../../blenlib - ../../blentranslation - ../../gpu - ../../makesdna - ../../makesrna - ../../windowmanager - ../../../../intern/guardedalloc - ../../../../intern/glew-mx + ../include + ../../blenfont + ../../blenkernel + ../../blenlib + ../../blentranslation + ../../gpu + ../../makesdna + ../../makesrna + ../../windowmanager + ../../../../intern/guardedalloc + ../../../../intern/glew-mx ) set(INC_SYS - ${GLEW_INCLUDE_PATH} + ${GLEW_INCLUDE_PATH} ) set(SRC - space_text.c - text_autocomplete.c - text_draw.c - text_format.c - text_format_lua.c - text_format_osl.c - text_format_pov.c - text_format_pov_ini.c - text_format_py.c - text_header.c - text_ops.c - text_undo.c + space_text.c + text_autocomplete.c + text_draw.c + text_format.c + text_format_lua.c + text_format_osl.c + text_format_pov.c + text_format_pov_ini.c + text_format_py.c + text_header.c + text_ops.c + text_undo.c - text_format.h - text_intern.h + text_format.h + text_intern.h ) set(LIB - bf_blenkernel - bf_blenlib + bf_blenkernel + bf_blenlib ) add_definitions(${GL_DEFINITIONS}) if(WITH_PYTHON) - list(APPEND INC - ../../python - ) - add_definitions(-DWITH_PYTHON) + list(APPEND INC + ../../python + ) + add_definitions(-DWITH_PYTHON) endif() if(WITH_INTERNATIONAL) - add_definitions(-DWITH_INTERNATIONAL) + add_definitions(-DWITH_INTERNATIONAL) endif() blender_add_lib(bf_editor_space_text "${SRC}" "${INC}" "${INC_SYS}" "${LIB}") diff --git a/source/blender/editors/space_text/space_text.c b/source/blender/editors/space_text/space_text.c index 55165e4840f..9232d0322ab 100644 --- a/source/blender/editors/space_text/space_text.c +++ b/source/blender/editors/space_text/space_text.c @@ -21,7 +21,6 @@ * \ingroup sptext */ - #include <string.h> #include "DNA_text_types.h" @@ -38,7 +37,6 @@ #include "ED_space_api.h" #include "ED_screen.h" - #include "WM_api.h" #include "WM_types.h" @@ -48,217 +46,217 @@ #include "RNA_access.h" - #include "text_format.h" -#include "text_intern.h" /* own include */ +#include "text_intern.h" /* own include */ #include "GPU_framebuffer.h" /* ******************** default callbacks for text space ***************** */ static SpaceLink *text_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene)) { - ARegion *ar; - SpaceText *stext; + ARegion *ar; + SpaceText *stext; - stext = MEM_callocN(sizeof(SpaceText), "inittext"); - stext->spacetype = SPACE_TEXT; + stext = MEM_callocN(sizeof(SpaceText), "inittext"); + stext->spacetype = SPACE_TEXT; - stext->lheight = 12; - stext->tabnumber = 4; - stext->margin_column = 80; + stext->lheight = 12; + stext->tabnumber = 4; + stext->margin_column = 80; - /* header */ - ar = MEM_callocN(sizeof(ARegion), "header for text"); + /* header */ + ar = MEM_callocN(sizeof(ARegion), "header for text"); - BLI_addtail(&stext->regionbase, ar); - ar->regiontype = RGN_TYPE_HEADER; - ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP; + BLI_addtail(&stext->regionbase, ar); + ar->regiontype = RGN_TYPE_HEADER; + ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP; - /* footer */ - ar = MEM_callocN(sizeof(ARegion), "footer for text"); - BLI_addtail(&stext->regionbase, ar); - ar->regiontype = RGN_TYPE_FOOTER; - ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_TOP : RGN_ALIGN_BOTTOM; + /* footer */ + ar = MEM_callocN(sizeof(ARegion), "footer for text"); + BLI_addtail(&stext->regionbase, ar); + ar->regiontype = RGN_TYPE_FOOTER; + ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_TOP : RGN_ALIGN_BOTTOM; - /* properties region */ - ar = MEM_callocN(sizeof(ARegion), "properties region for text"); + /* properties region */ + ar = MEM_callocN(sizeof(ARegion), "properties region for text"); - BLI_addtail(&stext->regionbase, ar); - ar->regiontype = RGN_TYPE_UI; - ar->alignment = RGN_ALIGN_LEFT; - ar->flag = RGN_FLAG_HIDDEN; + BLI_addtail(&stext->regionbase, ar); + ar->regiontype = RGN_TYPE_UI; + ar->alignment = RGN_ALIGN_LEFT; + ar->flag = RGN_FLAG_HIDDEN; - /* main region */ - ar = MEM_callocN(sizeof(ARegion), "main region for text"); + /* main region */ + ar = MEM_callocN(sizeof(ARegion), "main region for text"); - BLI_addtail(&stext->regionbase, ar); - ar->regiontype = RGN_TYPE_WINDOW; + BLI_addtail(&stext->regionbase, ar); + ar->regiontype = RGN_TYPE_WINDOW; - return (SpaceLink *)stext; + return (SpaceLink *)stext; } /* not spacelink itself */ static void text_free(SpaceLink *sl) { - SpaceText *stext = (SpaceText *) sl; + SpaceText *stext = (SpaceText *)sl; - stext->text = NULL; - text_free_caches(stext); + stext->text = NULL; + text_free_caches(stext); } - /* spacetype; init callback */ static void text_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa)) { - } static SpaceLink *text_duplicate(SpaceLink *sl) { - SpaceText *stextn = MEM_dupallocN(sl); + SpaceText *stextn = MEM_dupallocN(sl); - /* clear or remove stuff from old */ + /* clear or remove stuff from old */ - stextn->drawcache = NULL; /* space need it's own cache */ + stextn->drawcache = NULL; /* space need it's own cache */ - return (SpaceLink *)stextn; + return (SpaceLink *)stextn; } -static void text_listener(wmWindow *UNUSED(win), ScrArea *sa, wmNotifier *wmn, Scene *UNUSED(scene)) +static void text_listener(wmWindow *UNUSED(win), + ScrArea *sa, + wmNotifier *wmn, + Scene *UNUSED(scene)) { - SpaceText *st = sa->spacedata.first; - - /* context changes */ - switch (wmn->category) { - case NC_TEXT: - /* check if active text was changed, no need to redraw if text isn't active - * (reference == NULL) means text was unlinked, should update anyway for this - * case -- no way to know was text active before unlinking or not */ - if (wmn->reference && wmn->reference != st->text) - break; - - switch (wmn->data) { - case ND_DISPLAY: - ED_area_tag_redraw(sa); - break; - case ND_CURSOR: - if (st->text && st->text == wmn->reference) - text_scroll_to_cursor__area(st, sa, true); - - ED_area_tag_redraw(sa); - break; - } - - switch (wmn->action) { - case NA_EDITED: - if (st->text) { - text_drawcache_tag_update(st, 1); - text_update_edited(st->text); - } - - ED_area_tag_redraw(sa); - ATTR_FALLTHROUGH; /* fall down to tag redraw */ - case NA_ADDED: - case NA_REMOVED: - ED_area_tag_redraw(sa); - break; - case NA_SELECTED: - if (st->text && st->text == wmn->reference) - text_scroll_to_cursor__area(st, sa, true); - - break; - } - - break; - case NC_SPACE: - if (wmn->data == ND_SPACE_TEXT) - ED_area_tag_redraw(sa); - break; - } + SpaceText *st = sa->spacedata.first; + + /* context changes */ + switch (wmn->category) { + case NC_TEXT: + /* check if active text was changed, no need to redraw if text isn't active + * (reference == NULL) means text was unlinked, should update anyway for this + * case -- no way to know was text active before unlinking or not */ + if (wmn->reference && wmn->reference != st->text) + break; + + switch (wmn->data) { + case ND_DISPLAY: + ED_area_tag_redraw(sa); + break; + case ND_CURSOR: + if (st->text && st->text == wmn->reference) + text_scroll_to_cursor__area(st, sa, true); + + ED_area_tag_redraw(sa); + break; + } + + switch (wmn->action) { + case NA_EDITED: + if (st->text) { + text_drawcache_tag_update(st, 1); + text_update_edited(st->text); + } + + ED_area_tag_redraw(sa); + ATTR_FALLTHROUGH; /* fall down to tag redraw */ + case NA_ADDED: + case NA_REMOVED: + ED_area_tag_redraw(sa); + break; + case NA_SELECTED: + if (st->text && st->text == wmn->reference) + text_scroll_to_cursor__area(st, sa, true); + + break; + } + + break; + case NC_SPACE: + if (wmn->data == ND_SPACE_TEXT) + ED_area_tag_redraw(sa); + break; + } } static void text_operatortypes(void) { - WM_operatortype_append(TEXT_OT_new); - WM_operatortype_append(TEXT_OT_open); - WM_operatortype_append(TEXT_OT_reload); - WM_operatortype_append(TEXT_OT_unlink); - WM_operatortype_append(TEXT_OT_save); - WM_operatortype_append(TEXT_OT_save_as); - WM_operatortype_append(TEXT_OT_make_internal); - WM_operatortype_append(TEXT_OT_run_script); - WM_operatortype_append(TEXT_OT_refresh_pyconstraints); - - WM_operatortype_append(TEXT_OT_paste); - WM_operatortype_append(TEXT_OT_copy); - WM_operatortype_append(TEXT_OT_cut); - WM_operatortype_append(TEXT_OT_duplicate_line); - - WM_operatortype_append(TEXT_OT_convert_whitespace); - WM_operatortype_append(TEXT_OT_uncomment); - WM_operatortype_append(TEXT_OT_comment); - WM_operatortype_append(TEXT_OT_unindent); - WM_operatortype_append(TEXT_OT_indent); - - WM_operatortype_append(TEXT_OT_select_line); - WM_operatortype_append(TEXT_OT_select_all); - WM_operatortype_append(TEXT_OT_select_word); - - WM_operatortype_append(TEXT_OT_move_lines); - - WM_operatortype_append(TEXT_OT_jump); - WM_operatortype_append(TEXT_OT_move); - WM_operatortype_append(TEXT_OT_move_select); - WM_operatortype_append(TEXT_OT_delete); - WM_operatortype_append(TEXT_OT_overwrite_toggle); - - WM_operatortype_append(TEXT_OT_selection_set); - WM_operatortype_append(TEXT_OT_cursor_set); - WM_operatortype_append(TEXT_OT_scroll); - WM_operatortype_append(TEXT_OT_scroll_bar); - WM_operatortype_append(TEXT_OT_line_number); - - WM_operatortype_append(TEXT_OT_line_break); - WM_operatortype_append(TEXT_OT_insert); - - WM_operatortype_append(TEXT_OT_properties); - - WM_operatortype_append(TEXT_OT_find); - WM_operatortype_append(TEXT_OT_find_set_selected); - WM_operatortype_append(TEXT_OT_replace); - WM_operatortype_append(TEXT_OT_replace_set_selected); - - WM_operatortype_append(TEXT_OT_start_find); - - WM_operatortype_append(TEXT_OT_to_3d_object); - - WM_operatortype_append(TEXT_OT_resolve_conflict); - - WM_operatortype_append(TEXT_OT_autocomplete); + WM_operatortype_append(TEXT_OT_new); + WM_operatortype_append(TEXT_OT_open); + WM_operatortype_append(TEXT_OT_reload); + WM_operatortype_append(TEXT_OT_unlink); + WM_operatortype_append(TEXT_OT_save); + WM_operatortype_append(TEXT_OT_save_as); + WM_operatortype_append(TEXT_OT_make_internal); + WM_operatortype_append(TEXT_OT_run_script); + WM_operatortype_append(TEXT_OT_refresh_pyconstraints); + + WM_operatortype_append(TEXT_OT_paste); + WM_operatortype_append(TEXT_OT_copy); + WM_operatortype_append(TEXT_OT_cut); + WM_operatortype_append(TEXT_OT_duplicate_line); + + WM_operatortype_append(TEXT_OT_convert_whitespace); + WM_operatortype_append(TEXT_OT_uncomment); + WM_operatortype_append(TEXT_OT_comment); + WM_operatortype_append(TEXT_OT_unindent); + WM_operatortype_append(TEXT_OT_indent); + + WM_operatortype_append(TEXT_OT_select_line); + WM_operatortype_append(TEXT_OT_select_all); + WM_operatortype_append(TEXT_OT_select_word); + + WM_operatortype_append(TEXT_OT_move_lines); + + WM_operatortype_append(TEXT_OT_jump); + WM_operatortype_append(TEXT_OT_move); + WM_operatortype_append(TEXT_OT_move_select); + WM_operatortype_append(TEXT_OT_delete); + WM_operatortype_append(TEXT_OT_overwrite_toggle); + + WM_operatortype_append(TEXT_OT_selection_set); + WM_operatortype_append(TEXT_OT_cursor_set); + WM_operatortype_append(TEXT_OT_scroll); + WM_operatortype_append(TEXT_OT_scroll_bar); + WM_operatortype_append(TEXT_OT_line_number); + + WM_operatortype_append(TEXT_OT_line_break); + WM_operatortype_append(TEXT_OT_insert); + + WM_operatortype_append(TEXT_OT_properties); + + WM_operatortype_append(TEXT_OT_find); + WM_operatortype_append(TEXT_OT_find_set_selected); + WM_operatortype_append(TEXT_OT_replace); + WM_operatortype_append(TEXT_OT_replace_set_selected); + + WM_operatortype_append(TEXT_OT_start_find); + + WM_operatortype_append(TEXT_OT_to_3d_object); + + WM_operatortype_append(TEXT_OT_resolve_conflict); + + WM_operatortype_append(TEXT_OT_autocomplete); } static void text_keymap(struct wmKeyConfig *keyconf) { - WM_keymap_ensure(keyconf, "Text Generic", SPACE_TEXT, 0); - WM_keymap_ensure(keyconf, "Text", SPACE_TEXT, 0); + WM_keymap_ensure(keyconf, "Text Generic", SPACE_TEXT, 0); + WM_keymap_ensure(keyconf, "Text", SPACE_TEXT, 0); } const char *text_context_dir[] = {"edit_text", NULL}; static int text_context(const bContext *C, const char *member, bContextDataResult *result) { - SpaceText *st = CTX_wm_space_text(C); - - if (CTX_data_dir(member)) { - CTX_data_dir_set(result, text_context_dir); - return 1; - } - else if (CTX_data_equals(member, "edit_text")) { - CTX_data_id_pointer_set(result, &st->text->id); - return 1; - } - - return 0; + SpaceText *st = CTX_wm_space_text(C); + + if (CTX_data_dir(member)) { + CTX_data_dir_set(result, text_context_dir); + return 1; + } + else if (CTX_data_equals(member, "edit_text")) { + CTX_data_id_pointer_set(result, &st->text->id); + return 1; + } + + return 0; } /********************* main region ********************/ @@ -266,116 +264,120 @@ static int text_context(const bContext *C, const char *member, bContextDataResul /* add handlers, stuff you only do once or on area/region changes */ static void text_main_region_init(wmWindowManager *wm, ARegion *ar) { - wmKeyMap *keymap; - ListBase *lb; + wmKeyMap *keymap; + ListBase *lb; - UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy); + UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy); - /* own keymap */ - keymap = WM_keymap_ensure(wm->defaultconf, "Text Generic", SPACE_TEXT, 0); - WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct); - keymap = WM_keymap_ensure(wm->defaultconf, "Text", SPACE_TEXT, 0); - WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct); + /* own keymap */ + keymap = WM_keymap_ensure(wm->defaultconf, "Text Generic", SPACE_TEXT, 0); + WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct); + keymap = WM_keymap_ensure(wm->defaultconf, "Text", SPACE_TEXT, 0); + WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct); - /* add drop boxes */ - lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW); + /* add drop boxes */ + lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW); - WM_event_add_dropbox_handler(&ar->handlers, lb); + WM_event_add_dropbox_handler(&ar->handlers, lb); } static void text_main_region_draw(const bContext *C, ARegion *ar) { - /* draw entirely, view changes should be handled here */ - SpaceText *st = CTX_wm_space_text(C); - //View2D *v2d = &ar->v2d; + /* draw entirely, view changes should be handled here */ + SpaceText *st = CTX_wm_space_text(C); + //View2D *v2d = &ar->v2d; - /* clear and setup matrix */ - UI_ThemeClearColor(TH_BACK); - GPU_clear(GPU_COLOR_BIT); + /* clear and setup matrix */ + UI_ThemeClearColor(TH_BACK); + GPU_clear(GPU_COLOR_BIT); - // UI_view2d_view_ortho(v2d); + // UI_view2d_view_ortho(v2d); - /* data... */ - draw_text_main(st, ar); + /* data... */ + draw_text_main(st, ar); - /* reset view matrix */ - // UI_view2d_view_restore(C); + /* reset view matrix */ + // UI_view2d_view_restore(C); - /* scrollers? */ + /* scrollers? */ } static void text_cursor(wmWindow *win, ScrArea *sa, ARegion *ar) { - SpaceText *st = sa->spacedata.first; - int wmcursor = BC_TEXTEDITCURSOR; + SpaceText *st = sa->spacedata.first; + int wmcursor = BC_TEXTEDITCURSOR; - if (st->text && BLI_rcti_isect_pt(&st->txtbar, win->eventstate->x - ar->winrct.xmin, st->txtbar.ymin)) { - wmcursor = CURSOR_STD; - } + if (st->text && + BLI_rcti_isect_pt(&st->txtbar, win->eventstate->x - ar->winrct.xmin, st->txtbar.ymin)) { + wmcursor = CURSOR_STD; + } - WM_cursor_set(win, wmcursor); + WM_cursor_set(win, wmcursor); } - - /* ************* dropboxes ************* */ -static bool text_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip)) +static bool text_drop_poll(bContext *UNUSED(C), + wmDrag *drag, + const wmEvent *UNUSED(event), + const char **UNUSED(tooltip)) { - if (drag->type == WM_DRAG_PATH) { - /* rule might not work? */ - if (ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_TEXT, ICON_FILE_BLANK)) { - return true; - } - } - return false; + if (drag->type == WM_DRAG_PATH) { + /* rule might not work? */ + if (ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_TEXT, ICON_FILE_BLANK)) { + return true; + } + } + return false; } static void text_drop_copy(wmDrag *drag, wmDropBox *drop) { - /* copy drag path to properties */ - RNA_string_set(drop->ptr, "filepath", drag->path); + /* copy drag path to properties */ + RNA_string_set(drop->ptr, "filepath", drag->path); } -static bool text_drop_paste_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip)) +static bool text_drop_paste_poll(bContext *UNUSED(C), + wmDrag *drag, + const wmEvent *UNUSED(event), + const char **UNUSED(tooltip)) { - return (drag->type == WM_DRAG_ID); + return (drag->type == WM_DRAG_ID); } static void text_drop_paste(wmDrag *drag, wmDropBox *drop) { - char *text; - ID *id = WM_drag_ID(drag, 0); + char *text; + ID *id = WM_drag_ID(drag, 0); - /* copy drag path to properties */ - text = RNA_path_full_ID_py(id); - RNA_string_set(drop->ptr, "text", text); - MEM_freeN(text); + /* copy drag path to properties */ + text = RNA_path_full_ID_py(id); + RNA_string_set(drop->ptr, "text", text); + MEM_freeN(text); } /* this region dropbox definition */ static void text_dropboxes(void) { - ListBase *lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW); + ListBase *lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW); - WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy); - WM_dropbox_add(lb, "TEXT_OT_insert", text_drop_paste_poll, text_drop_paste); + WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy); + WM_dropbox_add(lb, "TEXT_OT_insert", text_drop_paste_poll, text_drop_paste); } /* ************* end drop *********** */ - /****************** header region ******************/ /* add handlers, stuff you only do once or on area/region changes */ static void text_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar) { - ED_region_header_init(ar); + ED_region_header_init(ar); } static void text_header_region_draw(const bContext *C, ARegion *ar) { - ED_region_header(C, ar); + ED_region_header(C, ar); } /****************** properties region ******************/ @@ -383,46 +385,45 @@ static void text_header_region_draw(const bContext *C, ARegion *ar) /* add handlers, stuff you only do once or on area/region changes */ static void text_properties_region_init(wmWindowManager *wm, ARegion *ar) { - wmKeyMap *keymap; - - ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE; - ED_region_panels_init(wm, ar); + wmKeyMap *keymap; - /* own keymaps */ - keymap = WM_keymap_ensure(wm->defaultconf, "Text Generic", SPACE_TEXT, 0); - WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct); + ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE; + ED_region_panels_init(wm, ar); + /* own keymaps */ + keymap = WM_keymap_ensure(wm->defaultconf, "Text Generic", SPACE_TEXT, 0); + WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct); } static void text_properties_region_draw(const bContext *C, ARegion *ar) { - SpaceText *st = CTX_wm_space_text(C); - - ED_region_panels(C, ar); - - /* this flag trick is make sure buttons have been added already */ - if (st->flags & ST_FIND_ACTIVATE) { - if (UI_textbutton_activate_rna(C, ar, st, "find_text")) { - /* if the panel was already open we need to do another redraw */ - ScrArea *sa = CTX_wm_area(C); - WM_event_add_notifier(C, NC_SPACE | ND_SPACE_TEXT, sa); - } - st->flags &= ~ST_FIND_ACTIVATE; - } + SpaceText *st = CTX_wm_space_text(C); + + ED_region_panels(C, ar); + + /* this flag trick is make sure buttons have been added already */ + if (st->flags & ST_FIND_ACTIVATE) { + if (UI_textbutton_activate_rna(C, ar, st, "find_text")) { + /* if the panel was already open we need to do another redraw */ + ScrArea *sa = CTX_wm_area(C); + WM_event_add_notifier(C, NC_SPACE | ND_SPACE_TEXT, sa); + } + st->flags &= ~ST_FIND_ACTIVATE; + } } static void text_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id) { - SpaceText *stext = (SpaceText *)slink; + SpaceText *stext = (SpaceText *)slink; - if (!ELEM(GS(old_id->name), ID_TXT)) { - return; - } + if (!ELEM(GS(old_id->name), ID_TXT)) { + return; + } - if ((ID *)stext->text == old_id) { - stext->text = (Text *)new_id; - id_us_ensure_real(new_id); - } + if ((ID *)stext->text == old_id) { + stext->text = (Text *)new_id; + id_us_ensure_real(new_id); + } } /********************* registration ********************/ @@ -430,68 +431,68 @@ static void text_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID /* only called once, from space/spacetypes.c */ void ED_spacetype_text(void) { - SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype text"); - ARegionType *art; - - st->spaceid = SPACE_TEXT; - strncpy(st->name, "Text", BKE_ST_MAXNAME); - - st->new = text_new; - st->free = text_free; - st->init = text_init; - st->duplicate = text_duplicate; - st->operatortypes = text_operatortypes; - st->keymap = text_keymap; - st->listener = text_listener; - st->context = text_context; - st->dropboxes = text_dropboxes; - st->id_remap = text_id_remap; - - /* regions: main window */ - art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); - art->regionid = RGN_TYPE_WINDOW; - art->init = text_main_region_init; - art->draw = text_main_region_draw; - art->cursor = text_cursor; - art->event_cursor = true; - - BLI_addhead(&st->regiontypes, art); - - /* regions: properties */ - art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); - art->regionid = RGN_TYPE_UI; - art->prefsizex = UI_COMPACT_PANEL_WIDTH; - art->keymapflag = ED_KEYMAP_UI; - - art->init = text_properties_region_init; - art->draw = text_properties_region_draw; - BLI_addhead(&st->regiontypes, art); - - /* regions: header */ - art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); - art->regionid = RGN_TYPE_HEADER; - art->prefsizey = HEADERY; - art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER; - - art->init = text_header_region_init; - art->draw = text_header_region_draw; - BLI_addhead(&st->regiontypes, art); - - /* regions: footer */ - art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); - art->regionid = RGN_TYPE_FOOTER; - art->prefsizey = HEADERY; - art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FOOTER; - art->init = text_header_region_init; - art->draw = text_header_region_draw; - BLI_addhead(&st->regiontypes, art); - - BKE_spacetype_register(st); - - /* register formatters */ - ED_text_format_register_py(); - ED_text_format_register_osl(); - ED_text_format_register_lua(); - ED_text_format_register_pov(); - ED_text_format_register_pov_ini(); + SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype text"); + ARegionType *art; + + st->spaceid = SPACE_TEXT; + strncpy(st->name, "Text", BKE_ST_MAXNAME); + + st->new = text_new; + st->free = text_free; + st->init = text_init; + st->duplicate = text_duplicate; + st->operatortypes = text_operatortypes; + st->keymap = text_keymap; + st->listener = text_listener; + st->context = text_context; + st->dropboxes = text_dropboxes; + st->id_remap = text_id_remap; + + /* regions: main window */ + art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); + art->regionid = RGN_TYPE_WINDOW; + art->init = text_main_region_init; + art->draw = text_main_region_draw; + art->cursor = text_cursor; + art->event_cursor = true; + + BLI_addhead(&st->regiontypes, art); + + /* regions: properties */ + art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); + art->regionid = RGN_TYPE_UI; + art->prefsizex = UI_COMPACT_PANEL_WIDTH; + art->keymapflag = ED_KEYMAP_UI; + + art->init = text_properties_region_init; + art->draw = text_properties_region_draw; + BLI_addhead(&st->regiontypes, art); + + /* regions: header */ + art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); + art->regionid = RGN_TYPE_HEADER; + art->prefsizey = HEADERY; + art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER; + + art->init = text_header_region_init; + art->draw = text_header_region_draw; + BLI_addhead(&st->regiontypes, art); + + /* regions: footer */ + art = MEM_callocN(sizeof(ARegionType), "spacetype text region"); + art->regionid = RGN_TYPE_FOOTER; + art->prefsizey = HEADERY; + art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FOOTER; + art->init = text_header_region_init; + art->draw = text_header_region_draw; + BLI_addhead(&st->regiontypes, art); + + BKE_spacetype_register(st); + + /* register formatters */ + ED_text_format_register_py(); + ED_text_format_register_osl(); + ED_text_format_register_lua(); + ED_text_format_register_pov(); + ED_text_format_register_pov_ini(); } diff --git a/source/blender/editors/space_text/text_autocomplete.c b/source/blender/editors/space_text/text_autocomplete.c index 65fb91893f3..10303aeba7c 100644 --- a/source/blender/editors/space_text/text_autocomplete.c +++ b/source/blender/editors/space_text/text_autocomplete.c @@ -43,85 +43,90 @@ #include "UI_interface.h" #include "text_format.h" -#include "text_intern.h" /* own include */ - +#include "text_intern.h" /* own include */ /* -------------------------------------------------------------------- */ /* Public API */ int text_do_suggest_select(SpaceText *st, ARegion *ar) { - SuggItem *item, *first, *last /* , *sel */ /* UNUSED */; - TextLine *tmp; - int l, x, y, w, h, i; - int tgti, *top; - int mval[2] = {0, 0}; - - if (!st || !st->text) return 0; - if (!texttool_text_is_active(st->text)) return 0; - - first = texttool_suggest_first(); - last = texttool_suggest_last(); - /* sel = texttool_suggest_selected(); */ /* UNUSED */ - top = texttool_suggest_top(); - - if (!last || !first) - return 0; - - /* Count the visible lines to the cursor */ - for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ; - if (l < 0) return 0; - - text_update_character_width(st); - - if (st->showlinenrs) { - x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4; - } - else { - x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4; - } - y = ar->winy - st->lheight_dpi * l - 2; - - w = SUGG_LIST_WIDTH * st->cwidth + U.widget_unit; - h = SUGG_LIST_SIZE * st->lheight_dpi + 0.4f * U.widget_unit; - - // XXX getmouseco_areawin(mval); - - if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1]) - return 0; - - /* Work out which of the items is at the top of the visible list */ - for (i = 0, item = first; i < *top && item->next; i++, item = item->next) ; - - /* Work out the target item index in the visible list */ - tgti = (y - mval[1] - 4) / st->lheight_dpi; - if (tgti < 0 || tgti > SUGG_LIST_SIZE) - return 1; - - for (i = tgti; i > 0 && item->next; i--, item = item->next) ; - if (item) - texttool_suggest_select(item); - return 1; + SuggItem *item, *first, *last /* , *sel */ /* UNUSED */; + TextLine *tmp; + int l, x, y, w, h, i; + int tgti, *top; + int mval[2] = {0, 0}; + + if (!st || !st->text) + return 0; + if (!texttool_text_is_active(st->text)) + return 0; + + first = texttool_suggest_first(); + last = texttool_suggest_last(); + /* sel = texttool_suggest_selected(); */ /* UNUSED */ + top = texttool_suggest_top(); + + if (!last || !first) + return 0; + + /* Count the visible lines to the cursor */ + for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) + ; + if (l < 0) + return 0; + + text_update_character_width(st); + + if (st->showlinenrs) { + x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4; + } + else { + x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4; + } + y = ar->winy - st->lheight_dpi * l - 2; + + w = SUGG_LIST_WIDTH * st->cwidth + U.widget_unit; + h = SUGG_LIST_SIZE * st->lheight_dpi + 0.4f * U.widget_unit; + + // XXX getmouseco_areawin(mval); + + if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1]) + return 0; + + /* Work out which of the items is at the top of the visible list */ + for (i = 0, item = first; i < *top && item->next; i++, item = item->next) + ; + + /* Work out the target item index in the visible list */ + tgti = (y - mval[1] - 4) / st->lheight_dpi; + if (tgti < 0 || tgti > SUGG_LIST_SIZE) + return 1; + + for (i = tgti; i > 0 && item->next; i--, item = item->next) + ; + if (item) + texttool_suggest_select(item); + return 1; } void text_pop_suggest_list(void) { - SuggItem *item, *sel; - int *top, i; - - item = texttool_suggest_first(); - sel = texttool_suggest_selected(); - top = texttool_suggest_top(); - - i = 0; - while (item && item != sel) { - item = item->next; - i++; - } - if (i > *top + SUGG_LIST_SIZE - 1) - *top = i - SUGG_LIST_SIZE + 1; - else if (i < *top) - *top = i; + SuggItem *item, *sel; + int *top, i; + + item = texttool_suggest_first(); + sel = texttool_suggest_selected(); + top = texttool_suggest_top(); + + i = 0; + while (item && item != sel) { + item = item->next; + i++; + } + if (i > *top + SUGG_LIST_SIZE - 1) + *top = i - SUGG_LIST_SIZE + 1; + else if (i < *top) + *top = i; } /* -------------------------------------------------------------------- */ @@ -131,469 +136,475 @@ static void text_autocomplete_free(bContext *C, wmOperator *op); static GHash *text_autocomplete_build(Text *text) { - GHash *gh; - int seek_len = 0; - const char *seek; - texttool_text_clear(); - - texttool_text_set_active(text); - - /* first get the word we're at */ - { - const int i = text_find_identifier_start(text->curl->line, text->curc); - seek_len = text->curc - i; - seek = text->curl->line + i; - - // BLI_strncpy(seek, seek_ptr, seek_len); - } - - /* now walk over entire doc and suggest words */ - { - TextLine *linep; - - gh = BLI_ghash_str_new(__func__); - - for (linep = text->lines.first; linep; linep = linep->next) { - size_t i_start = 0; - size_t i_end = 0; - size_t i_pos = 0; - - while (i_start < linep->len) { - /* seek identifier beginning */ - i_pos = i_start; - while ((i_start < linep->len) && - (!text_check_identifier_nodigit_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_start], &i_pos)))) - { - i_start = i_pos; - } - i_pos = i_end = i_start; - while ((i_end < linep->len) && - (text_check_identifier_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_end], &i_pos)))) - { - i_end = i_pos; - } - - if ((i_start != i_end) && - /* check we're at the beginning of a line or that the previous char is not an identifier - * this prevents digits from being added */ - ((i_start < 1) || !text_check_identifier_unicode(BLI_str_utf8_as_unicode(&linep->line[i_start - 1])))) - { - char *str_sub = &linep->line[i_start]; - const int choice_len = i_end - i_start; - - if ((choice_len > seek_len) && - (seek_len == 0 || STREQLEN(seek, str_sub, seek_len)) && - (seek != str_sub)) - { - // printf("Adding: %s\n", s); - char str_sub_last = str_sub[choice_len]; - str_sub[choice_len] = '\0'; - if (!BLI_ghash_lookup(gh, str_sub)) { - char *str_dup = BLI_strdupn(str_sub, choice_len); - /* A 'set' would make more sense here */ - BLI_ghash_insert(gh, str_dup, str_dup); - } - str_sub[choice_len] = str_sub_last; - } - } - if (i_end != i_start) { - i_start = i_end; - } - else { - /* highly unlikely, but prevent eternal loop */ - i_start++; - } - } - } - - { - GHashIterator gh_iter; - - /* get the formatter for highlighting */ - TextFormatType *tft; - tft = ED_text_format_get(text); - - GHASH_ITER (gh_iter, gh) { - const char *s = BLI_ghashIterator_getValue(&gh_iter); - texttool_suggest_add(s, tft->format_identifier(s)); - } - } - } - - texttool_suggest_prefix(seek, seek_len); - - return gh; + GHash *gh; + int seek_len = 0; + const char *seek; + texttool_text_clear(); + + texttool_text_set_active(text); + + /* first get the word we're at */ + { + const int i = text_find_identifier_start(text->curl->line, text->curc); + seek_len = text->curc - i; + seek = text->curl->line + i; + + // BLI_strncpy(seek, seek_ptr, seek_len); + } + + /* now walk over entire doc and suggest words */ + { + TextLine *linep; + + gh = BLI_ghash_str_new(__func__); + + for (linep = text->lines.first; linep; linep = linep->next) { + size_t i_start = 0; + size_t i_end = 0; + size_t i_pos = 0; + + while (i_start < linep->len) { + /* seek identifier beginning */ + i_pos = i_start; + while ((i_start < linep->len) && + (!text_check_identifier_nodigit_unicode( + BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_start], &i_pos)))) { + i_start = i_pos; + } + i_pos = i_end = i_start; + while ((i_end < linep->len) && + (text_check_identifier_unicode( + BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_end], &i_pos)))) { + i_end = i_pos; + } + + if ((i_start != i_end) && + /* check we're at the beginning of a line or that the previous char is not an identifier + * this prevents digits from being added */ + ((i_start < 1) || + !text_check_identifier_unicode(BLI_str_utf8_as_unicode(&linep->line[i_start - 1])))) { + char *str_sub = &linep->line[i_start]; + const int choice_len = i_end - i_start; + + if ((choice_len > seek_len) && (seek_len == 0 || STREQLEN(seek, str_sub, seek_len)) && + (seek != str_sub)) { + // printf("Adding: %s\n", s); + char str_sub_last = str_sub[choice_len]; + str_sub[choice_len] = '\0'; + if (!BLI_ghash_lookup(gh, str_sub)) { + char *str_dup = BLI_strdupn(str_sub, choice_len); + /* A 'set' would make more sense here */ + BLI_ghash_insert(gh, str_dup, str_dup); + } + str_sub[choice_len] = str_sub_last; + } + } + if (i_end != i_start) { + i_start = i_end; + } + else { + /* highly unlikely, but prevent eternal loop */ + i_start++; + } + } + } + + { + GHashIterator gh_iter; + + /* get the formatter for highlighting */ + TextFormatType *tft; + tft = ED_text_format_get(text); + + GHASH_ITER (gh_iter, gh) { + const char *s = BLI_ghashIterator_getValue(&gh_iter); + texttool_suggest_add(s, tft->format_identifier(s)); + } + } + } + + texttool_suggest_prefix(seek, seek_len); + + return gh; } /* -- */ static void get_suggest_prefix(Text *text, int offset) { - int i, len; - const char *line; - - if (!text) return; - if (!texttool_text_is_active(text)) return; - - line = text->curl->line; - i = text_find_identifier_start(line, text->curc + offset); - len = text->curc - i + offset; - texttool_suggest_prefix(line + i, len); + int i, len; + const char *line; + + if (!text) + return; + if (!texttool_text_is_active(text)) + return; + + line = text->curl->line; + i = text_find_identifier_start(line, text->curc + offset); + len = text->curc - i + offset; + texttool_suggest_prefix(line + i, len); } static void confirm_suggestion(Text *text, TextUndoBuf *utxt) { - SuggItem *sel; - int i, over = 0; - const char *line; + SuggItem *sel; + int i, over = 0; + const char *line; - if (!text) return; - if (!texttool_text_is_active(text)) return; + if (!text) + return; + if (!texttool_text_is_active(text)) + return; - sel = texttool_suggest_selected(); - if (!sel) return; + sel = texttool_suggest_selected(); + if (!sel) + return; - line = text->curl->line; - i = text_find_identifier_start(line, text->curc /* - skipleft */); - over = text->curc - i; + line = text->curl->line; + i = text_find_identifier_start(line, text->curc /* - skipleft */); + over = text->curc - i; -// for (i = 0; i < skipleft; i++) -// txt_move_left(text, 0); - BLI_assert(memcmp(sel->name, &line[i], over) == 0); - txt_insert_buf(text, utxt, sel->name + over); + // for (i = 0; i < skipleft; i++) + // txt_move_left(text, 0); + BLI_assert(memcmp(sel->name, &line[i], over) == 0); + txt_insert_buf(text, utxt, sel->name + over); -// for (i = 0; i < skipleft; i++) -// txt_move_right(text, 0); + // for (i = 0; i < skipleft; i++) + // txt_move_right(text, 0); - texttool_text_clear(); + texttool_text_clear(); } /* -- */ - static int text_autocomplete_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - - st->doplugins = true; - op->customdata = text_autocomplete_build(text); - - if (texttool_suggest_first()) { - - ED_area_tag_redraw(CTX_wm_area(C)); - - if (texttool_suggest_first() == texttool_suggest_last()) { - TextUndoBuf *utxt = ED_text_undo_push_init(C); - confirm_suggestion(st->text, utxt); - text_update_line_edited(st->text->curl); - text_autocomplete_free(C, op); - ED_undo_push(C, op->type->name); - return OPERATOR_FINISHED; - } - else { - WM_event_add_modal_handler(C, op); - return OPERATOR_RUNNING_MODAL; - } - } - else { - text_autocomplete_free(C, op); - return OPERATOR_CANCELLED; - } + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + + st->doplugins = true; + op->customdata = text_autocomplete_build(text); + + if (texttool_suggest_first()) { + + ED_area_tag_redraw(CTX_wm_area(C)); + + if (texttool_suggest_first() == texttool_suggest_last()) { + TextUndoBuf *utxt = ED_text_undo_push_init(C); + confirm_suggestion(st->text, utxt); + text_update_line_edited(st->text->curl); + text_autocomplete_free(C, op); + ED_undo_push(C, op->type->name); + return OPERATOR_FINISHED; + } + else { + WM_event_add_modal_handler(C, op); + return OPERATOR_RUNNING_MODAL; + } + } + else { + text_autocomplete_free(C, op); + return OPERATOR_CANCELLED; + } } static int doc_scroll = 0; static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW); - - int draw = 0, tools = 0, swallow = 0, scroll = 1; - Text *text = CTX_data_edit_text(C); - int retval = OPERATOR_RUNNING_MODAL; - - (void)text; - - if (st->doplugins && texttool_text_is_active(st->text)) { - if (texttool_suggest_first()) tools |= TOOL_SUGG_LIST; - if (texttool_docs_get()) tools |= TOOL_DOCUMENT; - } - - switch (event->type) { - case LEFTMOUSE: - if (event->val == KM_PRESS) { - if (text_do_suggest_select(st, ar)) - swallow = 1; - else { - if (tools & TOOL_SUGG_LIST) { - texttool_suggest_clear(); - } - if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - } - retval = OPERATOR_FINISHED; - } - draw = 1; - } - break; - case MIDDLEMOUSE: - if (event->val == KM_PRESS) { - if (text_do_suggest_select(st, ar)) { - TextUndoBuf *utxt = ED_text_undo_push_init(C); - confirm_suggestion(st->text, utxt); - text_update_line_edited(st->text->curl); - ED_undo_push(C, op->type->name); - swallow = 1; - } - else { - if (tools & TOOL_SUGG_LIST) { - texttool_suggest_clear(); - } - if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - } - retval = OPERATOR_FINISHED; - } - draw = 1; - } - break; - case ESCKEY: - if (event->val == KM_PRESS) { - draw = swallow = 1; - if (tools & TOOL_SUGG_LIST) { - texttool_suggest_clear(); - } - else if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - } - else draw = swallow = 0; - retval = OPERATOR_CANCELLED; - } - break; - case RETKEY: - case PADENTER: - if (event->val == KM_PRESS) { - if (tools & TOOL_SUGG_LIST) { - TextUndoBuf *utxt = ED_text_undo_push_init(C); - confirm_suggestion(st->text, utxt); - text_update_line_edited(st->text->curl); - ED_undo_push(C, op->type->name); - swallow = 1; - draw = 1; - } - if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - draw = 1; - } - retval = OPERATOR_FINISHED; - } - break; - case LEFTARROWKEY: - case BACKSPACEKEY: - if (event->val == KM_PRESS) { - if (tools & TOOL_SUGG_LIST) { - if (event->ctrl) { - texttool_suggest_clear(); - retval = OPERATOR_CANCELLED; - } - else { - /* Work out which char we are about to delete/pass */ - if (st->text->curl && st->text->curc > 0) { - char ch = st->text->curl->line[st->text->curc - 1]; - if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) { - get_suggest_prefix(st->text, -1); - text_pop_suggest_list(); - } - else { - texttool_suggest_clear(); - retval = OPERATOR_CANCELLED; - } - } - else { - texttool_suggest_clear(); - retval = OPERATOR_CANCELLED; - } - } - } - if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - } - } - break; - case RIGHTARROWKEY: - if (event->val == KM_PRESS) { - if (tools & TOOL_SUGG_LIST) { - if (event->ctrl) { - texttool_suggest_clear(); - retval = OPERATOR_CANCELLED; - } - else { - /* Work out which char we are about to pass */ - if (st->text->curl && st->text->curc < st->text->curl->len) { - char ch = st->text->curl->line[st->text->curc + 1]; - if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) { - get_suggest_prefix(st->text, 1); - text_pop_suggest_list(); - } - else { - texttool_suggest_clear(); - retval = OPERATOR_CANCELLED; - } - } - else { - texttool_suggest_clear(); - retval = OPERATOR_CANCELLED; - } - } - } - if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - } - } - break; - case PAGEDOWNKEY: - scroll = SUGG_LIST_SIZE - 1; - ATTR_FALLTHROUGH; - case WHEELDOWNMOUSE: - case DOWNARROWKEY: - if (event->val == KM_PRESS) { - if (tools & TOOL_DOCUMENT) { - doc_scroll++; - swallow = 1; - draw = 1; - } - else if (tools & TOOL_SUGG_LIST) { - SuggItem *sel = texttool_suggest_selected(); - if (!sel) { - texttool_suggest_select(texttool_suggest_first()); - } - else { - while (sel && scroll--) { - if (sel != texttool_suggest_last() && sel->next) { - texttool_suggest_select(sel->next); - sel = sel->next; - } - else { - texttool_suggest_select(texttool_suggest_first()); - sel = texttool_suggest_first(); - } - } - } - text_pop_suggest_list(); - swallow = 1; - draw = 1; - } - } - break; - case PAGEUPKEY: - scroll = SUGG_LIST_SIZE - 1; - ATTR_FALLTHROUGH; - case WHEELUPMOUSE: - case UPARROWKEY: - if (event->val == KM_PRESS) { - if (tools & TOOL_DOCUMENT) { - if (doc_scroll > 0) doc_scroll--; - swallow = 1; - draw = 1; - } - else if (tools & TOOL_SUGG_LIST) { - SuggItem *sel = texttool_suggest_selected(); - while (sel && scroll--) { - if (sel != texttool_suggest_first() && sel->prev) { - texttool_suggest_select(sel->prev); - sel = sel->prev; - } - else { - texttool_suggest_select(texttool_suggest_last()); - sel = texttool_suggest_last(); - } - } - text_pop_suggest_list(); - swallow = 1; - draw = 1; - } - } - break; - case RIGHTSHIFTKEY: - case LEFTSHIFTKEY: - break; + SpaceText *st = CTX_wm_space_text(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW); + + int draw = 0, tools = 0, swallow = 0, scroll = 1; + Text *text = CTX_data_edit_text(C); + int retval = OPERATOR_RUNNING_MODAL; + + (void)text; + + if (st->doplugins && texttool_text_is_active(st->text)) { + if (texttool_suggest_first()) + tools |= TOOL_SUGG_LIST; + if (texttool_docs_get()) + tools |= TOOL_DOCUMENT; + } + + switch (event->type) { + case LEFTMOUSE: + if (event->val == KM_PRESS) { + if (text_do_suggest_select(st, ar)) + swallow = 1; + else { + if (tools & TOOL_SUGG_LIST) { + texttool_suggest_clear(); + } + if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + } + retval = OPERATOR_FINISHED; + } + draw = 1; + } + break; + case MIDDLEMOUSE: + if (event->val == KM_PRESS) { + if (text_do_suggest_select(st, ar)) { + TextUndoBuf *utxt = ED_text_undo_push_init(C); + confirm_suggestion(st->text, utxt); + text_update_line_edited(st->text->curl); + ED_undo_push(C, op->type->name); + swallow = 1; + } + else { + if (tools & TOOL_SUGG_LIST) { + texttool_suggest_clear(); + } + if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + } + retval = OPERATOR_FINISHED; + } + draw = 1; + } + break; + case ESCKEY: + if (event->val == KM_PRESS) { + draw = swallow = 1; + if (tools & TOOL_SUGG_LIST) { + texttool_suggest_clear(); + } + else if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + } + else + draw = swallow = 0; + retval = OPERATOR_CANCELLED; + } + break; + case RETKEY: + case PADENTER: + if (event->val == KM_PRESS) { + if (tools & TOOL_SUGG_LIST) { + TextUndoBuf *utxt = ED_text_undo_push_init(C); + confirm_suggestion(st->text, utxt); + text_update_line_edited(st->text->curl); + ED_undo_push(C, op->type->name); + swallow = 1; + draw = 1; + } + if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + draw = 1; + } + retval = OPERATOR_FINISHED; + } + break; + case LEFTARROWKEY: + case BACKSPACEKEY: + if (event->val == KM_PRESS) { + if (tools & TOOL_SUGG_LIST) { + if (event->ctrl) { + texttool_suggest_clear(); + retval = OPERATOR_CANCELLED; + } + else { + /* Work out which char we are about to delete/pass */ + if (st->text->curl && st->text->curc > 0) { + char ch = st->text->curl->line[st->text->curc - 1]; + if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) { + get_suggest_prefix(st->text, -1); + text_pop_suggest_list(); + } + else { + texttool_suggest_clear(); + retval = OPERATOR_CANCELLED; + } + } + else { + texttool_suggest_clear(); + retval = OPERATOR_CANCELLED; + } + } + } + if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + } + } + break; + case RIGHTARROWKEY: + if (event->val == KM_PRESS) { + if (tools & TOOL_SUGG_LIST) { + if (event->ctrl) { + texttool_suggest_clear(); + retval = OPERATOR_CANCELLED; + } + else { + /* Work out which char we are about to pass */ + if (st->text->curl && st->text->curc < st->text->curl->len) { + char ch = st->text->curl->line[st->text->curc + 1]; + if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) { + get_suggest_prefix(st->text, 1); + text_pop_suggest_list(); + } + else { + texttool_suggest_clear(); + retval = OPERATOR_CANCELLED; + } + } + else { + texttool_suggest_clear(); + retval = OPERATOR_CANCELLED; + } + } + } + if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + } + } + break; + case PAGEDOWNKEY: + scroll = SUGG_LIST_SIZE - 1; + ATTR_FALLTHROUGH; + case WHEELDOWNMOUSE: + case DOWNARROWKEY: + if (event->val == KM_PRESS) { + if (tools & TOOL_DOCUMENT) { + doc_scroll++; + swallow = 1; + draw = 1; + } + else if (tools & TOOL_SUGG_LIST) { + SuggItem *sel = texttool_suggest_selected(); + if (!sel) { + texttool_suggest_select(texttool_suggest_first()); + } + else { + while (sel && scroll--) { + if (sel != texttool_suggest_last() && sel->next) { + texttool_suggest_select(sel->next); + sel = sel->next; + } + else { + texttool_suggest_select(texttool_suggest_first()); + sel = texttool_suggest_first(); + } + } + } + text_pop_suggest_list(); + swallow = 1; + draw = 1; + } + } + break; + case PAGEUPKEY: + scroll = SUGG_LIST_SIZE - 1; + ATTR_FALLTHROUGH; + case WHEELUPMOUSE: + case UPARROWKEY: + if (event->val == KM_PRESS) { + if (tools & TOOL_DOCUMENT) { + if (doc_scroll > 0) + doc_scroll--; + swallow = 1; + draw = 1; + } + else if (tools & TOOL_SUGG_LIST) { + SuggItem *sel = texttool_suggest_selected(); + while (sel && scroll--) { + if (sel != texttool_suggest_first() && sel->prev) { + texttool_suggest_select(sel->prev); + sel = sel->prev; + } + else { + texttool_suggest_select(texttool_suggest_last()); + sel = texttool_suggest_last(); + } + } + text_pop_suggest_list(); + swallow = 1; + draw = 1; + } + } + break; + case RIGHTSHIFTKEY: + case LEFTSHIFTKEY: + break; #if 0 - default: - if (tools & TOOL_SUGG_LIST) { - texttool_suggest_clear(); - draw = 1; - } - if (tools & TOOL_DOCUMENT) { - texttool_docs_clear(); - doc_scroll = 0; - draw = 1; - } + default: + if (tools & TOOL_SUGG_LIST) { + texttool_suggest_clear(); + draw = 1; + } + if (tools & TOOL_DOCUMENT) { + texttool_docs_clear(); + doc_scroll = 0; + draw = 1; + } #endif - } - - if (draw) { - ED_area_tag_redraw(sa); - } - -// if (swallow) { -// retval = OPERATOR_RUNNING_MODAL; -// } - - if (texttool_suggest_first()) { - if (retval != OPERATOR_RUNNING_MODAL) { - text_autocomplete_free(C, op); - } - return retval; - } - else { - text_autocomplete_free(C, op); - return OPERATOR_FINISHED; - } + } + + if (draw) { + ED_area_tag_redraw(sa); + } + + // if (swallow) { + // retval = OPERATOR_RUNNING_MODAL; + // } + + if (texttool_suggest_first()) { + if (retval != OPERATOR_RUNNING_MODAL) { + text_autocomplete_free(C, op); + } + return retval; + } + else { + text_autocomplete_free(C, op); + return OPERATOR_FINISHED; + } } static void text_autocomplete_free(bContext *C, wmOperator *op) { - GHash *gh = op->customdata; - if (gh) { - BLI_ghash_free(gh, NULL, MEM_freeN); - op->customdata = NULL; - } - - /* other stuff */ - { - SpaceText *st = CTX_wm_space_text(C); - st->doplugins = false; - texttool_text_clear(); - } + GHash *gh = op->customdata; + if (gh) { + BLI_ghash_free(gh, NULL, MEM_freeN); + op->customdata = NULL; + } + + /* other stuff */ + { + SpaceText *st = CTX_wm_space_text(C); + st->doplugins = false; + texttool_text_clear(); + } } static void text_autocomplete_cancel(bContext *C, wmOperator *op) { - text_autocomplete_free(C, op); + text_autocomplete_free(C, op); } void TEXT_OT_autocomplete(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Text Auto Complete"; - ot->description = "Show a list of used text in the open document"; - ot->idname = "TEXT_OT_autocomplete"; - - /* api callbacks */ - ot->invoke = text_autocomplete_invoke; - ot->cancel = text_autocomplete_cancel; - ot->modal = text_autocomplete_modal; - ot->poll = text_space_edit_poll; - - /* flags */ - /* Undo is handled conditionally by this operator. */ - ot->flag = OPTYPE_BLOCKING; + /* identifiers */ + ot->name = "Text Auto Complete"; + ot->description = "Show a list of used text in the open document"; + ot->idname = "TEXT_OT_autocomplete"; + + /* api callbacks */ + ot->invoke = text_autocomplete_invoke; + ot->cancel = text_autocomplete_cancel; + ot->modal = text_autocomplete_modal; + ot->poll = text_space_edit_poll; + + /* flags */ + /* Undo is handled conditionally by this operator. */ + ot->flag = OPTYPE_BLOCKING; } diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c index 1b49e637350..dadb86f439b 100644 --- a/source/blender/editors/space_text/text_draw.c +++ b/source/blender/editors/space_text/text_draw.c @@ -38,7 +38,6 @@ #include "ED_text.h" - #include "GPU_immediate.h" #include "GPU_state.h" @@ -52,21 +51,21 @@ /******************** text font drawing ******************/ typedef struct TextDrawContext { - int font_id; - int cwidth; - int lheight_dpi; + int font_id; + int cwidth; + int lheight_dpi; } TextDrawContext; static void text_draw_context_init(const SpaceText *st, TextDrawContext *tdc) { - tdc->font_id = blf_mono_font; - tdc->cwidth = 0; - tdc->lheight_dpi = st->lheight_dpi; + tdc->font_id = blf_mono_font; + tdc->cwidth = 0; + tdc->lheight_dpi = st->lheight_dpi; } static void text_font_begin(const TextDrawContext *tdc) { - BLF_size(tdc->font_id, tdc->lheight_dpi, 72); + BLF_size(tdc->font_id, tdc->lheight_dpi, 72); } static void text_font_end(const TextDrawContext *UNUSED(tdc)) @@ -75,81 +74,81 @@ static void text_font_end(const TextDrawContext *UNUSED(tdc)) static int text_font_draw(const TextDrawContext *tdc, int x, int y, const char *str) { - int columns; + int columns; - BLF_position(tdc->font_id, x, y, 0); - columns = BLF_draw_mono(tdc->font_id, str, BLF_DRAW_STR_DUMMY_MAX, tdc->cwidth); + BLF_position(tdc->font_id, x, y, 0); + columns = BLF_draw_mono(tdc->font_id, str, BLF_DRAW_STR_DUMMY_MAX, tdc->cwidth); - return tdc->cwidth * columns; + return tdc->cwidth * columns; } static int text_font_draw_character(const TextDrawContext *tdc, int x, int y, char c) { - BLF_position(tdc->font_id, x, y, 0); - BLF_draw(tdc->font_id, &c, 1); + BLF_position(tdc->font_id, x, y, 0); + BLF_draw(tdc->font_id, &c, 1); - return tdc->cwidth; + return tdc->cwidth; } static int text_font_draw_character_utf8(const TextDrawContext *tdc, int x, int y, const char *c) { - int columns; + int columns; - const size_t len = BLI_str_utf8_size_safe(c); - BLF_position(tdc->font_id, x, y, 0); - columns = BLF_draw_mono(tdc->font_id, c, len, tdc->cwidth); + const size_t len = BLI_str_utf8_size_safe(c); + BLF_position(tdc->font_id, x, y, 0); + columns = BLF_draw_mono(tdc->font_id, c, len, tdc->cwidth); - return tdc->cwidth * columns; + return tdc->cwidth * columns; } #if 0 /* Formats every line of the current text */ static void txt_format_text(SpaceText *st) { - TextLine *linep; + TextLine *linep; - if (!st->text) return; + if (!st->text) return; - for (linep = st->text->lines.first; linep; linep = linep->next) - txt_format_line(st, linep, 0); + for (linep = st->text->lines.first; linep; linep = linep->next) + txt_format_line(st, linep, 0); } #endif /* Sets the current drawing color based on the format character specified */ static void format_draw_color(const TextDrawContext *tdc, char formatchar) { - switch (formatchar) { - case FMT_TYPE_WHITESPACE: - break; - case FMT_TYPE_SYMBOL: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_S); - break; - case FMT_TYPE_COMMENT: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_C); - break; - case FMT_TYPE_NUMERAL: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_N); - break; - case FMT_TYPE_STRING: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_L); - break; - case FMT_TYPE_DIRECTIVE: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_D); - break; - case FMT_TYPE_SPECIAL: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_V); - break; - case FMT_TYPE_RESERVED: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_R); - break; - case FMT_TYPE_KEYWORD: - UI_FontThemeColor(tdc->font_id, TH_SYNTAX_B); - break; - case FMT_TYPE_DEFAULT: - default: - UI_FontThemeColor(tdc->font_id, TH_TEXT); - break; - } + switch (formatchar) { + case FMT_TYPE_WHITESPACE: + break; + case FMT_TYPE_SYMBOL: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_S); + break; + case FMT_TYPE_COMMENT: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_C); + break; + case FMT_TYPE_NUMERAL: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_N); + break; + case FMT_TYPE_STRING: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_L); + break; + case FMT_TYPE_DIRECTIVE: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_D); + break; + case FMT_TYPE_SPECIAL: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_V); + break; + case FMT_TYPE_RESERVED: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_R); + break; + case FMT_TYPE_KEYWORD: + UI_FontThemeColor(tdc->font_id, TH_SYNTAX_B); + break; + case FMT_TYPE_DEFAULT: + default: + UI_FontThemeColor(tdc->font_id, TH_TEXT); + break; + } } /************************** draw text *****************************/ @@ -185,533 +184,566 @@ static void format_draw_color(const TextDrawContext *tdc, char formatchar) int wrap_width(const SpaceText *st, ARegion *ar) { - int winx = ar->winx - TXT_SCROLL_WIDTH; - int x, max; + int winx = ar->winx - TXT_SCROLL_WIDTH; + int x, max; - x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - max = st->cwidth ? (winx - x) / st->cwidth : 0; - return max > 8 ? max : 8; + x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + max = st->cwidth ? (winx - x) / st->cwidth : 0; + return max > 8 ? max : 8; } /* Sets (offl, offc) for transforming (line, curs) to its wrapped position */ -void wrap_offset(const SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) +void wrap_offset( + const SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) { - Text *text; - TextLine *linep; - int i, j, start, end, max, chop; - char ch; - - *offl = *offc = 0; - - if (!st->text) return; - if (!st->wordwrap) return; - - text = st->text; - - /* Move pointer to first visible line (top) */ - linep = text->lines.first; - i = st->top; - while (i > 0 && linep) { - int lines = text_get_visible_lines(st, ar, linep->line); - - /* Line before top */ - if (linep == linein) { - if (lines <= i) - /* no visible part of line */ - return; - } - - if (i - lines < 0) { - break; - } - else { - linep = linep->next; - (*offl) += lines - 1; - i -= lines; - } - } - - max = wrap_width(st, ar); - cursin = txt_utf8_offset_to_column(linein->line, cursin); - - while (linep) { - start = 0; - end = max; - chop = 1; - *offc = 0; - for (i = 0, j = 0; linep->line[j]; j += BLI_str_utf8_size_safe(linep->line + j)) { - int chars; - int columns = BLI_str_utf8_char_width_safe(linep->line + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - ch = linep->line[j]; - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - if (linep == linein && i < cursin) cursin += chars - 1; - ch = ' '; - } - else { - chars = 1; - } - - while (chars--) { - if (i + columns - start > max) { - end = MIN2(end, i); - - if (chop && linep == linein && i >= cursin) { - if (i == cursin) { - (*offl)++; - *offc -= end - start; - } - - return; - } - - (*offl)++; - *offc -= end - start; - - start = end; - end += max; - chop = 1; - } - else if (ch == ' ' || ch == '-') { - end = i + 1; - chop = 0; - if (linep == linein && i >= cursin) - return; - } - i += columns; - } - } - if (linep == linein) break; - linep = linep->next; - } + Text *text; + TextLine *linep; + int i, j, start, end, max, chop; + char ch; + + *offl = *offc = 0; + + if (!st->text) + return; + if (!st->wordwrap) + return; + + text = st->text; + + /* Move pointer to first visible line (top) */ + linep = text->lines.first; + i = st->top; + while (i > 0 && linep) { + int lines = text_get_visible_lines(st, ar, linep->line); + + /* Line before top */ + if (linep == linein) { + if (lines <= i) + /* no visible part of line */ + return; + } + + if (i - lines < 0) { + break; + } + else { + linep = linep->next; + (*offl) += lines - 1; + i -= lines; + } + } + + max = wrap_width(st, ar); + cursin = txt_utf8_offset_to_column(linein->line, cursin); + + while (linep) { + start = 0; + end = max; + chop = 1; + *offc = 0; + for (i = 0, j = 0; linep->line[j]; j += BLI_str_utf8_size_safe(linep->line + j)) { + int chars; + int columns = BLI_str_utf8_char_width_safe(linep->line + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + ch = linep->line[j]; + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + if (linep == linein && i < cursin) + cursin += chars - 1; + ch = ' '; + } + else { + chars = 1; + } + + while (chars--) { + if (i + columns - start > max) { + end = MIN2(end, i); + + if (chop && linep == linein && i >= cursin) { + if (i == cursin) { + (*offl)++; + *offc -= end - start; + } + + return; + } + + (*offl)++; + *offc -= end - start; + + start = end; + end += max; + chop = 1; + } + else if (ch == ' ' || ch == '-') { + end = i + 1; + chop = 0; + if (linep == linein && i >= cursin) + return; + } + i += columns; + } + } + if (linep == linein) + break; + linep = linep->next; + } } /* cursin - mem, offc - view */ -void wrap_offset_in_line(const SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) +void wrap_offset_in_line( + const SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) { - int i, j, start, end, chars, max, chop; - char ch; - - *offl = *offc = 0; - - if (!st->text) return; - if (!st->wordwrap) return; - - max = wrap_width(st, ar); - - start = 0; - end = max; - chop = 1; - *offc = 0; - cursin = txt_utf8_offset_to_column(linein->line, cursin); - - for (i = 0, j = 0; linein->line[j]; j += BLI_str_utf8_size_safe(linein->line + j)) { - int columns = BLI_str_utf8_char_width_safe(linein->line + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - ch = linein->line[j]; - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - if (i < cursin) cursin += chars - 1; - ch = ' '; - } - else - chars = 1; - - while (chars--) { - if (i + columns - start > max) { - end = MIN2(end, i); - - if (chop && i >= cursin) { - if (i == cursin) { - (*offl)++; - *offc -= end - start; - } - - return; - } - - (*offl)++; - *offc -= end - start; - - start = end; - end += max; - chop = 1; - } - else if (ch == ' ' || ch == '-') { - end = i + 1; - chop = 0; - if (i >= cursin) - return; - } - i += columns; - } - } + int i, j, start, end, chars, max, chop; + char ch; + + *offl = *offc = 0; + + if (!st->text) + return; + if (!st->wordwrap) + return; + + max = wrap_width(st, ar); + + start = 0; + end = max; + chop = 1; + *offc = 0; + cursin = txt_utf8_offset_to_column(linein->line, cursin); + + for (i = 0, j = 0; linein->line[j]; j += BLI_str_utf8_size_safe(linein->line + j)) { + int columns = BLI_str_utf8_char_width_safe(linein->line + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + ch = linein->line[j]; + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + if (i < cursin) + cursin += chars - 1; + ch = ' '; + } + else + chars = 1; + + while (chars--) { + if (i + columns - start > max) { + end = MIN2(end, i); + + if (chop && i >= cursin) { + if (i == cursin) { + (*offl)++; + *offc -= end - start; + } + + return; + } + + (*offl)++; + *offc -= end - start; + + start = end; + end += max; + chop = 1; + } + else if (ch == ' ' || ch == '-') { + end = i + 1; + chop = 0; + if (i >= cursin) + return; + } + i += columns; + } + } } int text_get_char_pos(const SpaceText *st, const char *line, int cur) { - int a = 0, i; - - for (i = 0; i < cur && line[i]; i += BLI_str_utf8_size_safe(line + i)) { - if (line[i] == '\t') - a += st->tabnumber - a % st->tabnumber; - else - a += BLI_str_utf8_char_width_safe(line + i); - } - return a; + int a = 0, i; + + for (i = 0; i < cur && line[i]; i += BLI_str_utf8_size_safe(line + i)) { + if (line[i] == '\t') + a += st->tabnumber - a % st->tabnumber; + else + a += BLI_str_utf8_char_width_safe(line + i); + } + return a; } static const char *txt_utf8_forward_columns(const char *str, int columns, int *padding) { - int col; - const char *p = str; - while (*p) { - col = BLI_str_utf8_char_width(p); - if (columns - col < 0) - break; - columns -= col; - p += BLI_str_utf8_size_safe(p); - if (columns == 0) - break; - } - if (padding) - *padding = *p ? columns : 0; - return p; + int col; + const char *p = str; + while (*p) { + col = BLI_str_utf8_char_width(p); + if (columns - col < 0) + break; + columns -= col; + p += BLI_str_utf8_size_safe(p); + if (columns == 0) + break; + } + if (padding) + *padding = *p ? columns : 0; + return p; } -static int text_draw_wrapped( - const SpaceText *st, const TextDrawContext *tdc, - const char *str, int x, int y, int w, const char *format, int skip) +static int text_draw_wrapped(const SpaceText *st, + const TextDrawContext *tdc, + const char *str, + int x, + int y, + int w, + const char *format, + int skip) { - const bool use_syntax = (st->showsyntax && format); - FlattenString fs; - int basex, lines; - int i, wrap, end, max, columns, padding; /* column */ - /* warning, only valid when 'use_syntax' is set */ - int a, fstart, fpos; /* utf8 chars */ - int mi, ma, mstart, mend; /* mem */ - char fmt_prev = 0xff; - /* don't draw lines below this */ - const int clip_min_y = -(int)(st->lheight_dpi - 1); - - flatten_string(st, &fs, str); - str = fs.buf; - max = w / st->cwidth; - if (max < 8) max = 8; - basex = x; - lines = 1; - - fpos = fstart = 0; mstart = 0; - mend = txt_utf8_forward_columns(str, max, &padding) - str; - end = wrap = max - padding; - - for (i = 0, mi = 0; str[mi]; i += columns, mi += BLI_str_utf8_size_safe(str + mi)) { - columns = BLI_str_utf8_char_width_safe(str + mi); - if (i + columns > end) { - /* skip hidden part of line */ - if (skip) { - skip--; - if (use_syntax) { - /* currently fpos only used when formatting */ - fpos += BLI_strnlen_utf8(str + mstart, mend - mstart); - } - fstart = fpos; mstart = mend; - mend = txt_utf8_forward_columns(str + mend, max, &padding) - str; - end = (wrap += max - padding); - continue; - } - - /* Draw the visible portion of text on the overshot line */ - for (a = fstart, ma = mstart; ma < mend; a++, ma += BLI_str_utf8_size_safe(str + ma)) { - if (use_syntax) { - if (fmt_prev != format[a]) format_draw_color(tdc, fmt_prev = format[a]); - } - x += text_font_draw_character_utf8(tdc, x, y, str + ma); - fpos++; - } - y -= st->lheight_dpi + TXT_LINE_SPACING; - x = basex; - lines++; - fstart = fpos; mstart = mend; - mend = txt_utf8_forward_columns(str + mend, max, &padding) - str; - end = (wrap += max - padding); - - if (y <= clip_min_y) - break; - } - else if (str[mi] == ' ' || str[mi] == '-') { - wrap = i + 1; mend = mi + 1; - } - } - - /* Draw the remaining text */ - for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; a++, ma += BLI_str_utf8_size_safe(str + ma)) { - if (use_syntax) { - if (fmt_prev != format[a]) format_draw_color(tdc, fmt_prev = format[a]); - } - - x += text_font_draw_character_utf8(tdc, x, y, str + ma); - } - - flatten_string_free(&fs); - - return lines; + const bool use_syntax = (st->showsyntax && format); + FlattenString fs; + int basex, lines; + int i, wrap, end, max, columns, padding; /* column */ + /* warning, only valid when 'use_syntax' is set */ + int a, fstart, fpos; /* utf8 chars */ + int mi, ma, mstart, mend; /* mem */ + char fmt_prev = 0xff; + /* don't draw lines below this */ + const int clip_min_y = -(int)(st->lheight_dpi - 1); + + flatten_string(st, &fs, str); + str = fs.buf; + max = w / st->cwidth; + if (max < 8) + max = 8; + basex = x; + lines = 1; + + fpos = fstart = 0; + mstart = 0; + mend = txt_utf8_forward_columns(str, max, &padding) - str; + end = wrap = max - padding; + + for (i = 0, mi = 0; str[mi]; i += columns, mi += BLI_str_utf8_size_safe(str + mi)) { + columns = BLI_str_utf8_char_width_safe(str + mi); + if (i + columns > end) { + /* skip hidden part of line */ + if (skip) { + skip--; + if (use_syntax) { + /* currently fpos only used when formatting */ + fpos += BLI_strnlen_utf8(str + mstart, mend - mstart); + } + fstart = fpos; + mstart = mend; + mend = txt_utf8_forward_columns(str + mend, max, &padding) - str; + end = (wrap += max - padding); + continue; + } + + /* Draw the visible portion of text on the overshot line */ + for (a = fstart, ma = mstart; ma < mend; a++, ma += BLI_str_utf8_size_safe(str + ma)) { + if (use_syntax) { + if (fmt_prev != format[a]) + format_draw_color(tdc, fmt_prev = format[a]); + } + x += text_font_draw_character_utf8(tdc, x, y, str + ma); + fpos++; + } + y -= st->lheight_dpi + TXT_LINE_SPACING; + x = basex; + lines++; + fstart = fpos; + mstart = mend; + mend = txt_utf8_forward_columns(str + mend, max, &padding) - str; + end = (wrap += max - padding); + + if (y <= clip_min_y) + break; + } + else if (str[mi] == ' ' || str[mi] == '-') { + wrap = i + 1; + mend = mi + 1; + } + } + + /* Draw the remaining text */ + for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; + a++, ma += BLI_str_utf8_size_safe(str + ma)) { + if (use_syntax) { + if (fmt_prev != format[a]) + format_draw_color(tdc, fmt_prev = format[a]); + } + + x += text_font_draw_character_utf8(tdc, x, y, str + ma); + } + + flatten_string_free(&fs); + + return lines; } -static void text_draw( - const SpaceText *st, const TextDrawContext *tdc, - char *str, int cshift, int maxwidth, int x, int y, const char *format) +static void text_draw(const SpaceText *st, + const TextDrawContext *tdc, + char *str, + int cshift, + int maxwidth, + int x, + int y, + const char *format) { - const bool use_syntax = (st->showsyntax && format); - FlattenString fs; - int columns, size, n, w = 0, padding, amount = 0; - const char *in = NULL; - - for (n = flatten_string(st, &fs, str), str = fs.buf; n > 0; n--) { - columns = BLI_str_utf8_char_width_safe(str); - size = BLI_str_utf8_size_safe(str); - - if (!in) { - if (w >= cshift) { - padding = w - cshift; - in = str; - } - else if (format) - format++; - } - if (in) { - if (maxwidth && w + columns > cshift + maxwidth) - break; - amount++; - } - - w += columns; - str += size; - } - if (!in) { - flatten_string_free(&fs); - return; /* String is shorter than shift or ends with a padding */ - } - - x += tdc->cwidth * padding; - - if (use_syntax) { - int a, str_shift = 0; - char fmt_prev = 0xff; - - for (a = 0; a < amount; a++) { - if (format[a] != fmt_prev) format_draw_color(tdc, fmt_prev = format[a]); - x += text_font_draw_character_utf8(tdc, x, y, in + str_shift); - str_shift += BLI_str_utf8_size_safe(in + str_shift); - } - } - else { - text_font_draw(tdc, x, y, in); - } - - flatten_string_free(&fs); + const bool use_syntax = (st->showsyntax && format); + FlattenString fs; + int columns, size, n, w = 0, padding, amount = 0; + const char *in = NULL; + + for (n = flatten_string(st, &fs, str), str = fs.buf; n > 0; n--) { + columns = BLI_str_utf8_char_width_safe(str); + size = BLI_str_utf8_size_safe(str); + + if (!in) { + if (w >= cshift) { + padding = w - cshift; + in = str; + } + else if (format) + format++; + } + if (in) { + if (maxwidth && w + columns > cshift + maxwidth) + break; + amount++; + } + + w += columns; + str += size; + } + if (!in) { + flatten_string_free(&fs); + return; /* String is shorter than shift or ends with a padding */ + } + + x += tdc->cwidth * padding; + + if (use_syntax) { + int a, str_shift = 0; + char fmt_prev = 0xff; + + for (a = 0; a < amount; a++) { + if (format[a] != fmt_prev) + format_draw_color(tdc, fmt_prev = format[a]); + x += text_font_draw_character_utf8(tdc, x, y, in + str_shift); + str_shift += BLI_str_utf8_size_safe(in + str_shift); + } + } + else { + text_font_draw(tdc, x, y, in); + } + + flatten_string_free(&fs); } /************************ cache utilities *****************************/ typedef struct DrawCache { - int *line_height; - int total_lines, nlines; - - /* this is needed to check cache relevance */ - int winx, wordwrap, showlinenrs, tabnumber; - short lheight; - char cwidth; - char text_id[MAX_ID_NAME]; - - /* for partial lines recalculation */ - short update_flag; - int valid_head, valid_tail; /* amount of unchanged lines */ + int *line_height; + int total_lines, nlines; + + /* this is needed to check cache relevance */ + int winx, wordwrap, showlinenrs, tabnumber; + short lheight; + char cwidth; + char text_id[MAX_ID_NAME]; + + /* for partial lines recalculation */ + short update_flag; + int valid_head, valid_tail; /* amount of unchanged lines */ } DrawCache; static void text_drawcache_init(SpaceText *st) { - DrawCache *drawcache = MEM_callocN(sizeof(DrawCache), "text draw cache"); + DrawCache *drawcache = MEM_callocN(sizeof(DrawCache), "text draw cache"); - drawcache->winx = -1; - drawcache->nlines = BLI_listbase_count(&st->text->lines); - drawcache->text_id[0] = '\0'; + drawcache->winx = -1; + drawcache->nlines = BLI_listbase_count(&st->text->lines); + drawcache->text_id[0] = '\0'; - st->drawcache = drawcache; + st->drawcache = drawcache; } static void text_update_drawcache(SpaceText *st, ARegion *ar) { - DrawCache *drawcache; - int full_update = 0, nlines = 0; - Text *txt = st->text; - - if (!st->drawcache) text_drawcache_init(st); - - text_update_character_width(st); - - drawcache = (DrawCache *)st->drawcache; - nlines = drawcache->nlines; - - /* check if full cache update is needed */ - - /* area was resized */ - full_update |= drawcache->winx != ar->winx; - /* word-wrapping option was toggled */ - full_update |= drawcache->wordwrap != st->wordwrap; - /* word-wrapping option was toggled */ - full_update |= drawcache->showlinenrs != st->showlinenrs; - /* word-wrapping option was toggled */ - full_update |= drawcache->tabnumber != st->tabnumber; - /* word-wrapping option was toggled */ - full_update |= drawcache->lheight != st->lheight_dpi; - /* word-wrapping option was toggled */ - full_update |= drawcache->cwidth != st->cwidth; - /* text datablock was changed */ - full_update |= !STREQLEN(drawcache->text_id, txt->id.name, MAX_ID_NAME); - - if (st->wordwrap) { - /* update line heights */ - if (full_update || !drawcache->line_height) { - drawcache->valid_head = 0; - drawcache->valid_tail = 0; - drawcache->update_flag = 1; - } - - if (drawcache->update_flag) { - TextLine *line = st->text->lines.first; - int lineno = 0, size, lines_count; - int *fp = drawcache->line_height, *new_tail, *old_tail; - - nlines = BLI_listbase_count(&txt->lines); - size = sizeof(int) * nlines; - - if (fp) fp = MEM_reallocN(fp, size); - else fp = MEM_callocN(size, "text drawcache line_height"); - - drawcache->valid_tail = drawcache->valid_head = 0; - old_tail = fp + drawcache->nlines - drawcache->valid_tail; - new_tail = fp + nlines - drawcache->valid_tail; - memmove(new_tail, old_tail, drawcache->valid_tail); - - drawcache->total_lines = 0; - - if (st->showlinenrs) - st->linenrs_tot = integer_digits_i(nlines); - - while (line) { - if (drawcache->valid_head) { /* we're inside valid head lines */ - lines_count = fp[lineno]; - drawcache->valid_head--; - } - else if (lineno > new_tail - fp) { /* we-re inside valid tail lines */ - lines_count = fp[lineno]; - } - else { - lines_count = text_get_visible_lines(st, ar, line->line); - } - - fp[lineno] = lines_count; - - line = line->next; - lineno++; - drawcache->total_lines += lines_count; - } - - drawcache->line_height = fp; - } - } - else { - if (drawcache->line_height) { - MEM_freeN(drawcache->line_height); - drawcache->line_height = NULL; - } - - if (full_update || drawcache->update_flag) { - nlines = BLI_listbase_count(&txt->lines); - - if (st->showlinenrs) - st->linenrs_tot = integer_digits_i(nlines); - } - - drawcache->total_lines = nlines; - } - - drawcache->nlines = nlines; - - /* store settings */ - drawcache->winx = ar->winx; - drawcache->wordwrap = st->wordwrap; - drawcache->lheight = st->lheight_dpi; - drawcache->cwidth = st->cwidth; - drawcache->showlinenrs = st->showlinenrs; - drawcache->tabnumber = st->tabnumber; - - strncpy(drawcache->text_id, txt->id.name, MAX_ID_NAME); - - /* clear update flag */ - drawcache->update_flag = 0; - drawcache->valid_head = 0; - drawcache->valid_tail = 0; + DrawCache *drawcache; + int full_update = 0, nlines = 0; + Text *txt = st->text; + + if (!st->drawcache) + text_drawcache_init(st); + + text_update_character_width(st); + + drawcache = (DrawCache *)st->drawcache; + nlines = drawcache->nlines; + + /* check if full cache update is needed */ + + /* area was resized */ + full_update |= drawcache->winx != ar->winx; + /* word-wrapping option was toggled */ + full_update |= drawcache->wordwrap != st->wordwrap; + /* word-wrapping option was toggled */ + full_update |= drawcache->showlinenrs != st->showlinenrs; + /* word-wrapping option was toggled */ + full_update |= drawcache->tabnumber != st->tabnumber; + /* word-wrapping option was toggled */ + full_update |= drawcache->lheight != st->lheight_dpi; + /* word-wrapping option was toggled */ + full_update |= drawcache->cwidth != st->cwidth; + /* text datablock was changed */ + full_update |= !STREQLEN(drawcache->text_id, txt->id.name, MAX_ID_NAME); + + if (st->wordwrap) { + /* update line heights */ + if (full_update || !drawcache->line_height) { + drawcache->valid_head = 0; + drawcache->valid_tail = 0; + drawcache->update_flag = 1; + } + + if (drawcache->update_flag) { + TextLine *line = st->text->lines.first; + int lineno = 0, size, lines_count; + int *fp = drawcache->line_height, *new_tail, *old_tail; + + nlines = BLI_listbase_count(&txt->lines); + size = sizeof(int) * nlines; + + if (fp) + fp = MEM_reallocN(fp, size); + else + fp = MEM_callocN(size, "text drawcache line_height"); + + drawcache->valid_tail = drawcache->valid_head = 0; + old_tail = fp + drawcache->nlines - drawcache->valid_tail; + new_tail = fp + nlines - drawcache->valid_tail; + memmove(new_tail, old_tail, drawcache->valid_tail); + + drawcache->total_lines = 0; + + if (st->showlinenrs) + st->linenrs_tot = integer_digits_i(nlines); + + while (line) { + if (drawcache->valid_head) { /* we're inside valid head lines */ + lines_count = fp[lineno]; + drawcache->valid_head--; + } + else if (lineno > new_tail - fp) { /* we-re inside valid tail lines */ + lines_count = fp[lineno]; + } + else { + lines_count = text_get_visible_lines(st, ar, line->line); + } + + fp[lineno] = lines_count; + + line = line->next; + lineno++; + drawcache->total_lines += lines_count; + } + + drawcache->line_height = fp; + } + } + else { + if (drawcache->line_height) { + MEM_freeN(drawcache->line_height); + drawcache->line_height = NULL; + } + + if (full_update || drawcache->update_flag) { + nlines = BLI_listbase_count(&txt->lines); + + if (st->showlinenrs) + st->linenrs_tot = integer_digits_i(nlines); + } + + drawcache->total_lines = nlines; + } + + drawcache->nlines = nlines; + + /* store settings */ + drawcache->winx = ar->winx; + drawcache->wordwrap = st->wordwrap; + drawcache->lheight = st->lheight_dpi; + drawcache->cwidth = st->cwidth; + drawcache->showlinenrs = st->showlinenrs; + drawcache->tabnumber = st->tabnumber; + + strncpy(drawcache->text_id, txt->id.name, MAX_ID_NAME); + + /* clear update flag */ + drawcache->update_flag = 0; + drawcache->valid_head = 0; + drawcache->valid_tail = 0; } void text_drawcache_tag_update(SpaceText *st, int full) { - /* this happens if text editor ops are caled from python */ - if (st == NULL) - return; - - if (st->drawcache) { - DrawCache *drawcache = (DrawCache *)st->drawcache; - Text *txt = st->text; - - if (drawcache->update_flag) { - /* happens when tagging update from space listener */ - /* should do nothing to prevent locally tagged cache be fully recalculated */ - return; - } - - if (!full) { - int sellno = BLI_findindex(&txt->lines, txt->sell); - int curlno = BLI_findindex(&txt->lines, txt->curl); - - if (curlno < sellno) { - drawcache->valid_head = curlno; - drawcache->valid_tail = drawcache->nlines - sellno - 1; - } - else { - drawcache->valid_head = sellno; - drawcache->valid_tail = drawcache->nlines - curlno - 1; - } - - /* quick cache recalculation is also used in delete operator, - * which could merge lines which are adjacent to current selection lines - * expand recalculate area to this lines */ - if (drawcache->valid_head > 0) drawcache->valid_head--; - if (drawcache->valid_tail > 0) drawcache->valid_tail--; - } - else { - drawcache->valid_head = 0; - drawcache->valid_tail = 0; - } - - drawcache->update_flag = 1; - } + /* this happens if text editor ops are caled from python */ + if (st == NULL) + return; + + if (st->drawcache) { + DrawCache *drawcache = (DrawCache *)st->drawcache; + Text *txt = st->text; + + if (drawcache->update_flag) { + /* happens when tagging update from space listener */ + /* should do nothing to prevent locally tagged cache be fully recalculated */ + return; + } + + if (!full) { + int sellno = BLI_findindex(&txt->lines, txt->sell); + int curlno = BLI_findindex(&txt->lines, txt->curl); + + if (curlno < sellno) { + drawcache->valid_head = curlno; + drawcache->valid_tail = drawcache->nlines - sellno - 1; + } + else { + drawcache->valid_head = sellno; + drawcache->valid_tail = drawcache->nlines - curlno - 1; + } + + /* quick cache recalculation is also used in delete operator, + * which could merge lines which are adjacent to current selection lines + * expand recalculate area to this lines */ + if (drawcache->valid_head > 0) + drawcache->valid_head--; + if (drawcache->valid_tail > 0) + drawcache->valid_tail--; + } + else { + drawcache->valid_head = 0; + drawcache->valid_tail = 0; + } + + drawcache->update_flag = 1; + } } void text_free_caches(SpaceText *st) { - DrawCache *drawcache = (DrawCache *)st->drawcache; + DrawCache *drawcache = (DrawCache *)st->drawcache; - if (drawcache) { - if (drawcache->line_height) - MEM_freeN(drawcache->line_height); + if (drawcache) { + if (drawcache->line_height) + MEM_freeN(drawcache->line_height); - MEM_freeN(drawcache); - } + MEM_freeN(drawcache); + } } /************************ word-wrap utilities *****************************/ @@ -719,217 +751,231 @@ void text_free_caches(SpaceText *st) /* cache should be updated in caller */ static int text_get_visible_lines_no(const SpaceText *st, int lineno) { - const DrawCache *drawcache = st->drawcache; + const DrawCache *drawcache = st->drawcache; - return drawcache->line_height[lineno]; + return drawcache->line_height[lineno]; } int text_get_visible_lines(const SpaceText *st, ARegion *ar, const char *str) { - int i, j, start, end, max, lines, chars; - char ch; - - max = wrap_width(st, ar); - lines = 1; - start = 0; - end = max; - for (i = 0, j = 0; str[j]; j += BLI_str_utf8_size_safe(str + j)) { - int columns = BLI_str_utf8_char_width_safe(str + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - ch = str[j]; - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - ch = ' '; - } - else { - chars = 1; - } - - while (chars--) { - if (i + columns - start > max) { - lines++; - start = MIN2(end, i); - end += max; - } - else if (ch == ' ' || ch == '-') { - end = i + 1; - } - - i += columns; - } - } - - return lines; + int i, j, start, end, max, lines, chars; + char ch; + + max = wrap_width(st, ar); + lines = 1; + start = 0; + end = max; + for (i = 0, j = 0; str[j]; j += BLI_str_utf8_size_safe(str + j)) { + int columns = BLI_str_utf8_char_width_safe(str + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + ch = str[j]; + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + ch = ' '; + } + else { + chars = 1; + } + + while (chars--) { + if (i + columns - start > max) { + lines++; + start = MIN2(end, i); + end += max; + } + else if (ch == ' ' || ch == '-') { + end = i + 1; + } + + i += columns; + } + } + + return lines; } int text_get_span_wrap(const SpaceText *st, ARegion *ar, TextLine *from, TextLine *to) { - if (st->wordwrap) { - int ret = 0; - TextLine *tmp = from; - - /* Look forwards */ - while (tmp) { - if (tmp == to) return ret; - ret += text_get_visible_lines(st, ar, tmp->line); - tmp = tmp->next; - } - - return ret; - } - else { - return txt_get_span(from, to); - } + if (st->wordwrap) { + int ret = 0; + TextLine *tmp = from; + + /* Look forwards */ + while (tmp) { + if (tmp == to) + return ret; + ret += text_get_visible_lines(st, ar, tmp->line); + tmp = tmp->next; + } + + return ret; + } + else { + return txt_get_span(from, to); + } } int text_get_total_lines(SpaceText *st, ARegion *ar) { - DrawCache *drawcache; + DrawCache *drawcache; - text_update_drawcache(st, ar); - drawcache = st->drawcache; + text_update_drawcache(st, ar); + drawcache = st->drawcache; - return drawcache->total_lines; + return drawcache->total_lines; } /************************ draw scrollbar *****************************/ static void calc_text_rcts(SpaceText *st, ARegion *ar, rcti *scroll, rcti *back) { - int lhlstart, lhlend, ltexth, sell_off, curl_off; - short barheight, barstart, hlstart, hlend, blank_lines; - short pix_available, pix_top_margin, pix_bottom_margin, pix_bardiff; - - pix_top_margin = (0.4 * U.widget_unit); - pix_bottom_margin = (0.4 * U.widget_unit); - pix_available = ar->winy - pix_top_margin - pix_bottom_margin; - ltexth = text_get_total_lines(st, ar); - blank_lines = st->viewlines / 2; - - /* nicer code: use scroll rect for entire bar */ - back->xmin = ar->winx - (0.6 * U.widget_unit); - back->xmax = ar->winx; - back->ymin = 0; - back->ymax = ar->winy; - - scroll->xmax = ar->winx - (0.2 * U.widget_unit); - scroll->xmin = scroll->xmax - (0.4 * U.widget_unit); - scroll->ymin = pix_top_margin; - scroll->ymax = pix_available; - - /* when re-sizing a view-port with the bar at the bottom to a greater height - * more blank lines will be added */ - if (ltexth + blank_lines < st->top + st->viewlines) { - blank_lines = st->top + st->viewlines - ltexth; - } - - ltexth += blank_lines; - - barheight = (ltexth > 0) ? (st->viewlines * pix_available) / ltexth : 0; - pix_bardiff = 0; - if (barheight < 20) { - pix_bardiff = 20 - barheight; /* take into account the now non-linear sizing of the bar */ - barheight = 20; - } - barstart = (ltexth > 0) ? ((pix_available - pix_bardiff) * st->top) / ltexth : 0; - - st->txtbar = *scroll; - st->txtbar.ymax -= barstart; - st->txtbar.ymin = st->txtbar.ymax - barheight; - - CLAMP(st->txtbar.ymin, pix_bottom_margin, ar->winy - pix_top_margin); - CLAMP(st->txtbar.ymax, pix_bottom_margin, ar->winy - pix_top_margin); - - st->pix_per_line = (pix_available > 0) ? (float) ltexth / pix_available : 0; - if (st->pix_per_line < 0.1f) st->pix_per_line = 0.1f; - - curl_off = text_get_span_wrap(st, ar, st->text->lines.first, st->text->curl); - sell_off = text_get_span_wrap(st, ar, st->text->lines.first, st->text->sell); - lhlstart = MIN2(curl_off, sell_off); - lhlend = MAX2(curl_off, sell_off); - - if (ltexth > 0) { - hlstart = (lhlstart * pix_available) / ltexth; - hlend = (lhlend * pix_available) / ltexth; - - /* the scrollbar is non-linear sized */ - if (pix_bardiff > 0) { - /* the start of the highlight is in the current viewport */ - if (st->viewlines && lhlstart >= st->top && lhlstart <= st->top + st->viewlines) { - /* speed the progresion of the start of the highlight through the scrollbar */ - hlstart = ( ( (pix_available - pix_bardiff) * lhlstart) / ltexth) + (pix_bardiff * (lhlstart - st->top) / st->viewlines); - } - else if (lhlstart > st->top + st->viewlines && hlstart < barstart + barheight && hlstart > barstart) { - /* push hl start down */ - hlstart = barstart + barheight; - } - else if (lhlend > st->top && lhlstart < st->top && hlstart > barstart) { - /*fill out start */ - hlstart = barstart; - } - - if (hlend <= hlstart) { - hlend = hlstart + 2; - } - - /* the end of the highlight is in the current viewport */ - if (st->viewlines && lhlend >= st->top && lhlend <= st->top + st->viewlines) { - /* speed the progresion of the end of the highlight through the scrollbar */ - hlend = (((pix_available - pix_bardiff) * lhlend) / ltexth) + (pix_bardiff * (lhlend - st->top) / st->viewlines); - } - else if (lhlend < st->top && hlend >= barstart - 2 && hlend < barstart + barheight) { - /* push hl end up */ - hlend = barstart; - } - else if (lhlend > st->top + st->viewlines && lhlstart < st->top + st->viewlines && hlend < barstart + barheight) { - /* fill out end */ - hlend = barstart + barheight; - } - - if (hlend <= hlstart) { - hlstart = hlend - 2; - } - } - } - else { - hlstart = 0; - hlend = 0; - } - - if (hlend - hlstart < 2) { - hlend = hlstart + 2; - } - - st->txtscroll = *scroll; - st->txtscroll.ymax = ar->winy - pix_top_margin - hlstart; - st->txtscroll.ymin = ar->winy - pix_top_margin - hlend; - - CLAMP(st->txtscroll.ymin, pix_bottom_margin, ar->winy - pix_top_margin); - CLAMP(st->txtscroll.ymax, pix_bottom_margin, ar->winy - pix_top_margin); + int lhlstart, lhlend, ltexth, sell_off, curl_off; + short barheight, barstart, hlstart, hlend, blank_lines; + short pix_available, pix_top_margin, pix_bottom_margin, pix_bardiff; + + pix_top_margin = (0.4 * U.widget_unit); + pix_bottom_margin = (0.4 * U.widget_unit); + pix_available = ar->winy - pix_top_margin - pix_bottom_margin; + ltexth = text_get_total_lines(st, ar); + blank_lines = st->viewlines / 2; + + /* nicer code: use scroll rect for entire bar */ + back->xmin = ar->winx - (0.6 * U.widget_unit); + back->xmax = ar->winx; + back->ymin = 0; + back->ymax = ar->winy; + + scroll->xmax = ar->winx - (0.2 * U.widget_unit); + scroll->xmin = scroll->xmax - (0.4 * U.widget_unit); + scroll->ymin = pix_top_margin; + scroll->ymax = pix_available; + + /* when re-sizing a view-port with the bar at the bottom to a greater height + * more blank lines will be added */ + if (ltexth + blank_lines < st->top + st->viewlines) { + blank_lines = st->top + st->viewlines - ltexth; + } + + ltexth += blank_lines; + + barheight = (ltexth > 0) ? (st->viewlines * pix_available) / ltexth : 0; + pix_bardiff = 0; + if (barheight < 20) { + pix_bardiff = 20 - barheight; /* take into account the now non-linear sizing of the bar */ + barheight = 20; + } + barstart = (ltexth > 0) ? ((pix_available - pix_bardiff) * st->top) / ltexth : 0; + + st->txtbar = *scroll; + st->txtbar.ymax -= barstart; + st->txtbar.ymin = st->txtbar.ymax - barheight; + + CLAMP(st->txtbar.ymin, pix_bottom_margin, ar->winy - pix_top_margin); + CLAMP(st->txtbar.ymax, pix_bottom_margin, ar->winy - pix_top_margin); + + st->pix_per_line = (pix_available > 0) ? (float)ltexth / pix_available : 0; + if (st->pix_per_line < 0.1f) + st->pix_per_line = 0.1f; + + curl_off = text_get_span_wrap(st, ar, st->text->lines.first, st->text->curl); + sell_off = text_get_span_wrap(st, ar, st->text->lines.first, st->text->sell); + lhlstart = MIN2(curl_off, sell_off); + lhlend = MAX2(curl_off, sell_off); + + if (ltexth > 0) { + hlstart = (lhlstart * pix_available) / ltexth; + hlend = (lhlend * pix_available) / ltexth; + + /* the scrollbar is non-linear sized */ + if (pix_bardiff > 0) { + /* the start of the highlight is in the current viewport */ + if (st->viewlines && lhlstart >= st->top && lhlstart <= st->top + st->viewlines) { + /* speed the progresion of the start of the highlight through the scrollbar */ + hlstart = (((pix_available - pix_bardiff) * lhlstart) / ltexth) + + (pix_bardiff * (lhlstart - st->top) / st->viewlines); + } + else if (lhlstart > st->top + st->viewlines && hlstart < barstart + barheight && + hlstart > barstart) { + /* push hl start down */ + hlstart = barstart + barheight; + } + else if (lhlend > st->top && lhlstart < st->top && hlstart > barstart) { + /*fill out start */ + hlstart = barstart; + } + + if (hlend <= hlstart) { + hlend = hlstart + 2; + } + + /* the end of the highlight is in the current viewport */ + if (st->viewlines && lhlend >= st->top && lhlend <= st->top + st->viewlines) { + /* speed the progresion of the end of the highlight through the scrollbar */ + hlend = (((pix_available - pix_bardiff) * lhlend) / ltexth) + + (pix_bardiff * (lhlend - st->top) / st->viewlines); + } + else if (lhlend < st->top && hlend >= barstart - 2 && hlend < barstart + barheight) { + /* push hl end up */ + hlend = barstart; + } + else if (lhlend > st->top + st->viewlines && lhlstart < st->top + st->viewlines && + hlend < barstart + barheight) { + /* fill out end */ + hlend = barstart + barheight; + } + + if (hlend <= hlstart) { + hlstart = hlend - 2; + } + } + } + else { + hlstart = 0; + hlend = 0; + } + + if (hlend - hlstart < 2) { + hlend = hlstart + 2; + } + + st->txtscroll = *scroll; + st->txtscroll.ymax = ar->winy - pix_top_margin - hlstart; + st->txtscroll.ymin = ar->winy - pix_top_margin - hlend; + + CLAMP(st->txtscroll.ymin, pix_bottom_margin, ar->winy - pix_top_margin); + CLAMP(st->txtscroll.ymax, pix_bottom_margin, ar->winy - pix_top_margin); } static void draw_textscroll(const SpaceText *st, rcti *scroll, rcti *back) { - bTheme *btheme = UI_GetTheme(); - uiWidgetColors wcol = btheme->tui.wcol_scroll; - float col[4]; - float rad; - - /* background so highlights don't go behind the scrollbar */ - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - immUniformThemeColor(TH_BACK); - immRecti(pos, back->xmin, back->ymin, back->xmax, back->ymax); - immUnbindProgram(); - - UI_draw_widget_scroll(&wcol, scroll, &st->txtbar, (st->flags & ST_SCROLL_SELECT) ? UI_SCROLL_PRESSED : 0); - - UI_draw_roundbox_corner_set(UI_CNR_ALL); - rad = 0.4f * min_ii(BLI_rcti_size_x(&st->txtscroll), BLI_rcti_size_y(&st->txtscroll)); - UI_GetThemeColor3fv(TH_HILITE, col); - col[3] = 0.18f; - UI_draw_roundbox_aa(true, st->txtscroll.xmin + 1, st->txtscroll.ymin, st->txtscroll.xmax - 1, st->txtscroll.ymax, rad, col); + bTheme *btheme = UI_GetTheme(); + uiWidgetColors wcol = btheme->tui.wcol_scroll; + float col[4]; + float rad; + + /* background so highlights don't go behind the scrollbar */ + uint pos = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformThemeColor(TH_BACK); + immRecti(pos, back->xmin, back->ymin, back->xmax, back->ymax); + immUnbindProgram(); + + UI_draw_widget_scroll( + &wcol, scroll, &st->txtbar, (st->flags & ST_SCROLL_SELECT) ? UI_SCROLL_PRESSED : 0); + + UI_draw_roundbox_corner_set(UI_CNR_ALL); + rad = 0.4f * min_ii(BLI_rcti_size_x(&st->txtscroll), BLI_rcti_size_y(&st->txtscroll)); + UI_GetThemeColor3fv(TH_HILITE, col); + col[3] = 0.18f; + UI_draw_roundbox_aa(true, + st->txtscroll.xmin + 1, + st->txtscroll.ymin, + st->txtscroll.xmax - 1, + st->txtscroll.ymax, + rad, + col); } /*********************** draw documentation *******************************/ @@ -937,92 +983,92 @@ static void draw_textscroll(const SpaceText *st, rcti *scroll, rcti *back) #if 0 static void draw_documentation(const SpaceText *st, ARegion *ar) { - TextDrawContext tdc = {0}; - TextLine *tmp; - char *docs, buf[DOC_WIDTH + 1], *p; - int i, br, lines; - int boxw, boxh, l, x, y /* , top */ /* UNUSED */; - - if (!st || !st->text) return; - if (!texttool_text_is_active(st->text)) return; - - docs = texttool_docs_get(); - - if (!docs) return; - - text_draw_context_init(st, &tdc); - - /* Count the visible lines to the cursor */ - for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ; - if (l < 0) return; - - if (st->showlinenrs) { - x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4; - } - else { - x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4; - } - if (texttool_suggest_first()) { - x += SUGG_LIST_WIDTH * st->cwidth + 50; - } - - /* top = */ /* UNUSED */ y = ar->winy - st->lheight_dpi * l - 2; - boxw = DOC_WIDTH * st->cwidth + 20; - boxh = (DOC_HEIGHT + 1) * (st->lheight_dpi + TXT_LINE_SPACING); - - /* Draw panel */ - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - - immUniformThemeColor(TH_BACK); - immRecti(pos, x, y, x + boxw, y - boxh); - immUniformThemeColor(TH_SHADE1); - immBegin(GPU_PRIM_LINE_LOOP, 4); - immVertex2i(pos, x, y); - immVertex2i(pos, x + boxw, y); - immVertex2i(pos, x + boxw, y - boxh); - immVertex2i(pos, x, y - boxh); - immEnd(); - immBegin(GPU_PRIM_LINE_LOOP, 3); - immVertex2i(pos, x + boxw - 10, y - 7); - immVertex2i(pos, x + boxw - 4, y - 7); - immVertex2i(pos, x + boxw - 7, y - 2); - immEnd(); - immBegin(GPU_PRIM_LINE_LOOP, 3); - immVertex2i(pos, x + boxw - 10, y - boxh + 7); - immVertex2i(pos, x + boxw - 4, y - boxh + 7); - immVertex2i(pos, x + boxw - 7, y - boxh + 2); - immEnd(); - - immUnbindProgram(); - - UI_FontThemeColor(tdc.font_id, TH_TEXT); - - i = 0; br = DOC_WIDTH; lines = 0; // XXX -doc_scroll; - for (p = docs; *p; p++) { - if (*p == '\r' && *(++p) != '\n') *(--p) = '\n'; /* Fix line endings */ - if (*p == ' ' || *p == '\t') - br = i; - else if (*p == '\n') { - buf[i] = '\0'; - if (lines >= 0) { - y -= st->lheight_dpi; - text_draw(st, &tdc, buf, 0, 0, x + 4, y - 3, NULL); - } - i = 0; br = DOC_WIDTH; lines++; - } - buf[i++] = *p; - if (i == DOC_WIDTH) { /* Reached the width, go to last break and wrap there */ - buf[br] = '\0'; - if (lines >= 0) { - y -= st->lheight_dpi; - text_draw(st, &tdc, buf, 0, 0, x + 4, y - 3, NULL); - } - p -= i - br - 1; /* Rewind pointer to last break */ - i = 0; br = DOC_WIDTH; lines++; - } - if (lines >= DOC_HEIGHT) break; - } + TextDrawContext tdc = {0}; + TextLine *tmp; + char *docs, buf[DOC_WIDTH + 1], *p; + int i, br, lines; + int boxw, boxh, l, x, y /* , top */ /* UNUSED */; + + if (!st || !st->text) return; + if (!texttool_text_is_active(st->text)) return; + + docs = texttool_docs_get(); + + if (!docs) return; + + text_draw_context_init(st, &tdc); + + /* Count the visible lines to the cursor */ + for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ; + if (l < 0) return; + + if (st->showlinenrs) { + x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4; + } + else { + x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4; + } + if (texttool_suggest_first()) { + x += SUGG_LIST_WIDTH * st->cwidth + 50; + } + + /* top = */ /* UNUSED */ y = ar->winy - st->lheight_dpi * l - 2; + boxw = DOC_WIDTH * st->cwidth + 20; + boxh = (DOC_HEIGHT + 1) * (st->lheight_dpi + TXT_LINE_SPACING); + + /* Draw panel */ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + + immUniformThemeColor(TH_BACK); + immRecti(pos, x, y, x + boxw, y - boxh); + immUniformThemeColor(TH_SHADE1); + immBegin(GPU_PRIM_LINE_LOOP, 4); + immVertex2i(pos, x, y); + immVertex2i(pos, x + boxw, y); + immVertex2i(pos, x + boxw, y - boxh); + immVertex2i(pos, x, y - boxh); + immEnd(); + immBegin(GPU_PRIM_LINE_LOOP, 3); + immVertex2i(pos, x + boxw - 10, y - 7); + immVertex2i(pos, x + boxw - 4, y - 7); + immVertex2i(pos, x + boxw - 7, y - 2); + immEnd(); + immBegin(GPU_PRIM_LINE_LOOP, 3); + immVertex2i(pos, x + boxw - 10, y - boxh + 7); + immVertex2i(pos, x + boxw - 4, y - boxh + 7); + immVertex2i(pos, x + boxw - 7, y - boxh + 2); + immEnd(); + + immUnbindProgram(); + + UI_FontThemeColor(tdc.font_id, TH_TEXT); + + i = 0; br = DOC_WIDTH; lines = 0; // XXX -doc_scroll; + for (p = docs; *p; p++) { + if (*p == '\r' && *(++p) != '\n') *(--p) = '\n'; /* Fix line endings */ + if (*p == ' ' || *p == '\t') + br = i; + else if (*p == '\n') { + buf[i] = '\0'; + if (lines >= 0) { + y -= st->lheight_dpi; + text_draw(st, &tdc, buf, 0, 0, x + 4, y - 3, NULL); + } + i = 0; br = DOC_WIDTH; lines++; + } + buf[i++] = *p; + if (i == DOC_WIDTH) { /* Reached the width, go to last break and wrap there */ + buf[br] = '\0'; + if (lines >= 0) { + y -= st->lheight_dpi; + text_draw(st, &tdc, buf, 0, 0, x + 4, y - 3, NULL); + } + p -= i - br - 1; /* Rewind pointer to last break */ + i = 0; br = DOC_WIDTH; lines++; + } + if (lines >= DOC_HEIGHT) break; + } } #endif @@ -1030,641 +1076,683 @@ static void draw_documentation(const SpaceText *st, ARegion *ar) static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc, ARegion *ar) { - SuggItem *item, *first, *last, *sel; - char str[SUGG_LIST_WIDTH * BLI_UTF8_MAX + 1]; - int offl, offc, vcurl, vcurc; - int w, boxw = 0, boxh, i, x, y, *top; - const int lheight = st->lheight_dpi + TXT_LINE_SPACING; - const int margin_x = 2; + SuggItem *item, *first, *last, *sel; + char str[SUGG_LIST_WIDTH * BLI_UTF8_MAX + 1]; + int offl, offc, vcurl, vcurc; + int w, boxw = 0, boxh, i, x, y, *top; + const int lheight = st->lheight_dpi + TXT_LINE_SPACING; + const int margin_x = 2; - if (!st->text) return; - if (!texttool_text_is_active(st->text)) return; + if (!st->text) + return; + if (!texttool_text_is_active(st->text)) + return; - first = texttool_suggest_first(); - last = texttool_suggest_last(); + first = texttool_suggest_first(); + last = texttool_suggest_last(); - if (!first || !last) return; + if (!first || !last) + return; - text_pop_suggest_list(); - sel = texttool_suggest_selected(); - top = texttool_suggest_top(); + text_pop_suggest_list(); + sel = texttool_suggest_selected(); + top = texttool_suggest_top(); - wrap_offset(st, ar, st->text->curl, st->text->curc, &offl, &offc); - vcurl = txt_get_span(st->text->lines.first, st->text->curl) - st->top + offl; - vcurc = text_get_char_pos(st, st->text->curl->line, st->text->curc) - st->left + offc; + wrap_offset(st, ar, st->text->curl, st->text->curc, &offl, &offc); + vcurl = txt_get_span(st->text->lines.first, st->text->curl) - st->top + offl; + vcurc = text_get_char_pos(st, st->text->curl->line, st->text->curc) - st->left + offc; - x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - x += vcurc * st->cwidth - 4; - y = ar->winy - (vcurl + 1) * lheight - 2; + x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + x += vcurc * st->cwidth - 4; + y = ar->winy - (vcurl + 1) * lheight - 2; - /* offset back so the start of the text lines up with the suggestions, - * not essential but makes suggestions easier to follow */ - x -= st->cwidth * (st->text->curc - text_find_identifier_start(st->text->curl->line, st->text->curc)); + /* offset back so the start of the text lines up with the suggestions, + * not essential but makes suggestions easier to follow */ + x -= st->cwidth * + (st->text->curc - text_find_identifier_start(st->text->curl->line, st->text->curc)); - boxw = SUGG_LIST_WIDTH * st->cwidth + 20; - boxh = SUGG_LIST_SIZE * lheight + 8; + boxw = SUGG_LIST_WIDTH * st->cwidth + 20; + boxh = SUGG_LIST_SIZE * lheight + 8; - if (x + boxw > ar->winx) - x = MAX2(0, ar->winx - boxw); + if (x + boxw > ar->winx) + x = MAX2(0, ar->winx - boxw); - /* not needed but stands out nicer */ - UI_draw_box_shadow(220, x, y - boxh, x + boxw, y); + /* not needed but stands out nicer */ + UI_draw_box_shadow(220, x, y - boxh, x + boxw, y); - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + uint pos = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - immUniformThemeColor(TH_SHADE1); - immRecti(pos, x - 1, y + 1, x + boxw + 1, y - boxh - 1); - immUniformThemeColorShade(TH_BACK, 16); - immRecti(pos, x, y, x + boxw, y - boxh); + immUniformThemeColor(TH_SHADE1); + immRecti(pos, x - 1, y + 1, x + boxw + 1, y - boxh - 1); + immUniformThemeColorShade(TH_BACK, 16); + immRecti(pos, x, y, x + boxw, y - boxh); - immUnbindProgram(); + immUnbindProgram(); - /* Set the top 'item' of the visible list */ - for (i = 0, item = first; i < *top && item->next; i++, item = item->next) ; + /* Set the top 'item' of the visible list */ + for (i = 0, item = first; i < *top && item->next; i++, item = item->next) + ; - for (i = 0; i < SUGG_LIST_SIZE && item; i++, item = item->next) { - int len = txt_utf8_forward_columns(item->name, SUGG_LIST_WIDTH, NULL) - item->name; + for (i = 0; i < SUGG_LIST_SIZE && item; i++, item = item->next) { + int len = txt_utf8_forward_columns(item->name, SUGG_LIST_WIDTH, NULL) - item->name; - y -= lheight; + y -= lheight; - BLI_strncpy(str, item->name, len + 1); + BLI_strncpy(str, item->name, len + 1); - w = st->cwidth * text_get_char_pos(st, str, len); + w = st->cwidth * text_get_char_pos(st, str, len); - if (item == sel) { - uint posi = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + if (item == sel) { + uint posi = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - immUniformThemeColor(TH_SHADE2); - immRecti(posi, x + margin_x, y - 3, x + margin_x + w, y + lheight - 3); + immUniformThemeColor(TH_SHADE2); + immRecti(posi, x + margin_x, y - 3, x + margin_x + w, y + lheight - 3); - immUnbindProgram(); - } + immUnbindProgram(); + } - format_draw_color(tdc, item->type); - text_draw(st, tdc, str, 0, 0, x + margin_x, y - 1, NULL); + format_draw_color(tdc, item->type); + text_draw(st, tdc, str, 0, 0, x + margin_x, y - 1, NULL); - if (item == last) break; - } + if (item == last) + break; + } } /*********************** draw cursor ************************/ static void draw_text_decoration(SpaceText *st, ARegion *ar) { - Text *text = st->text; - int vcurl, vcurc, vsell, vselc, hidden = 0; - int x, y, w, i; - int offl, offc; - const int lheight = st->lheight_dpi + TXT_LINE_SPACING; - - /* Convert to view space character coordinates to determine if cursor is hidden */ - wrap_offset(st, ar, text->sell, text->selc, &offl, &offc); - vsell = txt_get_span(text->lines.first, text->sell) - st->top + offl; - vselc = text_get_char_pos(st, text->sell->line, text->selc) - st->left + offc; - - if (vselc < 0) { - vselc = 0; - hidden = 1; - } - - if (text->curl == text->sell && text->curc == text->selc && !st->line_hlight && hidden) { - /* Nothing to draw here */ - return; - } - - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - - /* Draw the selection */ - if (text->curl != text->sell || text->curc != text->selc) { - /* Convert all to view space character coordinates */ - wrap_offset(st, ar, text->curl, text->curc, &offl, &offc); - vcurl = txt_get_span(text->lines.first, text->curl) - st->top + offl; - vcurc = text_get_char_pos(st, text->curl->line, text->curc) - st->left + offc; - - if (vcurc < 0) { - vcurc = 0; - } - - immUniformThemeColor(TH_SHADE2); - - x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - y = ar->winy; - - if (vcurl == vsell) { - y -= vcurl * lheight; - - if (vcurc < vselc) { - immRecti(pos, x + vcurc * st->cwidth - 1, y, x + vselc * st->cwidth, y - lheight); - } - else { - immRecti(pos, x + vselc * st->cwidth - 1, y, x + vcurc * st->cwidth, y - lheight); - } - } - else { - int froml, fromc, tol, toc; - - if (vcurl < vsell) { - froml = vcurl; tol = vsell; - fromc = vcurc; toc = vselc; - } - else { - froml = vsell; tol = vcurl; - fromc = vselc; toc = vcurc; - } - - y -= froml * lheight; - - immRecti(pos, x + fromc * st->cwidth - 1, y, ar->winx, y - lheight); - y -= lheight; - - for (i = froml + 1; i < tol; i++) { - immRecti(pos, x - 4, y, ar->winx, y - lheight); - y -= lheight; - } - - immRecti(pos, x - 4, y, x + toc * st->cwidth, y - lheight); - y -= lheight; - } - } - - if (st->line_hlight) { - int x1, x2, y1, y2; - - if (st->wordwrap) { - int visible_lines = text_get_visible_lines(st, ar, text->sell->line); - - wrap_offset_in_line(st, ar, text->sell, text->selc, &offl, &offc); - - y1 = ar->winy - (vsell - offl) * lheight; - y2 = y1 - (lheight * visible_lines); - } - else { - y1 = ar->winy - vsell * lheight; - y2 = y1 - (lheight); - } - - if (!(y1 < 0 || y2 > ar->winy)) { /* check we need to draw */ - x1 = 0; // st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - x2 = x1 + ar->winx; - - immUniformColor4ub(255, 255, 255, 32); - - GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA); - GPU_blend(true); - immRecti(pos, x1 - 4, y1, x2, y2); - GPU_blend(false); - } - } - - if (!hidden) { - /* Draw the cursor itself (we draw the sel. cursor as this is the leading edge) */ - x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - x += vselc * st->cwidth; - y = ar->winy - vsell * lheight; - - immUniformThemeColor(TH_HILITE); - - if (st->overwrite) { - char ch = text->sell->line[text->selc]; - - y += TXT_LINE_SPACING; - w = st->cwidth; - if (ch == '\t') w *= st->tabnumber - (vselc + st->left) % st->tabnumber; - - immRecti(pos, x, y - lheight - 1, x + w, y - lheight + 1); - } - else { - immRecti(pos, x - 1, y, x + 1, y - lheight); - } - } - - immUnbindProgram(); + Text *text = st->text; + int vcurl, vcurc, vsell, vselc, hidden = 0; + int x, y, w, i; + int offl, offc; + const int lheight = st->lheight_dpi + TXT_LINE_SPACING; + + /* Convert to view space character coordinates to determine if cursor is hidden */ + wrap_offset(st, ar, text->sell, text->selc, &offl, &offc); + vsell = txt_get_span(text->lines.first, text->sell) - st->top + offl; + vselc = text_get_char_pos(st, text->sell->line, text->selc) - st->left + offc; + + if (vselc < 0) { + vselc = 0; + hidden = 1; + } + + if (text->curl == text->sell && text->curc == text->selc && !st->line_hlight && hidden) { + /* Nothing to draw here */ + return; + } + + uint pos = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + + /* Draw the selection */ + if (text->curl != text->sell || text->curc != text->selc) { + /* Convert all to view space character coordinates */ + wrap_offset(st, ar, text->curl, text->curc, &offl, &offc); + vcurl = txt_get_span(text->lines.first, text->curl) - st->top + offl; + vcurc = text_get_char_pos(st, text->curl->line, text->curc) - st->left + offc; + + if (vcurc < 0) { + vcurc = 0; + } + + immUniformThemeColor(TH_SHADE2); + + x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + y = ar->winy; + + if (vcurl == vsell) { + y -= vcurl * lheight; + + if (vcurc < vselc) { + immRecti(pos, x + vcurc * st->cwidth - 1, y, x + vselc * st->cwidth, y - lheight); + } + else { + immRecti(pos, x + vselc * st->cwidth - 1, y, x + vcurc * st->cwidth, y - lheight); + } + } + else { + int froml, fromc, tol, toc; + + if (vcurl < vsell) { + froml = vcurl; + tol = vsell; + fromc = vcurc; + toc = vselc; + } + else { + froml = vsell; + tol = vcurl; + fromc = vselc; + toc = vcurc; + } + + y -= froml * lheight; + + immRecti(pos, x + fromc * st->cwidth - 1, y, ar->winx, y - lheight); + y -= lheight; + + for (i = froml + 1; i < tol; i++) { + immRecti(pos, x - 4, y, ar->winx, y - lheight); + y -= lheight; + } + + immRecti(pos, x - 4, y, x + toc * st->cwidth, y - lheight); + y -= lheight; + } + } + + if (st->line_hlight) { + int x1, x2, y1, y2; + + if (st->wordwrap) { + int visible_lines = text_get_visible_lines(st, ar, text->sell->line); + + wrap_offset_in_line(st, ar, text->sell, text->selc, &offl, &offc); + + y1 = ar->winy - (vsell - offl) * lheight; + y2 = y1 - (lheight * visible_lines); + } + else { + y1 = ar->winy - vsell * lheight; + y2 = y1 - (lheight); + } + + if (!(y1 < 0 || y2 > ar->winy)) { /* check we need to draw */ + x1 = 0; // st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + x2 = x1 + ar->winx; + + immUniformColor4ub(255, 255, 255, 32); + + GPU_blend_set_func_separate( + GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA); + GPU_blend(true); + immRecti(pos, x1 - 4, y1, x2, y2); + GPU_blend(false); + } + } + + if (!hidden) { + /* Draw the cursor itself (we draw the sel. cursor as this is the leading edge) */ + x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + x += vselc * st->cwidth; + y = ar->winy - vsell * lheight; + + immUniformThemeColor(TH_HILITE); + + if (st->overwrite) { + char ch = text->sell->line[text->selc]; + + y += TXT_LINE_SPACING; + w = st->cwidth; + if (ch == '\t') + w *= st->tabnumber - (vselc + st->left) % st->tabnumber; + + immRecti(pos, x, y - lheight - 1, x + w, y - lheight + 1); + } + else { + immRecti(pos, x - 1, y, x + 1, y - lheight); + } + } + + immUnbindProgram(); } /******************* draw matching brackets *********************/ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegion *ar) { - TextLine *startl, *endl, *linep; - Text *text = st->text; - int b, fc, find, stack, viewc, viewl, offl, offc, x, y; - int startc, endc, c; - - char ch; - - // showsyntax must be on or else the format string will be null - if (!text->curl || !st->showsyntax) return; - - startl = text->curl; - startc = text->curc; - b = text_check_bracket(startl->line[startc]); - if (b == 0 && startc > 0) b = text_check_bracket(startl->line[--startc]); - if (b == 0) return; - - linep = startl; - c = startc; - fc = txt_utf8_offset_to_index(linep->line, startc); - endl = NULL; - endc = -1; - find = -b; - stack = 0; - - /* Don't highlight backets if syntax HL is off or bracket in string or comment. */ - if (!linep->format || linep->format[fc] == FMT_TYPE_STRING || linep->format[fc] == FMT_TYPE_COMMENT) - return; - - if (b > 0) { - /* opening bracket, search forward for close */ - fc++; - c += BLI_str_utf8_size_safe(linep->line + c); - while (linep) { - while (c < linep->len) { - if (linep->format && linep->format[fc] != FMT_TYPE_STRING && linep->format[fc] != FMT_TYPE_COMMENT) { - b = text_check_bracket(linep->line[c]); - if (b == find) { - if (stack == 0) { - endl = linep; - endc = c; - break; - } - stack--; - } - else if (b == -find) { - stack++; - } - } - fc++; - c += BLI_str_utf8_size_safe(linep->line + c); - } - if (endl) break; - linep = linep->next; - c = 0; - fc = 0; - } - } - else { - /* closing bracket, search backward for open */ - fc--; - if (c > 0) c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c); - while (linep) { - while (fc >= 0) { - if (linep->format && linep->format[fc] != FMT_TYPE_STRING && linep->format[fc] != FMT_TYPE_COMMENT) { - b = text_check_bracket(linep->line[c]); - if (b == find) { - if (stack == 0) { - endl = linep; - endc = c; - break; - } - stack--; - } - else if (b == -find) { - stack++; - } - } - fc--; - if (c > 0) c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c); - } - if (endl) break; - linep = linep->prev; - if (linep) { - if (linep->format) fc = strlen(linep->format) - 1; - else fc = -1; - if (linep->len) c = BLI_str_prev_char_utf8(linep->line + linep->len) - linep->line; - else fc = -1; - } - } - } - - if (!endl || endc == -1) - return; - - UI_FontThemeColor(tdc->font_id, TH_HILITE); - x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - y = ar->winy - st->lheight_dpi; - - /* draw opening bracket */ - ch = startl->line[startc]; - wrap_offset(st, ar, startl, startc, &offl, &offc); - viewc = text_get_char_pos(st, startl->line, startc) - st->left + offc; - - if (viewc >= 0) { - viewl = txt_get_span(text->lines.first, startl) - st->top + offl; - - text_font_draw_character(tdc, x + viewc * st->cwidth, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); - text_font_draw_character(tdc, x + viewc * st->cwidth + 1, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); - } - - /* draw closing bracket */ - ch = endl->line[endc]; - wrap_offset(st, ar, endl, endc, &offl, &offc); - viewc = text_get_char_pos(st, endl->line, endc) - st->left + offc; - - if (viewc >= 0) { - viewl = txt_get_span(text->lines.first, endl) - st->top + offl; - - text_font_draw_character(tdc, x + viewc * st->cwidth, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); - text_font_draw_character(tdc, x + viewc * st->cwidth + 1, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); - } + TextLine *startl, *endl, *linep; + Text *text = st->text; + int b, fc, find, stack, viewc, viewl, offl, offc, x, y; + int startc, endc, c; + + char ch; + + // showsyntax must be on or else the format string will be null + if (!text->curl || !st->showsyntax) + return; + + startl = text->curl; + startc = text->curc; + b = text_check_bracket(startl->line[startc]); + if (b == 0 && startc > 0) + b = text_check_bracket(startl->line[--startc]); + if (b == 0) + return; + + linep = startl; + c = startc; + fc = txt_utf8_offset_to_index(linep->line, startc); + endl = NULL; + endc = -1; + find = -b; + stack = 0; + + /* Don't highlight backets if syntax HL is off or bracket in string or comment. */ + if (!linep->format || linep->format[fc] == FMT_TYPE_STRING || + linep->format[fc] == FMT_TYPE_COMMENT) + return; + + if (b > 0) { + /* opening bracket, search forward for close */ + fc++; + c += BLI_str_utf8_size_safe(linep->line + c); + while (linep) { + while (c < linep->len) { + if (linep->format && linep->format[fc] != FMT_TYPE_STRING && + linep->format[fc] != FMT_TYPE_COMMENT) { + b = text_check_bracket(linep->line[c]); + if (b == find) { + if (stack == 0) { + endl = linep; + endc = c; + break; + } + stack--; + } + else if (b == -find) { + stack++; + } + } + fc++; + c += BLI_str_utf8_size_safe(linep->line + c); + } + if (endl) + break; + linep = linep->next; + c = 0; + fc = 0; + } + } + else { + /* closing bracket, search backward for open */ + fc--; + if (c > 0) + c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c); + while (linep) { + while (fc >= 0) { + if (linep->format && linep->format[fc] != FMT_TYPE_STRING && + linep->format[fc] != FMT_TYPE_COMMENT) { + b = text_check_bracket(linep->line[c]); + if (b == find) { + if (stack == 0) { + endl = linep; + endc = c; + break; + } + stack--; + } + else if (b == -find) { + stack++; + } + } + fc--; + if (c > 0) + c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c); + } + if (endl) + break; + linep = linep->prev; + if (linep) { + if (linep->format) + fc = strlen(linep->format) - 1; + else + fc = -1; + if (linep->len) + c = BLI_str_prev_char_utf8(linep->line + linep->len) - linep->line; + else + fc = -1; + } + } + } + + if (!endl || endc == -1) + return; + + UI_FontThemeColor(tdc->font_id, TH_HILITE); + x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + y = ar->winy - st->lheight_dpi; + + /* draw opening bracket */ + ch = startl->line[startc]; + wrap_offset(st, ar, startl, startc, &offl, &offc); + viewc = text_get_char_pos(st, startl->line, startc) - st->left + offc; + + if (viewc >= 0) { + viewl = txt_get_span(text->lines.first, startl) - st->top + offl; + + text_font_draw_character( + tdc, x + viewc * st->cwidth, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); + text_font_draw_character( + tdc, x + viewc * st->cwidth + 1, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); + } + + /* draw closing bracket */ + ch = endl->line[endc]; + wrap_offset(st, ar, endl, endc, &offl, &offc); + viewc = text_get_char_pos(st, endl->line, endc) - st->left + offc; + + if (viewc >= 0) { + viewl = txt_get_span(text->lines.first, endl) - st->top + offl; + + text_font_draw_character( + tdc, x + viewc * st->cwidth, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); + text_font_draw_character( + tdc, x + viewc * st->cwidth + 1, y - viewl * (st->lheight_dpi + TXT_LINE_SPACING), ch); + } } /*********************** main region drawing *************************/ void draw_text_main(SpaceText *st, ARegion *ar) { - TextDrawContext tdc = {0}; - Text *text = st->text; - TextFormatType *tft; - TextLine *tmp; - rcti scroll, back; - char linenr[12]; - int i, x, y, winx, linecount = 0, lineno = 0; - int wraplinecount = 0, wrap_skip = 0; - int margin_column_x; - - /* if no text, nothing to do */ - if (!text) - return; - - /* dpi controlled line height and font size */ - st->lheight_dpi = (U.widget_unit * st->lheight) / 20; - - /* don't draw lines below this */ - const int clip_min_y = -(int)(st->lheight_dpi - 1); - - st->viewlines = (st->lheight_dpi) ? (int)(ar->winy - clip_min_y) / (st->lheight_dpi + TXT_LINE_SPACING) : 0; - - text_draw_context_init(st, &tdc); - - text_update_drawcache(st, ar); - - /* make sure all the positional pointers exist */ - if (!text->curl || !text->sell || !text->lines.first || !text->lines.last) - txt_clean_text(text); - - /* update rects for scroll */ - calc_text_rcts(st, ar, &scroll, &back); /* scroll will hold the entire bar size */ - - /* update syntax formatting if needed */ - tft = ED_text_format_get(text); - tmp = text->lines.first; - lineno = 0; - for (i = 0; i < st->top && tmp; i++) { - if (st->showsyntax && !tmp->format) - tft->format_line(st, tmp, false); - - if (st->wordwrap) { - int lines = text_get_visible_lines_no(st, lineno); - - if (wraplinecount + lines > st->top) { - wrap_skip = st->top - wraplinecount; - break; - } - else { - wraplinecount += lines; - tmp = tmp->next; - linecount++; - } - } - else { - tmp = tmp->next; - linecount++; - } - - lineno++; - } - - - text_font_begin(&tdc); - - tdc.cwidth = max_ii((int)BLF_fixed_width(tdc.font_id), 1); - st->cwidth = tdc.cwidth; - - /* draw line numbers background */ - if (st->showlinenrs) { - x = TXT_OFFSET + TEXTXLOC; - - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - immUniformThemeColor(TH_GRID); - immRecti(pos, (TXT_OFFSET - 12), 0, (TXT_OFFSET - 5) + TEXTXLOC, ar->winy - 2); - immUnbindProgram(); - } - else { - st->linenrs_tot = 0; /* not used */ - x = TXT_OFFSET; - } - y = ar->winy - st->lheight_dpi; - winx = ar->winx - TXT_SCROLL_WIDTH; - - /* draw cursor, margin, selection and highlight */ - draw_text_decoration(st, ar); - - /* draw the text */ - UI_FontThemeColor(tdc.font_id, TH_TEXT); - - for (i = 0; y > clip_min_y && i < st->viewlines && tmp; i++, tmp = tmp->next) { - if (st->showsyntax && !tmp->format) - tft->format_line(st, tmp, false); - - if (st->showlinenrs && !wrap_skip) { - /* draw line number */ - if (tmp == text->curl) { - UI_FontThemeColor(tdc.font_id, TH_HILITE); - } - else { - UI_FontThemeColor(tdc.font_id, TH_TEXT); - } - - BLI_snprintf(linenr, sizeof(linenr), "%*d", st->linenrs_tot, i + linecount + 1); - /* itoa(i + linecount + 1, linenr, 10); */ /* not ansi-c :/ */ - text_font_draw(&tdc, TXT_OFFSET - 7, y, linenr); - - if (tmp == text->curl) { - UI_FontThemeColor(tdc.font_id, TH_TEXT); - } - } - - if (st->wordwrap) { - /* draw word wrapped text */ - int lines = text_draw_wrapped(st, &tdc, tmp->line, x, y, winx - x, tmp->format, wrap_skip); - y -= lines * (st->lheight_dpi + TXT_LINE_SPACING); - } - else { - /* draw unwrapped text */ - text_draw(st, &tdc, tmp->line, st->left, ar->winx / st->cwidth, x, y, tmp->format); - y -= st->lheight_dpi + TXT_LINE_SPACING; - } - - wrap_skip = 0; - } - - if (st->flags & ST_SHOW_MARGIN) { - margin_column_x = x + st->cwidth * (st->margin_column - st->left); - - if (margin_column_x >= x) { - const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - - immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR); - - float viewport_size[4]; - GPU_viewport_size_get_f(viewport_size); - immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC); - - immUniform1i("colors_len", 0); /* "simple" mode */ - immUniformThemeColor(TH_GRID); /* same color as line number background */ - immUniform1f("dash_width", 2.0f); - immUniform1f("dash_factor", 0.5f); - - immBegin(GPU_PRIM_LINES, 2); - immVertex2i(shdr_pos, margin_column_x, 0); - immVertex2i(shdr_pos, margin_column_x, ar->winy - 2); - immEnd(); - immUnbindProgram(); - } - } - - /* draw other stuff */ - draw_brackets(st, &tdc, ar); - draw_textscroll(st, &scroll, &back); - /* draw_documentation(st, ar); - No longer supported */ - draw_suggestion_list(st, &tdc, ar); - - text_font_end(&tdc); + TextDrawContext tdc = {0}; + Text *text = st->text; + TextFormatType *tft; + TextLine *tmp; + rcti scroll, back; + char linenr[12]; + int i, x, y, winx, linecount = 0, lineno = 0; + int wraplinecount = 0, wrap_skip = 0; + int margin_column_x; + + /* if no text, nothing to do */ + if (!text) + return; + + /* dpi controlled line height and font size */ + st->lheight_dpi = (U.widget_unit * st->lheight) / 20; + + /* don't draw lines below this */ + const int clip_min_y = -(int)(st->lheight_dpi - 1); + + st->viewlines = (st->lheight_dpi) ? + (int)(ar->winy - clip_min_y) / (st->lheight_dpi + TXT_LINE_SPACING) : + 0; + + text_draw_context_init(st, &tdc); + + text_update_drawcache(st, ar); + + /* make sure all the positional pointers exist */ + if (!text->curl || !text->sell || !text->lines.first || !text->lines.last) + txt_clean_text(text); + + /* update rects for scroll */ + calc_text_rcts(st, ar, &scroll, &back); /* scroll will hold the entire bar size */ + + /* update syntax formatting if needed */ + tft = ED_text_format_get(text); + tmp = text->lines.first; + lineno = 0; + for (i = 0; i < st->top && tmp; i++) { + if (st->showsyntax && !tmp->format) + tft->format_line(st, tmp, false); + + if (st->wordwrap) { + int lines = text_get_visible_lines_no(st, lineno); + + if (wraplinecount + lines > st->top) { + wrap_skip = st->top - wraplinecount; + break; + } + else { + wraplinecount += lines; + tmp = tmp->next; + linecount++; + } + } + else { + tmp = tmp->next; + linecount++; + } + + lineno++; + } + + text_font_begin(&tdc); + + tdc.cwidth = max_ii((int)BLF_fixed_width(tdc.font_id), 1); + st->cwidth = tdc.cwidth; + + /* draw line numbers background */ + if (st->showlinenrs) { + x = TXT_OFFSET + TEXTXLOC; + + uint pos = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformThemeColor(TH_GRID); + immRecti(pos, (TXT_OFFSET - 12), 0, (TXT_OFFSET - 5) + TEXTXLOC, ar->winy - 2); + immUnbindProgram(); + } + else { + st->linenrs_tot = 0; /* not used */ + x = TXT_OFFSET; + } + y = ar->winy - st->lheight_dpi; + winx = ar->winx - TXT_SCROLL_WIDTH; + + /* draw cursor, margin, selection and highlight */ + draw_text_decoration(st, ar); + + /* draw the text */ + UI_FontThemeColor(tdc.font_id, TH_TEXT); + + for (i = 0; y > clip_min_y && i < st->viewlines && tmp; i++, tmp = tmp->next) { + if (st->showsyntax && !tmp->format) + tft->format_line(st, tmp, false); + + if (st->showlinenrs && !wrap_skip) { + /* draw line number */ + if (tmp == text->curl) { + UI_FontThemeColor(tdc.font_id, TH_HILITE); + } + else { + UI_FontThemeColor(tdc.font_id, TH_TEXT); + } + + BLI_snprintf(linenr, sizeof(linenr), "%*d", st->linenrs_tot, i + linecount + 1); + /* itoa(i + linecount + 1, linenr, 10); */ /* not ansi-c :/ */ + text_font_draw(&tdc, TXT_OFFSET - 7, y, linenr); + + if (tmp == text->curl) { + UI_FontThemeColor(tdc.font_id, TH_TEXT); + } + } + + if (st->wordwrap) { + /* draw word wrapped text */ + int lines = text_draw_wrapped(st, &tdc, tmp->line, x, y, winx - x, tmp->format, wrap_skip); + y -= lines * (st->lheight_dpi + TXT_LINE_SPACING); + } + else { + /* draw unwrapped text */ + text_draw(st, &tdc, tmp->line, st->left, ar->winx / st->cwidth, x, y, tmp->format); + y -= st->lheight_dpi + TXT_LINE_SPACING; + } + + wrap_skip = 0; + } + + if (st->flags & ST_SHOW_MARGIN) { + margin_column_x = x + st->cwidth * (st->margin_column - st->left); + + if (margin_column_x >= x) { + const uint shdr_pos = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR); + + float viewport_size[4]; + GPU_viewport_size_get_f(viewport_size); + immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC); + + immUniform1i("colors_len", 0); /* "simple" mode */ + immUniformThemeColor(TH_GRID); /* same color as line number background */ + immUniform1f("dash_width", 2.0f); + immUniform1f("dash_factor", 0.5f); + + immBegin(GPU_PRIM_LINES, 2); + immVertex2i(shdr_pos, margin_column_x, 0); + immVertex2i(shdr_pos, margin_column_x, ar->winy - 2); + immEnd(); + immUnbindProgram(); + } + } + + /* draw other stuff */ + draw_brackets(st, &tdc, ar); + draw_textscroll(st, &scroll, &back); + /* draw_documentation(st, ar); - No longer supported */ + draw_suggestion_list(st, &tdc, ar); + + text_font_end(&tdc); } /************************** update ***************************/ void text_update_character_width(SpaceText *st) { - TextDrawContext tdc = {0}; + TextDrawContext tdc = {0}; - text_draw_context_init(st, &tdc); + text_draw_context_init(st, &tdc); - text_font_begin(&tdc); - st->cwidth = BLF_fixed_width(tdc.font_id); - st->cwidth = MAX2(st->cwidth, (char)1); - text_font_end(&tdc); + text_font_begin(&tdc); + st->cwidth = BLF_fixed_width(tdc.font_id); + st->cwidth = MAX2(st->cwidth, (char)1); + text_font_end(&tdc); } /* Moves the view to the cursor location, * also used to make sure the view isn't outside the file */ void text_scroll_to_cursor(SpaceText *st, ARegion *ar, const bool center) { - Text *text; - int i, x, winx = ar->winx; - - if (ELEM(NULL, st, st->text, st->text->curl)) return; - - text = st->text; - - text_update_character_width(st); - - i = txt_get_span(text->lines.first, text->sell); - if (st->wordwrap) { - int offl, offc; - wrap_offset(st, ar, text->sell, text->selc, &offl, &offc); - i += offl; - } - - if (center) { - if (st->top + st->viewlines <= i || st->top > i) { - st->top = i - st->viewlines / 2; - } - } - else { - if (st->top + st->viewlines <= i) { - st->top = i - (st->viewlines - 1); - } - else if (st->top > i) { - st->top = i; - } - } - - if (st->wordwrap) { - st->left = 0; - } - else { - x = st->cwidth * (text_get_char_pos(st, text->sell->line, text->selc) - st->left); - winx -= TXT_OFFSET + (st->showlinenrs ? TEXTXLOC : 0) + TXT_SCROLL_WIDTH; - - if (center) { - if (x <= 0 || x > winx) { - st->left += (x - winx / 2) / st->cwidth; - } - } - else { - if (x <= 0) { - st->left += ((x + 1) / st->cwidth) - 1; - } - else if (x > winx) { - st->left += ((x - (winx + 1)) / st->cwidth) + 1; - } - } - } - - if (st->top < 0) st->top = 0; - if (st->left < 0) st->left = 0; - - st->scroll_accum[0] = 0.0f; - st->scroll_accum[1] = 0.0f; + Text *text; + int i, x, winx = ar->winx; + + if (ELEM(NULL, st, st->text, st->text->curl)) + return; + + text = st->text; + + text_update_character_width(st); + + i = txt_get_span(text->lines.first, text->sell); + if (st->wordwrap) { + int offl, offc; + wrap_offset(st, ar, text->sell, text->selc, &offl, &offc); + i += offl; + } + + if (center) { + if (st->top + st->viewlines <= i || st->top > i) { + st->top = i - st->viewlines / 2; + } + } + else { + if (st->top + st->viewlines <= i) { + st->top = i - (st->viewlines - 1); + } + else if (st->top > i) { + st->top = i; + } + } + + if (st->wordwrap) { + st->left = 0; + } + else { + x = st->cwidth * (text_get_char_pos(st, text->sell->line, text->selc) - st->left); + winx -= TXT_OFFSET + (st->showlinenrs ? TEXTXLOC : 0) + TXT_SCROLL_WIDTH; + + if (center) { + if (x <= 0 || x > winx) { + st->left += (x - winx / 2) / st->cwidth; + } + } + else { + if (x <= 0) { + st->left += ((x + 1) / st->cwidth) - 1; + } + else if (x > winx) { + st->left += ((x - (winx + 1)) / st->cwidth) + 1; + } + } + } + + if (st->top < 0) + st->top = 0; + if (st->left < 0) + st->left = 0; + + st->scroll_accum[0] = 0.0f; + st->scroll_accum[1] = 0.0f; } /* takes an area instead of a region, use for listeners */ void text_scroll_to_cursor__area(SpaceText *st, ScrArea *sa, const bool center) { - ARegion *ar; + ARegion *ar; - if (ELEM(NULL, st, st->text, st->text->curl)) return; + if (ELEM(NULL, st, st->text, st->text->curl)) + return; - ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW); + ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW); - if (ar) { - text_scroll_to_cursor(st, ar, center); - } + if (ar) { + text_scroll_to_cursor(st, ar, center); + } } void text_update_cursor_moved(bContext *C) { - ScrArea *sa = CTX_wm_area(C); - SpaceText *st = CTX_wm_space_text(C); + ScrArea *sa = CTX_wm_area(C); + SpaceText *st = CTX_wm_space_text(C); - text_scroll_to_cursor__area(st, sa, true); + text_scroll_to_cursor__area(st, sa, true); } /** * Takes a cursor (row, character) and returns x,y pixel coords. */ -bool ED_text_region_location_from_cursor(SpaceText *st, ARegion *ar, const int cursor_co[2], int r_pixel_co[2]) +bool ED_text_region_location_from_cursor(SpaceText *st, + ARegion *ar, + const int cursor_co[2], + int r_pixel_co[2]) { - TextLine *line = NULL; - - if (!st->text) { - goto error; - } - - line = BLI_findlink(&st->text->lines, cursor_co[0]); - if (!line || (cursor_co[1] < 0) || (cursor_co[1] > line->len)) { - goto error; - } - else { - int offl, offc; - int linenr_offset = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - /* handle tabs as well! */ - int char_pos = text_get_char_pos(st, line->line, cursor_co[1]); - - wrap_offset(st, ar, line, cursor_co[1], &offl, &offc); - r_pixel_co[0] = (char_pos + offc - st->left) * st->cwidth + linenr_offset; - r_pixel_co[1] = (cursor_co[0] + offl - st->top) * (st->lheight_dpi + TXT_LINE_SPACING); - r_pixel_co[1] = (ar->winy - (r_pixel_co[1] + TXT_OFFSET)) - st->lheight_dpi; - } - return true; - + TextLine *line = NULL; + + if (!st->text) { + goto error; + } + + line = BLI_findlink(&st->text->lines, cursor_co[0]); + if (!line || (cursor_co[1] < 0) || (cursor_co[1] > line->len)) { + goto error; + } + else { + int offl, offc; + int linenr_offset = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + /* handle tabs as well! */ + int char_pos = text_get_char_pos(st, line->line, cursor_co[1]); + + wrap_offset(st, ar, line, cursor_co[1], &offl, &offc); + r_pixel_co[0] = (char_pos + offc - st->left) * st->cwidth + linenr_offset; + r_pixel_co[1] = (cursor_co[0] + offl - st->top) * (st->lheight_dpi + TXT_LINE_SPACING); + r_pixel_co[1] = (ar->winy - (r_pixel_co[1] + TXT_OFFSET)) - st->lheight_dpi; + } + return true; error: - r_pixel_co[0] = r_pixel_co[1] = -1; - return false; + r_pixel_co[0] = r_pixel_co[1] = -1; + return false; } diff --git a/source/blender/editors/space_text/text_format.c b/source/blender/editors/space_text/text_format.c index 0f5dfed080c..0a885fc5875 100644 --- a/source/blender/editors/space_text/text_format.c +++ b/source/blender/editors/space_text/text_format.c @@ -31,105 +31,107 @@ #include "text_format.h" - /****************** flatten string **********************/ static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len) { - int i; + int i; - if (fs->pos + len > fs->len) { - char *nbuf; int *naccum; - fs->len *= 2; + if (fs->pos + len > fs->len) { + char *nbuf; + int *naccum; + fs->len *= 2; - nbuf = MEM_callocN(sizeof(*fs->buf) * fs->len, "fs->buf"); - naccum = MEM_callocN(sizeof(*fs->accum) * fs->len, "fs->accum"); + nbuf = MEM_callocN(sizeof(*fs->buf) * fs->len, "fs->buf"); + naccum = MEM_callocN(sizeof(*fs->accum) * fs->len, "fs->accum"); - memcpy(nbuf, fs->buf, fs->pos * sizeof(*fs->buf)); - memcpy(naccum, fs->accum, fs->pos * sizeof(*fs->accum)); + memcpy(nbuf, fs->buf, fs->pos * sizeof(*fs->buf)); + memcpy(naccum, fs->accum, fs->pos * sizeof(*fs->accum)); - if (fs->buf != fs->fixedbuf) { - MEM_freeN(fs->buf); - MEM_freeN(fs->accum); - } + if (fs->buf != fs->fixedbuf) { + MEM_freeN(fs->buf); + MEM_freeN(fs->accum); + } - fs->buf = nbuf; - fs->accum = naccum; - } + fs->buf = nbuf; + fs->accum = naccum; + } - for (i = 0; i < len; i++) { - fs->buf[fs->pos + i] = c[i]; - fs->accum[fs->pos + i] = accum; - } + for (i = 0; i < len; i++) { + fs->buf[fs->pos + i] = c[i]; + fs->accum[fs->pos + i] = accum; + } - fs->pos += len; + fs->pos += len; } int flatten_string(const SpaceText *st, FlattenString *fs, const char *in) { - int r, i, total = 0; - - memset(fs, 0, sizeof(FlattenString)); - fs->buf = fs->fixedbuf; - fs->accum = fs->fixedaccum; - fs->len = sizeof(fs->fixedbuf); - - for (r = 0, i = 0; *in; r++) { - if (*in == '\t') { - i = st->tabnumber - (total % st->tabnumber); - total += i; - - while (i--) - flatten_string_append(fs, " ", r, 1); - - in++; - } - else { - size_t len = BLI_str_utf8_size_safe(in); - flatten_string_append(fs, in, r, len); - in += len; - total++; - } - } - - flatten_string_append(fs, "\0", r, 1); - - return total; + int r, i, total = 0; + + memset(fs, 0, sizeof(FlattenString)); + fs->buf = fs->fixedbuf; + fs->accum = fs->fixedaccum; + fs->len = sizeof(fs->fixedbuf); + + for (r = 0, i = 0; *in; r++) { + if (*in == '\t') { + i = st->tabnumber - (total % st->tabnumber); + total += i; + + while (i--) + flatten_string_append(fs, " ", r, 1); + + in++; + } + else { + size_t len = BLI_str_utf8_size_safe(in); + flatten_string_append(fs, in, r, len); + in += len; + total++; + } + } + + flatten_string_append(fs, "\0", r, 1); + + return total; } void flatten_string_free(FlattenString *fs) { - if (fs->buf != fs->fixedbuf) - MEM_freeN(fs->buf); - if (fs->accum != fs->fixedaccum) - MEM_freeN(fs->accum); + if (fs->buf != fs->fixedbuf) + MEM_freeN(fs->buf); + if (fs->accum != fs->fixedaccum) + MEM_freeN(fs->accum); } /* takes a string within fs->buf and returns its length */ int flatten_string_strlen(FlattenString *fs, const char *str) { - const int len = (fs->pos - (int)(str - fs->buf)) - 1; - BLI_assert(strlen(str) == len); - return len; + const int len = (fs->pos - (int)(str - fs->buf)) - 1; + BLI_assert(strlen(str) == len); + return len; } /* Ensures the format string for the given line is long enough, reallocating * as needed. Allocation is done here, alone, to ensure consistency. */ int text_check_format_len(TextLine *line, unsigned int len) { - if (line->format) { - if (strlen(line->format) < len) { - MEM_freeN(line->format); - line->format = MEM_mallocN(len + 2, "SyntaxFormat"); - if (!line->format) return 0; - } - } - else { - line->format = MEM_mallocN(len + 2, "SyntaxFormat"); - if (!line->format) return 0; - } - - return 1; + if (line->format) { + if (strlen(line->format) < len) { + MEM_freeN(line->format); + line->format = MEM_mallocN(len + 2, "SyntaxFormat"); + if (!line->format) + return 0; + } + } + else { + line->format = MEM_mallocN(len + 2, "SyntaxFormat"); + if (!line->format) + return 0; + } + + return 1; } /** @@ -140,25 +142,25 @@ int text_check_format_len(TextLine *line, unsigned int len) */ void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len) { - const char *str = *str_p; - char *fmt = *fmt_p; - int i = 0; + const char *str = *str_p; + char *fmt = *fmt_p; + int i = 0; - while (i < len) { - const int size = BLI_str_utf8_size_safe(str); - *fmt++ = type; + while (i < len) { + const int size = BLI_str_utf8_size_safe(str); + *fmt++ = type; - str += size; - i += 1; - } + str += size; + i += 1; + } - str--; - fmt--; + str--; + fmt--; - BLI_assert(*str != '\0'); + BLI_assert(*str != '\0'); - *str_p = str; - *fmt_p = fmt; + *str_p = str; + *fmt_p = fmt; } /** * ascii version of #text_format_fill, @@ -166,54 +168,54 @@ void text_format_fill(const char **str_p, char **fmt_p, const char type, const i */ void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len) { - const char *str = *str_p; - char *fmt = *fmt_p; + const char *str = *str_p; + char *fmt = *fmt_p; - memset(fmt, type, len); + memset(fmt, type, len); - str += len - 1; - fmt += len - 1; + str += len - 1; + fmt += len - 1; - BLI_assert(*str != '\0'); + BLI_assert(*str != '\0'); - *str_p = str; - *fmt_p = fmt; + *str_p = str; + *fmt_p = fmt; } /* *** Registration *** */ static ListBase tft_lb = {NULL, NULL}; void ED_text_format_register(TextFormatType *tft) { - BLI_addtail(&tft_lb, tft); + BLI_addtail(&tft_lb, tft); } TextFormatType *ED_text_format_get(Text *text) { - TextFormatType *tft; - - if (text) { - const char *text_ext = strchr(text->id.name + 2, '.'); - if (text_ext) { - text_ext++; /* skip the '.' */ - /* Check all text formats in the static list */ - for (tft = tft_lb.first; tft; tft = tft->next) { - /* All formats should have an ext, but just in case */ - const char **ext; - for (ext = tft->ext; *ext; ext++) { - /* If extension matches text name, return the matching tft */ - if (BLI_strcasecmp(text_ext, *ext) == 0) { - return tft; - } - } - } - } - - /* If we make it here we never found an extension that worked - return - * the "default" text format */ - return tft_lb.first; - } - else { - /* Return the "default" text format */ - return tft_lb.first; - } + TextFormatType *tft; + + if (text) { + const char *text_ext = strchr(text->id.name + 2, '.'); + if (text_ext) { + text_ext++; /* skip the '.' */ + /* Check all text formats in the static list */ + for (tft = tft_lb.first; tft; tft = tft->next) { + /* All formats should have an ext, but just in case */ + const char **ext; + for (ext = tft->ext; *ext; ext++) { + /* If extension matches text name, return the matching tft */ + if (BLI_strcasecmp(text_ext, *ext) == 0) { + return tft; + } + } + } + } + + /* If we make it here we never found an extension that worked - return + * the "default" text format */ + return tft_lb.first; + } + else { + /* Return the "default" text format */ + return tft_lb.first; + } } diff --git a/source/blender/editors/space_text/text_format.h b/source/blender/editors/space_text/text_format.h index 9fac0a34d77..07635e4227a 100644 --- a/source/blender/editors/space_text/text_format.h +++ b/source/blender/editors/space_text/text_format.h @@ -26,80 +26,80 @@ /* *** Flatten String *** */ typedef struct FlattenString { - char fixedbuf[256]; - int fixedaccum[256]; + char fixedbuf[256]; + int fixedaccum[256]; - char *buf; - int *accum; - int pos, len; + char *buf; + int *accum; + int pos, len; } FlattenString; /* format continuation flags (stored just after the NULL terminator) */ enum { - FMT_CONT_NOP = 0, /* no continuation */ - FMT_CONT_QUOTESINGLE = (1 << 0), /* single quotes */ - FMT_CONT_QUOTEDOUBLE = (1 << 1), /* double quotes */ - FMT_CONT_TRIPLE = (1 << 2), /* triplets of quotes: """ or ''' */ - FMT_CONT_QUOTESINGLE_TRIPLE = (FMT_CONT_TRIPLE | FMT_CONT_QUOTESINGLE), - FMT_CONT_QUOTEDOUBLE_TRIPLE = (FMT_CONT_TRIPLE | FMT_CONT_QUOTEDOUBLE), - FMT_CONT_COMMENT_C = (1 << 3) /* multi-line comments, OSL only (C style) */ + FMT_CONT_NOP = 0, /* no continuation */ + FMT_CONT_QUOTESINGLE = (1 << 0), /* single quotes */ + FMT_CONT_QUOTEDOUBLE = (1 << 1), /* double quotes */ + FMT_CONT_TRIPLE = (1 << 2), /* triplets of quotes: """ or ''' */ + FMT_CONT_QUOTESINGLE_TRIPLE = (FMT_CONT_TRIPLE | FMT_CONT_QUOTESINGLE), + FMT_CONT_QUOTEDOUBLE_TRIPLE = (FMT_CONT_TRIPLE | FMT_CONT_QUOTEDOUBLE), + FMT_CONT_COMMENT_C = (1 << 3) /* multi-line comments, OSL only (C style) */ }; #define FMT_CONT_ALL \ - (FMT_CONT_QUOTESINGLE | FMT_CONT_QUOTEDOUBLE | FMT_CONT_TRIPLE | FMT_CONT_COMMENT_C) + (FMT_CONT_QUOTESINGLE | FMT_CONT_QUOTEDOUBLE | FMT_CONT_TRIPLE | FMT_CONT_COMMENT_C) -int flatten_string(const struct SpaceText *st, FlattenString *fs, const char *in); +int flatten_string(const struct SpaceText *st, FlattenString *fs, const char *in); void flatten_string_free(FlattenString *fs); -int flatten_string_strlen(FlattenString *fs, const char *str); +int flatten_string_strlen(FlattenString *fs, const char *str); -int text_check_format_len(TextLine *line, unsigned int len); +int text_check_format_len(TextLine *line, unsigned int len); void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len); void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len); /* *** Generalize Formatting *** */ typedef struct TextFormatType { - struct TextFormatType *next, *prev; - - char (*format_identifier)(const char *string); - - /* Formats the specified line. If do_next is set, the process will move on to - * the succeeding line if it is affected (eg. multiline strings). Format strings - * may contain any of the following characters: - * - * It is terminated with a null-terminator '\0' followed by a continuation - * flag indicating whether the line is part of a multi-line string. - * - * See: FMT_TYPE_ enums below - */ - void (*format_line)(SpaceText *st, TextLine *line, const bool do_next); - - const char **ext; /* NULL terminated extensions */ + struct TextFormatType *next, *prev; + + char (*format_identifier)(const char *string); + + /* Formats the specified line. If do_next is set, the process will move on to + * the succeeding line if it is affected (eg. multiline strings). Format strings + * may contain any of the following characters: + * + * It is terminated with a null-terminator '\0' followed by a continuation + * flag indicating whether the line is part of a multi-line string. + * + * See: FMT_TYPE_ enums below + */ + void (*format_line)(SpaceText *st, TextLine *line, const bool do_next); + + const char **ext; /* NULL terminated extensions */ } TextFormatType; enum { - /** Whitespace */ - FMT_TYPE_WHITESPACE = '_', - /** Comment text */ - FMT_TYPE_COMMENT = '#', - /** Punctuation and other symbols */ - FMT_TYPE_SYMBOL = '!', - /** Numerals */ - FMT_TYPE_NUMERAL = 'n', - /** String letters */ - FMT_TYPE_STRING = 'l', - /** Decorator / Preprocessor directive */ - FMT_TYPE_DIRECTIVE = 'd', - /** Special variables (class, def) */ - FMT_TYPE_SPECIAL = 'v', - /** Reserved keywords currently not in use, but still prohibited (OSL -> switch e.g.) */ - FMT_TYPE_RESERVED = 'r', - /** Built-in names (return, for, etc.) */ - FMT_TYPE_KEYWORD = 'b', - /** Regular text (identifiers, etc.) */ - FMT_TYPE_DEFAULT = 'q', + /** Whitespace */ + FMT_TYPE_WHITESPACE = '_', + /** Comment text */ + FMT_TYPE_COMMENT = '#', + /** Punctuation and other symbols */ + FMT_TYPE_SYMBOL = '!', + /** Numerals */ + FMT_TYPE_NUMERAL = 'n', + /** String letters */ + FMT_TYPE_STRING = 'l', + /** Decorator / Preprocessor directive */ + FMT_TYPE_DIRECTIVE = 'd', + /** Special variables (class, def) */ + FMT_TYPE_SPECIAL = 'v', + /** Reserved keywords currently not in use, but still prohibited (OSL -> switch e.g.) */ + FMT_TYPE_RESERVED = 'r', + /** Built-in names (return, for, etc.) */ + FMT_TYPE_KEYWORD = 'b', + /** Regular text (identifiers, etc.) */ + FMT_TYPE_DEFAULT = 'q', }; TextFormatType *ED_text_format_get(Text *text); -void ED_text_format_register(TextFormatType *tft); +void ED_text_format_register(TextFormatType *tft); /* formatters */ void ED_text_format_register_py(void); @@ -109,6 +109,6 @@ void ED_text_format_register_pov(void); void ED_text_format_register_pov_ini(void); #define STR_LITERAL_STARTSWITH(str, str_literal, len_var) \ - (strncmp(str, str_literal, len_var = (sizeof(str_literal) - 1)) == 0) + (strncmp(str, str_literal, len_var = (sizeof(str_literal) - 1)) == 0) -#endif /* __TEXT_FORMAT_H__ */ +#endif /* __TEXT_FORMAT_H__ */ diff --git a/source/blender/editors/space_text/text_format_lua.c b/source/blender/editors/space_text/text_format_lua.c index 30080d2395b..b82e8aec122 100644 --- a/source/blender/editors/space_text/text_format_lua.c +++ b/source/blender/editors/space_text/text_format_lua.c @@ -43,37 +43,37 @@ */ static int txtfmt_lua_find_keyword(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - if (STR_LITERAL_STARTSWITH(string, "and", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "do", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "function", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "in", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "not", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "or", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "repeat", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "then", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "until", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + if (STR_LITERAL_STARTSWITH(string, "and", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "do", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "function", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "in", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "not", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "or", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "repeat", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "then", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "until", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } /** @@ -89,243 +89,261 @@ static int txtfmt_lua_find_keyword(const char *string) */ static int txtfmt_lua_find_specialvar(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - if (STR_LITERAL_STARTSWITH(string, "assert", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "collectgarbage", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "dofile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "_G", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "getfenv", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "getmetatable", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "__index", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ipairs", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "load", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "loadfile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "loadstring", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "next", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pairs", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pcall", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "print", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rawequal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rawget", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rawset", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "select", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "setfenv", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "setmetatable", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tonumber", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tostring", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "type", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "unpack", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "_VERSION", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "xpcall", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + if (STR_LITERAL_STARTSWITH(string, "assert", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "collectgarbage", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dofile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "_G", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "getfenv", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "getmetatable", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "__index", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ipairs", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "load", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "loadfile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "loadstring", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "next", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pairs", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pcall", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "print", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rawequal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rawget", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rawset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "select", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "setfenv", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "setmetatable", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tonumber", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tostring", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "unpack", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "_VERSION", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "xpcall", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } static int txtfmt_lua_find_bool(const char *string) { - int i, len; - - if (STR_LITERAL_STARTSWITH(string, "nil", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + int i, len; + + if (STR_LITERAL_STARTSWITH(string, "nil", len)) + i = len; + else if (STR_LITERAL_STARTSWITH(string, "true", len)) + i = len; + else if (STR_LITERAL_STARTSWITH(string, "false", len)) + i = len; + else + i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } static char txtfmt_lua_format_identifier(const char *str) { - char fmt; + char fmt; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - if ((txtfmt_lua_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; - else if ((txtfmt_lua_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; - else fmt = FMT_TYPE_DEFAULT; + if ((txtfmt_lua_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; + else if ((txtfmt_lua_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; + else fmt = FMT_TYPE_DEFAULT; - /* clang-format on */ + /* clang-format on */ - return fmt; + return fmt; } static void txtfmt_lua_format_line(SpaceText *st, TextLine *line, const bool do_next) { - FlattenString fs; - const char *str; - char *fmt; - char cont_orig, cont, find, prev = ' '; - int len, i; - - /* Get continuation from previous line */ - if (line->prev && line->prev->format != NULL) { - fmt = line->prev->format; - cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont) == cont); - } - else { - cont = FMT_CONT_NOP; - } - - /* Get original continuation from this line */ - if (line->format != NULL) { - fmt = line->format; - cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); - } - else { - cont_orig = 0xFF; - } - - len = flatten_string(st, &fs, line->line); - str = fs.buf; - if (!text_check_format_len(line, len)) { - flatten_string_free(&fs); - return; - } - fmt = line->format; - - while (*str) { - /* Handle escape sequences by skipping both \ and next char */ - if (*str == '\\') { - *fmt = prev; fmt++; str++; - if (*str == '\0') break; - *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); - continue; - } - /* Handle continuations */ - else if (cont) { - /* Multi-line comments */ - if (cont & FMT_CONT_COMMENT_C) { - if (*str == ']' && *(str + 1) == ']') { - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - cont = FMT_CONT_NOP; - } - else { - *fmt = FMT_TYPE_COMMENT; - } - /* Handle other comments */ - } - else { - find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; - if (*str == find) cont = 0; - *fmt = FMT_TYPE_STRING; - } - - str += BLI_str_utf8_size_safe(str) - 1; - } - /* Not in a string... */ - else { - /* Multi-line comments */ - if (*str == '-' && *(str + 1) == '-' && - *(str + 2) == '[' && *(str + 3) == '[') - { - cont = FMT_CONT_COMMENT_C; - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - } - /* Single line comment */ - else if (*str == '-' && *(str + 1) == '-') { - text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); - } - else if (*str == '"' || *str == '\'') { - /* Strings */ - find = *str; - cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; - *fmt = FMT_TYPE_STRING; - } - /* Whitespace (all ws. has been converted to spaces) */ - else if (*str == ' ') { - *fmt = FMT_TYPE_WHITESPACE; - } - /* Numbers (digits not part of an identifier and periods followed by digits) */ - else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || - (*str == '.' && text_check_digit(*(str + 1)))) - { - *fmt = FMT_TYPE_NUMERAL; - } - /* Booleans */ - else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_lua_find_bool(str)) != -1) { - if (i > 0) { - text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - /* Punctuation */ - else if ((*str != '#') && text_check_delim(*str)) { - *fmt = FMT_TYPE_SYMBOL; - } - /* Identifiers and other text (no previous ws. or delims. so text continues) */ - else if (prev == FMT_TYPE_DEFAULT) { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ - else { - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Special vars(v) or built-in keywords(b) */ - /* keep in sync with 'txtfmt_osl_format_identifier()' */ - if ((i = txtfmt_lua_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; - else if ((i = txtfmt_lua_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; - - /* clang-format on */ - - if (i > 0) { - text_format_fill_ascii(&str, &fmt, prev, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - } - prev = *fmt; fmt++; str++; - } - - /* Terminate and add continuation char */ - *fmt = '\0'; fmt++; - *fmt = cont; - - /* If continuation has changed and we're allowed, process the next line */ - if (cont != cont_orig && do_next && line->next) { - txtfmt_lua_format_line(st, line->next, do_next); - } - - flatten_string_free(&fs); + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; + fmt++; + str++; + if (*str == '\0') + break; + *fmt = prev; + fmt++; + str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* Multi-line comments */ + if (cont & FMT_CONT_COMMENT_C) { + if (*str == ']' && *(str + 1) == ']') { + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + cont = FMT_CONT_NOP; + } + else { + *fmt = FMT_TYPE_COMMENT; + } + /* Handle other comments */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) + cont = 0; + *fmt = FMT_TYPE_STRING; + } + + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* Multi-line comments */ + if (*str == '-' && *(str + 1) == '-' && *(str + 2) == '[' && *(str + 3) == '[') { + cont = FMT_CONT_COMMENT_C; + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + } + /* Single line comment */ + else if (*str == '-' && *(str + 1) == '-') { + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) { + *fmt = FMT_TYPE_NUMERAL; + } + /* Booleans */ + else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_lua_find_bool(str)) != -1) { + if (i > 0) { + text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + /* Punctuation */ + else if ((*str != '#') && text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_osl_format_identifier()' */ + if ((i = txtfmt_lua_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; + else if ((i = txtfmt_lua_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; + + /* clang-format on */ + + if (i > 0) { + text_format_fill_ascii(&str, &fmt, prev, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; + fmt++; + str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; + fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_lua_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); } void ED_text_format_register_lua(void) { - static TextFormatType tft = {NULL}; - static const char *ext[] = {"lua", NULL}; + static TextFormatType tft = {NULL}; + static const char *ext[] = {"lua", NULL}; - tft.format_identifier = txtfmt_lua_format_identifier; - tft.format_line = txtfmt_lua_format_line; - tft.ext = ext; + tft.format_identifier = txtfmt_lua_format_identifier; + tft.format_line = txtfmt_lua_format_line; + tft.ext = ext; - ED_text_format_register(&tft); + ED_text_format_register(&tft); } diff --git a/source/blender/editors/space_text/text_format_osl.c b/source/blender/editors/space_text/text_format_osl.c index 66980b30c4d..8473b3f80ce 100644 --- a/source/blender/editors/space_text/text_format_osl.c +++ b/source/blender/editors/space_text/text_format_osl.c @@ -32,102 +32,102 @@ static int txtfmt_osl_find_builtinfunc(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* list is from - * https://github.com/imageworks/OpenShadingLanguage/raw/master/src/doc/osl-languagespec.pdf - */ - if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "closure", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "color", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "continue", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "do", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "emit", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "float", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "illuminance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "illuminate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "int", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "matrix", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "normal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "output", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "point", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "public", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "string", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "struct", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vector", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "void", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* list is from + * https://github.com/imageworks/OpenShadingLanguage/raw/master/src/doc/osl-languagespec.pdf + */ + if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "closure", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "continue", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "do", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "emit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "float", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "illuminance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "illuminate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "int", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "matrix", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "output", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "point", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "public", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "string", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "struct", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vector", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "void", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } static int txtfmt_osl_find_reserved(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* list is from... - * https://github.com/imageworks/OpenShadingLanguage/raw/master/src/doc/osl-languagespec.pdf - */ - if (STR_LITERAL_STARTSWITH(string, "bool", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "catch", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "char", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "const", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "delete", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "double", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "enum", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "extern", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "friend", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "goto", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "long", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "new", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "operator", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "private", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "protected", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "short", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "signed", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sizeof", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "static", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "template", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "this", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "throw", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "try", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "typedef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uniform", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "union", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "unsigned", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "varying", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "virtual", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "volatile", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* list is from... + * https://github.com/imageworks/OpenShadingLanguage/raw/master/src/doc/osl-languagespec.pdf + */ + if (STR_LITERAL_STARTSWITH(string, "bool", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "catch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "char", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "const", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "delete", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "double", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "enum", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "extern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "friend", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "goto", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "long", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "new", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "operator", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "private", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "protected", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "short", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "signed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sizeof", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "static", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "template", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "this", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "throw", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "try", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "typedef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uniform", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "union", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "unsigned", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "varying", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "virtual", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "volatile", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } /* Checks the specified source string for a OSL special name. This name must @@ -139,216 +139,228 @@ static int txtfmt_osl_find_reserved(const char *string) static int txtfmt_osl_find_specialvar(const char *string) { - int i, len; + int i, len; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - /* OSL shader types */ - if (STR_LITERAL_STARTSWITH(string, "shader", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "surface", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "volume", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "displacement", len)) i = len; - else i = 0; + /* OSL shader types */ + if (STR_LITERAL_STARTSWITH(string, "shader", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "surface", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "volume", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "displacement", len)) i = len; + else i = 0; - /* clang-format on */ + /* clang-format on */ - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } /* matches py 'txtfmt_osl_find_decorator' */ static int txtfmt_osl_find_preprocessor(const char *string) { - if (string[0] == '#') { - int i = 1; - /* Whitespace is ok '# foo' */ - while (text_check_whitespace(string[i])) { - i++; - } - while (text_check_identifier(string[i])) { - i++; - } - return i; - } - return -1; + if (string[0] == '#') { + int i = 1; + /* Whitespace is ok '# foo' */ + while (text_check_whitespace(string[i])) { + i++; + } + while (text_check_identifier(string[i])) { + i++; + } + return i; + } + return -1; } static char txtfmt_osl_format_identifier(const char *str) { - char fmt; + char fmt; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - if ((txtfmt_osl_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; - else if ((txtfmt_osl_find_builtinfunc(str)) != -1) fmt = FMT_TYPE_KEYWORD; - else if ((txtfmt_osl_find_reserved(str)) != -1) fmt = FMT_TYPE_RESERVED; - else if ((txtfmt_osl_find_preprocessor(str)) != -1) fmt = FMT_TYPE_DIRECTIVE; - else fmt = FMT_TYPE_DEFAULT; + if ((txtfmt_osl_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; + else if ((txtfmt_osl_find_builtinfunc(str)) != -1) fmt = FMT_TYPE_KEYWORD; + else if ((txtfmt_osl_find_reserved(str)) != -1) fmt = FMT_TYPE_RESERVED; + else if ((txtfmt_osl_find_preprocessor(str)) != -1) fmt = FMT_TYPE_DIRECTIVE; + else fmt = FMT_TYPE_DEFAULT; - /* clang-format on */ + /* clang-format on */ - return fmt; + return fmt; } static void txtfmt_osl_format_line(SpaceText *st, TextLine *line, const bool do_next) { - FlattenString fs; - const char *str; - char *fmt; - char cont_orig, cont, find, prev = ' '; - int len, i; - - /* Get continuation from previous line */ - if (line->prev && line->prev->format != NULL) { - fmt = line->prev->format; - cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont) == cont); - } - else { - cont = FMT_CONT_NOP; - } - - /* Get original continuation from this line */ - if (line->format != NULL) { - fmt = line->format; - cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); - } - else { - cont_orig = 0xFF; - } - - len = flatten_string(st, &fs, line->line); - str = fs.buf; - if (!text_check_format_len(line, len)) { - flatten_string_free(&fs); - return; - } - fmt = line->format; - - while (*str) { - /* Handle escape sequences by skipping both \ and next char */ - if (*str == '\\') { - *fmt = prev; fmt++; str++; - if (*str == '\0') break; - *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); - continue; - } - /* Handle continuations */ - else if (cont) { - /* C-Style comments */ - if (cont & FMT_CONT_COMMENT_C) { - if (*str == '*' && *(str + 1) == '/') { - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - cont = FMT_CONT_NOP; - } - else { - *fmt = FMT_TYPE_COMMENT; - } - /* Handle other comments */ - } - else { - find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; - if (*str == find) cont = 0; - *fmt = FMT_TYPE_STRING; - } - - str += BLI_str_utf8_size_safe(str) - 1; - } - /* Not in a string... */ - else { - /* Deal with comments first */ - if (*str == '/' && *(str + 1) == '/') { - /* fill the remaining line */ - text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); - } - /* C-Style (multi-line) comments */ - else if (*str == '/' && *(str + 1) == '*') { - cont = FMT_CONT_COMMENT_C; - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - } - else if (*str == '"' || *str == '\'') { - /* Strings */ - find = *str; - cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; - *fmt = FMT_TYPE_STRING; - } - /* Whitespace (all ws. has been converted to spaces) */ - else if (*str == ' ') { - *fmt = FMT_TYPE_WHITESPACE; - } - /* Numbers (digits not part of an identifier and periods followed by digits) */ - else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || - (*str == '.' && text_check_digit(*(str + 1)))) - { - *fmt = FMT_TYPE_NUMERAL; - } - /* Punctuation */ - else if ((*str != '#') && text_check_delim(*str)) { - *fmt = FMT_TYPE_SYMBOL; - } - /* Identifiers and other text (no previous ws. or delims. so text continues) */ - else if (prev == FMT_TYPE_DEFAULT) { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ - else { - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Special vars(v) or built-in keywords(b) */ - /* keep in sync with 'txtfmt_osl_format_identifier()' */ - if ((i = txtfmt_osl_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; - else if ((i = txtfmt_osl_find_builtinfunc(str)) != -1) prev = FMT_TYPE_KEYWORD; - else if ((i = txtfmt_osl_find_reserved(str)) != -1) prev = FMT_TYPE_RESERVED; - else if ((i = txtfmt_osl_find_preprocessor(str)) != -1) prev = FMT_TYPE_DIRECTIVE; - - /* clang-format on */ - - if (i > 0) { - if (prev == FMT_TYPE_DIRECTIVE) { /* can contain utf8 */ - text_format_fill(&str, &fmt, prev, i); - } - else { - text_format_fill_ascii(&str, &fmt, prev, i); - } - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - } - prev = *fmt; fmt++; str++; - } - - /* Terminate and add continuation char */ - *fmt = '\0'; fmt++; - *fmt = cont; - - /* If continuation has changed and we're allowed, process the next line */ - if (cont != cont_orig && do_next && line->next) { - txtfmt_osl_format_line(st, line->next, do_next); - } - - flatten_string_free(&fs); + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; + fmt++; + str++; + if (*str == '\0') + break; + *fmt = prev; + fmt++; + str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* C-Style comments */ + if (cont & FMT_CONT_COMMENT_C) { + if (*str == '*' && *(str + 1) == '/') { + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + cont = FMT_CONT_NOP; + } + else { + *fmt = FMT_TYPE_COMMENT; + } + /* Handle other comments */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) + cont = 0; + *fmt = FMT_TYPE_STRING; + } + + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* Deal with comments first */ + if (*str == '/' && *(str + 1) == '/') { + /* fill the remaining line */ + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + /* C-Style (multi-line) comments */ + else if (*str == '/' && *(str + 1) == '*') { + cont = FMT_CONT_COMMENT_C; + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) { + *fmt = FMT_TYPE_NUMERAL; + } + /* Punctuation */ + else if ((*str != '#') && text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_osl_format_identifier()' */ + if ((i = txtfmt_osl_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; + else if ((i = txtfmt_osl_find_builtinfunc(str)) != -1) prev = FMT_TYPE_KEYWORD; + else if ((i = txtfmt_osl_find_reserved(str)) != -1) prev = FMT_TYPE_RESERVED; + else if ((i = txtfmt_osl_find_preprocessor(str)) != -1) prev = FMT_TYPE_DIRECTIVE; + + /* clang-format on */ + + if (i > 0) { + if (prev == FMT_TYPE_DIRECTIVE) { /* can contain utf8 */ + text_format_fill(&str, &fmt, prev, i); + } + else { + text_format_fill_ascii(&str, &fmt, prev, i); + } + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; + fmt++; + str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; + fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_osl_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); } void ED_text_format_register_osl(void) { - static TextFormatType tft = {NULL}; - static const char *ext[] = {"osl", NULL}; + static TextFormatType tft = {NULL}; + static const char *ext[] = {"osl", NULL}; - tft.format_identifier = txtfmt_osl_format_identifier; - tft.format_line = txtfmt_osl_format_line; - tft.ext = ext; + tft.format_identifier = txtfmt_osl_format_identifier; + tft.format_line = txtfmt_osl_format_line; + tft.ext = ext; - ED_text_format_register(&tft); + ED_text_format_register(&tft); } diff --git a/source/blender/editors/space_text/text_format_pov.c b/source/blender/editors/space_text/text_format_pov.c index c0f4507aa5d..8064f4e69e2 100644 --- a/source/blender/editors/space_text/text_format_pov.c +++ b/source/blender/editors/space_text/text_format_pov.c @@ -43,443 +43,442 @@ */ static int txtfmt_pov_find_keyword(const char *string) { - /* Keep aligned args for readability. */ - /* clang-format off */ - - int i, len; - /* Language Directives */ - if (STR_LITERAL_STARTSWITH(string, "deprecated", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "persistent", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "statistics", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "version", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "warning", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "declare", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "include", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "append", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "debug", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fclose", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fopen", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ifndef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ifdef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "patch", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "macro", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "range", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "read", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "render", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "undef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "write", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + /* Keep aligned args for readability. */ + /* clang-format off */ + + int i, len; + /* Language Directives */ + if (STR_LITERAL_STARTSWITH(string, "deprecated", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "persistent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "statistics", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "version", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "warning", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "declare", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "include", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "append", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "debug", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fclose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fopen", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifndef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifdef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "patch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "macro", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "range", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "read", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "render", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "undef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "write", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } static int txtfmt_pov_find_reserved_keywords(const char *string) { - int i, len; - /* POV-Ray Built-in Variables - * list is from... - * http://www.povray.org/documentation/view/3.7.0/212/ - */ - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Float Functions */ - if (STR_LITERAL_STARTSWITH(string, "conserve_energy", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_intersections", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "dimension_size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bitwise_and", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bitwise_or", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bitwise_xor", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "file_exists", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "precompute", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "dimensions", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clipped_by", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "shadowless", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "turb_depth", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "reciprocal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quaternion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "phong_size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tesselate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "save_file", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "load_file", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_trace", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "transform", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "translate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "direction", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "roughness", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "metallic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gts_load", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gts_save", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "location", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "altitude", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "function", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "evaluate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inverse", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "collect", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "target", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "albedo", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rotate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "matrix", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "look_at", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "jitter", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "angle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "right", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "scale", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "child", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "crand", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "blink", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "defined", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "degrees", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inside", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "radians", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vlength", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "select", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "floor", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "strcmp", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "strlen", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tessel", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sturm", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "abs", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "acosh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "prod", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "with", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "acos", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "asc", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "asinh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "asin", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "atan2", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "atand", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "atanh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "atan", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ceil", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "warp", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cosh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "log", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "min", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mod", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pow", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rand", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "seed", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "form", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sinh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sqrt", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tanh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vdot", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sin", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sqr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sum", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pwr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tan", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "val", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cos", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "div", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "exp", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "int", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sky", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "up", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ln", len)) i = len; - /* Color Identifiers */ - else if (STR_LITERAL_STARTSWITH(string, "transmit", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "filter", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "srgbft", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "srgbf", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "srgbt", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rgbft", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gamma", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "green", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "blue", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gray", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "srgb", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sRGB", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SRGB", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rgbf", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rgbt", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rgb", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "red", len)) i = len; - /* Color Spaces */ - else if (STR_LITERAL_STARTSWITH(string, "pov", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hsl", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hsv", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "xyl", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "xyv", len)) i = len; - /* Vector Functions */ - else if (STR_LITERAL_STARTSWITH(string, "vaxis_rotate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vturbulence", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "min_extent", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vnormalize", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_extent", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vrotate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vcross", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "trace", len)) i = len; - /* String Functions */ - else if (STR_LITERAL_STARTSWITH(string, "file_time", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "datetime", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "concat", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "strlwr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "strupr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "substr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vstr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "chr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "str", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + /* POV-Ray Built-in Variables + * list is from... + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Float Functions */ + if (STR_LITERAL_STARTSWITH(string, "conserve_energy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_intersections", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dimension_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bitwise_and", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bitwise_or", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bitwise_xor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "file_exists", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "precompute", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dimensions", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clipped_by", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "shadowless", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "turb_depth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "reciprocal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quaternion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "phong_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tesselate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "save_file", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "load_file", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_trace", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "transform", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "translate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "direction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "roughness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "metallic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gts_load", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gts_save", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "location", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "altitude", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "function", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "evaluate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inverse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "collect", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "target", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "albedo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rotate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "matrix", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "look_at", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "jitter", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "right", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "scale", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "child", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "crand", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blink", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "defined", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "degrees", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inside", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radians", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vlength", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "select", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "floor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strcmp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strlen", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tessel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sturm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "abs", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "acosh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "prod", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "with", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "acos", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "asc", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "asinh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "asin", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atan2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atand", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atanh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atan", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ceil", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "warp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cosh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "log", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "min", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mod", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rand", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "seed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "form", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sinh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sqrt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tanh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vdot", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sin", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sqr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sum", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pwr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tan", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "val", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cos", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "div", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "int", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sky", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "up", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ln", len)) i = len; + /* Color Identifiers */ + else if (STR_LITERAL_STARTSWITH(string, "transmit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "filter", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgbft", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgbf", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgbt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgbft", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "green", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blue", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gray", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgb", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sRGB", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SRGB", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgbf", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgbt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgb", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "red", len)) i = len; + /* Color Spaces */ + else if (STR_LITERAL_STARTSWITH(string, "pov", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hsl", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hsv", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "xyl", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "xyv", len)) i = len; + /* Vector Functions */ + else if (STR_LITERAL_STARTSWITH(string, "vaxis_rotate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vturbulence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "min_extent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vnormalize", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_extent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vrotate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vcross", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "trace", len)) i = len; + /* String Functions */ + else if (STR_LITERAL_STARTSWITH(string, "file_time", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "datetime", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "concat", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strlwr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strupr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "substr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vstr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "chr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "str", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } - static int txtfmt_pov_find_reserved_builtins(const char *string) { - int i, len; - - /* POV-Ray Built-in Variables - * list is from... - * http://www.povray.org/documentation/view/3.7.0/212/ - */ - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Language Keywords */ - if (STR_LITERAL_STARTSWITH(string, "reflection_exponent", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "area_illumination", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "all_intersections", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cutaway_textures", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "smooth_triangle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lommel_seeliger", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "falloff_angle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "aa_threshold", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hypercomplex", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "major_radius", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_distance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_iteration", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "colour_space", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "color_space", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "iridescence", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "subsurface", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "scattering", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "absorption", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "water_level", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "reflection", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_extent", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "oren_nayar", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "refraction", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hierarchy", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "radiosity", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tolerance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "interior", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "toroidal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "emission", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "material", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "internal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "photons", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "arc_angle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "minnaert", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "texture", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "array", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "black_hole", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "component", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "composite", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "coords", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cube", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "dist_exp", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "exterior", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "file_gamma", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "flatness", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "planet", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "screw", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "keep", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "flip", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "move", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "roll", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "look_at", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "metric", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "offset", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "orientation", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pattern", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "precision", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "repeat", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bend", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "alpha", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "slice", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "smooth", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "solid", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "all", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "now", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pot", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "type", len)) i = len; - /* Animation Options */ - else if (STR_LITERAL_STARTSWITH(string, "global_settings", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "input_file_name", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "initial_clock", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "initial_frame", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "frame_number", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "image_height", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "image_width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "final_clock", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "final_frame", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clock_delta", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clock_on", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clock", len)) i = len; - /* Spline Identifiers */ - else if (STR_LITERAL_STARTSWITH(string, "extended_x_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "general_x_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quadratic_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "basic_x_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "natural_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "linear_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bezier_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "akima_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cubic_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sor_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tcb_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "linear_sweep", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "conic_sweep", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "b_spline", len)) i = len; - /* Patterns */ - else if (STR_LITERAL_STARTSWITH(string, "pigment_pattern", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "image_pattern", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "density_file", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cylindrical", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "proportion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "triangular", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "image_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "proximity", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spherical", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bump_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "wrinkles", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "average", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "voronoi", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "masonry", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "binary", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "boxed", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bozo", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "brick", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bumps", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cells", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "checker", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "crackle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "dents", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "facets", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gradient", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "granite", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hexagon", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "julia", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "leopard", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "magnet", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mandel", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "marble", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "onion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pavement", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "planar", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quilted", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "radial", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ripples", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "slope", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spiral1", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spiral2", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spotted", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "square", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tile2", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tiling", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tiles", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "waves", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "wood", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "agate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "aoi", len)) i = len; - /* Objects */ - else if (STR_LITERAL_STARTSWITH(string, "superellipsoid", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bicubic_patch", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "julia_fractal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "height_field", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cubic_spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sphere_sweep", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "light_group", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "light_source", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "intersection", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "isosurface", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "background", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sky_sphere", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cylinder", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "difference", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "brilliance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "parametric", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "interunion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "intermerge", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "polynomial", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "displace", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "specular", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ambient", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "diffuse", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "polygon", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quadric", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quartic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "rainbow", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sphere", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spline", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "prism", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "camera", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "galley", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "phong", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cone", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "blob", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "box", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "disc", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fog", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lathe", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "merge", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mesh2", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mesh", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "object", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ovus", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lemon", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "plane", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "poly", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "irid", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sor", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "text", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "torus", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "triangle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "union", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "colour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "color", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "media", len)) i = len; - /* Built-in Vectors */ - else if (STR_LITERAL_STARTSWITH(string, "t", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "u", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "v", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "x", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "y", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "z", len)) i = len; - else i = 0; - - /* clang-format off */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + + /* POV-Ray Built-in Variables + * list is from... + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Language Keywords */ + if (STR_LITERAL_STARTSWITH(string, "reflection_exponent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "area_illumination", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "all_intersections", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cutaway_textures", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "smooth_triangle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lommel_seeliger", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "falloff_angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aa_threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hypercomplex", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "major_radius", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_distance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_iteration", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "colour_space", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color_space", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "iridescence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "subsurface", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "scattering", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "absorption", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "water_level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "reflection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_extent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "oren_nayar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "refraction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hierarchy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radiosity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tolerance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interior", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "toroidal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "emission", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "material", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "internal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "photons", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "arc_angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "minnaert", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "texture", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "array", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "black_hole", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "component", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "composite", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "coords", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cube", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dist_exp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exterior", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "file_gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "flatness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "planet", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "screw", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "keep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "flip", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "move", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "roll", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "look_at", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "metric", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "offset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "orientation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pattern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "precision", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "repeat", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bend", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "alpha", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "slice", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "smooth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "solid", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "all", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "now", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pot", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "type", len)) i = len; + /* Animation Options */ + else if (STR_LITERAL_STARTSWITH(string, "global_settings", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "input_file_name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "frame_number", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_height", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_delta", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock", len)) i = len; + /* Spline Identifiers */ + else if (STR_LITERAL_STARTSWITH(string, "extended_x_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "general_x_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quadratic_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "basic_x_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "natural_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "linear_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bezier_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "akima_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sor_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tcb_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "linear_sweep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "conic_sweep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "b_spline", len)) i = len; + /* Patterns */ + else if (STR_LITERAL_STARTSWITH(string, "pigment_pattern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_pattern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "density_file", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cylindrical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "proportion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "triangular", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "proximity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spherical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bump_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "wrinkles", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "average", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "voronoi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "masonry", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "binary", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "boxed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bozo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brick", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bumps", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cells", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "checker", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "crackle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dents", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "facets", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gradient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "granite", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hexagon", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "julia", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "leopard", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "magnet", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mandel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "marble", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "onion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pavement", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "planar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quilted", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radial", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ripples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "slope", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spiral1", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spiral2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spotted", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "square", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tile2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tiling", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tiles", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "waves", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "wood", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "agate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aoi", len)) i = len; + /* Objects */ + else if (STR_LITERAL_STARTSWITH(string, "superellipsoid", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bicubic_patch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "julia_fractal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "height_field", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sphere_sweep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "light_group", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "light_source", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "intersection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "isosurface", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "background", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sky_sphere", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cylinder", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "difference", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brilliance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "parametric", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interunion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "intermerge", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "polynomial", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "displace", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "specular", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ambient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "diffuse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "polygon", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quadric", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quartic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rainbow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sphere", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "prism", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "galley", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "phong", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cone", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blob", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "box", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "disc", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lathe", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "merge", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mesh2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mesh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "object", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ovus", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lemon", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "plane", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "poly", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "irid", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "text", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "torus", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "triangle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "union", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "media", len)) i = len; + /* Built-in Vectors */ + else if (STR_LITERAL_STARTSWITH(string, "t", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "u", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "v", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "x", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "y", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "z", len)) i = len; + else i = 0; + + /* clang-format off */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } @@ -496,438 +495,450 @@ static int txtfmt_pov_find_reserved_builtins(const char *string) */ static int txtfmt_pov_find_specialvar(const char *string) { - int i, len; - /* Modifiers */ - if (STR_LITERAL_STARTSWITH(string, "dispersion_samples", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "projected_through", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "double_illuminate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "expand_thresholds", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "media_interaction", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "media_attenuation", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "low_error_factor", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "recursion_limit", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "interior_texture", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_trace_level", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gray_threshold", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pretrace_start", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "normal_indices", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "normal_vectors", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "vertex_vectors", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "noise_generator", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "irid_wavelength", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "number_of_waves", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ambient_light", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inside_vector", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "face_indices", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "texture_list", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_gradient", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uv_indices", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uv_vectors", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fade_distance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "global_lights", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no_bump_scale", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pretrace_end", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no_radiosity", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no_reflection", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "assumed_gamma", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "scallop_wave", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "triangle_wave", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "nearest_count", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "maximum_reuse", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "minimum_reuse", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "always_sample", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "translucency", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "eccentricity", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "contained_by", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inside_point", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "adc_bailout", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "density_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "split_union", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mm_per_unit", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "agate_turb", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bounded_by", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "brick_size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hf_gray_16", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "dispersion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "extinction", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "thickness", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "color_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "colour_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cubic_wave", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fade_colour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fade_power", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fade_color", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "normal_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pigment_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quick_color", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "quick_colour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "material_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pass_through", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "interpolate", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "texture_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "error_bound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "brightness", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "use_color", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "use_alpha", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "use_colour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "use_index", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uv_mapping", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "importance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "max_sample", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "intervals", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sine_wave", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "slope_map", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "poly_wave", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no_shadow", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ramp_wave", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "precision", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "original", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "accuracy", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "map_type", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no_image", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "distance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "autostop", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "caustics", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "octaves", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "aa_level", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "frequency", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fog_offset", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "modulation", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "outbound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no_cache", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pigment", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "charset", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inbound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "outside", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "inner", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "turbulence", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "threshold", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "accuracy", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "polarity", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bump_size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "circular", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "control0", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "control1", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "maximal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "minimal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fog_type", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fog_alt", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "samples", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "origin", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "amount", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "adaptive", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "exponent", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "strength", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "density", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fresnel", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "albinos", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "finish", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "method", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "omega", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fixed", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spacing", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "u_steps", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "v_steps", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "offset", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hollow", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gather", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lambda", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mortar", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "count", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "once", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "orient", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "normal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "phase", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ratio", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "open", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ior", len)) i = len; - /* Light Types and options*/ - else if (STR_LITERAL_STARTSWITH(string, "area_light", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "looks_like", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fade_power", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tightness", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "spotlight", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "parallel", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "point_at", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "falloff", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "radius", len)) i = len; - /* Camera Types and options*/ - else if (STR_LITERAL_STARTSWITH(string, "omni_directional_stereo", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lambert_cylindrical", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "miller_cylindrical", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lambert_azimuthal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ultra_wide_angle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "camera_direction", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "camera_location ", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "van_der_grinten", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "aitoff_hammer", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "smyth_craster", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "orthographic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "camera_right", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "blur_samples", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "plate_carree", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "camera_type", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "perspective", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mesh_camera", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "focal_point", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "balthasart", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "confidence", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "parallaxe", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hobo_dyer", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "camera_up", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "panoramic", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "eckert_vi", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "eckert_iv", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mollweide", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "aperture", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "behrmann", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "variance", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "stereo", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "icosa", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tetra", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "octa", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "mercator", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "omnimax", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fisheye", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "edwards", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "peters", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gall", len)) i = len; - else i = 0; - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + /* Modifiers */ + if (STR_LITERAL_STARTSWITH(string, "dispersion_samples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "projected_through", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "double_illuminate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "expand_thresholds", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "media_interaction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "media_attenuation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "low_error_factor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "recursion_limit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interior_texture", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_trace_level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gray_threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pretrace_start", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal_indices", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal_vectors", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vertex_vectors", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "noise_generator", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "irid_wavelength", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "number_of_waves", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ambient_light", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inside_vector", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "face_indices", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "texture_list", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_gradient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uv_indices", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uv_vectors", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_distance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "global_lights", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_bump_scale", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pretrace_end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_radiosity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_reflection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "assumed_gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "scallop_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "triangle_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "nearest_count", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "maximum_reuse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "minimum_reuse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "always_sample", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "translucency", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "eccentricity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "contained_by", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inside_point", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "adc_bailout", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "density_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "split_union", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mm_per_unit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "agate_turb", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bounded_by", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brick_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hf_gray_16", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dispersion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "extinction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "thickness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "colour_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_power", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pigment_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quick_color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quick_colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "material_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pass_through", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interpolate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "texture_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error_bound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brightness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_alpha", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_index", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uv_mapping", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "importance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_sample", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "intervals", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sine_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "slope_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "poly_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_shadow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ramp_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "precision", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "original", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "accuracy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "map_type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_image", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "distance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "autostop", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "caustics", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "octaves", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aa_level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "frequency", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog_offset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "modulation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "outbound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_cache", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pigment", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "charset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inbound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "outside", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inner", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "turbulence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "accuracy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "polarity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bump_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "circular", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "control0", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "control1", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "maximal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "minimal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog_type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog_alt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "samples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "origin", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "amount", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "adaptive", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exponent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strength", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "density", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fresnel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "albinos", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "finish", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "omega", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fixed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spacing", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "u_steps", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "v_steps", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "offset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hollow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gather", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambda", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mortar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "count", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "once", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "orient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "phase", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ratio", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "open", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ior", len)) i = len; + /* Light Types and options*/ + else if (STR_LITERAL_STARTSWITH(string, "area_light", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "looks_like", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_power", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tightness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spotlight", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "parallel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "point_at", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "falloff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radius", len)) i = len; + /* Camera Types and options*/ + else if (STR_LITERAL_STARTSWITH(string, "omni_directional_stereo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambert_cylindrical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "miller_cylindrical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambert_azimuthal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ultra_wide_angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_direction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_location ", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "van_der_grinten", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aitoff_hammer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "smyth_craster", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "orthographic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_right", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blur_samples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "plate_carree", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "perspective", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mesh_camera", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "focal_point", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "balthasart", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "confidence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "parallaxe", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hobo_dyer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_up", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "panoramic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "eckert_vi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "eckert_iv", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mollweide", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aperture", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "behrmann", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "variance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "stereo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "icosa", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tetra", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "octa", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mercator", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "omnimax", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fisheye", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "edwards", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "peters", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gall", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } static int txtfmt_pov_find_bool(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Built-in Constants */ - if (STR_LITERAL_STARTSWITH(string, "unofficial", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "off", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "yes", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "on", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pi", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tau", len)) i = len; - /* Encodings */ - else if (STR_LITERAL_STARTSWITH(string, "sint16be", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint16le", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint32be", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint32le", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uint16be", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uint16le", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bt2020", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "bt709", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint8", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uint8", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ascii", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "utf8", len)) i = len; - /* Filetypes */ - else if (STR_LITERAL_STARTSWITH(string, "tiff", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "df3", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "exr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gif", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hdr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "iff", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "jpeg", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pgm", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "png", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ppm", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sys", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tga", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ttf", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Built-in Constants */ + if (STR_LITERAL_STARTSWITH(string, "unofficial", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "off", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "yes", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tau", len)) i = len; + /* Encodings */ + else if (STR_LITERAL_STARTSWITH(string, "sint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bt2020", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bt709", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ascii", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "utf8", len)) i = len; + /* Filetypes */ + else if (STR_LITERAL_STARTSWITH(string, "tiff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "df3", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hdr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "iff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "jpeg", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pgm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "png", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ppm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sys", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tga", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ttf", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } static char txtfmt_pov_format_identifier(const char *str) { - char fmt; + char fmt; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - if ((txtfmt_pov_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; - else if ((txtfmt_pov_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; - else if ((txtfmt_pov_find_reserved_keywords(str)) != -1) fmt = FMT_TYPE_RESERVED; - else if ((txtfmt_pov_find_reserved_builtins(str)) != -1) fmt = FMT_TYPE_DIRECTIVE; - else fmt = FMT_TYPE_DEFAULT; + if ((txtfmt_pov_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; + else if ((txtfmt_pov_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; + else if ((txtfmt_pov_find_reserved_keywords(str)) != -1) fmt = FMT_TYPE_RESERVED; + else if ((txtfmt_pov_find_reserved_builtins(str)) != -1) fmt = FMT_TYPE_DIRECTIVE; + else fmt = FMT_TYPE_DEFAULT; - /* clang-format on */ + /* clang-format on */ - return fmt; + return fmt; } static void txtfmt_pov_format_line(SpaceText *st, TextLine *line, const bool do_next) { - FlattenString fs; - const char *str; - char *fmt; - char cont_orig, cont, find, prev = ' '; - int len, i; - - /* Get continuation from previous line */ - if (line->prev && line->prev->format != NULL) { - fmt = line->prev->format; - cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont) == cont); - } - else { - cont = FMT_CONT_NOP; - } - - /* Get original continuation from this line */ - if (line->format != NULL) { - fmt = line->format; - cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); - } - else { - cont_orig = 0xFF; - } - - len = flatten_string(st, &fs, line->line); - str = fs.buf; - if (!text_check_format_len(line, len)) { - flatten_string_free(&fs); - return; - } - fmt = line->format; - - while (*str) { - /* Handle escape sequences by skipping both \ and next char */ - if (*str == '\\') { - *fmt = prev; fmt++; str++; - if (*str == '\0') break; - *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); - continue; - } - /* Handle continuations */ - else if (cont) { - /* C-Style comments */ - if (cont & FMT_CONT_COMMENT_C) { - if (*str == '*' && *(str + 1) == '/') { - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - cont = FMT_CONT_NOP; - } - else { - *fmt = FMT_TYPE_COMMENT; - } - /* Handle other comments */ - } - else { - find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; - if (*str == find) cont = 0; - *fmt = FMT_TYPE_STRING; - } - - str += BLI_str_utf8_size_safe(str) - 1; - } - /* Not in a string... */ - else { - /* C-Style (multi-line) comments */ - if (*str == '/' && *(str + 1) == '*') { - cont = FMT_CONT_COMMENT_C; - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - } - /* Single line comment */ - else if (*str == '/' && *(str + 1) == '/') { - text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); - } - else if (*str == '"' || *str == '\'') { - /* Strings */ - find = *str; - cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; - *fmt = FMT_TYPE_STRING; - } - /* Whitespace (all ws. has been converted to spaces) */ - else if (*str == ' ') { - *fmt = FMT_TYPE_WHITESPACE; - } - /* Numbers (digits not part of an identifier and periods followed by digits) */ - else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || - (*str == '.' && text_check_digit(*(str + 1)))) - { - *fmt = FMT_TYPE_NUMERAL; - } - /* Booleans */ - else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_pov_find_bool(str)) != -1) { - if (i > 0) { - text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - /* Punctuation */ - else if (text_check_delim(*str)) { - *fmt = FMT_TYPE_SYMBOL; - } - /* Identifiers and other text (no previous ws. or delims. so text continues) */ - else if (prev == FMT_TYPE_DEFAULT) { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ - else { - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Special vars(v) or built-in keywords(b) */ - /* keep in sync with 'txtfmt_pov_format_identifier()' */ - if ((i = txtfmt_pov_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; - else if ((i = txtfmt_pov_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; - else if ((i = txtfmt_pov_find_reserved_keywords(str)) != -1) prev = FMT_TYPE_RESERVED; - else if ((i = txtfmt_pov_find_reserved_builtins(str)) != -1) prev = FMT_TYPE_DIRECTIVE; - - /* clang-format on */ - - if (i > 0) { - text_format_fill_ascii(&str, &fmt, prev, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - } - prev = *fmt; fmt++; str++; - } - - /* Terminate and add continuation char */ - *fmt = '\0'; fmt++; - *fmt = cont; - - /* If continuation has changed and we're allowed, process the next line */ - if (cont != cont_orig && do_next && line->next) { - txtfmt_pov_format_line(st, line->next, do_next); - } - - flatten_string_free(&fs); + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; + fmt++; + str++; + if (*str == '\0') + break; + *fmt = prev; + fmt++; + str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* C-Style comments */ + if (cont & FMT_CONT_COMMENT_C) { + if (*str == '*' && *(str + 1) == '/') { + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + cont = FMT_CONT_NOP; + } + else { + *fmt = FMT_TYPE_COMMENT; + } + /* Handle other comments */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) + cont = 0; + *fmt = FMT_TYPE_STRING; + } + + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* C-Style (multi-line) comments */ + if (*str == '/' && *(str + 1) == '*') { + cont = FMT_CONT_COMMENT_C; + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + } + /* Single line comment */ + else if (*str == '/' && *(str + 1) == '/') { + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) { + *fmt = FMT_TYPE_NUMERAL; + } + /* Booleans */ + else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_pov_find_bool(str)) != -1) { + if (i > 0) { + text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + /* Punctuation */ + else if (text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_pov_format_identifier()' */ + if ((i = txtfmt_pov_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; + else if ((i = txtfmt_pov_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; + else if ((i = txtfmt_pov_find_reserved_keywords(str)) != -1) prev = FMT_TYPE_RESERVED; + else if ((i = txtfmt_pov_find_reserved_builtins(str)) != -1) prev = FMT_TYPE_DIRECTIVE; + + /* clang-format on */ + + if (i > 0) { + text_format_fill_ascii(&str, &fmt, prev, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; + fmt++; + str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; + fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_pov_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); } void ED_text_format_register_pov(void) { - static TextFormatType tft = {NULL}; - static const char *ext[] = {"pov", "inc", "mcr", "mac", NULL}; + static TextFormatType tft = {NULL}; + static const char *ext[] = {"pov", "inc", "mcr", "mac", NULL}; - tft.format_identifier = txtfmt_pov_format_identifier; - tft.format_line = txtfmt_pov_format_line; - tft.ext = ext; + tft.format_identifier = txtfmt_pov_format_identifier; + tft.format_line = txtfmt_pov_format_line; + tft.ext = ext; - ED_text_format_register(&tft); + ED_text_format_register(&tft); } diff --git a/source/blender/editors/space_text/text_format_pov_ini.c b/source/blender/editors/space_text/text_format_pov_ini.c index 5ef5746bffb..0dbf2b1bdd3 100644 --- a/source/blender/editors/space_text/text_format_pov_ini.c +++ b/source/blender/editors/space_text/text_format_pov_ini.c @@ -43,468 +43,478 @@ */ static int txtfmt_ini_find_keyword(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Language Directives */ - if (STR_LITERAL_STARTSWITH(string, "deprecated", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "statistics", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "declare", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "version", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "warning", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "include", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fclose", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ifndef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "append", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "debug", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "fopen", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ifdef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "macro", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "range", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "render", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "undef", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "write", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "read", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; - - else if (STR_LITERAL_STARTSWITH(string, "I", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "S", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "A", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Q", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "U", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "F", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "C", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "N", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "P", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "T", len)) i = len; - - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Language Directives */ + if (STR_LITERAL_STARTSWITH(string, "deprecated", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "statistics", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "declare", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "version", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "warning", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "include", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fclose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifndef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "append", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "debug", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fopen", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifdef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "macro", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "range", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "render", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "undef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "write", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "read", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + + else if (STR_LITERAL_STARTSWITH(string, "I", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "S", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "A", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Q", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "U", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "F", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "C", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "N", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "P", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "T", len)) i = len; + + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } static int txtfmt_ini_find_reserved(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* POV-Ray Built-in INI Variables - * list is from... - * http://www.povray.org/documentation/view/3.7.0/212/ - */ - if (STR_LITERAL_STARTSWITH(string, "RenderCompleteSoundEnabled", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Create_Continue_Trace_Log", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ParseErrorSoundEnabled", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "RenderErrorSoundEnabled", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "HideWhenMainMinimized", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Antialias_Confidence", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "RenderCompleteSound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ParseErrorSound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "RenderErrorSound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "UseExtensions", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ReadWriteSourceDir", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NormalPositionLeft", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NormalPositionTop", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NormalPositionRight", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NormalPositionBottom", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Pre_Scene_Command", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Pre_Frame_Command", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Post_Scene_Command", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Post_Frame_Command", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "User_Abort_Command", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Fatal_Error_Command", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NormalPositionX", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NormalPositionY", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Pre_Scene_Return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Pre_Frame_Return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Post_Scene_Return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Post_Frame_Return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "User_Abort_Return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Fatal_Error_Return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Antialias_Threshold", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Antialias_Gamma", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Antialias_Depth", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "input_file_name", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Subset_Start_Frame", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Subset_End_Frame", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "UseToolbar", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "UseTooltips", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Frame_Step", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Cyclic_Animation", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Field_Render", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Odd_Field", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "final_clock", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "final_frame", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "frame_number", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "initial_clock", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "initial_frame", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "image_height", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "image_width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Start_Column", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Start_Row", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "End_Column", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "End_Row", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Test_Abort_Count", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Test_Abort", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Continue_Trace", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Bounding_Method", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Create_Ini", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Display_Gamma", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Display", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Version", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Pause_When_Done", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Verbose", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Preview_Start_Size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Preview_End_Size", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Output_to_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Input_File_Name", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Output_File_Name", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Output_File_Type", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Output_Alpha", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Bits_Per_Color", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Compression", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Dither_Method", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Include_Header", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Library_Path", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Debug_Console", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Fatal_Console", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Render_Console", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Statistic_Console", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Warning_Console", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Warning_Level", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "All_Console", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Debug_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Fatal_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Render_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Statistic_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Warning_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "All_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Quality", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Bounding_Threshold", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Bounding", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Light_Buffer", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Vista_Buffer", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Remove_Bounds", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Split_Unions", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Antialias", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Glare_Desaturation", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Sampling_Method", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Stochastic_Seed", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Jitter_Amount", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Jitter", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Antialias_Depth", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "CheckNewVersion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "RunCount", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "CommandLine", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "TextColour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "WarningColour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ErrorColour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "BackgroundColour", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "DropToEditor", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastRenderName", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastRenderPath", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastQueuePath", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SecondaryINISection", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "BetaVersionNo64", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastBitmapName", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastBitmapPath", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastINIPath", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SecondaryINIFile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "BackgroundFile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SaveSettingsOnExit", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "TileBackground", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "HideNewUserHelp", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SendSystemInfo", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ItsAboutTime", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "LastPath", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Band0Width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Band1Width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Band2Width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Band3Width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Band4Width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ShowCmd", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Transparency", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Use8BitMode", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "MakeActive", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "KeepAboveMain", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "AutoClose", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "PreserveBitmap", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "FontSize", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "FontWeight", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "KeepMessages", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "AlertSound", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Completion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Priority", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "DutyCycle", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "AlertOnCompletion", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "AutoRender", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "PreventSleep", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NoShelloutWait", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SystemNoActive", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "NoShellOuts", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "VideoSource", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SceneFile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "OutputFile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "IniOutputFile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "CurrentDirectory", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "SourceFile", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Rendering", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "RenderwinClose", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Append_File", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Warning Level", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clock_delta", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clock_on", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "clock", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Height", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Width", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Dither", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Flags", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "Font", len)) i = len; - /* Filetypes */ - else if (STR_LITERAL_STARTSWITH(string, "df3", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "exr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "gif", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "hdr", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "iff", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "jpeg", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pgm", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "png", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "ppm", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sys", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tga", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tiff", len)) i = len; - /* Encodings */ - else if (STR_LITERAL_STARTSWITH(string, "ascii", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "utf8", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uint8", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uint16be", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "uint16le", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint8", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint16be", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint16le", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint32be", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "sint32le", len)) i = len; - - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* POV-Ray Built-in INI Variables + * list is from... + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + if (STR_LITERAL_STARTSWITH(string, "RenderCompleteSoundEnabled", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Create_Continue_Trace_Log", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ParseErrorSoundEnabled", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderErrorSoundEnabled", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "HideWhenMainMinimized", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Confidence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderCompleteSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ParseErrorSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderErrorSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "UseExtensions", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ReadWriteSourceDir", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionLeft", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionTop", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionRight", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionBottom", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Scene_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Frame_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Scene_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Frame_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "User_Abort_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_Error_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionX", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionY", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Scene_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Frame_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Scene_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Frame_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "User_Abort_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_Error_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Depth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "input_file_name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Subset_Start_Frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Subset_End_Frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "UseToolbar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "UseTooltips", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Frame_Step", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Cyclic_Animation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Field_Render", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Odd_Field", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "frame_number", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_height", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Start_Column", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Start_Row", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "End_Column", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "End_Row", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Test_Abort_Count", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Test_Abort", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Continue_Trace", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bounding_Method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Create_Ini", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Display_Gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Display", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Version", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pause_When_Done", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Verbose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Preview_Start_Size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Preview_End_Size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_to_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Input_File_Name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_File_Name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_File_Type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_Alpha", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bits_Per_Color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Compression", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Dither_Method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Include_Header", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Library_Path", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Debug_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Render_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Statistic_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning_Level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "All_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Debug_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Render_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Statistic_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "All_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Quality", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bounding_Threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bounding", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Light_Buffer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Vista_Buffer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Remove_Bounds", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Split_Unions", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Glare_Desaturation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Sampling_Method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Stochastic_Seed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Jitter_Amount", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Jitter", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Depth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "CheckNewVersion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RunCount", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "CommandLine", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "TextColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "WarningColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ErrorColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "BackgroundColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "DropToEditor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastRenderName", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastRenderPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastQueuePath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SecondaryINISection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "BetaVersionNo64", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastBitmapName", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastBitmapPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastINIPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SecondaryINIFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "BackgroundFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SaveSettingsOnExit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "TileBackground", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "HideNewUserHelp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SendSystemInfo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ItsAboutTime", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band0Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band1Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band2Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band3Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band4Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ShowCmd", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Transparency", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Use8BitMode", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "MakeActive", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "KeepAboveMain", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AutoClose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "PreserveBitmap", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "FontSize", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "FontWeight", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "KeepMessages", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AlertSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Completion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Priority", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "DutyCycle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AlertOnCompletion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AutoRender", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "PreventSleep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NoShelloutWait", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SystemNoActive", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NoShellOuts", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "VideoSource", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SceneFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "OutputFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "IniOutputFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "CurrentDirectory", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SourceFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Rendering", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderwinClose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Append_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning Level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_delta", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Height", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Dither", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Flags", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Font", len)) i = len; + /* Filetypes */ + else if (STR_LITERAL_STARTSWITH(string, "df3", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hdr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "iff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "jpeg", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pgm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "png", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ppm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sys", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tga", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tiff", len)) i = len; + /* Encodings */ + else if (STR_LITERAL_STARTSWITH(string, "ascii", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "utf8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32le", len)) i = len; + + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } - - - static int txtfmt_ini_find_bool(const char *string) { - int i, len; - - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Built-in Constants */ - if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "no", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "off", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "yes", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "on", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pi", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "tau", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "%o", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "%s", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "%n", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "%k", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "%h", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "%w", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ - return (i == 0 || text_check_identifier(string[i])) ? -1 : i; + int i, len; + + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Built-in Constants */ + if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "off", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "yes", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tau", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%o", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%s", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%n", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%k", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%h", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%w", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; } static char txtfmt_pov_ini_format_identifier(const char *str) { - char fmt; - if ((txtfmt_ini_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; - else if ((txtfmt_ini_find_reserved(str)) != -1) fmt = FMT_TYPE_RESERVED; - else fmt = FMT_TYPE_DEFAULT; - return fmt; + char fmt; + if ((txtfmt_ini_find_keyword(str)) != -1) + fmt = FMT_TYPE_KEYWORD; + else if ((txtfmt_ini_find_reserved(str)) != -1) + fmt = FMT_TYPE_RESERVED; + else + fmt = FMT_TYPE_DEFAULT; + return fmt; } static void txtfmt_pov_ini_format_line(SpaceText *st, TextLine *line, const bool do_next) { - FlattenString fs; - const char *str; - char *fmt; - char cont_orig, cont, find, prev = ' '; - int len, i; - - /* Get continuation from previous line */ - if (line->prev && line->prev->format != NULL) { - fmt = line->prev->format; - cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont) == cont); - } - else { - cont = FMT_CONT_NOP; - } - - /* Get original continuation from this line */ - if (line->format != NULL) { - fmt = line->format; - cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); - } - else { - cont_orig = 0xFF; - } - - len = flatten_string(st, &fs, line->line); - str = fs.buf; - if (!text_check_format_len(line, len)) { - flatten_string_free(&fs); - return; - } - fmt = line->format; - - while (*str) { - /* Handle escape sequences by skipping both \ and next char */ - if (*str == '\\') { - *fmt = prev; fmt++; str++; - if (*str == '\0') break; - *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); - continue; - } - /* Handle continuations */ - else if (cont) { - /* Multi-line comments */ - if (cont & FMT_CONT_COMMENT_C) { - if (*str == ']' && *(str + 1) == ']') { - *fmt = FMT_TYPE_COMMENT; fmt++; str++; - *fmt = FMT_TYPE_COMMENT; - cont = FMT_CONT_NOP; - } - else { - *fmt = FMT_TYPE_COMMENT; - } - /* Handle other comments */ - } - else { - find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; - if (*str == find) cont = 0; - *fmt = FMT_TYPE_STRING; - } - - str += BLI_str_utf8_size_safe(str) - 1; - } - /* Not in a string... */ - else { - /* Multi-line comments not supported */ - /* Single line comment */ - if (*str == ';') { - text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); - } - else if (*str == '"' || *str == '\'') { - /* Strings */ - find = *str; - cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; - *fmt = FMT_TYPE_STRING; - } - /* Whitespace (all ws. has been converted to spaces) */ - else if (*str == ' ') { - *fmt = FMT_TYPE_WHITESPACE; - } - /* Numbers (digits not part of an identifier and periods followed by digits) */ - else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || - (*str == '.' && text_check_digit(*(str + 1)))) - { - *fmt = FMT_TYPE_NUMERAL; - } - /* Booleans */ - else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_ini_find_bool(str)) != -1) { - if (i > 0) { - text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - /* Punctuation */ - else if ((*str != '#') && text_check_delim(*str)) { - *fmt = FMT_TYPE_SYMBOL; - } - /* Identifiers and other text (no previous ws. or delims. so text continues) */ - else if (prev == FMT_TYPE_DEFAULT) { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ - else { - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Special vars(v) or built-in keywords(b) */ - /* keep in sync with 'txtfmt_ini_format_identifier()' */ - if ((i = txtfmt_ini_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; - else if ((i = txtfmt_ini_find_reserved(str)) != -1) prev = FMT_TYPE_RESERVED; - - /* clang-format on */ - - if (i > 0) { - text_format_fill_ascii(&str, &fmt, prev, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - } - prev = *fmt; fmt++; str++; - } - - /* Terminate and add continuation char */ - *fmt = '\0'; fmt++; - *fmt = cont; - - /* If continuation has changed and we're allowed, process the next line */ - if (cont != cont_orig && do_next && line->next) { - txtfmt_pov_ini_format_line(st, line->next, do_next); - } - - flatten_string_free(&fs); + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; + fmt++; + str++; + if (*str == '\0') + break; + *fmt = prev; + fmt++; + str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* Multi-line comments */ + if (cont & FMT_CONT_COMMENT_C) { + if (*str == ']' && *(str + 1) == ']') { + *fmt = FMT_TYPE_COMMENT; + fmt++; + str++; + *fmt = FMT_TYPE_COMMENT; + cont = FMT_CONT_NOP; + } + else { + *fmt = FMT_TYPE_COMMENT; + } + /* Handle other comments */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) + cont = 0; + *fmt = FMT_TYPE_STRING; + } + + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* Multi-line comments not supported */ + /* Single line comment */ + if (*str == ';') { + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) { + *fmt = FMT_TYPE_NUMERAL; + } + /* Booleans */ + else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_ini_find_bool(str)) != -1) { + if (i > 0) { + text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + /* Punctuation */ + else if ((*str != '#') && text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_ini_format_identifier()' */ + if ((i = txtfmt_ini_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; + else if ((i = txtfmt_ini_find_reserved(str)) != -1) prev = FMT_TYPE_RESERVED; + + /* clang-format on */ + + if (i > 0) { + text_format_fill_ascii(&str, &fmt, prev, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; + fmt++; + str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; + fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_pov_ini_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); } void ED_text_format_register_pov_ini(void) { - static TextFormatType tft = {NULL}; - static const char *ext[] = {"ini", NULL}; + static TextFormatType tft = {NULL}; + static const char *ext[] = {"ini", NULL}; - tft.format_identifier = txtfmt_pov_ini_format_identifier; - tft.format_line = txtfmt_pov_ini_format_line; - tft.ext = ext; + tft.format_identifier = txtfmt_pov_ini_format_identifier; + tft.format_line = txtfmt_pov_ini_format_line; + tft.ext = ext; - ED_text_format_register(&tft); + ED_text_format_register(&tft); } diff --git a/source/blender/editors/space_text/text_format_py.c b/source/blender/editors/space_text/text_format_py.c index bb0b639f508..68e42335546 100644 --- a/source/blender/editors/space_text/text_format_py.c +++ b/source/blender/editors/space_text/text_format_py.c @@ -43,59 +43,59 @@ */ static int txtfmt_py_find_builtinfunc(const char *string) { - int i, len; - /* list is from... - * ", ".join(['"%s"' % kw - * for kw in __import__("keyword").kwlist - * if kw not in {"False", "None", "True", "def", "class"}]) - * - * ... and for this code: - * print("\n".join(['else if (STR_LITERAL_STARTSWITH(string, "%s", len)) i = len;' % kw - * for kw in __import__("keyword").kwlist - * if kw not in {"False", "None", "True", "def", "class"}])) - */ - - /* Keep aligned args for readability. */ - /* clang-format off */ - - if (STR_LITERAL_STARTSWITH(string, "and", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "as", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "assert", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "async", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "await", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "continue", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "del", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "elif", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "except", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "finally", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "from", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "global", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "import", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "in", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "is", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "lambda", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "nonlocal", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "not", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "or", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "pass", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "raise", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "return", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "try", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "with", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "yield", len)) i = len; - else i = 0; - - /* clang-format on */ - - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + int i, len; + /* list is from... + * ", ".join(['"%s"' % kw + * for kw in __import__("keyword").kwlist + * if kw not in {"False", "None", "True", "def", "class"}]) + * + * ... and for this code: + * print("\n".join(['else if (STR_LITERAL_STARTSWITH(string, "%s", len)) i = len;' % kw + * for kw in __import__("keyword").kwlist + * if kw not in {"False", "None", "True", "def", "class"}])) + */ + + /* Keep aligned args for readability. */ + /* clang-format off */ + + if (STR_LITERAL_STARTSWITH(string, "and", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "as", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "assert", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "async", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "await", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "continue", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "del", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "elif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "except", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "finally", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "from", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "global", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "import", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "in", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "is", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambda", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "nonlocal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "not", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "or", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pass", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "raise", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "try", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "with", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "yield", len)) i = len; + else i = 0; + + /* clang-format on */ + + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } /* Checks the specified source string for a Python special name. This name must @@ -107,240 +107,256 @@ static int txtfmt_py_find_builtinfunc(const char *string) static int txtfmt_py_find_specialvar(const char *string) { - int i, len; + int i, len; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - if (STR_LITERAL_STARTSWITH(string, "def", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "class", len)) i = len; - else i = 0; + if (STR_LITERAL_STARTSWITH(string, "def", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "class", len)) i = len; + else i = 0; - /* clang-format on */ + /* clang-format on */ - /* If next source char is an identifier (eg. 'i' in "definite") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + /* If next source char is an identifier (eg. 'i' in "definite") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } static int txtfmt_py_find_decorator(const char *string) { - if (string[0] != '@') { - return -1; - } - if (!text_check_identifier(string[1])) { - return -1; - } - /* Interpret as matrix multiplication when followed by whitespace. */ - if (text_check_whitespace(string[1])) { - return -1; - } - - int i = 1; - while (text_check_identifier(string[i])) { - i++; - } - return i; + if (string[0] != '@') { + return -1; + } + if (!text_check_identifier(string[1])) { + return -1; + } + /* Interpret as matrix multiplication when followed by whitespace. */ + if (text_check_whitespace(string[1])) { + return -1; + } + + int i = 1; + while (text_check_identifier(string[i])) { + i++; + } + return i; } static int txtfmt_py_find_bool(const char *string) { - int i, len; + int i, len; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - if (STR_LITERAL_STARTSWITH(string, "None", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "True", len)) i = len; - else if (STR_LITERAL_STARTSWITH(string, "False", len)) i = len; - else i = 0; + if (STR_LITERAL_STARTSWITH(string, "None", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "True", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "False", len)) i = len; + else i = 0; - /* clang-format on */ + /* clang-format on */ - /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ - if (i == 0 || text_check_identifier(string[i])) - return -1; - return i; + /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ + if (i == 0 || text_check_identifier(string[i])) + return -1; + return i; } static char txtfmt_py_format_identifier(const char *str) { - char fmt; + char fmt; - /* Keep aligned args for readability. */ - /* clang-format off */ + /* Keep aligned args for readability. */ + /* clang-format off */ - if ((txtfmt_py_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; - else if ((txtfmt_py_find_builtinfunc(str)) != -1) fmt = FMT_TYPE_KEYWORD; - else if ((txtfmt_py_find_decorator(str)) != -1) fmt = FMT_TYPE_RESERVED; - else fmt = FMT_TYPE_DEFAULT; + if ((txtfmt_py_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; + else if ((txtfmt_py_find_builtinfunc(str)) != -1) fmt = FMT_TYPE_KEYWORD; + else if ((txtfmt_py_find_decorator(str)) != -1) fmt = FMT_TYPE_RESERVED; + else fmt = FMT_TYPE_DEFAULT; - /* clang-format on */ - return fmt; + /* clang-format on */ + return fmt; } static void txtfmt_py_format_line(SpaceText *st, TextLine *line, const bool do_next) { - FlattenString fs; - const char *str; - char *fmt; - char cont_orig, cont, find, prev = ' '; - int len, i; - - /* Get continuation from previous line */ - if (line->prev && line->prev->format != NULL) { - fmt = line->prev->format; - cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont) == cont); - } - else { - cont = FMT_CONT_NOP; - } - - /* Get original continuation from this line */ - if (line->format != NULL) { - fmt = line->format; - cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ - BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); - } - else { - cont_orig = 0xFF; - } - - len = flatten_string(st, &fs, line->line); - str = fs.buf; - if (!text_check_format_len(line, len)) { - flatten_string_free(&fs); - return; - } - fmt = line->format; - - while (*str) { - /* Handle escape sequences by skipping both \ and next char */ - if (*str == '\\') { - *fmt = prev; fmt++; str++; - if (*str == '\0') break; - *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); - continue; - } - /* Handle continuations */ - else if (cont) { - /* Triple strings ("""...""" or '''...''') */ - if (cont & FMT_CONT_TRIPLE) { - find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; - if (*str == find && *(str + 1) == find && *(str + 2) == find) { - *fmt = FMT_TYPE_STRING; fmt++; str++; - *fmt = FMT_TYPE_STRING; fmt++; str++; - cont = FMT_CONT_NOP; - } - /* Handle other strings */ - } - else { - find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; - if (*str == find) cont = FMT_CONT_NOP; - } - - *fmt = FMT_TYPE_STRING; - str += BLI_str_utf8_size_safe(str) - 1; - } - /* Not in a string... */ - else { - /* Deal with comments first */ - if (*str == '#') { - /* fill the remaining line */ - text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); - } - else if (*str == '"' || *str == '\'') { - /* Strings */ - find = *str; - cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; - if (*(str + 1) == find && *(str + 2) == find) { - *fmt = FMT_TYPE_STRING; fmt++; str++; - *fmt = FMT_TYPE_STRING; fmt++; str++; - cont |= FMT_CONT_TRIPLE; - } - *fmt = FMT_TYPE_STRING; - } - /* Whitespace (all ws. has been converted to spaces) */ - else if (*str == ' ') { - *fmt = FMT_TYPE_WHITESPACE; - } - /* Numbers (digits not part of an identifier and periods followed by digits) */ - else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || - (*str == '.' && text_check_digit(*(str + 1)))) - { - *fmt = FMT_TYPE_NUMERAL; - } - /* Booleans */ - else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_py_find_bool(str)) != -1) { - if (i > 0) { - text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - /* Punctuation */ - else if ((*str != '@') && text_check_delim(*str)) { - *fmt = FMT_TYPE_SYMBOL; - } - /* Identifiers and other text (no previous ws. or delims. so text continues) */ - else if (prev == FMT_TYPE_DEFAULT) { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ - else { - /* Keep aligned args for readability. */ - /* clang-format off */ - - /* Special vars(v) or built-in keywords(b) */ - /* keep in sync with 'txtfmt_py_format_identifier()' */ - if ((i = txtfmt_py_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; - else if ((i = txtfmt_py_find_builtinfunc(str)) != -1) prev = FMT_TYPE_KEYWORD; - else if ((i = txtfmt_py_find_decorator(str)) != -1) prev = FMT_TYPE_DIRECTIVE; - - /* clang-format on */ - - if (i > 0) { - if (prev == FMT_TYPE_DIRECTIVE) { /* can contain utf8 */ - text_format_fill(&str, &fmt, prev, i); - } - else { - text_format_fill_ascii(&str, &fmt, prev, i); - } - } - else { - str += BLI_str_utf8_size_safe(str) - 1; - *fmt = FMT_TYPE_DEFAULT; - } - } - } - prev = *fmt; fmt++; str++; - } - - /* Terminate and add continuation char */ - *fmt = '\0'; fmt++; - *fmt = cont; - - /* If continuation has changed and we're allowed, process the next line */ - if (cont != cont_orig && do_next && line->next) { - txtfmt_py_format_line(st, line->next, do_next); - } - - flatten_string_free(&fs); + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; + fmt++; + str++; + if (*str == '\0') + break; + *fmt = prev; + fmt++; + str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* Triple strings ("""...""" or '''...''') */ + if (cont & FMT_CONT_TRIPLE) { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find && *(str + 1) == find && *(str + 2) == find) { + *fmt = FMT_TYPE_STRING; + fmt++; + str++; + *fmt = FMT_TYPE_STRING; + fmt++; + str++; + cont = FMT_CONT_NOP; + } + /* Handle other strings */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) + cont = FMT_CONT_NOP; + } + + *fmt = FMT_TYPE_STRING; + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* Deal with comments first */ + if (*str == '#') { + /* fill the remaining line */ + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + if (*(str + 1) == find && *(str + 2) == find) { + *fmt = FMT_TYPE_STRING; + fmt++; + str++; + *fmt = FMT_TYPE_STRING; + fmt++; + str++; + cont |= FMT_CONT_TRIPLE; + } + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) { + *fmt = FMT_TYPE_NUMERAL; + } + /* Booleans */ + else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_py_find_bool(str)) != -1) { + if (i > 0) { + text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + /* Punctuation */ + else if ((*str != '@') && text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Keep aligned args for readability. */ + /* clang-format off */ + + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_py_format_identifier()' */ + if ((i = txtfmt_py_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; + else if ((i = txtfmt_py_find_builtinfunc(str)) != -1) prev = FMT_TYPE_KEYWORD; + else if ((i = txtfmt_py_find_decorator(str)) != -1) prev = FMT_TYPE_DIRECTIVE; + + /* clang-format on */ + + if (i > 0) { + if (prev == FMT_TYPE_DIRECTIVE) { /* can contain utf8 */ + text_format_fill(&str, &fmt, prev, i); + } + else { + text_format_fill_ascii(&str, &fmt, prev, i); + } + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; + fmt++; + str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; + fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_py_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); } void ED_text_format_register_py(void) { - static TextFormatType tft = {NULL}; - static const char *ext[] = {"py", NULL}; + static TextFormatType tft = {NULL}; + static const char *ext[] = {"py", NULL}; - tft.format_identifier = txtfmt_py_format_identifier; - tft.format_line = txtfmt_py_format_line; - tft.ext = ext; + tft.format_identifier = txtfmt_py_format_identifier; + tft.format_line = txtfmt_py_format_line; + tft.ext = ext; - ED_text_format_register(&tft); + ED_text_format_register(&tft); } diff --git a/source/blender/editors/space_text/text_header.c b/source/blender/editors/space_text/text_header.c index e76fab28327..435a6fd67de 100644 --- a/source/blender/editors/space_text/text_header.c +++ b/source/blender/editors/space_text/text_header.c @@ -27,7 +27,6 @@ #include "BLI_blenlib.h" - #include "BKE_context.h" #include "BKE_screen.h" @@ -43,165 +42,166 @@ static ARegion *text_has_properties_region(ScrArea *sa) { - ARegion *ar, *arnew; + ARegion *ar, *arnew; - ar = BKE_area_find_region_type(sa, RGN_TYPE_UI); - if (ar) return ar; + ar = BKE_area_find_region_type(sa, RGN_TYPE_UI); + if (ar) + return ar; - /* add subdiv level; after header */ - ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER); + /* add subdiv level; after header */ + ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER); - /* is error! */ - if (ar == NULL) return NULL; + /* is error! */ + if (ar == NULL) + return NULL; - arnew = MEM_callocN(sizeof(ARegion), "properties region"); + arnew = MEM_callocN(sizeof(ARegion), "properties region"); - BLI_insertlinkafter(&sa->regionbase, ar, arnew); - arnew->regiontype = RGN_TYPE_UI; - arnew->alignment = RGN_ALIGN_LEFT; + BLI_insertlinkafter(&sa->regionbase, ar, arnew); + arnew->regiontype = RGN_TYPE_UI; + arnew->alignment = RGN_ALIGN_LEFT; - arnew->flag = RGN_FLAG_HIDDEN; + arnew->flag = RGN_FLAG_HIDDEN; - return arnew; + return arnew; } static bool text_properties_poll(bContext *C) { - return (CTX_wm_space_text(C) != NULL); + return (CTX_wm_space_text(C) != NULL); } static int text_properties_exec(bContext *C, wmOperator *UNUSED(op)) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = text_has_properties_region(sa); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = text_has_properties_region(sa); - if (ar) - ED_region_toggle_hidden(C, ar); + if (ar) + ED_region_toggle_hidden(C, ar); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_properties(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Toggle Sidebar"; - ot->description = "Toggle the properties region visibility"; - ot->idname = "TEXT_OT_properties"; - - /* api callbacks */ - ot->exec = text_properties_exec; - ot->poll = text_properties_poll; + /* identifiers */ + ot->name = "Toggle Sidebar"; + ot->description = "Toggle the properties region visibility"; + ot->idname = "TEXT_OT_properties"; + + /* api callbacks */ + ot->exec = text_properties_exec; + ot->poll = text_properties_poll; } static int text_text_search_exec(bContext *C, wmOperator *UNUSED(op)) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = text_has_properties_region(sa); - SpaceText *st = CTX_wm_space_text(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = text_has_properties_region(sa); + SpaceText *st = CTX_wm_space_text(C); - if (ar) { - if (ar->flag & RGN_FLAG_HIDDEN) - ED_region_toggle_hidden(C, ar); + if (ar) { + if (ar->flag & RGN_FLAG_HIDDEN) + ED_region_toggle_hidden(C, ar); - /* cannot send a button activate yet for case when region wasn't visible yet */ - /* flag gets checked and cleared in main draw callback */ - st->flags |= ST_FIND_ACTIVATE; + /* cannot send a button activate yet for case when region wasn't visible yet */ + /* flag gets checked and cleared in main draw callback */ + st->flags |= ST_FIND_ACTIVATE; - ED_region_tag_redraw(ar); - } - return OPERATOR_FINISHED; + ED_region_tag_redraw(ar); + } + return OPERATOR_FINISHED; } - void TEXT_OT_start_find(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Find"; - ot->description = "Start searching text"; - ot->idname = "TEXT_OT_start_find"; - - /* api callbacks */ - ot->exec = text_text_search_exec; - ot->poll = text_properties_poll; + /* identifiers */ + ot->name = "Find"; + ot->description = "Start searching text"; + ot->idname = "TEXT_OT_start_find"; + + /* api callbacks */ + ot->exec = text_text_search_exec; + ot->poll = text_properties_poll; } /******************** XXX popup menus *******************/ #if 0 { - // RMB - - uiPopupMenu *pup; - - if (text) { - pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE); - if (txt_has_sel(text)) { - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_cut"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_copy"); - } - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_paste"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save_as"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_run_script"); - UI_popup_menu_end(C, pup); - } - else { - pup = UI_popup_menu_begin(C, IFACE_("File"), ICON_NONE); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); - UI_popup_menu_end(C, pup); - } + // RMB + + uiPopupMenu *pup; + + if (text) { + pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE); + if (txt_has_sel(text)) { + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_cut"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_copy"); + } + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_paste"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save_as"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_run_script"); + UI_popup_menu_end(C, pup); + } + else { + pup = UI_popup_menu_begin(C, IFACE_("File"), ICON_NONE); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); + UI_popup_menu_end(C, pup); + } } { - // Alt+Shift+E + // Alt+Shift+E - uiPopupMenu *pup; + uiPopupMenu *pup; - pup = UI_popup_menu_begin(C, IFACE_("Edit"), ICON_NONE); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_cut"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_copy"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_paste"); - UI_popup_menu_end(C, pup); + pup = UI_popup_menu_begin(C, IFACE_("Edit"), ICON_NONE); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_cut"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_copy"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_paste"); + UI_popup_menu_end(C, pup); } { - // Alt+Shift+F - - uiPopupMenu *pup; - - if (text) { - pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save_as"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_run_script"); - UI_popup_menu_end(C, pup); - } - else { - pup = UI_popup_menu_begin(C, IFACE_("File"), ICON_NONE); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); - uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); - UI_popup_menu_end(C, pup); - } + // Alt+Shift+F + + uiPopupMenu *pup; + + if (text) { + pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save_as"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_run_script"); + UI_popup_menu_end(C, pup); + } + else { + pup = UI_popup_menu_begin(C, IFACE_("File"), ICON_NONE); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new"); + uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open"); + UI_popup_menu_end(C, pup); + } } { - // Alt+Shift+V - - uiPopupMenu *pup; - - pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE); - uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Top of File"), - 0, "type", FILE_TOP); - uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Bottom of File"), - 0, "type", FILE_BOTTOM); - uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Page Up"), 0, "type", PREV_PAGE); - uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Page Down"), - 0, "type", NEXT_PAGE); - UI_popup_menu_end(C, pup); + // Alt+Shift+V + + uiPopupMenu *pup; + + pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE); + uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Top of File"), + 0, "type", FILE_TOP); + uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Bottom of File"), + 0, "type", FILE_BOTTOM); + uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Page Up"), 0, "type", PREV_PAGE); + uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Page Down"), + 0, "type", NEXT_PAGE); + UI_popup_menu_end(C, pup); } #endif diff --git a/source/blender/editors/space_text/text_intern.h b/source/blender/editors/space_text/text_intern.h index bf31469c322..cf28f3d317c 100644 --- a/source/blender/editors/space_text/text_intern.h +++ b/source/blender/editors/space_text/text_intern.h @@ -44,23 +44,33 @@ void text_scroll_to_cursor(struct SpaceText *st, struct ARegion *ar, const bool void text_scroll_to_cursor__area(struct SpaceText *st, struct ScrArea *sa, const bool center); void text_update_cursor_moved(struct bContext *C); -#define TXT_OFFSET ((int)(0.5f * U.widget_unit)) -#define TXT_SCROLL_WIDTH U.widget_unit -#define TXT_SCROLL_SPACE ((int)(0.1f * U.widget_unit)) -#define TXT_LINE_SPACING ((int)(0.3f * st->lheight_dpi)) /* space between lines */ -#define TEXTXLOC (st->cwidth * st->linenrs_tot) +#define TXT_OFFSET ((int)(0.5f * U.widget_unit)) +#define TXT_SCROLL_WIDTH U.widget_unit +#define TXT_SCROLL_SPACE ((int)(0.1f * U.widget_unit)) +#define TXT_LINE_SPACING ((int)(0.3f * st->lheight_dpi)) /* space between lines */ +#define TEXTXLOC (st->cwidth * st->linenrs_tot) -#define SUGG_LIST_SIZE 7 -#define SUGG_LIST_WIDTH 20 -#define DOC_WIDTH 40 -#define DOC_HEIGHT 10 +#define SUGG_LIST_SIZE 7 +#define SUGG_LIST_WIDTH 20 +#define DOC_WIDTH 40 +#define DOC_HEIGHT 10 -#define TOOL_SUGG_LIST 0x01 -#define TOOL_DOCUMENT 0x02 +#define TOOL_SUGG_LIST 0x01 +#define TOOL_DOCUMENT 0x02 int wrap_width(const struct SpaceText *st, struct ARegion *ar); -void wrap_offset(const struct SpaceText *st, struct ARegion *ar, struct TextLine *linein, int cursin, int *offl, int *offc); -void wrap_offset_in_line(const struct SpaceText *st, struct ARegion *ar, struct TextLine *linep, int cursin, int *offl, int *offc); +void wrap_offset(const struct SpaceText *st, + struct ARegion *ar, + struct TextLine *linein, + int cursin, + int *offl, + int *offc); +void wrap_offset_in_line(const struct SpaceText *st, + struct ARegion *ar, + struct TextLine *linep, + int cursin, + int *offl, + int *offc); int text_get_char_pos(const struct SpaceText *st, const char *line, int cur); void text_drawcache_tag_update(struct SpaceText *st, int full); @@ -70,12 +80,27 @@ int text_do_suggest_select(struct SpaceText *st, struct ARegion *ar); void text_pop_suggest_list(void); int text_get_visible_lines(const struct SpaceText *st, struct ARegion *ar, const char *str); -int text_get_span_wrap(const struct SpaceText *st, struct ARegion *ar, struct TextLine *from, struct TextLine *to); +int text_get_span_wrap(const struct SpaceText *st, + struct ARegion *ar, + struct TextLine *from, + struct TextLine *to); int text_get_total_lines(struct SpaceText *st, struct ARegion *ar); /* text_ops.c */ -enum { LINE_BEGIN, LINE_END, FILE_TOP, FILE_BOTTOM, PREV_CHAR, NEXT_CHAR, - PREV_WORD, NEXT_WORD, PREV_LINE, NEXT_LINE, PREV_PAGE, NEXT_PAGE }; +enum { + LINE_BEGIN, + LINE_END, + FILE_TOP, + FILE_BOTTOM, + PREV_CHAR, + NEXT_CHAR, + PREV_WORD, + NEXT_WORD, + PREV_LINE, + NEXT_LINE, + PREV_PAGE, + NEXT_PAGE +}; enum { DEL_NEXT_CHAR, DEL_PREV_CHAR, DEL_NEXT_WORD, DEL_PREV_WORD }; void TEXT_OT_new(struct wmOperatorType *ot); diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 2bf1dff63c1..c069fc600e8 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -21,7 +21,6 @@ * \ingroup sptext */ - #include <string.h> #include <errno.h> @@ -54,7 +53,7 @@ #include "RNA_define.h" #ifdef WITH_PYTHON -#include "BPY_extern.h" +# include "BPY_extern.h" #endif #include "text_intern.h" @@ -64,593 +63,610 @@ static void txt_screen_clamp(SpaceText *st, ARegion *ar); /************************ poll ***************************/ - BLI_INLINE int text_pixel_x_to_column(SpaceText *st, const int x) { - /* add half the char width so mouse cursor selection is inbetween letters */ - return (x + (st->cwidth / 2)) / st->cwidth; + /* add half the char width so mouse cursor selection is inbetween letters */ + return (x + (st->cwidth / 2)) / st->cwidth; } static bool text_new_poll(bContext *UNUSED(C)) { - return 1; + return 1; } static bool text_edit_poll(bContext *C) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - if (!text) - return 0; + if (!text) + return 0; - if (ID_IS_LINKED(text)) { - // BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data"); - return 0; - } + if (ID_IS_LINKED(text)) { + // BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data"); + return 0; + } - return 1; + return 1; } bool text_space_edit_poll(bContext *C) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); - if (!st || !text) - return 0; + if (!st || !text) + return 0; - if (ID_IS_LINKED(text)) { - // BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data"); - return 0; - } + if (ID_IS_LINKED(text)) { + // BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data"); + return 0; + } - return 1; + return 1; } static bool text_region_edit_poll(bContext *C) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - ARegion *ar = CTX_wm_region(C); + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + ARegion *ar = CTX_wm_region(C); - if (!st || !text) - return 0; + if (!st || !text) + return 0; - if (!ar || ar->regiontype != RGN_TYPE_WINDOW) - return 0; + if (!ar || ar->regiontype != RGN_TYPE_WINDOW) + return 0; - if (ID_IS_LINKED(text)) { - // BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data"); - return 0; - } + if (ID_IS_LINKED(text)) { + // BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data"); + return 0; + } - return 1; + return 1; } /********************** updates *********************/ void text_update_line_edited(TextLine *line) { - if (!line) - return; + if (!line) + return; - /* we just free format here, and let it rebuild during draw */ - if (line->format) { - MEM_freeN(line->format); - line->format = NULL; - } + /* we just free format here, and let it rebuild during draw */ + if (line->format) { + MEM_freeN(line->format); + line->format = NULL; + } } void text_update_edited(Text *text) { - TextLine *line; + TextLine *line; - for (line = text->lines.first; line; line = line->next) - text_update_line_edited(line); + for (line = text->lines.first; line; line = line->next) + text_update_line_edited(line); } /******************* new operator *********************/ static int text_new_exec(bContext *C, wmOperator *UNUSED(op)) { - SpaceText *st = CTX_wm_space_text(C); - Main *bmain = CTX_data_main(C); - Text *text; - PointerRNA ptr, idptr; - PropertyRNA *prop; + SpaceText *st = CTX_wm_space_text(C); + Main *bmain = CTX_data_main(C); + Text *text; + PointerRNA ptr, idptr; + PropertyRNA *prop; - text = BKE_text_add(bmain, "Text"); + text = BKE_text_add(bmain, "Text"); - /* hook into UI */ - UI_context_active_but_prop_get_templateID(C, &ptr, &prop); + /* hook into UI */ + UI_context_active_but_prop_get_templateID(C, &ptr, &prop); - if (prop) { - RNA_id_pointer_create(&text->id, &idptr); - RNA_property_pointer_set(&ptr, prop, idptr); - RNA_property_update(C, &ptr, prop); - } - else if (st) { - st->text = text; - st->left = 0; - st->top = 0; - st->scroll_accum[0] = 0.0f; - st->scroll_accum[1] = 0.0f; - text_drawcache_tag_update(st, 1); - } + if (prop) { + RNA_id_pointer_create(&text->id, &idptr); + RNA_property_pointer_set(&ptr, prop, idptr); + RNA_property_update(C, &ptr, prop); + } + else if (st) { + st->text = text; + st->left = 0; + st->top = 0; + st->scroll_accum[0] = 0.0f; + st->scroll_accum[1] = 0.0f; + text_drawcache_tag_update(st, 1); + } - WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); + WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_new(wmOperatorType *ot) { - /* identifiers */ - ot->name = "New Text"; - ot->idname = "TEXT_OT_new"; - ot->description = "Create a new text data-block"; + /* identifiers */ + ot->name = "New Text"; + ot->idname = "TEXT_OT_new"; + ot->description = "Create a new text data-block"; - /* api callbacks */ - ot->exec = text_new_exec; - ot->poll = text_new_poll; + /* api callbacks */ + ot->exec = text_new_exec; + ot->poll = text_new_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* open operator *********************/ static void text_open_init(bContext *C, wmOperator *op) { - PropertyPointerRNA *pprop; + PropertyPointerRNA *pprop; - op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA"); - UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop); + op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA"); + UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop); } static void text_open_cancel(bContext *UNUSED(C), wmOperator *op) { - MEM_freeN(op->customdata); + MEM_freeN(op->customdata); } static int text_open_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Main *bmain = CTX_data_main(C); - Text *text; - PropertyPointerRNA *pprop; - PointerRNA idptr; - char str[FILE_MAX]; - const bool internal = RNA_boolean_get(op->ptr, "internal"); + SpaceText *st = CTX_wm_space_text(C); + Main *bmain = CTX_data_main(C); + Text *text; + PropertyPointerRNA *pprop; + PointerRNA idptr; + char str[FILE_MAX]; + const bool internal = RNA_boolean_get(op->ptr, "internal"); - RNA_string_get(op->ptr, "filepath", str); + RNA_string_get(op->ptr, "filepath", str); - text = BKE_text_load_ex(bmain, str, BKE_main_blendfile_path(bmain), internal); + text = BKE_text_load_ex(bmain, str, BKE_main_blendfile_path(bmain), internal); - if (!text) { - if (op->customdata) MEM_freeN(op->customdata); - return OPERATOR_CANCELLED; - } + if (!text) { + if (op->customdata) + MEM_freeN(op->customdata); + return OPERATOR_CANCELLED; + } - if (!op->customdata) - text_open_init(C, op); + if (!op->customdata) + text_open_init(C, op); - /* hook into UI */ - pprop = op->customdata; + /* hook into UI */ + pprop = op->customdata; - id_us_ensure_real(&text->id); + id_us_ensure_real(&text->id); - if (pprop->prop) { - RNA_id_pointer_create(&text->id, &idptr); - RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); - RNA_property_update(C, &pprop->ptr, pprop->prop); - } - else if (st) { - st->text = text; - st->left = 0; - st->top = 0; - st->scroll_accum[0] = 0.0f; - st->scroll_accum[1] = 0.0f; - } + if (pprop->prop) { + RNA_id_pointer_create(&text->id, &idptr); + RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); + RNA_property_update(C, &pprop->ptr, pprop->prop); + } + else if (st) { + st->text = text; + st->left = 0; + st->top = 0; + st->scroll_accum[0] = 0.0f; + st->scroll_accum[1] = 0.0f; + } - text_drawcache_tag_update(st, 1); - WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); + text_drawcache_tag_update(st, 1); + WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); - MEM_freeN(op->customdata); + MEM_freeN(op->customdata); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } static int text_open_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - Main *bmain = CTX_data_main(C); - Text *text = CTX_data_edit_text(C); - const char *path = (text && text->name) ? text->name : BKE_main_blendfile_path(bmain); + Main *bmain = CTX_data_main(C); + Text *text = CTX_data_edit_text(C); + const char *path = (text && text->name) ? text->name : BKE_main_blendfile_path(bmain); - if (RNA_struct_property_is_set(op->ptr, "filepath")) - return text_open_exec(C, op); + if (RNA_struct_property_is_set(op->ptr, "filepath")) + return text_open_exec(C, op); - text_open_init(C, op); - RNA_string_set(op->ptr, "filepath", path); - WM_event_add_fileselect(C, op); + text_open_init(C, op); + RNA_string_set(op->ptr, "filepath", path); + WM_event_add_fileselect(C, op); - return OPERATOR_RUNNING_MODAL; + return OPERATOR_RUNNING_MODAL; } void TEXT_OT_open(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Open Text"; - ot->idname = "TEXT_OT_open"; - ot->description = "Open a new text data-block"; + /* identifiers */ + ot->name = "Open Text"; + ot->idname = "TEXT_OT_open"; + ot->description = "Open a new text data-block"; - /* api callbacks */ - ot->exec = text_open_exec; - ot->invoke = text_open_invoke; - ot->cancel = text_open_cancel; - ot->poll = text_new_poll; + /* api callbacks */ + ot->exec = text_open_exec; + ot->invoke = text_open_invoke; + ot->cancel = text_open_cancel; + ot->poll = text_new_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; - /* properties */ - WM_operator_properties_filesel( - ot, FILE_TYPE_FOLDER | FILE_TYPE_TEXT | FILE_TYPE_PYSCRIPT, FILE_SPECIAL, FILE_OPENFILE, - WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); //XXX TODO, relative_path - RNA_def_boolean(ot->srna, "internal", 0, "Make internal", "Make text file internal after loading"); + /* properties */ + WM_operator_properties_filesel(ot, + FILE_TYPE_FOLDER | FILE_TYPE_TEXT | FILE_TYPE_PYSCRIPT, + FILE_SPECIAL, + FILE_OPENFILE, + WM_FILESEL_FILEPATH, + FILE_DEFAULTDISPLAY, + FILE_SORT_ALPHA); //XXX TODO, relative_path + RNA_def_boolean( + ot->srna, "internal", 0, "Make internal", "Make text file internal after loading"); } /******************* reload operator *********************/ static int text_reload_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - ARegion *ar = CTX_wm_region(C); + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + ARegion *ar = CTX_wm_region(C); - /* store view & cursor state */ - const int orig_top = st->top; - const int orig_curl = BLI_findindex(&text->lines, text->curl); - const int orig_curc = text->curc; + /* store view & cursor state */ + const int orig_top = st->top; + const int orig_curl = BLI_findindex(&text->lines, text->curl); + const int orig_curc = text->curc; - if (!BKE_text_reload(text)) { - BKE_report(op->reports, RPT_ERROR, "Could not reopen file"); - return OPERATOR_CANCELLED; - } + if (!BKE_text_reload(text)) { + BKE_report(op->reports, RPT_ERROR, "Could not reopen file"); + return OPERATOR_CANCELLED; + } #ifdef WITH_PYTHON - if (text->compiled) - BPY_text_free_code(text); + if (text->compiled) + BPY_text_free_code(text); #endif - text_update_edited(text); - text_update_cursor_moved(C); - text_drawcache_tag_update(CTX_wm_space_text(C), 1); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_edited(text); + text_update_cursor_moved(C); + text_drawcache_tag_update(CTX_wm_space_text(C), 1); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - /* return to scroll position */ - st->top = orig_top; - txt_screen_clamp(st, ar); - /* return cursor */ - txt_move_to(text, orig_curl, orig_curc, false); + /* return to scroll position */ + st->top = orig_top; + txt_screen_clamp(st, ar); + /* return cursor */ + txt_move_to(text, orig_curl, orig_curc, false); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_reload(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Reload"; - ot->idname = "TEXT_OT_reload"; - ot->description = "Reload active text data-block from its file"; + /* identifiers */ + ot->name = "Reload"; + ot->idname = "TEXT_OT_reload"; + ot->description = "Reload active text data-block from its file"; - /* api callbacks */ - ot->exec = text_reload_exec; - ot->invoke = WM_operator_confirm; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_reload_exec; + ot->invoke = WM_operator_confirm; + ot->poll = text_edit_poll; } /******************* delete operator *********************/ static bool text_unlink_poll(bContext *C) { - /* it should be possible to unlink texts if they're lib-linked in... */ - return CTX_data_edit_text(C) != NULL; + /* it should be possible to unlink texts if they're lib-linked in... */ + return CTX_data_edit_text(C) != NULL; } static int text_unlink_exec(bContext *C, wmOperator *UNUSED(op)) { - Main *bmain = CTX_data_main(C); - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); + Main *bmain = CTX_data_main(C); + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); - /* make the previous text active, if its not there make the next text active */ - if (st) { - if (text->id.prev) { - st->text = text->id.prev; - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - } - else if (text->id.next) { - st->text = text->id.next; - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - } - } + /* make the previous text active, if its not there make the next text active */ + if (st) { + if (text->id.prev) { + st->text = text->id.prev; + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + } + else if (text->id.next) { + st->text = text->id.next; + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + } + } - BKE_id_delete(bmain, text); + BKE_id_delete(bmain, text); - text_drawcache_tag_update(st, 1); - WM_event_add_notifier(C, NC_TEXT | NA_REMOVED, NULL); + text_drawcache_tag_update(st, 1); + WM_event_add_notifier(C, NC_TEXT | NA_REMOVED, NULL); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_unlink(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Unlink"; - ot->idname = "TEXT_OT_unlink"; - ot->description = "Unlink active text data-block"; + /* identifiers */ + ot->name = "Unlink"; + ot->idname = "TEXT_OT_unlink"; + ot->description = "Unlink active text data-block"; - /* api callbacks */ - ot->exec = text_unlink_exec; - ot->invoke = WM_operator_confirm; - ot->poll = text_unlink_poll; + /* api callbacks */ + ot->exec = text_unlink_exec; + ot->invoke = WM_operator_confirm; + ot->poll = text_unlink_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* make internal operator *********************/ static int text_make_internal_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - text->flags |= TXT_ISMEM | TXT_ISDIRTY; + text->flags |= TXT_ISMEM | TXT_ISDIRTY; - if (text->name) { - MEM_freeN(text->name); - text->name = NULL; - } + if (text->name) { + MEM_freeN(text->name); + text->name = NULL; + } - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_make_internal(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Make Internal"; - ot->idname = "TEXT_OT_make_internal"; - ot->description = "Make active text file internal"; + /* identifiers */ + ot->name = "Make Internal"; + ot->idname = "TEXT_OT_make_internal"; + ot->description = "Make active text file internal"; - /* api callbacks */ - ot->exec = text_make_internal_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_make_internal_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* save operator *********************/ static bool text_save_poll(bContext *C) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - if (!text_edit_poll(C)) - return 0; + if (!text_edit_poll(C)) + return 0; - return (text->name != NULL && !(text->flags & TXT_ISMEM)); + return (text->name != NULL && !(text->flags & TXT_ISMEM)); } static void txt_write_file(Main *bmain, Text *text, ReportList *reports) { - FILE *fp; - TextLine *tmp; - BLI_stat_t st; - char filepath[FILE_MAX]; - - BLI_strncpy(filepath, text->name, FILE_MAX); - BLI_path_abs(filepath, BKE_main_blendfile_path(bmain)); - - fp = BLI_fopen(filepath, "w"); - if (fp == NULL) { - BKE_reportf(reports, RPT_ERROR, "Unable to save '%s': %s", - filepath, errno ? strerror(errno) : TIP_("unknown error writing file")); - return; - } - - for (tmp = text->lines.first; tmp; tmp = tmp->next) { - fputs(tmp->line, fp); - if (tmp->next) { - fputc('\n', fp); - } - } - - fclose(fp); - - if (BLI_stat(filepath, &st) == 0) { - text->mtime = st.st_mtime; - - /* report since this can be called from key-shortcuts */ - BKE_reportf(reports, RPT_INFO, "Saved Text '%s'", filepath); - } - else { - text->mtime = 0; - BKE_reportf(reports, RPT_WARNING, "Unable to stat '%s': %s", - filepath, errno ? strerror(errno) : TIP_("unknown error stating file")); - } - - text->flags &= ~TXT_ISDIRTY; + FILE *fp; + TextLine *tmp; + BLI_stat_t st; + char filepath[FILE_MAX]; + + BLI_strncpy(filepath, text->name, FILE_MAX); + BLI_path_abs(filepath, BKE_main_blendfile_path(bmain)); + + fp = BLI_fopen(filepath, "w"); + if (fp == NULL) { + BKE_reportf(reports, + RPT_ERROR, + "Unable to save '%s': %s", + filepath, + errno ? strerror(errno) : TIP_("unknown error writing file")); + return; + } + + for (tmp = text->lines.first; tmp; tmp = tmp->next) { + fputs(tmp->line, fp); + if (tmp->next) { + fputc('\n', fp); + } + } + + fclose(fp); + + if (BLI_stat(filepath, &st) == 0) { + text->mtime = st.st_mtime; + + /* report since this can be called from key-shortcuts */ + BKE_reportf(reports, RPT_INFO, "Saved Text '%s'", filepath); + } + else { + text->mtime = 0; + BKE_reportf(reports, + RPT_WARNING, + "Unable to stat '%s': %s", + filepath, + errno ? strerror(errno) : TIP_("unknown error stating file")); + } + + text->flags &= ~TXT_ISDIRTY; } static int text_save_exec(bContext *C, wmOperator *op) { - Main *bmain = CTX_data_main(C); - Text *text = CTX_data_edit_text(C); + Main *bmain = CTX_data_main(C); + Text *text = CTX_data_edit_text(C); - txt_write_file(bmain, text, op->reports); + txt_write_file(bmain, text, op->reports); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_save(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Save"; - ot->idname = "TEXT_OT_save"; - ot->description = "Save active text data-block"; + /* identifiers */ + ot->name = "Save"; + ot->idname = "TEXT_OT_save"; + ot->description = "Save active text data-block"; - /* api callbacks */ - ot->exec = text_save_exec; - ot->poll = text_save_poll; + /* api callbacks */ + ot->exec = text_save_exec; + ot->poll = text_save_poll; } /******************* save as operator *********************/ static int text_save_as_exec(bContext *C, wmOperator *op) { - Main *bmain = CTX_data_main(C); - Text *text = CTX_data_edit_text(C); - char str[FILE_MAX]; + Main *bmain = CTX_data_main(C); + Text *text = CTX_data_edit_text(C); + char str[FILE_MAX]; - if (!text) - return OPERATOR_CANCELLED; + if (!text) + return OPERATOR_CANCELLED; - RNA_string_get(op->ptr, "filepath", str); + RNA_string_get(op->ptr, "filepath", str); - if (text->name) MEM_freeN(text->name); - text->name = BLI_strdup(str); - text->flags &= ~TXT_ISMEM; + if (text->name) + MEM_freeN(text->name); + text->name = BLI_strdup(str); + text->flags &= ~TXT_ISMEM; - txt_write_file(bmain, text, op->reports); + txt_write_file(bmain, text, op->reports); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } static int text_save_as_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - Main *bmain = CTX_data_main(C); - Text *text = CTX_data_edit_text(C); - const char *str; + Main *bmain = CTX_data_main(C); + Text *text = CTX_data_edit_text(C); + const char *str; - if (RNA_struct_property_is_set(op->ptr, "filepath")) - return text_save_as_exec(C, op); + if (RNA_struct_property_is_set(op->ptr, "filepath")) + return text_save_as_exec(C, op); - if (text->name) - str = text->name; - else if (text->flags & TXT_ISMEM) - str = text->id.name + 2; - else - str = BKE_main_blendfile_path(bmain); + if (text->name) + str = text->name; + else if (text->flags & TXT_ISMEM) + str = text->id.name + 2; + else + str = BKE_main_blendfile_path(bmain); - RNA_string_set(op->ptr, "filepath", str); - WM_event_add_fileselect(C, op); + RNA_string_set(op->ptr, "filepath", str); + WM_event_add_fileselect(C, op); - return OPERATOR_RUNNING_MODAL; + return OPERATOR_RUNNING_MODAL; } void TEXT_OT_save_as(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Save As"; - ot->idname = "TEXT_OT_save_as"; - ot->description = "Save active text file with options"; + /* identifiers */ + ot->name = "Save As"; + ot->idname = "TEXT_OT_save_as"; + ot->description = "Save active text file with options"; - /* api callbacks */ - ot->exec = text_save_as_exec; - ot->invoke = text_save_as_invoke; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_save_as_exec; + ot->invoke = text_save_as_invoke; + ot->poll = text_edit_poll; - /* properties */ - WM_operator_properties_filesel( - ot, FILE_TYPE_FOLDER | FILE_TYPE_TEXT | FILE_TYPE_PYSCRIPT, FILE_SPECIAL, FILE_SAVE, - WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); //XXX TODO, relative_path + /* properties */ + WM_operator_properties_filesel(ot, + FILE_TYPE_FOLDER | FILE_TYPE_TEXT | FILE_TYPE_PYSCRIPT, + FILE_SPECIAL, + FILE_SAVE, + WM_FILESEL_FILEPATH, + FILE_DEFAULTDISPLAY, + FILE_SORT_ALPHA); //XXX TODO, relative_path } /******************* run script operator *********************/ static bool text_run_script_poll(bContext *C) { - return (CTX_data_edit_text(C) != NULL); + return (CTX_data_edit_text(C) != NULL); } static int text_run_script(bContext *C, ReportList *reports) { #ifdef WITH_PYTHON - Text *text = CTX_data_edit_text(C); - const bool is_live = (reports == NULL); - - /* only for comparison */ - void *curl_prev = text->curl; - int curc_prev = text->curc; - - if (BPY_execute_text(C, text, reports, !is_live)) { - if (is_live) { - /* for nice live updates */ - WM_event_add_notifier(C, NC_WINDOW | NA_EDITED, NULL); - } - return OPERATOR_FINISHED; - } - - /* Don't report error messages while live editing */ - if (!is_live) { - /* text may have freed its self */ - if (CTX_data_edit_text(C) == text) { - if (text->curl != curl_prev || curc_prev != text->curc) { - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - } - } - - BKE_report(reports, RPT_ERROR, "Python script failed, check the message in the system console"); - - return OPERATOR_FINISHED; - } + Text *text = CTX_data_edit_text(C); + const bool is_live = (reports == NULL); + + /* only for comparison */ + void *curl_prev = text->curl; + int curc_prev = text->curc; + + if (BPY_execute_text(C, text, reports, !is_live)) { + if (is_live) { + /* for nice live updates */ + WM_event_add_notifier(C, NC_WINDOW | NA_EDITED, NULL); + } + return OPERATOR_FINISHED; + } + + /* Don't report error messages while live editing */ + if (!is_live) { + /* text may have freed its self */ + if (CTX_data_edit_text(C) == text) { + if (text->curl != curl_prev || curc_prev != text->curc) { + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + } + } + + BKE_report( + reports, RPT_ERROR, "Python script failed, check the message in the system console"); + + return OPERATOR_FINISHED; + } #else - (void)C; - (void)reports; + (void)C; + (void)reports; #endif /* !WITH_PYTHON */ - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } static int text_run_script_exec(bContext *C, wmOperator *op) { #ifndef WITH_PYTHON - (void)C; /* unused */ + (void)C; /* unused */ - BKE_report(op->reports, RPT_ERROR, "Python disabled in this build"); + BKE_report(op->reports, RPT_ERROR, "Python disabled in this build"); - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; #else - return text_run_script(C, op->reports); + return text_run_script(C, op->reports); #endif } void TEXT_OT_run_script(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Run Script"; - ot->idname = "TEXT_OT_run_script"; - ot->description = "Run active script"; + /* identifiers */ + ot->name = "Run Script"; + ot->idname = "TEXT_OT_run_script"; + ot->description = "Run active script"; - /* api callbacks */ - ot->poll = text_run_script_poll; - ot->exec = text_run_script_exec; + /* api callbacks */ + ot->poll = text_run_script_poll; + ot->exec = text_run_script_exec; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /******************* refresh pyconstraints operator *********************/ @@ -658,1087 +674,1136 @@ void TEXT_OT_run_script(wmOperatorType *ot) static int text_refresh_pyconstraints_exec(bContext *UNUSED(C), wmOperator *UNUSED(op)) { #ifdef WITH_PYTHON -#if 0 - Text *text = CTX_data_edit_text(C); - Object *ob; - bConstraint *con; - short update; - - /* check all pyconstraints */ - for (ob = CTX_data_main(C)->objects.first; ob; ob = ob->id.next) { - update = 0; - if (ob->type == OB_ARMATURE && ob->pose) { - bPoseChannel *pchan; - for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { - for (con = pchan->constraints.first; con; con = con->next) { - if (con->type == CONSTRAINT_TYPE_PYTHON) { - bPythonConstraint *data = con->data; - if (data->text == text) BPY_pyconstraint_update(ob, con); - update = 1; - - } - } - } - } - for (con = ob->constraints.first; con; con = con->next) { - if (con->type == CONSTRAINT_TYPE_PYTHON) { - bPythonConstraint *data = con->data; - if (data->text == text) BPY_pyconstraint_update(ob, con); - update = 1; - } - } - - if (update) { - DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY); - } - } -#endif +# if 0 + Text *text = CTX_data_edit_text(C); + Object *ob; + bConstraint *con; + short update; + + /* check all pyconstraints */ + for (ob = CTX_data_main(C)->objects.first; ob; ob = ob->id.next) { + update = 0; + if (ob->type == OB_ARMATURE && ob->pose) { + bPoseChannel *pchan; + for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { + for (con = pchan->constraints.first; con; con = con->next) { + if (con->type == CONSTRAINT_TYPE_PYTHON) { + bPythonConstraint *data = con->data; + if (data->text == text) BPY_pyconstraint_update(ob, con); + update = 1; + + } + } + } + } + for (con = ob->constraints.first; con; con = con->next) { + if (con->type == CONSTRAINT_TYPE_PYTHON) { + bPythonConstraint *data = con->data; + if (data->text == text) BPY_pyconstraint_update(ob, con); + update = 1; + } + } + + if (update) { + DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY); + } + } +# endif #endif - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_refresh_pyconstraints(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Refresh PyConstraints"; - ot->idname = "TEXT_OT_refresh_pyconstraints"; - ot->description = "Refresh all pyconstraints"; + /* identifiers */ + ot->name = "Refresh PyConstraints"; + ot->idname = "TEXT_OT_refresh_pyconstraints"; + ot->description = "Refresh all pyconstraints"; - /* api callbacks */ - ot->exec = text_refresh_pyconstraints_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_refresh_pyconstraints_exec; + ot->poll = text_edit_poll; } /******************* paste operator *********************/ static int text_paste_exec(bContext *C, wmOperator *op) { - const bool selection = RNA_boolean_get(op->ptr, "selection"); - Text *text = CTX_data_edit_text(C); - char *buf; - int buf_len; + const bool selection = RNA_boolean_get(op->ptr, "selection"); + Text *text = CTX_data_edit_text(C); + char *buf; + int buf_len; - buf = WM_clipboard_text_get(selection, &buf_len); + buf = WM_clipboard_text_get(selection, &buf_len); - if (!buf) - return OPERATOR_CANCELLED; + if (!buf) + return OPERATOR_CANCELLED; - text_drawcache_tag_update(CTX_wm_space_text(C), 0); + text_drawcache_tag_update(CTX_wm_space_text(C), 0); - TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_insert_buf(text, utxt, buf); - text_update_edited(text); + TextUndoBuf *utxt = ED_text_undo_push_init(C); + txt_insert_buf(text, utxt, buf); + text_update_edited(text); - MEM_freeN(buf); + MEM_freeN(buf); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - /* run the script while editing, evil but useful */ - if (CTX_wm_space_text(C)->live_edit) - text_run_script(C, NULL); + /* run the script while editing, evil but useful */ + if (CTX_wm_space_text(C)->live_edit) + text_run_script(C, NULL); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_paste(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Paste"; - ot->idname = "TEXT_OT_paste"; - ot->description = "Paste text from clipboard"; + /* identifiers */ + ot->name = "Paste"; + ot->idname = "TEXT_OT_paste"; + ot->description = "Paste text from clipboard"; - /* api callbacks */ - ot->exec = text_paste_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_paste_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; - /* properties */ - RNA_def_boolean(ot->srna, "selection", 0, "Selection", "Paste text selected elsewhere rather than copied (X11 only)"); + /* properties */ + RNA_def_boolean(ot->srna, + "selection", + 0, + "Selection", + "Paste text selected elsewhere rather than copied (X11 only)"); } /**************** duplicate operator *******************/ static int text_duplicate_line_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_duplicate_line(text, utxt); + txt_duplicate_line(text, utxt); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - /* run the script while editing, evil but useful */ - if (CTX_wm_space_text(C)->live_edit) { - text_run_script(C, NULL); - } + /* run the script while editing, evil but useful */ + if (CTX_wm_space_text(C)->live_edit) { + text_run_script(C, NULL); + } - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_duplicate_line(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Duplicate Line"; - ot->idname = "TEXT_OT_duplicate_line"; - ot->description = "Duplicate the current line"; + /* identifiers */ + ot->name = "Duplicate Line"; + ot->idname = "TEXT_OT_duplicate_line"; + ot->description = "Duplicate the current line"; - /* api callbacks */ - ot->exec = text_duplicate_line_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_duplicate_line_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* copy operator *********************/ static void txt_copy_clipboard(Text *text) { - char *buf; + char *buf; - if (!txt_has_sel(text)) - return; + if (!txt_has_sel(text)) + return; - buf = txt_sel_to_buf(text); + buf = txt_sel_to_buf(text); - if (buf) { - WM_clipboard_text_set(buf, 0); - MEM_freeN(buf); - } + if (buf) { + WM_clipboard_text_set(buf, 0); + MEM_freeN(buf); + } } static int text_copy_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - txt_copy_clipboard(text); + txt_copy_clipboard(text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_copy(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Copy"; - ot->idname = "TEXT_OT_copy"; - ot->description = "Copy selected text to clipboard"; + /* identifiers */ + ot->name = "Copy"; + ot->idname = "TEXT_OT_copy"; + ot->description = "Copy selected text to clipboard"; - /* api callbacks */ - ot->exec = text_copy_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_copy_exec; + ot->poll = text_edit_poll; } /******************* cut operator *********************/ static int text_cut_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - text_drawcache_tag_update(CTX_wm_space_text(C), 0); + text_drawcache_tag_update(CTX_wm_space_text(C), 0); - txt_copy_clipboard(text); + txt_copy_clipboard(text); - TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_delete_selected(text, utxt); + TextUndoBuf *utxt = ED_text_undo_push_init(C); + txt_delete_selected(text, utxt); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - /* run the script while editing, evil but useful */ - if (CTX_wm_space_text(C)->live_edit) - text_run_script(C, NULL); + /* run the script while editing, evil but useful */ + if (CTX_wm_space_text(C)->live_edit) + text_run_script(C, NULL); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_cut(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Cut"; - ot->idname = "TEXT_OT_cut"; - ot->description = "Cut selected text to clipboard"; + /* identifiers */ + ot->name = "Cut"; + ot->idname = "TEXT_OT_cut"; + ot->description = "Cut selected text to clipboard"; - /* api callbacks */ - ot->exec = text_cut_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_cut_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* indent operator *********************/ static int text_indent_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - text_drawcache_tag_update(CTX_wm_space_text(C), 0); + text_drawcache_tag_update(CTX_wm_space_text(C), 0); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - if (txt_has_sel(text)) { - txt_order_cursors(text, false); - txt_indent(text, utxt); - } - else { - txt_add_char(text, utxt, '\t'); - } + if (txt_has_sel(text)) { + txt_order_cursors(text, false); + txt_indent(text, utxt); + } + else { + txt_add_char(text, utxt, '\t'); + } - text_update_edited(text); + text_update_edited(text); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_indent(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Indent"; - ot->idname = "TEXT_OT_indent"; - ot->description = "Indent selected text"; + /* identifiers */ + ot->name = "Indent"; + ot->idname = "TEXT_OT_indent"; + ot->description = "Indent selected text"; - /* api callbacks */ - ot->exec = text_indent_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_indent_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* unindent operator *********************/ static int text_unindent_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - text_drawcache_tag_update(CTX_wm_space_text(C), 0); + text_drawcache_tag_update(CTX_wm_space_text(C), 0); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_order_cursors(text, false); - txt_unindent(text, utxt); + txt_order_cursors(text, false); + txt_unindent(text, utxt); - text_update_edited(text); + text_update_edited(text); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_unindent(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Unindent"; - ot->idname = "TEXT_OT_unindent"; - ot->description = "Unindent selected text"; + /* identifiers */ + ot->name = "Unindent"; + ot->idname = "TEXT_OT_unindent"; + ot->description = "Unindent selected text"; - /* api callbacks */ - ot->exec = text_unindent_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_unindent_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* line break operator *********************/ static int text_line_break_exec(bContext *C, wmOperator *UNUSED(op)) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - int a, curts; - int space = (text->flags & TXT_TABSTOSPACES) ? st->tabnumber : 1; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + int a, curts; + int space = (text->flags & TXT_TABSTOSPACES) ? st->tabnumber : 1; - text_drawcache_tag_update(st, 0); + text_drawcache_tag_update(st, 0); - // double check tabs/spaces before splitting the line - curts = txt_setcurr_tab_spaces(text, space); - TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_split_curline(text, utxt); + // double check tabs/spaces before splitting the line + curts = txt_setcurr_tab_spaces(text, space); + TextUndoBuf *utxt = ED_text_undo_push_init(C); + txt_split_curline(text, utxt); - for (a = 0; a < curts; a++) { - if (text->flags & TXT_TABSTOSPACES) { - txt_add_char(text, utxt, ' '); - } - else { - txt_add_char(text, utxt, '\t'); - } - } + for (a = 0; a < curts; a++) { + if (text->flags & TXT_TABSTOSPACES) { + txt_add_char(text, utxt, ' '); + } + else { + txt_add_char(text, utxt, '\t'); + } + } - if (text->curl) { - if (text->curl->prev) - text_update_line_edited(text->curl->prev); - text_update_line_edited(text->curl); - } + if (text->curl) { + if (text->curl->prev) + text_update_line_edited(text->curl->prev); + text_update_line_edited(text->curl); + } - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_line_break(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Line Break"; - ot->idname = "TEXT_OT_line_break"; - ot->description = "Insert line break at cursor position"; + /* identifiers */ + ot->name = "Line Break"; + ot->idname = "TEXT_OT_line_break"; + ot->description = "Insert line break at cursor position"; - /* api callbacks */ - ot->exec = text_line_break_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_line_break_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* comment operator *********************/ static int text_comment_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - if (txt_has_sel(text)) { - text_drawcache_tag_update(CTX_wm_space_text(C), 0); + if (txt_has_sel(text)) { + text_drawcache_tag_update(CTX_wm_space_text(C), 0); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_order_cursors(text, false); - txt_comment(text, utxt); - text_update_edited(text); + txt_order_cursors(text, false); + txt_comment(text, utxt); + text_update_edited(text); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; - } + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } void TEXT_OT_comment(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Comment"; - ot->idname = "TEXT_OT_comment"; - ot->description = "Convert selected text to comment"; + /* identifiers */ + ot->name = "Comment"; + ot->idname = "TEXT_OT_comment"; + ot->description = "Convert selected text to comment"; - /* api callbacks */ - ot->exec = text_comment_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_comment_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* uncomment operator *********************/ static int text_uncomment_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - if (txt_has_sel(text)) { - text_drawcache_tag_update(CTX_wm_space_text(C), 0); + if (txt_has_sel(text)) { + text_drawcache_tag_update(CTX_wm_space_text(C), 0); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_order_cursors(text, false); - txt_uncomment(text, utxt); - text_update_edited(text); + txt_order_cursors(text, false); + txt_uncomment(text, utxt); + text_update_edited(text); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; - } + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } void TEXT_OT_uncomment(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Uncomment"; - ot->idname = "TEXT_OT_uncomment"; - ot->description = "Convert selected comment to text"; + /* identifiers */ + ot->name = "Uncomment"; + ot->idname = "TEXT_OT_uncomment"; + ot->description = "Convert selected comment to text"; - /* api callbacks */ - ot->exec = text_uncomment_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_uncomment_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* convert whitespace operator *********************/ enum { TO_SPACES, TO_TABS }; static const EnumPropertyItem whitespace_type_items[] = { - {TO_SPACES, "SPACES", 0, "To Spaces", NULL}, - {TO_TABS, "TABS", 0, "To Tabs", NULL}, - {0, NULL, 0, NULL, NULL}, + {TO_SPACES, "SPACES", 0, "To Spaces", NULL}, + {TO_TABS, "TABS", 0, "To Tabs", NULL}, + {0, NULL, 0, NULL, NULL}, }; static int text_convert_whitespace_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - TextLine *tmp; - FlattenString fs; - size_t a, j, max_len = 0; - int type = RNA_enum_get(op->ptr, "type"); - - /* first convert to all space, this make it a lot easier to convert to tabs - * because there is no mixtures of ' ' && '\t' */ - for (tmp = text->lines.first; tmp; tmp = tmp->next) { - char *new_line; - - BLI_assert(tmp->line); - - flatten_string(st, &fs, tmp->line); - new_line = BLI_strdup(fs.buf); - flatten_string_free(&fs); - - MEM_freeN(tmp->line); - if (tmp->format) - MEM_freeN(tmp->format); - - /* Put new_line in the tmp->line spot still need to try and set the curc correctly. */ - tmp->line = new_line; - tmp->len = strlen(new_line); - tmp->format = NULL; - if (tmp->len > max_len) { - max_len = tmp->len; - } - } - - if (type == TO_TABS) { - char *tmp_line = MEM_mallocN(sizeof(*tmp_line) * (max_len + 1), __func__); - - for (tmp = text->lines.first; tmp; tmp = tmp->next) { - const char *text_check_line = tmp->line; - const int text_check_line_len = tmp->len; - char *tmp_line_cur = tmp_line; - const size_t tab_len = st->tabnumber; - - BLI_assert(text_check_line); - - for (a = 0; a < text_check_line_len;) { - /* A tab can only start at a position multiple of tab_len... */ - if (!(a % tab_len) && (text_check_line[a] == ' ')) { - /* a + 0 we already know to be ' ' char... */ - for (j = 1; (j < tab_len) && (a + j < text_check_line_len) && (text_check_line[a + j] == ' '); j++); - - if (j == tab_len) { - /* We found a set of spaces that can be replaced by a tab... */ - if ((tmp_line_cur == tmp_line) && a != 0) { - /* Copy all 'valid' string already 'parsed'... */ - memcpy(tmp_line_cur, text_check_line, a); - tmp_line_cur += a; - } - *tmp_line_cur = '\t'; - tmp_line_cur++; - a += j; - } - else { - if (tmp_line_cur != tmp_line) { - memcpy(tmp_line_cur, &text_check_line[a], j); - tmp_line_cur += j; - } - a += j; - } - } - else { - size_t len = BLI_str_utf8_size_safe(&text_check_line[a]); - if (tmp_line_cur != tmp_line) { - memcpy(tmp_line_cur, &text_check_line[a], len); - tmp_line_cur += len; - } - a += len; - } - } - - if (tmp_line_cur != tmp_line) { - *tmp_line_cur = '\0'; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + TextLine *tmp; + FlattenString fs; + size_t a, j, max_len = 0; + int type = RNA_enum_get(op->ptr, "type"); + + /* first convert to all space, this make it a lot easier to convert to tabs + * because there is no mixtures of ' ' && '\t' */ + for (tmp = text->lines.first; tmp; tmp = tmp->next) { + char *new_line; + + BLI_assert(tmp->line); + + flatten_string(st, &fs, tmp->line); + new_line = BLI_strdup(fs.buf); + flatten_string_free(&fs); + + MEM_freeN(tmp->line); + if (tmp->format) + MEM_freeN(tmp->format); + + /* Put new_line in the tmp->line spot still need to try and set the curc correctly. */ + tmp->line = new_line; + tmp->len = strlen(new_line); + tmp->format = NULL; + if (tmp->len > max_len) { + max_len = tmp->len; + } + } + + if (type == TO_TABS) { + char *tmp_line = MEM_mallocN(sizeof(*tmp_line) * (max_len + 1), __func__); + + for (tmp = text->lines.first; tmp; tmp = tmp->next) { + const char *text_check_line = tmp->line; + const int text_check_line_len = tmp->len; + char *tmp_line_cur = tmp_line; + const size_t tab_len = st->tabnumber; + + BLI_assert(text_check_line); + + for (a = 0; a < text_check_line_len;) { + /* A tab can only start at a position multiple of tab_len... */ + if (!(a % tab_len) && (text_check_line[a] == ' ')) { + /* a + 0 we already know to be ' ' char... */ + for (j = 1; + (j < tab_len) && (a + j < text_check_line_len) && (text_check_line[a + j] == ' '); + j++) + ; + + if (j == tab_len) { + /* We found a set of spaces that can be replaced by a tab... */ + if ((tmp_line_cur == tmp_line) && a != 0) { + /* Copy all 'valid' string already 'parsed'... */ + memcpy(tmp_line_cur, text_check_line, a); + tmp_line_cur += a; + } + *tmp_line_cur = '\t'; + tmp_line_cur++; + a += j; + } + else { + if (tmp_line_cur != tmp_line) { + memcpy(tmp_line_cur, &text_check_line[a], j); + tmp_line_cur += j; + } + a += j; + } + } + else { + size_t len = BLI_str_utf8_size_safe(&text_check_line[a]); + if (tmp_line_cur != tmp_line) { + memcpy(tmp_line_cur, &text_check_line[a], len); + tmp_line_cur += len; + } + a += len; + } + } + + if (tmp_line_cur != tmp_line) { + *tmp_line_cur = '\0'; #ifndef NDEBUG - BLI_assert(tmp_line_cur - tmp_line <= max_len); + BLI_assert(tmp_line_cur - tmp_line <= max_len); - flatten_string(st, &fs, tmp_line); - BLI_assert(STREQ(fs.buf, tmp->line)); - flatten_string_free(&fs); + flatten_string(st, &fs, tmp_line); + BLI_assert(STREQ(fs.buf, tmp->line)); + flatten_string_free(&fs); #endif - MEM_freeN(tmp->line); - if (tmp->format) - MEM_freeN(tmp->format); + MEM_freeN(tmp->line); + if (tmp->format) + MEM_freeN(tmp->format); - /* Put new_line in the tmp->line spot - * still need to try and set the curc correctly. */ - tmp->line = BLI_strdup(tmp_line); - tmp->len = strlen(tmp_line); - tmp->format = NULL; - } - } + /* Put new_line in the tmp->line spot + * still need to try and set the curc correctly. */ + tmp->line = BLI_strdup(tmp_line); + tmp->len = strlen(tmp_line); + tmp->format = NULL; + } + } - MEM_freeN(tmp_line); - } + MEM_freeN(tmp_line); + } - text_update_edited(text); - text_update_cursor_moved(C); - text_drawcache_tag_update(st, 1); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_edited(text); + text_update_cursor_moved(C); + text_drawcache_tag_update(st, 1); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_convert_whitespace(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Convert Whitespace"; - ot->idname = "TEXT_OT_convert_whitespace"; - ot->description = "Convert whitespaces by type"; + /* identifiers */ + ot->name = "Convert Whitespace"; + ot->idname = "TEXT_OT_convert_whitespace"; + ot->description = "Convert whitespaces by type"; - /* api callbacks */ - ot->exec = text_convert_whitespace_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_convert_whitespace_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; - /* properties */ - RNA_def_enum(ot->srna, "type", whitespace_type_items, TO_SPACES, "Type", "Type of whitespace to convert to"); + /* properties */ + RNA_def_enum(ot->srna, + "type", + whitespace_type_items, + TO_SPACES, + "Type", + "Type of whitespace to convert to"); } /******************* select all operator *********************/ static int text_select_all_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - txt_sel_all(text); + txt_sel_all(text); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_select_all(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select All"; - ot->idname = "TEXT_OT_select_all"; - ot->description = "Select all text"; + /* identifiers */ + ot->name = "Select All"; + ot->idname = "TEXT_OT_select_all"; + ot->description = "Select all text"; - /* api callbacks */ - ot->exec = text_select_all_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_select_all_exec; + ot->poll = text_edit_poll; } /******************* select line operator *********************/ static int text_select_line_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - txt_sel_line(text); + txt_sel_line(text); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_select_line(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select Line"; - ot->idname = "TEXT_OT_select_line"; - ot->description = "Select text by line"; + /* identifiers */ + ot->name = "Select Line"; + ot->idname = "TEXT_OT_select_line"; + ot->description = "Select text by line"; - /* api callbacks */ - ot->exec = text_select_line_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_select_line_exec; + ot->poll = text_edit_poll; } /******************* select word operator *********************/ static int text_select_word_exec(bContext *C, wmOperator *UNUSED(op)) { - Text *text = CTX_data_edit_text(C); - /* don't advance cursor before stepping */ - const bool use_init_step = false; + Text *text = CTX_data_edit_text(C); + /* don't advance cursor before stepping */ + const bool use_init_step = false; - txt_jump_left(text, false, use_init_step); - txt_jump_right(text, true, use_init_step); + txt_jump_left(text, false, use_init_step); + txt_jump_right(text, true, use_init_step); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_select_word(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select Word"; - ot->idname = "TEXT_OT_select_word"; - ot->description = "Select word under cursor"; + /* identifiers */ + ot->name = "Select Word"; + ot->idname = "TEXT_OT_select_word"; + ot->description = "Select word under cursor"; - /* api callbacks */ - ot->exec = text_select_word_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_select_word_exec; + ot->poll = text_edit_poll; } /********************* move lines operators ***********************/ static int move_lines_exec(bContext *C, wmOperator *op) { - Text *text = CTX_data_edit_text(C); - const int direction = RNA_enum_get(op->ptr, "direction"); + Text *text = CTX_data_edit_text(C); + const int direction = RNA_enum_get(op->ptr, "direction"); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_move_lines(text, utxt, direction); + txt_move_lines(text, utxt, direction); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - /* run the script while editing, evil but useful */ - if (CTX_wm_space_text(C)->live_edit) - text_run_script(C, NULL); + /* run the script while editing, evil but useful */ + if (CTX_wm_space_text(C)->live_edit) + text_run_script(C, NULL); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_move_lines(wmOperatorType *ot) { - static const EnumPropertyItem direction_items[] = { - {TXT_MOVE_LINE_UP, "UP", 0, "Up", ""}, - {TXT_MOVE_LINE_DOWN, "DOWN", 0, "Down", ""}, - {0, NULL, 0, NULL, NULL}, - }; + static const EnumPropertyItem direction_items[] = { + {TXT_MOVE_LINE_UP, "UP", 0, "Up", ""}, + {TXT_MOVE_LINE_DOWN, "DOWN", 0, "Down", ""}, + {0, NULL, 0, NULL, NULL}, + }; - /* identifiers */ - ot->name = "Move Lines"; - ot->idname = "TEXT_OT_move_lines"; - ot->description = "Move the currently selected line(s) up/down"; + /* identifiers */ + ot->name = "Move Lines"; + ot->idname = "TEXT_OT_move_lines"; + ot->description = "Move the currently selected line(s) up/down"; - /* api callbacks */ - ot->exec = move_lines_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = move_lines_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; - /* properties */ - RNA_def_enum(ot->srna, "direction", direction_items, 1, "Direction", ""); + /* properties */ + RNA_def_enum(ot->srna, "direction", direction_items, 1, "Direction", ""); } /************************ move operator ************************/ static const EnumPropertyItem move_type_items[] = { - {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""}, - {LINE_END, "LINE_END", 0, "Line End", ""}, - {FILE_TOP, "FILE_TOP", 0, "File Top", ""}, - {FILE_BOTTOM, "FILE_BOTTOM", 0, "File Bottom", ""}, - {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""}, - {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""}, - {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""}, - {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""}, - {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""}, - {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""}, - {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""}, - {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""}, - {0, NULL, 0, NULL, NULL}, + {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""}, + {LINE_END, "LINE_END", 0, "Line End", ""}, + {FILE_TOP, "FILE_TOP", 0, "File Top", ""}, + {FILE_BOTTOM, "FILE_BOTTOM", 0, "File Bottom", ""}, + {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""}, + {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""}, + {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""}, + {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""}, + {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""}, + {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""}, + {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""}, + {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""}, + {0, NULL, 0, NULL, NULL}, }; /* get cursor position in line by relative wrapped line and column positions */ static int text_get_cursor_rel(SpaceText *st, ARegion *ar, TextLine *linein, int rell, int relc) { - int i, j, start, end, max, chop, curs, loop, endj, found, selc; - char ch; - - max = wrap_width(st, ar); - - selc = start = endj = curs = found = 0; - end = max; - chop = loop = 1; - - for (i = 0, j = 0; loop; j += BLI_str_utf8_size_safe(linein->line + j)) { - int chars; - int columns = BLI_str_utf8_char_width_safe(linein->line + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - ch = linein->line[j]; - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - ch = ' '; - } - else { - chars = 1; - } - - while (chars--) { - if (rell == 0 && i - start <= relc && i + columns - start > relc) { - /* current position could be wrapped to next line */ - /* this should be checked when end of current line would be reached */ - selc = j; - found = 1; - } - else if (i - end <= relc && i + columns - end > relc) { - curs = j; - } - if (i + columns - start > max) { - end = MIN2(end, i); - - if (found) { - /* exact cursor position was found, check if it's */ - /* still on needed line (hasn't been wrapped) */ - if (selc > endj && !chop) selc = endj; - loop = 0; - break; - } - - if (chop) endj = j; - - start = end; - end += max; - chop = 1; - rell--; - - if (rell == 0 && i + columns - start > relc) { - selc = curs; - loop = 0; - break; - } - } - else if (ch == '\0') { - if (!found) selc = linein->len; - loop = 0; - break; - } - else if (ch == ' ' || ch == '-') { - if (found) { - loop = 0; - break; - } - - if (rell == 0 && i + columns - start > relc) { - selc = curs; - loop = 0; - break; - } - end = i + 1; - endj = j; - chop = 0; - } - i += columns; - } - } - - return selc; -} - -static int cursor_skip_find_line(SpaceText *st, ARegion *ar, - int lines, TextLine **linep, int *charp, int *rell, int *relc) -{ - int offl, offc, visible_lines; - - wrap_offset_in_line(st, ar, *linep, *charp, &offl, &offc); - *relc = text_get_char_pos(st, (*linep)->line, *charp) + offc; - *rell = lines; - - /* handle current line */ - if (lines > 0) { - visible_lines = text_get_visible_lines(st, ar, (*linep)->line); - - if (*rell - visible_lines + offl >= 0) { - if (!(*linep)->next) { - if (offl < visible_lines - 1) { - *rell = visible_lines - 1; - return 1; - } - - *charp = (*linep)->len; - return 0; - } - - *rell -= visible_lines - offl; - *linep = (*linep)->next; - } - else { - *rell += offl; - return 1; - } - } - else { - if (*rell + offl <= 0) { - if (!(*linep)->prev) { - if (offl) { - *rell = 0; - return 1; - } - - *charp = 0; - return 0; - } - - *rell += offl; - *linep = (*linep)->prev; - } - else { - *rell += offl; - return 1; - } - } - - /* skip lines and find destination line and offsets */ - while (*linep) { - visible_lines = text_get_visible_lines(st, ar, (*linep)->line); - - if (lines < 0) { /* moving top */ - if (*rell + visible_lines >= 0) { - *rell += visible_lines; - break; - } - - if (!(*linep)->prev) { - *rell = 0; - break; - } - - *rell += visible_lines; - *linep = (*linep)->prev; - } - else { /* moving bottom */ - if (*rell - visible_lines < 0) break; - - if (!(*linep)->next) { - *rell = visible_lines - 1; - break; - } - - *rell -= visible_lines; - *linep = (*linep)->next; - } - } - - return 1; + int i, j, start, end, max, chop, curs, loop, endj, found, selc; + char ch; + + max = wrap_width(st, ar); + + selc = start = endj = curs = found = 0; + end = max; + chop = loop = 1; + + for (i = 0, j = 0; loop; j += BLI_str_utf8_size_safe(linein->line + j)) { + int chars; + int columns = BLI_str_utf8_char_width_safe(linein->line + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + ch = linein->line[j]; + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + ch = ' '; + } + else { + chars = 1; + } + + while (chars--) { + if (rell == 0 && i - start <= relc && i + columns - start > relc) { + /* current position could be wrapped to next line */ + /* this should be checked when end of current line would be reached */ + selc = j; + found = 1; + } + else if (i - end <= relc && i + columns - end > relc) { + curs = j; + } + if (i + columns - start > max) { + end = MIN2(end, i); + + if (found) { + /* exact cursor position was found, check if it's */ + /* still on needed line (hasn't been wrapped) */ + if (selc > endj && !chop) + selc = endj; + loop = 0; + break; + } + + if (chop) + endj = j; + + start = end; + end += max; + chop = 1; + rell--; + + if (rell == 0 && i + columns - start > relc) { + selc = curs; + loop = 0; + break; + } + } + else if (ch == '\0') { + if (!found) + selc = linein->len; + loop = 0; + break; + } + else if (ch == ' ' || ch == '-') { + if (found) { + loop = 0; + break; + } + + if (rell == 0 && i + columns - start > relc) { + selc = curs; + loop = 0; + break; + } + end = i + 1; + endj = j; + chop = 0; + } + i += columns; + } + } + + return selc; +} + +static int cursor_skip_find_line( + SpaceText *st, ARegion *ar, int lines, TextLine **linep, int *charp, int *rell, int *relc) +{ + int offl, offc, visible_lines; + + wrap_offset_in_line(st, ar, *linep, *charp, &offl, &offc); + *relc = text_get_char_pos(st, (*linep)->line, *charp) + offc; + *rell = lines; + + /* handle current line */ + if (lines > 0) { + visible_lines = text_get_visible_lines(st, ar, (*linep)->line); + + if (*rell - visible_lines + offl >= 0) { + if (!(*linep)->next) { + if (offl < visible_lines - 1) { + *rell = visible_lines - 1; + return 1; + } + + *charp = (*linep)->len; + return 0; + } + + *rell -= visible_lines - offl; + *linep = (*linep)->next; + } + else { + *rell += offl; + return 1; + } + } + else { + if (*rell + offl <= 0) { + if (!(*linep)->prev) { + if (offl) { + *rell = 0; + return 1; + } + + *charp = 0; + return 0; + } + + *rell += offl; + *linep = (*linep)->prev; + } + else { + *rell += offl; + return 1; + } + } + + /* skip lines and find destination line and offsets */ + while (*linep) { + visible_lines = text_get_visible_lines(st, ar, (*linep)->line); + + if (lines < 0) { /* moving top */ + if (*rell + visible_lines >= 0) { + *rell += visible_lines; + break; + } + + if (!(*linep)->prev) { + *rell = 0; + break; + } + + *rell += visible_lines; + *linep = (*linep)->prev; + } + else { /* moving bottom */ + if (*rell - visible_lines < 0) + break; + + if (!(*linep)->next) { + *rell = visible_lines - 1; + break; + } + + *rell -= visible_lines; + *linep = (*linep)->next; + } + } + + return 1; } static void txt_wrap_move_bol(SpaceText *st, ARegion *ar, const bool sel) { - Text *text = st->text; - TextLine **linep; - int *charp; - int oldc, i, j, max, start, end, endj, chop, loop; - char ch; - - text_update_character_width(st); - - if (sel) { linep = &text->sell; charp = &text->selc; } - else { linep = &text->curl; charp = &text->curc; } - - oldc = *charp; - - max = wrap_width(st, ar); - - start = endj = 0; - end = max; - chop = loop = 1; - *charp = 0; - - for (i = 0, j = 0; loop; j += BLI_str_utf8_size_safe((*linep)->line + j)) { - int chars; - int columns = BLI_str_utf8_char_width_safe((*linep)->line + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - ch = (*linep)->line[j]; - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - ch = ' '; - } - else { - chars = 1; - } - - while (chars--) { - if (i + columns - start > max) { - end = MIN2(end, i); - - *charp = endj; - - if (j >= oldc) { - if (ch == '\0') *charp = txt_utf8_column_to_offset((*linep)->line, start); - loop = 0; - break; - } - - if (chop) endj = j; - - start = end; - end += max; - chop = 1; - } - else if (ch == ' ' || ch == '-' || ch == '\0') { - if (j >= oldc) { - *charp = txt_utf8_column_to_offset((*linep)->line, start); - loop = 0; - break; - } - - end = i + 1; - endj = j + 1; - chop = 0; - } - i += columns; - } - } - - if (!sel) txt_pop_sel(text); + Text *text = st->text; + TextLine **linep; + int *charp; + int oldc, i, j, max, start, end, endj, chop, loop; + char ch; + + text_update_character_width(st); + + if (sel) { + linep = &text->sell; + charp = &text->selc; + } + else { + linep = &text->curl; + charp = &text->curc; + } + + oldc = *charp; + + max = wrap_width(st, ar); + + start = endj = 0; + end = max; + chop = loop = 1; + *charp = 0; + + for (i = 0, j = 0; loop; j += BLI_str_utf8_size_safe((*linep)->line + j)) { + int chars; + int columns = BLI_str_utf8_char_width_safe((*linep)->line + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + ch = (*linep)->line[j]; + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + ch = ' '; + } + else { + chars = 1; + } + + while (chars--) { + if (i + columns - start > max) { + end = MIN2(end, i); + + *charp = endj; + + if (j >= oldc) { + if (ch == '\0') + *charp = txt_utf8_column_to_offset((*linep)->line, start); + loop = 0; + break; + } + + if (chop) + endj = j; + + start = end; + end += max; + chop = 1; + } + else if (ch == ' ' || ch == '-' || ch == '\0') { + if (j >= oldc) { + *charp = txt_utf8_column_to_offset((*linep)->line, start); + loop = 0; + break; + } + + end = i + 1; + endj = j + 1; + chop = 0; + } + i += columns; + } + } + + if (!sel) + txt_pop_sel(text); } static void txt_wrap_move_eol(SpaceText *st, ARegion *ar, const bool sel) { - Text *text = st->text; - TextLine **linep; - int *charp; - int oldc, i, j, max, start, end, endj, chop, loop; - char ch; - - text_update_character_width(st); - - if (sel) { linep = &text->sell; charp = &text->selc; } - else { linep = &text->curl; charp = &text->curc; } - - oldc = *charp; - - max = wrap_width(st, ar); - - start = endj = 0; - end = max; - chop = loop = 1; - *charp = 0; - - for (i = 0, j = 0; loop; j += BLI_str_utf8_size_safe((*linep)->line + j)) { - int chars; - int columns = BLI_str_utf8_char_width_safe((*linep)->line + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - ch = (*linep)->line[j]; - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - ch = ' '; - } - else { - chars = 1; - } - - while (chars--) { - if (i + columns - start > max) { - end = MIN2(end, i); - - if (chop) endj = BLI_str_prev_char_utf8((*linep)->line + j) - (*linep)->line; - - if (endj >= oldc) { - if (ch == '\0') *charp = (*linep)->len; - else *charp = endj; - loop = 0; - break; - } - - start = end; - end += max; - chop = 1; - } - else if (ch == '\0') { - *charp = (*linep)->len; - loop = 0; - break; - } - else if (ch == ' ' || ch == '-') { - end = i + 1; - endj = j; - chop = 0; - } - i += columns; - } - } - - if (!sel) txt_pop_sel(text); + Text *text = st->text; + TextLine **linep; + int *charp; + int oldc, i, j, max, start, end, endj, chop, loop; + char ch; + + text_update_character_width(st); + + if (sel) { + linep = &text->sell; + charp = &text->selc; + } + else { + linep = &text->curl; + charp = &text->curc; + } + + oldc = *charp; + + max = wrap_width(st, ar); + + start = endj = 0; + end = max; + chop = loop = 1; + *charp = 0; + + for (i = 0, j = 0; loop; j += BLI_str_utf8_size_safe((*linep)->line + j)) { + int chars; + int columns = BLI_str_utf8_char_width_safe((*linep)->line + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + ch = (*linep)->line[j]; + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + ch = ' '; + } + else { + chars = 1; + } + + while (chars--) { + if (i + columns - start > max) { + end = MIN2(end, i); + + if (chop) + endj = BLI_str_prev_char_utf8((*linep)->line + j) - (*linep)->line; + + if (endj >= oldc) { + if (ch == '\0') + *charp = (*linep)->len; + else + *charp = endj; + loop = 0; + break; + } + + start = end; + end += max; + chop = 1; + } + else if (ch == '\0') { + *charp = (*linep)->len; + loop = 0; + break; + } + else if (ch == ' ' || ch == '-') { + end = i + 1; + endj = j; + chop = 0; + } + i += columns; + } + } + + if (!sel) + txt_pop_sel(text); } static void txt_wrap_move_up(SpaceText *st, ARegion *ar, const bool sel) { - Text *text = st->text; - TextLine **linep; - int *charp; - int offl, offc, col; - - text_update_character_width(st); - - if (sel) { linep = &text->sell; charp = &text->selc; } - else { linep = &text->curl; charp = &text->curc; } - - wrap_offset_in_line(st, ar, *linep, *charp, &offl, &offc); - col = text_get_char_pos(st, (*linep)->line, *charp) + offc; - if (offl) { - *charp = text_get_cursor_rel(st, ar, *linep, offl - 1, col); - } - else { - if ((*linep)->prev) { - int visible_lines; - - *linep = (*linep)->prev; - visible_lines = text_get_visible_lines(st, ar, (*linep)->line); - *charp = text_get_cursor_rel(st, ar, *linep, visible_lines - 1, col); - } - else { - *charp = 0; - } - } - - if (!sel) txt_pop_sel(text); + Text *text = st->text; + TextLine **linep; + int *charp; + int offl, offc, col; + + text_update_character_width(st); + + if (sel) { + linep = &text->sell; + charp = &text->selc; + } + else { + linep = &text->curl; + charp = &text->curc; + } + + wrap_offset_in_line(st, ar, *linep, *charp, &offl, &offc); + col = text_get_char_pos(st, (*linep)->line, *charp) + offc; + if (offl) { + *charp = text_get_cursor_rel(st, ar, *linep, offl - 1, col); + } + else { + if ((*linep)->prev) { + int visible_lines; + + *linep = (*linep)->prev; + visible_lines = text_get_visible_lines(st, ar, (*linep)->line); + *charp = text_get_cursor_rel(st, ar, *linep, visible_lines - 1, col); + } + else { + *charp = 0; + } + } + + if (!sel) + txt_pop_sel(text); } static void txt_wrap_move_down(SpaceText *st, ARegion *ar, const bool sel) { - Text *text = st->text; - TextLine **linep; - int *charp; - int offl, offc, col, visible_lines; - - text_update_character_width(st); - - if (sel) { linep = &text->sell; charp = &text->selc; } - else { linep = &text->curl; charp = &text->curc; } - - wrap_offset_in_line(st, ar, *linep, *charp, &offl, &offc); - col = text_get_char_pos(st, (*linep)->line, *charp) + offc; - visible_lines = text_get_visible_lines(st, ar, (*linep)->line); - if (offl < visible_lines - 1) { - *charp = text_get_cursor_rel(st, ar, *linep, offl + 1, col); - } - else { - if ((*linep)->next) { - *linep = (*linep)->next; - *charp = text_get_cursor_rel(st, ar, *linep, 0, col); - } - else { - *charp = (*linep)->len; - } - } - - if (!sel) txt_pop_sel(text); + Text *text = st->text; + TextLine **linep; + int *charp; + int offl, offc, col, visible_lines; + + text_update_character_width(st); + + if (sel) { + linep = &text->sell; + charp = &text->selc; + } + else { + linep = &text->curl; + charp = &text->curc; + } + + wrap_offset_in_line(st, ar, *linep, *charp, &offl, &offc); + col = text_get_char_pos(st, (*linep)->line, *charp) + offc; + visible_lines = text_get_visible_lines(st, ar, (*linep)->line); + if (offl < visible_lines - 1) { + *charp = text_get_cursor_rel(st, ar, *linep, offl + 1, col); + } + else { + if ((*linep)->next) { + *linep = (*linep)->next; + *charp = text_get_cursor_rel(st, ar, *linep, 0, col); + } + else { + *charp = (*linep)->len; + } + } + + if (!sel) + txt_pop_sel(text); } /* Moves the cursor vertically by the specified number of lines. @@ -1749,1524 +1814,1602 @@ static void txt_wrap_move_down(SpaceText *st, ARegion *ar, const bool sel) */ static void cursor_skip(SpaceText *st, ARegion *ar, Text *text, int lines, const bool sel) { - TextLine **linep; - int *charp; - - if (sel) { linep = &text->sell; charp = &text->selc; } - else { linep = &text->curl; charp = &text->curc; } - - if (st && ar && st->wordwrap) { - int rell, relc; - - /* find line and offsets inside it needed to set cursor position */ - if (cursor_skip_find_line(st, ar, lines, linep, charp, &rell, &relc)) - *charp = text_get_cursor_rel(st, ar, *linep, rell, relc); - } - else { - while (lines > 0 && (*linep)->next) { - *linep = (*linep)->next; - lines--; - } - while (lines < 0 && (*linep)->prev) { - *linep = (*linep)->prev; - lines++; - } - } - - if (*charp > (*linep)->len) *charp = (*linep)->len; - - if (!sel) txt_pop_sel(text); + TextLine **linep; + int *charp; + + if (sel) { + linep = &text->sell; + charp = &text->selc; + } + else { + linep = &text->curl; + charp = &text->curc; + } + + if (st && ar && st->wordwrap) { + int rell, relc; + + /* find line and offsets inside it needed to set cursor position */ + if (cursor_skip_find_line(st, ar, lines, linep, charp, &rell, &relc)) + *charp = text_get_cursor_rel(st, ar, *linep, rell, relc); + } + else { + while (lines > 0 && (*linep)->next) { + *linep = (*linep)->next; + lines--; + } + while (lines < 0 && (*linep)->prev) { + *linep = (*linep)->prev; + lines++; + } + } + + if (*charp > (*linep)->len) + *charp = (*linep)->len; + + if (!sel) + txt_pop_sel(text); } static int text_move_cursor(bContext *C, int type, bool select) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - ARegion *ar = CTX_wm_region(C); - - /* ensure we have the right region, it's optional */ - if (ar && ar->regiontype != RGN_TYPE_WINDOW) - ar = NULL; - - switch (type) { - case LINE_BEGIN: - if (!select) { - txt_sel_clear(text); - } - if (st && st->wordwrap && ar) txt_wrap_move_bol(st, ar, select); - else txt_move_bol(text, select); - break; - - case LINE_END: - if (!select) { - txt_sel_clear(text); - } - if (st && st->wordwrap && ar) txt_wrap_move_eol(st, ar, select); - else txt_move_eol(text, select); - break; - - case FILE_TOP: - txt_move_bof(text, select); - break; - - case FILE_BOTTOM: - txt_move_eof(text, select); - break; - - case PREV_WORD: - if (txt_cursor_is_line_start(text)) { - txt_move_left(text, select); - } - txt_jump_left(text, select, true); - break; - - case NEXT_WORD: - if (txt_cursor_is_line_end(text)) { - txt_move_right(text, select); - } - txt_jump_right(text, select, true); - break; - - case PREV_CHAR: - if (txt_has_sel(text) && !select) { - txt_order_cursors(text, false); - txt_pop_sel(text); - } - else { - txt_move_left(text, select); - } - break; - - case NEXT_CHAR: - if (txt_has_sel(text) && !select) { - txt_order_cursors(text, true); - txt_pop_sel(text); - } - else { - txt_move_right(text, select); - } - break; - - case PREV_LINE: - if (st && st->wordwrap && ar) txt_wrap_move_up(st, ar, select); - else txt_move_up(text, select); - break; - - case NEXT_LINE: - if (st && st->wordwrap && ar) txt_wrap_move_down(st, ar, select); - else txt_move_down(text, select); - break; - - case PREV_PAGE: - if (st) cursor_skip(st, ar, st->text, -st->viewlines, select); - else cursor_skip(NULL, NULL, text, -10, select); - break; - - case NEXT_PAGE: - if (st) cursor_skip(st, ar, st->text, st->viewlines, select); - else cursor_skip(NULL, NULL, text, 10, select); - break; - } - - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); - - return OPERATOR_FINISHED; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + ARegion *ar = CTX_wm_region(C); + + /* ensure we have the right region, it's optional */ + if (ar && ar->regiontype != RGN_TYPE_WINDOW) + ar = NULL; + + switch (type) { + case LINE_BEGIN: + if (!select) { + txt_sel_clear(text); + } + if (st && st->wordwrap && ar) + txt_wrap_move_bol(st, ar, select); + else + txt_move_bol(text, select); + break; + + case LINE_END: + if (!select) { + txt_sel_clear(text); + } + if (st && st->wordwrap && ar) + txt_wrap_move_eol(st, ar, select); + else + txt_move_eol(text, select); + break; + + case FILE_TOP: + txt_move_bof(text, select); + break; + + case FILE_BOTTOM: + txt_move_eof(text, select); + break; + + case PREV_WORD: + if (txt_cursor_is_line_start(text)) { + txt_move_left(text, select); + } + txt_jump_left(text, select, true); + break; + + case NEXT_WORD: + if (txt_cursor_is_line_end(text)) { + txt_move_right(text, select); + } + txt_jump_right(text, select, true); + break; + + case PREV_CHAR: + if (txt_has_sel(text) && !select) { + txt_order_cursors(text, false); + txt_pop_sel(text); + } + else { + txt_move_left(text, select); + } + break; + + case NEXT_CHAR: + if (txt_has_sel(text) && !select) { + txt_order_cursors(text, true); + txt_pop_sel(text); + } + else { + txt_move_right(text, select); + } + break; + + case PREV_LINE: + if (st && st->wordwrap && ar) + txt_wrap_move_up(st, ar, select); + else + txt_move_up(text, select); + break; + + case NEXT_LINE: + if (st && st->wordwrap && ar) + txt_wrap_move_down(st, ar, select); + else + txt_move_down(text, select); + break; + + case PREV_PAGE: + if (st) + cursor_skip(st, ar, st->text, -st->viewlines, select); + else + cursor_skip(NULL, NULL, text, -10, select); + break; + + case NEXT_PAGE: + if (st) + cursor_skip(st, ar, st->text, st->viewlines, select); + else + cursor_skip(NULL, NULL, text, 10, select); + break; + } + + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); + + return OPERATOR_FINISHED; } static int text_move_exec(bContext *C, wmOperator *op) { - int type = RNA_enum_get(op->ptr, "type"); + int type = RNA_enum_get(op->ptr, "type"); - return text_move_cursor(C, type, 0); + return text_move_cursor(C, type, 0); } void TEXT_OT_move(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Move Cursor"; - ot->idname = "TEXT_OT_move"; - ot->description = "Move cursor to position type"; + /* identifiers */ + ot->name = "Move Cursor"; + ot->idname = "TEXT_OT_move"; + ot->description = "Move cursor to position type"; - /* api callbacks */ - ot->exec = text_move_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_move_exec; + ot->poll = text_edit_poll; - /* properties */ - RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to"); + /* properties */ + RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to"); } /******************* move select operator ********************/ static int text_move_select_exec(bContext *C, wmOperator *op) { - int type = RNA_enum_get(op->ptr, "type"); + int type = RNA_enum_get(op->ptr, "type"); - return text_move_cursor(C, type, 1); + return text_move_cursor(C, type, 1); } void TEXT_OT_move_select(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Move Select"; - ot->idname = "TEXT_OT_move_select"; - ot->description = "Move the cursor while selecting"; + /* identifiers */ + ot->name = "Move Select"; + ot->idname = "TEXT_OT_move_select"; + ot->description = "Move the cursor while selecting"; - /* api callbacks */ - ot->exec = text_move_select_exec; - ot->poll = text_space_edit_poll; + /* api callbacks */ + ot->exec = text_move_select_exec; + ot->poll = text_space_edit_poll; - /* properties */ - RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to, to make a selection"); + /* properties */ + RNA_def_enum(ot->srna, + "type", + move_type_items, + LINE_BEGIN, + "Type", + "Where to move cursor to, to make a selection"); } /******************* jump operator *********************/ static int text_jump_exec(bContext *C, wmOperator *op) { - Text *text = CTX_data_edit_text(C); - int line = RNA_int_get(op->ptr, "line"); - short nlines = txt_get_span(text->lines.first, text->lines.last) + 1; + Text *text = CTX_data_edit_text(C); + int line = RNA_int_get(op->ptr, "line"); + short nlines = txt_get_span(text->lines.first, text->lines.last) + 1; - if (line < 1) - txt_move_toline(text, 1, 0); - else if (line > nlines) - txt_move_toline(text, nlines - 1, 0); - else - txt_move_toline(text, line - 1, 0); + if (line < 1) + txt_move_toline(text, 1, 0); + else if (line > nlines) + txt_move_toline(text, nlines - 1, 0); + else + txt_move_toline(text, line - 1, 0); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } static int text_jump_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - return WM_operator_props_dialog_popup(C, op, 200, 100); - + return WM_operator_props_dialog_popup(C, op, 200, 100); } void TEXT_OT_jump(wmOperatorType *ot) { - PropertyRNA *prop; + PropertyRNA *prop; - /* identifiers */ - ot->name = "Jump"; - ot->idname = "TEXT_OT_jump"; - ot->description = "Jump cursor to line"; + /* identifiers */ + ot->name = "Jump"; + ot->idname = "TEXT_OT_jump"; + ot->description = "Jump cursor to line"; - /* api callbacks */ - ot->invoke = text_jump_invoke; - ot->exec = text_jump_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->invoke = text_jump_invoke; + ot->exec = text_jump_exec; + ot->poll = text_edit_poll; - /* properties */ - prop = RNA_def_int(ot->srna, "line", 1, 1, INT_MAX, "Line", "Line number to jump to", 1, 10000); - RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_TEXT); + /* properties */ + prop = RNA_def_int(ot->srna, "line", 1, 1, INT_MAX, "Line", "Line number to jump to", 1, 10000); + RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_TEXT); } /******************* delete operator **********************/ static const EnumPropertyItem delete_type_items[] = { - {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""}, - {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""}, - {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""}, - {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""}, - {0, NULL, 0, NULL, NULL}, + {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""}, + {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""}, + {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""}, + {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""}, + {0, NULL, 0, NULL, NULL}, }; static int text_delete_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - int type = RNA_enum_get(op->ptr, "type"); - - - text_drawcache_tag_update(st, 0); - - /* behavior could be changed here, - * but for now just don't jump words when we have a selection */ - if (txt_has_sel(text)) { - if (type == DEL_PREV_WORD) type = DEL_PREV_CHAR; - else if (type == DEL_NEXT_WORD) type = DEL_NEXT_CHAR; - } - - TextUndoBuf *utxt = ED_text_undo_push_init(C); - - if (type == DEL_PREV_WORD) { - if (txt_cursor_is_line_start(text)) { - txt_backspace_char(text, utxt); - } - txt_backspace_word(text, utxt); - } - else if (type == DEL_PREV_CHAR) { - - if (text->flags & TXT_TABSTOSPACES) { - if (!txt_has_sel(text) && !txt_cursor_is_line_start(text)) { - int tabsize = 0; - tabsize = txt_calc_tab_left(text->curl, text->curc); - if (tabsize) { - text->sell = text->curl; - text->selc = text->curc - tabsize; - txt_order_cursors(text, false); - } - } - } - - txt_backspace_char(text, utxt); - } - else if (type == DEL_NEXT_WORD) { - if (txt_cursor_is_line_end(text)) { - txt_delete_char(text, utxt); - } - txt_delete_word(text, utxt); - } - else if (type == DEL_NEXT_CHAR) { - - if (text->flags & TXT_TABSTOSPACES) { - if (!txt_has_sel(text) && !txt_cursor_is_line_end(text)) { - int tabsize = 0; - tabsize = txt_calc_tab_right(text->curl, text->curc); - if (tabsize) { - text->sell = text->curl; - text->selc = text->curc + tabsize; - txt_order_cursors(text, true); - } - } - } - - txt_delete_char(text, utxt); - } - - text_update_line_edited(text->curl); - - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - - /* run the script while editing, evil but useful */ - if (st->live_edit) - text_run_script(C, NULL); - - return OPERATOR_FINISHED; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + int type = RNA_enum_get(op->ptr, "type"); + + text_drawcache_tag_update(st, 0); + + /* behavior could be changed here, + * but for now just don't jump words when we have a selection */ + if (txt_has_sel(text)) { + if (type == DEL_PREV_WORD) + type = DEL_PREV_CHAR; + else if (type == DEL_NEXT_WORD) + type = DEL_NEXT_CHAR; + } + + TextUndoBuf *utxt = ED_text_undo_push_init(C); + + if (type == DEL_PREV_WORD) { + if (txt_cursor_is_line_start(text)) { + txt_backspace_char(text, utxt); + } + txt_backspace_word(text, utxt); + } + else if (type == DEL_PREV_CHAR) { + + if (text->flags & TXT_TABSTOSPACES) { + if (!txt_has_sel(text) && !txt_cursor_is_line_start(text)) { + int tabsize = 0; + tabsize = txt_calc_tab_left(text->curl, text->curc); + if (tabsize) { + text->sell = text->curl; + text->selc = text->curc - tabsize; + txt_order_cursors(text, false); + } + } + } + + txt_backspace_char(text, utxt); + } + else if (type == DEL_NEXT_WORD) { + if (txt_cursor_is_line_end(text)) { + txt_delete_char(text, utxt); + } + txt_delete_word(text, utxt); + } + else if (type == DEL_NEXT_CHAR) { + + if (text->flags & TXT_TABSTOSPACES) { + if (!txt_has_sel(text) && !txt_cursor_is_line_end(text)) { + int tabsize = 0; + tabsize = txt_calc_tab_right(text->curl, text->curc); + if (tabsize) { + text->sell = text->curl; + text->selc = text->curc + tabsize; + txt_order_cursors(text, true); + } + } + } + + txt_delete_char(text, utxt); + } + + text_update_line_edited(text->curl); + + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + + /* run the script while editing, evil but useful */ + if (st->live_edit) + text_run_script(C, NULL); + + return OPERATOR_FINISHED; } void TEXT_OT_delete(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Delete"; - ot->idname = "TEXT_OT_delete"; - ot->description = "Delete text by cursor position"; + /* identifiers */ + ot->name = "Delete"; + ot->idname = "TEXT_OT_delete"; + ot->description = "Delete text by cursor position"; - /* api callbacks */ - ot->exec = text_delete_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_delete_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; - /* properties */ - PropertyRNA *prop; - prop = RNA_def_enum(ot->srna, "type", delete_type_items, DEL_NEXT_CHAR, "Type", "Which part of the text to delete"); - RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); + /* properties */ + PropertyRNA *prop; + prop = RNA_def_enum(ot->srna, + "type", + delete_type_items, + DEL_NEXT_CHAR, + "Type", + "Which part of the text to delete"); + RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); } /******************* toggle overwrite operator **********************/ static int text_toggle_overwrite_exec(bContext *C, wmOperator *UNUSED(op)) { - SpaceText *st = CTX_wm_space_text(C); + SpaceText *st = CTX_wm_space_text(C); - st->overwrite = !st->overwrite; + st->overwrite = !st->overwrite; - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_overwrite_toggle(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Toggle Overwrite"; - ot->idname = "TEXT_OT_overwrite_toggle"; - ot->description = "Toggle overwrite while typing"; + /* identifiers */ + ot->name = "Toggle Overwrite"; + ot->idname = "TEXT_OT_overwrite_toggle"; + ot->description = "Toggle overwrite while typing"; - /* api callbacks */ - ot->exec = text_toggle_overwrite_exec; - ot->poll = text_space_edit_poll; + /* api callbacks */ + ot->exec = text_toggle_overwrite_exec; + ot->poll = text_space_edit_poll; } /******************* scroll operator **********************/ static void txt_screen_clamp(SpaceText *st, ARegion *ar) { - if (st->top <= 0) { - st->top = 0; - } - else { - int last; - last = text_get_total_lines(st, ar); - last = last - (st->viewlines / 2); - if (last > 0 && st->top > last) { - st->top = last; - } - } + if (st->top <= 0) { + st->top = 0; + } + else { + int last; + last = text_get_total_lines(st, ar); + last = last - (st->viewlines / 2); + if (last > 0 && st->top > last) { + st->top = last; + } + } } /* Moves the view vertically by the specified number of lines */ static void txt_screen_skip(SpaceText *st, ARegion *ar, int lines) { - st->top += lines; - txt_screen_clamp(st, ar); + st->top += lines; + txt_screen_clamp(st, ar); } /* quick enum for tsc->zone (scroller handles) */ enum { - SCROLLHANDLE_BAR, - SCROLLHANDLE_MIN_OUTSIDE, - SCROLLHANDLE_MAX_OUTSIDE, + SCROLLHANDLE_BAR, + SCROLLHANDLE_MIN_OUTSIDE, + SCROLLHANDLE_MAX_OUTSIDE, }; typedef struct TextScroll { - int old[2]; - int delta[2]; + int old[2]; + int delta[2]; - int first; - int scrollbar; + int first; + int scrollbar; - int zone; + int zone; } TextScroll; static bool text_scroll_poll(bContext *C) { - /* it should be possible to still scroll linked texts to read them, - * even if they can't be edited... */ - return CTX_data_edit_text(C) != NULL; + /* it should be possible to still scroll linked texts to read them, + * even if they can't be edited... */ + return CTX_data_edit_text(C) != NULL; } static int text_scroll_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - ARegion *ar = CTX_wm_region(C); + SpaceText *st = CTX_wm_space_text(C); + ARegion *ar = CTX_wm_region(C); - int lines = RNA_int_get(op->ptr, "lines"); + int lines = RNA_int_get(op->ptr, "lines"); - if (lines == 0) - return OPERATOR_CANCELLED; + if (lines == 0) + return OPERATOR_CANCELLED; - txt_screen_skip(st, ar, lines * U.wheellinescroll); + txt_screen_skip(st, ar, lines * U.wheellinescroll); - ED_area_tag_redraw(CTX_wm_area(C)); + ED_area_tag_redraw(CTX_wm_area(C)); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } static void text_scroll_apply(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - ARegion *ar = CTX_wm_region(C); - TextScroll *tsc = op->customdata; - int mval[2] = {event->x, event->y}; - int scroll_steps[2] = {0, 0}; - - text_update_character_width(st); - - /* compute mouse move distance */ - if (tsc->first) { - tsc->old[0] = mval[0]; - tsc->old[1] = mval[1]; - tsc->first = 0; - } - - if (event->type != MOUSEPAN) { - tsc->delta[0] = mval[0] - tsc->old[0]; - tsc->delta[1] = mval[1] - tsc->old[1]; - } - - /* accumulate scroll, in float values for events that give less than one - * line offset but taken together should still scroll */ - if (!tsc->scrollbar) { - st->scroll_accum[0] += -tsc->delta[0] / (float)st->cwidth; - st->scroll_accum[1] += tsc->delta[1] / (float)(st->lheight_dpi + TXT_LINE_SPACING); - } - else { - st->scroll_accum[1] += -tsc->delta[1] * st->pix_per_line; - } - - /* round to number of lines to scroll */ - scroll_steps[0] = (int)st->scroll_accum[0]; - scroll_steps[1] = (int)st->scroll_accum[1]; - - st->scroll_accum[0] -= scroll_steps[0]; - st->scroll_accum[1] -= scroll_steps[1]; - - /* perform vertical and/or horizontal scroll */ - if (scroll_steps[0] || scroll_steps[1]) { - txt_screen_skip(st, ar, scroll_steps[1]); - - if (st->wordwrap) { - st->left = 0; - } - else { - st->left += scroll_steps[0]; - if (st->left < 0) st->left = 0; - } - - ED_area_tag_redraw(CTX_wm_area(C)); - } - - tsc->old[0] = mval[0]; - tsc->old[1] = mval[1]; + SpaceText *st = CTX_wm_space_text(C); + ARegion *ar = CTX_wm_region(C); + TextScroll *tsc = op->customdata; + int mval[2] = {event->x, event->y}; + int scroll_steps[2] = {0, 0}; + + text_update_character_width(st); + + /* compute mouse move distance */ + if (tsc->first) { + tsc->old[0] = mval[0]; + tsc->old[1] = mval[1]; + tsc->first = 0; + } + + if (event->type != MOUSEPAN) { + tsc->delta[0] = mval[0] - tsc->old[0]; + tsc->delta[1] = mval[1] - tsc->old[1]; + } + + /* accumulate scroll, in float values for events that give less than one + * line offset but taken together should still scroll */ + if (!tsc->scrollbar) { + st->scroll_accum[0] += -tsc->delta[0] / (float)st->cwidth; + st->scroll_accum[1] += tsc->delta[1] / (float)(st->lheight_dpi + TXT_LINE_SPACING); + } + else { + st->scroll_accum[1] += -tsc->delta[1] * st->pix_per_line; + } + + /* round to number of lines to scroll */ + scroll_steps[0] = (int)st->scroll_accum[0]; + scroll_steps[1] = (int)st->scroll_accum[1]; + + st->scroll_accum[0] -= scroll_steps[0]; + st->scroll_accum[1] -= scroll_steps[1]; + + /* perform vertical and/or horizontal scroll */ + if (scroll_steps[0] || scroll_steps[1]) { + txt_screen_skip(st, ar, scroll_steps[1]); + + if (st->wordwrap) { + st->left = 0; + } + else { + st->left += scroll_steps[0]; + if (st->left < 0) + st->left = 0; + } + + ED_area_tag_redraw(CTX_wm_area(C)); + } + + tsc->old[0] = mval[0]; + tsc->old[1] = mval[1]; } static void scroll_exit(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); + SpaceText *st = CTX_wm_space_text(C); - st->flags &= ~ST_SCROLL_SELECT; - MEM_freeN(op->customdata); + st->flags &= ~ST_SCROLL_SELECT; + MEM_freeN(op->customdata); } static int text_scroll_modal(bContext *C, wmOperator *op, const wmEvent *event) { - TextScroll *tsc = op->customdata; - SpaceText *st = CTX_wm_space_text(C); - ARegion *ar = CTX_wm_region(C); - - switch (event->type) { - case MOUSEMOVE: - if (tsc->zone == SCROLLHANDLE_BAR) - text_scroll_apply(C, op, event); - break; - case LEFTMOUSE: - case RIGHTMOUSE: - case MIDDLEMOUSE: - if (event->val == KM_RELEASE) { - if (ELEM(tsc->zone, SCROLLHANDLE_MIN_OUTSIDE, SCROLLHANDLE_MAX_OUTSIDE)) { - txt_screen_skip(st, ar, st->viewlines * (tsc->zone == SCROLLHANDLE_MIN_OUTSIDE ? 1 : -1)); - - ED_area_tag_redraw(CTX_wm_area(C)); - } - scroll_exit(C, op); - return OPERATOR_FINISHED; - } - } - - return OPERATOR_RUNNING_MODAL; + TextScroll *tsc = op->customdata; + SpaceText *st = CTX_wm_space_text(C); + ARegion *ar = CTX_wm_region(C); + + switch (event->type) { + case MOUSEMOVE: + if (tsc->zone == SCROLLHANDLE_BAR) + text_scroll_apply(C, op, event); + break; + case LEFTMOUSE: + case RIGHTMOUSE: + case MIDDLEMOUSE: + if (event->val == KM_RELEASE) { + if (ELEM(tsc->zone, SCROLLHANDLE_MIN_OUTSIDE, SCROLLHANDLE_MAX_OUTSIDE)) { + txt_screen_skip( + st, ar, st->viewlines * (tsc->zone == SCROLLHANDLE_MIN_OUTSIDE ? 1 : -1)); + + ED_area_tag_redraw(CTX_wm_area(C)); + } + scroll_exit(C, op); + return OPERATOR_FINISHED; + } + } + + return OPERATOR_RUNNING_MODAL; } static void text_scroll_cancel(bContext *C, wmOperator *op) { - scroll_exit(C, op); + scroll_exit(C, op); } static int text_scroll_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - TextScroll *tsc; + SpaceText *st = CTX_wm_space_text(C); + TextScroll *tsc; - if (RNA_struct_property_is_set(op->ptr, "lines")) - return text_scroll_exec(C, op); + if (RNA_struct_property_is_set(op->ptr, "lines")) + return text_scroll_exec(C, op); - tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); - tsc->first = 1; - tsc->zone = SCROLLHANDLE_BAR; - op->customdata = tsc; + tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); + tsc->first = 1; + tsc->zone = SCROLLHANDLE_BAR; + op->customdata = tsc; - st->flags |= ST_SCROLL_SELECT; + st->flags |= ST_SCROLL_SELECT; - if (event->type == MOUSEPAN) { - text_update_character_width(st); + if (event->type == MOUSEPAN) { + text_update_character_width(st); - tsc->old[0] = event->x; - tsc->old[1] = event->y; - /* Sensitivity of scroll set to 4pix per line/char */ - tsc->delta[0] = (event->x - event->prevx) * st->cwidth / 4; - tsc->delta[1] = (event->y - event->prevy) * st->lheight_dpi / 4; - tsc->first = 0; - tsc->scrollbar = 0; - text_scroll_apply(C, op, event); - scroll_exit(C, op); - return OPERATOR_FINISHED; - } + tsc->old[0] = event->x; + tsc->old[1] = event->y; + /* Sensitivity of scroll set to 4pix per line/char */ + tsc->delta[0] = (event->x - event->prevx) * st->cwidth / 4; + tsc->delta[1] = (event->y - event->prevy) * st->lheight_dpi / 4; + tsc->first = 0; + tsc->scrollbar = 0; + text_scroll_apply(C, op, event); + scroll_exit(C, op); + return OPERATOR_FINISHED; + } - WM_event_add_modal_handler(C, op); + WM_event_add_modal_handler(C, op); - return OPERATOR_RUNNING_MODAL; + return OPERATOR_RUNNING_MODAL; } void TEXT_OT_scroll(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Scroll"; - /* don't really see the difference between this and - * scroll_bar. Both do basically the same thing (aside - * from keymaps).*/ - ot->idname = "TEXT_OT_scroll"; + /* identifiers */ + ot->name = "Scroll"; + /* don't really see the difference between this and + * scroll_bar. Both do basically the same thing (aside + * from keymaps).*/ + ot->idname = "TEXT_OT_scroll"; - /* api callbacks */ - ot->exec = text_scroll_exec; - ot->invoke = text_scroll_invoke; - ot->modal = text_scroll_modal; - ot->cancel = text_scroll_cancel; - ot->poll = text_scroll_poll; + /* api callbacks */ + ot->exec = text_scroll_exec; + ot->invoke = text_scroll_invoke; + ot->modal = text_scroll_modal; + ot->cancel = text_scroll_cancel; + ot->poll = text_scroll_poll; - /* flags */ - ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR | OPTYPE_INTERNAL; + /* flags */ + ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR | OPTYPE_INTERNAL; - /* properties */ - RNA_def_int(ot->srna, "lines", 1, INT_MIN, INT_MAX, "Lines", "Number of lines to scroll", -100, 100); + /* properties */ + RNA_def_int( + ot->srna, "lines", 1, INT_MIN, INT_MAX, "Lines", "Number of lines to scroll", -100, 100); } /******************** scroll bar operator *******************/ static bool text_region_scroll_poll(bContext *C) { - /* same as text_region_edit_poll except it works on libdata too */ - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - ARegion *ar = CTX_wm_region(C); + /* same as text_region_edit_poll except it works on libdata too */ + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + ARegion *ar = CTX_wm_region(C); - if (!st || !text) - return 0; + if (!st || !text) + return 0; - if (!ar || ar->regiontype != RGN_TYPE_WINDOW) - return 0; + if (!ar || ar->regiontype != RGN_TYPE_WINDOW) + return 0; - return 1; + return 1; } static int text_scroll_bar_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - ARegion *ar = CTX_wm_region(C); - TextScroll *tsc; - const int *mval = event->mval; - int zone = -1; - - if (RNA_struct_property_is_set(op->ptr, "lines")) - return text_scroll_exec(C, op); - - /* verify we are in the right zone */ - if (mval[0] > st->txtbar.xmin && mval[0] < st->txtbar.xmax) { - if (mval[1] >= st->txtbar.ymin && mval[1] <= st->txtbar.ymax) { - /* mouse inside scroll handle */ - zone = SCROLLHANDLE_BAR; - } - else if (mval[1] > TXT_SCROLL_SPACE && mval[1] < ar->winy - TXT_SCROLL_SPACE) { - if (mval[1] < st->txtbar.ymin) zone = SCROLLHANDLE_MIN_OUTSIDE; - else zone = SCROLLHANDLE_MAX_OUTSIDE; - } - } - - if (zone == -1) { - /* we are outside slider - nothing to do */ - return OPERATOR_PASS_THROUGH; - } - - tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); - tsc->first = 1; - tsc->scrollbar = 1; - tsc->zone = zone; - op->customdata = tsc; - st->flags |= ST_SCROLL_SELECT; - - /* jump scroll, works in v2d but needs to be added here too :S */ - if (event->type == MIDDLEMOUSE) { - tsc->old[0] = ar->winrct.xmin + BLI_rcti_cent_x(&st->txtbar); - tsc->old[1] = ar->winrct.ymin + BLI_rcti_cent_y(&st->txtbar); - - tsc->first = 0; - tsc->zone = SCROLLHANDLE_BAR; - text_scroll_apply(C, op, event); - } - - WM_event_add_modal_handler(C, op); - - return OPERATOR_RUNNING_MODAL; + SpaceText *st = CTX_wm_space_text(C); + ARegion *ar = CTX_wm_region(C); + TextScroll *tsc; + const int *mval = event->mval; + int zone = -1; + + if (RNA_struct_property_is_set(op->ptr, "lines")) + return text_scroll_exec(C, op); + + /* verify we are in the right zone */ + if (mval[0] > st->txtbar.xmin && mval[0] < st->txtbar.xmax) { + if (mval[1] >= st->txtbar.ymin && mval[1] <= st->txtbar.ymax) { + /* mouse inside scroll handle */ + zone = SCROLLHANDLE_BAR; + } + else if (mval[1] > TXT_SCROLL_SPACE && mval[1] < ar->winy - TXT_SCROLL_SPACE) { + if (mval[1] < st->txtbar.ymin) + zone = SCROLLHANDLE_MIN_OUTSIDE; + else + zone = SCROLLHANDLE_MAX_OUTSIDE; + } + } + + if (zone == -1) { + /* we are outside slider - nothing to do */ + return OPERATOR_PASS_THROUGH; + } + + tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); + tsc->first = 1; + tsc->scrollbar = 1; + tsc->zone = zone; + op->customdata = tsc; + st->flags |= ST_SCROLL_SELECT; + + /* jump scroll, works in v2d but needs to be added here too :S */ + if (event->type == MIDDLEMOUSE) { + tsc->old[0] = ar->winrct.xmin + BLI_rcti_cent_x(&st->txtbar); + tsc->old[1] = ar->winrct.ymin + BLI_rcti_cent_y(&st->txtbar); + + tsc->first = 0; + tsc->zone = SCROLLHANDLE_BAR; + text_scroll_apply(C, op, event); + } + + WM_event_add_modal_handler(C, op); + + return OPERATOR_RUNNING_MODAL; } void TEXT_OT_scroll_bar(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Scrollbar"; - /* don't really see the difference between this and - * scroll. Both do basically the same thing (aside - * from keymaps).*/ - ot->idname = "TEXT_OT_scroll_bar"; + /* identifiers */ + ot->name = "Scrollbar"; + /* don't really see the difference between this and + * scroll. Both do basically the same thing (aside + * from keymaps).*/ + ot->idname = "TEXT_OT_scroll_bar"; - /* api callbacks */ - ot->invoke = text_scroll_bar_invoke; - ot->modal = text_scroll_modal; - ot->cancel = text_scroll_cancel; - ot->poll = text_region_scroll_poll; + /* api callbacks */ + ot->invoke = text_scroll_bar_invoke; + ot->modal = text_scroll_modal; + ot->cancel = text_scroll_cancel; + ot->poll = text_region_scroll_poll; - /* flags */ - ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL; + /* flags */ + ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL; - /* properties */ - RNA_def_int(ot->srna, "lines", 1, INT_MIN, INT_MAX, "Lines", "Number of lines to scroll", -100, 100); + /* properties */ + RNA_def_int( + ot->srna, "lines", 1, INT_MIN, INT_MAX, "Lines", "Number of lines to scroll", -100, 100); } /******************* set selection operator **********************/ typedef struct SetSelection { - int selecting; - int selc, sell; - short old[2]; - wmTimer *timer; /* needed for scrolling when mouse at region bounds */ + int selecting; + int selc, sell; + short old[2]; + wmTimer *timer; /* needed for scrolling when mouse at region bounds */ } SetSelection; static int flatten_width(SpaceText *st, const char *str) { - int i, total = 0; + int i, total = 0; - for (i = 0; str[i]; i += BLI_str_utf8_size_safe(str + i)) { - if (str[i] == '\t') { - total += st->tabnumber - total % st->tabnumber; - } - else { - total += BLI_str_utf8_char_width_safe(str + i); - } - } + for (i = 0; str[i]; i += BLI_str_utf8_size_safe(str + i)) { + if (str[i] == '\t') { + total += st->tabnumber - total % st->tabnumber; + } + else { + total += BLI_str_utf8_char_width_safe(str + i); + } + } - return total; + return total; } static int flatten_column_to_offset(SpaceText *st, const char *str, int index) { - int i = 0, j = 0, col; + int i = 0, j = 0, col; - while (*(str + j)) { - if (str[j] == '\t') - col = st->tabnumber - i % st->tabnumber; - else - col = BLI_str_utf8_char_width_safe(str + j); + while (*(str + j)) { + if (str[j] == '\t') + col = st->tabnumber - i % st->tabnumber; + else + col = BLI_str_utf8_char_width_safe(str + j); - if (i + col > index) - break; + if (i + col > index) + break; - i += col; - j += BLI_str_utf8_size_safe(str + j); - } + i += col; + j += BLI_str_utf8_size_safe(str + j); + } - return j; + return j; } static TextLine *get_line_pos_wrapped(SpaceText *st, ARegion *ar, int *y) { - TextLine *linep = st->text->lines.first; - int i, lines; - - if (*y < -st->top) { - return NULL; /* We are beyond the first line... */ - } - - for (i = -st->top; i <= *y && linep; linep = linep->next, i += lines) { - lines = text_get_visible_lines(st, ar, linep->line); - - if (i + lines > *y) { - /* We found the line matching given vertical 'coordinate', - * now set y relative to this line's start. */ - *y -= i; - break; - } - } - return linep; -} - -static void text_cursor_set_to_pos_wrapped(SpaceText *st, ARegion *ar, int x, int y, const bool sel) -{ - Text *text = st->text; - int max = wrap_width(st, ar); /* column */ - int charp = -1; /* mem */ - bool found = false; /* flags */ - - /* Point to line matching given y position, if any. */ - TextLine *linep = get_line_pos_wrapped(st, ar, &y); - - if (linep) { - int i = 0, start = 0, end = max; /* column */ - int j, curs = 0, endj = 0; /* mem */ - bool chop = true; /* flags */ - char ch; - - for (j = 0 ; !found && ((ch = linep->line[j]) != '\0'); j += BLI_str_utf8_size_safe(linep->line + j)) { - int chars; - int columns = BLI_str_utf8_char_width_safe(linep->line + j); /* = 1 for tab */ - - /* Mimic replacement of tabs */ - if (ch == '\t') { - chars = st->tabnumber - i % st->tabnumber; - ch = ' '; - } - else { - chars = 1; - } - - while (chars--) { - /* Gone too far, go back to last wrap point */ - if (y < 0) { - charp = endj; - y = 0; - found = true; - break; - /* Exactly at the cursor */ - } - else if (y == 0 && i - start <= x && i + columns - start > x) { - /* current position could be wrapped to next line */ - /* this should be checked when end of current line would be reached */ - charp = curs = j; - found = true; - /* Prepare curs for next wrap */ - } - else if (i - end <= x && i + columns - end > x) { - curs = j; - } - if (i + columns - start > max) { - end = MIN2(end, i); - - if (found) { - /* exact cursor position was found, check if it's still on needed line - * (hasn't been wrapped) */ - if (charp > endj && !chop && ch != '\0') - charp = endj; - break; - } - - if (chop) - endj = j; - start = end; - end += max; - - if (j < linep->len) - y--; - - chop = true; - if (y == 0 && i + columns - start > x) { - charp = curs; - found = true; - break; - } - } - else if (ch == ' ' || ch == '-' || ch == '\0') { - if (found) { - break; - } - - if (y == 0 && i + columns - start > x) { - charp = curs; - found = true; - break; - } - end = i + 1; - endj = j; - chop = false; - } - i += columns; - } - } - - BLI_assert(y == 0); - - if (!found) { - /* On correct line but didn't meet cursor, must be at end */ - charp = linep->len; - } - } - else if (y < 0) { /* Before start of text. */ - linep = st->text->lines.first; - charp = 0; - } - else { /* Beyond end of text */ - linep = st->text->lines.last; - charp = linep->len; - } - - BLI_assert(linep && charp != -1); - - if (sel) { - text->sell = linep; - text->selc = charp; - } - else { - text->curl = linep; - text->curc = charp; - } + TextLine *linep = st->text->lines.first; + int i, lines; + + if (*y < -st->top) { + return NULL; /* We are beyond the first line... */ + } + + for (i = -st->top; i <= *y && linep; linep = linep->next, i += lines) { + lines = text_get_visible_lines(st, ar, linep->line); + + if (i + lines > *y) { + /* We found the line matching given vertical 'coordinate', + * now set y relative to this line's start. */ + *y -= i; + break; + } + } + return linep; +} + +static void text_cursor_set_to_pos_wrapped( + SpaceText *st, ARegion *ar, int x, int y, const bool sel) +{ + Text *text = st->text; + int max = wrap_width(st, ar); /* column */ + int charp = -1; /* mem */ + bool found = false; /* flags */ + + /* Point to line matching given y position, if any. */ + TextLine *linep = get_line_pos_wrapped(st, ar, &y); + + if (linep) { + int i = 0, start = 0, end = max; /* column */ + int j, curs = 0, endj = 0; /* mem */ + bool chop = true; /* flags */ + char ch; + + for (j = 0; !found && ((ch = linep->line[j]) != '\0'); + j += BLI_str_utf8_size_safe(linep->line + j)) { + int chars; + int columns = BLI_str_utf8_char_width_safe(linep->line + j); /* = 1 for tab */ + + /* Mimic replacement of tabs */ + if (ch == '\t') { + chars = st->tabnumber - i % st->tabnumber; + ch = ' '; + } + else { + chars = 1; + } + + while (chars--) { + /* Gone too far, go back to last wrap point */ + if (y < 0) { + charp = endj; + y = 0; + found = true; + break; + /* Exactly at the cursor */ + } + else if (y == 0 && i - start <= x && i + columns - start > x) { + /* current position could be wrapped to next line */ + /* this should be checked when end of current line would be reached */ + charp = curs = j; + found = true; + /* Prepare curs for next wrap */ + } + else if (i - end <= x && i + columns - end > x) { + curs = j; + } + if (i + columns - start > max) { + end = MIN2(end, i); + + if (found) { + /* exact cursor position was found, check if it's still on needed line + * (hasn't been wrapped) */ + if (charp > endj && !chop && ch != '\0') + charp = endj; + break; + } + + if (chop) + endj = j; + start = end; + end += max; + + if (j < linep->len) + y--; + + chop = true; + if (y == 0 && i + columns - start > x) { + charp = curs; + found = true; + break; + } + } + else if (ch == ' ' || ch == '-' || ch == '\0') { + if (found) { + break; + } + + if (y == 0 && i + columns - start > x) { + charp = curs; + found = true; + break; + } + end = i + 1; + endj = j; + chop = false; + } + i += columns; + } + } + + BLI_assert(y == 0); + + if (!found) { + /* On correct line but didn't meet cursor, must be at end */ + charp = linep->len; + } + } + else if (y < 0) { /* Before start of text. */ + linep = st->text->lines.first; + charp = 0; + } + else { /* Beyond end of text */ + linep = st->text->lines.last; + charp = linep->len; + } + + BLI_assert(linep && charp != -1); + + if (sel) { + text->sell = linep; + text->selc = charp; + } + else { + text->curl = linep; + text->curc = charp; + } } static void text_cursor_set_to_pos(SpaceText *st, ARegion *ar, int x, int y, const bool sel) { - Text *text = st->text; - text_update_character_width(st); - y = (ar->winy - 2 - y) / (st->lheight_dpi + TXT_LINE_SPACING); - - if (st->showlinenrs) x -= TXT_OFFSET + TEXTXLOC; - else x -= TXT_OFFSET; - - if (x < 0) x = 0; - x = text_pixel_x_to_column(st, x) + st->left; - - if (st->wordwrap) { - text_cursor_set_to_pos_wrapped(st, ar, x, y, sel); - } - else { - TextLine **linep; - int *charp; - int w; - - if (sel) { linep = &text->sell; charp = &text->selc; } - else { linep = &text->curl; charp = &text->curc; } - - y -= txt_get_span(text->lines.first, *linep) - st->top; - - if (y > 0) { - while (y-- != 0) { - if ((*linep)->next) *linep = (*linep)->next; - } - } - else if (y < 0) { - while (y++ != 0) { - if ((*linep)->prev) *linep = (*linep)->prev; - } - } - - - w = flatten_width(st, (*linep)->line); - if (x < w) *charp = flatten_column_to_offset(st, (*linep)->line, x); - else *charp = (*linep)->len; - } - if (!sel) txt_pop_sel(text); + Text *text = st->text; + text_update_character_width(st); + y = (ar->winy - 2 - y) / (st->lheight_dpi + TXT_LINE_SPACING); + + if (st->showlinenrs) + x -= TXT_OFFSET + TEXTXLOC; + else + x -= TXT_OFFSET; + + if (x < 0) + x = 0; + x = text_pixel_x_to_column(st, x) + st->left; + + if (st->wordwrap) { + text_cursor_set_to_pos_wrapped(st, ar, x, y, sel); + } + else { + TextLine **linep; + int *charp; + int w; + + if (sel) { + linep = &text->sell; + charp = &text->selc; + } + else { + linep = &text->curl; + charp = &text->curc; + } + + y -= txt_get_span(text->lines.first, *linep) - st->top; + + if (y > 0) { + while (y-- != 0) { + if ((*linep)->next) + *linep = (*linep)->next; + } + } + else if (y < 0) { + while (y++ != 0) { + if ((*linep)->prev) + *linep = (*linep)->prev; + } + } + + w = flatten_width(st, (*linep)->line); + if (x < w) + *charp = flatten_column_to_offset(st, (*linep)->line, x); + else + *charp = (*linep)->len; + } + if (!sel) + txt_pop_sel(text); } static void text_cursor_timer_ensure(bContext *C, SetSelection *ssel) { - if (ssel->timer == NULL) { - wmWindowManager *wm = CTX_wm_manager(C); - wmWindow *win = CTX_wm_window(C); + if (ssel->timer == NULL) { + wmWindowManager *wm = CTX_wm_manager(C); + wmWindow *win = CTX_wm_window(C); - ssel->timer = WM_event_add_timer(wm, win, TIMER, 0.02f); - } + ssel->timer = WM_event_add_timer(wm, win, TIMER, 0.02f); + } } static void text_cursor_timer_remove(bContext *C, SetSelection *ssel) { - if (ssel->timer) { - wmWindowManager *wm = CTX_wm_manager(C); - wmWindow *win = CTX_wm_window(C); + if (ssel->timer) { + wmWindowManager *wm = CTX_wm_manager(C); + wmWindow *win = CTX_wm_window(C); - WM_event_remove_timer(wm, win, ssel->timer); - } - ssel->timer = NULL; + WM_event_remove_timer(wm, win, ssel->timer); + } + ssel->timer = NULL; } - - static void text_cursor_set_apply(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - ARegion *ar = CTX_wm_region(C); - SetSelection *ssel = op->customdata; - - if (event->mval[1] < 0 || event->mval[1] > ar->winy) { - text_cursor_timer_ensure(C, ssel); - - if (event->type == TIMER) { - text_cursor_set_to_pos(st, ar, event->mval[0], event->mval[1], 1); - text_scroll_to_cursor(st, ar, false); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - } - } - else if (!st->wordwrap && (event->mval[0] < 0 || event->mval[0] > ar->winx)) { - text_cursor_timer_ensure(C, ssel); - - if (event->type == TIMER) { - text_cursor_set_to_pos(st, ar, CLAMPIS(event->mval[0], 0, ar->winx), event->mval[1], 1); - text_scroll_to_cursor(st, ar, false); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - } - } - else { - text_cursor_timer_remove(C, ssel); - - if (event->type != TIMER) { - text_cursor_set_to_pos(st, ar, event->mval[0], event->mval[1], 1); - text_scroll_to_cursor(st, ar, false); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - - ssel->old[0] = event->mval[0]; - ssel->old[1] = event->mval[1]; - } - } + SpaceText *st = CTX_wm_space_text(C); + ARegion *ar = CTX_wm_region(C); + SetSelection *ssel = op->customdata; + + if (event->mval[1] < 0 || event->mval[1] > ar->winy) { + text_cursor_timer_ensure(C, ssel); + + if (event->type == TIMER) { + text_cursor_set_to_pos(st, ar, event->mval[0], event->mval[1], 1); + text_scroll_to_cursor(st, ar, false); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + } + } + else if (!st->wordwrap && (event->mval[0] < 0 || event->mval[0] > ar->winx)) { + text_cursor_timer_ensure(C, ssel); + + if (event->type == TIMER) { + text_cursor_set_to_pos(st, ar, CLAMPIS(event->mval[0], 0, ar->winx), event->mval[1], 1); + text_scroll_to_cursor(st, ar, false); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + } + } + else { + text_cursor_timer_remove(C, ssel); + + if (event->type != TIMER) { + text_cursor_set_to_pos(st, ar, event->mval[0], event->mval[1], 1); + text_scroll_to_cursor(st, ar, false); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + + ssel->old[0] = event->mval[0]; + ssel->old[1] = event->mval[1]; + } + } } static void text_cursor_set_exit(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = st->text; - SetSelection *ssel = op->customdata; - char *buffer; + SpaceText *st = CTX_wm_space_text(C); + Text *text = st->text; + SetSelection *ssel = op->customdata; + char *buffer; - if (txt_has_sel(text)) { - buffer = txt_sel_to_buf(text); - WM_clipboard_text_set(buffer, 1); - MEM_freeN(buffer); - } + if (txt_has_sel(text)) { + buffer = txt_sel_to_buf(text); + WM_clipboard_text_set(buffer, 1); + MEM_freeN(buffer); + } - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - text_cursor_timer_remove(C, ssel); - MEM_freeN(ssel); + text_cursor_timer_remove(C, ssel); + MEM_freeN(ssel); } static int text_set_selection_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - SetSelection *ssel; + SpaceText *st = CTX_wm_space_text(C); + SetSelection *ssel; - if (event->mval[0] >= st->txtbar.xmin) - return OPERATOR_PASS_THROUGH; + if (event->mval[0] >= st->txtbar.xmin) + return OPERATOR_PASS_THROUGH; - op->customdata = MEM_callocN(sizeof(SetSelection), "SetCursor"); - ssel = op->customdata; - ssel->selecting = RNA_boolean_get(op->ptr, "select"); + op->customdata = MEM_callocN(sizeof(SetSelection), "SetCursor"); + ssel = op->customdata; + ssel->selecting = RNA_boolean_get(op->ptr, "select"); - ssel->old[0] = event->mval[0]; - ssel->old[1] = event->mval[1]; + ssel->old[0] = event->mval[0]; + ssel->old[1] = event->mval[1]; - ssel->sell = txt_get_span(st->text->lines.first, st->text->sell); - ssel->selc = st->text->selc; + ssel->sell = txt_get_span(st->text->lines.first, st->text->sell); + ssel->selc = st->text->selc; - WM_event_add_modal_handler(C, op); + WM_event_add_modal_handler(C, op); - text_cursor_set_apply(C, op, event); + text_cursor_set_apply(C, op, event); - return OPERATOR_RUNNING_MODAL; + return OPERATOR_RUNNING_MODAL; } static int text_set_selection_modal(bContext *C, wmOperator *op, const wmEvent *event) { - switch (event->type) { - case LEFTMOUSE: - case MIDDLEMOUSE: - case RIGHTMOUSE: - text_cursor_set_exit(C, op); - return OPERATOR_FINISHED; - case TIMER: - case MOUSEMOVE: - text_cursor_set_apply(C, op, event); - break; - } + switch (event->type) { + case LEFTMOUSE: + case MIDDLEMOUSE: + case RIGHTMOUSE: + text_cursor_set_exit(C, op); + return OPERATOR_FINISHED; + case TIMER: + case MOUSEMOVE: + text_cursor_set_apply(C, op, event); + break; + } - return OPERATOR_RUNNING_MODAL; + return OPERATOR_RUNNING_MODAL; } static void text_set_selection_cancel(bContext *C, wmOperator *op) { - text_cursor_set_exit(C, op); + text_cursor_set_exit(C, op); } void TEXT_OT_selection_set(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Set Selection"; - ot->idname = "TEXT_OT_selection_set"; - ot->description = "Set cursor selection"; + /* identifiers */ + ot->name = "Set Selection"; + ot->idname = "TEXT_OT_selection_set"; + ot->description = "Set cursor selection"; - /* api callbacks */ - ot->invoke = text_set_selection_invoke; - ot->modal = text_set_selection_modal; - ot->cancel = text_set_selection_cancel; - ot->poll = text_region_edit_poll; + /* api callbacks */ + ot->invoke = text_set_selection_invoke; + ot->modal = text_set_selection_modal; + ot->cancel = text_set_selection_cancel; + ot->poll = text_region_edit_poll; - /* properties */ - RNA_def_boolean(ot->srna, "select", 0, "Select", "Set selection end rather than cursor"); + /* properties */ + RNA_def_boolean(ot->srna, "select", 0, "Select", "Set selection end rather than cursor"); } /******************* set cursor operator **********************/ static int text_cursor_set_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - ARegion *ar = CTX_wm_region(C); - int x = RNA_int_get(op->ptr, "x"); - int y = RNA_int_get(op->ptr, "y"); + SpaceText *st = CTX_wm_space_text(C); + ARegion *ar = CTX_wm_region(C); + int x = RNA_int_get(op->ptr, "x"); + int y = RNA_int_get(op->ptr, "y"); - text_cursor_set_to_pos(st, ar, x, y, 0); + text_cursor_set_to_pos(st, ar, x, y, 0); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); - return OPERATOR_PASS_THROUGH; + return OPERATOR_PASS_THROUGH; } static int text_cursor_set_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); + SpaceText *st = CTX_wm_space_text(C); - if (event->mval[0] >= st->txtbar.xmin) - return OPERATOR_PASS_THROUGH; + if (event->mval[0] >= st->txtbar.xmin) + return OPERATOR_PASS_THROUGH; - RNA_int_set(op->ptr, "x", event->mval[0]); - RNA_int_set(op->ptr, "y", event->mval[1]); + RNA_int_set(op->ptr, "x", event->mval[0]); + RNA_int_set(op->ptr, "y", event->mval[1]); - return text_cursor_set_exec(C, op); + return text_cursor_set_exec(C, op); } void TEXT_OT_cursor_set(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Set Cursor"; - ot->idname = "TEXT_OT_cursor_set"; - ot->description = "Set cursor position"; + /* identifiers */ + ot->name = "Set Cursor"; + ot->idname = "TEXT_OT_cursor_set"; + ot->description = "Set cursor position"; - /* api callbacks */ - ot->invoke = text_cursor_set_invoke; - ot->exec = text_cursor_set_exec; - ot->poll = text_region_edit_poll; + /* api callbacks */ + ot->invoke = text_cursor_set_invoke; + ot->exec = text_cursor_set_exec; + ot->poll = text_region_edit_poll; - /* properties */ - RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX); - RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX); + /* properties */ + RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX); + RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX); } /******************* line number operator **********************/ static int text_line_number_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - ARegion *ar = CTX_wm_region(C); - const int *mval = event->mval; - double time; - static int jump_to = 0; - static double last_jump = 0; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + ARegion *ar = CTX_wm_region(C); + const int *mval = event->mval; + double time; + static int jump_to = 0; + static double last_jump = 0; - text_update_character_width(st); + text_update_character_width(st); - if (!st->showlinenrs) - return OPERATOR_PASS_THROUGH; + if (!st->showlinenrs) + return OPERATOR_PASS_THROUGH; - if (!(mval[0] > 2 && mval[0] < (TXT_OFFSET + TEXTXLOC) && mval[1] > 2 && mval[1] < ar->winy - 2)) - return OPERATOR_PASS_THROUGH; + if (!(mval[0] > 2 && mval[0] < (TXT_OFFSET + TEXTXLOC) && mval[1] > 2 && mval[1] < ar->winy - 2)) + return OPERATOR_PASS_THROUGH; - if (!(event->ascii >= '0' && event->ascii <= '9')) - return OPERATOR_PASS_THROUGH; + if (!(event->ascii >= '0' && event->ascii <= '9')) + return OPERATOR_PASS_THROUGH; - time = PIL_check_seconds_timer(); - if (last_jump < time - 1) - jump_to = 0; + time = PIL_check_seconds_timer(); + if (last_jump < time - 1) + jump_to = 0; - jump_to *= 10; - jump_to += (int)(event->ascii - '0'); + jump_to *= 10; + jump_to += (int)(event->ascii - '0'); - txt_move_toline(text, jump_to - 1, 0); - last_jump = time; + txt_move_toline(text, jump_to - 1, 0); + last_jump = time; - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_line_number(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Line Number"; - ot->idname = "TEXT_OT_line_number"; - ot->description = "The current line number"; + /* identifiers */ + ot->name = "Line Number"; + ot->idname = "TEXT_OT_line_number"; + ot->description = "The current line number"; - /* api callbacks */ - ot->invoke = text_line_number_invoke; - ot->poll = text_region_edit_poll; + /* api callbacks */ + ot->invoke = text_line_number_invoke; + ot->poll = text_region_edit_poll; } /******************* insert operator **********************/ static int text_insert_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - char *str; - bool done = false; - size_t i = 0; - unsigned int code; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + char *str; + bool done = false; + size_t i = 0; + unsigned int code; - text_drawcache_tag_update(st, 0); + text_drawcache_tag_update(st, 0); - str = RNA_string_get_alloc(op->ptr, "text", NULL, 0); + str = RNA_string_get_alloc(op->ptr, "text", NULL, 0); - TextUndoBuf *utxt = ED_text_undo_push_init(C); + TextUndoBuf *utxt = ED_text_undo_push_init(C); - if (st && st->overwrite) { - while (str[i]) { - code = BLI_str_utf8_as_unicode_step(str, &i); - done |= txt_replace_char(text, utxt, code); - } - } - else { - while (str[i]) { - code = BLI_str_utf8_as_unicode_step(str, &i); - done |= txt_add_char(text, utxt, code); - } - } + if (st && st->overwrite) { + while (str[i]) { + code = BLI_str_utf8_as_unicode_step(str, &i); + done |= txt_replace_char(text, utxt, code); + } + } + else { + while (str[i]) { + code = BLI_str_utf8_as_unicode_step(str, &i); + done |= txt_add_char(text, utxt, code); + } + } - MEM_freeN(str); + MEM_freeN(str); - if (!done) - return OPERATOR_CANCELLED; + if (!done) + return OPERATOR_CANCELLED; - text_update_line_edited(text->curl); + text_update_line_edited(text->curl); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } static int text_insert_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - int ret; - - // if (!RNA_struct_property_is_set(op->ptr, "text")) { /* always set from keymap XXX */ - if (!RNA_string_length(op->ptr, "text")) { - /* if alt/ctrl/super are pressed pass through except for utf8 character event - * (when input method are used for utf8 inputs, the user may assign key event - * including alt/ctrl/super like ctrl+m to commit utf8 string. in such case, - * the modifiers in the utf8 character event make no sense.) */ - if ((event->ctrl || event->oskey) && !event->utf8_buf[0]) { - return OPERATOR_PASS_THROUGH; - } - else { - char str[BLI_UTF8_MAX + 1]; - size_t len; - - if (event->utf8_buf[0]) { - len = BLI_str_utf8_size_safe(event->utf8_buf); - memcpy(str, event->utf8_buf, len); - } - else { - /* in theory, ghost can set value to extended ascii here */ - len = BLI_str_utf8_from_unicode(event->ascii, str); - } - str[len] = '\0'; - RNA_string_set(op->ptr, "text", str); - } - } - - ret = text_insert_exec(C, op); - - /* run the script while editing, evil but useful */ - if (ret == OPERATOR_FINISHED && CTX_wm_space_text(C)->live_edit) - text_run_script(C, NULL); - - return ret; + int ret; + + // if (!RNA_struct_property_is_set(op->ptr, "text")) { /* always set from keymap XXX */ + if (!RNA_string_length(op->ptr, "text")) { + /* if alt/ctrl/super are pressed pass through except for utf8 character event + * (when input method are used for utf8 inputs, the user may assign key event + * including alt/ctrl/super like ctrl+m to commit utf8 string. in such case, + * the modifiers in the utf8 character event make no sense.) */ + if ((event->ctrl || event->oskey) && !event->utf8_buf[0]) { + return OPERATOR_PASS_THROUGH; + } + else { + char str[BLI_UTF8_MAX + 1]; + size_t len; + + if (event->utf8_buf[0]) { + len = BLI_str_utf8_size_safe(event->utf8_buf); + memcpy(str, event->utf8_buf, len); + } + else { + /* in theory, ghost can set value to extended ascii here */ + len = BLI_str_utf8_from_unicode(event->ascii, str); + } + str[len] = '\0'; + RNA_string_set(op->ptr, "text", str); + } + } + + ret = text_insert_exec(C, op); + + /* run the script while editing, evil but useful */ + if (ret == OPERATOR_FINISHED && CTX_wm_space_text(C)->live_edit) + text_run_script(C, NULL); + + return ret; } void TEXT_OT_insert(wmOperatorType *ot) { - PropertyRNA *prop; + PropertyRNA *prop; - /* identifiers */ - ot->name = "Insert"; - ot->idname = "TEXT_OT_insert"; - ot->description = "Insert text at cursor position"; + /* identifiers */ + ot->name = "Insert"; + ot->idname = "TEXT_OT_insert"; + ot->description = "Insert text at cursor position"; - /* api callbacks */ - ot->exec = text_insert_exec; - ot->invoke = text_insert_invoke; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_insert_exec; + ot->invoke = text_insert_invoke; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; - /* properties */ - prop = RNA_def_string(ot->srna, "text", NULL, 0, "Text", "Text to insert at the cursor position"); - RNA_def_property_flag(prop, PROP_SKIP_SAVE); + /* properties */ + prop = RNA_def_string( + ot->srna, "text", NULL, 0, "Text", "Text to insert at the cursor position"); + RNA_def_property_flag(prop, PROP_SKIP_SAVE); } /******************* find operator *********************/ /* mode */ -#define TEXT_FIND 0 -#define TEXT_REPLACE 1 +#define TEXT_FIND 0 +#define TEXT_REPLACE 1 static int text_find_and_replace(bContext *C, wmOperator *op, short mode) { - Main *bmain = CTX_data_main(C); - SpaceText *st = CTX_wm_space_text(C); - Text *text = st->text; - int flags; - int found = 0; - char *tmp; - - if (!st->findstr[0]) - return OPERATOR_CANCELLED; - - flags = st->flags; - if (flags & ST_FIND_ALL) - flags &= ~ST_FIND_WRAP; - - /* Replace current */ - if (mode != TEXT_FIND && txt_has_sel(text)) { - tmp = txt_sel_to_buf(text); - - if (flags & ST_MATCH_CASE) found = STREQ(st->findstr, tmp); - else found = BLI_strcasecmp(st->findstr, tmp) == 0; - - if (found) { - if (mode == TEXT_REPLACE) { - TextUndoBuf *utxt = ED_text_undo_push_init(C); - txt_insert_buf(text, utxt, st->replacestr); - if (text->curl && text->curl->format) { - MEM_freeN(text->curl->format); - text->curl->format = NULL; - } - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); - text_drawcache_tag_update(CTX_wm_space_text(C), 1); - } - } - MEM_freeN(tmp); - tmp = NULL; - } - - /* Find next */ - if (txt_find_string(text, st->findstr, flags & ST_FIND_WRAP, flags & ST_MATCH_CASE)) { - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); - } - else if (flags & ST_FIND_ALL) { - if (text->id.next) - text = st->text = text->id.next; - else - text = st->text = bmain->texts.first; - txt_move_toline(text, 0, 0); - text_update_cursor_moved(C); - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); - } - else { - if (!found) BKE_reportf(op->reports, RPT_ERROR, "Text not found: %s", st->findstr); - } - - return OPERATOR_FINISHED; + Main *bmain = CTX_data_main(C); + SpaceText *st = CTX_wm_space_text(C); + Text *text = st->text; + int flags; + int found = 0; + char *tmp; + + if (!st->findstr[0]) + return OPERATOR_CANCELLED; + + flags = st->flags; + if (flags & ST_FIND_ALL) + flags &= ~ST_FIND_WRAP; + + /* Replace current */ + if (mode != TEXT_FIND && txt_has_sel(text)) { + tmp = txt_sel_to_buf(text); + + if (flags & ST_MATCH_CASE) + found = STREQ(st->findstr, tmp); + else + found = BLI_strcasecmp(st->findstr, tmp) == 0; + + if (found) { + if (mode == TEXT_REPLACE) { + TextUndoBuf *utxt = ED_text_undo_push_init(C); + txt_insert_buf(text, utxt, st->replacestr); + if (text->curl && text->curl->format) { + MEM_freeN(text->curl->format); + text->curl->format = NULL; + } + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + text_drawcache_tag_update(CTX_wm_space_text(C), 1); + } + } + MEM_freeN(tmp); + tmp = NULL; + } + + /* Find next */ + if (txt_find_string(text, st->findstr, flags & ST_FIND_WRAP, flags & ST_MATCH_CASE)) { + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); + } + else if (flags & ST_FIND_ALL) { + if (text->id.next) + text = st->text = text->id.next; + else + text = st->text = bmain->texts.first; + txt_move_toline(text, 0, 0); + text_update_cursor_moved(C); + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); + } + else { + if (!found) + BKE_reportf(op->reports, RPT_ERROR, "Text not found: %s", st->findstr); + } + + return OPERATOR_FINISHED; } static int text_find_exec(bContext *C, wmOperator *op) { - return text_find_and_replace(C, op, TEXT_FIND); + return text_find_and_replace(C, op, TEXT_FIND); } void TEXT_OT_find(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Find Next"; - ot->idname = "TEXT_OT_find"; - ot->description = "Find specified text"; + /* identifiers */ + ot->name = "Find Next"; + ot->idname = "TEXT_OT_find"; + ot->description = "Find specified text"; - /* api callbacks */ - ot->exec = text_find_exec; - ot->poll = text_space_edit_poll; + /* api callbacks */ + ot->exec = text_find_exec; + ot->poll = text_space_edit_poll; } /******************* replace operator *********************/ static int text_replace_exec(bContext *C, wmOperator *op) { - return text_find_and_replace(C, op, TEXT_REPLACE); + return text_find_and_replace(C, op, TEXT_REPLACE); } void TEXT_OT_replace(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Replace"; - ot->idname = "TEXT_OT_replace"; - ot->description = "Replace text with the specified text"; + /* identifiers */ + ot->name = "Replace"; + ot->idname = "TEXT_OT_replace"; + ot->description = "Replace text with the specified text"; - /* api callbacks */ - ot->exec = text_replace_exec; - ot->poll = text_space_edit_poll; + /* api callbacks */ + ot->exec = text_replace_exec; + ot->poll = text_space_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /******************* find set selected *********************/ static int text_find_set_selected_exec(bContext *C, wmOperator *op) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - char *tmp; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + char *tmp; - tmp = txt_sel_to_buf(text); - BLI_strncpy(st->findstr, tmp, ST_MAX_FIND_STR); - MEM_freeN(tmp); + tmp = txt_sel_to_buf(text); + BLI_strncpy(st->findstr, tmp, ST_MAX_FIND_STR); + MEM_freeN(tmp); - if (!st->findstr[0]) - return OPERATOR_FINISHED; + if (!st->findstr[0]) + return OPERATOR_FINISHED; - return text_find_and_replace(C, op, TEXT_FIND); + return text_find_and_replace(C, op, TEXT_FIND); } void TEXT_OT_find_set_selected(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Find Set Selected"; - ot->idname = "TEXT_OT_find_set_selected"; - ot->description = "Find specified text and set as selected"; + /* identifiers */ + ot->name = "Find Set Selected"; + ot->idname = "TEXT_OT_find_set_selected"; + ot->description = "Find specified text and set as selected"; - /* api callbacks */ - ot->exec = text_find_set_selected_exec; - ot->poll = text_space_edit_poll; + /* api callbacks */ + ot->exec = text_find_set_selected_exec; + ot->poll = text_space_edit_poll; } /******************* replace set selected *********************/ static int text_replace_set_selected_exec(bContext *C, wmOperator *UNUSED(op)) { - SpaceText *st = CTX_wm_space_text(C); - Text *text = CTX_data_edit_text(C); - char *tmp; + SpaceText *st = CTX_wm_space_text(C); + Text *text = CTX_data_edit_text(C); + char *tmp; - tmp = txt_sel_to_buf(text); - BLI_strncpy(st->replacestr, tmp, ST_MAX_FIND_STR); - MEM_freeN(tmp); + tmp = txt_sel_to_buf(text); + BLI_strncpy(st->replacestr, tmp, ST_MAX_FIND_STR); + MEM_freeN(tmp); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_replace_set_selected(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Replace Set Selected"; - ot->idname = "TEXT_OT_replace_set_selected"; - ot->description = "Replace text with specified text and set as selected"; + /* identifiers */ + ot->name = "Replace Set Selected"; + ot->idname = "TEXT_OT_replace_set_selected"; + ot->description = "Replace text with specified text and set as selected"; - /* api callbacks */ - ot->exec = text_replace_set_selected_exec; - ot->poll = text_space_edit_poll; + /* api callbacks */ + ot->exec = text_replace_set_selected_exec; + ot->poll = text_space_edit_poll; - /* flags */ - ot->flag = OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_UNDO; } /****************** resolve conflict operator ******************/ enum { RESOLVE_IGNORE, RESOLVE_RELOAD, RESOLVE_SAVE, RESOLVE_MAKE_INTERNAL }; static const EnumPropertyItem resolution_items[] = { - {RESOLVE_IGNORE, "IGNORE", 0, "Ignore", ""}, - {RESOLVE_RELOAD, "RELOAD", 0, "Reload", ""}, - {RESOLVE_SAVE, "SAVE", 0, "Save", ""}, - {RESOLVE_MAKE_INTERNAL, "MAKE_INTERNAL", 0, "Make Internal", ""}, - {0, NULL, 0, NULL, NULL}, + {RESOLVE_IGNORE, "IGNORE", 0, "Ignore", ""}, + {RESOLVE_RELOAD, "RELOAD", 0, "Reload", ""}, + {RESOLVE_SAVE, "SAVE", 0, "Save", ""}, + {RESOLVE_MAKE_INTERNAL, "MAKE_INTERNAL", 0, "Make Internal", ""}, + {0, NULL, 0, NULL, NULL}, }; static int text_resolve_conflict_exec(bContext *C, wmOperator *op) { - Text *text = CTX_data_edit_text(C); - int resolution = RNA_enum_get(op->ptr, "resolution"); + Text *text = CTX_data_edit_text(C); + int resolution = RNA_enum_get(op->ptr, "resolution"); - switch (resolution) { - case RESOLVE_RELOAD: - return text_reload_exec(C, op); - case RESOLVE_SAVE: - return text_save_exec(C, op); - case RESOLVE_MAKE_INTERNAL: - return text_make_internal_exec(C, op); - case RESOLVE_IGNORE: - BKE_text_file_modified_ignore(text); - return OPERATOR_FINISHED; - } + switch (resolution) { + case RESOLVE_RELOAD: + return text_reload_exec(C, op); + case RESOLVE_SAVE: + return text_save_exec(C, op); + case RESOLVE_MAKE_INTERNAL: + return text_make_internal_exec(C, op); + case RESOLVE_IGNORE: + BKE_text_file_modified_ignore(text); + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } static int text_resolve_conflict_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - Text *text = CTX_data_edit_text(C); - uiPopupMenu *pup; - uiLayout *layout; - - switch (BKE_text_file_modified_check(text)) { - case 1: - if (text->flags & TXT_ISDIRTY) { - /* modified locally and externally, ahhh. offer more possibilities. */ - pup = UI_popup_menu_begin(C, IFACE_("File Modified Outside and Inside Blender"), ICON_NONE); - layout = UI_popup_menu_layout(pup); - uiItemEnumO_ptr(layout, op->type, IFACE_("Reload from disk (ignore local changes)"), - 0, "resolution", RESOLVE_RELOAD); - uiItemEnumO_ptr(layout, op->type, IFACE_("Save to disk (ignore outside changes)"), - 0, "resolution", RESOLVE_SAVE); - uiItemEnumO_ptr(layout, op->type, IFACE_("Make text internal (separate copy)"), - 0, "resolution", RESOLVE_MAKE_INTERNAL); - UI_popup_menu_end(C, pup); - } - else { - pup = UI_popup_menu_begin(C, IFACE_("File Modified Outside Blender"), ICON_NONE); - layout = UI_popup_menu_layout(pup); - uiItemEnumO_ptr(layout, op->type, IFACE_("Reload from disk"), 0, "resolution", RESOLVE_RELOAD); - uiItemEnumO_ptr(layout, op->type, IFACE_("Make text internal (separate copy)"), - 0, "resolution", RESOLVE_MAKE_INTERNAL); - uiItemEnumO_ptr(layout, op->type, IFACE_("Ignore"), 0, "resolution", RESOLVE_IGNORE); - UI_popup_menu_end(C, pup); - } - break; - case 2: - pup = UI_popup_menu_begin(C, IFACE_("File Deleted Outside Blender"), ICON_NONE); - layout = UI_popup_menu_layout(pup); - uiItemEnumO_ptr(layout, op->type, IFACE_("Make text internal"), 0, "resolution", RESOLVE_MAKE_INTERNAL); - uiItemEnumO_ptr(layout, op->type, IFACE_("Recreate file"), 0, "resolution", RESOLVE_SAVE); - UI_popup_menu_end(C, pup); - break; - } - - return OPERATOR_INTERFACE; + Text *text = CTX_data_edit_text(C); + uiPopupMenu *pup; + uiLayout *layout; + + switch (BKE_text_file_modified_check(text)) { + case 1: + if (text->flags & TXT_ISDIRTY) { + /* modified locally and externally, ahhh. offer more possibilities. */ + pup = UI_popup_menu_begin( + C, IFACE_("File Modified Outside and Inside Blender"), ICON_NONE); + layout = UI_popup_menu_layout(pup); + uiItemEnumO_ptr(layout, + op->type, + IFACE_("Reload from disk (ignore local changes)"), + 0, + "resolution", + RESOLVE_RELOAD); + uiItemEnumO_ptr(layout, + op->type, + IFACE_("Save to disk (ignore outside changes)"), + 0, + "resolution", + RESOLVE_SAVE); + uiItemEnumO_ptr(layout, + op->type, + IFACE_("Make text internal (separate copy)"), + 0, + "resolution", + RESOLVE_MAKE_INTERNAL); + UI_popup_menu_end(C, pup); + } + else { + pup = UI_popup_menu_begin(C, IFACE_("File Modified Outside Blender"), ICON_NONE); + layout = UI_popup_menu_layout(pup); + uiItemEnumO_ptr( + layout, op->type, IFACE_("Reload from disk"), 0, "resolution", RESOLVE_RELOAD); + uiItemEnumO_ptr(layout, + op->type, + IFACE_("Make text internal (separate copy)"), + 0, + "resolution", + RESOLVE_MAKE_INTERNAL); + uiItemEnumO_ptr(layout, op->type, IFACE_("Ignore"), 0, "resolution", RESOLVE_IGNORE); + UI_popup_menu_end(C, pup); + } + break; + case 2: + pup = UI_popup_menu_begin(C, IFACE_("File Deleted Outside Blender"), ICON_NONE); + layout = UI_popup_menu_layout(pup); + uiItemEnumO_ptr( + layout, op->type, IFACE_("Make text internal"), 0, "resolution", RESOLVE_MAKE_INTERNAL); + uiItemEnumO_ptr(layout, op->type, IFACE_("Recreate file"), 0, "resolution", RESOLVE_SAVE); + UI_popup_menu_end(C, pup); + break; + } + + return OPERATOR_INTERFACE; } void TEXT_OT_resolve_conflict(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Resolve Conflict"; - ot->idname = "TEXT_OT_resolve_conflict"; - ot->description = "When external text is out of sync, resolve the conflict"; + /* identifiers */ + ot->name = "Resolve Conflict"; + ot->idname = "TEXT_OT_resolve_conflict"; + ot->description = "When external text is out of sync, resolve the conflict"; - /* api callbacks */ - ot->exec = text_resolve_conflict_exec; - ot->invoke = text_resolve_conflict_invoke; - ot->poll = text_save_poll; + /* api callbacks */ + ot->exec = text_resolve_conflict_exec; + ot->invoke = text_resolve_conflict_invoke; + ot->poll = text_save_poll; - /* properties */ - RNA_def_enum(ot->srna, "resolution", resolution_items, RESOLVE_IGNORE, "Resolution", "How to solve conflict due to differences in internal and external text"); + /* properties */ + RNA_def_enum(ot->srna, + "resolution", + resolution_items, + RESOLVE_IGNORE, + "Resolution", + "How to solve conflict due to differences in internal and external text"); } /********************** to 3d object operator *****************/ static int text_to_3d_object_exec(bContext *C, wmOperator *op) { - Text *text = CTX_data_edit_text(C); - const bool split_lines = RNA_boolean_get(op->ptr, "split_lines"); + Text *text = CTX_data_edit_text(C); + const bool split_lines = RNA_boolean_get(op->ptr, "split_lines"); - ED_text_to_object(C, text, split_lines); + ED_text_to_object(C, text, split_lines); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void TEXT_OT_to_3d_object(wmOperatorType *ot) { - /* identifiers */ - ot->name = "To 3D Object"; - ot->idname = "TEXT_OT_to_3d_object"; - ot->description = "Create 3D text object from active text data-block"; + /* identifiers */ + ot->name = "To 3D Object"; + ot->idname = "TEXT_OT_to_3d_object"; + ot->description = "Create 3D text object from active text data-block"; - /* api callbacks */ - ot->exec = text_to_3d_object_exec; - ot->poll = text_edit_poll; + /* api callbacks */ + ot->exec = text_to_3d_object_exec; + ot->poll = text_edit_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - /* properties */ - RNA_def_boolean(ot->srna, "split_lines", 0, "Split Lines", "Create one object per line in the text"); + /* properties */ + RNA_def_boolean( + ot->srna, "split_lines", 0, "Split Lines", "Create one object per line in the text"); } diff --git a/source/blender/editors/space_text/text_undo.c b/source/blender/editors/space_text/text_undo.c index 76da0531225..7db97f15a68 100644 --- a/source/blender/editors/space_text/text_undo.c +++ b/source/blender/editors/space_text/text_undo.c @@ -60,160 +60,165 @@ * \{ */ typedef struct TextUndoStep { - UndoStep step; - UndoRefID_Text text_ref; - TextUndoBuf data; + UndoStep step; + UndoRefID_Text text_ref; + TextUndoBuf data; } TextUndoStep; static bool text_undosys_poll(bContext *UNUSED(C)) { - /* Only use when operators initialized. */ - UndoStack *ustack = ED_undo_stack_get(); - return (ustack->step_init && (ustack->step_init->type == BKE_UNDOSYS_TYPE_TEXT)); + /* Only use when operators initialized. */ + UndoStack *ustack = ED_undo_stack_get(); + return (ustack->step_init && (ustack->step_init->type == BKE_UNDOSYS_TYPE_TEXT)); } static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p) { - TextUndoStep *us = (TextUndoStep *)us_p; - BLI_assert(BLI_array_is_zeroed(&us->data, 1)); + TextUndoStep *us = (TextUndoStep *)us_p; + BLI_assert(BLI_array_is_zeroed(&us->data, 1)); - UNUSED_VARS(C); - /* XXX, use to set the undo type only. */ + UNUSED_VARS(C); + /* XXX, use to set the undo type only. */ - us->data.buf = NULL; - us->data.len = 0; - us->data.pos = -1; + us->data.buf = NULL; + us->data.len = 0; + us->data.pos = -1; } -static bool text_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p) +static bool text_undosys_step_encode(struct bContext *C, + struct Main *UNUSED(bmain), + UndoStep *us_p) { - TextUndoStep *us = (TextUndoStep *)us_p; + TextUndoStep *us = (TextUndoStep *)us_p; - Text *text = CTX_data_edit_text(C); + Text *text = CTX_data_edit_text(C); - /* No undo data was generated. Hint, use global undo here. */ - if ((us->data.pos == -1) || (us->data.buf == NULL)) { - return false; - } + /* No undo data was generated. Hint, use global undo here. */ + if ((us->data.pos == -1) || (us->data.buf == NULL)) { + return false; + } - us_p->is_applied = true; + us_p->is_applied = true; - us->text_ref.ptr = text; + us->text_ref.ptr = text; - us->step.data_size = us->data.len; + us->step.data_size = us->data.len; - return true; + return true; } - static void text_undosys_step_decode_undo_impl(Text *text, TextUndoStep *us) { - BLI_assert(us->step.is_applied == true); - TextUndoBuf data = us->data; - while (data.pos > -1) { - txt_do_undo(text, &data); - } - BLI_assert(data.pos == -1); - us->step.is_applied = false; + BLI_assert(us->step.is_applied == true); + TextUndoBuf data = us->data; + while (data.pos > -1) { + txt_do_undo(text, &data); + } + BLI_assert(data.pos == -1); + us->step.is_applied = false; } static void text_undosys_step_decode_redo_impl(Text *text, TextUndoStep *us) { - BLI_assert(us->step.is_applied == false); - TextUndoBuf data = us->data; - data.pos = -1; - while (data.pos < us->data.pos) { - txt_do_redo(text, &data); - } - BLI_assert(data.pos == us->data.pos); - us->step.is_applied = true; + BLI_assert(us->step.is_applied == false); + TextUndoBuf data = us->data; + data.pos = -1; + while (data.pos < us->data.pos) { + txt_do_redo(text, &data); + } + BLI_assert(data.pos == us->data.pos); + us->step.is_applied = true; } static void text_undosys_step_decode_undo(Text *text, TextUndoStep *us) { - TextUndoStep *us_iter = us; - while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) { - if (us_iter->step.next->is_applied == false) { - break; - } - us_iter = (TextUndoStep *)us_iter->step.next; - } - while (us_iter != us) { - text_undosys_step_decode_undo_impl(text, us_iter); - us_iter = (TextUndoStep *)us_iter->step.prev; - } + TextUndoStep *us_iter = us; + while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) { + if (us_iter->step.next->is_applied == false) { + break; + } + us_iter = (TextUndoStep *)us_iter->step.next; + } + while (us_iter != us) { + text_undosys_step_decode_undo_impl(text, us_iter); + us_iter = (TextUndoStep *)us_iter->step.prev; + } } static void text_undosys_step_decode_redo(Text *text, TextUndoStep *us) { - TextUndoStep *us_iter = us; - while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) { - if (us_iter->step.prev->is_applied == true) { - break; - } - us_iter = (TextUndoStep *)us_iter->step.prev; - } - while (us_iter && (us_iter->step.is_applied == false)) { - text_undosys_step_decode_redo_impl(text, us_iter); - if (us_iter == us) { - break; - } - us_iter = (TextUndoStep *)us_iter->step.next; - } + TextUndoStep *us_iter = us; + while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) { + if (us_iter->step.prev->is_applied == true) { + break; + } + us_iter = (TextUndoStep *)us_iter->step.prev; + } + while (us_iter && (us_iter->step.is_applied == false)) { + text_undosys_step_decode_redo_impl(text, us_iter); + if (us_iter == us) { + break; + } + us_iter = (TextUndoStep *)us_iter->step.next; + } } -static void text_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int dir) +static void text_undosys_step_decode(struct bContext *C, + struct Main *UNUSED(bmain), + UndoStep *us_p, + int dir) { - TextUndoStep *us = (TextUndoStep *)us_p; - Text *text = us->text_ref.ptr; - - if (dir < 0) { - text_undosys_step_decode_undo(text, us); - } - else { - text_undosys_step_decode_redo(text, us); - } - - SpaceText *st = CTX_wm_space_text(C); - if (st) { - /* Not essential, always show text being undo where possible. */ - st->text = text; - } - text_update_edited(text); - text_update_cursor_moved(C); - text_drawcache_tag_update(st, 1); - WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); + TextUndoStep *us = (TextUndoStep *)us_p; + Text *text = us->text_ref.ptr; + + if (dir < 0) { + text_undosys_step_decode_undo(text, us); + } + else { + text_undosys_step_decode_redo(text, us); + } + + SpaceText *st = CTX_wm_space_text(C); + if (st) { + /* Not essential, always show text being undo where possible. */ + st->text = text; + } + text_update_edited(text); + text_update_cursor_moved(C); + text_drawcache_tag_update(st, 1); + WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text); } static void text_undosys_step_free(UndoStep *us_p) { - TextUndoStep *us = (TextUndoStep *)us_p; - MEM_SAFE_FREE(us->data.buf); + TextUndoStep *us = (TextUndoStep *)us_p; + MEM_SAFE_FREE(us->data.buf); } -static void text_undosys_foreach_ID_ref( - UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data) +static void text_undosys_foreach_ID_ref(UndoStep *us_p, + UndoTypeForEachIDRefFn foreach_ID_ref_fn, + void *user_data) { - TextUndoStep *us = (TextUndoStep *)us_p; - foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->text_ref)); + TextUndoStep *us = (TextUndoStep *)us_p; + foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->text_ref)); } /* Export for ED_undo_sys. */ void ED_text_undosys_type(UndoType *ut) { - ut->name = "Text"; - ut->poll = text_undosys_poll; - ut->step_encode_init = text_undosys_step_encode_init; - ut->step_encode = text_undosys_step_encode; - ut->step_decode = text_undosys_step_decode; - ut->step_free = text_undosys_step_free; + ut->name = "Text"; + ut->poll = text_undosys_poll; + ut->step_encode_init = text_undosys_step_encode_init; + ut->step_encode = text_undosys_step_encode; + ut->step_decode = text_undosys_step_decode; + ut->step_free = text_undosys_step_free; - ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref; + ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref; - ut->use_context = false; + ut->use_context = false; - ut->step_size = sizeof(TextUndoStep); + ut->step_size = sizeof(TextUndoStep); } /** \} */ @@ -225,10 +230,10 @@ void ED_text_undosys_type(UndoType *ut) /* Use operator system to finish the undo step. */ TextUndoBuf *ED_text_undo_push_init(bContext *C) { - UndoStack *ustack = ED_undo_stack_get(); - UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT); - TextUndoStep *us = (TextUndoStep *)us_p; - return &us->data; + UndoStack *ustack = ED_undo_stack_get(); + UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT); + TextUndoStep *us = (TextUndoStep *)us_p; + return &us->data; } /** \} */ |