handlers.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Keyboard Handlers
  2. enum {
  3. TM_MULTILINE=0x1, TM_DIALOG=0x2,
  4. };
  5. void KeyProcess(int key,int tmode) {
  6. int *kp;
  7. int k,n;
  8. #ifdef EVHOOK
  9. int EDH_proc=0;
  10. keyproc:
  11. #endif
  12. kp=get_keylist();
  13. /* UP/DOWN mode=0/2nd/Dmd/Shift/2nd+Shift/Dmd+Shift */
  14. if (key==*kp++) {
  15. if (ac_disp) { ac_on=1; ScrollUp(&ac_s); }
  16. else if (tmode&TM_MULTILINE) cpos=up(curpos,xcur),tab_chk_r(cpos),selpos=0;
  17. /* else if (tmode&TM_DIALOG)
  18. DialogMsg(DLG_UP);*/
  19. } else if (key==*kp++) {
  20. if (ac_disp) { if (ac_on) ScrollDn(&ac_s); else ac_on=1; }
  21. else if (tmode&TM_MULTILINE) cpos=down(curpos,xcur),tab_chk_r(cpos),selpos=0;
  22. /* else if (tmode&TM_DIALOG)
  23. DialogMsg(DLG_DN);*/
  24. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  25. n=NLINES; cpos=curpos; while (n--) cpos=up(cpos,xcur);
  26. tab_chk_l(cpos); selpos=0;
  27. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  28. n=NLINES; cpos=curpos; while (n--) cpos=down(cpos,xcur);
  29. tab_chk_l(cpos); selpos=0;
  30. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  31. cpos=1; tab_chk_l(cpos); selpos=0;
  32. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  33. cpos=curpos; while (tptr[cpos]) cpos=down(cpos,xcur);
  34. tab_chk_l(cpos); selpos=0;
  35. } else if (key==*kp++) selpos=up(curpos,xcur),tab_chk_r(selpos);
  36. else if (key==*kp++) selpos=down(curpos,xcur),tab_chk_r(selpos);
  37. else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  38. n=NLINES; selpos=curpos; while (n--) selpos=up(selpos,xcur);
  39. tab_chk_l(selpos);
  40. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  41. n=NLINES; selpos=curpos; while (n--) selpos=down(selpos,xcur);
  42. tab_chk_l(selpos);
  43. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  44. selpos=1; tab_chk_l(selpos);
  45. } else if (key==*kp++ && (tmode&TM_MULTILINE)) {
  46. selpos=curpos; while (tptr[selpos]) selpos=down(selpos,xcur);
  47. tab_chk_l(selpos);
  48. }
  49. /* LEFT/RIGHT mode=0/2nd/Shift/2nd+Shift */
  50. else if (key==*kp++) {
  51. if (sel1) cpos=sel1, selpos=0;
  52. else if (curpos>1) {
  53. cpos=curpos;
  54. cpos--; if (tptr[cpos-1]==NEWLINE) cpos--;
  55. selpos=0; tab_chk_l(cpos);
  56. }
  57. } else if (key==*kp++) {
  58. if (sel2) cpos=sel2;
  59. else if (cpos<size) { if (tptr[cpos]==NEWLINE) cpos++; cpos++; }
  60. selpos=0; tab_chk_r(cpos);
  61. } else if (key==*kp++) {
  62. cpos=curpos; while (cpos>1 && tptr[cpos-2]!=NEWLINE) cpos--; selpos=0;
  63. } else if (key==*kp++) {
  64. cpos=curpos; while (tptr[cpos] && tptr[cpos]!=NEWLINE) cpos++; selpos=0;
  65. } else if (key==*kp++) { if (curpos>1) {
  66. selpos=curpos; selpos--; if (tptr[selpos-1]==NEWLINE) selpos--; tab_chk_l(selpos);
  67. } } else if (key==*kp++) { if (curpos<size) {
  68. selpos=curpos; if (tptr[selpos]==NEWLINE) selpos++; selpos++; tab_chk_r(selpos);
  69. } } else if (key==*kp++) {
  70. selpos=curpos; while (selpos>1 && tptr[selpos-2]!=NEWLINE) selpos--;
  71. } else if (key==*kp++) {
  72. selpos=curpos; while (tptr[selpos] && tptr[selpos]!=NEWLINE) selpos++;
  73. } else if ((k=0,key==*kp++) || (k++,key==*kp++)) { /* BACKSPACE / DELETE */
  74. last_act=-1;
  75. if (sel1!=sel2) sel_del();
  76. else {
  77. if (k && cpos<size) { if (tptr[cpos]==NEWLINE || !tab_chk(cpos+1)) cpos++; cpos++; }
  78. if (cpos>1) {
  79. int n=tptr[cpos-2]==NEWLINE || !tab_chk(cpos-1)?2:1;
  80. memmove(tptr+cpos-n,tptr+cpos,size+1+1-cpos);
  81. size-=n; cpos-=n;
  82. }
  83. }
  84. } else if ((k=0,key==*kp++) || (k=1,key==*kp++)) { /* CUT/COPY */
  85. if (sel1!=sel2) {
  86. CB_replaceTEXT(tptr+sel1,sel2-sel1,0);
  87. if (!k) sel_del();
  88. }
  89. } else if (key==*kp++) { /* PASTE */
  90. HANDLE hd; long len;
  91. if (CB_fetchTEXT(&hd,&len))
  92. sel_del(), insert((unsigned short)len),
  93. memcpy(tptr+(cpos-(unsigned short)len),HeapDeref(hd),(unsigned short)len); // multitask-unsafe !
  94. } else if (key==KEY_ENTER) {
  95. if (tmode&TM_MULTILINE) {
  96. unsigned int dpos=0;
  97. if (ac_on) ac_do();
  98. else {
  99. sel_del();
  100. n=whitespace(cpos,cpos);
  101. if (tptr[cpos-1]=='{') {
  102. if (whitespace(cpos+2,-1)<=n) {
  103. int i=n+2;
  104. insert(2+i), tptr[cpos-i-2]=NEWLINE;
  105. do tptr[cpos-i-1]=' '; while (i--);
  106. dpos=cpos;
  107. } else n+=2;
  108. }
  109. insert(2+n), tptr[cpos-n-2]=NEWLINE;
  110. do tptr[cpos-n-1]=' '; while (n--);
  111. if (dpos) cpos=dpos;
  112. }
  113. } /*else if (tmode&TM_DIALOG)
  114. DialogMsg(DLG_OK);*/
  115. } else if (key==KEY_ESC && ac_disp) {
  116. key=0; ac_disp=ac_on=0;
  117. } else if (key==KEY_CLEAR) {
  118. int n;
  119. if (ac_on) ac_do();
  120. if (linebegin(sel1)!=linebegin(sel2)) { /* tabify selection */
  121. msg="Not implemented yet";
  122. } else
  123. sel_del(), insert(n=even(cpos)+1), tptr[cpos-n]=' ', tptr[cpos-1]=' ';
  124. } else if (key=='(' || key=='[' || key=='{') {
  125. int x=key=='('?0:(key=='['?1:2);
  126. if (ac_on) ac_do();
  127. text_enclose(((char *[]){"(","[","{"})[x],((char *[]){")","]","}"})[x]);
  128. } else if (key>=256 && ((k=key&~(32|4096|8192|16384))=='X' || k=='Y' || k=='Z' || k=='T')) {
  129. k-='X'; if (k<0) k=3; k+=k;
  130. if (key&4096) k++;
  131. text_enclose(((char *[]){"if (","if (","for (","for (","while (","while (","else ","else {"})[k],
  132. #ifdef PERFECT
  133. ((char *[]){")\n\t\n\b",") {\n\t\n\b}\n"})[k&1]);
  134. #else
  135. #if 0
  136. (
  137. #endif
  138. ((char *[]){") ",") {}",") ",") {}",") ",") {}","","}"})[k]);
  139. #endif
  140. } else if (k==KEY_ON && key!=k) {
  141. if (key&4096) key=KEY_ESC;
  142. Off();
  143. } else if ((k=(key&*kp++))>='1' && k<='9' && key>=256) { /* kbdprgm# - custom shortcuts */
  144. } else if (key==*kp++) { /* CATALOG */
  145. char *p;
  146. if ((p=Catalog()))
  147. sel_del(), insert(n=strlen(p)), memcpy(tptr+cpos-n,p,n);
  148. } else if (key==*kp++) { /* Find text */
  149. if (SearchDlg(0)) DoSearch();
  150. } else if (key==*kp++) { /* Replace text */
  151. if (SearchDlg(1)) DoSearch();
  152. } else if ((k=(key-KEY_F1+1))>=1 && k<=8) { /* Function key F# */
  153. /*if (k==1) TestDisp();
  154. else*/
  155. if (k==3) {
  156. if (*text_find) DoSearch();
  157. else SearchDlg(0);
  158. } else if (k==5) {
  159. editor_exit_requested=EDEX_COMPILE;
  160. }
  161. } else if (key==*kp++) { /* HOME */
  162. } else if (key==*kp++) { /* CHAR */
  163. char c;
  164. if ((c=CharDlg()) && c>NEWLINE)
  165. sel_del(), insert(1), tptr[cpos-1]=c;
  166. } else { /* other keys */
  167. char *str=charmap;
  168. while (*kp) {
  169. if (key==*kp++) {
  170. sel_del(), insert(n=strlen(str)), memcpy(tptr+cpos-n,str,n);
  171. break;
  172. }
  173. while (*str++);
  174. }
  175. if (!*str && key>=14 && key<=255) {
  176. if (!isidch(key) && ac_on) ac_do();
  177. sel_del(), insert(1), tptr[cpos-1]=key;
  178. }
  179. }
  180. #ifdef EVHOOK
  181. else if (key!=KEY_ESC && !EDH_proc && (key&0xFFF0)!=0x0150 /* arrow keys [they seem to bug] */) {
  182. int k2=0;
  183. void EVh(EVENT *ev) {
  184. if (ev->Type==CM_KEYPRESS) k2=ev->extra.Key.Code+1;
  185. else if (ev->Type==CM_STRING) {
  186. char *p=ev->extra.pasteText; int n;
  187. sel_del(), insert(n=strlen(p)), memcpy(tptr+cpos-n,p,n);
  188. }
  189. ER_throwVar(1);
  190. }
  191. EVENT e;
  192. while ((k=*kp++)) if (key==k) goto keydone;
  193. e.Type=CM_KEYPRESS;
  194. e.extra.Key.Code=key;
  195. // e.extra.Key.Mod =key&0xF000;
  196. EV_captureEvents((EVENT_HANDLER)EVh);
  197. PortRestore();
  198. TRY
  199. EV_defaultHandler(&e);
  200. ONERR
  201. EV_captureEvents(NULL);
  202. ENDTRY
  203. PortSet(Port,239,127);
  204. key=k2;
  205. EDH_proc=1;
  206. goto keyproc;
  207. /* } else {
  208. char b[100];
  209. sprintf(b,"[0x%x]",key);
  210. text_enclose(b,"");*/
  211. }
  212. keydone:
  213. #endif
  214. curpos=selpos?:cpos;
  215. if (selpos) {
  216. if (selpos<cpos) sel1=selpos, sel2=cpos;
  217. else sel2=selpos, sel1=cpos;
  218. } else sel1=0, sel2=0;
  219. if (sel1==sel2) sel1=0, sel2=0;
  220. view_cursor();
  221. }