editor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Text Editor
  2. #include "Workspace.h"
  3. int onopen_gotoline=-1;
  4. char *onopen_gotofile=NULL;
  5. //void Edit(SYM_STR sym_text,char *text_name) {
  6. void Edit(char *text_path) {
  7. if (!(Port=malloc(LCD_SIZE)))
  8. return;
  9. PortSet(Port,239,127);
  10. onopen_gotoline=-1;
  11. editor_reopen:
  12. if (onopen_gotofile!=NULL)
  13. text_path = onopen_gotofile, onopen_gotofile = NULL;
  14. char sym_buf[1+8+1+8+1];
  15. *sym_buf = 0;
  16. strncpy(sym_buf+1,text_path,sizeof(sym_buf)-1);
  17. SYM_STR sym_text = sym_buf+1;
  18. while (*sym_text) sym_text++;
  19. char *text_name = strrchr(text_path,'\\');
  20. if (!text_name)
  21. text_name = text_path;
  22. editor_reopen_samefile: {
  23. int key,k,n,z=0;
  24. void *ptr; int *kp;
  25. unsigned int ST_f;
  26. #ifdef HD2
  27. #define hdx hd2
  28. HANDLE hd2;
  29. #else
  30. #define hdx hd
  31. #endif
  32. int CU_state,KID,BKD,EDH_proc;
  33. HSym hs; int arch=0;
  34. if (!(hs=SymFind(sym_text)).folder)
  35. goto editor_quit;
  36. hdx=DerefSym(hs)->handle;
  37. NLINES=(LCD_HEIGHT-10)/6;
  38. YMAX=2+6*NLINES;
  39. #ifdef TITLEBAR
  40. NLINES--;
  41. #endif
  42. hs=SymFind(sym_text); // because malloc may cause garbage collection (shouldn't change HSym, but...)
  43. if (DerefSym(hs)->flags.bits.archived)
  44. arch=1
  45. #ifndef HD2
  46. , EM_moveSymFromExtMem(sym_text,HS_NULL), hdx=DerefSym(hs)->handle
  47. #endif
  48. ;
  49. #ifdef HD2
  50. hsize=*(unsigned int *)HeapDeref(hdx);
  51. hd=HeapAlloc(2+hsize);
  52. if (!hd) return;
  53. ptr=HLock(hd);
  54. memcpy(ptr,HeapDeref(hdx),2+hsize);
  55. #else
  56. ptr=HLock(hd);
  57. #endif
  58. tptr=ptr+4;
  59. spos=0; selpos=0; sel1=0; sel2=0;
  60. curpos=cpos=((unsigned int *)ptr)[1];
  61. if (onopen_gotoline>=0) {
  62. cpos=1;
  63. while (--onopen_gotoline)
  64. while (tptr[cpos]) {
  65. if (tptr[cpos]==NEWLINE) {
  66. cpos+=2;
  67. break;
  68. }
  69. cpos++;
  70. }
  71. //cpos=down(cpos,0);
  72. curpos=cpos;
  73. onopen_gotoline=-1;
  74. }
  75. /* tptr=" test-123123123123123123123123123123123123123\n \n \n OK :)"
  76. "\n \n \n end\n \n \n of\n \n \n file"
  77. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  78. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";*/
  79. /* tptr=" test-123123123123123123123123123123123123123\n \n \n \n \n OK :)"
  80. "\n \n \n \n \n end\n \n \n \n \n of\n \n \n \n \n file\0\0\0\0\0\0\0";*/
  81. size=strlen(tptr); hsize=*((unsigned int *)ptr);
  82. if (!cpos || cpos>size)
  83. cpos=1;
  84. InitSearch();
  85. // is_nl=1;
  86. CU_state=CU_start();
  87. scr_sptr=scr_stk;
  88. #ifndef PEDROM
  89. KID=OSInitKeyInitDelay(cKID);
  90. BKD=OSInitBetweenKeyDelay(cBKD);
  91. #endif
  92. view_cursor();
  93. msg="Welcome to GTC IDE !";
  94. curword[0]=0;
  95. pchnum=0;
  96. last_act=0; ac_on=0; ac_disp=0;
  97. add_all_pch();
  98. //add_pch("std");
  99. //add_pch("stdhead");
  100. //add_pch("gen");
  101. //add_pch("extgraph");
  102. //add_pch("graphx");
  103. //add_pch("keywords");
  104. OSTimerRestart(APD_TIMER);
  105. {
  106. #if 0
  107. char *p=(char *)sym_text;
  108. while (*--p);
  109. strcpy(curFileFullName,p+1);
  110. #else
  111. strcpy(curFileFullName,text_path);
  112. #endif
  113. }
  114. strcpy(curFileName,text_name);
  115. strcpy(curFileExt,GetFTString(hdx));
  116. strtolower(curFileExt);
  117. // WspNew();
  118. // WspNew();
  119. editor_exit_requested=0; // as the init in the loop is not sufficient...
  120. do {
  121. unsigned int par1s=par1;
  122. memset(Port,0,LCD_SIZE);
  123. par1=0; par2=0;
  124. if (!sel1) {
  125. unsigned int pos=cpos;
  126. int deep=0;
  127. while (pos>1 && tptr[pos-2]!=NEWLINE) {
  128. pos--;
  129. if (tptr[pos]==')') deep--;
  130. if (tptr[pos]=='(' && (++deep)>0) { par1=pos; break; }
  131. }
  132. if (par1) {
  133. while (tptr[pos] && tptr[pos]!=NEWLINE) {
  134. pos++;
  135. if (tptr[pos]=='(') deep++;
  136. if (tptr[pos]==')' && (--deep)<=0) { par2=pos; break; }
  137. }
  138. if (!par2) par1=0;
  139. }
  140. }
  141. if (chk_curword(curword,cpos,1) || (!curword[0] && par1!=par1s)) {
  142. // char *tp;
  143. nmatch=0;
  144. if (curword[0] && (msg=pch_search(curword,strlen(curword)>=2,MAX_MATCH,match)))
  145. msg=msg;
  146. else if (chk_curword(parword,par1,0) && (msg=pch_search(parword,0,0,NULL))) msg=msg;
  147. else msg=0;
  148. if (msg) { msg=memcpy(msgBuf,msg,58); msg[58]=0; }
  149. ac_disp=nmatch?last_act:0;
  150. ac_on=0;
  151. }
  152. display(z);
  153. if (ac_disp) ac_display(ac_on);
  154. #ifndef RELEASE
  155. if (xcur<0) msg="Cursor not encountered", xcur=1;
  156. if (!curpos || tptr[curpos-1]==NEWLINE) msg="Invalid curpos";
  157. #endif
  158. DrawStatus();
  159. LCD_restore(Port);
  160. in_loop:
  161. ST_f=ST_flagsL;
  162. while (!(key=kbhit()) && !OSTimerExpired(CURSOR_TIMER)
  163. && !OSTimerExpired(APD_TIMER) && ST_f==ST_flagsL)
  164. idle();
  165. if (ST_f!=ST_flagsL) {
  166. DrawStatus();
  167. LCD_restore(Port);
  168. goto in_loop;
  169. } else if (OSTimerExpired(APD_TIMER)) {
  170. Off();
  171. OSTimerRestart(APD_TIMER);
  172. continue;
  173. }
  174. OSTimerRestart(CURSOR_TIMER);
  175. z=~z;
  176. if (!key) continue;
  177. OSTimerRestart(APD_TIMER);
  178. last_act=0;
  179. key=ngetchx();
  180. editor_exit_requested=0;
  181. KeyProcess(key,TM_MULTILINE);
  182. z=0;
  183. } while (key!=KEY_ESC && !editor_exit_requested);
  184. close_pch();
  185. #ifndef PEDROM
  186. OSInitKeyInitDelay(KID);
  187. OSInitBetweenKeyDelay(BKD);
  188. #endif
  189. if (!CU_state) CU_stop();
  190. ((unsigned int *)tptr)[-1]=cpos;
  191. ((unsigned int *)tptr)[-2]=size+2+1+1;
  192. FreeMem();
  193. #ifdef HD2
  194. if (memcmp(tptr,2+2+HeapDeref(hdx),size+1+1)) {
  195. SimpleDlg(NULL,"\n Saving file"DOTS" \n",B_CENTER,W_NOKEY);
  196. LCD_restore(Port);
  197. hs=SymFind(sym_text);
  198. if (arch)
  199. EM_moveSymFromExtMem(sym_text,HS_NULL), hdx=DerefSym(hs)->handle;
  200. HeapFree(hdx);
  201. HeapUnlock(hd);
  202. HeapRealloc(hd,size+2+2+1+1);
  203. hs=SymFind(sym_text);
  204. DerefSym(hs)->handle=hd;
  205. if (arch)
  206. if (!EM_moveSymToExtMem(sym_text,HS_NULL))
  207. ST_helpMsg("!!! MEMORY !!!"),ngetchx();
  208. } else HeapUnlock(hd), HeapFree(hd);
  209. #else
  210. HeapUnlock(hd);
  211. HeapRealloc(hd,size+2+2+1+1);
  212. if (arch)
  213. EM_moveSymToExtMem(sym_text,HS_NULL);
  214. #endif
  215. if (editor_exit_requested==EDEX_COMPILE) {
  216. SimpleDlg(NULL,"\n Compiling project"DOTS" \n",B_CENTER,W_NOKEY);
  217. LCD_restore(Port);
  218. Compile(curFileFullName,PLUGIN_GTC);
  219. goto editor_reopen;
  220. }
  221. }
  222. editor_quit:
  223. PortRestore();
  224. free(Port);
  225. }