-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
2326 lines (2084 loc) · 61.7 KB
/
buffer.c
File metadata and controls
2326 lines (2084 loc) · 61.7 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
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.150 2003/10/17 17:41:23 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 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);
}
/* Create a buffer with a given name. The buffer will initially be empty;
* if it is meant to be associated with a particular file, then the file must
* be copied into the buffer in a separate operation. If there is already
* a buffer with the desired name, it returns a pointer to the old buffer
* instead of creating a new one.
*/
BUFFER bufalloc(name, bufinfo, internal)
CHAR *name; /* name of the buffer */
_BLKNO_ bufinfo;/* block number describing the buffer (0 to create) */
ELVBOOL internal;/* is this supposed to be an internal buffer? */
{
BUFFER buffer;
BUFFER scan, lag; /* used for inserting new buffer */
char unique[255]; /* name of untitled buffer */
static long bufid = 1; /* for generating bufid values */
static long intbufid = -1; /* used for generating internal names */
/* if no name was specified, generate a unique untitled name */
if (!name)
{
sprintf(unique, UNTITLED_BUF, (int)(internal ? intbufid : bufid));
name = toCHAR(unique);
if (internal)
intbufid--;
/* Note that bufid will be incremented below, when initializing
* the o_bufid(buf) option.
*/
}
/* see if there's already a buffer with that name */
buffer = buffind(name);
if (buffer)
{
return buffer;
}
/* allocate buffer struct */
buffer = (BUFFER)safekept(1, sizeof(*buffer));
/* create a low-level buffer */
if (bufinfo)
{
buffer->bufinfo = bufinfo;
}
else
{
buffer->bufinfo = lowalloc(tochar8(name));
}
/* initialize the buffer options */
optpreset(o_readonly(buffer), o_defaultreadonly, OPT_HIDE|OPT_NODFLT);
if (bufdefopts)
{
/* copy all options except the following: filename bufname bufid
* buflines bufchars modified edited newfile internal autotab
* partiallastline putstyle timestamp readeol.
*/
optpreset(buffer->retain, bufdefopts->retain, OPT_HIDE);
buffer->autoindent = bufdefopts->autoindent;
buffer->inputtab = bufdefopts->inputtab;
optpreset(o_tabstop(buffer),
(short *)safealloc(2 + o_tabstop(bufdefopts)[0],
sizeof(short)), OPT_FREE|OPT_REDRAW);
memcpy(o_tabstop(buffer), o_tabstop(bufdefopts),
(2 + o_tabstop(bufdefopts)[0]) * sizeof(short));
optpreset(o_shiftwidth(buffer),
(short *)safealloc(2 + o_shiftwidth(bufdefopts)[0],
sizeof(short)), OPT_FREE);
memcpy(o_shiftwidth(buffer), o_shiftwidth(bufdefopts),
(2 + o_shiftwidth(bufdefopts)[0]) * sizeof(short));
buffer->undolevels = bufdefopts->undolevels;
buffer->initialsyntax = bufdefopts->initialsyntax;
buffer->textwidth = bufdefopts->textwidth;
buffer->autotab = bufdefopts->autotab;
buffer->locked = bufdefopts->locked;
buffer->guidewidth = bufdefopts->guidewidth;
if (o_guidewidth(buffer))
{
optpreset(o_guidewidth(buffer),
(short *)safealloc(2 + o_guidewidth(bufdefopts)[0],
sizeof(short)), OPT_FREE|OPT_HIDE);
memcpy(o_guidewidth(buffer),
o_guidewidth(bufdefopts),
(2 + o_guidewidth(bufdefopts)[0]) * sizeof(short));
}
buffer->spell = bufdefopts->spell;
buffer->lisp = bufdefopts->lisp;
buffer->smartargs = bufdefopts->smartargs;
buffer->userprotocol = bufdefopts->userprotocol;
/* Strings are tricky, because we may need to allocate a
* duplicate of the value.
*/
buffer->cc = bufdefopts->cc;
if (buffer->cc.flags & OPT_FREE)
o_cc(buffer) = CHARdup(o_cc(bufdefopts));
buffer->cc.flags |= OPT_HIDE;
buffer->equalprg = bufdefopts->equalprg;
if (buffer->equalprg.flags & OPT_FREE)
o_equalprg(buffer) = CHARdup(o_equalprg(bufdefopts));
buffer->keywordprg = bufdefopts->keywordprg;
if (buffer->keywordprg.flags & OPT_FREE)
o_keywordprg(buffer) = CHARdup(o_keywordprg(bufdefopts));
buffer->make = bufdefopts->make;
if (buffer->make.flags & OPT_FREE)
o_make(buffer) = CHARdup(o_make(bufdefopts));
buffer->make.flags |= OPT_HIDE;
buffer->paragraphs = bufdefopts->paragraphs;
if (buffer->paragraphs.flags & OPT_FREE)
o_paragraphs(buffer) = CHARdup(o_paragraphs(bufdefopts));
buffer->sections = bufdefopts->sections;
if (buffer->sections.flags & OPT_FREE)
o_sections(buffer) = CHARdup(o_sections(bufdefopts));
buffer->bufdisplay = bufdefopts->bufdisplay;
if (buffer->bufdisplay.flags & OPT_FREE)
o_bufdisplay(buffer) = CHARdup(o_bufdisplay(bufdefopts));
buffer->hlobject = bufdefopts->hlobject;
if (buffer->hlobject.flags & OPT_FREE)
o_hlobject(buffer) = CHARdup(o_hlobject(bufdefopts));
buffer->mapmode = bufdefopts->mapmode;
if (buffer->mapmode.flags & OPT_FREE)
o_mapmode(buffer) = CHARdup(o_mapmode(bufdefopts));
}
else /* no default options -- set them explicitly */
{
o_inputtab(buffer) = 't';
o_autotab(buffer) = ElvTrue;
o_tabstop(buffer) = safealloc(2, sizeof(short));
o_tabstop(buffer)[0] = 0;
o_tabstop(buffer)[1] = 8;
optflags(o_tabstop(buffer)) = OPT_FREE|OPT_REDRAW;
#ifndef OSCCPRG
# define OSCCPRG "cc ($1?$1:$2)"
#endif
optpreset(o_cc(buffer), toCHAR(OSCCPRG), OPT_UNSAFE);
optpreset(o_equalprg(buffer), toCHAR("fmt"), OPT_UNSAFE);
optpreset(o_keywordprg(buffer), toCHAR("ref $1 file:$2"), OPT_UNSAFE);
#ifndef OSMAKEPRG
# define OSMAKEPRG "make $1"
#endif
optpreset(o_make(buffer), toCHAR(OSMAKEPRG), OPT_UNSAFE);
o_paragraphs(buffer) = toCHAR("PPppIPLPQPP");
o_sections(buffer) = toCHAR("NHSHSSSEse");
o_shiftwidth(buffer) = safealloc(2, sizeof(short));
o_shiftwidth(buffer)[0] = 0;
o_shiftwidth(buffer)[1] = 8;
optflags(o_shiftwidth(buffer)) = OPT_FREE;
o_undolevels(buffer) = 0;
optpreset(o_bufdisplay(buffer), toCHAR("normal"), OPT_NODFLT);
optpreset(o_initialsyntax(buffer), ElvFalse, OPT_HIDE|OPT_NODFLT);
optflags(o_textwidth(buffer)) = OPT_REDRAW;
optpreset(o_locked(buffer), ElvFalse, OPT_HIDE|OPT_NODFLT);
optflags(o_guidewidth(buffer)) = OPT_HIDE|OPT_SCRATCH;
optflags(o_hlobject(buffer)) = OPT_HIDE|OPT_REDRAW;
optpreset(o_spell(buffer), ElvFalse, OPT_HIDE);
optpreset(o_smartargs(buffer), ElvFalse, OPT_HIDE);
optpreset(o_userprotocol(buffer), ElvFalse, OPT_HIDE|OPT_NODFLT);
optflags(o_mapmode(buffer)) = OPT_HIDE|OPT_REDRAW;
}
/* set the name of this buffer, and limit access to some options */
optpreset(o_bufname(buffer), CHARkdup(name), OPT_HIDE|OPT_LOCK|OPT_FREE);
optpreset(o_internal(buffer), internal, OPT_HIDE|OPT_LOCK);
optflags(o_buflines(buffer)) = OPT_HIDE|OPT_LOCK;
optflags(o_bufchars(buffer)) = OPT_HIDE|OPT_LOCK;
optflags(o_bufid(buffer)) = OPT_HIDE|OPT_LOCK;
optflags(o_filename(buffer)) |= OPT_HIDE;
optflags(o_edited(buffer)) |= OPT_HIDE|OPT_NODFLT;
optflags(o_errlines(buffer)) |= OPT_HIDE|OPT_NODFLT;
optflags(o_modified(buffer)) |= OPT_HIDE|OPT_NODFLT;
optflags(o_newfile(buffer)) |= OPT_HIDE|OPT_NODFLT;
if (!internal)
{
o_bufid(buffer) = bufid++;
}
optpreset(o_partiallastline(buffer), ElvFalse, OPT_HIDE|OPT_NODFLT);
optpreset(o_putstyle(buffer), 'c', OPT_HIDE|OPT_LOCK);
optpreset(o_timestamp(buffer), NULL, OPT_HIDE|OPT_NODFLT);
optpreset(o_readeol(buffer), o_binary ? 'b' : 't', OPT_HIDE|OPT_NODFLT);
optpreset(o_bb(buffer), NULL, OPT_HIDE|OPT_NODFLT);
/* initialize the "willevent" field to a safe value */
buffer->willevent = -1;
/* Add the buffer to the linked list of buffers. Keep it sorted. */
for (lag = (BUFFER)0, scan = elvis_buffers;
scan && CHARcmp(o_bufname(scan), o_bufname(buffer)) < 0;
lag = scan, scan = scan->next)
{
}
buffer->next = scan;
if (lag)
{
lag->next = buffer;
}
else
{
elvis_buffers = buffer;
}
#ifdef FEATURE_AUTOCMD
if (!o_internal(buffer))
{
BUFFER oldbuf = bufdefault;
bufoptions(buffer);
auperform(windefault, ElvFalse, NULL, AU_BUFCREATE, NULL);
bufoptions(oldbuf);
}
#endif /* FEATURE_AUTOCMD */
/* return the new buffer */
return buffer;
}
/* Locate a buffer with a particular name. (Note that this uses the buffer
* name, not the filename.) Returns a pointer to the buffer, or NULL for
* unknown buffers.
*/
BUFFER buffind(name)
CHAR *name; /* name of the buffer to find */
{
BUFFER buffer;
long bufid;
CHAR *b, *n;
/* scan through buffers, looking for a match */
for (buffer = elvis_buffers; buffer && CHARcmp(name, o_bufname(buffer)); buffer = buffer->next)
{
}
/* If no exact match, and the name is a quote and some other char, then
* try searching for a cut buffer.
*/
if (!buffer && name[0] == '"' && name[1] && !name[2])
{
buffer = cutbuffer(name[1], ElvFalse);
}
/* If no exact match, and name is the initials of a buffer, then
* use that.
*/
if (!buffer && name[0] == '"')
{
for (buffer = elvis_buffers; buffer; buffer = buffer->next)
{
/* if first char doesn't match, skip it */
if (*o_bufname(buffer) != name[1])
continue;
/* check other chars as initials */
for (b = o_bufname(buffer) + 1, n = name + 2; *b; b++)
{
if (b[-1] == ' ')
{
if (*b == *n)
n++;
else
break;
}
}
/* if matched, then stop looking */
if (!*n && !*b)
break;
}
}
/* If no exact match, and the name looks like a number, then try
* searching by bufid.
*/
if (!buffer && *name == '#' && calcnumber(++name))
{
for (bufid = atol(tochar8(name)), buffer = elvis_buffers;
buffer && o_bufid(buffer) != bufid;
buffer = buffer->next)
{
}
}
/* If no exact match by buffer name, then try converting the name to
* an absolute name. This should allow users to say (Makefile) when
* they really mean (/home/steve/elvis-2.2/Makefile).
*/
if (!buffer)
{
name = toCHAR(ioabsolute(tochar8(name)));
for (buffer = elvis_buffers;
buffer && CHARcmp(name, o_bufname(buffer));
buffer = buffer->next)
{
}
}
return buffer;
}
/* Look up the filename of a buffer, given a reference to a scan variable.
* The scan variable should be in an existing scan context, and should point
* to a '#' character. If the filename is found, the scan variable is left
* on the last character of the # expression, and the filename is returned.
* Otherwise it returns NULL and the scan variable is undefined.
*
* This is used for expanding # in filenames.
*/
CHAR *buffilenumber(refp)
CHAR **refp;
{
long id;
BUFFER buf;
MARKBUF tmp;
assert(*refp && **refp == '#');
/* remember which buf we're scanning, so we can fix *refp after NULL */
buf = markbuffer(scanmark(refp));
if (buf)
{
/* the scanning context is in a buffer */
/* get the buffer number */
for (id = 0; scannext(refp) && elvdigit(**refp); )
{
id = id * 10 + **refp - '0';
}
/* move back one character, so *refp points to final char. This
* can be tricky if we bumped into the end of the buffer.
*/
if (*refp)
(void)scanprev(refp);
else
(void)scanseek(refp, marktmp(tmp, buf, o_bufchars(buf) - 1));
}
else
{
/* The scanning context is in a string. */
/* get the buffer number */
for (id = 0; elvdigit(*++*refp); )
{
id = id * 10 + **refp - '0';
}
--*refp;
}
/* if 0, then use alternate file */
if (id == 0)
return o_previousfile;
/* try to find a buffer with that value */
for (buf = elvis_buffers; buf && o_bufid(buf) != id; buf = buf->next)
{
}
return buf ? o_filename(buf) : NULL;
}
/* Read a text file or filter output into a specific place in the buffer */
ELVBOOL bufread(mark, rfile)
MARK mark; /* where to insert the new next */
char *rfile; /* file to read, or !cmd for filter */
{
BUFFER buf; /* the buffer we're reading into */
long offset; /* offset of mark before inserting text */
long origlines; /* original number of lines in file */
CHAR chunk[4096]; /* I/O buffer */
int nread; /* number of bytes in chunk[] */
ELVBOOL newbuf; /* is this a new buffer? */
#ifdef FEATURE_AUTOCMD
ELVBOOL filter;
ELVBOOL doevents;
BUFFER oldbuf = bufdefault;
MARKBUF from;
#endif
#ifdef FEATURE_PROTO
switch (urlalias(mark, NULL, rfile))
{
case RESULT_COMPLETE: return ElvTrue;
case RESULT_ERROR: return ElvFalse;
case RESULT_MORE: ; /* fall through to normal code */
}
#endif /* FEATURE PROTO */
/* initialize some vars */
buf = markbuffer(mark);
newbuf = (ELVBOOL)(o_bufchars(buf) == 0 && rfile[0] != '!' && (o_verbose || !o_internal(buf)));
origlines = o_buflines(buf);
#ifdef FEATURE_AUTOCMD
filter = (ELVBOOL)(*rfile == '!');
doevents = (ELVBOOL)(o_newfile(buf) || !o_filename(buf) || o_bufchars(buf) > 0);
from = *mark;
if (doevents)
{
bufoptions(markbuffer(mark));
if (auperform(windefault, ElvFalse, NULL,
filter ? AU_FILTERREADPRE : AU_FILEREADPRE,
toCHAR(rfile)))
return ElvFalse;
}
#endif
/* open the file/filter */
if (!ioopen(rfile, 'r', ElvFalse, ElvFalse, o_readeol(markbuffer(mark))))
{
/* failed -- error message already given */
return ElvFalse;
}
/* read the text */
if (newbuf)
msg(MSG_STATUS, "[s]reading $1", rfile);
while ((nread = ioread(chunk, QTY(chunk))) > 0)
{
if (guipoll(ElvFalse))
{
ioclose();
return ElvFalse;
}
offset = markoffset(mark);
bufreplace(mark, mark, chunk, nread);
marksetoffset(mark, offset + nread);
}
ioclose();
#ifdef FEATURE_AUTOCMD
if (doevents && o_buflines(buf) > origlines)
{
autop = markdup(&from);
aubottom = markdup(mark);
markaddoffset(aubottom, -1L);
bufoptions(markbuffer(mark));
(void)auperform(windefault, ElvFalse, NULL,
filter ? AU_FILTERREADPOST : AU_FILEREADPOST,
toCHAR(rfile));
markfree(aubottom);
markfree(autop);
autop = aubottom = NULL;
bufoptions(oldbuf);
}
#endif
if (newbuf)
msg(bufmsgtype, "[sdd]read $1, $2 lines, $3 chars", rfile,
o_buflines(buf), o_bufchars(buf));
else if (!o_internal(buf)
&& o_report != 0
&& o_buflines(buf) - origlines >= o_report)
msg(bufmsgtype, "[d]read $1 lines", o_buflines(buf) - origlines);
return ElvTrue;
}
/* Create a buffer for a given file, and then load the file. Return a pointer
* to the buffer. If the file can't be read for some reason, then complain and
* leave the buffer empty, but still return the empty buffer.
*
* If the buffer already exists and contains text, then the "reload" option
* can be used to force it to discard that text and reload the buffer; when
* "reload" is ElvFalse, it would leave the buffer unchanged instead.
*/
BUFFER bufload(bufname, filename, reload)
CHAR *bufname; /* name of buffer, or NULL to derive from filename */
char *filename; /* name of file to load into a buffer */
ELVBOOL reload; /* load from file even if previously loaded? */
{
BUFFER buf;
MARKBUF top;
MARKBUF end;
MARK mark;
int i;
ELVBOOL internal;
#ifdef FEATURE_CALC
BUFFER initbuf; /* buffer containing the initialization script*/
RESULT result;
EXCTLSTATE oldctlstate;
# ifdef FEATURE_MISC
void *locals;
# endif
#endif
#ifdef FEATURE_AUTOCMD
/* Loading a file shouldn't generate Edit autocmd events */
bufnoedit = ElvTrue;
#endif
/* Create a buffer. The buffer's default name is derived from the
* given file name. Note that we make a local copy of that name
* because ioabsolute() returns a static buffer which bufalloc()
* [really buffind()] clobbers the buffer before it uses the name.
*/
internal = (ELVBOOL)(bufname != NULL);
if (!bufname)
bufname = CHARdup(toCHAR(ioabsolute(filename)));
buf = bufalloc(bufname, 0, internal);
if (!internal)
safefree(bufname);
/* Does the buffer already contain text? */
if (o_bufchars(buf) > 0)
{
/* If we aren't supposed to reload, then just return the
* buffer as-is.
*/
if (!reload)
{
#ifdef FEATURE_AUTOMD
bufnoedit = ElvFalse;
#endif
return buf;
}
/* Save the text as an "undo" version, and then delete it */
if (windefault && markbuffer(windefault->cursor) == buf)
bufwilldo(windefault->cursor, ElvTrue);
else
bufwilldo(marktmp(top, buf, 0), ElvTrue);
bufreplace(marktmp(top, buf, 0), marktmp(end, buf, o_bufchars(buf)), (CHAR *)0, 0);
}
#ifdef FEATURE_REGION
/* Initially the buffer should contain no regions */
regiondel(marktmp(top, buf, 0), &top, '\0');
#endif
/* Set the buffer's options */
optpreset(o_filename(buf), CHARkdup(filename), OPT_HIDE|OPT_LOCK|OPT_FREE);
optpreset(o_edited(buf), ElvTrue, OPT_HIDE|OPT_NODFLT);
optpreset(o_readonly(buf), o_defaultreadonly, OPT_HIDE|OPT_NODFLT);
optpreset(o_newfile(buf), ElvFalse, OPT_HIDE|OPT_NODFLT);
switch (urlperm(filename))
{
case DIR_INVALID:
case DIR_BADPATH:
case DIR_NOTFILE:
case DIR_DIRECTORY:
case DIR_UNREADABLE:
case DIR_READONLY:
o_readonly(buf) = ElvTrue;
break;
case DIR_NEW:
o_newfile(buf) = ElvTrue;
break;
case DIR_READWRITE:
/* nothing needed */
break;
}
/* Execute the "before read" script, if it exists. If the script
* fails, then don't load the newly-created buffer.
*/
if (!o_internal(buf))
{
#ifdef FEATURE_CALC /* since it isn't real useful without :if */
initbuf = buffind(toCHAR(BEFOREREAD_BUF));
if (initbuf)
{
/* make the buffer available to :set */
bufoptions(buf);
/* execute the script */
# ifdef FEATURE_MISC
locals = optlocal(NULL);
# endif
exctlsave(oldctlstate);
# ifdef FEATURE_AUTOCMD
(void)auperform(windefault, ElvFalse, NULL, AU_SCRIPTENTER,
o_filename(initbuf));
# endif
result = experform(windefault, marktmp(top, initbuf, 0),
marktmp(end, initbuf, o_bufchars(initbuf)));
# ifdef FEATURE_AUTOCMD
(void)auperform(windefault, ElvFalse, NULL, AU_SCRIPTLEAVE,
o_filename(initbuf));
# endif
exctlrestore(oldctlstate);
# ifdef FEATURE_MISC
(void)optlocal(locals);
# endif
if (result != RESULT_COMPLETE)
{
# ifdef FEATURE_AUTOCMD
bufnoedit = ElvFalse;
# endif
return buf;
}
}
#endif /* FEATURE_CALC */
#ifdef FEATURE_AUTOCMD
/* perform the pre-read script */
(void)auperform(windefault, ElvFalse, NULL,
o_newfile(buf) ? AU_BUFNEWFILE : AU_BUFREADPRE, NULL);
#endif
}
/* read the file's contents into the buffer */
if (o_newfile(buf))
{
msg(bufmsgtype, "[sdd]$1 [NEW FILE]",
filename, o_buflines(buf), o_bufchars(buf));
}
else if (!bufread(marktmp(top, buf, 0), filename))
{
o_edited(buf) = ElvFalse;
#ifdef FEATURE_AUTOCMD
bufnoedit = ElvFalse;
#endif
return buf;
}
/* if it ends with a partial last line, then add a newline */
(void)marktmp(end, buf, o_bufchars(buf) - 1);
if (o_bufchars(buf) > 0L && scanchar(&end) != '\n')
{
o_partiallastline(buf) = ElvTrue;
marksetoffset(&end, markoffset(&end) + 1);
bufreplace(&end, &end, toCHAR("\n"), 1L);
}
else
o_partiallastline(buf) = ElvFalse;
/* set other options to describe the file */
o_modified(buf) = ElvFalse;
optpreset(o_errlines(buf), o_buflines(buf), OPT_HIDE);
#ifdef PROTOCOL_FTP
if (!strncmp(filename, "ftp:", 4))
o_readonly(buf) = (ELVBOOL)(ftpperms == DIR_READONLY);
#endif
/* Restore the marks to their previous offsets. Otherwise any marks
* which refer to this buffer will be set to the end of the file.
* Restoring them isn't perfect, but it beats setting them all to EOF!
* (Note: New buffers won't have an "undo" version.)
*/
if (buf->undo && buf->undo->marklist)
{
for (i = 0; buf->undo->marklist[i].mark; i++)
{
/* is the mark still in this buffer? */
for (mark = buf->marks;
mark && mark != buf->undo->marklist[i].mark;
mark = mark->next)
{
}
if (!mark)
/* not in this buffer anymore, so free it */
continue;
/* Restore its offset */
marksetoffset(mark, buf->undo->marklist[i].offset);
/* Some marks, newer than those in the marklist, may
* have offsets past the end of the buffer. Set their
* offset to the end of the buffer.
*/
for (mark = buf->marks; mark; mark = mark->next)
{
if (markoffset(mark) > o_bufchars(buf))
marksetoffset(mark, o_bufchars(buf));
}
}
}
/* execute the file initialization script, if it exists */
if (!o_internal(buf))
{
#ifndef FEATURE_CALC
/* Whoa! Since :if doesn't exist, the normal script won't
* work. Just take a guess at the display mode.
*/
if (o_filename(buf))
{
char *ext = strrchr(tochar8(o_filename(buf)), '.');
bufoptions(buf);
if (!ext)
/* do nothing */;
# ifdef DISPLAY_HTML
else if (!strncmp(ext, ".htm", 4)
|| !strncmp(ext, ".HTM", 4)
|| scanchar(marktmp(top, buf, 0)) == '<')
optputstr(toCHAR("bd"), toCHAR("html"), ElvFalse);
# endif
# ifdef DISPLAY_MAN
else if (!strcmp(ext, ".man")
|| (elvdigit(ext[1]) && !ext[2])
|| scanchar(marktmp(top, buf, 0)) == '.')
optputstr(toCHAR("bd"), toCHAR("man"), ElvFalse);
# endif
# ifdef DISPLAY_SYNTAX
else if (descr_known(tochar8(o_filename(buf)), SYNTAX_FILE))
optputstr(toCHAR("bd"), toCHAR("syntax"), ElvFalse);