BSD 4_1_snap development
[unix-history] / usr / doc / curses / intro.4
CommitLineData
f91e63b9
KA
1.sh 1 "Cursor Motion Optimization: Standing Alone"
2.pp
3It is possible to use the cursor optimization functions of this screen package
4without the overhead and additional size of the screen updating functions.
5The screen updating functions are designed for uses
6where parts of the screen are changed,
7but the overall image remains the same.
8This includes such programs as
9.b eye
10and
11.b vi \**.
12.(f
13\**
14.b Eye
15actually uses these functions,
16.b vi
17does not.
18.)f
19Certain other programs
20will find it difficult to use these functions in this manner
21without considerable unnecessary program overhead.
22For such applications,
23such as some
24.q "\fIcrt hacks\fR\|" \**
25.(f
26\**
27Graphics programs designed to run on character-oriented terminals.
28I could name many,
29but they come and go,
30so the list would be quickly out of date.
31Recently, there have been programs such as
32.b rocket
33and
34.b gun .
35.)f
36and optimizing
37.b cat (1)-type
38programs,
39all that is needed is the motion optimizations.
40This, therefore, is a description
41of what some of what goes on at the lower levels of this screen package.
42The descriptions assume a certain amount of familiarity
43with programming problems and some finer points of C.
44None of it is terribly difficult,
45but you should be forewarned.
46.sh 2 "Terminal Information"
47.pp
48In order to use a terminal's
49features to the best of a program's abilities,
50it must first know what they are\**.
51.(f
52\**
53If this comes as any surprise to you,
54there's this tower in Paris they're thinking of junking
55that I can let you have for a song.
56.)f
57The \*(tc \*(db describes these,
58but a certain amount of decoding is necessary,
59and there are, of course,
60both efficient and inefficient ways of reading them in.
61The algorithm that the uses is taken from
62.b vi
63and is hideously efficient.
64It reads them
65in a tight loop
66into a set of variables
67whose names are two uppercase letters with some mnemonic value.
68For example,
69.Vn HO
70is a string which moves the cursor to the "home" position\**.
71.(f
72\**
73These names are identical to those variables
74used in the
75.b /etc/termcap
76\*(db to describe each capability.
77See Appendix A for a complete list of those read,
78and
79.b termcap (5)
80for a full description.
81.)f
82As there are two types of variables involving ttys,
83there are two routines.
84The first,
85.Fn gettmode ,
86sets some variables based upon the tty modes accessed by
87.b gtty (2)
88and
89.b stty (2) .
90The second,
91.Fn setterm ,
92a larger task by reading in the descriptions from the \*(tc \*(db.
93This is the way these routines are used by
94.Fn initscr :
95.(b
96.(l I
97\*fif\fP (isatty(0)) {
98 gettmode();
99 \*fif\fP (sp=getenv("TERM"))
100 setterm(sp);
101}
102\*felse\fP
103 setterm(Def\*_term);
104\*_puts(TI);
105\*_puts(VS);
106.)l
107.)b
108.pp
109.Fn isatty
110checks to see if file descriptor 0 is a terminal\**.
111.(f
112\**
113.Fn isatty
114is defined in the default C library function routines.
115It does a
116.b gtty (2)
117on the descriptor and checks the return value.
118.)f
119If it is,
120.Fn gettmode
121sets the terminal description modes from a
122.b gtty (2) .
123.Fn getenv
124is then called to get the name of the terminal,
125and that value (if there is one) is passed to
126.Fn setterm ,
127which reads in the variables from \*(tc
128associated with that terminal.
129.Fn getenv "" (
130returns a pointer to a string containing the name of the terminal,
131which we save in the character pointer
132.Vn sp .)
133If
134.Fn isatty
135returns false,
136the default terminal
137.Vn Def\*_term
138is used.
139The
140.Vn TI
141and
142.Vn VS
143sequences initialize the terminal
144.Fn \*_puts "" (
145is a macro which uses
146.Fn tputs
147(see
148.b termcap (3))
149to put out a string).
150It is these things which
151.Fn endwin
152undoes.
153.sh 2 "Movement Optimizations, or, Getting Over Yonder"
154.pp
155Now that we have all this useful information,
156it would be nice to do something with it\**.
157.(f
158\**
159Actually,
160it
161.i can
162be emotionally fulfilling just to get the information.
163This is usually only true, however,
164if you have the social life of a kumquat.
165.)f
166The most difficult thing to do properly is motion optimization.
167When you consider how many different features various terminals have
168(tabs, backtabs, non-destructive space, home sequences, absolute tabs, .....)
169you can see that deciding how to get from here to there
170can be a decidedly non-trivial task.
171The editor
172.b vi
173uses many of these features,
174and the routines it uses to do this take up many pages of code.
175Fortunately, I was able to liberate them with the author's permission,
176and use them here.
177.pp
178After using
179.Fn gettmode
180and
181.Fn setterm
182to get the terminal descriptions,
183the function
184.Fn mvcur
185deals with this task.
186It usage is simple:
187you simply tell it where you are now and where you want to go.
188For example
189.(l
190mvcur(0\*,0\*,LINES/2\*,COLS/2)
191.)l
192.lp
193would move the cursor from the home position (0\*,0)
194to the middle of the screen.
195If you wish to force absolute addressing,
196you can use the function
197.Fn tgoto
198from the
199.b termlib (7)
200routines,
201or you can tell
202.Fn mvcur
203that you are impossibly far away,
204like Cleveland.
205For example,
206to absolutely address the lower left hand corner of the screen
207from anywhere
208just claim that you are in the upper right hand corner:
209.(l
210mvcur(0\*,COLS\-1\*,LINES\-1\*,0)
211.)l