aboutsummaryrefslogtreecommitdiff
path: root/contrib/nvi/tk/tk_funcs.c
blob: be2b0d96ea09fd174527be2b29792748b8a639b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)tk_funcs.c	8.11 (Berkeley) 9/23/96";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "../common/common.h"
#include "../vi/vi.h"
#include "tki.h"

/*
 * tk_addstr --
 *	Add len bytes from the string at the cursor, advancing the cursor.
 *
 * PUBLIC: int tk_addstr __P((SCR *, const char *, size_t));
 */
int
tk_addstr(sp, str, len)
	SCR *sp;
	const char *str;
	size_t len;
{
	TK_PRIVATE *tkp;
	int iv;
	char buf[20];

	iv = 0;

	tkp = TKP(sp);
	if (iv)
		(void)Tcl_Eval(tkp->interp, "tk_standout");

	(void)snprintf(buf, sizeof(buf), "%d ", (int)len);
	if ((Tcl_VarEval(tkp->interp,
	    "tk_addstr ", buf, "{", str, "}", NULL) != TCL_OK))
		return (1);

	if (iv)
		(void)Tcl_Eval(tkp->interp, "tk_standend");
	return (0);
}

/*
 * tk_attr --
 *	Toggle a screen attribute on/off.
 *
 * PUBLIC: int tk_attr __P((SCR *, scr_attr_t, int));
 */
int
tk_attr(sp, attribute, on)
	SCR *sp;
	scr_attr_t attribute;
	int on;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	switch (attribute) {
	case SA_ALTERNATE:			/* No alternate screen. */
		break;
	case SA_INVERSE:
		if (on)
			(void)Tcl_Eval(tkp->interp, "tk_standout");
		else
			(void)Tcl_Eval(tkp->interp, "tk_standend");
		break;
	default:
		abort();
	}
	return (0);
}

/*
 * tk_baud --
 *	Return the baud rate.
 *
 * PUBLIC: int tk_baud __P((SCR *, u_long *));
 */
int
tk_baud(sp, ratep)
	SCR *sp;
	u_long *ratep;
{
	*ratep = 9600;
	return (0);
}

/*
 * tk_bell --
 *	Ring the bell/flash the screen.
 *
 * PUBLIC: int tk_bell __P((SCR *));
 */
int
tk_bell(sp)
	SCR *sp;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	return (Tcl_Eval(tkp->interp, "tk_flash") != TCL_OK);
}

/*
 * tk_clrtoeol --
 *	Clear from the current cursor to the end of the line.
 *
 * PUBLIC: int tk_clrtoeol __P((SCR *));
 */
int
tk_clrtoeol(sp)
	SCR *sp;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	return (Tcl_Eval(tkp->interp, "tk_clrtoeol") != TCL_OK);
}

/*
 * tk_cursor --
 *	Return the current cursor position.
 *
 * PUBLIC: int tk_cursor __P((SCR *, size_t *, size_t *));
 */
int
tk_cursor(sp, yp, xp)
	SCR *sp;
	size_t *yp, *xp;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	*yp = (tkp->tk_cursor_row - 1) - sp->woff;
	*xp = tkp->tk_cursor_col;
	return (0);
}

/*
 * tk_deleteln --
 *	Delete the current line, scrolling all lines below it.
 *
 * PUBLIC: int tk_deleteln __P((SCR *));
 */
int
tk_deleteln(sp)
	SCR *sp;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	return (Tcl_Eval(tkp->interp, "tk_deleteln") != TCL_OK);
}

/* 
 * tk_ex_adjust --
 *	Adjust the screen for ex.
 *
 * PUBLIC: int tk_ex_adjust __P((SCR *, exadj_t));
 */
int
tk_ex_adjust(sp, action)
	SCR *sp;
	exadj_t action;
{
	abort();
	/* NOTREACHED */
}

/*
 * tk_insertln --
 *	Push down the current line, discarding the bottom line.
 *
 * PUBLIC: int tk_insertln __P((SCR *));
 */
int
tk_insertln(sp)
	SCR *sp;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	return (Tcl_Eval(tkp->interp, "tk_insertln") != TCL_OK);
}

/*
 * tk_keyval --
 *	Return the value for a special key.
 *
 * PUBLIC: int tk_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
 */
int
tk_keyval(sp, val, chp, dnep)
	SCR *sp;
	scr_keyval_t val;
	CHAR_T *chp;
	int *dnep;
{
	TK_PRIVATE *tkp;

	/*
	 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
	 * VWERASE is a 4BSD extension.
	 */
	tkp = TKP(sp);
	switch (val) {
	case KEY_VEOF:
		*dnep = (*chp = tkp->orig.c_cc[VEOF]) == _POSIX_VDISABLE;
		break;
	case KEY_VERASE:
		*dnep = (*chp = tkp->orig.c_cc[VERASE]) == _POSIX_VDISABLE;
		break;
	case KEY_VKILL:
		*dnep = (*chp = tkp->orig.c_cc[VKILL]) == _POSIX_VDISABLE;
		break;
#ifdef VWERASE
	case KEY_VWERASE:
		*dnep = (*chp = tkp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE;
		break;
#endif
	default:
		*dnep = 1;
		break;
	}
	return (0);
}

/*
 * tk_move --
 *	Move the cursor.
 *
 * PUBLIC: int tk_move __P((SCR *, size_t, size_t));
 */
int
tk_move(sp, lno, cno)
	SCR *sp;
	size_t lno, cno;
{
	TK_PRIVATE *tkp;
	char buf[40];

	(void)snprintf(buf, sizeof(buf), "%d %d", RLNO(sp, lno), cno);

	tkp = TKP(sp);
	return (Tcl_VarEval(tkp->interp, "tk_move ", buf, NULL) != TCL_OK);
}

/*
 * tk_refresh --
 *	Refresh the screen.
 *
 * PUBLIC: int tk_refresh __P((SCR *, int));
 */
int
tk_refresh(sp, repaint)
	SCR *sp;
	int repaint;
{
	TK_PRIVATE *tkp;

	/*
	 * If repaint is set, the editor is telling us that we don't know
	 * what's on the screen, so we have to repaint from scratch.
	 *
	 * XXX
	 * I have no idea how to do this in Tk.  My guess is that we have
	 * to delete all of the text and call the editor with an E_REPAINT
	 * event.
	 */
	if (repaint) {
	}

	tkp = TKP(sp);
	return (Tcl_Eval(tkp->interp, "update idletasks") != TCL_OK);
}

/*
 * tk_rename --
 *	Rename the file.
 *
 * PUBLIC: int tk_rename __P((SCR *));
 */
int
tk_rename(sp)
	SCR *sp;
{
	TK_PRIVATE *tkp;

	tkp = TKP(sp);
	return (Tcl_VarEval(tkp->interp,
	    "tk_rename ", sp->frp->name, NULL) != TCL_OK);
}

/*
 * tk_suspend --
 *	Suspend a screen.
 *
 * PUBLIC: int tk_suspend __P((SCR *, int *));
 */
int
tk_suspend(sp, allowedp)
	SCR *sp;
	int *allowedp;
{
	*allowedp = 0;
	return (0);
}

/*
 * tk_usage --
 *	Print out the Tk/Tcl usage messages.
 * 
 * PUBLIC: void tk_usage __P((void));
 */
void
tk_usage()
{
#define	USAGE "\
usage: tkvi [-eFlRrSv] [-c command] [-bg color] [-fg color]\n\
	    [-geometry widthxheight+x+y] [-i script] [-t tag] [-w size]\n\
	    [file ...]\n"
	(void)fprintf(stderr, "%s", USAGE);
#undef	USAGE
}