xref: /original-bsd/usr.bin/window/cmd1.c (revision df6dbad5)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)cmd1.c	3.32 (Berkeley) 02/21/88";
15 #endif /* not lint */
16 
17 #include "defs.h"
18 #include "char.h"
19 
20 c_window()
21 {
22 	int col, row, xcol, xrow;
23 	int id;
24 
25 	if ((id = findid()) < 0)
26 		return;
27 	if (!terse)
28 		wwputs("New window (upper left corner): ", cmdwin);
29 	col = 0;
30 	row = 1;
31 	wwadd(boxwin, framewin->ww_back);
32 	for (;;) {
33 		wwbox(boxwin, row - 1, col - 1, 3, 3);
34 		wwsetcursor(row, col);
35 		while (wwpeekc() < 0)
36 			wwiomux();
37 		switch (getpos(&row, &col, row > 1, 0,
38 			wwnrow - 1, wwncol - 1)) {
39 		case 3:
40 			wwunbox(boxwin);
41 			wwdelete(boxwin);
42 			return;
43 		case 2:
44 			wwunbox(boxwin);
45 			break;
46 		case 1:
47 			wwunbox(boxwin);
48 		case 0:
49 			continue;
50 		}
51 		break;
52 	}
53 	if (!terse)
54 		wwputs("\nNew window (lower right corner): ", cmdwin);
55 	xcol = col;
56 	xrow = row;
57 	for (;;) {
58 		wwbox(boxwin, row - 1, col - 1,
59 			xrow - row + 3, xcol - col + 3);
60 		wwsetcursor(xrow, xcol);
61 		wwflush();
62 		while (wwpeekc() < 0)
63 			wwiomux();
64 		switch (getpos(&xrow, &xcol, row, col, wwnrow - 1, wwncol - 1))
65 		{
66 		case 3:
67 			wwunbox(boxwin);
68 			wwdelete(boxwin);
69 			return;
70 		case 2:
71 			wwunbox(boxwin);
72 			break;
73 		case 1:
74 			wwunbox(boxwin);
75 		case 0:
76 			continue;
77 		}
78 		break;
79 	}
80 	wwdelete(boxwin);
81 	if (!terse)
82 		wwputc('\n', cmdwin);
83 	wwcurtowin(cmdwin);
84 	(void) openwin(id, row, col, xrow-row+1, xcol-col+1, nbufline,
85 		(char *) 0, 1, 1, shellfile, shell);
86 }
87 
88 getpos(row, col, minrow, mincol, maxrow, maxcol)
89 register int *row, *col;
90 int minrow, mincol;
91 int maxrow, maxcol;
92 {
93 	static int scount;
94 	int count;
95 	char c;
96 	int oldrow = *row, oldcol = *col;
97 
98 	while ((c = wwgetc()) >= 0) {
99 		switch (c) {
100 		case '0': case '1': case '2': case '3': case '4':
101 		case '5': case '6': case '7': case '8': case '9':
102 			scount = scount * 10 + c - '0';
103 			continue;
104 		}
105 		count = scount ? scount : 1;
106 		scount = 0;
107 		switch (c) {
108 		case 'h':
109 			if ((*col -= count) < mincol)
110 				*col = mincol;
111 			break;
112 		case 'H':
113 			*col = mincol;
114 			break;
115 		case 'l':
116 			if ((*col += count) > maxcol)
117 				*col = maxcol;
118 			break;
119 		case 'L':
120 			*col = maxcol;
121 			break;
122 		case 'j':
123 			if ((*row += count) > maxrow)
124 				*row = maxrow;
125 			break;
126 		case 'J':
127 			*row = maxrow;
128 			break;
129 		case 'k':
130 			if ((*row -= count) < minrow)
131 				*row = minrow;
132 			break;
133 		case 'K':
134 			*row = minrow;
135 			break;
136 		case ctrl('['):
137 			if (!terse)
138 				wwputs("\nCancelled.  ", cmdwin);
139 			return 3;
140 		case '\r':
141 			return 2;
142 		default:
143 			if (!terse)
144 				wwputs("\nType [hjklHJKL] to move, return to enter position, escape to cancel.", cmdwin);
145 			wwbell();
146 		}
147 	}
148 	return oldrow != *row || oldcol != *col;
149 }
150