diff options
Diffstat (limited to 'source/blender/editors/space_text/text_ops.c')
-rw-r--r-- | source/blender/editors/space_text/text_ops.c | 91 |
1 files changed, 58 insertions, 33 deletions
diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 2ae8d4cde7a..52a5ff609d8 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -159,6 +159,15 @@ static bool text_new_poll(bContext *UNUSED(C)) return 1; } +static bool text_data_poll(bContext *C) +{ + Text *text = CTX_data_edit_text(C); + if (!text) { + return false; + } + return true; +} + static bool text_edit_poll(bContext *C) { Text *text = CTX_data_edit_text(C); @@ -407,9 +416,9 @@ void TEXT_OT_open(wmOperatorType *ot) FILE_OPENFILE, WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, - FILE_SORT_ALPHA); // XXX TODO, relative_path + FILE_SORT_DEFAULT); /* TODO: relative_path. */ RNA_def_boolean( - ot->srna, "internal", 0, "Make internal", "Make text file internal after loading"); + ot->srna, "internal", 0, "Make Internal", "Make text file internal after loading"); } /** \} */ @@ -573,17 +582,6 @@ void TEXT_OT_make_internal(wmOperatorType *ot) /** \name Save Operator * \{ */ -static bool text_save_poll(bContext *C) -{ - Text *text = CTX_data_edit_text(C); - - if (!text_edit_poll(C)) { - return 0; - } - - return (text->filepath != NULL && !(text->flags & TXT_ISMEM)); -} - static void txt_write_file(Main *bmain, Text *text, ReportList *reports) { FILE *fp; @@ -594,6 +592,13 @@ static void txt_write_file(Main *bmain, Text *text, ReportList *reports) BLI_strncpy(filepath, text->filepath, FILE_MAX); BLI_path_abs(filepath, BKE_main_blendfile_path(bmain)); + /* Check if file write permission is ok. */ + if (BLI_exists(filepath) && !BLI_file_is_writable(filepath)) { + BKE_reportf( + reports, RPT_ERROR, "Cannot save text file, path \"%s\" is not writable", filepath); + return; + } + fp = BLI_fopen(filepath, "w"); if (fp == NULL) { BKE_reportf(reports, @@ -616,8 +621,8 @@ static void txt_write_file(Main *bmain, Text *text, ReportList *reports) 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); + /* Report since this can be called from key shortcuts. */ + BKE_reportf(reports, RPT_INFO, "Saved text \"%s\"", filepath); } else { text->mtime = 0; @@ -644,6 +649,18 @@ static int text_save_exec(bContext *C, wmOperator *op) return OPERATOR_FINISHED; } +static int text_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) +{ + Text *text = CTX_data_edit_text(C); + + /* Internal and texts without a filepath will go to "Save As". */ + if (text->filepath == NULL || (text->flags & TXT_ISMEM)) { + WM_operator_name_call(C, "TEXT_OT_save_as", WM_OP_INVOKE_DEFAULT, NULL); + return OPERATOR_CANCELLED; + } + return text_save_exec(C, op); +} + void TEXT_OT_save(wmOperatorType *ot) { /* identifiers */ @@ -653,7 +670,8 @@ void TEXT_OT_save(wmOperatorType *ot) /* api callbacks */ ot->exec = text_save_exec; - ot->poll = text_save_poll; + ot->invoke = text_save_invoke; + ot->poll = text_edit_poll; } /** \} */ @@ -733,7 +751,7 @@ void TEXT_OT_save_as(wmOperatorType *ot) FILE_SAVE, WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, - FILE_SORT_ALPHA); // XXX TODO, relative_path + FILE_SORT_DEFAULT); /* XXX TODO, relative_path. */ } /** \} */ @@ -742,11 +760,6 @@ void TEXT_OT_save_as(wmOperatorType *ot) /** \name Run Script Operator * \{ */ -static bool text_run_script_poll(bContext *C) -{ - return (CTX_data_edit_text(C) != NULL); -} - static int text_run_script(bContext *C, ReportList *reports) { #ifdef WITH_PYTHON @@ -808,7 +821,7 @@ void TEXT_OT_run_script(wmOperatorType *ot) ot->description = "Run active script"; /* api callbacks */ - ot->poll = text_run_script_poll; + ot->poll = text_data_poll; ot->exec = text_run_script_exec; /* flags */ @@ -1208,7 +1221,7 @@ static int text_line_break_exec(bContext *C, wmOperator *UNUSED(op)) text_drawcache_tag_update(st, 0); - // double check tabs/spaces before splitting the line + /* Double check tabs/spaces before splitting the line. */ curts = txt_setcurr_tab_spaces(text, space); ED_text_undo_push_init(C); txt_split_curline(text); @@ -1709,7 +1722,7 @@ static int text_get_cursor_rel( loop = 0; break; } - else if (ch == ' ' || ch == '-') { + else if (ELEM(ch, ' ', '-')) { if (found) { loop = 0; break; @@ -1884,7 +1897,7 @@ static void txt_wrap_move_bol(SpaceText *st, ARegion *region, const bool sel) end += max; chop = 1; } - else if (ch == ' ' || ch == '-' || ch == '\0') { + else if (ELEM(ch, ' ', '-', '\0')) { if (j >= oldc) { *charp = BLI_str_utf8_offset_from_column((*linep)->line, start); loop = 0; @@ -1974,7 +1987,7 @@ static void txt_wrap_move_eol(SpaceText *st, ARegion *region, const bool sel) loop = 0; break; } - else if (ch == ' ' || ch == '-') { + else if (ELEM(ch, ' ', '-')) { end = i + 1; endj = j; chop = 0; @@ -3052,7 +3065,7 @@ static void text_cursor_set_to_pos_wrapped( break; } } - else if (ch == ' ' || ch == '-' || ch == '\0') { + else if (ELEM(ch, ' ', '-', '\0')) { if (found) { break; } @@ -3463,7 +3476,8 @@ 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 */ + /* Note, the "text" property is always set from key-map, + * so we can't use #RNA_struct_property_is_set, check the length instead. */ 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 @@ -3692,7 +3706,7 @@ void TEXT_OT_replace(wmOperatorType *ot) /* properties */ PropertyRNA *prop; - prop = RNA_def_boolean(ot->srna, "all", false, "Replace all", "Replace all occurrences"); + prop = RNA_def_boolean(ot->srna, "all", false, "Replace All", "Replace all occurrences"); RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); } @@ -3780,6 +3794,17 @@ static const EnumPropertyItem resolution_items[] = { {0, NULL, 0, NULL, NULL}, }; +static bool text_resolve_conflict_poll(bContext *C) +{ + Text *text = CTX_data_edit_text(C); + + if (!text_edit_poll(C)) { + return false; + } + + return ((text->filepath != NULL) && !(text->flags & TXT_ISMEM)); +} + static int text_resolve_conflict_exec(bContext *C, wmOperator *op) { Text *text = CTX_data_edit_text(C); @@ -3871,7 +3896,7 @@ void TEXT_OT_resolve_conflict(wmOperatorType *ot) /* api callbacks */ ot->exec = text_resolve_conflict_exec; ot->invoke = text_resolve_conflict_invoke; - ot->poll = text_save_poll; + ot->poll = text_resolve_conflict_poll; /* properties */ RNA_def_enum(ot->srna, @@ -3890,7 +3915,7 @@ void TEXT_OT_resolve_conflict(wmOperatorType *ot) static int text_to_3d_object_exec(bContext *C, wmOperator *op) { - Text *text = CTX_data_edit_text(C); + const 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); @@ -3907,7 +3932,7 @@ void TEXT_OT_to_3d_object(wmOperatorType *ot) /* api callbacks */ ot->exec = text_to_3d_object_exec; - ot->poll = text_edit_poll; + ot->poll = text_data_poll; /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; |