aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/tedit/display/curses/getstr.x
blob: 6c1d38e6cca2cb009b97fbfea5f58e8aacb413c5 (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
include <ctype.h>
include "../curses.h"
include "window.h"

# GETSTR -- Get a string from the keyboard and display it in the window
#
# B.Simon	12-Dec-90	Original
# B.Simon	21-Mar-91	Add several new commands

procedure getstr (str, maxch)

char	str[ARB]	# o: String that was read from the keyboard
int	maxch		# i: Maximum string length
#--
int	ch

int	k_get()

begin
	str[1] = EOS
	call weditstr (STDSCR, str, maxch)

	ch = k_get ()	# discard pushed back character
end

procedure wgetstr (win, str, maxch)

pointer	win		# i: Window descriptor
char	str[ARB]	# o: String that was read from the keyboard
int	maxch		# i: Maximum string length
#--
int	ch

int	k_get()

begin
	str[1] = EOS
	call weditstr (win, str, maxch)

	ch = k_get ()	# discard pushed back character
end

# EDITSTR -- Edit a string while displaying it in the window

procedure editstr (str, maxch)

char	str[ARB]	# u: String to edit
int	maxch		# i: Maximum string length
#--

begin
	call weditstr (STDSCR, str, maxch)
end

procedure weditstr (win, str, maxch)

pointer	win		# i: Window descriptor
char	str[ARB]	# u: String to edit
int	maxch		# i: Maximum string length
#--
include "window.com"

pointer pwin

begin
	# NOTE: It is the reponsibility of the calling program
	# to make sure that the current window contents and the
	# string passed to this procedure are in agreement before
	# this procedure is called.

	pwin = warray[win]

	if (WIN_FUNC(pwin) == NULL) {
	    call editfn (win, str, maxch)
	} else {
	    call zcall3 (WIN_FUNC(pwin), win, str, maxch)
	}

end

# EDITFN -- Default function to process window input

procedure editfn (win, str, maxch)

int	win		# i: Window descriptor
char	str[ARB]	# u: String containing line
int	maxch		# i: Maximum line length
#--
int	row, col, ch, ic, jc, mc, nc
pointer	sp, buffer

int	strlen(), k_get(), winstat()

begin
	ic = 0
	nc = strlen (str)

	row = winstat (win, W_CURROW)
	col = winstat (win, W_CURCOL)

	call smark (sp)
	call salloc (buffer, SZ_LINE, TY_CHAR)
	Memc[buffer] = EOS

	while (nc < maxch) {

	    # Read character from keyboard

	    call ps_synch
	    ch = k_get ()

	    # Check for carriage return

	    if (ch == '\r')
		break

	    if (IS_PRINT(ch)) {
		ic = ic + 1
		nc = nc + 1

		if (ic == nc) {
		    str[ic] = ch
		    str[ic+1] = EOS
		    call waddstr (win, str[ic])

		} else {
		    call amovc (str[ic], str[ic+1], nc-ic+1)

		    str[ic] = ch
		    call winsch (win, str[ic])
		}

	    } else {
		switch (ch) {
		case K_UP:  # Move up one field
		    break

		case K_DOWN:  # Move down one field
		    break

		case K_RIGHT:  # Move right one column
		    if (ic < nc) {
			ic = ic + 1
			call wmove (win, row, col+ic)
		    }

		case K_LEFT:  # Move left one column
		    if (ic > 0) {
			ic = ic - 1
			call wmove (win, row, col+ic)
		    }

		case K_NEXTW:  # Move forwards one word
		    call mvword_next (str, ic, jc)

		    if (jc > ic) {
			ic = jc
			call wmove (win, row, col+ic)
		    }

		case K_PREVW:  # Move backwards one word
		    call mvword_prev (str, ic, jc)

		    if (jc < ic) {
			ic = jc
			call wmove (win, row, col+ic)
		    }

		case K_NEXTP:  # Move forwards one screen
		    break

		case K_PREVP:  # Move backwards one screen
		    break

		case K_HOME:  # Move to first field
		    break

		case K_END:  # Move to last field
		    break

		case K_BOL:  # Move to first column in line
		    if (ic > 0) {
			ic = 0
			call wmove (win, row, col)
		    }

		case K_EOL:  # Move to last column in line
		    if (ic < nc) {
			ic = nc
			call wmove (win, row, col+ic)
		    }

		case K_DEL:  # Delete character underneath cursor
		    if (ic < nc) {
			mc = strlen (Memc[buffer])

			Memc[buffer+mc] = str[ic+1]
			Memc[buffer+mc+1] = EOS

			call amovc (str[ic+2], str[ic+1], nc-ic)

			call wdelch (win)
			nc = nc - 1
		    }

		case K_BS:  # Delete character to left of cursor
		    if (ic > 0) {
			mc = strlen (Memc[buffer])

			call amovc (Memc[buffer], Memc[buffer+1], mc+1)
			Memc[buffer] = str[ic]

			ic = ic - 1
			call amovc (str[ic+2], str[ic+1], nc-ic)

			call wmove (win, row, col+ic)
			call wdelch (win)
			nc = nc - 1
		    }

		case K_DWORD:  # Delete next word
		    call mvword_next (str, ic, jc)

		    if (jc > ic) {
			mc = strlen (Memc[buffer])
			call strcpy (str[ic+1], Memc[buffer+mc], jc-ic)
			call amovc (str[jc+1], str[ic+1], nc-jc+1)

			call wclrtoeol (win)
			call waddstr (win, str[ic+1])
			call wmove (win, row, col+ic)
			nc = nc - (jc - ic)
		    }

		case K_DLINE:  # Delete entire line
		    if (nc > 0) {
			call strcpy (str[ic+1], Memc[buffer], nc-ic)
			str[ic+1] = EOS

			call wclrtoeol (win)
			nc = ic
		    }

		case K_UNDCHR: # Undelete a character
		    mc = strlen (Memc[buffer])
		    if (mc > 0) {
			call amovc (str[ic+1], str[ic+2], nc-ic+1)
			str[ic+1] = Memc[buffer+mc-1]

			Memc[buffer+mc-1] = EOS
			call winsch (win, str[ic+1])

			ic = ic + 1
			nc = nc + 1
		    }

		case K_UNDWRD: # Undelete a word
		    mc = strlen (Memc[buffer])
		    call mvword_prev (Memc[buffer], mc, jc)

		    mc = mc - jc
		    if (mc > 0) {
			call amovc (str[ic+1], str[ic+mc+1], nc-ic+1)
			call amovc (Memc[buffer+jc], str[ic+1], mc)

			Memc[buffer+jc] = EOS
			call wclrtoeol (win)
			call waddstr (win, str[ic+1])

			ic = ic + mc
			nc = nc + mc
			call wmove (win, row, col+ic)
		    }

		case K_UNDLIN: # Undelete a line
		    mc = strlen (Memc[buffer])
		    if (mc > 0) {
			call amovc (str[ic+1], str[ic+mc+1], nc-ic+1)
			call amovc (Memc[buffer], str[ic+1], mc)

			Memc[buffer] = EOS
			call wclrtoeol (win)
			call waddstr (win, str[ic+1])

			ic = ic + mc
			nc = nc + mc
			call wmove (win, row, col+ic)
		    }

		case K_HELP:  # Display help screen
		    break

		case K_PAINT:  # Redraw the screen
		    call clearok (STDSCR, true)
		    call wrefresh (STDSCR)
		    call wmove (win, row, col+ic)

		case K_EXIT:  # Exit procedure
		    break

		default: # Any other character
		    break
		}
	    }
	}

	# Terminate string with EOS and push back character 
	# that terminated input

	if (nc >= maxch)
	    ch = EOS

	str[nc+1] = EOS
	call k_pushbk (ch)

	call sfree (sp)
end