* reordered a little bit
[mascara-docs.git] / i86 / elks / elkscmd / levee / insert.c
blob1b361b675ad024d4f5d4a7c5bb942bd1ead2a8b5
1 /*
2 * LEVEE, or Captain Video; A vi clone
4 * Copyright (c) 1982-1997 David L Parsons
5 * All rights reserved.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by David L Parsons (orc@pell.chi.il.us). My name may not be used
13 * to endorse or promote products derived from this software without
14 * specific prior written permission. THIS SOFTWARE IS PROVIDED
15 * AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16 * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
17 * FITNESS FOR A PARTICULAR PURPOSE.
19 #include "levee.h"
20 #include "extern.h"
22 int PROC
23 insertion(count, openflag, dp, yp, visual)
24 int count, openflag;
25 int *dp;
26 register int *yp;
27 bool visual;
29 char cmd, c;
30 int rp; /* number of spaces to diddle */
31 int ts, ss; /* tabs && spaces to insert */
32 register cp; /* current position */
33 int i; /* random index */
34 int endd; /* last open place */
35 register rsize; /* size of upper buffer */
36 int currDLE = 0; /* what DLE is now */
37 int len; /* full insert size */
38 bool Dflag;
40 if (openflag != 0) { /* opening a line above || below */
41 if (openflag<0 && bufmax>0 && curr<bufmax) {
42 curr = 1+lend;
43 if (visual)
44 strput("\n");
46 else { /* open above current line */
47 (*yp)--;
48 curr = lstart;
50 if (autoindent)
51 currDLE = findDLE(lstart, &i, skipws(lstart),0);
52 if (visual)
53 #if VT52
54 if (OL) {
55 #else
56 if (OL && (*yp) < LINES-2) {
57 #endif
58 strput(OL);
59 (*yp)++;
60 curpos.y = *yp;
62 else {
63 mvcur(1+(*yp), 0);
64 strput(CE);
66 mvcur(-1, currDLE);
68 else {
69 if (autoindent)
70 currDLE = findDLE(lstart, &i, curr, 0);
71 if (curr == i) {
72 if (!delete_to_undo(&undo, lstart, i-lstart))
73 return(-1);
74 curr = lstart;
78 rsize = (bufmax-curr); /* amount of stuff above curr */
79 endd = SIZE - rsize; /* split the buffer */
80 if (rsize > 0)
81 moveright(&core[curr], &core[endd], rsize);
83 cp = curr;
84 do { /* Insert loop */
85 Dflag = (cp==0 || core[cp-1]==EOL);
86 do {
87 if (Dflag)
88 while ((cmd=peekc()) == 0x14 || cmd == 0x04) { /* handle ^T, ^D */
89 if (readchar() == 0x14)
90 currDLE = min(COLS,currDLE+shiftwidth);
91 else
92 currDLE = max(0,currDLE-shiftwidth);
93 mvcur(-1, currDLE);
95 } while (!(c = line(core, cp, endd-1, &len)));
96 if (Dflag && (len > 0 || c == ESC)) {
97 /* makeDLE : optimize leading whitespace for insert */
98 currDLE = findDLE(cp, &rp, cp+len, currDLE);
99 if (rp > cp) {
100 len -= (rp-cp);
101 moveleft(&core[rp], &core[cp], len); /* squash whitespace */
103 if (currDLE > 0) { /* create DLE indent */
104 ts = currDLE / tabsize;
105 ss = currDLE % tabsize;
106 moveright(&core[cp], &core[cp+ts+ss], len);
107 len += (ts+ss);
108 fillchar(&core[cp ], ts, TAB);
109 fillchar(&core[cp+ts], ss, 32);
112 cp += len;
113 if (c == EOL) { /* Diddle-Diddle */
114 core[cp++] = EOL; /* add in a \n */
115 strput(CE); /* clear this line */
116 println();
117 if (visual) {
118 #if RMX
119 /* at OL at bottom kludge... */
120 if (OL && (*yp) < LINES-2) {
121 #else
122 if (OL) {
123 #endif
124 strput(OL);
125 (*yp)++;
127 else
128 strput(CE);
130 if (!autoindent) /* reset currDLE? */
131 currDLE = 0;
132 mvcur(-1, currDLE);
134 } while (c != ESC && cp <= endd-INSSIZE);
135 *dp = cp; /* start display here */
137 if (count > 1) { /* repeated insertion */
138 len = cp-curr;
139 if ((count-1)*len < endd-cp)
140 for (i = 1;i <count;i++) {
141 moveright(&core[cp-len], &core[cp], len);
142 cp += len;
146 if (openflag != 0 /* open or insert at end of buffer */
147 || (rsize < 1 && cp > curr && core[cp-1] != EOL))
148 core[cp++] = EOL;
149 len = cp-curr;
151 if (rsize > 0) /* if not at end of buffer, stitch things together */
152 moveleft(&core[endd], &core[cp], rsize);
153 insert_to_undo(&undo, curr, len);
154 core[bufmax] = EOL;
155 return(cp);