-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregion.c
More file actions
523 lines (470 loc) · 14.5 KB
/
region.c
File metadata and controls
523 lines (470 loc) · 14.5 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
/*
* The routines in this file
* deal with the region, that magic space
* between "." and mark. Some functions are
* commands. Some functions are just for
* internal use.
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
static int regionio PP((int f, int n, CONSTA char *mode));
/*
* Kill the region. Ask "getregion"
* to figure out the bounds of the region.
* Move "." to the start, and kill the characters.
* Bound to "C-W".
*/
int killregion(f, n)
int f, n;
{
register int s;
REGION region;
UNUSED_ARGS_FN;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if ((s=getregion(®ion)) != TRUE)
return (s);
if ((lastflag&CFKILL) == 0) /* This is a kill type */
kdelete(); /* command, so do magic */
thisflag |= CFKILL; /* kill buffer stuff. */
curwp->w_dotp = region.r_linep;
curwp->w_doto = region.r_offset;
return (ldelete(region.r_size, TRUE));
}
/*
* Copy all of the characters in the
* region to the kill buffer. Don't move dot
* at all. This is a bit like a kill region followed
* by a yank. Bound to "M-W".
*/
int copyregion(f, n)
int f, n;
{
register LINE *linep;
register int loffs;
register int s;
REGION region;
UNUSED_ARGS_FN;
if ((s=getregion(®ion)) != TRUE)
return (s);
if ((lastflag&CFKILL) == 0) /* Kill type command. */
kdelete();
thisflag |= CFKILL;
linep = region.r_linep; /* Current line. */
loffs = region.r_offset; /* Current offset. */
while (region.r_size-- > 0L) {
if (loffs == llength(linep)) { /* End of line. */
if ((s=kinsert('\n')) != TRUE)
return (s);
linep = lforw(linep);
loffs = 0;
} else { /* Middle of line. */
if ((s=kinsert(lgetc(linep, loffs))) != TRUE)
return (s);
++loffs;
}
}
mlwrite("[region copied]");
return (TRUE);
}
/* Narrow-to-region (^X-<) makes all but the current region in
the current buffer invisable and unchangable
*/
int narrow(f, n)
int f, n;
{
register int status; /* return status */
BUFFER *bp; /* buffer being narrowed */
WINDOW *wp; /* windows to fix up pointers in as well */
REGION creg; /* region boundry structure */
UNUSED_ARGS_FN;
/* find the proper buffer and make sure we aren't already narrow */
bp = curwp->w_bufp; /* find the right buffer */
if (bp->b_flag&BFNAROW) {
mlwrite("%%This buffer is already narrowed");
return(FALSE);
}
/* find the boundries of the current region */
if ((status=getregion(&creg)) != TRUE)
return(status);
curwp->w_dotp = creg.r_linep; /* only by full lines please! */
curwp->w_doto = 0;
creg.r_size += (long)creg.r_offset;
if (creg.r_size <= (long)curwp->w_dotp->l_used) {
mlwrite("%%Must narrow at least 1 full line");
return(FALSE);
}
/* archive the top fragment */
if (bp->b_linep->l_fp != creg.r_linep) {
bp->b_topline = bp->b_linep->l_fp;
creg.r_linep->l_bp->l_fp = (LINE *)NULL;
bp->b_linep->l_fp = creg.r_linep;
creg.r_linep->l_bp = bp->b_linep;
}
/* move forward to the end of this region
(a long number of bytes perhaps) */
while (creg.r_size > (long)32000) {
forwchar(TRUE, 32000);
creg.r_size -= (long)32000;
}
forwchar(TRUE, (int)creg.r_size);
curwp->w_doto = 0; /* only full lines! */
/* archive the bottom fragment */
if (bp->b_linep != curwp->w_dotp) {
bp->b_botline = curwp->w_dotp;
bp->b_botline->l_bp->l_fp = bp->b_linep;
bp->b_linep->l_bp->l_fp = (LINE *)NULL;
bp->b_linep->l_bp = bp->b_botline->l_bp;
}
/* let all the proper windows be updated */
wp = wheadp;
while (wp) {
if (wp->w_bufp == bp) {
wp->w_linep = creg.r_linep;
wp->w_dotp = creg.r_linep;
wp->w_doto = 0;
wp->w_markp = creg.r_linep;
wp->w_marko = 0;
wp->w_flag |= (WFHARD|WFMODE);
}
wp = wp->w_wndp;
}
/* and now remember we are narrowed */
bp->b_flag |= BFNAROW;
mlwrite("[Buffer is narrowed]");
return(TRUE);
}
/* widen-from-region (^X->) restores a narrowed region */
int widen(f, n)
int f, n;
{
LINE *lp; /* temp line pointer */
BUFFER *bp; /* buffer being narrowed */
WINDOW *wp; /* windows to fix up pointers in as well */
UNUSED_ARGS_FN;
/* find the proper buffer and make sure we are narrow */
bp = curwp->w_bufp; /* find the right buffer */
if ((bp->b_flag&BFNAROW) == 0) {
mlwrite("%%This buffer is not narrowed");
return(FALSE);
}
/* recover the top fragment */
if (bp->b_topline != (LINE *)NULL) {
lp = bp->b_topline;
while (lp->l_fp != (LINE *)NULL)
lp = lp->l_fp;
lp->l_fp = bp->b_linep->l_fp;
lp->l_fp->l_bp = lp;
bp->b_linep->l_fp = bp->b_topline;
bp->b_topline->l_bp = bp->b_linep;
bp->b_topline = (LINE *)NULL;
}
/* recover the bottom fragment */
if (bp->b_botline != (LINE *)NULL) {
lp = bp->b_botline;
while (lp->l_fp != (LINE *)NULL)
lp = lp->l_fp;
lp->l_fp = bp->b_linep;
bp->b_linep->l_bp->l_fp = bp->b_botline;
bp->b_botline->l_bp = bp->b_linep->l_bp;
bp->b_linep->l_bp = lp;
bp->b_botline = (LINE *)NULL;
}
/* let all the proper windows be updated */
wp = wheadp;
while (wp) {
if (wp->w_bufp == bp)
wp->w_flag |= (WFHARD|WFMODE);
wp = wp->w_wndp;
}
/* and now remember we are not narrowed */
bp->b_flag &= (~BFNAROW);
mlwrite("[Buffer is widened]");
return(TRUE);
}
/*
* Lower case region. Zap all of the upper
* case characters in the region to lower case. Use
* the region code to set the limits. Scan the buffer,
* doing the changes. Call "lchange" to ensure that
* redisplay is done in all buffers. Bound to
* "C-X C-L".
*/
int lowerregion(f, n)
int f, n;
{
register LINE *linep;
register int loffs;
register int c;
register int s;
REGION region;
UNUSED_ARGS_FN;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if ((s=getregion(®ion)) != TRUE)
return (s);
lchange(WFHARD);
linep = region.r_linep;
loffs = region.r_offset;
while (region.r_size-- > 0L) {
if (loffs == llength(linep)) {
linep = lforw(linep);
loffs = 0;
} else {
c = lgetc(linep, loffs);
if (c>='A' && c<='Z')
lputc(linep, loffs, (char) (c+'a'-'A'));
++loffs;
}
}
return (TRUE);
}
/*
* Upper case region. Zap all of the lower
* case characters in the region to upper case. Use
* the region code to set the limits. Scan the buffer,
* doing the changes. Call "lchange" to ensure that
* redisplay is done in all buffers. Bound to
* "C-X C-L".
*/
int upperregion(f, n)
int f, n;
{
register LINE *linep;
register int loffs;
register int c;
register int s;
REGION region;
UNUSED_ARGS_FN;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if ((s=getregion(®ion)) != TRUE)
return (s);
lchange(WFHARD);
linep = region.r_linep;
loffs = region.r_offset;
while (region.r_size-- > 0L) {
if (loffs == llength(linep)) {
linep = lforw(linep);
loffs = 0;
} else {
c = lgetc(linep, loffs);
if (c>='a' && c<='z')
lputc(linep, loffs, (char) (c-'a'+'A'));
++loffs;
}
}
return (TRUE);
}
/*
* This routine figures out the
* bounds of the region in the current window, and
* fills in the fields of the "REGION" structure pointed
* to by "rp". Because the dot and mark are usually very
* close together, we scan outward from dot looking for
* mark. This should save time. Return a standard code.
* Callers of this routine should be prepared to get
* an "ABORT" status; we might make this have the
* conform thing later.
*/
int getregion(rp)
REGION *rp;
{
register LINE *flp;
register LINE *blp;
long fsize;
long bsize;
if (curwp->w_markp == NULL) {
mlwrite("No mark set in this window");
return (FALSE);
}
if (curwp->w_dotp == curwp->w_markp) {
rp->r_linep = curwp->w_dotp;
if (curwp->w_doto < curwp->w_marko) {
rp->r_offset = curwp->w_doto;
rp->r_size = (long)(curwp->w_marko-curwp->w_doto);
} else {
rp->r_offset = curwp->w_marko;
rp->r_size = (long)(curwp->w_doto-curwp->w_marko);
}
return (TRUE);
}
blp = curwp->w_dotp;
bsize = (long)curwp->w_doto;
flp = curwp->w_dotp;
fsize = (long)(llength(flp)-curwp->w_doto+1);
while (flp!=curbp->b_linep || lback(blp)!=curbp->b_linep) {
if (flp != curbp->b_linep) {
flp = lforw(flp);
if (flp == curwp->w_markp) {
rp->r_linep = curwp->w_dotp;
rp->r_offset = curwp->w_doto;
rp->r_size = fsize+curwp->w_marko;
return (TRUE);
}
fsize += llength(flp)+1;
}
if (lback(blp) != curbp->b_linep) {
blp = lback(blp);
bsize += llength(blp)+1;
if (blp == curwp->w_markp) {
rp->r_linep = blp;
rp->r_offset = curwp->w_marko;
rp->r_size = bsize - curwp->w_marko;
return (TRUE);
}
}
}
mlwrite("Bug: lost mark");
return (FALSE);
}
#if DECEDT
/*
* Kill the region to the kill buffer, and then remove the mark.
*/
int cutregion(f, n)
int f, n;
{
int s;
s = killregion(f, n);
if (s == TRUE) { curwp->w_markp = NULL; curwp->w_marko = 0; }
return(s);
}
/*
* Append the region to the kill buffer, and then kill it.
*/
int apkillregion(f, n)
int f, n;
{
lastflag |= CFKILL;
return(killregion(f, n));
}
/*
* Append all of the characters in the
* region to the kill buffer.
*/
int apcopyregion(f, n)
int f, n;
{
UNUSED_ARGS_FN;
lastflag |= CFKILL;
return(copyregion(f, n));
}
/*
* Change case in region. Use
* the region code to set the limits. Scan the buffer,
* doing the changes. Call "lchange" to ensure that
* redisplay is done in all buffers.
*/
int caseregion(f, n)
int f, n;
{
register LINE *linep;
register int loffs;
register int c;
register int s;
REGION region;
UNUSED_ARGS_FN;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if ((s=getregion(®ion)) != TRUE)
return (s);
lchange(WFHARD);
linep = region.r_linep;
loffs = region.r_offset;
while (region.r_size-- > 0L) {
if (loffs == llength(linep)) {
linep = lforw(linep);
loffs = 0;
} else {
c = lgetc(linep, loffs);
if (c>='a' && c<='z')
lputc(linep, loffs, (char) (c-'a'+'A'));
else if (c>='A' && c<='Z')
lputc(linep, loffs, (char) (c-'A'+'a'));
++loffs;
}
}
return (TRUE);
}
/*
* Ask for a file name, and write the
* contents of the current region to that file.
*
* This function performs the details of file
* writing. Uses the file management routines in the
* "fileio.c" package. The number of lines written is
* displayed. Sadly, it looks inside a LINE; provide
* a macro for this. Most of the grief is error
* checking of some sort.
*/
static int regionio(f, n, mode)
int f, n;
CONSTA char *mode;
{
register LINE *linep;
register int s;
register int loffs, len;
register int nline;
REGION region;
char *fn;
UNUSED_ARGS_FN;
if (restflag) /* don't allow this command if restricted */
return(resterr());
if ((s=getregion(®ion)) != TRUE)
return (s);
fn = gtfilename(((*mode=='w')? "Write file": "Append file"), TRUE);
if (fn == NULL)
return (FALSE);
#if CRYPT
s = resetkey();
if (s != TRUE)
return(s);
#endif
/* turn off ALL keyboard translation in case we get a dos error */
(VOID) (*term.t_kclose)();
if ((s=ffwopen(fn, mode)) != FIOSUC) { /* Open writes message. */
(VOID) (*term.t_kopen)();
return (FALSE);
}
mlwrite("[Writing...]"); /* tell us were writing */
nline = 0; /* Number of lines. */
linep = region.r_linep;
loffs = region.r_offset;
while (region.r_size > 0L) {
len = llength(linep) - loffs;
if (((long)len) > region.r_size) len = (int) region.r_size;
region.r_size -= (len + 1);
if ((s=ffputline(&linep->l_text[loffs], len)) != FIOSUC)
break;
++nline;
linep = lforw(linep);
loffs = 0;
}
if (s == FIOSUC) { /* No write error. */
s = ffwclose();
if (s == FIOSUC) { /* No close error. */
if (nline == 1)
mlwrite("[Wrote 1 line]");
else
mlwrite("[Wrote %d lines]", nline);
}
} else /* Ignore close error */
ffwclose(); /* if a write error. */
(VOID) (*term.t_kopen)();
if (s != FIOSUC) /* Some sort of error. */
return (FALSE);
return (TRUE);
}
int regionwrite(f, n)
int f, n;
{
return(regionio(f, n, "w"));
}
int regionappend(f, n)
int f, n;
{
return(regionio(f, n, "a"));
}
#endif