hschumann2/TempleOS-Source-Code
0832
1 2#help_index "DolDoc/Misc"3 4U0 EdReplaceTroubleOne(CDoc *doc,U8 *st_original,U8 *st_safe,I64 num,5 Bool to_safe,Bool sel)6{7 U8 buf[STR_LEN];8 StrPrint(buf,st_safe,num);9 if (to_safe)10 EdReplace(doc,st_original,buf,sel);11 else12 EdReplace(doc,buf,st_original,sel);13}14 15U0 EdReplaceTroubleAll(CDoc *doc,Bool to_safe,Bool sel)16{17 I64 i=0;18 EdReplaceTroubleOne(doc,"#assert" ,"//<@%[email protected]>" ,i++,to_safe,sel);19 EdReplaceTroubleOne(doc,"#define" ,"//<@%[email protected]>" ,i++,to_safe,sel);20 EdReplaceTroubleOne(doc,"#include","//<@%[email protected]>" ,i++,to_safe,sel);21//#if will match #if,#ifdef,#ifndef,#ifaot and #ifjit22 EdReplaceTroubleOne(doc,"#if" ,"//<@%[email protected]>" ,i++,to_safe,sel);23 EdReplaceTroubleOne(doc,"#endif" ,"//<@%[email protected]>" ,i++,to_safe,sel);24//Convert #exe to union because we want that indent pattern.25 EdReplaceTroubleOne(doc,"#exe" ,"union @%[email protected]",i++,to_safe,sel);26 EdReplaceTroubleOne(doc,"'{'" ,"'<@%[email protected]>'" ,i++,to_safe,sel);27 EdReplaceTroubleOne(doc,"'}'" ,"'<@%[email protected]>'" ,i++,to_safe,sel);28}29 30#define C_INDENT_SPACES 231#define ASM_RENUM_SPACING 532 33#define EF_REINDENT 034#define EF_CMP_CHK 135#define EF_RENUM_ASM 236#define EF_CTRL_SLIDER 337#define EF_CH_SC 438 39I64 PopUpEdFmt()40{41 I64 i;42 CDoc *doc=DocNew;43 DocPrint(doc,"$LTBLUE$$MU,\"Compile Check\",LE=EF_CMP_CHK$\n"44 "$MU,\"Reindent HolyC Fun (Beware braces in strings.)\","45 "LE=EF_REINDENT$\n"46 "$MU,\"Renum Asm Local @@ Labels for Fun\",LE=EF_RENUM_ASM$\n"47 "$MU,\"Insert Template Code: Ctrl Slider\",LE=EF_CTRL_SLIDER$\n"48 "$MU,\"Insert ASCII/Scan Code Hex Codes for key pressed\","49 "LE=EF_CH_SC$\n\n"50 "$MU,\"CANCEL\",LE=DOCM_CANCEL$\n\n"51 "$GREEN$<ALT-BACKSPACE>$FG$ to undo if not happy\n"52 "with the ress.\n");53 i=PopUpMenu(doc);54 DocDel(doc);55 return i;56}57 58class CRILex59{60 CCmpCtrl *cc1,*cc2;61 CQueVectU8 *indent;62 I64 depth,exp_depth,one_shot;63 Bool was_new_line,is_not_cont;64};65 66I64 EdRILex(CRILex *rx)67{68 rx->is_not_cont=FALSE;69 I64 i;70 CLexFile *tmpf;71 do {72 Lex(rx->cc1);73 Lex(rx->cc2);74 i=PrsKeyWord(rx->cc2);75 if (rx->cc1->token=='\n' && rx->cc2->token==';' || rx->cc2->token=='{' ||76 rx->cc2->token=='}' || rx->cc2->token==':' || rx->cc2->token==')' &&77 !rx->exp_depth || i==KW_ELSE || i==KW_CATCH || i==KW_DO)78 rx->is_not_cont=TRUE;79 if (rx->was_new_line && (rx->cc1->token!=':' || i==KW_CASE ||80 i==KW_DFT || i==KW_START || i==KW_END)) {81 tmpf=rx->cc2->lex_include_stk;82 while (tmpf->next)83 tmpf=tmpf->next;84 QueVectU8Put(rx->indent,tmpf->cur_entry->y,rx->depth+rx->one_shot);85 rx->one_shot=0;86 }87 if (rx->cc2->token=='\n')88 rx->was_new_line=TRUE;89 else90 rx->was_new_line=FALSE;91 } while (rx->cc1->token=='\n');92 return rx->cc1->token;93}94 95U0 EdRIExp(CRILex *rx)96{97 if (rx->cc1->token=='(') {98 if (!rx->exp_depth++)99 rx->depth+=3;100 EdRILex(rx);101 while (rx->cc1->token && rx->cc1->token!=')')102 EdRIExp(rx);103 if (!--rx->exp_depth) {104 rx->depth-=3;105 if (rx->depth<0) rx->depth=0;106 }107 } else if (rx->cc1->token=='[') {108 if (!rx->exp_depth++)109 rx->depth+=3;110 EdRILex(rx);111 while (rx->cc1->token && rx->cc1->token!=']')112 EdRIExp(rx);113 if (!--rx->exp_depth) {114 rx->depth-=3;115 if (rx->depth<0) rx->depth=0;116 }117 }118 EdRILex(rx);119}120 121U0 EdRIStmt(CRILex *rx,Bool indent)122{123 I64 i;124 Bool cont;125 if (rx->cc1->token=='{') {126 rx->depth++;127 EdRILex(rx);128 while (rx->cc1->token && rx->cc1->token!='}')129 EdRIStmt(rx,FALSE);130 if (--rx->depth<0) rx->depth=0;131 EdRILex(rx);132 } else {133 if (indent) rx->depth++;134 do {135 cont=FALSE;136 switch (PrsKeyWord(rx->cc1)) {137 case KW_IF:138 EdRILex(rx);139 EdRIExp(rx);140 EdRIStmt(rx,TRUE);141 if (PrsKeyWord(rx->cc1)==KW_ELSE) {142 EdRILex(rx);143 if (PrsKeyWord(rx->cc1)==KW_IF && rx->cc2->token!='\n')144 EdRIStmt(rx,FALSE);145 else146 EdRIStmt(rx,TRUE);147 }148 break;149 case KW_TRY:150 EdRILex(rx);151 EdRIStmt(rx,TRUE);152 if (PrsKeyWord(rx->cc1)==KW_CATCH) {153 EdRILex(rx);154 EdRIStmt(rx,TRUE);155 }156 break;157 case KW_LOCK:158 EdRILex(rx);159 EdRIStmt(rx,TRUE);160 break;161 case KW_FOR:162 case KW_WHILE:163 EdRILex(rx);164 EdRIExp(rx);165 EdRIStmt(rx,TRUE);166 break;167 case KW_ASM:168 case KW_CLASS:169 case KW_UNION:170 if (EdRILex(rx)==TK_IDENT)171 EdRILex(rx);172 EdRIStmt(rx,TRUE);173 break;174 case KW_DO:175 EdRILex(rx);176 EdRIStmt(rx,TRUE);177 if (PrsKeyWord(rx->cc1)==KW_WHILE) {178 EdRILex(rx);179 EdRIExp(rx);180 }181 if (rx->cc1->token==';')182 EdRILex(rx);183 break;184 case KW_SWITCH:185 EdRILex(rx);186 EdRIExp(rx);187 if (rx->cc1->token=='{') {188 rx->depth++;189 EdRILex(rx);190 i=0;191 while (rx->cc1->token && rx->cc1->token!='}') {192 switch (PrsKeyWord(rx->cc1)) {193 case KW_START:194 rx->depth+=i; i=0;195 while (EdRILex(rx) && rx->cc1->token!=':');196 EdRILex(rx);197 i++;198 break;199 case KW_END:200 rx->depth+=i; i=0;201 if (--rx->depth<0) rx->depth=0;202 while (EdRILex(rx) && rx->cc1->token!=':');203 EdRILex(rx);204 break;205 case KW_CASE:206 case KW_DFT:207 rx->depth+=i; i=0;208 while (EdRILex(rx) && rx->cc1->token!=':');209 EdRILex(rx);210 break;211 default:212 if (rx->cc1->token)213 EdRIStmt(rx,TRUE);214 }215 }216 if (--rx->depth<0) rx->depth=0;217 EdRILex(rx);218 }219 break;220 default:221 if (rx->cc1->token==TK_IDENT && rx->cc1->hash_entry &&222 rx->cc1->hash_entry->type&(HTT_OPCODE|HTT_ASM_KEYWORD)) {223// rx->one_shot=4-rx->depth;224 do EdRILex(rx);225 while (rx->cc2->token && rx->cc2->token!='\n');226 rx->is_not_cont=TRUE;227 } else {228 while (rx->cc1->token && rx->cc1->token!=';' &&229 rx->cc1->token!=':') {230 if (rx->cc2->token=='\n' && !rx->is_not_cont)231 rx->one_shot=3;232 EdRILex(rx);233 }234 if (rx->cc1->token==':')235 cont=TRUE;236 EdRILex(rx);237 }238 }239 } while (cont && rx->cc1->token!='}');240 if (indent && --rx->depth<0)241 rx->depth=0;242 }243}244 245CQueVectU8 *EdRICode(CDoc *doc)246{247 CQueVectU8 *res;248 CRILex *rx=CAlloc(sizeof(CRILex));249 250 rx->cc1=CmpCtrlNew(,CCF_KEEP_NEW_LINES|CCF_DONT_FREE_BUF,doc->filename.name);251 Free(rx->cc1->lex_include_stk->full_name);252 LexAttachDoc(rx->cc1,rx->cc1->lex_include_stk,doc,,253 doc->cur_entry,doc->cur_col);254 255 rx->cc2=CmpCtrlNew(,CCF_KEEP_NEW_LINES|CCF_DONT_FREE_BUF,doc->filename.name);256 Free(rx->cc2->lex_include_stk->full_name);257 LexAttachDoc(rx->cc2,rx->cc2->lex_include_stk,doc,,258 doc->cur_entry,doc->cur_col);259 260 rx->indent=QueVectU8New(doc->cur_entry->y);261 262 Lex(rx->cc1);263 EdRIStmt(rx,FALSE);264 265 CmpCtrlDel(rx->cc1);266 CmpCtrlDel(rx->cc2);267 res=rx->indent;268 Free(rx);269 return res;270}271 272U0 EdRemFunLeadingSpace(CDoc *doc)273{274 Bool unlock=DocLock(doc),275 start_of_line=TRUE;276 U8 *ptr;277 I64 ch,levels=1;278 CDocEntry *doc_e,*doc_e2;279 280 EdGoToFun(doc,FALSE,FALSE);281 doc_e=doc->cur_entry->next;282 do {283 doc_e2=doc_e->next;284 if (doc_e!=doc && doc_e!=doc->cur_entry &&285 !(doc_e->de_flags&(DOCEG_DONT_EDIT-DOCEF_SCROLLING_X)))286 switch (doc_e->type_u8) {287 case DOCT_TEXT:288 ptr=doc_e->tag;289 if (start_of_line) {290 while (*ptr==CH_SPACE)291 ptr++;292 if (*ptr)293 start_of_line=FALSE;294 ptr=StrNew(ptr,doc->mem_task);295 Free(doc_e->tag);296 doc_e->tag=ptr;297 }298 if (!*ptr)299 DocEntryDel(doc,doc_e);300 else {301 while (ch=*ptr++)302 if (ch=='{')303 levels++;304 else if (ch=='}') {305 if (!--levels)306 break;307 }308 if (!levels) goto ls_done;309 }310 break;311 case DOCT_TAB:312 if (start_of_line)313 DocEntryDel(doc,doc_e);314 break;315 case DOCT_NEW_LINE:316 start_of_line=TRUE;317 break;318 default:319 start_of_line=FALSE;320 }321 doc_e=doc_e2;322 } while (doc_e!=doc->cur_entry);323ls_done:324 DocRecalc(doc);325 DocCenter(doc);326 if (unlock)327 DocUnlock(doc);328}329 330class CRenum331{332 CRenum *next,*last;333 U8 label[sizeof(CEdFindText.find_text)];334};335 336I64 EdRAGetU8(CDoc *doc)337{338 I64 res=-1;339 while (doc->cur_entry!=doc &&340 doc->cur_entry->type&DOCET_SEL && res<0) {341 res=EdCurU8(doc);342 EdCursorRight(doc);343 }344 return res;345}346 347U0 EdRACollect(CDoc *doc,CRenum *head)348{349 I64 ch,i;350 CRenum *tmpr;351 U8 buf[sizeof(CEdFindText.find_text)];352 ch=EdRAGetU8(doc);353 while (ch>=0) {354 if (ch!='@')355 ch=EdRAGetU8(doc);356 else {357 ch=EdRAGetU8(doc);358 if (ch=='@') {359 ch=EdRAGetU8(doc);360 StrCpy(buf,"@@");361 i=2;362 while (ch>=0 && i<sizeof(CEdFindText.find_text)) {363 if (Bt(char_bmp_alpha_numeric,ch))364 buf[i++]=ch;365 else366 break;367 ch=EdRAGetU8(doc);368 }369 if (i<sizeof(CEdFindText.find_text)) {370 buf[i++]=0;371 while (ch>=0 && Bt(char_bmp_white_space,ch))372 ch=EdRAGetU8(doc);373 if (ch==':') {374 ch=EdRAGetU8(doc);375 tmpr=MAlloc(sizeof(CRenum));376 StrCpy(tmpr->label,buf);377 QueIns(tmpr,head->last);378 }379 }380 }381 }382 }383//This is needed because we moved the384 //cursor and it didn't recalc.385 DocRecalc(doc);386}387 388U0 EdRenumAsm(CDoc *doc)389{390 Bool unlock=DocLock(doc);391 I64 num=0;392 CRenum head,*tmpr,*tmpr1;393 U8 buf[sizeof(CEdFindText.find_text)],394 buf2[sizeof(CEdFindText.find_text)];395 396 QueInit(&head);397 EdSelFun(doc,TRUE);398 EdRACollect(doc,&head);399 400 tmpr=head.next;401 while (tmpr!=&head) {402 tmpr1=tmpr->next;403 num+=ASM_RENUM_SPACING;404 StrPrint(buf,"@#%02d",num);405 EdReplace(doc,tmpr->label,buf,TRUE,TRUE,TRUE);406 Free(tmpr);407 tmpr=tmpr1;408 }409 410 while (num) {411 StrPrint(buf, "@#%02d",num);412 StrPrint(buf2,"@@%02d",num);413 EdReplace(doc,buf,buf2,TRUE,TRUE,TRUE);414 num-=ASM_RENUM_SPACING;415 }416 EdSelAll(doc,FALSE);417 DocRecalc(doc);418 DocCenter(doc);419 if (unlock)420 DocUnlock(doc);421}422 423U0 EdCodeTools2(CDoc *doc,I64 tool_action,Bool beep=TRUE)424{425 Bool okay,unlock=DocLock(doc),start_of_line=TRUE;426 CDocEntry *doc_e,*doc_ne;427 I64 i,start_y,end_y,x,r,goto_line_num;428 U8 *b,*st,*st2,*prj_file;429 CTask *task=NULL;430 CJob *tmpc;431 CQueVectU8 *indent;432 433 DocRecalc(doc);434 goto_line_num=doc->cur_entry->y+1;435 436 DocCaptureUndo(doc,TRUE);437 switch (tool_action) {438 case EF_CMP_CHK:439 okay=FALSE;440 if (doc->flags&DOCF_PLAIN_TEXT)441 DocFlagsToggle(doc,DOCF_PLAIN_TEXT);442 DocWrite(doc);443 task=Spawn(&SrvCmdLine,NULL,"Srv",,Fs);444 st2=DirCur;445 st=MStrPrint("Cd(\"%s\");",st2);446 tmpc=TaskExe(task,Fs,st,1<<JOBf_WAKE_MASTER|1<<JOBf_FOCUS_MASTER);447 Free(st2);448 Free(st);449 WinHorz(Fs->win_left,Fs->win_right, task);450 WinVert(Fs->win_top, Fs->win_bottom,task);451 if (JobResScan(tmpc,&r)) {452 st=DirFile(doc->filename.name,,"PRJ.Z");453 prj_file=FileNameAbs(st,FUF_Z_OR_NOT_Z);454 Free(st);455 if (FileFind(prj_file)) {456 st2=DirFile(prj_file),457 st=MStrPrint("Cd(\"%s\");",st2);458 Free(st2);459 tmpc=TaskExe(task,Fs,st,1<<JOBf_WAKE_MASTER|460 1<<JOBf_FOCUS_MASTER|1<<JOBf_FREE_ON_COMPLETE);461 Free(st);462 st=MStrPrint("\"$WW,1$\";Cmp(\"%s\",\"SysTmp\",\"SysTmp\");",463 prj_file);464 tmpc=TaskExe(task,Fs,st,1<<JOBf_WAKE_MASTER|1<<JOBf_FOCUS_MASTER);465 Free(st);466 if (JobResScan(tmpc,&r))467 if (!r) {468 tmpc=TaskExe(task,Fs,469 "Load(\"SysTmp\",LDF_JUST_LOAD);",470 1<<JOBf_WAKE_MASTER|1<<JOBf_FOCUS_MASTER);471 if (JobResScan(tmpc,&r))472 okay=TRUE;473 }474 tmpc=TaskExe(task,Fs,"Del(\"SysTmp.*\");",475 1<<JOBf_WAKE_MASTER|1<<JOBf_FOCUS_MASTER);476 JobResScan(tmpc,&r);477 } else {478 Free(prj_file);479 st=DirFile(doc->filename.name,"Load","HC.Z");480 prj_file=FileNameAbs(st,FUF_Z_OR_NOT_Z);481 Free(st);482 if (FileFind(prj_file))483 st=MStrPrint("\"$WW,1$\";ExeFile(\"%s\",CCF_JUST_LOAD);",prj_file);484 else485 st=MStrPrint("\"$WW,1$\";ExeFile(\"%s\",CCF_JUST_LOAD);",486 doc->filename.name);487 tmpc=TaskExe(task,Fs,st,1<<JOBf_WAKE_MASTER|1<<JOBf_FOCUS_MASTER);488 Free(st);489 if (JobResScan(tmpc,&r) && r)490 okay=TRUE;491 }492 Free(prj_file);493 }494 if (!okay) {495 PopUpOk("Has Errors");496 while (LBts(&sys_semas[SEMA_FIX],0))497 Yield;498 ToFileLine(dbg.fix_file_line,&st,&i);499 LBtr(&sys_semas[SEMA_FIX],0);500 if (!StrCmp(st,doc->filename.name))501 goto_line_num=i;502 Free(st);503 }504 break;505 case EF_REINDENT:506 start_y=doc->cur_entry->y;507 EdReplaceTroubleAll(doc,TRUE,FALSE);508 DocGoToLine(doc,start_y+1);509 if (EdGoToFun(doc,FALSE,FALSE)) {510 start_y=doc->cur_entry->y;511 indent=EdRICode(doc);512 DocUnlock(doc);513 if (beep) {514 Snd(86); Sleep(150); Snd;515 Sleep(100);516 Snd(86); Sleep(150); Snd;517 }518 DocLock(doc);519 EdRemFunLeadingSpace(doc);520 DocGoToLine(doc,start_y+1);521 doc_e=doc->cur_entry;522 end_y=start_y+indent->total_cnt;523 while (start_y<=doc_e->y<end_y) {524 if (doc_e!=doc && doc_e!=doc->cur_entry &&525 !(doc_e->de_flags&(DOCEG_DONT_EDIT-DOCEF_SCROLLING_X))) {526 if (doc_e->type_u8==DOCT_NEW_LINE||527 doc_e->type_u8==DOCT_SOFT_NEW_LINE)528 start_of_line=TRUE;529 else {530 if (start_of_line) {531 i=QueVectU8Get(indent,doc_e->y)*C_INDENT_SPACES;532 x=doc_e->x+1;533 while (i>8) {534 doc_ne=DocEntryNewBase(doc,535 DOCT_TAB|doc->settings_head.dft_text_attr<<8,,536 x,doc_e->y,doc_e->page_line_num);537 MemCpy(&doc_ne->settings,538 &doc_e->settings,sizeof(CDocSettings));539 QueIns(doc_ne,doc_e->last);540 i-=8;541 x+=8;542 }543 if (i>0) {544 b=MAlloc(i+1,doc->mem_task);545 MemSet(b,CH_SPACE,i);546 b[i]=0;547 doc_ne=DocEntryNewBase(doc,548 DOCT_TEXT|doc->settings_head.dft_text_attr<<8,,549 x,doc_e->y,doc_e->page_line_num);550 doc_ne->tag=b;551 doc_ne->max_col=1;552 MemCpy(&doc_ne->settings,553 &doc_e->settings,sizeof(CDocSettings));554 QueIns(doc_ne,doc_e->last);555 }556 }557 start_of_line=FALSE;558 }559 }560 doc_e=doc_e->next;561 }562 QueVectU8Del(indent);563 }564 start_y=doc->cur_entry->y;565 EdReplaceTroubleAll(doc,FALSE,FALSE);566 DocGoToLine(doc,start_y+1);567 break;568 case EF_RENUM_ASM:569 if (EdGoToFun(doc,FALSE,TRUE)) {570 if (EdCurU8(doc)=='{') {571 EdCursorRight(doc);572 DocRecalc(doc);573 } else if (EdCurU8(doc)==':') {574 EdCursorRight(doc);575 if (EdCurU8(doc)==':')576 EdCursorRight(doc);577 DocRecalc(doc);578 }579 DocUnlock(doc);580 if (beep) {581 Snd(86); Sleep(150); Snd;582 Sleep(100);583 Snd(86); Sleep(150); Snd;584 }585 DocLock(doc);586 EdRenumAsm(doc);587 }588 break;589 }590 591 DocRecalc(doc);592 DocGoToLine(doc,goto_line_num);593 594 DocUnlock(doc);595 if (!unlock)596 DocLock(doc);597 if (task)598 Kill(task,FALSE);599}600 601U0 EdPopUpChSC(I64 *_ch,I64 *_sc)602{603 I64 sc;604 "Press A Key\n";605 DocPut->flags|=DOCF_SIZE_MIN;606 do GetMsg(_ch,&sc,1<<MSG_KEY_DOWN);607 while (sc.u8[0]==SC_SHIFT || sc.u8[0]==SC_CTRL || sc.u8[0]==SC_ALT);608 *_sc=sc;609}610 611U0 EdChSC(CDoc *doc)612{613 I64 ch,sc;614 U8 buf[STR_LEN];615 StrPrint(buf,"EdPopUpChSC(%d,%d);",&ch,&sc);616 PopUp(buf,Fs);617 if (ch==CH_BACKSPACE)618 DocPrint(doc,"CH_BACKSPACE,0x%X",sc);619 else if (ch=='\n')620 DocPrint(doc,"'\n',0x%X",sc);621 else if (CH_CTRLA<=ch<=CH_CTRLZ)622 DocPrint(doc,"CH_CTRL%C,0x%X",ch+'@',sc);623 else if (ch=='$')624 DocPrint(doc,"'$$',0x%X",sc);625 else if (ch=='\\')626 DocPrint(doc,"'\\\\',0x%X",sc);627 else if (ch=='\'')628 DocPrint(doc,"'\\\'',0x%X",sc);629 else if (ch==CH_ESC)630 DocPrint(doc,"CH_ESC,0x%X",sc);631 else if (ch==CH_SHIFT_ESC)632 DocPrint(doc,"CH_SHIFT_ESC,0x%X",sc);633 else if (ch==CH_SPACE)634 DocPrint(doc,"CH_SPACE,0x%X",sc);635 else if (ch==CH_SHIFT_SPACE)636 DocPrint(doc,"CH_SHIFT_SPACE,0x%X",sc);637 else if (Bt(char_bmp_displayable,ch))638 DocPrint(doc,"'%c',0x%X",ch,sc);639 else640 DocPrint(doc,"0x%X,0x%X",ch,sc);641}642 643U0 EdCodeTools(CDoc *doc)644{645 I64 tool_action=PopUpEdFmt;646 switch (tool_action) {647 case EF_CMP_CHK:648 case EF_REINDENT:649 case EF_RENUM_ASM:650 EdCodeTools2(doc,tool_action);651 break;652 case EF_CTRL_SLIDER:653 TemplateCtrlSlider(doc);654 break;655 case EF_CH_SC:656 EdChSC(doc);657 break;658 }659}660 