-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNCursesShell.cpp
More file actions
874 lines (764 loc) · 29.1 KB
/
NCursesShell.cpp
File metadata and controls
874 lines (764 loc) · 29.1 KB
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
//
// NCursesShell.cpp
// Copyright 2013 Michael T. Duffy.
// contact: mduffor@gmail.com
// This file is part of TEdit.
//
// TEdit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// TEdit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TEdit. If not, see <http://www.gnu.org/licenses/>.
//
/*
Next steps:
Build file list from opened files
Add string to StrArray without duplication
Given a file, break it apart and add all of its paths without duplication
Sort string array. (possibly use index sorter, then some sort of string mover)
add default paths.
On enter for path,
Find all children of this path. for each:
remove all non-loaded file entries.
remove all directories that aren't in a loaded file.
scan and update children
sort.
update arrays for changed entries.
Ctrl-O to launch file list in open mode.
Close Command
If modified, prompt for saving.
Save if returned y/Y
Set previous buffer to current
delete targeet buffer
Header swap
Store list of file/header extension pairs
Break filename of current buffer into component parts, split on period.
Find match in pairs for right-most token
Swap out token. Rebuild filename. OpenFile. set current.
*/
#include <stdio.h>
#include <string.h>
#include "Types.hpp"
#include "Debug.hpp"
ASSERTFILE (__FILE__)
#include <ncurses.h>
#include "NCursesShell.hpp"
#include "GapBuffer.hpp"
#include "SyntaxParser.hpp"
#include "VKeys.hpp"
#include "BufferCommands.hpp"
#include "EntryFieldHandler.hpp"
#include "FilePath.hpp"
//-----------------------------------------------------------------------------
NCursesShell::NCursesShell ()
{
bShowLineNumbers = TRUE;
bShowStatusBar = TRUE;
bShowFileList = TRUE;
iFileListWidth = 20;
// file list init. Move to own class
iFileListTopLine = 0;
iFileListCursor = 1;
astrFileList.Append ("./SyntaxTest.txt"); aiFileLoaded.Append (1);
astrFileList.Append ("./test/"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld.txt"); aiFileLoaded.Append (1);
astrFileList.Append ("./test/helloWorld2.txt"); aiFileLoaded.Append (1);
astrFileList.Append ("./test/helloWorld3.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld4.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld5.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld6.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld7.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld8.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld9.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld10.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld11.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld12.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld13.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld14.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld15.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld16.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld17.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld18.txt"); aiFileLoaded.Append (0);
astrFileList.Append ("./test/helloWorld19.txt"); aiFileLoaded.Append (0);
initscr (); // start ncurses
raw (); // disable line buffering
keypad (stdscr, TRUE); // enable function keys
noecho (); // don't echo characters; we will display them ourself
INT iBGColor = COLOR_BLACK;
start_color ();
init_pair (1, COLOR_RED, iBGColor);
init_pair (2, COLOR_GREEN, iBGColor);
init_pair (3, COLOR_YELLOW, iBGColor);
init_pair (4, COLOR_BLUE, iBGColor);
init_pair (5, COLOR_MAGENTA, iBGColor);
init_pair (6, COLOR_CYAN, iBGColor);
init_pair (7, COLOR_WHITE, iBGColor);
};
//-----------------------------------------------------------------------------
NCursesShell::~NCursesShell ()
{
endwin (); // end ncurses mode
};
//-----------------------------------------------------------------------------
VOID NCursesShell::Update (GapBufferManager * pBufferManager,
CommandManager & cmdManager,
EditorSettings & editorSettings,
EntryFieldHandler & entryFieldHandler)
{
int iRowMax = 0;
int iColMax = 0;
/*
printw ("Testing ncurses (%dx%d). Press a key to see it in bold. \n", iColMax, iRowMax);
int ch = getch();
if (ch == KEY_F(1))
{
printw ("F1 key pressed\n");
}
else
{
printw ("The key pressed is ");
attron(A_BOLD);
printw ("%c", ch);
attroff(A_BOLD);
}
*/
//move(0,0);
// addch (ch | A_BOLD | A_UNDERLINE);
BOOL bExit = FALSE;
while (!bExit)
{
getmaxyx (stdscr, iRowMax, iColMax);
GapBuffer * pBuffer = pBufferManager->GetCurrent();
if (pBuffer == NULL)
{
// no buffer. Error
return;
};
INT iHeader = 5;
INT iLeft = 0;
if (bShowFileList)
{
iLeft = iFileListWidth;
};
INT iStartX = 0;
INT iStartY = 0;
INT iWidth = iColMax;
INT iHeight = iRowMax;
GapBuffer * pBufferInputFocus = pBuffer;
if (entryFieldHandler.IsActive ())
{
pBufferInputFocus = &(entryFieldHandler.GetBuffer ());
}
DisplayWindow (iStartX + iLeft, iStartY + iHeader,
iWidth - iLeft, iHeight - iHeader, // iColMax, iRowMax,
pBuffer,
editorSettings,
entryFieldHandler);
//refresh();
if (bShowFileList)
{
DisplayFileList (pBufferManager, iStartX, iStartY + iHeader, iLeft, iHeight - iHeader);
bExit = ProcessInputFileList (pBufferManager, cmdManager, editorSettings);
}
else
{
bExit = ProcessInput (pBufferInputFocus, pBuffer, cmdManager, editorSettings, entryFieldHandler);
}
move(2,0);
printw("file: %s ", pBuffer->GetName());
refresh();
}
};
//-----------------------------------------------------------------------------
VOID NCursesShell::DisplayFileList (GapBufferManager * pBufferManager,
INT iScreenX,
INT iScreenY,
INT iWidth,
INT iHeight)
{
// This will eventually go into its own class
iFileListHeight = iHeight;
INT iMaxLines = astrFileList.Length ();
INT iLine = iFileListTopLine;
INT iRemaining = 0;
INT iIndex;
for (iIndex = 0; iIndex < iHeight; ++iIndex)
{
if (iLine >= iMaxLines) break;
//attron(A_BOLD);
move (iScreenY + iIndex, iScreenX);
INT iStringLength = astrFileList[iLine].GetLength ();
if (aiFileLoaded [iLine])
{
attrset(COLOR_PAIR (COLOR_GREEN));
};
if (iFileListCursor == iLine)
{
attron (A_REVERSE);
};
// TODO: display path indentation
// display modified flag
INT iModifiedFlagWidth = 0;
if (aiFileLoaded [iLine])
{
GapBuffer * pCurr = pBufferManager->GetBuffer (astrFileList[iLine].AsChar ());
if (pCurr != NULL)
{
if (pCurr->GetIsModified ())
{
addch ('*');
iModifiedFlagWidth = 1;
}
else
{
addch ('-');
iModifiedFlagWidth = 1;
}
}
}
// display file name
addnstr (astrFileList[iLine].AsChar (), TMin (iWidth, iStringLength));
iRemaining = iWidth - iStringLength - iModifiedFlagWidth;
while (iRemaining > 0)
{
addch (' ');
--iRemaining;
};
++iLine;
attrset(A_NORMAL);
}
for (; iIndex < iHeight; ++iIndex)
{
move (iScreenY + iIndex, iScreenX);
iRemaining = iWidth;
while (iRemaining > 0)
{
addch (' ');
--iRemaining;
};
}
}
//-----------------------------------------------------------------------------
VOID NCursesShell::KeepFileListCursorOnScreen (VOID)
{
if (iFileListCursor < iFileListTopLine)
{
iFileListTopLine = iFileListCursor;
}
if (iFileListCursor >= iFileListTopLine + iFileListHeight)
{
iFileListTopLine = TMax (0, iFileListCursor - iFileListHeight + 1);
}
}
//-----------------------------------------------------------------------------
VOID NCursesShell::FileListCursorUp (VOID)
{
// TODO: Handle collapsed sections
iFileListCursor = TMax (0, TMin (astrFileList.Length () - 1, iFileListCursor - 1));
KeepFileListCursorOnScreen ();
}
//-----------------------------------------------------------------------------
VOID NCursesShell::FileListCursorDown (VOID)
{
// TODO: Handle collapsed sections
iFileListCursor = TMax (0, TMin (astrFileList.Length () - 1, iFileListCursor + 1));
KeepFileListCursorOnScreen ();
}
//-----------------------------------------------------------------------------
VOID NCursesShell::ToggleFileListDisplay (VOID)
{
bShowFileList = bShowFileList ? FALSE : TRUE;
}
//-----------------------------------------------------------------------------
BOOL NCursesShell::ProcessInputFileList (GapBufferManager * pBufferManager,
CommandManager & cmdManager,
EditorSettings & editorSettings)
{
INT vKey = NCursesToVKey(getch());
if (vKey >= 32 && vKey <= 126)
{
// standard printable ASCII key
// TODO: seek out files in directory that match typed letters
}
else
{
// Note: This needs to be converted to a configureable hotkey lookup.
switch (vKey)
{
case VKEY_DOWN: FileListCursorDown (); break;
case VKEY_UP: FileListCursorUp (); break;
case VKEY_PAGEDOWN: break;
case VKEY_PAGEUP: break;
case VKEY_LEFT: break;
case VKEY_RIGHT: break;
case VKEY_HOME: break;
case VKEY_END: break;
case VKEY_ENTER:
{
// select if already open
GapBuffer * pExisting = pBufferManager->GetBufferByFileName (astrFileList[iFileListCursor]);
if (pExisting != NULL)
{
// found it
pBufferManager->SetCurrent(pExisting);
ToggleFileListDisplay ();
break;
}
// not already open. see if it is a file.
FilePath filePath;
if (filePath.FileExists (astrFileList[iFileListCursor]))
{
// open file
RStrArray arrayFileNameParts;
FilePath::SplitPath (astrFileList[iFileListCursor], '/', arrayFileNameParts);
GapBuffer * pBuffer = pBufferManager->CreateBuffer (arrayFileNameParts[arrayFileNameParts.Length() - 1]);
pBufferManager->SetCurrent (pBuffer);
pBuffer->AllocBuffer (256);
pBuffer->SetFileName (astrFileList[iFileListCursor]);
pBuffer->Load ();
pBuffer->FillGap ();
pBuffer->SetCursor(1, 0);
ToggleFileListDisplay ();
move(3,0);
printw("current : %s ", pBufferManager->GetCurrent()->GetName());
}
}
break;
case (CTRL_P): ToggleFileListDisplay (); break;
case (CTRL_Q):
return (TRUE);
};
};
return (FALSE);
};
//-----------------------------------------------------------------------------
BOOL NCursesShell::ProcessInput (GapBuffer * pInputBuffer,
GapBuffer * pDisplayBuffer,
CommandManager & cmdManager,
EditorSettings & editorSettings,
EntryFieldHandler & entryFieldHandler)
{
BufferCommandsSetBuffer (pInputBuffer, pDisplayBuffer);
INT vKey = NCursesToVKey(getch());
if (vKey >= 32 && vKey <= 126)
{
// standard printable ASCII key
if (pInputBuffer->IsSelectionValid ())
{
cmdManager.ExecuteCommand ("SelectionDelete", NULL);
}
else
{
if (editorSettings.IsInsertMode ())
{
pInputBuffer->InsertChar(vKey);
}
else
{
pInputBuffer->ReplaceChar(vKey);
}
}
}
else
{
// Note: This needs to be converted to a configureable hotkey lookup.
switch (vKey)
{
case VKEY_DOWN: cmdManager.ExecuteCommand ("CursorDown", NULL); break;
case VKEY_UP: cmdManager.ExecuteCommand ("CursorUp", NULL); break;
case VKEY_PAGEDOWN: cmdManager.ExecuteCommand ("CursorPageDown", NULL); break;
case VKEY_PAGEUP: cmdManager.ExecuteCommand ("CursorPageUp", NULL); break;
case VKEY_LEFT: cmdManager.ExecuteCommand ("CursorLeft", NULL); break;
case VKEY_RIGHT: cmdManager.ExecuteCommand ("CursorRight", NULL); break;
case VKEY_TAB: pInputBuffer->InsertString (editorSettings.GetTabString());
case VKEY_HOME: cmdManager.ExecuteCommand ("CursorStartLine", NULL); break;
case VKEY_END: cmdManager.ExecuteCommand ("CursorEndLine", NULL); break;
case VKEY_BACKSPACE: cmdManager.ExecuteCommand ("Backspace", NULL); break;
case VKEY_DELETE: cmdManager.ExecuteCommand ("Delete", NULL); break;
case VKEY_INSERT: editorSettings.SetInsertMode (!editorSettings.IsInsertMode()); break;
case VKEY_ENTER:
{
if (entryFieldHandler.IsActive ())
{
entryFieldHandler.HandleEnterKey (cmdManager);
}
else
{
pInputBuffer->InsertChar('\n');
CursorIndent ();
};
};
break;
case (VKFLG_CONTROL | VKEY_RIGHT): cmdManager.ExecuteCommand ("CursorNextWord", NULL); break;
case (VKFLG_CONTROL | VKEY_LEFT): cmdManager.ExecuteCommand ("CursorPrevWord", NULL); break;
case (VKFLG_CONTROL | VKEY_UP): cmdManager.ExecuteCommand ("ScrollWindowUp", NULL); break;
case (VKFLG_CONTROL | VKEY_DOWN): cmdManager.ExecuteCommand ("ScrollWindowDown", NULL); break;
case (VKFLG_CONTROL | VKFLG_SHIFT | VKEY_RIGHT): cmdManager.ExecuteCommand ("SelectNextWord", NULL); break;
case (VKFLG_CONTROL | VKFLG_SHIFT | VKEY_LEFT): cmdManager.ExecuteCommand ("SelectPrevWord", NULL); break;
case (VKFLG_SHIFT | VKEY_LEFT): cmdManager.ExecuteCommand ("SelectLeft", NULL); break;
case (VKFLG_SHIFT | VKEY_RIGHT): cmdManager.ExecuteCommand ("SelectRight", NULL); break;
case (VKFLG_SHIFT | VKEY_UP): cmdManager.ExecuteCommand ("SelectUp", NULL); break;
case (VKFLG_SHIFT | VKEY_DOWN): cmdManager.ExecuteCommand ("SelectDown", NULL); break;
case (CTRL_A): cmdManager.ExecuteCommand ("SelectAll", NULL); break;
case (CTRL_G): cmdManager.ExecuteCommand ("GotoLinePrompt", NULL); break;
case (CTRL_F): cmdManager.ExecuteCommand ("FindTextPrompt", NULL); break;
case (VKFLG_ALT | VKEY_UP): cmdManager.ExecuteCommand ("FindTextPrev", NULL); break;
case (VKFLG_ALT | VKEY_DOWN): cmdManager.ExecuteCommand ("FindTextNext", NULL); break;
case (CTRL_P): ToggleFileListDisplay (); break;
case (CTRL_S): cmdManager.ExecuteCommand ("Save", NULL); break;
case (CTRL_W): cmdManager.ExecuteCommand ("SaveAll", NULL); break;
case (CTRL_Q):
return (TRUE);
};
};
return (FALSE);
};
//-----------------------------------------------------------------------------
INT NCursesShell::NumDigits (INT iValueIn)
{
INT iCounter = 1;
INT iNumDigits = 0;
while (iValueIn >= iCounter)
{
++iNumDigits;
iCounter *= 10;
}
return (iNumDigits);
}
//-----------------------------------------------------------------------------
VOID NCursesShell::DisplayWindow (INT iScreenX,
INT iScreenY,
INT iWidth,
INT iHeight,
GapBuffer * pBuffer,
EditorSettings & editorSettings,
EntryFieldHandler & entryFieldHandler)
{
SyntaxParser syntaxParser;
Location locCursor = pBuffer->GetCursor();
Location locBufferCurr;
INT iScreenCol = 0;
INT iMaxLine = pBuffer->GetNumLines () + 1;
const INT LINE_BUFFER_LENGTH = 512;
static char szLine [LINE_BUFFER_LENGTH];
static INT szFullMarkup [LINE_BUFFER_LENGTH];
BOOL bGetStatusBarInput = entryFieldHandler.IsActive ();
BOOL bDrawStatusBar = bShowStatusBar || bGetStatusBarInput;
if (bDrawStatusBar)
{
iHeight -= 1;
}
// bounds check the window position
Location locWindowPosition = pBuffer->GetWindowPos ();
INT iTopLine = locWindowPosition.iLine;
INT iTopCol = locWindowPosition.iCol;
iTopLine = TMax (1, TMin (iMaxLine - iHeight, iTopLine));
iTopCol = TMax (0, iTopCol);
locWindowPosition.Set (iTopLine, iTopCol);
pBuffer->SetWindowPos (locWindowPosition);
// Draw the line numbers
if (bShowLineNumbers)
{
INT iMaxLineDigits = NumDigits (iMaxLine);
for (INT iScreenLine = 0; iScreenLine < iHeight; ++iScreenLine)
{
locBufferCurr.iLine = iScreenLine + iTopLine;
move(iScreenLine + iScreenY,iScreenX);
if (locBufferCurr.iLine < iMaxLine)
{
INT iCurrDigits = NumDigits (locBufferCurr.iLine);
INT iCurrPadding = iMaxLineDigits - iCurrDigits;
INT iDivisor = 1;
while (iCurrDigits > 1)
{
iDivisor *= 10;
--iCurrDigits;
};
// padding
for (; iCurrPadding > 0; --iCurrPadding)
{
addch (' ' | COLOR_PAIR(COLOR_WHITE) | A_REVERSE);
}
// line number
INT iDigit;
for (; iDivisor > 0; iDivisor /= 10)
{
iDigit = locBufferCurr.iLine / iDivisor;
locBufferCurr.iLine -= iDigit * iDivisor;
addch (('0' + iDigit) | COLOR_PAIR(COLOR_WHITE) | A_REVERSE);
}
// marking column (TODO)
addch (' ' | COLOR_PAIR(COLOR_WHITE) | A_REVERSE);
}
else
{
for (INT iIndex = 0; iIndex <= iMaxLineDigits; ++iIndex)
{
addch (' ' | COLOR_PAIR(COLOR_WHITE) | A_REVERSE);
};
}
}
iScreenX += iMaxLineDigits + 1;
iWidth -= iMaxLineDigits + 1;
}
pBuffer->SetLinesPerPage (iHeight);
pBuffer->SetColPerPage (iWidth);
// Draw the main window
// move to the starting point in the buffer
//printf ("Processing buffer with %d lines on a screen sized %d x %d\n\n", iMaxLine, iHeight, iWidth);
// step through each line to display
Location locHighlightStart = pBuffer->GetSelectionStart ();
Location locHighlightEnd = pBuffer->GetSelectionEnd ();
INT iHighlightFlag;
for (INT iScreenLine = iScreenY; iScreenLine < (iScreenY + iHeight); ++iScreenLine)
{
// TODO: determine if we are in a comment
locBufferCurr.iLine = (iScreenLine - iScreenY) + iTopLine;
// check and see if we are on a valid buffer line
if (locBufferCurr.iLine < iMaxLine)
{
INT iLineLength = pBuffer->GetLine (locBufferCurr.iLine, szLine, LINE_BUFFER_LENGTH);
szLine [iLineLength] = '\0';
syntaxParser.MarkupLine (szLine, iLineLength, szFullMarkup);
// display the line
iScreenCol = 0;
move (iScreenLine, iScreenX);
locBufferCurr.iCol = iTopCol;
for (; (iTopCol + iScreenCol < iLineLength) && (iScreenCol < iWidth); ++iScreenCol, ++locBufferCurr.iCol)
{
iHighlightFlag = GetHighlightState (locBufferCurr, locHighlightStart, locHighlightEnd) ? A_REVERSE : 0;
addch (szFullMarkup[iTopCol + iScreenCol] | iHighlightFlag);
}
for (; iScreenCol < iWidth; ++iScreenCol, ++locBufferCurr.iCol)
{
iHighlightFlag = GetHighlightState (locBufferCurr, locHighlightStart, locHighlightEnd) ? A_REVERSE : 0;
addch (' ' | iHighlightFlag);
}
}
else
{
move (iScreenLine, iScreenX);
for (iScreenCol = 0; iScreenCol < iWidth; ++iScreenCol)
{
addch (' ');
}
}
}
// display the status bar
// [1]Filename/Command [x] INS/OVR [xx] Line: 0000 / 00000 [xx] Col: 00[1]
static char szColNum [128];
static char szLineNum [128];
static char szFilename [512];
if (bDrawStatusBar)
{
snprintf (szColNum, sizeof (szColNum), "Col : %d", locCursor.iCol);
snprintf (szLineNum, sizeof (szLineNum), "Line: %d / %d", locCursor.iLine, iMaxLine);
int iColLen = strlen (szColNum);
int iLineLen = strlen (szLineNum);
int iFilenameMax = iWidth - 1 - 1 - 3 - 2 - iLineLen - 2 - iColLen - 1;
int iFilenameStart = 1;
int iFilenameLen = 0;
INT iStatusBarY = iScreenY + iHeight;
attron (A_REVERSE);
move (iStatusBarY, iScreenX);
addch (' ');
if (!bGetStatusBarInput)
{
snprintf (szFilename, iFilenameMax, "%s", pBuffer->GetFileName ());
iFilenameLen = strlen (szFilename);
addstr (szFilename);
}
for (INT iFill = 0; iFill < iFilenameMax - iFilenameLen; ++iFill)
{
addch (' ');
}
if (editorSettings.IsInsertMode ())
{
addstr (" INS ");
}
else
{
addstr (" OVR ");
};
addstr (szLineNum);
addch (' ');
addstr (szColNum);
addch (' ');
attroff (A_REVERSE);
// draw status line entry field if needed
if (bGetStatusBarInput)
{
move (iStatusBarY, iScreenX + iFilenameStart);
attron (A_REVERSE);
addstr (entryFieldHandler.GetInputField ().AsChar ());
addstr(": ");
attroff (A_REVERSE);
INT iEntryFieldInputLength = entryFieldHandler.GetInputField ().GetLength ();
DrawBufferEntryField (iScreenX + iFilenameStart + iEntryFieldInputLength + 2,
iStatusBarY,
iFilenameMax - iEntryFieldInputLength - 2,
entryFieldHandler.GetBuffer (),
editorSettings);
}
};
//move (0,0);
//printw("selectStart %d %d cursor %d %d", locHighlightStart.iLine, locHighlightStart.iCol, locCursor.iLine, locCursor.iCol);
// move the cursor to the correct place
if (!entryFieldHandler.IsActive ())
{
if (locHighlightStart.IsValid ())
{
curs_set(0);
move (-1, -1);
}
else
{
// move the ncurses cursor to our gap buffer cursor location
curs_set(2);
move (iScreenY + locCursor.iLine - iTopLine, iScreenX + locCursor.iCol - iTopCol);
}
};
};
//-----------------------------------------------------------------------------
VOID NCursesShell::DrawBufferEntryField (INT iScreenX,
INT iScreenY,
INT iWidth,
GapBuffer & buffer,
EditorSettings & editorSettings)
{
Location locCursor = buffer.GetCursor();
Location locBufferCurr;
INT iScreenCol = 0;
const INT LINE_BUFFER_LENGTH = 512;
static char szLine [LINE_BUFFER_LENGTH];
buffer.SetLinesPerPage (1);
buffer.SetColPerPage (iWidth);
Location locHighlightStart = buffer.GetSelectionStart ();
Location locHighlightEnd = buffer.GetSelectionEnd ();
Location locWindowPosition = buffer.GetWindowPos ();
INT iTopCol = locWindowPosition.iCol;
INT iHighlightFlag;
locBufferCurr.iLine = 1;
INT iLineLength = buffer.GetLine (locBufferCurr.iLine, szLine, LINE_BUFFER_LENGTH);
szLine [iLineLength] = '\0';
// display the line
iScreenCol = 0;
move (iScreenY, iScreenX);
locBufferCurr.iCol = iTopCol;
for (; (iTopCol + iScreenCol < iLineLength) && (iScreenCol < iWidth); ++iScreenCol, ++locBufferCurr.iCol)
{
iHighlightFlag = GetHighlightState (locBufferCurr, locHighlightStart, locHighlightEnd) ? A_REVERSE : 0;
addch (szLine[iTopCol + iScreenCol] | iHighlightFlag);
}
for (; iScreenCol < iWidth; ++iScreenCol, ++locBufferCurr.iCol)
{
iHighlightFlag = GetHighlightState (locBufferCurr, locHighlightStart, locHighlightEnd) ? A_REVERSE : 0;
addch (' ' | iHighlightFlag);
}
// move the cursor to the correct place
if (locHighlightStart.IsValid ())
{
curs_set(0);
move (-1, -1);
}
else
{
// move the ncurses cursor to our gap buffer cursor location
curs_set(2);
move (iScreenY + locCursor.iLine - 1, iScreenX + locCursor.iCol - iTopCol);
}
}
//-----------------------------------------------------------------------------
BOOL NCursesShell::GetHighlightState (Location & locCurr,
Location & locBegin,
Location & locEnd)
{
if ((locBegin.IsValid ())
&&
(locEnd.IsValid ())
&&
(locCurr >= locBegin)
&&
(locCurr <= locEnd)
)
{
return (TRUE);
};
return (FALSE);
};
//-----------------------------------------------------------------------------
INT NCursesShell::NCursesToVKey (INT nCursesKey)
{
move(1,0);
printw("ncurses key: %d %x %o ", nCursesKey, nCursesKey, nCursesKey);
if (nCursesKey >= 32 && nCursesKey <= 126)
{
return (nCursesKey);
};
switch (nCursesKey)
{
// TODO: These keys need to be handled better, possibly with libtermkey. The below approach
// is probably very terminal-implementation specific.
case 0x22f: return (VKFLG_CONTROL | VKEY_RIGHT);
case 0x220: return (VKFLG_CONTROL | VKEY_LEFT);
case 0x235: return (VKFLG_CONTROL | VKEY_UP);
case 0x20c: return (VKFLG_CONTROL | VKEY_DOWN);
case 0x22a: return (VKFLG_CONTROL | VKEY_PAGEUP);
case 0x225: return (VKFLG_CONTROL | VKEY_PAGEDOWN);
case 0x230: return (VKFLG_CONTROL | VKFLG_SHIFT | VKEY_RIGHT);
case 0x221: return (VKFLG_CONTROL | VKFLG_SHIFT | VKEY_LEFT);
case 0x00a: return (VKEY_ENTER);
case 0x09: return (VKEY_TAB); // replaces CTRL_I
case KEY_DOWN: return (VKEY_DOWN);
case KEY_UP: return (VKEY_UP);
case KEY_LEFT: return (VKEY_LEFT);
case KEY_RIGHT: return (VKEY_RIGHT);
case KEY_HOME: return (VKEY_HOME);
case KEY_END: return (VKEY_END);
case KEY_BACKSPACE: return (VKEY_BACKSPACE);
case KEY_DC: return (VKEY_DELETE);
case KEY_IC: return (VKEY_INSERT);
case KEY_ENTER: return (VKEY_NUMPAD_ENTER); // enter on numpad
// case KEY_TAB: return (VKEY_TAB);
case KEY_NPAGE: return (VKEY_PAGEDOWN);
case KEY_PPAGE: return (VKEY_PAGEUP);
case KEY_SLEFT: return (VKFLG_SHIFT | VKEY_LEFT);
case KEY_SRIGHT: return (VKFLG_SHIFT | VKEY_RIGHT);
case KEY_SR: return (VKFLG_SHIFT | VKEY_UP);
case KEY_SF: return (VKFLG_SHIFT | VKEY_DOWN);
case 0x233: return (VKFLG_ALT | VKEY_UP);
case 0x20a: return (VKFLG_ALT | VKEY_DOWN);
case 0x22d: return (VKFLG_ALT | VKEY_RIGHT);
case 0x21e: return (VKFLG_ALT | VKEY_LEFT);
default:
{
if (nCursesKey >= 1 && nCursesKey <= 26)
{
return (CTRL_A + nCursesKey - 1);
}
if (nCursesKey == KEY_F(1)) return (VKEY_F1);
else if (nCursesKey == KEY_F(2)) return (VKEY_F2);
else if (nCursesKey == KEY_F(3)) return (VKEY_F3);
else if (nCursesKey == KEY_F(4)) return (VKEY_F4);
else if (nCursesKey == KEY_F(5)) return (VKEY_F5);
else if (nCursesKey == KEY_F(6)) return (VKEY_F6);
else if (nCursesKey == KEY_F(7)) return (VKEY_F7);
else if (nCursesKey == KEY_F(8)) return (VKEY_F8);
else if (nCursesKey == KEY_F(9)) return (VKEY_F9);
else if (nCursesKey == KEY_F(10)) return (VKEY_F10);
else if (nCursesKey == KEY_F(11)) return (VKEY_F11);
else if (nCursesKey == KEY_F(12)) return (VKEY_F12);
else if (nCursesKey == KEY_F(13)) return (VKEY_F13);
else if (nCursesKey == KEY_F(14)) return (VKEY_F14);
else if (nCursesKey == KEY_F(15)) return (VKEY_F15);
}
break;
};
return (0);
};