-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathte_lines.c
231 lines (175 loc) · 4.02 KB
/
te_lines.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
/* te_lines.c
Text editor.
Operations with text lines.
Copyright (c) 2015-2021 Miguel Garcia / FloppySoftware
This program 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 2, or (at your option) any
later version.
This program 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 this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Changes:
06 Jan 2019 : Extracted from te.c.
07 Jan 2019 : Added InsertLine(), AppendLine(), SplitLine(), DeleteLine(), JoinLines().
08 Jan 2019 : Added SetLine(), ModifyLine(), ClearLine(). Modified InsertLine().
04 Jan 2021 : Use configuration variables.
*/
/* Return line # of first line printed on the editor box
-----------------------------------------------------
*/
GetFirstLine()
{
return lp_cur - box_shr;
}
/* Return line # of last line printed on the editor box
----------------------------------------------------
*/
GetLastLine()
{
int last;
last = GetFirstLine() + box_rows - 1;
return last >= lp_now - 1 ? lp_now - 1 : last; /* min(lp_now - 1, last) */
}
/* Set text in line #
------------------
Set 'text' to NULL for empty lines. Return NZ on success, else Z.
*/
SetLine(line, text, insert)
int line; char *text; int insert;
{
char *p;
int i;
if(insert && lp_now >= cf_mx_lines) {
ErrLineTooMany();
return 0;
}
if(!text) {
text = "";
}
if((p = AllocMem(strlen(text) + 1))) {
if(insert) {
for(i = lp_now; i > line; --i) {
lp_arr[i] = lp_arr[i - 1];
}
++lp_now;
}
else {
if(lp_arr[line]) {
free(lp_arr[line]);
}
}
lp_arr[line] = strcpy(p, text);
return 1;
}
return 0;
}
/* Modify text in line #
---------------------
Set 'text' to NULL for empty lines. Return NZ on success, else Z.
*/
ModifyLine(line, text)
int line; char *text;
{
return SetLine(line, text, 0);
}
/* Clear text in line #
--------------------
Return NZ on success, else Z.
*/
ClearLine(line)
int line;
{
return ModifyLine(line, NULL);
}
/* Insert new line before line #
-----------------------------
Set 'text' to NULL for empty lines. Return NZ on success, else Z.
*/
InsertLine(line, text)
int line; char *text;
{
return SetLine(line, text, 1);
}
/* Append new line after line #
-----------------------------
Set 'text' to NULL for empty lines. Return NZ on success, else Z.
*/
AppendLine(line, text)
int line; char *text;
{
return InsertLine(line + 1, text);
}
/* Split line # into two lines
---------------------------
Return NZ on success, else Z.
*/
SplitLine(line, pos)
int line, pos;
{
char *p, *p2;
if((p = AllocMem(pos + 1))) {
if(AppendLine(line, lp_arr[line] + pos)) {
/* We don't have strncpy() nor arrays of pointers yet, then... */
p2 = lp_arr[line];
p2[pos] = '\0';
strcpy(p, p2);
free(p2);
lp_arr[line] = p;
return 1;
}
free(p);
}
return 0;
}
/* Delete line #
-------------
Return NZ on success, else Z.
*/
DeleteLine(line)
int line;
{
int i;
free(lp_arr[line]);
--lp_now;
for(i = line; i < lp_now; ++i) {
lp_arr[i] = lp_arr[i + 1];
}
lp_arr[lp_now] = NULL;
return 1;
}
/* Join two consecutive lines
--------------------------
Return NZ on success, else Z.
*/
JoinLines(line)
int line;
{
char *p, *p1, *p2;
int s1, s2;
p1 = lp_arr[line];
p2 = lp_arr[line + 1];
s1 = strlen(p1);
s2 = strlen(p2);
if(s1 + s2 <= ln_max) {
if((p = AllocMem(s1 + s2 + 1))) {
/*
strcpy(p, p1); strcat(p, p2);
lp_arr[line] = p;
*/
//
lp_arr[line] = strcat(strcpy(p, p1), p2);
//
//lp_arr[line] = strcpy(strcpy(p, p1) + s1, p2); FIXME - What's wrong with this?
free(p1);
DeleteLine(line + 1);
return 1;
}
}
return 0;
}