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

define	TABSTOP		8

# ADDSTR -- Add a string to a window
#
# B.Simon	02-Oct-90	Original

procedure addstr (str)

char	str[ARB]	# i: String to add to window
#--

begin
	call waddstr (STDSCR, str)
end

procedure waddstr (win, str)

int	win		# i: Window descriptor
char	str[ARB]	# i: String to add to window
#--
include "window.com"

bool	moved
int	row, col, nrows, ncols, ic, jc, mxchar, itab
pointer	sp, line, pwin

begin
	pwin = warray[win]

	# Don't print anything if echoing is turned off

	if (echoed == NO)
	    return

	# Allocate an array to hold the string to print

	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Get the current cursor position and window size

	row = WIN_CURROW(pwin)
	col = WIN_CURCOL(pwin)
	ncols = WIN_WIDTH(pwin)
	nrows = WIN_HEIGHT(pwin)

	ic = 0
	jc = 0
	moved = false

	repeat {
	    ic = ic + 1

	    # If the character is printable, just copy it to the array
	    # The array will be printed when the EOS is read

	    if (IS_PRINT(str[ic])) {
		col = col + 1
		Memc[line+jc] = str[ic]
		jc = jc + 1

	    } else {
		# Print any characters in the array before handling
		# the non-printing character

		Memc[line+jc] = EOS
		if (jc > 0) {
		    jc = 0

		    mxchar = ncols - WIN_CURCOL(pwin) + 1
		    call ps_write (WIN_TOP(pwin)+WIN_CURROW(pwin)-1, 
				   WIN_LEFT(pwin)+WIN_CURCOL(pwin)-1, 
				   mxchar, Memc[line], WIN_ATRIB(pwin))

		    WIN_CURCOL(pwin) = col
		    WIN_CURROW(pwin) = row

		    # Check for writing past the edge of the window
		    # and scrolling

		    if (col > ncols) {
			col = 1
			row = row + 1
			moved = true

			if (row > nrows) {
			    row = nrows
			    if (WIN_SCROLL(pwin) == YES) 
				call wslide (WIN_RECT(pwin), DIR_UP, 1)
			}
		    }
		}

		# Some non-printing characters require special action
		# Others are mapped to printing characters and written
		# to the array

		switch (str[ic]) {
		case EOS:
		    ;

		case '\n':
		    call wclrtoeol (win)
		    col = 1
		    row = row + 1
		    moved = true

		    # Scroll window if we've hit the last row
		   
		    if (row > nrows) {
			row = nrows
			if (WIN_SCROLL(pwin) == YES)
			    call wslide (WIN_RECT(pwin), DIR_UP, 1)
		    }

		case '\t':
		    do itab = 1, TABSTOP - mod (col, TABSTOP) {
			Memc[line+jc] = ' '
			jc = jc + 1
			col = col + 1
		    }

		case '\r':
		    if (col > 1) {
			col = 1
			moved = true
		    }

		case '\010':
		    if (col > 1) {
			col = col - 1
			moved = true
		    }

		default:
		    Memc[line+jc] = ' '
		    col = col + 1
		    jc = jc + 1
		}
	    }

	    # Move cursor, as required

	    if (moved) {
		moved = false
		call wmove (win, row, col)
	    }

	} until (str[ic] == EOS)

	call sfree (sp)

end