Repository navigation
Expand file tree
/
Copy patheditor.cpp
More file actions
8738 lines (7308 loc) · 219 KB
/
Copy patheditor.cpp
File metadata and controls
8738 lines (7308 loc) · 219 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
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
/*
editor.cpp
Редактор
*/
/*
Copyright (c) 1996 Eugene Roshal
Copyright (c) 2000 Far Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "headers.hpp"
#include "editor.hpp"
#include "edit.hpp"
#include "keyboard.hpp"
#include "lang.hpp"
#include "macroopcode.hpp"
#include "keys.hpp"
#include "ctrlobj.hpp"
#include "chgprior.hpp"
#include "filestr.hpp"
#include "dialog.hpp"
#include "fileedit.hpp"
#include "savescr.hpp"
#include "scrbuf.hpp"
#include "TPreRedrawFunc.hpp"
#include "syslog.hpp"
#include "interf.hpp"
#include "message.hpp"
#include "mix.hpp"
#include "clipboard.hpp"
#include "xlat.hpp"
#include "datetime.hpp"
#include "stddlg.hpp"
#include "strmix.hpp"
#include "farcolors.hpp"
#include "DialogBuilder.hpp"
#include "wakeful.hpp"
#include "codepage.hpp"
#include "vmenu.hpp"
#include <algorithm>
static int ReplaceMode, ReplaceAll;
static int EditorID = 0;
static DWORD64 MakeWordWrapTailFillColor(DWORD64 color)
{
return color & ~(IMPORTANT_LINE_CHAR | EXPLICIT_LINE_BREAK | COMMON_LVB_STRIKEOUT | COMMON_LVB_UNDERSCORE);
}
static bool TryGetWordWrapEmptyVisualLineFillColor(Edit *line, DWORD64 &fill_color)
{
ColorItem ci;
for (size_t i = 0; line->GetColor(&ci, i); ++i)
{
if ((ci.StartPos <= 0 && ci.EndPos >= line->GetLength() - 1)
|| (ci.StartPos == -1 && ci.EndPos == -1))
{
fill_color = MakeWordWrapTailFillColor(ci.Color);
return true;
}
}
return false;
}
static void GetWordWrapLogicalColorRange(const ColorItem &ci, int xoff, int &logical_start, int &logical_end)
{
logical_start = ci.StartPos;
logical_end = ci.EndPos;
if (ci.StartPos != -1 && ci.EndPos != -1) {
logical_start -= xoff;
logical_end -= xoff;
}
}
static bool ColorCoversWordWrapVisualLine(
const ColorItem &ci,
int logical_start,
int logical_end,
int visual_line_start,
int visual_line_end)
{
return (ci.StartPos == -1 && ci.EndPos == -1)
|| (logical_start <= visual_line_start && logical_end >= visual_line_end - 1);
}
static bool TryMapColorToWordWrapVisualLine(
const ColorItem &ci,
int logical_start,
int logical_end,
int visual_line_start,
int visual_line_end,
ColorItem &mapped_ci)
{
const int visual_line_length = visual_line_end - visual_line_start;
if (visual_line_length <= 0)
return false;
mapped_ci = ci;
if (ci.StartPos == -1 && ci.EndPos == -1)
{
mapped_ci.StartPos = 0;
mapped_ci.EndPos = visual_line_length - 1;
return true;
}
if (logical_start >= visual_line_end || logical_end < visual_line_start)
return false;
mapped_ci.StartPos = std::max(logical_start, visual_line_start) - visual_line_start;
mapped_ci.EndPos = std::min(logical_end, visual_line_end - 1) - visual_line_start;
return mapped_ci.StartPos <= mapped_ci.EndPos;
}
static bool DeleteRealRange(Edit *line, int start, int end, int cursor_pos)
{
if (end <= start) {
return false;
}
int length = line->GetLength();
if (start < 0) {
start = 0;
}
if (start > length) {
start = length;
}
if (end > length) {
end = length;
}
if (end <= start) {
line->SetCurPos(start);
return false;
}
std::wstring tmp = line->GetString();
if (length > end) {
tmp.erase(start, end - start);
} else {
tmp.resize(start);
}
tmp+= line->GetEOL();
line->SetBinaryString(tmp.data(), static_cast<int>(tmp.size()));
line->SetCurPos(cursor_pos < start ? cursor_pos : start);
return true;
}
static bool DeleteColumnRange(Edit *line, int start_cell, int end_cell)
{
if (end_cell <= start_cell) {
return false;
}
const int start = line->CellPosToReal(start_cell);
const int end = line->CellPosToReal(end_cell);
return DeleteRealRange(line, start, end, start);
}
// EditorUndoData
enum
{
UNDO_EDIT = 1,
UNDO_INSSTR,
UNDO_DELSTR,
UNDO_BEGIN,
UNDO_END
};
Editor::Editor(ScreenObject *pOwner, bool DialogUsed)
:
UndoPos(nullptr),
UndoSavePos(nullptr),
UndoSkipLevel(0),
LastChangeStrPos(0),
NumLastLine(0),
NumLine(0),
Pasting(0),
BlockStart(nullptr),
BlockStartLine(0),
VBlockStart(nullptr),
MaxRightPos(0),
LastSearchCase(GlobalSearchCase),
LastSearchWholeWords(GlobalSearchWholeWords),
LastSearchReverse(GlobalSearchReverse),
LastSearchSelFound(Opt.EdOpt.SearchSelFound),
LastSearchRegexp(Opt.EdOpt.SearchRegexp),
m_WordWrapPreferredCellPos(0),
m_codepage(CP_OEMCP),
m_Color(FarColorToReal(COL_EDITORTEXT)),
m_SelColor(FarColorToReal(COL_EDITORSELECTEDTEXT)),
m_ColorUnChanged(FarColorToReal(COL_DIALOGEDITUNCHANGED)),
StartLine(-1),
StartChar(-1),
StackPos(0),
NewStackPos(FALSE),
EditorID(::EditorID++),
HostFileEditor(nullptr),
TopList(nullptr),
EndList(nullptr),
TopScreen(nullptr),
m_TopScreenVisualLine(0),
m_CachedTotalVisualLines(1),
m_CachedTopVisualLine(0),
m_CachedScrollbarTopScreen(nullptr),
m_CachedScrollbarTopScreenVisualLine(0),
m_VisualScrollbarDirty(true),
CurLine(nullptr),
LastGetLine(nullptr),
LastGetLineNumber(0),
SaveTabSettings(false),
m_MouseButtonIsHeld(false),
m_CachedTotalLines(0),
m_CachedLineNumWidth(0),
m_LineCountDirty(true),
m_BulkLoadMode(false),
m_showCursor(true)
{
_KEYMACRO(SysLog(L"Editor::Editor()"));
_KEYMACRO(SysLog(1));
EdOpt = Opt.EdOpt;
m_bWordWrap = EdOpt.WordWrap;
SetOwner(pOwner);
if (DialogUsed)
Flags.Set(FEDITOR_DIALOGMEMOEDIT);
/*
$ 26.10.2003 KM
Если установлен глобальный режим поиска 16-ричных кодов, тогда
сконвертируем GlobalSearchString в строку, ибо она содержит строку в
16-ричном представлении.
*/
if (GlobalSearchHex)
Transform(strLastSearchStr, strGlobalSearchString, L'S');
else
strLastSearchStr = strGlobalSearchString;
UnmarkMacroBlock();
/*
$ 12.01.2002 IS
По умолчанию конец строки так или иначе равен \r\n, поэтому нечего
пудрить мозги, пропишем его явно.
*/
wcscpy(GlobalEOL, NATIVE_EOLW);
memset(&SavePos, 0xff, sizeof(SavePos));
InsertString(nullptr, 0);
}
Editor::~Editor()
{
//_SVS(SysLog(L"[%p] Editor::~Editor()",this));
auto t = GetProcessUptimeMSec();
EcoString::sDebugPrintStats("~Editor start");
FreeAllocatedData();
KeepInitParameters();
EcoString::sDebugPrintStats("~Editor finish");
fprintf(stderr, "* ~Editor took %lu msec\n", (unsigned long)(GetProcessUptimeMSec() - t));
_KEYMACRO(SysLog(-1));
_KEYMACRO(SysLog(L"Editor::~Editor()"));
}
void Editor::AdjustScreenPosition()
{
if (!m_bWordWrap)
return;
const int cur_visual_line = GetCurVisualLine();
// First, place the current visual line at the top of the screen
TopScreen = CurLine;
m_TopScreenVisualLine = cur_visual_line;
// Then, scroll up by half the screen height to center it
int HalfScreen = (Y2 - Y1 + 1) / 2;
m_VisualScrollbarDirty = true;
for (int i = 0; i < HalfScreen; ++i)
{
if (!DecTopVisualLine()) {
// Reached the top of the file, can't scroll up further
break;
}
}
}
int Editor::GetCurVisualLine() const
{
return (m_bWordWrap && CurLine) ? CurLine->FindVisualLine(CurLine->GetCurPos()) : 0;
}
void Editor::RememberWordWrapPreferredCellPos()
{
if (!m_bWordWrap || !CurLine)
return;
int visual_line_start, ignored_end;
CurLine->GetVisualLine(GetCurVisualLine(), visual_line_start, ignored_end);
m_WordWrapPreferredCellPos = CurLine->RealPosToCell(0, visual_line_start, CurLine->GetCurPos(), nullptr);
}
void Editor::SetCursorByVisualLineCellOffset(int VisualLine, int horizontal_cell_pos)
{
int new_start, new_end;
CurLine->GetVisualLine(VisualLine, new_start, new_end);
int new_pos = new_start;
const int line_length = CurLine->GetLength();
const int limit_pos = std::min(new_end, line_length);
while (new_pos < limit_pos) {
const int next_pos = CurLine->CalcPosFwdTo(new_pos, limit_pos);
if (next_pos <= new_pos || CurLine->RealPosToCell(0, new_start, next_pos, nullptr) > horizontal_cell_pos)
break;
new_pos = next_pos;
}
// For wrapped sub-lines, the [start, end) boundary belongs to the next visual line.
// If the preferred x lands on or beyond that boundary, keep the cursor on the last
// position that still belongs to the current visual line.
if (new_end < line_length && new_pos >= new_end) {
new_pos = CurLine->CalcPosBwdTo(new_end);
}
// A safety clamp to ensure we don't go past the actual end of the string data.
if (new_pos > line_length) {
new_pos = line_length;
}
CurLine->SetCurPos(new_pos);
}
void Editor::RestoreWordWrapPreferredCellPos()
{
if (!m_bWordWrap || !CurLine)
return;
SetCursorByVisualLineCellOffset(GetCurVisualLine(), m_WordWrapPreferredCellPos);
}
void Editor::SetWordWrapCursorPosition(int NewPos)
{
if (!CurLine)
return;
CurLine->SetCurPos(NewPos);
RememberWordWrapPreferredCellPos();
}
void Editor::SetWordWrapCursorPosition(int NewPos, int VisualLine)
{
if (!CurLine)
return;
CurLine->SetCurPos(NewPos);
if (!m_bWordWrap)
return;
int visual_line_start, visual_line_end;
CurLine->GetVisualLine(VisualLine, visual_line_start, visual_line_end);
if (visual_line_end < CurLine->GetLength() && CurLine->GetCurPos() >= visual_line_end) {
CurLine->SetCurPos(CurLine->CalcPosBwdTo(visual_line_end));
}
m_WordWrapPreferredCellPos = CurLine->RealPosToCell(0, visual_line_start, CurLine->GetCurPos(), nullptr);
}
// Helper function to count total lines in the editor
int Editor::CalculateTotalLines()
{
int TotalLines = 0;
for (Edit *CountPtr = TopList; CountPtr; CountPtr = CountPtr->m_next) {
TotalLines++;
}
return TotalLines;
}
// Helper function to calculate the width needed for line number display
int Editor::CalculateLineNumberWidth()
{
if (!EdOpt.ShowLineNumbers) {
return EdOpt.ShowGutterMarks ? 1 : 0;
}
// Use cached value if available
if (!m_LineCountDirty) {
return m_CachedLineNumWidth;
}
// Recalculate when cache is dirty
int TotalLines = CalculateTotalLines();
int LineNumWidth = 1;
int temp = TotalLines;
while (temp >= 10) {
LineNumWidth++;
temp /= 10;
}
if (LineNumWidth < 4) {
LineNumWidth = 4;
}
LineNumWidth += 1; // Add space after numbers
// Update cache
m_CachedTotalLines = TotalLines;
m_CachedLineNumWidth = LineNumWidth;
m_LineCountDirty = false;
return LineNumWidth;
}
int Editor::CalculateTextAreaWidth(int BaseWidth, bool ReserveScrollBar)
{
int Width = BaseWidth;
if (ReserveScrollBar) {
Width--;
}
Width -= CalculateLineNumberWidth();
return std::max(Width, 1);
}
void Editor::RecalculateAllWordWraps(bool SyncWordWrapState)
{
int Width = 0;
if (ObjWidth() > 0) {
Width = CalculateTextAreaWidth(ObjWidth(), EdOpt.ShowScrollBar);
}
for (Edit *CurPtr = TopList; CurPtr; CurPtr = CurPtr->m_next)
{
if (SyncWordWrapState) {
CurPtr->SetWordWrap(m_bWordWrap);
}
CurPtr->X2 = X1 + Width - 1;
CurPtr->RecalculateWordWrap(Width, EdOpt.TabSize);
}
m_VisualScrollbarDirty = true;
}
void Editor::DrawGutterMark(int logical_line, int y, int line_num_x1)
{
if (line_num_x1 <= X1 || (!EdOpt.ShowLineNumbers && !EdOpt.ShowGutterMarks))
return;
const auto it = m_gutterMarks.find(logical_line);
if (it == m_gutterMarks.end())
return;
const int gx = line_num_x1 - 1;
uint64_t color = FarColorToReal(COL_EDITORLINENUMBER);
color &= ~(0x000000FFFFFF000Full | FOREGROUND_TRUECOLOR);
color |= it->second & (0x000000FFFFFF000Full | FOREGROUND_TRUECOLOR);
const CHAR_INFO cell{
{ .UnicodeChar = L'\x258d' },
color
};
ScrBuf.Write(gx, y, &cell, 1);
}
void Editor::FreeAllocatedData(bool FreeUndo)
{
for (Edit *e = TopList; e;) {
auto *d = e;
e = e->m_next;
d->~Edit();
}
TopList = EndList = TopScreen = nullptr;
EPool.Purge();
UndoData.Clear();
UndoSavePos = nullptr;
UndoPos = nullptr;
UndoSkipLevel = 0;
m_TopScreenVisualLine = 0;
m_VisualScrollbarDirty = true;
m_LineCountDirty = true; // Invalidate line number cache
ClearStackBookmarks();
NumLastLine = 0;
NumLine = 0;
}
void Editor::KeepInitParameters()
{
// Установлен глобальный режим поиска 16-ричных данных?
if (GlobalSearchHex)
Transform(strGlobalSearchString, strLastSearchStr, L'X');
else
strGlobalSearchString = strLastSearchStr;
GlobalSearchCase = LastSearchCase;
GlobalSearchWholeWords = LastSearchWholeWords;
GlobalSearchReverse = LastSearchReverse;
Opt.EdOpt.SearchSelFound = LastSearchSelFound;
Opt.EdOpt.SearchRegexp = LastSearchRegexp;
}
/*
преобразование из буфера в список
*/
int Editor::SetRawData(const wchar_t *SrcBuf, int SizeSrcBuf, int TextFormat)
{
FreeAllocatedData(true);
if (!SrcBuf) {
fprintf(stderr, "Editor::SetRawData null\n");
InsertString(nullptr, 0);
CurLine = TopList;
TopScreen = TopList;
NumLine = 0;
TextChanged(1);
return TRUE;
}
if (SizeSrcBuf < 0)
SizeSrcBuf = (int)StrLength(SrcBuf);
if (SizeSrcBuf == 0) {
fprintf(stderr, "Editor::SetRawData empty\n");
InsertString(nullptr, 0);
CurLine = TopList;
TopScreen = TopList;
NumLine = 0;
TextChanged(1);
return TRUE;
}
const wchar_t *ptr = SrcBuf;
const wchar_t *end = SrcBuf + SizeSrcBuf;
const wchar_t *text_eol = *GlobalEOL ? GlobalEOL : NATIVE_EOLW;
while (ptr < end) {
const wchar_t *line_start = ptr;
const wchar_t *eol_ptr = ptr;
while (eol_ptr < end && *eol_ptr != L'\r' && *eol_ptr != L'\n')
eol_ptr++;
int line_len = (int)(eol_ptr - line_start);
const wchar_t *eol = L"";
int eol_len = 0;
if (eol_ptr < end) {
if (*eol_ptr == L'\r') {
if (eol_ptr + 2 < end && eol_ptr[1] == L'\r' && eol_ptr[2] == L'\n') {
eol = L"\r\r\n";
eol_len = 3;
} else if (eol_ptr + 1 < end && eol_ptr[1] == L'\n') {
eol = L"\r\n";
eol_len = 2;
} else {
eol = L"\r";
eol_len = 1;
}
} else {
eol = L"\n";
eol_len = 1;
}
}
Edit *line = InsertString(line_start, line_len, nullptr, -1);
if (!line)
return FALSE;
if (eol_len > 0) {
line->SetEOL(TextFormat ? text_eol : eol);
}
if (eol_len == 0)
break;
ptr = eol_ptr + eol_len;
}
if (!TopList)
InsertString(nullptr, 0);
CurLine = TopList;
TopScreen = TopList;
NumLine = 0;
m_TopScreenVisualLine = 0;
TextChanged(1);
return TRUE;
}
/*
Editor::Edit2Str - преобразование из списка в буфер с учетом EOL
DestBuf - куда сохраняем (выделяется динамически!)
SizeDestBuf - размер сохранения
TextFormat - тип концовки строк
*/
int Editor::GetRawData(wchar_t **DestBuf, int &SizeDestBuf, int TextFormat)
{
wchar_t *PDest = nullptr;
SizeDestBuf = 0; // общий размер = 0
const wchar_t *EndSeq;
int Length;
// посчитаем количество строк и общий размер памяти (чтобы не дергать realloc)
Edit *CurPtr = TopList;
size_t AllLength = 0;
while (CurPtr) {
Length = CurPtr->GetLength(&EndSeq);
AllLength+= Length + StrLength(!TextFormat ? EndSeq : GlobalEOL) + 1;
CurPtr = CurPtr->m_next;
}
wchar_t *MemEditStr = reinterpret_cast<wchar_t *>(malloc((AllLength + 8) * sizeof(wchar_t)));
if (!MemEditStr) {
return FALSE;
}
*MemEditStr = 0;
PDest = MemEditStr;
// прйдемся по списку строк
CurPtr = TopList;
AllLength = 0;
while (CurPtr) {
Length = CurPtr->GetLength(&EndSeq);
CurPtr->GetString(PDest, Length + 1);
PDest+= Length;
size_t LenEndSeq;
if (!TextFormat) {
LenEndSeq = StrLength(EndSeq);
wmemcpy(PDest, EndSeq, LenEndSeq);
} else {
LenEndSeq = StrLength(GlobalEOL);
wmemcpy(PDest, GlobalEOL, LenEndSeq);
}
PDest+= LenEndSeq;
AllLength+= LenEndSeq + Length;
CurPtr = CurPtr->m_next;
}
*PDest = 0;
SizeDestBuf = (int)(PDest - MemEditStr);
if (DestBuf)
*DestBuf = MemEditStr;
return TRUE;
}
void Editor::DisplayObject()
{
ShowEditor(FALSE);
}
void Editor::ShowEditor(int CurLineOnly)
{
const int cur_visual_line = (m_bWordWrap && CurLine) ? GetCurVisualLine() : 0;
if (m_bWordWrap && CurLine)
{
EnsureTopScreenVisual();
const int off = VisualOffsetFromTop(CurLine, cur_visual_line);
if (off < 0 || off >= (Y2 - Y1 + 1)) {
AdjustScreenPosition();
}
}
if (Locked() || !TopList)
return;
if (m_bWordWrap)
{
CurLineOnly = FALSE;
// Centralized correction logic to prevent scrolling past the end of the file.
int ScreenHeight = Y2 - Y1 + 1;
int LinesBelow = GetVisualLinesBelow(TopScreen, m_TopScreenVisualLine, ScreenHeight);
if (LinesBelow < ScreenHeight)
{
int ScrollUpCount = ScreenHeight - LinesBelow;
for (int i = 0; i < ScrollUpCount; ++i)
{
if (!DecTopVisualLine()) {
// Reached the top of the file, can't scroll up further.
break;
}
}
}
CurLine->SetLeftPos(0);
// Ensure TopScreen pointers are initialized
EnsureTopScreenVisual();
if (!ScrBuf.GetLockCount()) {
if (Flags.Check(FEDITOR_JUSTMODIFIED)) {
Flags.Clear(FEDITOR_JUSTMODIFIED);
if (!Flags.Check(FEDITOR_DIALOGMEMOEDIT)
|| (CtrlObject && CtrlObject->Plugins.CurDialogEditor == this)) {
CtrlObject->Plugins.ProcessEditorEvent(EE_REDRAW, EEREDRAW_CHANGE);
}
} else {
if (!Flags.Check(FEDITOR_DIALOGMEMOEDIT)
|| (CtrlObject && CtrlObject->Plugins.CurDialogEditor == this)) {
CtrlObject->Plugins.ProcessEditorEvent(EE_REDRAW, CurLineOnly ? EEREDRAW_LINE : EEREDRAW_ALL);
}
}
}
DrawScrollbar();
Edit *CurLogicalLine = TopScreen;
int CurVisualLine = m_TopScreenVisualLine;
int CurLineNumber = GetTopScreenLineNumber() - 1;
for (int Y = Y1; Y <= Y2; Y++)
{
if (!CurLogicalLine)
{
// Clear the entire line including line number area when there's no content
SetScreen(X1, Y, XX2, Y, L' ', FarColorToReal(COL_EDITORTEXT));
continue;
}
RenderVisualLine(CurLineNumber, CurVisualLine, X1, Y, XX2);
// Advance to the next visual line
CurVisualLine++;
if (CurVisualLine >= CurLogicalLine->GetVisualLineCount())
{
CurVisualLine = 0;
CurLogicalLine = CurLogicalLine->m_next;
CurLineNumber++;
}
}
// transfer insert/overtype status to real string
// fix for https://github.com/elfmz/far2l/issues/3196
CurLine->SetOvertypeMode(Flags.Check(FEDITOR_OVERTYPE));
if (CurLogicalLine)
CurLogicalLine->SetOvertypeMode(Flags.Check(FEDITOR_OVERTYPE));
if (HostFileEditor)
HostFileEditor->ShowStatus();
return;
}
Edit *CurPtr;
int LeftPos, CurPos, Y;
// _SVS(SysLog(L"Enter to ShowEditor, CurLineOnly=%i",CurLineOnly));
/*
$ 10.08.2000 skv
To make sure that CurEditor is set to required value.
*/
if (!Flags.Check(FEDITOR_DIALOGMEMOEDIT))
CtrlObject->Plugins.CurEditor = HostFileEditor; // this;
if (NumLastLine > (Y2 - Y1) + 1)
XX2 = X2 - (EdOpt.ShowScrollBar ? 1 : 0);
else
XX2 = X2;
/*
17.04.2002 skv
Что б курсор не бегал при Alt-F9 в конце длинного файла.
Если на экране есть свободное место, и есть текст сверху,
перепозиционируем.
*/
if (!EdOpt.AllowEmptySpaceAfterEof) {
while (CalcDistance(TopScreen, nullptr, Y2 - Y1) < Y2 - Y1) {
if (TopScreen->m_prev)
TopScreen = TopScreen->m_prev;
else
break;
}
}
/*
если курсор удруг оказался "за экраном",
подвинем экран под курсор, а не
курсор загоним в экран.
*/
while (CalcDistance(TopScreen, CurLine, -1) >= Y2 - Y1 + 1) {
TopScreen = TopScreen->m_next;
// DisableOut=TRUE;
// ProcessKey(KEY_UP);
// DisableOut=FALSE;
}
CurPos = CurLine->GetCellCurPos();
if (!EdOpt.CursorBeyondEOL) {
MaxRightPos = CurPos;
int RealCurPos = CurLine->GetCurPos();
int Length = CurLine->GetLength();
if (RealCurPos > Length) {
CurLine->SetCurPos(Length);
CurLine->SetLeftPos(0);
CurPos = CurLine->GetCellCurPos();
}
}
if (!Pasting) {
/*
$ 10.08.2000 skv
Don't send EE_REDRAW while macro is being executed.
Send EE_REDRAW with param=2 if text was just modified.
*/
_SYS_EE_REDRAW(CleverSysLog Clev(L"Editor::ShowEditor()"));
if (!ScrBuf.GetLockCount()) {
/*
$ 13.09.2000 skv
EE_REDRAW 1 and 2 replaced.
*/
if (Flags.Check(FEDITOR_JUSTMODIFIED)) {
Flags.Clear(FEDITOR_JUSTMODIFIED);
if (!Flags.Check(FEDITOR_DIALOGMEMOEDIT)
|| (CtrlObject && CtrlObject->Plugins.CurDialogEditor == this)) {
_SYS_EE_REDRAW(SysLog(L"Call ProcessEditorEvent(EE_REDRAW,EEREDRAW_CHANGE)"));
CtrlObject->Plugins.ProcessEditorEvent(EE_REDRAW, EEREDRAW_CHANGE);
}
} else {
if (!Flags.Check(FEDITOR_DIALOGMEMOEDIT)
|| (CtrlObject && CtrlObject->Plugins.CurDialogEditor == this)) {
_SYS_EE_REDRAW(SysLog(L"Call ProcessEditorEvent(EE_REDRAW,%ls)",
(CurLineOnly ? "EEREDRAW_LINE" : "EEREDRAW_ALL")));
CtrlObject->Plugins.ProcessEditorEvent(EE_REDRAW,
CurLineOnly ? EEREDRAW_LINE : EEREDRAW_ALL);
}
}
}
_SYS_EE_REDRAW(else SysLog(L"ScrBuf Locked !!!"));
}
DrawScrollbar();
// Calculate line number width if needed (non-word-wrap mode)
int LineNumWidth = CalculateLineNumberWidth();
int LineNumX1 = X1 + LineNumWidth;
const bool HasLineNumArea = (LineNumWidth > 0);
int CurrentLineNum = GetTopScreenLineNumber();
if (!CurLineOnly) {
LeftPos = CurLine->GetLeftPos();
#if 0
// крайне эксперементальный кусок!
if (CurPos+LeftPos < XX2)
LeftPos=0;
else if (CurLine->X2 < XX2)
LeftPos=CurLine->GetLength()-CurPos;
if (LeftPos < 0)
LeftPos=0;
#endif
for (CurPtr = TopScreen, Y = Y1; Y <= Y2; Y++)
if (CurPtr) {
// Display line number if enabled, otherwise keep gutter area clean
if (HasLineNumArea) {
if (EdOpt.ShowLineNumbers) {
wchar_t LineNumStr[16];
swprintf(LineNumStr, ARRAYSIZE(LineNumStr), L"%*d ", LineNumWidth - 1, CurrentLineNum);
Text(X1, Y, FarColorToReal(COL_EDITORLINENUMBER), LineNumStr);
} else {
for (int i = 0; i < LineNumWidth; i++) {
Text(X1 + i, Y, FarColorToReal(COL_EDITORLINENUMBER), L" ");
}
}
DrawGutterMark(CurrentLineNum - 1, Y, LineNumX1);
}
CurPtr->SetEditBeyondEnd(TRUE);
CurPtr->SetPosition(HasLineNumArea ? LineNumX1 : X1, Y, XX2, Y);
// CurPtr->SetTables(UseDecodeTable ? &TableSet:nullptr);
//_D(SysLog(L"Setleftpos 3 to %i",LeftPos));
CurPtr->SetLeftPos(LeftPos);
if (CurPtr != CurLine) {
CurPtr->SetCellCurPos(CurPos);
CurPtr->FastShow();
}
CurPtr->SetEditBeyondEnd(EdOpt.CursorBeyondEOL);
CurPtr = CurPtr->m_next;
CurrentLineNum++;
} else {
// Clear the entire line including line number area when there's no content
SetScreen(X1, Y, XX2, Y, L' ', FarColorToReal(COL_EDITORTEXT)); // Пустые строки после конца текста
}
}
if (CurLineOnly && HasLineNumArea) {
const int CurLineY = Y1 + CalcDistance(TopScreen, CurLine, -1);
SetScreen(LineNumX1 - 1, CurLineY, LineNumX1 - 1, CurLineY, L' ', FarColorToReal(COL_EDITORLINENUMBER));
DrawGutterMark(NumLine, CurLineY, LineNumX1);
}
CurLine->SetOvertypeMode(Flags.Check(FEDITOR_OVERTYPE));
CurLine->SetCursorVisibleFlag(m_showCursor);
CurLine->Show();
if (VBlockStart && VBlockSizeX > 0 && VBlockSizeY > 0) {
int CurScreenLine = NumLine - CalcDistance(TopScreen, CurLine, -1);
LeftPos = CurLine->GetLeftPos();
// Account for line numbers when calculating VBlock positions
int VBlockBaseX = HasLineNumArea ? LineNumX1 : X1;
for (CurPtr = TopScreen, Y = Y1; Y <= Y2; Y++) {
if (CurPtr) {
if (CurScreenLine >= VBlockY && CurScreenLine < VBlockY + VBlockSizeY) {
int BlockX1 = VBlockX - LeftPos + VBlockBaseX;
int BlockX2 = VBlockX + VBlockSizeX - 1 - LeftPos + VBlockBaseX;
if (BlockX1 < VBlockBaseX)
BlockX1 = VBlockBaseX;
if (BlockX2 > XX2)
BlockX2 = XX2;
if (BlockX1 <= XX2 && BlockX2 >= VBlockBaseX)
ChangeBlockColor(BlockX1, Y, BlockX2, Y, FarColorToReal(COL_EDITORSELECTEDTEXT));
}
CurPtr = CurPtr->m_next;
CurScreenLine++;
}
}
}
if (HostFileEditor)
HostFileEditor->ShowStatus();
//_SVS(SysLog(L"Exit from ShowEditor"));
}
/*
$ 10.08.2000 skv
Wrapper for Modified.
Set JustModified every call to 1
to track any text state change.
Even if state==0, this can be
last UNDO.
*/
void Editor::TextChanged(int State)
{
Flags.Change(FEDITOR_MODIFIED, State);
Flags.Set(FEDITOR_JUSTMODIFIED);
}
bool Editor::CheckLine(Edit *line)
{
if (line) {
Edit *eLine;
for (eLine = TopList; eLine; eLine = eLine->m_next) {
if (eLine == line)
return true;
}