grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / classes / zune / texteditor / mcc / HandleARexx.c
blob29bedf2148464f0d5177a053684a725ef0da8a11
1 /***************************************************************************
3 TextEditor.mcc - Textediting MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2014 TextEditor.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 TextEditor class Support Site: http://www.sf.net/projects/texteditor-mcc
19 $Id$
21 ***************************************************************************/
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
27 #include <dos/rdargs.h>
28 #include <exec/memory.h>
29 #include <clib/alib_protos.h>
30 #include <proto/utility.h>
31 #include <proto/exec.h>
32 #include <proto/dos.h>
34 #include "private.h"
35 #include "Debug.h"
37 struct RexxCommand
39 const char *Command;
40 const char *Template;
43 static const struct RexxCommand Commands[] =
45 { "CLEAR", NULL },
46 { "CUT", NULL },
47 { "COPY", NULL },
48 { "PASTE", NULL },
49 { "ERASE", NULL },
50 { "GOTOLINE", "/N/A" },
51 { "GOTOCOLUMN", "/N/A" },
52 { "CURSOR", "Up/S,Down/S,Left/S,Right/S" },
53 { "LINE", "/N/A" },
54 { "COLUMN", "/N/A" },
55 { "NEXT", "Word/S,Sentence/S,Paragraph/S,Page/S" },
56 { "PREVIOUS", "Word/S,Sentence/S,Paragraph/S,Page/S" },
57 { "POSITION", "SOF/S,EOF/S,SOL/S,EOL/S,SOW/S,EOW/S,SOV/S,EOV/S" },
58 { "SETBOOKMARK", "/N/A" },
59 { "GOTOBOOKMARK", "/N/A" },
60 { "TEXT", "/F" },
61 { "UNDO", NULL },
62 { "REDO", NULL },
63 { "GETLINE", NULL },
64 { "GETCURSOR", "Line/S,Column/S" },
65 { "MARK", "On/S,Off/S" },
66 { "DELETE", NULL },
67 { "BACKSPACE", NULL },
68 { "KILLLINE", NULL },
69 { "TOUPPER", NULL },
70 { "TOLOWER", NULL },
71 { "SELECTALL", NULL },
72 { "SELECTNONE", NULL },
73 { NULL, NULL }
76 enum
78 CLEAR = 0, CUT, COPY, PASTE, ERASE, GOTOLINE, GOTOCOLUMN, CURSOR,
79 LINE, COLUMN, NEXT, PREVIOUS, POSITION, SETBOOKMARK, GOTOBOOKMARK,
80 InsTEXT, UNDO, REDO, GETLINE, GETCURSOR, MARK, DELETE, BACKSPACE,
81 KILLLINE, TOUPPER, TOLOWER, SELECTALL, SELECTNONE
84 #define MaxArgs 8
86 /// CallFunction()
87 static IPTR CallFunction(struct InstData *data, UWORD function, IPTR *args, const char *txtargs)
89 struct line_node *oldactualline = data->actualline;
90 LONG oldCPos_X = data->CPos_X;
91 IPTR result = TRUE;
92 LONG new_y = data->visual_y-1;
94 ENTER();
96 SHOWVALUE(DBF_REXX, function);
98 if(isFlagSet(data->flags, FLG_ReadOnly))
100 switch(function)
102 case CURSOR:
104 if(*args++)
105 new_y -= 1;
106 if(*args)
107 new_y += 1;
109 break;
111 case NEXT:
113 if(args[3])
114 new_y += data->maxlines;
116 break;
118 case PREVIOUS:
120 if(args[3])
121 new_y -= data->maxlines;
123 break;
127 if(new_y != data->visual_y-1)
129 if(new_y > data->totallines-data->maxlines)
130 new_y = data->totallines-data->maxlines;
131 if(new_y < 0)
132 new_y = 0;
133 set(data->object, MUIA_TextEditor_Prop_First, new_y*data->fontheight);
135 else
137 if(function > GOTOBOOKMARK || function < GOTOLINE)
138 clearFlag(data->flags, FLG_ARexxMark);
140 switch(function)
142 case CLEAR:
144 DoMethod(data->object, MUIM_TextEditor_ClearText);
146 break;
148 case CUT:
150 Key_Cut(data);
152 break;
154 case COPY:
156 Key_Copy(data);
158 break;
160 case PASTE:
162 Key_Paste(data);
164 break;
166 case ERASE:
168 if(Enabled(data))
169 Key_Clear(data);
171 break;
173 case GOTOLINE:
175 if(*args)
177 STRPTR buffer;
179 if((buffer = AllocVecShared(16, MEMF_ANY)) != NULL)
181 set(data->object, MUIA_TextEditor_CursorY, *(ULONG *)*args);
183 // return the current line number, this may differ from the input value!
184 snprintf(buffer, 16, "%ld", xget(data->object, MUIA_TextEditor_CursorY));
185 result = (IPTR)buffer;
189 break;
191 case GOTOCOLUMN:
193 if(*args)
195 STRPTR buffer;
197 if((buffer = AllocVecShared(16, MEMF_ANY)) != NULL)
199 set(data->object, MUIA_TextEditor_CursorX, *(ULONG *)*args);
200 // return the current column number, this may differ from the input value!
201 snprintf(buffer, 16, "%ld", xget(data->object, MUIA_TextEditor_CursorX));
202 result = (IPTR)buffer;
206 break;
208 case CURSOR:
210 if(*args++)
211 GoUp(data);
212 if(*args++)
213 GoDown(data);
214 if(*args++)
215 GoLeft(data);
216 if(*args)
217 GoRight(data);
219 break;
221 case LINE:
223 if(*args)
225 LONG line;
227 line = xget(data->object, MUIA_TextEditor_CursorY);
228 line += *(LONG *)*args;
229 set(data->object, MUIA_TextEditor_CursorY, (line < 0) ? 0 : line);
232 break;
234 case COLUMN:
236 if(*args)
238 LONG column;
240 column = xget(data->object, MUIA_TextEditor_CursorX);
241 column += *(LONG *)*args;
242 set(data->object, MUIA_TextEditor_CursorX, (column < 0) ? 0 : column);
245 break;
247 case NEXT:
249 if(*args++)
250 GoNextWord(data);
251 if(*args++)
252 GoNextSentence(data);
253 if(*args++)
254 GoNextLine(data);
255 if(*args)
256 GoNextPage(data);
258 break;
260 case PREVIOUS:
262 if(*args++)
263 GoPreviousWord(data);
264 if(*args++)
265 GoPreviousSentence(data);
266 if(*args++)
267 GoPreviousLine(data);
268 if(*args)
269 GoPreviousPage(data);
271 break;
273 case POSITION:
275 if(*args++)
276 GoTop(data);
277 if(*args++)
278 GoBottom(data);
279 if(*args++)
280 GoStartOfLine(data);
281 if(*args++)
282 GoEndOfLine(data);
283 if(*args++)
284 GoPreviousWord(data);
285 if(*args++)
286 GoNextWord(data);
287 if(*args++)
288 GoPreviousPage(data);
289 if(*args)
290 GoNextPage(data);
292 break;
294 case InsTEXT:
296 if(*txtargs)
298 struct Hook *oldhook = data->ImportHook;
300 data->ImportHook = &ImPlainHook;
301 DoMethod(data->object, MUIM_TextEditor_InsertText, txtargs, MUIV_TextEditor_InsertText_Cursor);
302 data->ImportHook = oldhook;
305 break;
307 case UNDO:
309 Undo(data);
311 break;
313 case REDO:
315 Redo(data);
317 break;
319 case GETLINE:
321 STRPTR buffer;
323 if((buffer = AllocVecShared(data->actualline->line.Length+1, MEMF_ANY)) != NULL)
325 strlcpy(buffer, data->actualline->line.Contents, data->actualline->line.Length+1);
326 result = (IPTR)buffer;
328 break;
331 case GETCURSOR:
333 STRPTR buffer;
335 if((buffer = AllocVecShared(16, MEMF_ANY)) != NULL)
337 LONG pos = 0;
339 if(*args)
340 pos = xget(data->object, MUIA_TextEditor_CursorY);
341 else
342 pos = xget(data->object, MUIA_TextEditor_CursorX);
344 snprintf(buffer, 16, "%d", (int)pos);
346 result = (IPTR)buffer;
348 break;
351 case SETBOOKMARK:
353 if(*args)
354 SetBookmark(data, *(ULONG *)*args-1);
356 break;
358 case GOTOBOOKMARK:
360 if(*args)
361 GotoBookmark(data, *(ULONG *)*args-1);
363 break;
365 case MARK:
367 if(*++args)
369 data->flags &= ~FLG_ARexxMark;
370 if(data->blockinfo.enabled == TRUE)
372 data->blockinfo.enabled = FALSE;
373 MarkText(data, data->blockinfo.startx, data->blockinfo.startline, data->blockinfo.stopx, data->blockinfo.stopline);
376 else
378 setFlag(data->flags, FLG_ARexxMark);
381 break;
383 case DELETE:
385 Key_Delete(data);
387 break;
389 case BACKSPACE:
391 Key_Backspace(data);
393 break;
395 case KILLLINE:
397 Key_DelLine(data);
399 break;
401 case TOUPPER:
403 Key_ToUpper(data);
405 break;
407 case TOLOWER:
409 Key_ToLower(data);
411 break;
413 case SELECTALL:
415 MarkAllBlock(data, &data->blockinfo);
416 MarkText(data, data->blockinfo.startx, data->blockinfo.startline, data->blockinfo.stopx, data->blockinfo.stopline);
418 break;
420 case SELECTNONE:
422 if(data->blockinfo.enabled == TRUE)
424 data->blockinfo.enabled = FALSE;
425 MarkText(data, data->blockinfo.startx, data->blockinfo.startline, data->blockinfo.stopx, data->blockinfo.stopline);
427 else
428 W(DBF_BLOCK, "no text selected (startline=%08lx, stopline=%08lx)", data->blockinfo.startline, data->blockinfo.stopline);
430 break;
433 if(data->CPos_X != oldCPos_X || oldactualline != data->actualline)
435 if(isFlagSet(data->flags, FLG_Active) && function <= GOTOBOOKMARK && function >= GOTOLINE)
436 SetCursor(data, oldCPos_X, oldactualline, FALSE);
438 if(isFlagSet(data->flags, FLG_ARexxMark))
440 data->blockinfo.stopline = data->actualline;
441 data->blockinfo.stopx = data->CPos_X;
442 if(data->blockinfo.enabled == FALSE)
444 data->blockinfo.enabled = TRUE;
445 data->blockinfo.startline = oldactualline;
446 data->blockinfo.startx = oldCPos_X;
448 MarkText(data, oldCPos_X, oldactualline, data->CPos_X, data->actualline);
450 else
452 if(data->blockinfo.enabled == TRUE)
454 data->blockinfo.enabled = FALSE;
455 MarkText(data, data->blockinfo.startx, data->blockinfo.startline, data->blockinfo.stopx, data->blockinfo.stopline);
459 ScrollIntoDisplay(data);
461 if(isFlagSet(data->flags, FLG_Active) && function <= GOTOBOOKMARK && function >= GOTOLINE)
462 SetCursor(data, data->CPos_X, data->actualline, TRUE);
464 // make sure to notify others that the cursor has changed and so on.
465 data->NoNotify = TRUE;
467 if(data->CPos_X != oldCPos_X)
468 set(data->object, MUIA_TextEditor_CursorX, data->CPos_X);
470 if(data->actualline != oldactualline)
471 set(data->object, MUIA_TextEditor_CursorY, LineNr(data, data->actualline)-1);
473 data->NoNotify = FALSE;
477 RETURN(result);
478 return(result);
482 /// mHandleARexx()
483 IPTR mHandleARexx(struct IClass *cl, Object *obj, struct MUIP_TextEditor_ARexxCmd *msg)
485 struct InstData *data = INST_DATA(cl, obj);
486 IPTR result = 0;
487 STRPTR command = msg->command;
489 ENTER();
491 if(data->shown == TRUE && command != NULL && command[0] != '\0')
493 const char *txtargs = "";
494 const char *cmd;
495 int function;
497 SHOWSTRING(DBF_REXX, command);
499 for(function=0; (cmd = Commands[function].Command) != NULL; function++)
501 int cmdlen = strlen(cmd);
503 if(strnicmp(command, cmd, cmdlen) == 0 &&
504 (command[cmdlen] == ' ' || command[cmdlen] == '\0'))
506 txtargs = &command[cmdlen];
507 break;
511 SHOWVALUE(DBF_REXX, function);
512 SHOWSTRING(DBF_REXX, txtargs);
514 if(Commands[function].Command != NULL && (*txtargs == '\0' || Commands[function].Template != NULL))
516 IPTR Args[MaxArgs];
518 memset(Args, 0, sizeof(Args));
520 // skip leading spaces
521 while(isspace(*txtargs))
522 txtargs++;
524 if(*txtargs != '\0' && function != InsTEXT)
526 struct RDArgs *myrdargs = NULL;
528 if((myrdargs = AllocDosObject(DOS_RDARGS, NULL)) != NULL)
530 ULONG length = strlen(txtargs);
531 char *buffer;
533 if((buffer = AllocVecPooled(data->mypool, length+2)) != NULL)
535 struct RDArgs *ra_result = NULL;
537 snprintf(buffer, length+2, "%s\n", txtargs);
539 myrdargs->RDA_Source.CS_Buffer = buffer;
540 myrdargs->RDA_Source.CS_Length = length+1;
541 myrdargs->RDA_Source.CS_CurChr = 0;
542 myrdargs->RDA_Flags |= RDAF_NOPROMPT;
544 if((ra_result = ReadArgs(Commands[function].Template, (APTR)Args, myrdargs)) != NULL)
546 result = CallFunction(data, function, Args, NULL);
548 FreeArgs(ra_result);
550 FreeVecPooled(data->mypool, buffer);
552 FreeDosObject(DOS_RDARGS, myrdargs);
555 else
556 result = CallFunction(data, function, Args, (char *)txtargs);
560 RETURN(result);
561 return result;