xref: /original-bsd/usr.bin/window/cmd.c (revision 9a897be2)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)cmd.c	3.40 (Berkeley) 06/06/90";
13 #endif /* not lint */
14 
15 #include "defs.h"
16 #include "char.h"
17 
18 docmd()
19 {
20 	register char c;
21 	register struct ww *w;
22 	char out = 0;
23 
24 	while (!out && !quit) {
25 		if ((c = wwgetc()) < 0) {
26 			if (terse)
27 				wwsetcursor(0, 0);
28 			else {
29 				wwputs("Command: ", cmdwin);
30 				wwcurtowin(cmdwin);
31 			}
32 			do
33 				wwiomux();
34 			while ((c = wwgetc()) < 0);
35 		}
36 		if (!terse)
37 			wwputc('\n', cmdwin);
38 		switch (c) {
39 		default:
40 			if (c != escapec)
41 				break;
42 		case 'h': case 'j': case 'k': case 'l':
43 		case ctrl('y'):
44 		case ctrl('e'):
45 		case ctrl('u'):
46 		case ctrl('d'):
47 		case ctrl('b'):
48 		case ctrl('f'):
49 		case ctrl('s'):
50 		case ctrl('q'):
51 		case ctrl('['):
52 			if (selwin == 0) {
53 				error("No window.");
54 				continue;
55 			}
56 		}
57 		switch (c) {
58 		case '1': case '2': case '3': case '4': case '5':
59 		case '6': case '7': case '8': case '9':
60 			if ((w = window[c - '1']) == 0) {
61 				error("%c: No such window.", c);
62 				break;
63 			}
64 			setselwin(w);
65 			if (checkproc(selwin) >= 0)
66 				 out = 1;
67 			break;
68 		case '%':
69 			if ((w = getwin()) != 0)
70 				setselwin(w);
71 			break;
72 		case ctrl('^'):
73 			if (lastselwin != 0) {
74 				setselwin(lastselwin);
75 				if (checkproc(selwin) >= 0)
76 					out = 1;
77 			} else
78 				error("No previous window.");
79 			break;
80 		case 'c':
81 			if ((w = getwin()) != 0)
82 				closewin(w);
83 			break;
84 		case 'w':
85 			c_window();
86 			break;
87 		case 'm':
88 			if ((w = getwin()) != 0)
89 				c_move(w);
90 			break;
91 		case 'M':
92 			if ((w = getwin()) != 0)
93 				movewin(w, w->ww_alt.t, w->ww_alt.l);
94 			break;
95 		case 's':
96 			if ((w = getwin()) != 0)
97 				c_size(w);
98 			break;
99 		case 'S':
100 			if ((w = getwin()) != 0)
101 				sizewin(w, w->ww_alt.nr, w->ww_alt.nc);
102 			break;
103 		case ':':
104 			c_colon();
105 			break;
106 		case 'h':
107 			(void) wwwrite(selwin, "\b", 1);
108 			break;
109 		case 'j':
110 			(void) wwwrite(selwin, "\n", 1);
111 			break;
112 		case 'k':
113 			(void) wwwrite(selwin, "\033A", 2);
114 			break;
115 		case 'l':
116 			(void) wwwrite(selwin, "\033C", 2);
117 			break;
118 		case ctrl('e'):
119 			wwscroll(selwin, 1);
120 			break;
121 		case ctrl('y'):
122 			wwscroll(selwin, -1);
123 			break;
124 		case ctrl('d'):
125 			wwscroll(selwin, selwin->ww_w.nr / 2);
126 			break;
127 		case ctrl('u'):
128 			wwscroll(selwin, - selwin->ww_w.nr / 2);
129 			break;
130 		case ctrl('f'):
131 			wwscroll(selwin, selwin->ww_w.nr);
132 			break;
133 		case ctrl('b'):
134 			wwscroll(selwin, - selwin->ww_w.nr);
135 			break;
136 		case ctrl('s'):
137 			stopwin(selwin);
138 			break;
139 		case ctrl('q'):
140 			startwin(selwin);
141 			break;
142 		case ctrl('l'):
143 			wwredraw();
144 			break;
145 		case '?':
146 			c_help();
147 			break;
148 		case ctrl('['):
149 			if (checkproc(selwin) >= 0)
150 				out = 1;
151 			break;
152 		case ctrl('z'):
153 			wwsuspend();
154 			break;
155 		case 'q':
156 			c_quit();
157 			break;
158 		/* debugging stuff */
159 		case '&':
160 			if (debug) {
161 				c_debug();
162 				break;
163 			}
164 		default:
165 			if (c == escapec) {
166 				if (checkproc(selwin) >= 0) {
167 					(void) write(selwin->ww_pty,
168 						&escapec, 1);
169 					out = 1;
170 				}
171 			} else {
172 				if (!terse)
173 					wwbell();
174 				error("Type ? for help.");
175 			}
176 		}
177 	}
178 	if (!quit)
179 		setcmd(0);
180 }
181 
182 struct ww *
183 getwin()
184 {
185 	register int c;
186 	struct ww *w = 0;
187 
188 	if (!terse)
189 		wwputs("Which window? ", cmdwin);
190 	wwcurtowin(cmdwin);
191 	while ((c = wwgetc()) < 0)
192 		wwiomux();
193 	if (debug && c == 'c')
194 		w = cmdwin;
195 	else if (debug && c == 'f')
196 		w = framewin;
197 	else if (debug && c == 'b')
198 		w = boxwin;
199 	else if (c >= '1' && c < NWINDOW + '1')
200 		w = window[c - '1'];
201 	else if (c == '+')
202 		w = selwin;
203 	else if (c == '-')
204 		w = lastselwin;
205 	if (w == 0)
206 		wwbell();
207 	if (!terse)
208 		wwputc('\n', cmdwin);
209 	return w;
210 }
211 
212 checkproc(w)
213 struct ww *w;
214 {
215 	if (w->ww_state != WWS_HASPROC) {
216 		error("No process in window.");
217 		return -1;
218 	}
219 	return 0;
220 }
221 
222 setcmd(new)
223 char new;
224 {
225 	if (new && !incmd) {
226 		if (!terse)
227 			wwadd(cmdwin, &wwhead);
228 		if (selwin != 0)
229 			wwcursor(selwin, 1);
230 		wwcurwin = 0;
231 	} else if (!new && incmd) {
232 		if (!terse) {
233 			wwdelete(cmdwin);
234 			reframe();
235 		}
236 		if (selwin != 0)
237 			wwcursor(selwin, 0);
238 		wwcurwin = selwin;
239 	}
240 	incmd = new;
241 }
242 
243 setterse(new)
244 char new;
245 {
246 	if (incmd)
247 		if (new && !terse) {
248 			wwdelete(cmdwin);
249 			reframe();
250 		} else if (!new && terse)
251 			wwadd(cmdwin, &wwhead);
252 	terse = new;
253 }
254 
255 /*
256  * Set the current window.
257  */
258 setselwin(w)
259 struct ww *w;
260 {
261 	if (selwin == w)
262 		return;
263 	if (selwin != 0)
264 		lastselwin = selwin;
265 	if ((selwin = w) != 0)
266 		front(selwin, 1);
267 }
268