-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfn_misc.cpp
137 lines (118 loc) · 4.06 KB
/
fn_misc.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
//
// Copyright 2015-2022 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"
bool ARG::true_ () { return true ; }
bool ARG::false_() { return false; }
bool ARG::wrstatefile() {
WriteStateFile();
return true;
}
bool ARG::cwd() {
return PushVariableMacro( Path::GetCwd().c_str() );
}
bool ARG::dirty() {
return g_CurFBuf()->IsDirty();
}
bool ARG::seteol() {
// d_cArg == 1: set UNIX EOL mode
// d_cArg >= 2: set DOS EOL mode
return g_CurFBuf()->SetEolModeChanged( d_cArg >= 2 ? EolCRLF : EolLF );
}
#if defined(_WIN32)
bool FBUF::MakeDiskFileWritable() {
auto fFileROAttrChanged( false );
if( FnmIsDiskWritable() ) {
FileAttribs fa( Name() );
if( fa.Exists() && fa.IsReadonly() ) {
if( fa.MakeWritableFailed( Name() ) ) {
return Msg( "Could not make '%s' writable!", Name() );
}
fFileROAttrChanged = true;
}
}
SetDiskRW();
if( !IsNoEdit() ) { // buffer already RW?
if( fFileROAttrChanged ) {
return !Msg( "%s buf was RW, diskfile now RW", Name() );
}
return Msg( "%s buf was RW, did nothing", Name() );
}
SetDiskRW();
ClrNoEdit();
Msg( "%s buf now RW, diskfile %s RW"
, Name()
, fFileROAttrChanged ? "now" : "was"
);
return true;
}
#endif
#ifdef fn_rw
bool ARG::rw() {
return g_CurFBuf()->MakeDiskFileWritable();
}
#endif
//-------------------------------------------------------------------------------------------------
#ifdef fn_mergefilenames
bool ARG::mergefilenames() {
switch( d_argType ) {
case TEXTARG: {
linebuf lbuf;
Path::Union( BSOB(lbuf), d_textarg.pText, g_CurFBuf()->Name() );
Msg( "%s", lbuf );
return true;
}
case NOARG: {
pathbuf drive, dir, fname, ext;
_splitpath( g_CurFBuf()->Name(), drive, dir, fname, ext );
Msg( "%s | %s | %s | %s", drive, dir, fname, ext );
return true;
}
default:
return BadArg();
}
}
#endif
#ifdef fn_nonseq_gap
STATIC_FXN bool strs_diff_by_1( stref s1, stref s2, UI base=10 ) { enum { SD=1 };
int e1; uintmax_t v1; stref a1; UI b1; std::tie( e1, v1, a1, b1 ) = conv_u( s1, base );
int e2; uintmax_t v2; stref a2; UI b2; std::tie( e2, v2, a2, b2 ) = conv_u( s2, base );
const bool rv( e1 || e2 ? false : ((v1+1 == v2) || (v1 == v2+1)) );
SD && DBG( "%s: %d '%" PR_BSR "'!'%" PR_BSR "'=%ju;%d vs '%" PR_BSR "'!'%" PR_BSR "'=%ju;%d", __func__, rv, BSR(s1), BSR(a1), v1, e1, BSR(s2), BSR(a2), v2, e2 );
return rv;
}
bool ARG::nonseq_gap() {
LINE yMin, yMax; GetLineRange ( &yMin, &yMax );
COL xMin, xMax; GetColumnRange( &xMin, &xMax );
const auto possible(yMax - yMin);
auto num_diff(0);
for( auto iy(yMin) ; iy < yMax; ++iy ) {
const auto sr1( g_CurFBuf()->PeekRawLineSeg( iy , xMin, xMax ) );
const auto sr2( g_CurFBuf()->PeekRawLineSeg( iy+1, xMin, xMax ) );
if( !IsStringBlank( sr1 )
&& !IsStringBlank( sr2 )
&& !strs_diff_by_1( sr1, sr2 )
) {
g_CurFBuf()->InsBlankLinesBefore( iy+1 );
++iy, ++yMax, ++num_diff;
}
}
Msg( "%d of %d nonsequential lines", num_diff, possible );
return num_diff > 0;
}
#endif