xref: /original-bsd/usr.bin/window/cmd.c (revision 8251a00e)
1 #ifndef lint
2 static	char *sccsid = "@(#)cmd.c	3.1 83/08/11";
3 #endif
4 
5 #include "defs.h"
6 
7 struct ww *getwin();
8 
9 docmd()
10 {
11 	register char c;
12 	register struct ww *w;
13 
14 	if (!terse)
15 		wwadd(cmdwin, &wwhead);
16 	/*
17 	if (selwin != 0)
18 		Woncursor(selwin->ww_win, 1);
19 	*/
20 top:
21 	while ((c = bgetc()) >= 0) {
22 		if (!terse)
23 			(void) wwputs("\r\n", cmdwin);
24 		switch (c) {
25 		default:
26 			if (c == escapec)
27 				goto foo;
28 			break;
29 		case 'h': case 'j': case 'k': case 'l':
30 		case CTRL(u):
31 		case CTRL(d):
32 		case CTRL(b):
33 		case CTRL(f):
34 		case CTRL([):
35 		foo:
36 			if (selwin == 0) {
37 				error("No window.");
38 				continue;
39 			}
40 		}
41 		switch (c) {
42 		case '1': case '2': case '3': case '4': case '5':
43 		case '6': case '7': case '8': case '9':
44 			if ((w = window[c - '1']) == 0) {
45 				wwbell();
46 				break;
47 			}
48 			setselwin(w);
49 			goto out;
50 		case '%':
51 			if ((w = getwin()) != 0)
52 				setselwin(w);
53 			break;
54 		case 'c':
55 			if ((w = getwin()) != 0)
56 				c_close(w);
57 			break;
58 		case 'C':
59 			c_close((struct ww *)0);
60 			break;
61 		case 'Z':
62 			error("Command Z is now C.");
63 			break;
64 		case 'w':
65 			c_window();
66 			break;
67 		case 'S':
68 			c_show();
69 			break;
70 		case 'L':
71 			c_list();
72 			break;
73 		case 's':
74 			c_stat();
75 			break;
76 		case 'M':
77 			wwdumpsmap();
78 			break;
79 		case 'V':
80 			if ((w = getwin()) != 0)
81 				wwdumpnvis(w);
82 			break;
83 		case 'D':
84 			if ((w = getwin()) != 0)
85 				wwdumpcov(w);
86 			break;
87 		case 'W':
88 			if ((w = getwin()) != 0)
89 				wwdumpwin(w);
90 			break;
91 		case 't':
92 			c_time(RUSAGE_SELF);
93 			break;
94 		case 'T':
95 			c_time(RUSAGE_CHILDREN);
96 			break;
97 		case ':':
98 			c_colon();
99 			break;
100 		case 'h':
101 			(void) wwwrite(selwin, "\b", 1);
102 			break;
103 		case 'j':
104 			(void) wwwrite(selwin, "\n", 1);
105 			break;
106 		case 'k':
107 			(void) wwwrite(selwin, "\033A", 2);
108 			break;
109 		case 'l':
110 			(void) wwwrite(selwin, "\033C", 2);
111 			break;
112 		/*
113 		case CTRL(d):
114 			c_scroll(1);
115 			break;
116 		case CTRL(u):
117 			c_scroll(-1);
118 			break;
119 		case CTRL(f):
120 			c_scroll(2);
121 			break;
122 		case CTRL(b):
123 			c_scroll(-2);
124 			break;
125 		*/
126 		case CTRL(l):
127 			wwredraw();
128 			break;
129 		case '?':
130 			c_help();
131 			break;
132 		case CTRL([):
133 			goto out;
134 		case CTRL(z):
135 			wwsuspend();
136 			break;
137 		case 'q':
138 			c_quit();
139 			if (quit)
140 				goto out;
141 			break;
142 		case '.':
143 			error("Use q to quit.");
144 			break;
145 		default:
146 			if (c == escapec) {
147 				(void) write(selwin->ww_pty, &escapec, 1);
148 				goto out;
149 			}
150 			if (!terse)
151 				wwbell();
152 			error("Type ? for help.");
153 			break;
154 		}
155 	}
156 	if (terse)
157 		wwsetcursor(0, 0);
158 	else {
159 		if (!terse)
160 			(void) wwputs("Command: ", cmdwin);
161 		wwsetcursor(wwcurrow(cmdwin), wwcurcol(cmdwin));
162 	}
163 	while (bpeekc() < 0)
164 		bread();
165 	goto top;
166 out:
167 	if (!quit) {
168 		curwin = selwin;
169 		if (!terse) {
170 			wwdelete(cmdwin);
171 			reframe();
172 		}
173 	}
174 	/*
175 	if (selwin != 0)
176 		Woncursor(selwin->ww_win, 0);
177 	*/
178 }
179 
180 struct ww *
181 getwin()
182 {
183 	register int c;
184 	struct ww *w = 0;
185 
186 	if (!terse)
187 		(void) wwputs("Which window? ", cmdwin);
188 	wwsetcursor(wwcurrow(cmdwin), wwcurcol(cmdwin));
189 	while ((c = bgetc()) < 0)
190 		bread();
191 	if (debug && c == 'c')
192 		w = cmdwin;
193 	else if (debug && c == 'f')
194 		w = framewin;
195 	else if (c >= '1' && c < NWINDOW + '1')
196 		w = window[c - '1'];
197 	if (w == 0)
198 		wwbell();
199 	if (!terse)
200 		(void) wwputs("\r\n", cmdwin);
201 	return w;
202 }
203 
204 setselwin(w)
205 register struct ww *w;
206 {
207 	register struct ww *oldselwin = selwin;
208 
209 	if (w == oldselwin)
210 		return;
211 	if (selwin = w) {
212 		wwdelete(w);
213 		/*
214 		 * Stick it in front of the old selected window,
215 		 * or behind everbody else.
216 		 */
217 		wwadd(w, (oldselwin ? oldselwin : &wwhead)->ww_back);
218 		/*
219 		Woncursor(w->ww_win, 1);
220 		*/
221 	}
222 	/*
223 	if (oldselwin) {
224 		Woncursor(oldselwin->ww_win, 0);
225 	}
226 	*/
227 	reframe();
228 }
229 
230 labelwin(w)
231 register struct ww *w;
232 {
233 	int mode = w == selwin ? WWM_REV : 0;
234 
235 	if (w->ww_id >= 0) {
236 		char buf[2];
237 
238 		buf[0] = w->ww_id + '1';
239 		buf[1] = 0;
240 		(void) wwlabel(w, framewin, 1, buf, mode);
241 	}
242 	if (w->ww_label) {
243 		int col;
244 
245 		if (w->ww_center) {
246 			col = (w->ww_w.nc - strlen(w->ww_label)) / 2;
247 			col = MAX(3, col);
248 		} else
249 			col = 3;
250 		(void) wwlabel(w, framewin, col, w->ww_label, mode);
251 	}
252 }
253