020a7925146a7a6dd258aec59435ca7b23eebc63
[dragonfly.git] / usr.bin / window / cmd.c
1 /*      @(#)cmd.c       8.1 (Berkeley) 6/6/93   */
2 /*      $NetBSD: cmd.c,v 1.8 2003/08/07 11:17:21 agc Exp $      */
3
4 /*
5  * Copyright (c) 1983, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Wang at The University of California, Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <unistd.h>
37 #include "defs.h"
38 #include "char.h"
39
40 int     checkproc(struct ww *);
41
42 void
43 docmd(void)
44 {
45         int c;
46         struct ww *w;
47         char out = 0;
48
49         while (!out && !quit) {
50                 if ((c = wwgetc()) < 0) {
51                         if (terse)
52                                 wwsetcursor(0, 0);
53                         else {
54                                 wwputs("Command: ", cmdwin);
55                                 wwcurtowin(cmdwin);
56                         }
57                         do
58                                 wwiomux();
59                         while ((c = wwgetc()) < 0);
60                 }
61                 if (!terse)
62                         wwputc('\n', cmdwin);
63                 switch (c) {
64                 default:
65                         if (c != escapec)
66                                 break;
67                 case 'h': case 'j': case 'k': case 'l':
68                 case 'y': case 'p':
69                 case ctrl('y'):
70                 case ctrl('e'):
71                 case ctrl('u'):
72                 case ctrl('d'):
73                 case ctrl('b'):
74                 case ctrl('f'):
75                 case ctrl('s'):
76                 case ctrl('q'):
77                 case ctrl('['):
78                         if (selwin == 0) {
79                                 error("No window.");
80                                 continue;
81                         }
82                 }
83                 switch (c) {
84                 case '1': case '2': case '3': case '4': case '5':
85                 case '6': case '7': case '8': case '9':
86                         if ((w = window[c - '1']) == NULL) {
87                                 error("%c: No such window.", c);
88                                 break;
89                         }
90                         setselwin(w);
91                         if (checkproc(selwin) >= 0)
92                                  out = 1;
93                         break;
94                 case '%':
95                         if ((w = getwin()) != NULL)
96                                 setselwin(w);
97                         break;
98                 case ctrl('^'):
99                         if (lastselwin != 0) {
100                                 setselwin(lastselwin);
101                                 if (checkproc(selwin) >= 0)
102                                         out = 1;
103                         } else
104                                 error("No previous window.");
105                         break;
106                 case 'c':
107                         if ((w = getwin()) != NULL)
108                                 closewin(w);
109                         break;
110                 case 'w':
111                         c_window();
112                         break;
113                 case 'm':
114                         if ((w = getwin()) != NULL)
115                                 c_move(w);
116                         break;
117                 case 'M':
118                         if ((w = getwin()) != NULL)
119                                 movewin(w, w->ww_alt.t, w->ww_alt.l);
120                         break;
121                 case 's':
122                         if ((w = getwin()) != NULL)
123                                 c_size(w);
124                         break;
125                 case 'S':
126                         if ((w = getwin()) != NULL)
127                                 sizewin(w, w->ww_alt.nr, w->ww_alt.nc);
128                         break;
129                 case 'y':
130                         c_yank();
131                         break;
132                 case 'p':
133                         c_put();
134                         break;
135                 case ':':
136                         c_colon();
137                         break;
138                 case 'h':
139                         (void) wwwrite(selwin, "\b", 1);
140                         break;
141                 case 'j':
142                         (void) wwwrite(selwin, "\n", 1);
143                         break;
144                 case 'k':
145                         (void) wwwrite(selwin, "\033A", 2);
146                         break;
147                 case 'l':
148                         (void) wwwrite(selwin, "\033C", 2);
149                         break;
150                 case ctrl('e'):
151                         wwscroll(selwin, 1);
152                         break;
153                 case ctrl('y'):
154                         wwscroll(selwin, -1);
155                         break;
156                 case ctrl('d'):
157                         wwscroll(selwin, selwin->ww_w.nr / 2);
158                         break;
159                 case ctrl('u'):
160                         wwscroll(selwin, - selwin->ww_w.nr / 2);
161                         break;
162                 case ctrl('f'):
163                         wwscroll(selwin, selwin->ww_w.nr);
164                         break;
165                 case ctrl('b'):
166                         wwscroll(selwin, - selwin->ww_w.nr);
167                         break;
168                 case ctrl('s'):
169                         stopwin(selwin);
170                         break;
171                 case ctrl('q'):
172                         startwin(selwin);
173                         break;
174                 case ctrl('l'):
175                         wwredraw();
176                         break;
177                 case '?':
178                         c_help();
179                         break;
180                 case ctrl('['):
181                         if (checkproc(selwin) >= 0)
182                                 out = 1;
183                         break;
184                 case ctrl('z'):
185                         wwsuspend();
186                         break;
187                 case 'q':
188                         c_quit();
189                         break;
190                 /* debugging stuff */
191                 case '&':
192                         if (debug) {
193                                 c_debug();
194                                 break;
195                         }
196                 default:
197                         if (c == escapec) {
198                                 if (checkproc(selwin) >= 0) {
199                                         (void) write(selwin->ww_pty,
200                                                 &escapec, 1);
201                                         out = 1;
202                                 }
203                         } else {
204                                 if (!terse)
205                                         wwbell();
206                                 error("Type ? for help.");
207                         }
208                 }
209         }
210         if (!quit)
211                 setcmd(0);
212 }
213
214 struct ww *
215 getwin(void)
216 {
217         int c;
218         struct ww *w = NULL;
219
220         if (!terse)
221                 wwputs("Which window? ", cmdwin);
222         wwcurtowin(cmdwin);
223         while ((c = wwgetc()) < 0)
224                 wwiomux();
225         if (debug && c == 'c')
226                 w = cmdwin;
227         else if (debug && c == 'f')
228                 w = framewin;
229         else if (debug && c == 'b')
230                 w = boxwin;
231         else if (c >= '1' && c < NWINDOW + '1')
232                 w = window[c - '1'];
233         else if (c == '+')
234                 w = selwin;
235         else if (c == '-')
236                 w = lastselwin;
237         if (w == NULL)
238                 wwbell();
239         if (!terse)
240                 wwputc('\n', cmdwin);
241         return w;
242 }
243
244 int
245 checkproc(struct ww *w)
246 {
247         if (w->ww_state != WWS_HASPROC) {
248                 error("No process in window.");
249                 return -1;
250         }
251         return 0;
252 }
253
254 void
255 setcmd(char new)
256 {
257         if (new && !incmd) {
258                 if (!terse)
259                         wwadd(cmdwin, &wwhead);
260                 if (selwin != 0)
261                         wwcursor(selwin, 1);
262                 wwcurwin = 0;
263         } else if (!new && incmd) {
264                 if (!terse) {
265                         wwdelete(cmdwin);
266                         reframe();
267                 }
268                 if (selwin != 0)
269                         wwcursor(selwin, 0);
270                 wwcurwin = selwin;
271         }
272         incmd = new;
273 }
274
275 void
276 setterse(char new)
277 {
278         if (incmd) {
279                 if (new && !terse) {
280                         wwdelete(cmdwin);
281                         reframe();
282                 } else if (!new && terse)
283                         wwadd(cmdwin, &wwhead);
284         }
285         terse = new;
286 }
287
288 /*
289  * Set the current window.
290  */
291 void
292 setselwin(struct ww *w)
293 {
294         if (selwin == w)
295                 return;
296         if (selwin != 0)
297                 lastselwin = selwin;
298         if ((selwin = w) != NULL)
299                 front(selwin, 1);
300 }