code cleanup: favor braces when blocks have mixed brace use.
[blender.git] / source / blender / editors / space_text / text_ops.c
index 5b7f927..e6f95c3 100644 (file)
  */
 
 
-#include <stdlib.h>
 #include <string.h>
-#include <ctype.h> /* ispunct */
-#include <sys/stat.h>
 #include <errno.h>
 
 #include "MEM_guardedalloc.h"
 
 #include "DNA_text_types.h"
-#include "DNA_userdef_types.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_utildefines.h"
 
 #include "BLF_translation.h"
 
@@ -72,6 +67,7 @@
 #endif
 
 #include "text_intern.h"
+#include "text_format.h"
 
 /************************ poll ***************************/
 
@@ -102,7 +98,7 @@ static int text_edit_poll(bContext *C)
        return 1;
 }
 
-static int text_space_edit_poll(bContext *C)
+int text_space_edit_poll(bContext *C)
 {
        SpaceText *st = CTX_wm_space_text(C);
        Text *text = CTX_data_edit_text(C);
@@ -165,11 +161,12 @@ void text_update_edited(Text *text)
 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;
 
-       text = BKE_text_add("Text");
+       text = BKE_text_add(bmain, "Text");
 
        /* hook into UI */
        uiIDContextProperty(C, &ptr, &prop);
@@ -230,6 +227,7 @@ static int text_open_cancel(bContext *UNUSED(C), wmOperator *op)
 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;
@@ -238,7 +236,7 @@ static int text_open_exec(bContext *C, wmOperator *op)
 
        RNA_string_get(op->ptr, "filepath", str);
 
-       text = BKE_text_load(str, G.main->name);
+       text = BKE_text_load(bmain, str, G.main->name);
 
        if (!text) {
                if (op->customdata) MEM_freeN(op->customdata);
@@ -1131,21 +1129,20 @@ static int text_convert_whitespace_exec(bContext *C, wmOperator *op)
        TextLine *tmp;
        FlattenString fs;
        size_t a, j;
-       char *text_check_line, *new_line;
+       char *new_line;
        int extra, number; //unknown for now
        int type = RNA_enum_get(op->ptr, "type");
-       
-       tmp = text->lines.first;
-       
+
        /* first convert to all space, this make it a lot easier to convert to tabs
         * because there is no mixtures of ' ' && '\t' */
-       while (tmp) {
-               text_check_line = tmp->line;
+       for (tmp = text->lines.first; tmp; tmp = tmp->next) {
+               const char *text_check_line     = tmp->line;
+               const int   text_check_line_len = tmp->len;
                number = flatten_string(st, &fs, text_check_line) + 1;
                flatten_string_free(&fs);
                new_line = MEM_callocN(number, "Converted_Line");
                j = 0;
-               for (a = 0; a < strlen(text_check_line); a++) { //foreach char in line
+               for (a = 0; a < text_check_line_len; a++) { //foreach char in line
                        if (text_check_line[a] == '\t') { //checking for tabs
                                //get the number of spaces this tabs is showing
                                //i don't like doing it this way but will look into it later
@@ -1175,20 +1172,19 @@ static int text_convert_whitespace_exec(bContext *C, wmOperator *op)
                tmp->line = new_line;
                tmp->len = strlen(new_line);
                tmp->format = NULL;
-               tmp = tmp->next;
        }
        
        if (type == TO_TABS) { // Converting to tabs
                //start over from the beginning
-               tmp = text->lines.first;
                
-               while (tmp) {
-                       text_check_line = tmp->line;
+               for (tmp = text->lines.first; tmp; tmp = tmp->next) {
+                       const char *text_check_line     = tmp->line;
+                       const int   text_check_line_len = tmp->len;
                        extra = 0;
-                       for (a = 0; a < strlen(text_check_line); a++) {
+                       for (a = 0; a < text_check_line_len; a++) {
                                number = 0;
                                for (j = 0; j < (size_t)st->tabnumber; j++) {
-                                       if ((a + j) <= strlen(text_check_line)) { //check to make sure we are not pass the end of the line
+                                       if ((a + j) <= text_check_line_len) { //check to make sure we are not pass the end of the line
                                                if (text_check_line[a + j] != ' ') {
                                                        number = 1;
                                                }
@@ -1201,12 +1197,12 @@ static int text_convert_whitespace_exec(bContext *C, wmOperator *op)
                        }
                        
                        if (extra > 0) {   //got tabs make malloc and do what you have to do
-                               new_line = MEM_callocN(strlen(text_check_line) - (((st->tabnumber * extra) - extra) - 1), "Converted_Line");
+                               new_line = MEM_callocN(text_check_line_len - (((st->tabnumber * extra) - extra) - 1), "Converted_Line");
                                extra = 0; //reuse vars
-                               for (a = 0; a < strlen(text_check_line); a++) {
+                               for (a = 0; a < text_check_line_len; a++) {
                                        number = 0;
                                        for (j = 0; j < (size_t)st->tabnumber; j++) {
-                                               if ((a + j) <= strlen(text_check_line)) { //check to make sure we are not pass the end of the line
+                                               if ((a + j) <= text_check_line_len) { //check to make sure we are not pass the end of the line
                                                        if (text_check_line[a + j] != ' ') {
                                                                number = 1;
                                                        }
@@ -1233,7 +1229,6 @@ static int text_convert_whitespace_exec(bContext *C, wmOperator *op)
                                tmp->len = strlen(new_line);
                                tmp->format = NULL;
                        }
-                       tmp = tmp->next;
                }
        }
 
@@ -1317,9 +1312,11 @@ void TEXT_OT_select_line(wmOperatorType *ot)
 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;
 
-       txt_jump_left(text, 0);
-       txt_jump_right(text, 1);
+       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);
@@ -1416,7 +1413,9 @@ static int text_get_cursor_rel(SpaceText *st, ARegion *ar, TextLine *linein, int
                        chars = st->tabnumber - i % st->tabnumber;
                        ch = ' ';
                }
-               else chars = 1;
+               else {
+                       chars = 1;
+               }
 
                while (chars--) {
                        if (rell == 0 && i - start == relc) {
@@ -1594,7 +1593,9 @@ static void txt_wrap_move_bol(SpaceText *st, ARegion *ar, short sel)
                        chars = st->tabnumber - i % st->tabnumber;
                        ch = ' ';
                }
-               else chars = 1;
+               else {
+                       chars = 1;
+               }
 
                while (chars--) {
                        if (i - start >= max) {
@@ -1660,7 +1661,9 @@ static void txt_wrap_move_eol(SpaceText *st, ARegion *ar, short sel)
                        chars = st->tabnumber - i % st->tabnumber;
                        ch = ' ';
                }
-               else chars = 1;
+               else {
+                       chars = 1;
+               }
 
                while (chars--) {
                        if (i - start >= max) {
@@ -1719,7 +1722,9 @@ static void txt_wrap_move_up(SpaceText *st, ARegion *ar, short sel)
                        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;
+               else {
+                       *charp = 0;
+               }
        }
 
        if (!sel) txt_pop_sel(text);
@@ -1748,7 +1753,9 @@ static void txt_wrap_move_down(SpaceText *st, ARegion *ar, short sel)
                        *linep = (*linep)->next;
                        *charp = text_get_cursor_rel(st, ar, *linep, 0, col);
                }
-               else *charp = (*linep)->len;
+               else {
+                       *charp = (*linep)->len;
+               }
        }
 
        if (!sel) txt_pop_sel(text);
@@ -1821,11 +1828,11 @@ static int text_move_cursor(bContext *C, int type, int select)
                        break;
 
                case PREV_WORD:
-                       txt_jump_left(text, select);
+                       txt_jump_left(text, select, true);
                        break;
 
                case NEXT_WORD:
-                       txt_jump_right(text, select);
+                       txt_jump_right(text, select, true);
                        break;
 
                case PREV_CHAR:
@@ -1932,12 +1939,14 @@ static int text_jump_exec(bContext *C, wmOperator *op)
 
 static int text_jump_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
-       return WM_operator_props_dialog_popup(C, op, 200, 100);
+       return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 5 * UI_UNIT_Y);
 
 }
 
 void TEXT_OT_jump(wmOperatorType *ot)
 {
+       PropertyRNA *prop;
+
        /* identifiers */
        ot->name = "Jump";
        ot->idname = "TEXT_OT_jump";
@@ -1949,7 +1958,8 @@ void TEXT_OT_jump(wmOperatorType *ot)
        ot->poll = text_edit_poll;
 
        /* properties */
-       RNA_def_int(ot->srna, "line", 1, 1, INT_MAX, "Line", "Line number to jump to", 1, 10000);
+       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, BLF_I18NCONTEXT_ID_TEXT);
 }
 
 /******************* delete operator **********************/
@@ -2106,10 +2116,10 @@ static void text_scroll_apply(bContext *C, wmOperator *op, wmEvent *event)
 
        if (!tsc->scrollbar) {
                txtdelta[0] = -tsc->delta[0] / st->cwidth;
-               txtdelta[1] = tsc->delta[1] / (st->lheight + TXT_LINE_SPACING);
+               txtdelta[1] = tsc->delta[1] / (st->lheight_dpi + TXT_LINE_SPACING);
 
                tsc->delta[0] %= st->cwidth;
-               tsc->delta[1] %= (st->lheight + TXT_LINE_SPACING);
+               tsc->delta[1] %= (st->lheight_dpi + TXT_LINE_SPACING);
        }
        else {
                txtdelta[1] = -tsc->delta[1] * st->pix_per_line;
@@ -2204,7 +2214,7 @@ static int text_scroll_invoke(bContext *C, wmOperator *op, wmEvent *event)
                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 / 4;
+               tsc->delta[1] = (event->y - event->prevy) * st->lheight_dpi / 4;
                tsc->first = 0;
                tsc->scrollbar = 0;
                text_scroll_apply(C, op, event);
@@ -2350,7 +2360,9 @@ static int flatten_len(SpaceText *st, const char *str)
                if (str[i] == '\t') {
                        total += st->tabnumber - total % st->tabnumber;
                }
-               else total++;
+               else {
+                       total++;
+               }
        }
        
        return total;
@@ -2412,7 +2424,9 @@ static void text_cursor_set_to_pos_wrapped(SpaceText *st, ARegion *ar, int x, in
                                chars = st->tabnumber - i % st->tabnumber;
                                ch = ' ';
                        }
-                       else chars = 1;
+                       else {
+                               chars = 1;
+                       }
                        
                        while (chars--) {
                                /* Gone too far, go back to last wrap point */
@@ -2503,7 +2517,7 @@ static void text_cursor_set_to_pos(SpaceText *st, ARegion *ar, int x, int y, int
 {
        Text *text = st->text;
        text_update_character_width(st);
-       y = (ar->winy - 2 - y) / (st->lheight + TXT_LINE_SPACING);
+       y = (ar->winy - 2 - y) / (st->lheight_dpi + TXT_LINE_SPACING);
 
        if (st->showlinenrs) x -= TXT_OFFSET + TEXTXLOC;
        else x -= TXT_OFFSET;
@@ -3110,27 +3124,31 @@ static int text_resolve_conflict_invoke(bContext *C, wmOperator *op, wmEvent *UN
                case 1:
                        if (text->flags & TXT_ISDIRTY) {
                                /* modified locally and externally, ahhh. offer more possibilites. */
-                               pup = uiPupMenuBegin(C, "File Modified Outside and Inside Blender", ICON_NONE);
+                               pup = uiPupMenuBegin(C, IFACE_("File Modified Outside and Inside Blender"), ICON_NONE);
                                layout = uiPupMenuLayout(pup);
-                               uiItemEnumO_ptr(layout, op->type, "Reload from disk (ignore local changes)", 0, "resolution", RESOLVE_RELOAD);
-                               uiItemEnumO_ptr(layout, op->type, "Save to disk (ignore outside changes)", 0, "resolution", RESOLVE_SAVE);
-                               uiItemEnumO_ptr(layout, op->type, "Make text internal (separate copy)", 0, "resolution", RESOLVE_MAKE_INTERNAL);
+                               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);
                                uiPupMenuEnd(C, pup);
                        }
                        else {
-                               pup = uiPupMenuBegin(C, "File Modified Outside Blender", ICON_NONE);
+                               pup = uiPupMenuBegin(C, IFACE_("File Modified Outside Blender"), ICON_NONE);
                                layout = uiPupMenuLayout(pup);
-                               uiItemEnumO_ptr(layout, op->type, "Reload from disk", 0, "resolution", RESOLVE_RELOAD);
-                               uiItemEnumO_ptr(layout, op->type, "Make text internal (separate copy)", 0, "resolution", RESOLVE_MAKE_INTERNAL);
-                               uiItemEnumO_ptr(layout, op->type, "Ignore", 0, "resolution", RESOLVE_IGNORE);
+                               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);
                                uiPupMenuEnd(C, pup);
                        }
                        break;
                case 2:
-                       pup = uiPupMenuBegin(C, "File Deleted Outside Blender", ICON_NONE);
+                       pup = uiPupMenuBegin(C, IFACE_("File Deleted Outside Blender"), ICON_NONE);
                        layout = uiPupMenuLayout(pup);
-                       uiItemEnumO_ptr(layout, op->type, "Make text internal", 0, "resolution", RESOLVE_MAKE_INTERNAL);
-                       uiItemEnumO_ptr(layout, op->type, "Recreate file", 0, "resolution", RESOLVE_SAVE);
+                       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);
                        uiPupMenuEnd(C, pup);
                        break;
        }