-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocaml.c
1601 lines (1409 loc) · 53.7 KB
/
ocaml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************/
/* */
/* OCamlTopWin */
/* */
/* Developed by Jacob Navia and Chris Watford. */
/* */
/* Copyright 2001 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU General Public License version 2 */
/* or (at your option) any later version. */
/* */
/***********************************************************************/
/***********************************************************************/
/* Changes made by Chris Watford to enhance the source editor */
/* Began 14 Sept 2003 - watford@uiuc.edu */
/***********************************************************************/
/* $Id: ocaml.c 12149 2012-02-10 16:15:24Z doligez $ */
/*@@ Wedit generated application. Written Sat Jun 02 18:22:38 2001
@@header: D:\lcc\inria\inriares.h
@@resources: D:\lcc\inria\inria.rc
Do not edit outside the indicated areas */
/*<---------------------------------------------------------------------->*/
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include <direct.h>
#include <Richedit.h>
#include "inriares.h"
#include "inria.h"
#define VK_BACKSPACE 0x108
/*<---------------------------------------------------------------------->*/
int EditControls = IDEDITCONTROL;
static WNDPROC lpEProc;
static char lineBuffer[1024*32];
int ReadToLineBuffer(void);
int AddLineBuffer(void);
static int busy;
static DWORD TimerId;
POSITION LastPromptPosition;
char LibDir[512];
char OcamlPath[512];
HBRUSH BackgroundBrush;
COLORREF BackColor = RGB(255,255,255);
PROGRAM_PARAMS ProgramParams;
StatementHistory *History = NULL;
StatementHistory *HistoryTail = NULL;
StatementHistory *historyEntry = NULL;
EditBuffer *CurrentEditBuffer = NULL; // current edit buffer
/*<----------------- global variables --------------------------------------->*/
HANDLE hInst; // Instance handle
HWND hwndMain; //Main window handle
HWND hwndSession;
HWND hwndMDIClient; //Mdi client window handle
static LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
static LRESULT CALLBACK MdiChildWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
PROCESS_INFORMATION pi;
HWND hWndStatusbar;
/*------------------------------------------------------------------------
Procedure: UpdateStatusBar ID:1
Purpose: Updates the statusbar control with the appropiate
text
Input: lpszStatusString: Charactar string that will be shown
partNumber: index of the status bar part number.
displayFlags: Decoration flags
Output: none
Errors: none
------------------------------------------------------------------------*/
void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
{
SendMessage(hWndStatusbar,
SB_SETTEXT,
partNumber | displayFlags,
(LPARAM)lpszStatusString);
}
/*------------------------------------------------------------------------
Procedure: MsgMenuSelect ID:1
Purpose: Shows in the status bar a descriptive explaation of
the purpose of each menu item.The message
WM_MENUSELECT is sent when the user starts browsing
the menu for each menu item where the mouse passes.
Input: Standard windows.
Output: The string from the resources string table is shown
Errors: If the string is not found nothing will be shown.
------------------------------------------------------------------------*/
LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
{
static char szBuffer[256];
UINT nStringID = 0;
UINT fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
UINT uCmd = GET_WM_MENUSELECT_CMD(wparam, lparam);
HMENU hMenu = GET_WM_MENUSELECT_HMENU(wparam, lparam);
szBuffer[0] = 0; // First reset the buffer
if (fuFlags == 0xffff && hMenu == NULL) // Menu has been closed
nStringID = 0;
else if (fuFlags & MFT_SEPARATOR) // Ignore separators
nStringID = 0;
else if (fuFlags & MF_POPUP) // Popup menu
{
if (fuFlags & MF_SYSMENU) // System menu
nStringID = IDS_SYSMENU;
else
// Get string ID for popup menu from idPopup array.
nStringID = 0;
} // for MF_POPUP
else // Must be a command item
nStringID = uCmd; // String ID == Command ID
// Load the string if we have an ID
if (0 != nStringID)
LoadString(hInst, nStringID, szBuffer, sizeof(szBuffer));
// Finally... send the string to the status bar
UpdateStatusBar(szBuffer, 0, 0);
return 0;
}
/*------------------------------------------------------------------------
Procedure: TimerProc ID:1
Purpose: This procedure will be called by windows about 4
times a second. It will just send a message to the
mdi child window to look at the pipe.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
static VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
SendMessage(hwndSession, WM_TIMERTICK, 0, 0);
}
/*------------------------------------------------------------------------
Procedure: InitializeStatusBar ID:1
Purpose: Initialize the status bar
Input: hwndParent: the parent window
nrOfParts: The status bar can contain more than one
part. What is difficult, is to figure out how this
should be drawn. So, for the time being only one is
being used...
Output: The status bar is created
Errors:
------------------------------------------------------------------------*/
void InitializeStatusBar(HWND hwndParent,int nrOfParts)
{
const int cSpaceInBetween = 8;
int ptArray[40]; // Array defining the number of parts/sections
RECT rect;
HDC hDC;
/* * Fill in the ptArray... */
hDC = GetDC(hwndParent);
GetClientRect(hwndParent, &rect);
ptArray[nrOfParts-1] = rect.right;
//---TODO--- Add code to calculate the size of each part of the status
// bar here.
ReleaseDC(hwndParent, hDC);
SendMessage(hWndStatusbar,
SB_SETPARTS,
nrOfParts,
(LPARAM)(LPINT)ptArray);
UpdateStatusBar("Ready", 0, 0);
}
/*------------------------------------------------------------------------
Procedure: CreateSBar ID:1
Purpose: Calls CreateStatusWindow to create the status bar
Input: hwndParent: the parent window
initial text: the initial contents of the status bar
Output:
Errors:
------------------------------------------------------------------------*/
static BOOL CreateSBar(HWND hwndParent,char *initialText,int nrOfParts)
{
hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER|SBARS_SIZEGRIP,
initialText,
hwndParent,
IDM_STATUSBAR);
if(hWndStatusbar)
{
InitializeStatusBar(hwndParent,nrOfParts);
return TRUE;
}
return FALSE;
}
/*------------------------------------------------------------------------
Procedure: InitApplication ID:1
Purpose: Registers two window classes: the "inria" window
class with the main window, and the mdi child
window's window class.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
static BOOL InitApplication(void)
{
WNDCLASS wc;
memset(&wc,0,sizeof(WNDCLASS));
wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "inriaWndClass";
wc.lpszMenuName = MAKEINTRESOURCE(IDMAINMENU);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(OCAML_ICON));
if (!RegisterClass(&wc))
return 0;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)MdiChildWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 20;
wc.hInstance = hInst; // Owner of this class
wc.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(OCAML_ICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Default color
wc.lpszMenuName = NULL;
wc.lpszClassName = "MdiChildWndClass";
if (!RegisterClass((LPWNDCLASS)&wc))
return FALSE;
return 1;
}
/*------------------------------------------------------------------------
Procedure: CreateinriaWndClassWnd ID:1
Purpose: Creates the main window
Input:
Output:
Errors:
------------------------------------------------------------------------*/
HWND CreateinriaWndClassWnd(void)
{
return CreateWindow("inriaWndClass","OCamlWinPlus v1.9RC4",
WS_MINIMIZEBOX|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_MAXIMIZEBOX|WS_CAPTION|WS_BORDER|WS_SYSMENU|WS_THICKFRAME,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,
NULL,
NULL,
hInst,
NULL);
}
/*------------------------------------------------------------------------
Procedure: MDICmdFileNew ID:1
Purpose: Creates a new session window. Note that multiple
windows with multiple sessions are possible.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
static HWND MDICmdFileNew(char *title, int show)
{
HWND hwndChild;
char rgch[150];
static int cUntitled;
MDICREATESTRUCT mcs;
if (title == NULL)
wsprintf(rgch,"Session%d", cUntitled++);
else {
strncpy(rgch,title,149);
rgch[149] = 0;
}
// Create the MDI child window
mcs.szClass = "MdiChildWndClass"; // window class name
mcs.szTitle = rgch; // window title
mcs.hOwner = hInst; // owner
mcs.x = CW_USEDEFAULT; // x position
mcs.y = CW_USEDEFAULT; // y position
mcs.cx = CW_USEDEFAULT; // width
mcs.cy = CW_USEDEFAULT; // height
mcs.style = 0; // window style
mcs.lParam = 0; // lparam
hwndChild = (HWND) SendMessage(hwndMDIClient,
WM_MDICREATE,
0,
(LPARAM)(LPMDICREATESTRUCT) &mcs);
if (hwndChild != NULL && show)
ShowWindow(hwndChild, SW_SHOW);
return hwndChild;
}
static HWND CreateMdiClient(HWND hwndparent)
{
CLIENTCREATESTRUCT ccs = {0};
HWND hwndMDIClient;
int icount = GetMenuItemCount(GetMenu(hwndparent));
// Find window menu where children will be listed
ccs.hWindowMenu = GetSubMenu(GetMenu(hwndparent), icount-2);
ccs.idFirstChild = IDM_WINDOWCHILD;
// Create the MDI client filling the client area
hwndMDIClient = CreateWindow("mdiclient",
NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL |
WS_HSCROLL,
0, 0, 0, 0,
hwndparent,
(HMENU)0xCAC,
hInst,
(LPVOID)&ccs);
ShowWindow(hwndMDIClient, SW_SHOW);
return hwndMDIClient;
}
void GotoEOF(void)
{
HWND hEdit = (HWND)GetWindowLongPtr(hwndSession,DWLP_USER);
int linesCount = SendMessage(hEdit,EM_GETLINECOUNT,0,0);
int lineindex = SendMessage(hEdit,EM_LINEINDEX,linesCount-1,0);
int lastLineLength = SendMessage(hEdit,EM_LINELENGTH,linesCount-1,0);
lineindex += lastLineLength;
SendMessage(hEdit,EM_SETSEL,lineindex,lineindex);
}
/*------------------------------------------------------------------------
Procedure: GotoPrompt ID:1
Author: Chris Watford watford@uiuc.edu
Purpose: Puts the cursor on the prompt line right after the '# '
Input:
Output:
Errors:
------------------------------------------------------------------------*/
void GotoPrompt(void)
{
HWND hEdit = (HWND)GetWindowLongPtr(hwndSession,DWLP_USER);
int lineindex = SendMessage(hEdit,EM_LINEINDEX,LastPromptPosition.line,0)+2;
SendMessage(hEdit,EM_SETSEL,lineindex,lineindex);
}
int GetCurLineIndex(HWND hEdit)
{
return SendMessage(hEdit,EM_LINEFROMCHAR,(WPARAM)-1,0);
}
int GetNumberOfLines(HWND hEdit)
{
return SendMessage(hEdit,EM_GETLINECOUNT,0,0);
}
static int GetWordUnderCursor(HWND hwndEditControl,char *buf,int len)
{
char *line,*p,*pstart,*pend;
int lineidx,start,end,length,offset,cursorpos,startingChar;
SendMessage(hwndEditControl,EM_GETSEL,(WPARAM)&start,(LPARAM)&end);
lineidx = SendMessage(hwndEditControl,EM_EXLINEFROMCHAR,0,start);
startingChar = SendMessage(hwndEditControl,EM_LINEINDEX,lineidx,0);
start -= startingChar;
end -= startingChar;
lineidx = SendMessage(hwndEditControl,EM_LINEFROMCHAR,start,0);
length = SendMessage(hwndEditControl,EM_LINELENGTH,lineidx,0);
offset = SendMessage(hwndEditControl,EM_LINEINDEX,lineidx,0);
line = SafeMalloc(length+1);
memset(line,0,length+1);
*(unsigned short *)line = length;
SendMessage(hwndEditControl,EM_GETLINE,lineidx,(LPARAM)line);
cursorpos = start-offset;
p = line + cursorpos;
pstart = p;
while (*pstart
&& *pstart != ' '
&& *pstart != '\t'
&& *pstart != '('
&& pstart > line)
pstart--;
pend = p;
while (*pend
&& *pend != ' '
&& *pend != '\t'
&& *pend != '('
&& pend < line + length)
pend++;
if (*pstart == ' ' || *pstart == '\t')
pstart++;
if (*pend == ' ' || *pend == '\t')
pend--;
memcpy(buf,pstart,1+pend-pstart);
buf[pend-pstart] = 0;
free(line);
return 1;
}
/*------------------------------------------------------------------------
Procedure: GetLastLine ID:1
Purpose: Gets the data in the line containing the cursor to
the interpreter.
Input: The edit control window handle
Output: None explicit
Errors: None
------------------------------------------------------------------------*/
char* GetLastLine(HWND hEdit)
{
int curline = GetCurLineIndex(hEdit);
char *linebuffer = (char*)SafeMalloc(2048*sizeof(char));
int n;
int linescount = GetNumberOfLines(hEdit);
*(unsigned short *)linebuffer = 2047;
n = SendMessage(hEdit,EM_GETLINE,curline,(LPARAM)linebuffer);
if (n >= 2 && linebuffer[0] == '#' && linebuffer[1] == ' ') {
n -= 2;
memmove(linebuffer, linebuffer+2, n);
}
linebuffer[n] = '\0';
return linebuffer;
}
void DoHelp(HWND hwnd)
{
char word[256];
GetWordUnderCursor(hwnd,word,sizeof(word));
MessageBox(NULL,word,"Aide pour:",MB_OK);
}
/*------------------------------------------------------------------------
Procedure: RewriteCurrentEditBuffer ID:1
Purpose: Rewrites what is at the prompt with the current contents of
the edit buffer
Input: None
Output: None explicit
Errors: None
------------------------------------------------------------------------*/
void RewriteCurrentEditBuffer(void)
{
// get the editbox's handle
HWND hEdit = (HWND)GetWindowLongPtr(hwndSession,DWLP_USER);
// calculate what to highlight
int linesCount = SendMessage(hEdit,EM_GETLINECOUNT,0,0);
int lineindex = SendMessage(hEdit,EM_LINEINDEX,LastPromptPosition.line,0) + 2;
int lastLine = SendMessage(hEdit,EM_LINEINDEX,linesCount-1,0) + SendMessage(hEdit,EM_LINELENGTH,linesCount-1,0) + 100;
// delete the current text
SendMessage(hEdit, EM_SETSEL, (WPARAM)lineindex, (LPARAM)lastLine);
SendMessage(hEdit, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)"");
{
// loop through each line in the edit buffer and add it to the control
LineList* line = CurrentEditBuffer->Lines;
for(; line != NULL; line = line->Next)
{
// if there is a line before me, add a newline
if(line->Prev != NULL)
SendMessage(hEdit, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)"\r\n");
// add the line
SendMessage(hEdit, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)line->Text);
}
}
}
/*------------------------------------------------------------------------
Procedure: RefreshCurrentEditBuffer ID:1
Purpose: Rewrites what is in the CurrentEditBuffer with what is
actually there
Input: None
Output: None explicit
Errors: None
------------------------------------------------------------------------*/
void RefreshCurrentEditBuffer(void)
{
// get the editbox's handle
HWND hEdit = (HWND)GetWindowLongPtr(hwndSession,DWLP_USER);
// get the last line index
int linesCount = SendMessage(hEdit,EM_GETLINECOUNT,0,0) - 1;
int i = 0, n = 0;
// where to hold the line we grab
char *linebuffer = (char*)SafeMalloc(2048*sizeof(char));
*(unsigned short *)linebuffer = 2047;
editbuffer_destroy(CurrentEditBuffer);
CurrentEditBuffer = editbuffer_new();
// loop through each line updating or adding it to the current edit buffer
for( ; (i + LastPromptPosition.line) <= linesCount; i++)
{
n = SendMessage(hEdit, EM_GETLINE, (i + LastPromptPosition.line), (LPARAM)linebuffer);
if ((n >= 2) && (linebuffer[0] == '#') && (linebuffer[1] == ' ')) {
n -= 2;
memmove(linebuffer, linebuffer+2, n);
}
linebuffer[n] = '\0';
{ // remove line breaks and feeds
char* ln = linebuffer;
while((*ln) != 0)
{
switch((*ln))
{
case '\r':
case '\n':
(*ln) = ' ';
}
ln++;
}
}
editbuffer_addline(CurrentEditBuffer, linebuffer);
}
}
/*------------------------------------------------------------------------
Procedure: NextHistoryEntry ID:1
Purpose: Scrolls to the next history entry
Input: None
Output: None explicit
Errors: None
--------------------------------------------------------------------------
Edit History:
17 Sept 2003 - Chris Watford watford@uiuc.edu
- Added this as a helper function
18 Sept 2003 - Chris Watford watford@uiuc.edu
- Corrected doubly linked list problems
------------------------------------------------------------------------*/
void NextHistoryEntry(void)
{
// out of bounds, put it back into bounds
if(historyEntry == NULL && History == NULL)
{
return;
} else if (historyEntry == NULL && History != NULL) {
historyEntry = History;
} else {
if(historyEntry->Next == NULL)
return;
historyEntry = historyEntry->Next;
}
// if its valid
if(historyEntry != NULL)
{
// copy the history entry to a new buffer
EditBuffer* newBuf = editbuffer_copy(historyEntry->Statement);
// destroy the old buffer
editbuffer_destroy(CurrentEditBuffer);
// setup the current one to the copy
CurrentEditBuffer = newBuf;
// rewrite the old one and go to the prompt
RewriteCurrentEditBuffer();
GotoPrompt();
}
}
/*------------------------------------------------------------------------
Procedure: PrevHistoryEntry ID:1
Purpose: Scrolls to the previous history entry
Input: None
Output: None explicit
Errors: None
--------------------------------------------------------------------------
Edit History:
17 Sept 2003 - Chris Watford watford@uiuc.edu
- Added this as a helper function
18 Sept 2003 - Chris Watford watford@uiuc.edu
- Corrected doubly linked list problems
------------------------------------------------------------------------*/
void PrevHistoryEntry(void)
{
// out of bounds, put it back into bounds
if(historyEntry == NULL || History == NULL)
{
return;
} else {
if(historyEntry->Prev == NULL)
return;
historyEntry = historyEntry->Prev;
}
// if its valid
if(historyEntry != NULL)
{
// copy the history entry to a new buffer
EditBuffer* newBuf = editbuffer_copy(historyEntry->Statement);
// destroy the old buffer
editbuffer_destroy(CurrentEditBuffer);
// setup the current one to the copy
CurrentEditBuffer = newBuf;
// rewrite the old one and go to the prompt
RewriteCurrentEditBuffer();
GotoPrompt();
}
}
/*------------------------------------------------------------------------
Procedure: SubClassEdit ID:1
Purpose: Handles messages to the editbox
Input:
Output:
Errors:
--------------------------------------------------------------------------
Edit History:
14 Sept 2003 - Chris Watford watford@uiuc.edu
- Setup handler for up and down arrows
15 Sept 2003 - Chris Watford watford@uiuc.edu
- Setup framework for history on up arrow
- Saves lines you move off of in the edit buffer
16 Sept 2003 - Chris Watford watford@uiuc.edu
- Proper handling of newline message finished
- Fixed ENTER on middle of interior line, moves cursor to the end
and sends the line
- Setup the copying and destroying of the old buffer
- Included buffer rewrite
17 Sept 2003 - Chris Watford watford@uiuc.edu
- Added C-p/C-n support
- Changed UpArrow to C-UpArrow so as to not confuse users
18 Sept 2003 - Chris Watford watford@uiuc.edu
- Added Left and Right arrow line saving
- Added backspace and delete line saving and removing
- Fixed history scrolling
21 Sept 2003 - Chris Watford watford@uiuc.edu
- Fixed pasting errors associated with lines being out of bounds
for the buffer
- Added error handling, possibly able to handle it diff down the
line
- Removed C-Up/C-Dn for history scrolling, buggy at best on my
machine
------------------------------------------------------------------------*/
static LRESULT CALLBACK SubClassEdit(HWND hwnd, UINT msg, WPARAM mp1, LPARAM mp2)
{
LRESULT r;
int postit=0,nl;
if (msg == WM_CHAR && mp1 == '\r') {
if (!busy) {
r = GetCurLineIndex(hwnd);
nl = GetNumberOfLines(hwnd);
// if we're not the last line
if (r != nl-1)
{
// update or add us, we might not have any lines in the edit buffer
editbuffer_updateoraddline(CurrentEditBuffer, r-LastPromptPosition.line, GetLastLine(hwnd));
// scroll to the end, add CrLf then post the newline message
GotoEOF();
AddStringToControl("\r\n");
PostMessage(GetParent(hwnd),WM_NEWLINE,0,0);
return 0;
}
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_END,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_END,1);
postit = 1;
}
}
else if (msg == WM_CHAR && mp1 == (char)0x08) {
int lineindex = SendMessage(hwnd, EM_LINEINDEX, LastPromptPosition.line, 0) + 2;
int curline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)-1,0);
int nextline = 0;
int curpoint = 0;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&curpoint, (LPARAM)NULL);
nextline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)(curpoint - 1),0);
if(curpoint <= lineindex)
{
return 0;
} else if(nextline != curline) {
// delete the line we're on
// grab the index
curline -= LastPromptPosition.line;
// kill it
editbuffer_removeline(CurrentEditBuffer, curline);
}
}
else if (msg == WM_KEYDOWN && mp1 == VK_F1) {
DoHelp(hwnd);
}
else if ((msg == WM_KEYDOWN || msg == WM_KEYUP) && mp1 == VK_UP) {
int curline = GetCurLineIndex(hwnd);
/*if((msg == WM_KEYDOWN) && (GetKeyState(VK_CONTROL) && 0x8000))
{ // go forward once in history
NextHistoryEntry();
return 0;
} else */
if((curline > LastPromptPosition.line) && (curline <= (LastPromptPosition.line + CurrentEditBuffer->LineCount)))
{
// update current line
if (msg == WM_KEYDOWN)
{
int lineidx = (curline - LastPromptPosition.line);
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_END,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_END,1);
// we may have to add this line, otherwise update it
editbuffer_updateoraddline(CurrentEditBuffer, lineidx, GetLastLine(hwnd));
}
} else {
return 0;
}
}
else if ((msg == WM_KEYDOWN || msg == WM_KEYUP) && (mp1 == VK_LEFT)) {
int lineindex = SendMessage(hwnd, EM_LINEINDEX, LastPromptPosition.line, 0) + 2;
int curline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)-1,0);
int nextline = 0;
int curpoint = 0;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&curpoint, (LPARAM)NULL);
nextline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)(curpoint - 1),0);
if(curpoint <= lineindex)
{ // no left arrow to the left of the prompt
return 0;
} else if(nextline != curline) {
// update current line
if (msg == WM_KEYDOWN)
{
int lineidx = (curline - LastPromptPosition.line);
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_END,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_END,1);
// we may have to add this line, otherwise update it
editbuffer_updateoraddline(CurrentEditBuffer, lineidx, GetLastLine(hwnd));
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_HOME,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_HOME,1);
}
}
}
else if ((msg == WM_KEYDOWN || msg == WM_KEYUP) && (mp1 == VK_DOWN)) {
int curline = GetCurLineIndex(hwnd);
/*if((msg == WM_KEYDOWN) && (GetKeyState(VK_CONTROL) && 0x8000))
{ // go back once in history
PrevHistoryEntry();
return 0;
} else*/
if((curline >= LastPromptPosition.line) && (curline < (LastPromptPosition.line + CurrentEditBuffer->LineCount)))
{
// We don't post the newline, but instead update the current line
if (msg == WM_KEYDOWN)
{
int lineidx = (curline - LastPromptPosition.line);
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_END,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_END,1);
editbuffer_updateline(CurrentEditBuffer, lineidx, GetLastLine(hwnd));
}
} else {
return 0;
}
}
else if ((msg == WM_KEYDOWN || msg == WM_KEYUP) && (mp1 == VK_RIGHT)) {
int lineindex = SendMessage(hwnd, EM_LINEINDEX, LastPromptPosition.line, 0) + 1;
int curline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)-1,0);
int nextline = 0;
int curpoint = 0;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&curpoint, (LPARAM)NULL);
nextline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)(curpoint + 2),0);
if(curpoint <= lineindex)
{ // no movement behind the prompt
return 0;
} else if((nextline != curline) && (msg = WM_KEYDOWN)) {
int lineidx = (curline - LastPromptPosition.line);
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_END,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_END,1);
editbuffer_updateline(CurrentEditBuffer, lineidx, GetLastLine(hwnd));
}
}
else if ((msg == WM_KEYDOWN) && (mp1 == VK_PRIOR) && (GetKeyState(VK_CONTROL) && 0x8000)) {
// C-p
NextHistoryEntry();
return 0;
}
else if ((msg == WM_KEYDOWN) && (mp1 == VK_NEXT) && (GetKeyState(VK_CONTROL) && 0x8000)) {
// C-n
PrevHistoryEntry();
return 0;
}
else if ((msg == WM_KEYDOWN || msg == WM_KEYUP) && (mp1 == VK_DELETE)) {
// see if we're the last char on the line, if so delete the next line
// don't allow deleting left of the prompt
int lineindex = SendMessage(hwnd, EM_LINEINDEX, LastPromptPosition.line, 0) + 2;
int curline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)-1,0);
int nextline = 0;
int curpoint = 0;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&curpoint, (LPARAM)NULL);
nextline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)(curpoint + 2),0);
if(curpoint < lineindex)
{ // no chomping behind the prompt
return 0;
} else if(nextline != curline) {
// deleting
// grab the next line index
curline -= LastPromptPosition.line;
// kill it
editbuffer_removeline(CurrentEditBuffer, curline+1);
}
}
else if (msg == WM_PASTE) {
// if they paste text, allow it
r = CallWindowProc(lpEProc, hwnd, msg, mp1, mp2);
// update the current edit buffer
RefreshCurrentEditBuffer();
return r;
}
// handle errors
switch(msg)
{
case WM_SYNTAXERROR:
case WM_ILLEGALCHAR:
case WM_UNBOUNDVAL:
{ // currently I handle them all the same
// get the start of the line
int start = SendMessage(hwnd, EM_LINEINDEX, LastPromptPosition.line, 0) + 2;
// get the statement that error'd
NextHistoryEntry();
// tell the history that the last line errored
if(History != NULL)
if(History->Statement != NULL)
History->Statement->isCorrect = FALSE;
// highlight the offending chars
SendMessage(hwnd,EM_SETSEL,(WPARAM)(start + mp1), (LPARAM)(start + mp2));
return 0;
}
}
r = CallWindowProc(lpEProc, hwnd, msg, mp1, mp2);
if (postit)
PostMessage(GetParent(hwnd),WM_NEWLINE,0,0);
return r;
}
static void SubClassEditField(HWND hwnd)
{
if (lpEProc == NULL) {
lpEProc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
}
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) SubClassEdit);
}
/*------------------------------------------------------------------------
Procedure: SendLastLine ID:1
Purpose: Sends the data in the line containing the cursor to
the interpreter. If this is NOT the last line, copy
the line to the end of the text.
Input: The edit control window handle
Output: None explicit
Errors: None
REMOVED!
------------------------------------------------------------------------*/
void SendLastLine(HWND hEdit)
{
/* int curline = GetCurLineIndex(hEdit);
char *p,linebuffer[2048];
int n;
int linescount = GetNumberOfLines(hEdit);
*(unsigned short *)linebuffer = sizeof(linebuffer)-1;
if (curline != linescount-1)
n = SendMessage(hEdit,EM_GETLINE,curline,(LPARAM)linebuffer);
else
n = SendMessage(hEdit,EM_GETLINE,curline-1,(LPARAM)linebuffer);
if (n >= 2 && linebuffer[0] == '#' && linebuffer[1] == ' ') {
n -= 2;
memmove(linebuffer, linebuffer+2, n);
}
linebuffer[n] = 0;
// Record user input!
AddToHistory(linebuffer);
linebuffer[n] = '\n';
linebuffer[n+1] = 0;
WriteToPipe(linebuffer);
if (curline != linescount-1) {
// Copy the line sent to the end of the text
p = strrchr(linebuffer,'\n');
if (p) {
*p = 0;
}
busy = 1;
AddLineToControl(linebuffer);
busy = 0;
}*/
}
/*------------------------------------------------------------------------
Procedure: SendLastEditBuffer ID:1
Author: Chris Watford watford@uiuc.edu
Purpose: Sends an edit buffer to the pipe
Input:
Output:
Errors:
--------------------------------------------------------------------------
Edit History:
7 Aug 2004 - Chris Watford christopher.watford@gmail.com
- Fixed error where SendLastEditBuffer sent waaaay too many
newlines which completely broke the underlying connection to the
ocaml.exe pipe
15 Sept 2003 - Chris Watford watford@uiuc.edu
- Sends line to the pipe and adds newline to the end
------------------------------------------------------------------------*/
void SendLastEditBuffer(HWND hwndChild)
{
char* line = editbuffer_getasbuffer(CurrentEditBuffer);
int l = strlen(line) - 1;
char* linebuffer = (char*)SafeMalloc(l+2);
// save current edit buffer to history and create a new blank edit buffer
CurrentEditBuffer->isCorrect = TRUE;
AddToHistory(CurrentEditBuffer);
CurrentEditBuffer = (EditBuffer*)SafeMalloc(sizeof(EditBuffer));
CurrentEditBuffer->LineCount = 0;
CurrentEditBuffer->Lines = NULL;
// trim and add the newline to the end
strncpy(linebuffer, line, l+1);
while((linebuffer[l] == '\n' || linebuffer[l] == '\r') && (l >= 0))
{
linebuffer[l--] = '\0';
}
linebuffer[l+1] = '\n';
linebuffer[l+2] = '\0';
// save line to the pipe
WriteToPipe(linebuffer);
}
/*------------------------------------------------------------------------
Procedure: SendingFullCommand ID:1
Author: Chris Watford watford@uiuc.edu
Purpose: Returns if the command being sent
Input: The edit control window handle
Output: None explicit
Errors: None
--------------------------------------------------------------------------
Edit History:
7 Aug 2004 - Chris Watford christopher.watford@gmail.com
- Fixed bug #2932 where many carraige returns were sent and it came