diff options
author | Ricki Myers <antihc3@gmail.com> | 2006-03-05 22:50:14 +0300 |
---|---|---|
committer | Ricki Myers <antihc3@gmail.com> | 2006-03-05 22:50:14 +0300 |
commit | 7cf600be30090136e43cdbf6b5ea88c5f3c084a9 (patch) | |
tree | 6822e23f5559092db524f16cc9bf70a3379e4183 | |
parent | f325da228ca824bb61310e7883c6b8a209f38acb (diff) |
Adds menu item in text editor under format menu to convert whitespace to
Spaces or to tabs.
Adds function void convert_tabs(struct SpaceText *st, int tab)
int tab is eather 0 or 1; 1 if converting to tabs
I was going to make this auto run when running a script but did not know what that would do to the GE or any thing else.
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 1 | ||||
-rw-r--r-- | source/blender/include/BSE_headerbuttons.h | 1 | ||||
-rw-r--r-- | source/blender/src/drawtext.c | 105 | ||||
-rw-r--r-- | source/blender/src/header_text.c | 29 |
4 files changed, 136 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index 447a94dd487..d12b1f28570 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -94,6 +94,7 @@ void comment (struct Text *text); void indent (struct Text *text); void uncomment (struct Text *text); int setcurr_tab (struct Text *text); +void convert_tabs (struct SpaceText *st, int tab); /* Undo opcodes */ diff --git a/source/blender/include/BSE_headerbuttons.h b/source/blender/include/BSE_headerbuttons.h index e9aea3fbada..d405e2683a6 100644 --- a/source/blender/include/BSE_headerbuttons.h +++ b/source/blender/include/BSE_headerbuttons.h @@ -121,6 +121,7 @@ void spaceipo_assign_ipo(struct SpaceIpo *si, struct Ipo *ipo); /* header_text.c */ void do_text_editmenu_to3dmenu(void *arg, int event); +void do_text_formatmenu_convert(void *arg, int event); /* header_info.c */ void do_info_add_meshmenu(void *arg, int event); diff --git a/source/blender/src/drawtext.c b/source/blender/src/drawtext.c index 684acf15e10..23a881ea72a 100644 --- a/source/blender/src/drawtext.c +++ b/source/blender/src/drawtext.c @@ -2049,3 +2049,108 @@ static int check_numbers(char *string) } return 0; } + +void convert_tabs (struct SpaceText *st, int tab) +{ + Text *text = st->text; + TextLine *tmp; + char *check_line, *new_line, *format; + int a, j, extra, number; //unknown for now + + if (!text) return; + + tmp = text->lines.first; + + //first convert to all space, this make it alot easier to convert to tabs because there is no mixtures of ' ' && '\t' + while(tmp) { + check_line = tmp->line; + new_line = MEM_mallocN(render_string(check_line)+1, "Converted_Line"); + format = MEM_mallocN(render_string(check_line)+1, "Converted_Syntax_format"); + j = 0; + for (a=0; a < strlen(check_line); a++) { //foreach char in line + if(check_line[a] == '\t') { //checking for tabs + //get the number of spaces this tabs is showing + //i dont like doing it this way but will look into it later + new_line[j] = '\0'; + number = render_string(new_line); + new_line[j] = '\t'; + new_line[j+1] = '\0'; + number = render_string(new_line)-number; + for(extra = 0; extra < number; extra++) { + new_line[j] = ' '; + j++; + } + } else { + new_line[j] = check_line[a]; + ++j; + } + } + new_line[j] = '\0'; + // put new_line in the tmp->line spot still need to try and set the curc correctly + if (tmp->line) MEM_freeN(tmp->line); + if(tmp->format) MEM_freeN(tmp->format); + + tmp->line = new_line; + tmp->len = strlen(new_line); + tmp->format = format; + tmp = tmp->next; + } + + if (tab) // Converting to tabs + { //start over from the begining + tmp = text->lines.first; + + while(tmp) { + check_line = tmp->line; + extra = 0; + for (a = 0; a < strlen(check_line); a++) { + number = 0; + for (j = 0; j < st->tabnumber; j++) { + if ((a+j) <= strlen(check_line)) { //check to make sure we are not pass the end of the line + if(check_line[a+j] != ' ') { + number = 1; + } + } + } + if (!number) { //found all number of space to equal a tab + a = a+(st->tabnumber-1); + extra = extra+1; + } + } + + if ( extra > 0 ) { //got tabs make malloc and do what you have to do + new_line = MEM_mallocN(strlen(check_line)-(((st->tabnumber*extra)-extra)-1), "Converted_Line"); + format = MEM_mallocN(strlen(check_line)-(((st->tabnumber*extra)-extra)-1), "Converted_Syntax_format"); + extra = 0; //reuse vars + for (a = 0; a < strlen(check_line); a++) { + number = 0; + for (j = 0; j < st->tabnumber; j++) { + if ((a+j) <= strlen(check_line)) { //check to make sure we are not pass the end of the line + if(check_line[a+j] != ' ') { + number = 1; + } + } + } + if (!number) { //found all number of space to equal a tab + new_line[extra] = '\t'; + a = a+(st->tabnumber-1); + ++extra; + + } else { //not adding a tab + new_line[extra] = check_line[a]; + ++extra; + } + } + new_line[extra] = '\0'; + // put new_line in the tmp->line spot still need to try and set the curc correctly + if (tmp->line) MEM_freeN(tmp->line); + if(tmp->format) MEM_freeN(tmp->format); + + tmp->line = new_line; + tmp->len = strlen(new_line); + tmp->format = format; + } + tmp = tmp->next; + } + } +} diff --git a/source/blender/src/header_text.c b/source/blender/src/header_text.c index e0f1153f9d4..cfd85742672 100644 --- a/source/blender/src/header_text.c +++ b/source/blender/src/header_text.c @@ -425,6 +425,33 @@ static uiBlock *text_editmenu_selectmenu(void *arg_unused) return block; } +void do_text_formatmenu_convert(void *arg, int event) +{ + SpaceText *st= curarea->spacedata.first; + + switch(event) { + case 1: convert_tabs(st, 0); break; + case 2: convert_tabs(st, 1); break; + } + allqueue(REDRAWVIEW3D, 0); +} + +static uiBlock *text_formatmenu_convert(void *arg_unused) +{ + uiBlock *block; + short yco = 20, menuwidth = 120; + + block= uiNewBlock(&curarea->uiblocks, "do_text_formatmenu_convert", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin); + uiBlockSetButmFunc(block, do_text_formatmenu_convert, NULL); + + uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "To Spaces", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "Converts script whitespace to spaces based on Tab:"); + uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "To Tabs", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 2, "Converts script whitespace to tabs based on Tab:"); + + uiBlockSetDirection(block, UI_RIGHT); + uiTextBoundsBlock(block, 60); + return block; +} + /* Format menu */ static uiBlock *text_formatmenu(void *arg_unused) { @@ -440,6 +467,8 @@ static uiBlock *text_formatmenu(void *arg_unused) uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, ""); uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Comment", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, ""); uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Uncomment|Ctrl Shift D", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, ""); + uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, ""); + uiDefIconTextBlockBut(block, text_formatmenu_convert, NULL, ICON_RIGHTARROW_THIN, "Convert whitespace", 0, yco-=20, menuwidth, 19, ""); if(curarea->headertype==HEADERTOP) { uiBlockSetDirection(block, UI_DOWN); |