split is "sp", not "s"
[nvi.git] / common / screen.c
blobb2b96189250fb3f98c0d85cef4f69302c2a03425
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: screen.c,v 8.51 1994/01/11 22:36:23 bostic Exp $ (Berkeley) $Date: 1994/01/11 22:36:23 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
19 #include "vi.h"
20 #include "vcmd.h"
21 #include "excmd.h"
22 #include "tag.h"
25 * screen_init --
26 * Do the default initialization of an SCR structure.
28 int
29 screen_init(orig, spp, flags)
30 SCR *orig, **spp;
31 u_int flags;
33 SCR *sp;
34 size_t len;
36 *spp = NULL;
37 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
38 *spp = sp;
40 /* INITIALIZED AT SCREEN CREATE. */
41 sp->gp = __global_list; /* All ref the GS structure. */
43 LIST_INIT(&sp->msgq);
44 CIRCLEQ_INIT(&sp->frefq);
46 sp->ccnt = 2; /* Anything > 1 */
48 FD_ZERO(&sp->rdfd);
50 CIRCLEQ_INIT(&sp->tiq);
52 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
53 if (orig == NULL) {
54 sp->searchdir = NOTSET;
55 sp->csearchdir = CNOTSET;
57 switch (flags & S_SCREENS) {
58 case S_EX:
59 if (sex_screen_init(sp))
60 return (1);
61 break;
62 case S_VI_CURSES:
63 if (svi_screen_init(sp))
64 return (1);
65 break;
66 case S_VI_XAW:
67 if (xaw_screen_init(sp))
68 return (1);
69 break;
70 default:
71 abort();
74 sp->flags = flags;
75 } else {
76 if (orig->alt_name != NULL &&
77 (sp->alt_name = strdup(orig->alt_name)) == NULL)
78 goto mem;
80 /* Retain all searching/substitution information. */
81 if (F_ISSET(orig, S_SRE_SET)) {
82 F_SET(sp, S_SRE_SET);
83 sp->sre = orig->sre;
85 if (F_ISSET(orig, S_SUBRE_SET)) {
86 F_SET(sp, S_SUBRE_SET);
87 sp->subre = orig->subre;
89 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
90 sp->csearchdir = CNOTSET;
91 sp->lastckey = orig->lastckey;
93 if (orig->matchsize) {
94 len = orig->matchsize * sizeof(regmatch_t);
95 MALLOC(sp, sp->match, regmatch_t *, len);
96 if (sp->match == NULL)
97 goto mem;
98 sp->matchsize = orig->matchsize;
99 memmove(sp->match, orig->match, len);
101 if (orig->repl_len) {
102 MALLOC(sp, sp->repl, char *, orig->repl_len);
103 if (sp->repl == NULL)
104 goto mem;
105 sp->repl_len = orig->repl_len;
106 memmove(sp->repl, orig->repl, orig->repl_len);
108 if (orig->newl_len) {
109 len = orig->newl_len * sizeof(size_t);
110 MALLOC(sp, sp->newl, size_t *, len);
111 if (sp->newl == NULL)
112 goto mem;
113 sp->newl_len = orig->newl_len;
114 sp->newl_cnt = orig->newl_cnt;
115 memmove(sp->newl, orig->newl, len);
118 sp->saved_vi_mode = orig->saved_vi_mode;
120 if (opts_copy(orig, sp)) {
121 mem: msgq(orig, M_SYSERR, "new screen attributes");
122 (void)screen_end(sp);
123 return (1);
126 sp->s_bell = orig->s_bell;
127 sp->s_bg = orig->s_bg;
128 sp->s_busy = orig->s_busy;
129 sp->s_change = orig->s_change;
130 sp->s_chposition = orig->s_chposition;
131 sp->s_clear = orig->s_clear;
132 sp->s_column = orig->s_column;
133 sp->s_confirm = orig->s_confirm;
134 sp->s_down = orig->s_down;
135 sp->s_edit = orig->s_edit;
136 sp->s_end = orig->s_end;
137 sp->s_ex_cmd = orig->s_ex_cmd;
138 sp->s_ex_run = orig->s_ex_run;
139 sp->s_ex_write = orig->s_ex_write;
140 sp->s_fg = orig->s_fg;
141 sp->s_fill = orig->s_fill;
142 sp->s_get = orig->s_get;
143 sp->s_key_read = orig->s_key_read;
144 sp->s_optchange = orig->s_optchange;
145 sp->s_position = orig->s_position;
146 sp->s_rabs = orig->s_rabs;
147 sp->s_refresh = orig->s_refresh;
148 sp->s_relative = orig->s_relative;
149 sp->s_rrel = orig->s_rrel;
150 sp->s_split = orig->s_split;
151 sp->s_suspend = orig->s_suspend;
152 sp->s_up = orig->s_up;
154 F_SET(sp, F_ISSET(orig, S_SCREENS));
157 if (xaw_screen_copy(orig, sp)) /* Init S_VI_XAW screen. */
158 return (1);
159 if (svi_screen_copy(orig, sp)) /* Init S_VI_CURSES screen. */
160 return (1);
161 if (sex_screen_copy(orig, sp)) /* Init S_EX screen. */
162 return (1);
163 if (v_screen_copy(orig, sp)) /* Init vi. */
164 return (1);
165 if (ex_screen_copy(orig, sp)) /* Init ex. */
166 return (1);
168 *spp = sp;
169 return (0);
173 * screen_end --
174 * Release a screen.
177 screen_end(sp)
178 SCR *sp;
180 int rval;
182 rval = 0;
183 if (xaw_screen_end(sp)) /* End S_VI_XAW screen. */
184 rval = 1;
185 if (svi_screen_end(sp)) /* End S_VI_CURSES screen. */
186 rval = 1;
187 if (sex_screen_end(sp)) /* End S_EX screen. */
188 rval = 1;
189 if (v_screen_end(sp)) /* End vi. */
190 rval = 1;
191 if (ex_screen_end(sp)) /* End ex. */
192 rval = 1;
194 /* Free FREF's. */
195 { FREF *frp;
196 while ((frp = sp->frefq.cqh_first) != (FREF *)&sp->frefq) {
197 CIRCLEQ_REMOVE(&sp->frefq, frp, q);
198 if (frp->cname != NULL)
199 free(frp->cname);
200 if (frp->name != NULL)
201 free(frp->name);
202 if (frp->tname != NULL)
203 free(frp->tname);
204 FREE(frp, sizeof(FREF));
208 /* Free any text input. */
209 text_lfree(&sp->tiq);
211 /* Free any script information. */
212 if (F_ISSET(sp, S_SCRIPT))
213 sscr_end(sp);
215 /* Free alternate file name. */
216 if (sp->alt_name != NULL)
217 FREE(sp->alt_name, strlen(sp->alt_name) + 1);
219 /* Free up search information. */
220 if (sp->match != NULL)
221 FREE(sp->match, sizeof(regmatch_t));
222 if (sp->repl != NULL)
223 FREE(sp->repl, sp->repl_len);
224 if (sp->newl != NULL)
225 FREE(sp->newl, sp->newl_len);
227 /* Free all the options */
228 opts_free(sp);
231 * Free the message chain last, so previous failures have a place
232 * to put messages. Copy messages to (in order) a related screen,
233 * any screen, the global area.
235 { SCR *c_sp; MSG *mp, *next;
236 if ((c_sp = sp->q.cqe_prev) != (void *)&sp->gp->dq) {
237 if (F_ISSET(sp, S_BELLSCHED))
238 F_SET(c_sp, S_BELLSCHED);
239 } else if ((c_sp = sp->q.cqe_next) != (void *)&sp->gp->dq) {
240 if (F_ISSET(sp, S_BELLSCHED))
241 F_SET(c_sp, S_BELLSCHED);
242 } else if ((c_sp =
243 sp->gp->hq.cqh_first) != (void *)&sp->gp->hq) {
244 if (F_ISSET(sp, S_BELLSCHED))
245 F_SET(c_sp, S_BELLSCHED);
246 } else {
247 c_sp = NULL;
248 if (F_ISSET(sp, S_BELLSCHED))
249 F_SET(sp->gp, G_BELLSCHED);
252 for (mp = sp->msgq.lh_first; mp != NULL; mp = next) {
253 if (!F_ISSET(mp, M_EMPTY))
254 msg_app(sp->gp, c_sp,
255 mp->flags & M_INV_VIDEO, mp->mbuf, mp->len);
256 next = mp->q.le_next;
257 if (mp->mbuf != NULL)
258 FREE(mp->mbuf, mp->blen);
259 FREE(mp, sizeof(MSG));
263 /* Remove the screen from the displayed queue. */
264 CIRCLEQ_REMOVE(&sp->gp->dq, sp, q);
266 /* Free the screen itself. */
267 FREE(sp, sizeof(SCR));
269 return (rval);