-
Notifications
You must be signed in to change notification settings - Fork 26
/
exaction.c
1580 lines (1400 loc) · 38.8 KB
/
exaction.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
/* exaction.c */
/* Copyright 1995 by Steve Kirkendall */
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_exaction[] = "$Id: exaction.c,v 2.127 2004/03/22 00:20:03 steve Exp $";
#endif
#ifdef FEATURE_MISC
/* This function implements the :@x command. */
RESULT ex_at(xinf)
EXINFO *xinf;
{
BUFFER cutbuf;
MARKBUF top, bottom;
/* a cut buffer name is required */
if (!xinf->cutbuf)
{
msg(MSG_ERROR, "cut buffer name required");
return RESULT_ERROR;
}
/* find the cut buffer */
cutbuf = cutbuffer(xinf->cutbuf, ElvFalse);
if (!cutbuf || o_bufchars(cutbuf) == 0L)
{
msg(MSG_ERROR, "[C]cut buffer $1 empty", xinf->cutbuf);
return RESULT_ERROR;
}
/* execute the cut buffer */
return experform(xinf->window, marktmp(top, cutbuf, 0L),
marktmp(bottom, cutbuf, o_bufchars(cutbuf)));
}
/* This function implements the :buffer command, and :browse and :sbrowse. */
RESULT ex_buffer(xinf)
EXINFO *xinf;
{
BUFFER html; /* an html document, for browing */
MARKBUF tmp, tmp2; /* a temporary mark, for adding to html */
long first; /* offset of first buffer name in html */
BUFFER buf; /* a buffer */
WINDOW win; /* a window that shows a given buffer */
CHAR winlist[200]; /* buffer, holds string containing output line */
CHAR *name;
int len; /* length of string in winlist[] */
int bnlen; /* length of buffer name */
CHAR *htmlhead = toCHAR("\
<html><head>\n\
<title>Buffer List</title>\n\
</head><body>\n\
<h1>Buffer List</h1>\n\
<menu>\n");
CHAR *htmltail = toCHAR("</menu></body></html>\n");
assert(xinf->command == EX_BUFFER || xinf->command == EX_BBROWSE || xinf->command == EX_SBBROWSE);
/* do we have an argument? */
if (xinf->rhs)
{
/* switch to the named buffer */
buf = buffind(xinf->rhs);
if (!buf)
{
msg(MSG_ERROR, "[S]no buffer named $1", xinf->rhs);
return RESULT_ERROR;
}
xinf->newcurs = markalloc(buf, buf->docursor);
}
else
{
/* if browsing, then create a buffer */
html = NULL;
first = 0L;
if (xinf->command != EX_BUFFER)
{
html = bufalloc(toCHAR(BBROWSE_BUF), 0, ElvTrue);
bufreplace(marktmp(tmp, html, 0),
marktmp(tmp2, html, o_bufchars(html)),
htmlhead,
CHARlen(htmlhead));
o_bufdisplay(html) = toCHAR("html");
o_initialsyntax(html) = ElvFalse;
o_readonly(html) = ElvTrue;
}
/* no arguments -- list the buffers */
for (buf = buflist((BUFFER)0); buf; buf = buflist(buf))
{
/* If no ! given, then ignore internal buffers.
* And always ignore the buffer list itself
*/
if (buf == html || (o_internal(buf) && !xinf->bang))
{
continue;
}
/* make a list of the windows showing that buffer */
name = o_filename(buf) ? o_filename(buf) : o_bufname(buf);
bnlen = CHARlen(name);
len = 0;
if (!html)
{
for (win = winofbuf((WINDOW)0, buf);
len < (int)(QTY(winlist) - 5 - bnlen) && win;
win = winofbuf(win, buf))
{
/* add this window to the list */
sprintf((char *)winlist + len, "%ld: ", o_windowid(win));
len = CHARlen(winlist);
}
while (len < 6)
{
winlist[len] = ' ';
len++;
}
}
winlist[len++] = (o_internal(buf) ? '-' : o_modified(buf) ? '*' : ' ');
(void)CHARncpy(&winlist[len], name, (size_t)bnlen);
len += bnlen;
if (o_bufid(buf) && len < QTY(winlist) - 10)
{
sprintf((char *)&winlist[len], " (#%ld)", o_bufid(buf));
len = CHARlen(winlist);
}
/* output a line of info about the buffer */
if (html)
{
marksetoffset(&tmp, o_bufchars(html));
bufreplace(&tmp, &tmp, toCHAR("<li><a href=\"buffer:"), 20L);
marksetoffset(&tmp, o_bufchars(html));
bufreplace(&tmp, &tmp, o_bufname(buf), CHARlen(o_bufname(buf)));
marksetoffset(&tmp, o_bufchars(html));
bufreplace(&tmp, &tmp, toCHAR("\">"), 2L);
if (first == 0L)
first = o_bufchars(html);
marksetoffset(&tmp, o_bufchars(html));
bufreplace(&tmp, &tmp, winlist, len);
marksetoffset(&tmp, o_bufchars(html));
bufreplace(&tmp, &tmp, toCHAR("</a>\n"), 5L);
}
else
{
winlist[len++] = '\n';
drawextext(windefault, winlist, len);
}
}
/* if generating html document */
if (html)
{
/* finish off the HTML document */
marksetoffset(&tmp, o_bufchars(html));
bufreplace(&tmp, &tmp, htmltail, CHARlen(htmltail));
o_modified(html) = ElvFalse;
/* if :sbbrowse, try to create a new window. If that
* fails, or if :bbrowse, then replace current window.
*/
bufwilldo(marktmp(tmp, html, first), ElvFalse);
if (xinf->command == EX_BBROWSE
|| !(*gui->creategw)(tochar8(o_bufname(html)), ""))
{
/* :bb -- replace current buffer */
tepush(xinf->window, o_previoustag);
o_previoustag = NULL;
xinf->newcurs = markalloc(html, first);
}
}
}
return RESULT_COMPLETE;
}
RESULT ex_all(xinf)
EXINFO *xinf;
{
BUFFER buf;
MARKBUF bufline1;
MARK origcurs;
RESULT result;
/* An RHS is required */
if (!xinf->rhs)
{
msg(MSG_ERROR, "missing rhs");
return RESULT_ERROR;
}
/* Remember the original cursor */
if (!xinf->window)
origcurs = NULL;
else if (xinf->window->state->pop)
origcurs = xinf->window->state->pop->cursor;
else
origcurs = xinf->window->cursor;
/* for each buffer... */
for (buf = buflist((BUFFER)0), result = RESULT_COMPLETE;
buf && result == RESULT_COMPLETE;
buf = buflist(buf))
{
/* unless ":all!", ignore internal buffers */
if (!xinf->bang && o_internal(buf))
continue;
/* Make this buffer the default buffer */
bufoptions(buf);
/* Temporarily move the window's cursor to the first line
* of this buffer.
*/
if (xinf->window)
{
if (xinf->window->state->pop)
xinf->window->state->pop->cursor = marktmp(bufline1, buf, 0);
else
xinf->window->cursor = marktmp(bufline1, buf, 0);
}
/* execute the command */
result = exstring(xinf->window, xinf->rhs, NULL);
}
/* Restore the original cursor */
if (origcurs)
{
if (xinf->window->state->pop)
xinf->window->state->pop->cursor = origcurs;
else
xinf->window->cursor = origcurs;
}
return result;
}
/* This implements the :window command */
RESULT ex_window(xinf)
EXINFO *xinf;
{
WINDOW win;
BUFFER buf;
char idstr[20];
long id;
/* if no argument, then list windows */
if (!xinf->lhs)
{
for (win = winofbuf(NULL, NULL); win; win = winofbuf(win, NULL))
{
sprintf(idstr, "%6ld: ", o_windowid(win));
drawextext(xinf->window, toCHAR(idstr), strlen(idstr));
drawextext(xinf->window,
o_bufname(markbuffer(win->cursor)),
CHARlen(o_bufname(markbuffer(win->cursor))));
drawextext(xinf->window, toCHAR("\n"), 1);
}
return RESULT_COMPLETE;
}
/* otherwise we are trying to switch windows... */
if (calcnumber(xinf->lhs))
{
/* find a window with the given window id */
id = atol(tochar8(xinf->lhs));
for (win = winofbuf(NULL, NULL);
win && o_windowid(win) != id;
win = winofbuf(win, NULL))
{
}
}
else if (xinf->lhs[0] == '+')
{
/* move up 1 window */
for (win = NULL; winofbuf(win, NULL) != xinf->window; )
{
win = winofbuf(win, NULL);
if (!win)
break;
}
if (!win && xinf->lhs[1])
{
for (win = winofbuf(NULL, NULL);
winofbuf(win, NULL);
win = winofbuf(win, NULL))
{
}
}
}
else if (xinf->lhs[0] == '-')
{
/* move down 1 window */
win = winofbuf(xinf->window, NULL);
if (!win && xinf->lhs[1])
win = winofbuf(NULL, NULL);
}
else
{
/* try to find a window showing a given buffer/file */
buf = buffind(xinf->lhs);
if (!buf)
{
msg(MSG_ERROR, "[S]no buffer named $1", xinf->lhs);
return RESULT_ERROR;
}
win = winofbuf(xinf->window, buf);
if (!win)
{
win = winofbuf(NULL, buf);
}
}
/* did we find the new window? */
if (!win)
{
msg(MSG_ERROR, "no such window");
return RESULT_ERROR;
}
/* switch to the window */
if (gui->focusgw)
{
(*gui->focusgw)(win->gw);
}
else
{
eventfocus(win->gw, ElvTrue);
}
return RESULT_COMPLETE;
}
#endif /* FEATURE_MISC */
RESULT ex_cd(xinf)
EXINFO *xinf;
{
BUFFER buf;
CHAR oldpwd[256];
assert(xinf->command == EX_CD);
/* check arguments */
if (xinf->nfiles != 1)
{
msg(MSG_ERROR, "directory name required");
return RESULT_ERROR;
}
/* if any user buffers have been modified but not saved, complain
* unless '!' was given.
*/
if (!xinf->bang)
{
for (buf = NULL; (buf = buflist(buf)) != NULL; )
{
if (!o_internal(buf) && o_modified(buf))
{
msg(MSG_ERROR, "[S]$1 modified, not saved", o_filename(buf) ? o_filename(buf) : o_bufname(buf));
return RESULT_ERROR;
}
}
}
/* remember the old directory name */
CHARcpy(oldpwd, toCHAR(dircwd()));
/* try to switch to that directory */
if (!dirchdir(xinf->file[0]))
{
msg(MSG_ERROR, "can't change directory");
return RESULT_ERROR;
}
/* store the old directory name as "previousdir" option */
if (optflags(o_previousdir) & OPT_FREE)
safefree(o_previousdir);
o_previousdir = CHARkdup(oldpwd);
optflags(o_previousdir) |= OPT_FREE;
/* turn off the "edited" flag for all user buffers */
for (buf = NULL; (buf = buflist(buf)) != NULL; )
{
if (!o_internal(buf))
o_edited(buf) = ElvFalse;
}
return RESULT_COMPLETE;
}
RESULT ex_edit(xinf)
EXINFO *xinf;
{
BUFFER oldbuf; /* buffer to switch from */
BUFFER newbuf; /* buffer to switch to */
STATE *s; /* used for stepping though state stack */
CHAR *p; /* used for scanning movement string */
EXINFO xinfb; /* dummy command buffer, used for parsing address */
long line=0; /* default line number to start on (0 = no default) */
assert(xinf->command == EX_EDIT || xinf->command == EX_OPEN
|| xinf->command == EX_VISUAL || xinf->command == EX_PUSH);
/* These only work when the ex command acts on the main buffer */
if (xinf->window->state->acton && xinf->window->state->acton->acton)
{
msg(MSG_ERROR, "[s]$1 only works on window's main buffer", xinf->cmdname);
return RESULT_ERROR;
}
/* If visual mode isn't supported, then either fail or use open mode */
if (gui->exonly && xinf->command != EX_EDIT && xinf->command != EX_PUSH)
{
msg(MSG_ERROR, "this gui only supports ex mode");
return RESULT_ERROR;
}
else if (!gui->moveto && xinf->command == EX_VISUAL)
{
msg(MSG_WARNING, "using open mode");
xinf->command = EX_OPEN;
}
/* Are we supposed to switch buffers? */
if (xinf->nfiles > 0)
{
/* If we expect to load the "previousfile" then the default
* starting line is the "previousfileline".
*/
if (o_previousfile
&& !CHARcmp(o_previousfile, xinf->file[0]))
{
line = o_previousfileline;
}
/* For now, set the "previous file" to the named file. If the
* command is successful, this will be replaced by the name of
* the old file before ex_edit() returns. Exception: ":e%"
* should not affect the "previous file".
*/
if (o_filename(markbuffer(xinf->window->cursor))
&& strcmp(tochar8(o_filename(markbuffer(xinf->window->cursor))), xinf->file[0]))
{
if (o_previousfile)
safefree(o_previousfile);
optpreset(o_previousfile, CHARdup(toCHAR(xinf->file[0])), OPT_FREE);
o_previousfileline = 1L;
}
/* If the buffer we'll be loading already exists, and has been
* modified, then fail unless "!" was given.
*/
newbuf = buffind(toCHAR(xinf->file[0]));
if (newbuf && o_modified(newbuf) && !xinf->bang)
{
msg(MSG_ERROR, "[S]$1 modified, not saved", o_filename(newbuf) ? o_filename(newbuf) : o_bufname(newbuf));
return RESULT_ERROR;
}
/* None of these commands are ever supposed to save the old
* buffer. When we eventually switch to the new buffer,
* the old buffer will be unloaded (except maybe for :push),
* so if it has been modified and isn't used by anything else,
* we should either fail (if no ! given) or reset the
* "modified" flag (if !, even for :push)
*/
oldbuf = markbuffer(xinf->window->cursor);
if (winofbuf(NULL, oldbuf) == xinf->window
&& winofbuf(xinf->window, oldbuf) == NULL
&& o_modified(oldbuf))
{
if (xinf->bang)
o_modified(oldbuf) = ElvFalse;
else if (!o_autowrite || !bufsave(oldbuf, ElvFalse, ElvFalse))
{
msg(MSG_ERROR, "[S]$1 modified, not saved", o_filename(oldbuf) ? o_filename(oldbuf) : o_bufname(oldbuf));
return RESULT_ERROR;
}
}
/* For :push, we want to save the old position before switching
* to the new buffer.
*/
if (xinf->command == EX_PUSH)
{
tepush(xinf->window, o_previoustag);
o_previoustag = NULL;
}
/* load the new buffer */
newbuf = bufload(NULL, xinf->file[0], ElvTrue);
/* if the line number is invalid, ignore it */
if (line < 0 || line > o_buflines(newbuf))
{
line = 1;
}
/* If we were given a command, and the buffer is either new or
* non-empty, then run the command.
*/
memset((char *)&xinfb, 0, sizeof xinfb);
xinfb.defaddr.buffer = newbuf;
if (line > 0)
xinfb.defaddr.offset = lowline(bufbufinfo(newbuf), line);
else
xinfb.defaddr.offset = newbuf->docursor;
if (xinf->lhs && (o_newfile(newbuf) || o_bufchars(newbuf) > 0))
{
(void)scanstring(&p, xinf->lhs);
if (exparseaddress(&p, &xinfb))
{
xinfb.defaddr.offset = lowline(
bufbufinfo(xinfb.defaddr.buffer),
xinfb.to);
}
scanfree(&p);
#if 1
marksetbuffer(xinf->window->cursor, newbuf);
marksetoffset(xinf->window->cursor, xinfb.defaddr.offset);
exstring(xinf->window, xinf->lhs, "+");
#endif
}
/* change the buffer of this window. */
xinf->newcurs = markdup(&xinfb.defaddr);
}
else if (xinf->command == EX_PUSH || xinf->command == EX_VISUAL)
{
/* Either ":push" or ":vi" without a filename, but maybe with
* a +line argument.
*/
/* :push saves the old position */
if (xinf->command == EX_PUSH)
{
tepush(xinf->window, o_previoustag);
o_previoustag = NULL;
}
/* both commands accept a "+line" argument */
/*!!! THIS ONLY HANDLES LINE NUMBERS, NOT WHOLE COMMANDS */
if (xinf->lhs)
{
xinfb.defaddr = xinf->defaddr;
(void)scanstring(&p, xinf->lhs);
if (exparseaddress(&p, &xinfb))
{
xinfb.defaddr.offset = lowline(
bufbufinfo(xinfb.defaddr.buffer),
xinfb.to);
xinf->newcurs = markdup(&xinfb.defaddr);
}
scanfree(&p);
}
}
/* Set the main buffer to visual/open mode? */
if (xinf->command != EX_EDIT && xinf->command != EX_PUSH)
{
/* exit "ex" mode */
for (s = xinf->window->state; s != xinf->window->state->acton; s = s->pop)
s->flags |= ELVIS_1LINE;
/* force main edit state use use bottom line, or not to */
if (xinf->command == EX_VISUAL)
{
for (; s; s = s->pop)
s->flags &= ~ELVIS_BOTTOM;
}
else
{
for (; s; s = s->pop)
s->flags |= ELVIS_BOTTOM;
}
}
return RESULT_COMPLETE;
}
RESULT ex_file(xinf)
EXINFO *xinf;
{
long lnum;
MARK cursor;
BUFFER buf;
BUFFER other;
switch (xinf->command)
{
case EX_EQUAL:
if (xinf->anyaddr)
{
if (xinf->from != xinf->to)
msg(MSG_INFO, "[dd]$1,$2 = ($2-$1+1) lines",
xinf->from, xinf->to);
else
msg(MSG_INFO, "[d]$1", xinf->from);
}
else
{
msg(MSG_INFO, "(buflines) lines");
}
break;
default: /* EX_FILE */
/* who are we talking about here? */
cursor = &xinf->defaddr;
buf = markbuffer(cursor);
lnum = markline(cursor);
/* were we given a new name for this buffer? */
if (xinf->nfiles == 1)
{
/* see if another buffer has that name already */
other = buffind(toCHAR(xinf->file[0]));
if (other && !xinf->bang)
{
msg(MSG_ERROR, "another buffer already has that name");
return RESULT_ERROR;
}
/* store the given filename as the new file of this buffer */
o_edited(buf) = ElvFalse;
if (optflags(o_filename(buf)) & OPT_FREE)
{
safefree(o_filename(buf));
}
o_filename(buf) = CHARdup(toCHAR(xinf->file[0]));
optflags(o_filename(buf)) |= OPT_FREE;
buftitle(buf, toCHAR(ioabsolute(xinf->file[0])));
}
/* output statistics of this file */
if (!xinf->bang && xinf->window != NULL)
{
if (markbuffer(xinf->window->cursor) == buf && o_buflines(buf) > 0)
{
msg(MSG_INFO, "[dd](filename)(readonly?\" [READONLY]\")(modified?\" [MODIFIED]\")(!edited?\" [NOT EDITED]\")(newfile?\" [NEW FILE]\") ($1 * 100 / $2)%",
lnum, o_buflines(buf));
}
else
{
msg(MSG_INFO, "(filename)(readonly?\" [READONLY]\")(modified?\" [MODIFIED]\")(!edited?\" [NOT EDITED]\")(newfile?\" [NEW FILE]\")");
}
}
break;
}
return RESULT_COMPLETE;
}
#ifdef FEATURE_LPR
/* This function implements the :lpr command */
RESULT ex_lpr(xinf)
EXINFO *xinf;
{
RESULT ret;
CHAR *origlp;
long origcols;
/* if a filename/filter is given on command line, use it */
origlp = o_lpout;
if (xinf->rhs)
{
o_lpout = xinf->rhs;
}
else if (xinf->nfiles >= 1)
{
assert(xinf->nfiles == 1);
o_lpout = toCHAR(xinf->file[0]);
}
/* temporarily set "columns" to match "lpcolumns", so markup text
* will be formatted to fit the page, not the screen.
*/
origcols = o_columns(xinf->window);
o_columns(xinf->window) = o_lpcolumns;
ret = lp(xinf->window, xinf->fromaddr, xinf->toaddr, xinf->bang);
/* restore columns & lpout to their original values */
o_columns(xinf->window) = origcols;
o_lpout = origlp;
return ret;
}
#endif
/* This function implements the :mark and :k commands */
RESULT ex_mark(xinf)
EXINFO *xinf;
{
/* check mark name */
if (!xinf->lhs || *xinf->lhs < 'a' || *xinf->lhs > 'z')
{
msg(MSG_ERROR, "bad mark name");
return RESULT_ERROR;
}
/* if mark already set, then free its old value. */
if (namedmark[*xinf->lhs - 'a'])
{
markfree(namedmark[*xinf->lhs - 'a']);
}
/* set the mark */
namedmark[*xinf->lhs - 'a'] = markdup(xinf->anyaddr || !xinf->window
? xinf->fromaddr
: xinf->window->cursor);
return RESULT_COMPLETE;
}
#ifdef FEATURE_MKEXRC
RESULT ex_mkexrc(xinf)
EXINFO *xinf;
{
BUFFER buf = buffind(toCHAR(CUSTOM_BUF));
MARKBUF top, bottom;
char *filename;
/* if a name was given on the command line, then set mkexrcfile to
* that name, and regenerate the CUSTOM_BUF.
*/
if (xinf->nfiles == 1 || !o_mkexrcfile)
{
/* choose a filename -- the given one, or ~/CUSTOM_FILE */
if (xinf->nfiles == 1)
filename = xinf->file[0];
else
filename = dirpath(tochar8(o_home), CUSTOM_FILE);
/* store the name in mkexrcfile, and regenerate the CUSTOM_BUF
* so it contains the new value of mkexrcfile.
*/
(void)optputstr(toCHAR("mkexrcfile"), toCHAR(filename), ElvFalse);
eventupdatecustom(ElvTrue);
eventupdatecustom(ElvFalse);
buf = buffind(toCHAR(CUSTOM_BUF));
}
else
/* safe to overwrite, since we'll be writing back out to the
* file that we read these options from in the first place.
*/
xinf->bang = ElvTrue;
/* if no changes have been made, great! */
if (!buf)
{
return RESULT_COMPLETE;
}
/* else write the buffer out. Allow an existing file to be overwritten
* only if no filename was given
*/
if (bufwrite(marktmp(top,buf,0), marktmp(bottom,buf,o_bufchars(buf)),
tochar8(o_mkexrcfile), xinf->bang))
{
return RESULT_COMPLETE;
}
return RESULT_ERROR;
}
#endif
/* This implements the commands which read the args list... :next, :Next,
* :previous, :rewind, :last, :wnext, :snext, :sNext, :sprevious, :srewind,
* and :slast.
*/
RESULT ex_next(xinf)
EXINFO *xinf;
{
int newargnext; /* value argnext should have if successful */
BUFFER oldbuf; /* the buffer we're leaving */
BUFFER newbuf; /* the buffer we're entering */
#ifdef FEATURE_SPLIT
ELVBOOL splitting; /* are we going to create a new window? */
#endif
ELVBOOL closing; /* are we going to delete the old buffer? */
char **tmp; /* used for swapping args lists */
assert(xinf->command == EX_NEXT || xinf->command == EX_PREVIOUS ||
xinf->command == EX_LAST || xinf->command == EX_REWIND ||
xinf->command == EX_SNEXT || xinf->command == EX_SPREVIOUS ||
xinf->command == EX_SLAST || xinf->command == EX_SREWIND ||
xinf->command == EX_WNEXT);
/* initialize some variables */
oldbuf = markbuffer(xinf->window->cursor);
#ifdef FEATURE_SPLIT
splitting = (ELVBOOL)(xinf->command == EX_SNEXT || xinf->command == EX_SPREVIOUS
|| xinf->command == EX_SLAST || xinf->command == EX_SREWIND);
closing = (ELVBOOL)(!splitting && (xinf->command == EX_WNEXT || (winofbuf(NULL, oldbuf) == xinf->window
&& winofbuf(xinf->window, oldbuf) == NULL)));
#else
closing = (ELVBOOL)(xinf->command == EX_WNEXT || (winofbuf(NULL, oldbuf) == xinf->window
&& winofbuf(xinf->window, oldbuf) == NULL));
#endif
/* diddle with the args list. Upon exit, arglist[argnext] is name
* of the next file to load.
*/
newargnext = argnext;
if (xinf->command == EX_REWIND || xinf->command == EX_SREWIND)
{
newargnext = 0;
}
else if (xinf->command == EX_LAST || xinf->command == EX_SLAST)
{
for (newargnext = 0; arglist[newargnext] && arglist[newargnext + 1]; newargnext++)
{
}
}
else if (xinf->command == EX_PREVIOUS || xinf->command == EX_SPREVIOUS)
{
if (argnext < 2)
{
msg(MSG_ERROR, "no more files");
return RESULT_ERROR;
}
newargnext -= 2;
}
else if (xinf->nfiles > 0)
{
/* swap the new args list with the old one. When the ex
* command is freed, the old args list will be freed and the
* new one will remain.
*/
tmp = xinf->file;
xinf->file = arglist;
arglist = tmp;
for (xinf->nfiles = 0; xinf->file[xinf->nfiles]; xinf->nfiles++)
{
}
newargnext = argnext = 0;
}
/* if there is no next file, complain */
if (!arglist[newargnext])
{
msg(MSG_ERROR, "no more files");
return RESULT_ERROR;
}
/* if we'll be closing this buffer, be cautious */
if (closing && o_modified(oldbuf) && !o_autowrite && xinf->command != EX_WNEXT)
{
/* Trying to leave a modifed file which wouldn't be saved */
if (!xinf->bang)
{
msg(MSG_ERROR, "modified, not written");
return RESULT_ERROR;
}
/* else ":n!", so turn off the modified flag */
o_modified(oldbuf) = ElvFalse;
}
if (!closing || bufsave(oldbuf, xinf->bang, (ELVBOOL)(xinf->command == EX_WNEXT)))
{
/* load the new buffer */
newbuf = bufload(NULL, arglist[newargnext++], ElvFalse);
/* either create a new window, or change the buffer
* of this window.
*/
#ifdef FEATURE_SPLIT
if (splitting)
{
if (!(*gui->creategw)(tochar8(o_bufname(newbuf)), ""))
{
return RESULT_ERROR;
}
}
else
#endif
{
xinf->newcurs = markalloc(newbuf, newbuf->docursor);
}
}
argnext = newargnext;
return RESULT_COMPLETE;
}
RESULT ex_pop(xinf)
EXINFO *xinf;
{
int i;
BUFFER buf = markbuffer(xinf->window->cursor);
/* if the tag stack is empty, fail */
if (!xinf->window->tagstack[0].origin)
{
msg(MSG_ERROR, "tag stack empty");
return RESULT_ERROR;
}
/* If the popped buffer is different from the current buffer, then
* we'll want to save the current buffer before switching. If we
* can't switch, then fail unless ! given.
*/
if (markbuffer(xinf->window->tagstack[0].origin) != buf
&& !xinf->bang
&& (o_autowrite ? !bufsave(buf, ElvFalse, ElvFalse)
: o_modified(buf)))
{
if (!o_autowrite)
{
msg(MSG_ERROR, "[S]$1 modified, not saved", o_filename(buf) ? o_filename(buf) : o_bufname(buf));
}
return RESULT_ERROR;
}
/* set the "previous tag" back to what it was when this stack entry
* was pushed.
*/
if (o_previoustag)
safefree(o_previoustag);
o_previoustag = xinf->window->tagstack[0].prevtag;
/* change this window's display mode to what it was when tag pushed */
(void)dispset(xinf->window, xinf->window->tagstack[0].display);
/* cause the cursor to be moved to the position on top of tag stack */
xinf->newcurs = xinf->window->tagstack[0].origin;
/* delete the top item from the tag stack */
for (i = 0; i < TAGSTK - 1; i++)
{
xinf->window->tagstack[i] = xinf->window->tagstack[i + 1];
}
xinf->window->tagstack[i].origin = NULL;
xinf->window->tagstack[i].prevtag = NULL;
#ifdef FEATURE_TAGS
/* clobber the list of matching tags -- this search is done */
if (taglist)
{
tsadjust(taglist, '+');
tagdelete(ElvTrue);
}
#endif
return RESULT_COMPLETE;
}
RESULT ex_bang(xinf)
EXINFO *xinf;
{
MARK mark;
CHAR *cp;
char *bangcmd;
CHAR iobuf[4096];
int len;
ELVBOOL origrefresh;
ELVBOOL partiallast;
ELVBOOL last;
assert(xinf->command == EX_BANG);
/* rhs is required */
if (!xinf->rhs)
{
msg(MSG_ERROR, "filter name is missing");
return RESULT_ERROR;
}
/* if no lines were specified, then just execute the command and
* show its output in the window.
*/
if (!xinf->fromaddr)
{
/* If the GUI has prgopen()/prgclose(), then we can trust it to
* do good things with stdio. Otherwise we'll need to fake it.
*/
if (gui->prgopen)
{
if (xinf->window)
{
drawopencomplete(xinf->window);
assert(xinf->window->di->drawstate == DRAW_OPENOUTPUT);
}
assert(gui->prgclose);
if (gui->flush)
(*gui->flush)();
if ((*gui->prgopen)((char *)xinf->rhs, ElvFalse, ElvFalse)
&& prggo()
&& (*gui->prgclose)() == 0)