forked from mbert/elvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
3220 lines (2873 loc) · 82.8 KB
/
buffer.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
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
/* buffer.c */
/* Copyright 1995 by Steve Kirkendall */
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_buffer[] = "$Id: buffer.c,v 2.160 2004/03/23 18:23:16 steve Exp $";
#endif
#define swaplong(x,y) {long tmp; tmp = (x); (x) = (y); (y) = tmp;}
#if USE_PROTOTYPES
static void freeundo(BUFFER buffer);
static struct undo_s *allocundo(BUFFER buf);
static void bufdo(BUFFER buf, ELVBOOL wipe);
static void didmodify(BUFFER buf);
# ifdef FEATURE_MISC
static void proc(_BLKNO_ bufinfo, long nchars, long nlines, long changes,
long prevloc, CHAR *name);
# endif
# ifdef FEATURE_PERSIST
static CHAR *persistget(void);
static void persistload(BUFFER buf);
static ELVBOOL persistinternal(BUFFER buf);
static void persisthist(char *field, char *prompt, char *bufname, BUFFER persbuf);
static void persistbuf(BUFFER buf, BUFFER persbuf);
static void persistother(BUFFER buf, BUFFER persbuf);
# endif
# ifdef DEBUG_ALLOC
static void checkundo(char *where);
static void removeundo(struct undo_s *undo);
# endif
#endif
/* This variable points to the head of a linked list of buffers */
BUFFER elvis_buffers;
/* This is the default buffer. Its options have been inserted into the
* list accessible via optset(). This variable should only be changed by
* the bufoptions() function.
*/
BUFFER bufdefault;
/* This stores the message type that will be used for reporting the number
* of lines read or written. It is normally MSG_STATUS so that other messages
* will be allowed to overwrite it; however, when quitting it is set to
* MSG_INFO so messages will be queued and eventually displayed somewhere
* else after the window is closed. It is also set to MSG_INFO during
* initialization so the "read..." message appears in the new window.
*/
MSGIMP bufmsgtype = MSG_INFO;
/* This buffer's contents are irrelevent. The values of its options, though,
* are significant because the values of its options are used as the default
* values of any new buffer. This buffer is also used as the default buffer
* during execution of the initialization scripts.
*/
BUFFER bufdefopts;
#ifdef FEATURE_AUTOCMD
/* This option is set while reading text into the buffer, so that it won't
* trigger an Edit autocmd.
*/
static ELVBOOL bufnoedit;
#endif
/* This array describes buffer options */
static OPTDESC bdesc[] =
{
{"filename", "file", optsstring, optisstring },
{"bufname", "buffer", optsstring, optisstring },
{"bufid", "bufferid", optnstring, optisnumber },
{"buflines", "bl", optnstring, optisnumber },
{"bufchars", "bc", optnstring, optisnumber },
{"retain", "ret", NULL, NULL },
{"modified", "mod", NULL, NULL, },
{"edited", "samename", NULL, NULL, },
{"newfile", "new", NULL, NULL, },
{"readonly", "ro", NULL, NULL, },
{"autoindent", "ai", NULL, NULL, },
{"inputtab", "it", opt1string, optisoneof, "tab spaces ex filename identifier"},
{"autotab", "at", NULL, NULL, },
{"tabstop", "ts", opttstring, optistab, "8"},
{"ccprg", "cp", optsstring, optisstring },
{"equalprg", "ep", optsstring, optisstring },
{"keywordprg", "kp", optsstring, optisstring },
{"makeprg", "mp", optsstring, optisstring },
{"paragraphs", "para", optsstring, optisstring },
{"sections", "sect", optsstring, optisstring },
{"shiftwidth", "sw", opttstring, optistab, "8"},
{"undolevels", "ul", optnstring, optisnumber },
{"textwidth", "tw", optnstring, optisnumber },
{"internal", "internal",NULL, NULL },
{"bufdisplay", "bd", optsstring, optisstring },
{"initialsyntax","isyn",NULL, NULL },
{"errlines", "errlines",optnstring, optisnumber },
{"readeol", "reol", opt1string, optisoneof, "unix dos mac text binary"},
{"locked", "lock", NULL, NULL },
{"partiallastline","pll",NULL, NULL },
{"putstyle", "ps", opt1string, optisoneof, "character line rectangle"},
{"timestamp", "time", optsstring, optisstring },
{"guidewidth", "gw", opttstring, optistab },
{"hlobject", "hlo", optsstring, optisstring },
{"spell", "sp", NULL, NULL },
{"lisp", "lisp", NULL, NULL },
{"mapmode", "mm", optsstring, optisstring },
{"smartargs", "sa", NULL, NULL },
{"userprotocol", "up", NULL, NULL },
{"bb", "bb", optsstring, optisstring }
};
#ifdef DEBUG_ALLOC
/* This are used for maintaining a linked list of all undo versions. */
struct undo_s *undohead, *undotail;
/* This function is called after code which is suspected of leaking memory.
* It checks all of the undo versions, making sure that each one is still
* accessible via some buffer.
*/
static void checkundo(where)
char *where;
{
struct undo_s *scan, *undo;
BUFFER buf;
/* for each undo version... */
for (scan = undohead; scan; scan = scan->link1)
{
/* make sure the buffer still exists */
for (buf = elvis_buffers; buf != scan->buf; buf = buf->next)
{
if (!buf)
msg(MSG_FATAL, "[s]$1 - buffer disappeared, undo/redo not freed", where);
}
/* make sure this is an undo/redo for this buffer */
if (scan->undoredo == 'l')
{
if (scan != buf->undolnptr)
{
msg(MSG_FATAL, "[s]$1 - undolnptr version leaked", where);
}
}
else
{
for (undo = scan->undoredo=='u' ? buf->undo : buf->redo;
undo != scan;
undo = undo->next)
{
if (!undo)
msg(MSG_FATAL, "[ss]$1 - $2 version leaked", where, scan->undoredo=='u'?"undo":"redo");
}
}
}
}
static void removeundo(undo)
struct undo_s *undo;
{
if (undo->link1)
undo->link1->link2 = undo->link2;
else
undotail = undo->link2;
if (undo->link2)
undo->link2->link1 = undo->link1;
else
undohead = undo->link1;
}
#else
# define checkundo(s)
#endif
#ifdef FEATURE_MISC
/* This function is called during session file initialization. It creates
* a BUFFER struct for the buffer, and collects the undo versions.
*/
static void proc(bufinfo, nchars, nlines, changes, prevloc, name)
_BLKNO_ bufinfo; /* block describing the buffer */
long nchars; /* number of characters in buffer */
long nlines; /* number of lines in buffer */
long changes; /* value of "changes" counter */
long prevloc; /* offset of most recent change to buffer */
CHAR *name; /* name of the buffer */
{
BUFFER buf;
BLKNO tmp;
struct undo_s *undo, *scan, *lag;
ELVBOOL internal;
/* try to find a buffer by this name */
for (buf = elvis_buffers; buf && CHARcmp(o_bufname(buf), name); buf = buf->next)
{
}
/* if no buffer exists yet, then create one and make it use the old
* bufinfo block.
*/
if (!buf)
{
internal = (ELVBOOL)(!CHARncmp(name, toCHAR("Elvis "), 6) &&
CHARncmp(name, toCHAR("Elvis untitled"), 14));
buf = bufalloc(name, bufinfo, internal);
buf->bufinfo = bufinfo;
o_buflines(buf) = nlines;
o_bufchars(buf) = nchars;
buf->changes = changes;
buf->changepos = prevloc;
/* guess some values for a few other critical options */
if (!CHARncmp(name, toCHAR("Elvis "), 6) &&
CHARncmp(name, toCHAR("Elvis untitled"), 14))
{
/* probably an internal buffer */
optpreset(o_internal(buf), ElvTrue, OPT_HIDE|OPT_NODFLT);
optpreset(o_modified(buf), ElvFalse, OPT_HIDE|OPT_NODFLT);
optpreset(o_filename(buf), NULL, OPT_HIDE|OPT_LOCK);
}
else
{
/* the filename is probably the same as the buffer name */
optpreset(o_filename(buf), CHARdup(name), OPT_FREE|OPT_HIDE);
optpreset(o_internal(buf), ElvFalse, OPT_HIDE);
/* Mark it as readonly so the user will have to think
* before clobbering an existing file.
*/
optpreset(o_readonly(buf), ElvTrue, OPT_HIDE|OPT_NODFLT);
/* Mark it as modified, so the user has to think
* before exitting and losing this session file.
*/
optpreset(o_modified(buf), ElvTrue, OPT_HIDE|OPT_NODFLT);
}
return;
}
/* We found the buffer. Is this the newest version found so far? */
if (changes > buf->changes)
{
/* yes, this is the newest. Swap this one with the version
* currently in the buf struct. That will leave this version
* (the newest) as the current version, and the current
* (second newest) in the arguements and ready to be added
* to the undo list.
*/
tmp = buf->bufinfo;
buf->bufinfo = bufinfo;
bufinfo = tmp;
swaplong(o_buflines(buf), nlines);
swaplong(o_bufchars(buf), nchars);
swaplong(buf->changes, changes);
swaplong(buf->changepos, prevloc);
}
/* insert as an "undo" version */
undo = (struct undo_s *)safealloc(1, sizeof *undo);
undo->changes = changes;
undo->changepos = prevloc;
undo->buflines = nlines;
undo->bufchars = nchars;
undo->bufinfo = bufinfo;
for (scan = buf->undo, lag = NULL;
scan && scan->changes > changes;
lag = scan, scan = scan->next)
{
}
undo->next = scan;
if (lag)
{
lag->next = undo;
}
else
{
buf->undo = undo;
}
#ifdef DEBUG_ALLOC
undo->link1 = undohead;
undohead = undo;
undo->link2 = NULL;
if (undo->link1)
undo->link1->link2 = undo;
else
undotail = undo;
undo->buf = buf;
undo->undoredo = 'u';
#endif
}
#endif /* FEATURE_MISC */
/* Restart a session */
void bufinit()
{
assert(BUFOPTQTY == QTY(bdesc));
/* find any buffers left over from a previous edit */
#ifndef FEATURE_MISC
lowinit(NULL);
#else
lowinit(proc);
#endif
/* create the default options buffer, if it doesn't exist already */
bufdefopts = bufalloc(toCHAR(DEFAULT_BUF), 0, ElvTrue);
bufoptions(bufdefopts);
}
#ifdef FEATURE_PERSIST
/* Return ElvTrue if a buffer is internal (not persistent) */
static ELVBOOL persistinternal(buf)
BUFFER buf; /* a buffer to be saved, or NULL for all */
{
/* all buffers can't be internal */
if (!buf)
return ElvFalse;
if (o_internal(buf)
|| !CHARncmp(o_bufname(buf), toCHAR("Elvis untitled"), 14)
|| !o_filename(buf))
return ElvTrue;
return ElvFalse;
}
/* Return the next line, or NULL if there is no next line. The \n is stripped
* off, but there's guaranteed to be room in the buffer for it, so you can
* CHARcat(line, toCHAR("\n")) to put it back. This function assumes you've
* already called ioopen() to start reading the file.
*/
static CHAR *persistget()
{
static CHAR line[300];
CHAR *val;
/* fetch the next line */
*line = '\0';
for (val = line;
val < &line[QTY(line) - 1]
&& ioread(val, 1) == 1
&& *val != '\n';
val++)
{
}
/* if nothing read, then return NULL */
if (*line == '\0')
return NULL;
/* return the line */
*val = '\0';
return line;
}
/* Load global persistent information */
void bufpersistinit()
{
ELVBOOL doex, dosearch, doargs;
BUFFER exbuf, searchbuf;
CHAR *line;
int i, nargs;
char **newargs;
int gotnext;
ELVBOOL oldhide;
/* do nothing if persistfile is unset */
if (!o_persistfile)
return;
/* figure out what we're supposed to load */
doex = (ELVBOOL)(calcelement(o_persist,toCHAR("ex")) != NULL);
dosearch = (ELVBOOL)(calcelement(o_persist,toCHAR("search")) != NULL);
doargs = (ELVBOOL)(calcelement(o_persist,toCHAR("args")) != NULL);
if (arglist && *arglist)
doargs = ElvFalse; /* already have args */
/* if not supposed to do any globals, then don't */
if (!doex && !dosearch && !doargs)
return;
/* try to open the file */
oldhide = msghide(ElvTrue);
if (!ioopen(iofilename(tochar8(o_persistfile), '\0'), 'r', ElvFalse, ElvFalse, 't'))
{
(void)msghide(oldhide);
return;
}
(void)msghide(oldhide);
/* locate the history buffers */
exbuf = bufalloc(toCHAR(EX_BUF), 0, ElvTrue);
searchbuf = bufalloc(toCHAR(REGEXP_BUF), 0, ElvTrue);
/* for each line up to the first bufname line... */
nargs = 0;
gotnext = -1;
while ((line = persistget()) != NULL && CHARncmp(line, "bufname ", 8))
{
/* skip blank lines. */
if (!*line)
continue;
/* handle it */
if (*line == ':')
{
/* skip if not doing ex history */
if (!doex)
continue;
/* append to ex history */
CHARcat(line, toCHAR("\n"));
bufappend(exbuf, line, 0);
}
else if (*line == '/' || *line == '?')
{
/* skip of not doing search history */
if (!dosearch)
continue;
/* append to search history */
CHARcat(line, toCHAR("\n"));
bufappend(searchbuf, line, 0);
}
else if (!CHARncmp(line, "arg ", 4))
{
/* skip if not doing args */
if (!doargs)
continue;
/* append to the args list */
newargs = safealloc(nargs + 2, sizeof(char *));
for (i = 0; i < nargs; i++)
newargs[i] = arglist[i];
newargs[nargs++] = safedup(tochar8(line + 4));
newargs[nargs] = NULL;
if (arglist)
safefree(arglist);
arglist = newargs;
}
else if (!CHARncmp(line, "argnext ", 8))
{
/* skip if not doing args */
if (!doargs)
continue;
/* remember the "argnext" value for later */
gotnext = (int)CHAR2long(line + 8);
}
}
/* maybe restore argnext */
if (doargs && gotnext > 0 && gotnext <= nargs)
argnext = gotnext - 1; /* so we start on the one before next */
/* close the file */
(void)ioclose();
}
/* Load the peristent information about a given buffer */
static void persistload(buf)
BUFFER buf;
{
CHAR *line;
char *line8;
long cursor, change; /* found cursor & change */
int hours; /* age of found timestamp for entry */
int gotYYYY, gotMM, gotDD, gothh, gotmm; /* parsed timestamp */
int nowYYYY, nowMM, nowDD, nowhh, nowmm; /* parsed current time */
ELVBOOL domarks; /* supposed to load marks? */
ELVBOOL skip;
char markname[2];
CHAR *phours;
long value;
int i;
ELVBOOL oldhide;
long oldbuflines, oldbufchars;
CHAR *external;
#ifdef FEATURE_REGION
ELVBOOL doregions; /* supposed to load regions? */
int regions; /* found number of regions */
_char_ face;
char facename[50];
#endif
#ifdef FEATURE_FOLD
ELVBOOL dofolds; /* supposed to load folds? */
int folds; /* found number of folds */
FOLD newfold;
#endif
#if defined(FEATURE_REGION) || defined(FEATURE_FOLD)
long top, bottom;
MARKBUF marktop, markbottom;
char comment[300];
#endif
/* if the persistfile option is unset, then do nothing */
if (!o_persistfile)
return;
/* do nothing for internal buffers, buffers without a filename, or
* untitled buffers.
*/
if (persistinternal(buf))
return;
/* Try to open the file */
oldhide = msghide(ElvTrue);
if (!ioopen(iofilename(tochar8(o_persistfile), '\0'), 'r', ElvFalse, ElvFalse, 't'))
{
(void)msghide(oldhide);
return;
}
(void)msghide(oldhide);
/* detect whether we're supposed to load some specific things */
domarks = (ELVBOOL)(calcelement(o_persist,toCHAR("marks")) != NULL);
#ifdef FEATURE_REGION
doregions = (ELVBOOL)(calcelement(o_persist,toCHAR("regions")) != NULL);
#endif
#ifdef FEATURE_FOLD
dofolds = (ELVBOOL)(calcelement(o_persist,toCHAR("folds")) != NULL);
#endif
/* for now, assume buflines and bufchars won't change */
oldbuflines = o_buflines(buf);
oldbufchars = o_bufchars(buf);
external = calcelement(o_persist, toCHAR("external"));
if (external && *external == ':')
external++;
else
external = toCHAR("b");
/* For each line... */
cursor = change = -1L;
hours = 0;
#ifdef FEATURE_REGION
regions = 0;
#endif
#ifdef FEATURE_FOLD
folds = 0;
#endif
skip = ElvTrue;
while ((line = persistget()) != NULL)
{
/* is it a bufname line? */
if (!CHARncmp(line, toCHAR("bufname "), 8))
{
/* is this the end of this buffer's section? */
if (!skip)
break;
/* is it the start of this buffer's section */
skip = (ELVBOOL)(CHARcmp(line+8, o_bufname(buf)) != 0);
}
/* skip if not for this buffer */
if (skip)
continue;
/* is it the entry's timestamp? */
line8 = tochar8(line);
if (sscanf(line8, "hours %4d%2d%2dT%2d:%2d",
&gotYYYY, &gotMM, &gotDD, &gothh, &gotmm) == 5)
{
/* get the current timestamp */
sscanf(dirtime(NULL), "%4d%2d%2dT%2d:%2d",
&nowYYYY, &nowMM, &nowDD, &nowhh, &nowmm);
/* compute the hours between them (sloppily) */
if (nowYYYY > gotYYYY)
nowMM += 12;
if (nowMM > gotMM)
nowDD += 28;/* best to err on the low side */
hours = ((nowmm - gotmm) + 60 * (nowhh - gothh) + 1440 * (nowDD - gotDD)) / 60;
continue;
}
/* is it the number of expected lines or characters? */
if (sscanf(line8, "bufchars %ld", &value) == 1)
{
oldbufchars = value;
continue;
}
if (sscanf(line8, "buflines %ld", &value) == 1)
{
oldbuflines = value;
continue;
}
/* is it the cursor? */
if (sscanf(line8, "cursor %ld", &value) == 1)
{
/* adjust for external changes */
switch (*external)
{
case 't':
/* adjust relative to bottom */
value += o_bufchars(buf) - oldbufchars;
break;
case 's':
/* skip if different */
if (oldbufchars != o_bufchars(buf))
continue;
break;
}
/* store the value as the buffer's "docursor" */
if (value >= 0 && value < o_bufchars(buf))
cursor = value;
continue;
}
/* is it the change position? */
if (sscanf(line8, "change %ld", &value) == 1)
{
/* adjust for external changes */
switch (*external)
{
case 't':
/* adjust relative to bottom */
value += o_bufchars(buf) - oldbufchars;
break;
case 's':
/* skip if different */
if (oldbufchars != o_bufchars(buf))
continue;
break;
}
/* store the value as the buffer's "last change" */
if (value >= 0 && value < o_bufchars(buf))
change = value;
continue;
}
/* is it a specific mark? and do we care? */
if (sscanf(line8, "mark %1[a-z] %ld", markname, &value) == 2)
{
/* if not doing marks, then skip it */
if (!domarks)
continue;
/* adjust for external changes */
switch (*external)
{
case 't':
/* adjust relative to bottom */
value += o_bufchars(buf) - oldbufchars;
break;
case 's':
/* skip if different */
if (oldbufchars != o_bufchars(buf))
continue;
break;
}
/* if invalid position, then skip it */
if (value < 0 || value >= o_bufchars(buf))
continue;
/* restore the mark */
i = markname[0] - 'a';
if (namedmark[i])
{
marksetbuffer(namedmark[i], buf);
marksetoffset(namedmark[i], value);
}
else
{
namedmark[i] = markalloc(buf, value);
}
continue;
}
#ifdef FEATURE_REGION
/* is it a region? */
if (sscanf(line8, "region %ld,%ld %s %[^\n]", &top, &bottom, facename, comment) == 4)
{
/* skip if not doing regions */
if (!doregions)
continue;
/* adjust for external changes */
switch (*external)
{
case 't':
/* adjust relative to bottom */
top += o_buflines(buf) - oldbuflines;
bottom += o_buflines(buf) - oldbuflines;
break;
case 's':
/* skip if different */
if (oldbuflines != o_buflines(buf))
continue;
break;
}
/* skip if invalid */
if (top < 1 || top > bottom || bottom > o_buflines(buf))
continue;
/* rebuild the region */
face = colorfind(toCHAR(facename));
if (!face)
break;
regionadd(marksetline(marktmp(marktop, buf, 0), top),
marksetline(marktmp(markbottom, buf, 0), bottom+1),
face, toCHAR(comment));
}
#endif /* FEATURE_REGION */
#ifdef FEATURE_FOLD
/* is it a fold or unfold? */
if (sscanf(line8, " fold %ld,%ld %[^\n]",
&top, &bottom, comment) == 3
|| sscanf(line8, " unfold %ld,%ld %[^\n]",
&top, &bottom, comment) == 3)
{
/* skip if we aren't doing folds */
if (!dofolds)
continue;
/* adjust for external changes */
switch (*external)
{
case 't':
/* adjust relative to bottom */
top += o_buflines(buf) - oldbuflines;
bottom += o_buflines(buf) - oldbuflines;
break;
case 's':
/* skip if different */
if (oldbuflines != o_buflines(buf))
continue;
break;
}
/* skip if invalid */
if (top < 1 || top > bottom || bottom > o_buflines(buf))
continue;
/* create the new fold */
(void)marksetline(marktmp(marktop, buf, 0), top);
(void)marksetline(marktmp(markbottom, buf, 0),bottom+1);
markaddoffset(&markbottom, -1L);
newfold = foldalloc(&marktop, &markbottom, toCHAR(comment));
/* there shouldn't be any overlapping folds,
* but just to be safe...
*/
(void)foldbyrange(newfold->from, newfold->to,
ElvTrue, FOLD_NOEXTRA|FOLD_DESTROY);
(void)foldbyrange(newfold->from, newfold->to,
ElvFalse, FOLD_NOEXTRA|FOLD_DESTROY);
/* add it as a fold or unfold */
foldadd(newfold, (ELVBOOL)(*line8 == 'f'));
}
#endif /* FEATURE_FOLD */
}
/* close the file */
(void)ioclose();
/* get the persist.hours value */
phours = calcelement(o_persist, toCHAR("hours"));
if (phours)
phours++;
/* clobber cursor and/or change, if not supposed to load */
if (!calcelement(o_persist, toCHAR("cursor")))
cursor = -1L;
if (!calcelement(o_persist, toCHAR("change")))
change = -1L;
/* if it hasn't expired then restore cursor */
if (!phours || hours < atoi(tochar8(phours)))
{
if (cursor >= 0)
{
buf->docursor = cursor;
if (change >= 0)
buf->changepos = change;
}
else if (change >= 0)
buf->docursor = change;
}
else /* maybe still restore '' mark */
{
if (cursor >= 0)
buf->changepos = cursor;
else if (change >= 0)
buf->changepos = change;
}
}
/* Append the end of a history buffer to persbuf */
static void persisthist(field, prompt, bufname, persbuf)
char *field; /* name of field in 'persist' option */
char *prompt; /* acceptable prompt characters: ":" or "/?" */
char *bufname; /* name of buffer containing history */
BUFFER persbuf; /* temp buffer used to construct persistfile */
{
long lines; /* number of lines to keep */
CHAR *scan;
BUFFER histbuf;
MARK mark;
MARKBUF histeol, persend;
/* locate the history buffer. If not found, there's nothing to save */
histbuf = buffind(toCHAR(bufname));
if (!histbuf || o_bufchars(histbuf) == 0L)
return;
/* get the number of lines to keep. If 0, then save nothing */
scan = calcelement(o_persist, toCHAR(field));
if (!scan || *scan++ != ':')
return;
for (lines = 0; elvdigit(*scan); scan++)
lines = lines * 10 + *scan - '0';
if (lines == 0)
return;
/* locate the first line to copy */
mark = markalloc(histbuf, 0L);
if (lines < o_buflines(histbuf))
marksetline(mark, o_buflines(histbuf) - lines + 1);
/* for each line in history ... */
scanalloc(&scan, mark);
for(;;)
{
/* find the end of the line */
while (scan && *scan != '\n')
scannext(&scan);
if (!scan)
break;
scannext(&scan);
/* if line starts with prompt char, then copy it */
if (CHARchr(toCHAR(prompt), scanchar(mark)) != NULL)
{
/* need to stop scanning while changing */
if (scan)
histeol = *scanmark(&scan);
else
{
histeol.buffer = histbuf;
histeol.offset = o_bufchars(histbuf);
}
/* copy the history to the end of the persist buffer */
bufpaste(marktmp(persend, persbuf, o_bufchars(persbuf)),
mark, &histeol);
/* resume scanning. we changed persbuf, not histbuf, * so the offset of previous scan should be good
*/
if (scan)
scanalloc(&scan, &histeol);
}
/* move the mark to the start of the next line */
if (!scan)
break;
marksetoffset(mark, histeol.offset);
}
markfree(mark);
}
/* Append information describing one buffer */
static void persistbuf(buf, persbuf)
BUFFER buf; /* the buffer to be stored */
BUFFER persbuf;/* temp buffer used to construct new persistfile */
{
char line[300];
int i;
#ifdef FEATURE_REGION
struct region_s *region;
#endif
#ifdef FEATURE_FOLD
FOLD fold;
#endif
/* always start with a "bufname" line, and timestamp */
sprintf(line, "bufname %.289s\nhours %s\n",
o_bufname(buf), dirtime(NULL));
bufappend(persbuf, toCHAR(line), 0);
/* always store bufchars, buflines, cursor, and change */
sprintf(line, "bufchars %ld\nbuflines %ld\ncursor %ld\nchange %ld\n",
o_bufchars(buf), o_buflines(buf), buf->docursor,buf->changepos);
bufappend(persbuf, toCHAR(line), 0);
/* maybe store the named marks */
if (calcelement(o_persist, toCHAR("marks")))
{
/* for each mark... */
for (i = 0; i < QTY(namedmark); i++)
{
/* skip if unset, or in a different buffer */
if (!namedmark[i] || markbuffer(namedmark[i]) != buf)
continue;
/* save it */
sprintf(line, "mark %c %ld\n",
'a'+i, markoffset(namedmark[i]));
bufappend(persbuf, toCHAR(line), 0);
}
}
#ifdef FEATURE_REGION
/* maybe save regions */
if (calcelement(o_persist, toCHAR("regions")))
{
/* for each region in this buffer... */
for (region = buf->regions; region; region = region->next)
{
sprintf(line, "region %ld,%ld %s %s\n",
markline(region->from), markline(region->to)-1,
tochar8(colorinfo[(int)region->font].name),
tochar8(region->comment));
bufappend(persbuf, toCHAR(line), 0);
}
}
#endif
#ifdef FEATURE_FOLD
/* maybe save folds */
if (calcelement(o_persist, toCHAR("folds")))
{
/* save each fold... */
for (fold = buf->fold; fold; fold = fold->next)
{
sprintf(line, "fold %ld,%ld %s\n",
markline(fold->from), markline(fold->to),
tochar8(fold->name));
bufappend(persbuf, toCHAR(line), 0);
}
/* save each unfold... */
for (fold = buf->unfold; fold; fold = fold->next)
{
sprintf(line, "unfold %ld,%ld %s\n",
markline(fold->from), markline(fold->to),
tochar8(fold->name));
bufappend(persbuf, toCHAR(line), 0);
}
}
#endif
}
/* Append file-specific information from the old persistent file onto the
* end of the new persistent buffer. Skip information for buffers that we've
* just explicitly updated via persistbuf().
*/
static void persistother(buf, persbuf)
BUFFER buf; /* buffer to skip, or NULL to skip all current bufs */
BUFFER persbuf;/* where to append the file's contents */
{
CHAR *line;
ELVBOOL skip;
long max;
/* try to open the file */
if (!ioopen(iofilename(tochar8(o_persistfile), '\0'), 'r', ElvFalse, ElvFalse, 't'))
{
return;
}
/* initially we'll skip. We don't want to copy the global info */
skip = ElvTrue;
/* fetch the limit, if any */
max = -1;
line = calcelement(o_persist, toCHAR("max"));
if (line && *line++ == ':')
{
for (max = 0; elvdigit(*line); line++)
max = max * 10 + *line - '0';
if (*line == 'k' || *line == 'K')
max <<= 10;
else if (*line == 'm' || *line == 'M')
max <<= 20;
}
/* for each line of the original persist file... */
while ((line = persistget()) != NULL)
{
/* is it "bufname" line? */
if (!CHARncmp(line, toCHAR("bufname "), 8))
{
/* if the buffer has reached its limit, then break */
if (max >= 0 && o_bufchars(persbuf) >= max)
break;