-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmark.cpp
295 lines (249 loc) · 9.21 KB
/
mark.cpp
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
//
// Copyright 2015-2021 by Kevin L. Goodwin [[email protected]]; All rights reserved
//
// This file is part of K.
//
// K is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// K 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 K. If not, see <http://www.gnu.org/licenses/>.
//
#include "ed_main.h"
struct NamedPoint {
DLinkEntry<NamedPoint> dlink;
Point d_pt;
PChar d_pszName;
NamedPoint( const Point& pt, PCChar name ) : d_pt(pt), d_pszName( Strdup(name) ) {}
~NamedPoint() { Free0( d_pszName ); }
bool NameMatch( PCChar pszName ) const { return strcmp( d_pszName, pszName ) == 0; }
PCChar Name() const { return d_pszName; }
const Point &Pt() const { return d_pt; }
void AddLineDelta( int delta ) { d_pt.lin += delta; }
};
STATIC_FXN void NamedPointListDestroy( NamedPointHead &hd ) {
while( auto pEl=hd.front() ) {
DLINK_REMOVE_FIRST( hd, pEl, dlink );
delete pEl;
}
Assert( hd.empty() );
}
void FBUF::DestroyMarks() { NamedPointListDestroy( d_MarkHead ); }
STATIC_FXN void NamedPointListInsert( NamedPointHead &hd, NamedPoint *pNew ) {
DLINKC_FIRST_TO_LASTA( hd, dlink, pNP ) {
if( pNew->Pt() < pNP->Pt() ) {
DLINK_INSERT_BEFORE( hd, pNP, pNew, dlink );
return;
}
}
DLINK_INSERT_LAST( hd, pNew, dlink );
}
STATIC_FXN const NamedPoint *NamedPointFind( const NamedPointHead &hd, PCChar pszName ) {
DLINKC_FIRST_TO_LASTA( hd, dlink, pNP ) {
if( pNP->NameMatch( pszName ) )
return pNP;
}
return nullptr;
}
STATIC_FXN int NamedPointListDeleteAllMatches( NamedPointHead &hd, PCChar pszName, const Point *pPt=nullptr ) {
auto matchCount(0);
NamedPoint *pThis, *pNext;
DLINK_FIRST_TO_LAST( hd, dlink, pThis, pNext ) {
if( pThis->NameMatch( pszName ) && (!pPt || *pPt != pThis->Pt()) ) {
++matchCount;
DLINK_REMOVE( hd, pThis, dlink );
Delete0( pThis );
}
}
return matchCount;
}
STATIC_FXN void NamedPointListAddLineOfsPast( NamedPointHead &hd, const LINE yThld, const LINE yDelta ) {
auto fPastThld(false);
DLINKC_FIRST_TO_LASTA( hd, dlink, pNP ) {
if( !fPastThld && !(fPastThld = (pNP->Pt().lin > yThld)) ) {
pNP->AddLineDelta( yDelta );
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// ARG::showmarks ("mks"): show all marks defined, per file
//
STATIC_FXN int NamedPointListDump( const NamedPointHead &hd, PFBUF ofile, PCChar pszFilename ) {
auto markCount(0);
DLINKC_FIRST_TO_LASTA( hd, dlink, pNP ) {
++markCount;
ofile->FmtLastLine( "%s %u %uL1: %s"
, pszFilename
, pNP->Pt().lin + 1
, pNP->Pt().col + 1
, pNP->Name()
);
}
return markCount;
}
bool ARG::showmarks() {
STATIC_VAR int markdump_ctr;
auto ofile( OpenFileNotDir_NoCreate( FmtStr<25>( "<marks_%u>", markdump_ctr++ ) ) );
ofile->MakeEmpty();
auto totalMarks(0);
#if FBUF_TREE
rb_traverse( pNd, g_FBufIdx )
#else
DLINKC_FIRST_TO_LASTA(g_FBufHead, dlinkAllFBufs, pFBuf)
#endif
{
#if FBUF_TREE
auto pFBuf( IdxNodeToFBUF( pNd ) );
#endif
totalMarks += NamedPointListDump( pFBuf->d_MarkHead, ofile, pFBuf->Name() );
}
ofile->Undo_Reinit();
ofile->UnDirty();
ofile->SetNoEdit();
ofile->PutFocusOn();
return true;
}
//
/////////////////////////////////////////////////////////////////////////////////////////
bool FBUF::FindMark( PCChar pszMarkname, FBufLocn *pFL ) {
auto pMatchMk = NamedPointFind( d_MarkHead, pszMarkname );
if( !pMatchMk )
return false;
pFL->Set( this, pMatchMk->Pt() );
return true;
}
struct FileHasMark {
PCChar d_pszMarkname;
FBufLocn *d_pFL;
FileHasMark( PCChar pszMarkname, FBufLocn *pFL ) : d_pszMarkname(pszMarkname), d_pFL (pFL ) {}
bool operator()( PFBUF pFBuf ) { return pFBuf->FindMark( d_pszMarkname, d_pFL ); }
};
STATIC_FXN bool FindMarkAllFiles( PCChar pszMarkname, FBufLocn *pFL ) {
#if FBUF_TREE
rb_traverse( pNd, g_FBufIdx )
#else
DLINKC_FIRST_TO_LASTA(g_FBufHead, dlinkAllFBufs, pFBuf)
#endif
{
#if FBUF_TREE
auto pFBuf( IdxNodeToFBUF( pNd ) );
#endif
if( pFBuf->FindMark( pszMarkname, pFL ) )
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////
bool MarkGoto( PCChar pszMarkname ) {
0 && DBG( "MK: %s+ '%s'", __func__, pszMarkname );
FBufLocn markLocn;
if( !FindMarkAllFiles( pszMarkname, &markLocn ) )
return ErrorDialogBeepf( "Mark '%s' not found", pszMarkname );
markLocn.ScrollToOk();
// 0 && DBG( "MK: %s- (%d,%d) in '%s'", __func__, markLocn.d_pt.col, markLocn.d_pt.lin, pFBuf->Name() );
return true;
}
STATIC_FXN int MarkDelete( PCChar pszMarkname, bool fVerbose=true ) {
int totalMarks = 0;
#if FBUF_TREE
rb_traverse( pNd, g_FBufIdx )
#else
DLINKC_FIRST_TO_LASTA(g_FBufHead, dlinkAllFBufs, pFBuf)
#endif
{
#if FBUF_TREE
auto pFBuf( IdxNodeToFBUF( pNd ) );
#endif
totalMarks += NamedPointListDeleteAllMatches( pFBuf->d_MarkHead, pszMarkname );
}
if( fVerbose ) {
if( totalMarks )
Msg( "%s: %d mark%s deleted", pszMarkname, totalMarks, Add_s(totalMarks) );
else
ErrorDialogBeepf( "%s: Mark not found", pszMarkname );
}
return totalMarks;
}
void MarkDefineAtCurPos( PCChar pszNewMarkName ) {
const int totalMarks( MarkDelete( pszNewMarkName, false ) );
0 && DBG( "%s: zapped %d prev defines of '%s'", __func__, totalMarks, pszNewMarkName );
NamedPointListInsert( g_CurFBuf()->d_MarkHead, new NamedPoint( g_Cursor(), pszNewMarkName ) );
}
bool ARG::mark() {
DBG("MK: fn_mark");
switch( d_argType ) {
default: return BadArg();
case NOARG: g_CurView()->MoveCursor( 0, 0 ); return true;
case NULLARG: g_CurView()->ScrollToPrev(); return true;
case TEXTARG: if( StrSpnSignedInt( d_textarg.pText ) ) {
g_CurView()->MoveCursor( atoi( d_textarg.pText ) - 1, 0 );
return true;
}
if( d_cArg == 2 ) {
if( d_fMeta )
MarkDelete( d_textarg.pText, true );
else
MarkDefineAtCurPos( d_textarg.pText );
return true;
}
return MarkGoto( d_textarg.pText );
}
}
void AdjMarksForBoxDeletion( PFBUF pFBuf, COL xLeft, LINE yTop, COL xRight, LINE yBottom ) {
0 && DBG( "MK: %s '%s'", __func__, pFBuf->Name() );
}
void AdjustMarksForLineDeletion( PFBUF pFBuf,int xLeft,int yTop,int xRight,int yBottom ) {
0 && DBG( "MK: %s '%s'", __func__, pFBuf->Name() );
}
void AdjustMarksForLineInsertion( LINE Line, int LineDelta, PFBUF pFBuf ) {
0 && DBG( "MK: %s '%s'", __func__, pFBuf->Name() );
NamedPointListAddLineOfsPast( pFBuf->d_MarkHead, Line, LineDelta );
}
#if 0
void UpdateMarksForBoxDelete( PFBUF pFBuf, COL xLeft, LINE yTop, COL xRight, LINE yBottom ); {
if( pFBuf->d_MarkHead ) {
const Point upperLeftCorner ( yTop , xLeft );
const Point lowerRightCorner( yBottom, xRight );
auto fAFlag(false);
for( auto pMark( &pFBuf->d_MarkHead->mark ); pMark->Valid(); pMark=pMark->Next() ) {
if( !fAFlag && upperLeftCorner >= pMark->pt ) pastTopLeftAlready_QQQ
break;
if( pMark->pt >= lowerRightCorner )
break;
fAFlag = true;
if( pMark->pt.col >= xLeft && xRight >= pMark->pt.col ) { // nextMarkInBuf
pMark->pt.col = pMark->pt.col - xLeft + 1;
pMark->pt.lin = pMark->pt.lin - yTop + 1;
}
}
}
}
#endif
void AdjMarksForInsertion( PCFBUF pFBufSrc, PFBUF pFBufDest, int xLeft, int yTop, int xRight, int yBottom, int xDest, int yDest )
{
#if 0
auto pFBuf(pFBufSrc);
if( pFBufSrc ) {
if( pFBufSrc != pFBufDest // file -> file copy (not simple insertion of blank lines)?
&& pFBufSrc != g_pFbufClipboard // neither of the two PFBUFs is <clipboard>
&& pFBufDest != g_pFbufClipboard
)
return; // then there is nothing to do!
}
else {
pFBuf = pFBufDest;
const COL xtmp( xRight + 1 );
xRight = BUFBYTES;
xDest = xtmp;
}
UpdateMarksForBoxDelete( pFBuf, xLeft, yTop, xRight, yBottom );
#endif
}